Initial Commit
This commit is contained in:
113
inc/database/products/class-product-query.php
Normal file
113
inc/database/products/class-product-query.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/**
|
||||
* Class used for querying products.
|
||||
*
|
||||
* @package WP_Ultimo
|
||||
* @subpackage Database\Products
|
||||
* @since 2.0.0
|
||||
*/
|
||||
|
||||
namespace WP_Ultimo\Database\Products;
|
||||
|
||||
use WP_Ultimo\Database\Engine\Query;
|
||||
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Class used for querying products.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class Product_Query extends Query {
|
||||
|
||||
/** Table Properties ******************************************************/
|
||||
|
||||
/**
|
||||
* Name of the database table to query.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
protected $table_name = 'products';
|
||||
|
||||
/**
|
||||
* String used to alias the database table in MySQL statement.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
protected $table_alias = 'p';
|
||||
|
||||
/**
|
||||
* Name of class used to setup the database schema
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
protected $table_schema = '\\WP_Ultimo\\Database\\Products\\Products_Schema';
|
||||
|
||||
/** Item ******************************************************************/
|
||||
|
||||
/**
|
||||
* Name for a single item
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
protected $item_name = 'product';
|
||||
|
||||
/**
|
||||
* Plural version for a group of items.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
protected $item_name_plural = 'products';
|
||||
|
||||
/**
|
||||
* Callback function for turning IDs into objects
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
* @var mixed
|
||||
*/
|
||||
protected $item_shape = '\\WP_Ultimo\\Models\\Product';
|
||||
|
||||
/**
|
||||
* Group to cache queries and queried items in.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
protected $cache_group = 'products';
|
||||
|
||||
/**
|
||||
* If we should use a global cache group.
|
||||
*
|
||||
* @since 2.1.2
|
||||
* @var bool
|
||||
*/
|
||||
protected $global_cache = true;
|
||||
|
||||
/**
|
||||
* Sets up the customer query, based on the query vars passed.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @param string|array $query Array of query arguments.
|
||||
*/
|
||||
public function __construct($query = array()) {
|
||||
|
||||
parent::__construct($query);
|
||||
|
||||
} // end __construct;
|
||||
|
||||
} // end class Product_Query;
|
65
inc/database/products/class-product-type.php
Normal file
65
inc/database/products/class-product-type.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Product Types enum.
|
||||
*
|
||||
* @package WP_Ultimo
|
||||
* @subpackage WP_Ultimo\Database\Products
|
||||
* @since 2.0.0
|
||||
*/
|
||||
|
||||
namespace WP_Ultimo\Database\Products;
|
||||
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
use \WP_Ultimo\Database\Engine\Enum;
|
||||
|
||||
/**
|
||||
* Product Types.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class Product_Type extends Enum {
|
||||
|
||||
/**
|
||||
* Default product type.
|
||||
*/
|
||||
const __default = 'plan'; // phpcs:ignore
|
||||
|
||||
const PLAN = 'plan';
|
||||
const PACKAGE = 'package';
|
||||
const SERVICE = 'service';
|
||||
|
||||
/**
|
||||
* Returns an array with values => CSS Classes.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return array
|
||||
*/
|
||||
protected function classes() {
|
||||
|
||||
return array(
|
||||
static::PLAN => 'wu-bg-green-200 wu-text-green-700',
|
||||
static::PACKAGE => 'wu-bg-gray-200 wu-text-blue-700',
|
||||
static::SERVICE => 'wu-bg-yellow-200 wu-text-yellow-700',
|
||||
);
|
||||
|
||||
} // end classes;
|
||||
|
||||
/**
|
||||
* Returns an array with values => labels.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return array
|
||||
*/
|
||||
protected function labels() {
|
||||
|
||||
return array(
|
||||
static::PLAN => __('Plan', 'wp-ultimo'),
|
||||
static::PACKAGE => __('Package', 'wp-ultimo'),
|
||||
static::SERVICE => __('Service', 'wp-ultimo'),
|
||||
);
|
||||
|
||||
} // end labels;
|
||||
|
||||
} // end class Product_Type;
|
82
inc/database/products/class-products-meta-table.php
Normal file
82
inc/database/products/class-products-meta-table.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Class used for querying products' meta data.
|
||||
*
|
||||
* @package WP_Ultimo
|
||||
* @subpackage Database\Products
|
||||
* @since 2.0.0
|
||||
*/
|
||||
|
||||
namespace WP_Ultimo\Database\Products;
|
||||
|
||||
use WP_Ultimo\Database\Engine\Table;
|
||||
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Setup the "wu_productmeta" database table
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
final class Products_Meta_Table extends Table {
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'productmeta';
|
||||
|
||||
/**
|
||||
* Is this table global?
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @var boolean
|
||||
*/
|
||||
protected $global = true;
|
||||
|
||||
/**
|
||||
* Table current version
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @var string
|
||||
*/
|
||||
protected $version = '2.0.0';
|
||||
|
||||
/**
|
||||
* Products constructor.
|
||||
*
|
||||
* @access public
|
||||
* @since 2.0.0
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
parent::__construct();
|
||||
|
||||
} // end __construct;
|
||||
|
||||
/**
|
||||
* Setup the database schema
|
||||
*
|
||||
* @access protected
|
||||
* @since 2.0.0
|
||||
* @return void
|
||||
*/
|
||||
protected function set_schema() {
|
||||
|
||||
$max_index_length = 191;
|
||||
|
||||
$this->schema = "meta_id bigint(20) unsigned NOT NULL auto_increment,
|
||||
wu_product_id bigint(20) unsigned NOT NULL default '0',
|
||||
meta_key varchar(255) DEFAULT NULL,
|
||||
meta_value longtext DEFAULT NULL,
|
||||
PRIMARY KEY (meta_id),
|
||||
KEY wu_product_id (wu_product_id),
|
||||
KEY meta_key (meta_key({$max_index_length}))";
|
||||
|
||||
} // end set_schema;
|
||||
|
||||
} // end class Products_Meta_Table;
|
221
inc/database/products/class-products-schema.php
Normal file
221
inc/database/products/class-products-schema.php
Normal file
@ -0,0 +1,221 @@
|
||||
<?php
|
||||
/**
|
||||
* Product schema class
|
||||
*
|
||||
* @package WP_Ultimo
|
||||
* @subpackage Database\Products
|
||||
* @since 2.0.0
|
||||
*/
|
||||
|
||||
namespace WP_Ultimo\Database\Products;
|
||||
|
||||
use WP_Ultimo\Database\Engine\Schema;
|
||||
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Products Schema Class.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class Products_Schema extends Schema {
|
||||
|
||||
/**
|
||||
* Array of database column objects
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $columns = array(
|
||||
|
||||
array(
|
||||
'name' => 'id',
|
||||
'type' => 'bigint',
|
||||
'length' => '20',
|
||||
'unsigned' => true,
|
||||
'extra' => 'auto_increment',
|
||||
'primary' => true,
|
||||
'sortable' => true
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'slug',
|
||||
'type' => 'varchar',
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'parent_id',
|
||||
'type' => 'bigint',
|
||||
'length' => '20',
|
||||
'unsigned' => true,
|
||||
'sortable' => true,
|
||||
'transition' => true,
|
||||
'allow_null' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'migrated_from_id',
|
||||
'type' => 'bigint',
|
||||
'length' => '20',
|
||||
'unsigned' => true,
|
||||
'sortable' => true,
|
||||
'allow_null' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'name',
|
||||
'type' => 'varchar',
|
||||
'searchable' => true,
|
||||
'sortable' => true
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'description',
|
||||
'type' => 'longtext',
|
||||
'default' => '',
|
||||
'searchable' => true
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'product_group',
|
||||
'type' => 'varchar',
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
'allow_null' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'currency',
|
||||
'type' => 'varchar',
|
||||
'length' => '10',
|
||||
'default' => 'USD',
|
||||
'sortable' => true
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'pricing_type',
|
||||
'type' => 'varchar',
|
||||
'length' => '10',
|
||||
'default' => 'paid',
|
||||
'sortable' => true
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'amount',
|
||||
'type' => 'decimal(13,4)',
|
||||
'default' => '',
|
||||
'sortable' => true,
|
||||
'transition' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'setup_fee',
|
||||
'type' => 'decimal(13,4)',
|
||||
'default' => '',
|
||||
'sortable' => true,
|
||||
'transition' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'recurring',
|
||||
'type' => 'tinyint',
|
||||
'length' => '4',
|
||||
'unsigned' => true,
|
||||
'default' => 1,
|
||||
'transition' => true,
|
||||
'sortable' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'trial_duration',
|
||||
'type' => 'smallint',
|
||||
'unsigned' => true,
|
||||
'default' => '0',
|
||||
'sortable' => true,
|
||||
'transition' => true
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'trial_duration_unit',
|
||||
'type' => 'enum(\'day\', \'month\', \'week\', \'year\')',
|
||||
'default' => 'none',
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'duration',
|
||||
'type' => 'smallint',
|
||||
'unsigned' => true,
|
||||
'default' => '0',
|
||||
'sortable' => true,
|
||||
'transition' => true
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'duration_unit',
|
||||
'type' => 'enum(\'day\', \'month\', \'week\', \'year\')',
|
||||
'default' => 'none',
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'billing_cycles',
|
||||
'type' => 'smallint',
|
||||
'unsigned' => true,
|
||||
'default' => '0',
|
||||
'sortable' => true,
|
||||
'transition' => true
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'list_order',
|
||||
'type' => 'tinyint',
|
||||
'length' => '4',
|
||||
'unsigned' => true,
|
||||
'default' => 10,
|
||||
'transition' => true,
|
||||
'sortable' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'active',
|
||||
'type' => 'tinyint',
|
||||
'length' => '4',
|
||||
'unsigned' => true,
|
||||
'default' => 1,
|
||||
'transition' => true,
|
||||
'sortable' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'date_created',
|
||||
'type' => 'datetime',
|
||||
'default' => null,
|
||||
'created' => true,
|
||||
'date_query' => true,
|
||||
'sortable' => true,
|
||||
'allow_null' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'date_modified',
|
||||
'type' => 'datetime',
|
||||
'default' => null,
|
||||
'modified' => true,
|
||||
'date_query' => true,
|
||||
'sortable' => true,
|
||||
'allow_null' => true,
|
||||
),
|
||||
|
||||
array(
|
||||
'name' => 'type',
|
||||
'type' => 'varchar',
|
||||
'searchable' => true,
|
||||
'sortable' => true
|
||||
),
|
||||
|
||||
);
|
||||
|
||||
} // end class Products_Schema;
|
197
inc/database/products/class-products-table.php
Normal file
197
inc/database/products/class-products-table.php
Normal file
@ -0,0 +1,197 @@
|
||||
<?php
|
||||
/**
|
||||
* Class used for querying products.
|
||||
*
|
||||
* @package WP_Ultimo
|
||||
* @subpackage Database\Products
|
||||
* @since 2.0.0
|
||||
*/
|
||||
|
||||
namespace WP_Ultimo\Database\Products;
|
||||
|
||||
use WP_Ultimo\Database\Engine\Table;
|
||||
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Setup the "wu_product" database table
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
final class Products_Table extends Table {
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'products';
|
||||
|
||||
/**
|
||||
* Is this table global?
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @var boolean
|
||||
*/
|
||||
protected $global = true;
|
||||
|
||||
/**
|
||||
* Table current version
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @var string
|
||||
*/
|
||||
protected $version = '2.0.1-revision.20230601';
|
||||
|
||||
/**
|
||||
* List of table upgrades.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $upgrades = array(
|
||||
'2.0.1-revision.20210419' => 20_210_419,
|
||||
'2.0.1-revision.20210607' => 20_210_607,
|
||||
'2.0.1-revision.20230601' => 20_230_601,
|
||||
);
|
||||
|
||||
/**
|
||||
* Products constructor.
|
||||
*
|
||||
* @access public
|
||||
* @since 2.0.0
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
parent::__construct();
|
||||
|
||||
} // end __construct;
|
||||
|
||||
/**
|
||||
* Setup the database schema
|
||||
*
|
||||
* @access protected
|
||||
* @since 2.0.0
|
||||
* @return void
|
||||
*/
|
||||
protected function set_schema() {
|
||||
|
||||
$this->schema = "id bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
name tinytext NOT NULL DEFAULT '',
|
||||
slug tinytext NOT NULL DEFAULT '',
|
||||
parent_id bigint(20),
|
||||
migrated_from_id bigint(20) DEFAULT NULL,
|
||||
description longtext NOT NULL default '',
|
||||
product_group varchar(20) DEFAULT '',
|
||||
currency varchar(10) NOT NULL DEFAULT 'USD',
|
||||
pricing_type varchar(10) NOT NULL DEFAULT 'paid',
|
||||
amount decimal(13,4) default 0,
|
||||
setup_fee decimal(13,4) default 0,
|
||||
recurring tinyint(4) default 1,
|
||||
trial_duration smallint default 0,
|
||||
trial_duration_unit enum('day', 'week', 'month', 'year'),
|
||||
duration smallint default 0,
|
||||
duration_unit enum('day', 'week', 'month', 'year'),
|
||||
billing_cycles smallint default 0,
|
||||
list_order tinyint default 10,
|
||||
active tinyint(4) default 1,
|
||||
type tinytext NOT NULL DEFAULT '',
|
||||
date_created datetime NULL,
|
||||
date_modified datetime NULL,
|
||||
PRIMARY KEY (id)";
|
||||
|
||||
} // end set_schema;
|
||||
|
||||
/**
|
||||
* Adds the product_group column.
|
||||
*
|
||||
* This does not work on older versions of MySQL, so we needed
|
||||
* the other migration below.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return bool
|
||||
*/
|
||||
protected function __20210419() { // phpcs:ignore
|
||||
|
||||
$result = $this->column_exists('product_group');
|
||||
|
||||
// Maybe add column
|
||||
if (empty($result)) {
|
||||
|
||||
$query = "ALTER TABLE {$this->table_name} ADD COLUMN `product_group` varchar(20) default '' AFTER `description`;";
|
||||
|
||||
$result = $this->get_db()->query($query);
|
||||
|
||||
} // end if;
|
||||
|
||||
// Return success/fail
|
||||
return $this->is_success($result);
|
||||
|
||||
} // end __20210419;
|
||||
|
||||
/**
|
||||
* Adds the product_group column.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @return bool
|
||||
*/
|
||||
protected function __20210607() { // phpcs:ignore
|
||||
|
||||
$result = $this->column_exists('product_group');
|
||||
|
||||
// Maybe add column
|
||||
if (empty($result)) {
|
||||
|
||||
$query_set = "SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';";
|
||||
|
||||
$result_set = $this->get_db()->query($query_set);
|
||||
|
||||
if ($this->is_success($result_set) === false) {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
|
||||
$query = "ALTER TABLE {$this->table_name} ADD COLUMN `product_group` varchar(20) default '' AFTER `description`;";
|
||||
|
||||
$result = $this->get_db()->query($query);
|
||||
|
||||
} // end if;
|
||||
|
||||
// Return success/fail
|
||||
return $this->is_success($result);
|
||||
|
||||
} // end __20210607;
|
||||
/**
|
||||
* Fixes the datetime columns to accept null.
|
||||
*
|
||||
* @since 2.1.2
|
||||
*/
|
||||
protected function __20230601(): bool {
|
||||
|
||||
$null_columns = array(
|
||||
'date_created',
|
||||
'date_modified',
|
||||
);
|
||||
|
||||
foreach ($null_columns as $column) {
|
||||
|
||||
$query = "ALTER TABLE {$this->table_name} MODIFY COLUMN `{$column}` datetime DEFAULT NULL;";
|
||||
|
||||
$result = $this->get_db()->query($query);
|
||||
|
||||
if (!$this->is_success($result)) {
|
||||
|
||||
return false;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
|
||||
return true;
|
||||
|
||||
} // end __20230601;
|
||||
|
||||
} // end class Products_Table;
|
Reference in New Issue
Block a user