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