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

@ -51,21 +51,20 @@ class Arr {
*/
public static function filter_by_property($array, $property, $expected_value, $flag = 0) {
$result = Arr::filter($array, function($value) use ($property, $expected_value) {
$result = self::filter(
$array,
function ($value) use ($property, $expected_value) {
return Arr::get($value, $property, null) == $expected_value; // phpcs:ignore
});
}
);
if ($flag) {
$result = $flag === Arr::RESULTS_FIRST ? reset($result) : end($result);
} // end if;
$result = $flag === self::RESULTS_FIRST ? reset($result) : end($result);
}
return $result;
} // end filter_by_property;
}
/**
* Filters an array using a callback.
@ -79,26 +78,19 @@ class Arr {
public static function filter($array, $closure) {
if ($closure) {
$result = array();
foreach ($array as $key => $value) {
if (call_user_func($closure, $value, $key)) {
$result[] = $value;
} // end if;
} // end foreach;
}
}
return $result;
} // end if;
}
return array_filter($array);
} // end filter;
}
/**
* Get a nested value inside an array. Dot notation is supported.
@ -113,32 +105,23 @@ class Arr {
public static function get($array, $key, $default = null) {
if (is_null($key)) {
return $array;
}
} // end if;
if (isset($array[$key])) {
return $array[$key];
} // end if;
if (isset($array[ $key ])) {
return $array[ $key ];
}
foreach (explode('.', $key) as $segment) {
if (!is_array($array) || !array_key_exists($segment, $array)) {
if ( ! is_array($array) || ! array_key_exists($segment, $array)) {
return $default;
}
} // end if;
$array = $array[$segment];
} // end foreach;
$array = $array[ $segment ];
}
return $array;
} // end get;
}
/**
* Set a nested value inside an array. Dot notation is supported.
@ -153,38 +136,30 @@ class Arr {
public static function set(&$array, $key, $value) {
if (is_null($key)) {
return $array = $value; // phpcs:ignore
} // end if;
}
$keys = explode('.', $key);
while (count($keys) > 1) {
$key = array_shift($keys);
if (!isset($array[$key]) || !is_array($array[$key])) {
if ( ! isset($array[ $key ]) || ! is_array($array[ $key ])) {
$array[ $key ] = array();
}
$array[$key] = array();
$array =& $array[ $key ];
}
} // end if;
$array =& $array[$key];
} // end while;
$array[array_shift($keys)] = $value;
$array[ array_shift($keys) ] = $value;
return $array;
} // end set;
}
/**
* Static class only.
*
* @since 2.0.11
*/
private function __construct() {} // end __construct;
} // end class Arr;
private function __construct() {}
}

View File

@ -29,7 +29,7 @@ class Hash {
/**
* Static-only class.
*/
private function __construct() {} // end __construct;
private function __construct() {}
/**
* Encodes a number or ID. Do not use to encode strings.
@ -45,8 +45,7 @@ class Hash {
$hasher = new Hashids($group, self::LENGTH, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
return $hasher->encode($number);
} // end encode;
}
/**
* Decodes a hash back into an integer.
@ -62,7 +61,5 @@ class Hash {
$hasher = new Hashids($group, 10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
return current($hasher->decode($hash));
} // end decode;
} // end class Hash;
}
}

View File

@ -22,83 +22,77 @@ defined('ABSPATH') || exit;
class Screenshot {
/**
* Returns the api link for the screenshot.
*
* @since 2.0.0
*
* @param string $domain Original site domain.
*/
* Returns the api link for the screenshot.
*
* @since 2.0.0
*
* @param string $domain Original site domain.
*/
public static function api_url($domain): string {
return 'https://image.thum.io/get/' . $domain;
}
/**
* Takes in a URL and creates it as an attachment.
*
* @since 2.0.0
*
* @param string $url Image URL to download.
* @return string|false
*/
public static function take_screenshot($url) {
/**
* Takes in a URL and creates it as an attachment.
*
* @since 2.0.0
*
* @param string $url Image URL to download.
* @return string|false
*/
public static function take_screenshot($url) {
$url = self::api_url($url);
return self::save_image_from_url($url);
} // end take_screenshot;
/**
* Downloads the image from the URL.
*
* @since 2.0.0
*
* @param string $url Image URL to download.
* @return int|false
*/
public static function save_image_from_url($url) {
}
/**
* Downloads the image from the URL.
*
* @since 2.0.0
*
* @param string $url Image URL to download.
* @return int|false
*/
public static function save_image_from_url($url) {
// translators: %s is the API URL.
$log_prefix = sprintf(__('Downloading image from "%s":'), $url) . ' ';
$response = wp_remote_get($url, array(
'timeout' => 50,
));
$response = wp_remote_get(
$url,
array(
'timeout' => 50,
)
);
if (wp_remote_retrieve_response_code($response) !== 200) {
wu_log_add('screenshot-generator', $log_prefix . wp_remote_retrieve_response_message($response), LogLevel::ERROR);
return false;
} // end if;
}
if (is_wp_error($response)) {
wu_log_add('screenshot-generator', $log_prefix . $response->get_error_message(), LogLevel::ERROR);
return false;
} // end if;
}
/*
* Check if the results contain a PNG header.
*/
if (strncmp($response['body'], "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", strlen("\x89\x50\x4e\x47\x0d\x0a\x1a\x0a")) !== 0) {
wu_log_add('screenshot-generator', $log_prefix . __('Result is not a PNG file.', 'wp-ultimo'), LogLevel::ERROR);
return false;
} // end if;
}
$upload = wp_upload_bits('screenshot-' . gmdate('Y-m-d-H-i-s') . '.png', null, $response['body']);
if (!empty($upload['error'])) {
if ( ! empty($upload['error'])) {
wu_log_add('screenshot-generator', $log_prefix . json_encode($upload['error']), LogLevel::ERROR);
return false;
} // end if;
}
$file_path = $upload['file'];
$file_name = basename($file_path);
@ -118,7 +112,7 @@ class Screenshot {
$attach_id = wp_insert_attachment($post_info, $file_path);
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once ABSPATH . 'wp-admin/includes/image.php';
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata($attach_id, $file_path);
@ -129,7 +123,5 @@ class Screenshot {
wu_log_add('screenshot-generator', $log_prefix . __('Success!', 'wp-ultimo'));
return $attach_id;
} // end save_image_from_url;
} // end class Screenshot;
}
}

