ajax_get_keyword_usage_and_post_types()Yoast 1.0

Retrieves the post ids where the keyword is used before as well as the types of those posts.

Хуков нет.

Возвращает

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

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

ajax_get_keyword_usage_and_post_types();

Код ajax_get_keyword_usage_and_post_types() Yoast 23.4

function ajax_get_keyword_usage_and_post_types() {
	check_ajax_referer( 'wpseo-keyword-usage-and-post-types', 'nonce' );

	if ( ! isset( $_POST['post_id'], $_POST['keyword'] ) || ! is_string( $_POST['keyword'] ) ) {
		die( '-1' );
	}

	// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We are casting to an integer.
	$post_id = (int) wp_unslash( $_POST['post_id'] );

	if ( $post_id === 0 || ! current_user_can( 'edit_post', $post_id ) ) {
		die( '-1' );
	}

	$keyword = sanitize_text_field( wp_unslash( $_POST['keyword'] ) );

	$post_ids = WPSEO_Meta::keyword_usage( $keyword, $post_id );

	if ( ! empty( $post_ids ) ) {
		$post_types = WPSEO_Meta::post_types_for_ids( $post_ids );
	}
	else {
		$post_types = [];
	}

	$return_object = [
		'keyword_usage' => $post_ids,
		'post_types'    => $post_types,
	];

	wp_die(
		// phpcs:ignore WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
		WPSEO_Utils::format_json_encode( $return_object )
	);
}