Completed
Push — master ( 6b4bda...747b89 )
by Justin
07:06
created

build_field_html()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 9.6666
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
        $wp_customize->register_control_type( 'WPSC_Customizer_Thumbnail_Control' );
42
43
        if ( ! isset( $wp_customize->selective_refresh ) ) {
44
    		return;
45
    	}
46
47
        $wp_customize->add_panel( 'wpsc', array(
48
            'title'       => __( 'Store', 'wp-e-commerce' ),
49
            'description' => __( 'Presentational settings for your store.' ), // Include html tags such as <p>.
50
            'priority'    => 160, // Mixed with top-level-section hierarchy.
51
        ) );
52
53
        foreach ( $this->sections as $name => $label ) {
54
            $wp_customize->add_section( $name, array(
55
                'title' => $label,
56
                'panel' => 'wpsc',
57
            ) );
58
        }
59
60
        foreach ( $this->settings as $name => $settings ) {
61
62
            $wp_customize->add_setting( $name, $settings['setting'] );
63
64
            if ( isset( $settings['control']['class'] ) && 'WP_Customize_Control' == get_parent_class( $settings['control']['class'] ) ) {
65
                $control = $settings['control']['class'];
66
                $wp_customize->add_control( new $control( $wp_customize, $name, $settings['control'] ) );
67
            } else {
68
                $wp_customize->add_control( $name, $settings['control'] );
69
            }
70
71
            if ( isset( $settings['partial'] ) ) {
72
                $wp_customize->selective_refresh->add_partial( $name, $settings['partial'] );
73
            }
74
        }
75
76
    }
77
}
78
79
function wpsc_default_customizer_settings( $settings ) {
80
81
    $settings['wpsc_crop_thumbnails'] = array(
82
            'control' => array(
83
                'type'            => 'checkbox',
84
                'priority'        => 10,
85
                'section'         => 'wpsc_thumbnails',
86
                'label'           => __( 'Crop Thumbnails' ),
87
                'default'         => false,
88
                'description'     => __( 'Crop images to the specified dimensions using center positions.' ),
89
            ),
90
            'setting' => array(
91
                'type'              => 'option',
92
                'capability'        => 'manage_options',
93
                'default'           => false,
94
                'sanitize_callback' => 'esc_attr',
95
            )
96
        );
97
98
        // TODO: We need to create a custom control here for thumbnails...which means some modifications will be necessary.
99
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
100
101
        /*
102
    	add_image_size(
103
    		'wpsc_product_single_thumbnail',
104
    		get_option( 'single_view_image_width' ),
105
    		get_option( 'single_view_image_height' ),
106
    		$crop
107
    	);
108
109
    	add_image_size(
110
    		'wpsc_product_archive_thumbnail',
111
    		get_option( 'product_image_width' ),
112
    		get_option( 'product_image_height' ),
113
    		$crop
114
    	);
115
116
    	add_image_size(
117
    		'wpsc_product_taxonomy_thumbnail',
118
    		get_option( 'category_image_width' ),
119
    		get_option( 'category_image_height' ),
120
    		$crop
121
    	);
122
*/
123
124
    $settings['wpsc_products_per_page'] = array(
125
        'control' => array(
126
            'type'            => 'number',
127
            'priority'        => 20,
128
            'section'         => 'wpsc_general',
129
            'label'           => __( 'Products Per Page' ),
130
            'default'         => get_option( 'posts_per_page' ),
131
            'description'     => __( 'Set the maximum number of products per page.', 'wp-e-commerce' ),
132
        ),
133
        'setting' => array(
134
            'type'              => 'option',
135
            'capability'        => 'manage_options',
136
            'default'           => 'auto',
137
            'sanitize_callback' => 'is_numeric',
138
        ),
139
        'partial' => array(
140
            'selector'            => '#wpsc-products',
141
            'render_callback'     => function() {
142
                wpsc_get_template_part( 'loop', 'products' );
143
             }
144
        )
145
    );
146
147
    $settings['wpsc_fancy_notifications'] = array(
148
            'control' => array(
149
                'type'            => 'checkbox',
150
                'priority'        => 10,
151
                'section'         => 'wpsc_general',
152
                'label'           => __( 'Add to Cart Notifications' ),
153
                'default'         => false,
154
                'description'     => __( 'Enable Add to Cart notifications. When adding an item to your cart, this will create a popup notification for users.' ),
155
            ),
156
            'setting' => array(
157
                'type'              => 'option',
158
                'capability'        => 'manage_options',
159
                'default'           => false,
160
                'sanitize_callback' => 'esc_attr',
161
            ),
162
            'partial' => array(
163
                'selector'            => '#wpsc-products',
164
                'render_callback'     => function() {
165
                    wpsc_get_template_part( 'loop', 'products' );
166
                 }
167
            )
168
        );
169
170
    $settings['wpsc_layout'] = array(
171
            'control' => array(
172
                'type'            => 'select',
173
                'priority'        => 10,
174
                'section'         => 'wpsc_layout',
175
                'label'           => __( 'Layout' ),
176
                'default'         => 'grid',
177
                'description'     => __( 'Change the layout of your store.' ),
178
                'choices'         => apply_filters( 'wpsc_layouts', array(
179
                    'grid' => __( 'Grid', 'wp-e-commerce' ),
180
                    'list' => __( 'List', 'wp-e-commerce' )
181
                ) )
182
            ),
183
            'setting' => array(
184
                'type'              => 'option',
185
                'capability'        => 'manage_options',
186
                'default'           => 'grid',
187
                'sanitize_callback' => 'sanitize_text_field',
188
            ),
189
            'partial' => array(
190
                'selector'            => '#wpsc-products',
191
                'render_callback'     => function() {
192
                    wpsc_get_template_part( 'loop', 'products' );
193
                 }
194
            )
195
        );
196
197
    $settings['wpsc_products_per_row'] = array(
198
        'control' => array(
199
            'type'            => 'select',
200
            'priority'        => 12,
201
            'section'         => 'wpsc_layout',
202
            'label'           => __( 'Products Per Row' ),
203
            'default'         => 'auto',
204
            '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' ),
205
            'choices'         => apply_filters( 'wpsc_products_per_row_options', array(
206
                'auto' => __( 'Automatic', 'wp-e-commerce' ),
207
                '1'    => __( '1', 'wp-e-commerce' ),
208
                '2'    => __( '2', 'wp-e-commerce' ),
209
                '3'    => __( '3', 'wp-e-commerce' ),
210
                '4'    => __( '4', 'wp-e-commerce' ),
211
                '5'    => __( '5', 'wp-e-commerce' ),
212
                '6'    => __( '6', 'wp-e-commerce' ),
213
            ) )
214
        ),
215
        'setting' => array(
216
            'type'              => 'option',
217
            'capability'        => 'manage_options',
218
            'default'           => 'auto',
219
            'sanitize_callback' => 'is_numeric',
220
        ),
221
        'partial' => array(
222
            'selector'            => '#wpsc-products',
223
            'render_callback'     => function() {
224
                wpsc_get_template_part( 'loop', 'products' );
225
             }
226
        )
227
    );
