*/ // 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); } add_action( 'rest_api_init', function () { /** * Custom 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_material', 'by_episode']; foreach ($artist_sorts as $sort) { $unsnaked = _unsnake($sort); $sort_controller = new Make_Sort_By('artist', $sort); $sort_controller->register_custom_route("artist/$unsnaked"); } $date_sorts = ['by_past', 'by_current_and_upcoming']; $date_types = ['exhibition', 'event']; foreach ($date_sorts as $sort) { $unsnaked = _unsnake($sort); foreach ($date_types as $date_type) { $sort_controller = new Make_Sort_By($date_type, $sort); $sort_controller->register_custom_route("$date_type/$unsnaked"); } } $episode_sorts = ['by_episode']; $episode_types = ['artist','exhibition', 'event']; foreach ($episode_sorts as $sort) { $unsnaked = _unsnake($sort); foreach ($episode_types as $episode_type) { $sort_controller = new Make_Sort_By($episode_type, $sort); $sort_controller->register_custom_route("$episode_type/$unsnaked"); } } }); /** * Register the /wp-json/craft/v2/ 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; });