rest_find_any_matching_schema()


WordPressのrest_find_any_matching_schema()関数は、REST APIのスキーマを検索し、リクエストに一致するスキーマを返します。

構文

rest_find_any_matching_schema( array $schemas, array $request );

引数の説明:

  • $schemas(array)—検索対象のスキーマ配列
  • $request(array)—照合するリクエストデータ

例1: 基本的なスキーママッチング

単純なスキーマ配列とリクエストを照合します。

$matched = rest_find_any_matching_schema( $schemas, $request );

例2: カスタムスキーマの検索

カスタムスキーマを定義して照合します。

$schemas = array( 'custom' => array( 'type' => 'object' ) ); $matched = rest_find_any_matching_schema( $schemas, array( 'type' => 'object' ) );

例3: 複数スキーマからの一致検索

複数のスキーマから最初に一致するものを取得します。

$schemas = array( array( 'type' => 'string' ), array( 'type' => 'integer' ) ); $result = rest_find_any_matching_schema( $schemas, array( 'type' => 'integer' ) );

例4: 空のリクエストでの使用

空のリクエストでデフォルトスキーマを取得します。

$default = rest_find_any_matching_schema( $schemas, array() );

例5: ネストされたスキーマの照合

ネストされたスキーマ構造を検索します。

$schemas = array( 'nested' => array( 'properties' => array( 'id' => array( 'type' => 'integer' ) ) ) ); $match = rest_find_any_matching_schema( $schemas, array( 'properties' => array( 'id' => array( 'type' => 'integer' ) ) ) );

例6: タイプ不一致時の挙動

タイプが一致しない場合の動作を確認します。

$no_match = rest_find_any_matching_schema( array( array( 'type' => 'string' ) ), array( 'type' => 'boolean' ) );

例7: 実際のREST APIリクエストでの使用

REST APIコールバック内で使用する例です。

add_filter( 'rest_request_before_callbacks', function( $response, $handler, $request ) { $schema = rest_find_any_matching_schema( $handler->get_schema(), $request->get_params() ); return $response; }, 10, 3 );

注意点:

  • スキーマ構造が不正な場合、予期せぬ結果が発生する可能性があります
  • 大規模なスキーマ配列ではパフォーマンスに影響する場合があります

関連機能: