class-categories.php
1 year ago
class-import-image.php
1 year ago
class-import-items.php
1 year ago
class-import-price-list.php
1 year ago
class-multi-currency.php
1 year ago
class-order-sync.php
11 months ago
class-product.php
1 year ago
class-users-contact.php
1 year ago
index.php
1 year ago
class-import-image.php
122 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CommerceBird |
| 4 | * |
| 5 | * @package CommerceBird |
| 6 | */ |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Class for All Product Data from Woo To Zoho |
| 13 | * |
| 14 | * @package WooZo Inventory |
| 15 | */ |
| 16 | |
| 17 | class CMBIRD_Image_ZI { |
| 18 | |
| 19 | private $config; |
| 20 | public function __construct() { |
| 21 | $this->config = array( |
| 22 | 'ProductZI' => array( |
| 23 | 'OID' => get_option( 'cmbird_zoho_inventory_oid' ), |
| 24 | 'APIURL' => get_option( 'cmbird_zoho_inventory_url' ), |
| 25 | ), |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Attach image from zoho |
| 31 | * |
| 32 | * @param [string] $item_id - Item id for image details. |
| 33 | * @param [string] $item_name - Item name. |
| 34 | * @param [string] $post_id - Post id of product. |
| 35 | * @param [string] $image_name - Image name. |
| 36 | * @return integer | void |
| 37 | */ |
| 38 | public function cmbird_zi_get_image( $item_id, $item_name, $post_id, $image_name ) { |
| 39 | // $fd = fopen( __DIR__ . '/image_sync.txt', 'a+' ); |
| 40 | |
| 41 | $attachment_id = $this->compare_image_with_media_library( $item_name, $image_name ); |
| 42 | if ( $attachment_id ) { |
| 43 | set_post_thumbnail( $post_id, $attachment_id ); |
| 44 | update_post_meta( $attachment_id, '_wp_attachment_image_alt', $item_name ); |
| 45 | update_post_meta( $post_id, '_thumbnail_id', $attachment_id ); |
| 46 | wp_update_image_subsizes( $attachment_id ); |
| 47 | // also delete the zoho_image folder files. |
| 48 | $upload = wp_upload_dir(); |
| 49 | $folder_path = $upload['basedir'] . '/zoho_image/'; |
| 50 | $file_paths = glob( $folder_path . '/*' ); |
| 51 | foreach ( $file_paths as $file_path ) { |
| 52 | if ( is_file( $file_path ) ) { |
| 53 | wp_delete_file( $file_path ); |
| 54 | } |
| 55 | } |
| 56 | return $attachment_id; |
| 57 | } |
| 58 | |
| 59 | $zoho_inventory_oid = $this->config['ProductZI']['OID']; |
| 60 | $zoho_inventory_url = $this->config['ProductZI']['APIURL']; |
| 61 | $url = $zoho_inventory_url . 'inventory/v1/items/' . $item_id . '/image'; |
| 62 | $url .= '?organization_id=' . $zoho_inventory_oid; |
| 63 | |
| 64 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 65 | $image_url = $execute_curl_call_handle->execute_curl_call_image_get( $url, $image_name ); |
| 66 | if ( is_wp_error( $image_url ) ) { |
| 67 | // log the error message and the item name. |
| 68 | $error_message = $image_url->get_error_message(); |
| 69 | error_log( "Error fetching image for item $item_name: $error_message" ); |
| 70 | return; |
| 71 | } |
| 72 | $attachment_id = media_sideload_image( $image_url, $post_id, $image_name, 'id' ); |
| 73 | if ( $attachment_id ) { |
| 74 | set_post_thumbnail( $post_id, $attachment_id ); |
| 75 | update_post_meta( $attachment_id, '_wp_attachment_image_alt', $item_name ); |
| 76 | update_post_meta( $post_id, '_thumbnail_id', $attachment_id ); |
| 77 | wp_update_image_subsizes( $attachment_id ); |
| 78 | // also delete the zoho_image folder files. |
| 79 | $upload = wp_upload_dir(); |
| 80 | $folder_path = $upload['basedir'] . '/zoho_image/'; |
| 81 | $file_paths = glob( $folder_path . '/*' ); |
| 82 | foreach ( $file_paths as $file_path ) { |
| 83 | if ( is_file( $file_path ) ) { |
| 84 | wp_delete_file( $file_path ); |
| 85 | } |
| 86 | } |
| 87 | return $attachment_id; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Compare the image with existing media library images based on titles. |
| 93 | * |
| 94 | * @param string $item_image The name of the image. |
| 95 | * @return int|bool The ID of the existing image if a match is found, or false if no match is found. |
| 96 | * @since 1.0.0 |
| 97 | */ |
| 98 | protected function compare_image_with_media_library( $item_name, $item_image ) { |
| 99 | |
| 100 | if ( ! empty( $item_image ) ) { |
| 101 | $args = array( |
| 102 | 'post_type' => 'attachment', |
| 103 | 'post_mime_type' => 'image', |
| 104 | 'posts_per_page' => -1, |
| 105 | ); |
| 106 | $media_library_images = get_posts( $args ); |
| 107 | foreach ( $media_library_images as $media_image ) { |
| 108 | // Get the postmeta zoho_product_image_id of the existing image |
| 109 | $existing_image_title = get_the_title( $media_image->ID ); |
| 110 | if ( str_contains( $existing_image_title, $item_name ) !== false ) { |
| 111 | return $media_image->ID; // Return the ID of the existing image |
| 112 | } |
| 113 | if ( str_contains( $existing_image_title, $item_image ) !== false ) { |
| 114 | return $media_image->ID; // Return the ID of the existing image |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | // If no match is found in the loop, return false |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 |