changing the register route to use class variables and changing class name
authorAnthony Talarico <talarico@gaslightmedia.com>
Tue, 2 Jul 2019 20:40:47 +0000 (16:40 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Tue, 2 Jul 2019 20:40:47 +0000 (16:40 -0400)
setup/routes.php

index aa22013..a7bfe34 100644 (file)
@@ -22,7 +22,7 @@ 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
+class GLMA_Member_Rest_Controller
 {
     /**
      * __construct
@@ -34,7 +34,7 @@ class GLMA_Rest_Api
      */
     public function __construct( $wpdb, $config )
     {
-        $this->namespace     = '/glm/v1/';
+        $this->namespace     = 'glm/v1/';
         $this->resource_name = 'members';
 
         // Setup wpdb and config
@@ -53,8 +53,8 @@ class GLMA_Rest_Api
     {
         // Members Route
         register_rest_route(
-            'glm/v1',   // namespace
-            'members', // route
+            $this->namespace,   // namespace
+            $this->resource_name , // route
             array(      // array of arrays for multiple methods. Add more arrays for different endpoints.
                 array(  // endpoint for getting members
                     'methods'  => WP_REST_Server::READABLE,
@@ -81,6 +81,7 @@ class GLMA_Rest_Api
 
                         // Get List of Members
                         $members = $memberData->getList( $where, 'member_name');
+
                         return rest_ensure_response( array_values($members ) );
                     },
                 )
@@ -89,8 +90,8 @@ class GLMA_Rest_Api
 
         // Member Route
         register_rest_route(
-            'glm/v1',
-            'members' . '/(?P<id>[\d]+)',
+            $this->namespace,
+            $this->namespace  . '/(?P<id>[\d]+)',
             array(
                 array(
                     'methods'  => WP_REST_Server::READABLE,
@@ -100,12 +101,9 @@ class GLMA_Rest_Api
                 )
             )
         );
-
-
     }
 
-    public function get_collection_params()
-    {
+    public function get_collection_params(){
         return array(
             'page' => array(
                 'description'       => 'Current page of the collection',
@@ -127,14 +125,12 @@ class GLMA_Rest_Api
         );
     }
 
-    public function authorization_status_code()
-    {
+    public function authorization_status_code(){
         $status = 401;
 
         if ( is_user_logged_in() ) {
             $status = 403;
         }
-
         return $status;
     }
 
@@ -146,7 +142,7 @@ class GLMA_Rest_Api
  add_action(
      'rest_api_init',
      function(){
-         $controller = new GLMA_Rest_Api( $this->wpdb, $this->config );
+         $controller = new GLMA_Member_Rest_Controller( $this->wpdb, $this->config );
          $controller->register_routes();
      }
  );