PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.7
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.7
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / zoho-inventory / class-import-image.php
commercebird / includes / classes / zoho-inventory Last commit date
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 1 year ago class-product.php 1 year ago class-users-contact.php 1 year ago index.php 1 year ago
class-import-image.php
119 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 ( empty( $image_url ) ) {
67 return;
68 }
69 $attachment_id = media_sideload_image( $image_url, $post_id, $image_name, 'id' );
70 if ( $attachment_id ) {
71 set_post_thumbnail( $post_id, $attachment_id );
72 update_post_meta( $attachment_id, '_wp_attachment_image_alt', $item_name );
73 update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
74 wp_update_image_subsizes( $attachment_id );
75 // also delete the zoho_image folder files.
76 $upload = wp_upload_dir();
77 $folder_path = $upload['basedir'] . '/zoho_image/';
78 $file_paths = glob( $folder_path . '/*' );
79 foreach ( $file_paths as $file_path ) {
80 if ( is_file( $file_path ) ) {
81 wp_delete_file( $file_path );
82 }
83 }
84 return $attachment_id;
85 }
86 }
87
88 /**
89 * Compare the image with existing media library images based on titles.
90 *
91 * @param string $item_image The name of the image.
92 * @return int|bool The ID of the existing image if a match is found, or false if no match is found.
93 * @since 1.0.0
94 */
95 protected function compare_image_with_media_library( $item_name, $item_image ) {
96
97 if ( ! empty( $item_image ) ) {
98 $args = array(
99 'post_type' => 'attachment',
100 'post_mime_type' => 'image',
101 'posts_per_page' => -1,
102 );
103 $media_library_images = get_posts( $args );
104 foreach ( $media_library_images as $media_image ) {
105 // Get the postmeta zoho_product_image_id of the existing image
106 $existing_image_title = get_the_title( $media_image->ID );
107 if ( strpos( $existing_image_title, $item_name ) !== false ) {
108 return $media_image->ID; // Return the ID of the existing image
109 }
110 if ( strpos( $existing_image_title, $item_image ) !== false ) {
111 return $media_image->ID; // Return the ID of the existing image
112 }
113 }
114 }
115 // If no match is found in the loop, return false
116 return false;
117 }
118 }
119