@@ -15,14 +15,14 @@ discard block |
||
15 | 15 | * Abort if this file is called directly |
16 | 16 | */ |
17 | 17 | if (!defined('ABSPATH')) { |
18 | - exit; |
|
18 | + exit; |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | /** |
22 | 22 | * Include plugin file if function is_plugin_active does not exist |
23 | 23 | */ |
24 | 24 | if (!function_exists('is_plugin_active')) { |
25 | - require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
25 | + require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | /** |
@@ -45,229 +45,229 @@ discard block |
||
45 | 45 | define('ALGOLIA_API_KEY', '_admin_api_key'); |
46 | 46 | |
47 | 47 | if (!class_exists('Algolia_Send_Products')) { |
48 | - /** |
|
49 | - * Algolia WooIndexer main class |
|
50 | - */ |
|
51 | - // TODO Rename class "Algolia_Send_Products" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
52 | - class Algolia_Send_Products |
|
53 | - { |
|
54 | - const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
55 | - const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
56 | - |
|
57 | - /** |
|
58 | - * The Algolia instance |
|
59 | - * |
|
60 | - * @var \Algolia\AlgoliaSearch\SearchClient |
|
61 | - */ |
|
62 | - private static $algolia = null; |
|
63 | - |
|
64 | - /** |
|
65 | - * Check if we can connect to Algolia, if not, handle the exception, display an error and then return |
|
66 | - */ |
|
67 | - public static function can_connect_to_algolia() |
|
68 | - { |
|
69 | - try { |
|
70 | - self::$algolia->listApiKeys(); |
|
71 | - } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
|
72 | - add_action( |
|
73 | - 'admin_notices', |
|
74 | - function () { |
|
75 | - echo '<div class="error notice"> |
|
48 | + /** |
|
49 | + * Algolia WooIndexer main class |
|
50 | + */ |
|
51 | + // TODO Rename class "Algolia_Send_Products" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
52 | + class Algolia_Send_Products |
|
53 | + { |
|
54 | + const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
55 | + const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
56 | + |
|
57 | + /** |
|
58 | + * The Algolia instance |
|
59 | + * |
|
60 | + * @var \Algolia\AlgoliaSearch\SearchClient |
|
61 | + */ |
|
62 | + private static $algolia = null; |
|
63 | + |
|
64 | + /** |
|
65 | + * Check if we can connect to Algolia, if not, handle the exception, display an error and then return |
|
66 | + */ |
|
67 | + public static function can_connect_to_algolia() |
|
68 | + { |
|
69 | + try { |
|
70 | + self::$algolia->listApiKeys(); |
|
71 | + } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
|
72 | + add_action( |
|
73 | + 'admin_notices', |
|
74 | + function () { |
|
75 | + echo '<div class="error notice"> |
|
76 | 76 | <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> |
77 | 77 | </div>'; |
78 | - } |
|
79 | - ); |
|
80 | - return; |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Get sale price or regular price based on product type |
|
86 | - * |
|
87 | - * @param mixed $product Product to check |
|
88 | - * @return array ['sale_price' => $sale_price,'regular_price' => $regular_price] Array with regular price and sale price |
|
89 | - */ |
|
90 | - public static function get_product_type_price($product) |
|
91 | - { |
|
92 | - $sale_price = 0; |
|
93 | - $regular_price = 0; |
|
94 | - if ($product->is_type('simple')) { |
|
95 | - $sale_price = $product->get_sale_price(); |
|
96 | - $regular_price = $product->get_regular_price(); |
|
97 | - } elseif ($product->is_type('variable')) { |
|
98 | - $sale_price = $product->get_variation_sale_price('min', true); |
|
99 | - $regular_price = $product->get_variation_regular_price('max', true); |
|
100 | - } |
|
101 | - return array( |
|
102 | - 'sale_price' => $sale_price, |
|
103 | - 'regular_price' => $regular_price |
|
104 | - ); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Get attributes from product |
|
109 | - * |
|
110 | - * @param mixed $product Product to check |
|
111 | - * @return array ['pa_name' => ['value1', 'value2']] Array with key set to the product attribute internal name and values as array. returns false if not attributes found. |
|
112 | - */ |
|
113 | - public static function get_product_attributes($product) |
|
114 | - { |
|
115 | - $rawAttributes = $product->get_attributes(); |
|
116 | - |
|
117 | - if (!$rawAttributes) { |
|
118 | - return false; |
|
119 | - } |
|
120 | - |
|
121 | - $attributes = []; |
|
122 | - foreach ($rawAttributes as $attribute) { |
|
123 | - if ($attribute->get_variation()) { |
|
124 | - continue; |
|
125 | - } |
|
126 | - $name = $attribute->get_name(); |
|
127 | - if ($attribute->is_taxonomy()) { |
|
128 | - $terms = wp_get_post_terms($product->get_id(), $name, 'all'); |
|
129 | - $tax_terms = array(); |
|
130 | - foreach ($terms as $term) { |
|
131 | - $single_term = esc_html($term->name); |
|
132 | - array_push($tax_terms, $single_term); |
|
133 | - } |
|
134 | - $attributes[$name] = $tax_terms; |
|
135 | - } |
|
136 | - } |
|
137 | - return $attributes; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Send WooCommerce products to Algolia |
|
142 | - * |
|
143 | - * @param Int $id Product to send to Algolia if we send only a single product |
|
144 | - * @return void |
|
145 | - */ |
|
146 | - public static function send_products_to_algolia($id = '') |
|
147 | - { |
|
148 | - /** |
|
149 | - * Remove classes from plugin URL and autoload Algolia with Composer |
|
150 | - */ |
|
151 | - |
|
152 | - $base_plugin_directory = str_replace('classes', '', dirname(__FILE__)); |
|
153 | - require_once $base_plugin_directory . '/vendor/autoload.php'; |
|
154 | - |
|
155 | - /** |
|
156 | - * Fetch the required variables from the Settings API |
|
157 | - */ |
|
158 | - |
|
159 | - $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
160 | - $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME; |
|
161 | - |
|
162 | - $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
163 | - $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
|
164 | - |
|
165 | - $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
166 | - $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
167 | - |
|
168 | - /** |
|
169 | - * Display admin notice and return if not all values have been set |
|
170 | - */ |
|
171 | - |
|
172 | - Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name); |
|
173 | - |
|
174 | - /** |
|
175 | - * Initiate the Algolia client |
|
176 | - */ |
|
177 | - self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key); |
|
178 | - |
|
179 | - /** |
|
180 | - * Check if we can connect, if not, handle the exception, display an error and then return |
|
181 | - */ |
|
182 | - self::can_connect_to_algolia(); |
|
183 | - |
|
184 | - /** |
|
185 | - * Initialize the search index and set the name to the option from the database |
|
186 | - */ |
|
187 | - $index = self::$algolia->initIndex($algolia_index_name); |
|
188 | - |
|
189 | - /** |
|
190 | - * Setup arguments for sending all products to Algolia |
|
191 | - * |
|
192 | - * Limit => -1 means we send all products |
|
193 | - */ |
|
194 | - $arguments = array( |
|
195 | - 'status' => 'publish', |
|
196 | - 'limit' => -1, |
|
197 | - 'paginate' => false, |
|
198 | - ); |
|
199 | - |
|
200 | - /** |
|
201 | - * Setup arguments for sending only a single product |
|
202 | - */ |
|
203 | - if (isset($id) && '' !== $id) { |
|
204 | - $arguments = array( |
|
205 | - 'status' => 'publish', |
|
206 | - 'include' => array($id), |
|
207 | - 'paginate' => false, |
|
208 | - ); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Fetch all products from WooCommerce |
|
213 | - * |
|
214 | - * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html |
|
215 | - */ |
|
216 | - $products = |
|
217 | - /** @scrutinizer ignore-call */ |
|
218 | - wc_get_products($arguments); |
|
219 | - |
|
220 | - if (empty($products)) { |
|
221 | - return; |
|
222 | - } |
|
223 | - $records = array(); |
|
224 | - $record = array(); |
|
225 | - |
|
226 | - foreach ($products as $product) { |
|
227 | - /** |
|
228 | - * Set sale price or regular price based on product type |
|
229 | - */ |
|
230 | - $product_type_price = self::get_product_type_price($product); |
|
231 | - $sale_price = $product_type_price['sale_price']; |
|
232 | - $regular_price = $product_type_price['regular_price']; |
|
233 | - |
|
234 | - /** |
|
235 | - * Extract image from $product->get_image() |
|
236 | - */ |
|
237 | - preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result); |
|
238 | - $product_image = array_pop($result); |
|
239 | - |
|
240 | - /** |
|
241 | - * Build the record array using the information from the WooCommerce product |
|
242 | - */ |
|
243 | - $record['objectID'] = $product->get_id(); |
|
244 | - $record['product_name'] = $product->get_name(); |
|
245 | - $record['product_image'] = $product_image; |
|
246 | - $record['short_description'] = $product->get_short_description(); |
|
247 | - $record['regular_price'] = $regular_price; |
|
248 | - $record['sale_price'] = $sale_price; |
|
249 | - $record['on_sale'] = $product->is_on_sale(); |
|
250 | - $record['attributes'] = self::get_product_attributes($product); |
|
251 | - $records[] = $record; |
|
252 | - } |
|
253 | - wp_reset_postdata(); |
|
254 | - |
|
255 | - /** |
|
256 | - * Send the information to Algolia and save the result |
|
257 | - * If result is NullResponse, print an error message |
|
258 | - */ |
|
259 | - $result = $index->saveObjects($records); |
|
260 | - |
|
261 | - if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) { |
|
262 | - wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings')); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Display success message |
|
267 | - */ |
|
268 | - echo '<div class="notice notice-success is-dismissible"> |
|
78 | + } |
|
79 | + ); |
|
80 | + return; |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Get sale price or regular price based on product type |
|
86 | + * |
|
87 | + * @param mixed $product Product to check |
|
88 | + * @return array ['sale_price' => $sale_price,'regular_price' => $regular_price] Array with regular price and sale price |
|
89 | + */ |
|
90 | + public static function get_product_type_price($product) |
|
91 | + { |
|
92 | + $sale_price = 0; |
|
93 | + $regular_price = 0; |
|
94 | + if ($product->is_type('simple')) { |
|
95 | + $sale_price = $product->get_sale_price(); |
|
96 | + $regular_price = $product->get_regular_price(); |
|
97 | + } elseif ($product->is_type('variable')) { |
|
98 | + $sale_price = $product->get_variation_sale_price('min', true); |
|
99 | + $regular_price = $product->get_variation_regular_price('max', true); |
|
100 | + } |
|
101 | + return array( |
|
102 | + 'sale_price' => $sale_price, |
|
103 | + 'regular_price' => $regular_price |
|
104 | + ); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Get attributes from product |
|
109 | + * |
|
110 | + * @param mixed $product Product to check |
|
111 | + * @return array ['pa_name' => ['value1', 'value2']] Array with key set to the product attribute internal name and values as array. returns false if not attributes found. |
|
112 | + */ |
|
113 | + public static function get_product_attributes($product) |
|
114 | + { |
|
115 | + $rawAttributes = $product->get_attributes(); |
|
116 | + |
|
117 | + if (!$rawAttributes) { |
|
118 | + return false; |
|
119 | + } |
|
120 | + |
|
121 | + $attributes = []; |
|
122 | + foreach ($rawAttributes as $attribute) { |
|
123 | + if ($attribute->get_variation()) { |
|
124 | + continue; |
|
125 | + } |
|
126 | + $name = $attribute->get_name(); |
|
127 | + if ($attribute->is_taxonomy()) { |
|
128 | + $terms = wp_get_post_terms($product->get_id(), $name, 'all'); |
|
129 | + $tax_terms = array(); |
|
130 | + foreach ($terms as $term) { |
|
131 | + $single_term = esc_html($term->name); |
|
132 | + array_push($tax_terms, $single_term); |
|
133 | + } |
|
134 | + $attributes[$name] = $tax_terms; |
|
135 | + } |
|
136 | + } |
|
137 | + return $attributes; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Send WooCommerce products to Algolia |
|
142 | + * |
|
143 | + * @param Int $id Product to send to Algolia if we send only a single product |
|
144 | + * @return void |
|
145 | + */ |
|
146 | + public static function send_products_to_algolia($id = '') |
|
147 | + { |
|
148 | + /** |
|
149 | + * Remove classes from plugin URL and autoload Algolia with Composer |
|
150 | + */ |
|
151 | + |
|
152 | + $base_plugin_directory = str_replace('classes', '', dirname(__FILE__)); |
|
153 | + require_once $base_plugin_directory . '/vendor/autoload.php'; |
|
154 | + |
|
155 | + /** |
|
156 | + * Fetch the required variables from the Settings API |
|
157 | + */ |
|
158 | + |
|
159 | + $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
160 | + $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME; |
|
161 | + |
|
162 | + $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
163 | + $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
|
164 | + |
|
165 | + $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
166 | + $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
167 | + |
|
168 | + /** |
|
169 | + * Display admin notice and return if not all values have been set |
|
170 | + */ |
|
171 | + |
|
172 | + Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name); |
|
173 | + |
|
174 | + /** |
|
175 | + * Initiate the Algolia client |
|
176 | + */ |
|
177 | + self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key); |
|
178 | + |
|
179 | + /** |
|
180 | + * Check if we can connect, if not, handle the exception, display an error and then return |
|
181 | + */ |
|
182 | + self::can_connect_to_algolia(); |
|
183 | + |
|
184 | + /** |
|
185 | + * Initialize the search index and set the name to the option from the database |
|
186 | + */ |
|
187 | + $index = self::$algolia->initIndex($algolia_index_name); |
|
188 | + |
|
189 | + /** |
|
190 | + * Setup arguments for sending all products to Algolia |
|
191 | + * |
|
192 | + * Limit => -1 means we send all products |
|
193 | + */ |
|
194 | + $arguments = array( |
|
195 | + 'status' => 'publish', |
|
196 | + 'limit' => -1, |
|
197 | + 'paginate' => false, |
|
198 | + ); |
|
199 | + |
|
200 | + /** |
|
201 | + * Setup arguments for sending only a single product |
|
202 | + */ |
|
203 | + if (isset($id) && '' !== $id) { |
|
204 | + $arguments = array( |
|
205 | + 'status' => 'publish', |
|
206 | + 'include' => array($id), |
|
207 | + 'paginate' => false, |
|
208 | + ); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Fetch all products from WooCommerce |
|
213 | + * |
|
214 | + * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html |
|
215 | + */ |
|
216 | + $products = |
|
217 | + /** @scrutinizer ignore-call */ |
|
218 | + wc_get_products($arguments); |
|
219 | + |
|
220 | + if (empty($products)) { |
|
221 | + return; |
|
222 | + } |
|
223 | + $records = array(); |
|
224 | + $record = array(); |
|
225 | + |
|
226 | + foreach ($products as $product) { |
|
227 | + /** |
|
228 | + * Set sale price or regular price based on product type |
|
229 | + */ |
|
230 | + $product_type_price = self::get_product_type_price($product); |
|
231 | + $sale_price = $product_type_price['sale_price']; |
|
232 | + $regular_price = $product_type_price['regular_price']; |
|
233 | + |
|
234 | + /** |
|
235 | + * Extract image from $product->get_image() |
|
236 | + */ |
|
237 | + preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result); |
|
238 | + $product_image = array_pop($result); |
|
239 | + |
|
240 | + /** |
|
241 | + * Build the record array using the information from the WooCommerce product |
|
242 | + */ |
|
243 | + $record['objectID'] = $product->get_id(); |
|
244 | + $record['product_name'] = $product->get_name(); |
|
245 | + $record['product_image'] = $product_image; |
|
246 | + $record['short_description'] = $product->get_short_description(); |
|
247 | + $record['regular_price'] = $regular_price; |
|
248 | + $record['sale_price'] = $sale_price; |
|
249 | + $record['on_sale'] = $product->is_on_sale(); |
|
250 | + $record['attributes'] = self::get_product_attributes($product); |
|
251 | + $records[] = $record; |
|
252 | + } |
|
253 | + wp_reset_postdata(); |
|
254 | + |
|
255 | + /** |
|
256 | + * Send the information to Algolia and save the result |
|
257 | + * If result is NullResponse, print an error message |
|
258 | + */ |
|
259 | + $result = $index->saveObjects($records); |
|
260 | + |
|
261 | + if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) { |
|
262 | + wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings')); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Display success message |
|
267 | + */ |
|
268 | + echo '<div class="notice notice-success is-dismissible"> |
|
269 | 269 | <p>' . esc_html__('Product(s) sent to Algolia.', 'algolia-woo-indexer') . '</p> |
270 | 270 | </div>'; |
271 | - } |
|
272 | - } |
|
271 | + } |
|
272 | + } |
|
273 | 273 | } |
@@ -14,14 +14,14 @@ discard block |
||
14 | 14 | /** |
15 | 15 | * Abort if this file is called directly |
16 | 16 | */ |
17 | -if (!defined('ABSPATH')) { |
|
17 | +if ( ! defined('ABSPATH')) { |
|
18 | 18 | exit; |
19 | 19 | } |
20 | 20 | |
21 | 21 | /** |
22 | 22 | * Include plugin file if function is_plugin_active does not exist |
23 | 23 | */ |
24 | -if (!function_exists('is_plugin_active')) { |
|
24 | +if ( ! function_exists('is_plugin_active')) { |
|
25 | 25 | require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
26 | 26 | } |
27 | 27 | |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | define('ALGOLIA_APP_ID', '_application_id'); |
45 | 45 | define('ALGOLIA_API_KEY', '_admin_api_key'); |
46 | 46 | |
47 | -if (!class_exists('Algolia_Send_Products')) { |
|
47 | +if ( ! class_exists('Algolia_Send_Products')) { |
|
48 | 48 | /** |
49 | 49 | * Algolia WooIndexer main class |
50 | 50 | */ |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
72 | 72 | add_action( |
73 | 73 | 'admin_notices', |
74 | - function () { |
|
74 | + function() { |
|
75 | 75 | echo '<div class="error notice"> |
76 | 76 | <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> |
77 | 77 | </div>'; |
@@ -92,11 +92,11 @@ discard block |
||
92 | 92 | $sale_price = 0; |
93 | 93 | $regular_price = 0; |
94 | 94 | if ($product->is_type('simple')) { |
95 | - $sale_price = $product->get_sale_price(); |
|
96 | - $regular_price = $product->get_regular_price(); |
|
95 | + $sale_price = $product->get_sale_price(); |
|
96 | + $regular_price = $product->get_regular_price(); |
|
97 | 97 | } elseif ($product->is_type('variable')) { |
98 | - $sale_price = $product->get_variation_sale_price('min', true); |
|
99 | - $regular_price = $product->get_variation_regular_price('max', true); |
|
98 | + $sale_price = $product->get_variation_sale_price('min', true); |
|
99 | + $regular_price = $product->get_variation_regular_price('max', true); |
|
100 | 100 | } |
101 | 101 | return array( |
102 | 102 | 'sale_price' => $sale_price, |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | { |
115 | 115 | $rawAttributes = $product->get_attributes(); |
116 | 116 | |
117 | - if (!$rawAttributes) { |
|
117 | + if ( ! $rawAttributes) { |
|
118 | 118 | return false; |
119 | 119 | } |
120 | 120 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
164 | 164 | |
165 | 165 | $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
166 | - $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
166 | + $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
167 | 167 | |
168 | 168 | /** |
169 | 169 | * Display admin notice and return if not all values have been set |