Ticket #30737: 30737.10.diff
File 30737.10.diff, 44.1 KB (added by , 9 years ago) |
---|
-
src/wp-admin/customize.php
diff --git src/wp-admin/customize.php src/wp-admin/customize.php index 65caa29..ca41e9f 100644
do_action( 'customize_controls_print_scripts' ); 174 174 <div id="customize-preview" class="wp-full-overlay-main"></div> 175 175 <?php 176 176 177 // Render control templates. 177 // Render Panel, Section, and Control templates. 178 $wp_customize->render_panel_templates(); 179 $wp_customize->render_section_templates(); 178 180 $wp_customize->render_control_templates(); 179 181 180 182 /** … … do_action( 'customize_controls_print_scripts' ); 258 260 259 261 // Prepare Customize Setting objects to pass to JavaScript. 260 262 foreach ( $wp_customize->settings() as $id => $setting ) { 261 $settings['settings'][ $id ] = array( 262 'value' => $setting->js_value(), 263 'transport' => $setting->transport, 264 'dirty' => $setting->dirty, 265 ); 263 if ( $setting->check_capabilities() ) { 264 $settings['settings'][ $id ] = array( 265 'value' => $setting->js_value(), 266 'transport' => $setting->transport, 267 'dirty' => $setting->dirty, 268 ); 269 } 266 270 } 267 271 268 272 // Prepare Customize Control objects to pass to JavaScript. 269 273 foreach ( $wp_customize->controls() as $id => $control ) { 270 $settings['controls'][ $id ] = $control->json(); 274 if ( $control->check_capabilities() ) { 275 $settings['controls'][ $id ] = $control->json(); 276 } 271 277 } 272 278 273 279 // Prepare Customize Section objects to pass to JavaScript. 274 280 foreach ( $wp_customize->sections() as $id => $section ) { 275 $settings['sections'][ $id ] = $section->json(); 281 if ( $section->check_capabilities() ) { 282 $settings['sections'][ $id ] = $section->json(); 283 } 276 284 } 277 285 278 286 // Prepare Customize Panel objects to pass to JavaScript. 279 foreach ( $wp_customize->panels() as $id => $panel ) { 280 $settings['panels'][ $id ] = $panel->json(); 281 foreach ( $panel->sections as $section_id => $section ) { 282 $settings['sections'][ $section_id ] = $section->json(); 287 foreach ( $wp_customize->panels() as $panel_id => $panel ) { 288 if ( $panel->check_capabilities() ) { 289 $settings['panels'][ $panel_id ] = $panel->json(); 290 foreach ( $panel->sections as $section_id => $section ) { 291 if ( $section->check_capabilities() ) { 292 $settings['sections'][ $section_id ] = $section->json(); 293 } 294 } 283 295 } 284 296 } 285 297 -
src/wp-admin/js/customize-controls.js
diff --git src/wp-admin/js/customize-controls.js src/wp-admin/js/customize-controls.js index e211a41..35534b2 100644
156 156 Container = api.Class.extend({ 157 157 defaultActiveArguments: { duration: 'fast', completeCallback: $.noop }, 158 158 defaultExpandedArguments: { duration: 'fast', completeCallback: $.noop }, 159 containerType: 'container', 159 160 160 161 /** 161 162 * @since 4.1.0 … … 168 169 container.id = id; 169 170 container.params = {}; 170 171 $.extend( container, options || {} ); 172 container.templateSelector = 'customize-' + container.containerType + '-' + container.params.type; 171 173 container.container = $( container.params.content ); 174 if ( 0 === container.container.length ) { 175 container.container = $( container.getContainer() ); 176 } 172 177 173 178 container.deferred = { 174 179 embedded: new $.Deferred() … … 191 196 container.onChangeExpanded( expanded, args ); 192 197 }); 193 198 194 container.attachEvents(); 199 container.deferred.embedded.done( function () { 200 container.attachEvents(); 201 }); 195 202 196 203 api.utils.bubbleChildValueChanges( container, [ 'priority', 'active' ] ); 197 204 … … 366 373 * Bring the container into view and then expand this and bring it into view 367 374 * @param {Object} [params] 368 375 */ 369 focus: focus 376 focus: focus, 377 378 /** 379 * Return the container html, generated from its JS template, if it exists. 380 * 381 * @since 4.3.0 382 */ 383 getContainer: function () { 384 var template, 385 container = this; 386 387 if ( 0 !== $( '#tmpl-' + container.templateSelector ).length ) { 388 template = wp.template( container.templateSelector ); 389 if ( template && container.container ) { 390 return $.trim( template( container.params ) ); 391 } 392 } 393 394 return '<li></li>'; 395 } 370 396 }); 371 397 372 398 /** … … 376 402 * @augments wp.customize.Class 377 403 */ 378 404 api.Section = Container.extend({ 405 containerType: 'section', 379 406 380 407 /** 381 408 * @since 4.1.0 … … 964 991 * @augments wp.customize.Class 965 992 */ 966 993 api.Panel = Container.extend({ 994 containerType: 'panel', 995 967 996 /** 968 997 * @since 4.1.0 969 998 * … … 990 1019 991 1020 if ( ! panel.container.parent().is( parentContainer ) ) { 992 1021 parentContainer.append( panel.container ); 1022 panel.renderContent(); 993 1023 } 994 1024 panel.deferred.embedded.resolve(); 995 1025 }, … … 1012 1042 } 1013 1043 }); 1014 1044 1015 meta = panel.container.find( '.panel-meta:first' ); 1016 1017 meta.find( '> .accordion-section-title' ).on( 'click keydown', function( event ) { 1045 panel.container.on( 'click keydown', '.panel-meta > .accordion-section-title', function( event ) { 1018 1046 if ( api.utils.isKeydownButNotEnterEvent( event ) ) { 1019 1047 return; 1020 1048 } 1021 1049 event.preventDefault(); // Keep this AFTER the key filter above 1022 1050 1051 meta = panel.container.find( '.panel-meta' ); 1023 1052 if ( meta.hasClass( 'cannot-expand' ) ) { 1024 1053 return; 1025 1054 } … … 1142 1171 panelTitle.focus(); 1143 1172 container.scrollTop( 0 ); 1144 1173 } 1174 }, 1175 1176 /** 1177 * Render the panel from its JS template, if it exists. 1178 * 1179 * The panel's container must already exist in the DOM. 1180 * 1181 * @since 4.3.0 1182 */ 1183 renderContent: function () { 1184 var template, 1185 panel = this; 1186 1187 // Add the content to the container. 1188 if ( 0 !== $( '#tmpl-' + panel.templateSelector + '-content' ).length ) { 1189 template = wp.template( panel.templateSelector + '-content' ); 1190 if ( template && panel.container ) { 1191 panel.container.find( '.accordion-sub-container' ).html( template( panel.params ) ); 1192 } 1193 } 1145 1194 } 1146 1195 }); 1147 1196 -
src/wp-includes/class-wp-customize-manager.php
diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php index 98539b0..47c0407 100644
final class WP_Customize_Manager { 60 60 protected $customized; 61 61 62 62 /** 63 * Controls that may be rendered from JS templates. 63 * Panel types that may be rendered from JS templates. 64 * 65 * @since 4.3.0 66 * @access protected 67 * @var array 68 */ 69 protected $registered_panel_types = array(); 70 71 /** 72 * Section types that may be rendered from JS templates. 73 * 74 * @since 4.3.0 75 * @access protected 76 * @var array 77 */ 78 protected $registered_section_types = array(); 79 80 /** 81 * Control types that may be rendered from JS templates. 64 82 * 65 83 * @since 4.1.0 66 84 * @access protected … … final class WP_Customize_Manager { 612 630 } 613 631 614 632 foreach ( $this->settings as $id => $setting ) { 615 $settings['values'][ $id ] = $setting->js_value(); 633 if ( $setting->check_capabilities() ) { 634 $settings['values'][ $id ] = $setting->js_value(); 635 } 616 636 } 617 foreach ( $this->panels as $id => $panel ) { 618 $settings['activePanels'][ $id ] = $panel->active(); 619 foreach ( $panel->sections as $id => $section ) { 620 $settings['activeSections'][ $id ] = $section->active(); 637 foreach ( $this->panels as $panel_id => $panel ) { 638 if ( $panel->check_capabilities() ) { 639 $settings['activePanels'][ $panel_id ] = $panel->active(); 640 foreach ( $panel->sections as $section_id => $section ) { 641 if ( $section->check_capabilities() ) { 642 $settings['activeSections'][ $section_id ] = $section->active(); 643 } 644 } 621 645 } 622 646 } 623 647 foreach ( $this->sections as $id => $section ) { 624 $settings['activeSections'][ $id ] = $section->active(); 648 if ( $section->check_capabilities() ) { 649 $settings['activeSections'][ $id ] = $section->active(); 650 } 625 651 } 626 652 foreach ( $this->controls as $id => $control ) { 627 $settings['activeControls'][ $id ] = $control->active(); 653 if ( $control->check_capabilities() ) { 654 $settings['activeControls'][ $id ] = $control->active(); 655 } 628 656 } 629 657 630 658 ?> … … final class WP_Customize_Manager { 965 993 } 966 994 967 995 /** 996 * Register a customize panel type. 997 * 998 * Registered types are eligible to be rendered via JS and created dynamically. 999 * 1000 * @since 4.3.0 1001 * @access public 1002 * 1003 * @param string $panel Name of a custom panel which is a subclass of 1004 * {@see WP_Customize_Panel}. 1005 */ 1006 public function register_panel_type( $panel ) { 1007 $this->registered_panel_types[] = $panel; 1008 } 1009 1010 /** 1011 * Render JS templates for all registered panel types. 1012 * 1013 * @since 4.3.0 1014 * @access public 1015 */ 1016 public function render_panel_templates() { 1017 foreach ( $this->registered_panel_types as $panel_type ) { 1018 $panel = new $panel_type( $this, 'temp', array() ); 1019 $panel->print_template(); 1020 } 1021 } 1022 1023 /** 968 1024 * Add a customize section. 969 1025 * 970 1026 * @since 3.4.0 … … final class WP_Customize_Manager { 1006 1062 } 1007 1063 1008 1064 /** 1065 * Register a customize section type. 1066 * 1067 * Registered types are eligible to be rendered via JS and created dynamically. 1068 * 1069 * @since 4.3.0 1070 * @access public 1071 * 1072 * @param string $section Name of a custom section which is a subclass of 1073 * {@see WP_Customize_Section}. 1074 */ 1075 public function register_section_type( $section ) { 1076 $this->registered_section_types[] = $section; 1077 } 1078 1079 /** 1080 * Render JS templates for all registered section types. 1081 * 1082 * @since 4.3.0 1083 * @access public 1084 */ 1085 public function render_section_templates() { 1086 foreach ( $this->registered_section_types as $section_type ) { 1087 $section = new $section_type( $this, 'temp', array() ); 1088 $section->print_template(); 1089 } 1090 } 1091 1092 /** 1009 1093 * Add a customize control. 1010 1094 * 1011 1095 * @since 3.4.0 … … final class WP_Customize_Manager { 1176 1260 */ 1177 1261 public function register_controls() { 1178 1262 1179 /* Control Types (custom control classes) */ 1263 /* Panel, Section, and Control Types */ 1264 $this->register_panel_type( 'WP_Customize_Panel' ); 1265 $this->register_section_type( 'WP_Customize_Section' ); 1266 $this->register_section_type( 'WP_Customize_Sidebar_Section' ); 1180 1267 $this->register_control_type( 'WP_Customize_Color_Control' ); 1181 1268 $this->register_control_type( 'WP_Customize_Media_Control' ); 1182 1269 $this->register_control_type( 'WP_Customize_Upload_Control' ); -
src/wp-includes/class-wp-customize-panel.php
diff --git src/wp-includes/class-wp-customize-panel.php src/wp-includes/class-wp-customize-panel.php index ee9f846..791287f 100644
class WP_Customize_Panel { 212 212 * @return array The array to be exported to the client as JSON. 213 213 */ 214 214 public function json() { 215 $array = wp_array_slice_assoc( (array) $this, array( ' title', 'description', 'priority', 'type' ) );215 $array = wp_array_slice_assoc( (array) $this, array( 'id', 'title', 'description', 'priority', 'type' ) ); 216 216 $array['content'] = $this->get_content(); 217 217 $array['active'] = $this->active(); 218 218 $array['instanceNumber'] = $this->instance_number; … … class WP_Customize_Panel { 287 287 } 288 288 289 289 /** 290 * Render the panel container, and then its contents. 290 * Render the panel container, and then its contents (via `this->render_content()`) in a subclass. 291 * 292 * Panel containers are now rendered in JS by default, see {@see WP_Customize_Panel::print_template()}. 291 293 * 292 294 * @since 4.0.0 293 295 * @access protected 294 296 */ 295 protected function render() { 296 $classes = 'accordion-section control-section control-panel control-panel-' . $this->type; 297 protected function render() {} 298 299 /** 300 * Render the panel UI in a subclass. 301 * 302 * Panel contents are now rendered in JS by default, see {@see WP_Customize_Panel::print_template()}. 303 * 304 * @since 4.1.0 305 * @access protected 306 */ 307 protected function render_content() {} 308 309 /** 310 * Render the panel's JS templates. 311 * 312 * This function is only run for panel types that have been registered with 313 * {@see WP_Customize_Manager::register_panel_type()}. 314 * 315 * @since 4.3.0 316 */ 317 public function print_template() { 297 318 ?> 298 <li id="accordion-panel-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>"> 319 <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content"> 320 <?php $this->content_template(); ?> 321 </script> 322 <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>"> 323 <?php $this->render_template(); ?> 324 </script> 325 <?php 326 } 327 328 /** 329 * An Underscore (JS) template for rendering this panel's container. 330 * 331 * Class variables for this panel class are available in the `data` JS object; 332 * export custom variables by overriding {@see WP_Customize_Panel::json()}. 333 * 334 * @see WP_Customize_Panel::print_template() 335 * 336 * @since 4.3.0 337 */ 338 protected function render_template() { 339 ?> 340 <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}"> 299 341 <h3 class="accordion-section-title" tabindex="0"> 300 <?php echo esc_html( $this->title ); ?>342 {{ data.title }} 301 343 <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span> 302 344 </h3> 303 <ul class="accordion-sub-container control-panel-content"> 304 <?php $this->render_content(); ?> 305 </ul> 345 <ul class="accordion-sub-container control-panel-content"></ul> 306 346 </li> 307 347 <?php 308 348 } 309 349 310 350 /** 311 * Render the sections that have been added to the panel.351 * An Underscore (JS) template for this panel's content (but not its container). 312 352 * 313 * @since 4.1.0 314 * @access protected 353 * Class variables for this panel class are available in the `data` JS object; 354 * export custom variables by overriding {@see WP_Customize_Panel::json()}. 355 * 356 * @see WP_Customize_Panel::print_template() 357 * 358 * @since 4.3.0 315 359 */ 316 protected function render_content() {360 protected function content_template() { 317 361 ?> 318 <li class="panel-meta accordion-section control-section< ?php if ( empty( $this->description ) ) { echo ' cannot-expand'; } ?>">362 <li class="panel-meta accordion-section control-section<# if ( ! data.description ) { #> cannot-expand<# } #>"> 319 363 <div class="accordion-section-title" tabindex="0"> 320 364 <span class="preview-notice"><?php 321 365 /* translators: %s is the site/panel title in the Customizer */ 322 echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title"> ' . esc_html( $this->title ) . '</strong>' );366 echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' ); 323 367 ?></span> 324 368 </div> 325 < ?php if ( ! empty( $this->description ) ) : ?>369 <# if ( data.description ) { #> 326 370 <div class="accordion-section-content description"> 327 <?php echo $this->description; ?>371 {{{ data.description }}} 328 372 </div> 329 < ?php endif; ?>373 <# } #> 330 374 </li> 331 375 <?php 332 376 } -
src/wp-includes/class-wp-customize-section.php
diff --git src/wp-includes/class-wp-customize-section.php src/wp-includes/class-wp-customize-section.php index a27f22b..7b2f7c9 100644
class WP_Customize_Section { 221 221 * @return array The array to be exported to the client as JSON. 222 222 */ 223 223 public function json() { 224 $array = wp_array_slice_assoc( (array) $this, array( ' title', 'description', 'priority', 'panel', 'type' ) );224 $array = wp_array_slice_assoc( (array) $this, array( 'id', 'title', 'description', 'priority', 'panel', 'type' ) ); 225 225 $array['content'] = $this->get_content(); 226 226 $array['active'] = $this->active(); 227 227 $array['instanceNumber'] = $this->instance_number; … … class WP_Customize_Section { 249 249 } 250 250 251 251 /** 252 * Get the section's content templatefor insertion into the Customizer pane.252 * Get the section's content for insertion into the Customizer pane. 253 253 * 254 254 * @since 4.1.0 255 255 * … … class WP_Customize_Section { 295 295 } 296 296 297 297 /** 298 * Render the section, and the controls that have been added to it. 298 * Render the section UI in a subclass. 299 * 300 * Sections are now rendered in JS by default, see {@see WP_Customize_Section::print_template()}. 299 301 * 300 302 * @since 3.4.0 301 303 */ 302 protected function render() { 303 $classes = 'accordion-section control-section control-section-' . $this->type; 304 protected function render() {} 305 306 /** 307 * Render the section's JS template. 308 * 309 * This function is only run for section types that have been registered with 310 * {@see WP_Customize_Manager::register_section_type()}. 311 * 312 * @since 4.3.0 313 */ 314 public function print_template() { 315 ?> 316 <script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>"> 317 <?php $this->render_template(); ?> 318 </script> 319 <?php 320 } 321 322 /** 323 * An Underscore (JS) template for rendering this section. 324 * 325 * Class variables for this section class are available in the `data` JS object; 326 * export custom variables by overriding {@see WP_Customize_Section::json()}. 327 * 328 * @see WP_Customize_Section::print_template() 329 * 330 * @since 4.3.0 331 */ 332 protected function render_template() { 304 333 ?> 305 <li id="accordion-section- <?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">334 <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}"> 306 335 <h3 class="accordion-section-title" tabindex="0"> 307 <?php echo esc_html( $this->title ); ?>336 {{ data.title }} 308 337 <span class="screen-reader-text"><?php _e( 'Press return or enter to expand' ); ?></span> 309 338 </h3> 310 339 <ul class="accordion-section-content"> 311 < ?php if ( ! empty( $this->description ) ) : ?>340 <# if ( data.description ) { #> 312 341 <li class="customize-section-description-container"> 313 <p class="description customize-section-description"> <?php echo $this->description; ?></p>342 <p class="description customize-section-description">{{{ data.description }}}</p> 314 343 </li> 315 < ?php endif; ?>344 <# } #> 316 345 </ul> 317 346 </li> 318 347 <?php -
new file tests/phpunit/tests/customize/panel.php
diff --git tests/phpunit/tests/customize/panel.php tests/phpunit/tests/customize/panel.php new file mode 100644 index 0000000..69fcef8
- + 1 <?php 2 3 /** 4 * Tests for the WP_Customize_Panel class. 5 * 6 * @group customize 7 */ 8 class Tests_WP_Customize_Panel extends WP_UnitTestCase { 9 10 /** 11 * @var WP_Customize_Manager 12 */ 13 protected $manager; 14 15 function setUp() { 16 parent::setUp(); 17 require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); 18 $GLOBALS['wp_customize'] = new WP_Customize_Manager(); 19 $this->manager = $GLOBALS['wp_customize']; 20 $this->undefined = new stdClass(); 21 } 22 23 function tearDown() { 24 $this->manager = null; 25 unset( $GLOBALS['wp_customize'] ); 26 parent::tearDown(); 27 } 28 29 /** 30 * @see WP_Customize_Panel::__construct() 31 */ 32 function test_construct_default_args() { 33 $panel = new WP_Customize_Panel( $this->manager, 'foo' ); 34 $this->assertInternalType( 'int', $panel->instance_number ); 35 $this->assertEquals( $this->manager, $panel->manager ); 36 $this->assertEquals( 'foo', $panel->id ); 37 $this->assertEquals( 160, $panel->priority ); 38 $this->assertEquals( 'edit_theme_options', $panel->capability ); 39 $this->assertEquals( '', $panel->theme_supports ); 40 $this->assertEquals( '', $panel->title ); 41 $this->assertEquals( '', $panel->description ); 42 $this->assertEmpty( $panel->sections ); 43 $this->assertEquals( 'default', $panel->type ); 44 $this->assertEquals( array( $panel, 'active_callback' ), $panel->active_callback ); 45 } 46 47 /** 48 * @see WP_Customize_Panel::__construct() 49 */ 50 function test_construct_custom_args() { 51 $args = array( 52 'priority' => 200, 53 'capability' => 'edit_posts', 54 'theme_supports' => 'html5', 55 'title' => 'Hello World', 56 'description' => 'Lorem Ipsum', 57 'type' => 'horizontal', 58 'active_callback' => '__return_true', 59 ); 60 61 $panel = new WP_Customize_Panel( $this->manager, 'foo', $args ); 62 foreach ( $args as $key => $value ) { 63 $this->assertEquals( $value, $panel->$key ); 64 } 65 } 66 67 /** 68 * @see WP_Customize_Panel::__construct() 69 */ 70 function test_construct_custom_type() { 71 $panel = new Custom_Panel_Test( $this->manager, 'foo' ); 72 $this->assertEquals( 'titleless', $panel->type ); 73 } 74 75 /** 76 * @see WP_Customize_Panel::active() 77 * @see WP_Customize_Panel::active_callback() 78 */ 79 function test_active() { 80 $panel = new WP_Customize_Panel( $this->manager, 'foo' ); 81 $this->assertTrue( $panel->active() ); 82 83 $panel = new WP_Customize_Panel( $this->manager, 'foo', array( 84 'active_callback' => '__return_false', 85 ) ); 86 $this->assertFalse( $panel->active() ); 87 add_filter( 'customize_panel_active', array( $this, 'filter_active_test' ), 10, 2 ); 88 $this->assertTrue( $panel->active() ); 89 } 90 91 /** 92 * @param bool $active 93 * @param WP_Customize_Panel $panel 94 * @return bool 95 */ 96 function filter_active_test( $active, $panel ) { 97 $this->assertFalse( $active ); 98 $this->assertInstanceOf( 'WP_Customize_Panel', $panel ); 99 $active = true; 100 return $active; 101 } 102 103 /** 104 * @see WP_Customize_Panel::json() 105 */ 106 function test_json() { 107 $args = array( 108 'priority' => 200, 109 'capability' => 'edit_posts', 110 'theme_supports' => 'html5', 111 'title' => 'Hello World', 112 'description' => 'Lorem Ipsum', 113 'type' => 'horizontal', 114 'active_callback' => '__return_true', 115 ); 116 $panel = new WP_Customize_Panel( $this->manager, 'foo', $args ); 117 $data = $panel->json(); 118 $this->assertEquals( 'foo', $data['id'] ); 119 foreach ( array( 'title', 'description', 'priority', 'type' ) as $key ) { 120 $this->assertEquals( $args[ $key ], $data[ $key ] ); 121 } 122 $this->assertEmpty( $data['content'] ); 123 $this->assertTrue( $data['active'] ); 124 $this->assertInternalType( 'int', $data['instanceNumber'] ); 125 } 126 127 /** 128 * @see WP_Customize_Panel::check_capabilities() 129 */ 130 function test_check_capabilities() { 131 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 132 wp_set_current_user( $user_id ); 133 134 $panel = new WP_Customize_Panel( $this->manager, 'foo' ); 135 $this->assertTrue( $panel->check_capabilities() ); 136 $old_cap = $panel->capability; 137 $panel->capability = 'do_not_allow'; 138 $this->assertFalse( $panel->check_capabilities() ); 139 $panel->capability = $old_cap; 140 $this->assertTrue( $panel->check_capabilities() ); 141 $panel->theme_supports = 'impossible_feature'; 142 $this->assertFalse( $panel->check_capabilities() ); 143 } 144 145 /** 146 * @see WP_Customize_Panel::get_content() 147 */ 148 function test_get_content() { 149 $panel = new WP_Customize_Panel( $this->manager, 'foo' ); 150 $this->assertEmpty( $panel->get_content() ); 151 } 152 153 /** 154 * @see WP_Customize_Panel::maybe_render() 155 */ 156 function test_maybe_render() { 157 wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); 158 $panel = new WP_Customize_Panel( $this->manager, 'bar' ); 159 $customize_render_panel_count = did_action( 'customize_render_panel' ); 160 add_action( 'customize_render_panel', array( $this, 'action_customize_render_panel_test' ) ); 161 ob_start(); 162 $panel->maybe_render(); 163 $content = ob_get_clean(); 164 $this->assertTrue( $panel->check_capabilities() ); 165 $this->assertEmpty( $content ); 166 $this->assertEquals( $customize_render_panel_count + 1, did_action( 'customize_render_panel' ), 'Unexpected did_action count for customize_render_panel' ); 167 $this->assertEquals( 1, did_action( "customize_render_panel_{$panel->id}" ), "Unexpected did_action count for customize_render_panel_{$panel->id}" ); 168 } 169 170 /** 171 * @see WP_Customize_Panel::maybe_render() 172 * @param WP_Customize_Panel $panel 173 */ 174 function action_customize_render_panel_test( $panel ) { 175 $this->assertInstanceOf( 'WP_Customize_Panel', $panel ); 176 } 177 178 /** 179 * @see WP_Customize_Panel::print_template() 180 */ 181 function test_print_templates_standard() { 182 wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); 183 184 $panel = new WP_Customize_Panel( $this->manager, 'baz' ); 185 ob_start(); 186 $panel->print_template(); 187 $content = ob_get_clean(); 188 $this->assertContains( '<script type="text/html" id="tmpl-customize-panel-default-content">', $content ); 189 $this->assertContains( 'accordion-section-title', $content ); 190 $this->assertContains( 'control-panel-content', $content ); 191 $this->assertContains( '<script type="text/html" id="tmpl-customize-panel-default">', $content ); 192 $this->assertContains( 'accordion-section-content', $content ); 193 $this->assertContains( 'preview-notice', $content ); 194 } 195 196 /** 197 * @see WP_Customize_Panel::print_template() 198 */ 199 function test_print_templates_custom() { 200 wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); 201 202 $panel = new Custom_Panel_Test( $this->manager, 'baz' ); 203 ob_start(); 204 $panel->print_template(); 205 $content = ob_get_clean(); 206 $this->assertContains( '<script type="text/html" id="tmpl-customize-panel-titleless-content">', $content ); 207 $this->assertNotContains( 'accordion-section-title', $content ); 208 209 $this->assertContains( '<script type="text/html" id="tmpl-customize-panel-titleless">', $content ); 210 $this->assertNotContains( 'preview-notice', $content ); 211 } 212 } 213 214 require_once ABSPATH . WPINC . '/class-wp-customize-panel.php'; 215 class Custom_Panel_Test extends WP_Customize_Panel { 216 public $type = 'titleless'; 217 218 protected function render_template() { 219 ?> 220 <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}"> 221 <ul class="accordion-sub-container control-panel-content"></ul> 222 </li> 223 <?php 224 } 225 226 protected function content_template() { 227 ?> 228 <li class="panel-meta accordion-section control-section<# if ( ! data.description ) { #> cannot-expand<# } #>"> 229 <# if ( data.description ) { #> 230 <div class="accordion-section-content description"> 231 {{{ data.description }}} 232 </div> 233 <# } #> 234 </li> 235 <?php 236 } 237 238 } -
new file tests/phpunit/tests/customize/section.php
diff --git tests/phpunit/tests/customize/section.php tests/phpunit/tests/customize/section.php new file mode 100644 index 0000000..bf5a33f
- + 1 <?php 2 3 /** 4 * Tests for the WP_Customize_Section class. 5 * 6 * @group customize 7 */ 8 class Tests_WP_Customize_Section extends WP_UnitTestCase { 9 10 /** 11 * @var WP_Customize_Manager 12 */ 13 protected $manager; 14 15 function setUp() { 16 parent::setUp(); 17 require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); 18 $GLOBALS['wp_customize'] = new WP_Customize_Manager(); 19 $this->manager = $GLOBALS['wp_customize']; 20 $this->undefined = new stdClass(); 21 } 22 23 function tearDown() { 24 $this->manager = null; 25 unset( $GLOBALS['wp_customize'] ); 26 parent::tearDown(); 27 } 28 29 /** 30 * @see WP_Customize_Section::__construct() 31 */ 32 function test_construct_default_args() { 33 $section = new WP_Customize_Section( $this->manager, 'foo' ); 34 $this->assertInternalType( 'int', $section->instance_number ); 35 $this->assertEquals( $this->manager, $section->manager ); 36 $this->assertEquals( 'foo', $section->id ); 37 $this->assertEquals( 160, $section->priority ); 38 $this->assertEquals( 'edit_theme_options', $section->capability ); 39 $this->assertEquals( '', $section->theme_supports ); 40 $this->assertEquals( '', $section->title ); 41 $this->assertEquals( '', $section->description ); 42 $this->assertEmpty( $section->panel ); 43 $this->assertEquals( 'default', $section->type ); 44 $this->assertEquals( array( $section, 'active_callback' ), $section->active_callback ); 45 } 46 47 /** 48 * @see WP_Customize_Section::__construct() 49 */ 50 function test_construct_custom_args() { 51 $args = array( 52 'priority' => 200, 53 'capability' => 'edit_posts', 54 'theme_supports' => 'html5', 55 'title' => 'Hello World', 56 'description' => 'Lorem Ipsum', 57 'type' => 'horizontal', 58 'active_callback' => '__return_true', 59 'panel' => 'bar', 60 ); 61 62 $this->manager->add_panel( 'bar' ); 63 64 $section = new WP_Customize_Section( $this->manager, 'foo', $args ); 65 foreach ( $args as $key => $value ) { 66 $this->assertEquals( $value, $section->$key ); 67 } 68 } 69 70 /** 71 * @see WP_Customize_Section::__construct() 72 */ 73 function test_construct_custom_type() { 74 $section = new Custom_Section_Test( $this->manager, 'foo' ); 75 $this->assertEquals( 'titleless', $section->type ); 76 } 77 78 /** 79 * @see WP_Customize_Section::active() 80 * @see WP_Customize_Section::active_callback() 81 */ 82 function test_active() { 83 $section = new WP_Customize_Section( $this->manager, 'foo' ); 84 $this->assertTrue( $section->active() ); 85 86 $section = new WP_Customize_Section( $this->manager, 'foo', array( 87 'active_callback' => '__return_false', 88 ) ); 89 $this->assertFalse( $section->active() ); 90 add_filter( 'customize_section_active', array( $this, 'filter_active_test' ), 10, 2 ); 91 $this->assertTrue( $section->active() ); 92 } 93 94 /** 95 * @param bool $active 96 * @param WP_Customize_Section $section 97 * @return bool 98 */ 99 function filter_active_test( $active, $section ) { 100 $this->assertFalse( $active ); 101 $this->assertInstanceOf( 'WP_Customize_Section', $section ); 102 $active = true; 103 return $active; 104 } 105 106 /** 107 * @see WP_Customize_Section::json() 108 */ 109 function test_json() { 110 $args = array( 111 'priority' => 200, 112 'capability' => 'edit_posts', 113 'theme_supports' => 'html5', 114 'title' => 'Hello World', 115 'description' => 'Lorem Ipsum', 116 'type' => 'horizontal', 117 'panel' => 'bar', 118 'active_callback' => '__return_true', 119 ); 120 $section = new WP_Customize_Section( $this->manager, 'foo', $args ); 121 $data = $section->json(); 122 $this->assertEquals( 'foo', $data['id'] ); 123 foreach ( array( 'title', 'description', 'priority', 'panel', 'type' ) as $key ) { 124 $this->assertEquals( $args[ $key ], $data[ $key ] ); 125 } 126 $this->assertEmpty( $data['content'] ); 127 $this->assertTrue( $data['active'] ); 128 $this->assertInternalType( 'int', $data['instanceNumber'] ); 129 } 130 131 /** 132 * @see WP_Customize_Section::check_capabilities() 133 */ 134 function test_check_capabilities() { 135 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 136 wp_set_current_user( $user_id ); 137 138 $section = new WP_Customize_Section( $this->manager, 'foo' ); 139 $this->assertTrue( $section->check_capabilities() ); 140 $old_cap = $section->capability; 141 $section->capability = 'do_not_allow'; 142 $this->assertFalse( $section->check_capabilities() ); 143 $section->capability = $old_cap; 144 $this->assertTrue( $section->check_capabilities() ); 145 $section->theme_supports = 'impossible_feature'; 146 $this->assertFalse( $section->check_capabilities() ); 147 } 148 149 /** 150 * @see WP_Customize_Section::get_content() 151 */ 152 function test_get_content() { 153 $section = new WP_Customize_Section( $this->manager, 'foo' ); 154 $this->assertEmpty( $section->get_content() ); 155 } 156 157 /** 158 * @see WP_Customize_Section::maybe_render() 159 */ 160 function test_maybe_render() { 161 wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); 162 $section = new WP_Customize_Section( $this->manager, 'bar' ); 163 $customize_render_section_count = did_action( 'customize_render_section' ); 164 add_action( 'customize_render_section', array( $this, 'action_customize_render_section_test' ) ); 165 ob_start(); 166 $section->maybe_render(); 167 $content = ob_get_clean(); 168 $this->assertTrue( $section->check_capabilities() ); 169 $this->assertEmpty( $content ); 170 $this->assertEquals( $customize_render_section_count + 1, did_action( 'customize_render_section' ), 'Unexpected did_action count for customize_render_section' ); 171 $this->assertEquals( 1, did_action( "customize_render_section_{$section->id}" ), "Unexpected did_action count for customize_render_section_{$section->id}" ); 172 } 173 174 /** 175 * @see WP_Customize_Section::maybe_render() 176 * @param WP_Customize_Section $section 177 */ 178 function action_customize_render_section_test( $section ) { 179 $this->assertInstanceOf( 'WP_Customize_Section', $section ); 180 } 181 182 /** 183 * @see WP_Customize_Section::print_template() 184 */ 185 function test_print_templates_standard() { 186 wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); 187 188 $section = new WP_Customize_Section( $this->manager, 'baz' ); 189 ob_start(); 190 $section->print_template(); 191 $content = ob_get_clean(); 192 $this->assertContains( '<script type="text/html" id="tmpl-customize-section-default">', $content ); 193 $this->assertContains( 'accordion-section-title', $content ); 194 $this->assertContains( 'accordion-section-content', $content ); 195 } 196 197 /** 198 * @see WP_Customize_Section::print_template() 199 */ 200 function test_print_templates_custom() { 201 wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); 202 203 $section = new Custom_Section_Test( $this->manager, 'baz' ); 204 ob_start(); 205 $section->print_template(); 206 $content = ob_get_clean(); 207 $this->assertContains( '<script type="text/html" id="tmpl-customize-section-titleless">', $content ); 208 $this->assertNotContains( 'accordion-section-title', $content ); 209 $this->assertContains( 'accordion-section-content', $content ); 210 } 211 } 212 213 require_once ABSPATH . WPINC . '/class-wp-customize-section.php'; 214 class Custom_Section_Test extends WP_Customize_Section { 215 public $type = 'titleless'; 216 217 protected function render_template() { 218 ?> 219 <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}"> 220 <ul class="accordion-section-content"> 221 <# if ( data.description ) { #> 222 <li class="customize-section-description-container"> 223 <p class="description customize-section-description">{{{ data.description }}}</p> 224 </li> 225 <# } #> 226 </ul> 227 </li> 228 <?php 229 } 230 231 } -
tests/qunit/fixtures/customize-settings.js
diff --git tests/qunit/fixtures/customize-settings.js tests/qunit/fixtures/customize-settings.js index 993f767..6feef67 100644
window._wpCustomizeSettings = { 39 39 'description': 'Lorem ipsum', 40 40 'instanceNumber': 1, 41 41 'priority': 110, 42 'title': ' Lorem Ipsum',42 'title': 'Fixture panel with content', 43 43 'type': 'default' 44 }, 45 'fixture-panel-default-templated': { 46 'active': true, 47 'description': 'Lorem ipsum', 48 'instanceNumber': 2, 49 'priority': 110, 50 'title': 'Fixture default panel using template', 51 'type': 'default' 52 }, 53 'fixture-panel-titleless-templated': { 54 'active': true, 55 'description': 'Lorem ipsum', 56 'instanceNumber': 3, 57 'priority': 110, 58 'title': 'Fixture titleless panel using template', 59 'type': 'titleless' 44 60 } 45 61 }, 46 62 'sections': { … … window._wpCustomizeSettings = { 53 69 'priority': 20, 54 70 'title': 'Fixture Section', 55 71 'type': 'default' 72 }, 73 'fixture-section-default-templated': { 74 'active': true, 75 'description': '', 76 'instanceNumber': 3, 77 'panel': 'fixture-panel', 78 'priority': 20, 79 'title': 'Fixture default section using template', 80 'type': 'default' 81 }, 82 'fixture-section-titleless-templated': { 83 'active': true, 84 'description': '', 85 'instanceNumber': 4, 86 'panel': 'fixture-panel', 87 'priority': 20, 88 'title': 'Fixture titleless section using template', 89 'type': 'titleless' 56 90 } 57 91 }, 58 92 'settings': { -
tests/qunit/index.html
diff --git tests/qunit/index.html tests/qunit/index.html index 55b95fe..1b0e0c1 100644
9 9 <script src="../../src/wp-includes/js/underscore.min.js"></script> 10 10 <script src="../../src/wp-includes/js/backbone.min.js"></script> 11 11 <script src="../../src/wp-includes/js/zxcvbn.min.js"></script> 12 <script src="../../src/wp-includes/js/wp-util.js"></script> 12 13 13 14 <!-- QUnit --> 14 15 <link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" /> … … 39 40 <script src="wp-includes/js/shortcode.js"></script> 40 41 <script src="wp-admin/js/customize-controls.js"></script> 41 42 <script src="wp-admin/js/customize-controls-utils.js"></script> 43 44 <!-- Customizer templates for sections --> 45 <script type="text/html" id="tmpl-customize-section-default"> 46 <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}"> 47 <h3 class="accordion-section-title" tabindex="0"> 48 {{ data.title }} 49 <span class="screen-reader-text"><?php _e( 'Press return or enter to expand' ); ?></span> 50 </h3> 51 <ul class="accordion-section-content"> 52 <# if ( data.description ) { #> 53 <li class="customize-section-description-container"> 54 <p class="description customize-section-description">{{{ data.description }}}</p> 55 </li> 56 <# } #> 57 </ul> 58 </li> 59 </script> 60 <script type="text/html" id="tmpl-customize-section-titleless"> 61 <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}"> 62 <!-- Notice the lack of an h3 with title displayed inside. --> 63 <ul class="accordion-section-content"> 64 <# if ( data.description ) { #> 65 <li class="customize-section-description-container"> 66 <p class="description customize-section-description">{{{ data.description }}}</p> 67 </li> 68 <# } #> 69 </ul> 70 </li> 71 </script> 72 73 <!-- Customizer templates for panels --> 74 <script type="text/html" id="tmpl-customize-panel-default"> 75 <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}"> 76 <h3 class="accordion-section-title" tabindex="0"> 77 {{ data.title }} 78 <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span> 79 </h3> 80 <ul class="accordion-sub-container control-panel-content"></ul> 81 </li> 82 </script> 83 <script type="text/html" id="tmpl-customize-panel-default-content"> 84 <li class="panel-meta accordion-section control-section<# if ( ! data.description ) { #> cannot-expand<# } #>"> 85 <div class="accordion-section-title" tabindex="0"> 86 <span class="preview-notice"><?php 87 /* translators: %s is the site/panel title in the Customizer */ 88 echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' ); 89 ?></span> 90 </div> 91 <# if ( data.description ) { #> 92 <div class="accordion-section-content description"> 93 {{{ data.description }}} 94 </div> 95 <# } #> 96 </li> 97 </script> 98 <script type="text/html" id="tmpl-customize-panel-titleless"> 99 <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}"> 100 <!-- Notice the lack of an h3 with title displayed inside. --> 101 <ul class="accordion-sub-container control-panel-content"></ul> 102 </li> 103 </script> 104 <script type="text/html" id="tmpl-customize-panel-titleless-content"> 105 <li class="panel-meta accordion-section control-section<# if ( ! data.description ) { #> cannot-expand<# } #>"> 106 <!-- Notice lack of title containing preview notice --> 107 <# if ( data.description ) { #> 108 <div class="accordion-section-content description"> 109 {{{ data.description }}} 110 </div> 111 <# } #> 112 </li> 113 </script> 42 114 </body> 43 115 </html> -
tests/qunit/wp-admin/js/customize-controls.js
diff --git tests/qunit/wp-admin/js/customize-controls.js tests/qunit/wp-admin/js/customize-controls.js index cb8767f..9ab7ce1 100644
jQuery( window ).load( function (){ 95 95 equal( control.section(), 'fixture-section' ); 96 96 } ); 97 97 98 // Begin sections. 98 99 module( 'Customizer Section in Fixture' ); 99 100 test( 'Fixture section exists', function () { 100 101 ok( wp.customize.section.has( 'fixture-section' ) ); 101 102 } ); 102 103 test( 'Fixture section has control among controls()', function () { 103 104 var section = wp.customize.section( 'fixture-section' ); 104 equal( section.controls().length, 1 ); 105 equal( section.controls()[0].id, 'fixture-control' ); 105 ok( -1 !== _.pluck( section.controls(), 'id' ).indexOf( 'fixture-control' ) ); 106 106 } ); 107 test( 'Fixture section has control among controls()', function () {107 test( 'Fixture section has has expected panel', function () { 108 108 var section = wp.customize.section( 'fixture-section' ); 109 109 equal( section.panel(), 'fixture-panel' ); 110 110 } ); 111 111 112 module( 'Customizer Default Section with Template in Fixture' ); 113 test( 'Fixture section exists', function () { 114 ok( wp.customize.section.has( 'fixture-section-default-templated' ) ); 115 } ); 116 test( 'Fixture section has expected content', function () { 117 var id = 'fixture-section-default-templated', section; 118 section = wp.customize.section( id ); 119 ok( ! section.params.content ); 120 ok( !! section.container ); 121 ok( section.container.is( '.control-section.control-section-default' ) ); 122 ok( 1 === section.container.find( '> .accordion-section-title' ).length ); 123 ok( 1 === section.container.find( '> .accordion-section-content' ).length ); 124 } ); 125 126 module( 'Customizer Custom Type (titleless) Section with Template in Fixture' ); 127 test( 'Fixture section exists', function () { 128 ok( wp.customize.section.has( 'fixture-section-titleless-templated' ) ); 129 } ); 130 test( 'Fixture section has expected content', function () { 131 var id = 'fixture-section-titleless-templated', section; 132 section = wp.customize.section( id ); 133 ok( ! section.params.content ); 134 ok( !! section.container ); 135 ok( section.container.is( '.control-section.control-section-titleless' ) ); 136 ok( 0 === section.container.find( '> .accordion-section-title' ).length ); 137 ok( 1 === section.container.find( '> .accordion-section-content' ).length ); 138 } ); 139 140 // Begin panels. 112 141 module( 'Customizer Panel in Fixture' ); 113 142 test( 'Fixture panel exists', function () { 114 143 ok( wp.customize.panel.has( 'fixture-panel' ) ); 115 144 } ); 116 test( 'Fixture section has control among controls()', function () { 145 test( 'Fixture panel has content', function () { 146 var panel = wp.customize.panel( 'fixture-panel' ); 147 ok( !! panel.params.content ); 148 ok( !! panel.container ); 149 } ); 150 test( 'Fixture panel has section among its sections()', function () { 117 151 var panel = wp.customize.panel( 'fixture-panel' ); 118 equal( panel.sections().length, 1 ); 119 equal( panel.sections()[0].id, 'fixture-section' ); 152 ok( -1 !== _.pluck( panel.sections(), 'id' ).indexOf( 'fixture-section' ) ); 120 153 } ); 121 154 test( 'Panel is not expanded by default', function () { 122 155 var panel = wp.customize.panel( 'fixture-panel' ); … … jQuery( window ).load( function (){ 138 171 ok( panel.expanded() ); 139 172 } ); 140 173 174 module( 'Customizer Default Panel with Template in Fixture' ); 175 test( 'Fixture section exists', function () { 176 ok( wp.customize.panel.has( 'fixture-panel-default-templated' ) ); 177 } ); 178 test( 'Fixture panel has expected content', function () { 179 var id = 'fixture-panel-default-templated', panel; 180 panel = wp.customize.panel( id ); 181 ok( ! panel.params.content ); 182 ok( !! panel.container ); 183 ok( panel.container.is( '.control-panel.control-panel-default' ) ); 184 ok( 1 === panel.container.find( '> .accordion-section-title' ).length ); 185 ok( 1 === panel.container.find( '> .control-panel-content' ).length ); 186 } ); 187 188 module( 'Customizer Custom Type Panel (titleless) with Template in Fixture' ); 189 test( 'Fixture panel exists', function () { 190 ok( wp.customize.panel.has( 'fixture-panel-titleless-templated' ) ); 191 } ); 192 test( 'Fixture panel has expected content', function () { 193 var id = 'fixture-panel-titleless-templated', panel; 194 panel = wp.customize.panel( id ); 195 ok( ! panel.params.content ); 196 ok( !! panel.container ); 197 ok( panel.container.is( '.control-panel.control-panel-titleless' ) ); 198 ok( 0 === panel.container.find( '> .accordion-section-title' ).length ); 199 ok( 1 === panel.container.find( '> .control-panel-content' ).length ); 200 } ); 201 141 202 142 203 module( 'Dynamically-created Customizer Setting Model' ); 143 204 settingId = 'new_blogname'; … … jQuery( window ).load( function (){ 163 224 sectionContent = '<li id="accordion-section-mock_title_tagline" class="control-section accordion-section"></li>'; 164 225 sectionData = { 165 226 content: sectionContent, 166 active: true 227 active: true, 228 type: 'default' 167 229 }; 168 230 169 231 mockSection = new wp.customize.Section( sectionId, { params: sectionData } ); … … jQuery( window ).load( function (){ 277 339 content: panelContent, 278 340 title: panelTitle, 279 341 description: panelDescription, 280 active: true // @todo This should default to true 342 active: true, // @todo This should default to true 343 type: 'default' 281 344 }; 282 345 283 346 mockPanel = new wp.customize.Panel( panelId, { params: panelData } );