[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> site-logo.php (source)

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


Generated: Tue Jul 27 01:00:09 2021 Cross-referenced by PHPXref 0.7.1