PluginProbe
Gutenberg / 10.5.3
Gutenberg v10.5.3
24.0.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 All 403 releases
gutenberg / build / block-library / blocks / site-logo.php

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

109 lines 3.1 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'] ) ) {
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 $custom_logo = get_custom_logo();
26 $classnames = array();
27 if ( ! empty( $attributes['className'] ) ) {
28 $classnames[] = $attributes['className'];
29 }
30
31 if ( ! empty( $attributes['align'] ) && in_array( $attributes['align'], array( 'center', 'left', 'right' ), true ) ) {
32 $classnames[] = "align{$attributes['align']}";
33 }
34
35 if ( empty( $attributes['width'] ) ) {
36 $classnames[] = 'is-default-size';
37 }
38
39 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
40 $html = sprintf( '<div %s>%s</div>', $wrapper_attributes, $custom_logo );
41 remove_filter( 'wp_get_attachment_image_src', $adjust_width_height_filter );
42 return $html;
43 }
44
45
46 /**
47 * Registers the `core/site-logo` block on the server.
48 */
49 function gutenberg_register_block_core_site_logo() {
50 register_block_type_from_metadata(
51 __DIR__ . '/site-logo',
52 array(
53 'render_callback' => 'gutenberg_render_block_core_site_logo',
54 )
55 );
56 add_filter( 'pre_set_theme_mod_custom_logo', 'gutenberg_sync_site_logo_to_theme_mod' );
57 add_filter( 'theme_mod_custom_logo', 'gutenberg_override_custom_logo_theme_mod' );
58 }
59 add_action( 'init', 'gutenberg_register_block_core_site_logo', 20 );
60
61 /**
62 * Overrides the custom logo with a site logo, if the option is set.
63 *
64 * @param string $custom_logo The custom logo set by a theme.
65 *
66 * @return string The site logo if set.
67 */
68 function gutenberg_override_custom_logo_theme_mod( $custom_logo ) {
69 $sitelogo = get_option( 'sitelogo' );
70 return false === $sitelogo ? $custom_logo : $sitelogo;
71 }
72
73 /**
74 * Syncs the site logo with the theme modified logo.
75 *
76 * @param string $custom_logo The custom logo set by a theme.
77 *
78 * @return string The custom logo.
79 */
80 function gutenberg_sync_site_logo_to_theme_mod( $custom_logo ) {
81 // Delete the option when the custom logo does not exist or was removed.
82 // This step ensures the option stays in sync.
83 if ( empty( $custom_logo ) ) {
84 delete_option( 'sitelogo' );
85 } else {
86 update_option( 'sitelogo', $custom_logo );
87 }
88 return $custom_logo;
89 }
90
91 /**
92 * Register a core site setting for a site logo
93 */
94 function gutenberg_gutenberg_register_block_core_site_logo_setting() {
95 register_setting(
96 'general',
97 'sitelogo',
98 array(
99 'show_in_rest' => array(
100 'name' => 'sitelogo',
101 ),
102 'type' => 'string',
103 'description' => __( 'Site logo.' ),
104 )
105 );
106 }
107
108 add_action( 'rest_api_init', 'gutenberg_gutenberg_register_block_core_site_logo_setting', 10 );
109