Advertisement
nshelper

Untitled

Jun 11th, 2025
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.     add_filter ( 'woogc/ps/synchronize_product/child_product', '_woogc_synchronize_product_data', 10, 3 );
  5.     /**
  6.     * Ensure the synchronyzed image files can be found on local shop
  7.     *
  8.     * @param mixed $child_product
  9.     * @param mixed $main_product_data
  10.     * @param mixed $origin_product_blog_ID
  11.     */
  12.     function _woogc_synchronize_product_data( $child_product, $main_product_data, $origin_product_blog_ID )
  13.         {
  14.            
  15.             $rtwpvg_images  =   $child_product->get_meta( 'rtwpvg_images' );
  16.            
  17.             if ( empty ( $rtwpvg_images )   ||  ! is_array ( $rtwpvg_images )   ||  count ( $rtwpvg_images )    <   1  )
  18.                 return $child_product;
  19.                
  20.             $gallery    =   array();
  21.            
  22.             foreach ( $rtwpvg_images    as  $origin_image_id )
  23.                 {
  24.                     $image_id   =   _woogc_synchronize_image( $origin_image_id, $origin_product_blog_ID );
  25.                     if ( $image_id  >   0   )
  26.                         $gallery[]  =   $image_id;
  27.                 }
  28.                
  29.             if ( count ( $gallery ) >   0   )
  30.                 {
  31.                     $child_product->update_meta_data( 'rtwpvg_images', $gallery );
  32.                 }
  33.                
  34.             return $child_product;  
  35.         }
  36.        
  37.        
  38.    
  39.     /**
  40.     * Synchronize image to child product
  41.     *
  42.     * @param mixed $prop_value
  43.     * @param mixed $origin_product_blog_ID
  44.     * @param mixed $synchronized_product_ID
  45.     */
  46.     function _woogc_synchronize_image( $prop_value, $origin_product_blog_ID )
  47.         {
  48.             //Check if the current image ID has been previously synchronized tothe  current shop
  49.             global $wpdb;
  50.            
  51.             $mysql_query        =   $wpdb->prepare( "SELECT meta_value FROM ". $wpdb->postmeta . " WHERE meta_key = '_woogc_image_sync_%d_%d'", $origin_product_blog_ID, $prop_value );
  52.             $local_image_id     =   $wpdb->get_var( $mysql_query );
  53.        
  54.             if ( ! empty ( $local_image_id ) )
  55.                 {                            
  56.                     //check if the image still exists
  57.                     $image_data =   get_post( $local_image_id );
  58.                    
  59.                     if ( is_object ( $image_data ) &&   $image_data->post_type  ==  'attachment' )
  60.                         return $local_image_id;
  61.                 }
  62.            
  63.             switch_to_blog( $origin_product_blog_ID );
  64.            
  65.             $image_data =   wp_get_attachment_image_src( $prop_value, 'full' );
  66.            
  67.             restore_current_blog();
  68.                
  69.             $local_image_id =   media_sideload_image ( $image_data[0], 0, null, 'id' );
  70.            
  71.             if ( is_int ( $local_image_id )  &&  $local_image_id > 0 )
  72.                 {
  73.                     update_post_meta( $local_image_id, '_woogc_image_sync_' . $origin_product_blog_ID . '_' . $prop_value,  $local_image_id );
  74.                    
  75.                     return $local_image_id;    
  76.                 }
  77.            
  78.             return FALSE;
  79.        
  80.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement