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