228
229
    return $settings;
230
}
231
232
add_filter( 'wpsc_customizer_settings', 'wpsc_default_customizer_settings' );
233
234
function wpsc_default_customizer_sections( $sections ) {
235
    return array_merge( array(
236
        'wpsc_general'    => __( 'General', 'wp-e-commerce' ),
237
        'wpsc_layout'     => __( 'Layout', 'wp-e-commerce' ),
238
        'wpsc_thumbnails' => __( 'Thumbnails', 'wp-e-commerce' ),
239
    ), $sections );
240
}
241
242
add_filter( 'wpsc_customizer_sections', 'wpsc_default_customizer_sections' );
243
244
$c = new WPSC_Customizer();
245
$c->init();
246
247
/**
248
 * Custom control for Customizer.
249
 *
250
 * Allows us to have a width and a height input for thumbnail settings.
251
 *
252
 * @package WP eCommerce
253
 * @subpackage Customizer
254
 * @since 4.0
255
 */
256
257
add_action( 'customize_register', function() {
258
    /**
259
     * Thumbnail setting control for WxH settings in Customizer.
260
     *
261
     * @todo Move to its own file.
262
     * @since 4.0
263
     */
264
    class WPSC_Customizer_Thumbnail_Control extends WP_Customize_Control {
265
266
        public $html = array();
267
268
        public function build_field_html( $key, $setting ) {
269
            $value = '';
270
271
            if ( isset( $this->settings[ $key ] ) ) {
272
                $value = $this->settings[ $key ]->value();
273
            }
274
275
            $this->html[] = '<div><input type="text" value="' . esc_attr( $value ) . '" '.$this->get_link( $key ).' /></div>';
276
        }
277
278
        public function render_content() {
279
            $output =  '<label>' . esc_html( $this->label ) .'</label>';
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
280
281
            echo $output;
282
283
            foreach( $this->settings as $key => $value ) {
284
                $this->build_field_html( $key, $value );
285
            }
286
287
            echo implode( '', $this->html );
288
        }
289
290
    }
291
} );
292