PHP code example of moderntribe / square1-routes

1. Go to this page and download the library: Download moderntribe/square1-routes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moderntribe / square1-routes example snippets


     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' )
					)
				)
			);
		}
	}

     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' );
		}
	}

     declare( strict_types=1 );
	/**
	* Sample Route template.
	*
	* @package Project
	*/
	get_header();