From 030ccbc82c0e944a9310dfd0e40e1043c4382d30 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 11 Nov 2014 22:18:03 -0500 Subject: [PATCH] Completed plugin first version. Includes user capability features. --- controllers/admin.php | 34 +++++++++------- index.php | 4 +- lib/smartyTemplateSupport.php | 6 +-- models/activate.php | 40 ++++++++++++++++++- models/admin/add/index.php | 60 +++++++++++++++-------------- models/admin/prototypes/display.php | 5 ++- models/admin/prototypes/edit.php | 60 +++++++++++++++-------------- models/admin/prototypes/index.php | 8 +++- models/deactivate.php | 31 ++++++++++++++- views/admin/add/index.html | 22 +++++------ views/admin/prototypes/display.html | 6 +-- views/admin/prototypes/edit.html | 22 +++++------ views/admin/prototypes/index.html | 17 ++++---- 13 files changed, 200 insertions(+), 115 deletions(-) diff --git a/controllers/admin.php b/controllers/admin.php index ca0deb7..1877d65 100644 --- a/controllers/admin.php +++ b/controllers/admin.php @@ -185,7 +185,7 @@ class glmProtoAdmin // Add a new main menu item for management and display of customer // prototypes. - add_menu_page('Site Prototypes', 'Prototypes', 'manage_options', + add_menu_page('Site Prototypes', 'Prototypes', 'read', 'glm-proto-admin-menu-prototypes', array( $this, @@ -195,12 +195,18 @@ class glmProtoAdmin // Add sub-menu for adding a new prototype under the Prototypes main // menu item. - add_submenu_page('glm-proto-admin-menu-prototypes', 'Site Prototypes', - 'Add', 'manage_options', 'glm-proto-admin-menu-add', - array( - $this, - 'glmProtoAdminMenuAdd' - )); + // See if user is permitted to add, edit, and delete prototypes + $canEdit = current_user_can('glm_proto_edit'); + $user_ID = get_current_user_id(); + if ($canEdit) { + add_submenu_page('glm-proto-admin-menu-prototypes', + 'Site Prototypes', 'Add', 'manage_options', + 'glm-proto-admin-menu-add', + array( + $this, + 'glmProtoAdminMenuAdd' + )); + } } /* @@ -272,7 +278,8 @@ class glmProtoAdmin * * Controller parameters * - * @menuItem string Name of the menu item that is being processed + * @param string $menuItem + * Name of the menu item that is being processed * * @return void * @access public @@ -358,16 +365,15 @@ class glmProtoAdmin $smarty = new smartyTemplateSupport(); // Add some standard parameters - $smarty->templateAssign( - array( - 'request_uri' => $_SERVER['REQUEST_URI'] - )); + $smarty->templateAssign('request_uri', $_SERVER['REQUEST_URI']); // Add data from model to Smarty template $haveData = false; if (is_array($results['data']) && count($results['data']) > 0) { - $haveData = true; - $smarty->templateAssign('data', $results['data']); + foreach ($results['data'] as $k => $d) { + $haveData = true; + $smarty->templateAssign($k, $d); + } } $smarty->templateAssign('haveData', $haveData); diff --git a/index.php b/index.php index 85e9752..ce0cdf7 100644 --- a/index.php +++ b/index.php @@ -209,9 +209,9 @@ function glmProtoPluginDeactivate () { global $wpdb; require_once (GLM_PROTO_PLUGIN_DIR . '/models/deactivate.php'); - new glmProtoPluginDectivate($wpdb); + new glmProtoPluginDeactivate($wpdb); } -register_deactivation_hook(__FILE__, 'glmProtoPluginDeativate'); +register_deactivation_hook(__FILE__, 'glmProtoPluginDeactivate'); // Uninstall function glmProtoPluginUninstall () diff --git a/lib/smartyTemplateSupport.php b/lib/smartyTemplateSupport.php index 39244a3..7325035 100644 --- a/lib/smartyTemplateSupport.php +++ b/lib/smartyTemplateSupport.php @@ -76,12 +76,11 @@ class smartyTemplateSupport * Assign parameters to the template * * This method assigns either one parameter to the template object in this - * class - * or an array of parameters. + * class or an array of parameters. * * Submit an array of parameters to the template. * - * @param array Array of arrays with parameter name (key), value pairs + * @name array Array of arrays with parameter name (key), value pairs * * or * @@ -97,7 +96,6 @@ class smartyTemplateSupport // If this is a single assignment if ($value !== null) { - $this->template->assign($param, $value); // Otherwise it's an array of parameter/value pairs diff --git a/models/activate.php b/models/activate.php index 4d6b5cd..2141999 100644 --- a/models/activate.php +++ b/models/activate.php @@ -17,6 +17,9 @@ /* * This class performs all necessary additional work when this * plugin is activated. + * + * Currently the only actions are to add role capability to display and modify + * prototypes. */ class glmProtoPluginActivate { @@ -45,7 +48,42 @@ class glmProtoPluginActivate // Save WordPress Database object $this->wpdb = $wpdb; - // Perform any activation tasks here + /* + * Perform any activation tasks here + */ + + // Add user capabilties + $this->addRoleCapability('glm_proto_edit', false); + } + + /* + * Add a role capability to all current roles + * + * @param string $capability Name of capability to add + * @param string $default Whether capability should be on by default + * + * @return void + * @access public + */ + public function addRoleCapability ($capability, $default) + { + // Get list of role objects + $roleObjects = $GLOBALS['wp_roles']->role_objects; + + // Get list of roles we can edit + $roles = get_editable_roles(); + + // For each role object + foreach ($roleObjects as $key => $role) { + + // Check if the role exists in list of editable roles and capability + // does not exist + if (isset($roles[$key]) && ! isset($role->capabilities[$capability])) { + + // Add the role + $role->add_cap($capability, $default); + } + } } } diff --git a/models/admin/add/index.php b/models/admin/add/index.php index 6bc1ae7..1c6bac8 100644 --- a/models/admin/add/index.php +++ b/models/admin/add/index.php @@ -90,7 +90,7 @@ class glmProtoAdmin_add_index */ public function modelAction () { - $data = array( + $content = array( 'glm_proto_title' => '', 'glm_proto_title_error' => '', 'glm_proto_width' => '1500', @@ -111,51 +111,51 @@ class glmProtoAdmin_add_index if (isset($_REQUEST['glm_proto_title'])) { // Clean up all input - $data['glm_proto_title'] = sanitize_text_field( + $content['glm_proto_title'] = sanitize_text_field( $_REQUEST['glm_proto_title']); - $data['glm_proto_width'] = sanitize_text_field( + $content['glm_proto_width'] = sanitize_text_field( $_REQUEST['glm_proto_width']); - $data['glm_proto_height'] = sanitize_text_field( + $content['glm_proto_height'] = sanitize_text_field( $_REQUEST['glm_proto_height']); - $data['glm_proto_background'] = sanitize_text_field( + $content['glm_proto_background'] = sanitize_text_field( $_REQUEST['glm_proto_background']); - $data['glm_proto_prototype'] = sanitize_text_field( + $content['glm_proto_prototype'] = sanitize_text_field( $_REQUEST['glm_proto_prototype']); // Check title field - if ($data['glm_proto_title'] == '') { - $data['glm_proto_title_error'] = 'Required title not supplied'; + if ($content['glm_proto_title'] == '') { + $content['glm_proto_title_error'] = 'Required title not supplied'; $submitError = true; } // Check width field - if ($data['glm_proto_width'] == '') { - $data['glm_proto_width_error'] = 'Required width not supplied'; + if ($content['glm_proto_width'] == '') { + $content['glm_proto_width_error'] = 'Required width not supplied'; $submitError = true; } // Check height field - if ($data['glm_proto_height'] == '') { - $data['glm_proto_height_error'] = 'Required height not supplied'; + if ($content['glm_proto_height'] == '') { + $content['glm_proto_height_error'] = 'Required height not supplied'; $submitError = true; } // Check background image - Not required but must exist if provided - if ($data['glm_proto_background'] != '' && ! $this->glmProtoIsUploaded( - $data['glm_proto_background'])) { - $data['glm_proto_background_error'] = 'Supplied background image does not exists'; + if ($content['glm_proto_background'] != '' && ! $this->glmProtoIsUploaded( + $content['glm_proto_background'])) { + $content['glm_proto_background_error'] = 'Supplied background image does not exists'; $submitError = true; } // Check prototype image - if ($data['glm_proto_prototype'] == '') { - $data['glm_proto_prototype_error'] = 'Required prototype image not supplied '; + if ($content['glm_proto_prototype'] == '') { + $content['glm_proto_prototype_error'] = 'Required prototype image not supplied '; $submitError = true; } else { $exists = $this->glmProtoIsUploaded( - $data['glm_proto_prototype']); + $content['glm_proto_prototype']); if (! $exists) { - $data['glm_proto_prototype_error'] .= 'Supplied prototype image does not exists'; + $content['glm_proto_prototype_error'] .= 'Supplied prototype image does not exists'; $submitError = true; } } @@ -169,13 +169,13 @@ class glmProtoAdmin_add_index $gmtDate = date('Y-m-d', time()); date_default_timezone_set($timezoneBackup); - $content = serialize( + $contentSerialized = serialize( array( - 'title' => $data['glm_proto_title'], - 'width' => $data['glm_proto_width'], - 'height' => $data['glm_proto_height'], - 'background' => $data['glm_proto_background'], - 'prototype' => $data['glm_proto_prototype'] + 'title' => $content['glm_proto_title'], + 'width' => $content['glm_proto_width'], + 'height' => $content['glm_proto_height'], + 'background' => $content['glm_proto_background'], + 'prototype' => $content['glm_proto_prototype'] )); // Store into wp_posts table @@ -183,15 +183,15 @@ class glmProtoAdmin_add_index array( 'post_date' => $date, 'post_date_gmt' => $gmtDate, - 'post_content' => $content, - 'post_title' => $data['glm_proto_title'], + 'post_content' => $contentSerialized, + 'post_title' => $content['glm_proto_title'], 'post_type' => 'glm_proto' )); // If there was a problem storing the prototype, pass that to // the template if (! $result) { - $data['glm_proto_title_error'] = 'There was an unknown problem storing this prototype.'; + $content['glm_proto_title_error'] = 'There was an unknown problem storing this prototype.'; } $prototypeSubmitted = true; @@ -218,7 +218,9 @@ class glmProtoAdmin_add_index 'menuItemRedirect' => false, 'modelRedirect' => false, 'view' => $view, - 'data' => $data + 'data' => array( + 'content' => $content + ) ); } diff --git a/models/admin/prototypes/display.php b/models/admin/prototypes/display.php index 49d59e1..2945d69 100644 --- a/models/admin/prototypes/display.php +++ b/models/admin/prototypes/display.php @@ -107,7 +107,6 @@ class glmProtoAdmin_prototypes_display $d = unserialize($res['post_content']); $res['content'] = $d; } - } // Return status, any suggested view, and any data to controller @@ -116,7 +115,9 @@ class glmProtoAdmin_prototypes_display 'menuItemRedirect' => false, 'modelRedirect' => false, 'view' => 'admin/prototypes/display.html', - 'data' => $res['content'] + 'data' => array( + 'content' => $res['content'] + ) ); } } diff --git a/models/admin/prototypes/edit.php b/models/admin/prototypes/edit.php index 39f5533..f8fecf9 100644 --- a/models/admin/prototypes/edit.php +++ b/models/admin/prototypes/edit.php @@ -117,7 +117,7 @@ class glmProtoAdmin_prototypes_edit } // Setup input data array - $data = array( + $content = array( 'glm_proto_title' => $content['title'], 'glm_proto_title_error' => '', 'glm_proto_width' => $content['width'], @@ -138,51 +138,51 @@ class glmProtoAdmin_prototypes_edit if (isset($_REQUEST['glm_proto_title'])) { // Clean up all input - $data['glm_proto_title'] = sanitize_text_field( + $content['glm_proto_title'] = sanitize_text_field( $_REQUEST['glm_proto_title']); - $data['glm_proto_width'] = sanitize_text_field( + $content['glm_proto_width'] = sanitize_text_field( $_REQUEST['glm_proto_width']); - $data['glm_proto_height'] = sanitize_text_field( + $content['glm_proto_height'] = sanitize_text_field( $_REQUEST['glm_proto_height']); - $data['glm_proto_background'] = sanitize_text_field( + $content['glm_proto_background'] = sanitize_text_field( $_REQUEST['glm_proto_background']); - $data['glm_proto_prototype'] = sanitize_text_field( + $content['glm_proto_prototype'] = sanitize_text_field( $_REQUEST['glm_proto_prototype']); // Check title field - if ($data['glm_proto_title'] == '') { - $data['glm_proto_title_error'] = 'Required title not supplied'; + if ($content['glm_proto_title'] == '') { + $content['glm_proto_title_error'] = 'Required title not supplied'; $submitError = true; } // Check width field - if ($data['glm_proto_width'] == '') { - $data['glm_proto_width_error'] = 'Required width not supplied'; + if ($content['glm_proto_width'] == '') { + $content['glm_proto_width_error'] = 'Required width not supplied'; $submitError = true; } // Check height field - if ($data['glm_proto_height'] == '') { - $data['glm_proto_height_error'] = 'Required height not supplied'; + if ($content['glm_proto_height'] == '') { + $content['glm_proto_height_error'] = 'Required height not supplied'; $submitError = true; } // Check background image - Not required but must exist if provided - if ($data['glm_proto_background'] != '' && ! $this->glmProtoIsUploaded( - $data['glm_proto_background'])) { - $data['glm_proto_background_error'] = 'Supplied background image does not exists'; + if ($content['glm_proto_background'] != '' && ! $this->glmProtoIsUploaded( + $content['glm_proto_background'])) { + $content['glm_proto_background_error'] = 'Supplied background image does not exists'; $submitError = true; } // Check prototype image - if ($data['glm_proto_prototype'] == '') { - $data['glm_proto_prototype_error'] = 'Required prototype image not supplied '; + if ($content['glm_proto_prototype'] == '') { + $content['glm_proto_prototype_error'] = 'Required prototype image not supplied '; $submitError = true; } else { $exists = $this->glmProtoIsUploaded( - $data['glm_proto_prototype']); + $content['glm_proto_prototype']); if (! $exists) { - $data['glm_proto_prototype_error'] .= 'Supplied prototype image does not exists'; + $content['glm_proto_prototype_error'] .= 'Supplied prototype image does not exists'; $submitError = true; } } @@ -196,13 +196,13 @@ class glmProtoAdmin_prototypes_edit $gmtDate = date('Y-m-d', time()); date_default_timezone_set($timezoneBackup); - $content = serialize( + $contentSerialized = serialize( array( - 'title' => $data['glm_proto_title'], - 'width' => $data['glm_proto_width'], - 'height' => $data['glm_proto_height'], - 'background' => $data['glm_proto_background'], - 'prototype' => $data['glm_proto_prototype'] + 'title' => $content['glm_proto_title'], + 'width' => $content['glm_proto_width'], + 'height' => $content['glm_proto_height'], + 'background' => $content['glm_proto_background'], + 'prototype' => $content['glm_proto_prototype'] )); // Store into wp_posts table @@ -211,8 +211,8 @@ class glmProtoAdmin_prototypes_edit array( 'post_date' => $date, 'post_date_gmt' => $gmtDate, - 'post_content' => $content, - 'post_title' => $data['glm_proto_title'], + 'post_content' => $contentSerialized, + 'post_title' => $content['glm_proto_title'], 'post_type' => 'glm_proto' ), array( @@ -222,7 +222,7 @@ class glmProtoAdmin_prototypes_edit // If there was a problem storing the prototype, pass that to // the template if (! $result) { - $data['glm_proto_title_error'] = 'There was an unknown problem updating this prototype.'; + $content['glm_proto_title_error'] = 'There was an unknown problem updating this prototype.'; } $prototypeSubmitted = true; @@ -249,7 +249,9 @@ class glmProtoAdmin_prototypes_edit 'menuItemRedirect' => false, 'modelRedirect' => false, 'view' => $view, - 'data' => $data + 'data' => array( + 'content' => $content + ) ); } diff --git a/models/admin/prototypes/index.php b/models/admin/prototypes/index.php index 8c8bc71..43738af 100644 --- a/models/admin/prototypes/index.php +++ b/models/admin/prototypes/index.php @@ -102,13 +102,19 @@ class glmProtoAdmin_prototypes_index } } + // Determine if current user can add, edit, delete prototypes + $canEdit = current_user_can('glm_proto_edit'); + // Return status, suggested view, and data to controller return array( 'status' => $success, 'menuItemRedirect' => false, 'modelRedirect' => false, 'view' => 'admin/prototypes/index.html', - 'data' => $list + 'data' => array( + 'prototypes' => $list, + 'canEdit' => $canEdit + ) ); } diff --git a/models/deactivate.php b/models/deactivate.php index 7022c29..9555394 100644 --- a/models/deactivate.php +++ b/models/deactivate.php @@ -45,7 +45,36 @@ class glmProtoPluginDeactivate // Save WordPress Database object $this->wpdb = $wpdb; - // Perform any deactivation tasks here + // Add user capabilties + $this->removeRoleCapability('glm_proto_edit'); + } + + /* + * Remove a role capability from all current roles + * + * @param string $capability + * + * @return void + * @access public + */ + public function removeRoleCapability ($capability) + { + // Get list of role objects + $roleObjects = $GLOBALS['wp_roles']->role_objects; + + // Get list of roles we can edit + $roles = get_editable_roles(); + + // For each role object + foreach ($roleObjects as $key => $role) { + // Check if the role exists in list of editable roles and capability + // does not exist + if (isset($roles[$key]) && isset($role->capabilities[$capability])) { + + // Remove role + $role->remove_cap($capability); + } + } } } diff --git a/views/admin/add/index.html b/views/admin/add/index.html index f7270a4..713453d 100644 --- a/views/admin/add/index.html +++ b/views/admin/add/index.html @@ -2,44 +2,44 @@

Add a New Prototype

-
+ diff --git a/views/admin/prototypes/display.html b/views/admin/prototypes/display.html index 8c27573..7689f9e 100644 --- a/views/admin/prototypes/display.html +++ b/views/admin/prototypes/display.html @@ -1,11 +1,11 @@ - Prototype: {$data.title} + Prototype: {$content.title} -
-
+
+
diff --git a/views/admin/prototypes/edit.html b/views/admin/prototypes/edit.html index 79739ed..5d627b7 100644 --- a/views/admin/prototypes/edit.html +++ b/views/admin/prototypes/edit.html @@ -2,44 +2,44 @@

Edit a Prototype

- +
- - {if $data.glm_proto_title_error != ''}
{$data.glm_proto_title_error}{/if} + + {if $content.glm_proto_title_error != ''}
{$content.glm_proto_title_error}{/if}
- - {if $data.glm_proto_width_error != ''}
{$data.glm_proto_width_error}{/if} + + {if $content.glm_proto_width_error != ''}
{$content.glm_proto_width_error}{/if}
- - {if $data.glm_proto_height_error != ''}
{$data.glm_proto_height_error}{/if} + + {if $content.glm_proto_height_error != ''}
{$content.glm_proto_height_error}{/if}
- + - {if $data.glm_proto_background_error != ''}
{$data.glm_proto_background_error}{/if} + {if $content.glm_proto_background_error != ''}
{$content.glm_proto_background_error}{/if}
- + - {if $data.glm_proto_prototype_error != ''}
{$data.glm_proto_prototype_error}{/if} + {if $content.glm_proto_prototype_error != ''}
{$content.glm_proto_prototype_error}{/if}
diff --git a/views/admin/prototypes/index.html b/views/admin/prototypes/index.html index 26d81dc..6afe6dc 100644 --- a/views/admin/prototypes/index.html +++ b/views/admin/prototypes/index.html @@ -19,17 +19,20 @@ + {if $haveData} - {foreach $data as $d} + {foreach $prototypes as $p} - - + + {/foreach} -- 2.17.1
- - {if $data.glm_proto_title_error != ''}
{$data.glm_proto_title_error}{/if} + + {if $content.glm_proto_title_error != ''}
{$content.glm_proto_title_error}{/if}
- - {if $data.glm_proto_width_error != ''}
{$data.glm_proto_width_error}{/if} + + {if $content.glm_proto_width_error != ''}
{$content.glm_proto_width_error}{/if}
- - {if $data.glm_proto_height_error != ''}
{$data.glm_proto_height_error}{/if} + + {if $content.glm_proto_height_error != ''}
{$content.glm_proto_height_error}{/if}
- + - {if $data.glm_proto_background_error != ''}
{$data.glm_proto_background_error}{/if} + {if $content.glm_proto_background_error != ''}
{$content.glm_proto_background_error}{/if}
- + - {if $data.glm_proto_prototype_error != ''}
{$data.glm_proto_prototype_error}{/if} + {if $content.glm_proto_prototype_error != ''}
{$content.glm_proto_prototype_error}{/if}
{$d.p_date}{$d.post_title}{$p.p_date}{$p.post_title} - View - Edit - Delete + {if $canEdit} + Edit + Delete + {/if}