setting up valid actions, model , views and the setup-> routes file master
authorAnthony Talarico <talarico@gaslightmedia.com>
Tue, 2 Jul 2019 15:33:30 +0000 (11:33 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Tue, 2 Jul 2019 15:33:30 +0000 (11:33 -0400)
index.php
models/admin/routes/index.php [new file with mode: 0644]
models/admin/routes/routes.php [new file with mode: 0644]
setup/adminMenus.php
setup/routes.php [new file with mode: 0644]
setup/validActions.php
views/admin/routes/index.html [new file with mode: 0644]

index 5ccd57c..411d483 100644 (file)
--- a/index.php
+++ b/index.php
@@ -240,5 +240,4 @@ ${GLM_MEMBERS_REST_API_PLUGIN_PREFIX."updateChecker"}->declareCredentials(array(
 /*
  * Hooks for testing capabilities provided by this add-on
  */
-require_once GLM_MEMBERS_REST_API_PLUGIN_SETUP_PATH.'/permissions.php';
-
+require_once GLM_MEMBERS_REST_API_PLUGIN_SETUP_PATH.'/permissions.php';
\ No newline at end of file
diff --git a/models/admin/routes/index.php b/models/admin/routes/index.php
new file mode 100644 (file)
index 0000000..217700c
--- /dev/null
@@ -0,0 +1,130 @@
+<?php
+
+/**
+ * Gaslight Media Members Database
+ * Admin Members Dashboard
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Steve Sutton <steve@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @version  0.1
+ */
+
+require_once 'routes.php';
+
+/**
+ * Dashboard Class Model
+ *
+ * Each Add-On can have one or more dashboards.
+ */
+
+class GlmMembersadmin_routes_index
+{
+    /**
+     * Word Press Database Object
+     *
+     * @var $wpdb
+     * @access public
+     */
+    public $wpdb;
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+
+    /**
+     * Constructor
+     *
+     * This contructor sets up this model. At this time that only includes
+     * storing away the WordPress data object.
+     *
+     * @return object Class object
+     *
+     */
+    public function __construct ($wpdb, $config)
+    {
+
+        // Save WordPress Database object
+        $this->wpdb = $wpdb;
+
+        // Save plugin configuration object
+        $this->config = $config;
+
+        // Run constructor for members data class
+        // parent::__construct(false, false);
+
+    }
+
+    /**
+     * Perform Model Action
+     *
+     * This method does the work for this model and returns any resulting data
+     *
+     * @return array Status and data array
+     *
+     * 'status'
+     *
+     * True if successful and false if there was a fatal failure.
+     *
+     * 'menuItemRedirect'
+     *
+     * If not false, provides a menu item the controller should
+     * execute after this one. Normally if this is used, there would also be a
+     * modelRedirect value supplied as well.
+     *
+     * 'modelRedirect'
+     *
+     * If not false, provides an action the controller should execute after
+     * this one.
+     *
+     * 'view'
+     *
+     * A suggested view name that the controller should use instead of the
+     * default view for this model or false to indicate that the default view
+     * should be used.
+     *
+     * 'data'
+     *
+     * Data that the model is returning for use in merging with the view to
+     * produce output.
+     *
+     */
+    public function modelAction ( $actionData = false )
+    {
+      
+        /**
+         * 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();
+        //     }
+        // );
+
+
+
+        // Compile template data.
+        $templateData = array(
+
+        );
+
+        // Return status, suggested view, and data to controller.
+        return array(
+            'status'           => $success,
+            'menuItemRedirect' => false,
+            'modelRedirect'    => false,
+            'view'             => 'admin/routes/index.html',
+            'data'             => $templateData
+        );
+
+    }
+
+}
diff --git a/models/admin/routes/routes.php b/models/admin/routes/routes.php
new file mode 100644 (file)
index 0000000..be0e2e6
--- /dev/null
@@ -0,0 +1,141 @@
+<?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 glmAssoc_Controller_Members
+{
+    /**
+     * __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;
+    }
+}
index 4f011ed..5b59cbe 100644 (file)
  *
  */
 
+if (isset($this->config['loggedInUser']) && isset($this->config['loggedInUser']['contactUser']) && $this->config['loggedInUser']['contactUser']) {
+
+    // check the settings to see if members are allow to manage events
+    $memberRestApiAllowed = $this->config['settings']['member_rest_api_allowed'];
+    if ( $memberRestApiAllowed ) {
+        add_submenu_page(
+            $mainMenuSlug,
+            'REST API',
+            'REST API',
+            'glm_members_edit_my_entity',
+            'glm-members-admin-menu-rest-api-index',
+            function() {$this->controller('routes');}
+        );
+    }
+
+// Otherwise it's an administrator
+} else {
+
+    add_submenu_page(
+        'glm-members-admin-menu-members',
+        'REST API',
+        'REST API',
+        'glm_members_members',
+        'glm-members-admin-menu-rest-api-index',
+        function() {$this->controller('routes');}
+    );
+
+}
\ No newline at end of file
diff --git a/setup/routes.php b/setup/routes.php
new file mode 100644 (file)
index 0000000..7c1ef05
--- /dev/null
@@ -0,0 +1,153 @@
+<?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();
+     }
+ );
index cf37119..a3f858f 100644 (file)
 
 $glmMembersRestApiAddOnValidActions = array(
     'adminActions' => array(
+        'routes' => array(
+            'index' => GLM_MEMBERS_REST_API_PLUGIN_SLUG,
+        )
     ),
     'frontActions' => array(
     )
 );
-
 ?>
\ No newline at end of file
diff --git a/views/admin/routes/index.html b/views/admin/routes/index.html
new file mode 100644 (file)
index 0000000..96b8e50
--- /dev/null
@@ -0,0 +1 @@
+<div>Test</div>
\ No newline at end of file