+<?php
+/**
+ * Gaslight Media Members Database
+ * GLM Members Misc Routes For API
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package glmMembersDatabase
+ * @author Chuck Scott <cscott@gaslightmedia.com>
+ * @license http://www.gaslightmedia.com Gaslightmedia
+ * @release adminHooks.php,v 1.0 2014/10/31 19:31:47 cscott Exp $
+ * @link http://dev.gaslightmedia.com/
+ */
+
+/**
+ * Require the Contact Data Class.
+ */
+require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataMemberInfo.php';
+
+/**
+ * Documentation for Wordpress API and adding custom endpoints.
+ * https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
+ */
+class GLMA_Rest_Api
+{
+ /**
+ * __construct
+ *
+ * Class Constructor
+ *
+ * @param $wpdb Wordpress wpdb Database Object
+ * @param $config GlmAssociate config array
+ */
+ public function __construct( $wpdb, $config )
+ {
+ $this->namespace = '/glm/v1/';
+ $this->resource_name = 'members';
+
+ // Setup wpdb and config
+ $this->wpdb = $wpdb;
+ $this->config = $config;
+ }
+
+ /**
+ * register_routes
+ *
+ * Registers the routes for this class.
+ *
+ * @return void
+ */
+ public function register_routes()
+ {
+ // Members Route
+ register_rest_route(
+ 'glm/v1', // namespace
+ 'members', // route
+ array( // array of arrays for multiple methods. Add more arrays for different endpoints.
+ array( // endpoint for getting members
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => function( $request ) {
+ // Callback function can be a lot shorter.
+ // You could just create a new model to get the results in an array
+ // and use return rest_ensure_response( $data );
+ $memberData = new GlmDataMemberInfo( $this->wpdb, $this->config );
+
+ $where = "
+ (
+ SELECT access
+ FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members
+ WHERE id = T.member
+ ) IN (
+ ".$this->config['access_numb']['NoAccess'].",
+ ".$this->config['access_numb']['Moderated'].",
+ ".$this->config['access_numb']['Full']."
+ )
+ AND T.id < 200
+ AND T.lat <> 0
+ AND T.lon <> 0
+ AND T.status = ".$this->config['status_numb']['Active'];
+
+ // Get List of Members
+ $members = $memberData->getList( $where, 'member_name');
+
+ return rest_ensure_response( array_values($members ) );
+ },
+ )
+ )
+ );
+
+ // Member Route
+ register_rest_route(
+ 'glm/v1',
+ 'members' . '/(?P<id>[\d]+)',
+ array(
+ array(
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => function( $request ){
+ $params = $request->get_params();
+ },
+ )
+ )
+ );
+
+
+ }
+
+ public function get_collection_params()
+ {
+ return array(
+ 'page' => array(
+ 'description' => 'Current page of the collection',
+ 'type' => 'integer',
+ 'default' => 1,
+ 'sanitize_callback' => 'absint',
+ ),
+ 'per_page' => array(
+ 'description' => 'Maximum number of items to be returned in result set.',
+ 'type' => 'integer',
+ 'default' => 10,
+ 'sanitize_callback' => 'absint',
+ ),
+ 'search' => array(
+ 'description' => 'Limit results to those matching a string',
+ 'type' => 'string',
+ 'sanitize_callback' => 'sanitize_text_field',
+ ),
+ );
+ }
+
+ public function authorization_status_code()
+ {
+ $status = 401;
+
+ if ( is_user_logged_in() ) {
+ $status = 403;
+ }
+
+ return $status;
+ }
+
+}
+
+/**
+ * Create Action for the REST API Route.
+ */
+ add_action(
+ 'rest_api_init',
+ function(){
+ $controller = new glmAssoc_Controller_Members( $this->wpdb, $this->config );
+ $controller->register_routes();
+ }
+ );