wp/wp-includes/blocks/site-logo.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    12  *
    12  *
    13  * @return string The render.
    13  * @return string The render.
    14  */
    14  */
    15 function render_block_core_site_logo( $attributes ) {
    15 function render_block_core_site_logo( $attributes ) {
    16 	$adjust_width_height_filter = function ( $image ) use ( $attributes ) {
    16 	$adjust_width_height_filter = function ( $image ) use ( $attributes ) {
    17 		if ( empty( $attributes['width'] ) || empty( $image ) ) {
    17 		if ( empty( $attributes['width'] ) || empty( $image ) || ! $image[1] || ! $image[2] ) {
    18 			return $image;
    18 			return $image;
    19 		}
    19 		}
    20 		$height = (float) $attributes['width'] / ( (float) $image[1] / (float) $image[2] );
    20 		$height = (float) $attributes['width'] / ( (float) $image[1] / (float) $image[2] );
    21 		return array( $image[0], (int) $attributes['width'], (int) $height );
    21 		return array( $image[0], (int) $attributes['width'], (int) $height );
    22 	};
    22 	};
    38 
    38 
    39 	if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) {
    39 	if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) {
    40 		// Add the link target after the rel="home".
    40 		// Add the link target after the rel="home".
    41 		// Add an aria-label for informing that the page opens in a new tab.
    41 		// 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)' ) . '"';
    42 		$aria_label  = 'aria-label="' . esc_attr__( '(Home link, opens in a new tab)' ) . '"';
    43 		$custom_logo = str_replace( 'rel="home"', 'rel="home" target="' . $attributes['linkTarget'] . '"' . $aria_label, $custom_logo );
    43 		$custom_logo = str_replace( 'rel="home"', 'rel="home" target="' . esc_attr( $attributes['linkTarget'] ) . '"' . $aria_label, $custom_logo );
    44 	}
    44 	}
    45 
    45 
    46 	$classnames = array();
    46 	$classnames = array();
    47 	if ( ! empty( $attributes['className'] ) ) {
       
    48 		$classnames[] = $attributes['className'];
       
    49 	}
       
    50 
       
    51 	if ( ! empty( $attributes['align'] ) && in_array( $attributes['align'], array( 'center', 'left', 'right' ), true ) ) {
       
    52 		$classnames[] = "align{$attributes['align']}";
       
    53 	}
       
    54 
       
    55 	if ( empty( $attributes['width'] ) ) {
    47 	if ( empty( $attributes['width'] ) ) {
    56 		$classnames[] = 'is-default-size';
    48 		$classnames[] = 'is-default-size';
    57 	}
    49 	}
    58 
    50 
    59 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
    51 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
    79 }
    71 }
    80 
    72 
    81 add_action( 'rest_api_init', 'register_block_core_site_logo_setting', 10 );
    73 add_action( 'rest_api_init', 'register_block_core_site_logo_setting', 10 );
    82 
    74 
    83 /**
    75 /**
       
    76  * Register a core site setting for a site icon
       
    77  */
       
    78 function register_block_core_site_icon_setting() {
       
    79 	register_setting(
       
    80 		'general',
       
    81 		'site_icon',
       
    82 		array(
       
    83 			'show_in_rest' => true,
       
    84 			'type'         => 'integer',
       
    85 			'description'  => __( 'Site icon.' ),
       
    86 		)
       
    87 	);
       
    88 }
       
    89 
       
    90 add_action( 'rest_api_init', 'register_block_core_site_icon_setting', 10 );
       
    91 
       
    92 /**
    84  * Registers the `core/site-logo` block on the server.
    93  * Registers the `core/site-logo` block on the server.
    85  */
    94  */
    86 function register_block_core_site_logo() {
    95 function register_block_core_site_logo() {
    87 	register_block_type_from_metadata(
    96 	register_block_type_from_metadata(
    88 		__DIR__ . '/site-logo',
    97 		__DIR__ . '/site-logo',
   131  *
   140  *
   132  * @param array $old_value Previous theme mod settings.
   141  * @param array $old_value Previous theme mod settings.
   133  * @param array $value     Updated theme mod settings.
   142  * @param array $value     Updated theme mod settings.
   134  */
   143  */
   135 function _delete_site_logo_on_remove_custom_logo( $old_value, $value ) {
   144 function _delete_site_logo_on_remove_custom_logo( $old_value, $value ) {
       
   145 	global $_ignore_site_logo_changes;
       
   146 
       
   147 	if ( $_ignore_site_logo_changes ) {
       
   148 		return;
       
   149 	}
       
   150 
   136 	// If the custom_logo is being unset, it's being removed from theme mods.
   151 	// If the custom_logo is being unset, it's being removed from theme mods.
   137 	if ( isset( $old_value['custom_logo'] ) && ! isset( $value['custom_logo'] ) ) {
   152 	if ( isset( $old_value['custom_logo'] ) && ! isset( $value['custom_logo'] ) ) {
   138 		delete_option( 'site_logo' );
   153 		delete_option( 'site_logo' );
   139 	}
   154 	}
   140 }
   155 }
   141 
   156 
   142 /**
   157 /**
   143  * Deletes the site logo when all theme mods are being removed.
   158  * Deletes the site logo when all theme mods are being removed.
   144  */
   159  */
   145 function _delete_site_logo_on_remove_theme_mods() {
   160 function _delete_site_logo_on_remove_theme_mods() {
       
   161 	global $_ignore_site_logo_changes;
       
   162 
       
   163 	if ( $_ignore_site_logo_changes ) {
       
   164 		return;
       
   165 	}
       
   166 
   146 	if ( false !== get_theme_support( 'custom-logo' ) ) {
   167 	if ( false !== get_theme_support( 'custom-logo' ) ) {
   147 		delete_option( 'site_logo' );
   168 		delete_option( 'site_logo' );
   148 	}
   169 	}
   149 }
   170 }
   150 
   171 
   158 	$theme = get_option( 'stylesheet' );
   179 	$theme = get_option( 'stylesheet' );
   159 	add_action( "update_option_theme_mods_$theme", '_delete_site_logo_on_remove_custom_logo', 10, 2 );
   180 	add_action( "update_option_theme_mods_$theme", '_delete_site_logo_on_remove_custom_logo', 10, 2 );
   160 	add_action( "delete_option_theme_mods_$theme", '_delete_site_logo_on_remove_theme_mods' );
   181 	add_action( "delete_option_theme_mods_$theme", '_delete_site_logo_on_remove_theme_mods' );
   161 }
   182 }
   162 add_action( 'setup_theme', '_delete_site_logo_on_remove_custom_logo_on_setup_theme', 11 );
   183 add_action( 'setup_theme', '_delete_site_logo_on_remove_custom_logo_on_setup_theme', 11 );
       
   184 
       
   185 /**
       
   186  * Removes the custom_logo theme-mod when the site_logo option gets deleted.
       
   187  */
       
   188 function _delete_custom_logo_on_remove_site_logo() {
       
   189 	global $_ignore_site_logo_changes;
       
   190 
       
   191 	// Prevent _delete_site_logo_on_remove_custom_logo and
       
   192 	// _delete_site_logo_on_remove_theme_mods from firing and causing an
       
   193 	// infinite loop.
       
   194 	$_ignore_site_logo_changes = true;
       
   195 
       
   196 	// Remove the custom logo.
       
   197 	remove_theme_mod( 'custom_logo' );
       
   198 
       
   199 	$_ignore_site_logo_changes = false;
       
   200 }
       
   201 add_action( 'delete_option_site_logo', '_delete_custom_logo_on_remove_site_logo' );