-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclass-wc-remove-product-sorting.php
547 lines (431 loc) · 15.1 KB
/
class-wc-remove-product-sorting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<?php
/**
* WooCommerce Remove Product Sorting
*
* This source file is subject to the GNU General Public License v3.0
* that is bundled with this package in the file license.txt.
* It is also available through the world-wide-web at this URL:
* http://www.gnu.org/licenses/gpl-3.0.html
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade WooCommerce Remove Product Sorting to newer
* versions in the future. If you wish to customize WooCommerce Remove Product Sorting for your
* needs please refer to http://skyverge.com/products/woocommerce-remove-product-sorting/ for more information.
*
* @package WC-Remove-Product-Sorting
* @author SkyVerge
* @category Admin
* @copyright Copyright (c) 2014-2018, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/
namespace SkyVerge\WooCommerce\RemoveProductSorting;
defined( 'ABSPATH' ) or exit;
use SkyVerge\WooCommerce\PluginUpdater as Updater;
/**
* Class Plugin.
* Sets up the main plugin class.
*
* @since 1.1.0
*/
class Plugin {
const VERSION = '1.2.0';
/** @var \SkyVerge\WooCommerce\RemoveProductSorting\Plugin single instance of this plugin */
protected static $instance;
/** @var Updater\License $license license class instance */
protected $license;
/**
* \SkyVerge\WooCommerce\RemoveProductSorting\Plugin constructor. Initializes the plugin.
*
* @since 1.1.0
*/
public function __construct() {
// load translations
add_action( 'init', array( $this, 'load_translation' ) );
// modify sorting options
add_filter( 'woocommerce_default_catalog_orderby_options', array( $this, 'remove_sorting_from_settings' ) );
add_filter( 'woocommerce_catalog_orderby', array( $this, 'remove_frontend_sorting_option' ) );
// add settings to product display settings
if ( self::is_wc_gte( '3.3' ) ) {
add_action( 'customize_register', array( $this, 'add_settings' ) );
} else {
add_filter( 'woocommerce_product_settings', array( $this, 'legacy_add_settings' ) );
}
// unhook the sorting dropdown completely if there are no options
add_action( 'wp', array( $this, 'maybe_remove_catalog_orderby' ), 99 );
$this->includes();
if ( is_admin() && ! is_ajax() ) {
// add plugin links
add_filter( 'plugin_action_links_' . plugin_basename( $this->get_plugin_file() ), array( $this, 'add_plugin_links' ) );
$this->install();
}
}
/**
* Loads plugin files.
*
* @since 1.0.0
*/
public function includes() {
if ( ! class_exists( 'Updater\\License' ) ) {
require_once( $this->get_plugin_path() . '/lib/skyverge/updater/class-skyverge-plugin-license.php');
}
// item ID is from skyverge.com download WP_Post ID
$this->license = new Updater\License( $this->get_plugin_file(), $this->get_plugin_path(), $this->get_plugin_url(), $this->get_plugin_name(), $this->get_version(), 4589 );
}
/** Plugin methods ******************************************************/
/**
* Removes sorting option from the Product Settings
*
* @since 1.1.0
*
* @param array $options the settings options
* @return array updated options
*/
public function remove_sorting_from_settings( $options ) {
foreach( $this->get_removed_sorting_options() as $remove ) {
unset( $options[ $remove ] );
}
return $options;
}
/**
* Removes sorting option from the shop template
*
* @since 1.1.0
*
* @param array $orderby the orderby options
* @return array updated options
*/
public function remove_frontend_sorting_option( $orderby ) {
foreach( $this->get_removed_sorting_options() as $remove ) {
unset( $orderby[ $remove ] );
}
return $orderby;
}
/**
* Add Settings to WooCommerce Settings > Products page after "Default Product Sorting" setting
*
* @since 1.1.0
*
* @param array $settings the settings options
* @return array updated settings
*/
public function legacy_add_settings( $settings ) {
$updated_settings = array();
foreach ( $settings as $setting ) {
$updated_settings[] = $setting;
if ( isset( $setting['id'] ) && 'woocommerce_default_catalog_orderby' === $setting['id'] ) {
$updated_settings[] = array(
'name' => __( 'Remove Product Sorting:', 'woocommerce-remove-product-sorting' ),
'desc_tip' => __( 'Choose sorting options to remove from your shop.', 'woocommerce-remove-product-sorting' ),
'id' => 'wc_remove_product_sorting',
'type' => 'multiselect',
'class' => 'chosen_select wc_enhanced_select',
'default' => '',
'options' => $this->get_core_sorting_options(),
'custom_attributes' => array(
'data-placeholder' => __( 'Choose sorting options to remove from your shop.', 'woocommerce-remove-product-sorting' ),
),
);
}
}
return $updated_settings;
}
/**
* Add Settings to WooCommerce Settings > Products page after "Default Product Sorting" setting.
*
* @since 1.0.0
*
* @param \WP_Customize_Manager $wp_customize
*/
public function add_settings( $wp_customize ) {
// load our custom control type
require_once( dirname( __FILE__ ) . '/includes/class-wc-remove-sorting-customizer-checkbox-multiple.php' );
// make sure we can insert our desired controls where we want them {BR 2018-02-10}
// this is heavy-handed, but WC core doesn't add priorities for us, shikata ga nai ¯\_(ツ)_/¯
if ( $catalog_columns_control = $wp_customize->get_control( 'woocommerce_catalog_columns' ) ) {
$catalog_columns_control->priority = 15;
}
if ( $catalog_rows_control = $wp_customize->get_control( 'woocommerce_catalog_rows' ) ) {
$catalog_rows_control->priority = 15;
}
$wp_customize->add_setting(
'wc_remove_product_sorting',
array(
'default' => array(),
'capability' => 'manage_woocommerce',
'sanitize_callback' => array( $this, 'sanitize_option_list' ),
)
);
$wp_customize->add_control(
new Customize_Checkbox_Multiple(
$wp_customize,
'wc_remove_product_sorting',
array(
'type' => 'checkbox-multiple',
'label' => __( 'Remove Product Sorting:', 'woocommerce-remove-product-sorting' ),
'description' => __( 'Choose sorting options to remove from your shop.', 'woocommerce-remove-product-sorting' ),
'section' => 'woocommerce_product_catalog',
'priority' => 10,
'choices' => $this->get_core_sorting_options(),
)
)
);
}
/**
* Unhooks the sorting dropdown if all options have been removed.
*
* @since 1.2.0
*/
public function maybe_remove_catalog_orderby() {
$enabled = array_diff( array_keys( $this->get_core_sorting_options() ), array_values( $this->get_removed_sorting_options() ) );
$active_plugins = (array) get_option( 'active_plugins', array() );
$extra_sorting_plugin = 'woocommerce-extra-product-sorting-options/woocommerce-extra-product-sorting-options.php';
if ( is_multisite() ) {
$active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
}
// check for our extra sorting options plugin, just in case there are custom options added, too
if ( in_array( $extra_sorting_plugin, $active_plugins ) || array_key_exists( $extra_sorting_plugin, $active_plugins ) ) {
$extra_sorting = self::is_wc_gte( '3.3' ) ? get_theme_mod( 'wc_extra_product_sorting_options', array() ) : get_option( 'wc_extra_product_sorting_options', array() );
$enabled = array_merge( $enabled, $extra_sorting );
}
/**
* Filters whether the sorting dropdown should be unhooked from the shop page when there are no core sorting options.
*
* @since 1.2.0
*
* @param bool $remove true if the dropdown should be removed
*/
if ( empty( $enabled ) && apply_filters( 'wc_remove_sorting_options_hide_dropdown', true ) ) {
// WC core output
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
// Storefront theme
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 10 );
remove_action( 'woocommerce_after_shop_loop', 'woocommerce_catalog_ordering', 10 );
}
}
/**
* Helper to get WooCommerce core sorting options.
*
* @since 1.0.0
*
* @return string[]
*/
protected function get_core_sorting_options() {
// the woocommerce text domain here is intentional
// we're also not filtering this given we probably don't need to remove *custom* sorting for people
return array(
'menu_order' => __( 'Default sorting (custom ordering + name)', 'woocommerce' ),
'popularity' => __( 'Popularity (sales)', 'woocommerce' ),
'rating' => __( 'Average rating', 'woocommerce' ),
'date' => __( 'Sort by most recent', 'woocommerce' ),
'price' => __( 'Sort by price (asc)', 'woocommerce' ),
'price-desc' => __( 'Sort by price (desc)', 'woocommerce' ),
);
}
/**
* Sanitize the default sorting callback.
*
* @since 1.0.0
*
* @param string[] $values the option value
* @return string[]
*/
public function sanitize_option_list( $values ) {
$multi_values = ! is_array( $values ) ? explode( ',', $values ) : $values;
return ! empty( $multi_values ) ? array_map( 'sanitize_text_field', $multi_values ) : array();
}
/**
* Gets the sorting options that should be removed.
*
* @since 1.2.0
*
* @return array options removed
*/
private function get_removed_sorting_options() {
return self::is_wc_gte( '3.3' ) ? get_theme_mod('wc_remove_product_sorting', array() ) : get_option( 'wc_remove_product_sorting', array() );
}
/** Helper methods ******************************************************/
/**
* Gets the updater class instance.
*
* @since 1.0.0
*
* @return Updater\License
*/
public function get_license_instance() {
return $this->license;
}
/**
* Main Remove Sorting Instance, ensures only one instance is/can be loaded.
*
* @since 1.1.0
* @see wc_remove_product_sorting()
*
* @return \SkyVerge\WooCommerce\RemoveProductSorting\Plugin
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Cloning instances is forbidden due to singleton pattern.
*
* @since 1.1.0
*/
public function __clone() {
/* translators: Placeholders: %s - plugin name */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot clone instances of %s.', 'woocommerce-remove-product-sorting' ), 'WooCommerce Remove Product Sorting' ), '1.1.0' );
}
/**
* Unserializing instances is forbidden due to singleton pattern.
*
* @since 1.1.0
*/
public function __wakeup() {
/* translators: Placeholders: %s - plugin name */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot unserialize instances of %s.', 'woocommerce-remove-product-sorting' ), 'WooCommerce Remove Product Sorting' ), '1.1.0' );
}
/**
* Helper to get the plugin URL.
*
* @since 1.1.0
*
* @return string the plugin URL
*/
public function get_plugin_url() {
return untrailingslashit( plugins_url( '/', $this->get_file() ) );
}
/**
* Helper to return the plugin name.
*
* @since 1.0.0
*
* @return string plugin name
*/
public function get_plugin_name() {
return __( 'WooCommerce Remove Product Sorting', 'woocommerce-remove-product-sorting' );
}
/**
* Helper to get the plugin path.
*
* @since 1.0.0
*
* @return string the plugin path
*/
public function get_plugin_path() {
return untrailingslashit( plugin_dir_path( $this->get_file() ) );
}
/**
* Helper to get the plugin file.
*
* @since 1.0.0
*
* @return string the plugin version
*/
public function get_file() {
return __FILE__;
}
/**
* Gets the main plugin file.
*
* @since 1.0.0
*
* @return string
*/
public function get_plugin_file() {
$slug = dirname( plugin_basename( $this->get_file() ) );
return trailingslashit( $slug ) . $slug . '.php';
}
/**
* Helper to get the plugin version.
*
* @since 1.0.0
*
* @return string the plugin version
*/
public function get_version() {
return self::VERSION;
}
/**
* Adds plugin page links.
*
* @since 1.1.0
*
* @param array $links all plugin links
* @return array $links all plugin links + our custom links (i.e., "Settings")
*/
public function add_plugin_links( $links ) {
if ( self::is_wc_gte( '3.3' ) ) {
$configure_url = admin_url( 'customize.php?url=' . wc_get_page_permalink( 'shop' ) . '&autofocus[section]=woocommerce_product_catalog' );
} else {
$configure_url = admin_url( 'admin.php?page=wc-settings&tab=products§ion=display' );
}
$license_text = $this->get_license_instance()->is_license_valid() ? __( 'License', 'woocommerce-memberships-directory-shortcode' ) : __( 'Get updates', 'woocommerce-memberships-directory-shortcode' );
$plugin_links = array(
'<a href="' . esc_url( $configure_url ) . '">' . __( 'Configure', 'woocommerce-extra-product-sorting-options' ) . '</a>',
'<a href="http://www.skyverge.com/product/woocommerce-remove-product-sorting/">'. __( 'FAQ', 'woocommerce-extra-product-sorting-options' ) . '</a>',
'<a href="' . $this->get_license_instance()->get_license_settings_url() . '">' . esc_html( $license_text ) . '</a>',
);
return array_merge( $plugin_links, $links );
}
/**
* Load Translations
*
* @since 1.1.0
*/
public function load_translation() {
// localization
load_plugin_textdomain( 'woocommerce-remove-product-sorting', false, dirname( plugin_basename( __FILE__ ) ) . '/i18n/languages' );
}
/**
* Checks if WooCommerce is greater than a specific version.
*
* @internal
*
* @since 1.1.0
*
* @param string $version version number
* @return bool true if > version
*/
public static function is_wc_gte( $version ) {
return defined( 'WC_VERSION' ) && WC_VERSION && version_compare( WC_VERSION, $version, '>=' );
}
/** Lifecycle methods ******************************************************/
/**
* Run every time. Used since the activation hook is not executed when updating a plugin.
*
* @since 2.0.0
*/
private function install() {
// get current version to check for upgrade
$installed_version = get_option( 'wc_remove_product_sorting_version' );
// force upgrade to 1.1.0, prior versions did not have version option set
if ( ! $installed_version && ! get_option( 'wc_remove_product_sorting_version' ) ) {
$this->upgrade( '1.1.0' );
}
// upgrade if installed version lower than plugin version
if ( -1 === version_compare( $installed_version, self::VERSION ) ) {
$this->upgrade( $installed_version );
}
}
/**
* Perform any version-related changes.
*
* @since 1.1.0
*
* @param string $installed_version the currently installed version of the plugin
*/
private function upgrade( $installed_version ) {
// upgrade to 1.1.0; migrate option to theme mod
if ( '1.1.0' === $installed_version ) {
set_theme_mod( 'wc_remove_product_sorting', get_option( 'wc_remove_product_sorting', array() ) );
}
// update the installed version option
update_option( 'wc_remove_product_sorting_version', self::VERSION );
}
}