WPSEO_Meta::clean_up()public staticYoast 1.0

General clean-up of the saved meta values.

  • Remove potentially lingering old meta keys;
  • Remove all default and invalid values.

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

Возвращает

null. Ничего (null).

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

$result = WPSEO_Meta::clean_up();

Код WPSEO_Meta::clean_up() Yoast 22.3

public static function clean_up() {
	global $wpdb;

	/*
	 * Clean up '_yoast_wpseo_meta-robots'.
	 *
	 * Retrieve all '_yoast_wpseo_meta-robots' meta values and convert if no new values found.
	 *
	 * {@internal Query is pretty well optimized this way.}}
	 *
	 * @todo [JRF => Yoast] Find out all possible values which the old '_yoast_wpseo_meta-robots' could contain
	 * to convert the data correctly.
	 */
	$query  = $wpdb->prepare(
		"
			SELECT `a`.*
			FROM {$wpdb->postmeta} AS a
			WHERE `a`.`meta_key` = %s
				AND NOT	EXISTS (
					SELECT DISTINCT `post_id` , count( `meta_id` ) AS count
					FROM {$wpdb->postmeta} AS b
					WHERE `a`.`post_id` = `b`.`post_id`
						AND ( `meta_key` = %s
						OR `meta_key` = %s )
					GROUP BY `post_id`
				)
			;",
		self::$meta_prefix . 'meta-robots',
		self::$meta_prefix . 'meta-robots-noindex',
		self::$meta_prefix . 'meta-robots-nofollow'
	);
	$oldies = $wpdb->get_results( $query );

	if ( is_array( $oldies ) && $oldies !== [] ) {
		foreach ( $oldies as $old ) {
			$old_values = explode( ',', $old->meta_value );
			foreach ( $old_values as $value ) {
				if ( $value === 'noindex' ) {
					update_post_meta( $old->post_id, self::$meta_prefix . 'meta-robots-noindex', 1 );
				}
				elseif ( $value === 'nofollow' ) {
					update_post_meta( $old->post_id, self::$meta_prefix . 'meta-robots-nofollow', 1 );
				}
			}
		}
	}
	unset( $query, $oldies, $old, $old_values, $value );

	// Delete old keys.
	delete_post_meta_by_key( self::$meta_prefix . 'meta-robots' );

	/*
	 * Remove all default values and (most) invalid option values.
	 * Invalid option values for the multiselect (meta-robots-adv) field will be dealt with seperately.
	 *
	 * {@internal Some of the defaults have changed in v1.5, but as the defaults will
	 *            be removed and new defaults will now automatically be passed when no
	 *            data found, this update is automatic (as long as we remove the old
	 *            values which we do in the below routine).}}
	 *
	 * {@internal Unfortunately we can't use the normal delete_meta() with key/value combination
	 *            as '' (empty string) values will be ignored and would result in all metas
	 *            with that key being deleted, not just the empty fields.
	 *            Still, the below implementation is largely based on the delete_meta() function.}}
	 */
	$query = [];

	foreach ( self::$meta_fields as $subset => $field_group ) {
		foreach ( $field_group as $key => $field_def ) {
			if ( ! isset( $field_def['default_value'] ) ) {
				continue;
			}

			if ( isset( $field_def['options'] ) && is_array( $field_def['options'] ) && $field_def['options'] !== [] ) {
				$valid = $field_def['options'];
				// Remove the default value from the valid options.
				unset( $valid[ $field_def['default_value'] ] );
				$valid = array_keys( $valid );

				$query[] = $wpdb->prepare(
					"( meta_key = %s AND meta_value NOT IN ( '" . implode( "','", esc_sql( $valid ) ) . "' ) )",
					self::$meta_prefix . $key
				);
				unset( $valid );
			}
			elseif ( is_string( $field_def['default_value'] ) && $field_def['default_value'] !== '' ) {
				$query[] = $wpdb->prepare(
					'( meta_key = %s AND meta_value = %s )',
					self::$meta_prefix . $key,
					$field_def['default_value']
				);
			}
			else {
				$query[] = $wpdb->prepare(
					"( meta_key = %s AND meta_value = '' )",
					self::$meta_prefix . $key
				);
			}
		}
	}
	unset( $subset, $field_group, $key, $field_def );

	$query    = "SELECT meta_id FROM {$wpdb->postmeta} WHERE " . implode( ' OR ', $query ) . ';';
	$meta_ids = $wpdb->get_col( $query );

	if ( is_array( $meta_ids ) && $meta_ids !== [] ) {
		// WP native action.
		do_action( 'delete_post_meta', $meta_ids, null, null, null );

		$query = "DELETE FROM {$wpdb->postmeta} WHERE meta_id IN( " . implode( ',', $meta_ids ) . ' )';
		$count = $wpdb->query( $query );

		if ( $count ) {
			foreach ( $meta_ids as $object_id ) {
				wp_cache_delete( $object_id, 'post_meta' );
			}

			// WP native action.
			do_action( 'deleted_post_meta', $meta_ids, null, null, null );
		}
	}
	unset( $query, $meta_ids, $count, $object_id );

	/*
	 * Deal with the multiselect (meta-robots-adv) field.
	 *
	 * Removes invalid option combinations, such as 'none,noarchive'.
	 *
	 * Default values have already been removed, so we should have a small result set and
	 * (hopefully) even smaller set of invalid results.
	 */
	$query  = $wpdb->prepare(
		"SELECT meta_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s",
		self::$meta_prefix . 'meta-robots-adv'
	);
	$oldies = $wpdb->get_results( $query );

	if ( is_array( $oldies ) && $oldies !== [] ) {
		foreach ( $oldies as $old ) {
			$clean = self::validate_meta_robots_adv( $old->meta_value );

			if ( $clean !== $old->meta_value ) {
				if ( $clean !== self::$meta_fields['advanced']['meta-robots-adv']['default_value'] ) {
					update_metadata_by_mid( 'post', $old->meta_id, $clean );
				}
				else {
					delete_metadata_by_mid( 'post', $old->meta_id );
				}
			}
		}
	}
	unset( $query, $oldies, $old, $clean );

	do_action( 'wpseo_meta_clean_up' );
}