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,139 @@
<?php // phpcs:disable
/**
* Adds a validation rules that allows us to check if a given parameter is unique.
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.0
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
use WP_Ultimo\Managers\Signup_Fields_Manager;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Adds a validation rules that allows us to check if a given parameter is unique.
*
* @since 2.0.0
*/
class Checkout_Steps extends Rule {
/**
* Error message to be returned when this value has been used.
*
* @since 2.0.0
* @var string
*/
protected $message = ':attribute is wrongly setup.';
/**
* Parameters that this rule accepts.
*
* @since 2.0.0
* @var array
*/
protected $fillableParams = array(); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.0
*
* @param mixed $value Value being checked.
*/
public function check($value) : bool {
if (is_string($value)) {
$value = maybe_unserialize($value);
} // end if;
$required_fields = Signup_Fields_Manager::get_instance()->get_required_fields();
$required_fields_list = array_keys($required_fields);
if (!$value || is_string($value)) {
return true;
} // end if;
$fields = array_column($value, 'fields');
if (empty($fields)) {
return true;
} // end if;
$all_fields = call_user_func_array('array_merge', $fields);
$all_fields_list = array_column($all_fields, 'type');
/**
* First, we validated that all of our required fields are present.
*/
$all_present = true;
foreach ($required_fields_list as $field_slug) {
if (!in_array($field_slug, $all_fields_list, true)) {
$this->message = sprintf(__('The %s field must be present in at least one of the checkout form steps.', 'wp-ultimo'), wu_slug_to_name($field_slug));
return false;
} // end if;
} // end if;
/**
* Allow developers to bypass the check if a field is auto-submittable.
*
* @since 2.0.0
* @param array $submittable_field_types The list of field types.
* @return array
*/
$submittable_field_types = apply_filters(
'wu_checkout_step_validation_submittable_field_types',
array(
'submit_button',
'pricing_table',
'template_selection',
)
);
/**
* Second, we must validate if every step has a submit button.
*/
foreach ($value as $step) {
$found_submittable_field_types = \WP_Ultimo\Dependencies\Arrch\Arrch::find($step['fields'], array(
'where' => array(
array('type', $submittable_field_types),
),
));
if (empty($found_submittable_field_types)) {
$this->message = sprintf(__('The %s step is missing a submit field', 'wp-ultimo'), $step['name']);
return false;
} // end if;
} // end foreach;
/*
* @todo: Plan, product selection fields must come before the order summary and payment fields.
*/
return true;
} // end check;
} // end class Checkout_Steps;

View File

@ -0,0 +1,64 @@
<?php
/**
* Adds a validation rules for cities
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.11
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
// Exit if accessed directly
defined('ABSPATH') || exit;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
/**
* Validates template sites.
*
* @since 2.0.4
*/
class City extends Rule {
/**
* Parameters that this rule accepts.
*
* @since 2.0.4
* @var array
*/
protected $fillableParams = array('country', 'state'); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.11
*
* @param mixed $city The city value detected.
*/
public function check($city) : bool { // phpcs:ignore
$check = true;
$country = $this->parameter('country') ?? wu_request('billing_country');
$state = $this->parameter('state') ?? wu_request('billing_state');
if ($country && $state && $city) {
$state = strtoupper((string) $state);
$allowed_cities = wu_get_country_cities(strtoupper((string) $country), $state, false);
if (!empty($allowed_cities)) {
$check = in_array($city, $allowed_cities, true);
} // end if;
} // end if;
return $check;
} // end check;
} // end class City;

View File

@ -0,0 +1,56 @@
<?php
/**
* Adds a validation rules for countries
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.11
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
// Exit if accessed directly
defined('ABSPATH') || exit;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
/**
* Validates template sites.
*
* @since 2.0.4
*/
class Country extends Rule {
/**
* Parameters that this rule accepts.
*
* @since 2.0.4
* @var array
*/
protected $fillableParams = array(); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.4
*
* @param mixed $country The country value detected.
*/
public function check($country) : bool { // phpcs:ignore
$check = true;
if ($country) {
$country = strtoupper((string) $country);
$allowed_countries = array_keys(wu_get_countries());
$check = in_array($country, $allowed_countries, true);
} // end if;
return $check;
} // end check;
} // end class Country;

View File

@ -0,0 +1,52 @@
<?php
/**
* Adds a validation rules that allows us to check if a given parameter exists.
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.0
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Adds a validation rules that allows us to check if a given parameter exists.
*
* @since 2.0.0
*/
class Domain extends Rule {
/**
* Error message to be returned when this value has been used.
*
* @since 2.0.0
* @var string
*/
protected $message = ':attribute :value is not valid';
/**
* Parameters that this rule accepts.
*
* @since 2.0.0
* @var array
*/
protected $fillableParams = array(); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.0
*
* @param mixed $value Value being checked.
*/
public function check($value) : bool { // phpcs:ignore
return (bool) preg_match('/^(?!\-)(?:(?:[a-zA-Z\d][a-zA-Z\d\-]{0,61})?[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/', (string) $value);
} // end check;
} // end class Domain;

