-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc-products-per-page.php
executable file
·132 lines (101 loc) · 4.14 KB
/
wc-products-per-page.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
<?php
/*
Plugin Name: Products per Page for WooCommerce
Description: This plugin adds a Customizer section to change number of products displayed in WooCommerce Shop page.
Author: tishonator
Version: 1.0.0
Author URI: http://tishonator.com/
Contributors: tishonator
Text Domain: wc-products-per-page
*/
if ( !class_exists('wc_products_per_page') ) :
/**
* Register the plugin.
*
*/
class wc_products_per_page {
/**
* Instance object
*
* @var object
* @see get_instance()
*/
protected static $instance = NULL;
/**
* Constructor
*/
public function __construct() {}
/**
* Setup
*/
public function setup() {
add_action('customize_register', array(&$this, 'customize_register') );
add_filter( 'loop_shop_per_page', array(&$this, 'woocommerce_products_per_page' ), 20 );
}
public function customize_register( $wp_customize ) {
wc_products_per_page::wc_customize_register_woocommerce_settings( $wp_customize );
}
public static function customizer_add_section( $wp_customize, $sectionId, $sectionTitle ) {
$wp_customize->add_section(
$sectionId,
array(
'title' => $sectionTitle,
'capability' => 'edit_theme_options',
)
);
}
private static function customizer_add_customize_control( $wp_customize, $sectionId, $controlId, $controlLabel, $controlDefaultVar, $sanitizeCallback, $type ) {
if ($controlDefaultVar) {
$wp_customize->add_setting( $controlId, array(
'sanitize_callback' => $sanitizeCallback,
'default' => $controlDefaultVar,
) );
} else {
$wp_customize->add_setting( $controlId, array(
'sanitize_callback' => $sanitizeCallback,
) );
}
$wp_customize->add_control( new WP_Customize_Control( $wp_customize, $controlId,
array(
'label' => $controlLabel,
'section' => $sectionId,
'settings' => $controlId,
'type' => $type,
)
)
);
}
private static function customizer_add_number_control( $wp_customize, $sectionId, $controlId, $controlLabel, $controlDefaultVar ) {
wc_products_per_page::customizer_add_customize_control( $wp_customize, $sectionId, $controlId,
$controlLabel, $controlDefaultVar, 'absint', 'number' );
}
public static function wc_customize_register_woocommerce_settings( $wp_customize ) {
// Add WooCommerce Settings Section
wc_products_per_page::customizer_add_section( $wp_customize,
'woocommerce_prodperpage_settings',
__( 'WooCommerce Products per Page', 'wc-products-per-page' ) );
// Add Number of Products per Page
wc_products_per_page::customizer_add_number_control( $wp_customize,
'woocommerce_prodperpage_settings',
'woocommerce_productsperpage', __( 'Products per Page', 'wc-products-per-page' ), 10 );
}
public function woocommerce_products_per_page() {
return wc_products_per_page::read_customizer_option('woocommerce_productsperpage', 10);
}
public static function read_customizer_option($name, $default) {
return get_theme_mod($name, $default);
}
/**
* Used to access the instance
*
* @return object - class instance
*/
public static function get_instance() {
if ( NULL === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
endif; // wc_products_per_page
add_action('plugins_loaded', array( wc_products_per_page::get_instance(), 'setup' ), 10);