Skip to content

Commit

Permalink
Release/1.1.1 (#22)
Browse files Browse the repository at this point in the history
* Adds role support.

* Adds metadata support.
  • Loading branch information
alexstandiford authored Mar 10, 2021
1 parent cb3f804 commit fb277b7
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 6 deletions.
109 changes: 109 additions & 0 deletions lib/abstracts/Meta_Record_Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Underpin\Abstracts;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

abstract class Meta_Record_Type {

protected $key = '';

protected $default_value = '';

protected $type = '';

/**
* Option constructor.
*
* @sinced 1.1.1
*
* @param string $key The option key
* @param string $description A human-readable description of this option
* @param string $name Human readable name.
* @param mixed $default_value The default value to set for this setting
*/
public function __construct( $key, $description, $name, $default_value = [] ) {
$this->key = $key;
$this->description = $description;
$this->name = $name;
$this->default_value = $default_value;
}

/**
* Adds the metadata.
*
* @since 1.1.1
*
* @return bool
*/
public function add( $object_id, $unique = false ) {
return add_metadata( $this->type, $object_id, $this->key, $this->default_value, $unique );
}

/**
* Retrieves the record.
*
* @since 1.1.1
*
* @param int $object_id ID of the object metadata is for.
* @param bool $single Optional. If true, return only the first value of the specified meta_key.
* This parameter has no effect if meta_key is not specified. Default false.
*
* @return mixed|void
*/
public function get( $object_id, $single = false ) {
return get_metadata( $this->type, $object_id, $this->key, $single );
}

/**
* Updates the record to the specified value.
*
* @since 1.1.1
*
* @param int $object_id ID of the object metadata is for.
* @param mixed $value Metadata value. Must be serializable if non-scalar.
* @param mixed $prev_value Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries. Default empty.
*
* @return bool True if updated, otherwise false
*/
public function update( $object_id, $value, $prev_value = '' ) {
return update_metadata( $this->type, $object_id, $this->key, $value, $prev_value );
}

/**
* Deletes the record.
*
* @since 1.1.1
*
* @param int $object_id ID of the object metadata is for.
* @param mixed $value Optional. Metadata value. Must be serializable if non-scalar.
* If specified, only delete metadata entries with this value.
* Otherwise, delete all entries with the specified meta_key.
* Pass `null`, `false`, or an empty string to skip this check.
* (For backward compatibility, it is not possible to pass an empty string
* to delete those entries with an empty string for a value.)
*
* @return bool
*/
public function delete( $object_id, $value = '' ) {
return delete_metadata( $this->type, $object_id, $this->key, $value );
}

/**
* Resets the record to the default value.
*
* @since 1.0.0
*
* @param int $object_id ID of the object metadata is for.
*
* @return bool
*/
public function reset( $object_id ) {
return $this->update( $object_id, $this->default_value );
}

}
44 changes: 44 additions & 0 deletions lib/abstracts/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Role Abstraction
*
* @since 1.1.1
* @package Underpin\Abstracts
*/

namespace Underpin\Abstracts;

use Underpin\Traits\Feature_Extension;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

abstract class Role {
use Feature_Extension;

/**
* id
* String that identifies this role.
*
* @var string Role
*/
protected $id = '';

/**
* capabilities
* List of capabilities keyed by the capability name, e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
*
* @var array
*/
protected $capabilities = array();

/**
* @inheritDoc
*/
public function do_actions() {
// Add the role.
add_role( $this->id, $this->name, $this->capabilities );
}

}
2 changes: 1 addition & 1 deletion lib/abstracts/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function get_param( $param ) {
* @since 1.0.0
*
* @param string $key The key for the localized value.
* @param string $value The value
* @param mixed $value The value
* @return true|WP_Error True if successful, \WP_Error if param was added too late.
*/
public function set_param( $key, $value ) {
Expand Down
33 changes: 33 additions & 0 deletions lib/abstracts/Underpin.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,28 @@ public function sidebars() {
return $this->_get_loader( 'Sidebars' );
}

/**
* Retrieves the user meta loader.
*
* @since 1.0.0
*
* @return Loaders\User_Meta
*/
public function user_meta() {
return $this->_get_loader( 'User_Meta' );
}

/**
* Retrieves the post meta loader.
*
* @since 1.0.0
*
* @return Loaders\Post_Meta
*/
public function post_meta() {
return $this->_get_loader( 'Post_Meta' );
}

/**
* Retrieves the menus loader.
*
Expand Down Expand Up @@ -451,6 +473,17 @@ public function admin_menus() {
return $this->_get_loader( 'Admin_Menus' );
}

/**
* Retrieves the admin menus loader.
*
* @since 1.0.0
*
* @return Loaders\Roles
*/
public function roles() {
return $this->_get_loader( 'Roles' );
}

/**
* Retrieves the rest endpoints loader.
*
Expand Down
51 changes: 51 additions & 0 deletions lib/factories/Post_Meta_Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* WordPress Option Abstraction
*
* @since 1.0.0
* @package Lib\Core\Abstracts
*/


namespace Underpin\Factories;

use Underpin\Abstracts\Meta_Record_Type;
use WP_Error;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Option
* WordPress Option Class
*
* @since 1.0.0
* @package Lib\Core\Abstracts
*/
class Post_Meta_Type extends Meta_Record_Type {

protected $type = 'post';

/**
* @inheritDoc
*/
public function update( $object_id, $value, $prev_value = '' ) {
return update_post_meta( $object_id, $value, $prev_value );
}

/**
* @inheritDoc
*/
public function delete( $object_id, $value = '' ) {
return delete_post_meta( $object_id, $this->key, $value );
}

/**
* @inheritDoc
*/
public function add( $object_id, $unique = false ) {
return add_post_meta( $object_id, $this->key, $this->default_value, $unique );
}

}
28 changes: 28 additions & 0 deletions lib/factories/User_Meta_Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* WordPress Option Abstraction
*
* @since 1.0.0
* @package Lib\Core\Abstracts
*/


namespace Underpin\Factories;

use Underpin\Abstracts\Meta_Record_Type;
use WP_Error;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Option
* WordPress Option Class
*
* @since 1.0.0
* @package Lib\Core\Abstracts
*/
class User_Meta_Type extends Meta_Record_Type {
protected $type = 'user';
}
27 changes: 27 additions & 0 deletions lib/loaders/Post_Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Underpin\Loaders;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

use Underpin\Abstracts\Registries\Loader_Registry;

class Post_Meta extends Loader_Registry {

protected $abstraction_class = 'Underpin\Factories\Post_Meta_Type';

protected function set_default_items() {
// TODO: Implement set_default_items() method.
}

/**
* @param string $key
*
* @return \Underpin\Factories\Post_Meta_Type|\WP_Error Post Meta instance, if it exists. WP_Error, otherwise.
*/
public function get( $key ) {
return parent::get( $key );
}

}
50 changes: 50 additions & 0 deletions lib/loaders/Roles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Role Registry
*
* @since 1.0.0
* @package Underpin\Registries\Loaders
*/


namespace Underpin\Loaders;

use Underpin\Abstracts\Custom_Post_Type;
use Underpin\Abstracts\Registries\Loader_Registry;
use Underpin\Abstracts\Role;
use WP_Error;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Roles
* Registry for Roles
*
* @since 1.0.0
* @package Underpin\Registries\Loaders
*/
class Roles extends Loader_Registry {

/**
* @inheritDoc
*/
protected $abstraction_class = 'Underpin\Abstracts\Role';

/**
* @inheritDoc
*/
protected function set_default_items() {
// $this->add( 'role', 'Namespace\To\Class');
}

/**
* @param string $key
* @return Role|WP_Error Script Resulting REST Endpoint class, if it exists. WP_Error, otherwise.
*/
public function get( $key ) {
return parent::get( $key );
}

}
Loading

0 comments on commit fb277b7

Please sign in to comment.