Passed
Push — master ( 623877...66a44c )
by Daniel
05:55 queued 01:42
created
classes/class-send-products.php 2 patches
Indentation   +168 added lines, -168 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,174 +45,174 @@  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
-         * Send WooCommerce products to Algolia
85
-         *
86
-         * @param Int $id Product to send to Algolia if we send only a single product
87
-         * @return void
88
-         */
89
-        public static function send_products_to_algolia($id = '')
90
-        {
91
-            /**
92
-             * Remove classes from plugin URL and autoload Algolia with Composer
93
-             */
94
-
95
-            $base_plugin_directory = str_replace('classes', '', dirname(__FILE__));
96
-            require_once $base_plugin_directory . '/vendor/autoload.php';
97
-
98
-            /**
99
-             * Fetch the required variables from the Settings API
100
-             */
101
-
102
-            $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID);
103
-            $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME;
104
-
105
-            $algolia_api_key        = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY);
106
-            $algolia_api_key        = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME;
107
-
108
-            $algolia_index_name     = get_option(ALGOWOO_DB_OPTION . INDEX_NAME);
109
-            $algolia_index_name        = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME;
110
-
111
-            /**
112
-             * Display admin notice and return if not all values have been set
113
-             */
114
-
115
-            Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name);
116
-
117
-            /**
118
-             * Initiate the Algolia client
119
-             */
120
-            self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key);
121
-
122
-            /**
123
-             * Check if we can connect, if not, handle the exception, display an error and then return
124
-             */
125
-            self::can_connect_to_algolia();
126
-
127
-            /**
128
-             * Initialize the search index and set the name to the option from the database
129
-             */
130
-            $index = self::$algolia->initIndex($algolia_index_name);
131
-
132
-            /**
133
-             * Setup arguments for sending all products to Algolia
134
-             *
135
-             * Limit => -1 means we send all products
136
-             */
137
-            $arguments = array(
138
-                'status'   => 'publish',
139
-                'limit'    => -1,
140
-                'paginate' => false,
141
-            );
142
-
143
-            /**
144
-             * Setup arguments for sending only a single product
145
-             */
146
-            if (isset($id) && '' !== $id) {
147
-                $arguments = array(
148
-                    'status'   => 'publish',
149
-                    'include'  => array($id),
150
-                    'paginate' => false,
151
-                );
152
-            }
153
-
154
-            /**
155
-             * Fetch all products from WooCommerce
156
-             *
157
-             * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html
158
-             */
159
-            $products =
160
-                /** @scrutinizer ignore-call */
161
-                wc_get_products($arguments);
162
-
163
-            if (empty($products)) {
164
-                return;
165
-            }
166
-            $records = array();
167
-            $record  = array();
168
-
169
-            foreach ($products as $product) {
170
-                /**
171
-                 * Set sale price or regular price based on product type
172
-                 */
173
-                if ($product->is_type('simple')) {
174
-                    $sale_price     =  $product->get_sale_price();
175
-                    $regular_price  =  $product->get_regular_price();
176
-                } elseif ($product->is_type('variable')) {
177
-                    $sale_price     =  $product->get_variation_sale_price('min', true);
178
-                    $regular_price  =  $product->get_variation_regular_price('max', true);
179
-                }
180
-
181
-                /**
182
-                 * Extract image from $product->get_image()
183
-                 */
184
-                preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result);
185
-                $product_image = array_pop($result);
186
-                /**
187
-                 * Build the record array using the information from the WooCommerce product
188
-                 */
189
-                $record['objectID']                      = $product->get_id();
190
-                $record['product_name']                  = $product->get_name();
191
-                $record['product_image']                 = $product_image;
192
-                $record['short_description']             = $product->get_short_description();
193
-                $record['regular_price']                 = $regular_price;
194
-                $record['sale_price']                    = $sale_price;
195
-                $record['on_sale']                       = $product->is_on_sale();
196
-                $records[] = $record;
197
-            }
198
-            wp_reset_postdata();
199
-
200
-            /**
201
-             * Send the information to Algolia and save the result
202
-             * If result is NullResponse, print an error message
203
-             */
204
-            $result = $index->saveObjects($records);
205
-
206
-            if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) {
207
-                wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings'));
208
-            }
209
-
210
-            /**
211
-             * Display success message
212
-             */
213
-            echo '<div class="notice notice-success is-dismissible">
77
+					}
78
+				);
79
+				return;
80
+			}
81
+		}
82
+
83
+		/**
84
+		 * Send WooCommerce products to Algolia
85
+		 *
86
+		 * @param Int $id Product to send to Algolia if we send only a single product
87
+		 * @return void
88
+		 */
89
+		public static function send_products_to_algolia($id = '')
90
+		{
91
+			/**
92
+			 * Remove classes from plugin URL and autoload Algolia with Composer
93
+			 */
94
+
95
+			$base_plugin_directory = str_replace('classes', '', dirname(__FILE__));
96
+			require_once $base_plugin_directory . '/vendor/autoload.php';
97
+
98
+			/**
99
+			 * Fetch the required variables from the Settings API
100
+			 */
101
+
102
+			$algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APPLICATION_ID);
103
+			$algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME;
104
+
105
+			$algolia_api_key        = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY);
106
+			$algolia_api_key        = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME;
107
+
108
+			$algolia_index_name     = get_option(ALGOWOO_DB_OPTION . INDEX_NAME);
109
+			$algolia_index_name        = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME;
110
+
111
+			/**
112
+			 * Display admin notice and return if not all values have been set
113
+			 */
114
+
115
+			Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name);
116
+
117
+			/**
118
+			 * Initiate the Algolia client
119
+			 */
120
+			self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key);
121
+
122
+			/**
123
+			 * Check if we can connect, if not, handle the exception, display an error and then return
124
+			 */
125
+			self::can_connect_to_algolia();
126
+
127
+			/**
128
+			 * Initialize the search index and set the name to the option from the database
129
+			 */
130
+			$index = self::$algolia->initIndex($algolia_index_name);
131
+
132
+			/**
133
+			 * Setup arguments for sending all products to Algolia
134
+			 *
135
+			 * Limit => -1 means we send all products
136
+			 */
137
+			$arguments = array(
138
+				'status'   => 'publish',
139
+				'limit'    => -1,
140
+				'paginate' => false,
141
+			);
142
+
143
+			/**
144
+			 * Setup arguments for sending only a single product
145
+			 */
146
+			if (isset($id) && '' !== $id) {
147
+				$arguments = array(
148
+					'status'   => 'publish',
149
+					'include'  => array($id),
150
+					'paginate' => false,
151
+				);
152
+			}
153
+
154
+			/**
155
+			 * Fetch all products from WooCommerce
156
+			 *
157
+			 * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html
158
+			 */
159
+			$products =
160
+				/** @scrutinizer ignore-call */
161
+				wc_get_products($arguments);
162
+
163
+			if (empty($products)) {
164
+				return;
165
+			}
166
+			$records = array();
167
+			$record  = array();
168
+
169
+			foreach ($products as $product) {
170
+				/**
171
+				 * Set sale price or regular price based on product type
172
+				 */
173
+				if ($product->is_type('simple')) {
174
+					$sale_price     =  $product->get_sale_price();
175
+					$regular_price  =  $product->get_regular_price();
176
+				} elseif ($product->is_type('variable')) {
177
+					$sale_price     =  $product->get_variation_sale_price('min', true);
178
+					$regular_price  =  $product->get_variation_regular_price('max', true);
179
+				}
180
+
181
+				/**
182
+				 * Extract image from $product->get_image()
183
+				 */
184
+				preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result);
185
+				$product_image = array_pop($result);
186
+				/**
187
+				 * Build the record array using the information from the WooCommerce product
188
+				 */
189
+				$record['objectID']                      = $product->get_id();
190
+				$record['product_name']                  = $product->get_name();
191
+				$record['product_image']                 = $product_image;
192
+				$record['short_description']             = $product->get_short_description();
193
+				$record['regular_price']                 = $regular_price;
194
+				$record['sale_price']                    = $sale_price;
195
+				$record['on_sale']                       = $product->is_on_sale();
196
+				$records[] = $record;
197
+			}
198
+			wp_reset_postdata();
199
+
200
+			/**
201
+			 * Send the information to Algolia and save the result
202
+			 * If result is NullResponse, print an error message
203
+			 */
204
+			$result = $index->saveObjects($records);
205
+
206
+			if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) {
207
+				wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings'));
208
+			}
209
+
210
+			/**
211
+			 * Display success message
212
+			 */
213
+			echo '<div class="notice notice-success is-dismissible">
214 214
 					 	<p>' . esc_html__('Product(s) sent to Algolia.', 'algolia-woo-indexer') . '</p>
