Initial Commit

This commit is contained in:
David Stone
2024-11-30 18:24:12 -07:00
commit e8f7955c1c
5432 changed files with 1397750 additions and 0 deletions

View File

@ -0,0 +1,80 @@
<?php
/**
* Customer User Role Limit Module.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.10
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Customer User Role Limit Module.
*
* @since 2.0.10
*/
class Limit_Customer_User_Role extends Limit {
/**
* The module id.
*
* @since 2.0.10
* @var string
*/
protected $id = 'customer_user_role';
/**
* Returns a default state.
*
* @since 2.0.10
* @return array
*/
public static function default_state() {
return array(
'enabled' => true,
'limit' => 'default'
);
} // end default_state;
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.10
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
public function check($value_to_check, $limit, $type = '') {
return true;
} // end check;
/**
* Gets the limit data.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return mixed
*/
public function get_limit($type = '') {
$default_value = wu_get_setting('default_role', 'administrator');
return empty($this->limit) || $this->limit === 'default' ? $default_value : $this->limit;
} // end get_limit;
} // end class Limit_Customer_User_Role;

View File

@ -0,0 +1,49 @@
<?php
/**
* Limit manager for sites.
*
* @package WP_Ultimo
* @subpackage Objects
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Limit manager for sites.
*
* @since 2.0.0
*/
class Limit_Disk_Space extends Limit {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id = 'disk_space';
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
public function check($value_to_check, $limit, $type = '') {
return true;
} // end check;
} // end class Limit_Disk_Space;

View File

@ -0,0 +1,127 @@
<?php
/**
* Domain_Mapping Limit Module.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Domain_Mapping Limit Module.
*
* @since 2.0.0
*/
class Limit_Domain_Mapping extends Limit {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id = 'domain_mapping';
/**
* The mode of template assignment/selection.
*
* @since 2.0.0
* @var string
*/
protected $mode = 'default';
/**
* Allows sub-type limits to set their own default value for enabled.
*
* @since 2.0.0
* @var bool
*/
private bool $enabled_default_value = true;
/**
* Sets up the module based on the module data.
*
* @since 2.0.0
*
* @param array $data The module data.
* @return void
*/
public function setup($data) {
parent::setup($data);
$this->mode = wu_get_isset($data, 'mode', 'default');
} // end setup;
/**
* Returns the mode. Can be one of three: default, assign_template and choose_available_templates.
*
* @since 2.0.0
* @return string
*/
public function get_mode() {
return $this->mode;
} // end get_mode;
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
public function check($value_to_check, $limit, $type = '') {
$check = true;
return $check;
} // end check;
/**
* Returns default permissions.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return array
*/
public function get_default_permissions($type) {
return array(
'behavior' => 'available',
);
} // end get_default_permissions;
/**
* Returns a default state.
*
* @since 2.0.0
* @return array
*/
public static function default_state() {
return array(
'enabled' => true,
'limit' => null,
'mode' => 'default',
);
} // end default_state;
} // end class Limit_Domain_Mapping;

View File

