Nav apraksta

site-logo.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 ) ) {
  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="' . $attributes['linkTarget'] . '"' . $aria_label, $custom_logo );
  37. }
  38. $classnames = array();
  39. if ( ! empty( $attributes['className'] ) ) {
  40. $classnames[] = $attributes['className'];
  41. }
  42. if ( ! empty( $attributes['align'] ) && in_array( $attributes['align'], array( 'center', 'left', 'right' ), true ) ) {
  43. $classnames[] = "align{$attributes['align']}";
  44. }
  45. if ( empty( $attributes['width'] ) ) {
  46. $classnames[] = 'is-default-size';
  47. }
  48. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
  49. $html = sprintf( '<div %s>%s</div>', $wrapper_attributes, $custom_logo );
  50. return $html;
  51. }
  52. /**
  53. * Register a core site setting for a site logo
  54. */
  55. function register_block_core_site_logo_setting() {
  56. register_setting(
  57. 'general',
  58. 'site_logo',
  59. array(
  60. 'show_in_rest' => array(
  61. 'name' => 'site_logo',
  62. ),
  63. 'type' => 'integer',
  64. 'description' => __( 'Site logo.' ),
  65. )
  66. );
  67. }
  68. add_action( 'rest_api_init', 'register_block_core_site_logo_setting', 10 );
  69. /**
  70. * Registers the `core/site-logo` block on the server.
  71. */
  72. function register_block_core_site_logo() {
  73. register_block_type_from_metadata(
  74. __DIR__ . '/site-logo',
  75. array(
  76. 'render_callback' => 'render_block_core_site_logo',
  77. )
  78. );
  79. }
  80. add_action( 'init', 'register_block_core_site_logo' );
  81. /**
  82. * Overrides the custom logo with a site logo, if the option is set.
  83. *
  84. * @param string $custom_logo The custom logo set by a theme.
  85. *
  86. * @return string The site logo if set.
  87. */
  88. function _override_custom_logo_theme_mod( $custom_logo ) {
  89. $site_logo = get_option( 'site_logo' );
  90. return false === $site_logo ? $custom_logo : $site_logo;
  91. }
  92. add_filter( 'theme_mod_custom_logo', '_override_custom_logo_theme_mod' );
  93. /**
  94. * Updates the site_logo option when the custom_logo theme-mod gets updated.
  95. *
  96. * @param mixed $value Attachment ID of the custom logo or an empty value.
  97. * @return mixed
  98. */
  99. function _sync_custom_logo_to_site_logo( $value ) {
  100. if ( empty( $value ) ) {
  101. delete_option( 'site_logo' );
  102. } else {
  103. update_option( 'site_logo', $value );
  104. }
  105. return $value;
  106. }
  107. add_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' );
  108. /**
  109. * Deletes the site_logo when the custom_logo theme mod is removed.
  110. *
  111. * @param array $old_value Previous theme mod settings.
  112. * @param array $value Updated theme mod settings.
  113. */
  114. function _delete_site_logo_on_remove_custom_logo( $old_value, $value ) {
  115. // If the custom_logo is being unset, it's being removed from theme mods.
  116. if ( isset( $old_value['custom_logo'] ) && ! isset( $value['custom_logo'] ) ) {
  117. delete_option( 'site_logo' );
  118. }
  119. }
  120. /**
  121. * Deletes the site logo when all theme mods are being removed.
  122. */
  123. function _delete_site_logo_on_remove_theme_mods() {
  124. if ( false !== get_theme_support( 'custom-logo' ) ) {
  125. delete_option( 'site_logo' );
  126. }
  127. }
  128. /**
  129. * Hooks `_delete_site_logo_on_remove_custom_logo` in `update_option_theme_mods_$theme`.
  130. * Hooks `_delete_site_logo_on_remove_theme_mods` in `delete_option_theme_mods_$theme`.
  131. *
  132. * Runs on `setup_theme` to account for dynamically-switched themes in the Customizer.
  133. */
  134. function _delete_site_logo_on_remove_custom_logo_on_setup_theme() {
  135. $theme = get_option( 'stylesheet' );
  136. add_action( "update_option_theme_mods_$theme", '_delete_site_logo_on_remove_custom_logo', 10, 2 );
  137. add_action( "delete_option_theme_mods_$theme", '_delete_site_logo_on_remove_theme_mods' );
  138. }
  139. add_action( 'setup_theme', '_delete_site_logo_on_remove_custom_logo_on_setup_theme', 11 );