Customize: Only add custom logo’s header text control if needed.

Dissolves `WP_CustomLogo` and adopts a structure similar to custom header and background (See `_delete_attachment_theme_mod()`).
The option to hide header text only gets added if it’s not already part of custom header, and only if selectors have been registered when theme support for custom logos was declared. Themes can add `postMessage` support for it as well.

Example:
{{{
add_theme_support( 'custom-logo', array(
    'size' => ‘large’,
    'header-text' => array( 'site-title', 'site-description' ),
) );
}}}

See #33755.


git-svn-id: https://develop.svn.wordpress.org/trunk@36915 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Konstantin Obenland 2016-03-09 23:43:49 +00:00
parent 4286a3e63b
commit 6d951b3c5d
7 changed files with 36 additions and 163 deletions

View File

@ -72,9 +72,6 @@ require_once(ABSPATH . 'wp-admin/includes/user.php');
/** WordPress Site Icon API */
require_once(ABSPATH . 'wp-admin/includes/class-wp-site-icon.php');
/** WordPress Custom Logo API */
require_once(ABSPATH . 'wp-admin/includes/class-wp-custom-logo.php');
/** WordPress Update Administration API */
require_once(ABSPATH . 'wp-admin/includes/update.php');

View File

@ -1,109 +0,0 @@
<?php
/**
* Administration API: WP_Custom_Logo class
*
* @package WordPress
* @subpackage Administration
* @since 4.5.0
*/
/**
* Core class used to implement custom logo functionality.
*
* @since 4.5.0
*/
class WP_Custom_Logo {
/**
* Get current logo settings stored in theme mod.
*
* @since 4.5.0
* @access public
*/
public function __construct() {
add_action( 'wp_head', array( $this, 'head_text_styles' ) );
add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ) );
}
/**
* Hides header text on the front end if necessary.
*
* @since 4.5.0
* @access public
*/
public function head_text_styles() {
// Bail if our theme supports custom headers.
if ( current_theme_supports( 'custom-header' ) || get_theme_mod( 'custom_logo_header_text', true ) ) {
return;
}
// Is Display Header Text unchecked? If so, hide the header text.
?>
<!-- Custom Logo: hide header text -->
<style type="text/css">
<?php echo sanitize_html_class( $this->header_text_classes() ); ?> {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
}
</style>
<?php
}
/**
* Reset the custom logo if the current logo is deleted in the media manager.
*
* @since 4.5.0
* @access public
*
* @param int $post_id Post ID.
*/
public function delete_attachment_data( $post_id ) {
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( $custom_logo_id && $custom_logo_id == $post_id ) {
remove_theme_mod( 'custom_logo' );
}
}
/**
* Retrieves the header text classes.
*
* If not defined in add_theme_support(), defaults from Underscores will be used.
*
* @since 4.5.0
* @access protected
*
* @return string String of classes to hide.
*/
protected function header_text_classes() {
$args = get_theme_support( 'custom-logo' );
if ( isset( $args[0]['header-text'] ) ) {
// Use any classes defined in add_theme_support().
$classes = $args[0]['header-text'];
} else {
// Otherwise, use these defaults, which will work with any Underscores-based theme.
$classes = array(
'site-title',
'site-description',
);
}
// If there's an array of classes, reduce them to a string for output.
if ( is_array( $classes ) ) {
$classes = array_map( 'sanitize_html_class', $classes );
$classes = (string) '.' . implode( ', .', $classes );
} else {
$classes = (string) '.' . $classes;
}
return $classes;
}
}
/**
* WP_Custom_Logo instance.
*
* @global WP_Custom_Logo $wp_custom_logo
*/
$GLOBALS['wp_custom_logo'] = new WP_Custom_Logo;

View File