View File

@ -0,0 +1,62 @@
<?php
/**
* Adds a validation rules that allows us to check if a given parameter exists.
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.0
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Adds a validation rules that allows us to check if a given parameter exists.
*
* @since 2.0.0
*/
class Exists extends Rule {
/**
* Error message to be returned when this value has been used.
*
* @since 2.0.0
* @var string
*/
protected $message = ':attribute :value is not valid';
/**
* Parameters that this rule accepts.
*
* @since 2.0.0
* @var array
*/
protected $fillableParams = array('model', 'column', 'except'); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.0
*
* @param mixed $value Value being checked.
*/
public function check($value) : bool {
$this->requireParameters(array(
'model',
'column'
));
$column = $this->parameter('column');
$model = $this->parameter('model');
// do query
return !!$model::get_by($column, $value);
} // end check;
} // end class Exists;

View File

@ -0,0 +1,125 @@
<?php // phpcs:disable
/**
* Adds a validation rules that allows us to check if a given parameter is unique.
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.0
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Adds a validation rules that allows us to check if a given parameter is unique.
*
* @since 2.0.0
*/
class Price_Variations extends Rule {
/**
* Error message to be returned when this value has been used.
*
* @since 2.0.0
* @var string
*/
protected $message = ':attribute is wrongly setup.';
/**
* Parameters that this rule accepts.
*
* @since 2.0.0
* @var array
*/
protected $fillableParams = array('duration', 'duration_unit'); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.0
*
* @param mixed $value Value being checked.
*/
public function check($value) : bool {
if (is_string($value)) {
$value = maybe_unserialize($value);
} // end if;
if (!is_array($value)) {
return false;
} // end if;
foreach ($value as $price_variation) {
/**
* Validation Duration
*/
$duration = wu_get_isset($price_variation, 'duration', false);
if (!is_numeric($duration) || (int) $duration <= 0) {
return false;
} // end if;
/**
* Validation Unit
*/
$unit = wu_get_isset($price_variation, 'duration_unit', false);
$allowed_units = array(
'day',
'week',
'month',
'year',
);
if (!in_array($unit, $allowed_units, true)) {
return false;
} // end if;
/**
* Check if it is the same as the main duration
*/
if ($this->parameter('duration') == $duration && $this->parameter('duration_unit') === $unit) {
$this->message = 'This product cannot have a price variation for the same duration and duration unit values as the product itself.';
return false;
} // end if;
/**
* Validation Amount
*/
$amount = wu_get_isset($price_variation, 'amount', false);
if ($amount) {
$amount = wu_to_float($amount);
} // end if;
if (!is_numeric($amount)) {
return false;
} // end if;
} // end foreach;
return true;
} // end check;
} // end class Price_Variations;

View File

@ -0,0 +1,66 @@
<?php
/**
* Adds a validation rule for products.
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.4
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Validates products.
*
* @since 2.0.4
*/
class Products extends Rule {
/**
* Error message to be returned when this value has been used.
*
* @since 2.0.4
* @var string
*/
protected $message = '';
/**
* Parameters that this rule accepts.
*
* @since 2.0.4
* @var array
*/
protected $fillableParams = array(); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.4
*
* @param mixed $products Value being checked.
*/
public function check($products) : bool { // phpcs:ignore
$products = (array) $products;
$product_objects = array_map('wu_get_product', $products);
list($plan, $additional_products) = wu_segregate_products($product_objects);
if ($plan) {
return true;
} // end if;
$this->message = __('A plan is required.', 'wp-ultimo');
return false;
} // end check;
} // end class Products;

View File