215 215
 				  		</div>';
216
-        }
217
-    }
216
+		}
217
+	}
218 218
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 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>';
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
             $algolia_api_key        = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME;
107 107
 
108 108
             $algolia_index_name     = get_option(ALGOWOO_DB_OPTION . INDEX_NAME);
109
-            $algolia_index_name        = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME;
109
+            $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME;
110 110
 
111 111
             /**
112 112
              * Display admin notice and return if not all values have been set
@@ -171,11 +171,11 @@  discard block
 block discarded – undo
171 171
                  * Set sale price or regular price based on product type
172 172
                  */
173 173
                 if ($product->is_type('simple')) {
174
-                    $sale_price     =  $product->get_sale_price();
175
-                    $regular_price  =  $product->get_regular_price();
174
+                    $sale_price     = $product->get_sale_price();
175
+                    $regular_price  = $product->get_regular_price();
176 176
                 } elseif ($product->is_type('variable')) {
177
-                    $sale_price     =  $product->get_variation_sale_price('min', true);
178
-                    $regular_price  =  $product->get_variation_regular_price('max', true);
177
+                    $sale_price     = $product->get_variation_sale_price('min', true);
178
+                    $regular_price  = $product->get_variation_regular_price('max', true);
179 179
                 }
180 180
 
181 181
                 /**
Please login to merge, or discard this patch.