|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Server-side rendering of the `core/site-logo` block. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Renders the `core/site-logo` block on the server. |
| 10 | + * |
| 11 | + * @param array $attributes The block attributes. |
| 12 | + * |
| 13 | + * @return string The render. |
| 14 | + */ |
| 15 | +function render_block_core_site_logo( $attributes ) { |
| 16 | + $adjust_width_height_filter = function ( $image ) use ( $attributes ) { |
| 17 | + if ( empty( $attributes['width'] ) ) { |
| 18 | + return $image; |
| 19 | + } |
| 20 | + $height = (float) $attributes['width'] / ( (float) $image[1] / (float) $image[2] ); |
| 21 | + return array( $image[0], (int) $attributes['width'], (int) $height ); |
| 22 | + }; |
| 23 | + |
| 24 | + add_filter( 'wp_get_attachment_image_src', $adjust_width_height_filter ); |
| 25 | + |
| 26 | + $custom_logo = get_custom_logo(); |
| 27 | + |
| 28 | + remove_filter( 'wp_get_attachment_image_src', $adjust_width_height_filter ); |
| 29 | + |
| 30 | + if ( empty( $custom_logo ) ) { |
| 31 | + return ''; // Return early if no custom logo is set, avoiding extraneous wrapper div. |
| 32 | + } |
| 33 | + |
| 34 | + if ( ! $attributes['isLink'] ) { |
| 35 | + // Remove the link. |
| 36 | + $custom_logo = preg_replace( '#<a.*?>(.*?)</a>#i', '\1', $custom_logo ); |
| 37 | + } |
| 38 | + |
| 39 | + if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) { |
| 40 | + // Add the link target after the rel="home". |
| 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)' ) . '"'; |
| 43 | + $custom_logo = str_replace( 'rel="home"', 'rel="home" target="' . $attributes['linkTarget'] . '"' . $aria_label, $custom_logo ); |
| 44 | + } |
| 45 | + |
| 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'] ) ) { |
| 56 | + $classnames[] = 'is-default-size'; |
| 57 | + } |
| 58 | + |
| 59 | + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) ); |
| 60 | + $html = sprintf( '<div %s>%s</div>', $wrapper_attributes, $custom_logo ); |
| 61 | + return $html; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Register a core site setting for a site logo |
| 66 | + */ |
| 67 | +function register_block_core_site_logo_setting() { |
| 68 | + register_setting( |
| 69 | + 'general', |
| 70 | + 'site_logo', |
| 71 | + array( |
| 72 | + 'show_in_rest' => array( |
| 73 | + 'name' => 'site_logo', |
| 74 | + ), |
| 75 | + 'type' => 'integer', |
| 76 | + 'description' => __( 'Site logo.' ), |
| 77 | + ) |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +add_action( 'rest_api_init', 'register_block_core_site_logo_setting', 10 ); |
| 82 | + |
| 83 | +/** |
| 84 | + * Registers the `core/site-logo` block on the server. |
| 85 | + */ |
| 86 | +function register_block_core_site_logo() { |
| 87 | + register_block_type_from_metadata( |
| 88 | + __DIR__ . '/site-logo', |
| 89 | + array( |
| 90 | + 'render_callback' => 'render_block_core_site_logo', |
| 91 | + ) |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +add_action( 'init', 'register_block_core_site_logo' ); |
| 96 | + |
| 97 | +/** |
| 98 | + * Overrides the custom logo with a site logo, if the option is set. |
| 99 | + * |
| 100 | + * @param string $custom_logo The custom logo set by a theme. |
| 101 | + * |
| 102 | + * @return string The site logo if set. |
| 103 | + */ |
| 104 | +function _override_custom_logo_theme_mod( $custom_logo ) { |
| 105 | + $site_logo = get_option( 'site_logo' ); |
| 106 | + return false === $site_logo ? $custom_logo : $site_logo; |
| 107 | +} |
| 108 | + |
| 109 | +add_filter( 'theme_mod_custom_logo', '_override_custom_logo_theme_mod' ); |
| 110 | + |
| 111 | +/** |
| 112 | + * Updates the site_logo option when the custom_logo theme-mod gets updated. |
| 113 | + * |
| 114 | + * This function is hooked on "update_option_theme_mods_$theme" and not |
| 115 | + * "pre_set_theme_mod_custom_logo" because by hooking in `update_option` |
| 116 | + * the function accounts for remove_theme_mod() as well. |
| 117 | + * |
| 118 | + * @param mixed $old_value The old option value. |
| 119 | + * @param mixed $value The new option value. |
| 120 | + */ |
| 121 | +function _sync_custom_logo_to_site_logo( $old_value, $value ) { |
| 122 | + // Delete the option when the custom logo does not exist or was removed. |
| 123 | + // This step ensures the option stays in sync. |
| 124 | + if ( empty( $value['custom_logo'] ) ) { |
| 125 | + delete_option( 'site_logo' ); |
| 126 | + } else { |
| 127 | + remove_action( 'update_option_site_logo', '_sync_site_logo_to_custom_logo' ); |
| 128 | + update_option( 'site_logo', $value['custom_logo'] ); |
| 129 | + add_action( 'update_option_site_logo', '_sync_site_logo_to_custom_logo', 10, 2 ); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Hooks `_sync_custom_logo_to_site_logo` in `update_option_theme_mods_$theme`. |
| 135 | + * |
| 136 | + * Runs on `setup_theme` to account for dynamically-switched themes in the Customizer. |
| 137 | + */ |
| 138 | +function _sync_custom_logo_to_site_logo_on_setup_theme() { |
| 139 | + $theme = get_option( 'stylesheet' ); |
| 140 | + add_action( "update_option_theme_mods_$theme", '_sync_custom_logo_to_site_logo', 10, 2 ); |
| 141 | +} |
| 142 | +add_action( 'setup_theme', '_sync_custom_logo_to_site_logo_on_setup_theme', 11 ); |
| 143 | + |
| 144 | +/** |
| 145 | + * Updates the custom_logo theme-mod when the site_logo option gets updated. |
| 146 | + * |
| 147 | + * @param mixed $old_value The old option value. |
| 148 | + * @param mixed $value The new option value. |
| 149 | + * |
| 150 | + * @return void |
| 151 | + */ |
| 152 | +function _sync_site_logo_to_custom_logo( $old_value, $value ) { |
| 153 | + // Delete the option when the custom logo does not exist or was removed. |
| 154 | + // This step ensures the option stays in sync. |
| 155 | + if ( empty( $value ) ) { |
| 156 | + remove_theme_mod( 'custom_logo' ); |
| 157 | + } else { |
| 158 | + remove_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' ); |
| 159 | + set_theme_mod( 'custom_logo', $value ); |
| 160 | + add_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' ); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +add_action( 'update_option_site_logo', '_sync_site_logo_to_custom_logo', 10, 2 ); |
0 commit comments