PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / block-library / blocks / site-logo.php

site-logo.php in Gutenberg 12.6.0, at build/block-library/blocks/site-logo.php

206 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 gutenberg_render_block_core_site_logo( $attributes ) {
16 $adjust_width_height_filter = function ( $image ) use ( $attributes ) {
17 if ( empty( $attributes['width'] ) || empty( $image ) ) {
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="' . esc_attr( $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['width'] ) ) {
52 $classnames[] = 'is-default-size';
53 }
54
55 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
56 $html = sprintf( '<div %s>%s</div>', $wrapper_attributes, $custom_logo );
57 return $html;
58 }
59
60 /**
61 * Register a core site setting for a site logo
62 */
63 function gutenberg_gutenberg_register_block_core_site_logo_setting() {
64 register_setting(
65 'general',
66 'site_logo',
67 array(
68 'show_in_rest' => array(
69 'name' => 'site_logo',
70 ),
71 'type' => 'integer',
72 'description' => __( 'Site logo.' ),
73 )
74 );
75 }
76
77 add_action( 'rest_api_init', 'gutenberg_gutenberg_register_block_core_site_logo_setting', 10 );
78
79 /**
80 * Register a core site setting for a site icon
81 */
82 function gutenberg_register_block_core_site_icon_setting() {
83 register_setting(
84 'general',
85 'site_icon',
86 array(
87 'show_in_rest' => true,
88 'type' => 'integer',
89 'description' => __( 'Site icon.' ),
90 )
91 );
92 }
93
94 add_action( 'rest_api_init', 'gutenberg_register_block_core_site_icon_setting', 10 );
95
96 /**
97 * Registers the `core/site-logo` block on the server.
98 */
99 function gutenberg_register_block_core_site_logo() {
100 register_block_type_from_metadata(
101 __DIR__ . '/site-logo',
102 array(
103 'render_callback' => 'gutenberg_render_block_core_site_logo',
104 )
105 );
106 }
107
108 add_action( 'init', 'gutenberg_register_block_core_site_logo', 20 );
109
110 /**
111 * Overrides the custom logo with a site logo, if the option is set.
112 *
113 * @param string $custom_logo The custom logo set by a theme.
114 *
115 * @return string The site logo if set.
116 */
117 function gutenberg__override_custom_logo_theme_mod( $custom_logo ) {
118 $site_logo = get_option( 'site_logo' );
119 return false === $site_logo ? $custom_logo : $site_logo;
120 }
121
122 add_filter( 'theme_mod_custom_logo', 'gutenberg__override_custom_logo_theme_mod' );
123
124 /**
125 * Updates the site_logo option when the custom_logo theme-mod gets updated.
126 *
127 * @param mixed $value Attachment ID of the custom logo or an empty value.
128 * @return mixed
129 */
130 function gutenberg__sync_custom_logo_to_site_logo( $value ) {
131 if ( empty( $value ) ) {
132 delete_option( 'site_logo' );
133 } else {
134 update_option( 'site_logo', $value );
135 }
136
137 return $value;
138 }
139
140 add_filter( 'pre_set_theme_mod_custom_logo', 'gutenberg__sync_custom_logo_to_site_logo' );
141
142 /**
143 * Deletes the site_logo when the custom_logo theme mod is removed.
144 *
145 * @param array $old_value Previous theme mod settings.
146 * @param array $value Updated theme mod settings.
147 */
148 function gutenberg__delete_site_logo_on_remove_custom_logo( $old_value, $value ) {
149 global $_ignore_site_logo_changes;
150
151 if ( $_ignore_site_logo_changes ) {
152 return;
153 }
154
155 // If the custom_logo is being unset, it's being removed from theme mods.
156 if ( isset( $old_value['custom_logo'] ) && ! isset( $value['custom_logo'] ) ) {
157 delete_option( 'site_logo' );
158 }
159 }
160
161 /**
162 * Deletes the site logo when all theme mods are being removed.
163 */
164 function gutenberg__delete_site_logo_on_remove_theme_mods() {
165 global $_ignore_site_logo_changes;
166
167 if ( $_ignore_site_logo_changes ) {
168 return;
169 }
170
171 if ( false !== get_theme_support( 'custom-logo' ) ) {
172 delete_option( 'site_logo' );
173 }
174 }
175
176 /**
177 * Hooks `gutenberg__delete_site_logo_on_remove_custom_logo` in `update_option_theme_mods_$theme`.
178 * Hooks `gutenberg__delete_site_logo_on_remove_theme_mods` in `delete_option_theme_mods_$theme`.
179 *
180 * Runs on `setup_theme` to account for dynamically-switched themes in the Customizer.
181 */
182 function gutenberg_gutenberg__delete_site_logo_on_remove_custom_logo_on_setup_theme() {
183 $theme = get_option( 'stylesheet' );
184 add_action( "update_option_theme_mods_$theme", 'gutenberg__delete_site_logo_on_remove_custom_logo', 10, 2 );
185 add_action( "delete_option_theme_mods_$theme", 'gutenberg__delete_site_logo_on_remove_theme_mods' );
186 }
187 add_action( 'setup_theme', 'gutenberg_gutenberg__delete_site_logo_on_remove_custom_logo_on_setup_theme', 11 );
188
189 /**
190 * Removes the custom_logo theme-mod when the site_logo option gets deleted.
191 */
192 function gutenberg__delete_custom_logo_on_remove_site_logo() {
193 global $_ignore_site_logo_changes;
194
195 // Prevent gutenberg__delete_site_logo_on_remove_custom_logo and
196 // gutenberg__delete_site_logo_on_remove_theme_mods from firing and causing an
197 // infinite loop.
198 $_ignore_site_logo_changes = true;
199
200 // Remove the custom logo.
201 remove_theme_mod( 'custom_logo' );
202
203 $_ignore_site_logo_changes = false;
204 }
205 add_action( 'delete_option_site_logo', 'gutenberg__delete_custom_logo_on_remove_site_logo' );
206