WPSEO_Sitemaps::get_last_modified_gmt()public staticYoast 3.2

Get the GMT modification date for the last modified post in the post type.

Метод класса: WPSEO_Sitemaps{}

Хуков нет.

Возвращает

Строку|Массив|false.

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

$result = WPSEO_Sitemaps::get_last_modified_gmt( $post_types, $return_all );
$post_types(строка|массив) (обязательный)
Post type or array of types.
$return_all(true|false)
Flag to return array of values.
По умолчанию: false

Список изменений

С версии 3.2 Введена.

Код WPSEO_Sitemaps::get_last_modified_gmt() Yoast 22.4

public static function get_last_modified_gmt( $post_types, $return_all = false ) {

	global $wpdb;

	static $post_type_dates = null;

	if ( ! is_array( $post_types ) ) {
		$post_types = [ $post_types ];
	}

	foreach ( $post_types as $post_type ) {
		if ( ! isset( $post_type_dates[ $post_type ] ) ) { // If we hadn't seen post type before. R.
			$post_type_dates = null;
			break;
		}
	}

	if ( is_null( $post_type_dates ) ) {

		$post_type_dates = [];
		$post_type_names = WPSEO_Post_Type::get_accessible_post_types();

		if ( ! empty( $post_type_names ) ) {
			$post_statuses = array_map( 'esc_sql', self::get_post_statuses() );
			$replacements  = array_merge(
				[
					'post_type',
					'post_modified_gmt',
					'date',
					$wpdb->posts,
					'post_status',
				],
				$post_statuses,
				[ 'post_type' ],
				array_keys( $post_type_names ),
				[
					'post_type',
					'date',
				]
			);

			//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- We need to use a direct query here.
			//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches.
			$dates = $wpdb->get_results(
				//phpcs:disable WordPress.DB.PreparedSQLPlaceholders -- %i placeholder is still not recognized.
				$wpdb->prepare(
					'
				SELECT %i, MAX(%i) AS %i
				FROM %i
				WHERE %i IN (' . implode( ', ', array_fill( 0, count( $post_statuses ), '%s' ) ) . ')
					AND %i IN (' . implode( ', ', array_fill( 0, count( $post_type_names ), '%s' ) ) . ')
				GROUP BY %i
				ORDER BY %i DESC
			',
					$replacements
				)
			);

			foreach ( $dates as $obj ) {
				$post_type_dates[ $obj->post_type ] = $obj->date;
			}
		}
	}

	$dates = array_intersect_key( $post_type_dates, array_flip( $post_types ) );

	if ( count( $dates ) > 0 ) {
		if ( $return_all ) {
			return $dates;
		}

		return max( $dates );
	}

	return false;
}