From: Anthony Talarico Date: Tue, 2 Jul 2019 15:33:30 +0000 (-0400) Subject: setting up valid actions, model , views and the setup-> routes file X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=HEAD;p=WP-Plugins%2Fglm-member-db-rest-api.git setting up valid actions, model , views and the setup-> routes file --- diff --git a/index.php b/index.php index 5ccd57c..411d483 100644 --- 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 index 0000000..217700c --- /dev/null +++ b/models/admin/routes/index.php @@ -0,0 +1,130 @@ + + * @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 index 0000000..be0e2e6 --- /dev/null +++ b/models/admin/routes/routes.php @@ -0,0 +1,141 @@ + + * @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[\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; + } +} diff --git a/setup/adminMenus.php b/setup/adminMenus.php index 4f011ed..5b59cbe 100644 --- a/setup/adminMenus.php +++ b/setup/adminMenus.php @@ -50,3 +50,32 @@ * */ +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 index 0000000..7c1ef05 --- /dev/null +++ b/setup/routes.php @@ -0,0 +1,153 @@ + + * @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[\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(); + } + ); diff --git a/setup/validActions.php b/setup/validActions.php index cf37119..a3f858f 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -59,9 +59,11 @@ $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 index 0000000..96b8e50 --- /dev/null +++ b/views/admin/routes/index.html @@ -0,0 +1 @@ +
Test
\ No newline at end of file