| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?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-endpoint.php');
- require_once('includes/class.make-sticky.php');
- require_once('includes/class.make-sortby.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');
-
- /**
- * 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'
- ];
- _make_sorts($by_episode_types, $episode_sorts);
-
- $material_sorts = ['by_material'];
- $by_material_types = [
- 'artist', 'guide', 'short',
- 'object', 'publication',
- ];
- _make_sorts($by_material_types, $material_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;
- });
|