*/ // 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-terms.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'); /** * Terms endpoint */ $terms_controller = new Make_Terms_Endpoint(); $terms_controller->register_custom_route('terms'); /** * 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/ 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; });