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