Created
July 26, 2016 01:22
-
-
Save joelstransky/40308a1aeceb801a2ebb2a84539a07ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: DCDC Docs Endpoint | |
Description: Exposes the docs/v1 namespace and search endpoint for use by other VPA sites in the network | |
Version: 0.1 | |
Author: Joel Stransky | |
Author URI: https://dcdc.coe.hawaii.edu/ | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'DCDC_Docs_Endpoint' ) ) { | |
class DCDC_Docs_Endpoint { | |
public static function init() { | |
self::includes(); | |
self::hooks(); | |
} | |
private static function includes() { | |
if ( class_exists( 'WP_REST_Controller' ) ) { | |
require_once dirname( __FILE__ ) . '/lib/endpoints/class-wp-rest-dcdc-docs-endpoint-controller.php'; | |
} | |
} | |
private static function hooks() { | |
/** | |
* Add REST API support to an already registered post type. | |
*/ | |
add_action( 'init', array( __CLASS__, 'set_documents_rest_controller'), 25 ); | |
if ( class_exists( 'WP_REST_Controller' ) ) { | |
add_action( 'rest_api_init', array( __CLASS__, 'create_rest_routes'), 10 ); | |
} else { | |
add_action( 'admin_notices', array( __CLASS__, 'missing_notice' ) ); | |
} | |
// inspect acf-to-rest-api | |
add_filter( 'acf/rest_api/type', function( $type ) { | |
// write_log(array('acf rest type', $type)); | |
return $type; | |
} ); | |
if (class_exists( 'acf' ) && class_exists( 'ACF_To_REST_API' ) ) { | |
add_filter( 'acf/rest_api/document_cpt/get_fields', function($data, $request, $response, $object) { | |
// write_log( array('acf rest get fields', $data, $request, $response, $object ) ); | |
// include list of document_type terms | |
$terms = get_the_terms( $object->ID, 'document_type'); | |
foreach ($terms as $term) { | |
$data->data['acf']['document_type_terms'][] = $term; | |
} | |
// include list of document_tags terms | |
$terms = get_the_terms( $object->ID, 'document_tags'); | |
foreach ($terms as $term) { | |
$data->data['acf']['document_tags_terms'][] = $term; | |
} | |
return $data; | |
}, 10, 4); | |
} | |
} | |
public static function set_documents_rest_controller() { | |
global $wp_post_types; | |
$post_type_name = 'document_cpt'; | |
if( isset( $wp_post_types[ $post_type_name ] ) ) { | |
$wp_post_types[$post_type_name]->show_in_rest = true; | |
$wp_post_types[$post_type_name]->rest_base = $post_type_name; | |
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_DCDC_Docs_Endpoint_Controller'; | |
} | |
} | |
public static function create_rest_routes() { | |
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' ); | |
if( isset( $types['document_cpt'] ) ) { | |
$controller = new WP_REST_DCDC_Docs_Endpoint_Controller( 'document_cpt' ); | |
$controller->register_routes(); | |
} | |
} | |
public static function missing_notice() { | |
?> | |
<div class="notice error is-dismissible"> | |
<p><strong>DCDC Docs Endpoint</strong> Requires WordPress REST API, ACF and ACF to REST API plugins.</p> | |
</div> | |
<?php | |
} | |
} | |
add_action( 'plugins_loaded', array( 'DCDC_Docs_Endpoint', 'init' ) ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WP_REST_DCDC_Docs_Endpoint_Controller' ) ) { | |
class WP_REST_DCDC_Docs_Endpoint_Controller extends WP_REST_Posts_Controller { | |
public function __construct() { | |
$this->post_type = 'document_cpt'; | |
$this->namespace = 'docs/v1'; | |
$this->rest_base = 's'; | |
} | |
public function register_routes() { | |
register_rest_route( $this->namespace, '/' . $this->rest_base, array( | |
array( | |
'methods' => WP_REST_Server::READABLE, | |
'callback' => array( $this, 'get_items' ), | |
// 'permission_callback' => array( $this, 'get_items_permissions_check' ), | |
// 'args' => $this->get_collection_params(), | |
), | |
// 'schema' => array( $this, 'get_public_item_schema' ), | |
) ); | |
} | |
public function get_items( $request ) { | |
write_log(array('get items', $request['search'])); | |
$args = array( | |
'is_doc_query' => true, | |
's' => isset( $request['search'] ) ? $request['search'] : "", | |
'post_type' => array( 'document_cpt' ) | |
); | |
// add doctype filters as a property of the WP_Query object | |
if ( isset( $request['filter']['doctypes'] ) && $request['filter']['doctypes'] != "" ) { | |
$args['doctypes'] = explode(",", $request['filter']['doctypes'] ); | |
} | |
$doc_query = new WP_Query(); | |
$query_result = $doc_query->query( $args ); | |
$posts = array(); | |
foreach ( $query_result as $post ) { | |
// if ( ! $this->check_read_permission( $post ) ) { | |
// continue; | |
// } | |
$data = $this->prepare_item_for_response( $post, $request ); | |
$posts[] = $this->prepare_response_for_collection( $data ); | |
} | |
return new WP_REST_Response( $posts, 200 ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment