Use new code style
This commit is contained in:
@ -9,7 +9,7 @@
|
||||
|
||||
namespace WP_Ultimo\Models;
|
||||
|
||||
use \WP_Ultimo\Helpers\Hash;
|
||||
use WP_Ultimo\Helpers\Hash;
|
||||
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
@ -25,11 +25,11 @@ defined('ABSPATH') || exit;
|
||||
abstract class Base_Model implements \JsonSerializable {
|
||||
|
||||
/**
|
||||
* ID of the object
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @var integer
|
||||
*/
|
||||
* ID of the object
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @var integer
|
||||
*/
|
||||
protected $id = 0;
|
||||
|
||||
/**
|
||||
@ -140,20 +140,15 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$this->model = sanitize_key((new \ReflectionClass($this))->getShortName());
|
||||
|
||||
if (is_array($object)) {
|
||||
|
||||
$object = (object) $object;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!is_object($object)) {
|
||||
|
||||
if ( ! is_object($object)) {
|
||||
return;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$this->setup_model($object);
|
||||
|
||||
} // end __construct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of slug
|
||||
@ -163,8 +158,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function get_slug() {
|
||||
|
||||
return $this->slug;
|
||||
|
||||
} // end get_slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of slug
|
||||
@ -174,8 +168,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function set_slug($slug) {
|
||||
|
||||
$this->slug = $slug;
|
||||
|
||||
} // end set_slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a hashed version of the id. Useful for displaying data publicly.
|
||||
@ -188,17 +181,14 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
|
||||
$value = call_user_func(array($this, "get_{$field}"));
|
||||
|
||||
if (!is_numeric($value)) {
|
||||
|
||||
if ( ! is_numeric($value)) {
|
||||
_doing_it_wrong(__METHOD__, __('You can only use numeric fields to generate hashes.', 'wp-ultimo'), '2.0.0');
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return Hash::encode($value, $this->model);
|
||||
|
||||
} // end get_hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup properties.
|
||||
@ -211,25 +201,20 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
private function setup_model($object) {
|
||||
|
||||
if (!is_object($object)) {
|
||||
|
||||
if ( ! is_object($object)) {
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$vars = get_object_vars($object);
|
||||
|
||||
$this->attributes($vars);
|
||||
|
||||
if (empty($this->id)) {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} // end setup_model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the attributes of the model using the setters available.
|
||||
@ -242,45 +227,34 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function attributes($atts) {
|
||||
|
||||
foreach ($atts as $key => $value) {
|
||||
|
||||
if ($key === 'meta' && is_array($value)) {
|
||||
|
||||
$this->meta = is_array($this->meta) ? array_merge($this->meta, $value) : $value;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if (method_exists($this, "set_$key")) {
|
||||
|
||||
call_user_func(array($this, "set_$key"), $value);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$mapping = wu_get_isset($this->_mappings, $key);
|
||||
|
||||
if ($mapping && method_exists($this, "set_$mapping")) {
|
||||
|
||||
call_user_func(array($this, "set_$mapping"), $value);
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Keeps the original.
|
||||
*/
|
||||
if ($this->_original === null) {
|
||||
|
||||
$original = get_object_vars($this);
|
||||
|
||||
unset($original['_original']);
|
||||
|
||||
$this->_original = $original;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
||||
} // end attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the model schema. useful to list all models fields.
|
||||
@ -306,8 +280,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
fn($column) => $column->to_array(),
|
||||
$columns
|
||||
);
|
||||
|
||||
} // end get_schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this model was already saved to the database.
|
||||
@ -317,9 +290,8 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function exists() {
|
||||
|
||||
return !empty($this->id);
|
||||
|
||||
} // end exists;
|
||||
return ! empty($this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a single database row by the primary column ID, possibly from cache.
|
||||
@ -333,18 +305,15 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public static function get_by_id($item_id) {
|
||||
|
||||
if (empty($item_id)) {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$instance = new static();
|
||||
|
||||
$query_class = new $instance->query_class();
|
||||
|
||||
return $query_class->get_item($item_id);
|
||||
|
||||
} // end get_by_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a single database row by the hash, possibly from cache.
|
||||
@ -364,8 +333,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$query_class = new $instance->query_class();
|
||||
|
||||
return $query_class->get_item($item_id);
|
||||
|
||||
} // end get_by_hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a model instance by a column value.
|
||||
@ -383,8 +351,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$query_class = new $instance->query_class();
|
||||
|
||||
return $query_class->get_item_by($column, $value);
|
||||
|
||||
} // end get_by;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for a Query call.
|
||||
@ -399,8 +366,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$instance = new static();
|
||||
|
||||
return (new $instance->query_class($query))->query();
|
||||
|
||||
} // end get_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for a Query call, but returns the list as arrays.
|
||||
@ -417,8 +383,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$list = (new $instance->query_class($query))->query();
|
||||
|
||||
return array_map(fn($item) => $item->to_array(), $list);
|
||||
|
||||
} // end get_items_as_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of the model.
|
||||
@ -430,8 +395,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function get_id() {
|
||||
|
||||
return absint($this->id);
|
||||
|
||||
} // end get_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this model has a job running.
|
||||
@ -441,16 +405,17 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function has_running_jobs() {
|
||||
|
||||
$jobs = wu_get_scheduled_actions(array(
|
||||
'status' => \ActionScheduler_Store::STATUS_RUNNING,
|
||||
'args' => array(
|
||||
"{$this->model}_id" => $this->get_id(),
|
||||
),
|
||||
));
|
||||
$jobs = wu_get_scheduled_actions(
|
||||
array(
|
||||
'status' => \ActionScheduler_Store::STATUS_RUNNING,
|
||||
'args' => array(
|
||||
"{$this->model}_id" => $this->get_id(),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
return $jobs;
|
||||
|
||||
} // end has_running_jobs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set iD of the object.
|
||||
@ -462,8 +427,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
private function set_id($id) {
|
||||
|
||||
$this->id = $id;
|
||||
|
||||
} // end set_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the validation rules for this particular model.
|
||||
@ -478,8 +442,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function validation_rules() {
|
||||
|
||||
return array();
|
||||
|
||||
} // end validation_rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the rules and make sure we only save models when necessary.
|
||||
@ -490,30 +453,23 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function validate() {
|
||||
|
||||
if ($this->skip_validation) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
foreach ($validator->get_validation()->getValidData() as $key => $value) {
|
||||
|
||||
$this->{$key} = $value;
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} // end validate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save (create or update) the model on the database.
|
||||
@ -529,10 +485,8 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$data = get_object_vars($this);
|
||||
|
||||
if (isset($data['id']) && empty($data['id'])) {
|
||||
|
||||
unset($data['id']);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
unset($data['_original']);
|
||||
|
||||
@ -540,7 +494,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
|
||||
$meta = wu_get_isset($data, 'meta', array());
|
||||
|
||||
$new = !$this->exists();
|
||||
$new = ! $this->exists();
|
||||
|
||||
/**
|
||||
* Filters the data meta before it is serialized to be stored into the database.
|
||||
@ -559,26 +513,24 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
);
|
||||
|
||||
foreach ($blocked_attributes as $attribute) {
|
||||
|
||||
unset($data[$attribute]);
|
||||
|
||||
} // end foreach;
|
||||
unset($data[ $attribute ]);
|
||||
}
|
||||
|
||||
$this->validate();
|
||||
|
||||
$data = array_map('maybe_serialize', $data);
|
||||
|
||||
$data = array_map(function($_data) {
|
||||
$data = array_map(
|
||||
function ($_data) {
|
||||
|
||||
if (is_serialized($_data)) {
|
||||
if (is_serialized($_data)) {
|
||||
$_data = addslashes($_data);
|
||||
}
|
||||
|
||||
$_data = addslashes($_data);
|
||||
|
||||
} // end if;
|
||||
|
||||
return $_data;
|
||||
|
||||
}, $data);
|
||||
return $_data;
|
||||
},
|
||||
$data
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the object data before it is stored into the database.
|
||||
@ -593,39 +545,29 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
|
||||
$is_valid_data = $this->validate();
|
||||
|
||||
if (is_wp_error($is_valid_data) && !$this->skip_validation) {
|
||||
|
||||
if (is_wp_error($is_valid_data) && ! $this->skip_validation) {
|
||||
return $is_valid_data;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$saved = false;
|
||||
|
||||
if (!$this->get_id()) {
|
||||
|
||||
if ( ! $this->get_id()) {
|
||||
$new_id = $query_class->add_item($data);
|
||||
|
||||
if ($new_id) {
|
||||
|
||||
$this->id = $new_id;
|
||||
|
||||
$saved = true;
|
||||
|
||||
} // end if;
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
$saved = $query_class->update_item($this->get_id(), $data);
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!empty($meta)) {
|
||||
|
||||
if ( ! empty($meta)) {
|
||||
$this->update_meta_batch($meta);
|
||||
|
||||
$saved = true;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete object cache to prevent errors.
|
||||
@ -658,8 +600,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
do_action("wu_{$this->model}_post_save", $data, $this, $new);
|
||||
|
||||
return $saved;
|
||||
|
||||
} // end save;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the model from the database.
|
||||
@ -670,11 +611,9 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function delete() {
|
||||
|
||||
if (!$this->get_id()) {
|
||||
|
||||
if ( ! $this->get_id()) {
|
||||
return new \WP_Error("wu_{$this->model}_delete_unsaved_item", __('Item not found.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after an object is stored into the database.
|
||||
@ -700,8 +639,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
do_action("wu_{$this->model}_post_delete", $result, $this);
|
||||
|
||||
return $result;
|
||||
|
||||
} // end delete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the meta type name.
|
||||
@ -715,14 +653,13 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$query_class = new $this->query_class();
|
||||
|
||||
// Maybe apply table prefix
|
||||
$table = !empty($query_class->prefix)
|
||||
$table = ! empty($query_class->prefix)
|
||||
? "{$query_class->prefix}_{$query_class->item_name}"
|
||||
: $query_class->item_name;
|
||||
|
||||
// Return table if exists, or false if not
|
||||
return $table;
|
||||
|
||||
} // end get_meta_type_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the meta table name.
|
||||
@ -736,8 +673,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$table = $this->get_meta_type_name();
|
||||
|
||||
return _get_meta_table($table);
|
||||
|
||||
} // end get_meta_table_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if metadata handling is available, i.e., if there is a meta table
|
||||
@ -748,25 +684,22 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
protected function is_meta_available() {
|
||||
|
||||
if (!$this->get_meta_table_name()) {
|
||||
if ( ! $this->get_meta_table_name()) {
|
||||
|
||||
// _doing_it_wrong(__METHOD__, __('This model does not support metadata.', 'wp-ultimo'), '2.0.0');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!$this->get_id() && !$this->_mocked) {
|
||||
if ( ! $this->get_id() && ! $this->_mocked) {
|
||||
|
||||
// _doing_it_wrong(__METHOD__, __('Model metadata only works for already saved models.', 'wp-ultimo'), '2.0.0');
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} // end is_meta_available;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the meta data, if set. Otherwise, returns the default.
|
||||
@ -780,23 +713,18 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function get_meta($key, $default = false, $single = true) {
|
||||
|
||||
if (!$this->is_meta_available()) {
|
||||
|
||||
if ( ! $this->is_meta_available()) {
|
||||
return $default;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$meta_type = $this->get_meta_type_name();
|
||||
|
||||
if (metadata_exists($meta_type, $this->get_id(), $key)) {
|
||||
|
||||
return get_metadata($meta_type, $this->get_id(), $key, $single);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $default;
|
||||
|
||||
} // end get_meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds or updates meta data in batch.
|
||||
@ -808,33 +736,26 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function update_meta_batch($meta) {
|
||||
|
||||
if (!$this->is_meta_available()) {
|
||||
|
||||
if ( ! $this->is_meta_available()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!is_array($meta)) {
|
||||
|
||||
if ( ! is_array($meta)) {
|
||||
_doing_it_wrong(__METHOD__, __('This method expects an array as argument.', 'wp-ultimo'), '2.0.0');
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$meta_type = $this->get_meta_type_name();
|
||||
|
||||
$success = true;
|
||||
|
||||
foreach ($meta as $key => $value) {
|
||||
|
||||
update_metadata($meta_type, $this->get_id(), $key, $value);
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
|
||||
return $success;
|
||||
|
||||
} // end update_meta_batch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds or updates the meta data.
|
||||
@ -848,17 +769,14 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function update_meta($key, $value) {
|
||||
|
||||
if (!$this->is_meta_available()) {
|
||||
|
||||
if ( ! $this->is_meta_available()) {
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$meta_type = $this->get_meta_type_name();
|
||||
|
||||
return update_metadata($meta_type, $this->get_id(), $key, $value);
|
||||
|
||||
} // end update_meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the meta data.
|
||||
@ -870,17 +788,14 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function delete_meta($key) {
|
||||
|
||||
if (!$this->is_meta_available()) {
|
||||
|
||||
if ( ! $this->is_meta_available()) {
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$meta_type = $this->get_meta_type_name();
|
||||
|
||||
return delete_metadata($meta_type, $this->get_id(), $key);
|
||||
|
||||
} // end delete_meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries object in the database.
|
||||
@ -899,8 +814,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$items = $query_class->query($args);
|
||||
|
||||
return $items;
|
||||
|
||||
} // end query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the object into an assoc array.
|
||||
@ -921,18 +835,13 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
unset($array['_mocked']);
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
|
||||
if (strncmp('_', $key, strlen($key)) === 0) {
|
||||
|
||||
unset($array[$key]);
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
unset($array[ $key ]);
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
|
||||
} // end to_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert data to Mapping instance
|
||||
@ -945,8 +854,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
protected static function to_instance($data) {
|
||||
|
||||
return new static($data);
|
||||
|
||||
} // end to_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert list of data to Mapping instances
|
||||
@ -957,8 +865,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
protected static function to_instances($data) {
|
||||
|
||||
return array_map(array(get_called_class(), 'to_instance'), $data);
|
||||
|
||||
} // end to_instances;
|
||||
}
|
||||
|
||||
/**
|
||||
* By default, we just use the to_array method, but you can rewrite this.
|
||||
@ -969,8 +876,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function to_search_results() {
|
||||
|
||||
return $this->to_array();
|
||||
|
||||
} // end to_search_results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines how we should encode this.
|
||||
@ -982,14 +888,11 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function jsonSerialize() {
|
||||
|
||||
if (wp_doing_ajax() && wu_request('action') === 'wu_search') {
|
||||
|
||||
return $this->to_search_results();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->to_array();
|
||||
|
||||
} // end jsonSerialize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date when this model was created.
|
||||
@ -999,15 +902,12 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function get_date_created() {
|
||||
|
||||
if (!wu_validate_date($this->date_created)) {
|
||||
|
||||
if ( ! wu_validate_date($this->date_created)) {
|
||||
return wu_get_current_time('mysql');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->date_created;
|
||||
|
||||
} // end get_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date when this model was last modified.
|
||||
@ -1017,15 +917,12 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function get_date_modified() {
|
||||
|
||||
if (!wu_validate_date($this->date_modified)) {
|
||||
|
||||
if ( ! wu_validate_date($this->date_modified)) {
|
||||
return wu_get_current_time('mysql');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->date_modified;
|
||||
|
||||
} // end get_date_modified;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set model creation date.
|
||||
@ -1037,8 +934,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function set_date_created($date_created) {
|
||||
|
||||
$this->date_created = $date_created;
|
||||
|
||||
} // end set_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set model last modification date.
|
||||
@ -1050,8 +946,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function set_date_modified($date_modified) {
|
||||
|
||||
$this->date_modified = $date_modified;
|
||||
|
||||
} // end set_date_modified;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the id of the original 1.X model that was used to generate this item on migration.
|
||||
@ -1062,8 +957,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function get_migrated_from_id() {
|
||||
|
||||
return $this->migrated_from_id;
|
||||
|
||||
} // end get_migrated_from_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the id of the original 1.X model that was used to generate this item on migration.
|
||||
@ -1075,8 +969,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function set_migrated_from_id($migrated_from_id) {
|
||||
|
||||
$this->migrated_from_id = absint($migrated_from_id);
|
||||
|
||||
} // end set_migrated_from_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this model is a migration from 1.X.
|
||||
@ -1086,9 +979,8 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
*/
|
||||
public function is_migrated() {
|
||||
|
||||
return !empty($this->get_migrated_from_id());
|
||||
|
||||
} // end is_migrated;
|
||||
return ! empty($this->get_migrated_from_id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to return formatted values.
|
||||
@ -1106,14 +998,11 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$value = (float) $this->{"get_{$key}"}();
|
||||
|
||||
if (is_numeric($value)) {
|
||||
|
||||
return wu_format_currency($value);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
||||
} // end get_formatted_amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to return formatted dates.
|
||||
@ -1131,14 +1020,11 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$value = $this->{"get_{$key}"}();
|
||||
|
||||
if (wu_validate_date($value)) {
|
||||
|
||||
return date_i18n(get_option('date_format'), wu_date($value)->format('U'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
||||
} // end get_formatted_date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all items.
|
||||
@ -1157,8 +1043,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$items = $query_class->query($query_args);
|
||||
|
||||
return $items;
|
||||
|
||||
} // end get_all;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the given model adn resets it's id to a 'new' state.
|
||||
@ -1175,14 +1060,11 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
$clone->set_id(0);
|
||||
|
||||
if (method_exists($clone, 'set_date_created')) {
|
||||
|
||||
$clone->set_date_created(wu_get_current_time('mysql'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $clone;
|
||||
|
||||
} // end duplicate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the data the resides on meta tables.
|
||||
@ -1198,7 +1080,6 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
unset($attributes['meta']);
|
||||
|
||||
foreach ($attributes as $attribute => $maybe_null) {
|
||||
|
||||
$possible_setters = array(
|
||||
"get_{$attribute}",
|
||||
"is_{$attribute}",
|
||||
@ -1207,21 +1088,16 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
foreach ($possible_setters as $setter) {
|
||||
$setter = method_exists($this, $setter) ? $setter : '';
|
||||
|
||||
if (!$setter || !method_exists($this, "set_{$attribute}")) {
|
||||
|
||||
if ( ! $setter || ! method_exists($this, "set_{$attribute}")) {
|
||||
continue;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$value = $this->{$setter}();
|
||||
|
||||
$this->{"set_{$attribute}"}($value);
|
||||
|
||||
} // end foreach;
|
||||
|
||||
} // end foreach;
|
||||
|
||||
} // end hydrate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set set this to true to skip validations when saving..
|
||||
@ -1233,8 +1109,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function set_skip_validation($skip_validation = false) {
|
||||
|
||||
$this->skip_validation = $skip_validation;
|
||||
|
||||
} // end set_skip_validation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the original parameters of the object.
|
||||
@ -1245,8 +1120,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function _get_original() {
|
||||
|
||||
return $this->_original;
|
||||
|
||||
} // end _get_original;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks this model.
|
||||
@ -1257,8 +1131,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function lock() {
|
||||
|
||||
return $this->update_meta('wu_lock', true);
|
||||
|
||||
} // end lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check ths lock status of the model.
|
||||
@ -1269,8 +1142,7 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function is_locked() {
|
||||
|
||||
return $this->get_meta('wu_lock', false);
|
||||
|
||||
} // end is_locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks the model.
|
||||
@ -1281,7 +1153,5 @@ abstract class Base_Model implements \JsonSerializable {
|
||||
public function unlock() {
|
||||
|
||||
return $this->delete_meta('wu_lock');
|
||||
|
||||
} // end unlock;
|
||||
|
||||
} // end class Base_Model;
|
||||
}
|
||||
}
|
||||
|
@ -88,15 +88,12 @@ class Broadcast extends Post_Base_Model {
|
||||
|
||||
$object = (array) $object;
|
||||
|
||||
if (!wu_get_isset($object, 'migrated_from_id')) {
|
||||
|
||||
if ( ! wu_get_isset($object, 'migrated_from_id')) {
|
||||
unset($object['migrated_from_id']);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
parent::__construct($object);
|
||||
|
||||
} // end __construct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the validation rules for this particular model.
|
||||
@ -118,8 +115,7 @@ class Broadcast extends Post_Base_Model {
|
||||
'content' => 'required|min:3',
|
||||
'type' => 'required|in:broadcast_email,broadcast_notice|default:broadcast_notice',
|
||||
);
|
||||
|
||||
} // end validation_rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the id of the original 1.X model that was used to generate this item on migration.
|
||||
@ -130,14 +126,11 @@ class Broadcast extends Post_Base_Model {
|
||||
public function get_migrated_from_id() {
|
||||
|
||||
if ($this->migrated_from_id === null) {
|
||||
|
||||
$this->migrated_from_id = $this->get_meta('migrated_from_id', 0);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->migrated_from_id;
|
||||
|
||||
} // end get_migrated_from_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the id of the original 1.X model that was used to generate this item on migration.
|
||||
@ -151,8 +144,7 @@ class Broadcast extends Post_Base_Model {
|
||||
$this->meta['migrated_from_id'] = $migrated_from_id;
|
||||
|
||||
$this->migrated_from_id = $this->meta['migrated_from_id'];
|
||||
|
||||
} // end set_migrated_from_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of the broadcast
|
||||
@ -163,8 +155,7 @@ class Broadcast extends Post_Base_Model {
|
||||
public function get_name() {
|
||||
|
||||
return $this->get_title();
|
||||
|
||||
} // end get_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get title of the broadcast
|
||||
@ -175,8 +166,7 @@ class Broadcast extends Post_Base_Model {
|
||||
public function get_title() {
|
||||
|
||||
return $this->title;
|
||||
|
||||
} // end get_title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get notice type
|
||||
@ -187,14 +177,11 @@ class Broadcast extends Post_Base_Model {
|
||||
public function get_notice_type() {
|
||||
|
||||
if ($this->notice_type === null) {
|
||||
|
||||
$this->notice_type = $this->get_meta('notice_type', 'success');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->notice_type;
|
||||
|
||||
} // end get_notice_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message targets.
|
||||
@ -205,8 +192,7 @@ class Broadcast extends Post_Base_Model {
|
||||
public function get_message_targets() {
|
||||
|
||||
return $this->get_meta('message_targets');
|
||||
|
||||
} // end get_message_targets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message product and/or customer targets.
|
||||
@ -219,8 +205,7 @@ class Broadcast extends Post_Base_Model {
|
||||
public function set_message_targets($message_targets) {
|
||||
|
||||
$this->meta['message_targets'] = $message_targets;
|
||||
|
||||
} // end set_message_targets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the type of the notice.
|
||||
@ -236,8 +221,7 @@ class Broadcast extends Post_Base_Model {
|
||||
$this->meta['notice_type'] = $notice_type;
|
||||
|
||||
$this->notice_type = $this->meta['notice_type'];
|
||||
|
||||
} // end set_notice_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set title using the name parameter.
|
||||
@ -250,8 +234,7 @@ class Broadcast extends Post_Base_Model {
|
||||
public function set_name($name) {
|
||||
|
||||
$this->set_title($name);
|
||||
|
||||
} // end set_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds checks to prevent saving the model with the wrong type.
|
||||
@ -263,15 +246,12 @@ class Broadcast extends Post_Base_Model {
|
||||
*/
|
||||
public function set_type($type) {
|
||||
|
||||
if (!in_array($type, $this->allowed_types, true)) {
|
||||
|
||||
if ( ! in_array($type, $this->allowed_types, true)) {
|
||||
$type = 'broadcast_notice';
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$this->type = $type;
|
||||
|
||||
} // end set_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* * Adds checks to prevent saving the model with the wrong status.
|
||||
@ -283,14 +263,10 @@ class Broadcast extends Post_Base_Model {
|
||||
*/
|
||||
public function set_status($status) {
|
||||
|
||||
if (!in_array($status, $this->allowed_status, true)) {
|
||||
|
||||
if ( ! in_array($status, $this->allowed_status, true)) {
|
||||
$status = 'publish';
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$this->status = $status;
|
||||
|
||||
} // end set_status;
|
||||
|
||||
} // end class Broadcast;
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -24,7 +24,8 @@ defined('ABSPATH') || exit;
|
||||
*/
|
||||
class Customer extends Base_Model {
|
||||
|
||||
use Traits\Billable, Traits\Notable;
|
||||
use Traits\Billable;
|
||||
use Traits\Notable;
|
||||
|
||||
/**
|
||||
* User ID of the associated user.
|
||||
@ -136,8 +137,7 @@ class Customer extends Base_Model {
|
||||
public function save() {
|
||||
|
||||
return parent::save();
|
||||
|
||||
} // end save;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the validation rules for this particular model.
|
||||
@ -164,8 +164,7 @@ class Customer extends Base_Model {
|
||||
'extra_information' => 'default:',
|
||||
'signup_form' => 'default:',
|
||||
);
|
||||
|
||||
} // end validation_rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user ID of the associated user.
|
||||
@ -176,8 +175,7 @@ class Customer extends Base_Model {
|
||||
public function get_user_id() {
|
||||
|
||||
return absint($this->user_id);
|
||||
|
||||
} // end get_user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user ID of the associated user.
|
||||
@ -189,8 +187,7 @@ class Customer extends Base_Model {
|
||||
public function set_user_id($user_id) {
|
||||
|
||||
$this->user_id = $user_id;
|
||||
|
||||
} // end set_user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user associated with this customer.
|
||||
@ -201,8 +198,7 @@ class Customer extends Base_Model {
|
||||
public function get_user() {
|
||||
|
||||
return get_user_by('id', $this->get_user_id());
|
||||
|
||||
} // end get_user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the customer's display name.
|
||||
@ -215,14 +211,11 @@ class Customer extends Base_Model {
|
||||
$user = $this->get_user();
|
||||
|
||||
if (empty($user)) {
|
||||
|
||||
return __('User Deleted', 'wp-ultimo');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $user->display_name;
|
||||
|
||||
} // end get_display_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default billing address.
|
||||
@ -235,13 +228,14 @@ class Customer extends Base_Model {
|
||||
*/
|
||||
public function get_default_billing_address() {
|
||||
|
||||
return new \WP_Ultimo\Objects\Billing_Address(array(
|
||||
'company_name' => $this->get_display_name(),
|
||||
'billing_email' => $this->get_email_address(),
|
||||
'billing_country' => $this->get_meta('ip_country'),
|
||||
));
|
||||
|
||||
} // end get_default_billing_address;
|
||||
return new \WP_Ultimo\Objects\Billing_Address(
|
||||
array(
|
||||
'company_name' => $this->get_display_name(),
|
||||
'billing_email' => $this->get_email_address(),
|
||||
'billing_country' => $this->get_meta('ip_country'),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the customer country.
|
||||
@ -255,15 +249,12 @@ class Customer extends Base_Model {
|
||||
|
||||
$country = $billing_address->billing_country;
|
||||
|
||||
if (!$country) {
|
||||
|
||||
if ( ! $country) {
|
||||
return $this->get_meta('ip_country');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $country;
|
||||
|
||||
} // end get_country;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the customer's username.
|
||||
@ -276,14 +267,11 @@ class Customer extends Base_Model {
|
||||
$user = $this->get_user();
|
||||
|
||||
if (empty($user)) {
|
||||
|
||||
return __('none', 'wp-ultimo');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $user->user_login;
|
||||
|
||||
} // end get_username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the customer's email address.
|
||||
@ -296,14 +284,11 @@ class Customer extends Base_Model {
|
||||
$user = $this->get_user();
|
||||
|
||||
if (empty($user)) {
|
||||
|
||||
return __('none', 'wp-ultimo');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $user->user_email;
|
||||
|
||||
} // end get_email_address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get date when the customer was created.
|
||||
@ -315,8 +300,7 @@ class Customer extends Base_Model {
|
||||
public function get_date_registered($formatted = true) {
|
||||
|
||||
return $this->date_registered;
|
||||
|
||||
} // end get_date_registered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set date when the customer was created.
|
||||
@ -328,8 +312,7 @@ class Customer extends Base_Model {
|
||||
public function set_date_registered($date_registered) {
|
||||
|
||||
$this->date_registered = $date_registered;
|
||||
|
||||
} // end set_date_registered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get email verification status - either `none`, `pending`, or `verified`.
|
||||
@ -340,8 +323,7 @@ class Customer extends Base_Model {
|
||||
public function get_email_verification() {
|
||||
|
||||
return $this->email_verification;
|
||||
|
||||
} // end get_email_verification;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set email verification status - either `none`, `pending`, or `verified`.
|
||||
@ -353,8 +335,7 @@ class Customer extends Base_Model {
|
||||
public function set_email_verification($email_verification) {
|
||||
|
||||
$this->email_verification = $email_verification;
|
||||
|
||||
} // end set_email_verification;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get date this customer last logged in.
|
||||
@ -366,8 +347,7 @@ class Customer extends Base_Model {
|
||||
public function get_last_login($formatted = true) {
|
||||
|
||||
return $this->last_login;
|
||||
|
||||
} // end get_last_login;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set date this customer last logged in.
|
||||
@ -379,8 +359,7 @@ class Customer extends Base_Model {
|
||||
public function set_last_login($last_login) {
|
||||
|
||||
$this->last_login = $last_login;
|
||||
|
||||
} // end set_last_login;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether or not the customer has trialed before.
|
||||
@ -391,35 +370,30 @@ class Customer extends Base_Model {
|
||||
public function has_trialed() {
|
||||
|
||||
if ((bool) $this->has_trialed) {
|
||||
|
||||
return true;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$this->has_trialed = $this->get_meta('wu_has_trialed');
|
||||
|
||||
if (!$this->has_trialed) {
|
||||
|
||||
$trial = wu_get_memberships(array(
|
||||
'customer_id' => $this->get_id(),
|
||||
'date_trial_end__not_in' => array(null, '0000-00-00 00:00:00'),
|
||||
'fields' => 'ids',
|
||||
'number' => 1,
|
||||
));
|
||||
|
||||
if (!empty($trial)) {
|
||||
if ( ! $this->has_trialed) {
|
||||
$trial = wu_get_memberships(
|
||||
array(
|
||||
'customer_id' => $this->get_id(),
|
||||
'date_trial_end__not_in' => array(null, '0000-00-00 00:00:00'),
|
||||
'fields' => 'ids',
|
||||
'number' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! empty($trial)) {
|
||||
$this->update_meta('wu_has_trialed', true);
|
||||
|
||||
$this->has_trialed = true;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->has_trialed;
|
||||
|
||||
} // end has_trialed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether or not the customer has trialed before.
|
||||
@ -433,8 +407,7 @@ class Customer extends Base_Model {
|
||||
$this->meta['wu_has_trialed'] = $has_trialed;
|
||||
|
||||
$this->has_trialed = $has_trialed;
|
||||
|
||||
} // end set_has_trialed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if this customer is a VIP customer or not.
|
||||
@ -445,8 +418,7 @@ class Customer extends Base_Model {
|
||||
public function is_vip() {
|
||||
|
||||
return (bool) $this->vip;
|
||||
|
||||
} // end is_vip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if this customer is a VIP customer or not.
|
||||
@ -458,8 +430,7 @@ class Customer extends Base_Model {
|
||||
public function set_vip($vip) {
|
||||
|
||||
$this->vip = $vip;
|
||||
|
||||
} // end set_vip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of IP addresses used by this customer.
|
||||
@ -470,20 +441,15 @@ class Customer extends Base_Model {
|
||||
public function get_ips() {
|
||||
|
||||
if (empty($this->ips)) {
|
||||
|
||||
return array();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if (is_string($this->ips)) {
|
||||
|
||||
$this->ips = maybe_unserialize($this->ips);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->ips;
|
||||
|
||||
} // end get_ips;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last IP address recorded for the customer.
|
||||
@ -496,8 +462,7 @@ class Customer extends Base_Model {
|
||||
$ips = $this->get_ips();
|
||||
|
||||
return array_pop($ips);
|
||||
|
||||
} // end get_last_ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set list of IP addresses used by this customer.
|
||||
@ -509,14 +474,11 @@ class Customer extends Base_Model {
|
||||
public function set_ips($ips) {
|
||||
|
||||
if (is_string($ips)) {
|
||||
|
||||
$ips = maybe_unserialize(wp_unslash($ips));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$this->ips = $ips;
|
||||
|
||||
} // end set_ips;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new IP to the IP list.
|
||||
@ -530,26 +492,21 @@ class Customer extends Base_Model {
|
||||
|
||||
$ips = $this->get_ips();
|
||||
|
||||
if (!is_array($ips)) {
|
||||
|
||||
if ( ! is_array($ips)) {
|
||||
$ips = array();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* IP already exists.
|
||||
*/
|
||||
if (in_array($ip, $ips, true)) {
|
||||
|
||||
return;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$ips[] = sanitize_text_field($ip);
|
||||
|
||||
$this->set_ips($ips);
|
||||
|
||||
} // end add_ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the last login, as well as the ip and country if necessary.
|
||||
@ -562,28 +519,25 @@ class Customer extends Base_Model {
|
||||
*/
|
||||
public function update_last_login($update_ip = true, $update_country_and_state = false) {
|
||||
|
||||
$this->attributes(array(
|
||||
'last_login' => wu_get_current_time('mysql', true),
|
||||
));
|
||||
$this->attributes(
|
||||
array(
|
||||
'last_login' => wu_get_current_time('mysql', true),
|
||||
)
|
||||
);
|
||||
|
||||
$geolocation = $update_ip || $update_country_and_state ? \WP_Ultimo\Geolocation::geolocate_ip('', true) : false;
|
||||
|
||||
if ($update_ip) {
|
||||
|
||||
$this->add_ip($geolocation['ip']);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if ($update_country_and_state) {
|
||||
|
||||
$this->update_meta('ip_country', $geolocation['country']);
|
||||
$this->update_meta('ip_state', $geolocation['state']);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->save();
|
||||
|
||||
} // end update_last_login;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get extra information.
|
||||
@ -594,16 +548,13 @@ class Customer extends Base_Model {
|
||||
public function get_extra_information() {
|
||||
|
||||
if ($this->extra_information === null) {
|
||||
|
||||
$extra_information = (array) $this->get_meta('wu_customer_extra_information');
|
||||
|
||||
$this->extra_information = array_filter($extra_information);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->extra_information;
|
||||
|
||||
} // end get_extra_information;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set featured extra information.
|
||||
@ -618,8 +569,7 @@ class Customer extends Base_Model {
|
||||
|
||||
$this->extra_information = $extra_information;
|
||||
$this->meta['wu_customer_extra_information'] = $extra_information;
|
||||
|
||||
} // end set_extra_information;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the subscriptions attached to this customer.
|
||||
@ -629,11 +579,12 @@ class Customer extends Base_Model {
|
||||
*/
|
||||
public function get_memberships() {
|
||||
|
||||
return Membership::query(array(
|
||||
'customer_id' => $this->get_id(),
|
||||
));
|
||||
|
||||
} // end get_memberships;
|
||||
return Membership::query(
|
||||
array(
|
||||
'customer_id' => $this->get_id(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sites attached to this customer.
|
||||
@ -657,8 +608,7 @@ class Customer extends Base_Model {
|
||||
);
|
||||
|
||||
return Site::query($query_args);
|
||||
|
||||
} // end get_sites;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all pending sites associated with a customer.
|
||||
@ -673,20 +623,15 @@ class Customer extends Base_Model {
|
||||
$memberships = $this->get_memberships();
|
||||
|
||||
foreach ($memberships as $membership) {
|
||||
|
||||
$pending_site = $membership->get_pending_site();
|
||||
|
||||
if ($pending_site) {
|
||||
|
||||
$pending_sites[] = $pending_site;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
}
|
||||
|
||||
return $pending_sites;
|
||||
|
||||
} // end get_pending_sites;
|
||||
}
|
||||
|
||||
/**
|
||||
* The the primary site ID if available.
|
||||
@ -703,17 +648,14 @@ class Customer extends Base_Model {
|
||||
|
||||
$primary_site_id = get_user_option('primary_blog', $this->get_user_id());
|
||||
|
||||
if (!$primary_site_id) {
|
||||
|
||||
if ( ! $primary_site_id) {
|
||||
$sites = $this->get_sites();
|
||||
|
||||
$primary_site_id = $sites ? $sites[0]->get_id() : wu_get_main_site_id();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $primary_site_id;
|
||||
|
||||
} // end get_primary_site_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the payments attached to this customer.
|
||||
@ -723,11 +665,12 @@ class Customer extends Base_Model {
|
||||
*/
|
||||
public function get_payments() {
|
||||
|
||||
return Payment::query(array(
|
||||
'customer_id' => $this->get_id(),
|
||||
));
|
||||
|
||||
} // end get_payments;
|
||||
return Payment::query(
|
||||
array(
|
||||
'customer_id' => $this->get_id(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* By default, we just use the to_array method, but you can rewrite this.
|
||||
@ -740,32 +683,33 @@ class Customer extends Base_Model {
|
||||
$user = get_userdata($this->get_user_id());
|
||||
|
||||
if (isset($this->_user)) {
|
||||
|
||||
$user = $this->_user; // Allows for injection, which is useful for mocking.
|
||||
|
||||
unset($this->_user);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$search_result = $this->to_array();
|
||||
|
||||
if ($user) {
|
||||
|
||||
$user->data->avatar = get_avatar($user->data->user_email, 40, 'identicon', '', array(
|
||||
'force_display' => true,
|
||||
'class' => 'wu-rounded-full wu-mr-3',
|
||||
));
|
||||
$user->data->avatar = get_avatar(
|
||||
$user->data->user_email,
|
||||
40,
|
||||
'identicon',
|
||||
'',
|
||||
array(
|
||||
'force_display' => true,
|
||||
'class' => 'wu-rounded-full wu-mr-3',
|
||||
)
|
||||
);
|
||||
|
||||
$search_result = array_merge((array) $user->data, $search_result);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$search_result['billing_address_data'] = $this->get_billing_address()->to_array();
|
||||
$search_result['billing_address'] = $this->get_billing_address()->to_string();
|
||||
|
||||
return $search_result;
|
||||
|
||||
} // end to_search_results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the customer type.
|
||||
@ -776,8 +720,7 @@ class Customer extends Base_Model {
|
||||
public function get_type() {
|
||||
|
||||
return $this->type;
|
||||
|
||||
} // end get_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the customer type.
|
||||
@ -790,8 +733,7 @@ class Customer extends Base_Model {
|
||||
public function set_type($type) {
|
||||
|
||||
$this->type = $type;
|
||||
|
||||
} // end set_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the total grossed by the customer so far.
|
||||
@ -806,19 +748,16 @@ class Customer extends Base_Model {
|
||||
static $sum;
|
||||
|
||||
if ($sum === null) {
|
||||
|
||||
$sum = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT SUM(total) FROM {$wpdb->base_prefix}wu_payments WHERE parent_id = 0 AND customer_id = %d",
|
||||
$this->get_id()
|
||||
)
|
||||
);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $sum;
|
||||
|
||||
} // end get_total_grossed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if the customer is online or not.
|
||||
@ -829,10 +768,8 @@ class Customer extends Base_Model {
|
||||
public function is_online() {
|
||||
|
||||
if ($this->get_last_login() === '0000-00-00 00:00:00') {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$last_login_date = new \DateTime($this->get_last_login());
|
||||
$now = new \DateTime('now');
|
||||
@ -843,8 +780,7 @@ class Customer extends Base_Model {
|
||||
$minutes_interval += $interval->i;
|
||||
|
||||
return $minutes_interval <= apply_filters('wu_is_online_minutes_interval', 3) ? true : false;
|
||||
|
||||
} // end is_online;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a verification key.
|
||||
@ -859,19 +795,17 @@ class Customer extends Base_Model {
|
||||
$hash = \WP_Ultimo\Helpers\Hash::encode($seed, 'verification-key');
|
||||
|
||||
return $this->update_meta('wu_verification_key', $hash);
|
||||
|
||||
} // end generate_verification_key;
|
||||
/**
|
||||
* Returns the saved verification key.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return string|bool
|
||||
*/
|
||||
public function get_verification_key() {
|
||||
}
|
||||
/**
|
||||
* Returns the saved verification key.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return string|bool
|
||||
*/
|
||||
public function get_verification_key() {
|
||||
|
||||
return $this->get_meta('wu_verification_key', false);
|
||||
|
||||
} // end get_verification_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disabled the verification by setting the key to false.
|
||||
@ -882,30 +816,29 @@ class Customer extends Base_Model {
|
||||
public function disable_verification_key() {
|
||||
|
||||
return $this->update_meta('wu_verification_key', false);
|
||||
|
||||
} // end disable_verification_key;
|
||||
/**
|
||||
* Returns the link of the email verification endpoint.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return string|bool
|
||||
*/
|
||||
public function get_verification_url() {
|
||||
}
|
||||
/**
|
||||
* Returns the link of the email verification endpoint.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return string|bool
|
||||
*/
|
||||
public function get_verification_url() {
|
||||
|
||||
$key = $this->get_verification_key();
|
||||
|
||||
if (!$key) {
|
||||
|
||||
if ( ! $key) {
|
||||
return get_site_url(wu_get_main_site_id());
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
return add_query_arg(array(
|
||||
'email-verification-key' => $key,
|
||||
'customer' => $this->get_hash(),
|
||||
), get_site_url(wu_get_main_site_id()));
|
||||
|
||||
} // end get_verification_url;
|
||||
return add_query_arg(
|
||||
array(
|
||||
'email-verification-key' => $key,
|
||||
'customer' => $this->get_hash(),
|
||||
),
|
||||
get_site_url(wu_get_main_site_id())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send verification email.
|
||||
@ -923,8 +856,7 @@ class Customer extends Base_Model {
|
||||
);
|
||||
|
||||
wu_do_event('confirm_email_address', $payload);
|
||||
|
||||
} // end send_verification_email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form used to signup.
|
||||
@ -935,8 +867,7 @@ class Customer extends Base_Model {
|
||||
public function get_signup_form() {
|
||||
|
||||
return $this->signup_form;
|
||||
|
||||
} // end get_signup_form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the form used to signup.
|
||||
@ -948,7 +879,5 @@ class Customer extends Base_Model {
|
||||
public function set_signup_form($signup_form) {
|
||||
|
||||
$this->signup_form = $signup_form;
|
||||
|
||||
} // end set_signup_form;
|
||||
|
||||
} // end class Customer;
|
||||
}
|
||||
}
|
||||
|
@ -185,8 +185,7 @@ class Discount_Code extends Base_Model {
|
||||
'allowed_products' => 'array',
|
||||
'limit_products' => 'default:0',
|
||||
);
|
||||
|
||||
} // end validation_rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of the discount code.
|
||||
@ -197,8 +196,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_name() {
|
||||
|
||||
return $this->name;
|
||||
|
||||
} // end get_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set name of the discount code.
|
||||
@ -210,8 +208,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_name($name) {
|
||||
|
||||
$this->name = $name;
|
||||
|
||||
} // end set_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get code to redeem the discount code.
|
||||
@ -222,8 +219,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_code() {
|
||||
|
||||
return $this->code;
|
||||
|
||||
} // end get_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set code to redeem the discount code.
|
||||
@ -235,8 +231,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_code($code) {
|
||||
|
||||
$this->code = $code;
|
||||
|
||||
} // end set_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get text describing the coupon code. Useful for identifying it.
|
||||
@ -247,8 +242,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_description() {
|
||||
|
||||
return $this->description;
|
||||
|
||||
} // end get_description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set text describing the coupon code. Useful for identifying it.
|
||||
@ -260,8 +254,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_description($description) {
|
||||
|
||||
$this->description = $description;
|
||||
|
||||
} // end set_description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of times this discount was applied.
|
||||
@ -272,8 +265,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_uses() {
|
||||
|
||||
return (int) $this->uses;
|
||||
|
||||
} // end get_uses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set number of times this discount was applied.
|
||||
@ -285,8 +277,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_uses($uses) {
|
||||
|
||||
$this->uses = (int) $uses;
|
||||
|
||||
} // end set_uses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add uses to this discount code.
|
||||
@ -300,8 +291,7 @@ class Discount_Code extends Base_Model {
|
||||
$use_count = (int) $this->get_uses();
|
||||
|
||||
$this->set_uses($use_count + (int) $uses);
|
||||
|
||||
} // end add_use;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of times this discount can be used before becoming inactive.
|
||||
@ -312,8 +302,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_max_uses() {
|
||||
|
||||
return (int) $this->max_uses;
|
||||
|
||||
} // end get_max_uses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the number of times this discount can be used before becoming inactive.
|
||||
@ -325,8 +314,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_max_uses($max_uses) {
|
||||
|
||||
$this->max_uses = (int) $max_uses;
|
||||
|
||||
} // end set_max_uses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given discount code has a number of max uses.
|
||||
@ -337,8 +325,7 @@ class Discount_Code extends Base_Model {
|
||||
public function has_max_uses() {
|
||||
|
||||
return $this->get_max_uses() > 0;
|
||||
|
||||
} // end has_max_uses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if we should apply this coupon to renewals as well.
|
||||
@ -349,8 +336,7 @@ class Discount_Code extends Base_Model {
|
||||
public function should_apply_to_renewals() {
|
||||
|
||||
return (bool) $this->apply_to_renewals;
|
||||
|
||||
} // end should_apply_to_renewals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if we should apply this coupon to renewals as well.
|
||||
@ -362,8 +348,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_apply_to_renewals($apply_to_renewals) {
|
||||
|
||||
$this->apply_to_renewals = (bool) $apply_to_renewals;
|
||||
|
||||
} // end set_apply_to_renewals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get type of the discount. Can be a percentage or absolute.
|
||||
@ -374,8 +359,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_type() {
|
||||
|
||||
return $this->type;
|
||||
|
||||
} // end get_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set type of the discount. Can be a percentage or absolute.
|
||||
@ -388,8 +372,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_type($type) {
|
||||
|
||||
$this->type = $type;
|
||||
|
||||
} // end set_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get amount discounted in cents.
|
||||
@ -400,8 +383,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_value() {
|
||||
|
||||
return (float) $this->value;
|
||||
|
||||
} // end get_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set amount discounted in cents.
|
||||
@ -413,8 +395,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_value($value) {
|
||||
|
||||
$this->value = $value;
|
||||
|
||||
} // end set_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get type of the discount for the setup fee value. Can be a percentage or absolute.
|
||||
@ -425,8 +406,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_setup_fee_type() {
|
||||
|
||||
return $this->setup_fee_type;
|
||||
|
||||
} // end get_setup_fee_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set type of the discount for the setup fee value. Can be a percentage or absolute.
|
||||
@ -439,8 +419,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_setup_fee_type($setup_fee_type) {
|
||||
|
||||
$this->setup_fee_type = $setup_fee_type;
|
||||
|
||||
} // end set_setup_fee_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get amount discounted fpr setup fees in cents.
|
||||
@ -451,8 +430,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_setup_fee_value() {
|
||||
|
||||
return (float) $this->setup_fee_value;
|
||||
|
||||
} // end get_setup_fee_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set amount discounted for setup fees in cents.
|
||||
@ -464,8 +442,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_setup_fee_value($setup_fee_value) {
|
||||
|
||||
$this->setup_fee_value = $setup_fee_value;
|
||||
|
||||
} // end set_setup_fee_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if this coupon code is active or not.
|
||||
@ -476,8 +453,7 @@ class Discount_Code extends Base_Model {
|
||||
public function is_active() {
|
||||
|
||||
return (bool) $this->active;
|
||||
|
||||
} // end is_active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given coupon code is valid and can be applied.
|
||||
@ -489,19 +465,15 @@ class Discount_Code extends Base_Model {
|
||||
public function is_valid($product = false) {
|
||||
|
||||
if ($this->is_active() === false) {
|
||||
|
||||
return new \WP_Error('discount_code', __('This coupon code is not valid.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for uses
|
||||
*/
|
||||
if ($this->has_max_uses() && $this->get_uses() >= $this->get_max_uses()) {
|
||||
|
||||
return new \WP_Error('discount_code', __('This discount code was already redeemed the maximum amount of times allowed.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* Fist, check date boundaries.
|
||||
@ -512,62 +484,43 @@ class Discount_Code extends Base_Model {
|
||||
$now = wu_date();
|
||||
|
||||
if ($start_date) {
|
||||
|
||||
$start_date_instance = wu_date($start_date);
|
||||
|
||||
if ($now < $start_date_instance) {
|
||||
|
||||
return new \WP_Error('discount_code', __('This coupon code is not valid.', 'wp-ultimo'));
|
||||
|
||||
return new \WP_Error( 'discount_code', __( 'The coupon code is not valid yet.', 'wp-ultimo' ) );
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
return new \WP_Error('discount_code', __('The coupon code is not valid yet.', 'wp-ultimo'));
|
||||
}
|
||||
}
|
||||
|
||||
if ($expiration_date) {
|
||||
|
||||
$expiration_date_instance = wu_date($expiration_date);
|
||||
|
||||
if ($now > $expiration_date_instance) {
|
||||
|
||||
return new \WP_Error('discount_code', __('This coupon code is not valid.', 'wp-ultimo'));
|
||||
}
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!$this->get_limit_products()) {
|
||||
|
||||
if ( ! $this->get_limit_products()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!empty($product)) {
|
||||
|
||||
if ( ! empty($product)) {
|
||||
if (is_a($product, '\WP_Ultimo\Models\Product')) {
|
||||
|
||||
$product_id = $product->get_id();
|
||||
|
||||
} elseif (is_numeric($product)) {
|
||||
|
||||
$product_id = $product;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$allowed = $this->get_limit_products() && in_array($product_id, $this->get_allowed_products()); // phpcs:ignore
|
||||
|
||||
if ($allowed === false) {
|
||||
|
||||
return new \WP_Error('discount_code', __('This coupon code is not valid.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} // end is_valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this discount applies just for the first payment.
|
||||
@ -578,8 +531,7 @@ class Discount_Code extends Base_Model {
|
||||
public function is_one_time() {
|
||||
|
||||
return (bool) $this->should_apply_to_renewals();
|
||||
|
||||
} // end is_one_time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if this coupon code is active or not.
|
||||
@ -591,8 +543,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_active($active) {
|
||||
|
||||
$this->active = (bool) $active;
|
||||
|
||||
} // end set_active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get start date for the coupon code to be considered valid.
|
||||
@ -602,15 +553,12 @@ class Discount_Code extends Base_Model {
|
||||
*/
|
||||
public function get_date_start() {
|
||||
|
||||
if (!wu_validate_date($this->date_start)) {
|
||||
|
||||
if ( ! wu_validate_date($this->date_start)) {
|
||||
return '';
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->date_start;
|
||||
|
||||
} // end get_date_start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set start date for the coupon code to be considered valid.
|
||||
@ -622,8 +570,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_date_start($date_start) {
|
||||
|
||||
$this->date_start = $date_start;
|
||||
|
||||
} // end set_date_start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get expiration date for the coupon code.
|
||||
@ -633,15 +580,12 @@ class Discount_Code extends Base_Model {
|
||||
*/
|
||||
public function get_date_expiration() {
|
||||
|
||||
if (!wu_validate_date($this->date_expiration)) {
|
||||
|
||||
if ( ! wu_validate_date($this->date_expiration)) {
|
||||
return '';
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->date_expiration;
|
||||
|
||||
} // end get_date_expiration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set expiration date for the coupon code.
|
||||
@ -653,8 +597,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_date_expiration($date_expiration) {
|
||||
|
||||
$this->date_expiration = $date_expiration;
|
||||
|
||||
} // end set_date_expiration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get date when this discount code was created.
|
||||
@ -665,8 +608,7 @@ class Discount_Code extends Base_Model {
|
||||
public function get_date_created() {
|
||||
|
||||
return $this->date_created;
|
||||
|
||||
} // end get_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set date when this discount code was created.
|
||||
@ -678,8 +620,7 @@ class Discount_Code extends Base_Model {
|
||||
public function set_date_created($date_created) {
|
||||
|
||||
$this->date_created = $date_created;
|
||||
|
||||
} // end set_date_created;
|
||||
}
|
||||
/**
|
||||
* Returns a text describing the discount code values.
|
||||
*
|
||||
@ -690,44 +631,35 @@ class Discount_Code extends Base_Model {
|
||||
$description = array();
|
||||
|
||||
if ($this->get_value() > 0) {
|
||||
|
||||
$value = wu_format_currency($this->get_value());
|
||||
|
||||
if ($this->get_type() === 'percentage') {
|
||||
|
||||
$value = $this->get_value() . '%';
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$description[] = sprintf(
|
||||
// translators: placeholder is the value off. Can be wither $X.XX or X%
|
||||
__('%1$s OFF on Subscriptions', 'wp-ultimo'),
|
||||
$value
|
||||
);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if ($this->get_setup_fee_value() > 0) {
|
||||
|
||||
$setup_fee_value = wu_format_currency($this->get_setup_fee_value());
|
||||
|
||||
if ($this->get_setup_fee_type() === 'percentage') {
|
||||
|
||||
$setup_fee_value = $this->get_setup_fee_value() . '%';
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$description[] = sprintf(
|
||||
// translators: placeholder is the value off. Can be wither $X.XX or X%
|
||||
__('%1$s OFF on Setup Fees', 'wp-ultimo'),
|
||||
$setup_fee_value
|
||||
);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return implode(' ' . __('and', 'wp-ultimo') . ' ', $description);
|
||||
|
||||
} // end get_discount_description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the object into an assoc array.
|
||||
@ -742,8 +674,7 @@ class Discount_Code extends Base_Model {
|
||||
$array['discount_description'] = $this->get_discount_description();
|
||||
|
||||
return $array;
|
||||
|
||||
} // end to_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save (create or update) the model on the database.
|
||||
@ -756,23 +687,18 @@ class Discount_Code extends Base_Model {
|
||||
|
||||
$results = parent::save();
|
||||
|
||||
if (!is_wp_error($results) && has_action('wp_ultimo_coupon_after_save')) {
|
||||
|
||||
if ( ! is_wp_error($results) && has_action('wp_ultimo_coupon_after_save')) {
|
||||
if (did_action('wp_ultimo_coupon_after_save')) {
|
||||
|
||||
return $results;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$compat_coupon = $this;
|
||||
|
||||
do_action_deprecated('wp_ultimo_coupon_after_save', array($compat_coupon), '2.0.0', 'wu_discount_code_post_save');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $results;
|
||||
|
||||
} // end save;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get holds the list of allowed products.
|
||||
@ -783,14 +709,11 @@ class Discount_Code extends Base_Model {
|
||||
public function get_allowed_products() {
|
||||
|
||||
if ($this->allowed_products === null) {
|
||||
|
||||
$this->allowed_products = $this->get_meta('wu_allowed_products', array());
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return (array) $this->allowed_products;
|
||||
|
||||
} // end get_allowed_products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set holds the list of allowed products.
|
||||
@ -804,8 +727,7 @@ class Discount_Code extends Base_Model {
|
||||
$this->meta['wu_allowed_products'] = (array) $allowed_products;
|
||||
|
||||
$this->allowed_products = $this->meta['wu_allowed_products'];
|
||||
|
||||
} // end set_allowed_products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if we should check for products or not.
|
||||
@ -816,14 +738,11 @@ class Discount_Code extends Base_Model {
|
||||
public function get_limit_products() {
|
||||
|
||||
if ($this->limit_products === null) {
|
||||
|
||||
$this->limit_products = $this->get_meta('wu_limit_products', false);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return (bool) $this->limit_products;
|
||||
|
||||
} // end get_limit_products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if we should check for products or not.
|
||||
@ -837,7 +756,5 @@ class Discount_Code extends Base_Model {
|
||||
$this->meta['wu_limit_products'] = (bool) $limit_products;
|
||||
|
||||
$this->limit_products = $this->meta['wu_limit_products'];
|
||||
|
||||
} // end set_limit_products;
|
||||
|
||||
} // end class Discount_Code;
|
||||
}
|
||||
}
|
||||
|
@ -126,8 +126,7 @@ class Domain extends Base_Model {
|
||||
'secure' => 'default:0',
|
||||
'primary_domain' => 'default:0',
|
||||
);
|
||||
|
||||
} // end validation_rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the domain address mapped.
|
||||
@ -138,8 +137,7 @@ class Domain extends Base_Model {
|
||||
public function get_domain() {
|
||||
|
||||
return $this->domain;
|
||||
|
||||
} // end get_domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the domain of this model object;
|
||||
@ -152,8 +150,7 @@ class Domain extends Base_Model {
|
||||
public function set_domain($domain) {
|
||||
|
||||
$this->domain = strtolower($domain);
|
||||
|
||||
} // end set_domain;
|
||||
}
|
||||
/**
|
||||
* Gets the URL with schema and all.
|
||||
*
|
||||
@ -166,8 +163,7 @@ class Domain extends Base_Model {
|
||||
$schema = $this->is_secure() ? 'https://' : 'http://';
|
||||
|
||||
return sprintf('%s%s/%s', $schema, $this->get_domain(), $path);
|
||||
|
||||
} // end get_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of the corresponding site.
|
||||
@ -179,8 +175,7 @@ class Domain extends Base_Model {
|
||||
public function get_blog_id() {
|
||||
|
||||
return (int) $this->blog_id;
|
||||
|
||||
} // end get_blog_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the blog_id of this model object;
|
||||
@ -193,8 +188,7 @@ class Domain extends Base_Model {
|
||||
public function set_blog_id($blog_id) {
|
||||
|
||||
$this->blog_id = $blog_id;
|
||||
|
||||
} // end set_blog_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of the corresponding site.
|
||||
@ -205,15 +199,14 @@ class Domain extends Base_Model {
|
||||
public function get_site_id() {
|
||||
|
||||
return $this->get_blog_id();
|
||||
|
||||
} // end get_site_id;
|
||||
/**
|
||||
* Get the site object for this particular mapping.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return \WP_Site|\WP_Ultimo\Models\Site|false
|
||||
*/
|
||||
public function get_site() {
|
||||
}
|
||||
/**
|
||||
* Get the site object for this particular mapping.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return \WP_Site|\WP_Ultimo\Models\Site|false
|
||||
*/
|
||||
public function get_site() {
|
||||
|
||||
/**
|
||||
* In a domain mapping environment, the user is not yet logged in.
|
||||
@ -222,15 +215,12 @@ class Domain extends Base_Model {
|
||||
*
|
||||
* To bypass this limitation, we use the default WordPress function on those cases.
|
||||
*/
|
||||
if (!function_exists('current_user_can')) {
|
||||
|
||||
if ( ! function_exists('current_user_can')) {
|
||||
return \WP_Site::get_instance($this->get_blog_id());
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return wu_get_site($this->get_blog_id());
|
||||
|
||||
} // end get_site;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this particular mapping is active.
|
||||
@ -241,14 +231,11 @@ class Domain extends Base_Model {
|
||||
public function is_active() {
|
||||
|
||||
if ($this->has_inactive_stage()) {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return (bool) $this->active;
|
||||
|
||||
} // end is_active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the active state of this model object;
|
||||
@ -261,8 +248,7 @@ class Domain extends Base_Model {
|
||||
public function set_active($active) {
|
||||
|
||||
$this->active = $active;
|
||||
|
||||
} // end set_active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is a primary domain.
|
||||
@ -273,8 +259,7 @@ class Domain extends Base_Model {
|
||||
public function is_primary_domain() {
|
||||
|
||||
return (bool) $this->primary_domain;
|
||||
|
||||
} // end is_primary_domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the primary_domain state of this model object;
|
||||
@ -287,8 +272,7 @@ class Domain extends Base_Model {
|
||||
public function set_primary_domain($primary_domain) {
|
||||
|
||||
$this->primary_domain = $primary_domain;
|
||||
|
||||
} // end set_primary_domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we should use this domain securely (via HTTPS).
|
||||
@ -299,8 +283,7 @@ class Domain extends Base_Model {
|
||||
public function is_secure() {
|
||||
|
||||
return (bool) $this->secure;
|
||||
|
||||
} // end is_secure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the secure state of this model object;
|
||||
@ -313,8 +296,7 @@ class Domain extends Base_Model {
|
||||
public function set_secure($secure) {
|
||||
|
||||
$this->secure = $secure;
|
||||
|
||||
} // end set_secure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the stage in which this domain is in at the moment.
|
||||
@ -327,8 +309,7 @@ class Domain extends Base_Model {
|
||||
public function get_stage() {
|
||||
|
||||
return $this->stage;
|
||||
|
||||
} // end get_stage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the stage of this model object;
|
||||
@ -341,8 +322,7 @@ class Domain extends Base_Model {
|
||||
public function set_stage($stage) {
|
||||
|
||||
$this->stage = $stage;
|
||||
|
||||
} // end set_stage;
|
||||
}
|
||||
/**
|
||||
* Check if this domain is on a inactive stage.
|
||||
*
|
||||
@ -351,8 +331,7 @@ class Domain extends Base_Model {
|
||||
public function has_inactive_stage(): bool {
|
||||
|
||||
return in_array($this->get_stage(), self::INACTIVE_STAGES, true);
|
||||
|
||||
} // end has_inactive_stage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Label for a given stage level.
|
||||
@ -365,8 +344,7 @@ class Domain extends Base_Model {
|
||||
$type = new Domain_Stage($this->get_stage());
|
||||
|
||||
return $type->get_label();
|
||||
|
||||
} // end get_stage_label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the classes for a given stage level.
|
||||
@ -379,8 +357,7 @@ class Domain extends Base_Model {
|
||||
$type = new Domain_Stage($this->get_stage());
|
||||
|
||||
return $type->get_classes();
|
||||
|
||||
} // end get_stage_class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get date when this was created.
|
||||
@ -391,8 +368,7 @@ class Domain extends Base_Model {
|
||||
public function get_date_created() {
|
||||
|
||||
return $this->date_created;
|
||||
|
||||
} // end get_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set date when this was created.
|
||||
@ -404,8 +380,7 @@ class Domain extends Base_Model {
|
||||
public function set_date_created($date_created) {
|
||||
|
||||
$this->date_created = $date_created;
|
||||
|
||||
} // end set_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the domain is correctly set-up in terms of DNS resolution.
|
||||
@ -426,16 +401,12 @@ class Domain extends Base_Model {
|
||||
$domains_and_ips = array_column($results, 'data');
|
||||
|
||||
if (in_array($current_site->domain, $domains_and_ips, true)) {
|
||||
|
||||
return true;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if (in_array($network_ip_address, $domains_and_ips, true)) {
|
||||
|
||||
return true;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$result = false;
|
||||
|
||||
@ -451,8 +422,7 @@ class Domain extends Base_Model {
|
||||
$result = apply_filters('wu_domain_has_correct_dns', $result, $this, $domains_and_ips);
|
||||
|
||||
return $result;
|
||||
|
||||
} // end has_correct_dns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current domain has a valid SSL certificate that covers it.
|
||||
@ -463,8 +433,7 @@ class Domain extends Base_Model {
|
||||
public function has_valid_ssl_certificate() {
|
||||
|
||||
return Helper::has_valid_ssl_certificate($this->get_domain());
|
||||
|
||||
} // end has_valid_ssl_certificate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save (create or update) the model on the database.
|
||||
@ -485,11 +454,8 @@ class Domain extends Base_Model {
|
||||
$results = parent::save();
|
||||
|
||||
if (is_wp_error($results) === false) {
|
||||
|
||||
if ($new_domain) {
|
||||
|
||||
if (has_action('mercator.mapping.created')) {
|
||||
|
||||
$deprecated_args = array(
|
||||
$this,
|
||||
);
|
||||
@ -503,13 +469,8 @@ class Domain extends Base_Model {
|
||||
* @return void.
|
||||
*/
|
||||
do_action_deprecated('mercator.mapping.created', $deprecated_args, '2.0.0', 'wu_domain_post_save');
|
||||
|
||||
} // end if;
|
||||
|
||||
} else {
|
||||
|
||||
if (has_action('mercator.mapping.updated')) {
|
||||
|
||||
}
|
||||
} elseif (has_action('mercator.mapping.updated')) {
|
||||
$deprecated_args = array(
|
||||
$this,
|
||||
$before_changes,
|
||||
@ -524,10 +485,7 @@ class Domain extends Base_Model {
|
||||
* @return void.
|
||||
*/
|
||||
do_action_deprecated('mercator.mapping.updated', $deprecated_args, '2.0.0', 'wu_domain_post_save');
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* Resets cache.
|
||||
@ -536,24 +494,21 @@ class Domain extends Base_Model {
|
||||
* after a change is made.
|
||||
*/
|
||||
wp_cache_flush();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $results;
|
||||
|
||||
} // end save;
|
||||
/**
|
||||
* Delete the model from the database.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return \WP_Error|bool
|
||||
*/
|
||||
public function delete() {
|
||||
}
|
||||
/**
|
||||
* Delete the model from the database.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return \WP_Error|bool
|
||||
*/
|
||||
public function delete() {
|
||||
|
||||
$results = parent::delete();
|
||||
|
||||
if (is_wp_error($results) === false && has_action('mercator.mapping.deleted')) {
|
||||
|
||||
$deprecated_args = array(
|
||||
$this,
|
||||
);
|
||||
@ -566,8 +521,7 @@ class Domain extends Base_Model {
|
||||
* @return void.
|
||||
*/
|
||||
do_action_deprecated('mercator.mapping.deleted', $deprecated_args, '2.0.0', 'wu_domain_post_delete');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete log file.
|
||||
@ -577,8 +531,7 @@ class Domain extends Base_Model {
|
||||
wu_log_add("domain-{$this->get_domain()}", __('Domain deleted and logs cleared...', 'wp-ultimo'));
|
||||
|
||||
return $results;
|
||||
|
||||
} // end delete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mapping by site ID
|
||||
@ -594,16 +547,12 @@ class Domain extends Base_Model {
|
||||
|
||||
// Allow passing a site object in
|
||||
if (is_object($site) && isset($site->blog_id)) {
|
||||
|
||||
$site = $site->blog_id;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!is_numeric($site)) {
|
||||
|
||||
if ( ! is_numeric($site)) {
|
||||
return new \WP_Error('wu_domain_mapping_invalid_id');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$site = absint($site);
|
||||
|
||||
@ -611,16 +560,12 @@ class Domain extends Base_Model {
|
||||
$mappings = wp_cache_get('id:' . $site, 'domain_mapping');
|
||||
|
||||
if ($mappings === 'none') {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!empty($mappings)) {
|
||||
|
||||
if ( ! empty($mappings)) {
|
||||
return static::to_instances($mappings);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
// Cache missed, fetch from DB
|
||||
// Suppress errors in case the table doesn't exist
|
||||
@ -632,19 +577,16 @@ class Domain extends Base_Model {
|
||||
|
||||
$wpdb->suppress_errors($suppress);
|
||||
|
||||
if (!$mappings) {
|
||||
|
||||
if ( ! $mappings) {
|
||||
wp_cache_set('id:' . $site, 'none', 'domain_mapping');
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
wp_cache_set('id:' . $site, $mappings, 'domain_mapping');
|
||||
|
||||
return static::to_instances($mappings);
|
||||
|
||||
} // end get_by_site;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets mappings by domain names
|
||||
@ -666,28 +608,21 @@ class Domain extends Base_Model {
|
||||
$not_exists = 0;
|
||||
|
||||
foreach ($domains as $domain) {
|
||||
|
||||
$data = wp_cache_get('domain:' . $domain, 'domain_mappings');
|
||||
|
||||
if (!empty($data) && $data !== 'notexists') {
|
||||
|
||||
if ( ! empty($data) && $data !== 'notexists') {
|
||||
return new static($data);
|
||||
|
||||
} elseif ($data === 'notexists') {
|
||||
|
||||
$not_exists++;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
++$not_exists;
|
||||
}
|
||||
}
|
||||
|
||||
if ($not_exists === count($domains)) {
|
||||
|
||||
// Every domain we checked was found in the cache, but doesn't exist
|
||||
// so skip the query
|
||||
return null;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$placeholders = array_fill(0, count($domains), '%s');
|
||||
|
||||
@ -709,19 +644,14 @@ class Domain extends Base_Model {
|
||||
|
||||
// Cache that it doesn't exist
|
||||
foreach ($domains as $domain) {
|
||||
|
||||
wp_cache_set('domain:' . $domain, 'notexists', 'domain_mappings');
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
wp_cache_set('domain:' . $mapping->domain, $mapping, 'domain_mappings');
|
||||
|
||||
return new static($mapping);
|
||||
|
||||
} // end get_by_domain;
|
||||
|
||||
} // end class Domain;
|
||||
}
|
||||
}
|
||||
|
@ -160,8 +160,7 @@ class Email extends Post_Base_Model {
|
||||
'active' => 'default:1',
|
||||
'legacy' => 'boolean|default:0',
|
||||
);
|
||||
|
||||
} // end validation_rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get event of the email
|
||||
@ -172,14 +171,11 @@ class Email extends Post_Base_Model {
|
||||
public function get_event() {
|
||||
|
||||
if ($this->event === null) {
|
||||
|
||||
$this->event = $this->get_meta('wu_system_email_event');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->event;
|
||||
|
||||
} // end get_event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get title of the email
|
||||
@ -190,8 +186,7 @@ class Email extends Post_Base_Model {
|
||||
public function get_title() {
|
||||
|
||||
return $this->title;
|
||||
|
||||
} // end get_title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get title of the email using get_name
|
||||
@ -202,8 +197,7 @@ class Email extends Post_Base_Model {
|
||||
public function get_name() {
|
||||
|
||||
return $this->title;
|
||||
|
||||
} // end get_name;
|
||||
}
|
||||
/**
|
||||
* Get style of the email
|
||||
*
|
||||
@ -215,10 +209,8 @@ class Email extends Post_Base_Model {
|
||||
$this->style = $this->get_meta('wu_style', 'html');
|
||||
|
||||
if ($this->style === 'use_default') {
|
||||
|
||||
$this->style = wu_get_setting('email_template_type', 'html');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do an extra check for old installs
|
||||
@ -226,14 +218,11 @@ class Email extends Post_Base_Model {
|
||||
* properly installed.
|
||||
*/
|
||||
if (empty($this->style)) {
|
||||
|
||||
$this->style = 'html';
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->style;
|
||||
|
||||
} // end get_style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the style.
|
||||
@ -249,8 +238,7 @@ class Email extends Post_Base_Model {
|
||||
$this->style = $style;
|
||||
|
||||
$this->meta['wu_style'] = $this->style;
|
||||
|
||||
} // end set_style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if the email has a schedule.
|
||||
@ -261,14 +249,11 @@ class Email extends Post_Base_Model {
|
||||
public function has_schedule() {
|
||||
|
||||
if ($this->schedule === null) {
|
||||
|
||||
$this->schedule = $this->get_meta('wu_schedule', false);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->schedule;
|
||||
|
||||
} // end has_schedule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the email schedule.
|
||||
@ -282,8 +267,7 @@ class Email extends Post_Base_Model {
|
||||
$this->schedule = $schedule;
|
||||
|
||||
$this->meta['wu_schedule'] = $schedule;
|
||||
|
||||
} // end set_schedule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the email schedule.
|
||||
@ -294,8 +278,7 @@ class Email extends Post_Base_Model {
|
||||
public function get_schedule_type() {
|
||||
|
||||
return $this->get_meta('system_email_schedule_type', 'days');
|
||||
|
||||
} // end get_schedule_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get schedule send in days of the email
|
||||
@ -306,8 +289,7 @@ class Email extends Post_Base_Model {
|
||||
public function get_send_days() {
|
||||
|
||||
return $this->get_meta('system_email_send_days', 0);
|
||||
|
||||
} // end get_send_days;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get schedule send in hours of the email.
|
||||
@ -318,8 +300,7 @@ class Email extends Post_Base_Model {
|
||||
public function get_send_hours() {
|
||||
|
||||
return $this->get_meta('system_email_send_hours', '12:00');
|
||||
|
||||
} // end get_send_hours;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a timestamp in the future when this email should be sent.
|
||||
@ -331,29 +312,22 @@ class Email extends Post_Base_Model {
|
||||
|
||||
$when_to_send = 0;
|
||||
|
||||
if (!$this->has_schedule()) {
|
||||
|
||||
if ( ! $this->has_schedule()) {
|
||||
return $when_to_send;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if ($this->get_schedule_type() === 'hours') {
|
||||
|
||||
$send_time = explode(':', $this->get_send_hours());
|
||||
|
||||
$when_to_send = strtotime('+' . $send_time[0] . ' hours ' . $send_time[1] . ' minutes');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if ($this->get_schedule_type() === 'days') {
|
||||
|
||||
$when_to_send = strtotime('+' . $this->get_send_days() . ' days');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $when_to_send;
|
||||
|
||||
} // end get_when_to_send;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get email slug.
|
||||
@ -364,8 +338,7 @@ class Email extends Post_Base_Model {
|
||||
public function get_slug() {
|
||||
|
||||
return $this->slug;
|
||||
|
||||
} // end get_slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the custom sender option.
|
||||
@ -376,8 +349,7 @@ class Email extends Post_Base_Model {
|
||||
public function get_custom_sender() {
|
||||
|
||||
return $this->get_meta('system_email_custom_sender');
|
||||
|
||||
} // end get_custom_sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the custom sender name.
|
||||
@ -388,8 +360,7 @@ class Email extends Post_Base_Model {
|
||||
public function get_custom_sender_name() {
|
||||
|
||||
return $this->get_meta('system_email_custom_sender_name');
|
||||
|
||||
} // end get_custom_sender_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the custom sender email.
|
||||
@ -400,8 +371,7 @@ class Email extends Post_Base_Model {
|
||||
public function get_custom_sender_email() {
|
||||
|
||||
return $this->get_meta('system_email_custom_sender_email');
|
||||
|
||||
} // end get_custom_sender_email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds checks to prevent saving the model with the wrong type.
|
||||
@ -413,15 +383,12 @@ class Email extends Post_Base_Model {
|
||||
*/
|
||||
public function set_type($type) {
|
||||
|
||||
if (!in_array($type, $this->allowed_types, true)) {
|
||||
|
||||
if ( ! in_array($type, $this->allowed_types, true)) {
|
||||
$type = 'system_email';
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$this->type = $type;
|
||||
|
||||
} // end set_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the email event.
|
||||
@ -436,8 +403,7 @@ class Email extends Post_Base_Model {
|
||||
$this->event = $event;
|
||||
|
||||
$this->meta['wu_system_email_event'] = $event;
|
||||
|
||||
} // end set_event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if the email is schedule.
|
||||
@ -450,8 +416,7 @@ class Email extends Post_Base_Model {
|
||||
public function set_email_schedule($email_schedule) {
|
||||
|
||||
$this->meta['system_email_schedule'] = $email_schedule;
|
||||
|
||||
} // end set_email_schedule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the schedule date in hours.
|
||||
@ -464,8 +429,7 @@ class Email extends Post_Base_Model {
|
||||
public function set_send_hours($send_hours) {
|
||||
|
||||
$this->meta['system_email_send_hours'] = $send_hours;
|
||||
|
||||
} // end set_send_hours;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the schedule date in days.
|
||||
@ -478,8 +442,7 @@ class Email extends Post_Base_Model {
|
||||
public function set_send_days($send_days) {
|
||||
|
||||
$this->meta['system_email_send_days'] = $send_days;
|
||||
|
||||
} // end set_send_days;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the schedule type.
|
||||
@ -493,8 +456,7 @@ class Email extends Post_Base_Model {
|
||||
public function set_schedule_type($schedule_type) {
|
||||
|
||||
$this->meta['system_email_schedule_type'] = $schedule_type;
|
||||
|
||||
} // end set_schedule_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set title using the name parameter.
|
||||
@ -507,8 +469,7 @@ class Email extends Post_Base_Model {
|
||||
public function set_name($name) {
|
||||
|
||||
$this->set_title($name);
|
||||
|
||||
} // end set_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the slug.
|
||||
@ -521,8 +482,7 @@ class Email extends Post_Base_Model {
|
||||
public function set_slug($slug) {
|
||||
|
||||
$this->slug = $slug;
|
||||
|
||||
} // end set_slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the custom sender.
|
||||
@ -535,8 +495,7 @@ class Email extends Post_Base_Model {
|
||||
public function set_custom_sender($custom_sender) {
|
||||
|
||||
$this->meta['system_email_custom_sender'] = $custom_sender;
|
||||
|
||||
} // end set_custom_sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the custom sender name.
|
||||
@ -549,8 +508,7 @@ class Email extends Post_Base_Model {
|
||||
public function set_custom_sender_name($custom_sender_name) {
|
||||
|
||||
$this->meta['system_email_custom_sender_name'] = $custom_sender_name;
|
||||
|
||||
} // end set_custom_sender_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the custom sender email.
|
||||
@ -563,8 +521,7 @@ class Email extends Post_Base_Model {
|
||||
public function set_custom_sender_email($custom_sender_email) {
|
||||
|
||||
$this->meta['system_email_custom_sender_email'] = $custom_sender_email;
|
||||
|
||||
} // end set_custom_sender_email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if we should send this to a customer or to the network admin.
|
||||
@ -575,14 +532,11 @@ class Email extends Post_Base_Model {
|
||||
public function get_target() {
|
||||
|
||||
if ($this->target === null) {
|
||||
|
||||
$this->target = $this->get_meta('wu_target', 'admin');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->target;
|
||||
|
||||
} // end get_target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if we should send this to a customer or to the network admin.
|
||||
@ -597,8 +551,7 @@ class Email extends Post_Base_Model {
|
||||
$this->target = $target;
|
||||
|
||||
$this->meta['wu_target'] = $target;
|
||||
|
||||
} // end set_target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of targets for an email.
|
||||
@ -615,24 +568,17 @@ class Email extends Post_Base_Model {
|
||||
$target_type = $this->get_target();
|
||||
|
||||
if ($target_type === 'admin') {
|
||||
|
||||
$target_list = self::get_super_admin_targets();
|
||||
|
||||
} elseif ($target_type === 'customer') {
|
||||
|
||||
if (!wu_get_isset($payload, 'customer_id')) {
|
||||
|
||||
if ( ! wu_get_isset($payload, 'customer_id')) {
|
||||
return array();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$customer = wu_get_customer($payload['customer_id']);
|
||||
|
||||
if (!$customer) {
|
||||
|
||||
if ( ! $customer) {
|
||||
return array();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$target_list[] = array(
|
||||
'name' => $customer->get_display_name(),
|
||||
@ -643,18 +589,14 @@ class Email extends Post_Base_Model {
|
||||
* Maybe ad super admins as well.
|
||||
*/
|
||||
if ($this->get_send_copy_to_admin()) {
|
||||
|
||||
$admin_targets = self::get_super_admin_targets();
|
||||
|
||||
$target_list = array_merge($target_list, $admin_targets);
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
}
|
||||
|
||||
return $target_list;
|
||||
|
||||
} // end get_target_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of super admin targets.
|
||||
@ -669,23 +611,18 @@ class Email extends Post_Base_Model {
|
||||
$super_admins = get_super_admins();
|
||||
|
||||
foreach ($super_admins as $super_admin) {
|
||||
|
||||
$user = get_user_by('login', $super_admin);
|
||||
|
||||
if ($user) {
|
||||
|
||||
$target_list[] = array(
|
||||
'name' => $user->display_name,
|
||||
'email' => $user->user_email,
|
||||
);
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
}
|
||||
|
||||
return $target_list;
|
||||
|
||||
} // end get_super_admin_targets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if we should send a copy of the email to the admin.
|
||||
@ -696,14 +633,11 @@ class Email extends Post_Base_Model {
|
||||
public function get_send_copy_to_admin() {
|
||||
|
||||
if ($this->send_copy_to_admin === null) {
|
||||
|
||||
$this->send_copy_to_admin = $this->get_meta('wu_send_copy_to_admin', false);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->send_copy_to_admin;
|
||||
|
||||
} // end get_send_copy_to_admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if we should send a copy of the email to the admin.
|
||||
@ -717,8 +651,7 @@ class Email extends Post_Base_Model {
|
||||
$this->send_copy_to_admin = $send_copy_to_admin;
|
||||
|
||||
$this->meta['wu_send_copy_to_admin'] = $send_copy_to_admin;
|
||||
|
||||
} // end set_send_copy_to_admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the active status of an email.
|
||||
@ -729,14 +662,11 @@ class Email extends Post_Base_Model {
|
||||
public function is_active() {
|
||||
|
||||
if ($this->active === null) {
|
||||
|
||||
$this->active = $this->get_meta('wu_active', true);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->active;
|
||||
|
||||
} // end is_active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the active status of an email.
|
||||
@ -750,8 +680,7 @@ class Email extends Post_Base_Model {
|
||||
$this->active = $active;
|
||||
|
||||
$this->meta['wu_active'] = $active;
|
||||
|
||||
} // end set_active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether or not this is a legacy email.
|
||||
@ -762,14 +691,11 @@ class Email extends Post_Base_Model {
|
||||
public function is_legacy() {
|
||||
|
||||
if ($this->legacy === null) {
|
||||
|
||||
$this->legacy = $this->get_meta('wu_legacy', false);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->legacy;
|
||||
|
||||
} // end is_legacy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether or not this is a legacy email.
|
||||
@ -783,7 +709,5 @@ class Email extends Post_Base_Model {
|
||||
$this->legacy = $legacy;
|
||||
|
||||
$this->meta['wu_legacy'] = $legacy;
|
||||
|
||||
} // end set_legacy;
|
||||
|
||||
} // end class Email;
|
||||
}
|
||||
}
|
||||
|
@ -128,10 +128,9 @@ class Event extends Base_Model {
|
||||
'object_id' => 'integer|default:0',
|
||||
'author_id' => 'integer|default:0',
|
||||
'slug' => 'required|alpha_dash',
|
||||
'initiator' => 'required|in:system,manual'
|
||||
'initiator' => 'required|in:system,manual',
|
||||
);
|
||||
|
||||
} // end validation_rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get severity of the problem..
|
||||
@ -142,8 +141,7 @@ class Event extends Base_Model {
|
||||
public function get_severity() {
|
||||
|
||||
return (int) $this->severity;
|
||||
|
||||
} // end get_severity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Label for a given severity level.
|
||||
@ -154,16 +152,15 @@ class Event extends Base_Model {
|
||||
public function get_severity_label() {
|
||||
|
||||
$labels = array(
|
||||
Event::SEVERITY_SUCCESS => __('Success', 'wp-ultimo'),
|
||||
Event::SEVERITY_NEUTRAL => __('Neutral', 'wp-ultimo'),
|
||||
Event::SEVERITY_INFO => __('Info', 'wp-ultimo'),
|
||||
Event::SEVERITY_WARNING => __('Warning', 'wp-ultimo'),
|
||||
Event::SEVERITY_FATAL => __('Fatal', 'wp-ultimo'),
|
||||
self::SEVERITY_SUCCESS => __('Success', 'wp-ultimo'),
|
||||
self::SEVERITY_NEUTRAL => __('Neutral', 'wp-ultimo'),
|
||||
self::SEVERITY_INFO => __('Info', 'wp-ultimo'),
|
||||
self::SEVERITY_WARNING => __('Warning', 'wp-ultimo'),
|
||||
self::SEVERITY_FATAL => __('Fatal', 'wp-ultimo'),
|
||||
);
|
||||
|
||||
return isset($labels[$this->get_severity()]) ? $labels[$this->get_severity()] : __('Note', 'wp-ultimo');
|
||||
|
||||
} // end get_severity_label;
|
||||
return isset($labels[ $this->get_severity() ]) ? $labels[ $this->get_severity() ] : __('Note', 'wp-ultimo');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the classes for a given severity level.
|
||||
@ -174,16 +171,15 @@ class Event extends Base_Model {
|
||||
public function get_severity_class() {
|
||||
|
||||
$classes = array(
|
||||
Event::SEVERITY_SUCCESS => 'wu-bg-green-200 wu-text-green-700',
|
||||
Event::SEVERITY_NEUTRAL => 'wu-bg-gray-200 wu-text-gray-700',
|
||||
Event::SEVERITY_INFO => 'wu-bg-blue-200 wu-text-blue-700',
|
||||
Event::SEVERITY_WARNING => 'wu-bg-yellow-200 wu-text-yellow-700',
|
||||
Event::SEVERITY_FATAL => 'wu-bg-red-200 wu-text-red-700',
|
||||
self::SEVERITY_SUCCESS => 'wu-bg-green-200 wu-text-green-700',
|
||||
self::SEVERITY_NEUTRAL => 'wu-bg-gray-200 wu-text-gray-700',
|
||||
self::SEVERITY_INFO => 'wu-bg-blue-200 wu-text-blue-700',
|
||||
self::SEVERITY_WARNING => 'wu-bg-yellow-200 wu-text-yellow-700',
|
||||
self::SEVERITY_FATAL => 'wu-bg-red-200 wu-text-red-700',
|
||||
);
|
||||
|
||||
return isset($classes[$this->get_severity()]) ? $classes[$this->get_severity()] : '';
|
||||
|
||||
} // end get_severity_class;
|
||||
return isset($classes[ $this->get_severity() ]) ? $classes[ $this->get_severity() ] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set severity of the problem..
|
||||
@ -195,8 +191,7 @@ class Event extends Base_Model {
|
||||
public function set_severity($severity) {
|
||||
|
||||
$this->severity = $severity;
|
||||
|
||||
} // end set_severity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get date when the event was created..
|
||||
@ -207,8 +202,7 @@ class Event extends Base_Model {
|
||||
public function get_date_created() {
|
||||
|
||||
return $this->date_created;
|
||||
|
||||
} // end get_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set date when the event was created..
|
||||
@ -220,8 +214,7 @@ class Event extends Base_Model {
|
||||
public function set_date_created($date_created) {
|
||||
|
||||
$this->date_created = $date_created;
|
||||
|
||||
} // end set_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get payload of the event..
|
||||
@ -234,8 +227,7 @@ class Event extends Base_Model {
|
||||
$payload = (array) maybe_unserialize(wp_unslash($this->payload));
|
||||
|
||||
return $payload;
|
||||
|
||||
} // end get_payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set payload of the event..
|
||||
@ -247,8 +239,7 @@ class Event extends Base_Model {
|
||||
public function set_payload($payload) {
|
||||
|
||||
$this->payload = $payload;
|
||||
|
||||
} // end set_payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get message for the event.
|
||||
@ -261,8 +252,7 @@ class Event extends Base_Model {
|
||||
$message = self::get_default_system_messages($this->slug);
|
||||
|
||||
return $this->interpolate_message($message, $this->get_payload());
|
||||
|
||||
} // end get_message;
|
||||
}
|
||||
/**
|
||||
* Interpolates the value of a message and its placeholders with the contents of the payload.
|
||||
*
|
||||
@ -278,16 +268,12 @@ class Event extends Base_Model {
|
||||
$interpolation_keys = array();
|
||||
|
||||
foreach ($payload as $key => &$value) {
|
||||
|
||||
$interpolation_keys[] = "{{{$key}}}";
|
||||
|
||||
if (is_array($value)) {
|
||||
|
||||
$value = implode(' → ', wu_array_flatten($value));
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
}
|
||||
|
||||
$interpolation = array_combine($interpolation_keys, $payload);
|
||||
|
||||
@ -298,8 +284,7 @@ class Event extends Base_Model {
|
||||
$interpolation['{{object_id}}'] = $this->object_id;
|
||||
|
||||
return strtr($message, $interpolation);
|
||||
|
||||
} // end interpolate_message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default system messages for events.
|
||||
@ -319,8 +304,7 @@ class Event extends Base_Model {
|
||||
$default_messages = apply_filters('wu_get_default_system_messages', $default_messages);
|
||||
|
||||
return wu_get_isset($default_messages, $slug, __('No Message', 'wp-ultimo'));
|
||||
|
||||
} // end get_default_system_messages;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@ -332,8 +316,7 @@ class Event extends Base_Model {
|
||||
public function get_initiator() {
|
||||
|
||||
return $this->initiator;
|
||||
|
||||
} // end get_initiator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set by people (admins, customers), saved as 'manual'.
|
||||
@ -346,8 +329,7 @@ class Event extends Base_Model {
|
||||
public function set_initiator($initiator) {
|
||||
|
||||
$this->initiator = $initiator;
|
||||
|
||||
} // end set_initiator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the author of the action, saved as the user_id.
|
||||
@ -358,8 +340,7 @@ class Event extends Base_Model {
|
||||
public function get_author_id() {
|
||||
|
||||
return $this->author_id;
|
||||
|
||||
} // end get_author_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user associated with this author.
|
||||
@ -370,18 +351,13 @@ class Event extends Base_Model {
|
||||
public function get_author_user() {
|
||||
|
||||
if ($this->author_id) {
|
||||
|
||||
$user = get_user_by('id', $this->author_id);
|
||||
|
||||
if ($user) {
|
||||
|
||||
return $user;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end get_author_user;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the authors' display name.
|
||||
@ -394,12 +370,9 @@ class Event extends Base_Model {
|
||||
$user = $this->get_author_user();
|
||||
|
||||
if ($user) {
|
||||
|
||||
return $user->display_name;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end get_author_display_name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the authors' email address.
|
||||
@ -412,12 +385,9 @@ class Event extends Base_Model {
|
||||
$user = $this->get_author_user();
|
||||
|
||||
if ($user) {
|
||||
|
||||
return $user->user_email;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end get_author_email_address;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the author of the action, saved as the user_id.
|
||||
@ -429,8 +399,7 @@ class Event extends Base_Model {
|
||||
public function set_author_id($author_id) {
|
||||
|
||||
$this->author_id = $author_id;
|
||||
|
||||
} // end set_author_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the object of this event.
|
||||
@ -445,109 +414,91 @@ class Event extends Base_Model {
|
||||
$function_name = "wu_get_{$object_type}";
|
||||
|
||||
if (function_exists($function_name)) {
|
||||
|
||||
return $function_name($this->get_object_id());
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
} // end get_object;
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_membership() {
|
||||
}
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_membership() {
|
||||
|
||||
$object_type = $this->get_object_type();
|
||||
|
||||
if ($object_type !== 'membership') {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->get_object();
|
||||
|
||||
} // end get_membership;
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_product() {
|
||||
}
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_product() {
|
||||
|
||||
$object_type = $this->get_object_type();
|
||||
|
||||
if ($object_type !== 'product') {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->get_object();
|
||||
|
||||
} // end get_product;
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_site() {
|
||||
}
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_site() {
|
||||
|
||||
$object_type = $this->get_object_type();
|
||||
|
||||
if ($object_type !== 'site') {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->get_object();
|
||||
|
||||
} // end get_site;
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_customer() {
|
||||
}
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_customer() {
|
||||
|
||||
$object_type = $this->get_object_type();
|
||||
|
||||
if ($object_type !== 'customer') {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->get_object();
|
||||
|
||||
} // end get_customer;
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_payment() {
|
||||
}
|
||||
/**
|
||||
* Polyfill for the get_object method.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return false|object
|
||||
*/
|
||||
public function get_payment() {
|
||||
|
||||
$object_type = $this->get_object_type();
|
||||
|
||||
if ($object_type !== 'payment') {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->get_object();
|
||||
|
||||
} // end get_payment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the object type associated with this event.
|
||||
@ -558,8 +509,7 @@ class Event extends Base_Model {
|
||||
public function get_object_type() {
|
||||
|
||||
return $this->object_type;
|
||||
|
||||
} // end get_object_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the object type associated with this event.
|
||||
@ -571,8 +521,7 @@ class Event extends Base_Model {
|
||||
public function set_object_type($object_type) {
|
||||
|
||||
$this->object_type = $object_type;
|
||||
|
||||
} // end set_object_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the object type associated with this event.
|
||||
@ -583,8 +532,7 @@ class Event extends Base_Model {
|
||||
public function get_slug() {
|
||||
|
||||
return $this->slug;
|
||||
|
||||
} // end get_slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the object type associated with this event.
|
||||
@ -596,8 +544,7 @@ class Event extends Base_Model {
|
||||
public function set_slug($slug) {
|
||||
|
||||
$this->slug = $slug;
|
||||
|
||||
} // end set_slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get iD of the related objects..
|
||||
@ -608,8 +555,7 @@ class Event extends Base_Model {
|
||||
public function get_object_id() {
|
||||
|
||||
return $this->object_id;
|
||||
|
||||
} // end get_object_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set iD of the related objects.
|
||||
@ -621,8 +567,7 @@ class Event extends Base_Model {
|
||||
public function set_object_id($object_id) {
|
||||
|
||||
$this->object_id = $object_id;
|
||||
|
||||
} // end set_object_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the object into an assoc array.
|
||||
@ -645,27 +590,25 @@ class Event extends Base_Model {
|
||||
$array['author'] = array();
|
||||
|
||||
if ($this->get_initiator() === 'manual') {
|
||||
|
||||
$user = get_user_by('ID', $this->get_author_id());
|
||||
|
||||
if ($user) {
|
||||
|
||||
$array['author'] = (array) $user->data;
|
||||
|
||||
unset($array['author']['user_pass']);
|
||||
unset($array['author']['user_activation_key']);
|
||||
|
||||
$array['author']['avatar'] = get_avatar_url($this->get_author_id(), array(
|
||||
'default' => 'identicon',
|
||||
));
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
$array['author']['avatar'] = get_avatar_url(
|
||||
$this->get_author_id(),
|
||||
array(
|
||||
'default' => 'identicon',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
|
||||
} // end to_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override to clear event count.
|
||||
@ -675,16 +618,12 @@ class Event extends Base_Model {
|
||||
*/
|
||||
public function save() {
|
||||
|
||||
if (!$this->exists() && function_exists('get_current_user_id')) {
|
||||
|
||||
if ( ! $this->exists() && function_exists('get_current_user_id')) {
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
delete_site_transient("wu_{$user_id}_unseen_events_count");
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return parent::save();
|
||||
|
||||
} // end save;
|
||||
|
||||
} // end class Event;
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -9,12 +9,12 @@
|
||||
|
||||
namespace WP_Ultimo\Models;
|
||||
|
||||
use \WP_Ultimo\Models\Base_Model;
|
||||
use \WP_Ultimo\Database\Payments\Payment_Status;
|
||||
use \WP_Ultimo\Checkout\Line_Item;
|
||||
use \WP_Ultimo\Models\Product;
|
||||
use \WP_Ultimo\Models\Customer;
|
||||
use \WP_Ultimo\Models\Membership;
|
||||
use WP_Ultimo\Models\Base_Model;
|
||||
use WP_Ultimo\Database\Payments\Payment_Status;
|
||||
use WP_Ultimo\Checkout\Line_Item;
|
||||
use WP_Ultimo\Models\Product;
|
||||
use WP_Ultimo\Models\Customer;
|
||||
use WP_Ultimo\Models\Membership;
|
||||
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
@ -189,14 +189,11 @@ class Payment extends Base_Model {
|
||||
$method_key = str_replace('_formatted', '', $name);
|
||||
|
||||
if (strpos($name, '_formatted') !== false && method_exists($this, $method_key)) {
|
||||
|
||||
return wu_format_currency($this->{"$method_key"}(), $this->get_currency());
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
throw new \BadMethodCallException($name);
|
||||
|
||||
} // end __call;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the validation rules for this particular model.
|
||||
@ -233,8 +230,7 @@ class Payment extends Base_Model {
|
||||
'invoice_number' => 'default:',
|
||||
'cancel_membership_on_refund' => 'boolean|default:0',
|
||||
);
|
||||
|
||||
} // end validation_rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the customer object associated with this payment.
|
||||
@ -246,8 +242,7 @@ class Payment extends Base_Model {
|
||||
public function get_customer() {
|
||||
|
||||
return wu_get_customer($this->get_customer_id());
|
||||
|
||||
} // end get_customer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of customer_id.
|
||||
@ -258,8 +253,7 @@ class Payment extends Base_Model {
|
||||
public function get_customer_id(): int {
|
||||
|
||||
return absint($this->customer_id);
|
||||
|
||||
} // end get_customer_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of customer_id.
|
||||
@ -271,20 +265,18 @@ class Payment extends Base_Model {
|
||||
public function set_customer_id($customer_id) {
|
||||
|
||||
$this->customer_id = absint($customer_id);
|
||||
|
||||
} // end set_customer_id;
|
||||
/**
|
||||
* Gets the membership object associated with this payment.
|
||||
*
|
||||
* @todo Implement this.
|
||||
* @since 2.0.0
|
||||
* @return \WP_Ultimo\Models\Membership|false
|
||||
*/
|
||||
public function get_membership() {
|
||||
}
|
||||
/**
|
||||
* Gets the membership object associated with this payment.
|
||||
*
|
||||
* @todo Implement this.
|
||||
* @since 2.0.0
|
||||
* @return \WP_Ultimo\Models\Membership|false
|
||||
*/
|
||||
public function get_membership() {
|
||||
|
||||
return wu_get_membership($this->get_membership_id());
|
||||
|
||||
} // end get_membership;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get membership ID.
|
||||
@ -295,8 +287,7 @@ class Payment extends Base_Model {
|
||||
public function get_membership_id() {
|
||||
|
||||
return $this->membership_id;
|
||||
|
||||
} // end get_membership_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set membership ID.
|
||||
@ -308,8 +299,7 @@ class Payment extends Base_Model {
|
||||
public function set_membership_id($membership_id) {
|
||||
|
||||
$this->membership_id = $membership_id;
|
||||
|
||||
} // end set_membership_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parent payment ID.
|
||||
@ -320,8 +310,7 @@ class Payment extends Base_Model {
|
||||
public function get_parent_id() {
|
||||
|
||||
return $this->parent_id;
|
||||
|
||||
} // end get_parent_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set parent payment ID.
|
||||
@ -333,8 +322,7 @@ class Payment extends Base_Model {
|
||||
public function set_parent_id($parent_id) {
|
||||
|
||||
$this->parent_id = $parent_id;
|
||||
|
||||
} // end set_parent_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get currency for this payment. 3-letter currency code.
|
||||
@ -346,8 +334,7 @@ class Payment extends Base_Model {
|
||||
|
||||
// return $this->currency; For now, multi-currency is not yet supported.
|
||||
return wu_get_setting('currency_symbol', 'USD');
|
||||
|
||||
} // end get_currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set currency for this payment. 3-letter currency code.
|
||||
@ -359,8 +346,7 @@ class Payment extends Base_Model {
|
||||
public function set_currency($currency) {
|
||||
|
||||
$this->currency = $currency;
|
||||
|
||||
} // end set_currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value before taxes, discounts, fees and etc.
|
||||
@ -371,8 +357,7 @@ class Payment extends Base_Model {
|
||||
public function get_subtotal(): float {
|
||||
|
||||
return $this->subtotal;
|
||||
|
||||
} // end get_subtotal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set value before taxes, discounts, fees and etc.
|
||||
@ -384,8 +369,7 @@ class Payment extends Base_Model {
|
||||
public function set_subtotal($subtotal) {
|
||||
|
||||
$this->subtotal = $subtotal;
|
||||
|
||||
} // end set_subtotal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get refund total in this payment.
|
||||
@ -396,8 +380,7 @@ class Payment extends Base_Model {
|
||||
public function get_refund_total(): float {
|
||||
|
||||
return $this->refund_total;
|
||||
|
||||
} // end get_refund_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set refund total in this payment.
|
||||
@ -409,8 +392,7 @@ class Payment extends Base_Model {
|
||||
public function set_refund_total($refund_total): void {
|
||||
|
||||
$this->refund_total = $refund_total;
|
||||
|
||||
} // end set_refund_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount, in currency, of the tax.
|
||||
@ -421,8 +403,7 @@ class Payment extends Base_Model {
|
||||
public function get_tax_total(): float {
|
||||
|
||||
return (float) $this->tax_total;
|
||||
|
||||
} // end get_tax_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the amount, in currency, of the tax.
|
||||
@ -434,8 +415,7 @@ class Payment extends Base_Model {
|
||||
public function set_tax_total($tax_total): void {
|
||||
|
||||
$this->tax_total = $tax_total;
|
||||
|
||||
} // end set_tax_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get discount code used.
|
||||
@ -446,8 +426,7 @@ class Payment extends Base_Model {
|
||||
public function get_discount_code() {
|
||||
|
||||
return $this->discount_code;
|
||||
|
||||
} // end get_discount_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set discount code used.
|
||||
@ -459,8 +438,7 @@ class Payment extends Base_Model {
|
||||
public function set_discount_code($discount_code) {
|
||||
|
||||
$this->discount_code = $discount_code;
|
||||
|
||||
} // end set_discount_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this takes into account fees, discounts, credits, etc.
|
||||
@ -471,8 +449,7 @@ class Payment extends Base_Model {
|
||||
public function get_total(): float {
|
||||
|
||||
return (float) $this->total;
|
||||
|
||||
} // end get_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set this takes into account fees, discounts, credits, etc.
|
||||
@ -484,8 +461,7 @@ class Payment extends Base_Model {
|
||||
public function set_total($total) {
|
||||
|
||||
$this->total = $total;
|
||||
|
||||
} // end set_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Label for a given severity level.
|
||||
@ -498,8 +474,7 @@ class Payment extends Base_Model {
|
||||
$status = new Payment_Status($this->get_status());
|
||||
|
||||
return $status->get_label();
|
||||
|
||||
} // end get_status_label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the classes for a given class.
|
||||
@ -512,8 +487,7 @@ class Payment extends Base_Model {
|
||||
$status = new Payment_Status($this->get_status());
|
||||
|
||||
return $status->get_classes();
|
||||
|
||||
} // end get_status_class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status of the status.
|
||||
@ -524,8 +498,7 @@ class Payment extends Base_Model {
|
||||
public function get_status() {
|
||||
|
||||
return $this->status;
|
||||
|
||||
} // end get_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set status of the status.
|
||||
@ -538,8 +511,7 @@ class Payment extends Base_Model {
|
||||
public function set_status($status) {
|
||||
|
||||
$this->status = $status;
|
||||
|
||||
} // end set_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get gateway used to process this payment.
|
||||
@ -550,8 +522,7 @@ class Payment extends Base_Model {
|
||||
public function get_gateway() {
|
||||
|
||||
return $this->gateway;
|
||||
|
||||
} // end get_gateway;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set gateway used to process this payment.
|
||||
@ -563,8 +534,7 @@ class Payment extends Base_Model {
|
||||
public function set_gateway($gateway) {
|
||||
|
||||
$this->gateway = $gateway;
|
||||
|
||||
} // end set_gateway;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the payment method used. Usually it is the public name of the gateway.
|
||||
@ -576,25 +546,20 @@ class Payment extends Base_Model {
|
||||
|
||||
$gateway = $this->get_gateway();
|
||||
|
||||
if (!$gateway) {
|
||||
|
||||
if ( ! $gateway) {
|
||||
return __('None', 'wp-ultimo');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$gateway_class = wu_get_gateway($gateway);
|
||||
|
||||
if (!$gateway_class) {
|
||||
|
||||
if ( ! $gateway_class) {
|
||||
return __('None', 'wp-ultimo');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$title = $gateway_class->get_public_title();
|
||||
|
||||
return apply_filters("wu_gateway_{$gateway}_as_option_title", $title, $gateway_class);
|
||||
|
||||
} // end get_payment_method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the product associated to this payment.
|
||||
@ -605,8 +570,7 @@ class Payment extends Base_Model {
|
||||
public function get_product() {
|
||||
|
||||
return wu_get_product($this->product_id);
|
||||
|
||||
} // end get_product;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this payment has line items.
|
||||
@ -618,9 +582,8 @@ class Payment extends Base_Model {
|
||||
*/
|
||||
public function has_line_items(): bool {
|
||||
|
||||
return !empty($this->get_line_items());
|
||||
|
||||
} // end has_line_items;
|
||||
return ! empty($this->get_line_items());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the line items for this payment.
|
||||
@ -634,16 +597,13 @@ class Payment extends Base_Model {
|
||||
public function get_line_items(): array {
|
||||
|
||||
if ($this->line_items === null) {
|
||||
|
||||
$line_items = (array) $this->get_meta('wu_line_items');
|
||||
|
||||
$this->line_items = array_filter($line_items);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return (array) $this->line_items;
|
||||
|
||||
} // end get_line_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the line items of this payment.
|
||||
@ -660,8 +620,7 @@ class Payment extends Base_Model {
|
||||
$this->meta['wu_line_items'] = $line_items;
|
||||
|
||||
$this->line_items = $line_items;
|
||||
|
||||
} // end set_line_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new line item.
|
||||
@ -675,19 +634,16 @@ class Payment extends Base_Model {
|
||||
|
||||
$line_items = $this->get_line_items();
|
||||
|
||||
if (!is_a($line_item, self::class)) {
|
||||
|
||||
if ( ! is_a($line_item, self::class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
$line_items[$line_item->get_id()] = $line_item;
|
||||
$line_items[ $line_item->get_id() ] = $line_item;
|
||||
|
||||
$this->set_line_items($line_items);
|
||||
|
||||
krsort($this->line_items);
|
||||
|
||||
} // end add_line_item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing the subtotal per tax rate.
|
||||
@ -702,30 +658,23 @@ class Payment extends Base_Model {
|
||||
$tax_brackets = array();
|
||||
|
||||
foreach ($line_items as $line_item) {
|
||||
|
||||
$tax_bracket = $line_item->get_tax_rate();
|
||||
|
||||
if (!$tax_bracket) {
|
||||
if ( ! $tax_bracket) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($tax_brackets[ $tax_bracket ])) {
|
||||
$tax_brackets[ $tax_bracket ] += $line_item->get_tax_total();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (isset($tax_brackets[$tax_bracket])) {
|
||||
|
||||
$tax_brackets[$tax_bracket] += $line_item->get_tax_total();
|
||||
|
||||
continue;
|
||||
|
||||
} // end if;
|
||||
|
||||
$tax_brackets[$tax_bracket] = $line_item->get_tax_total();
|
||||
|
||||
} // end foreach;
|
||||
$tax_brackets[ $tax_bracket ] = $line_item->get_tax_total();
|
||||
}
|
||||
|
||||
return $tax_brackets;
|
||||
|
||||
} // end get_tax_breakthrough;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate payment totals.
|
||||
@ -748,7 +697,6 @@ class Payment extends Base_Model {
|
||||
$total = 0;
|
||||
|
||||
foreach ($line_items as $line_item) {
|
||||
|
||||
$line_item->recalculate_totals();
|
||||
|
||||
$tax_total += $line_item->get_tax_total();
|
||||
@ -758,23 +706,21 @@ class Payment extends Base_Model {
|
||||
$total += $line_item->get_total();
|
||||
|
||||
if ($line_item->get_type() === 'refund') {
|
||||
|
||||
$refund_total += $line_item->get_subtotal();
|
||||
}
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
|
||||
$this->attributes(array(
|
||||
'tax_total' => $tax_total,
|
||||
'subtotal' => $sub_total,
|
||||
'refund_total' => $refund_total,
|
||||
'total' => $total,
|
||||
));
|
||||
$this->attributes(
|
||||
array(
|
||||
'tax_total' => $tax_total,
|
||||
'subtotal' => $sub_total,
|
||||
'refund_total' => $refund_total,
|
||||
'total' => $total,
|
||||
)
|
||||
);
|
||||
|
||||
return $this;
|
||||
|
||||
} // end recalculate_totals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this payment is payable still.
|
||||
@ -784,14 +730,16 @@ class Payment extends Base_Model {
|
||||
*/
|
||||
public function is_payable(): bool {
|
||||
|
||||
$payable_statuses = apply_filters('wu_payment_payable_statuses', array(
|
||||
Payment_Status::PENDING,
|
||||
Payment_Status::FAILED,
|
||||
));
|
||||
$payable_statuses = apply_filters(
|
||||
'wu_payment_payable_statuses',
|
||||
array(
|
||||
Payment_Status::PENDING,
|
||||
Payment_Status::FAILED,
|
||||
)
|
||||
);
|
||||
|
||||
return $this->get_total() > 0 && in_array($this->get_status(), $payable_statuses, true);
|
||||
|
||||
} // end is_payable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the link to pay for this payment.
|
||||
@ -801,19 +749,19 @@ class Payment extends Base_Model {
|
||||
*/
|
||||
public function get_payment_url() {
|
||||
|
||||
if (!$this->is_payable()) {
|
||||
|
||||
if ( ! $this->is_payable()) {
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$slug = $this->get_hash();
|
||||
|
||||
return add_query_arg(array(
|
||||
'payment' => $slug,
|
||||
), wu_get_registration_url());
|
||||
|
||||
} // end get_payment_url;
|
||||
return add_query_arg(
|
||||
array(
|
||||
'payment' => $slug,
|
||||
),
|
||||
wu_get_registration_url()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get iD of the product of this payment.
|
||||
@ -824,8 +772,7 @@ class Payment extends Base_Model {
|
||||
public function get_product_id() {
|
||||
|
||||
return $this->product_id;
|
||||
|
||||
} // end get_product_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set iD of the product of this payment.
|
||||
@ -837,8 +784,7 @@ class Payment extends Base_Model {
|
||||
public function set_product_id($product_id) {
|
||||
|
||||
$this->product_id = $product_id;
|
||||
|
||||
} // end set_product_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the Invoice URL.
|
||||
@ -855,8 +801,7 @@ class Payment extends Base_Model {
|
||||
);
|
||||
|
||||
return add_query_arg($url_atts, get_site_url(wu_get_main_site_id()));
|
||||
|
||||
} // end get_invoice_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get iD of the payment on the gateway, if it exists.
|
||||
@ -867,8 +812,7 @@ class Payment extends Base_Model {
|
||||
public function get_gateway_payment_id() {
|
||||
|
||||
return $this->gateway_payment_id;
|
||||
|
||||
} // end get_gateway_payment_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set iD of the payment on the gateway, if it exists.
|
||||
@ -880,8 +824,7 @@ class Payment extends Base_Model {
|
||||
public function set_gateway_payment_id($gateway_payment_id) {
|
||||
|
||||
$this->gateway_payment_id = $gateway_payment_id;
|
||||
|
||||
} // end set_gateway_payment_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* By default, we just use the to_array method, but you can rewrite this.
|
||||
@ -900,8 +843,7 @@ class Payment extends Base_Model {
|
||||
$search_result['product_names'] = implode(', ', array_column($line_items, 'title'));
|
||||
|
||||
return $search_result;
|
||||
|
||||
} // end to_search_results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total value in discounts.
|
||||
@ -912,8 +854,7 @@ class Payment extends Base_Model {
|
||||
public function get_discount_total() {
|
||||
|
||||
return (float) $this->discount_total;
|
||||
|
||||
} // end get_discount_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the total value in discounts.
|
||||
@ -925,8 +866,7 @@ class Payment extends Base_Model {
|
||||
public function set_discount_total($discount_total) {
|
||||
|
||||
$this->discount_total = (float) $discount_total;
|
||||
|
||||
} // end set_discount_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the invoice number actually saved on the payment.
|
||||
@ -937,14 +877,11 @@ class Payment extends Base_Model {
|
||||
public function get_saved_invoice_number() {
|
||||
|
||||
if ($this->invoice_number === null) {
|
||||
|
||||
$this->invoice_number = $this->get_meta('wu_invoice_number', '');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->invoice_number;
|
||||
|
||||
} // end get_saved_invoice_number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sequential invoice number assigned to this payment.
|
||||
@ -955,26 +892,20 @@ class Payment extends Base_Model {
|
||||
public function get_invoice_number() {
|
||||
|
||||
if (wu_get_setting('invoice_numbering_scheme', 'reference_code') === 'reference_code') {
|
||||
|
||||
return $this->get_hash();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$provisional = false;
|
||||
|
||||
if ($this->invoice_number === null) {
|
||||
|
||||
$this->invoice_number = $this->get_meta('wu_invoice_number');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if ($this->invoice_number === false) {
|
||||
|
||||
$provisional = true;
|
||||
|
||||
$this->invoice_number = wu_get_setting('next_invoice_number');
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$prefix = wu_get_setting('invoice_prefix', '');
|
||||
|
||||
@ -999,8 +930,7 @@ class Payment extends Base_Model {
|
||||
$prefix = str_replace($search, $replace, (string) $prefix);
|
||||
|
||||
return sprintf('%s%s %s', $prefix, $this->invoice_number, $provisional ? __('(provisional)', 'wp-ultimo') : '');
|
||||
|
||||
} // end get_invoice_number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set sequential invoice number assigned to this payment.
|
||||
@ -1014,8 +944,7 @@ class Payment extends Base_Model {
|
||||
$this->meta['wu_invoice_number'] = $invoice_number;
|
||||
|
||||
$this->invoice_number = $invoice_number;
|
||||
|
||||
} // end set_invoice_number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all non-recurring items from the payment.
|
||||
@ -1031,22 +960,17 @@ class Payment extends Base_Model {
|
||||
$line_items = $this->get_line_items();
|
||||
|
||||
foreach ($line_items as $line_item_id => $line_item) {
|
||||
|
||||
if (!$line_item->is_recurring()) {
|
||||
|
||||
unset($line_items[$line_item_id]);
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
if ( ! $line_item->is_recurring()) {
|
||||
unset($line_items[ $line_item_id ]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->set_line_items($line_items);
|
||||
|
||||
$this->recalculate_totals();
|
||||
|
||||
return $this;
|
||||
|
||||
} // end remove_non_recurring_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get holds if we need to cancel the membership on refund..
|
||||
@ -1057,14 +981,11 @@ class Payment extends Base_Model {
|
||||
public function should_cancel_membership_on_refund() {
|
||||
|
||||
if ($this->cancel_membership_on_refund === null) {
|
||||
|
||||
$this->cancel_membership_on_refund = $this->get_meta('wu_cancel_membership_on_refund', false);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->cancel_membership_on_refund;
|
||||
|
||||
} // end should_cancel_membership_on_refund;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set holds if we need to cancel the membership on refund..
|
||||
@ -1078,8 +999,7 @@ class Payment extends Base_Model {
|
||||
$this->meta['wu_cancel_membership_on_refund'] = $cancel_membership_on_refund;
|
||||
|
||||
$this->cancel_membership_on_refund = $cancel_membership_on_refund;
|
||||
|
||||
} // end set_cancel_membership_on_refund;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a payment refund.
|
||||
@ -1108,10 +1028,8 @@ class Payment extends Base_Model {
|
||||
* refund the full amount.
|
||||
*/
|
||||
if (empty($amount)) {
|
||||
|
||||
$amount = $this->get_total();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$amount = wu_to_float($amount);
|
||||
|
||||
@ -1119,10 +1037,8 @@ class Payment extends Base_Model {
|
||||
* Do the same for the behavior regarding memberships.
|
||||
*/
|
||||
if (is_null($should_cancel_membership_on_refund)) {
|
||||
|
||||
$should_cancel_membership_on_refund = $this->should_cancel_membership_on_refund();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* First, deal with the status.
|
||||
@ -1133,18 +1049,14 @@ class Payment extends Base_Model {
|
||||
* it is a partial refund.
|
||||
*/
|
||||
if ($amount >= $this->get_total()) {
|
||||
|
||||
$title = __('Full Refund', 'wp-ultimo');
|
||||
|
||||
$new_status = Payment_Status::REFUND;
|
||||
|
||||
} else {
|
||||
|
||||
$title = __('Partial Refund', 'wp-ultimo');
|
||||
|
||||
$new_status = Payment_Status::PARTIAL_REFUND;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$time = current_time('timestamp'); // phpcs:ignore
|
||||
|
||||
@ -1175,30 +1087,23 @@ class Payment extends Base_Model {
|
||||
$status = $this->save();
|
||||
|
||||
if (is_wp_error($status)) {
|
||||
|
||||
return $status;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updating the payment went well.
|
||||
* Let's deal with the membership, if needed.
|
||||
*/
|
||||
if ($should_cancel_membership_on_refund) {
|
||||
|
||||
$membership = $this->get_membership();
|
||||
|
||||
if ($membership) {
|
||||
|
||||
$membership->cancel();
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} // end refund;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the given model adn resets it's id to a 'new' state.
|
||||
@ -1215,7 +1120,5 @@ class Payment extends Base_Model {
|
||||
$new_payment->set_line_items($line_items);
|
||||
|
||||
return $new_payment;
|
||||
|
||||
} // end duplicate;
|
||||
|
||||
} // end class Payment;
|
||||
}
|
||||
}
|
||||
|
@ -104,8 +104,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function get_author_id() {
|
||||
|
||||
return $this->author_id;
|
||||
|
||||
} // end get_author_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set author ID.
|
||||
@ -115,8 +114,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function set_author_id($author_id) {
|
||||
|
||||
$this->author_id = $author_id;
|
||||
|
||||
} // end set_author_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post type.
|
||||
@ -126,8 +124,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function get_type() {
|
||||
|
||||
return $this->type;
|
||||
|
||||
} // end get_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set post type.
|
||||
@ -137,8 +134,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function set_type($type) {
|
||||
|
||||
$this->type = $type;
|
||||
|
||||
} // end set_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post title.
|
||||
@ -148,8 +144,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function get_title() {
|
||||
|
||||
return $this->title;
|
||||
|
||||
} // end get_title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set post title.
|
||||
@ -159,8 +154,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function set_title($title) {
|
||||
|
||||
$this->title = $title;
|
||||
|
||||
} // end set_title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post content.
|
||||
@ -173,8 +167,7 @@ class Post_Base_Model extends Base_Model {
|
||||
* Also we need to add the paragraphs and line breaks back.
|
||||
*/
|
||||
return wpautop(stripslashes($this->content));
|
||||
|
||||
} // end get_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set post content.
|
||||
@ -184,8 +177,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function set_content($content) {
|
||||
|
||||
$this->content = $content;
|
||||
|
||||
} // end set_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post excerpt.
|
||||
@ -195,8 +187,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function get_excerpt() {
|
||||
|
||||
return $this->excerpt;
|
||||
|
||||
} // end get_excerpt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set post excerpt.
|
||||
@ -206,8 +197,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function set_excerpt($excerpt) {
|
||||
|
||||
$this->excerpt = $excerpt;
|
||||
|
||||
} // end set_excerpt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post creation date.
|
||||
@ -217,8 +207,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function get_date_created() {
|
||||
|
||||
return $this->date_created;
|
||||
|
||||
} // end get_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set post creation date.
|
||||
@ -228,8 +217,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function set_date_created($date_created) {
|
||||
|
||||
$this->date_created = $date_created;
|
||||
|
||||
} // end set_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post last modification date.
|
||||
@ -239,8 +227,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function get_date_modified() {
|
||||
|
||||
return $this->date_modified;
|
||||
|
||||
} // end get_date_modified;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set post last modification date.
|
||||
@ -250,8 +237,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function set_date_modified($date_modified) {
|
||||
|
||||
$this->date_modified = $date_modified;
|
||||
|
||||
} // end set_date_modified;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the post list order.
|
||||
@ -261,8 +247,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function get_list_order() {
|
||||
|
||||
return $this->list_order;
|
||||
|
||||
} // end get_list_order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the post list order.
|
||||
@ -272,8 +257,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function set_list_order($list_order) {
|
||||
|
||||
$this->list_order = $list_order;
|
||||
|
||||
} // end set_list_order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the post status.
|
||||
@ -283,8 +267,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function get_status() {
|
||||
|
||||
return $this->status;
|
||||
|
||||
} // end get_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the post status.
|
||||
@ -294,8 +277,7 @@ class Post_Base_Model extends Base_Model {
|
||||
public function set_status($status) {
|
||||
|
||||
$this->status = $status;
|
||||
|
||||
} // end set_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save (create or update) the model on the database,
|
||||
@ -306,13 +288,11 @@ class Post_Base_Model extends Base_Model {
|
||||
*/
|
||||
public function save() {
|
||||
|
||||
if (!$this->author_id) {
|
||||
|
||||
if ( ! $this->author_id) {
|
||||
$this->author_id = get_current_user_id();
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!$this->status) {
|
||||
if ( ! $this->status) {
|
||||
|
||||
/**
|
||||
* Filters the object data before it is stored into the database.
|
||||
@ -324,11 +304,8 @@ class Post_Base_Model extends Base_Model {
|
||||
* @param Base_Model $this The object instance.
|
||||
*/
|
||||
$this->status = apply_filters('wu_post_default_status', 'draft', $this->type, $this);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return parent::save();
|
||||
|
||||
} // end save;
|
||||
|
||||
} // end class Post_Base_Model;
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -121,10 +121,9 @@ class Webhook extends Base_Model {
|
||||
'active' => 'default:1',
|
||||
'hidden' => 'default:0',
|
||||
'integration' => 'required|min:2',
|
||||
'date_last_failed' => 'default:'
|
||||
'date_last_failed' => 'default:',
|
||||
);
|
||||
|
||||
} // end validation_rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of name.
|
||||
@ -134,8 +133,7 @@ class Webhook extends Base_Model {
|
||||
public function get_name() {
|
||||
|
||||
return $this->name;
|
||||
|
||||
} // end get_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of name.
|
||||
@ -145,8 +143,7 @@ class Webhook extends Base_Model {
|
||||
public function set_name($name) {
|
||||
|
||||
$this->name = $name;
|
||||
|
||||
} // end set_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of webhook_url.
|
||||
@ -156,8 +153,7 @@ class Webhook extends Base_Model {
|
||||
public function get_webhook_url() {
|
||||
|
||||
return $this->webhook_url;
|
||||
|
||||
} // end get_webhook_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of webhook_url.
|
||||
@ -167,8 +163,7 @@ class Webhook extends Base_Model {
|
||||
public function set_webhook_url($webhook_url) {
|
||||
|
||||
$this->webhook_url = $webhook_url;
|
||||
|
||||
} // end set_webhook_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of event.
|
||||
@ -178,8 +173,7 @@ class Webhook extends Base_Model {
|
||||
public function get_event() {
|
||||
|
||||
return $this->event;
|
||||
|
||||
} // end get_event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of event.
|
||||
@ -189,8 +183,7 @@ class Webhook extends Base_Model {
|
||||
public function set_event($event) {
|
||||
|
||||
$this->event = $event;
|
||||
|
||||
} // end set_event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of event_count.
|
||||
@ -200,8 +193,7 @@ class Webhook extends Base_Model {
|
||||
public function get_event_count() {
|
||||
|
||||
return (int) $this->event_count;
|
||||
|
||||
} // end get_event_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of event_count.
|
||||
@ -211,8 +203,7 @@ class Webhook extends Base_Model {
|
||||
public function set_event_count($event_count) {
|
||||
|
||||
$this->event_count = $event_count;
|
||||
|
||||
} // end set_event_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this particular mapping is active.
|
||||
@ -223,8 +214,7 @@ class Webhook extends Base_Model {
|
||||
public function is_active() {
|
||||
|
||||
return (bool) $this->active;
|
||||
|
||||
} // end is_active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the active state of this model object;
|
||||
@ -237,8 +227,7 @@ class Webhook extends Base_Model {
|
||||
public function set_active($active) {
|
||||
|
||||
$this->active = (bool) wu_string_to_bool($active);
|
||||
|
||||
} // end set_active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get is this webhook hidden?
|
||||
@ -248,8 +237,7 @@ class Webhook extends Base_Model {
|
||||
public function is_hidden() {
|
||||
|
||||
return (bool) $this->hidden;
|
||||
|
||||
} // end is_hidden;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set is this webhook hidden?
|
||||
@ -259,8 +247,7 @@ class Webhook extends Base_Model {
|
||||
public function set_hidden($hidden) {
|
||||
|
||||
$this->hidden = $hidden;
|
||||
|
||||
} // end set_hidden;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get integration name.
|
||||
@ -270,8 +257,7 @@ class Webhook extends Base_Model {
|
||||
public function get_integration() {
|
||||
|
||||
return $this->integration;
|
||||
|
||||
} // end get_integration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get date when this was created..
|
||||
@ -282,8 +268,7 @@ class Webhook extends Base_Model {
|
||||
public function get_date_created() {
|
||||
|
||||
return $this->date_created;
|
||||
|
||||
} // end get_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get date when this was created..
|
||||
@ -294,8 +279,7 @@ class Webhook extends Base_Model {
|
||||
public function get_date_last_failed() {
|
||||
|
||||
return $this->date_last_failed;
|
||||
|
||||
} // end get_date_last_failed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set date when this was created..
|
||||
@ -307,8 +291,7 @@ class Webhook extends Base_Model {
|
||||
public function set_date_created($date_created) {
|
||||
|
||||
$this->date_created = $date_created;
|
||||
|
||||
} // end set_date_created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set integration name.
|
||||
@ -318,7 +301,5 @@ class Webhook extends Base_Model {
|
||||
public function set_integration($integration) {
|
||||
|
||||
$this->integration = $integration;
|
||||
|
||||
} // end set_integration;
|
||||
|
||||
} // end class Webhook;
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
namespace WP_Ultimo\Models\Traits;
|
||||
|
||||
use \WP_Ultimo\Objects\Billing_Address;
|
||||
use WP_Ultimo\Objects\Billing_Address;
|
||||
|
||||
/**
|
||||
* Singleton trait.
|
||||
@ -44,16 +44,13 @@ trait Billable {
|
||||
public function get_billing_address() {
|
||||
|
||||
if ($this->billing_address === null) {
|
||||
|
||||
$billing_address = $this->get_meta('wu_billing_address');
|
||||
|
||||
$this->billing_address = is_a($billing_address, '\WP_Ultimo\Objects\Billing_Address') ? $billing_address : $this->get_default_billing_address();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->billing_address;
|
||||
|
||||
} // end get_billing_address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the billing address.
|
||||
@ -66,15 +63,11 @@ trait Billable {
|
||||
public function set_billing_address($billing_address) {
|
||||
|
||||
if (is_array($billing_address)) {
|
||||
|
||||
$billing_address = new Billing_Address($billing_address);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$this->meta['wu_billing_address'] = $billing_address;
|
||||
|
||||
$this->billing_address = $billing_address;
|
||||
|
||||
} // end set_billing_address;
|
||||
|
||||
} // end trait Billable;
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,8 @@
|
||||
|
||||
namespace WP_Ultimo\Models\Traits;
|
||||
|
||||
use \WP_Ultimo\Database\Sites\Site_Type;
|
||||
use \WP_Ultimo\Objects\Limitations;
|
||||
use WP_Ultimo\Database\Sites\Site_Type;
|
||||
use WP_Ultimo\Objects\Limitations;
|
||||
|
||||
/**
|
||||
* Singleton trait.
|
||||
@ -54,10 +54,8 @@ trait Limitable {
|
||||
* This is because we don't want to limit sites other than the customer owned ones.
|
||||
*/
|
||||
if ($this->model === 'site' && $this->get_type() !== Site_Type::CUSTOMER_OWNED) {
|
||||
|
||||
return new Limitations(array());
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$cache_key = $waterfall ? '_composite_limitations_' : '_limitations_';
|
||||
|
||||
@ -67,32 +65,23 @@ trait Limitable {
|
||||
|
||||
$cached_version = wu_get_isset($this->_limitations, $cache_key);
|
||||
|
||||
if (!empty($cached_version)) {
|
||||
|
||||
if ( ! empty($cached_version)) {
|
||||
return $cached_version;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!is_array($this->meta)) {
|
||||
|
||||
if ( ! is_array($this->meta)) {
|
||||
$this->meta = array();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if (did_action('muplugins_loaded') === false) {
|
||||
|
||||
$modules_data = $this->get_meta('wu_limitations', array());
|
||||
|
||||
} else {
|
||||
|
||||
$modules_data = Limitations::early_get_limitations($this->model, $this->get_id());
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$limitations = new Limitations(array());
|
||||
|
||||
if ($waterfall) {
|
||||
|
||||
$limitations = $limitations->merge(...$this->limitations_to_merge());
|
||||
|
||||
/**
|
||||
@ -102,23 +91,17 @@ trait Limitable {
|
||||
* This will return only the parents permissions and is super useful for
|
||||
* comparisons.
|
||||
*/
|
||||
if (!$skip_self) {
|
||||
|
||||
if ( ! $skip_self) {
|
||||
$limitations = $limitations->merge(true, $modules_data);
|
||||
|
||||
} // end if;
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
$limitations = $limitations->merge($modules_data);
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
$this->_limitations[$cache_key] = $limitations;
|
||||
$this->_limitations[ $cache_key ] = $limitations;
|
||||
|
||||
return $limitations;
|
||||
|
||||
} // end get_limitations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this site has limitations or not.
|
||||
@ -129,8 +112,7 @@ trait Limitable {
|
||||
public function has_limitations() {
|
||||
|
||||
return $this->get_limitations()->has_limitations();
|
||||
|
||||
} // end has_limitations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a particular module is being limited.
|
||||
@ -143,8 +125,7 @@ trait Limitable {
|
||||
public function has_module_limitation($module) {
|
||||
|
||||
return $this->get_limitations()->is_module_enabled($module);
|
||||
|
||||
} // end has_module_limitation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all user role quotas.
|
||||
@ -155,8 +136,7 @@ trait Limitable {
|
||||
public function get_user_role_quotas() {
|
||||
|
||||
return $this->get_limitations()->get_user_role_quotas();
|
||||
|
||||
} // end get_user_role_quotas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy method to retrieve the allowed user roles.
|
||||
@ -167,8 +147,7 @@ trait Limitable {
|
||||
public function get_allowed_user_roles() {
|
||||
|
||||
return $this->get_limitations()->get_allowed_user_roles();
|
||||
|
||||
} // end get_allowed_user_roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules plugins to be activated or deactivated based on the current limitations;
|
||||
@ -181,50 +160,35 @@ trait Limitable {
|
||||
$sites = array();
|
||||
|
||||
if ($this->model === 'site') {
|
||||
|
||||
$sites[] = $this;
|
||||
|
||||
} elseif ($this->model === 'membership') {
|
||||
|
||||
$sites = $this->get_sites();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
foreach ($sites as $site_object) {
|
||||
|
||||
if (!$site_object->get_id() || $site_object->get_type() !== Site_Type::CUSTOMER_OWNED) {
|
||||
|
||||
if ( ! $site_object->get_id() || $site_object->get_type() !== Site_Type::CUSTOMER_OWNED) {
|
||||
continue;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$site_id = $site_object->get_id();
|
||||
$limitations = $site_object->get_limitations();
|
||||
|
||||
if (!$limitations->plugins->is_enabled()) {
|
||||
|
||||
if ( ! $limitations->plugins->is_enabled()) {
|
||||
continue;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$plugins_to_deactivate = $limitations->plugins->get_by_type('force_inactive');
|
||||
$plugins_to_activate = $limitations->plugins->get_by_type('force_active');
|
||||
|
||||
if ($plugins_to_deactivate) {
|
||||
|
||||
wu_async_deactivate_plugins($site_id, array_keys($plugins_to_deactivate));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if ($plugins_to_activate) {
|
||||
|
||||
wu_async_activate_plugins($site_id, array_keys($plugins_to_activate));
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
|
||||
} // end sync_plugins;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes sure we save limitations when we are supposed to.
|
||||
@ -241,11 +205,9 @@ trait Limitable {
|
||||
/*
|
||||
* Only handle limitations if there are to handle in the first place.
|
||||
*/
|
||||
if (!wu_request('modules')) {
|
||||
|
||||
if ( ! wu_request('modules')) {
|
||||
return;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$object_limitations = $this->get_limitations(false);
|
||||
|
||||
@ -258,22 +220,16 @@ trait Limitable {
|
||||
$current_limitations = $this->get_limitations(true, true);
|
||||
|
||||
foreach ($limitations as $limitation_id => $class_name) {
|
||||
|
||||
$module = wu_get_isset($saved_limitations, $limitation_id, array());
|
||||
|
||||
try {
|
||||
|
||||
if (is_string($module)) {
|
||||
|
||||
$module = json_decode($module, true);
|
||||
|
||||
} // end if;
|
||||
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
|
||||
// Silence is golden.
|
||||
|
||||
} // end try;
|
||||
}
|
||||
|
||||
$module['enabled'] = $object_limitations->{$limitation_id}->handle_enabled();
|
||||
|
||||
@ -282,32 +238,23 @@ trait Limitable {
|
||||
$module = $object_limitations->{$limitation_id}->handle_others($module);
|
||||
|
||||
if ($module) {
|
||||
|
||||
$modules_to_save[$limitation_id] = $module;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
$modules_to_save[ $limitation_id ] = $module;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->model !== 'product') {
|
||||
/*
|
||||
* Set the new permissions, based on the diff.
|
||||
*/
|
||||
$limitations = wu_array_recursive_diff($modules_to_save, $current_limitations->to_array());
|
||||
|
||||
} elseif ($this->model === 'product' && $this->get_type() !== 'plan') {
|
||||
|
||||
$limitations = wu_array_recursive_diff($modules_to_save, Limitations::get_empty()->to_array());
|
||||
|
||||
} else {
|
||||
|
||||
$limitations = $modules_to_save;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$this->meta['wu_limitations'] = $limitations;
|
||||
|
||||
} // end handle_limitations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of product slugs associated with this model.
|
||||
@ -318,31 +265,22 @@ trait Limitable {
|
||||
public function get_applicable_product_slugs() {
|
||||
|
||||
if ($this->model === 'product') {
|
||||
|
||||
return array($this->get_slug());
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$slugs = array();
|
||||
|
||||
if ($this->model === 'membership') {
|
||||
|
||||
$membership = $this;
|
||||
|
||||
} elseif ($this->model === 'site') {
|
||||
|
||||
$membership = $this->get_membership();
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!empty($membership)) {
|
||||
|
||||
if ( ! empty($membership)) {
|
||||
$slugs = array_column(array_map('wu_cast_model_to_array', array_column($membership->get_all_products(), 'product')), 'slug'); // WOW
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $slugs;
|
||||
|
||||
} // end get_applicable_product_slugs;
|
||||
|
||||
} // end trait Limitable;
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
namespace WP_Ultimo\Models\Traits;
|
||||
|
||||
use \WP_Ultimo\Objects\Note;
|
||||
use WP_Ultimo\Objects\Note;
|
||||
|
||||
/**
|
||||
* Singleton trait.
|
||||
@ -33,14 +33,11 @@ trait Notable {
|
||||
public function get_notes() {
|
||||
|
||||
if ($this->notes === null) {
|
||||
|
||||
$this->notes = get_metadata($this->get_meta_data_table_name(), $this->get_id(), 'wu_note', false);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $this->notes;
|
||||
|
||||
} // end get_notes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new note to this model.
|
||||
@ -52,25 +49,20 @@ trait Notable {
|
||||
*/
|
||||
public function add_note($note) {
|
||||
|
||||
if (!is_a($note, 'Note')) {
|
||||
|
||||
if ( ! is_a($note, 'Note')) {
|
||||
$note = new Note($note);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$status = $note->validate();
|
||||
|
||||
if (is_wp_error($status)) {
|
||||
|
||||
return $status;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$status = add_metadata($this->get_meta_data_table_name(), $this->get_id(), 'wu_note', $note, false);
|
||||
|
||||
return $status;
|
||||
|
||||
} // end add_note;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all notes related to this model.
|
||||
@ -83,8 +75,7 @@ trait Notable {
|
||||
$status = delete_metadata($this->get_meta_data_table_name(), $this->get_id(), 'wu_note', '', true);
|
||||
|
||||
return $status;
|
||||
|
||||
} // end clear_notes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove one note related to this model.
|
||||
@ -104,9 +95,7 @@ trait Notable {
|
||||
$mid = false;
|
||||
|
||||
foreach ($notes as $note) {
|
||||
|
||||
if ($note->note_id && $note->note_id === $note_id) {
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$prefix = $wpdb->base_prefix;
|
||||
@ -116,36 +105,27 @@ trait Notable {
|
||||
$column_name = "wu_{$model}_id";
|
||||
|
||||
if ($model === 'site') {
|
||||
|
||||
$table_name = "{$wpdb->base_prefix}blogmeta";
|
||||
|
||||
$column_name = 'blog_id';
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$mid = $wpdb->get_row($wpdb->prepare("SELECT meta_id FROM $table_name WHERE $column_name = %d AND meta_key = %s AND meta_value = %s", $this->get_id(), 'wu_note', maybe_serialize($note)), ARRAY_A); // phpcs:ignore
|
||||
}
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
|
||||
if (!$mid) {
|
||||
|
||||
if ( ! $mid) {
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$status = delete_metadata_by_mid("wu_{$model}", $mid['meta_id']);
|
||||
|
||||
if ($model === 'site') {
|
||||
|
||||
$status = delete_metadata_by_mid('blog', $mid['meta_id']);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return $status;
|
||||
|
||||
} // end delete_note;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the meta data meta table.
|
||||
@ -160,12 +140,10 @@ trait Notable {
|
||||
$query_class = new $this->query_class();
|
||||
|
||||
// Maybe apply table prefix
|
||||
$table = !empty($query_class->prefix)
|
||||
$table = ! empty($query_class->prefix)
|
||||
? "{$query_class->prefix}_{$query_class->item_name}"
|
||||
: $query_class->item_name;
|
||||
|
||||
return $table;
|
||||
|
||||
} // end get_meta_data_table_name;
|
||||
|
||||
} // end trait Notable;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user