-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds role support. * Adds metadata support.
- Loading branch information
1 parent
cb3f804
commit fb277b7
Showing
10 changed files
with
374 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
Oops, something went wrong.