PluginProbe
WooReer / 2.2.3
WooReer v2.2.3
trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 3.0.0 3.0.1 All 34 releases
wcsdm / includes / classes / class-wcsdm.php

class-wcsdm.php in WooReer 2.2.3, at includes/classes/class-wcsdm.php

439 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file that defines the core plugin class
4 *
5 * A class definition that includes attributes and functions used across both the
6 * public-facing side of the site and the admin area.
7 *
8 * @link https://github.com/sofyansitorus
9 * @since 1.0.0
10 *
11 * @package Wcsdm
12 * @subpackage Wcsdm/includes
13 */
14
15 // If this file is called directly, abort.
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * The core plugin class.
22 *
23 * This is used to define internationalization, admin-specific hooks, and
24 * public-facing site hooks.
25 *
26 * Also maintains the unique identifier of this plugin as well as the current
27 * version of the plugin.
28 *
29 * @since 1.0.0
30 * @package Wcsdm
31 * @subpackage Wcsdm/includes
32 * @author Sofyan Sitorus <sofyansitorus@gmail.com>
33 */
34 class Wcsdm {
35
36 /**
37 * Hold an instance of the class
38 *
39 * @var Wcsdm
40 */
41 private static $instance = null;
42
43 /**
44 * The object is created from within the class itself
45 * only if the class has no instance.
46 *
47 * @return Wcsdm
48 */
49 public static function get_instance() {
50 if ( null === self::$instance ) {
51 self::$instance = new Wcsdm();
52 }
53
54 return self::$instance;
55 }
56
57 /**
58 * Class Constructor
59 *
60 * @since 1.0.0
61 */
62 private function __construct() {
63 // Hook to load plugin textdomain.
64 add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
65
66 // Hook to add plugin action links.
67 add_action( 'plugin_action_links_' . plugin_basename( WCSDM_FILE ), array( $this, 'plugin_action_links' ) );
68
69 // Hook to register the shipping method.
70 add_filter( 'woocommerce_shipping_methods', array( $this, 'register_shipping_method' ) );
71
72 // Hook to AJAX actions.
73 add_action( 'wp_ajax_wcsdm_validate_api_key_server', array( $this, 'validate_api_key_server' ) );
74
75 // Hook to modify after shipping calculator form.
76 add_action( 'woocommerce_after_shipping_calculator', array( $this, 'after_shipping_calculator' ) );
77
78 // Hook to woocommerce_cart_shipping_packages to inject filed address_2.
79 add_filter( 'woocommerce_cart_shipping_packages', array( $this, 'inject_cart_shipping_packages' ) );
80
81 // Hook to enqueue scripts & styles assets.
82 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_backend_assets' ), 999 );
83 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ), 999 );
84
85 // Hook to declare compatibility with the High-Performance Order Storage.
86 add_action(
87 'before_woocommerce_init',
88 function() {
89 if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
90 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', WCSDM_FILE, true );
91 }
92 }
93 );
94
95 // Hook to declare incompatibility with the Cart and Checkout Blocks.
96 add_action(
97 'before_woocommerce_init',
98 function() {
99 if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
100 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, false );
101 }
102 }
103 );
104 }
105
106 /**
107 * Load plugin textdomain.
108 *
109 * @since 1.0.0
110 */
111 public function load_textdomain() {
112 load_plugin_textdomain( 'wcsdm', false, basename( WCSDM_PATH ) . '/languages' );
113 }
114
115 /**
116 * Add plugin action links.
117 *
118 * Add a link to the settings page on the plugins.php page.
119 *
120 * @since 1.1.3
121 *
122 * @param array $links List of existing plugin action links.
123 * @return array List of modified plugin action links.
124 */
125 public function plugin_action_links( $links ) {
126 $zone_id = 0;
127
128 if ( ! class_exists( 'WC_Shipping_Zones' ) ) {
129 return $links;
130 }
131
132 foreach ( WC_Shipping_Zones::get_zones() as $zone ) {
133 if ( empty( $zone['shipping_methods'] ) || empty( $zone['zone_id'] ) ) {
134 continue;
135 }
136
137 foreach ( $zone['shipping_methods'] as $zone_shipping_method ) {
138 if ( $zone_shipping_method instanceof Wcsdm_Shipping_Method ) {
139 $zone_id = $zone['zone_id'];
140 break;
141 }
142 }
143
144 if ( $zone_id ) {
145 break;
146 }
147 }
148
149 $links = array_merge(
150 array(
151 '<a href="' . esc_url(
152 add_query_arg(
153 array(
154 'page' => 'wc-settings',
155 'tab' => 'shipping',
156 'zone_id' => $zone_id,
157 'wcsdm_settings' => true,
158 ),
159 admin_url( 'admin.php' )
160 )
161 ) . '">' . __( 'Settings', 'wcsdm' ) . '</a>',
162 ),
163 $links
164 );
165
166 return $links;
167 }
168
169 /**
170 * Enqueue backend scripts.
171 *
172 * @since 1.0.0
173 * @param string $hook Passed screen ID in admin area.
174 */
175 public function enqueue_backend_assets( $hook = null ) {
176 if ( false === strpos( $hook, 'wc-settings' ) ) {
177 return;
178 }
179
180 $is_dev_env = wcsdm_is_dev_env();
181
182 // Define the styles URL.
183 $css_url = WCSDM_URL . 'assets/css/wcsdm-backend-legacy.min.css';
184 if ( $is_dev_env ) {
185 $css_url = add_query_arg( array( 't' => time() ), str_replace( '.min', '', $css_url ) );
186 }
187
188 // Enqueue admin styles.
189 wp_enqueue_style(
190 'wcsdm-backend-legacy', // Give the script a unique ID.
191 $css_url, // Define the path to the JS file.
192 array(), // Define dependencies.
193 WCSDM_VERSION, // Define a version (optional).
194 false // Specify whether to put in footer (leave this false).
195 );
196
197 if ( version_compare( WC()->version, '8.4.0', '>=' ) ) {
198 // Define the styles URL.
199 $css_url = WCSDM_URL . 'assets/css/wcsdm-backend.min.css';
200 if ( $is_dev_env ) {
201 $css_url = add_query_arg( array( 't' => time() ), str_replace( '.min', '', $css_url ) );
202 }
203
204 // Enqueue admin styles.
205 wp_enqueue_style(
206 'wcsdm-backend', // Give the script a unique ID.
207 $css_url, // Define the path to the JS file.
208 array(), // Define dependencies.
209 WCSDM_VERSION, // Define a version (optional).
210 false // Specify whether to put in footer (leave this false).
211 );
212 }
213
214 // Define the scripts URL.
215 $js_url = WCSDM_URL . 'assets/js/wcsdm-backend.min.js';
216 if ( $is_dev_env ) {
217 $js_url = add_query_arg( array( 't' => time() ), str_replace( '.min', '', $js_url ) );
218 }
219
220 // Enqueue admin scripts.
221 wp_enqueue_script(
222 'wcsdm-backend', // Give the script a unique ID.
223 $js_url, // Define the path to the JS file.
224 array( 'jquery' ), // Define dependencies.
225 WCSDM_VERSION, // Define a version (optional).
226 true // Specify whether to put in footer (leave this true).
227 );
228
229 // Localize the script data.
230 wp_localize_script(
231 'wcsdm-backend',
232 'wcsdmBackendVars',
233 array(
234 'showSettings' => isset( $_GET['wcsdm_settings'] ) && is_admin(), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
235 'methodId' => WCSDM_METHOD_ID,
236 'methodTitle' => WCSDM_METHOD_TITLE,
237 'marker' => WCSDM_URL . 'assets/img/marker.png',
238 'defaultLat' => WCSDM_DEFAULT_LAT,
239 'defaultLng' => WCSDM_DEFAULT_LNG,
240 'testLat' => WCSDM_TEST_LAT,
241 'testLng' => WCSDM_TEST_LNG,
242 'language' => get_locale(),
243 'isDevEnv' => $is_dev_env,
244 'i18n' => wcsdm_i18n(),
245 'ajax_url' => admin_url( 'admin-ajax.php' ),
246 'validate_api_key_nonce' => wp_create_nonce( 'wcsdm_validate_api_key_server' ),
247 )
248 );
249 }
250
251 /**
252 * Enqueue frontend scripts.
253 *
254 * @since 1.0.0
255 */
256 public function enqueue_frontend_assets() {
257 // Bail early if there is no instances enabled.
258 if ( ! wcsdm_instances() ) {
259 return;
260 }
261
262 $is_cart_page = function_exists( 'is_cart' ) && is_cart();
263 $is_checkout_page = function_exists( 'is_checkout' ) && is_checkout();
264
265 $enqueue_frontend_assets = apply_filters( 'wcsdm_enqueue_frontend_assets', ( $is_cart_page | $is_checkout_page ) );
266
267 if ( ! $enqueue_frontend_assets ) {
268 return;
269 }
270
271 $is_dev_env = wcsdm_is_dev_env();
272
273 // Define scripts URL.
274 $js_url = WCSDM_URL . 'assets/js/wcsdm-frontend.min.js';
275
276 if ( $is_dev_env ) {
277 $js_url = add_query_arg( array( 't' => time() ), str_replace( '.min', '', $js_url ) );
278 }
279
280 // Enqueue scripts.
281 wp_enqueue_script(
282 'wcsdm-frontend', // Give the script a unique ID.
283 $js_url, // Define the path to the JS file.
284 array( 'jquery', 'wp-util' ), // Define dependencies.
285 WCSDM_VERSION, // Define a version (optional).
286 true // Specify whether to put in footer (leave this true).
287 );
288
289 $wcsdm_frontend_vars = apply_filters(
290 'wcsdm_frontend_vars',
291 array(
292 'forms' => array(
293 'calc_shipping' => array(
294 'wrapper' => '.shipping-calculator-form',
295 'fields' => wcsdm_calc_shipping_fields(),
296 ),
297 'billing' => array(
298 'wrapper' => '.woocommerce-billing-fields__field-wrapper',
299 'fields' => wcsdm_billing_fields(),
300 ),
301 'shipping' => array(
302 'wrapper' => '.woocommerce-shipping-fields__field-wrapper',
303 'fields' => wcsdm_shipping_fields(),
304 ),
305 ),
306 )
307 );
308
309 wp_localize_script( 'wcsdm-frontend', 'wcsdmFrontendVars', $wcsdm_frontend_vars );
310 }
311
312 /**
313 * Register shipping method to WooCommerce.
314 *
315 * @since 1.0.0
316 * @param array $methods registered shipping methods.
317 */
318 public function register_shipping_method( $methods ) {
319 if ( class_exists( 'Wcsdm_Shipping_Method' ) ) {
320 $methods[ WCSDM_METHOD_ID ] = 'Wcsdm_Shipping_Method';
321 }
322
323 return $methods;
324 }
325
326 /**
327 * Print hidden element for the custom address 1 field and address 2 field value
328 * in shipping calculator form.
329 *
330 * @since 2.0
331 * @return void
332 */
333 public function after_shipping_calculator() {
334 // Bail early if there is no instances enabled.
335 if ( ! wcsdm_instances() ) {
336 return;
337 }
338
339 $calc_shipping_fields = wcsdm_calc_shipping_fields();
340
341 if ( ! $calc_shipping_fields ) {
342 return;
343 }
344
345 $custom_fields = array(
346 'address_1',
347 'address_2',
348 );
349
350 foreach ( $calc_shipping_fields as $key => $field ) {
351 if ( ! in_array( $key, $custom_fields, true ) ) {
352 continue;
353 }
354
355 $value = call_user_func( array( WC()->cart->get_customer(), 'get_shipping_' . $key ) );
356 ?>
357 <input type="hidden" id="wcsdm-calc-shipping-value-field--<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $value ); ?>" />
358 <script type="text/template" id="tmpl-wcsdm-calc-shipping-custom-field--<?php echo esc_attr( $key ); ?>">
359 <p class="form-row form-row-wide" id="calc_shipping_{{ data.fieldKey }}_field">
360 <input type="text" class="input-text" value="{{ data.value }}" placeholder="{{ data.placeholder }}" name="calc_shipping_{{ data.fieldKey }}" id="calc_shipping_{{ data.fieldKey }}" />
361 </p>
362 </script>
363 <?php
364 }
365 }
366
367 /**
368 * AJAX handler for Server Side API Key validation.
369 *
370 * @since 2.0.8
371 *
372 * @return void
373 */
374 public function validate_api_key_server() {
375 $key = isset( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : '';
376 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wcsdm_validate_api_key_server' ) ) {
377 wp_send_json_error( 'Sorry, your nonce did not verify.', 400 );
378 }
379
380 if ( ! $key ) {
381 $key = 'InvalidKey';
382 }
383
384 $distance = Wcsdm_API::calculate_distance(
385 array(
386 'key' => $key,
387 ),
388 true
389 );
390
391 if ( is_wp_error( $distance ) ) {
392 wp_send_json_error( $distance->get_error_message(), 400 );
393 }
394
395 wp_send_json_success( $distance );
396 }
397
398 /**
399 * Inject cart cart packages to calculate shipping for address fields.
400 *
401 * @since 1.0.0
402 * @param array $packages Current cart contents packages.
403 * @return array
404 */
405 public function inject_cart_shipping_packages( $packages ) {
406 if ( ! wcsdm_is_calc_shipping() ) {
407 return $packages;
408 }
409
410 $address_fields = array(
411 'address_1' => false,
412 'address_2' => false,
413 );
414
415 foreach ( array_keys( $address_fields ) as $field_key ) {
416 $address_fields[ $field_key ] = wcsdm_calc_shipping_field_value( 'calc_shipping_' . $field_key );
417 }
418
419 foreach ( array_keys( $packages ) as $package_key ) {
420 foreach ( $address_fields as $field_key => $field_value ) {
421 if ( false === $field_value ) {
422 continue;
423 }
424
425 // Set customer billing address.
426 call_user_func( array( WC()->customer, 'set_billing_' . $field_key ), $field_value );
427
428 // Set customer shipping address.
429 call_user_func( array( WC()->customer, 'set_shipping_' . $field_key ), $field_value );
430
431 // Set package destination address.
432 $packages[ $package_key ]['destination'][ $field_key ] = $field_value;
433 }
434 }
435
436 return $packages;
437 }
438 }
439