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'] ) || empty( $image ) || ! $image[1] || ! $image[2] ) { |
---|
18 | return $image; |
---|
19 | } |
---|
20 | |
---|
21 | $height = (float) $attributes['width'] / ( (float) $image[1] / (float) $image[2] ); |
---|
22 | return array( $image[0], (int) $attributes['width'], (int) $height ); |
---|
23 | }; |
---|
24 | |
---|
25 | add_filter( 'wp_get_attachment_image_src', $adjust_width_height_filter ); |
---|
26 | |
---|
27 | $custom_logo = get_custom_logo(); |
---|
28 | |
---|
29 | remove_filter( 'wp_get_attachment_image_src', $adjust_width_height_filter ); |
---|
30 | |
---|
31 | if ( empty( $custom_logo ) ) { |
---|
32 | return ''; // Return early if no custom logo is set, avoiding extraneous wrapper div. |
---|
33 | } |
---|
34 | |
---|
35 | if ( ! $attributes['isLink'] ) { |
---|
36 | // Remove the link. |
---|
37 | $custom_logo = preg_replace( '#<a.*?>(.*?)</a>#i', '\1', $custom_logo ); |
---|
38 | } |
---|
39 | |
---|
40 | if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) { |
---|
41 | // Add the link target after the rel="home". |
---|
42 | // Add an aria-label for informing that the page opens in a new tab. |
---|
43 | $aria_label = 'aria-label="' . esc_attr__( '(Home link, opens in a new tab)' ) . '"'; |
---|
44 | $custom_logo = str_replace( 'rel="home"', 'rel="home" target="' . $attributes['linkTarget'] . '"' . $aria_label, $custom_logo ); |
---|
45 | } |
---|
46 | |
---|
47 | $classnames = array(); |
---|
48 | if ( ! empty( $attributes['className'] ) ) { |
---|
49 | $classnames[] = $attributes['className']; |
---|
50 | } |
---|
51 | |
---|
52 | if ( empty( $attributes['width'] ) ) { |
---|
53 | $classnames[] = 'is-default-size'; |
---|
54 | } |
---|
55 | |
---|
56 | $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) ); |
---|
57 | $html = sprintf( '<div %s>%s</div>', $wrapper_attributes, $custom_logo ); |
---|
58 | return $html; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Register a core site setting for a site logo |
---|
63 | */ |
---|
64 | function register_block_core_site_logo_setting() { |
---|
65 | register_setting( |
---|
66 | 'general', |
---|
67 | 'site_logo', |
---|
68 | array( |
---|
69 | 'show_in_rest' => array( |
---|
70 | 'name' => 'site_logo', |
---|
71 | ), |
---|
72 | 'type' => 'integer', |
---|
73 | 'description' => __( 'Site logo.' ), |
---|
74 | ) |
---|
75 | ); |
---|
76 | } |
---|
77 | |
---|
78 | add_action( 'rest_api_init', 'register_block_core_site_logo_setting', 10 ); |
---|
79 | |
---|
80 | /** |
---|
81 | * Register a core site setting for a site icon |
---|
82 | */ |
---|
83 | function register_block_core_site_icon_setting() { |
---|
84 | register_setting( |
---|
85 | 'general', |
---|
86 | 'site_icon', |
---|
87 | array( |
---|
88 | 'show_in_rest' => true, |
---|
89 | 'type' => 'integer', |
---|
90 | 'description' => __( 'Site icon.' ), |
---|
91 | ) |
---|
92 | ); |
---|
93 | } |
---|
94 | |
---|
95 | add_action( 'rest_api_init', 'register_block_core_site_icon_setting', 10 ); |
---|
96 | |
---|
97 | /** |
---|
98 | * Registers the `core/site-logo` block on the server. |
---|
99 | */ |
---|
100 | function register_block_core_site_logo() { |
---|
101 | register_block_type_from_metadata( |
---|
102 | __DIR__ . '/site-logo', |
---|
103 | array( |
---|
104 | 'render_callback' => 'render_block_core_site_logo', |
---|
105 | ) |
---|
106 | ); |
---|
107 | } |
---|
108 | |
---|
109 | add_action( 'init', 'register_block_core_site_logo' ); |
---|
110 | |
---|
111 | /** |
---|
112 | * Overrides the custom logo with a site logo, if the option is set. |
---|
113 | * |
---|
114 | * @param string $custom_logo The custom logo set by a theme. |
---|
115 | * |
---|
116 | * @return string The site logo if set. |
---|
117 | */ |
---|
118 | function _override_custom_logo_theme_mod( $custom_logo ) { |
---|
119 | $site_logo = get_option( 'site_logo' ); |
---|
120 | return false === $site_logo ? $custom_logo : $site_logo; |
---|
121 | } |
---|
122 | |
---|
123 | add_filter( 'theme_mod_custom_logo', '_override_custom_logo_theme_mod' ); |
---|
124 | |
---|
125 | /** |
---|
126 | * Updates the site_logo option when the custom_logo theme-mod gets updated. |
---|
127 | * |
---|
128 | * @param mixed $value Attachment ID of the custom logo or an empty value. |
---|
129 | * @return mixed |
---|
130 | */ |
---|
131 | function _sync_custom_logo_to_site_logo( $value ) { |
---|
132 | if ( empty( $value ) ) { |
---|
133 | delete_option( 'site_logo' ); |
---|
134 | } else { |
---|
135 | update_option( 'site_logo', $value ); |
---|
136 | } |
---|
137 | |
---|
138 | return $value; |
---|
139 | } |
---|
140 | |
---|
141 | add_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' ); |
---|
142 | |
---|
143 | /** |
---|
144 | * Deletes the site_logo when the custom_logo theme mod is removed. |
---|
145 | * |
---|
146 | * @param array $old_value Previous theme mod settings. |
---|
147 | * @param array $value Updated theme mod settings. |
---|
148 | */ |
---|
149 | function _delete_site_logo_on_remove_custom_logo( $old_value, $value ) { |
---|
150 | global $_ignore_site_logo_changes; |
---|
151 | |
---|
152 | if ( $_ignore_site_logo_changes ) { |
---|
153 | return; |
---|
154 | } |
---|
155 | |
---|
156 | // If the custom_logo is being unset, it's being removed from theme mods. |
---|
157 | if ( isset( $old_value['custom_logo'] ) && ! isset( $value['custom_logo'] ) ) { |
---|
158 | delete_option( 'site_logo' ); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Deletes the site logo when all theme mods are being removed. |
---|
164 | */ |
---|
165 | function _delete_site_logo_on_remove_theme_mods() { |
---|
166 | global $_ignore_site_logo_changes; |
---|
167 | |
---|
168 | if ( $_ignore_site_logo_changes ) { |
---|
169 | return; |
---|
170 | } |
---|
171 | |
---|
172 | if ( false !== get_theme_support( 'custom-logo' ) ) { |
---|
173 | delete_option( 'site_logo' ); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Hooks `_delete_site_logo_on_remove_custom_logo` in `update_option_theme_mods_$theme`. |
---|
179 | * Hooks `_delete_site_logo_on_remove_theme_mods` in `delete_option_theme_mods_$theme`. |
---|
180 | * |
---|
181 | * Runs on `setup_theme` to account for dynamically-switched themes in the Customizer. |
---|
182 | */ |
---|
183 | function _delete_site_logo_on_remove_custom_logo_on_setup_theme() { |
---|
184 | $theme = get_option( 'stylesheet' ); |
---|
185 | add_action( "update_option_theme_mods_$theme", '_delete_site_logo_on_remove_custom_logo', 10, 2 ); |
---|
186 | add_action( "delete_option_theme_mods_$theme", '_delete_site_logo_on_remove_theme_mods' ); |
---|
187 | } |
---|
188 | add_action( 'setup_theme', '_delete_site_logo_on_remove_custom_logo_on_setup_theme', 11 ); |
---|
189 | |
---|
190 | /** |
---|
191 | * Removes the custom_logo theme-mod when the site_logo option gets deleted. |
---|
192 | */ |
---|
193 | function _delete_custom_logo_on_remove_site_logo() { |
---|
194 | global $_ignore_site_logo_changes; |
---|
195 | |
---|
196 | // Prevent _delete_site_logo_on_remove_custom_logo and |
---|
197 | // _delete_site_logo_on_remove_theme_mods from firing and causing an |
---|
198 | // infinite loop. |
---|
199 | $_ignore_site_logo_changes = true; |
---|
200 | |
---|
201 | // Remove the custom logo. |
---|
202 | remove_theme_mod( 'custom_logo' ); |
---|
203 | |
---|
204 | $_ignore_site_logo_changes = false; |
---|
205 | } |
---|
206 | add_action( 'delete_option_site_logo', '_delete_custom_logo_on_remove_site_logo' ); |
---|