| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- /**
- * Craft in America Custom Endpoints Plugin
- *
- * @since 1.0.0
- * @package cia_endpoints
- *
- * @wordpress-plugin
- * Plugin Name: Craft in America - API Endpoints
- * Plugin URI:
- * Description: Plugin that adds custom rest interface
- * Version: 1.0.0
- * Author: TOJ <john@yvvas.com>
- */
-
- // If this file is called directly, abort.
- if ( ! defined( 'WPINC' ) ) { die; }
-
- require_once('includes/class.make-endpoint.php');
- require_once('includes/class.make-sortby.php');
-
- add_action( 'rest_api_init', function () {
- /**
- * The standard wordpress post_types
- */
- $page_controller = new Make_Endpoint_For('page');
- $page_controller->register_custom_route('pages');
-
- $media_controller = new Make_Endpoint_For('media');
- $media_controller->register_custom_route('media');
-
- $post_controller = new Make_Endpoint_For('post');
- $post_controller->register_custom_route('posts');
-
- /**
- * Craft in America custom post_types
- */
- $episode_controller = new Make_Endpoint_For('episode');
- $episode_controller->register_custom_route('episodes');
-
- $artist_controller = new Make_Endpoint_For('artist');
- $artist_controller->register_custom_route('artists');
-
- $event_controller = new Make_Endpoint_For('event');
- $event_controller->register_custom_route('events');
-
- $exhibition_controller = new Make_Endpoint_For('exhibition');
- $exhibition_controller->register_custom_route('exhibitions');
-
- /**
- * Craft in America custom sort_types
- */
- $sort_controller = new Make_Sort_By('artist', 'by_alpha');
- $sort_controller->register_custom_route('artists/by-alpha');
- $sort_controller = new Make_Sort_By('artist', 'by_material');
- $sort_controller->register_custom_route('artists/by-material');
- });
-
- /**
- * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
- * Depends on: WP REST Cache
- * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
- */
- add_filter('wp_rest_cache/allowed_endpoints', function () {
- // The standard wordpress post_types
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('posts', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'posts';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('pages', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'pages';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('media', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'media';
-
- // Craft in America custom post_types
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('episodes', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'episodes';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('events', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'events';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('exhibitions', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'exhibitions';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('artists', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'artists';
-
- return $allowed_endpoints;
- }, 10, 1);
|