-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.php
380 lines (325 loc) · 9.54 KB
/
example.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
<?php
/* Example Implementation:
* - Setup
* - Sections
* - Settings
* - Controls
* - Saving
* - Templating
*/
/* Setup */
function customizer_demo_prefix() {
return 'customizer-demo-';
}
function customizer_demo_static() {
wp_enqueue_script(
'customizer_demo_static',
plugins_url( 'frontend.js', __FILE__ ),
array( 'jquery','customize-preview' ),
'1.0',
true
);
}
add_action( 'customize_preview_init', 'customizer_demo_static', 20, 2 );
function customizer_demo_maybe_clear( $wp_customize ) {
$preview = customizer_wrapper_preview_type();
if ( 'single' === $preview[ 'type' ] ) {
add_filter( 'customizer_wrapper_clear_header', '__return_true', 10, 1 );
add_filter( 'customizer_wrapper_clear_elements', '__return_true', 10, 1 );
}
}
//purposefully late on customize_register
add_action( 'customize_register', 'customizer_demo_maybe_clear', 0, 1 );
/* Sections */
function customizer_demo_sections( $sections = array() ) {
$preview_type = customizer_wrapper_preview_type();
$prefix = customizer_demo_prefix();
if ( 'single' === $preview_type[ 'type' ] ) {
$sections[] = array(
'id' => $prefix . 'single',
'title' => 'Customize Post',
'priority' => 20
);
}
return $sections;
}
add_filter( 'customizer_wrapper_sections', 'customizer_demo_sections', 20, 1 );
/* Settings */
function customizer_demo_settings( $settings = array() ) {
$preview_type = customizer_wrapper_preview_type();
$prefix = customizer_demo_prefix();
if ( 'single' === $preview_type[ 'type' ] ) {
//we are setting up article-level tooling
//so declare a current $post_id far for reuse
$post_id = $preview_type[ 'id' ];
//checkbox
//using the same key for postmeta and customizer setting makes life easier
$key = $prefix . 'single-checkbox';
//fetch existing value to populate as "default"
$value = get_post_meta( $post_id, $key, true );
//if the existing value doesn't exist, use an actual default
$value = empty( $value ) ? true : $value;
//add the setting to the stack
$settings[] = array(
'id' => $key,
'default' => $value,
'transport' => 'postMessage',
'capability' => 'edit_others_posts'
);
//other control types typically follow the same pattern,
//except for images, which may require some fancy footwork
//if you'd like to store attachment ids
//radio
$key = $prefix . 'single-radio';
$value = get_post_meta( $post_id, $key, true );
$value = empty( $value ) ? 'showing' : $value;
$settings[] = array(
'id' => $key,
'default' => $value,
'transport' => 'postMessage',
'capability' => 'edit_others_posts'
);
//text
$key = $prefix . 'single-text';
$value = get_post_meta( $post_id, $key, true );
$value = empty( $value ) ? get_bloginfo( 'description' ) : $value;
$settings[] = array(
'id' => $key,
'default' => $value,
'transport' => 'postMessage',
'capability' => 'edit_others_posts'
);
//select
$key = $prefix . 'single-select';
$value = get_post_meta( $post_id, $key, true );
$value = empty( $value ) ? 'showing' : $value;
$settings[] = array(
'id' => $key,
'default' => $value,
'transport' => 'postMessage',
'capability' => 'edit_others_posts'
);
//color
$key = $prefix . 'single-color';
$value = get_post_meta( $post_id, $key, true );
$value = empty( $value ) ? '#336699' : $value;
$settings[] = array(
'id' => $key,
'default' => $value,
'transport' => 'postMessage',
'capability' => 'edit_others_posts'
);
//image
//images are somewhat special when setting up sections
$key = $prefix . 'single-image';
$value = get_post_meta( $post_id, $key, true );
//it can be much more useful to store a post ID rather than a raw image
if ( empty( $value ) ) {
//when it's an image ID, translate that into a img url
//as required by the image control
$value = wp_get_attachment_image_src( $value, 'full' );
$value = $value[ 0 ];
}
$settings[] = array(
'id' => $key,
'default' => $value,
'capability' => 'edit_others_posts',
'transport' => 'postMessage'
);
error_log("SETTINGS" . json_encode( $settings ) );
}
return $settings;
}
add_filter( 'customizer_wrapper_settings', 'customizer_demo_settings', 20, 1 );
/* Controls */
function customizer_demo_controls( $controls = array() ) {
$type = customizer_wrapper_preview_type();
$prefix = customizer_demo_prefix();
if ( 'single' === $type[ 'type' ] ) {
//text
$controls[] = array(
'id' => $prefix . 'single-text',
'settings' => $prefix . 'single-text',
'label' => 'Sitename',
'type' => 'text',
'section' => $prefix . 'single'
);
//checkbox
$controls[] = array(
'id' => $prefix . 'single-checkbox',
'settings' => $prefix . 'single-checkbox',
'label' => 'Header Description',
'type' => 'checkbox',
'section' => $prefix . 'single'
);
//image
$controls[] = array(
'id' => $prefix . 'single-image',
'settings' => $prefix . 'single-image',
'label' => 'Header Image',
'type' => 'image',
'section' => $prefix . 'single'
);
//color
$controls[] = array(
'id' => $prefix . 'single-color',
'settings' => $prefix . 'single-color',
'label' => 'Header Color',
'type' => 'color',
'section' => $prefix . 'single'
);
//radio
$controls[] = array(
'id' => $prefix . 'single-radio',
'settings' => $prefix . 'single-radio',
'label' => 'Font size',
'type' => 'radio',
'choices' => array(
'normal' => 'Normal',
'large' => 'Large',
'xlarge' => 'XL'
),
'section' => $prefix . 'single'
);
//select
$controls[] = array(
'id' => $prefix . 'single-select',
'settings' => $prefix . 'single-select',
'label' => 'Example Select',
'type' => 'select',
'choices' => array(
'showing' => 'Show Search Bar',
'hiding' => 'Hide Search Bar'
),
'section' => $prefix . 'single'
);
}
return $controls;
}
add_filter( 'customizer_wrapper_controls', 'customizer_demo_controls', 20, 1 );
/* Saving */
function customizer_demo_save( $data, $wp_customize, $type, $controls ) {
$type = customizer_wrapper_preview_type();
$prefix = customizer_demo_prefix();
if ( 'single' === $type[ 'type' ] ) {
foreach ( $data as $id => $value ) {
$item = $controls[ $id ];
//say you wanted to pull data from the original control
//$control = $item[ 'control' ];
//$default = $control->settings[ 'default' ]->default;
$delete = false;
if ( $prefix . 'single-image' === $id ) {
$attachment = customizer_wrapper_attachment_by_url( $value );
if ( null !== $attachment ) {
$value = $attachment->ID;
}
}
if ( true === $value ) {
$value = 'true';
} else if ( false === $value ) {
$value = 'false';
}
if ( true === empty( $value ) ) {
delete_post_meta( $type[ 'id' ], $id );
} else {
update_post_meta( $type[ 'id' ], $id, $value );
}
}
}
}
add_filter( 'customizer_wrapper_save', 'customizer_demo_save', 20, 4 );
/* Templating */
function customizer_demo_maybe_filter_blogname( $title = '' ) {
if ( true === is_single() ) {
global $post;
$meta_title = get_post_meta( $post->ID, 'customizer-demo-single-text', true );
if ( false === empty( $meta_title ) ) {
return $meta_title;
}
}
return $title;
}
add_filter( 'pre_option_blogname', 'customizer_demo_maybe_filter_blogname', 10, 1 );
function customizer_demo_maybe_hide_description() {
if ( true === is_single() ) {
$prefix = customizer_demo_prefix();
global $post;
$desc = get_post_meta( $post->ID, $prefix . 'single-checkbox', true );
if ( 'false' === $desc ) {
echo <<<CSS
<style type="text/css">
.site-description { display: none !important; }
</style>
CSS;
}
}
}
add_action( 'wp_print_footer_scripts' , 'customizer_demo_maybe_hide_description', 10 );
function customizer_demo_maybe_filter_header_image( $description = '' ) {
if ( true === is_single() ) {
$prefix = customizer_demo_prefix();
global $post;
$meta_image = get_post_meta( $post->ID, $prefix . 'single-image', true );
if ( intval( $meta_image ) > 0 ) {
$image = wp_get_attachment_image_src( $meta_image, 'full' );
return $image[ 0 ];
}
}
return $description;
}
add_filter( 'theme_mod_header_image', 'customizer_demo_maybe_filter_header_image', 10, 1 );
function customizer_demo_maybe_change_color() {
if ( true === is_single() ) {
$prefix = customizer_demo_prefix();
global $post;
$color = get_post_meta( $post->ID, $prefix . 'single-color', true );
if ( false === empty( $color ) ) {
echo <<<CSS
<style type="text/css">
.site-title { color: {$color}; }
.site-description { color: {$color}; }
</style>
CSS;
}
}
}
add_action( 'wp_print_footer_scripts' , 'customizer_demo_maybe_change_color', 10 );
function customizer_demo_maybe_change_font_size() {
if ( true === is_single() ) {
$prefix = customizer_demo_prefix();
global $post;
$size = get_post_meta( $post->ID, $prefix . 'single-radio', true );
if ( 'normal' !== $size ) {
if ( 'large' === $size ) {
$px = 24;
$hpx = 54;
}
if ( 'xlarge' === $size ) {
$px = 32;
$hpx = 64;
}
echo <<<CSS
<style type="text/css">
.entry-content { font-size: {$px}px !important; }
h1 { font-size:{$hpx}px !important; }
</style>
CSS;
}
}
}
add_action( 'wp_print_footer_scripts' , 'customizer_demo_maybe_change_font_size', 10 );
function customizer_demo_maybe_hide_bar() {
if ( true === is_single() ) {
$prefix = customizer_demo_prefix();
global $post;
$bar = get_post_meta( $post->ID, $prefix . 'single-select', true );
if ( 'hiding' === $bar ) {
echo <<<CSS
<style type="text/css">
#navbar { display: none; }
</style>
CSS;
}
}
}
add_action( 'wp_print_footer_scripts' , 'customizer_demo_maybe_hide_bar', 10 );