@ -1922,13 +1922,12 @@ final class WP_Customize_Manager {
'section' => 'title_tagline',
) );
// Add a setting to hide header text if the theme isn't supporting the feature itself.
// @todo
if ( ! current_theme_supports( 'custom-header' ) ) {
// Add a setting to hide header text if the theme doesn't support custom headers.
if ( ! current_theme_supports( 'custom-header', 'header-text' ) ) {
$this->add_setting( 'header_text', array(
'theme_supports' => array( 'custom-logo', 'header-text' ),
'default' => 1,
'sanitize_callback' => 'absint',
'transport' => 'postMessage',
) );
$this->add_control( 'header_text', array(

View File

@ -371,6 +371,7 @@ add_action( 'parse_request', 'rest_api_loaded' );
*/
// Theme
add_action( 'wp_loaded', '_custom_header_background_just_in_time' );
add_action( 'wp_head', '_custom_logo_header_styles' );
add_action( 'plugins_loaded', '_wp_customize_include' );
add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' );
add_action( 'delete_attachment', '_delete_attachment_theme_mod' );

View File

@ -880,8 +880,7 @@ function get_custom_logo( $blog_id = 0 ) {
if ( is_multisite() && ms_is_switched() ) {
restore_current_blog();
}
$size = get_theme_support( 'custom-logo' );
$size = $size[0]['size'];
$size = get_theme_support( 'custom-logo', 'size' );
// We have a logo. Logo is go.
if ( $custom_logo_id ) {

View File

@ -1731,6 +1731,30 @@ function _custom_header_background_just_in_time() {
}
}
/**
* Adds CSS to hide header text for custom logo, based on Customizer setting.
*
* @since 4.5.0
* @access private
*/
function _custom_logo_header_styles() {
if ( ! current_theme_supports( 'custom-header', 'header-text' ) && get_theme_support( 'custom-logo', 'header-text' ) && ! get_theme_mod( 'header_text', true ) ) {
$classes = (array) get_theme_support( 'custom-logo', 'header-text' );
$classes = array_map( 'sanitize_html_class', $classes );
$classes = '.' . implode( ', .', $classes );
?>
<!-- Custom Logo: hide header text -->
<style id="custom-logo-css" type="text/css">
<?php echo $classes; ?> {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
}
</style>
<?php
}
}
/**
* Gets the theme support arguments passed when registering that support
*
@ -1927,6 +1951,7 @@ function require_if_theme_supports( $feature, $include ) {
* @access private
* @since 3.0.0
* @since 4.3.0 Also removes `header_image_data`.
* @since 4.5.0 Also removes custom logo theme mods.
*
* @param int $id The attachment id.
*/
@ -1934,6 +1959,12 @@ function _delete_attachment_theme_mod( $id ) {
$attachment_image = wp_get_attachment_url( $id );
$header_image = get_header_image();
$background_image = get_background_image();
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( $custom_logo_id && $custom_logo_id == $id ) {
remove_theme_mod( 'custom_logo' );
remove_theme_mod( 'header_text' );
}
if ( $header_image && $header_image == $attachment_image ) {
remove_theme_mod( 'header_image' );

View File

@ -1,45 +0,0 @@
<?php
/**
* Tests for the WP_Custom_logo class.
*
* @group custom_logo
*/
class Tests_WP_Custom_Logo extends WP_UnitTestCase {
public $attachment_id = 0;
function setUp() {
parent::setUp();
require_once ABSPATH . 'wp-admin/includes/class-wp-custom-logo.php';
}
function tearDown() {
$this->custom_logo = null;
$this->remove_added_uploads();
parent::tearDown();
}
function test_delete_attachment_data() {
$attachment_id = $this->_insert_attachment();
set_theme_mod( 'custom_logo', $attachment_id );
wp_delete_attachment( $attachment_id, true );
$this->assertFalse( get_theme_mod( 'custom_logo' ) );
}
function _insert_attachment() {
if ( $this->attachment_id ) {
return $this->attachment_id;
}
$filename = DIR_TESTDATA . '/images/test-image.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( basename( $filename ), null, $contents );
$this->attachment_id = $this->_make_attachment( $upload );
return $this->attachment_id;
}
}