-
Notifications
You must be signed in to change notification settings - Fork 0
The abstract component base class
The abstract class WPDLib\Components\Base
is the superclass that every component must be derived from. All the components in any of the Definitely plugins are subclasses of it, and if you're considering to use WPDLib for a plugin of your own, make sure to stick to that convention.
The class includes functionality related to adding children, retrieving component data and validating the component.
The following is the class reference for the abstract class WPDLib\Components\Base
.
Note that, although all properties are protected, they can still be accessed publicly through the magic __get()
function. This also applies to all properties stored inside the $args
array - they can be publicly accessed as if they were members of the class.
- Type: string
- Default: empty string
- Access: protected
Holds the slug of the component.
- Type: array
- Default: empty array
- Access: protected
Holds the properties of the component.
- Type: string
- Default: empty string
- Access: protected
Holds the scope the component belongs to.
- Type: array
- Default: empty array
- Access: protected
Holds the component's parent components (in most cases it will be just one).
- Type: array
- Default: empty array
- Access: protected
Holds the component's child components, separated by class name.
- Type: bool
- Default: false
- Access: protected
Stores whether the component has been validated yet.
- Type: bool|null
- Default: null
- Access: protected
Stores whether the component slug is valid (if it has already been validated).
- Type: string
- Default: empty string
- Access: protected
Holds the name of the filter that should be executed once the component has been validated.
- Access: public
-
Parameters:
-
string
$slug
: the slug of the component -
array
$args
: an array of properties for the component
-
string
The constructor sets the component slug and properties.
- Access: public
-
Parameters:
-
string
$property
: property to set a value for (either a class member or a property in the$args
member) -
mixed
$value
: value to set this property to
-
string
This magic methods provides public write access to all class members and also to all properties in the $args
array. Note that writing any parameters at runtime is discouraged as it may lead to unexpected behavior. In particular, you should never be in need to adjust class members (WPDLib already takes care of those) - only properties in $args
, if at all. You can only write properties before the WordPress init
hook.
- Access: public
-
Parameters:
-
string
$property
: property to get its value (either a class member or a property in the$args
member)
-
string
- Return Value: mixed|null: the value of the property or null if the property does not exist
This magic method provides public read access to all class members and also to all properties in the $args
array. You will mostly only use it to access $slug
and properties in $args
. While you can also access $parents
and $children
this way, it is recommended to use the equivalent functions for those instead (see further below).
- Access: public
-
Parameters:
-
string
$property
: property to check whether it is available
-
string
- Return Value: bool: true if the property is available, false otherwise
This magic method provides provides a way to check whether a property exists in the class.
- Access: public
-
Parameters:
-
WPDLib\Components\Base
$component
: a component to add as a child for this component
-
WPDLib\Components\Base
- Return Value: WPDLib\Components\Base|WPDLib\Util\Error: either the component added or an error object
This method adds another component as a child for this component. The function will perform multiple checks whether the component to add is a valid child for the current component. It will furthermore validate the new component's properties and then add it as a child if everything was successful. This function must be called before the WordPress init
hook.
- Access: public
- Return Value: string the slug path to the component
This method returns the path to the component which you can use to access the component in WPDLib\Components\Manager::get()
. A such path consists of the component slugs leading to this component, each separated by a dot.
- Access: public
- Return Value: string the slug path to the component
This method returns the class path to the component class which you can use in combination with the path returned by get_path()
to access the component in WPDLib\Components\Manager::get()
. A such path consists of the class names leading to this component class, each separated by a dot.
- Access: public
-
Parameters:
-
string
$class
(optional): a class name to only return children of that class
-
string
- Return Value: array: an array of child components
This method returns the children of the component. By default it will return all children. If a specific class name is provided, it will only return children of that class.
- Access: public
-
Parameters:
-
integer
$index
(optional): an integer to specify the index of the parent (only valid if class can have multiple parents) -
integer
$depth
(optional): an integer to specify the depth of the parent to return (for example 2 to return the grandparent component)
-
integer
- Return Value: WPDLib\Components\Base|null: the parent component or null if it does not exist
This method returns the parent of this component. For most components, the function can easily be called without any parameters since most components only allow one parent anyways.
- Access: public
-
Parameters:
-
WPDLib\Components\Base|null
$parent
(optional): the parent component for this component
-
WPDLib\Components\Base|null
- Return Value: bool|WPDLib\Util\Error: true if the component has been validated, false if it already has been validated before or an error object if an error occurred during the validation
This method validates the component. Although it is public, it should not be used - it is called by WPDLib itself when the component is added (either to another component or as a toplevel component).
- Access: public
-
Parameters:
-
WPDLib\Components\Base|null
$parent
(optional): the parent component for this component
-
WPDLib\Components\Base|null
- Return Value: bool: true if the component slug is valid, false otherwise
This method checks whether the component slug is valid.
- (abstract)
- Access: protected
- Return Value: array: the array of default values for each property
This method returns the default values for all properties in the $args
member.
- (abstract)
- Access: protected
- Return Value: bool: whether this component supports multiple parents
This method returns whether the component supports to have multiple parents. In most cases this should simply return false, but depending on what the component is needed for, there may be exceptions.
- (abstract)
- Access: protected
- Return Value: bool|string: whether this component supports global slugs
This method returns whether the component supports global slug. There are the following options: The function can either return true to basically allow any slug, false to ensure that the slug is a 100% unique or a class name of any parent component class to ensure that the slug is unique among all other components that are children of that component class.
WPDLib is the foundation for all Definitely plugins which allow developers to build their admin interfaces more quickly. Those plugins are: