PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.7.0
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.7.0
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-cmbird-image-zi.php
commercebird / includes / classes / zoho-inventory Last commit date
class-cmbird-categories-zi.php 7 months ago class-cmbird-image-zi.php 5 months ago class-import-items.php 5 months ago class-import-price-list.php 9 months ago class-multi-currency.php 7 months ago class-order-sync.php 8 months ago class-product.php 5 months ago class-users-contact.php 9 months ago index.php 1 year ago
class-cmbird-image-zi.php
204 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 $image_name - Image name.
35 * @param string $post_id - Post id of product.
36 * @return integer | void
37 */
38 public function cmbird_zi_get_image( $item_id, $item_name, $image_name, $post_id = null ) {
39 // $fd = fopen( __DIR__ . '/cmbird_zi_get_image.txt', 'a+' );
40
41 $attachment_id = $this->compare_image_by_title( $image_name );
42 if ( $attachment_id && $post_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 // Clean up only the specific downloaded image file.
48 $upload_dir = wp_upload_dir();
49 $local_path = $upload_dir['basedir'] . '/zoho_image/' . basename( $image_name );
50 if ( file_exists( $local_path ) ) {
51 wp_delete_file( $local_path );
52 }
53 return $attachment_id;
54 } elseif ( $attachment_id ) {
55 return $attachment_id;
56 }
57
58 $zoho_inventory_oid = $this->config['ProductZI']['OID'];
59 $zoho_inventory_url = $this->config['ProductZI']['APIURL'];
60 $url = "{$zoho_inventory_url}inventory/v1/items/$item_id/image";
61 $url .= "?organization_id=$zoho_inventory_oid";
62
63 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
64 $image_url = $execute_curl_call_handle->execute_curl_call_image_get( $url, $image_name );
65 // log image url.
66 // fwrite( $fd, "Fetched image URL: $image_url.\n" );
67 if ( is_wp_error( $image_url ) || empty( $image_url ) ) {
68 // log the error message and the item name.
69 // $error_message = $image_url->get_error_message();
70 // fwrite( $fd, "Error fetching image for item $item_name: $error_message\n" );
71 return;
72 }
73 $upload_dir = wp_upload_dir();
74 $local_path = $upload_dir['basedir'] . '/zoho_image/' . basename( $image_url );
75 if ( file_exists( $local_path ) ) {
76
77 // Sideload the image properly to WordPress uploads directory.
78 require_once ABSPATH . 'wp-admin/includes/media.php';
79 require_once ABSPATH . 'wp-admin/includes/file.php';
80 require_once ABSPATH . 'wp-admin/includes/image.php';
81
82 // Prepare file array for sideloading.
83 $file_array = array(
84 'name' => sanitize_file_name( $image_name ),
85 'tmp_name' => $local_path,
86 );
87
88 // Sideload the image.
89 $attach_id = media_handle_sideload( $file_array, $post_id, $item_name );
90
91 if ( is_wp_error( $attach_id ) ) {
92 // Clean up the temporary file on error.
93 wp_delete_file( $local_path );
94 return false;
95 }
96
97 // Set as featured image and update metadata.
98 set_post_thumbnail( $post_id, $attach_id );
99 update_post_meta( $attach_id, '_wp_attachment_image_alt', $item_name );
100 update_post_meta( $post_id, '_thumbnail_id', $attach_id );
101 wp_update_image_subsizes( $attach_id );
102
103 // Clean up the temporary zoho_image file after successful sideload.
104 wp_delete_file( $local_path );
105
106 return $attach_id;
107 } else {
108 return;
109 }
110 }
111
112 /**
113 * Compare the image with existing media library images based on titles.
114 *
115 * @param string $item_image The name of the image.
116 * @return int|bool The ID of the existing image if a match is found, or false if no match is found.
117 * @since 1.0.0
118 */
119 protected function compare_image_by_title( $item_image ) {
120 if ( empty( $item_image ) ) {
121 return false;
122 }
123
124 $base_name = pathinfo( $item_image, PATHINFO_FILENAME );
125 $wp_sanitized = sanitize_title_with_dashes( $base_name );
126 $hyphenated = str_replace( array( ' ', '(', ')', '_', '.' ), '-', $base_name );
127 $hyphenated = preg_replace( '/-+/', '-', $hyphenated );
128 $hyphenated = trim( $hyphenated, '-' );
129
130 $search_variations = array_unique(
131 array(
132 $base_name,
133 strtolower( $base_name ),
134 $wp_sanitized,
135 $hyphenated,
136 strtolower( $hyphenated ),
137 )
138 );
139
140 global $wpdb;
141
142 // Try exact title matches (case-insensitive).
143 foreach ( $search_variations as $variation ) {
144 $exact_match = $wpdb->get_var(
145 $wpdb->prepare(
146 "SELECT p.ID FROM {$wpdb->posts} p WHERE p.post_type = 'attachment' AND p.post_mime_type LIKE %s AND LOWER(p.post_title) = %s ORDER BY p.post_date DESC LIMIT 1",
147 'image/%',
148 strtolower( $variation )
149 )
150 );
151 if ( $exact_match ) {
152 return intval( $exact_match );
153 }
154 }
155
156 // Try LIKE match for sanitized/slug variations in _wp_attached_file (any extension).
157 foreach ( $search_variations as $variation ) {
158 // Match: base, base-1, base-2, etc. (case-insensitive, any extension).
159 $like_pattern = $wpdb->esc_like( $variation ) . '%';
160 $filename_match = $wpdb->get_var(
161 $wpdb->prepare(
162 "SELECT p.ID FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id WHERE p.post_type = 'attachment' AND p.post_mime_type LIKE %s AND pm.meta_key = '_wp_attached_file' AND (pm.meta_value LIKE %s OR pm.meta_value REGEXP %s) ORDER BY p.post_date DESC LIMIT 1",
163 'image/%',
164 '%' . $like_pattern . '.%',
165 '/' . preg_quote( $variation, '/' ) . '(-[0-9]+)?\\.[a-zA-Z0-9]+$/'
166 )
167 );
168 if ( $filename_match ) {
169 return intval( $filename_match );
170 }
171 }
172
173 // Try LIKE match for titles with -N suffix (case-insensitive).
174 foreach ( $search_variations as $variation ) {
175 $title_like_pattern = $wpdb->esc_like( $variation ) . '%';
176 $title_match = $wpdb->get_var(
177 $wpdb->prepare(
178 "SELECT p.ID FROM {$wpdb->posts} p WHERE p.post_type = 'attachment' AND p.post_mime_type LIKE %s AND (LOWER(p.post_title) LIKE %s OR LOWER(p.post_title) REGEXP %s) ORDER BY p.post_date DESC LIMIT 1",
179 'image/%',
180 strtolower( $title_like_pattern ),
181 '^' . preg_quote( strtolower( $variation ), '/' ) . '(-[0-9]+)?$'
182 )
183 );
184 if ( $title_match ) {
185 return intval( $title_match );
186 }
187 }
188
189 // Fallback: Try LIKE search for the raw $item_image (case-insensitive).
190 $raw_filename_match = $wpdb->get_var(
191 $wpdb->prepare(
192 "SELECT p.ID FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id WHERE p.post_type = 'attachment' AND p.post_mime_type LIKE %s AND pm.meta_key = '_wp_attached_file' AND LOWER(pm.meta_value) LIKE %s ORDER BY p.post_date DESC LIMIT 1",
193 'image/%',
194 '%' . strtolower( $item_image ) . '%'
195 )
196 );
197 if ( $raw_filename_match ) {
198 return intval( $raw_filename_match );
199 }
200
201 return false;
202 }
203 }
204