wp/wp-includes/blocks/site-logo.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     6  */
     6  */
     7 
     7 
     8 /**
     8 /**
     9  * Renders the `core/site-logo` block on the server.
     9  * Renders the `core/site-logo` block on the server.
    10  *
    10  *
       
    11  * @since 5.8.0
       
    12  *
    11  * @param array $attributes The block attributes.
    13  * @param array $attributes The block attributes.
    12  *
    14  *
    13  * @return string The render.
    15  * @return string The render.
    14  */
    16  */
    15 function render_block_core_site_logo( $attributes ) {
    17 function render_block_core_site_logo( $attributes ) {
    16 	$adjust_width_height_filter = function ( $image ) use ( $attributes ) {
    18 	$adjust_width_height_filter = static function ( $image ) use ( $attributes ) {
    17 		if ( empty( $attributes['width'] ) || empty( $image ) || ! $image[1] || ! $image[2] ) {
    19 		if ( empty( $attributes['width'] ) || empty( $image ) || ! $image[1] || ! $image[2] ) {
    18 			return $image;
    20 			return $image;
    19 		}
    21 		}
    20 		$height = (float) $attributes['width'] / ( (float) $image[1] / (float) $image[2] );
    22 		$height = (float) $attributes['width'] / ( (float) $image[1] / (float) $image[2] );
    21 		return array( $image[0], (int) $attributes['width'], (int) $height );
    23 		return array( $image[0], (int) $attributes['width'], (int) $height );
    37 	}
    39 	}
    38 
    40 
    39 	if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) {
    41 	if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) {
    40 		// Add the link target after the rel="home".
    42 		// Add the link target after the rel="home".
    41 		// Add an aria-label for informing that the page opens in a new tab.
    43 		// Add an aria-label for informing that the page opens in a new tab.
    42 		$aria_label  = 'aria-label="' . esc_attr__( '(Home link, opens in a new tab)' ) . '"';
    44 		$processor = new WP_HTML_Tag_Processor( $custom_logo );
    43 		$custom_logo = str_replace( 'rel="home"', 'rel="home" target="' . esc_attr( $attributes['linkTarget'] ) . '"' . $aria_label, $custom_logo );
    45 		$processor->next_tag( 'a' );
       
    46 		if ( 'home' === $processor->get_attribute( 'rel' ) ) {
       
    47 			$processor->set_attribute( 'aria-label', __( '(Home link, opens in a new tab)' ) );
       
    48 			$processor->set_attribute( 'target', $attributes['linkTarget'] );
       
    49 		}
       
    50 		$custom_logo = $processor->get_updated_html();
    44 	}
    51 	}
    45 
    52 
    46 	$classnames = array();
    53 	$classnames = array();
    47 	if ( empty( $attributes['width'] ) ) {
    54 	if ( empty( $attributes['width'] ) ) {
    48 		$classnames[] = 'is-default-size';
    55 		$classnames[] = 'is-default-size';
    53 	return $html;
    60 	return $html;
    54 }
    61 }
    55 
    62 
    56 /**
    63 /**
    57  * Register a core site setting for a site logo
    64  * Register a core site setting for a site logo
       
    65  *
       
    66  * @since 5.8.0
    58  */
    67  */
    59 function register_block_core_site_logo_setting() {
    68 function register_block_core_site_logo_setting() {
    60 	register_setting(
    69 	register_setting(
    61 		'general',
    70 		'general',
    62 		'site_logo',
    71 		'site_logo',
    63 		array(
    72 		array(
    64 			'show_in_rest' => array(
    73 			'show_in_rest' => array(
    65 				'name' => 'site_logo',
    74 				'name' => 'site_logo',
    66 			),
    75 			),
    67 			'type'         => 'integer',
    76 			'type'         => 'integer',
       
    77 			'label'        => __( 'Logo' ),
    68 			'description'  => __( 'Site logo.' ),
    78 			'description'  => __( 'Site logo.' ),
    69 		)
    79 		)
    70 	);
    80 	);
    71 }
    81 }
    72 
    82 
    73 add_action( 'rest_api_init', 'register_block_core_site_logo_setting', 10 );
    83 add_action( 'rest_api_init', 'register_block_core_site_logo_setting', 10 );
    74 
    84 
    75 /**
    85 /**
    76  * Register a core site setting for a site icon
    86  * Register a core site setting for a site icon
       
    87  *
       
    88  * @since 5.9.0
    77  */
    89  */
    78 function register_block_core_site_icon_setting() {
    90 function register_block_core_site_icon_setting() {
    79 	register_setting(
    91 	register_setting(
    80 		'general',
    92 		'general',
    81 		'site_icon',
    93 		'site_icon',
    82 		array(
    94 		array(
    83 			'show_in_rest' => true,
    95 			'show_in_rest' => true,
    84 			'type'         => 'integer',
    96 			'type'         => 'integer',
       
    97 			'label'        => __( 'Icon' ),
    85 			'description'  => __( 'Site icon.' ),
    98 			'description'  => __( 'Site icon.' ),
    86 		)
    99 		)
    87 	);
   100 	);
    88 }
   101 }
    89 
   102 
    90 add_action( 'rest_api_init', 'register_block_core_site_icon_setting', 10 );
   103 add_action( 'rest_api_init', 'register_block_core_site_icon_setting', 10 );
    91 
   104 
    92 /**
   105 /**
    93  * Registers the `core/site-logo` block on the server.
   106  * Registers the `core/site-logo` block on the server.
       
   107  *
       
   108  * @since 5.8.0
    94  */
   109  */
    95 function register_block_core_site_logo() {
   110 function register_block_core_site_logo() {
    96 	register_block_type_from_metadata(
   111 	register_block_type_from_metadata(
    97 		__DIR__ . '/site-logo',
   112 		__DIR__ . '/site-logo',
    98 		array(
   113 		array(
   104 add_action( 'init', 'register_block_core_site_logo' );
   119 add_action( 'init', 'register_block_core_site_logo' );
   105 
   120 
   106 /**
   121 /**
   107  * Overrides the custom logo with a site logo, if the option is set.
   122  * Overrides the custom logo with a site logo, if the option is set.
   108  *
   123  *
       
   124  * @since 5.8.0
       
   125  *
   109  * @param string $custom_logo The custom logo set by a theme.
   126  * @param string $custom_logo The custom logo set by a theme.
   110  *
   127  *
   111  * @return string The site logo if set.
   128  * @return string The site logo if set.
   112  */
   129  */
   113 function _override_custom_logo_theme_mod( $custom_logo ) {
   130 function _override_custom_logo_theme_mod( $custom_logo ) {
   117 
   134 
   118 add_filter( 'theme_mod_custom_logo', '_override_custom_logo_theme_mod' );
   135 add_filter( 'theme_mod_custom_logo', '_override_custom_logo_theme_mod' );
   119 
   136 
   120 /**
   137 /**
   121  * Updates the site_logo option when the custom_logo theme-mod gets updated.
   138  * Updates the site_logo option when the custom_logo theme-mod gets updated.
       
   139  *
       
   140  * @since 5.8.0
   122  *
   141  *
   123  * @param  mixed $value Attachment ID of the custom logo or an empty value.
   142  * @param  mixed $value Attachment ID of the custom logo or an empty value.
   124  * @return mixed
   143  * @return mixed
   125  */
   144  */
   126 function _sync_custom_logo_to_site_logo( $value ) {
   145 function _sync_custom_logo_to_site_logo( $value ) {
   136 add_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' );
   155 add_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' );
   137 
   156 
   138 /**
   157 /**
   139  * Deletes the site_logo when the custom_logo theme mod is removed.
   158  * Deletes the site_logo when the custom_logo theme mod is removed.
   140  *
   159  *
       
   160  * @since 5.8.0
       
   161  *
   141  * @param array $old_value Previous theme mod settings.
   162  * @param array $old_value Previous theme mod settings.
   142  * @param array $value     Updated theme mod settings.
   163  * @param array $value     Updated theme mod settings.
   143  */
   164  */
   144 function _delete_site_logo_on_remove_custom_logo( $old_value, $value ) {
   165 function _delete_site_logo_on_remove_custom_logo( $old_value, $value ) {
   145 	global $_ignore_site_logo_changes;
   166 	global $_ignore_site_logo_changes;
   154 	}
   175 	}
   155 }
   176 }
   156 
   177 
   157 /**
   178 /**
   158  * Deletes the site logo when all theme mods are being removed.
   179  * Deletes the site logo when all theme mods are being removed.
       
   180  *
       
   181  * @since 5.8.0
   159  */
   182  */
   160 function _delete_site_logo_on_remove_theme_mods() {
   183 function _delete_site_logo_on_remove_theme_mods() {
   161 	global $_ignore_site_logo_changes;
   184 	global $_ignore_site_logo_changes;
   162 
   185 
   163 	if ( $_ignore_site_logo_changes ) {
   186 	if ( $_ignore_site_logo_changes ) {
   172 /**
   195 /**
   173  * Hooks `_delete_site_logo_on_remove_custom_logo` in `update_option_theme_mods_$theme`.
   196  * Hooks `_delete_site_logo_on_remove_custom_logo` in `update_option_theme_mods_$theme`.
   174  * Hooks `_delete_site_logo_on_remove_theme_mods` in `delete_option_theme_mods_$theme`.
   197  * Hooks `_delete_site_logo_on_remove_theme_mods` in `delete_option_theme_mods_$theme`.
   175  *
   198  *
   176  * Runs on `setup_theme` to account for dynamically-switched themes in the Customizer.
   199  * Runs on `setup_theme` to account for dynamically-switched themes in the Customizer.
       
   200  *
       
   201  * @since 5.8.0
   177  */
   202  */
   178 function _delete_site_logo_on_remove_custom_logo_on_setup_theme() {
   203 function _delete_site_logo_on_remove_custom_logo_on_setup_theme() {
   179 	$theme = get_option( 'stylesheet' );
   204 	$theme = get_option( 'stylesheet' );
   180 	add_action( "update_option_theme_mods_$theme", '_delete_site_logo_on_remove_custom_logo', 10, 2 );
   205 	add_action( "update_option_theme_mods_$theme", '_delete_site_logo_on_remove_custom_logo', 10, 2 );
   181 	add_action( "delete_option_theme_mods_$theme", '_delete_site_logo_on_remove_theme_mods' );
   206 	add_action( "delete_option_theme_mods_$theme", '_delete_site_logo_on_remove_theme_mods' );
   182 }
   207 }
   183 add_action( 'setup_theme', '_delete_site_logo_on_remove_custom_logo_on_setup_theme', 11 );
   208 add_action( 'setup_theme', '_delete_site_logo_on_remove_custom_logo_on_setup_theme', 11 );
   184 
   209 
   185 /**
   210 /**
   186  * Removes the custom_logo theme-mod when the site_logo option gets deleted.
   211  * Removes the custom_logo theme-mod when the site_logo option gets deleted.
       
   212  *
       
   213  * @since 5.9.0
   187  */
   214  */
   188 function _delete_custom_logo_on_remove_site_logo() {
   215 function _delete_custom_logo_on_remove_site_logo() {
   189 	global $_ignore_site_logo_changes;
   216 	global $_ignore_site_logo_changes;
   190 
   217 
   191 	// Prevent _delete_site_logo_on_remove_custom_logo and
   218 	// Prevent _delete_site_logo_on_remove_custom_logo and