WPSEO_Metabox::save_postdata()publicYoast 1.0

Saves the WP SEO metadata for posts.

{@internal $_POST parameters are validated via sanitize_post_meta().}}

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

Возвращает

true|false|null. Boolean false if invalid save post request.

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

$WPSEO_Metabox = new WPSEO_Metabox();
$WPSEO_Metabox->save_postdata( $post_id );
$post_id(int) (обязательный)
Post ID.

Код WPSEO_Metabox::save_postdata() Yoast 22.4

public function save_postdata( $post_id ) {
	// Bail if this is a multisite installation and the site has been switched.
	if ( is_multisite() && ms_is_switched() ) {
		return false;
	}

	if ( $post_id === null ) {
		return false;
	}

	// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized in wp_verify_none.
	if ( ! isset( $_POST['yoast_free_metabox_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['yoast_free_metabox_nonce'] ), 'yoast_free_metabox' ) ) {
		return false;
	}

	if ( wp_is_post_revision( $post_id ) ) {
		$post_id = wp_is_post_revision( $post_id );
	}

	/**
	 * Determine we're not accidentally updating a different post.
	 * We can't use filter_input here as the ID isn't available at this point, other than in the $_POST data.
	 */
	if ( ! isset( $_POST['ID'] ) || $post_id !== (int) $_POST['ID'] ) {
		return false;
	}

	clean_post_cache( $post_id );
	$post = get_post( $post_id );

	if ( ! is_object( $post ) ) {
		// Non-existent post.
		return false;
	}

	do_action( 'wpseo_save_compare_data', $post );

	$social_fields = [];
	if ( $this->social_is_enabled ) {
		$social_fields = WPSEO_Meta::get_meta_field_defs( 'social' );
	}

	$meta_boxes = apply_filters( 'wpseo_save_metaboxes', [] );
	$meta_boxes = array_merge(
		$meta_boxes,
		WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
		WPSEO_Meta::get_meta_field_defs( 'advanced' ),
		$social_fields,
		WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
	);

	foreach ( $meta_boxes as $key => $meta_box ) {

		// If analysis is disabled remove that analysis score value from the DB.
		if ( $this->is_meta_value_disabled( $key ) ) {
			WPSEO_Meta::delete( $key, $post_id );
			continue;
		}

		$data       = null;
		$field_name = WPSEO_Meta::$form_prefix . $key;

		if ( $meta_box['type'] === 'checkbox' ) {
			$data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
		}
		else {
			if ( isset( $_POST[ $field_name ] ) ) {
				// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We're preparing to do just that.
				$data = wp_unslash( $_POST[ $field_name ] );

				// For multi-select.
				if ( is_array( $data ) ) {
					$data = array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
				}

				if ( is_string( $data ) ) {
					$data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
				}
			}

			// Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
			if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
				$data = [];
			}
		}

		if ( $data !== null ) {
			WPSEO_Meta::set_value( $key, $data, $post_id );
		}
	}

	do_action( 'wpseo_saved_postdata' );
}