PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.4.4
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.4.4
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
244 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__ . '/cmbird_zi_get_image.txt', 'a+' );
40
41 $attachment_id = $this->compare_image_by_title( $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 // 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 }
55
56 $zoho_inventory_oid = $this->config['ProductZI']['OID'];
57 $zoho_inventory_url = $this->config['ProductZI']['APIURL'];
58 $url = "{$zoho_inventory_url}inventory/v1/items/$item_id/image";
59 $url .= "?organization_id=$zoho_inventory_oid";
60
61 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
62 $image_url = $execute_curl_call_handle->execute_curl_call_image_get( $url, $image_name );
63 // log image url
64 // fwrite( $fd, "Fetched image URL: $image_url.\n" );
65 if ( is_wp_error( $image_url ) || empty( $image_url ) ) {
66 // log the error message and the item name.
67 // $error_message = $image_url->get_error_message();
68 // fwrite( $fd, "Error fetching image for item $item_name: $error_message\n" );
69 return;
70 }
71 $upload_dir = wp_upload_dir();
72 $local_path = $upload_dir['basedir'] . '/zoho_image/' . basename( $image_url );
73 if ( file_exists( $local_path ) ) {
74
75 // Sideload the image properly to WordPress uploads directory
76 require_once ABSPATH . 'wp-admin/includes/media.php';
77 require_once ABSPATH . 'wp-admin/includes/file.php';
78 require_once ABSPATH . 'wp-admin/includes/image.php';
79
80 // Prepare file array for sideloading
81 $file_array = array(
82 'name' => sanitize_file_name( $image_name ),
83 'tmp_name' => $local_path,
84 );
85
86 // Sideload the image
87 $attach_id = media_handle_sideload( $file_array, $post_id, $item_name );
88
89 if ( is_wp_error( $attach_id ) ) {
90 // Clean up the temporary file on error
91 wp_delete_file( $local_path );
92 return false;
93 }
94
95 // Set as featured image and update metadata
96 set_post_thumbnail( $post_id, $attach_id );
97 update_post_meta( $attach_id, '_wp_attachment_image_alt', $item_name );
98 update_post_meta( $post_id, '_thumbnail_id', $attach_id );
99 wp_update_image_subsizes( $attach_id );
100
101 // Clean up the temporary zoho_image file after successful sideload
102 wp_delete_file( $local_path );
103
104 return $attach_id;
105 } else {
106 // fwrite( $fd, "File not found locally: $local_path\n" );
107 return;
108 }
109 }
110
111 /**
112 * Compare the image with existing media library images based on titles.
113 *
114 * @param string $item_image The name of the image.
115 * @return int|bool The ID of the existing image if a match is found, or false if no match is found.
116 * @since 1.0.0
117 */
118 protected function compare_image_by_title( $item_image ) {
119 if ( empty( $item_image ) ) {
120 return false;
121 }
122
123 $base_name = pathinfo( $item_image, PATHINFO_FILENAME );
124 $extension = pathinfo( $item_image, PATHINFO_EXTENSION );
125
126 // If no extension found, default to common image extensions
127 if ( empty( $extension ) ) {
128 $extensions = array( 'jpg', 'jpeg', 'png', 'webp', 'gif' );
129 } else {
130 $extensions = array( strtolower( $extension ) );
131 }
132
133 // Create variations of the name to search for
134 $search_variations = array(
135 $base_name, // Original: "01 Con Rod Bearing"
136 str_replace( ' ', '-', $base_name ), // Hyphenated: "01-Con-Rod-Bearing"
137 str_replace( ' ', '_', $base_name ), // Underscored: "01_Con_Rod_Bearing"
138 sanitize_title( $base_name ), // WordPress sanitized version
139 );
140
141 // Remove duplicates
142 $search_variations = array_unique( $search_variations );
143
144 // Use direct database query for better performance and accuracy
145 global $wpdb;
146
147 // Try each variation for exact title matches
148 foreach ( $search_variations as $variation ) {
149 $exact_match = $wpdb->get_var(
150 $wpdb->prepare(
151 "SELECT p.ID
152 FROM {$wpdb->posts} p
153 WHERE p.post_type = 'attachment'
154 AND p.post_mime_type LIKE %s
155 AND p.post_title = %s
156 ORDER BY p.post_date DESC
157 LIMIT 1",
158 'image/%',
159 $variation
160 )
161 );
162
163 if ( $exact_match ) {
164 return intval( $exact_match );
165 }
166 }
167
168 // Search for similar titles using the main variation (hyphenated)
169 $main_search = str_replace( ' ', '-', $base_name );
170 $like_pattern = $wpdb->esc_like( $main_search ) . '%';
171 $similar_matches = $wpdb->get_results(
172 $wpdb->prepare(
173 "SELECT p.ID, p.post_title
174 FROM {$wpdb->posts} p
175 WHERE p.post_type = 'attachment'
176 AND p.post_mime_type LIKE %s
177 AND p.post_title LIKE %s
178 ORDER BY p.post_date DESC
179 LIMIT 20",
180 'image/%',
181 $like_pattern
182 )
183 );
184
185 foreach ( $similar_matches as $match ) {
186 $existing_title = $match->post_title;
187 // Match base name with optional WordPress suffix like "-1", "-12", etc.
188 if ( preg_match( '/^' . preg_quote( $main_search, '/' ) . '(-[0-9]+)?$/i', $existing_title ) ) {
189 return intval( $match->ID );
190 }
191 }
192
193 // Search by filename in _wp_attached_file meta for each extension
194 foreach ( $search_variations as $variation ) {
195 foreach ( $extensions as $ext ) {
196 $filename_match = $wpdb->get_var(
197 $wpdb->prepare(
198 "SELECT p.ID
199 FROM {$wpdb->posts} p
200 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
201 WHERE p.post_type = 'attachment'
202 AND p.post_mime_type LIKE %s
203 AND pm.meta_key = '_wp_attached_file'
204 AND pm.meta_value LIKE %s
205 ORDER BY p.post_date DESC
206 LIMIT 1",
207 'image/%',
208 '%' . $wpdb->esc_like( $variation . '.' . $ext )
209 )
210 );
211
212 if ( $filename_match ) {
213 return intval( $filename_match );
214 }
215 }
216 }
217
218 // Search for any file that ends with our base name (any extension)
219 foreach ( $extensions as $ext ) {
220 $base_filename_match = $wpdb->get_var(
221 $wpdb->prepare(
222 "SELECT p.ID
223 FROM {$wpdb->posts} p
224 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
225 WHERE p.post_type = 'attachment'
226 AND p.post_mime_type LIKE %s
227 AND pm.meta_key = '_wp_attached_file'
228 AND pm.meta_value LIKE %s
229 ORDER BY p.post_date DESC
230 LIMIT 1",
231 'image/%',
232 '%' . $wpdb->esc_like( $base_name . '.' . $ext )
233 )
234 );
235
236 if ( $base_filename_match ) {
237 return intval( $base_filename_match );
238 }
239 }
240
241 return false;
242 }
243 }
244