site-logo.php 5.7 KB

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