WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Получай пассивный доход от сайта с помощью браузерных PUSH уведомлений
класс не описан

Yoast\WP\SEO\Integrations\Front_End

WP_Robots_Integration{} Yoast 1.0

Class WP_Robots_Integration

Хуков нет.

Возвращает

null. Ничего.

Использование

$WP_Robots_Integration = new WP_Robots_Integration();
// use class methods

Методы

  1. __construct( Meta_Tags_Context_Memoizer $context_memoizer )
  2. add_robots( $robots )
  3. filter_robots_no_index( $robots )
  4. format_robots( $robots )
  5. get_conditionals()
  6. get_robots_value()
  7. register_hooks()
  8. sort_robots( $robots )

Заметки

  • Пакет: Yoast\WP\SEO\Integrations\Front_End

Код WP_Robots_Integration{} Yoast 16.1.1

<?php
class WP_Robots_Integration implements Integration_Interface {

	/**
	 * The meta tags context memoizer.
	 *
	 * @var Meta_Tags_Context_Memoizer
	 */
	protected $context_memoizer;

	/**
	 * Sets the dependencies for this integration.
	 *
	 * @param Meta_Tags_Context_Memoizer $context_memoizer The meta tags context memoizer.
	 */
	public function __construct( Meta_Tags_Context_Memoizer $context_memoizer ) {
		$this->context_memoizer = $context_memoizer;
	}

	/**
	 * Initializes the integration.
	 *
	 * This is the place to register hooks and filters.
	 *
	 * @return void
	 */
	public function register_hooks() {
		\add_filter( 'wp_robots', [ $this, 'add_robots' ], PHP_INT_MAX );
	}

	/**
	 * Returns the conditionals based on which this loadable should be active.
	 *
	 * @return array The conditionals.
	 */
	public static function get_conditionals() {
		return [
			Front_End_Conditional::class,
			WP_Robots_Conditional::class,
		];
	}

	/**
	 * Adds our robots tag value to the WordPress robots tag output.
	 *
	 * @param array $robots The current robots data.
	 *
	 * @return array The robots data.
	 */
	public function add_robots( $robots ) {
		if ( ! is_array( $robots ) ) {
			return $this->get_robots_value();
		}

		$merged_robots   = array_merge( $robots, $this->get_robots_value() );
		$filtered_robots = $this->filter_robots_no_index( $merged_robots );
		$sorted_robots   = $this->sort_robots( $filtered_robots );

		// Filter all falsy-null robot values.
		return array_filter( $sorted_robots );
	}

	/**
	 * Retrieves the robots key-value pairs.
	 *
	 * @returns array The robots key-value pairs.
	 */
	protected function get_robots_value() {
		global $wp_query;

		$old_wp_query = $wp_query;
		// phpcs:ignore WordPress.WP.DiscouragedFunctions.wp_reset_query_wp_reset_query -- Reason: The recommended function, wp_reset_postdata, doesn't reset wp_query.
		\wp_reset_query();

		$context = $this->context_memoizer->for_current_page();

		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Reason: we have to restore the query.
		$GLOBALS['wp_query'] = $old_wp_query;

		$robots_presenter               = new Robots_Presenter();
		$robots_presenter->presentation = $context->presentation;

		return $this->format_robots( $robots_presenter->get() );
	}

	/**
	 * Formats our robots fields, to match the pattern WordPress is using.
	 *
	 * Our format: `[ 'index' => 'noindex', 'max-image-preview' => 'max-image-preview:large', ... ]`
	 * WordPress format: `[ 'noindex' => true, 'max-image-preview' => 'large', ... ]`
	 *
	 * @param array $robots Our robots value.
	 *
	 * @return array The formatted robots.
	 */
	protected function format_robots( $robots ) {
		foreach ( $robots as $key => $value ) {
			// When the entry represents for example: max-image-preview:large.
			$colon_position = \strpos( $value, ':' );
			if ( $colon_position !== false ) {
				$robots[ $key ] = \substr( $value, ( $colon_position + 1 ) );

				continue;
			}

			// When index => noindex, we want a separate noindex as entry in array.
			if ( strpos( $value, 'no' ) === 0 ) {
				$robots[ $key ]   = false;
				$robots[ $value ] = true;

				continue;
			}

			// When the key is equal to the value, just make its value a boolean.
			if ( $key === $value ) {
				$robots[ $key ] = true;
			}
		}

		return $robots;
	}

	/**
	 * Filters robots value when page is set noindex.
	 *
	 * WordPress might add some robot values again.
	 * When the page is set to noindex we want to filter out these values.
	 *
	 * @param array $robots The robots.
	 *
	 * @return array The filtered robots.
	 */
	protected function filter_robots_no_index( $robots ) {
		if ( ! isset( $robots['noindex'] ) ) {
			return $robots;
		}

		$robots['imageindex']        = null;
		$robots['noimageindex']      = null;
		$robots['archive']           = null;
		$robots['noarchive']         = null;
		$robots['snippet']           = null;
		$robots['nosnippet']         = null;
		$robots['max-snippet']       = null;
		$robots['max-image-preview'] = null;
		$robots['max-video-preview'] = null;

		return $robots;
	}

	/**
	 * Sorts the robots array.
	 *
	 * @param array $robots The robots array.
	 *
	 * @return array The sorted robots array.
	 */
	protected function sort_robots( $robots ) {
		\uksort(
			$robots,
			function ( $a, $b ) {
				$order = [
					'index'             => 0,
					'noindex'           => 1,
					'follow'            => 2,
					'nofollow'          => 3,
				];
				$ai    = isset( $order[ $a ] ) ? $order[ $a ] : 4;
				$bi    = isset( $order[ $b ] ) ? $order[ $b ] : 4;

				return ( $ai - $bi );
			}
		);

		return $robots;
	}
}