Skip to content

Commit

Permalink
Updates to the permission
Browse files Browse the repository at this point in the history
  • Loading branch information
walisc committed Jan 13, 2023
1 parent 3521a99 commit 206c373
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/wpOOW/Core/Permissions/WP_PERMISSIONS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace wpOOW\Core\Permissions;

/**
* Class WP_PERMISSIONS
*
* wrapper for the main wordpress permissions
* @package wpAPI\Base
*/
class WP_PERMISSIONS
{

CONST MANAGE_OPTIONS = "manage_options";
}

147 changes: 147 additions & 0 deletions src/wpOOW/Core/Permissions/wpAPIPermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace wpOOW\Core\Permissions;

/**
* Class wpAPIPermissions
*
* Permission class used for the wpAPI_VIEW.
* wpAPI Elements can set of permissions which will be linked to the wpAPI_VIEW permissions (viewstate) for a page.
* Based on this, elements can either have create, read, update delete rights.
*
* For the wpAPI there are 5 viewstates that a wpAPI page can have. The view of a page can set using by setting the
* global $CURRENT_VIEW_STATE when the page is created. By default this permission/viewsate is @EditPage. These
* viewstates are detailed below. It must be noted that these can be used arbitrarily. The names are more suggestive
* as to when to use them
*
* ViewPage - Can be use for a custom page when it is in read only mode. Not always usually useful for wordpress
* as most pages are not readonly
* AddPage - Can be used on a custom page when adding elements. In this case, not always useful in wordpress.
* - When you use the wpAPI post type this is set when you add a new post
* EditPage - This is default view state set for all pages. Can be used on a custom page when editing elements.
* - When you use the wpAPI post type this is set when you editing a new post
* ViewTable - Can be used on a custom page when you have a table/grid layout
* - When you use the wpAPI post type this is set when you are view all your post types
* EditTable - Can be used on a custom page when you have a edited inline table/grid layout
* - TODO: enable on wordpress inline editing
*
* For each of these viewstate and element can either create, read and update permissions set. By default all viewstates
* for an element are set to create, read, update. i.e
*
* element.WP_PERMISSIONS = [
wpAPIPermissions::ViewPage => "cru",
wpAPIPermissions::AddPage => "cru",
wpAPIPermissions::EditPage => "cru",
wpAPIPermissions::EditTable => "cru",
wpAPIPermissions::ViewTable => "cru",
];
*
* when a page is rendered the permission of each wpAPI element is is computed dependant on the page viewstate. The
* element is
* - presented if read permissions are true.
* - editable if update permissions are true and the viewstate matches.
* - editable if create permissions are true and the viewstate matches.
*
* @package wpAPI\Base
*/


class wpAPIPermissions
{

const ViewPage = "ViewPage";
const AddPage = "AddPage";
const EditPage = "EditPage";
const ViewTable = "ViewTable";
const EditTable = "EditTable";

// cru - create - read - update
/**
* @var array
*/
private $permissionMatrix = [
wpAPIPermissions::ViewPage => "cru",
wpAPIPermissions::AddPage => "cru",
wpAPIPermissions::EditPage => "cru",
wpAPIPermissions::EditTable => "cru",
wpAPIPermissions::ViewTable => "cru",

];

/**
* An array with viewstate -> permission relations to be set for the element such as the field. Example Below
*
* [
* "EditTable" = > "cr"
* "EditPage" = > "cru"
* "ViewTable" = > "cru"
* "ViewPage" = > "cru"
* ]
*
* Note:- for viewstates you can use the const i.e wpAPIPermissions::EditTable => "cru"
* Also note by default all viewstates have the permission cru.
* because of this you don't have to specify all viewstates. Only the one you want
*
*
* @param array $permissions
* @return wpAPIPermissions
* @throws Exception
*/
public static function SetPermission($permissions = [])
{
if (!is_array($permissions)) {
throw new Exception("Permission should be an array with the 4 view states EditTable, EditPage, ViewTable, ViewPage");
}
$wP = new wpAPIPermissions();


foreach ($permissions as $pageState => $permission) {
$wP->permissionMatrix[$pageState] = $permission;
}
return $wP;
}


/**
* Get the permission of a pagestate
*
* @param $pageState
* @return mixed
*/
public function GetPermission($pageState)
{
//TODO: consider returning an object
return $this->permissionMatrix[$pageState];
}

//TODO: Rename this to check permission
/**
* See is for the given page state an action is allow. Action can either be u(pdate), or r(ead), v(iew)
*
* @param $pageState
* @param $action
* @return bool|int
*/
public function CheckPermissionAction($pageState, $action)
{
return strpos($this->permissionMatrix[$pageState], $action);
}

public function CanEdit($pageState)
{
return strpos($this->permissionMatrix[$pageState], 'u');
}

public function CanRead($pageState)
{
return strpos($this->permissionMatrix[$pageState], 'r');
}

public function CanCreate($pageState)
{
return strpos($this->permissionMatrix[$pageState], 'c');
}


}
126 changes: 126 additions & 0 deletions src/wpOOW/Core/wpAPIView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace wpOOW\Core;
/**
*
* Class responsible for rendering wp-admin menu pages. This uses the php template engine twig.
* A wpAPIVIEW can either be linked to a twig template or be based on a template string
*
* Class wpAPIVIEW
* @package wpAPI\Base
*/
class wpAPIView
{
/**
* Constant that lets wpAPIVIEW know that it should render the page based on a twig template file
*/
CONST PATH = 1;
/**
* Constant that lets wpAPIVIEW know that it should render the page based on a template string
*/
CONST CONTENT = 2;

/**
* Either wpAPIVIEW::PATH or wpAPIVIEW::CONTENT
* @var
*/
private $type;

/**
* When wpAPIVIEW::PATH is used for type this links to the path of the twig template
* When wpAPIVIEW::CONTENT is used for type this is used as a string template for rendering the page
* @var
*/

private $path_content;

/**
*
* key, value array to use in the twig template/string
*
* @var array
*/
private $data = [];

/**
*
* base path to use when finding templates. If not specified use ABSPATH
*
* @var array
*/
private $base_path = [];


/**
* wpAPIVIEW constructor.
* @param $type
* @param $path_content
* @param $data
* @param $base_path
*/
function __construct($type, $path_content, $data=[], $base_path=null)
{
$this->type = $type;
$this->path_content = $path_content;
$this->data = array_merge($this->data, $data);
$this->base_path = $base_path != null ? $base_path : ABSPATH;

}

/**
*
* Method that renders the twig template
*
*/
function Render()
{

if ($this->type == self::PATH)
{
//TODO: Make this global

$loader = new \Twig\Loader\FilesystemLoader($this->base_path);
$twig = new \Twig\Environment($loader);

echo $twig->render($this->path_content, $this->data);
}
else if ($this->type == self::CONTENT)
{

$loader = new \Twig\Loader\ArrayLoader(array(
'page.html' => $this->path_content,
));

$twig = new \Twig\Environment($loader);

echo $twig->render('page.html', $this->data);

}



}

/**
*
* Allows you to set the key, value data to be used when rendering the view.
* The data can either be appended to already existing data, or replace already existing data.
*
* @param $data
* @param bool $append
*/
function SetData($data, $append=true)
{
if ($append) {
$this->data = array_merge($this->data, $data);
}
else
{
$this->data = $data;
}

}
}



32 changes: 32 additions & 0 deletions src/wpOOW/Utilities/VersionDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace wpOOW\Utilities;

class VersionDetails{

public $major_number = 0;
public $minor_number = 0;
public $patch_number = 0;
public $build_number = 0;
public $build_date = null;

function __construct($composer_package_details)
{
$version_number = explode(".", $composer_package_details["version"]);

$this->major_number = $version_number[0];
$this->minor_number = $version_number[1];
$this->patch_number = $version_number[2];
$this->build_number = $version_number[3];
$this->build_date = $composer_package_details["build_date"];
}

public function GetFullVersion()
{
return implode(".", [$this->major_number, $this->minor_number, $this->patch_number,$this->build_number]);
}

public function __toString(){
return sprintf("%s (%s)", $this->GetFullVersion(), $this->build_date);
}
}

0 comments on commit 206c373

Please sign in to comment.