@ -0,0 +1,157 @@
<?php
/**
* Plugins Limit Module.
*
* @package WP_Ultimo
* @subpackage Objects
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Plugins Limit Module.
*
* @since 2.0.0
*/
class Limit_Plugins extends Limit {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id = 'plugins';
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
public function check($value_to_check, $limit, $type = '') {
$plugin = (object) $this->{$value_to_check};
$types = array(
'visible' => $plugin->visibility === 'visible',
'hidden' => $plugin->visibility === 'hidden',
'default' => $plugin->behavior === 'default',
'force_active' => $plugin->behavior === 'force_active',
'force_inactive' => $plugin->behavior === 'force_inactive',
'force_active_locked' => $plugin->behavior === 'force_active_locked',
'force_inactive_locked' => $plugin->behavior === 'force_inactive_locked',
);
return wu_get_isset($types, $type, false);
} // end check;
/**
* Adds a magic getter for plugins.
*
* @since 2.0.0
*
* @param string $plugin_name The plugin name.
* @return object
*/
public function __get($plugin_name) {
$plugin = (object) wu_get_isset($this->get_limit(), $plugin_name, $this->get_default_permissions($plugin_name));
return (object) wp_parse_args($plugin, $this->get_default_permissions($plugin_name));
} // end __get;
/**
* Returns a list of plugins by behavior and visibility.
*
* @since 2.0.0
*
* @param null|string $behavior The behaviour to search for.
* @param null|string $visibility The visibility to search for.
* @return array
*/
public function get_by_type($behavior = null, $visibility = null) {
$search_params = array();
if ($behavior) {
$search_params[] = array('behavior', $behavior);
} // end if;
if ($visibility) {
$search_params[] = array('visibility', $visibility);
} // end if;
$results = \WP_Ultimo\Dependencies\Arrch\Arrch::find((array) $this->get_limit(), array(
'where' => $search_params,
));
return $results;
} // end get_by_type;
/**
* Returns default permissions.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return array
*/
public function get_default_permissions($type) {
return array(
'visibility' => 'visible',
'behavior' => 'default',
);
} // end get_default_permissions;
/**
* Checks if a theme exists on the current module.
*
* @since 2.0.0
*
* @param string $plugin_name The theme name.
* @return bool
*/
public function exists($plugin_name) {
$results = wu_get_isset($this->get_limit(), $plugin_name, array());
return wu_get_isset($results, 'visibility', 'not-set') !== 'not-set' || wu_get_isset($results, 'behavior', 'not-set') !== 'not-set';
} // end exists;
/**
* Checks if the module is enabled.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return boolean
*/
public function is_enabled($type = '') {
return true;
} // end is_enabled;
} // end class Limit_Plugins;

View File

@ -0,0 +1,110 @@
<?php
/**
* Post Types Limit Module.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Post Types Limit Module.
*
* @since 2.0.0
*/
class Limit_Post_Types extends Limit_Subtype {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id = 'post_types';
/**
* Check if we are already above the post quota.
*
* @since 2.0.0
*
* @param string $post_type The post type to check against.
* @return boolean
*/
public function is_post_above_limit($post_type) {
/*
* Calculate post count based on all different status
*/
$post_count = $this->get_post_count($post_type);
// Get the allowed quota
$quota = $this->{$post_type}->number;
/**
* Checks if a given post type is allowed on this plan
* Allow plugin developers to filter the return value
*
* @since 1.7.0
* @param bool If the post type is disabled or not
* @param WU_Plan Plan of the current user
* @param int User id
*/
return apply_filters('wu_limits_is_post_above_limit', $quota > 0 && $post_count >= $quota);
} // end is_post_above_limit;
/**
* Get the post count for this site.
*
* @since 2.0.0
*
* @param string $post_type The post type to check against.
* @return int
*/
public static function get_post_count($post_type) {
$count = 0;
$post_count = wp_count_posts($post_type);
$statuses = $post_type === 'attachment' ? array('inherit') : array('publish', 'private');
/**
* Allow plugin developers to change which post status should be counted
* By default, published and private posts are counted
*
* @since 1.9.1
* @param array $post_status The list of post statuses
* @param string $post_type The post type slug
* @return array New array of post status
*/
$post_statuses = apply_filters('wu_post_count_statuses', $statuses, $post_type);
foreach ($post_statuses as $post_status) {
if (isset($post_count->{$post_status})) {
$count += (int) $post_count->{$post_status};
} // end if;
} // end foreach;
/**
* Allow plugin developers to change the count total
*
* @since 1.9.1
* @param int $count The total post count
* @param object $post_counts WordPress object return by the wp_count_posts fn
* @param string $post_type The post type slug
* @return int New total
*/
return apply_filters('wu_post_count', $count, $post_count, $post_type);
} // end get_post_count;
} // end class Limit_Post_Types;

View File

