Adding route for members
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 6 May 2019 20:14:10 +0000 (16:14 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 6 May 2019 20:14:10 +0000 (16:14 -0400)
Members list json API

setup/routes.php

index a40f325..2865ffa 100644 (file)
 /**
  * Require the Contact Data Class.
  */
-require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/settings/dataSettingsGeneral.php';
+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 glmAssoc_Controller_Contacts
+class glmAssoc_Controller_Members
 {
     /**
      * __construct
@@ -35,7 +35,7 @@ class glmAssoc_Controller_Contacts
     public function __construct( $wpdb, $config )
     {
         $this->namespace     = '/glm/v1/';
-        $this->resource_name = 'settings';
+        $this->resource_name = 'members';
 
         // Setup wpdb and config
         $this->wpdb   = $wpdb;
@@ -51,34 +51,78 @@ class glmAssoc_Controller_Contacts
      */
     public function register_routes()
     {
+        // Members Route
         register_rest_route(
             'glm/v1',   // namespace
-            'settings', // route
+            'members', // route
             array(      // array of arrays for multiple methods. Add more arrays for different endpoints.
-                array(  // endpoint for getting settings
+                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 );
-                        $settingsData = new GlmDataSettingsGeneral( $this->wpdb, $this->config );
+                        $memberData = new GlmDataMemberInfo( $this->wpdb, $this->config );
 
-                        $settingsData->groupSelect = array( 'misc' );
-                        // $settingsData->selectGroupFields();
-                        $settings = $settingsData->getEntry( 1 );
-                        // $settingsData->restoreFields();
+                        $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.status = ".$this->config['status_numb']['Active'];
 
-                        return rest_ensure_response( $settings );
+                        // Get List of Members
+                        $members = $memberData->getList( $where, 'member_name');
+
+                        return rest_ensure_response( $members );
                     },
-                    'permission_callback' => function(){ // can require capability for this rest endpoint.
-                        if ( ! current_user_can( 'glm_members_members' ) ) {
-                            return new WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the post resource.' ), array( 'status' => $this->authorization_status_code() ) );
-                        }
-                        return true;
+                )
+            )
+        );
+
+        // 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()
@@ -100,7 +144,7 @@ class glmAssoc_Controller_Contacts
 add_action(
     'rest_api_init',
     function(){
-        $controller = new glmAssoc_Controller_Contacts( $this->wpdb, $this->config );
+        $controller = new glmAssoc_Controller_Members( $this->wpdb, $this->config );
         $controller->register_routes();
     }
 );