class-categories.php
1 year ago
class-import-image.php
11 months ago
class-import-items.php
11 months 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
139 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( $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 | // log image url |
| 67 | // fwrite( $fd, "Fetched image URL: $image_url.\n" ); |
| 68 | if ( is_wp_error( $image_url ) || empty( $image_url ) ) { |
| 69 | // log the error message and the item name. |
| 70 | // $error_message = $image_url->get_error_message(); |
| 71 | // fwrite( $fd, "Error fetching image for item $item_name: $error_message\n" ); |
| 72 | return; |
| 73 | } |
| 74 | $upload_dir = wp_upload_dir(); |
| 75 | $local_path = $upload_dir['basedir'] . '/zoho_image/' . basename( $image_url ); |
| 76 | if ( file_exists( $local_path ) ) { |
| 77 | $filetype = wp_check_filetype( basename( $local_path ), null ); |
| 78 | |
| 79 | $attachment = array( |
| 80 | 'post_mime_type' => $filetype['type'], |
| 81 | 'post_title' => sanitize_file_name( $image_name ), |
| 82 | 'post_content' => '', |
| 83 | 'post_status' => 'inherit', |
| 84 | ); |
| 85 | |
| 86 | $attach_id = wp_insert_attachment( $attachment, $local_path, $post_id ); |
| 87 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 88 | $attach_data = wp_generate_attachment_metadata( $attach_id, $local_path ); |
| 89 | wp_update_attachment_metadata( $attach_id, $attach_data ); |
| 90 | |
| 91 | set_post_thumbnail( $post_id, $attach_id ); |
| 92 | update_post_meta( $attach_id, '_wp_attachment_image_alt', $item_name ); |
| 93 | update_post_meta( $post_id, '_thumbnail_id', $attach_id ); |
| 94 | wp_update_image_subsizes( $attach_id ); |
| 95 | |
| 96 | return $attach_id; |
| 97 | } else { |
| 98 | // fwrite( $fd, "File not found locally: $local_path\n" ); |
| 99 | return; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Compare the image with existing media library images based on titles. |
| 105 | * |
| 106 | * @param string $item_image The name of the image. |
| 107 | * @return int|bool The ID of the existing image if a match is found, or false if no match is found. |
| 108 | * @since 1.0.0 |
| 109 | */ |
| 110 | protected function compare_image_with_media_library( $item_image ) { |
| 111 | // $fd = fopen( __DIR__ . '/image_sync.txt', 'a+' ); |
| 112 | if ( empty( $item_image ) ) { |
| 113 | return false; |
| 114 | } |
| 115 | // log the image name |
| 116 | // fwrite( $fd, "Comparing image: $item_image with media library images.\n" ); |
| 117 | |
| 118 | $base_name = pathinfo( $item_image, PATHINFO_FILENAME ); |
| 119 | |
| 120 | $args = array( |
| 121 | 'post_type' => 'attachment', |
| 122 | 'post_mime_type' => 'image', |
| 123 | 'posts_per_page' => -1, |
| 124 | ); |
| 125 | |
| 126 | $media_library_images = get_posts( $args ); |
| 127 | |
| 128 | foreach ( $media_library_images as $media_image ) { |
| 129 | $existing_title = get_the_title( $media_image->ID ); |
| 130 | // Match base name with optional suffix like "-1", "-12", etc. Only match suffixes starting with hyphen followed by a number between 1 and 99. |
| 131 | if ( preg_match( '/^' . preg_quote( $base_name, '/' ) . '(-[1-9][0-9]?)?$/i', $existing_title ) ) { |
| 132 | return $media_image->ID; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 |