initial commit
This commit is contained in:
858
includes/abstracts/abstract-wc-data.php
Normal file
858
includes/abstracts/abstract-wc-data.php
Normal file
@ -0,0 +1,858 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract Data.
|
||||
*
|
||||
* Handles generic data interaction which is implemented by
|
||||
* the different data store classes.
|
||||
*
|
||||
* @class WC_Data
|
||||
* @version 3.0.0
|
||||
* @package WooCommerce\Classes
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract WC Data Class
|
||||
*
|
||||
* Implemented by classes using the same CRUD(s) pattern.
|
||||
*
|
||||
* @version 2.6.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
abstract class WC_Data {
|
||||
|
||||
/**
|
||||
* ID for this object.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var int
|
||||
*/
|
||||
protected $id = 0;
|
||||
|
||||
/**
|
||||
* Core data for this object. Name value pairs (name + default value).
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var array
|
||||
*/
|
||||
protected $data = array();
|
||||
|
||||
/**
|
||||
* Core data changes for this object.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var array
|
||||
*/
|
||||
protected $changes = array();
|
||||
|
||||
/**
|
||||
* This is false until the object is read from the DB.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var bool
|
||||
*/
|
||||
protected $object_read = false;
|
||||
|
||||
/**
|
||||
* This is the name of this object type.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var string
|
||||
*/
|
||||
protected $object_type = 'data';
|
||||
|
||||
/**
|
||||
* Extra data for this object. Name value pairs (name + default value).
|
||||
* Used as a standard way for sub classes (like product types) to add
|
||||
* additional information to an inherited class.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var array
|
||||
*/
|
||||
protected $extra_data = array();
|
||||
|
||||
/**
|
||||
* Set to _data on construct so we can track and reset data if needed.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var array
|
||||
*/
|
||||
protected $default_data = array();
|
||||
|
||||
/**
|
||||
* Contains a reference to the data store for this class.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var object
|
||||
*/
|
||||
protected $data_store;
|
||||
|
||||
/**
|
||||
* Stores meta in cache for future reads.
|
||||
* A group must be set to to enable caching.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var string
|
||||
*/
|
||||
protected $cache_group = '';
|
||||
|
||||
/**
|
||||
* Stores additional meta data.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @var array
|
||||
*/
|
||||
protected $meta_data = null;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*
|
||||
* @param int|object|array $read ID to load from the DB (optional) or already queried data.
|
||||
*/
|
||||
public function __construct( $read = 0 ) {
|
||||
$this->data = array_merge( $this->data, $this->extra_data );
|
||||
$this->default_data = $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only store the object ID to avoid serializing the data object instance.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __sleep() {
|
||||
return array( 'id' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-run the constructor with the object ID.
|
||||
*
|
||||
* If the object no longer exists, remove the ID.
|
||||
*/
|
||||
public function __wakeup() {
|
||||
try {
|
||||
$this->__construct( absint( $this->id ) );
|
||||
} catch ( Exception $e ) {
|
||||
$this->set_id( 0 );
|
||||
$this->set_object_read( true );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* When the object is cloned, make sure meta is duplicated correctly.
|
||||
*
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public function __clone() {
|
||||
$this->maybe_read_meta_data();
|
||||
if ( ! empty( $this->meta_data ) ) {
|
||||
foreach ( $this->meta_data as $array_key => $meta ) {
|
||||
$this->meta_data[ $array_key ] = clone $meta;
|
||||
if ( ! empty( $meta->id ) ) {
|
||||
$this->meta_data[ $array_key ]->id = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data store.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @return object
|
||||
*/
|
||||
public function get_data_store() {
|
||||
return $this->data_store;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unique ID for this object.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return int
|
||||
*/
|
||||
public function get_id() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an object, set the ID to 0, and return result.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param bool $force_delete Should the date be deleted permanently.
|
||||
* @return bool result
|
||||
*/
|
||||
public function delete( $force_delete = false ) {
|
||||
if ( $this->data_store ) {
|
||||
$this->data_store->delete( $this, array( 'force_delete' => $force_delete ) );
|
||||
$this->set_id( 0 );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save should create or update based on object existence.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return int
|
||||
*/
|
||||
public function save() {
|
||||
if ( ! $this->data_store ) {
|
||||
return $this->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger action before saving to the DB. Allows you to adjust object props before save.
|
||||
*
|
||||
* @param WC_Data $this The object being saved.
|
||||
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
|
||||
*/
|
||||
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
|
||||
|
||||
if ( $this->get_id() ) {
|
||||
$this->data_store->update( $this );
|
||||
} else {
|
||||
$this->data_store->create( $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger action after saving to the DB.
|
||||
*
|
||||
* @param WC_Data $this The object being saved.
|
||||
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
|
||||
*/
|
||||
do_action( 'woocommerce_after_' . $this->object_type . '_object_save', $this, $this->data_store );
|
||||
|
||||
return $this->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Change data to JSON format.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return string Data in JSON format.
|
||||
*/
|
||||
public function __toString() {
|
||||
return wp_json_encode( $this->get_data() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all data for this object.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return array
|
||||
*/
|
||||
public function get_data() {
|
||||
return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of expected data keys for this object.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @return array
|
||||
*/
|
||||
public function get_data_keys() {
|
||||
return array_keys( $this->data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all "extra" data keys for an object (for sub objects like product types).
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @return array
|
||||
*/
|
||||
public function get_extra_data_keys() {
|
||||
return array_keys( $this->extra_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter null meta values from array.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param mixed $meta Meta value to check.
|
||||
* @return bool
|
||||
*/
|
||||
protected function filter_null_meta( $meta ) {
|
||||
return ! is_null( $meta->value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get All Meta Data.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return array of objects.
|
||||
*/
|
||||
public function get_meta_data() {
|
||||
$this->maybe_read_meta_data();
|
||||
return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the key is an internal one.
|
||||
*
|
||||
* @since 3.2.0
|
||||
* @param string $key Key to check.
|
||||
* @return bool true if it's an internal key, false otherwise
|
||||
*/
|
||||
protected function is_internal_meta_key( $key ) {
|
||||
$internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true );
|
||||
|
||||
if ( ! $internal_meta_key ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) );
|
||||
|
||||
if ( ! $has_setter_or_getter ) {
|
||||
return false;
|
||||
}
|
||||
/* translators: %s: $key Key to check */
|
||||
wc_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'woocommerce' ), $key ), '3.2.0' );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Meta Data by Key.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $key Meta Key.
|
||||
* @param bool $single return first found meta with key, or all with $key.
|
||||
* @param string $context What the value is for. Valid values are view and edit.
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_meta( $key = '', $single = true, $context = 'view' ) {
|
||||
if ( $this->is_internal_meta_key( $key ) ) {
|
||||
$function = 'get_' . $key;
|
||||
|
||||
if ( is_callable( array( $this, $function ) ) ) {
|
||||
return $this->{$function}();
|
||||
}
|
||||
}
|
||||
|
||||
$this->maybe_read_meta_data();
|
||||
$meta_data = $this->get_meta_data();
|
||||
$array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true );
|
||||
$value = $single ? '' : array();
|
||||
|
||||
if ( ! empty( $array_keys ) ) {
|
||||
// We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()).
|
||||
if ( $single ) {
|
||||
$value = $meta_data[ current( $array_keys ) ]->value;
|
||||
} else {
|
||||
$value = array_intersect_key( $meta_data, array_flip( $array_keys ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( 'view' === $context ) {
|
||||
$value = apply_filters( $this->get_hook_prefix() . $key, $value, $this );
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* See if meta data exists, since get_meta always returns a '' or array().
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param string $key Meta Key.
|
||||
* @return boolean
|
||||
*/
|
||||
public function meta_exists( $key = '' ) {
|
||||
$this->maybe_read_meta_data();
|
||||
$array_keys = wp_list_pluck( $this->get_meta_data(), 'key' );
|
||||
return in_array( $key, $array_keys, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all meta data from array.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param array $data Key/Value pairs.
|
||||
*/
|
||||
public function set_meta_data( $data ) {
|
||||
if ( ! empty( $data ) && is_array( $data ) ) {
|
||||
$this->maybe_read_meta_data();
|
||||
foreach ( $data as $meta ) {
|
||||
$meta = (array) $meta;
|
||||
if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) {
|
||||
$this->meta_data[] = new WC_Meta_Data(
|
||||
array(
|
||||
'id' => $meta['id'],
|
||||
'key' => $meta['key'],
|
||||
'value' => $meta['value'],
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add meta data.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*
|
||||
* @param string $key Meta key.
|
||||
* @param string|array $value Meta value.
|
||||
* @param bool $unique Should this be a unique key?.
|
||||
*/
|
||||
public function add_meta_data( $key, $value, $unique = false ) {
|
||||
if ( $this->is_internal_meta_key( $key ) ) {
|
||||
$function = 'set_' . $key;
|
||||
|
||||
if ( is_callable( array( $this, $function ) ) ) {
|
||||
return $this->{$function}( $value );
|
||||
}
|
||||
}
|
||||
|
||||
$this->maybe_read_meta_data();
|
||||
if ( $unique ) {
|
||||
$this->delete_meta_data( $key );
|
||||
}
|
||||
$this->meta_data[] = new WC_Meta_Data(
|
||||
array(
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update meta data by key or ID, if provided.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*
|
||||
* @param string $key Meta key.
|
||||
* @param string|array $value Meta value.
|
||||
* @param int $meta_id Meta ID.
|
||||
*/
|
||||
public function update_meta_data( $key, $value, $meta_id = 0 ) {
|
||||
if ( $this->is_internal_meta_key( $key ) ) {
|
||||
$function = 'set_' . $key;
|
||||
|
||||
if ( is_callable( array( $this, $function ) ) ) {
|
||||
return $this->{$function}( $value );
|
||||
}
|
||||
}
|
||||
|
||||
$this->maybe_read_meta_data();
|
||||
|
||||
$array_key = false;
|
||||
|
||||
if ( $meta_id ) {
|
||||
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true );
|
||||
$array_key = $array_keys ? current( $array_keys ) : false;
|
||||
} else {
|
||||
// Find matches by key.
|
||||
$matches = array();
|
||||
foreach ( $this->meta_data as $meta_data_array_key => $meta ) {
|
||||
if ( $meta->key === $key ) {
|
||||
$matches[] = $meta_data_array_key;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $matches ) ) {
|
||||
// Set matches to null so only one key gets the new value.
|
||||
foreach ( $matches as $meta_data_array_key ) {
|
||||
$this->meta_data[ $meta_data_array_key ]->value = null;
|
||||
}
|
||||
$array_key = current( $matches );
|
||||
}
|
||||
}
|
||||
|
||||
if ( false !== $array_key ) {
|
||||
$meta = $this->meta_data[ $array_key ];
|
||||
$meta->key = $key;
|
||||
$meta->value = $value;
|
||||
} else {
|
||||
$this->add_meta_data( $key, $value, true );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete meta data.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $key Meta key.
|
||||
*/
|
||||
public function delete_meta_data( $key ) {
|
||||
$this->maybe_read_meta_data();
|
||||
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true );
|
||||
|
||||
if ( $array_keys ) {
|
||||
foreach ( $array_keys as $array_key ) {
|
||||
$this->meta_data[ $array_key ]->value = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete meta data.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param int $mid Meta ID.
|
||||
*/
|
||||
public function delete_meta_data_by_mid( $mid ) {
|
||||
$this->maybe_read_meta_data();
|
||||
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true );
|
||||
|
||||
if ( $array_keys ) {
|
||||
foreach ( $array_keys as $array_key ) {
|
||||
$this->meta_data[ $array_key ]->value = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read meta data if null.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
protected function maybe_read_meta_data() {
|
||||
if ( is_null( $this->meta_data ) ) {
|
||||
$this->read_meta_data();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to compute meta cache key. Different from WP Meta cache key in that meta data cached using this key also contains meta_id column.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_meta_cache_key() {
|
||||
if ( ! $this->get_id() ) {
|
||||
wc_doing_it_wrong( 'get_meta_cache_key', 'ID needs to be set before fetching a cache key.', '4.7.0' );
|
||||
return false;
|
||||
}
|
||||
return self::generate_meta_cache_key( $this->get_id(), $this->cache_group );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate cache key from id and group.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @param int|string $id Object ID.
|
||||
* @param string $cache_group Group name use to store cache. Whole group cache can be invalidated in one go.
|
||||
*
|
||||
* @return string Meta cache key.
|
||||
*/
|
||||
public static function generate_meta_cache_key( $id, $cache_group ) {
|
||||
return WC_Cache_Helper::get_cache_prefix( $cache_group ) . WC_Cache_Helper::get_cache_prefix( 'object_' . $id ) . 'object_meta_' . $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prime caches for raw meta data. This includes meta_id column as well, which is not included by default in WP meta data.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @param array $raw_meta_data_collection Array of objects of { object_id => array( meta_row_1, meta_row_2, ... }.
|
||||
* @param string $cache_group Name of cache group.
|
||||
*/
|
||||
public static function prime_raw_meta_data_cache( $raw_meta_data_collection, $cache_group ) {
|
||||
foreach ( $raw_meta_data_collection as $object_id => $raw_meta_data_array ) {
|
||||
$cache_key = self::generate_meta_cache_key( $object_id, $cache_group );
|
||||
wp_cache_set( $cache_key, $raw_meta_data_array, $cache_group );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read Meta Data from the database. Ignore any internal properties.
|
||||
* Uses it's own caches because get_metadata does not provide meta_ids.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param bool $force_read True to force a new DB read (and update cache).
|
||||
*/
|
||||
public function read_meta_data( $force_read = false ) {
|
||||
$this->meta_data = array();
|
||||
$cache_loaded = false;
|
||||
|
||||
if ( ! $this->get_id() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $this->data_store ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! empty( $this->cache_group ) ) {
|
||||
// Prefix by group allows invalidation by group until https://core.trac.wordpress.org/ticket/4476 is implemented.
|
||||
$cache_key = $this->get_meta_cache_key();
|
||||
}
|
||||
|
||||
if ( ! $force_read ) {
|
||||
if ( ! empty( $this->cache_group ) ) {
|
||||
$cached_meta = wp_cache_get( $cache_key, $this->cache_group );
|
||||
$cache_loaded = ! empty( $cached_meta );
|
||||
}
|
||||
}
|
||||
|
||||
// We filter the raw meta data again when loading from cache, in case we cached in an earlier version where filter conditions were different.
|
||||
$raw_meta_data = $cache_loaded ? $this->data_store->filter_raw_meta_data( $this, $cached_meta ) : $this->data_store->read_meta( $this );
|
||||
|
||||
if ( $raw_meta_data ) {
|
||||
foreach ( $raw_meta_data as $meta ) {
|
||||
$this->meta_data[] = new WC_Meta_Data(
|
||||
array(
|
||||
'id' => (int) $meta->meta_id,
|
||||
'key' => $meta->meta_key,
|
||||
'value' => maybe_unserialize( $meta->meta_value ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! $cache_loaded && ! empty( $this->cache_group ) ) {
|
||||
wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Meta Data in the database.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function save_meta_data() {
|
||||
if ( ! $this->data_store || is_null( $this->meta_data ) ) {
|
||||
return;
|
||||
}
|
||||
foreach ( $this->meta_data as $array_key => $meta ) {
|
||||
if ( is_null( $meta->value ) ) {
|
||||
if ( ! empty( $meta->id ) ) {
|
||||
$this->data_store->delete_meta( $this, $meta );
|
||||
unset( $this->meta_data[ $array_key ] );
|
||||
}
|
||||
} elseif ( empty( $meta->id ) ) {
|
||||
$meta->id = $this->data_store->add_meta( $this, $meta );
|
||||
$meta->apply_changes();
|
||||
} else {
|
||||
if ( $meta->get_changes() ) {
|
||||
$this->data_store->update_meta( $this, $meta );
|
||||
$meta->apply_changes();
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( ! empty( $this->cache_group ) ) {
|
||||
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . WC_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id();
|
||||
wp_cache_delete( $cache_key, $this->cache_group );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param int $id ID.
|
||||
*/
|
||||
public function set_id( $id ) {
|
||||
$this->id = absint( $id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all props to default values.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function set_defaults() {
|
||||
$this->data = $this->default_data;
|
||||
$this->changes = array();
|
||||
$this->set_object_read( false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set object read property.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param boolean $read Should read?.
|
||||
*/
|
||||
public function set_object_read( $read = true ) {
|
||||
$this->object_read = (bool) $read;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get object read property.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @return boolean
|
||||
*/
|
||||
public function get_object_read() {
|
||||
return (bool) $this->object_read;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a collection of props in one go, collect any errors, and return the result.
|
||||
* Only sets using public methods.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param array $props Key value pairs to set. Key is the prop and should map to a setter function name.
|
||||
* @param string $context In what context to run this.
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function set_props( $props, $context = 'set' ) {
|
||||
$errors = false;
|
||||
|
||||
foreach ( $props as $prop => $value ) {
|
||||
try {
|
||||
/**
|
||||
* Checks if the prop being set is allowed, and the value is not null.
|
||||
*/
|
||||
if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
$setter = "set_$prop";
|
||||
|
||||
if ( is_callable( array( $this, $setter ) ) ) {
|
||||
$this->{$setter}( $value );
|
||||
}
|
||||
} catch ( WC_Data_Exception $e ) {
|
||||
if ( ! $errors ) {
|
||||
$errors = new WP_Error();
|
||||
}
|
||||
$errors->add( $e->getErrorCode(), $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
return $errors && count( $errors->get_error_codes() ) ? $errors : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a prop for a setter method.
|
||||
*
|
||||
* This stores changes in a special array so we can track what needs saving
|
||||
* the the DB later.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param string $prop Name of prop to set.
|
||||
* @param mixed $value Value of the prop.
|
||||
*/
|
||||
protected function set_prop( $prop, $value ) {
|
||||
if ( array_key_exists( $prop, $this->data ) ) {
|
||||
if ( true === $this->object_read ) {
|
||||
if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) {
|
||||
$this->changes[ $prop ] = $value;
|
||||
}
|
||||
} else {
|
||||
$this->data[ $prop ] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return data changes only.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @return array
|
||||
*/
|
||||
public function get_changes() {
|
||||
return $this->changes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge changes with data and clear.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function apply_changes() {
|
||||
$this->data = array_replace_recursive( $this->data, $this->changes ); // @codingStandardsIgnoreLine
|
||||
$this->changes = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefix for action and filter hooks on data.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @return string
|
||||
*/
|
||||
protected function get_hook_prefix() {
|
||||
return 'woocommerce_' . $this->object_type . '_get_';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a prop for a getter method.
|
||||
*
|
||||
* Gets the value from either current pending changes, or the data itself.
|
||||
* Context controls what happens to the value before it's returned.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param string $prop Name of prop to get.
|
||||
* @param string $context What the value is for. Valid values are view and edit.
|
||||
* @return mixed
|
||||
*/
|
||||
protected function get_prop( $prop, $context = 'view' ) {
|
||||
$value = null;
|
||||
|
||||
if ( array_key_exists( $prop, $this->data ) ) {
|
||||
$value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ];
|
||||
|
||||
if ( 'view' === $context ) {
|
||||
$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a date prop whilst handling formatting and datetime objects.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param string $prop Name of prop to set.
|
||||
* @param string|integer $value Value of the prop.
|
||||
*/
|
||||
protected function set_date_prop( $prop, $value ) {
|
||||
try {
|
||||
if ( empty( $value ) ) {
|
||||
$this->set_prop( $prop, null );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( is_a( $value, 'WC_DateTime' ) ) {
|
||||
$datetime = $value;
|
||||
} elseif ( is_numeric( $value ) ) {
|
||||
// Timestamps are handled as UTC timestamps in all cases.
|
||||
$datetime = new WC_DateTime( "@{$value}", new DateTimeZone( 'UTC' ) );
|
||||
} else {
|
||||
// Strings are defined in local WP timezone. Convert to UTC.
|
||||
if ( 1 === preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|((-|\+)\d{2}:\d{2}))$/', $value, $date_bits ) ) {
|
||||
$offset = ! empty( $date_bits[7] ) ? iso8601_timezone_to_offset( $date_bits[7] ) : wc_timezone_offset();
|
||||
$timestamp = gmmktime( $date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1] ) - $offset;
|
||||
} else {
|
||||
$timestamp = wc_string_to_timestamp( get_gmt_from_date( gmdate( 'Y-m-d H:i:s', wc_string_to_timestamp( $value ) ) ) );
|
||||
}
|
||||
$datetime = new WC_DateTime( "@{$timestamp}", new DateTimeZone( 'UTC' ) );
|
||||
}
|
||||
|
||||
// Set local timezone or offset.
|
||||
if ( get_option( 'timezone_string' ) ) {
|
||||
$datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) );
|
||||
} else {
|
||||
$datetime->set_utc_offset( wc_timezone_offset() );
|
||||
}
|
||||
|
||||
$this->set_prop( $prop, $datetime );
|
||||
} catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
|
||||
}
|
||||
|
||||
/**
|
||||
* When invalid data is found, throw an exception unless reading from the DB.
|
||||
*
|
||||
* @throws WC_Data_Exception Data Exception.
|
||||
* @since 3.0.0
|
||||
* @param string $code Error code.
|
||||
* @param string $message Error message.
|
||||
* @param int $http_status_code HTTP status code.
|
||||
* @param array $data Extra error data.
|
||||
*/
|
||||
protected function error( $code, $message, $http_status_code = 400, $data = array() ) {
|
||||
throw new WC_Data_Exception( $code, $message, $http_status_code, $data );
|
||||
}
|
||||
}
|
120
includes/abstracts/abstract-wc-deprecated-hooks.php
Normal file
120
includes/abstracts/abstract-wc-deprecated-hooks.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract deprecated hooks
|
||||
*
|
||||
* @package WooCommerce\Abstracts
|
||||
* @since 3.0.0
|
||||
* @version 3.3.0
|
||||
*/
|
||||
|
||||
use Automattic\Jetpack\Constants;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WC_Deprecated_Hooks class maps old actions and filters to new ones. This is the base class for handling those deprecated hooks.
|
||||
*
|
||||
* Based on the WCS_Hook_Deprecator class by Prospress.
|
||||
*/
|
||||
abstract class WC_Deprecated_Hooks {
|
||||
|
||||
/**
|
||||
* Array of deprecated hooks we need to handle.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $deprecated_hooks = array();
|
||||
|
||||
/**
|
||||
* Array of versions on each hook has been deprecated.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $deprecated_version = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$new_hooks = array_keys( $this->deprecated_hooks );
|
||||
array_walk( $new_hooks, array( $this, 'hook_in' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook into the new hook so we can handle deprecated hooks once fired.
|
||||
*
|
||||
* @param string $hook_name Hook name.
|
||||
*/
|
||||
abstract public function hook_in( $hook_name );
|
||||
|
||||
/**
|
||||
* Get old hooks to map to new hook.
|
||||
*
|
||||
* @param string $new_hook New hook name.
|
||||
* @return array
|
||||
*/
|
||||
public function get_old_hooks( $new_hook ) {
|
||||
$old_hooks = isset( $this->deprecated_hooks[ $new_hook ] ) ? $this->deprecated_hooks[ $new_hook ] : array();
|
||||
$old_hooks = is_array( $old_hooks ) ? $old_hooks : array( $old_hooks );
|
||||
|
||||
return $old_hooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the hook is Deprecated, call the old hooks here.
|
||||
*/
|
||||
public function maybe_handle_deprecated_hook() {
|
||||
$new_hook = current_filter();
|
||||
$old_hooks = $this->get_old_hooks( $new_hook );
|
||||
$new_callback_args = func_get_args();
|
||||
$return_value = $new_callback_args[0];
|
||||
|
||||
foreach ( $old_hooks as $old_hook ) {
|
||||
$return_value = $this->handle_deprecated_hook( $new_hook, $old_hook, $new_callback_args, $return_value );
|
||||
}
|
||||
|
||||
return $return_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the old hook is in-use, trigger it.
|
||||
*
|
||||
* @param string $new_hook New hook name.
|
||||
* @param string $old_hook Old hook name.
|
||||
* @param array $new_callback_args New callback args.
|
||||
* @param mixed $return_value Returned value.
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function handle_deprecated_hook( $new_hook, $old_hook, $new_callback_args, $return_value );
|
||||
|
||||
/**
|
||||
* Get deprecated version.
|
||||
*
|
||||
* @param string $old_hook Old hook name.
|
||||
* @return string
|
||||
*/
|
||||
protected function get_deprecated_version( $old_hook ) {
|
||||
return ! empty( $this->deprecated_version[ $old_hook ] ) ? $this->deprecated_version[ $old_hook ] : Constants::get_constant( 'WC_VERSION' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a deprecated notice for old hooks.
|
||||
*
|
||||
* @param string $old_hook Old hook.
|
||||
* @param string $new_hook New hook.
|
||||
*/
|
||||
protected function display_notice( $old_hook, $new_hook ) {
|
||||
wc_deprecated_hook( esc_html( $old_hook ), esc_html( $this->get_deprecated_version( $old_hook ) ), esc_html( $new_hook ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire off a legacy hook with it's args.
|
||||
*
|
||||
* @param string $old_hook Old hook name.
|
||||
* @param array $new_callback_args New callback args.
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function trigger_hook( $old_hook, $new_callback_args );
|
||||
}
|
85
includes/abstracts/abstract-wc-integration.php
Normal file
85
includes/abstracts/abstract-wc-integration.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract Integration class
|
||||
*
|
||||
* Extension of the Settings API which in turn gets extended
|
||||
* by individual integrations to offer additional functionality.
|
||||
*
|
||||
* @class WC_Settings_API
|
||||
* @version 2.6.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract Integration Class
|
||||
*
|
||||
* Extended by individual integrations to offer additional functionality.
|
||||
*
|
||||
* @class WC_Integration
|
||||
* @extends WC_Settings_API
|
||||
* @version 2.6.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
abstract class WC_Integration extends WC_Settings_API {
|
||||
|
||||
/**
|
||||
* Yes or no based on whether the integration is enabled.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $enabled = 'yes';
|
||||
|
||||
/**
|
||||
* Integration title.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $method_title = '';
|
||||
|
||||
/**
|
||||
* Integration description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $method_description = '';
|
||||
|
||||
/**
|
||||
* Return the title for admin screens.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_method_title() {
|
||||
return apply_filters( 'woocommerce_integration_title', $this->method_title, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the description for admin screens.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_method_description() {
|
||||
return apply_filters( 'woocommerce_integration_description', $this->method_description, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the gateway settings screen.
|
||||
*/
|
||||
public function admin_options() {
|
||||
echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>';
|
||||
echo wp_kses_post( wpautop( $this->get_method_description() ) );
|
||||
echo '<div><input type="hidden" name="section" value="' . esc_attr( $this->id ) . '" /></div>';
|
||||
parent::admin_options();
|
||||
}
|
||||
|
||||
/**
|
||||
* Init settings for gateways.
|
||||
*/
|
||||
public function init_settings() {
|
||||
parent::init_settings();
|
||||
$this->enabled = ! empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
|
||||
}
|
||||
}
|
57
includes/abstracts/abstract-wc-log-handler.php
Normal file
57
includes/abstracts/abstract-wc-log-handler.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* Log handling functionality.
|
||||
*
|
||||
* @class WC_Log_Handler
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract WC Log Handler Class
|
||||
*
|
||||
* @version 1.0.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
abstract class WC_Log_Handler implements WC_Log_Handler_Interface {
|
||||
|
||||
/**
|
||||
* Formats a timestamp for use in log messages.
|
||||
*
|
||||
* @param int $timestamp Log timestamp.
|
||||
* @return string Formatted time for use in log entry.
|
||||
*/
|
||||
protected static function format_time( $timestamp ) {
|
||||
return date( 'c', $timestamp );
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a log entry text from level, timestamp and message.
|
||||
*
|
||||
* @param int $timestamp Log timestamp.
|
||||
* @param string $level emergency|alert|critical|error|warning|notice|info|debug.
|
||||
* @param string $message Log message.
|
||||
* @param array $context Additional information for log handlers.
|
||||
*
|
||||
* @return string Formatted log entry.
|
||||
*/
|
||||
protected static function format_entry( $timestamp, $level, $message, $context ) {
|
||||
$time_string = self::format_time( $timestamp );
|
||||
$level_string = strtoupper( $level );
|
||||
$entry = "{$time_string} {$level_string} {$message}";
|
||||
|
||||
return apply_filters(
|
||||
'woocommerce_format_log_entry',
|
||||
$entry,
|
||||
array(
|
||||
'timestamp' => $timestamp,
|
||||
'level' => $level,
|
||||
'message' => $message,
|
||||
'context' => $context,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
95
includes/abstracts/abstract-wc-object-query.php
Normal file
95
includes/abstracts/abstract-wc-object-query.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* Query abstraction layer functionality.
|
||||
*
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract WC Object Query Class
|
||||
*
|
||||
* Extended by classes to provide a query abstraction layer for safe object searching.
|
||||
*
|
||||
* @version 3.1.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
abstract class WC_Object_Query {
|
||||
|
||||
/**
|
||||
* Stores query data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $query_vars = array();
|
||||
|
||||
/**
|
||||
* Create a new query.
|
||||
*
|
||||
* @param array $args Criteria to query on in a format similar to WP_Query.
|
||||
*/
|
||||
public function __construct( $args = array() ) {
|
||||
$this->query_vars = wp_parse_args( $args, $this->get_default_query_vars() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current query vars.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_query_vars() {
|
||||
return $this->query_vars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a query variable.
|
||||
*
|
||||
* @param string $query_var Query variable to get value for.
|
||||
* @param mixed $default Default value if query variable is not set.
|
||||
* @return mixed Query variable value if set, otherwise default.
|
||||
*/
|
||||
public function get( $query_var, $default = '' ) {
|
||||
if ( isset( $this->query_vars[ $query_var ] ) ) {
|
||||
return $this->query_vars[ $query_var ];
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a query variable.
|
||||
*
|
||||
* @param string $query_var Query variable to set.
|
||||
* @param mixed $value Value to set for query variable.
|
||||
*/
|
||||
public function set( $query_var, $value ) {
|
||||
$this->query_vars[ $query_var ] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default allowed query vars.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_default_query_vars() {
|
||||
|
||||
return array(
|
||||
'name' => '',
|
||||
'parent' => '',
|
||||
'parent_exclude' => '',
|
||||
'exclude' => '',
|
||||
|
||||
'limit' => get_option( 'posts_per_page' ),
|
||||
'page' => 1,
|
||||
'offset' => '',
|
||||
'paginate' => false,
|
||||
|
||||
'order' => 'DESC',
|
||||
'orderby' => 'date',
|
||||
|
||||
'return' => 'objects',
|
||||
);
|
||||
}
|
||||
}
|
2203
includes/abstracts/abstract-wc-order.php
Normal file
2203
includes/abstracts/abstract-wc-order.php
Normal file
File diff suppressed because it is too large
Load Diff
562
includes/abstracts/abstract-wc-payment-gateway.php
Normal file
562
includes/abstracts/abstract-wc-payment-gateway.php
Normal file
@ -0,0 +1,562 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract payment gateway
|
||||
*
|
||||
* Hanldes generic payment gateway functionality which is extended by idividual payment gateways.
|
||||
*
|
||||
* @class WC_Payment_Gateway
|
||||
* @version 2.1.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
use Automattic\Jetpack\Constants;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WooCommerce Payment Gateway class.
|
||||
*
|
||||
* Extended by individual payment gateways to handle payments.
|
||||
*
|
||||
* @class WC_Payment_Gateway
|
||||
* @extends WC_Settings_API
|
||||
* @version 2.1.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
abstract class WC_Payment_Gateway extends WC_Settings_API {
|
||||
|
||||
/**
|
||||
* Set if the place order button should be renamed on selection.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $order_button_text;
|
||||
|
||||
/**
|
||||
* Yes or no based on whether the method is enabled.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $enabled = 'yes';
|
||||
|
||||
/**
|
||||
* Payment method title for the frontend.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $title;
|
||||
|
||||
/**
|
||||
* Payment method description for the frontend.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
|
||||
/**
|
||||
* Chosen payment method id.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $chosen;
|
||||
|
||||
/**
|
||||
* Gateway title.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $method_title = '';
|
||||
|
||||
/**
|
||||
* Gateway description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $method_description = '';
|
||||
|
||||
/**
|
||||
* True if the gateway shows fields on the checkout.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $has_fields;
|
||||
|
||||
/**
|
||||
* Countries this gateway is allowed for.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $countries;
|
||||
|
||||
/**
|
||||
* Available for all counties or specific.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $availability;
|
||||
|
||||
/**
|
||||
* Icon for the gateway.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $icon;
|
||||
|
||||
/**
|
||||
* Supported features such as 'default_credit_card_form', 'refunds'.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $supports = array( 'products' );
|
||||
|
||||
/**
|
||||
* Maximum transaction amount, zero does not define a maximum.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $max_amount = 0;
|
||||
|
||||
/**
|
||||
* Optional URL to view a transaction.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $view_transaction_url = '';
|
||||
|
||||
/**
|
||||
* Optional label to show for "new payment method" in the payment
|
||||
* method/token selection radio selection.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $new_method_label = '';
|
||||
|
||||
/**
|
||||
* Pay button ID if supported.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $pay_button_id = '';
|
||||
|
||||
/**
|
||||
* Contains a users saved tokens for this gateway.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $tokens = array();
|
||||
|
||||
/**
|
||||
* Returns a users saved tokens for this gateway.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return array
|
||||
*/
|
||||
public function get_tokens() {
|
||||
if ( count( $this->tokens ) > 0 ) {
|
||||
return $this->tokens;
|
||||
}
|
||||
|
||||
if ( is_user_logged_in() && $this->supports( 'tokenization' ) ) {
|
||||
$this->tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id(), $this->id );
|
||||
}
|
||||
|
||||
return $this->tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the title for admin screens.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_method_title() {
|
||||
return apply_filters( 'woocommerce_gateway_method_title', $this->method_title, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the description for admin screens.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_method_description() {
|
||||
return apply_filters( 'woocommerce_gateway_method_description', $this->method_description, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the gateway settings screen.
|
||||
*/
|
||||
public function admin_options() {
|
||||
echo '<h2>' . esc_html( $this->get_method_title() );
|
||||
wc_back_link( __( 'Return to payments', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) );
|
||||
echo '</h2>';
|
||||
echo wp_kses_post( wpautop( $this->get_method_description() ) );
|
||||
parent::admin_options();
|
||||
}
|
||||
|
||||
/**
|
||||
* Init settings for gateways.
|
||||
*/
|
||||
public function init_settings() {
|
||||
parent::init_settings();
|
||||
$this->enabled = ! empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether or not this gateway still requires setup to function.
|
||||
*
|
||||
* When this gateway is toggled on via AJAX, if this returns true a
|
||||
* redirect will occur to the settings page instead.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @return bool
|
||||
*/
|
||||
public function needs_setup() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the return url (thank you page).
|
||||
*
|
||||
* @param WC_Order|null $order Order object.
|
||||
* @return string
|
||||
*/
|
||||
public function get_return_url( $order = null ) {
|
||||
if ( $order ) {
|
||||
$return_url = $order->get_checkout_order_received_url();
|
||||
} else {
|
||||
$return_url = wc_get_endpoint_url( 'order-received', '', wc_get_checkout_url() );
|
||||
}
|
||||
|
||||
return apply_filters( 'woocommerce_get_return_url', $return_url, $order );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a link to the transaction on the 3rd party gateway site (if applicable).
|
||||
*
|
||||
* @param WC_Order $order the order object.
|
||||
* @return string transaction URL, or empty string.
|
||||
*/
|
||||
public function get_transaction_url( $order ) {
|
||||
|
||||
$return_url = '';
|
||||
$transaction_id = $order->get_transaction_id();
|
||||
|
||||
if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) {
|
||||
$return_url = sprintf( $this->view_transaction_url, $transaction_id );
|
||||
}
|
||||
|
||||
return apply_filters( 'woocommerce_get_transaction_url', $return_url, $order, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the order total in checkout and pay_for_order.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
protected function get_order_total() {
|
||||
|
||||
$total = 0;
|
||||
$order_id = absint( get_query_var( 'order-pay' ) );
|
||||
|
||||
// Gets order total from "pay for order" page.
|
||||
if ( 0 < $order_id ) {
|
||||
$order = wc_get_order( $order_id );
|
||||
if ( $order ) {
|
||||
$total = (float) $order->get_total();
|
||||
}
|
||||
|
||||
// Gets order total from cart/checkout.
|
||||
} elseif ( 0 < WC()->cart->total ) {
|
||||
$total = (float) WC()->cart->total;
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the gateway is available for use.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_available() {
|
||||
$is_available = ( 'yes' === $this->enabled );
|
||||
|
||||
if ( WC()->cart && 0 < $this->get_order_total() && 0 < $this->max_amount && $this->max_amount < $this->get_order_total() ) {
|
||||
$is_available = false;
|
||||
}
|
||||
|
||||
return $is_available;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the gateway has fields on the checkout.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has_fields() {
|
||||
return (bool) $this->has_fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the gateway's title.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_title() {
|
||||
return apply_filters( 'woocommerce_gateway_title', $this->title, $this->id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the gateway's description.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return apply_filters( 'woocommerce_gateway_description', $this->description, $this->id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the gateway's icon.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_icon() {
|
||||
|
||||
$icon = $this->icon ? '<img src="' . WC_HTTPS::force_https_url( $this->icon ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : '';
|
||||
|
||||
return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the gateway's pay button ID.
|
||||
*
|
||||
* @since 3.9.0
|
||||
* @return string
|
||||
*/
|
||||
public function get_pay_button_id() {
|
||||
return sanitize_html_class( $this->pay_button_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set as current gateway.
|
||||
*
|
||||
* Set this as the current gateway.
|
||||
*/
|
||||
public function set_current() {
|
||||
$this->chosen = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process Payment.
|
||||
*
|
||||
* Process the payment. Override this in your gateway. When implemented, this should.
|
||||
* return the success and redirect in an array. e.g:
|
||||
*
|
||||
* return array(
|
||||
* 'result' => 'success',
|
||||
* 'redirect' => $this->get_return_url( $order )
|
||||
* );
|
||||
*
|
||||
* @param int $order_id Order ID.
|
||||
* @return array
|
||||
*/
|
||||
public function process_payment( $order_id ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process refund.
|
||||
*
|
||||
* If the gateway declares 'refunds' support, this will allow it to refund.
|
||||
* a passed in amount.
|
||||
*
|
||||
* @param int $order_id Order ID.
|
||||
* @param float|null $amount Refund amount.
|
||||
* @param string $reason Refund reason.
|
||||
* @return boolean True or false based on success, or a WP_Error object.
|
||||
*/
|
||||
public function process_refund( $order_id, $amount = null, $reason = '' ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate frontend fields.
|
||||
*
|
||||
* Validate payment fields on the frontend.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validate_fields() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* If There are no payment fields show the description if set.
|
||||
* Override this in your gateway if you have some.
|
||||
*/
|
||||
public function payment_fields() {
|
||||
$description = $this->get_description();
|
||||
if ( $description ) {
|
||||
echo wpautop( wptexturize( $description ) ); // @codingStandardsIgnoreLine.
|
||||
}
|
||||
|
||||
if ( $this->supports( 'default_credit_card_form' ) ) {
|
||||
$this->credit_card_form(); // Deprecated, will be removed in a future version.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a gateway supports a given feature.
|
||||
*
|
||||
* Gateways should override this to declare support (or lack of support) for a feature.
|
||||
* For backward compatibility, gateways support 'products' by default, but nothing else.
|
||||
*
|
||||
* @param string $feature string The name of a feature to test support for.
|
||||
* @return bool True if the gateway supports the feature, false otherwise.
|
||||
* @since 1.5.7
|
||||
*/
|
||||
public function supports( $feature ) {
|
||||
return apply_filters( 'woocommerce_payment_gateway_supports', in_array( $feature, $this->supports ), $feature, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the order be refunded via this gateway?
|
||||
*
|
||||
* Should be extended by gateways to do their own checks.
|
||||
*
|
||||
* @param WC_Order $order Order object.
|
||||
* @return bool If false, the automatic refund button is hidden in the UI.
|
||||
*/
|
||||
public function can_refund_order( $order ) {
|
||||
return $order && $this->supports( 'refunds' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Core credit card form which gateways can use if needed. Deprecated - inherit WC_Payment_Gateway_CC instead.
|
||||
*
|
||||
* @param array $args Arguments.
|
||||
* @param array $fields Fields.
|
||||
*/
|
||||
public function credit_card_form( $args = array(), $fields = array() ) {
|
||||
wc_deprecated_function( 'credit_card_form', '2.6', 'WC_Payment_Gateway_CC->form' );
|
||||
$cc_form = new WC_Payment_Gateway_CC();
|
||||
$cc_form->id = $this->id;
|
||||
$cc_form->supports = $this->supports;
|
||||
$cc_form->form();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues our tokenization script to handle some of the new form options.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function tokenization_script() {
|
||||
wp_enqueue_script(
|
||||
'woocommerce-tokenization-form',
|
||||
plugins_url( '/assets/js/frontend/tokenization-form' . ( Constants::is_true( 'SCRIPT_DEBUG' ) ? '' : '.min' ) . '.js', WC_PLUGIN_FILE ),
|
||||
array( 'jquery' ),
|
||||
WC()->version
|
||||
);
|
||||
|
||||
wp_localize_script(
|
||||
'woocommerce-tokenization-form',
|
||||
'wc_tokenization_form_params',
|
||||
array(
|
||||
'is_registration_required' => WC()->checkout()->is_registration_required(),
|
||||
'is_logged_in' => is_user_logged_in(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab and display our saved payment methods.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function saved_payment_methods() {
|
||||
$html = '<ul class="woocommerce-SavedPaymentMethods wc-saved-payment-methods" data-count="' . esc_attr( count( $this->get_tokens() ) ) . '">';
|
||||
|
||||
foreach ( $this->get_tokens() as $token ) {
|
||||
$html .= $this->get_saved_payment_method_option_html( $token );
|
||||
}
|
||||
|
||||
$html .= $this->get_new_payment_method_option_html();
|
||||
$html .= '</ul>';
|
||||
|
||||
echo apply_filters( 'wc_payment_gateway_form_saved_payment_methods_html', $html, $this ); // @codingStandardsIgnoreLine
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets saved payment method HTML from a token.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param WC_Payment_Token $token Payment Token.
|
||||
* @return string Generated payment method HTML
|
||||
*/
|
||||
public function get_saved_payment_method_option_html( $token ) {
|
||||
$html = sprintf(
|
||||
'<li class="woocommerce-SavedPaymentMethods-token">
|
||||
<input id="wc-%1$s-payment-token-%2$s" type="radio" name="wc-%1$s-payment-token" value="%2$s" style="width:auto;" class="woocommerce-SavedPaymentMethods-tokenInput" %4$s />
|
||||
<label for="wc-%1$s-payment-token-%2$s">%3$s</label>
|
||||
</li>',
|
||||
esc_attr( $this->id ),
|
||||
esc_attr( $token->get_id() ),
|
||||
esc_html( $token->get_display_name() ),
|
||||
checked( $token->is_default(), true, false )
|
||||
);
|
||||
|
||||
return apply_filters( 'woocommerce_payment_gateway_get_saved_payment_method_option_html', $html, $token, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a radio button for entering a new payment method (new CC details) instead of using a saved method.
|
||||
* Only displayed when a gateway supports tokenization.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function get_new_payment_method_option_html() {
|
||||
$label = apply_filters( 'woocommerce_payment_gateway_get_new_payment_method_option_html_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'woocommerce' ), $this );
|
||||
$html = sprintf(
|
||||
'<li class="woocommerce-SavedPaymentMethods-new">
|
||||
<input id="wc-%1$s-payment-token-new" type="radio" name="wc-%1$s-payment-token" value="new" style="width:auto;" class="woocommerce-SavedPaymentMethods-tokenInput" />
|
||||
<label for="wc-%1$s-payment-token-new">%2$s</label>
|
||||
</li>',
|
||||
esc_attr( $this->id ),
|
||||
esc_html( $label )
|
||||
);
|
||||
|
||||
return apply_filters( 'woocommerce_payment_gateway_get_new_payment_method_option_html', $html, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a checkbox for saving a new payment method to the database.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function save_payment_method_checkbox() {
|
||||
$html = sprintf(
|
||||
'<p class="form-row woocommerce-SavedPaymentMethods-saveNew">
|
||||
<input id="wc-%1$s-new-payment-method" name="wc-%1$s-new-payment-method" type="checkbox" value="true" style="width:auto;" />
|
||||
<label for="wc-%1$s-new-payment-method" style="display:inline;">%2$s</label>
|
||||
</p>',
|
||||
esc_attr( $this->id ),
|
||||
esc_html__( 'Save to account', 'woocommerce' )
|
||||
);
|
||||
|
||||
echo apply_filters( 'woocommerce_payment_gateway_save_new_payment_method_option_html', $html, $this ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
* Add payment method via account screen. This should be extended by gateway plugins.
|
||||
*
|
||||
* @since 3.2.0 Included here from 3.2.0, but supported from 3.0.0.
|
||||
* @return array
|
||||
*/
|
||||
public function add_payment_method() {
|
||||
return array(
|
||||
'result' => 'failure',
|
||||
'redirect' => wc_get_endpoint_url( 'payment-methods' ),
|
||||
);
|
||||
}
|
||||
}
|
233
includes/abstracts/abstract-wc-payment-token.php
Normal file
233
includes/abstracts/abstract-wc-payment-token.php
Normal file
@ -0,0 +1,233 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract payment tokens
|
||||
*
|
||||
* Generic payment tokens functionality which can be extended by individual types of payment tokens.
|
||||
*
|
||||
* @class WC_Payment_Token
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once WC_ABSPATH . 'includes/legacy/abstract-wc-legacy-payment-token.php';
|
||||
|
||||
/**
|
||||
* WooCommerce Payment Token.
|
||||
*
|
||||
* Representation of a general payment token to be extended by individuals types of tokens
|
||||
* examples: Credit Card, eCheck.
|
||||
*
|
||||
* @class WC_Payment_Token
|
||||
* @version 3.0.0
|
||||
* @since 2.6.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
abstract class WC_Payment_Token extends WC_Legacy_Payment_Token {
|
||||
|
||||
/**
|
||||
* Token Data (stored in the payment_tokens table).
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data = array(
|
||||
'gateway_id' => '',
|
||||
'token' => '',
|
||||
'is_default' => false,
|
||||
'user_id' => 0,
|
||||
'type' => '',
|
||||
);
|
||||
|
||||
/**
|
||||
* Token Type (CC, eCheck, or a custom type added by an extension).
|
||||
* Set by child classes.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = '';
|
||||
|
||||
/**
|
||||
* Initialize a payment token.
|
||||
*
|
||||
* These fields are accepted by all payment tokens:
|
||||
* is_default - boolean Optional - Indicates this is the default payment token for a user
|
||||
* token - string Required - The actual token to store
|
||||
* gateway_id - string Required - Identifier for the gateway this token is associated with
|
||||
* user_id - int Optional - ID for the user this token is associated with. 0 if this token is not associated with a user
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param mixed $token Token.
|
||||
*/
|
||||
public function __construct( $token = '' ) {
|
||||
parent::__construct( $token );
|
||||
|
||||
if ( is_numeric( $token ) ) {
|
||||
$this->set_id( $token );
|
||||
} elseif ( is_object( $token ) ) {
|
||||
$token_id = $token->get_id();
|
||||
if ( ! empty( $token_id ) ) {
|
||||
$this->set_id( $token->get_id() );
|
||||
}
|
||||
} else {
|
||||
$this->set_object_read( true );
|
||||
}
|
||||
|
||||
$this->data_store = WC_Data_Store::load( 'payment-token' );
|
||||
if ( $this->get_id() > 0 ) {
|
||||
$this->data_store->read( $this );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*--------------------------------------------------------------------------
|
||||
* Getters
|
||||
*--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns the raw payment token.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $context Context in which to call this.
|
||||
* @return string Raw token
|
||||
*/
|
||||
public function get_token( $context = 'view' ) {
|
||||
return $this->get_prop( 'token', $context );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of this payment token (CC, eCheck, or something else).
|
||||
* Overwritten by child classes.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $deprecated Deprecated since WooCommerce 3.0.
|
||||
* @return string Payment Token Type (CC, eCheck)
|
||||
*/
|
||||
public function get_type( $deprecated = '' ) {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get type to display to user.
|
||||
* Get's overwritten by child classes.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $deprecated Deprecated since WooCommerce 3.0.
|
||||
* @return string
|
||||
*/
|
||||
public function get_display_name( $deprecated = '' ) {
|
||||
return $this->get_type();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user ID associated with the token or false if this token is not associated.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $context In what context to execute this.
|
||||
* @return int User ID if this token is associated with a user or 0 if no user is associated
|
||||
*/
|
||||
public function get_user_id( $context = 'view' ) {
|
||||
return $this->get_prop( 'user_id', $context );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of the gateway associated with this payment token.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $context In what context to execute this.
|
||||
* @return string Gateway ID
|
||||
*/
|
||||
public function get_gateway_id( $context = 'view' ) {
|
||||
return $this->get_prop( 'gateway_id', $context );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of the gateway associated with this payment token.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $context In what context to execute this.
|
||||
* @return string Gateway ID
|
||||
*/
|
||||
public function get_is_default( $context = 'view' ) {
|
||||
return $this->get_prop( 'is_default', $context );
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Setters
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Set the raw payment token.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $token Payment token.
|
||||
*/
|
||||
public function set_token( $token ) {
|
||||
$this->set_prop( 'token', $token );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user ID for the user associated with this order.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param int $user_id User ID.
|
||||
*/
|
||||
public function set_user_id( $user_id ) {
|
||||
$this->set_prop( 'user_id', absint( $user_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the gateway ID.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $gateway_id Gateway ID.
|
||||
*/
|
||||
public function set_gateway_id( $gateway_id ) {
|
||||
$this->set_prop( 'gateway_id', $gateway_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the payment as default or non-default.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param boolean $is_default True or false.
|
||||
*/
|
||||
public function set_default( $is_default ) {
|
||||
$this->set_prop( 'is_default', (bool) $is_default );
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Other Methods
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns if the token is marked as default.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return boolean True if the token is default
|
||||
*/
|
||||
public function is_default() {
|
||||
return (bool) $this->get_prop( 'is_default', 'view' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate basic token info (token and type are required).
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return boolean True if the passed data is valid
|
||||
*/
|
||||
public function validate() {
|
||||
$token = $this->get_prop( 'token', 'edit' );
|
||||
if ( empty( $token ) ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
162
includes/abstracts/abstract-wc-privacy.php
Normal file
162
includes/abstracts/abstract-wc-privacy.php
Normal file
@ -0,0 +1,162 @@
|
||||
<?php
|
||||
/**
|
||||
* WooCommerce abstract privacy class.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Abstract class that is intended to be extended by
|
||||
* specific privacy class. It handles the display
|
||||
* of the privacy message of the privacy id to the admin,
|
||||
* privacy data to be exported and privacy data to be deleted.
|
||||
*
|
||||
* @version 3.4.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
abstract class WC_Abstract_Privacy {
|
||||
/**
|
||||
* This is the name of this object type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* This is a list of exporters.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $exporters = array();
|
||||
|
||||
/**
|
||||
* This is a list of erasers.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $erasers = array();
|
||||
|
||||
/**
|
||||
* This is a priority for the wp_privacy_personal_data_exporters filter
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $export_priority;
|
||||
|
||||
/**
|
||||
* This is a priority for the wp_privacy_personal_data_erasers filter
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $erase_priority;
|
||||
|
||||
/**
|
||||
* WC_Abstract_Privacy Constructor.
|
||||
*
|
||||
* @param string $name Plugin identifier.
|
||||
* @param int $export_priority Export priority.
|
||||
* @param int $erase_priority Erase priority.
|
||||
*/
|
||||
public function __construct( $name = '', $export_priority = 5, $erase_priority = 10 ) {
|
||||
$this->name = $name;
|
||||
$this->export_priority = $export_priority;
|
||||
$this->erase_priority = $erase_priority;
|
||||
$this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook in events.
|
||||
*/
|
||||
protected function init() {
|
||||
add_action( 'admin_init', array( $this, 'add_privacy_message' ) );
|
||||
// We set priority to 5 to help WooCommerce's findings appear before those from extensions in exported items.
|
||||
add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_exporters' ), $this->export_priority );
|
||||
add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_erasers' ), $this->erase_priority );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the privacy message on WC privacy page.
|
||||
*/
|
||||
public function add_privacy_message() {
|
||||
if ( function_exists( 'wp_add_privacy_policy_content' ) ) {
|
||||
$content = $this->get_privacy_message();
|
||||
|
||||
if ( $content ) {
|
||||
wp_add_privacy_policy_content( $this->name, $this->get_privacy_message() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the message of the privacy to display.
|
||||
* To be overloaded by the implementor.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_privacy_message() {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrate this exporter implementation within the WordPress core exporters.
|
||||
*
|
||||
* @param array $exporters List of exporter callbacks.
|
||||
* @return array
|
||||
*/
|
||||
public function register_exporters( $exporters = array() ) {
|
||||
foreach ( $this->exporters as $id => $exporter ) {
|
||||
$exporters[ $id ] = $exporter;
|
||||
}
|
||||
return $exporters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrate this eraser implementation within the WordPress core erasers.
|
||||
*
|
||||
* @param array $erasers List of eraser callbacks.
|
||||
* @return array
|
||||
*/
|
||||
public function register_erasers( $erasers = array() ) {
|
||||
foreach ( $this->erasers as $id => $eraser ) {
|
||||
$erasers[ $id ] = $eraser;
|
||||
}
|
||||
return $erasers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add exporter to list of exporters.
|
||||
*
|
||||
* @param string $id ID of the Exporter.
|
||||
* @param string $name Exporter name.
|
||||
* @param string|array $callback Exporter callback.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_exporter( $id, $name, $callback ) {
|
||||
$this->exporters[ $id ] = array(
|
||||
'exporter_friendly_name' => $name,
|
||||
'callback' => $callback,
|
||||
);
|
||||
return $this->exporters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add eraser to list of erasers.
|
||||
*
|
||||
* @param string $id ID of the Eraser.
|
||||
* @param string $name Exporter name.
|
||||
* @param string|array $callback Exporter callback.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_eraser( $id, $name, $callback ) {
|
||||
$this->erasers[ $id ] = array(
|
||||
'eraser_friendly_name' => $name,
|
||||
'callback' => $callback,
|
||||
);
|
||||
return $this->erasers;
|
||||
}
|
||||
}
|
2100
includes/abstracts/abstract-wc-product.php
Normal file
2100
includes/abstracts/abstract-wc-product.php
Normal file
File diff suppressed because it is too large
Load Diff
127
includes/abstracts/abstract-wc-session.php
Normal file
127
includes/abstracts/abstract-wc-session.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* Handle data for the current customers session
|
||||
*
|
||||
* @class WC_Session
|
||||
* @version 2.0.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WC_Session
|
||||
*/
|
||||
abstract class WC_Session {
|
||||
|
||||
/**
|
||||
* Customer ID.
|
||||
*
|
||||
* @var int $_customer_id Customer ID.
|
||||
*/
|
||||
protected $_customer_id;
|
||||
|
||||
/**
|
||||
* Session Data.
|
||||
*
|
||||
* @var array $_data Data array.
|
||||
*/
|
||||
protected $_data = array();
|
||||
|
||||
/**
|
||||
* Dirty when the session needs saving.
|
||||
*
|
||||
* @var bool $_dirty When something changes
|
||||
*/
|
||||
protected $_dirty = false;
|
||||
|
||||
/**
|
||||
* Init hooks and session data. Extended by child classes.
|
||||
*
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public function init() {}
|
||||
|
||||
/**
|
||||
* Cleanup session data. Extended by child classes.
|
||||
*/
|
||||
public function cleanup_sessions() {}
|
||||
|
||||
/**
|
||||
* Magic get method.
|
||||
*
|
||||
* @param mixed $key Key to get.
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get( $key ) {
|
||||
return $this->get( $key );
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic set method.
|
||||
*
|
||||
* @param mixed $key Key to set.
|
||||
* @param mixed $value Value to set.
|
||||
*/
|
||||
public function __set( $key, $value ) {
|
||||
$this->set( $key, $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic isset method.
|
||||
*
|
||||
* @param mixed $key Key to check.
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset( $key ) {
|
||||
return isset( $this->_data[ sanitize_title( $key ) ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic unset method.
|
||||
*
|
||||
* @param mixed $key Key to unset.
|
||||
*/
|
||||
public function __unset( $key ) {
|
||||
if ( isset( $this->_data[ $key ] ) ) {
|
||||
unset( $this->_data[ $key ] );
|
||||
$this->_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a session variable.
|
||||
*
|
||||
* @param string $key Key to get.
|
||||
* @param mixed $default used if the session variable isn't set.
|
||||
* @return array|string value of session variable
|
||||
*/
|
||||
public function get( $key, $default = null ) {
|
||||
$key = sanitize_key( $key );
|
||||
return isset( $this->_data[ $key ] ) ? maybe_unserialize( $this->_data[ $key ] ) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a session variable.
|
||||
*
|
||||
* @param string $key Key to set.
|
||||
* @param mixed $value Value to set.
|
||||
*/
|
||||
public function set( $key, $value ) {
|
||||
if ( $value !== $this->get( $key ) ) {
|
||||
$this->_data[ sanitize_key( $key ) ] = maybe_serialize( $value );
|
||||
$this->_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer ID.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_customer_id() {
|
||||
return $this->_customer_id;
|
||||
}
|
||||
}
|
959
includes/abstracts/abstract-wc-settings-api.php
Normal file
959
includes/abstracts/abstract-wc-settings-api.php
Normal file
@ -0,0 +1,959 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract Settings API Class
|
||||
*
|
||||
* Admin Settings API used by Integrations, Shipping Methods, and Payment Gateways.
|
||||
*
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WC_Settings_API class.
|
||||
*/
|
||||
abstract class WC_Settings_API {
|
||||
|
||||
/**
|
||||
* The plugin ID. Used for option names.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $plugin_id = 'woocommerce_';
|
||||
|
||||
/**
|
||||
* ID of the class extending the settings API. Used in option names.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $id = '';
|
||||
|
||||
/**
|
||||
* Validation errors.
|
||||
*
|
||||
* @var array of strings
|
||||
*/
|
||||
public $errors = array();
|
||||
|
||||
/**
|
||||
* Setting values.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array();
|
||||
|
||||
/**
|
||||
* Form option fields.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $form_fields = array();
|
||||
|
||||
/**
|
||||
* The posted settings data. When empty, $_POST data will be used.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data = array();
|
||||
|
||||
/**
|
||||
* Get the form fields after they are initialized.
|
||||
*
|
||||
* @return array of options
|
||||
*/
|
||||
public function get_form_fields() {
|
||||
return apply_filters( 'woocommerce_settings_api_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->form_fields ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default required properties for each field.
|
||||
*
|
||||
* @param array $field Setting field array.
|
||||
* @return array
|
||||
*/
|
||||
protected function set_defaults( $field ) {
|
||||
if ( ! isset( $field['default'] ) ) {
|
||||
$field['default'] = '';
|
||||
}
|
||||
return $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the admin options table.
|
||||
*/
|
||||
public function admin_options() {
|
||||
echo '<table class="form-table">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>'; // WPCS: XSS ok.
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise settings form fields.
|
||||
*
|
||||
* Add an array of fields to be displayed on the gateway's settings screen.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function init_form_fields() {}
|
||||
|
||||
/**
|
||||
* Return the name of the option in the WP DB.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return string
|
||||
*/
|
||||
public function get_option_key() {
|
||||
return $this->plugin_id . $this->id . '_settings';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a fields type. Defaults to "text" if not set.
|
||||
*
|
||||
* @param array $field Field key.
|
||||
* @return string
|
||||
*/
|
||||
public function get_field_type( $field ) {
|
||||
return empty( $field['type'] ) ? 'text' : $field['type'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a fields default value. Defaults to "" if not set.
|
||||
*
|
||||
* @param array $field Field key.
|
||||
* @return string
|
||||
*/
|
||||
public function get_field_default( $field ) {
|
||||
return empty( $field['default'] ) ? '' : $field['default'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a field's posted and validated value.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $field Field array.
|
||||
* @param array $post_data Posted data.
|
||||
* @return string
|
||||
*/
|
||||
public function get_field_value( $key, $field, $post_data = array() ) {
|
||||
$type = $this->get_field_type( $field );
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$post_data = empty( $post_data ) ? $_POST : $post_data; // WPCS: CSRF ok, input var ok.
|
||||
$value = isset( $post_data[ $field_key ] ) ? $post_data[ $field_key ] : null;
|
||||
|
||||
if ( isset( $field['sanitize_callback'] ) && is_callable( $field['sanitize_callback'] ) ) {
|
||||
return call_user_func( $field['sanitize_callback'], $value );
|
||||
}
|
||||
|
||||
// Look for a validate_FIELDID_field method for special handling.
|
||||
if ( is_callable( array( $this, 'validate_' . $key . '_field' ) ) ) {
|
||||
return $this->{'validate_' . $key . '_field'}( $key, $value );
|
||||
}
|
||||
|
||||
// Look for a validate_FIELDTYPE_field method.
|
||||
if ( is_callable( array( $this, 'validate_' . $type . '_field' ) ) ) {
|
||||
return $this->{'validate_' . $type . '_field'}( $key, $value );
|
||||
}
|
||||
|
||||
// Fallback to text.
|
||||
return $this->validate_text_field( $key, $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the POSTed data. This method can be used to set specific data, instead of taking it from the $_POST array.
|
||||
*
|
||||
* @param array $data Posted data.
|
||||
*/
|
||||
public function set_post_data( $data = array() ) {
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the POSTed data, to be used to save the settings.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_post_data() {
|
||||
if ( ! empty( $this->data ) && is_array( $this->data ) ) {
|
||||
return $this->data;
|
||||
}
|
||||
return $_POST; // WPCS: CSRF ok, input var ok.
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single option.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @param string $key Option key.
|
||||
* @param mixed $value Value to set.
|
||||
* @return bool was anything saved?
|
||||
*/
|
||||
public function update_option( $key, $value = '' ) {
|
||||
if ( empty( $this->settings ) ) {
|
||||
$this->init_settings();
|
||||
}
|
||||
|
||||
$this->settings[ $key ] = $value;
|
||||
|
||||
return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ), 'yes' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes and saves options.
|
||||
* If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
|
||||
*
|
||||
* @return bool was anything saved?
|
||||
*/
|
||||
public function process_admin_options() {
|
||||
$this->init_settings();
|
||||
|
||||
$post_data = $this->get_post_data();
|
||||
|
||||
foreach ( $this->get_form_fields() as $key => $field ) {
|
||||
if ( 'title' !== $this->get_field_type( $field ) ) {
|
||||
try {
|
||||
$this->settings[ $key ] = $this->get_field_value( $key, $field, $post_data );
|
||||
} catch ( Exception $e ) {
|
||||
$this->add_error( $e->getMessage() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ), 'yes' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an error message for display in admin on save.
|
||||
*
|
||||
* @param string $error Error message.
|
||||
*/
|
||||
public function add_error( $error ) {
|
||||
$this->errors[] = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get admin error messages.
|
||||
*/
|
||||
public function get_errors() {
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display admin error messages.
|
||||
*/
|
||||
public function display_errors() {
|
||||
if ( $this->get_errors() ) {
|
||||
echo '<div id="woocommerce_errors" class="error notice is-dismissible">';
|
||||
foreach ( $this->get_errors() as $error ) {
|
||||
echo '<p>' . wp_kses_post( $error ) . '</p>';
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise Settings.
|
||||
*
|
||||
* Store all settings in a single database entry
|
||||
* and make sure the $settings array is either the default
|
||||
* or the settings stored in the database.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @uses get_option(), add_option()
|
||||
*/
|
||||
public function init_settings() {
|
||||
$this->settings = get_option( $this->get_option_key(), null );
|
||||
|
||||
// If there are no settings defined, use defaults.
|
||||
if ( ! is_array( $this->settings ) ) {
|
||||
$form_fields = $this->get_form_fields();
|
||||
$this->settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get option from DB.
|
||||
*
|
||||
* Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
|
||||
*
|
||||
* @param string $key Option key.
|
||||
* @param mixed $empty_value Value when empty.
|
||||
* @return string The value specified for the option or a default value for the option.
|
||||
*/
|
||||
public function get_option( $key, $empty_value = null ) {
|
||||
if ( empty( $this->settings ) ) {
|
||||
$this->init_settings();
|
||||
}
|
||||
|
||||
// Get option default if unset.
|
||||
if ( ! isset( $this->settings[ $key ] ) ) {
|
||||
$form_fields = $this->get_form_fields();
|
||||
$this->settings[ $key ] = isset( $form_fields[ $key ] ) ? $this->get_field_default( $form_fields[ $key ] ) : '';
|
||||
}
|
||||
|
||||
if ( ! is_null( $empty_value ) && '' === $this->settings[ $key ] ) {
|
||||
$this->settings[ $key ] = $empty_value;
|
||||
}
|
||||
|
||||
return $this->settings[ $key ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefix key for settings.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @return string
|
||||
*/
|
||||
public function get_field_key( $key ) {
|
||||
return $this->plugin_id . $this->id . '_' . $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Settings HTML.
|
||||
*
|
||||
* Generate the HTML for the fields on the "settings" screen.
|
||||
*
|
||||
* @param array $form_fields (default: array()) Array of form fields.
|
||||
* @param bool $echo Echo or return.
|
||||
* @return string the html for the settings
|
||||
* @since 1.0.0
|
||||
* @uses method_exists()
|
||||
*/
|
||||
public function generate_settings_html( $form_fields = array(), $echo = true ) {
|
||||
if ( empty( $form_fields ) ) {
|
||||
$form_fields = $this->get_form_fields();
|
||||
}
|
||||
|
||||
$html = '';
|
||||
foreach ( $form_fields as $k => $v ) {
|
||||
$type = $this->get_field_type( $v );
|
||||
|
||||
if ( method_exists( $this, 'generate_' . $type . '_html' ) ) {
|
||||
$html .= $this->{'generate_' . $type . '_html'}( $k, $v );
|
||||
} else {
|
||||
$html .= $this->generate_text_html( $k, $v );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $echo ) {
|
||||
echo $html; // WPCS: XSS ok.
|
||||
} else {
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get HTML for tooltips.
|
||||
*
|
||||
* @param array $data Data for the tooltip.
|
||||
* @return string
|
||||
*/
|
||||
public function get_tooltip_html( $data ) {
|
||||
if ( true === $data['desc_tip'] ) {
|
||||
$tip = $data['description'];
|
||||
} elseif ( ! empty( $data['desc_tip'] ) ) {
|
||||
$tip = $data['desc_tip'];
|
||||
} else {
|
||||
$tip = '';
|
||||
}
|
||||
|
||||
return $tip ? wc_help_tip( $tip, true ) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get HTML for descriptions.
|
||||
*
|
||||
* @param array $data Data for the description.
|
||||
* @return string
|
||||
*/
|
||||
public function get_description_html( $data ) {
|
||||
if ( true === $data['desc_tip'] ) {
|
||||
$description = '';
|
||||
} elseif ( ! empty( $data['desc_tip'] ) ) {
|
||||
$description = $data['description'];
|
||||
} elseif ( ! empty( $data['description'] ) ) {
|
||||
$description = $data['description'];
|
||||
} else {
|
||||
$description = '';
|
||||
}
|
||||
|
||||
return $description ? '<p class="description">' . wp_kses_post( $description ) . '</p>' . "\n" : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom attributes.
|
||||
*
|
||||
* @param array $data Field data.
|
||||
* @return string
|
||||
*/
|
||||
public function get_custom_attribute_html( $data ) {
|
||||
$custom_attributes = array();
|
||||
|
||||
if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) ) {
|
||||
foreach ( $data['custom_attributes'] as $attribute => $attribute_value ) {
|
||||
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
|
||||
}
|
||||
}
|
||||
|
||||
return implode( ' ', $custom_attributes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Text Input HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_text_html( $key, $data ) {
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$defaults = array(
|
||||
'title' => '',
|
||||
'disabled' => false,
|
||||
'class' => '',
|
||||
'css' => '',
|
||||
'placeholder' => '',
|
||||
'type' => 'text',
|
||||
'desc_tip' => false,
|
||||
'description' => '',
|
||||
'custom_attributes' => array(),
|
||||
);
|
||||
|
||||
$data = wp_parse_args( $data, $defaults );
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<tr valign="top">
|
||||
<th scope="row" class="titledesc">
|
||||
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
|
||||
</th>
|
||||
<td class="forminp">
|
||||
<fieldset>
|
||||
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
|
||||
<input class="input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> />
|
||||
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
|
||||
</fieldset>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Price Input HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_price_html( $key, $data ) {
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$defaults = array(
|
||||
'title' => '',
|
||||
'disabled' => false,
|
||||
'class' => '',
|
||||
'css' => '',
|
||||
'placeholder' => '',
|
||||
'type' => 'text',
|
||||
'desc_tip' => false,
|
||||
'description' => '',
|
||||
'custom_attributes' => array(),
|
||||
);
|
||||
|
||||
$data = wp_parse_args( $data, $defaults );
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<tr valign="top">
|
||||
<th scope="row" class="titledesc">
|
||||
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
|
||||
</th>
|
||||
<td class="forminp">
|
||||
<fieldset>
|
||||
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
|
||||
<input class="wc_input_price input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_price( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> />
|
||||
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
|
||||
</fieldset>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Decimal Input HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_decimal_html( $key, $data ) {
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$defaults = array(
|
||||
'title' => '',
|
||||
'disabled' => false,
|
||||
'class' => '',
|
||||
'css' => '',
|
||||
'placeholder' => '',
|
||||
'type' => 'text',
|
||||
'desc_tip' => false,
|
||||
'description' => '',
|
||||
'custom_attributes' => array(),
|
||||
);
|
||||
|
||||
$data = wp_parse_args( $data, $defaults );
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<tr valign="top">
|
||||
<th scope="row" class="titledesc">
|
||||
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
|
||||
</th>
|
||||
<td class="forminp">
|
||||
<fieldset>
|
||||
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
|
||||
<input class="wc_input_decimal input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_decimal( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> />
|
||||
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
|
||||
</fieldset>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Password Input HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_password_html( $key, $data ) {
|
||||
$data['type'] = 'password';
|
||||
return $this->generate_text_html( $key, $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Color Picker Input HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_color_html( $key, $data ) {
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$defaults = array(
|
||||
'title' => '',
|
||||
'disabled' => false,
|
||||
'class' => '',
|
||||
'css' => '',
|
||||
'placeholder' => '',
|
||||
'desc_tip' => false,
|
||||
'description' => '',
|
||||
'custom_attributes' => array(),
|
||||
);
|
||||
|
||||
$data = wp_parse_args( $data, $defaults );
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<tr valign="top">
|
||||
<th scope="row" class="titledesc">
|
||||
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
|
||||
</th>
|
||||
<td class="forminp">
|
||||
<fieldset>
|
||||
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
|
||||
<span class="colorpickpreview" style="background:<?php echo esc_attr( $this->get_option( $key ) ); ?>;"> </span>
|
||||
<input class="colorpick <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> />
|
||||
<div id="colorPickerDiv_<?php echo esc_attr( $field_key ); ?>" class="colorpickdiv" style="z-index: 100; background: #eee; border: 1px solid #ccc; position: absolute; display: none;"></div>
|
||||
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
|
||||
</fieldset>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Textarea HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_textarea_html( $key, $data ) {
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$defaults = array(
|
||||
'title' => '',
|
||||
'disabled' => false,
|
||||
'class' => '',
|
||||
'css' => '',
|
||||
'placeholder' => '',
|
||||
'type' => 'text',
|
||||
'desc_tip' => false,
|
||||
'description' => '',
|
||||
'custom_attributes' => array(),
|
||||
);
|
||||
|
||||
$data = wp_parse_args( $data, $defaults );
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<tr valign="top">
|
||||
<th scope="row" class="titledesc">
|
||||
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
|
||||
</th>
|
||||
<td class="forminp">
|
||||
<fieldset>
|
||||
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
|
||||
<textarea rows="3" cols="20" class="input-text wide-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?>><?php echo esc_textarea( $this->get_option( $key ) ); ?></textarea>
|
||||
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
|
||||
</fieldset>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Checkbox HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_checkbox_html( $key, $data ) {
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$defaults = array(
|
||||
'title' => '',
|
||||
'label' => '',
|
||||
'disabled' => false,
|
||||
'class' => '',
|
||||
'css' => '',
|
||||
'type' => 'text',
|
||||
'desc_tip' => false,
|
||||
'description' => '',
|
||||
'custom_attributes' => array(),
|
||||
);
|
||||
|
||||
$data = wp_parse_args( $data, $defaults );
|
||||
|
||||
if ( ! $data['label'] ) {
|
||||
$data['label'] = $data['title'];
|
||||
}
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<tr valign="top">
|
||||
<th scope="row" class="titledesc">
|
||||
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
|
||||
</th>
|
||||
<td class="forminp">
|
||||
<fieldset>
|
||||
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
|
||||
<label for="<?php echo esc_attr( $field_key ); ?>">
|
||||
<input <?php disabled( $data['disabled'], true ); ?> class="<?php echo esc_attr( $data['class'] ); ?>" type="checkbox" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="1" <?php checked( $this->get_option( $key ), 'yes' ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> /> <?php echo wp_kses_post( $data['label'] ); ?></label><br/>
|
||||
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
|
||||
</fieldset>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Select HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_select_html( $key, $data ) {
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$defaults = array(
|
||||
'title' => '',
|
||||
'disabled' => false,
|
||||
'class' => '',
|
||||
'css' => '',
|
||||
'placeholder' => '',
|
||||
'type' => 'text',
|
||||
'desc_tip' => false,
|
||||
'description' => '',
|
||||
'custom_attributes' => array(),
|
||||
'options' => array(),
|
||||
);
|
||||
|
||||
$data = wp_parse_args( $data, $defaults );
|
||||
$value = $this->get_option( $key );
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<tr valign="top">
|
||||
<th scope="row" class="titledesc">
|
||||
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
|
||||
</th>
|
||||
<td class="forminp">
|
||||
<fieldset>
|
||||
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
|
||||
<select class="select <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?>>
|
||||
<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
|
||||
<?php if ( is_array( $option_value ) ) : ?>
|
||||
<optgroup label="<?php echo esc_attr( $option_key ); ?>">
|
||||
<?php foreach ( $option_value as $option_key_inner => $option_value_inner ) : ?>
|
||||
<option value="<?php echo esc_attr( $option_key_inner ); ?>" <?php selected( (string) $option_key_inner, esc_attr( $value ) ); ?>><?php echo esc_html( $option_value_inner ); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</optgroup>
|
||||
<?php else : ?>
|
||||
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( (string) $option_key, esc_attr( $value ) ); ?>><?php echo esc_html( $option_value ); ?></option>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
|
||||
</fieldset>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Multiselect HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_multiselect_html( $key, $data ) {
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$defaults = array(
|
||||
'title' => '',
|
||||
'disabled' => false,
|
||||
'class' => '',
|
||||
'css' => '',
|
||||
'placeholder' => '',
|
||||
'type' => 'text',
|
||||
'desc_tip' => false,
|
||||
'description' => '',
|
||||
'custom_attributes' => array(),
|
||||
'select_buttons' => false,
|
||||
'options' => array(),
|
||||
);
|
||||
|
||||
$data = wp_parse_args( $data, $defaults );
|
||||
$value = (array) $this->get_option( $key, array() );
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<tr valign="top">
|
||||
<th scope="row" class="titledesc">
|
||||
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
|
||||
</th>
|
||||
<td class="forminp">
|
||||
<fieldset>
|
||||
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
|
||||
<select multiple="multiple" class="multiselect <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>[]" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?>>
|
||||
<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
|
||||
<?php if ( is_array( $option_value ) ) : ?>
|
||||
<optgroup label="<?php echo esc_attr( $option_key ); ?>">
|
||||
<?php foreach ( $option_value as $option_key_inner => $option_value_inner ) : ?>
|
||||
<option value="<?php echo esc_attr( $option_key_inner ); ?>" <?php selected( in_array( (string) $option_key_inner, $value, true ), true ); ?>><?php echo esc_html( $option_value_inner ); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</optgroup>
|
||||
<?php else : ?>
|
||||
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( (string) $option_key, $value, true ), true ); ?>><?php echo esc_html( $option_value ); ?></option>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
|
||||
<?php if ( $data['select_buttons'] ) : ?>
|
||||
<br/><a class="select_all button" href="#"><?php esc_html_e( 'Select all', 'woocommerce' ); ?></a> <a class="select_none button" href="#"><?php esc_html_e( 'Select none', 'woocommerce' ); ?></a>
|
||||
<?php endif; ?>
|
||||
</fieldset>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Title HTML.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param array $data Field data.
|
||||
* @since 1.0.0
|
||||
* @return string
|
||||
*/
|
||||
public function generate_title_html( $key, $data ) {
|
||||
$field_key = $this->get_field_key( $key );
|
||||
$defaults = array(
|
||||
'title' => '',
|
||||
'class' => '',
|
||||
);
|
||||
|
||||
$data = wp_parse_args( $data, $defaults );
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
</table>
|
||||
<h3 class="wc-settings-sub-title <?php echo esc_attr( $data['class'] ); ?>" id="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></h3>
|
||||
<?php if ( ! empty( $data['description'] ) ) : ?>
|
||||
<p><?php echo wp_kses_post( $data['description'] ); ?></p>
|
||||
<?php endif; ?>
|
||||
<table class="form-table">
|
||||
<?php
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Text Field.
|
||||
*
|
||||
* Make sure the data is escaped correctly, etc.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param string $value Posted Value.
|
||||
* @return string
|
||||
*/
|
||||
public function validate_text_field( $key, $value ) {
|
||||
$value = is_null( $value ) ? '' : $value;
|
||||
return wp_kses_post( trim( stripslashes( $value ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Price Field.
|
||||
*
|
||||
* Make sure the data is escaped correctly, etc.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param string $value Posted Value.
|
||||
* @return string
|
||||
*/
|
||||
public function validate_price_field( $key, $value ) {
|
||||
$value = is_null( $value ) ? '' : $value;
|
||||
return ( '' === $value ) ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Decimal Field.
|
||||
*
|
||||
* Make sure the data is escaped correctly, etc.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param string $value Posted Value.
|
||||
* @return string
|
||||
*/
|
||||
public function validate_decimal_field( $key, $value ) {
|
||||
$value = is_null( $value ) ? '' : $value;
|
||||
return ( '' === $value ) ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Password Field. No input sanitization is used to avoid corrupting passwords.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param string $value Posted Value.
|
||||
* @return string
|
||||
*/
|
||||
public function validate_password_field( $key, $value ) {
|
||||
$value = is_null( $value ) ? '' : $value;
|
||||
return trim( stripslashes( $value ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Textarea Field.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param string $value Posted Value.
|
||||
* @return string
|
||||
*/
|
||||
public function validate_textarea_field( $key, $value ) {
|
||||
$value = is_null( $value ) ? '' : $value;
|
||||
return wp_kses(
|
||||
trim( stripslashes( $value ) ),
|
||||
array_merge(
|
||||
array(
|
||||
'iframe' => array(
|
||||
'src' => true,
|
||||
'style' => true,
|
||||
'id' => true,
|
||||
'class' => true,
|
||||
),
|
||||
),
|
||||
wp_kses_allowed_html( 'post' )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Checkbox Field.
|
||||
*
|
||||
* If not set, return "no", otherwise return "yes".
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param string $value Posted Value.
|
||||
* @return string
|
||||
*/
|
||||
public function validate_checkbox_field( $key, $value ) {
|
||||
return ! is_null( $value ) ? 'yes' : 'no';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Select Field.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param string $value Posted Value.
|
||||
* @return string
|
||||
*/
|
||||
public function validate_select_field( $key, $value ) {
|
||||
$value = is_null( $value ) ? '' : $value;
|
||||
return wc_clean( stripslashes( $value ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Multiselect Field.
|
||||
*
|
||||
* @param string $key Field key.
|
||||
* @param string $value Posted Value.
|
||||
* @return string|array
|
||||
*/
|
||||
public function validate_multiselect_field( $key, $value ) {
|
||||
return is_array( $value ) ? array_map( 'wc_clean', array_map( 'stripslashes', $value ) ) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the data on the "Settings" form.
|
||||
*
|
||||
* @deprecated 2.6.0 No longer used.
|
||||
* @param array $form_fields Array of fields.
|
||||
*/
|
||||
public function validate_settings_fields( $form_fields = array() ) {
|
||||
wc_deprecated_function( 'validate_settings_fields', '2.6' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Format settings if needed.
|
||||
*
|
||||
* @deprecated 2.6.0 Unused.
|
||||
* @param array $value Value to format.
|
||||
* @return array
|
||||
*/
|
||||
public function format_settings( $value ) {
|
||||
wc_deprecated_function( 'format_settings', '2.6' );
|
||||
return $value;
|
||||
}
|
||||
}
|
569
includes/abstracts/abstract-wc-shipping-method.php
Normal file
569
includes/abstracts/abstract-wc-shipping-method.php
Normal file
@ -0,0 +1,569 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract shipping method
|
||||
*
|
||||
* @class WC_Shipping_Method
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WooCommerce Shipping Method Class.
|
||||
*
|
||||
* Extended by shipping methods to handle shipping calculations etc.
|
||||
*
|
||||
* @class WC_Shipping_Method
|
||||
* @version 3.0.0
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
abstract class WC_Shipping_Method extends WC_Settings_API {
|
||||
|
||||
/**
|
||||
* Features this method supports. Possible features used by core:
|
||||
* - shipping-zones Shipping zone functionality + instances
|
||||
* - instance-settings Instance settings screens.
|
||||
* - settings Non-instance settings screens. Enabled by default for BW compatibility with methods before instances existed.
|
||||
* - instance-settings-modal Allows the instance settings to be loaded within a modal in the zones UI.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $supports = array( 'settings' );
|
||||
|
||||
/**
|
||||
* Unique ID for the shipping method - must be set.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $id = '';
|
||||
|
||||
/**
|
||||
* Method title.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $method_title = '';
|
||||
|
||||
/**
|
||||
* Method description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $method_description = '';
|
||||
|
||||
/**
|
||||
* Yes or no based on whether the method is enabled.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $enabled = 'yes';
|
||||
|
||||
/**
|
||||
* Shipping method title for the frontend.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $title;
|
||||
|
||||
/**
|
||||
* This is an array of rates - methods must populate this array to register shipping costs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $rates = array();
|
||||
|
||||
/**
|
||||
* If 'taxable' tax will be charged for this method (if applicable).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $tax_status = 'taxable';
|
||||
|
||||
/**
|
||||
* Fee for the method (if applicable).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $fee = null;
|
||||
|
||||
/**
|
||||
* Minimum fee for the method (if applicable).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $minimum_fee = null;
|
||||
|
||||
/**
|
||||
* Instance ID if used.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $instance_id = 0;
|
||||
|
||||
/**
|
||||
* Instance form fields.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $instance_form_fields = array();
|
||||
|
||||
/**
|
||||
* Instance settings.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $instance_settings = array();
|
||||
|
||||
/**
|
||||
* Availability - legacy. Used for method Availability.
|
||||
* No longer useful for instance based shipping methods.
|
||||
*
|
||||
* @deprecated 2.6.0
|
||||
* @var string
|
||||
*/
|
||||
public $availability;
|
||||
|
||||
/**
|
||||
* Availability countries - legacy. Used for method Availability.
|
||||
* No longer useful for instance based shipping methods.
|
||||
*
|
||||
* @deprecated 2.6.0
|
||||
* @var array
|
||||
*/
|
||||
public $countries = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $instance_id Instance ID.
|
||||
*/
|
||||
public function __construct( $instance_id = 0 ) {
|
||||
$this->instance_id = absint( $instance_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a shipping method supports a given feature.
|
||||
*
|
||||
* Methods should override this to declare support (or lack of support) for a feature.
|
||||
*
|
||||
* @param string $feature The name of a feature to test support for.
|
||||
* @return bool True if the shipping method supports the feature, false otherwise.
|
||||
*/
|
||||
public function supports( $feature ) {
|
||||
return apply_filters( 'woocommerce_shipping_method_supports', in_array( $feature, $this->supports ), $feature, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to calculate shipping rates for this method. Rates can be added using the add_rate() method.
|
||||
*
|
||||
* @param array $package Package array.
|
||||
*/
|
||||
public function calculate_shipping( $package = array() ) {}
|
||||
|
||||
/**
|
||||
* Whether or not we need to calculate tax on top of the shipping rate.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_taxable() {
|
||||
return wc_tax_enabled() && 'taxable' === $this->tax_status && ( WC()->customer && ! WC()->customer->get_is_vat_exempt() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not this method is enabled in settings.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_enabled() {
|
||||
return 'yes' === $this->enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the shipping method instance ID.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return int
|
||||
*/
|
||||
public function get_instance_id() {
|
||||
return $this->instance_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the shipping method title.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return string
|
||||
*/
|
||||
public function get_method_title() {
|
||||
return apply_filters( 'woocommerce_shipping_method_title', $this->method_title, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the shipping method description.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return string
|
||||
*/
|
||||
public function get_method_description() {
|
||||
return apply_filters( 'woocommerce_shipping_method_description', $this->method_description, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the shipping title which is user set.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_title() {
|
||||
return apply_filters( 'woocommerce_shipping_method_title', $this->title, $this->id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return calculated rates for a package.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param array $package Package array.
|
||||
* @return array
|
||||
*/
|
||||
public function get_rates_for_package( $package ) {
|
||||
$this->rates = array();
|
||||
if ( $this->is_available( $package ) && ( empty( $package['ship_via'] ) || in_array( $this->id, $package['ship_via'] ) ) ) {
|
||||
$this->calculate_shipping( $package );
|
||||
}
|
||||
return $this->rates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a rate ID based on this methods ID and instance, with an optional
|
||||
* suffix if distinguishing between multiple rates.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param string $suffix Suffix.
|
||||
* @return string
|
||||
*/
|
||||
public function get_rate_id( $suffix = '' ) {
|
||||
$rate_id = array( $this->id );
|
||||
|
||||
if ( $this->instance_id ) {
|
||||
$rate_id[] = $this->instance_id;
|
||||
}
|
||||
|
||||
if ( $suffix ) {
|
||||
$rate_id[] = $suffix;
|
||||
}
|
||||
|
||||
return implode( ':', $rate_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a shipping rate. If taxes are not set they will be calculated based on cost.
|
||||
*
|
||||
* @param array $args Arguments (default: array()).
|
||||
*/
|
||||
public function add_rate( $args = array() ) {
|
||||
$args = apply_filters(
|
||||
'woocommerce_shipping_method_add_rate_args',
|
||||
wp_parse_args(
|
||||
$args,
|
||||
array(
|
||||
'id' => $this->get_rate_id(), // ID for the rate. If not passed, this id:instance default will be used.
|
||||
'label' => '', // Label for the rate.
|
||||
'cost' => '0', // Amount or array of costs (per item shipping).
|
||||
'taxes' => '', // Pass taxes, or leave empty to have it calculated for you, or 'false' to disable calculations.
|
||||
'calc_tax' => 'per_order', // Calc tax per_order or per_item. Per item needs an array of costs.
|
||||
'meta_data' => array(), // Array of misc meta data to store along with this rate - key value pairs.
|
||||
'package' => false, // Package array this rate was generated for @since 2.6.0.
|
||||
'price_decimals' => wc_get_price_decimals(),
|
||||
)
|
||||
),
|
||||
$this
|
||||
);
|
||||
|
||||
// ID and label are required.
|
||||
if ( ! $args['id'] || ! $args['label'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Total up the cost.
|
||||
$total_cost = is_array( $args['cost'] ) ? array_sum( $args['cost'] ) : $args['cost'];
|
||||
$taxes = $args['taxes'];
|
||||
|
||||
// Taxes - if not an array and not set to false, calc tax based on cost and passed calc_tax variable. This saves shipping methods having to do complex tax calculations.
|
||||
if ( ! is_array( $taxes ) && false !== $taxes && $total_cost > 0 && $this->is_taxable() ) {
|
||||
$taxes = 'per_item' === $args['calc_tax'] ? $this->get_taxes_per_item( $args['cost'] ) : WC_Tax::calc_shipping_tax( $total_cost, WC_Tax::get_shipping_tax_rates() );
|
||||
}
|
||||
|
||||
// Round the total cost after taxes have been calculated.
|
||||
$total_cost = wc_format_decimal( $total_cost, $args['price_decimals'] );
|
||||
|
||||
// Create rate object.
|
||||
$rate = new WC_Shipping_Rate();
|
||||
$rate->set_id( $args['id'] );
|
||||
$rate->set_method_id( $this->id );
|
||||
$rate->set_instance_id( $this->instance_id );
|
||||
$rate->set_label( $args['label'] );
|
||||
$rate->set_cost( $total_cost );
|
||||
$rate->set_taxes( $taxes );
|
||||
|
||||
if ( ! empty( $args['meta_data'] ) ) {
|
||||
foreach ( $args['meta_data'] as $key => $value ) {
|
||||
$rate->add_meta_data( $key, $value );
|
||||
}
|
||||
}
|
||||
|
||||
// Store package data.
|
||||
if ( $args['package'] ) {
|
||||
$items_in_package = array();
|
||||
foreach ( $args['package']['contents'] as $item ) {
|
||||
$product = $item['data'];
|
||||
$items_in_package[] = $product->get_name() . ' × ' . $item['quantity'];
|
||||
}
|
||||
$rate->add_meta_data( __( 'Items', 'woocommerce' ), implode( ', ', $items_in_package ) );
|
||||
}
|
||||
|
||||
$this->rates[ $args['id'] ] = apply_filters( 'woocommerce_shipping_method_add_rate', $rate, $args, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Calc taxes per item being shipping in costs array.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param array $costs Costs.
|
||||
* @return array of taxes
|
||||
*/
|
||||
protected function get_taxes_per_item( $costs ) {
|
||||
$taxes = array();
|
||||
|
||||
// If we have an array of costs we can look up each items tax class and add tax accordingly.
|
||||
if ( is_array( $costs ) ) {
|
||||
|
||||
$cart = WC()->cart->get_cart();
|
||||
|
||||
foreach ( $costs as $cost_key => $amount ) {
|
||||
if ( ! isset( $cart[ $cost_key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$item_taxes = WC_Tax::calc_shipping_tax( $amount, WC_Tax::get_shipping_tax_rates( $cart[ $cost_key ]['data']->get_tax_class() ) );
|
||||
|
||||
// Sum the item taxes.
|
||||
foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
|
||||
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
|
||||
}
|
||||
}
|
||||
|
||||
// Add any cost for the order - order costs are in the key 'order'.
|
||||
if ( isset( $costs['order'] ) ) {
|
||||
$item_taxes = WC_Tax::calc_shipping_tax( $costs['order'], WC_Tax::get_shipping_tax_rates() );
|
||||
|
||||
// Sum the item taxes.
|
||||
foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
|
||||
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $taxes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this method available?
|
||||
*
|
||||
* @param array $package Package.
|
||||
* @return bool
|
||||
*/
|
||||
public function is_available( $package ) {
|
||||
$available = $this->is_enabled();
|
||||
|
||||
// Country availability (legacy, for non-zone based methods).
|
||||
if ( ! $this->instance_id && $available ) {
|
||||
$countries = is_array( $this->countries ) ? $this->countries : array();
|
||||
|
||||
switch ( $this->availability ) {
|
||||
case 'specific':
|
||||
case 'including':
|
||||
$available = in_array( $package['destination']['country'], array_intersect( $countries, array_keys( WC()->countries->get_shipping_countries() ) ) );
|
||||
break;
|
||||
case 'excluding':
|
||||
$available = in_array( $package['destination']['country'], array_diff( array_keys( WC()->countries->get_shipping_countries() ), $countries ) );
|
||||
break;
|
||||
default:
|
||||
$available = in_array( $package['destination']['country'], array_keys( WC()->countries->get_shipping_countries() ) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fee to add to shipping cost.
|
||||
*
|
||||
* @param string|float $fee Fee.
|
||||
* @param float $total Total.
|
||||
* @return float
|
||||
*/
|
||||
public function get_fee( $fee, $total ) {
|
||||
if ( strstr( $fee, '%' ) ) {
|
||||
$fee = ( $total / 100 ) * str_replace( '%', '', $fee );
|
||||
}
|
||||
if ( ! empty( $this->minimum_fee ) && $this->minimum_fee > $fee ) {
|
||||
$fee = $this->minimum_fee;
|
||||
}
|
||||
return $fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this method have a settings page?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has_settings() {
|
||||
return $this->instance_id ? $this->supports( 'instance-settings' ) : $this->supports( 'settings' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return admin options as a html string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_admin_options_html() {
|
||||
if ( $this->instance_id ) {
|
||||
$settings_html = $this->generate_settings_html( $this->get_instance_form_fields(), false );
|
||||
} else {
|
||||
$settings_html = $this->generate_settings_html( $this->get_form_fields(), false );
|
||||
}
|
||||
|
||||
return '<table class="form-table">' . $settings_html . '</table>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the shipping settings screen.
|
||||
*/
|
||||
public function admin_options() {
|
||||
if ( ! $this->instance_id ) {
|
||||
echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>';
|
||||
}
|
||||
echo wp_kses_post( wpautop( $this->get_method_description() ) );
|
||||
echo $this->get_admin_options_html(); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
* Get_option function.
|
||||
*
|
||||
* Gets and option from the settings API, using defaults if necessary to prevent undefined notices.
|
||||
*
|
||||
* @param string $key Key.
|
||||
* @param mixed $empty_value Empty value.
|
||||
* @return mixed The value specified for the option or a default value for the option.
|
||||
*/
|
||||
public function get_option( $key, $empty_value = null ) {
|
||||
// Instance options take priority over global options.
|
||||
if ( $this->instance_id && array_key_exists( $key, $this->get_instance_form_fields() ) ) {
|
||||
return $this->get_instance_option( $key, $empty_value );
|
||||
}
|
||||
|
||||
// Return global option.
|
||||
$option = apply_filters( 'woocommerce_shipping_' . $this->id . '_option', parent::get_option( $key, $empty_value ), $key, $this );
|
||||
return $option;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
|
||||
*
|
||||
* @param string $key Key.
|
||||
* @param mixed $empty_value Empty value.
|
||||
* @return mixed The value specified for the option or a default value for the option.
|
||||
*/
|
||||
public function get_instance_option( $key, $empty_value = null ) {
|
||||
if ( empty( $this->instance_settings ) ) {
|
||||
$this->init_instance_settings();
|
||||
}
|
||||
|
||||
// Get option default if unset.
|
||||
if ( ! isset( $this->instance_settings[ $key ] ) ) {
|
||||
$form_fields = $this->get_instance_form_fields();
|
||||
$this->instance_settings[ $key ] = $this->get_field_default( $form_fields[ $key ] );
|
||||
}
|
||||
|
||||
if ( ! is_null( $empty_value ) && '' === $this->instance_settings[ $key ] ) {
|
||||
$this->instance_settings[ $key ] = $empty_value;
|
||||
}
|
||||
|
||||
$instance_option = apply_filters( 'woocommerce_shipping_' . $this->id . '_instance_option', $this->instance_settings[ $key ], $key, $this );
|
||||
return $instance_option;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get settings fields for instances of this shipping method (within zones).
|
||||
* Should be overridden by shipping methods to add options.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return array
|
||||
*/
|
||||
public function get_instance_form_fields() {
|
||||
return apply_filters( 'woocommerce_shipping_instance_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->instance_form_fields ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the option in the WP DB.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return string
|
||||
*/
|
||||
public function get_instance_option_key() {
|
||||
return $this->instance_id ? $this->plugin_id . $this->id . '_' . $this->instance_id . '_settings' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise Settings for instances.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function init_instance_settings() {
|
||||
$this->instance_settings = get_option( $this->get_instance_option_key(), null );
|
||||
|
||||
// If there are no settings defined, use defaults.
|
||||
if ( ! is_array( $this->instance_settings ) ) {
|
||||
$form_fields = $this->get_instance_form_fields();
|
||||
$this->instance_settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes and saves global shipping method options in the admin area.
|
||||
*
|
||||
* This method is usually attached to woocommerce_update_options_x hooks.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @return bool was anything saved?
|
||||
*/
|
||||
public function process_admin_options() {
|
||||
if ( ! $this->instance_id ) {
|
||||
return parent::process_admin_options();
|
||||
}
|
||||
|
||||
// Check we are processing the correct form for this instance.
|
||||
if ( ! isset( $_REQUEST['instance_id'] ) || absint( $_REQUEST['instance_id'] ) !== $this->instance_id ) { // WPCS: input var ok, CSRF ok.
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->init_instance_settings();
|
||||
|
||||
$post_data = $this->get_post_data();
|
||||
|
||||
foreach ( $this->get_instance_form_fields() as $key => $field ) {
|
||||
if ( 'title' !== $this->get_field_type( $field ) ) {
|
||||
try {
|
||||
$this->instance_settings[ $key ] = $this->get_field_value( $key, $field, $post_data );
|
||||
} catch ( Exception $e ) {
|
||||
$this->add_error( $e->getMessage() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return update_option( $this->get_instance_option_key(), apply_filters( 'woocommerce_shipping_' . $this->id . '_instance_settings_values', $this->instance_settings, $this ), 'yes' );
|
||||
}
|
||||
}
|
408
includes/abstracts/abstract-wc-widget.php
Normal file
408
includes/abstracts/abstract-wc-widget.php
Normal file
@ -0,0 +1,408 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract widget class
|
||||
*
|
||||
* @class WC_Widget
|
||||
* @package WooCommerce\Abstracts
|
||||
*/
|
||||
|
||||
use Automattic\Jetpack\Constants;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WC_Widget
|
||||
*
|
||||
* @package WooCommerce\Abstracts
|
||||
* @version 2.5.0
|
||||
* @extends WP_Widget
|
||||
*/
|
||||
abstract class WC_Widget extends WP_Widget {
|
||||
|
||||
/**
|
||||
* CSS class.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $widget_cssclass;
|
||||
|
||||
/**
|
||||
* Widget description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $widget_description;
|
||||
|
||||
/**
|
||||
* Widget ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $widget_id;
|
||||
|
||||
/**
|
||||
* Widget name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $widget_name;
|
||||
|
||||
/**
|
||||
* Settings.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $settings;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$widget_ops = array(
|
||||
'classname' => $this->widget_cssclass,
|
||||
'description' => $this->widget_description,
|
||||
'customize_selective_refresh' => true,
|
||||
'show_instance_in_rest' => true,
|
||||
);
|
||||
|
||||
parent::__construct( $this->widget_id, $this->widget_name, $widget_ops );
|
||||
|
||||
add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
|
||||
add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
|
||||
add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached widget.
|
||||
*
|
||||
* @param array $args Arguments.
|
||||
* @return bool true if the widget is cached otherwise false
|
||||
*/
|
||||
public function get_cached_widget( $args ) {
|
||||
// Don't get cache if widget_id doesn't exists.
|
||||
if ( empty( $args['widget_id'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cache = wp_cache_get( $this->get_widget_id_for_cache( $this->widget_id ), 'widget' );
|
||||
|
||||
if ( ! is_array( $cache ) ) {
|
||||
$cache = array();
|
||||
}
|
||||
|
||||
if ( isset( $cache[ $this->get_widget_id_for_cache( $args['widget_id'] ) ] ) ) {
|
||||
echo $cache[ $this->get_widget_id_for_cache( $args['widget_id'] ) ]; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache the widget.
|
||||
*
|
||||
* @param array $args Arguments.
|
||||
* @param string $content Content.
|
||||
* @return string the content that was cached
|
||||
*/
|
||||
public function cache_widget( $args, $content ) {
|
||||
// Don't set any cache if widget_id doesn't exist.
|
||||
if ( empty( $args['widget_id'] ) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$cache = wp_cache_get( $this->get_widget_id_for_cache( $this->widget_id ), 'widget' );
|
||||
|
||||
if ( ! is_array( $cache ) ) {
|
||||
$cache = array();
|
||||
}
|
||||
|
||||
$cache[ $this->get_widget_id_for_cache( $args['widget_id'] ) ] = $content;
|
||||
|
||||
wp_cache_set( $this->get_widget_id_for_cache( $this->widget_id ), $cache, 'widget' );
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the cache.
|
||||
*/
|
||||
public function flush_widget_cache() {
|
||||
foreach ( array( 'https', 'http' ) as $scheme ) {
|
||||
wp_cache_delete( $this->get_widget_id_for_cache( $this->widget_id, $scheme ), 'widget' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this widgets title.
|
||||
*
|
||||
* @param array $instance Array of instance options.
|
||||
* @return string
|
||||
*/
|
||||
protected function get_instance_title( $instance ) {
|
||||
if ( isset( $instance['title'] ) ) {
|
||||
return $instance['title'];
|
||||
}
|
||||
|
||||
if ( isset( $this->settings, $this->settings['title'], $this->settings['title']['std'] ) ) {
|
||||
return $this->settings['title']['std'];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the html at the start of a widget.
|
||||
*
|
||||
* @param array $args Arguments.
|
||||
* @param array $instance Instance.
|
||||
*/
|
||||
public function widget_start( $args, $instance ) {
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
|
||||
|
||||
$title = apply_filters( 'widget_title', $this->get_instance_title( $instance ), $instance, $this->id_base );
|
||||
|
||||
if ( $title ) {
|
||||
echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the html at the end of a widget.
|
||||
*
|
||||
* @param array $args Arguments.
|
||||
*/
|
||||
public function widget_end( $args ) {
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a particular instance of a widget.
|
||||
*
|
||||
* @see WP_Widget->update
|
||||
* @param array $new_instance New instance.
|
||||
* @param array $old_instance Old instance.
|
||||
* @return array
|
||||
*/
|
||||
public function update( $new_instance, $old_instance ) {
|
||||
|
||||
$instance = $old_instance;
|
||||
|
||||
if ( empty( $this->settings ) ) {
|
||||
return $instance;
|
||||
}
|
||||
|
||||
// Loop settings and get values to save.
|
||||
foreach ( $this->settings as $key => $setting ) {
|
||||
if ( ! isset( $setting['type'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Format the value based on settings type.
|
||||
switch ( $setting['type'] ) {
|
||||
case 'number':
|
||||
$instance[ $key ] = absint( $new_instance[ $key ] );
|
||||
|
||||
if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
|
||||
$instance[ $key ] = max( $instance[ $key ], $setting['min'] );
|
||||
}
|
||||
|
||||
if ( isset( $setting['max'] ) && '' !== $setting['max'] ) {
|
||||
$instance[ $key ] = min( $instance[ $key ], $setting['max'] );
|
||||
}
|
||||
break;
|
||||
case 'textarea':
|
||||
$instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
|
||||
break;
|
||||
case 'checkbox':
|
||||
$instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
|
||||
break;
|
||||
default:
|
||||
$instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : $setting['std'];
|
||||
break;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the value of a setting.
|
||||
*/
|
||||
$instance[ $key ] = apply_filters( 'woocommerce_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting );
|
||||
}
|
||||
|
||||
$this->flush_widget_cache();
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the settings update form.
|
||||
*
|
||||
* @see WP_Widget->form
|
||||
*
|
||||
* @param array $instance Instance.
|
||||
*/
|
||||
public function form( $instance ) {
|
||||
|
||||
if ( empty( $this->settings ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $this->settings as $key => $setting ) {
|
||||
|
||||
$class = isset( $setting['class'] ) ? $setting['class'] : '';
|
||||
$value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
|
||||
|
||||
switch ( $setting['type'] ) {
|
||||
|
||||
case 'text':
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label><?php // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?>
|
||||
<input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" />
|
||||
</p>
|
||||
<?php
|
||||
break;
|
||||
|
||||
case 'number':
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label>
|
||||
<input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="number" step="<?php echo esc_attr( $setting['step'] ); ?>" min="<?php echo esc_attr( $setting['min'] ); ?>" max="<?php echo esc_attr( $setting['max'] ); ?>" value="<?php echo esc_attr( $value ); ?>" />
|
||||
</p>
|
||||
<?php
|
||||
break;
|
||||
|
||||
case 'select':
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label>
|
||||
<select class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>">
|
||||
<?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
|
||||
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</p>
|
||||
<?php
|
||||
break;
|
||||
|
||||
case 'textarea':
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label>
|
||||
<textarea class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" cols="20" rows="3"><?php echo esc_textarea( $value ); ?></textarea>
|
||||
<?php if ( isset( $setting['desc'] ) ) : ?>
|
||||
<small><?php echo esc_html( $setting['desc'] ); ?></small>
|
||||
<?php endif; ?>
|
||||
</p>
|
||||
<?php
|
||||
break;
|
||||
|
||||
case 'checkbox':
|
||||
?>
|
||||
<p>
|
||||
<input class="checkbox <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> />
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label>
|
||||
</p>
|
||||
<?php
|
||||
break;
|
||||
|
||||
// Default: run an action.
|
||||
default:
|
||||
do_action( 'woocommerce_widget_field_' . $setting['type'], $key, $value, $setting, $instance );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current page URL with various filtering props supported by WC.
|
||||
*
|
||||
* @return string
|
||||
* @since 3.3.0
|
||||
*/
|
||||
protected function get_current_page_url() {
|
||||
if ( Constants::is_defined( 'SHOP_IS_ON_FRONT' ) ) {
|
||||
$link = home_url();
|
||||
} elseif ( is_shop() ) {
|
||||
$link = get_permalink( wc_get_page_id( 'shop' ) );
|
||||
} elseif ( is_product_category() ) {
|
||||
$link = get_term_link( get_query_var( 'product_cat' ), 'product_cat' );
|
||||
} elseif ( is_product_tag() ) {
|
||||
$link = get_term_link( get_query_var( 'product_tag' ), 'product_tag' );
|
||||
} else {
|
||||
$queried_object = get_queried_object();
|
||||
$link = get_term_link( $queried_object->slug, $queried_object->taxonomy );
|
||||
}
|
||||
|
||||
// Min/Max.
|
||||
if ( isset( $_GET['min_price'] ) ) {
|
||||
$link = add_query_arg( 'min_price', wc_clean( wp_unslash( $_GET['min_price'] ) ), $link );
|
||||
}
|
||||
|
||||
if ( isset( $_GET['max_price'] ) ) {
|
||||
$link = add_query_arg( 'max_price', wc_clean( wp_unslash( $_GET['max_price'] ) ), $link );
|
||||
}
|
||||
|
||||
// Order by.
|
||||
if ( isset( $_GET['orderby'] ) ) {
|
||||
$link = add_query_arg( 'orderby', wc_clean( wp_unslash( $_GET['orderby'] ) ), $link );
|
||||
}
|
||||
|
||||
/**
|
||||
* Search Arg.
|
||||
* To support quote characters, first they are decoded from " entities, then URL encoded.
|
||||
*/
|
||||
if ( get_search_query() ) {
|
||||
$link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query() ) ), $link );
|
||||
}
|
||||
|
||||
// Post Type Arg.
|
||||
if ( isset( $_GET['post_type'] ) ) {
|
||||
$link = add_query_arg( 'post_type', wc_clean( wp_unslash( $_GET['post_type'] ) ), $link );
|
||||
|
||||
// Prevent post type and page id when pretty permalinks are disabled.
|
||||
if ( is_shop() ) {
|
||||
$link = remove_query_arg( 'page_id', $link );
|
||||
}
|
||||
}
|
||||
|
||||
// Min Rating Arg.
|
||||
if ( isset( $_GET['rating_filter'] ) ) {
|
||||
$link = add_query_arg( 'rating_filter', wc_clean( wp_unslash( $_GET['rating_filter'] ) ), $link );
|
||||
}
|
||||
|
||||
// All current filters.
|
||||
if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure, WordPress.CodeAnalysis.AssignmentInCondition.Found
|
||||
foreach ( $_chosen_attributes as $name => $data ) {
|
||||
$filter_name = wc_attribute_taxonomy_slug( $name );
|
||||
if ( ! empty( $data['terms'] ) ) {
|
||||
$link = add_query_arg( 'filter_' . $filter_name, implode( ',', $data['terms'] ), $link );
|
||||
}
|
||||
if ( 'or' === $data['query_type'] ) {
|
||||
$link = add_query_arg( 'query_type_' . $filter_name, 'or', $link );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return apply_filters( 'woocommerce_widget_get_current_page_url', $link, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get widget id plus scheme/protocol to prevent serving mixed content from (persistently) cached widgets.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @param string $widget_id Id of the cached widget.
|
||||
* @param string $scheme Scheme for the widget id.
|
||||
* @return string Widget id including scheme/protocol.
|
||||
*/
|
||||
protected function get_widget_id_for_cache( $widget_id, $scheme = '' ) {
|
||||
if ( $scheme ) {
|
||||
$widget_id_for_cache = $widget_id . '-' . $scheme;
|
||||
} else {
|
||||
$widget_id_for_cache = $widget_id . '-' . ( is_ssl() ? 'https' : 'http' );
|
||||
}
|
||||
|
||||
return apply_filters( 'woocommerce_cached_widget_id', $widget_id_for_cache );
|
||||
}
|
||||
}
|
212
includes/abstracts/class-wc-background-process.php
Normal file
212
includes/abstracts/class-wc-background-process.php
Normal file
@ -0,0 +1,212 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract WP_Background_Process class.
|
||||
*
|
||||
* Uses https://github.com/A5hleyRich/wp-background-processing to handle DB
|
||||
* updates in the background.
|
||||
*
|
||||
* @package WooCommerce\Classes
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( ! class_exists( 'WP_Async_Request', false ) ) {
|
||||
include_once dirname( WC_PLUGIN_FILE ) . '/includes/libraries/wp-async-request.php';
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WP_Background_Process', false ) ) {
|
||||
include_once dirname( WC_PLUGIN_FILE ) . '/includes/libraries/wp-background-process.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* WC_Background_Process class.
|
||||
*/
|
||||
abstract class WC_Background_Process extends WP_Background_Process {
|
||||
|
||||
/**
|
||||
* Is queue empty.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_queue_empty() {
|
||||
global $wpdb;
|
||||
|
||||
$table = $wpdb->options;
|
||||
$column = 'option_name';
|
||||
|
||||
if ( is_multisite() ) {
|
||||
$table = $wpdb->sitemeta;
|
||||
$column = 'meta_key';
|
||||
}
|
||||
|
||||
$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
|
||||
|
||||
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s", $key ) ); // @codingStandardsIgnoreLine.
|
||||
|
||||
return ! ( $count > 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get batch.
|
||||
*
|
||||
* @return stdClass Return the first batch from the queue.
|
||||
*/
|
||||
protected function get_batch() {
|
||||
global $wpdb;
|
||||
|
||||
$table = $wpdb->options;
|
||||
$column = 'option_name';
|
||||
$key_column = 'option_id';
|
||||
$value_column = 'option_value';
|
||||
|
||||
if ( is_multisite() ) {
|
||||
$table = $wpdb->sitemeta;
|
||||
$column = 'meta_key';
|
||||
$key_column = 'meta_id';
|
||||
$value_column = 'meta_value';
|
||||
}
|
||||
|
||||
$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
|
||||
|
||||
$query = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1", $key ) ); // @codingStandardsIgnoreLine.
|
||||
|
||||
$batch = new stdClass();
|
||||
$batch->key = $query->$column;
|
||||
$batch->data = array_filter( (array) maybe_unserialize( $query->$value_column ) );
|
||||
|
||||
return $batch;
|
||||
}
|
||||
|
||||
/**
|
||||
* See if the batch limit has been exceeded.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function batch_limit_exceeded() {
|
||||
return $this->time_exceeded() || $this->memory_exceeded();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle.
|
||||
*
|
||||
* Pass each queue item to the task handler, while remaining
|
||||
* within server memory and time limit constraints.
|
||||
*/
|
||||
protected function handle() {
|
||||
$this->lock_process();
|
||||
|
||||
do {
|
||||
$batch = $this->get_batch();
|
||||
|
||||
foreach ( $batch->data as $key => $value ) {
|
||||
$task = $this->task( $value );
|
||||
|
||||
if ( false !== $task ) {
|
||||
$batch->data[ $key ] = $task;
|
||||
} else {
|
||||
unset( $batch->data[ $key ] );
|
||||
}
|
||||
|
||||
if ( $this->batch_limit_exceeded() ) {
|
||||
// Batch limits reached.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Update or delete current batch.
|
||||
if ( ! empty( $batch->data ) ) {
|
||||
$this->update( $batch->key, $batch->data );
|
||||
} else {
|
||||
$this->delete( $batch->key );
|
||||
}
|
||||
} while ( ! $this->batch_limit_exceeded() && ! $this->is_queue_empty() );
|
||||
|
||||
$this->unlock_process();
|
||||
|
||||
// Start next batch or complete process.
|
||||
if ( ! $this->is_queue_empty() ) {
|
||||
$this->dispatch();
|
||||
} else {
|
||||
$this->complete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get memory limit.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function get_memory_limit() {
|
||||
if ( function_exists( 'ini_get' ) ) {
|
||||
$memory_limit = ini_get( 'memory_limit' );
|
||||
} else {
|
||||
// Sensible default.
|
||||
$memory_limit = '128M';
|
||||
}
|
||||
|
||||
if ( ! $memory_limit || -1 === intval( $memory_limit ) ) {
|
||||
// Unlimited, set to 32GB.
|
||||
$memory_limit = '32G';
|
||||
}
|
||||
|
||||
return wp_convert_hr_to_bytes( $memory_limit );
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule cron healthcheck.
|
||||
*
|
||||
* @param array $schedules Schedules.
|
||||
* @return array
|
||||
*/
|
||||
public function schedule_cron_healthcheck( $schedules ) {
|
||||
$interval = apply_filters( $this->identifier . '_cron_interval', 5 );
|
||||
|
||||
if ( property_exists( $this, 'cron_interval' ) ) {
|
||||
$interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval );
|
||||
}
|
||||
|
||||
// Adds every 5 minutes to the existing schedules.
|
||||
$schedules[ $this->identifier . '_cron_interval' ] = array(
|
||||
'interval' => MINUTE_IN_SECONDS * $interval,
|
||||
/* translators: %d: interval */
|
||||
'display' => sprintf( __( 'Every %d minutes', 'woocommerce' ), $interval ),
|
||||
);
|
||||
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all batches.
|
||||
*
|
||||
* @return WC_Background_Process
|
||||
*/
|
||||
public function delete_all_batches() {
|
||||
global $wpdb;
|
||||
|
||||
$table = $wpdb->options;
|
||||
$column = 'option_name';
|
||||
|
||||
if ( is_multisite() ) {
|
||||
$table = $wpdb->sitemeta;
|
||||
$column = 'meta_key';
|
||||
}
|
||||
|
||||
$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
|
||||
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE {$column} LIKE %s", $key ) ); // @codingStandardsIgnoreLine.
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Kill process.
|
||||
*
|
||||
* Stop processing queue items, clear cronjob and delete all batches.
|
||||
*/
|
||||
public function kill_process() {
|
||||
if ( ! $this->is_queue_empty() ) {
|
||||
$this->delete_all_batches();
|
||||
wp_clear_scheduled_hook( $this->cron_hook_identifier );
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user