forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartial.php
More file actions
398 lines (366 loc) · 12.2 KB
/
partial.php
File metadata and controls
398 lines (366 loc) · 12.2 KB
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
<?php
/**
* Test_WP_Customize_Partial tests.
*
* @package WordPress
*/
/**
* Tests for the Test_WP_Customize_Partial class.
*
* @group customize
*/
class Test_WP_Customize_Partial extends WP_UnitTestCase {
/**
* Manager.
*
* @var WP_Customize_Manager
*/
public $wp_customize;
/**
* Component.
*
* @var WP_Customize_Selective_Refresh
*/
public $selective_refresh;
/**
* Set up.
*/
function setUp() {
parent::setUp();
require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
$GLOBALS['wp_customize'] = new WP_Customize_Manager();
$this->wp_customize = $GLOBALS['wp_customize'];
if ( isset( $this->wp_customize->selective_refresh ) ) {
$this->selective_refresh = $this->wp_customize->selective_refresh;
}
}
/**
* Test WP_Customize_Partial::__construct().
*
* @see WP_Customize_Partial::__construct()
*/
function test_construct_default_args() {
$partial_id = 'blogname';
$partial = new WP_Customize_Partial( $this->selective_refresh, $partial_id );
$this->assertEquals( $partial_id, $partial->id );
$this->assertEquals( $this->selective_refresh, $partial->component );
$this->assertEquals( 'default', $partial->type );
$this->assertEmpty( $partial->selector );
$this->assertEquals( array( $partial_id ), $partial->settings );
$this->assertEquals( $partial_id, $partial->primary_setting );
$this->assertEquals( array( $partial, 'render_callback' ), $partial->render_callback );
$this->assertEquals( false, $partial->container_inclusive );
$this->assertEquals( true, $partial->fallback_refresh );
}
/**
* Render post content partial.
*
* @param WP_Customize_Partial $partial Partial.
* @return string|false Content or false if error.
*/
function render_post_content_partial( $partial ) {
$id_data = $partial->id_data();
$post_id = intval( $id_data['keys'][0] );
if ( empty( $post_id ) ) {
return false;
}
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
return apply_filters( 'the_content', $post->post_content );
}
/**
* Test WP_Customize_Partial::__construct().
*
* @see WP_Customize_Partial::__construct()
*/
function test_construct_non_default_args() {
$post_id = self::factory()->post->create(
array(
'post_title' => 'Hello World',
'post_content' => 'Lorem Ipsum',
)
);
$partial_id = sprintf( 'post_content[%d]', $post_id );
$args = array(
'type' => 'post',
'selector' => "article.post-$post_id .entry-content",
'settings' => array( 'user[1]', "post[$post_id]" ),
'primary_setting' => "post[$post_id]",
'render_callback' => array( $this, 'render_post_content_partial' ),
'container_inclusive' => false,
'fallback_refresh' => false,
);
$partial = new WP_Customize_Partial( $this->selective_refresh, $partial_id, $args );
$this->assertEquals( $partial_id, $partial->id );
$this->assertEquals( $this->selective_refresh, $partial->component );
$this->assertEquals( $args['type'], $partial->type );
$this->assertEquals( $args['selector'], $partial->selector );
$this->assertEqualSets( $args['settings'], $partial->settings );
$this->assertEquals( $args['primary_setting'], $partial->primary_setting );
$this->assertEquals( $args['render_callback'], $partial->render_callback );
$this->assertEquals( false, $partial->container_inclusive );
$this->assertEquals( false, $partial->fallback_refresh );
$this->assertContains( 'Lorem Ipsum', $partial->render() );
$partial = new WP_Customize_Partial(
$this->selective_refresh,
$partial_id,
array(
'settings' => 'blogdescription',
)
);
$this->assertEquals( array( 'blogdescription' ), $partial->settings );
$this->assertEquals( 'blogdescription', $partial->primary_setting );
}
/**
* Test WP_Customize_Partial::id_data().
*
* @see WP_Customize_Partial::id_data()
*/
function test_id_data() {
$partial = new WP_Customize_Partial( $this->selective_refresh, 'foo' );
$id_data = $partial->id_data();
$this->assertEquals( 'foo', $id_data['base'] );
$this->assertEquals( array(), $id_data['keys'] );
$partial = new WP_Customize_Partial( $this->selective_refresh, 'bar[baz][quux]' );
$id_data = $partial->id_data();
$this->assertEquals( 'bar', $id_data['base'] );
$this->assertEquals( array( 'baz', 'quux' ), $id_data['keys'] );
}
/**
* Keep track of filter calls to customize_partial_render.
*
* @var int
*/
protected $count_filter_customize_partial_render = 0;
/**
* Keep track of filter calls to customize_partial_render_{$partial->id}.
*
* @var int
*/
protected $count_filter_customize_partial_render_with_id = 0;
/**
* Filter customize_partial_render.
*
* @param string|false $rendered Content.
* @param WP_Customize_Partial $partial Partial.
* @param array $container_context Data.
* @return string|false Content.
*/
function filter_customize_partial_render( $rendered, $partial, $container_context ) {
$this->assertTrue( false === $rendered || is_string( $rendered ) );
$this->assertInstanceOf( 'WP_Customize_Partial', $partial );
$this->assertInternalType( 'array', $container_context );
$this->count_filter_customize_partial_render += 1;
return $rendered;
}
/**
* Filter customize_partial_render_{$partial->id}.
*
* @param string|false $rendered Content.
* @param WP_Customize_Partial $partial Partial.
* @param array $container_context Data.
* @return string|false Content.
*/
function filter_customize_partial_render_with_id( $rendered, $partial, $container_context ) {
$this->assertEquals( sprintf( 'customize_partial_render_%s', $partial->id ), current_filter() );
$this->assertTrue( false === $rendered || is_string( $rendered ) );
$this->assertInstanceOf( 'WP_Customize_Partial', $partial );
$this->assertInternalType( 'array', $container_context );
$this->count_filter_customize_partial_render_with_id += 1;
return $rendered;
}
/**
* Bad render_callback().
*
* @return string Content.
*/
function render_echo_and_return() {
echo 'foo';
return 'bar';
}
/**
* Echo render_callback().
*/
function render_echo() {
echo 'foo';
}
/**
* Return render_callback().
*
* @return string Content.
*/
function render_return() {
return 'bar';
}
/**
* Test WP_Customize_Partial::render() with a bad return_callback.
*
* @see WP_Customize_Partial::render()
*/
function test_render_with_bad_callback_should_give_preference_to_return_value() {
$partial = new WP_Customize_Partial(
$this->selective_refresh,
'foo',
array(
'render_callback' => array( $this, 'render_echo_and_return' ),
)
);
$this->setExpectedIncorrectUsage( 'render' );
$this->assertSame( 'bar', $partial->render() );
}
/**
* Test WP_Customize_Partial::render() with a return_callback that echos.
*
* @see WP_Customize_Partial::render()
*/
function test_render_echo_callback() {
$partial = new WP_Customize_Partial(
$this->selective_refresh,
'foo',
array(
'render_callback' => array( $this, 'render_echo' ),
)
);
$count_filter_customize_partial_render = $this->count_filter_customize_partial_render;
$count_filter_customize_partial_render_with_id = $this->count_filter_customize_partial_render_with_id;
add_filter( 'customize_partial_render', array( $this, 'filter_customize_partial_render' ), 10, 3 );
add_filter( "customize_partial_render_{$partial->id}", array( $this, 'filter_customize_partial_render_with_id' ), 10, 3 );
$rendered = $partial->render();
$this->assertEquals( 'foo', $rendered );
$this->assertEquals( $count_filter_customize_partial_render + 1, $this->count_filter_customize_partial_render );
$this->assertEquals( $count_filter_customize_partial_render_with_id + 1, $this->count_filter_customize_partial_render_with_id );
}
/**
* Test WP_Customize_Partial::render() with a return_callback that echos.
*
* @see WP_Customize_Partial::render()
*/
function test_render_return_callback() {
$partial = new WP_Customize_Partial(
$this->selective_refresh,
'foo',
array(
'render_callback' => array( $this, 'render_return' ),
)
);
$count_filter_customize_partial_render = $this->count_filter_customize_partial_render;
$count_filter_customize_partial_render_with_id = $this->count_filter_customize_partial_render_with_id;
add_filter( 'customize_partial_render', array( $this, 'filter_customize_partial_render' ), 10, 3 );
add_filter( "customize_partial_render_{$partial->id}", array( $this, 'filter_customize_partial_render_with_id' ), 10, 3 );
$rendered = $partial->render();
$this->assertEquals( 'bar', $rendered );
$this->assertEquals( $count_filter_customize_partial_render + 1, $this->count_filter_customize_partial_render );
$this->assertEquals( $count_filter_customize_partial_render_with_id + 1, $this->count_filter_customize_partial_render_with_id );
}
/**
* Test WP_Customize_Partial::render_callback() default.
*
* @see WP_Customize_Partial::render_callback()
*/
function test_render_callback_default() {
$partial = new WP_Customize_Partial( $this->selective_refresh, 'foo' );
$this->assertFalse( $partial->render_callback( $partial, array() ) );
$this->assertFalse( call_user_func( $partial->render_callback, $partial, array() ) );
}
/**
* Test WP_Customize_Partial::json().
*
* @see WP_Customize_Partial::json()
*/
function test_json() {
$post_id = 123;
$partial_id = sprintf( 'post_content[%d]', $post_id );
$args = array(
'type' => 'post',
'selector' => "article.post-$post_id .entry-content",
'settings' => array( 'user[1]', "post[$post_id]" ),
'primary_setting' => "post[$post_id]",
'render_callback' => array( $this, 'render_post_content_partial' ),
'container_inclusive' => false,
'fallback_refresh' => false,
);
$partial = new WP_Customize_Partial( $this->selective_refresh, $partial_id, $args );
$exported = $partial->json();
$this->assertArrayHasKey( 'settings', $exported );
$this->assertArrayHasKey( 'primarySetting', $exported );
$this->assertArrayHasKey( 'selector', $exported );
$this->assertArrayHasKey( 'type', $exported );
$this->assertArrayHasKey( 'fallbackRefresh', $exported );
$this->assertArrayHasKey( 'containerInclusive', $exported );
}
/**
* Test WP_Customize_Partial::check_capabilities().
*
* @see WP_Customize_Partial::check_capabilities()
*/
function test_check_capabilities() {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
do_action( 'customize_register', $this->wp_customize );
$partial = new WP_Customize_Partial(
$this->selective_refresh,
'blogname',
array(
'settings' => array( 'blogname' ),
)
);
$this->assertTrue( $partial->check_capabilities() );
$partial = new WP_Customize_Partial(
$this->selective_refresh,
'blogname',
array(
'settings' => array( 'blogname', 'non_existing' ),
)
);
$this->assertFalse( $partial->check_capabilities() );
$this->wp_customize->add_setting(
'top_secret_message',
array(
'capability' => 'top_secret_clearance',
)
);
$partial = new WP_Customize_Partial(
$this->selective_refresh,
'blogname',
array(
'settings' => array( 'blogname', 'top_secret_clearance' ),
)
);
$this->assertFalse( $partial->check_capabilities() );
$partial = new WP_Customize_Partial(
$this->selective_refresh,
'no_setting',
array(
'settings' => array(),
)
);
$this->assertTrue( $partial->check_capabilities() );
$partial = new WP_Customize_Partial(
$this->selective_refresh,
'no_setting',
array(
'settings' => array(),
'capability' => 'top_secret_clearance',
)
);
$this->assertFalse( $partial->check_capabilities() );
$partial = new WP_Customize_Partial(
$this->selective_refresh,
'no_setting',
array(
'settings' => array(),
'capability' => 'edit_theme_options',
)
);
$this->assertTrue( $partial->check_capabilities() );
}
/**
* Tear down.
*/
function tearDown() {
$this->wp_customize = null;
unset( $GLOBALS['wp_customize'] );
parent::tearDown();
}
}