rest_api_initхук-событиеWC 4.4.0

Это хук WordPress - rest_api_init. Плагин его просто использует.

Срабатывает в начале обработки REST API запроса (когда подготавливается обслуживание REST запроса).

Во время срабатывания этого хука нужно регистрировать новые маршруты REST (см. register_rest_route()), чтобы быть уверенным, что они будут срабатывать в момент REST запроса.

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

add_action( 'rest_api_init', 'wp_kama_rest_api_init_action' );

/**
 * Function for `rest_api_init` action-hook.
 * 
 * @param WP_REST_Server $wp_rest_server Server object.
 *
 * @return void
 */
function wp_kama_rest_api_init_action( $wp_rest_server ){

	// action...
}
$wp_rest_server(WP_REST_Server)
Объект REST сервера.

Примеры

10

#1 Создание новой конечной точки для своего плагина

add_action( 'rest_api_init', function(){

	register_rest_route( 'myplug/v2', '/posts', array(
		'methods'  => 'GET',
		'callback' => 'myplug_get_post_items',
	) );

} );

function myplug_get_post_items(){
	$posts = get_posts( array (
		'post_status' => 'publish',
		'numberposts' => 100
	) ) ;

	$items = array();

	foreach( $posts as $post ){
		$items[] = array(
			'id'      => $post->ID,
			'title'   => $post->post_title,
			'author'  => get_the_author_meta( 'display_name', $post->post_author ),
			'content' => apply_filters( 'the_content', $post->post_content ),
			'teaser'  => $post->post_excerpt
		);
	}

	return $items;
}

После установки этого кода, эндпоинт:

GET http://example.com/wp-json/myplug/v2/posts

Будет отдавать посты сайта в указанном формате.

Где вызывается хук

WC_CLI_Runner::after_wp_load()
rest_api_init
woocommerce/includes/cli/class-wc-cli-runner.php 59
do_action( 'rest_api_init', $wp_rest_server );

Где используется хук в WooCommerce

woocommerce/includes/admin/helper/class-wc-helper-admin.php 40
add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
woocommerce/includes/admin/helper/class-wc-helper-orders-api.php 24
add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
woocommerce/includes/admin/helper/class-wc-helper-subscriptions-api.php 26
add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
woocommerce/includes/class-wc-brands.php 67
add_action( 'rest_api_init', array( $this, 'rest_api_register_routes' ) );
woocommerce/includes/class-woocommerce.php 304
add_action( 'rest_api_init', array( $this, 'register_wp_admin_settings' ) );
woocommerce/includes/rest-api/Server.php 32
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 );
woocommerce/packages/email-editor/src/Engine/class-email-editor.php 98
add_action( 'rest_api_init', array( $this, 'register_email_editor_api_routes' ) );
woocommerce/src/Admin/API/Init.php 48
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
woocommerce/src/Admin/Features/Blueprint/Init.php 47
add_action( 'rest_api_init', array( $this, 'init_rest_api' ) );
woocommerce/src/Admin/Features/ProductBlockEditor/Init.php 76
add_action( 'rest_api_init', array( $this, 'register_layout_templates' ) );
woocommerce/src/Admin/Features/ProductBlockEditor/Init.php 77
add_action( 'rest_api_init', array( $this, 'register_user_metas' ) );
woocommerce/src/Blocks/BlockTypes/Checkout.php 41
add_action( 'rest_api_init', array( $this, 'register_settings' ) );
woocommerce/src/Blocks/BlockTypes/ProductCollection/Controller.php 71
add_action( 'rest_api_init', array( $this, 'register_settings' ) );
woocommerce/src/Blocks/Shipping/ShippingController.php 67
add_action( 'rest_api_init', array( $this, 'register_settings' ) );
woocommerce/src/Internal/Admin/WCAdminUser.php 21
add_action( 'rest_api_init', array( $this, 'register_user_data' ) );