| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?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/all-types.php');
- require_once('includes/class.make-oneofeach.php');
- require_once('includes/class.make-endpoint.php');
- require_once('includes/class.make-terms.php');
- require_once('includes/class.make-sticky.php');
- require_once('includes/class.make-sortby.php');
- require_once('includes/class.make-search.php');
-
- function _unsnake($input) {
- return str_replace('_', '-', $input);
- }
-
- function _make_sorts($post_types, $sorts_types) {
- foreach ($sorts_types as $sort) {
- $unsnaked = _unsnake($sort);
- foreach ($post_types as $type) {
- $sort_controller = new Make_Sort_By($type, $sort);
- $sort_controller->register_custom_route("$type/$unsnaked");
- }
- }
- }
-
- add_action( 'rest_api_init', function () {
- /**
- * Custom post type endpoints
- */
- $types = get_all_post_types();
- foreach($types as $type) {
- $controller = new Make_Endpoint_For($type);
- $controller->register_custom_route($type);
- }
-
- /**
- * Sticky endpoint
- */
- $sticky_controller = new Make_Sticky_Endpoint();
- $sticky_controller->register_custom_route('sticky');
-
- /**
- * OneEach endpoint
- */
- $oneeach_controller = new Make_One_Each_Endpoint();
- $oneeach_controller->register_custom_route('oneofeach');
-
- /**
- * Terms endpoint
- */
- $terms_controller = new Make_Terms_Endpoint();
- $terms_controller->register_custom_route('terms');
-
- /**
- * Search endpoint
- */
- $search_controller = new Make_Search_Endpoint();
- $search_controller->register_custom_route('search');
-
- /**
- * Craft in America custom sort_types
- */
- $artist_sorts = ['by_alpha'];
- $by_alpha_types = ['artist'];
- _make_sorts($by_alpha_types, $artist_sorts);
-
- $date_sorts = ['by_past', 'by_current_and_upcoming'];
- $by_date_types = ['exhibition', 'event'];
- _make_sorts($by_date_types, $date_sorts);
-
- $episode_sorts = ['by_episode'];
- $by_episode_types = [
- 'artist', 'guide', 'short',
- 'technique'
- ];
- _make_sorts($by_episode_types, $episode_sorts);
-
- $material_sorts = ['by_material'];
- $by_material_types = [
- 'artist', 'guide', 'short',
- 'object', 'publication', 'technique'
- ];
- _make_sorts($by_material_types, $material_sorts);
-
- $subtype_sorts = ['by_type'];
- $by_subtype_types = [
- 'artist', 'event', 'post'
- ];
- _make_sorts($by_subtype_types, $subtype_sorts);
- });
-
- /**
- * 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 () {
- $types = get_all_post_types();
- foreach($types as $type) {
- if ( !isset($allowed_endpoints['craft/v2']) || !in_array($type, $allowed_endpoints['craft/v2']) )
- $allowed_endpoints['craft/v2'][] = $type;
- }
- return $allowed_endpoints;
- }, 10, 1);
-
- add_filter('excerpt_length', function ($length) {
- return 30;
- });
|