View File

@ -47,8 +47,7 @@ class Sender {
$args = wp_parse_args($args, $default_args);
return $args;
} // end parse_args;
}
/**
* Send an email to one or more users.
@ -62,25 +61,23 @@ class Sender {
*/
public static function send_mail($from = array(), $to = array(), $args = array()) {
if (!$from) {
if ( ! $from) {
$from = array(
'email' => wu_get_setting('from_email'),
'name' => wu_get_setting('from_name'),
);
}
} // end if;
$args = Sender::parse_args($args);
$args = self::parse_args($args);
/*
* First, replace shortcodes.
*/
$payload = wu_get_isset($args, 'payload', array());
$subject = Sender::process_shortcodes(wu_get_isset($args, 'subject', ''), $payload);
$subject = self::process_shortcodes(wu_get_isset($args, 'subject', ''), $payload);
$content = Sender::process_shortcodes(wu_get_isset($args, 'content', ''), $payload);
$content = self::process_shortcodes(wu_get_isset($args, 'content', ''), $payload);
/*
* Content type and template
@ -88,7 +85,6 @@ class Sender {
$headers = array();
if (wu_get_isset($args, 'style', 'html') === 'html') {
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$default_settings = \WP_Ultimo\Admin_Pages\Email_Template_Customize_Admin_Page::get_default_settings();
@ -97,23 +93,24 @@ class Sender {
$template_settings = wp_parse_args($template_settings, $default_settings);
$template = wu_get_template_contents('broadcast/emails/base', array(
'site_name' => get_network_option(null, 'site_name'),
'site_url' => get_site_url(wu_get_main_site_id()),
'logo_url' => wu_get_network_logo(),
'is_editor' => false,
'subject' => $subject,
'content' => $content,
'template_settings' => $template_settings,
));
$template = wu_get_template_contents(
'broadcast/emails/base',
array(
'site_name' => get_network_option(null, 'site_name'),
'site_url' => get_site_url(wu_get_main_site_id()),
'logo_url' => wu_get_network_logo(),
'is_editor' => false,
'subject' => $subject,
'content' => $content,
'template_settings' => $template_settings,
)
);
} else {
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$template = nl2br(strip_tags($content, '<p><a><br>')); // by default, set the plain email content.
} // end if;
}
$bcc = '';
@ -121,7 +118,6 @@ class Sender {
* Build the recipients list.
*/
if (count($to) > 1) {
$to = array_map(fn($item) => wu_format_email_string(wu_get_isset($item, 'email'), wu_get_isset($item, 'name')), $to);
/*
@ -132,36 +128,33 @@ class Sender {
* emails sent out.
*/
if (apply_filters('wu_sender_recipients_strategy', 'bcc') === 'bcc') {
$main_to = $to[0];
unset($to[0]);
$bcc_array = array_map(function($item) {
$bcc_array = array_map(
function ($item) {
$email = is_array($item) ? $item['email'] : $item;
$email = is_array($item) ? $item['email'] : $item;
preg_match('/<([^>]+)>/', $email, $matches);
preg_match('/<([^>]+)>/', $email, $matches);
return !empty($matches[1]) ? $matches[1] : $email;
}, $to);
return ! empty($matches[1]) ? $matches[1] : $email;
},
$to
);
$bcc = implode(', ', array_filter($bcc_array));
$headers[] = "Bcc: $bcc";
$to = $main_to;
} // end if;
}
} else {
$to = array(
wu_format_email_string(wu_get_isset($to[0], 'email'), wu_get_isset($to[0], 'name'))
wu_format_email_string(wu_get_isset($to[0], 'email'), wu_get_isset($to[0], 'name')),
);
} // end if;
}
/*
* Build From
@ -184,12 +177,11 @@ class Sender {
// 'attachments' => $attachments,
// ));
// } // end if;
// }
// Send the actual email
return wp_mail($to, $subject, $template, $headers, $attachments);
} // end send_mail;
}
/**
* Change the shortcodes for values in the content.
@ -203,10 +195,8 @@ class Sender {
public static function process_shortcodes($content, $payload = array()) {
if (empty($payload)) {
return $content;
} // end if;
}
$match = array();
@ -215,15 +205,11 @@ class Sender {
$shortcodes = shortcode_atts(array_flip($match[1]), $payload);
foreach ($shortcodes as $shortcode_key => $shortcode_value) {
$shortcode_str = '{{' . $shortcode_key . '}}';
$content = str_replace($shortcode_str, nl2br((string) $shortcode_value), $content);
} // end foreach;
}
return $content;
} // end process_shortcodes;
} // end class Sender;
}
}

View File

@ -16,11 +16,9 @@ defined('ABSPATH') || exit;
require_once WP_ULTIMO_PLUGIN_DIR . '/inc/duplication/duplicate.php';
if (!defined('MUCD_PRIMARY_SITE_ID')) {
if ( ! defined('MUCD_PRIMARY_SITE_ID')) {
define('MUCD_PRIMARY_SITE_ID', get_current_network_id()); // phpcs:ignore
} // end if;
}
/**
* Exposes the public API to handle site duplication.
@ -36,7 +34,7 @@ class Site_Duplicator {
/**
* Static-only class.
*/
private function __construct() {} // end __construct;
private function __construct() {}
/**
* Duplicate an existing network site.
@ -53,7 +51,7 @@ class Site_Duplicator {
$args['from_site_id'] = $from_site_id;
$args['title'] = $title;
$duplicate_site = Site_Duplicator::process_duplication($args);
$duplicate_site = self::process_duplication($args);
if (is_wp_error($duplicate_site)) {
@ -63,8 +61,7 @@ class Site_Duplicator {
wu_log_add('site-duplication', $message, LogLevel::ERROR);
return $duplicate_site;
} // end if;
}
// translators: %1$d is the ID of the site template used, and %2$d is the id of the new site.
$message = sprintf(__('Attempt to duplicate site %1$d successful - New site id: %2$d', 'wp-ultimo'), $from_site_id, $duplicate_site);
@ -72,8 +69,7 @@ class Site_Duplicator {
wu_log_add('site-duplication', $message);
return $duplicate_site;
} // end duplicate_site;
}
/**
* Replace the contents of a site with the contents of another.
@ -95,14 +91,17 @@ class Site_Duplicator {
$to_site_customer = $to_site_membership->get_customer();
$args = wp_parse_args($args, array(
'email' => $to_site_customer->get_email_address(),
'title' => $to_site->get_title(),
'path' => $to_site->get_path(),
'from_site_id' => $from_site_id,
'to_site_id' => $to_site_id,
'meta' => $to_site->meta
));
$args = wp_parse_args(
$args,
array(
'email' => $to_site_customer->get_email_address(),
'title' => $to_site->get_title(),
'path' => $to_site->get_path(),
'from_site_id' => $from_site_id,
'to_site_id' => $to_site_id,
'meta' => $to_site->meta,
)
);
$duplicate_site_id = self::process_duplication($args);
@ -114,8 +113,7 @@ class Site_Duplicator {
wu_log_add('site-duplication', $message, LogLevel::ERROR);
return false;
} // end if;
}
$new_to_site = wu_get_site($duplicate_site_id);
@ -139,10 +137,8 @@ class Site_Duplicator {
wu_log_add('site-duplication', $message);
return $saved;
} // end if;
} // end override_site;
}
}
/**
* Processes a site duplication.
@ -166,20 +162,23 @@ class Site_Duplicator {
global $current_site, $wpdb;
$args = wp_parse_args($args, array(
'email' => '', // Required arguments.
'title' => '', // Required arguments.
'path' => '/', // Required arguments.
'from_site_id' => false, // Required arguments.
'to_site_id' => false,
'keep_users' => true,
'public' => true,
'domain' => $current_site->domain,
'copy_files' => wu_get_setting('copy_media', true),
'network_id' => get_current_network_id(),
'meta' => array(),
'user_id' => 0,
));
$args = wp_parse_args(
$args,
array(
'email' => '', // Required arguments.
'title' => '', // Required arguments.
'path' => '/', // Required arguments.
'from_site_id' => false, // Required arguments.
'to_site_id' => false,
'keep_users' => true,
'public' => true,
'domain' => $current_site->domain,
'copy_files' => wu_get_setting('copy_media', true),
'network_id' => get_current_network_id(),
'meta' => array(),
'user_id' => 0,
)
);
// Checks
$args = (object) $args;
@ -188,55 +187,41 @@ class Site_Duplicator {
$wpdb->hide_errors();
if (!$args->from_site_id) {
if ( ! $args->from_site_id) {
return new \WP_Error('from_site_id_required', __('You need to provide a valid site to duplicate.', 'wp-ultimo'));
}
} // end if;
$user_id = !empty($args->user_id) ? $args->user_id : self::create_admin($args->email, $site_domain);
$user_id = ! empty($args->user_id) ? $args->user_id : self::create_admin($args->email, $site_domain);
if (is_wp_error($user_id)) {
return $user_id;
}
} // end if;
if (!$args->to_site_id) {
if ( ! $args->to_site_id) {
$meta = array_merge($args->meta, array('public' => $args->public));
$args->to_site_id = wpmu_create_blog($args->domain, $args->path, $args->title, $user_id, $meta, $args->network_id);
$wpdb->show_errors();
} // end if;
}
if (is_wp_error($args->to_site_id)) {
return $args->to_site_id;
}
} // end if;
if (!is_numeric($args->to_site_id)) {
if ( ! is_numeric($args->to_site_id)) {
return new \WP_Error('site_creation_failed', __('An attempt to create a new site failed.', 'wp-ultimo'));
}
} // end if;
if (!is_super_admin($user_id) && !get_user_option('primary_blog', $user_id)) {
if ( ! is_super_admin($user_id) && ! get_user_option('primary_blog', $user_id)) {
update_user_option($user_id, 'primary_blog', $args->to_site_id, true);
} // end if;
}
\MUCD_Duplicate::bypass_server_limit();
if ($args->copy_files) {
$result = \MUCD_Files::copy_files($args->from_site_id, $args->to_site_id);
} // end if;
}
/**
* Supress email change notification on site duplication processes.
@ -246,10 +231,8 @@ class Site_Duplicator {
$result = \MUCD_Data::copy_data($args->from_site_id, $args->to_site_id);
if ($args->keep_users) {
$result = \MUCD_Duplicate::copy_users($args->from_site_id, $args->to_site_id);
} // end if;
}
wp_cache_flush();
@ -259,13 +242,15 @@ class Site_Duplicator {
* @since 1.9.4
* @return void
*/
do_action('wu_duplicate_site', array(
'site_id' => $args->to_site_id,
));
do_action(
'wu_duplicate_site',
array(
'site_id' => $args->to_site_id,
)
);
return $args->to_site_id;
} // end process_duplication;
}
/**
* Creates an admin user if no user exists with this email.
@ -282,26 +267,19 @@ class Site_Duplicator {
$user_id = email_exists($email);
if (!$user_id) { // Create a new user with a random password
if ( ! $user_id) { // Create a new user with a random password
$password = wp_generate_password(12, false);
$user_id = wpmu_create_user($domain, $password, $email);
if (false === $user_id) {
return new \WP_Error('user_creation_error', __('We were not able to create a new admin user for the site being duplicated.', 'wp-ultimo'));
} else {
wp_new_user_notification($user_id);
} // end if;
} // end if;
}
}
return $user_id;
} // end create_admin;
} // end class Site_Duplicator;
}
}

View File

@ -9,18 +9,18 @@
namespace WP_Ultimo\Helpers;
use \Rakit\Validation\Validator as Validator_Helper;
use \WP_Ultimo\Helpers\Validation_Rules\Unique;
use \WP_Ultimo\Helpers\Validation_Rules\Unique_Site;
use \WP_Ultimo\Helpers\Validation_Rules\Exists;
use \WP_Ultimo\Helpers\Validation_Rules\Checkout_Steps;
use \WP_Ultimo\Helpers\Validation_Rules\Price_Variations;
use \WP_Ultimo\Helpers\Validation_Rules\Domain;
use \WP_Ultimo\Helpers\Validation_Rules\Site_Template;
use \WP_Ultimo\Helpers\Validation_Rules\Products;
use \WP_Ultimo\Helpers\Validation_Rules\Country;
use \WP_Ultimo\Helpers\Validation_Rules\State;
use \WP_Ultimo\Helpers\Validation_Rules\City;
use Rakit\Validation\Validator as Validator_Helper;
use WP_Ultimo\Helpers\Validation_Rules\Unique;
use WP_Ultimo\Helpers\Validation_Rules\Unique_Site;
use WP_Ultimo\Helpers\Validation_Rules\Exists;
use WP_Ultimo\Helpers\Validation_Rules\Checkout_Steps;
use WP_Ultimo\Helpers\Validation_Rules\Price_Variations;
use WP_Ultimo\Helpers\Validation_Rules\Domain;
use WP_Ultimo\Helpers\Validation_Rules\Site_Template;
use WP_Ultimo\Helpers\Validation_Rules\Products;
use WP_Ultimo\Helpers\Validation_Rules\Country;
use WP_Ultimo\Helpers\Validation_Rules\State;
use WP_Ultimo\Helpers\Validation_Rules\City;
// Exit if accessed directly
defined('ABSPATH') || exit;
@ -66,44 +66,49 @@ class Validator {
// translators: %s is the field name.
$field_required_message = sprintf(__('The %s field is required', 'wp-ultimo'), ':attribute');
$validation_error_messages = apply_filters('wu_validator_error_messages', array(
'required' => $field_required_message,
'required_without' => $field_required_message,
'required_with' => $field_required_message,
// translators: %s is the email field identifier
'email' => sprintf(__('The %s is not valid email', 'wp-ultimo'), ':attribute'),
// translators: 1st %s is the field name; 2nd is the allowed value
'min' => sprintf(__('The %1$s minimum is %2$s', 'wp-ultimo'), ':attribute', ':min'),
// translators: 1st %s is the field name; 2nd is the allowed value
'max' => sprintf(__('The %1$s maximum is %2$s', 'wp-ultimo'), ':attribute', ':max'),
// translators: %s is the field identifier
'alpha_dash' => sprintf(__('The %s only allows a-z, 0-9, _ and -', 'wp-ultimo'), ':attribute'),
// translators: %s is the field identifier
'lowercase' => sprintf(__('The %s must be lowercase', 'wp-ultimo'), ':attribute'),
// translators: %s is the field identifier
'integer' => sprintf(__('The %s must be integer', 'wp-ultimo'), ':attribute')
), $this);
$validation_error_messages = apply_filters(
'wu_validator_error_messages',
array(
'required' => $field_required_message,
'required_without' => $field_required_message,
'required_with' => $field_required_message,
// translators: %s is the email field identifier
'email' => sprintf(__('The %s is not valid email', 'wp-ultimo'), ':attribute'),
// translators: 1st %s is the field name; 2nd is the allowed value
'min' => sprintf(__('The %1$s minimum is %2$s', 'wp-ultimo'), ':attribute', ':min'),
// translators: 1st %s is the field name; 2nd is the allowed value
'max' => sprintf(__('The %1$s maximum is %2$s', 'wp-ultimo'), ':attribute', ':max'),
// translators: %s is the field identifier
'alpha_dash' => sprintf(__('The %s only allows a-z, 0-9, _ and -', 'wp-ultimo'), ':attribute'),
// translators: %s is the field identifier
'lowercase' => sprintf(__('The %s must be lowercase', 'wp-ultimo'), ':attribute'),
// translators: %s is the field identifier
'integer' => sprintf(__('The %s must be integer', 'wp-ultimo'), ':attribute'),
),
$this
);
$this->validator = new Validator_Helper($validation_error_messages);
$this->validator->setTranslations(array(
'and' => __('and', 'wp-ultimo'),
'or' => __('or', 'wp-ultimo'),
));
$this->validator->setTranslations(
array(
'and' => __('and', 'wp-ultimo'),
'or' => __('or', 'wp-ultimo'),
)
);
$this->validator->addValidator('unique', new Unique);
$this->validator->addValidator('unique_site', new Unique_Site);
$this->validator->addValidator('exists', new Exists);
$this->validator->addValidator('checkout_steps', new Checkout_Steps);
$this->validator->addValidator('price_variations', new Price_Variations);
$this->validator->addValidator('domain', new Domain);
$this->validator->addValidator('site_template', new Site_Template);
$this->validator->addValidator('products', new Products);
$this->validator->addValidator('country', new Country);
$this->validator->addValidator('state', new State);
$this->validator->addValidator('city', new City);
} // end __construct;
$this->validator->addValidator('unique', new Unique());
$this->validator->addValidator('unique_site', new Unique_Site());
$this->validator->addValidator('exists', new Exists());
$this->validator->addValidator('checkout_steps', new Checkout_Steps());
$this->validator->addValidator('price_variations', new Price_Variations());
$this->validator->addValidator('domain', new Domain());
$this->validator->addValidator('site_template', new Site_Template());
$this->validator->addValidator('products', new Products());
$this->validator->addValidator('country', new Country());
$this->validator->addValidator('state', new State());
$this->validator->addValidator('city', new City());
}
/**
* Validates the data passed according to the rules passed.
@ -118,7 +123,7 @@ class Validator {
*/
public function validate($data, $rules = array(), $aliases = array()) {
$this->errors = new \WP_Error;
$this->errors = new \WP_Error();
$this->validation = $this->validator->make($data, $rules);
@ -127,8 +132,7 @@ class Validator {
$this->validation->validate();
return $this;
} // end validate;
}
/**
* Returns true when the validation fails.
@ -139,8 +143,7 @@ class Validator {
public function fails() {
return $this->validation->fails();
} // end fails;
}
/**
* Returns a WP_Error object containing all validation errors.
@ -155,8 +158,7 @@ class Validator {
$this->cast_to_wp_error($errors);
return $this->errors;
} // end get_errors;
}
/**
* Converts the native error structure to a WP_Error object.
@ -169,16 +171,11 @@ class Validator {
protected function cast_to_wp_error($errors) {
foreach ($errors as $key => $error_messages) {
foreach ($error_messages as $error_message) {
$this->errors->add($key, $error_message);
} // end foreach;
} // end foreach;
} // end cast_to_wp_error;
}
}
}
/**
* Get holds an instance of the validation being performed.
@ -189,7 +186,5 @@ class Validator {
public function get_validation() {
return $this->validation;
} // end get_validation;
} // end class Validator;
}
}

View File

@ -22,24 +22,23 @@ class WP_Config {
use \WP_Ultimo\Traits\Singleton;
/**
* Inject the constant into the wp-config.php file.
*
* @since 2.0.0
*
* @param string $constant The name of the constant. e.g. WP_ULTIMO_CONSTANT.
* @param string|int $value The value of that constant.
* @return bool|\WP_Error
*/
public function inject_wp_config_constant($constant, $value) {
* Inject the constant into the wp-config.php file.
*
* @since 2.0.0
*
* @param string $constant The name of the constant. e.g. WP_ULTIMO_CONSTANT.
* @param string|int $value The value of that constant.
* @return bool|\WP_Error
*/
public function inject_wp_config_constant($constant, $value) {
$config_path = $this->get_wp_config_path();
if (!is_writeable($config_path)) {
if ( ! is_writeable($config_path)) {
// translators: %s is the file name.
return new \WP_Error('not-writeable', sprintf(__('The file %s is not writable', 'wp-ultimo'), $config_path));
} // end if;
}
$config = file($config_path);
@ -53,32 +52,24 @@ class WP_Config {
$hook_line = $this->find_reference_hook_line($config);
if ($hook_line === false) {
return new \WP_Error('unknown-wpconfig', __("WP Multisite WaaS can't recognize your wp-config.php, please revert it to original state for further process.", 'wp-ultimo'));
} // end if;
}
$config = $this->inject_contents($config, $hook_line + 1, PHP_EOL . $content . PHP_EOL);
return file_put_contents($config_path, implode('', $config), LOCK_EX);
} else {
list($value, $line) = $line;
if ($value !== true) {
$config[$line] = $content . PHP_EOL;
$config[ $line ] = $content . PHP_EOL;
return file_put_contents($config_path, implode('', $config), LOCK_EX);
} // end if;
} // end if;
}
}
return false;
} // end inject_wp_config_constant;
}
/**
* Actually inserts the new lines into the array of wp-config.php lines.
@ -92,17 +83,14 @@ class WP_Config {
*/
public function inject_contents($content_array, $line, $value) {
if (!is_array($value)) {
if ( ! is_array($value)) {
$value = array($value);
} // end if;
}
array_splice($content_array, $line, 0, $value);
return $content_array;
} // end inject_contents;
}
/**
* Gets the correct path to the wp-config.php file.
@ -113,20 +101,13 @@ class WP_Config {
public function get_wp_config_path() {
if (file_exists(ABSPATH . 'wp-config.php')) {
return (ABSPATH . 'wp-config.php');
} elseif (@file_exists(dirname(ABSPATH) . '/wp-config.php') && !@file_exists(dirname(ABSPATH) . '/wp-settings.php')) {
} elseif (@file_exists(dirname(ABSPATH) . '/wp-config.php') && ! @file_exists(dirname(ABSPATH) . '/wp-settings.php')) {
return (dirname(ABSPATH) . '/wp-config.php');
} elseif (defined('WP_TESTS_MULTISITE') && constant('WP_TESTS_MULTISITE') === true) {
return '/tmp/wordpress-tests-lib/wp-tests-config.php';
} // end if;
} // end get_wp_config_path;
}
}
/**
* Find reference line for injection.
@ -159,34 +140,30 @@ class WP_Config {
* A negative number of lines can be passed to write before the found line,
* instead of writing after it.
*/
$patterns = apply_filters('wu_wp_config_reference_hook_line_patterns', array(
'/^\$table_prefix\s*=\s*[\'|\"]' . $wpdb->prefix . '[\'|\"]/' => 0,
'/^( ){0,}\$table_prefix\s*=.*[\'|\"]' . $wpdb->prefix . '[\'|\"]/' => 0,
'/(\/\* That\'s all, stop editing! Happy publishing\. \*\/)/' => -2,
'/<\?php/' => 0,
));
$patterns = apply_filters(
'wu_wp_config_reference_hook_line_patterns',
array(
'/^\$table_prefix\s*=\s*[\'|\"]' . $wpdb->prefix . '[\'|\"]/' => 0,
'/^( ){0,}\$table_prefix\s*=.*[\'|\"]' . $wpdb->prefix . '[\'|\"]/' => 0,
'/(\/\* That\'s all, stop editing! Happy publishing\. \*\/)/' => -2,
'/<\?php/' => 0,
)
);
$line = 1;
foreach ($patterns as $pattern => $lines_to_add) {
foreach ($config as $k => $line) {
if (preg_match($pattern, (string) $line)) {
$line = $k + $lines_to_add;
break 2;
} // end if;
} // end foreach;
} // end foreach;
}
}
}
return $line;
} // end find_reference_hook_line;
}
/**
* Revert the injection of a constant in wp-config.php
@ -200,23 +177,19 @@ class WP_Config {
$config_path = $this->get_wp_config_path();
if (!is_writeable($config_path)) {
if ( ! is_writeable($config_path)) {
// translators: %s is the file name.
return new \WP_Error('not-writeable', sprintf(__('The file %s is not writable', 'wp-ultimo'), $config_path));
} // end if;
}
$config = file($config_path);
$line = $this->find_injected_line($config, $constant);
if ($line === false) {
return;
} else {
$value = $line[0];
$line = $line[1];
@ -224,41 +197,32 @@ class WP_Config {
if ($value === 'true' || $value === '1') {
// value is true, we will remove this
unset($config[$line]);
unset($config[ $line ]);
// save it
return file_put_contents($config_path, implode('', $config), LOCK_EX);
} // end if;
} // end if;
} // end revert;
/**
* Checks for the injected line inside of the wp-config.php file.
*
* @since 2.0.0
*
* @param array $config Array containing the lines of the config file, for searching.
* @param string $constant The constant name.
* @return mixed[]|bool
*/
public function find_injected_line($config, $constant) {
}
}
}
/**
* Checks for the injected line inside of the wp-config.php file.
*
* @since 2.0.0
*
* @param array $config Array containing the lines of the config file, for searching.
* @param string $constant The constant name.
* @return mixed[]|bool
*/
public function find_injected_line($config, $constant) {
$pattern = "/^define\(\s*['|\"]" . $constant . "['|\"],(.*)\)/";
foreach ($config as $k => $line) {
if (preg_match($pattern, (string) $line, $matches)) {
return array(trim($matches[1]), $k);
} // end if;
} // end foreach;
}
}
return false;
} // end find_injected_line;
} // end class WP_Config;
}
}

View File

@ -50,7 +50,7 @@ class Checkout_Steps extends Rule {
$value = maybe_unserialize($value);
} // end if;
}
$required_fields = Signup_Fields_Manager::get_instance()->get_required_fields();
@ -60,7 +60,7 @@ class Checkout_Steps extends Rule {
return true;
} // end if;
}
$fields = array_column($value, 'fields');
@ -68,7 +68,7 @@ class Checkout_Steps extends Rule {
return true;
} // end if;
}
$all_fields = call_user_func_array('array_merge', $fields);
@ -87,9 +87,9 @@ class Checkout_Steps extends Rule {
return false;
} // end if;
}
} // end if;
}
/**
* Allow developers to bypass the check if a field is auto-submittable.
@ -124,9 +124,9 @@ class Checkout_Steps extends Rule {
return false;
} // end if;
}
} // end foreach;
}
/*
* @todo: Plan, product selection fields must come before the order summary and payment fields.
@ -134,6 +134,6 @@ class Checkout_Steps extends Rule {
return true;
} // end check;
}
} // end class Checkout_Steps;
}

View File

@ -44,21 +44,15 @@ class City extends Rule {
$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)) {
if (! empty($allowed_cities)) {
$check = in_array($city, $allowed_cities, true);
} // end if;
} // end if;
}
}
return $check;
} // end check;
} // end class City;
}
}

View File

@ -35,22 +35,18 @@ class Country extends Rule {
*
* @param mixed $country The country value detected.
*/
public function check($country) : bool { // phpcs:ignore
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

@ -46,7 +46,5 @@ class Domain extends Rule {
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

@ -44,19 +44,19 @@ class Exists extends Rule {
*
* @param mixed $value Value being checked.
*/
public function check($value) : bool {
public function check($value): bool {
$this->requireParameters(array(
'model',
'column'
));
$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;
return (bool) $model::get_by($column, $value);
}
}

View File

@ -49,13 +49,13 @@ class Price_Variations extends Rule {
$value = maybe_unserialize($value);
} // end if;
}
if (!is_array($value)) {
return false;
} // end if;
}
foreach ($value as $price_variation) {
@ -68,7 +68,7 @@ class Price_Variations extends Rule {
return false;
} // end if;
}
/**
* Validation Unit
@ -86,7 +86,7 @@ class Price_Variations extends Rule {
return false;
} // end if;
}
/**
* Check if it is the same as the main duration
@ -97,7 +97,7 @@ class Price_Variations extends Rule {
return false;
} // end if;
}
/**
* Validation Amount
@ -108,18 +108,18 @@ class Price_Variations extends Rule {
$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

@ -43,7 +43,7 @@ class Products extends Rule {
*
* @param mixed $products Value being checked.
*/
public function check($products) : bool { // phpcs:ignore
public function check($products) : bool { // phpcs:ignore
$products = (array) $products;
@ -52,15 +52,11 @@ class Products extends Rule {
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

@ -9,9 +9,9 @@
namespace WP_Ultimo\Helpers\Validation_Rules;
use \Rakit\Validation\Rule;
use \WP_Ultimo\Checkout\Checkout;
use \WP_Ultimo\Database\Sites\Site_Type;
use Rakit\Validation\Rule;
use WP_Ultimo\Checkout\Checkout;
use WP_Ultimo\Database\Sites\Site_Type;
// Exit if accessed directly
defined('ABSPATH') || exit;
@ -45,49 +45,39 @@ class Site_Template extends Rule {
*
* @param mixed $template_id Value being checked.
*/
public function check($template_id) : bool { // phpcs:ignore
public function check($template_id) : bool { // phpcs:ignore
$template_id = absint($template_id);
if (!$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)) {
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')) {
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()) {
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;
@ -96,7 +86,6 @@ class Site_Template extends Rule {
$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();
@ -106,25 +95,19 @@ class Site_Template extends Rule {
$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

@ -35,28 +35,22 @@ class State extends Rule {
*
* @param mixed $state The state value detected.
*/
public function check($state) : bool { // phpcs:ignore
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)) {
if (! empty($allowed_states)) {
$check = in_array($state, $allowed_states, true);
} // end if;
} // end if;
}
}
return $check;
} // end check;
} // end class State;
}
}

View File

@ -43,7 +43,7 @@ class Unique_Site extends Rule {
*
* @param mixed $value Value being checked.
*/
public function check($value) : bool { // phpcs:ignore
public function check($value) : bool { // phpcs:ignore
$this->requireParameters(array());
@ -52,15 +52,11 @@ class Unique_Site extends Rule {
$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

@ -44,12 +44,14 @@ class Unique extends Rule {
*
* @param mixed $value Value being checked.
*/
public function check($value) : bool {
public function check($value): bool {
$this->requireParameters(array(
'model',
'column',
));
$this->requireParameters(
array(
'model',
'column',
)
);
$column = $this->parameter('column');
$model = $this->parameter('model');
@ -76,24 +78,18 @@ class Unique extends Rule {
* 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) {
if ( ! $existing) {
return true;
} // end if;
}
if ( $existing instanceof \WP_User) {
$id = $existing->ID;
} else {
$id = method_exists( $existing, 'get_id' ) ? $existing->get_id() : $existing->id;
$id = method_exists($existing, 'get_id') ? $existing->get_id() : $existing->id;
}
return absint($id) === absint($self_id);
} // end check;
} // end class Unique;
}
}