@ -0,0 +1,130 @@
<?php
/**
* Adds a validation rules that allows us to check if a given parameter is unique.
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.4
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
use \WP_Ultimo\Dependencies\Rakit\Validation\Rule;
use \WP_Ultimo\Checkout\Checkout;
use \WP_Ultimo\Database\Sites\Site_Type;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Validates template sites.
*
* @since 2.0.4
*/
class Site_Template extends Rule {
/**
* Error message to be returned when this value has been used.
*
* @since 2.0.4
* @var string
*/
protected $message = '';
/**
* Parameters that this rule accepts.
*
* @since 2.0.4
* @var array
*/
protected $fillableParams = array(); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.4
*
* @param mixed $template_id Value being checked.
*/
public function check($template_id) : bool { // phpcs:ignore
$template_id = absint($template_id);
if (!$template_id) {
return true;
} // end if;
$site = wu_get_site($template_id);
if (!$site || ($site->get_type() !== Site_Type::SITE_TEMPLATE && $site->get_type() !== Site_Type::CUSTOMER_OWNED)) {
$this->message = __('The Template ID does not correspond to a valid Template', 'wp-ultimo');
return false;
} // end if;
if ($site->get_type() === Site_Type::CUSTOMER_OWNED) {
if (!wu_get_setting('allow_own_site_as_template')) {
$this->message = __('You can not use your sites as template', 'wp-ultimo');
return false;
} // end if;
$customer = wu_get_current_customer();
if (!$customer || $site->get_customer_id() !== $customer->get_id()) {
$this->message = __('The selected template is not available.', 'wp-ultimo');
return false;
} // end if;
return true;
} // end if;
$allowed_templates = false;
$product_ids_or_slugs = Checkout::get_instance()->request_or_session('products', array());
$product_ids_or_slugs = array_unique($product_ids_or_slugs);
if ($product_ids_or_slugs) {
$products = array_map('wu_get_product', $product_ids_or_slugs);
$limits = new \WP_Ultimo\Objects\Limitations();
list($plan, $additional_products) = wu_segregate_products($products);
$products = array_merge(array($plan), $additional_products);
foreach ($products as $product) {
$limits = $limits->merge($product->get_limitations());
} // end foreach;
$allowed_templates = $limits->site_templates->get_available_site_templates();
} // end if;
if (is_array($allowed_templates) && !in_array($template_id, $allowed_templates)) { // phpcs:ignore
$this->message = __('The selected template is not available for this product.', 'wp-ultimo');
return false;
} // end if;
return true;
} // end check;
} // end class Site_Template;

View File

@ -0,0 +1,62 @@
<?php
/**
* Adds a validation rules for states
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.11
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
// Exit if accessed directly
defined('ABSPATH') || exit;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
/**
* Validates template sites.
*
* @since 2.0.4
*/
class State extends Rule {
/**
* Parameters that this rule accepts.
*
* @since 2.0.4
* @var array
*/
protected $fillableParams = array('country'); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.11
*
* @param mixed $state The state value detected.
*/
public function check($state) : bool { // phpcs:ignore
$check = true;
$country = $this->parameter('country') ?? wu_request('billing_country');
if ($country && $state) {
$state = strtoupper((string) $state);
$allowed_states = array_keys(wu_get_country_states(strtoupper((string) $country), false));
if (!empty($allowed_states)) {
$check = in_array($state, $allowed_states, true);
} // end if;
} // end if;
return $check;
} // end check;
} // end class State;

View File

@ -0,0 +1,66 @@
<?php
/**
* Adds a validation rules that allows us to check if a given parameter is unique.
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.0
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Adds a validation rules that allows us to check if a given parameter is unique.
*
* @since 2.0.0
*/
class Unique_Site extends Rule {
/**
* Error message to be returned when this value has been used.
*
* @since 2.0.0
* @var string
*/
protected $message = '';
/**
* Parameters that this rule accepts.
*
* @since 2.0.0
* @var array
*/
protected $fillableParams = array('self_id'); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.0
*
* @param mixed $value Value being checked.
*/
public function check($value) : bool { // phpcs:ignore
$this->requireParameters(array());
$self_id = $this->parameter('self_id');
$results = wpmu_validate_blog_signup($value, 'Test Title');
if ($results['errors']->has_errors()) {
$this->message = $results['errors']->get_error_message();
return false;
} // end if;
return true;
} // end check;
} // end class Unique_Site;

View File

@ -0,0 +1,96 @@
<?php
/**
* Adds a validation rules that allows us to check if a given parameter is unique.
*
* @package WP_Ultimo
* @subpackage Helpers/Validation_Rules
* @since 2.0.0
*/
namespace WP_Ultimo\Helpers\Validation_Rules;
use WP_Ultimo\Dependencies\Rakit\Validation\Rule;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Adds a validation rules that allows us to check if a given parameter is unique.
*
* @since 2.0.0
*/
class Unique extends Rule {
/**
* Error message to be returned when this value has been used.
*
* @since 2.0.0
* @var string
*/
protected $message = ':attribute :value has been used';
/**
* Parameters that this rule accepts.
*
* @since 2.0.0
* @var array
*/
protected $fillableParams = array('model', 'column', 'self_id'); // phpcs:ignore
/**
* Performs the actual check.
*
* @since 2.0.0
*
* @param mixed $value Value being checked.
*/
public function check($value) : bool {
$this->requireParameters(array(
'model',
'column',
));
$column = $this->parameter('column');
$model = $this->parameter('model');
$self_id = $this->parameter('self_id');
switch ($model) {
case '\WP_User':
$callback = 'get_user_by';
break;
default:
$callback = array($model, 'get_by');
break;
}
// do query
$existing = call_user_func($callback, $column, $value);
$user_models = array(
'\WP_User',
'\WP_Ultimo\Models\Customer',
);
/*
* Customize the error message for the customer.
*/
if (in_array($model, $user_models, true)) {
$this->message = __('A customer with the same email address or username already exists.', 'wp-ultimo');
} // end if;
if (!$existing) {
return true;
} // end if;
$id = method_exists($existing, 'get_id') ? $existing->get_id() : $existing->id;
return absint($id) === absint($self_id);
} // end check;
} // end class Unique;