Use new code style

This commit is contained in:
David Stone
2025-02-07 19:02:33 -07:00
parent 0181024ae1
commit 8433379d90
672 changed files with 37107 additions and 45249 deletions

View File

@ -37,8 +37,7 @@ class Billing_Address {
public function __construct($data = array()) {
$this->attributes($data);
} // end __construct;
}
/**
* Loops through allowed fields and loads them.
@ -53,16 +52,11 @@ class Billing_Address {
$allowed_attributes = array_keys(self::fields());
foreach ($data as $key => $value) {
if (in_array($key, $allowed_attributes, true)) {
$this->attributes[$key] = $value;
} // end if;
} // end foreach;
} // end attributes;
$this->attributes[ $key ] = $value;
}
}
}
/**
* Checks if this billing address has any content at all.
@ -72,9 +66,8 @@ class Billing_Address {
*/
public function exists() {
return !empty(array_filter($this->attributes));
} // end exists;
return ! empty(array_filter($this->attributes));
}
/**
* Checks if a parameter exists.
@ -87,8 +80,7 @@ class Billing_Address {
public function __isset($name) {
return wu_get_isset($this->attributes, $name, '');
} // end __isset;
}
/**
* Gets a billing address field.
@ -103,8 +95,7 @@ class Billing_Address {
$value = wu_get_isset($this->attributes, $name, '');
return apply_filters("wu_billing_address_get_{$name}", $value, $this);
} // end __get;
}
/**
* Sets a billing address field.
@ -118,9 +109,8 @@ class Billing_Address {
$value = apply_filters("wu_billing_address_set_{$name}", $value, $this);
$this->attributes[$name] = $value;
} // end __set;
$this->attributes[ $name ] = $value;
}
/**
* Returns the validation rules for the billing address fields.
*
@ -133,8 +123,7 @@ class Billing_Address {
$keys = array_keys(array_filter($fields, fn($item) => wu_get_isset($item, 'validation_rules')));
return array_combine($keys, array_column($fields, 'validation_rules'));
} // end validation_rules;
}
/**
* Validates the fields following the validation rules.
@ -144,19 +133,16 @@ class Billing_Address {
*/
public function validate() {
$validator = new \WP_Ultimo\Helpers\Validator;
$validator = new \WP_Ultimo\Helpers\Validator();
$validator->validate($this->to_array(), $this->validation_rules());
if ($validator->fails()) {
return $validator->get_errors();
} // end if;
}
return true;
} // end validate;
}
/**
* Returns a key => value representation of the billing address.
@ -173,20 +159,15 @@ class Billing_Address {
$fields = self::fields();
foreach ($fields as $field_key => $field) {
if (!empty($this->{$field_key})) {
if ( ! empty($this->{$field_key})) {
$key = $labels ? $field['title'] : $field_key;
$address_array[$key] = $this->{$field_key};
} // end if;
} // end foreach;
$address_array[ $key ] = $this->{$field_key};
}
}
return $address_array;
} // end to_array;
}
/**
* Returns a string representation of the billing address.
*
@ -204,8 +185,7 @@ class Billing_Address {
public function to_string($delimiter = PHP_EOL): string {
return implode($delimiter, $this->to_array());
} // end to_string;
}
/**
* Returns the field array with values added.
@ -219,14 +199,11 @@ class Billing_Address {
$fields = self::fields($zip_only);
foreach ($fields as $field_key => &$field) {
$field['value'] = $this->{$field_key};
} // end foreach;
}
return $fields;
} // end get_fields;
}
/**
* Billing Address field definitions.
@ -246,23 +223,17 @@ class Billing_Address {
// Get allowed countries in form
if ($checkout_form && $checkout_form->has_country_lock()) {
$allowed_countries = $checkout_form->get_allowed_countries();
// Allow the Select Country field
$allowed_countries[] = '';
foreach ($countries as $country_code => $country) {
if (!in_array($country_code, $allowed_countries, true)) {
unset($countries[$country_code]);
} // end if;
} // end foreach;
} // end if;
if ( ! in_array($country_code, $allowed_countries, true)) {
unset($countries[ $country_code ]);
}
}
}
$fields['company_name'] = array(
'type' => 'text',
@ -329,13 +300,11 @@ class Billing_Address {
$fields = wu_set_order_from_index($fields); // Adds missing order attributes
if ($zip_only) {
$fields = array(
'billing_zip_code' => $fields['billing_zip_code'],
'billing_country' => $fields['billing_country'],
);
} // end if;
}
/**
* Allow plugin developers to filter the billing address fields.
@ -343,7 +312,7 @@ class Billing_Address {
* @since 2.0.0
*
* @param array $fields Billing Address array.
* @param bool $zip_only If we only need zip and country.
* @param bool $zip_only If we only need zip and country.
* @return array
*/
$fields = apply_filters('wu_billing_address_fields', $fields, $zip_only);
@ -351,8 +320,7 @@ class Billing_Address {
uasort($fields, 'wu_sort_by_order');
return $fields;
} // end fields;
}
/**
* Billing Address fields array for REST API.
@ -366,27 +334,21 @@ class Billing_Address {
$fields_for_rest = array();
foreach (self::fields($zip_only) as $field_key => $field) {
$options = wu_get_isset($field, 'options', false);
$enum = is_callable($options) ? call_user_func($options) : false;
$fields_for_rest[$field_key] = array(
$fields_for_rest[ $field_key ] = array(
'description' => wu_get_isset($field, 'title', false) . '. ' . wu_get_isset($field, 'default_placeholder', false),
'type' => 'string',
'required' => wu_get_isset($field, 'required', false),
);
if ($enum) {
$fields_for_rest[$field_key]['enum'] = array_keys($enum);
} // end if;
} // end foreach;
$fields_for_rest[ $field_key ]['enum'] = array_keys($enum);
}
}
return $fields_for_rest;
} // end fields_for_rest;
} // end class Billing_Address;
}
}

View File

@ -68,8 +68,7 @@ class Limitations {
public function __construct($modules_data = array()) {
$this->build_modules($modules_data);
} // end __construct;
}
/**
* Returns the module via magic getter.
@ -84,26 +83,21 @@ class Limitations {
$module = wu_get_isset($this->modules, $name, false);
if ($module === false) {
$repo = self::repository();
$class_name = wu_get_isset($repo, $name, false);
if (class_exists($class_name)) {
$module = new $class_name(array());
$this->modules[$name] = $module;
$this->modules[ $name ] = $module;
return $module;
} // end if;
} // end if;
}
}
return $module;
} // end __get;
}
/**
* Prepare to serialization.
@ -115,8 +109,7 @@ class Limitations {
public function __serialize() { // phpcs:ignore
return $this->to_array();
} // end __serialize;
}
/**
* Handles un-serialization.
@ -130,8 +123,7 @@ class Limitations {
public function __unserialize($modules_data) { // phpcs:ignore
$this->build_modules($modules_data);
} // end __unserialize;
}
/**
* Builds the module list based on the module data.
@ -144,48 +136,38 @@ class Limitations {
public function build_modules($modules_data) {
foreach ($modules_data as $type => $data) {
$module = self::build($data, $type);
if ($module) {
$this->modules[$type] = $module;
} // end if;
} // end foreach;
$this->modules[ $type ] = $module;
}
}
return $this;
} // end build_modules;
/**
* Build a module, based on the data.
*
* @since 2.0.0
*
* @param string|array $data The module data.
* @param string $module_name The module_name.
* @return false|\WP_Ultimo\Limitations\Limit
*/
static public function build($data, $module_name) {
}
/**
* Build a module, based on the data.
*
* @since 2.0.0
*
* @param string|array $data The module data.
* @param string $module_name The module_name.
* @return false|\WP_Ultimo\Limitations\Limit
*/
public static function build($data, $module_name) {
$class = wu_get_isset(self::repository(), $module_name);
if (class_exists($class)) {
if (is_string($data)) {
$data = json_decode($data, true);
}
return new $class($data);
} // end if;
}
return false;
} // end build;
}
/**
* Checks if a limitation model exists in this limitations.
@ -198,8 +180,7 @@ class Limitations {
public function exists($module) {
return wu_get_isset($this->modules, $module, false);
} // end exists;
}
/**
* Checks if we have any limitation modules setup at all.
@ -212,18 +193,13 @@ class Limitations {
$has_limitations = false;
foreach ($this->modules as $module) {
if ($module->is_enabled()) {
return true;
} // end if;
} // end foreach;
}
}
return $has_limitations;
} // end has_limitations;
}
/**
* Checks if a particular module is enabled.
@ -238,8 +214,7 @@ class Limitations {
$module = $this->{$module_name};
return $module ? $module->is_enabled() : false;
} // end is_module_enabled;
}
/**
* Merges limitations from other entities.
@ -257,37 +232,29 @@ class Limitations {
*/
public function merge($override = false, ...$limitations) {
if (!is_bool($override)) {
if ( ! is_bool($override)) {
$limitations[] = $override;
$override = false;
} // end if;
}
$results = $this->to_array();
foreach ($limitations as $limitation) {
if (is_a($limitation, \WP_Ultimo\Objects\Limitations::class)) { // @phpstan-ignore-line
if (is_a($limitation, self::class)) { // @phpstan-ignore-line
$limitation = $limitation->to_array();
}
} // end if;
if (!is_array($limitation)) {
if ( ! is_array($limitation)) {
continue;
}
} // end if;
$this->merge_recursive($results, $limitation, !$override);
} // end foreach;
$this->merge_recursive($results, $limitation, ! $override);
}
return new self($results);
} // end merge;
}
/**
* Merges a limitation array
@ -310,26 +277,20 @@ class Limitations {
$force_enabled = in_array($current_id, $force_enabled_list, true);
if ($force_enabled && (!wu_get_isset($array1, 'enabled', true) || !wu_get_isset($array2, 'enabled', true))) {
if ($force_enabled && (! wu_get_isset($array1, 'enabled', true) || ! wu_get_isset($array2, 'enabled', true))) {
$array1['enabled'] = true;
$array2['enabled'] = true;
}
} // end if;
if (!wu_get_isset($array1, 'enabled', true)) {
if ( ! wu_get_isset($array1, 'enabled', true)) {
$array1 = array(
'enabled' => false,
);
}
} // end if;
if (!wu_get_isset($array2, 'enabled', true) && $should_sum) {
if ( ! wu_get_isset($array2, 'enabled', true) && $should_sum) {
return;
} // end if;
}
foreach ($array2 as $key => &$value) {
/**
@ -338,24 +299,19 @@ class Limitations {
*/
$value = is_object($value) ? get_object_vars($value) : $value;
if (isset($array1[$key])) {
if (isset($array1[ $key ])) {
$array1[ $key ] = is_object($array1[ $key ]) ? get_object_vars($array1[ $key ]) : $array1[ $key ];
}
$array1[$key] = is_object($array1[$key]) ? get_object_vars($array1[$key]) : $array1[$key];
} // end if;
if (is_array($value) && isset($array1[$key]) && is_array($array1[$key])) {
$array1_id = wu_get_isset($array1[$key], 'id', $current_id);
if (is_array($value) && isset($array1[ $key ]) && is_array($array1[ $key ])) {
$array1_id = wu_get_isset($array1[ $key ], 'id', $current_id);
$this->current_merge_id = wu_get_isset($value, 'id', $array1_id);
$this->merge_recursive($array1[$key], $value, $should_sum);
$this->merge_recursive($array1[ $key ], $value, $should_sum);
$this->current_merge_id = $current_id;
} else {
$original_value = wu_get_isset($array1, $key);
// If the value is 0 or '' it can be a unlimited value
@ -366,22 +322,16 @@ class Limitations {
* We use values 0 or '' as unlimited in our limits
*/
continue;
} elseif (isset($array1[$key]) && is_numeric($array1[$key]) && is_numeric($value) && $should_sum && !$is_unlimited) {
$array1[$key] = ((int) $array1[$key]) + $value;
} elseif ($key === 'visibility' && isset($array1[$key]) && $should_sum) {
} elseif (isset($array1[ $key ]) && is_numeric($array1[ $key ]) && is_numeric($value) && $should_sum && ! $is_unlimited) {
$array1[ $key ] = ((int) $array1[ $key ]) + $value;
} elseif ($key === 'visibility' && isset($array1[ $key ]) && $should_sum) {
$key_priority = array(
'hidden' => 0,
'visible' => 1,
);
$array1[$key] = $key_priority[$value] > $key_priority[$array1[$key]] ? $value : $array1[$key];
} elseif ($key === 'behavior' && isset($array1[$key]) && $should_sum) {
$array1[ $key ] = $key_priority[ $value ] > $key_priority[ $array1[ $key ] ] ? $value : $array1[ $key ];
} elseif ($key === 'behavior' && isset($array1[ $key ]) && $should_sum) {
$key_priority_list = array(
'plugins' => array(
'default' => 10,
@ -402,24 +352,19 @@ class Limitations {
),
);
$key_priority = apply_filters("wu_limitation_{$current_id}_priority", $key_priority_list[$current_id]);
$array1[$key] = $key_priority[$value] > $key_priority[$array1[$key]] ? $value : $array1[$key];
$key_priority = apply_filters("wu_limitation_{$current_id}_priority", $key_priority_list[ $current_id ]);
$array1[ $key ] = $key_priority[ $value ] > $key_priority[ $array1[ $key ] ] ? $value : $array1[ $key ];
} else {
// Avoid change true values
$array1[$key] = $original_value !== true || !$should_sum ? $value : true;
$array1[ $key ] = $original_value !== true || ! $should_sum ? $value : true;
$array1[$key] = $original_value !== true || !$should_sum ? $value : true;
} // end if;
} // end if;
} // end foreach;
} // end merge_recursive;
$array1[ $key ] = $original_value !== true || ! $should_sum ? $value : true;
}
}
}
}
/**
* Converts the limitations list to an array.
*
@ -428,8 +373,7 @@ class Limitations {
public function to_array(): array {
return array_map(fn($module) => method_exists($module, 'to_array') ? $module->to_array() : (array) $module, $this->modules);
} // end to_array;
}
/**
* Static method to return limitations in very early stages of the WordPress lifecycle.
@ -449,22 +393,18 @@ class Limitations {
* for the native tables of blogs.
*/
if ($slug === 'site') {
$slug = 'blog';
$wu_prefix = '';
} // end if;
}
$cache = static::$limitations_cache;
$key = sprintf('%s-%s', $slug, $id);
if (isset($cache[$key])) {
return $cache[$key];
} // end if;
if (isset($cache[ $key ])) {
return $cache[ $key ];
}
global $wpdb;
@ -476,20 +416,17 @@ class Limitations {
$results = $wpdb->get_var($sql); // phpcs:ignore
if (!empty($results)) {
if ( ! empty($results)) {
$limitations = unserialize($results);
} // end if;
}
/*
* Caches the results.
*/
static::$limitations_cache[$key] = $limitations;
static::$limitations_cache[ $key ] = $limitations;
return $limitations;
} // end early_get_limitations;
}
/**
* Delete limitations.
@ -511,20 +448,17 @@ class Limitations {
* so no need to use low-level sql calls.
*/
if ($slug === 'site') {
$wu_prefix = '';
$slug = 'blog';
} // end if;
}
$table_name = "{$wpdb->base_prefix}{$wu_prefix}{$slug}meta";
$sql = $wpdb->prepare("DELETE FROM {$table_name} WHERE meta_key = 'wu_limitations' AND {$wu_prefix}{$slug}_id = %d LIMIT 1", $id); // phpcs:ignore
$wpdb->get_var($sql); // phpcs:ignore
} // end remove_limitations;
}
/**
* Returns an empty permission set, with modules.
@ -532,19 +466,16 @@ class Limitations {
* @since 2.0.0
* @return self
*/
static public function get_empty() {
public static function get_empty() {
$limitations = new self();
foreach (array_keys(self::repository()) as $module_name) {
$limitations->{$module_name};
} // end foreach;
}
return $limitations;
} // end get_empty;
}
/**
* Repository of the limitation modules.
@ -554,7 +485,7 @@ class Limitations {
* @since 2.0.0
* @return array
*/
static public function repository() {
public static function repository() {
$classes = array(
'post_types' => \WP_Ultimo\Limitations\Limit_Post_Types::class,
@ -570,7 +501,5 @@ class Limitations {
);
return apply_filters('wu_limit_classes', $classes);
} // end repository;
} // end class Limitations;
}
}