@ -0,0 +1,285 @@
<?php
/**
* Site_Templates Limit Module.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Site_Templates Limit Module.
*
* @since 2.0.0
*/
class Limit_Site_Templates extends Limit {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id = 'site_templates';
/**
* The mode of template assignment/selection.
*
* @since 2.0.0
* @var string
*/
protected $mode = 'default';
/**
* Allows sub-type limits to set their own default value for enabled.
*
* @since 2.0.0
* @var bool
*/
private bool $enabled_default_value = true;
/**
* Sets up the module based on the module data.
*
* @since 2.0.0
*
* @param array $data The module data.
* @return void
*/
public function setup($data) {
parent::setup($data);
$this->mode = wu_get_isset($data, 'mode', 'default');
} // end setup;
/**
* Returns the mode. Can be one of three: default, assign_template and choose_available_templates.
*
* @since 2.0.0
* @return string
*/
public function get_mode() {
return $this->mode;
} // end get_mode;
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
public function check($value_to_check, $limit, $type = '') {
$template = (object) $this->{$value_to_check};
$types = array(
'available' => $template->behavior === 'available',
'not_available' => $template->behavior === 'not_available',
'pre_selected' => $template->behavior === 'pre_selected',
);
return wu_get_isset($types, $type, true);
} // end check;
/**
* Adds a magic getter for themes.
*
* @since 2.0.0
*
* @param string $template_id The template site id.
* @return object
*/
public function __get($template_id) {
$template_id = str_replace('site_', '', $template_id);
$template = (object) wu_get_isset($this->get_limit(), $template_id, $this->get_default_permissions($template_id));
return (object) wp_parse_args($template, $this->get_default_permissions($template_id));
} // end __get;
/**
* Returns default permissions.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return array
*/
public function get_default_permissions($type) {
return array(
'behavior' => 'available',
);
} // end get_default_permissions;
/**
* Checks if a theme exists on the current module.
*
* @since 2.0.0
*
* @param string $template_id The template site id.
* @return bool
*/
public function exists($template_id) {
$template_id = str_replace('site_', '', $template_id);
$results = wu_get_isset($this->get_limit(), $template_id, array());
return wu_get_isset($results, 'behavior', 'not-set') !== 'not-set';
} // end exists;
/**
* Get all themes.
*
* @since 2.0.0
* @return array List of theme stylesheets.
*/
public function get_all_templates(): array {
$templates = (array) $this->get_limit();
return array_keys($templates);
} // end get_all_templates;
/**
* Get available themes.
*
* @since 2.0.0
* @return array
*/
public function get_available_site_templates() {
$limits = $this->get_limit();
if (!$limits) {
return false;
} // end if;
$limits = (array) $limits;
$available = array();
foreach ($limits as $site_id => $site_settings) {
$site_settings = (object) $site_settings;
if ($site_settings->behavior === 'available' || $site_settings->behavior === 'pre_selected' || $this->mode === 'default') {
$available[] = $site_id;
} // end if;
} // end foreach;
return $available;
} // end get_available_site_templates;
/**
* Get the forced active theme for the current limitations.
*
* @since 2.0.0
* @return string
*/
public function get_pre_selected_site_template() {
$limits = $this->get_limit();
$pre_selected_site_template = false;
if (!$limits) {
return $pre_selected_site_template;
} // end if;
foreach ($limits as $site_id => $site_settings) {
$site_settings = (object) $site_settings;
if ($site_settings->behavior === 'pre_selected') {
$pre_selected_site_template = $site_id;
} // end if;
} // end foreach;
return $pre_selected_site_template;
} // end get_pre_selected_site_template;
/**
* Handles limits on post submission.
*
* @since 2.0.0
* @return mixed
*/
public function handle_limit() {
$module = wu_get_isset($_POST['modules'], $this->id, array());
return wu_get_isset($module, 'limit', $this->get_limit());
} // end handle_limit;
/**
* Handles other elements when saving. Used for custom attributes.
*
* @since 2.0.0
*
* @param array $module The current module, extracted from the request.
* @return array
*/
public function handle_others($module) {
$_module = wu_get_isset($_POST['modules'], $this->id, array());
$module['mode'] = wu_get_isset($_module, 'mode', 'default');
return $module;
} // end handle_others;
/**
* Returns a default state.
*
* @since 2.0.0
* @return array
*/
public static function default_state() {
return array(
'enabled' => true,
'limit' => null,
'mode' => 'default',
);
} // end default_state;
} // end class Limit_Site_Templates;

View File

