| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?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-sticky.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('page');
- $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('post');
-
- /**
- * Craft in America custom post_types
- */
- $episode_controller = new Make_Endpoint_For('episode');
- $episode_controller->register_custom_route('episode');
-
- $artist_controller = new Make_Endpoint_For('artist');
- $artist_controller->register_custom_route('artist');
-
- $event_controller = new Make_Endpoint_For('event');
- $event_controller->register_custom_route('event');
-
- $exhibition_controller = new Make_Endpoint_For('exhibition');
- $exhibition_controller->register_custom_route('exhibition');
-
- $guide_controller = new Make_Endpoint_For('guide');
- $guide_controller->register_custom_route('guide');
-
- $short_controller = new Make_Endpoint_For('short');
- $short_controller->register_custom_route('short');
-
- $object_controller = new Make_Endpoint_For('object');
- $object_controller->register_custom_route('object');
-
- $publication_controller = new Make_Endpoint_For('publication');
- $publication_controller->register_custom_route('publication');
-
- $sticky_controller = new Make_Sticky_Endpoint();
- $sticky_controller->register_custom_route('sticky');
-
- /**
- * Craft in America custom sort_types
- */
- $sort_controller = new Make_Sort_By('artist', 'by_alpha');
- $sort_controller->register_custom_route('artist/by-alpha');
- $sort_controller = new Make_Sort_By('artist', 'by_material');
- $sort_controller->register_custom_route('artist/by-material');
- $sort_controller = new Make_Sort_By('event', 'by_past');
- $sort_controller->register_custom_route('event/by-past');
- $sort_controller = new Make_Sort_By('exhibition', 'by_past');
- $sort_controller->register_custom_route('exhibition/by-past');
- $sort_controller = new Make_Sort_By('event', 'by_current');
- $sort_controller->register_custom_route('event/by-current');
- $sort_controller = new Make_Sort_By('exhibition', 'by_current');
- $sort_controller->register_custom_route('exhibition/by-current');
- $sort_controller = new Make_Sort_By('event', 'by_current_and_upcoming');
- $sort_controller->register_custom_route('event/by-current-and-upcoming');
- $sort_controller = new Make_Sort_By('exhibition', 'by_current_and_upcoming');
- $sort_controller->register_custom_route('exhibition/by-current-and-upcoming');
- });
-
- /**
- * 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('post', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'post';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('page', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'page';
- 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('episode', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'episode';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('event', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'event';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('exhibition', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'exhibition';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('artist', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'artist';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('guide', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'guide';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('short', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'short';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('object', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'object';
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array('publication', $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = 'publication';
-
- return $allowed_endpoints;
- }, 10, 1);
-
- add_filter('excerpt_length', function ($length) {
- return 60;
- });
|