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


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1