Completed
Push — master ( 00af8d...0d3726 )
by Justin
07:13
created

WPSC_Customizer::customizer()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 31
rs 8.439
1
<?php
2
3
/**
4
 * API for interfacing WP eCommerce settings with the Customizer
5
 *
6
 * @package WP eCommerce
7
 * @subpackage Customizer
8
 * @since 4.0
9
 */
10
11
/**
12
 * Class used to implement Customizer ( specifically Selective Refresh ) functionality.
13
 *
14
 * @since 4.0
15
 */
16
17
class WPSC_Customizer {
18
19
    public $settings = array();
20
    public $sections = array();
21
22
    public function __construct() {
23
24
        $this->settings = apply_filters( 'wpsc_customizer_settings', $this->settings );
25
        $this->sections = apply_filters( 'wpsc_customizer_sections', $this->sections );
26
27
        $this->init();
28
    }
29
30
    public function init() {
31
        add_action( 'customize_register', array( $this, 'customizer' ), 100 );
32
    }
33
34
    /**
35
     * Register selective refresh partial.
36
     *
37
     * @param \WP_Customize_Manager $wp_customize Manager.
38
     */
39
    public function customizer( WP_Customize_Manager $wp_customize ) {
40
41
        if ( ! isset( $wp_customize->selective_refresh ) ) {
42
    		return;
43
    	}
44
45
        $wp_customize->add_panel( 'wpsc', array(
46
            'title'       => __( 'Store', 'wp-e-commerce' ),
47
            'description' => __( 'Presentational settings for your store.' ), // Include html tags such as <p>.
48
            'priority'    => 160, // Mixed with top-level-section hierarchy.
49
        ) );
50
51
        foreach ( $this->sections as $name => $label ) {
52
            $wp_customize->add_section( $name, array(
53
                'title' => $label,
54
                'panel' => 'wpsc',
55
            ) );
56
        }
57
58
        foreach ( $this->settings as $name => $settings ) {
59
60
            $wp_customize->add_setting( $name, $settings['setting'] );
61
62
            $wp_customize->add_control( $name, $settings['control'] );
63
64
            if ( isset( $settings['partial'] ) ) {
65
                $wp_customize->selective_refresh->add_partial( $name, $settings['partial'] );
66
            }
67
        }
68
69
    }
70
}
71
72
function wpsc_default_customizer_settings( $settings ) {
73
74
    $settings['wpsc_crop_thumbnails'] = array(
75
            'control' => array(
76
                'type'            => 'checkbox',
77
                'priority'        => 10,
78
                'section'         => 'wpsc_thumbnails',
79
                'label'           => __( 'Crop Thumbnails' ),
80
                'default'         => false,
81
                'description'     => __( 'Crop images to the specified dimensions using center positions.' ),
82
            ),
83
            'setting' => array(
84
                'type'              => 'option',
85
                'capability'        => 'manage_options',
86
                'default'           => false,
87
                'sanitize_callback' => 'esc_attr',
88
            )
89
        );
90
91
        // TODO: We need to create a custom control here for thumbnails...which means some modifications will be necessary.
92
93
/*
94
    	add_image_size(
95
    		'wpsc_product_single_thumbnail',
96
    		get_option( 'single_view_image_width' ),
97
    		get_option( 'single_view_image_height' ),
98
    		$crop
99
    	);
100
101
    	add_image_size(
102
    		'wpsc_product_archive_thumbnail',
103
    		get_option( 'product_image_width' ),
104
    		get_option( 'product_image_height' ),
105
    		$crop
106
    	);
107
108
    	add_image_size(
109
    		'wpsc_product_taxonomy_thumbnail',
110
    		get_option( 'category_image_width' ),
111
    		get_option( 'category_image_height' ),
112
    		$crop
113
    	);
114
*/
115
116
    $settings['wpsc_products_per_row'] = array(
117
        'control' => array(
118
            'type'            => 'number',
119
            'priority'        => 20,
120
            'section'         => 'wpsc_general',
121
            'label'           => __( 'Products Per Page' ),
122
            'default'         => get_option( 'posts_per_page' ),
123
            'description'     => __( 'Set the maximum number of products per page.', 'wp-e-commerce' ),
124
        ),
125
        'setting' => array(
126
            'type'              => 'option',
127
            'capability'        => 'manage_options',
128
            'default'           => 'auto',
129
            'sanitize_callback' => 'is_numeric',
130
        ),
131
        'partial' => array(
132
            'selector'            => '#wpsc-products',
133
            'render_callback'     => function() {
134
                wpsc_get_template_part( 'loop', 'products' );
135
             }
136
        )
137
    );
138
139
    $settings['wpsc_fancy_notifications'] = array(
140
            'control' => array(
141
                'type'            => 'checkbox',
142
                'priority'        => 10,
143
                'section'         => 'wpsc_general',
144
                'label'           => __( 'Add to Cart Notifications' ),
145
                'default'         => false,
146
                'description'     => __( 'Enable Add to Cart notifications. When adding an item to your cart, this will create a popup notification for users.' ),
147
            ),
148
            'setting' => array(
149
                'type'              => 'option',
150
                'capability'        => 'manage_options',
151
                'default'           => false,
152
                'sanitize_callback' => 'esc_attr',
153
            ),
154
            'partial' => array(
155
                'selector'            => '#wpsc-products',
156
                'render_callback'     => function() {
157
                    wpsc_get_template_part( 'loop', 'products' );
158
                 }
159
            )
160
        );
161
162
    $settings['wpsc_layout'] = array(
163
            'control' => array(
164
                'type'            => 'select',
165
                'priority'        => 10,
166
                'section'         => 'wpsc_layout',
167
                'label'           => __( 'Layout' ),
168
                'default'         => 'grid',
169
                'description'     => __( 'Change the layout of your store.' ),
170
                'choices'         => apply_filters( 'wpsc_layouts', array(
171
                    'grid' => __( 'Grid', 'wp-e-commerce' ),
172
                    'list' => __( 'List', 'wp-e-commerce' )
173
                ) )
174
            ),
175
            'setting' => array(
176
                'type'              => 'option',
177
                'capability'        => 'manage_options',
178
                'default'           => 'grid',
179
                'sanitize_callback' => 'sanitize_text_field',
180
            ),
181
            'partial' => array(
182
                'selector'            => '#wpsc-products',
183
                'render_callback'     => function() {
184
                    wpsc_get_template_part( 'loop', 'products' );
185
                 }
186
            )
187
        );
188
189
    $settings['wpsc_products_per_row'] = array(
190
        'control' => array(
191
            'type'            => 'select',
192
            'priority'        => 12,
193
            'section'         => 'wpsc_layout',
194
            'label'           => __( 'Products Per Row' ),
195
            'default'         => 'auto',
196
            'description'     => __( 'Set the maximum number of products per row. Defaults to showing as many as will fit, up to six products per row', 'wp-e-commerce' ),
197
            'choices'         => apply_filters( 'wpsc_products_per_row_options', array(
198
                'auto' => __( 'Automatic', 'wp-e-commerce' ),
199
                '1'    => __( '1', 'wp-e-commerce' ),
200
                '2'    => __( '2', 'wp-e-commerce' ),
201
                '3'    => __( '3', 'wp-e-commerce' ),
202
                '4'    => __( '4', 'wp-e-commerce' ),
203
                '5'    => __( '5', 'wp-e-commerce' ),
204
                '6'    => __( '6', 'wp-e-commerce' ),
205
            ) )
206
        ),
207
        'setting' => array(
208
            'type'              => 'option',
209
            'capability'        => 'manage_options',
210
            'default'           => 'auto',
211
            'sanitize_callback' => 'is_numeric',
212
        ),
213
        'partial' => array(
214
            'selector'            => '#wpsc-products',
215
            'render_callback'     => function() {
216
                wpsc_get_template_part( 'loop', 'products' );
217
             }
218
        )
219
    );
220
221
    return $settings;
222
}
223
224
add_filter( 'wpsc_customizer_settings', 'wpsc_default_customizer_settings' );
225
226
function wpsc_default_customizer_sections( $sections ) {
227
    return array_merge( array(
228
        'wpsc_general'    => __( 'General', 'wp-e-commerce' ),
229
        'wpsc_layout'     => __( 'Layout', 'wp-e-commerce' ),
230
        'wpsc_thumbnails' => __( 'Thumbnails', 'wp-e-commerce' ),
231
    ), $sections );
232
}
233
234
add_filter( 'wpsc_customizer_sections', 'wpsc_default_customizer_sections' );
235
236
$c = new WPSC_Customizer();
237
$c->init();
238