@ -0,0 +1,49 @@
<?php
/**
* Sites Limit Module.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Sites Limit Module.
*
* @since 2.0.0
*/
class Limit_Sites extends Limit {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id = 'sites';
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
public function check($value_to_check, $limit, $type = '') {
return true;
} // end check;
} // end class Limit_Sites;

View File

@ -0,0 +1,142 @@
<?php
/**
* Subtypes Limit Module.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Subtypes Limit Module.
*
* @since 2.0.0
*/
class Limit_Subtype extends Limit {
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
public function check($value_to_check, $limit, $type = '') {
/*
* If no type is passed, bail.
*/
if (!$type) {
return false;
} // end if;
/*
* Set default values for inexistent post types.
* By default they are enabled and unlimited.
*/
$post_type_limit = (object) wu_get_isset($limit, $type, $this->get_default_permissions($type));
/*
* Case post type disabled.
*/
if (!$post_type_limit->enabled) {
return false;
} // end if;
/*
* Case unlimited posts
*/
if (absint($post_type_limit->number) === 0) {
return true;
} // end if;
return absint($value_to_check) < absint($post_type_limit->number);
} // end check;
/**
* Adds a magic getter for subtypes.
*
* @since 2.0.0
*
* @param string $sub_type The sub type.
* @return object
*/
public function __get($sub_type) {
$type = (object) wu_get_isset($this->get_limit(), $sub_type, $this->get_default_permissions($sub_type));
return (object) wp_parse_args($type, $this->get_default_permissions($sub_type));
} // end __get;
/**
* Returns default permissions.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return array
*/
public function get_default_permissions($type) {
return array(
'enabled' => true,
'number' => '', // unlimited
);
} // end get_default_permissions;
/**
* Checks if a theme exists on the current module.
*
* @since 2.0.0
*
* @param string $type The type.
* @return bool
*/
public function exists($type) {
$results = wu_get_isset($this->get_limit(), $type, array());
return wu_get_isset($results, 'number', 'not-set') !== 'not-set';
} // end exists;
/**
* Handles limits on post submission.
*
* @since 2.0.0
* @return mixed
*/
public function handle_limit() {
$received = wu_get_isset($_POST['modules'][$this->id], 'limit', array());
foreach ($received as $post_type => &$limitations) {
$limitations['enabled'] = (bool) wu_get_isset($_POST['modules'][$this->id]['limit'][$post_type], 'enabled', false);
} // end foreach;
return $received;
} // end handle_limit;
} // end class Limit_Subtype;

View File

@ -0,0 +1,213 @@
<?php
/**
* Themes Limit Module.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Themes Limit Module.
*
* @since 2.0.0
*/
class Limit_Themes extends Limit {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id = 'themes';
/**
* The theme being currently forced for this site.
*
* @since 2.0.0
* @var null|false|string Null when first initialized, false when no theme is forced or the theme name.
*/
protected $forced_active_theme;
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
public function check($value_to_check, $limit, $type = '') {
$theme = (object) $this->{$value_to_check};
$types = array(
'visible' => $theme->visibility === 'visible',
'hidden' => $theme->visibility === 'hidden',
'available' => $theme->behavior === 'available',
'not_available' => $theme->behavior === 'not_available',
);
return wu_get_isset($types, $type, false);
} // end check;
/**
* Adds a magic getter for themes.
*
* @since 2.0.0
*
* @param string $theme_name The theme name.
* @return object
*/
public function __get($theme_name) {
$theme = (object) wu_get_isset($this->get_limit(), $theme_name, $this->get_default_permissions($theme_name));
return (object) wp_parse_args($theme, $this->get_default_permissions($theme_name));
} // end __get;
/**
* Returns default permissions.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return array
*/
public function get_default_permissions($type) {
return array(
'visibility' => 'visible',
'behavior' => 'available',
);
} // end get_default_permissions;
/**
* Checks if a theme exists on the current module.
*
* @since 2.0.0
*
* @param string $theme_name The theme name.
* @return bool
*/
public function exists($theme_name) {
$results = wu_get_isset($this->get_limit(), $theme_name, array());
return wu_get_isset($results, 'visibility', 'not-set') !== 'not-set' || wu_get_isset($results, 'behavior', 'not-set') !== 'not-set';
} // end exists;
/**
* Get all themes.
*
* @since 2.0.0
* @return array List of theme stylesheets.
*/
public function get_all_themes() {
$themes = (array) $this->get_limit();
return array_keys($themes);
} // end get_all_themes;
/**
* Get available themes.
*
* @since 2.0.0
* @return array
*/
public function get_available_themes() {
$limits = $this->get_limit();
$available = array();
foreach ($limits as $theme_slug => $theme_settings) {
$theme_settings = (object) $theme_settings;
if ($theme_settings->behavior === 'available') {
$available[] = $theme_slug;
} // end if;
} // end foreach;
return $available;
} // end get_available_themes;
/**
* Get the forced active theme for the current limitations.
*
* @since 2.0.0
* @return string
*/
public function get_forced_active_theme() {
$active_theme = false;
$limits = $this->get_limit();
if (empty($limits)) {
return $active_theme;
} // end if;
if ($this->forced_active_theme !== null) {
return $this->forced_active_theme;
} // end if;
foreach ($limits as $theme_slug => $theme_settings) {
$theme_settings = (object) $theme_settings;
if ($theme_settings->behavior === 'force_active') {
$active_theme = $theme_slug;
} // end if;
} // end foreach;
$this->forced_active_theme = $active_theme;
return $this->forced_active_theme;
} // end get_forced_active_theme;
/**
* Checks if the module is enabled.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return boolean
*/
public function is_enabled($type = '') {
return true;
} // end is_enabled;
} // end class Limit_Themes;