View File

@ -37,8 +37,7 @@ class Note {
public function __construct($data = array()) {
$this->attributes($data);
} // end __construct;
}
/**
* Loops through allowed fields and loads them.
@ -53,18 +52,13 @@ class Note {
$allowed_attributes = array_keys(self::fields());
foreach ($data as $key => $value) {
if (in_array($key, $allowed_attributes, true)) {
$this->attributes[$key] = $value;
} // end if;
} // end foreach;
$this->attributes[ $key ] = $value;
}
}
$this->attributes['date_created'] = wu_get_current_time('mysql', true);
} // end attributes;
}
/**
* Checks if this note has any content at all.
@ -74,9 +68,8 @@ class Note {
*/
public function exists() {
return !empty(array_filter($this->attributes));
} // end exists;
return ! empty(array_filter($this->attributes));
}
/**
* Checks if a parameter exists.
@ -89,8 +82,7 @@ class Note {
public function __isset($name) {
return wu_get_isset($this->attributes, $name, '');
} // end __isset;
}
/**
* Gets a note field.
@ -105,8 +97,7 @@ class Note {
$value = wu_get_isset($this->attributes, $name, '');
return apply_filters("wu_note_get_{$name}", $value, $this);
} // end __get;
}
/**
* Sets a note field.
@ -120,9 +111,8 @@ class Note {
$value = apply_filters("wu_note_set_{$name}", $value, $this);
$this->attributes[$name] = $value;
} // end __set;
$this->attributes[ $name ] = $value;
}
/**
* Returns the validation rules for new notes.
@ -133,8 +123,7 @@ class Note {
protected function validation_rules() {
return array();
} // end validation_rules;
}
/**
* Validates the fields following the validation rules.
@ -144,19 +133,16 @@ class Note {
*/
public function validate() {
$validator = new \WP_Ultimo\Helpers\Validator;
$validator = new \WP_Ultimo\Helpers\Validator();
$validator->validate($this->to_array(), $this->validation_rules());
if ($validator->fails()) {
return $validator->get_errors();
} // end if;
}
return true;
} // end validate;
}
/**
* Returns a key => value representation of the notes fields.
@ -173,20 +159,15 @@ class Note {
$fields = self::fields();
foreach ($fields as $field_key => $field) {
if (!empty($this->{$field_key})) {
if ( ! empty($this->{$field_key})) {
$key = $labels ? $field['title'] : $field_key;
$address_array[$key] = $this->{$field_key};
} // end if;
} // end foreach;
$address_array[ $key ] = $this->{$field_key};
}
}
return $address_array;
} // end to_array;
}
/**
* Returns the contents of the note.
*
@ -197,8 +178,7 @@ class Note {
public function to_string($delimiter = PHP_EOL): string {
return implode($delimiter, $this->to_array());
} // end to_string;
}
/**
* Note field definitions.
@ -230,7 +210,5 @@ class Note {
uasort($fields, 'wu_sort_by_order');
return $fields;
} // end fields;
} // end class Note;
}
}

View File

@ -23,10 +23,10 @@ class Visits {
* Key to save on the database.
*/
const KEY = 'wu_visits';
/**
* @var int
*/
protected $site_id;
/**
* @var int
*/
protected $site_id;
/**
* Sets the current site to manage.
@ -35,10 +35,9 @@ class Visits {
*
* @param int $site_id The current site id.
*/
public function __construct($site_id)
{
$this->site_id = $site_id;
} // end __construct;
public function __construct($site_id) {
$this->site_id = $site_id;
}
/**
* Returns the meta key to save visits.
*
@ -49,8 +48,7 @@ class Visits {
protected function get_meta_key($day): string {
return sprintf('%s_%s', self::KEY, $day);
} // end get_meta_key;
}
/**
* Adds visits to a site count.
@ -63,11 +61,9 @@ class Visits {
*/
public function add_visit($count = 1, $day = false) {
if (!$day) {
if ( ! $day) {
$day = gmdate('Ymd');
} // end if;
}
$key = $this->get_meta_key($day);
@ -76,8 +72,7 @@ class Visits {
$new_value = $current_value + $count;
return update_site_meta($this->site_id, $key, $new_value);
} // end add_visit;
}
/**
* Returns an array of the dates and counts by day.
@ -92,29 +87,27 @@ class Visits {
global $wpdb;
if (!$start_date) {
if ( ! $start_date) {
$start_date = wu_get_current_time('mysql', true);
}
} // end if;
if (!$end_date) {
if ( ! $end_date) {
$end_date = wu_get_current_time('mysql', true);
}
} // end if;
$query = $wpdb->prepare("
$query = $wpdb->prepare(
"
SELECT meta_value as count, str_to_date(meta_key, 'wu_visits_%%Y%%m%%d') as day, blog_id as site_id
FROM {$wpdb->base_prefix}blogmeta
WHERE blog_id = %d
", $this->site_id);
",
$this->site_id
);
$query .= $wpdb->prepare(" AND str_to_date(meta_key, 'wu_visits_%%Y%%m%%d') BETWEEN %s AND %s", gmdate('Y-m-d', strtotime($start_date)), gmdate('Y-m-d', strtotime($end_date)));
return $wpdb->get_results($query); // phpcs:ignore
} // end get_visits;
}
/**
* The total visits for the current site.
@ -129,29 +122,27 @@ class Visits {
global $wpdb;
if (!$start_date) {
if ( ! $start_date) {
$start_date = wu_get_current_time('mysql', true);
}
} // end if;
if (!$end_date) {
if ( ! $end_date) {
$end_date = wu_get_current_time('mysql', true);
}
} // end if;
$query = $wpdb->prepare("
$query = $wpdb->prepare(
"
SELECT SUM(meta_value) as count
FROM {$wpdb->base_prefix}blogmeta
WHERE blog_id = %d
", $this->site_id);
",
$this->site_id
);
$query .= $wpdb->prepare(" AND str_to_date(meta_key, 'wu_visits_%%Y%%m%%d') BETWEEN %s AND %s", gmdate('Y-m-d', strtotime($start_date)), gmdate('Y-m-d', strtotime($end_date)));
return (int) $wpdb->get_var($query); // phpcs:ignore
} // end get_visit_total;
}
/**
* Get sites by visit count.
@ -167,17 +158,13 @@ class Visits {
global $wpdb;
if (!$start_date) {
if ( ! $start_date) {
$start_date = wu_get_current_time('mysql', true);
}
} // end if;
if (!$end_date) {
if ( ! $end_date) {
$end_date = wu_get_current_time('mysql', true);
} // end if;
}
$sub_query = "
SELECT SUM(meta_value) as count, blog_id
@ -200,7 +187,5 @@ class Visits {
// phpcs:enable
return $wpdb->get_results($query); // phpcs:ignore
} // end get_sites_by_visit_count;
} // end class Visits;
}
}