<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[36689] trunk: Customize: Allow controls to be registered without any associated settings.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/36689">36689</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/36689","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-02-24 18:27:45 +0000 (Wed, 24 Feb 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Customize: Allow controls to be registered without any associated settings.
* Improves parity between partials and controls. A partial or control can be settingless if instantiated with `settings` param as empty array (otherwise, if null, then the partial/control ID is used).
* Eliminate need to create dummy settings that serve no purpose except to place a control in the UI.
* Removes dummy settings for `create_new_menu` and `new_menu_name`.
* Introduces `WP_Customize_Control::$capability` and `WP_Customize_Partial::$capability`, and if set checks them in the respective `check_capabilities()` methods.
* Prevents PHP fatal error from happening when non-existing settings are provided to control: "Call to a member function `check_capabilities()` on a non-object".
* Fixes issue where nav menu items and widgets were no longer working with selective refresh because cap check was failing.
See <a href="https://core.trac.wordpress.org/ticket/27355">#27355</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/35926">#35926</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjscustomizecontrolsjs">trunk/src/wp-admin/js/customize-controls.js</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizecontrolphp">trunk/src/wp-includes/class-wp-customize-control.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizenavmenusphp">trunk/src/wp-includes/class-wp-customize-nav-menus.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizewidgetsphp">trunk/src/wp-includes/class-wp-customize-widgets.php</a></li>
<li><a href="#trunksrcwpincludescustomizeclasswpcustomizepartialphp">trunk/src/wp-includes/customize/class-wp-customize-partial.php</a></li>
<li><a href="#trunktestsphpunittestscustomizepartialphp">trunk/tests/phpunit/tests/customize/partial.php</a></li>
<li><a href="#trunktestsqunitfixturescustomizemenusjs">trunk/tests/qunit/fixtures/customize-menus.js</a></li>
<li><a href="#trunktestsqunitwpadminjscustomizecontrolsjs">trunk/tests/qunit/wp-admin/js/customize-controls.js</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestscustomizecontrolphp">trunk/tests/phpunit/tests/customize/control.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjscustomizecontrolsjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/customize-controls.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/customize-controls.js 2016-02-24 17:37:36 UTC (rev 36688)
+++ trunk/src/wp-admin/js/customize-controls.js 2016-02-24 18:27:45 UTC (rev 36689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1521,19 +1521,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> settings = $.map( control.params.settings, function( value ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return value;
</span><span class="cx" style="display: block; padding: 0 10px"> });
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- api.apply( api, settings.concat( function () {
- var key;
</del><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 0 === settings.length ) {
+ control.setting = null;
</ins><span class="cx" style="display: block; padding: 0 10px"> control.settings = {};
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- for ( key in control.params.settings ) {
- control.settings[ key ] = api( control.params.settings[ key ] );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ control.embed();
+ } else {
+ api.apply( api, settings.concat( function() {
+ var key;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- control.setting = control.settings['default'] || null;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ control.settings = {};
+ for ( key in control.params.settings ) {
+ control.settings[ key ] = api( control.params.settings[ key ] );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- control.embed();
- }) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ control.setting = control.settings['default'] || null;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ control.embed();
+ }) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // After the control is embedded on the page, invoke the "ready" method.
</span><span class="cx" style="display: block; padding: 0 10px"> control.deferred.embedded.done( function () {
</span><span class="cx" style="display: block; padding: 0 10px"> control.ready();
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizecontrolphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-control.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-control.php 2016-02-24 17:37:36 UTC (rev 36688)
+++ trunk/src/wp-includes/class-wp-customize-control.php 2016-02-24 18:27:45 UTC (rev 36689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -65,7 +65,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $setting = 'default';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Capability required to use this control.
+ *
+ * Normally this is empty and the capability is derived from the capabilities
+ * of the associated `$settings`.
+ *
+ * @since 4.5.0
</ins><span class="cx" style="display: block; padding: 0 10px"> * @access public
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @var string
+ */
+ public $capability;
+
+ /**
+ * @access public
</ins><span class="cx" style="display: block; padding: 0 10px"> * @var int
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public $priority = 10;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -187,7 +199,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->instance_number = self::$instance_count;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Process settings.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $this->settings ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $this->settings ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->settings = $id;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -196,7 +208,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $this->settings as $key => $setting ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $settings[ $key ] = $this->manager->get_setting( $setting );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } else {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } else if ( is_string( $this->settings ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->setting = $this->manager->get_setting( $this->settings );
</span><span class="cx" style="display: block; padding: 0 10px"> $settings['default'] = $this->setting;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -299,21 +311,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if the theme supports the control and check user capabilities.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if the user can use this control.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Returns false if the user cannot manipulate one of the associated settings,
+ * or if one of the associated settings does not exist. Also returns false if
+ * the associated section does not exist or if its capability check returns
+ * false.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 3.4.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> final public function check_capabilities() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $this->capability ) && ! current_user_can( $this->capability ) ) {
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $this->settings as $setting ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $setting->check_capabilities() )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $setting || ! $setting->check_capabilities() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $section = $this->manager->get_section( $this->section );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $section ) && ! $section->check_capabilities() )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $section ) && ! $section->check_capabilities() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizenavmenusphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-nav-menus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-nav-menus.php 2016-02-24 17:37:36 UTC (rev 36688)
+++ trunk/src/wp-includes/class-wp-customize-nav-menus.php 2016-02-24 18:27:45 UTC (rev 36689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -606,28 +606,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'priority' => 999,
</span><span class="cx" style="display: block; padding: 0 10px"> ) ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->manager->add_setting( 'new_menu_name', array(
- 'type' => 'new_menu',
- 'default' => '',
- 'transport' => isset( $this->manager->selective_refresh ) ? 'postMessage' : 'refresh',
- ) );
-
</del><span class="cx" style="display: block; padding: 0 10px"> $this->manager->add_control( 'new_menu_name', array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'label' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'section' => 'add_menu',
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'text',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'settings' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'input_attrs' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'class' => 'menu-name-field',
</span><span class="cx" style="display: block; padding: 0 10px"> 'placeholder' => __( 'New menu name' ),
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->manager->add_setting( 'create_new_menu', array(
- 'type' => 'new_menu',
- ) );
-
</del><span class="cx" style="display: block; padding: 0 10px"> $this->manager->add_control( new WP_Customize_New_Menu_Control( $this->manager, 'create_new_menu', array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'section' => 'add_menu',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'section' => 'add_menu',
+ 'settings' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -851,6 +843,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'nav_menu_instance',
</span><span class="cx" style="display: block; padding: 0 10px"> 'render_callback' => array( $this, 'render_nav_menu_partial' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'container_inclusive' => true,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'settings' => array(), // Empty because the nav menu instance may relate to a menu or a location.
+ 'capability' => 'edit_theme_options',
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizewidgetsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-widgets.php 2016-02-24 17:37:36 UTC (rev 36688)
+++ trunk/src/wp-includes/class-wp-customize-widgets.php 2016-02-24 18:27:45 UTC (rev 36689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1485,16 +1485,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function customize_dynamic_partial_args( $partial_args, $partial_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( preg_match( '/^widget\[.+\]$/', $partial_id ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( preg_match( '/^widget\[(?P<widget_id>.+)\]$/', $partial_id, $matches ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( false === $partial_args ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $partial_args = array();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> $partial_args = array_merge(
</span><span class="cx" style="display: block; padding: 0 10px"> $partial_args,
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'type' => 'widget',
- 'render_callback' => array( $this, 'render_widget_partial' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'type' => 'widget',
+ 'render_callback' => array( $this, 'render_widget_partial' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'container_inclusive' => true,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'settings' => array( $this->get_setting_id( $matches['widget_id'] ) ),
+ 'capability' => 'edit_theme_options',
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludescustomizeclasswpcustomizepartialphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/customize/class-wp-customize-partial.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/customize/class-wp-customize-partial.php 2016-02-24 17:37:36 UTC (rev 36688)
+++ trunk/src/wp-includes/customize/class-wp-customize-partial.php 2016-02-24 18:27:45 UTC (rev 36689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -90,6 +90,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $primary_setting;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Capability required to edit this partial.
+ *
+ * Normally this is empty and the capability is derived from the capabilities
+ * of the associated `$settings`.
+ *
+ * @since 4.5.0
+ * @access public
+ * @var string
+ */
+ public $capability;
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Render callback.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.5.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -157,7 +169,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Process settings.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $this->settings ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $this->settings ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->settings = array( $id );
</span><span class="cx" style="display: block; padding: 0 10px"> } else if ( is_string( $this->settings ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->settings = array( $this->settings );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -299,6 +311,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * or if one of the associated settings does not exist.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> final public function check_capabilities() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $this->capability ) && ! current_user_can( $this->capability ) ) {
+ return false;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $this->settings as $setting_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $setting = $this->component->manager->get_setting( $setting_id );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $setting || ! $setting->check_capabilities() ) {
</span></span></pre></div>
<a id="trunktestsphpunittestscustomizecontrolphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/customize/control.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/control.php (rev 0)
+++ trunk/tests/phpunit/tests/customize/control.php 2016-02-24 18:27:45 UTC (rev 36689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,88 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Test_WP_Customize_Control tests.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Tests for the Test_WP_Customize_Control class.
+ *
+ * @todo This is missing dedicated tests for all but one of the methods.
+ *
+ * @group customize
+ */
+class Test_WP_Customize_Control extends WP_UnitTestCase {
+
+ /**
+ * Manager.
+ *
+ * @var WP_Customize_Manager
+ */
+ public $wp_customize;
+
+ /**
+ * Set up.
+ */
+ function setUp() {
+ parent::setUp();
+ require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
+ // @codingStandardsIgnoreStart
+ $GLOBALS['wp_customize'] = new WP_Customize_Manager();
+ // @codingStandardsIgnoreEnd
+ $this->wp_customize = $GLOBALS['wp_customize'];
+ }
+
+ /**
+ * Test WP_Customize_Control::check_capabilities().
+ *
+ * @see WP_Customize_Control::check_capabilities()
+ */
+ function test_check_capabilities() {
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ do_action( 'customize_register', $this->wp_customize );
+ $control = new WP_Customize_Control( $this->wp_customize, 'blogname', array(
+ 'settings' => array( 'blogname' ),
+ ) );
+ $this->assertTrue( $control->check_capabilities() );
+
+ $control = new WP_Customize_Control( $this->wp_customize, 'blogname', array(
+ 'settings' => array( 'blogname', 'non_existing' ),
+ ) );
+ $this->assertFalse( $control->check_capabilities() );
+
+ $this->wp_customize->add_setting( 'top_secret_message', array(
+ 'capability' => 'top_secret_clearance',
+ ) );
+ $control = new WP_Customize_Control( $this->wp_customize, 'blogname', array(
+ 'settings' => array( 'blogname', 'top_secret_clearance' ),
+ ) );
+ $this->assertFalse( $control->check_capabilities() );
+
+ $control = new WP_Customize_Control( $this->wp_customize, 'no_setting', array(
+ 'settings' => array(),
+ ) );
+ $this->assertTrue( $control->check_capabilities() );
+
+ $control = new WP_Customize_Control( $this->wp_customize, 'no_setting', array(
+ 'settings' => array(),
+ 'capability' => 'top_secret_clearance',
+ ) );
+ $this->assertFalse( $control->check_capabilities() );
+
+ $control = new WP_Customize_Control( $this->wp_customize, 'no_setting', array(
+ 'settings' => array(),
+ 'capability' => 'edit_theme_options',
+ ) );
+ $this->assertTrue( $control->check_capabilities() );
+ }
+
+ /**
+ * Tear down.
+ */
+ function tearDown() {
+ $this->wp_customize = null;
+ unset( $GLOBALS['wp_customize'] );
+ parent::tearDown();
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestscustomizepartialphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/customize/partial.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/partial.php 2016-02-24 17:37:36 UTC (rev 36688)
+++ trunk/tests/phpunit/tests/customize/partial.php 2016-02-24 18:27:45 UTC (rev 36689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -325,6 +325,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'settings' => array( 'blogname', 'top_secret_clearance' ),
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $partial->check_capabilities() );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $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() );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsqunitfixturescustomizemenusjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/customize-menus.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/customize-menus.js 2016-02-24 17:37:36 UTC (rev 36688)
+++ trunk/tests/qunit/fixtures/customize-menus.js 2016-02-24 18:27:45 UTC (rev 36689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -394,11 +394,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'description': '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'instanceNumber': 46
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-window._wpCustomizeSettings.settings.new_menu_name = {
- 'value': '',
- 'transport': 'postMessage',
- 'dirty': false
-};
</del><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // From nav-menu.js
</span><span class="cx" style="display: block; padding: 0 10px"> window.wpNavMenu = {
</span></span></pre></div>
<a id="trunktestsqunitwpadminjscustomizecontrolsjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/wp-admin/js/customize-controls.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/wp-admin/js/customize-controls.js 2016-02-24 17:37:36 UTC (rev 36688)
+++ trunk/tests/qunit/wp-admin/js/customize-controls.js 2016-02-24 18:27:45 UTC (rev 36689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -100,6 +100,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> equal( control.section(), 'fixture-section' );
</span><span class="cx" style="display: block; padding: 0 10px"> } );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ module( 'Customizer control without associated settings' );
+ test( 'Control can be created without settings', function() {
+ var control = new wp.customize.Control( 'settingless', {
+ params: {
+ content: jQuery( '<li class="settingless">Hello World</li>' ),
+ section: 'fixture-section'
+ }
+ } );
+ wp.customize.control.add( control.id, control );
+ equal( control.deferred.embedded.state(), 'resolved' );
+ ok( null === control.setting );
+ ok( jQuery.isEmptyObject( control.settings ) );
+ } );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Begin sections.
</span><span class="cx" style="display: block; padding: 0 10px"> module( 'Customizer Section in Fixture' );
</span><span class="cx" style="display: block; padding: 0 10px"> test( 'Fixture section exists', function () {
</span></span></pre>
</div>
</div>
</body>
</html>