@@ -137,7 +137,7 @@ function str_ends_with( $haystack, $needle ) {
137137 * @type string $block_name Required if data is in a block.
138138 * @type string|array $block_attribute Required if data is in a block.
139139 * }
140- * @type string $type Type of data, e.g., 'image ', 'post', or 'term'. If set, default callbacks can be used.
140+ * @type string $type Type of data, e.g., 'media ', 'post', or 'term'. If set, default callbacks can be used.
141141 * Cannot be combined with custom callbacks. Adding custom callbacks will override the default behavior.
142142 * @type callable $pre_distribute_cb Function that returns extra data that needs to be added
143143 * to the request (source processing).
@@ -193,9 +193,9 @@ function distributor_register_data( $data_name, $args ) {
193193 // Set default callbacks based on type.
194194 if ( ! empty ( $ args ['type ' ] ) ) {
195195 switch ( $ args ['type ' ] ) {
196- case 'image ' :
197- $ args ['pre_distribute_cb ' ] = 'distributor_image_pre_distribute_callback ' ;
198- $ args ['post_distribute_cb ' ] = 'distributor_image_post_distribute_callback ' ;
196+ case 'media ' :
197+ $ args ['pre_distribute_cb ' ] = 'distributor_media_pre_distribute_callback ' ;
198+ $ args ['post_distribute_cb ' ] = 'distributor_media_post_distribute_callback ' ;
199199 break ;
200200 case 'post ' :
201201 $ args ['pre_distribute_cb ' ] = 'distributor_post_pre_distribute_callback ' ;
@@ -231,29 +231,81 @@ function distributor_get_registered_data() {
231231}
232232
233233/**
234- * Pre-distribute callback for image data.
235- * This is the default pre-distribute callback for image data, used when the type is set to 'image ' in distributor_register_data().
234+ * Pre-distribute callback for media data.
235+ * This is the default pre-distribute callback for media data, used when the type is set to 'media ' in distributor_register_data().
236236 *
237- * @param mixed $image The image ID to be processed before distribution.
238- * @return array The extra data of the image to be distributed to the target site.
237+ * @param mixed $media_id The media ID to be processed before distribution.
238+ * @return array The extra data of the media to be distributed to the target site.
239239 */
240- function distributor_image_pre_distribute_callback ( $ image_id ) {
241- // TODO: Implement pre processing.
242- return array ();
240+ function distributor_media_pre_distribute_callback ( $ media_id ) {
241+ if ( ! $ media_id ) {
242+ return array ();
243+ }
244+
245+ // Get the media data.
246+ $ media = get_post ( $ media_id );
247+ if ( ! $ media ) {
248+ return array ();
249+ }
250+
251+ return array (
252+ 'title ' => $ media ->post_title ,
253+ 'caption ' => $ media ->post_excerpt ,
254+ 'description ' => $ media ->post_content ,
255+ 'alt ' => get_post_meta ( $ media ->ID , '_wp_attachment_image_alt ' , true ),
256+ 'url ' => wp_get_attachment_url ( $ media ->ID ),
257+ );
243258}
244259
245260/**
246- * Post-distribute callback for image data.
247- * This is the default post-distribute callback for image data, used when the type is set to 'image ' in distributor_register_data().
261+ * Post-distribute callback for media data.
262+ * This is the default post-distribute callback for media data, used when the type is set to 'media ' in distributor_register_data().
248263 *
249- * @param mixed $image_extra_data The extra data to be processed after distribution.
250- * @param mixed $source_image_id The source image ID.
264+ * @param mixed $media_extra_data The extra data to be processed after distribution.
265+ * @param mixed $source_media_id The source media ID.
251266 * @param mixed $post_data The post data.
252- * @return int The ID of the distributed image .
267+ * @return int The ID of the distributed media .
253268 */
254- function distributor_image_post_distribute_callback ( $ image_extra_data , $ source_image_id , $ post_data ) {
255- // Do something with the data.
256- return $ source_image_id ;
269+ function distributor_media_post_distribute_callback ( $ media_extra_data , $ source_media_id , $ post_data ) {
270+ if ( ! isset ( $ media_extra_data ['url ' ] ) ) {
271+ return $ source_media_id ;
272+ }
273+
274+ $ source_media_url = $ media_extra_data ['url ' ];
275+
276+ // Check if the media already exists on the target site. If yes, return its ID.
277+ $ media_id = Distributor \Utils \get_attachment_id_by_original_data ( $ source_media_id , $ source_media_url );
278+
279+ // If media exists, return the media ID.
280+ if ( ! empty ( $ media_id ) ) {
281+ return $ media_id ;
282+ }
283+
284+ $ media_id = Distributor \Utils \process_media ( $ source_media_url , 0 , [] );
285+ // if the media not processed, return the source media ID.
286+ if ( empty ( $ media_id ) ) {
287+ return $ source_media_id ;
288+ }
289+
290+ // Update the media data.
291+ wp_update_post (
292+ array (
293+ 'ID ' => $ media_id ,
294+ 'post_title ' => sanitize_text_field ( $ media_extra_data ['title ' ] ),
295+ 'post_excerpt ' => sanitize_textarea_field ( $ media_extra_data ['caption ' ] ),
296+ 'post_content ' => sanitize_textarea_field ( $ media_extra_data ['description ' ] ),
297+ )
298+ );
299+
300+ // Update the media meta.
301+ if ( ! empty ( $ media_extra_data ['alt ' ] ) ) {
302+ update_post_meta ( $ media_id , '_wp_attachment_image_alt ' , sanitize_textarea_field ( $ media_extra_data ['alt ' ] ) );
303+ }
304+ update_post_meta ( $ media_id , 'dt_original_media_id ' , wp_slash ( $ source_media_id ) );
305+ update_post_meta ( $ media_id , 'dt_original_media_url ' , wp_slash ( $ source_media_url ) );
306+
307+ // Return the media ID to replace the source reference.
308+ return $ media_id ;
257309}
258310
259311/**
0 commit comments