@@ -18,28 +18,28 @@ discard block |
||
| 18 | 18 | * Abort if this file is called directly |
| 19 | 19 | */ |
| 20 | 20 | if (! defined('ABSPATH')) { |
| 21 | - exit; |
|
| 21 | + exit; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | if (! class_exists('Algolia_Check_Requirements')) { |
| 25 | - /** |
|
| 26 | - * Check requirements for Algolia plugin |
|
| 27 | - */ |
|
| 28 | - class Algolia_Check_Requirements |
|
| 29 | - { |
|
| 25 | + /** |
|
| 26 | + * Check requirements for Algolia plugin |
|
| 27 | + */ |
|
| 28 | + class Algolia_Check_Requirements |
|
| 29 | + { |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Check for required PHP version. |
|
| 33 | - * |
|
| 34 | - * @return bool |
|
| 35 | - */ |
|
| 36 | - public static function algolia_php_version_check() |
|
| 37 | - { |
|
| 38 | - if (version_compare(PHP_VERSION, ALGOLIA_MIN_PHP_VERSION, '<')) { |
|
| 39 | - return false; |
|
| 40 | - } |
|
| 41 | - return true; |
|
| 42 | - } |
|
| 31 | + /** |
|
| 32 | + * Check for required PHP version. |
|
| 33 | + * |
|
| 34 | + * @return bool |
|
| 35 | + */ |
|
| 36 | + public static function algolia_php_version_check() |
|
| 37 | + { |
|
| 38 | + if (version_compare(PHP_VERSION, ALGOLIA_MIN_PHP_VERSION, '<')) { |
|
| 39 | + return false; |
|
| 40 | + } |
|
| 41 | + return true; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * Check if values are empty and display error notice if not all values have been set |
@@ -52,53 +52,53 @@ discard block |
||
| 52 | 52 | public static function check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name ) |
| 53 | 53 | { |
| 54 | 54 | if (empty($algolia_application_id) || empty($algolia_api_key || empty($algolia_index_name))) { |
| 55 | - add_action( |
|
| 56 | - 'admin_notices', |
|
| 57 | - function () { |
|
| 58 | - echo '<div class="error notice"> |
|
| 55 | + add_action( |
|
| 56 | + 'admin_notices', |
|
| 57 | + function () { |
|
| 58 | + echo '<div class="error notice"> |
|
| 59 | 59 | <p>' . esc_html__('All settings need to be set for the plugin to work.', 'algolia-woo-indexer') . '</p> |
| 60 | 60 | </div>'; |
| 61 | - } |
|
| 62 | - ); |
|
| 63 | - return; |
|
| 64 | - } |
|
| 65 | - } |
|
| 61 | + } |
|
| 62 | + ); |
|
| 63 | + return; |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Check for required WordPress version. |
|
| 69 | - * |
|
| 70 | - * @return bool |
|
| 71 | - */ |
|
| 72 | - public static function algolia_wp_version_check() |
|
| 73 | - { |
|
| 74 | - if (version_compare($GLOBALS['wp_version'], ALGOLIA_MIN_WP_VERSION, '<')) { |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - return true; |
|
| 78 | - } |
|
| 67 | + /** |
|
| 68 | + * Check for required WordPress version. |
|
| 69 | + * |
|
| 70 | + * @return bool |
|
| 71 | + */ |
|
| 72 | + public static function algolia_wp_version_check() |
|
| 73 | + { |
|
| 74 | + if (version_compare($GLOBALS['wp_version'], ALGOLIA_MIN_WP_VERSION, '<')) { |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + return true; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Check that we have all of the required PHP extensions installed |
|
| 82 | - * |
|
| 83 | - * @return void |
|
| 84 | - */ |
|
| 85 | - public static function check_unmet_requirements() |
|
| 86 | - { |
|
| 87 | - if (! extension_loaded('mbstring')) { |
|
| 88 | - echo '<div class="error notice"> |
|
| 80 | + /** |
|
| 81 | + * Check that we have all of the required PHP extensions installed |
|
| 82 | + * |
|
| 83 | + * @return void |
|
| 84 | + */ |
|
| 85 | + public static function check_unmet_requirements() |
|
| 86 | + { |
|
| 87 | + if (! extension_loaded('mbstring')) { |
|
| 88 | + echo '<div class="error notice"> |
|
| 89 | 89 | <p>' . esc_html__('Algolia Woo Indexer requires the "mbstring" PHP extension to be enabled. Please contact your hosting provider.', 'algolia-woo-indexer') . '</p> |
| 90 | 90 | </div>'; |
| 91 | - } elseif (! function_exists('mb_ereg_replace')) { |
|
| 92 | - echo '<div class="error notice"> |
|
| 91 | + } elseif (! function_exists('mb_ereg_replace')) { |
|
| 92 | + echo '<div class="error notice"> |
|
| 93 | 93 | <p>' . esc_html__('Algolia Woo Indexer needs "mbregex" NOT to be disabled. Please contact your hosting provider.', 'algolia-woo-indexer') . '</p> |
| 94 | 94 | </div>'; |
| 95 | - } |
|
| 96 | - if (! extension_loaded('curl')) { |
|
| 97 | - echo '<div class="error notice"> |
|
| 95 | + } |
|
| 96 | + if (! extension_loaded('curl')) { |
|
| 97 | + echo '<div class="error notice"> |
|
| 98 | 98 | <p>' . esc_html__('Algolia Woo Indexer requires the "cURL" PHP extension to be enabled. Please contact your hosting provider.', 'algolia-woo-indexer') . '</p> |
| 99 | 99 | </div>'; |
| 100 | - return; |
|
| 101 | - } |
|
| 102 | - } |
|
| 100 | + return; |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | 103 | } |
| 104 | 104 | } |
@@ -17,11 +17,11 @@ discard block |
||
| 17 | 17 | /** |
| 18 | 18 | * Abort if this file is called directly |
| 19 | 19 | */ |
| 20 | -if (! defined('ABSPATH')) { |
|
| 20 | +if ( ! defined('ABSPATH')) { |
|
| 21 | 21 | exit; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | -if (! class_exists('Algolia_Check_Requirements')) { |
|
| 24 | +if ( ! class_exists('Algolia_Check_Requirements')) { |
|
| 25 | 25 | /** |
| 26 | 26 | * Check requirements for Algolia plugin |
| 27 | 27 | */ |
@@ -49,12 +49,12 @@ discard block |
||
| 49 | 49 | * @param string $algolia_index_name Algolia index name. |
| 50 | 50 | * |
| 51 | 51 | */ |
| 52 | - public static function check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name ) |
|
| 52 | + public static function check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name) |
|
| 53 | 53 | { |
| 54 | 54 | if (empty($algolia_application_id) || empty($algolia_api_key || empty($algolia_index_name))) { |
| 55 | 55 | add_action( |
| 56 | 56 | 'admin_notices', |
| 57 | - function () { |
|
| 57 | + function() { |
|
| 58 | 58 | echo '<div class="error notice"> |
| 59 | 59 | <p>' . esc_html__('All settings need to be set for the plugin to work.', 'algolia-woo-indexer') . '</p> |
| 60 | 60 | </div>'; |
@@ -84,16 +84,16 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | public static function check_unmet_requirements() |
| 86 | 86 | { |
| 87 | - if (! extension_loaded('mbstring')) { |
|
| 87 | + if ( ! extension_loaded('mbstring')) { |
|
| 88 | 88 | echo '<div class="error notice"> |
| 89 | 89 | <p>' . esc_html__('Algolia Woo Indexer requires the "mbstring" PHP extension to be enabled. Please contact your hosting provider.', 'algolia-woo-indexer') . '</p> |
| 90 | 90 | </div>'; |
| 91 | - } elseif (! function_exists('mb_ereg_replace')) { |
|
| 91 | + } elseif ( ! function_exists('mb_ereg_replace')) { |
|
| 92 | 92 | echo '<div class="error notice"> |
| 93 | 93 | <p>' . esc_html__('Algolia Woo Indexer needs "mbregex" NOT to be disabled. Please contact your hosting provider.', 'algolia-woo-indexer') . '</p> |
| 94 | 94 | </div>'; |
| 95 | 95 | } |
| 96 | - if (! extension_loaded('curl')) { |
|
| 96 | + if ( ! extension_loaded('curl')) { |
|
| 97 | 97 | echo '<div class="error notice"> |
| 98 | 98 | <p>' . esc_html__('Algolia Woo Indexer requires the "cURL" PHP extension to be enabled. Please contact your hosting provider.', 'algolia-woo-indexer') . '</p> |
| 99 | 99 | </div>'; |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | * Include plugin file if function is_plugin_active does not exist |
| 22 | 22 | */ |
| 23 | 23 | if (! function_exists('is_plugin_active')) { |
| 24 | - require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
| 24 | + require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -44,162 +44,162 @@ discard block |
||
| 44 | 44 | define('ALGOLIA_API_KEY', '_admin_api_key'); |
| 45 | 45 | |
| 46 | 46 | if (! class_exists('Algolia_Send_Products')) { |
| 47 | - /** |
|
| 48 | - * Algolia WooIndexer main class |
|
| 49 | - */ |
|
| 50 | - class Algolia_Send_Products |
|
| 51 | - { |
|
| 52 | - const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
| 53 | - const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The Algolia instance |
|
| 57 | - * |
|
| 58 | - * @var \Algolia\AlgoliaSearch\SearchClient |
|
| 59 | - */ |
|
| 60 | - private static $algolia = null; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Check if we can connect to Algolia, if not, handle the exception, display an error and then return |
|
| 64 | - */ |
|
| 65 | - public static function can_connect_to_algolia() |
|
| 66 | - { |
|
| 67 | - try { |
|
| 68 | - self::$algolia->listApiKeys(); |
|
| 69 | - } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
|
| 70 | - add_action( |
|
| 71 | - 'admin_notices', |
|
| 72 | - function () { |
|
| 73 | - echo '<div class="error notice"> |
|
| 47 | + /** |
|
| 48 | + * Algolia WooIndexer main class |
|
| 49 | + */ |
|
| 50 | + class Algolia_Send_Products |
|
| 51 | + { |
|
| 52 | + const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
| 53 | + const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The Algolia instance |
|
| 57 | + * |
|
| 58 | + * @var \Algolia\AlgoliaSearch\SearchClient |
|
| 59 | + */ |
|
| 60 | + private static $algolia = null; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Check if we can connect to Algolia, if not, handle the exception, display an error and then return |
|
| 64 | + */ |
|
| 65 | + public static function can_connect_to_algolia() |
|
| 66 | + { |
|
| 67 | + try { |
|
| 68 | + self::$algolia->listApiKeys(); |
|
| 69 | + } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
|
| 70 | + add_action( |
|
| 71 | + 'admin_notices', |
|
| 72 | + function () { |
|
| 73 | + echo '<div class="error notice"> |
|
| 74 | 74 | <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> |
| 75 | 75 | </div>'; |
| 76 | - } |
|
| 77 | - ); |
|
| 78 | - return; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Send WooCommerce products to Algolia |
|
| 84 | - * |
|
| 85 | - * @param Int $id Product to send to Algolia if we send only a single product |
|
| 86 | - * @return void |
|
| 87 | - */ |
|
| 88 | - public static function send_products_to_algolia($id = '') |
|
| 89 | - { |
|
| 90 | - /** |
|
| 91 | - * Remove classes from plugin URL and autoload Algolia with Composer |
|
| 92 | - */ |
|
| 93 | - |
|
| 94 | - $base_plugin_directory = str_replace('classes', '', dirname(__FILE__)); |
|
| 95 | - require_once $base_plugin_directory . '/vendor/autoload.php'; |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Fetch the required variables from the Settings API |
|
| 99 | - */ |
|
| 100 | - |
|
| 101 | - $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
| 102 | - $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME; |
|
| 103 | - |
|
| 104 | - $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
| 105 | - $algolia_api_key = is_string($algolia_api_key) ?$algolia_api_key : CHANGE_ME; |
|
| 106 | - |
|
| 107 | - $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
| 108 | - $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Display admin notice and return if not all values have been set |
|
| 112 | - */ |
|
| 113 | - |
|
| 114 | - Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name); |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Initiate the Algolia client |
|
| 118 | - */ |
|
| 119 | - self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key); |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Check if we can connect, if not, handle the exception, display an error and then return |
|
| 123 | - */ |
|
| 124 | - self::can_connect_to_algolia(); |
|
| 76 | + } |
|
| 77 | + ); |
|
| 78 | + return; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Send WooCommerce products to Algolia |
|
| 84 | + * |
|
| 85 | + * @param Int $id Product to send to Algolia if we send only a single product |
|
| 86 | + * @return void |
|
| 87 | + */ |
|
| 88 | + public static function send_products_to_algolia($id = '') |
|
| 89 | + { |
|
| 90 | + /** |
|
| 91 | + * Remove classes from plugin URL and autoload Algolia with Composer |
|
| 92 | + */ |
|
| 93 | + |
|
| 94 | + $base_plugin_directory = str_replace('classes', '', dirname(__FILE__)); |
|
| 95 | + require_once $base_plugin_directory . '/vendor/autoload.php'; |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Fetch the required variables from the Settings API |
|
| 99 | + */ |
|
| 100 | + |
|
| 101 | + $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
| 102 | + $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME; |
|
| 103 | + |
|
| 104 | + $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
| 105 | + $algolia_api_key = is_string($algolia_api_key) ?$algolia_api_key : CHANGE_ME; |
|
| 106 | + |
|
| 107 | + $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
| 108 | + $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Display admin notice and return if not all values have been set |
|
| 112 | + */ |
|
| 113 | + |
|
| 114 | + Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name); |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Initiate the Algolia client |
|
| 118 | + */ |
|
| 119 | + self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key); |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Check if we can connect, if not, handle the exception, display an error and then return |
|
| 123 | + */ |
|
| 124 | + self::can_connect_to_algolia(); |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Initialize the search index and set the name to the option from the database |
|
| 128 | - */ |
|
| 129 | - $index = self::$algolia->initIndex($algolia_index_name); |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Setup arguments for sending all products to Algolia |
|
| 133 | - * |
|
| 134 | - * Limit => -1 means we send all products |
|
| 135 | - */ |
|
| 136 | - $arguments = array( |
|
| 137 | - 'status' => 'publish', |
|
| 138 | - 'limit' => -1, |
|
| 139 | - 'paginate' => false, |
|
| 140 | - ); |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Setup arguments for sending only a single product |
|
| 144 | - */ |
|
| 145 | - if (isset($id) && '' !== $id) { |
|
| 146 | - $arguments = array( |
|
| 147 | - 'status' => 'publish', |
|
| 148 | - 'include' => array( $id ), |
|
| 149 | - 'paginate' => false, |
|
| 150 | - ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Fetch all products from WooCommerce |
|
| 155 | - * |
|
| 156 | - * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html |
|
| 157 | - */ |
|
| 158 | - $products = /** @scrutinizer ignore-call */ wc_get_products($arguments); |
|
| 159 | - |
|
| 160 | - if (empty($products)) { |
|
| 161 | - return; |
|
| 162 | - } |
|
| 163 | - $records = array(); |
|
| 164 | - $record = array(); |
|
| 165 | - |
|
| 166 | - foreach ($products as $product) { |
|
| 167 | - /** |
|
| 168 | - * Extract image from $product->get_image() |
|
| 169 | - */ |
|
| 170 | - preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result); |
|
| 171 | - $product_image = array_pop($result); |
|
| 172 | - /** |
|
| 173 | - * Build the record array using the information from the WooCommerce product |
|
| 174 | - */ |
|
| 175 | - $record['objectID'] = $product->get_id(); |
|
| 176 | - $record['product_name'] = $product->get_name(); |
|
| 177 | - $record['product_image'] = $product_image; |
|
| 178 | - $record['short_description'] = $product->get_short_description(); |
|
| 179 | - $record['regular_price'] = $product->get_regular_price(); |
|
| 180 | - $record['sale_price'] = $product->get_sale_price(); |
|
| 181 | - $record['on_sale'] = $product->is_on_sale(); |
|
| 182 | - |
|
| 183 | - $records[] = $record; |
|
| 184 | - } |
|
| 185 | - wp_reset_postdata(); |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Send the information to Algolia and save the result |
|
| 189 | - * If result is NullResponse, print an error message |
|
| 190 | - */ |
|
| 191 | - $result = $index->saveObjects($records); |
|
| 192 | - |
|
| 193 | - if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) { |
|
| 194 | - wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings')); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Display success message |
|
| 199 | - */ |
|
| 200 | - echo '<div class="notice notice-success is-dismissible"> |
|
| 126 | + /** |
|
| 127 | + * Initialize the search index and set the name to the option from the database |
|
| 128 | + */ |
|
| 129 | + $index = self::$algolia->initIndex($algolia_index_name); |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Setup arguments for sending all products to Algolia |
|
| 133 | + * |
|
| 134 | + * Limit => -1 means we send all products |
|
| 135 | + */ |
|
| 136 | + $arguments = array( |
|
| 137 | + 'status' => 'publish', |
|
| 138 | + 'limit' => -1, |
|
| 139 | + 'paginate' => false, |
|
| 140 | + ); |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Setup arguments for sending only a single product |
|
| 144 | + */ |
|
| 145 | + if (isset($id) && '' !== $id) { |
|
| 146 | + $arguments = array( |
|
| 147 | + 'status' => 'publish', |
|
| 148 | + 'include' => array( $id ), |
|
| 149 | + 'paginate' => false, |
|
| 150 | + ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Fetch all products from WooCommerce |
|
| 155 | + * |
|
| 156 | + * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html |
|
| 157 | + */ |
|
| 158 | + $products = /** @scrutinizer ignore-call */ wc_get_products($arguments); |
|
| 159 | + |
|
| 160 | + if (empty($products)) { |
|
| 161 | + return; |
|
| 162 | + } |
|
| 163 | + $records = array(); |
|
| 164 | + $record = array(); |
|
| 165 | + |
|
| 166 | + foreach ($products as $product) { |
|
| 167 | + /** |
|
| 168 | + * Extract image from $product->get_image() |
|
| 169 | + */ |
|
| 170 | + preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result); |
|
| 171 | + $product_image = array_pop($result); |
|
| 172 | + /** |
|
| 173 | + * Build the record array using the information from the WooCommerce product |
|
| 174 | + */ |
|
| 175 | + $record['objectID'] = $product->get_id(); |
|
| 176 | + $record['product_name'] = $product->get_name(); |
|
| 177 | + $record['product_image'] = $product_image; |
|
| 178 | + $record['short_description'] = $product->get_short_description(); |
|
| 179 | + $record['regular_price'] = $product->get_regular_price(); |
|
| 180 | + $record['sale_price'] = $product->get_sale_price(); |
|
| 181 | + $record['on_sale'] = $product->is_on_sale(); |
|
| 182 | + |
|
| 183 | + $records[] = $record; |
|
| 184 | + } |
|
| 185 | + wp_reset_postdata(); |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Send the information to Algolia and save the result |
|
| 189 | + * If result is NullResponse, print an error message |
|
| 190 | + */ |
|
| 191 | + $result = $index->saveObjects($records); |
|
| 192 | + |
|
| 193 | + if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) { |
|
| 194 | + wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings')); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Display success message |
|
| 199 | + */ |
|
| 200 | + echo '<div class="notice notice-success is-dismissible"> |
|
| 201 | 201 | <p>' . esc_html__('Product(s) sent to Algolia.', 'algolia-woo-indexer') . '</p> |
| 202 | 202 | </div>'; |
| 203 | - } |
|
| 204 | - } |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | 205 | } |
| 206 | 206 | \ No newline at end of file |
@@ -13,27 +13,27 @@ discard block |
||
| 13 | 13 | /** |
| 14 | 14 | * Abort if this file is called directly |
| 15 | 15 | */ |
| 16 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 16 | +if ( ! defined('ABSPATH')) { |
|
| 17 | 17 | exit; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Include plugin file if function is_plugin_active does not exist |
| 22 | 22 | */ |
| 23 | -if (! function_exists('is_plugin_active')) { |
|
| 23 | +if ( ! function_exists('is_plugin_active')) { |
|
| 24 | 24 | require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * Define the plugin version and the database table name |
| 29 | 29 | */ |
| 30 | -define( 'ALGOWOO_DB_OPTION', '_algolia_woo_indexer' ); |
|
| 31 | -define( 'ALGOWOO_CURRENT_DB_VERSION', '0.3' ); |
|
| 30 | +define('ALGOWOO_DB_OPTION', '_algolia_woo_indexer'); |
|
| 31 | +define('ALGOWOO_CURRENT_DB_VERSION', '0.3'); |
|
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * Define application constants |
| 35 | 35 | */ |
| 36 | -define( 'CHANGE_ME', 'change me' ); |
|
| 36 | +define('CHANGE_ME', 'change me'); |
|
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * Database table names |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | define('ALGOLIA_APPLICATION_ID', '_application_id'); |
| 44 | 44 | define('ALGOLIA_API_KEY', '_admin_api_key'); |
| 45 | 45 | |
| 46 | -if (! class_exists('Algolia_Send_Products')) { |
|
| 46 | +if ( ! class_exists('Algolia_Send_Products')) { |
|
| 47 | 47 | /** |
| 48 | 48 | * Algolia WooIndexer main class |
| 49 | 49 | */ |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
| 70 | 70 | add_action( |
| 71 | 71 | 'admin_notices', |
| 72 | - function () { |
|
| 72 | + function() { |
|
| 73 | 73 | echo '<div class="error notice"> |
| 74 | 74 | <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> |
| 75 | 75 | </div>'; |
@@ -102,10 +102,10 @@ discard block |
||
| 102 | 102 | $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME; |
| 103 | 103 | |
| 104 | 104 | $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
| 105 | - $algolia_api_key = is_string($algolia_api_key) ?$algolia_api_key : CHANGE_ME; |
|
| 105 | + $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
|
| 106 | 106 | |
| 107 | 107 | $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
| 108 | - $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
| 108 | + $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
| 109 | 109 | |
| 110 | 110 | /** |
| 111 | 111 | * Display admin notice and return if not all values have been set |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | if (isset($id) && '' !== $id) { |
| 146 | 146 | $arguments = array( |
| 147 | 147 | 'status' => 'publish', |
| 148 | - 'include' => array( $id ), |
|
| 148 | + 'include' => array($id), |
|
| 149 | 149 | 'paginate' => false, |
| 150 | 150 | ); |
| 151 | 151 | } |
@@ -23,425 +23,425 @@ discard block |
||
| 23 | 23 | * Include plugin file if function is_plugin_active does not exist |
| 24 | 24 | */ |
| 25 | 25 | if (! function_exists('is_plugin_active')) { |
| 26 | - require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
| 26 | + require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | if (! class_exists('Algolia_Woo_Indexer')) { |
| 30 | - /** |
|
| 31 | - * Algolia WooIndexer main class |
|
| 32 | - */ |
|
| 33 | - class Algolia_Woo_Indexer |
|
| 34 | - { |
|
| 35 | - const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
| 36 | - const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Class instance |
|
| 40 | - * |
|
| 41 | - * @var object |
|
| 42 | - */ |
|
| 43 | - private static $instance; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The plugin URL |
|
| 47 | - * |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - private static $plugin_url = ''; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The Algolia instance |
|
| 54 | - * |
|
| 55 | - * @var \Algolia\AlgoliaSearch\SearchClient |
|
| 56 | - */ |
|
| 57 | - private static $algolia = null; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Class constructor |
|
| 61 | - * |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - public function __construct() |
|
| 65 | - { |
|
| 66 | - $this->init(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Setup sections and fields to store and retrieve values from Settings API |
|
| 71 | - * |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - public static function setup_settings_sections() |
|
| 75 | - { |
|
| 76 | - /** |
|
| 77 | - * Setup arguments for settings sections and fields |
|
| 78 | - * |
|
| 79 | - * @see https://developer.wordpress.org/reference/functions/register_setting/ |
|
| 80 | - */ |
|
| 81 | - if (is_admin()) { |
|
| 82 | - $arguments = array( |
|
| 83 | - 'type' => 'string', |
|
| 84 | - 'sanitize_callback' => 'settings_fields_validate_options', |
|
| 85 | - 'default' => null, |
|
| 86 | - ); |
|
| 87 | - register_setting('algolia_woo_options', 'algolia_woo_options', $arguments); |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Make sure we reference the instance of the current class by using self::get_instance() |
|
| 91 | - * This way we can setup the correct callback function for add_settings_section and add_settings_field |
|
| 92 | - */ |
|
| 93 | - $algowooindexer = self::get_instance(); |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Add our necessary settings sections and fields |
|
| 97 | - */ |
|
| 98 | - add_settings_section( |
|
| 99 | - 'algolia_woo_indexer_main', |
|
| 100 | - esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
|
| 101 | - array( $algowooindexer, 'algolia_woo_indexer_section_text' ), |
|
| 102 | - 'algolia_woo_indexer' |
|
| 103 | - ); |
|
| 104 | - add_settings_field( |
|
| 105 | - 'algolia_woo_indexer_application_id', |
|
| 106 | - esc_html__('Application ID', 'algolia-woo-indexer'), |
|
| 107 | - array( $algowooindexer, 'algolia_woo_indexer_application_id_output' ), |
|
| 108 | - 'algolia_woo_indexer', |
|
| 109 | - 'algolia_woo_indexer_main' |
|
| 110 | - ); |
|
| 111 | - add_settings_field( |
|
| 112 | - 'algolia_woo_indexer_admin_api_key', |
|
| 113 | - esc_html__('Admin API Key', 'algolia-woo-indexer'), |
|
| 114 | - array( $algowooindexer, 'algolia_woo_indexer_admin_api_key_output' ), |
|
| 115 | - 'algolia_woo_indexer', |
|
| 116 | - 'algolia_woo_indexer_main' |
|
| 117 | - ); |
|
| 118 | - add_settings_field( |
|
| 119 | - 'algolia_woo_indexer_index_name', |
|
| 120 | - esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
|
| 121 | - array( $algowooindexer, 'algolia_woo_indexer_index_name_output' ), |
|
| 122 | - 'algolia_woo_indexer', |
|
| 123 | - 'algolia_woo_indexer_main' |
|
| 124 | - ); |
|
| 125 | - add_settings_field( |
|
| 126 | - 'algolia_woo_indexer_automatically_send_new_products', |
|
| 127 | - esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
|
| 128 | - array( $algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output' ), |
|
| 129 | - 'algolia_woo_indexer', |
|
| 130 | - 'algolia_woo_indexer_main' |
|
| 131 | - ); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Output for admin API key field |
|
| 137 | - * |
|
| 138 | - * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ |
|
| 139 | - * |
|
| 140 | - * @return void |
|
| 141 | - */ |
|
| 142 | - public static function algolia_woo_indexer_admin_api_key_output() |
|
| 143 | - { |
|
| 144 | - $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
| 145 | - $api_key = is_string($api_key) ? $api_key : CHANGE_ME; |
|
| 146 | - |
|
| 147 | - wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name'); |
|
| 148 | - |
|
| 149 | - echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]' |
|
| 30 | + /** |
|
| 31 | + * Algolia WooIndexer main class |
|
| 32 | + */ |
|
| 33 | + class Algolia_Woo_Indexer |
|
| 34 | + { |
|
| 35 | + const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
| 36 | + const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Class instance |
|
| 40 | + * |
|
| 41 | + * @var object |
|
| 42 | + */ |
|
| 43 | + private static $instance; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The plugin URL |
|
| 47 | + * |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + private static $plugin_url = ''; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The Algolia instance |
|
| 54 | + * |
|
| 55 | + * @var \Algolia\AlgoliaSearch\SearchClient |
|
| 56 | + */ |
|
| 57 | + private static $algolia = null; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Class constructor |
|
| 61 | + * |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + public function __construct() |
|
| 65 | + { |
|
| 66 | + $this->init(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Setup sections and fields to store and retrieve values from Settings API |
|
| 71 | + * |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + public static function setup_settings_sections() |
|
| 75 | + { |
|
| 76 | + /** |
|
| 77 | + * Setup arguments for settings sections and fields |
|
| 78 | + * |
|
| 79 | + * @see https://developer.wordpress.org/reference/functions/register_setting/ |
|
| 80 | + */ |
|
| 81 | + if (is_admin()) { |
|
| 82 | + $arguments = array( |
|
| 83 | + 'type' => 'string', |
|
| 84 | + 'sanitize_callback' => 'settings_fields_validate_options', |
|
| 85 | + 'default' => null, |
|
| 86 | + ); |
|
| 87 | + register_setting('algolia_woo_options', 'algolia_woo_options', $arguments); |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Make sure we reference the instance of the current class by using self::get_instance() |
|
| 91 | + * This way we can setup the correct callback function for add_settings_section and add_settings_field |
|
| 92 | + */ |
|
| 93 | + $algowooindexer = self::get_instance(); |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Add our necessary settings sections and fields |
|
| 97 | + */ |
|
| 98 | + add_settings_section( |
|
| 99 | + 'algolia_woo_indexer_main', |
|
| 100 | + esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
|
| 101 | + array( $algowooindexer, 'algolia_woo_indexer_section_text' ), |
|
| 102 | + 'algolia_woo_indexer' |
|
| 103 | + ); |
|
| 104 | + add_settings_field( |
|
| 105 | + 'algolia_woo_indexer_application_id', |
|
| 106 | + esc_html__('Application ID', 'algolia-woo-indexer'), |
|
| 107 | + array( $algowooindexer, 'algolia_woo_indexer_application_id_output' ), |
|
| 108 | + 'algolia_woo_indexer', |
|
| 109 | + 'algolia_woo_indexer_main' |
|
| 110 | + ); |
|
| 111 | + add_settings_field( |
|
| 112 | + 'algolia_woo_indexer_admin_api_key', |
|
| 113 | + esc_html__('Admin API Key', 'algolia-woo-indexer'), |
|
| 114 | + array( $algowooindexer, 'algolia_woo_indexer_admin_api_key_output' ), |
|
| 115 | + 'algolia_woo_indexer', |
|
| 116 | + 'algolia_woo_indexer_main' |
|
| 117 | + ); |
|
| 118 | + add_settings_field( |
|
| 119 | + 'algolia_woo_indexer_index_name', |
|
| 120 | + esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
|
| 121 | + array( $algowooindexer, 'algolia_woo_indexer_index_name_output' ), |
|
| 122 | + 'algolia_woo_indexer', |
|
| 123 | + 'algolia_woo_indexer_main' |
|
| 124 | + ); |
|
| 125 | + add_settings_field( |
|
| 126 | + 'algolia_woo_indexer_automatically_send_new_products', |
|
| 127 | + esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
|
| 128 | + array( $algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output' ), |
|
| 129 | + 'algolia_woo_indexer', |
|
| 130 | + 'algolia_woo_indexer_main' |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Output for admin API key field |
|
| 137 | + * |
|
| 138 | + * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ |
|
| 139 | + * |
|
| 140 | + * @return void |
|
| 141 | + */ |
|
| 142 | + public static function algolia_woo_indexer_admin_api_key_output() |
|
| 143 | + { |
|
| 144 | + $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
| 145 | + $api_key = is_string($api_key) ? $api_key : CHANGE_ME; |
|
| 146 | + |
|
| 147 | + wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name'); |
|
| 148 | + |
|
| 149 | + echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]' |
|
| 150 | 150 | type='text' value='" . esc_attr($api_key) . "' />"; |
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Output for application ID field |
|
| 155 | - * |
|
| 156 | - * @return void |
|
| 157 | - */ |
|
| 158 | - public static function algolia_woo_indexer_application_id_output() |
|
| 159 | - { |
|
| 160 | - $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
| 161 | - $application_id = is_string($application_id) ? $application_id : CHANGE_ME; |
|
| 162 | - |
|
| 163 | - echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]' |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Output for application ID field |
|
| 155 | + * |
|
| 156 | + * @return void |
|
| 157 | + */ |
|
| 158 | + public static function algolia_woo_indexer_application_id_output() |
|
| 159 | + { |
|
| 160 | + $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
| 161 | + $application_id = is_string($application_id) ? $application_id : CHANGE_ME; |
|
| 162 | + |
|
| 163 | + echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]' |
|
| 164 | 164 | type='text' value='" . esc_attr($application_id) . "' />"; |
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Output for index name field |
|
| 169 | - * |
|
| 170 | - * @return void |
|
| 171 | - */ |
|
| 172 | - public static function algolia_woo_indexer_index_name_output() |
|
| 173 | - { |
|
| 174 | - $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
| 175 | - $index_name = is_string($index_name) ? $index_name : CHANGE_ME; |
|
| 176 | - |
|
| 177 | - echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]' |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Output for index name field |
|
| 169 | + * |
|
| 170 | + * @return void |
|
| 171 | + */ |
|
| 172 | + public static function algolia_woo_indexer_index_name_output() |
|
| 173 | + { |
|
| 174 | + $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
| 175 | + $index_name = is_string($index_name) ? $index_name : CHANGE_ME; |
|
| 176 | + |
|
| 177 | + echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]' |
|
| 178 | 178 | type='text' value='" . esc_attr($index_name) . "' />"; |
| 179 | - } |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Output for checkbox to check if we automatically send new products to Algolia |
|
| 183 | - * |
|
| 184 | - * @return void |
|
| 185 | - */ |
|
| 186 | - public static function algolia_woo_indexer_automatically_send_new_products_output() |
|
| 187 | - { |
|
| 188 | - /** |
|
| 189 | - * Sanitization is not really needed as the variable is not directly echoed |
|
| 190 | - * But I have still done it to be 100% safe |
|
| 191 | - */ |
|
| 192 | - $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
| 193 | - $automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; ?> |
|
| 181 | + /** |
|
| 182 | + * Output for checkbox to check if we automatically send new products to Algolia |
|
| 183 | + * |
|
| 184 | + * @return void |
|
| 185 | + */ |
|
| 186 | + public static function algolia_woo_indexer_automatically_send_new_products_output() |
|
| 187 | + { |
|
| 188 | + /** |
|
| 189 | + * Sanitization is not really needed as the variable is not directly echoed |
|
| 190 | + * But I have still done it to be 100% safe |
|
| 191 | + */ |
|
| 192 | + $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
| 193 | + $automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; ?> |
|
| 194 | 194 | <input id="algolia_woo_indexer_automatically_send_new_products" name="algolia_woo_indexer_automatically_send_new_products[checked]" |
| 195 | 195 | type="checkbox" <?php checked(1, $automatically_send_new_products); ?> /> |
| 196 | 196 | <?php |
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Section text for plugin settings section text |
|
| 201 | - * |
|
| 202 | - * @return void |
|
| 203 | - */ |
|
| 204 | - public static function algolia_woo_indexer_section_text() |
|
| 205 | - { |
|
| 206 | - echo esc_html__('Enter your settings here', 'algolia-woo-indexer'); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Check if we are going to send products by verifying send products nonce |
|
| 211 | - * |
|
| 212 | - * @return void |
|
| 213 | - */ |
|
| 214 | - public static function maybe_send_products() |
|
| 215 | - { |
|
| 216 | - if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
| 217 | - Algolia_Send_Products::send_products_to_algolia(); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Initialize class, setup settings sections and fields |
|
| 223 | - * |
|
| 224 | - * @return void |
|
| 225 | - */ |
|
| 226 | - public static function init() |
|
| 227 | - { |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Fetch the option to see if we are going to automatically send new products |
|
| 231 | - */ |
|
| 232 | - $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Check that we have the minimum versions required and all of the required PHP extensions |
|
| 236 | - */ |
|
| 237 | - Algolia_Check_Requirements::check_unmet_requirements(); |
|
| 238 | - |
|
| 239 | - if (! Algolia_Check_Requirements::algolia_wp_version_check() || ! Algolia_Check_Requirements::algolia_php_version_check()) { |
|
| 240 | - add_action( |
|
| 241 | - 'admin_notices', |
|
| 242 | - function () { |
|
| 243 | - echo '<div class="error notice"> |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Section text for plugin settings section text |
|
| 201 | + * |
|
| 202 | + * @return void |
|
| 203 | + */ |
|
| 204 | + public static function algolia_woo_indexer_section_text() |
|
| 205 | + { |
|
| 206 | + echo esc_html__('Enter your settings here', 'algolia-woo-indexer'); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Check if we are going to send products by verifying send products nonce |
|
| 211 | + * |
|
| 212 | + * @return void |
|
| 213 | + */ |
|
| 214 | + public static function maybe_send_products() |
|
| 215 | + { |
|
| 216 | + if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
| 217 | + Algolia_Send_Products::send_products_to_algolia(); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Initialize class, setup settings sections and fields |
|
| 223 | + * |
|
| 224 | + * @return void |
|
| 225 | + */ |
|
| 226 | + public static function init() |
|
| 227 | + { |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Fetch the option to see if we are going to automatically send new products |
|
| 231 | + */ |
|
| 232 | + $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Check that we have the minimum versions required and all of the required PHP extensions |
|
| 236 | + */ |
|
| 237 | + Algolia_Check_Requirements::check_unmet_requirements(); |
|
| 238 | + |
|
| 239 | + if (! Algolia_Check_Requirements::algolia_wp_version_check() || ! Algolia_Check_Requirements::algolia_php_version_check()) { |
|
| 240 | + add_action( |
|
| 241 | + 'admin_notices', |
|
| 242 | + function () { |
|
| 243 | + echo '<div class="error notice"> |
|
| 244 | 244 | <p>' . esc_html__('Please check the server requirements for Algolia Woo Indexer. <br/> It requires minimum PHP version 7.2 and WordPress version 5.0', 'algolia-woo-indexer') . '</p> |
| 245 | 245 | </div>'; |
| 246 | - } |
|
| 247 | - ); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - $ob_class = get_called_class(); |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Setup translations |
|
| 254 | - */ |
|
| 255 | - add_action('plugins_loaded', array( $ob_class, 'load_textdomain' )); |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Add actions to setup admin menu |
|
| 259 | - */ |
|
| 260 | - if (is_admin()) { |
|
| 261 | - add_action('admin_menu', array( $ob_class, 'admin_menu' )); |
|
| 262 | - add_action('admin_init', array( $ob_class, 'setup_settings_sections' )); |
|
| 263 | - add_action('admin_init', array( $ob_class, 'update_settings_options' )); |
|
| 264 | - add_action('admin_init', array( $ob_class, 'maybe_send_products' )); |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Register hook to automatically send new products if the option is set |
|
| 268 | - */ |
|
| 269 | - |
|
| 270 | - if ('1' === $automatically_send_new_products) { |
|
| 271 | - add_action('save_post', array( $ob_class, 'send_new_product_to_algolia' ), 10, 3); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
|
| 275 | - |
|
| 276 | - if (! is_plugin_active('woocommerce/woocommerce.php')) { |
|
| 277 | - add_action( |
|
| 278 | - 'admin_notices', |
|
| 279 | - function () { |
|
| 280 | - echo '<div class="error notice"> |
|
| 246 | + } |
|
| 247 | + ); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + $ob_class = get_called_class(); |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Setup translations |
|
| 254 | + */ |
|
| 255 | + add_action('plugins_loaded', array( $ob_class, 'load_textdomain' )); |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Add actions to setup admin menu |
|
| 259 | + */ |
|
| 260 | + if (is_admin()) { |
|
| 261 | + add_action('admin_menu', array( $ob_class, 'admin_menu' )); |
|
| 262 | + add_action('admin_init', array( $ob_class, 'setup_settings_sections' )); |
|
| 263 | + add_action('admin_init', array( $ob_class, 'update_settings_options' )); |
|
| 264 | + add_action('admin_init', array( $ob_class, 'maybe_send_products' )); |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Register hook to automatically send new products if the option is set |
|
| 268 | + */ |
|
| 269 | + |
|
| 270 | + if ('1' === $automatically_send_new_products) { |
|
| 271 | + add_action('save_post', array( $ob_class, 'send_new_product_to_algolia' ), 10, 3); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
|
| 275 | + |
|
| 276 | + if (! is_plugin_active('woocommerce/woocommerce.php')) { |
|
| 277 | + add_action( |
|
| 278 | + 'admin_notices', |
|
| 279 | + function () { |
|
| 280 | + echo '<div class="error notice"> |
|
| 281 | 281 | <p>' . esc_html__('WooCommerce plugin must be enabled for Algolia Woo Indexer to work.', 'algolia-woo-indexer') . '</p> |
| 282 | 282 | </div>'; |
| 283 | - } |
|
| 284 | - ); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Send a single product to Algolia once a new product has been published |
|
| 291 | - * |
|
| 292 | - * @param int $post_id ID of the product. |
|
| 293 | - * @param array $post Post array. |
|
| 294 | - * |
|
| 295 | - * @return void |
|
| 296 | - */ |
|
| 297 | - public static function send_new_product_to_algolia($post_id, $post) |
|
| 298 | - { |
|
| 299 | - if ('publish' !== $post->post_status || 'product' !== $post->post_type) { |
|
| 300 | - return; |
|
| 301 | - } |
|
| 302 | - self::send_products_to_algolia($post_id); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Verify nonces before we update options and settings |
|
| 307 | - * Also retrieve the value from the send_products_to_algolia hidden field to check if we are sending products to Algolia |
|
| 308 | - * |
|
| 309 | - * @return void |
|
| 310 | - */ |
|
| 311 | - public static function update_settings_options() |
|
| 312 | - { |
|
| 313 | - Algolia_Verify_Nonces::verify_settings_nonce(); |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Do not proceed if we are going to send products |
|
| 317 | - */ |
|
| 318 | - if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
| 319 | - return; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Filter the application id, api key, index name and verify that the input is an array |
|
| 324 | - * |
|
| 325 | - * @see https://www.php.net/manual/en/function.filter-input.php |
|
| 326 | - */ |
|
| 327 | - $post_application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 328 | - $post_api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 329 | - $post_index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 330 | - $automatically_send_new_products = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Properly sanitize text fields before updating data |
|
| 334 | - * |
|
| 335 | - * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
| 336 | - */ |
|
| 337 | - $filtered_application_id = sanitize_text_field($post_application_id['id']); |
|
| 338 | - $filtered_api_key = sanitize_text_field($post_api_key['key']); |
|
| 339 | - $filtered_index_name = sanitize_text_field($post_index_name['name']); |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Sanitizing by setting the value to either 1 or 0 |
|
| 343 | - */ |
|
| 344 | - $filtered_automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Values have been filtered and sanitized |
|
| 348 | - * Check if set and not empty and update the database |
|
| 349 | - * |
|
| 350 | - * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
| 351 | - */ |
|
| 352 | - if (isset($filtered_application_id) && (! empty($filtered_application_id))) { |
|
| 353 | - update_option( |
|
| 354 | - ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID, |
|
| 355 | - $filtered_application_id |
|
| 356 | - ); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - if (isset($filtered_api_key) && (! empty($filtered_api_key))) { |
|
| 360 | - update_option( |
|
| 361 | - ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
| 362 | - $filtered_api_key |
|
| 363 | - ); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - if (isset($filtered_index_name) && (! empty($filtered_index_name))) { |
|
| 367 | - update_option( |
|
| 368 | - ALGOWOO_DB_OPTION . INDEX_NAME, |
|
| 369 | - $filtered_index_name |
|
| 370 | - ); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - if (isset($filtered_automatically_send_new_products) && (! empty($filtered_automatically_send_new_products))) { |
|
| 374 | - update_option( |
|
| 375 | - ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
| 376 | - $filtered_automatically_send_new_products |
|
| 377 | - ); |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z |
|
| 383 | - * |
|
| 384 | - * @param string $input Settings text data |
|
| 385 | - * @return array |
|
| 386 | - */ |
|
| 387 | - public static function settings_fields_validate_options($input) |
|
| 388 | - { |
|
| 389 | - $valid = array(); |
|
| 390 | - $valid['name'] = preg_replace( |
|
| 391 | - '/[^a-zA-Z\s]/', |
|
| 392 | - '', |
|
| 393 | - $input['name'] |
|
| 394 | - ); |
|
| 395 | - return $valid; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Load text domain for translations |
|
| 400 | - * |
|
| 401 | - * @return void |
|
| 402 | - */ |
|
| 403 | - public static function load_textdomain() |
|
| 404 | - { |
|
| 405 | - load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/'); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Add the new menu to settings section so that we can configure the plugin |
|
| 410 | - * |
|
| 411 | - * @return void |
|
| 412 | - */ |
|
| 413 | - public static function admin_menu() |
|
| 414 | - { |
|
| 415 | - add_submenu_page( |
|
| 416 | - 'options-general.php', |
|
| 417 | - esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
| 418 | - esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
| 419 | - 'manage_options', |
|
| 420 | - 'algolia-woo-indexer-settings', |
|
| 421 | - array( get_called_class(), 'algolia_woo_indexer_settings' ) |
|
| 422 | - ); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * Display settings and allow user to modify them |
|
| 427 | - * |
|
| 428 | - * @return void |
|
| 429 | - */ |
|
| 430 | - public static function algolia_woo_indexer_settings() |
|
| 431 | - { |
|
| 432 | - /** |
|
| 433 | - * Verify that the user can access the settings page |
|
| 434 | - */ |
|
| 435 | - if (! current_user_can('manage_options')) { |
|
| 436 | - wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
|
| 437 | - } ?> |
|
| 283 | + } |
|
| 284 | + ); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Send a single product to Algolia once a new product has been published |
|
| 291 | + * |
|
| 292 | + * @param int $post_id ID of the product. |
|
| 293 | + * @param array $post Post array. |
|
| 294 | + * |
|
| 295 | + * @return void |
|
| 296 | + */ |
|
| 297 | + public static function send_new_product_to_algolia($post_id, $post) |
|
| 298 | + { |
|
| 299 | + if ('publish' !== $post->post_status || 'product' !== $post->post_type) { |
|
| 300 | + return; |
|
| 301 | + } |
|
| 302 | + self::send_products_to_algolia($post_id); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Verify nonces before we update options and settings |
|
| 307 | + * Also retrieve the value from the send_products_to_algolia hidden field to check if we are sending products to Algolia |
|
| 308 | + * |
|
| 309 | + * @return void |
|
| 310 | + */ |
|
| 311 | + public static function update_settings_options() |
|
| 312 | + { |
|
| 313 | + Algolia_Verify_Nonces::verify_settings_nonce(); |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Do not proceed if we are going to send products |
|
| 317 | + */ |
|
| 318 | + if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
| 319 | + return; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Filter the application id, api key, index name and verify that the input is an array |
|
| 324 | + * |
|
| 325 | + * @see https://www.php.net/manual/en/function.filter-input.php |
|
| 326 | + */ |
|
| 327 | + $post_application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 328 | + $post_api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 329 | + $post_index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 330 | + $automatically_send_new_products = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Properly sanitize text fields before updating data |
|
| 334 | + * |
|
| 335 | + * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
| 336 | + */ |
|
| 337 | + $filtered_application_id = sanitize_text_field($post_application_id['id']); |
|
| 338 | + $filtered_api_key = sanitize_text_field($post_api_key['key']); |
|
| 339 | + $filtered_index_name = sanitize_text_field($post_index_name['name']); |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Sanitizing by setting the value to either 1 or 0 |
|
| 343 | + */ |
|
| 344 | + $filtered_automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Values have been filtered and sanitized |
|
| 348 | + * Check if set and not empty and update the database |
|
| 349 | + * |
|
| 350 | + * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
| 351 | + */ |
|
| 352 | + if (isset($filtered_application_id) && (! empty($filtered_application_id))) { |
|
| 353 | + update_option( |
|
| 354 | + ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID, |
|
| 355 | + $filtered_application_id |
|
| 356 | + ); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + if (isset($filtered_api_key) && (! empty($filtered_api_key))) { |
|
| 360 | + update_option( |
|
| 361 | + ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
| 362 | + $filtered_api_key |
|
| 363 | + ); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + if (isset($filtered_index_name) && (! empty($filtered_index_name))) { |
|
| 367 | + update_option( |
|
| 368 | + ALGOWOO_DB_OPTION . INDEX_NAME, |
|
| 369 | + $filtered_index_name |
|
| 370 | + ); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + if (isset($filtered_automatically_send_new_products) && (! empty($filtered_automatically_send_new_products))) { |
|
| 374 | + update_option( |
|
| 375 | + ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
| 376 | + $filtered_automatically_send_new_products |
|
| 377 | + ); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z |
|
| 383 | + * |
|
| 384 | + * @param string $input Settings text data |
|
| 385 | + * @return array |
|
| 386 | + */ |
|
| 387 | + public static function settings_fields_validate_options($input) |
|
| 388 | + { |
|
| 389 | + $valid = array(); |
|
| 390 | + $valid['name'] = preg_replace( |
|
| 391 | + '/[^a-zA-Z\s]/', |
|
| 392 | + '', |
|
| 393 | + $input['name'] |
|
| 394 | + ); |
|
| 395 | + return $valid; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Load text domain for translations |
|
| 400 | + * |
|
| 401 | + * @return void |
|
| 402 | + */ |
|
| 403 | + public static function load_textdomain() |
|
| 404 | + { |
|
| 405 | + load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/'); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Add the new menu to settings section so that we can configure the plugin |
|
| 410 | + * |
|
| 411 | + * @return void |
|
| 412 | + */ |
|
| 413 | + public static function admin_menu() |
|
| 414 | + { |
|
| 415 | + add_submenu_page( |
|
| 416 | + 'options-general.php', |
|
| 417 | + esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
| 418 | + esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
| 419 | + 'manage_options', |
|
| 420 | + 'algolia-woo-indexer-settings', |
|
| 421 | + array( get_called_class(), 'algolia_woo_indexer_settings' ) |
|
| 422 | + ); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * Display settings and allow user to modify them |
|
| 427 | + * |
|
| 428 | + * @return void |
|
| 429 | + */ |
|
| 430 | + public static function algolia_woo_indexer_settings() |
|
| 431 | + { |
|
| 432 | + /** |
|
| 433 | + * Verify that the user can access the settings page |
|
| 434 | + */ |
|
| 435 | + if (! current_user_can('manage_options')) { |
|
| 436 | + wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
|
| 437 | + } ?> |
|
| 438 | 438 | <div class="wrap"> |
| 439 | 439 | <h1><?php esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'); ?></h1> |
| 440 | 440 | <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
| 441 | 441 | <?php |
| 442 | - settings_fields('algolia_woo_options'); |
|
| 443 | - do_settings_sections('algolia_woo_indexer'); |
|
| 444 | - submit_button('', 'primary wide'); ?> |
|
| 442 | + settings_fields('algolia_woo_options'); |
|
| 443 | + do_settings_sections('algolia_woo_indexer'); |
|
| 444 | + submit_button('', 'primary wide'); ?> |
|
| 445 | 445 | </form> |
| 446 | 446 | <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
| 447 | 447 | <?php wp_nonce_field('send_products_to_algolia_nonce_action', 'send_products_to_algolia_nonce_name'); ?> |
@@ -450,75 +450,75 @@ discard block |
||
| 450 | 450 | </form> |
| 451 | 451 | </div> |
| 452 | 452 | <?php |
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Get active object instance |
|
| 457 | - * |
|
| 458 | - * @return object |
|
| 459 | - */ |
|
| 460 | - public static function get_instance() |
|
| 461 | - { |
|
| 462 | - if (! self::$instance) { |
|
| 463 | - self::$instance = new Algolia_Woo_Indexer(); |
|
| 464 | - } |
|
| 465 | - return self::$instance; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * The actions to execute when the plugin is activated. |
|
| 470 | - * |
|
| 471 | - * @return void |
|
| 472 | - */ |
|
| 473 | - public static function activate_plugin() |
|
| 474 | - { |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Set default values for options if not already set |
|
| 478 | - */ |
|
| 479 | - $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
| 480 | - $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
| 481 | - $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
| 482 | - $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Get active object instance |
|
| 457 | + * |
|
| 458 | + * @return object |
|
| 459 | + */ |
|
| 460 | + public static function get_instance() |
|
| 461 | + { |
|
| 462 | + if (! self::$instance) { |
|
| 463 | + self::$instance = new Algolia_Woo_Indexer(); |
|
| 464 | + } |
|
| 465 | + return self::$instance; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * The actions to execute when the plugin is activated. |
|
| 470 | + * |
|
| 471 | + * @return void |
|
| 472 | + */ |
|
| 473 | + public static function activate_plugin() |
|
| 474 | + { |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Set default values for options if not already set |
|
| 478 | + */ |
|
| 479 | + $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
| 480 | + $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
| 481 | + $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
| 482 | + $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
| 483 | 483 | |
| 484 | - if (empty($automatically_send_new_products)) { |
|
| 485 | - add_option( |
|
| 486 | - ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
| 487 | - '0' |
|
| 488 | - ); |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - if (empty($algolia_application_id)) { |
|
| 492 | - add_option( |
|
| 493 | - ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID, |
|
| 494 | - 'Change me' |
|
| 495 | - ); |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - if (empty($algolia_api_key)) { |
|
| 499 | - add_option( |
|
| 500 | - ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
| 501 | - 'Change me' |
|
| 502 | - ); |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - if (empty($algolia_index_name)) { |
|
| 506 | - add_option( |
|
| 507 | - ALGOWOO_DB_OPTION . INDEX_NAME, |
|
| 508 | - 'Change me' |
|
| 509 | - ); |
|
| 510 | - } |
|
| 511 | - set_transient(self::PLUGIN_TRANSIENT, true); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * The actions to execute when the plugin is deactivated. |
|
| 516 | - * |
|
| 517 | - * @return void |
|
| 518 | - */ |
|
| 519 | - public static function deactivate_plugin() |
|
| 520 | - { |
|
| 521 | - delete_transient(self::PLUGIN_TRANSIENT); |
|
| 522 | - } |
|
| 523 | - } |
|
| 484 | + if (empty($automatically_send_new_products)) { |
|
| 485 | + add_option( |
|
| 486 | + ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
| 487 | + '0' |
|
| 488 | + ); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + if (empty($algolia_application_id)) { |
|
| 492 | + add_option( |
|
| 493 | + ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID, |
|
| 494 | + 'Change me' |
|
| 495 | + ); |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + if (empty($algolia_api_key)) { |
|
| 499 | + add_option( |
|
| 500 | + ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
| 501 | + 'Change me' |
|
| 502 | + ); |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + if (empty($algolia_index_name)) { |
|
| 506 | + add_option( |
|
| 507 | + ALGOWOO_DB_OPTION . INDEX_NAME, |
|
| 508 | + 'Change me' |
|
| 509 | + ); |
|
| 510 | + } |
|
| 511 | + set_transient(self::PLUGIN_TRANSIENT, true); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * The actions to execute when the plugin is deactivated. |
|
| 516 | + * |
|
| 517 | + * @return void |
|
| 518 | + */ |
|
| 519 | + public static function deactivate_plugin() |
|
| 520 | + { |
|
| 521 | + delete_transient(self::PLUGIN_TRANSIENT); |
|
| 522 | + } |
|
| 523 | + } |
|
| 524 | 524 | } |
| 525 | 525 | \ No newline at end of file |
@@ -15,18 +15,18 @@ discard block |
||
| 15 | 15 | /** |
| 16 | 16 | * Abort if this file is called directly |
| 17 | 17 | */ |
| 18 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 18 | +if ( ! defined('ABSPATH')) { |
|
| 19 | 19 | exit; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * Include plugin file if function is_plugin_active does not exist |
| 24 | 24 | */ |
| 25 | -if (! function_exists('is_plugin_active')) { |
|
| 25 | +if ( ! function_exists('is_plugin_active')) { |
|
| 26 | 26 | require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | -if (! class_exists('Algolia_Woo_Indexer')) { |
|
| 29 | +if ( ! class_exists('Algolia_Woo_Indexer')) { |
|
| 30 | 30 | /** |
| 31 | 31 | * Algolia WooIndexer main class |
| 32 | 32 | */ |
@@ -98,34 +98,34 @@ discard block |
||
| 98 | 98 | add_settings_section( |
| 99 | 99 | 'algolia_woo_indexer_main', |
| 100 | 100 | esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
| 101 | - array( $algowooindexer, 'algolia_woo_indexer_section_text' ), |
|
| 101 | + array($algowooindexer, 'algolia_woo_indexer_section_text'), |
|
| 102 | 102 | 'algolia_woo_indexer' |
| 103 | 103 | ); |
| 104 | 104 | add_settings_field( |
| 105 | 105 | 'algolia_woo_indexer_application_id', |
| 106 | 106 | esc_html__('Application ID', 'algolia-woo-indexer'), |
| 107 | - array( $algowooindexer, 'algolia_woo_indexer_application_id_output' ), |
|
| 107 | + array($algowooindexer, 'algolia_woo_indexer_application_id_output'), |
|
| 108 | 108 | 'algolia_woo_indexer', |
| 109 | 109 | 'algolia_woo_indexer_main' |
| 110 | 110 | ); |
| 111 | 111 | add_settings_field( |
| 112 | 112 | 'algolia_woo_indexer_admin_api_key', |
| 113 | 113 | esc_html__('Admin API Key', 'algolia-woo-indexer'), |
| 114 | - array( $algowooindexer, 'algolia_woo_indexer_admin_api_key_output' ), |
|
| 114 | + array($algowooindexer, 'algolia_woo_indexer_admin_api_key_output'), |
|
| 115 | 115 | 'algolia_woo_indexer', |
| 116 | 116 | 'algolia_woo_indexer_main' |
| 117 | 117 | ); |
| 118 | 118 | add_settings_field( |
| 119 | 119 | 'algolia_woo_indexer_index_name', |
| 120 | 120 | esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
| 121 | - array( $algowooindexer, 'algolia_woo_indexer_index_name_output' ), |
|
| 121 | + array($algowooindexer, 'algolia_woo_indexer_index_name_output'), |
|
| 122 | 122 | 'algolia_woo_indexer', |
| 123 | 123 | 'algolia_woo_indexer_main' |
| 124 | 124 | ); |
| 125 | 125 | add_settings_field( |
| 126 | 126 | 'algolia_woo_indexer_automatically_send_new_products', |
| 127 | 127 | esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
| 128 | - array( $algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output' ), |
|
| 128 | + array($algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output'), |
|
| 129 | 129 | 'algolia_woo_indexer', |
| 130 | 130 | 'algolia_woo_indexer_main' |
| 131 | 131 | ); |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | * But I have still done it to be 100% safe |
| 191 | 191 | */ |
| 192 | 192 | $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
| 193 | - $automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; ?> |
|
| 193 | + $automatically_send_new_products = ( ! empty($automatically_send_new_products)) ? 1 : 0; ?> |
|
| 194 | 194 | <input id="algolia_woo_indexer_automatically_send_new_products" name="algolia_woo_indexer_automatically_send_new_products[checked]" |
| 195 | 195 | type="checkbox" <?php checked(1, $automatically_send_new_products); ?> /> |
| 196 | 196 | <?php |
@@ -236,10 +236,10 @@ discard block |
||
| 236 | 236 | */ |
| 237 | 237 | Algolia_Check_Requirements::check_unmet_requirements(); |
| 238 | 238 | |
| 239 | - if (! Algolia_Check_Requirements::algolia_wp_version_check() || ! Algolia_Check_Requirements::algolia_php_version_check()) { |
|
| 239 | + if ( ! Algolia_Check_Requirements::algolia_wp_version_check() || ! Algolia_Check_Requirements::algolia_php_version_check()) { |
|
| 240 | 240 | add_action( |
| 241 | 241 | 'admin_notices', |
| 242 | - function () { |
|
| 242 | + function() { |
|
| 243 | 243 | echo '<div class="error notice"> |
| 244 | 244 | <p>' . esc_html__('Please check the server requirements for Algolia Woo Indexer. <br/> It requires minimum PHP version 7.2 and WordPress version 5.0', 'algolia-woo-indexer') . '</p> |
| 245 | 245 | </div>'; |
@@ -252,31 +252,31 @@ discard block |
||
| 252 | 252 | /** |
| 253 | 253 | * Setup translations |
| 254 | 254 | */ |
| 255 | - add_action('plugins_loaded', array( $ob_class, 'load_textdomain' )); |
|
| 255 | + add_action('plugins_loaded', array($ob_class, 'load_textdomain')); |
|
| 256 | 256 | |
| 257 | 257 | /** |
| 258 | 258 | * Add actions to setup admin menu |
| 259 | 259 | */ |
| 260 | 260 | if (is_admin()) { |
| 261 | - add_action('admin_menu', array( $ob_class, 'admin_menu' )); |
|
| 262 | - add_action('admin_init', array( $ob_class, 'setup_settings_sections' )); |
|
| 263 | - add_action('admin_init', array( $ob_class, 'update_settings_options' )); |
|
| 264 | - add_action('admin_init', array( $ob_class, 'maybe_send_products' )); |
|
| 261 | + add_action('admin_menu', array($ob_class, 'admin_menu')); |
|
| 262 | + add_action('admin_init', array($ob_class, 'setup_settings_sections')); |
|
| 263 | + add_action('admin_init', array($ob_class, 'update_settings_options')); |
|
| 264 | + add_action('admin_init', array($ob_class, 'maybe_send_products')); |
|
| 265 | 265 | |
| 266 | 266 | /** |
| 267 | 267 | * Register hook to automatically send new products if the option is set |
| 268 | 268 | */ |
| 269 | 269 | |
| 270 | 270 | if ('1' === $automatically_send_new_products) { |
| 271 | - add_action('save_post', array( $ob_class, 'send_new_product_to_algolia' ), 10, 3); |
|
| 271 | + add_action('save_post', array($ob_class, 'send_new_product_to_algolia'), 10, 3); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
| 275 | 275 | |
| 276 | - if (! is_plugin_active('woocommerce/woocommerce.php')) { |
|
| 276 | + if ( ! is_plugin_active('woocommerce/woocommerce.php')) { |
|
| 277 | 277 | add_action( |
| 278 | 278 | 'admin_notices', |
| 279 | - function () { |
|
| 279 | + function() { |
|
| 280 | 280 | echo '<div class="error notice"> |
| 281 | 281 | <p>' . esc_html__('WooCommerce plugin must be enabled for Algolia Woo Indexer to work.', 'algolia-woo-indexer') . '</p> |
| 282 | 282 | </div>'; |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | /** |
| 342 | 342 | * Sanitizing by setting the value to either 1 or 0 |
| 343 | 343 | */ |
| 344 | - $filtered_automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; |
|
| 344 | + $filtered_automatically_send_new_products = ( ! empty($automatically_send_new_products)) ? 1 : 0; |
|
| 345 | 345 | |
| 346 | 346 | /** |
| 347 | 347 | * Values have been filtered and sanitized |
@@ -349,28 +349,28 @@ discard block |
||
| 349 | 349 | * |
| 350 | 350 | * @see https://developer.wordpress.org/reference/functions/update_option/ |
| 351 | 351 | */ |
| 352 | - if (isset($filtered_application_id) && (! empty($filtered_application_id))) { |
|
| 352 | + if (isset($filtered_application_id) && ( ! empty($filtered_application_id))) { |
|
| 353 | 353 | update_option( |
| 354 | 354 | ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID, |
| 355 | 355 | $filtered_application_id |
| 356 | 356 | ); |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - if (isset($filtered_api_key) && (! empty($filtered_api_key))) { |
|
| 359 | + if (isset($filtered_api_key) && ( ! empty($filtered_api_key))) { |
|
| 360 | 360 | update_option( |
| 361 | 361 | ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
| 362 | 362 | $filtered_api_key |
| 363 | 363 | ); |
| 364 | 364 | } |
| 365 | 365 | |
| 366 | - if (isset($filtered_index_name) && (! empty($filtered_index_name))) { |
|
| 366 | + if (isset($filtered_index_name) && ( ! empty($filtered_index_name))) { |
|
| 367 | 367 | update_option( |
| 368 | 368 | ALGOWOO_DB_OPTION . INDEX_NAME, |
| 369 | 369 | $filtered_index_name |
| 370 | 370 | ); |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | - if (isset($filtered_automatically_send_new_products) && (! empty($filtered_automatically_send_new_products))) { |
|
| 373 | + if (isset($filtered_automatically_send_new_products) && ( ! empty($filtered_automatically_send_new_products))) { |
|
| 374 | 374 | update_option( |
| 375 | 375 | ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
| 376 | 376 | $filtered_automatically_send_new_products |
@@ -418,7 +418,7 @@ discard block |
||
| 418 | 418 | esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
| 419 | 419 | 'manage_options', |
| 420 | 420 | 'algolia-woo-indexer-settings', |
| 421 | - array( get_called_class(), 'algolia_woo_indexer_settings' ) |
|
| 421 | + array(get_called_class(), 'algolia_woo_indexer_settings') |
|
| 422 | 422 | ); |
| 423 | 423 | } |
| 424 | 424 | |
@@ -432,7 +432,7 @@ discard block |
||
| 432 | 432 | /** |
| 433 | 433 | * Verify that the user can access the settings page |
| 434 | 434 | */ |
| 435 | - if (! current_user_can('manage_options')) { |
|
| 435 | + if ( ! current_user_can('manage_options')) { |
|
| 436 | 436 | wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
| 437 | 437 | } ?> |
| 438 | 438 | <div class="wrap"> |
@@ -459,7 +459,7 @@ discard block |
||
| 459 | 459 | */ |
| 460 | 460 | public static function get_instance() |
| 461 | 461 | { |
| 462 | - if (! self::$instance) { |
|
| 462 | + if ( ! self::$instance) { |
|
| 463 | 463 | self::$instance = new Algolia_Woo_Indexer(); |
| 464 | 464 | } |
| 465 | 465 | return self::$instance; |
@@ -18,31 +18,31 @@ |
||
| 18 | 18 | /** |
| 19 | 19 | * Abort if this file is called directly |
| 20 | 20 | */ |
| 21 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 21 | +if ( ! defined('ABSPATH')) { |
|
| 22 | 22 | exit; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * Main class file |
| 27 | 27 | */ |
| 28 | -require_once plugin_dir_path( __FILE__ ) . '/classes/class-algolia-woo-indexer.php'; |
|
| 28 | +require_once plugin_dir_path(__FILE__) . '/classes/class-algolia-woo-indexer.php'; |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * Class for checking plugin requirements |
| 32 | 32 | */ |
| 33 | -require_once plugin_dir_path( __FILE__ ) . '/classes/class-check-requirements.php'; |
|
| 33 | +require_once plugin_dir_path(__FILE__) . '/classes/class-check-requirements.php'; |
|
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | 36 | * Class for verifying nonces |
| 37 | 37 | */ |
| 38 | -require_once plugin_dir_path( __FILE__ ) . '/classes/class-verify-nonces.php'; |
|
| 38 | +require_once plugin_dir_path(__FILE__) . '/classes/class-verify-nonces.php'; |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * Class for sending products |
| 42 | 42 | */ |
| 43 | -require_once plugin_dir_path( __FILE__ ) . '/classes/class-send-products.php'; |
|
| 43 | +require_once plugin_dir_path(__FILE__) . '/classes/class-send-products.php'; |
|
| 44 | 44 | |
| 45 | 45 | $algowooindexer = \Algowoo\Algolia_Woo_Indexer::get_instance(); |
| 46 | 46 | |
| 47 | -register_activation_hook( __FILE__, array( $algowooindexer, 'activate_plugin' ) ); |
|
| 48 | -register_deactivation_hook( __FILE__, array( $algowooindexer, 'deactivate_plugin' ) ); |
|
| 47 | +register_activation_hook(__FILE__, array($algowooindexer, 'activate_plugin')); |
|
| 48 | +register_deactivation_hook(__FILE__, array($algowooindexer, 'deactivate_plugin')); |
|