@@ -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_APPLICATION_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_APPLICATION_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 | } |
@@ -23,419 +23,419 @@ 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 | - // TODO Rename class "Algolia_Woo_Indexer" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
34 | - class Algolia_Woo_Indexer |
|
35 | - { |
|
36 | - const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
37 | - const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
38 | - |
|
39 | - /** |
|
40 | - * Class instance |
|
41 | - * |
|
42 | - * @var object |
|
43 | - */ |
|
44 | - private static $instance; |
|
45 | - |
|
46 | - /** |
|
47 | - * The plugin URL |
|
48 | - * |
|
49 | - * @var string |
|
50 | - */ |
|
51 | - private static $plugin_url = ''; |
|
52 | - |
|
53 | - /** |
|
54 | - * Class constructor |
|
55 | - * |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function __construct() |
|
59 | - { |
|
60 | - $this->init(); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Setup sections and fields to store and retrieve values from Settings API |
|
65 | - * |
|
66 | - * @return void |
|
67 | - */ |
|
68 | - public static function setup_settings_sections() |
|
69 | - { |
|
70 | - /** |
|
71 | - * Setup arguments for settings sections and fields |
|
72 | - * |
|
73 | - * @see https://developer.wordpress.org/reference/functions/register_setting/ |
|
74 | - */ |
|
75 | - if (is_admin()) { |
|
76 | - $arguments = array( |
|
77 | - 'type' => 'string', |
|
78 | - 'sanitize_callback' => 'settings_fields_validate_options', |
|
79 | - 'default' => null, |
|
80 | - ); |
|
81 | - register_setting('algolia_woo_options', 'algolia_woo_options', $arguments); |
|
82 | - |
|
83 | - /** |
|
84 | - * Make sure we reference the instance of the current class by using self::get_instance() |
|
85 | - * This way we can setup the correct callback function for add_settings_section and add_settings_field |
|
86 | - */ |
|
87 | - $algowooindexer = self::get_instance(); |
|
88 | - |
|
89 | - /** |
|
90 | - * Add our necessary settings sections and fields |
|
91 | - */ |
|
92 | - add_settings_section( |
|
93 | - 'algolia_woo_indexer_main', |
|
94 | - esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
|
95 | - array( $algowooindexer, 'algolia_woo_indexer_section_text' ), |
|
96 | - 'algolia_woo_indexer' |
|
97 | - ); |
|
98 | - add_settings_field( |
|
99 | - 'algolia_woo_indexer_application_id', |
|
100 | - esc_html__('Application ID', 'algolia-woo-indexer'), |
|
101 | - array( $algowooindexer, 'algolia_woo_indexer_application_id_output' ), |
|
102 | - 'algolia_woo_indexer', |
|
103 | - 'algolia_woo_indexer_main' |
|
104 | - ); |
|
105 | - add_settings_field( |
|
106 | - 'algolia_woo_indexer_admin_api_key', |
|
107 | - esc_html__('Admin API Key', 'algolia-woo-indexer'), |
|
108 | - array( $algowooindexer, 'algolia_woo_indexer_admin_api_key_output' ), |
|
109 | - 'algolia_woo_indexer', |
|
110 | - 'algolia_woo_indexer_main' |
|
111 | - ); |
|
112 | - add_settings_field( |
|
113 | - 'algolia_woo_indexer_index_name', |
|
114 | - esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
|
115 | - array( $algowooindexer, 'algolia_woo_indexer_index_name_output' ), |
|
116 | - 'algolia_woo_indexer', |
|
117 | - 'algolia_woo_indexer_main' |
|
118 | - ); |
|
119 | - add_settings_field( |
|
120 | - 'algolia_woo_indexer_automatically_send_new_products', |
|
121 | - esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
|
122 | - array( $algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output' ), |
|
123 | - 'algolia_woo_indexer', |
|
124 | - 'algolia_woo_indexer_main' |
|
125 | - ); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Output for admin API key field |
|
131 | - * |
|
132 | - * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ |
|
133 | - * |
|
134 | - * @return void |
|
135 | - */ |
|
136 | - public static function algolia_woo_indexer_admin_api_key_output() |
|
137 | - { |
|
138 | - $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
139 | - $api_key = is_string($api_key) ? $api_key : CHANGE_ME; |
|
140 | - |
|
141 | - wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name'); |
|
142 | - |
|
143 | - 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 | + // TODO Rename class "Algolia_Woo_Indexer" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
34 | + class Algolia_Woo_Indexer |
|
35 | + { |
|
36 | + const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
37 | + const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
38 | + |
|
39 | + /** |
|
40 | + * Class instance |
|
41 | + * |
|
42 | + * @var object |
|
43 | + */ |
|
44 | + private static $instance; |
|
45 | + |
|
46 | + /** |
|
47 | + * The plugin URL |
|
48 | + * |
|
49 | + * @var string |
|
50 | + */ |
|
51 | + private static $plugin_url = ''; |
|
52 | + |
|
53 | + /** |
|
54 | + * Class constructor |
|
55 | + * |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function __construct() |
|
59 | + { |
|
60 | + $this->init(); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Setup sections and fields to store and retrieve values from Settings API |
|
65 | + * |
|
66 | + * @return void |
|
67 | + */ |
|
68 | + public static function setup_settings_sections() |
|
69 | + { |
|
70 | + /** |
|
71 | + * Setup arguments for settings sections and fields |
|
72 | + * |
|
73 | + * @see https://developer.wordpress.org/reference/functions/register_setting/ |
|
74 | + */ |
|
75 | + if (is_admin()) { |
|
76 | + $arguments = array( |
|
77 | + 'type' => 'string', |
|
78 | + 'sanitize_callback' => 'settings_fields_validate_options', |
|
79 | + 'default' => null, |
|
80 | + ); |
|
81 | + register_setting('algolia_woo_options', 'algolia_woo_options', $arguments); |
|
82 | + |
|
83 | + /** |
|
84 | + * Make sure we reference the instance of the current class by using self::get_instance() |
|
85 | + * This way we can setup the correct callback function for add_settings_section and add_settings_field |
|
86 | + */ |
|
87 | + $algowooindexer = self::get_instance(); |
|
88 | + |
|
89 | + /** |
|
90 | + * Add our necessary settings sections and fields |
|
91 | + */ |
|
92 | + add_settings_section( |
|
93 | + 'algolia_woo_indexer_main', |
|
94 | + esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
|
95 | + array( $algowooindexer, 'algolia_woo_indexer_section_text' ), |
|
96 | + 'algolia_woo_indexer' |
|
97 | + ); |
|
98 | + add_settings_field( |
|
99 | + 'algolia_woo_indexer_application_id', |
|
100 | + esc_html__('Application ID', 'algolia-woo-indexer'), |
|
101 | + array( $algowooindexer, 'algolia_woo_indexer_application_id_output' ), |
|
102 | + 'algolia_woo_indexer', |
|
103 | + 'algolia_woo_indexer_main' |
|
104 | + ); |
|
105 | + add_settings_field( |
|
106 | + 'algolia_woo_indexer_admin_api_key', |
|
107 | + esc_html__('Admin API Key', 'algolia-woo-indexer'), |
|
108 | + array( $algowooindexer, 'algolia_woo_indexer_admin_api_key_output' ), |
|
109 | + 'algolia_woo_indexer', |
|
110 | + 'algolia_woo_indexer_main' |
|
111 | + ); |
|
112 | + add_settings_field( |
|
113 | + 'algolia_woo_indexer_index_name', |
|
114 | + esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
|
115 | + array( $algowooindexer, 'algolia_woo_indexer_index_name_output' ), |
|
116 | + 'algolia_woo_indexer', |
|
117 | + 'algolia_woo_indexer_main' |
|
118 | + ); |
|
119 | + add_settings_field( |
|
120 | + 'algolia_woo_indexer_automatically_send_new_products', |
|
121 | + esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
|
122 | + array( $algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output' ), |
|
123 | + 'algolia_woo_indexer', |
|
124 | + 'algolia_woo_indexer_main' |
|
125 | + ); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Output for admin API key field |
|
131 | + * |
|
132 | + * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ |
|
133 | + * |
|
134 | + * @return void |
|
135 | + */ |
|
136 | + public static function algolia_woo_indexer_admin_api_key_output() |
|
137 | + { |
|
138 | + $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
139 | + $api_key = is_string($api_key) ? $api_key : CHANGE_ME; |
|
140 | + |
|
141 | + wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name'); |
|
142 | + |
|
143 | + echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]' |
|
144 | 144 | type='text' value='" . esc_attr($api_key) . "' />"; |
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Output for application ID field |
|
149 | - * |
|
150 | - * @return void |
|
151 | - */ |
|
152 | - public static function algolia_woo_indexer_application_id_output() |
|
153 | - { |
|
154 | - $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
155 | - $application_id = is_string($application_id) ? $application_id : CHANGE_ME; |
|
156 | - |
|
157 | - echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]' |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Output for application ID field |
|
149 | + * |
|
150 | + * @return void |
|
151 | + */ |
|
152 | + public static function algolia_woo_indexer_application_id_output() |
|
153 | + { |
|
154 | + $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
155 | + $application_id = is_string($application_id) ? $application_id : CHANGE_ME; |
|
156 | + |
|
157 | + echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]' |
|
158 | 158 | type='text' value='" . esc_attr($application_id) . "' />"; |
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Output for index name field |
|
163 | - * |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - public static function algolia_woo_indexer_index_name_output() |
|
167 | - { |
|
168 | - $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
169 | - $index_name = is_string($index_name) ? $index_name : CHANGE_ME; |
|
170 | - |
|
171 | - echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]' |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Output for index name field |
|
163 | + * |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + public static function algolia_woo_indexer_index_name_output() |
|
167 | + { |
|
168 | + $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
169 | + $index_name = is_string($index_name) ? $index_name : CHANGE_ME; |
|
170 | + |
|
171 | + echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]' |
|
172 | 172 | type='text' value='" . esc_attr($index_name) . "' />"; |
173 | - } |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Output for checkbox to check if we automatically send new products to Algolia |
|
177 | - * |
|
178 | - * @return void |
|
179 | - */ |
|
180 | - public static function algolia_woo_indexer_automatically_send_new_products_output() |
|
181 | - { |
|
182 | - /** |
|
183 | - * Sanitization is not really needed as the variable is not directly echoed |
|
184 | - * But I have still done it to be 100% safe |
|
185 | - */ |
|
186 | - $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
187 | - $automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; ?> |
|
175 | + /** |
|
176 | + * Output for checkbox to check if we automatically send new products to Algolia |
|
177 | + * |
|
178 | + * @return void |
|
179 | + */ |
|
180 | + public static function algolia_woo_indexer_automatically_send_new_products_output() |
|
181 | + { |
|
182 | + /** |
|
183 | + * Sanitization is not really needed as the variable is not directly echoed |
|
184 | + * But I have still done it to be 100% safe |
|
185 | + */ |
|
186 | + $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
187 | + $automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; ?> |
|
188 | 188 | <input id="algolia_woo_indexer_automatically_send_new_products" name="algolia_woo_indexer_automatically_send_new_products[checked]" |
189 | 189 | type="checkbox" <?php checked(1, $automatically_send_new_products); ?> /> |
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 | - $automatically_send_new_products = 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 | + $automatically_send_new_products = 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' === $automatically_send_new_products) { |
|
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' === $automatically_send_new_products) { |
|
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 | - /** |
|
310 | - * Do not proceed if we are going to send products |
|
311 | - */ |
|
312 | - if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
313 | - return; |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Filter the application id, api key, index name and verify that the input is an array |
|
318 | - * |
|
319 | - * @see https://www.php.net/manual/en/function.filter-input.php |
|
320 | - */ |
|
321 | - $post_application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
322 | - $post_api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
323 | - $post_index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
324 | - $automatically_send_new_products = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
325 | - |
|
326 | - /** |
|
327 | - * Properly sanitize text fields before updating data |
|
328 | - * |
|
329 | - * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
330 | - */ |
|
331 | - $filtered_application_id = sanitize_text_field($post_application_id['id']); |
|
332 | - $filtered_api_key = sanitize_text_field($post_api_key['key']); |
|
333 | - $filtered_index_name = sanitize_text_field($post_index_name['name']); |
|
334 | - |
|
335 | - /** |
|
336 | - * Sanitizing by setting the value to either 1 or 0 |
|
337 | - */ |
|
338 | - $filtered_automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; |
|
339 | - |
|
340 | - /** |
|
341 | - * Values have been filtered and sanitized |
|
342 | - * Check if set and not empty and update the database |
|
343 | - * |
|
344 | - * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
345 | - */ |
|
346 | - if (isset($filtered_application_id) && (! empty($filtered_application_id))) { |
|
347 | - update_option( |
|
348 | - ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID, |
|
349 | - $filtered_application_id |
|
350 | - ); |
|
351 | - } |
|
352 | - |
|
353 | - if (isset($filtered_api_key) && (! empty($filtered_api_key))) { |
|
354 | - update_option( |
|
355 | - ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
356 | - $filtered_api_key |
|
357 | - ); |
|
358 | - } |
|
359 | - |
|
360 | - if (isset($filtered_index_name) && (! empty($filtered_index_name))) { |
|
361 | - update_option( |
|
362 | - ALGOWOO_DB_OPTION . INDEX_NAME, |
|
363 | - $filtered_index_name |
|
364 | - ); |
|
365 | - } |
|
366 | - |
|
367 | - if (isset($filtered_automatically_send_new_products) && (! empty($filtered_automatically_send_new_products))) { |
|
368 | - update_option( |
|
369 | - ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
370 | - $filtered_automatically_send_new_products |
|
371 | - ); |
|
372 | - } |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z |
|
377 | - * |
|
378 | - * @param string $input Settings text data |
|
379 | - * @return array |
|
380 | - */ |
|
381 | - public static function settings_fields_validate_options($input) |
|
382 | - { |
|
383 | - $valid = array(); |
|
384 | - $valid['name'] = preg_replace( |
|
385 | - '/[^a-zA-Z\s]/', |
|
386 | - '', |
|
387 | - $input['name'] |
|
388 | - ); |
|
389 | - return $valid; |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Load text domain for translations |
|
394 | - * |
|
395 | - * @return void |
|
396 | - */ |
|
397 | - public static function load_textdomain() |
|
398 | - { |
|
399 | - load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/'); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Add the new menu to settings section so that we can configure the plugin |
|
404 | - * |
|
405 | - * @return void |
|
406 | - */ |
|
407 | - public static function admin_menu() |
|
408 | - { |
|
409 | - add_submenu_page( |
|
410 | - 'options-general.php', |
|
411 | - esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
412 | - esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
413 | - 'manage_options', |
|
414 | - 'algolia-woo-indexer-settings', |
|
415 | - array( get_called_class(), 'algolia_woo_indexer_settings' ) |
|
416 | - ); |
|
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * Display settings and allow user to modify them |
|
421 | - * |
|
422 | - * @return void |
|
423 | - */ |
|
424 | - public static function algolia_woo_indexer_settings() |
|
425 | - { |
|
426 | - /** |
|
427 | - * Verify that the user can access the settings page |
|
428 | - */ |
|
429 | - if (! current_user_can('manage_options')) { |
|
430 | - wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
|
431 | - } ?> |
|
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 | + /** |
|
310 | + * Do not proceed if we are going to send products |
|
311 | + */ |
|
312 | + if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
313 | + return; |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Filter the application id, api key, index name and verify that the input is an array |
|
318 | + * |
|
319 | + * @see https://www.php.net/manual/en/function.filter-input.php |
|
320 | + */ |
|
321 | + $post_application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
322 | + $post_api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
323 | + $post_index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
324 | + $automatically_send_new_products = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
325 | + |
|
326 | + /** |
|
327 | + * Properly sanitize text fields before updating data |
|
328 | + * |
|
329 | + * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
330 | + */ |
|
331 | + $filtered_application_id = sanitize_text_field($post_application_id['id']); |
|
332 | + $filtered_api_key = sanitize_text_field($post_api_key['key']); |
|
333 | + $filtered_index_name = sanitize_text_field($post_index_name['name']); |
|
334 | + |
|
335 | + /** |
|
336 | + * Sanitizing by setting the value to either 1 or 0 |
|
337 | + */ |
|
338 | + $filtered_automatically_send_new_products = (! empty($automatically_send_new_products)) ? 1 : 0; |
|
339 | + |
|
340 | + /** |
|
341 | + * Values have been filtered and sanitized |
|
342 | + * Check if set and not empty and update the database |
|
343 | + * |
|
344 | + * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
345 | + */ |
|
346 | + if (isset($filtered_application_id) && (! empty($filtered_application_id))) { |
|
347 | + update_option( |
|
348 | + ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID, |
|
349 | + $filtered_application_id |
|
350 | + ); |
|
351 | + } |
|
352 | + |
|
353 | + if (isset($filtered_api_key) && (! empty($filtered_api_key))) { |
|
354 | + update_option( |
|
355 | + ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
356 | + $filtered_api_key |
|
357 | + ); |
|
358 | + } |
|
359 | + |
|
360 | + if (isset($filtered_index_name) && (! empty($filtered_index_name))) { |
|
361 | + update_option( |
|
362 | + ALGOWOO_DB_OPTION . INDEX_NAME, |
|
363 | + $filtered_index_name |
|
364 | + ); |
|
365 | + } |
|
366 | + |
|
367 | + if (isset($filtered_automatically_send_new_products) && (! empty($filtered_automatically_send_new_products))) { |
|
368 | + update_option( |
|
369 | + ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
370 | + $filtered_automatically_send_new_products |
|
371 | + ); |
|
372 | + } |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z |
|
377 | + * |
|
378 | + * @param string $input Settings text data |
|
379 | + * @return array |
|
380 | + */ |
|
381 | + public static function settings_fields_validate_options($input) |
|
382 | + { |
|
383 | + $valid = array(); |
|
384 | + $valid['name'] = preg_replace( |
|
385 | + '/[^a-zA-Z\s]/', |
|
386 | + '', |
|
387 | + $input['name'] |
|
388 | + ); |
|
389 | + return $valid; |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Load text domain for translations |
|
394 | + * |
|
395 | + * @return void |
|
396 | + */ |
|
397 | + public static function load_textdomain() |
|
398 | + { |
|
399 | + load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/'); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Add the new menu to settings section so that we can configure the plugin |
|
404 | + * |
|
405 | + * @return void |
|
406 | + */ |
|
407 | + public static function admin_menu() |
|
408 | + { |
|
409 | + add_submenu_page( |
|
410 | + 'options-general.php', |
|
411 | + esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
412 | + esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
413 | + 'manage_options', |
|
414 | + 'algolia-woo-indexer-settings', |
|
415 | + array( get_called_class(), 'algolia_woo_indexer_settings' ) |
|
416 | + ); |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * Display settings and allow user to modify them |
|
421 | + * |
|
422 | + * @return void |
|
423 | + */ |
|
424 | + public static function algolia_woo_indexer_settings() |
|
425 | + { |
|
426 | + /** |
|
427 | + * Verify that the user can access the settings page |
|
428 | + */ |
|
429 | + if (! current_user_can('manage_options')) { |
|
430 | + wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
|
431 | + } ?> |
|
432 | 432 | <div class="wrap"> |
433 | 433 | <h1><?php esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'); ?></h1> |
434 | 434 | <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
435 | 435 | <?php |
436 | - settings_fields('algolia_woo_options'); |
|
437 | - do_settings_sections('algolia_woo_indexer'); |
|
438 | - submit_button('', 'primary wide'); ?> |
|
436 | + settings_fields('algolia_woo_options'); |
|
437 | + do_settings_sections('algolia_woo_indexer'); |
|
438 | + submit_button('', 'primary wide'); ?> |
|
439 | 439 | </form> |
440 | 440 | <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
441 | 441 | <?php wp_nonce_field('send_products_to_algolia_nonce_action', 'send_products_to_algolia_nonce_name'); ?> |
@@ -444,75 +444,75 @@ discard block |
||
444 | 444 | </form> |
445 | 445 | </div> |
446 | 446 | <?php |
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Get active object instance |
|
451 | - * |
|
452 | - * @return object |
|
453 | - */ |
|
454 | - public static function get_instance() |
|
455 | - { |
|
456 | - if (! self::$instance) { |
|
457 | - self::$instance = new Algolia_Woo_Indexer(); |
|
458 | - } |
|
459 | - return self::$instance; |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * The actions to execute when the plugin is activated. |
|
464 | - * |
|
465 | - * @return void |
|
466 | - */ |
|
467 | - public static function activate_plugin() |
|
468 | - { |
|
469 | - |
|
470 | - /** |
|
471 | - * Set default values for options if not already set |
|
472 | - */ |
|
473 | - $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
474 | - $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
475 | - $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
476 | - $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Get active object instance |
|
451 | + * |
|
452 | + * @return object |
|
453 | + */ |
|
454 | + public static function get_instance() |
|
455 | + { |
|
456 | + if (! self::$instance) { |
|
457 | + self::$instance = new Algolia_Woo_Indexer(); |
|
458 | + } |
|
459 | + return self::$instance; |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * The actions to execute when the plugin is activated. |
|
464 | + * |
|
465 | + * @return void |
|
466 | + */ |
|
467 | + public static function activate_plugin() |
|
468 | + { |
|
469 | + |
|
470 | + /** |
|
471 | + * Set default values for options if not already set |
|
472 | + */ |
|
473 | + $automatically_send_new_products = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
474 | + $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID); |
|
475 | + $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
476 | + $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
477 | 477 | |
478 | - if (empty($automatically_send_new_products)) { |
|
479 | - add_option( |
|
480 | - ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
481 | - '0' |
|
482 | - ); |
|
483 | - } |
|
484 | - |
|
485 | - if (empty($algolia_application_id)) { |
|
486 | - add_option( |
|
487 | - ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID, |
|
488 | - 'Change me' |
|
489 | - ); |
|
490 | - } |
|
491 | - |
|
492 | - if (empty($algolia_api_key)) { |
|
493 | - add_option( |
|
494 | - ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
495 | - 'Change me' |
|
496 | - ); |
|
497 | - } |
|
498 | - |
|
499 | - if (empty($algolia_index_name)) { |
|
500 | - add_option( |
|
501 | - ALGOWOO_DB_OPTION . INDEX_NAME, |
|
502 | - 'Change me' |
|
503 | - ); |
|
504 | - } |
|
505 | - set_transient(self::PLUGIN_TRANSIENT, true); |
|
506 | - } |
|
507 | - |
|
508 | - /** |
|
509 | - * The actions to execute when the plugin is deactivated. |
|
510 | - * |
|
511 | - * @return void |
|
512 | - */ |
|
513 | - public static function deactivate_plugin() |
|
514 | - { |
|
515 | - delete_transient(self::PLUGIN_TRANSIENT); |
|
516 | - } |
|
517 | - } |
|
478 | + if (empty($automatically_send_new_products)) { |
|
479 | + add_option( |
|
480 | + ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
481 | + '0' |
|
482 | + ); |
|
483 | + } |
|
484 | + |
|
485 | + if (empty($algolia_application_id)) { |
|
486 | + add_option( |
|
487 | + ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID, |
|
488 | + 'Change me' |
|
489 | + ); |
|
490 | + } |
|
491 | + |
|
492 | + if (empty($algolia_api_key)) { |
|
493 | + add_option( |
|
494 | + ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
495 | + 'Change me' |
|
496 | + ); |
|
497 | + } |
|
498 | + |
|
499 | + if (empty($algolia_index_name)) { |
|
500 | + add_option( |
|
501 | + ALGOWOO_DB_OPTION . INDEX_NAME, |
|
502 | + 'Change me' |
|
503 | + ); |
|
504 | + } |
|
505 | + set_transient(self::PLUGIN_TRANSIENT, true); |
|
506 | + } |
|
507 | + |
|
508 | + /** |
|
509 | + * The actions to execute when the plugin is deactivated. |
|
510 | + * |
|
511 | + * @return void |
|
512 | + */ |
|
513 | + public static function deactivate_plugin() |
|
514 | + { |
|
515 | + delete_transient(self::PLUGIN_TRANSIENT); |
|
516 | + } |
|
517 | + } |
|
518 | 518 | } |
519 | 519 | \ No newline at end of file |