moderntribe / square1-routes
为square one提供路由包装。
4.2.0
2022-10-25 16:44 UTC
Requires
- php: >=7.4
- moderntribe/square1-container: ^4.2
README
示例可以在: wp-content/plugins/core/src/Routes/
中找到
注册路由
REST 路由
在 wp-content/plugins/core/src/Routes/
中创建一个新的类,该类扩展了 Tribe\Libs\Routes\Abstract_REST_Route
<?php declare( strict_types=1 ); /** * Sample REST route. * * @package Project */ namespace Tribe\Project\Routes; use Tribe\Libs\Routes\Abstract_REST_Route; /** * Class for an example REST route. */ class Sample_REST_Route extends Abstract_REST_Route { /** * Registers routes. * * @return void */ public function register(): void { register_rest_route( $this->get_project_namespace(), '/sample', [ 'methods' => \WP_REST_Server::READABLE, 'callback' => [ $this, 'query' ], 'args' => $this->get_supported_args(), 'permission_callback' => '__return_true', ] ); } /** * List of valid query parameters supported by the endpoint. * * @return array Valid parameters for this endpoint. */ public function get_supported_args(): array { return [ 'name' => [ 'type' => 'string', 'default' => '', 'description' => __( 'Example argument.', 'tribe' ), ], ]; } /** * Callback for REST endpoint. * * Example: https://square1.tribe/wp-json/tribe/v1/sample/?name=test * * @param \WP_REST_Request $request The rest request class. * @return \WP_REST_Response|\WP_Error The response object, \WP_Error on failure. */ public function query( $request ) { return rest_ensure_response( new \WP_Error( 'sample_error', sprintf( esc_html__( 'Sample REST Endpoint Error. Params: {name: %1$s}', 'tribe' ), $request->get_param( 'name' ) ) ) ); } }
在 wp-content/plugins/core/src/Routes/Routes_Definer.php
中,将类添加到 Route_Definer::REST_ROUTES
数组中。
重写规则
<?php declare( strict_types=1 ); /** * Sample route. * * @package Project */ namespace Tribe\Project\Routes; use Tribe\Libs\Routes\Abstract_Route; /** * Class to define a sample route. */ class Sample_Route extends Abstract_Route { /** * Javascript configuration for this route. * * @param array $data The current core JS configuration. * @return array Modified core JS configuration. */ public function js_config( array $data = [] ): array { $data['FormSubmitEndpoint'] = rest_url( '/tribe/v1/submit-form/' ); return $data; } /** * The request methods that are authorized on this route. Only GET * is authorized by default. * * @return array Acceptable request methods for this route. */ public function get_request_methods(): array { return [ \WP_REST_Server::READABLE, ]; } /** * Returns the name for the route. * * @return string The name for the route. */ public function get_name(): string { return 'sample'; } /** * Returns the pattern for the route. * * Example: https://square1.tribe/sample/2021/ * * @return string The pattern for the route. */ public function get_pattern(): string { return '^sample\/?((?:19|20)\d{2}?)?\/?$'; } /** * Returns matches for the route. * * @return array Matches for the route. */ public function get_matches(): array { return [ 'year' => '$matches[1]', ]; } /** * Returns the priority of the rewrite rule. * * @return string */ public function get_priority(): string { return 'top'; } /** * Returns query var names for the route. * * @return array Query var names for the route. */ public function get_query_var_names(): array { return array_keys( $this->get_matches() ); } /** * The template to use for the route. * * @return string The template name for the route. */ public function get_template(): string { return locate_template( 'routes/sample.php' ); } /** * Filter the title tag. * * @return string Title for the page. */ public function get_title(): string { return esc_html__( 'Sample | Project', 'project' ); } }
get_priority 方法返回 "top" 或 "bottom",以确定路由应添加到重写数组中的位置。顺序很重要,因为WordPress会查找第一个匹配的路由。
get_pattern 和 get_matches 方法用于根据重写规则的正则表达式设置查询变量。在上面的示例中,路由名称后的第一个匹配项将被设置为年份。当设置重写规则使用的模板时,这将很重要。
重写规则模板
重写规则模板默认存储在 wp-content/themes/core/routes
中,但可以在 get_template 方法中覆盖。上面示例中一个模板的示例可能是
<?php declare( strict_types=1 ); /** * Sample Route template. * * @package Project */ get_header(); ?> <p><?php echo esc_html__( 'Year: ') . get_query_var( 'year' ); ?></p> <p><?php esc_html_e( 'Sample Route', 'tribe' ); ?></p> <?php get_footer();