View File

@ -0,0 +1,30 @@
<?php
/**
* Users Limit Module.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Users Limit Module.
*
* @since 2.0.0
*/
class Limit_Users extends Limit_Subtype {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id = 'users';
} // end class Limit_Users;

View File

@ -0,0 +1,49 @@
<?php
/**
* Visits Limit Module.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Visits Limit Module.
*
* @since 2.0.0
*/
class Limit_Visits extends Limit {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id = 'visits';
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
public function check($value_to_check, $limit, $type = '') {
return true;
} // end check;
} // end class Limit_Visits;

View File

@ -0,0 +1,368 @@
<?php
/**
* Base limit module class.
*
* @package WP_Ultimo
* @subpackage Limitations
* @since 2.0.0
*/
namespace WP_Ultimo\Limitations;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Base limit module class.
*
* @since 2.0.0
*/
abstract class Limit implements \JsonSerializable {
/**
* The module id.
*
* @since 2.0.0
* @var string
*/
protected $id;
/**
* The limit data.
*
* Here, each module will have different schemas.
* From simple int values for disk_space, for example,
* to arrays with fields, like plugins and post_types.
*
* @since 2.0.0
* @var mixed
*/
protected $limit;
/**
* The on/off status of the module.
*
* Limitations are only applied if the module
* is enabled.
*
* @since 2.0.0
* @var bool
*/
protected $enabled;
/**
* Controls if this limit has its own limit.
*
* When the limit is inherited from other models,
* such as memberships or products.
*
* @since 2.0.0
* @var bool
*/
private bool $has_own_limit = true;
/**
* Controls if this limit has its own enabled status.
*
* When the enabled is inherited from other models,
* such as memberships or products.
*
* @since 2.0.0
* @var bool
*/
private bool $has_own_enabled = true;
/**
* Allows sub-type limits to set their own default value for enabled.
*
* @since 2.0.0
* @var bool
*/
private bool $enabled_default_value = true;
/**
* Constructs the limit module.
*
* @since 2.0.0
* @param array $data The module data.
*/
public function __construct($data) {
$this->setup($data);
} // end __construct;
/**
* Prepare for serialization.
*
* @since 2.0.0
* @return string
*/
public function __serialize() { // phpcs:ignore
return serialize($this->to_array());
} // end __serialize;
/**
* Handles un-serialization.
*
* @since 2.0.0
*
* @param string $data The un-serialized data.
* @return void
*/
public function __unserialize($data) { // phpcs:ignore
$this->setup(unserialize($data));
} // end __unserialize;
/**
* Sets up the module based on the module data.
*
* @since 2.0.0
*
* @param array $data The module data.
* @return void
*/
public function setup($data) {
if (!is_array($data)) {
$data = (array) $data;
} // end if;
$current_limit = wu_get_isset($data, 'limit', 'not-set');
/*
* Sets the own limit flag, if necessary.
*/
if ($current_limit === 'not-set' || $current_limit === '') {
$this->has_own_limit = false;
} // end if;
/*
* Sets the own enabled flag, if necessary.
*/
if (wu_get_isset($data, 'enabled', 'not-set') === 'not-set') {
$this->has_own_enabled = false;
} // end if;
$data = wp_parse_args($data, array(
'limit' => null,
'enabled' => $this->enabled_default_value,
));
$this->limit = is_array($data['limit']) ? (object) $data['limit'] : $data['limit'];
$this->enabled = (bool) $data['enabled'];
do_action("wu_{$this->id}_limit_setup", $data, $this);
} // end setup;
/**
* Returns the id of the module.
*
* @since 2.0.0
* @return string
*/
public function get_id() {
return $this->id;
} // end get_id;
/**
* Checks if a value is allowed under this limit.
*
* This function is what you should be calling when validating
* limits. This method is final, so it can't be redefined.
*
* Limits should implement a check method that gets
* called in here.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param string $type The type parameter.
* @return bool
*/
public final function allowed($value_to_check, $type = '') {
$allowed = $this->is_enabled();
if ($allowed) {
$allowed = $this->check($value_to_check, $this->limit, $type);
} // end if;
return apply_filters("wu_limit_{$this->id}_{$type}_allowed", $allowed, $type, $this);
} // end allowed;
/**
* The check method is what gets called when allowed is called.
*
* Each module needs to implement a check method, that returns a boolean.
* This check can take any form the developer wants.
*
* @since 2.0.0
*
* @param mixed $value_to_check Value to check.
* @param mixed $limit The list of limits in this modules.
* @param string $type Type for sub-checking.
* @return bool
*/
abstract public function check($value_to_check, $limit, $type = '');
/**
* Gets the limit data.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return mixed
*/
public function get_limit($type = '') {
return $this->limit;
} // end get_limit;
/**
* Checks if the module is enabled.
*
* @since 2.0.0
*
* @param string $type Type for sub-checking.
* @return boolean
*/
public function is_enabled($type = '') {
return $this->enabled;
} // end is_enabled;
/**
* Converts the limitations list to an array.
*
* @since 2.0.0
* @return array
*/
public function to_array() {
$array = get_object_vars($this);
/*
* Removes the unnecessary data.
*/
unset($array['has_own_limit']);
unset($array['has_own_enabled']);
unset($array['enabled_default_value']);
return $array;
} // end to_array;
/**
* Prepares for serialization.
*
* @since 2.0.0
* @return string
*/
#[\ReturnTypeWillChange]
public function jsonSerialize() {
return json_encode($this->to_array());
} // end jsonSerialize;
/**
* Checks if this module has its own limit.
*
* @since 2.0.0
* @return boolean
*/
public function has_own_limit() {
return $this->has_own_limit;
} // end has_own_limit;
/**
* Checks if this module has its own enabled.
*
* @since 2.0.0
* @return boolean
*/
public function has_own_enabled() {
return $this->has_own_enabled;
} // end has_own_enabled;
/**
* Handles enabled status on post submission.
*
* @since 2.0.0
* @return bool
*/
public function handle_enabled() {
$module = wu_get_isset($_POST['modules'], $this->id, array());
return (bool) wu_get_isset($module, 'enabled', false);
} // end handle_enabled;
/**
* Handles other elements when saving. Used for custom attributes.
*
* @since 2.0.0
*
* @param array $module The current module, extracted from the request.
* @return array
*/
public function handle_others($module) {
return $module;
} // end handle_others;
/**
* Handles limits on post submission.
*
* @since 2.0.0
* @return mixed
*/
public function handle_limit() {
$module = wu_get_isset($_POST['modules'], $this->id, array());
return wu_get_isset($module, 'limit', null);
} // end handle_limit;
/**
* Returns a default state.
*
* @since 2.0.0
* @return array
*/
public static function default_state() {
return array(
'enabled' => false,
'limit' => null,
);
} // end default_state;
} // end class Limit;