version 4.13.0

This commit is contained in:
2021-12-07 11:08:05 +00:00
commit cb26d2c0c4
1285 changed files with 254735 additions and 0 deletions

View File

@ -0,0 +1,221 @@
<?php
abstract class ET_Core_Post_Object {
/**
* @var ET_Core_Data_Utils
*/
protected static $_;
/**
* Current instances of this class organized by type.
*
* @since 3.0.99
* @var array[] {
*
* @type ET_Core_Post_Object[] $type {
*
* @type ET_Core_Post_Object $name Instance.
* ...
* }
* ...
* }
*/
private static $_instances = array();
/**
* The `$args` array used when registering this post object.
*
* @since 3.0.99
* @var array
*/
protected $_args;
/**
* The owner of this instance. Default 'core'. Accepts 'divi', 'builder', 'epanel', 'bloom', 'monarch'.
*
* @since 3.0.99
* @var string
*/
protected $_owner = 'core';
/**
* Whether or not the object has been registered.
*
* @since 3.0.99
* @var bool
*/
protected $_registered = false;
/**
* The WP object for this instance.
*
* @since 3.0.99
* @var WP_Post_Type|WP_Taxonomy
*/
protected $_wp_object;
/**
* Post object key.
*
* @since 3.0.99
* @var string
*/
public $name;
/**
* Post object type. Accepts 'cpt', 'taxonomy'.
*
* @since 3.0.99
* @var string
*/
public $wp_type;
/**
* ET_Core_Post_Base constructor.
*/
public function __construct() {
$this->_args = $this->_get_args();
$this->_args['labels'] = $this->_get_labels();
$this->_apply_filters();
$this->_sanity_check();
if ( empty( self::$_instances ) ) {
self::$_instances['cpt'] = array();
self::$_instances['taxonomy'] = array();
add_action( 'init', 'ET_Core_Post_Object::register_all' );
}
self::$_instances[ $this->wp_type ][ $this->name ] = $this;
}
/**
* Applies filters to the instance's filterable properties.
*/
protected function _apply_filters() {
$name = $this->name;
if ( 'cpt' === $this->wp_type ) {
/**
* Filters the `$args` for a custom post type. The dynamic portion of the
* filter, `$name`, refers to the name/key of the post type being registered.
*
* @since 3.0.99
*
* @param array $args {@see register_post_type()}
*/
$this->_args = apply_filters( "et_core_cpt_{$name}_args", $this->_args );
} else if ( 'taxonomy' === $this->wp_type ) {
/**
* Filters the `$args` for a custom post taxonomy. The dynamic portion of the
* filter, `$name`, refers to the name/key of the taxonomy being registered.
*
* @since 3.0.99
*
* @param array $args {@see register_taxonomy()}
*/
$this->_args = apply_filters( "et_core_taxonomy_{$name}_args", $this->_args );
}
}
/**
* This method is called right before registering the object. It is intended to be
* overridden by child classes as needed.
*/
protected function _before_register() {}
/**
* Returns the args for the instance.
* See {@see register_post_type()} or {@see register_taxonomy()}.
*
* @return array $args
*/
abstract protected function _get_args();
/**
* Returns labels for the instance.
* See {@see register_post_type()} or {@see register_taxonomy()}.
*
* @return array $labels
*/
abstract protected function _get_labels();
/**
* Checks for required properties and existing instances.
*/
protected function _sanity_check() {
if ( ! $this->_args || ! $this->name || ! $this->wp_type ) {
et_error( 'Missing required properties!' );
wp_die();
} else if ( isset( self::$_instances[ $this->wp_type ][ $this->name ] ) ) {
et_error( 'Multiple instances are not allowed!' );
wp_die();
}
}
/**
* Get a derived class instance.
*
* @since 3.0.99
*
* @param string $type See {@see self::$wp_type} for accepted values. Default is 'cpt'.
* @param string $name The name/slug of the derived object. Default is an empty string.
*
* @return self|null
*/
public static function instance( $type = 'cpt', $name = '' ) {
if ( ! self::$_ ) {
self::$_ = ET_Core_Data_Utils::instance();
}
return self::$_->array_get( self::$_instances, "{$type}.{$name}", null );
}
/**
* Calls either {@see register_post_type} or {@see register_taxonomy} for each instance.
*
* @since 3.0.99
*
* @param string $owner Optional. Only register objects owned by a part of the codebase.
* See {@see self::_owner} for accepted values.
*/
public static function register_all( $owner = null ) {
if ( empty( self::$_instances ) ) {
return;
}
global $wp_taxonomies;
foreach ( self::$_instances['taxonomy'] as $name => $instance ) {
$can_register = is_null( $owner ) || $owner === $instance->_owner;
if ( $instance->_registered || ! $can_register ) {
continue;
}
$instance->_before_register();
register_taxonomy( $name, $instance->post_types, $instance->_args );
$instance->_wp_object = $wp_taxonomies[ $name ];
$instance->_registered = true;
}
foreach ( self::$_instances['cpt'] as $name => $instance ) {
$can_register = is_null( $owner ) || $owner === $instance->_owner;
if ( $instance->_registered || ! $can_register ) {
continue;
}
$instance->_before_register();
$instance->_wp_object = register_post_type( $name, $instance->_args );
$instance->_registered = true;
}
}
}

View File

@ -0,0 +1,326 @@
<?php
class ET_Core_Post_Query {
/**
* @var ET_Core_Data_Utils
*/
protected static $_;
/**
* Whether or not to negate the next query arg that is set. Default 'false'.
*
* @since 3.0.99
* @var bool
*/
protected $_negate = false;
/**
* The query result.
*
* @since 3.0.99
* @var WP_Post|WP_Post[]
*/
protected $_query_result;
/**
* The args that will be passed to {@see WP_Query} the next time {@see self::run()} is called.
*
* @since 3.0.99
* @var array
*/
protected $_wp_query_args;
/**
* The name of the primary category-style taxonomy for this post type.
*
* @since 3.0.99
* @var string
*/
public $category_tax;
/**
* The post type (slug) for this instance.
*
* @since 3.0.99
* @var string
*/
public $post_type;
/**
* The name of the primary tag-style taxonomy for this post type.
*
* @since 3.0.99
* @var string
*/
public $tag_tax;
/**
* ET_Core_Post_Query constructor.
*
* @since 3.0.99
*
* @param string $post_type See {@see self::$post_type}
* @param string $category_tax See {@see self::$category_tax}
* @param string $tag_tax See {@see self::$tag_tax}
*/
public function __construct( $post_type = '', $category_tax = '', $tag_tax = '' ) {
$this->post_type = $this->post_type ? $this->post_type : $post_type;
$this->category_tax = $this->category_tax ? $this->category_tax : $category_tax;
$this->tag_tax = $this->tag_tax ? $this->tag_tax : $tag_tax;
$this->_wp_query_args = array(
'post_type' => $this->post_type,
'posts_per_page' => -1,
);
if ( ! self::$_ ) {
self::$_ = ET_Core_Data_Utils::instance();
}
}
/**
* Adds a meta query to the WP Query args for this instance.
*
* @since 3.0.99
*
* @param string $key The meta key.
* @param string $value The meta value.
* @param bool $negate Whether or not to negate this meta query.
*/
protected function _add_meta_query( $key, $value, $negate ) {
if ( ! isset( $this->_wp_query_args['meta_query'] ) ) {
$this->_wp_query_args['meta_query'] = array();
}
if ( is_null( $value ) ) {
$compare = $negate ? 'NOT EXISTS' : 'EXISTS';
} else if ( is_array( $value ) ) {
$compare = $negate ? 'NOT IN' : 'IN';
} else {
$compare = $negate ? '!=' : '=';
}
$query = array(
'key' => $key,
'compare' => $compare,
);
if ( ! is_null( $value ) ) {
$query['value'] = $value;
}
if ( '!=' === $compare ) {
$query = array(
'relation' => 'OR',
array(
'key' => $key,
'compare' => 'NOT EXISTS',
),
$query,
);
}
$this->_wp_query_args['meta_query'][] = $query;
}
/**
* Adds a tax query to the WP Query args for this instance.
*
* @since 3.0.99
*
* @param string $taxonomy The taxonomy name.
* @param array $terms Taxonomy terms.
* @param bool $negate Whether or not to negate this tax query.
*/
protected function _add_tax_query( $taxonomy, $terms, $negate ) {
if ( ! isset( $this->_wp_query_args['tax_query'] ) ) {
$this->_wp_query_args['tax_query'] = array();
}
$operator = $negate ? 'NOT IN' : 'IN';
$field = is_int( $terms[0] ) ? 'term_id' : 'name';
$query = array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => $terms,
'operator' => $operator,
);
if ( $negate ) {
$query = array(
'relation' => 'OR',
array(
'taxonomy' => $taxonomy,
'operator' => 'NOT EXISTS',
),
$query,
);
}
$this->_wp_query_args['tax_query'][] = $query;
}
/**
* Resets {@see self::$_negate} to default then returns the previous value.
*
* @return bool
*/
protected function _reset_negate() {
$negate = $this->_negate;
$this->_negate = false;
return $negate;
}
/**
* Adds a tax query to this instance's WP Query args for it's category taxonomy.
*
* @since 3.0.99
*
* @param mixed ...$categories Variable number of category arguments where each arg can be
* a single category name or ID or an array of names or IDs.
*
* @return $this
*/
public function in_category() {
$negate = $this->_reset_negate();
if ( ! $this->category_tax ) {
et_error( 'A category taxonomy has not been set for this query!' );
return $this;
}
$args = func_get_args();
$args = self::$_->array_flatten( $args );
if ( ! $args ) {
return $this;
}
$this->_add_tax_query( $this->category_tax, $args, $negate );
return $this;
}
/**
* Negates the next query arg that is set.
*
* @since 3.0.99
*
* @return $this
*/
public function not() {
$this->_negate = true;
return $this;
}
/**
* Performs a new WP Query using the instance's current query params and then returns the
* results. Typically, this method is the last method call in a set of chained calls to other
* methods on this class during which various query params are set.
*
* Examples:
*
* $cpt_query
* ->in_category( 'some_cat' )
* ->with_tag( 'some_tag' )
* ->run();
*
* $cpt_query
* ->with_tag( 'some_tag' )
* ->not()->in_category( 'some_cat' )
* ->run();
*
* @since 3.0.99
*
* @param array $args Optional. Additional arguments for {@see WP_Query}.
*
* @return WP_Post|WP_Post[] $posts
*/
public function run( $args = array() ) {
if ( ! is_null( $this->_query_result ) ) {
return $this->_query_result;
}
$name = $this->post_type;
if ( $args ) {
$this->_wp_query_args = array_merge_recursive( $this->_wp_query_args, $args );
}
/**
* Filters the WP Query args for a custom post type query. The dynamic portion of
* the filter name, $name, refers to the name of the custom post type.
*
* @since 3.0.99
*
* @param array $args {@see WP_Query::__construct()}
*/
$this->_wp_query_args = apply_filters( "et_core_cpt_{$name}_query_args", $this->_wp_query_args );
$query = new WP_Query( $this->_wp_query_args );
$this->_query_result = $query->posts;
if ( 1 === count( $this->_query_result ) ) {
$this->_query_result = array_pop( $this->_query_result );
}
return $this->_query_result;
}
/**
* Adds a meta query to this instance's WP Query args.
*
* @since 3.0.99
*
* @param string $key The meta key.
* @param mixed $value Optional. The meta value to compare. When `$value` is not provided,
* the comparison will be 'EXISTS' or 'NOT EXISTS' (when negated).
* When `$value` is an array, comparison will be 'IN' or 'NOT IN'.
* When `$value` is not an array, comparison will be '=' or '!='.
*
* @return $this
*/
public function with_meta( $key, $value = null ) {
$this->_add_meta_query( $key, $value, $this->_reset_negate() );
return $this;
}
/**
* Adds a tax query to this instance's WP Query args for it's primary tag-like taxonomy.
*
* @since 3.0.99
*
* @param mixed ...$tags Variable number of tag arguments where each arg can be
* a single tag name or ID, or an array of tag names or IDs.
*
* @return $this
*/
public function with_tag() {
$negate = $this->_reset_negate();
if ( ! $this->tag_tax ) {
et_error( 'A tag taxonomy has not been set for this query!' );
return $this;
}
$args = func_get_args();
$args = self::$_->array_flatten( $args );
if ( ! $args ) {
return $this;
}
$this->_add_tax_query( $this->tag_tax, $args, $negate );
return $this;
}
}

View File

@ -0,0 +1,95 @@
<?php
abstract class ET_Core_Post_Taxonomy extends ET_Core_Post_Object {
/**
* The `$args` array used when registering this taxonomy.
*
* @since 3.0.99
* @var array
*/
protected $_args;
/**
* The WP Taxonomy object for this instance.
*
* @since 3.0.99
* @var WP_Taxonomy
*/
protected $_wp_object;
/**
* Taxonomy key.
*
* @since 3.0.99
* @var string
*/
public $name;
/**
* The post types to which this taxonomy applies.
*
* @since 3.0.99
* @var array
*/
public $post_types;
/**
* This taxonomy's terms.
*
* @var WP_Term[]
*/
public $terms;
/**
* @inheritDoc
*/
public $wp_type = 'taxonomy';
/**
* ET_Core_Post_Taxonomy constructor.
*/
public function __construct() {
parent::__construct();
$name = $this->name;
/**
* Filters the supported post types for a custom taxonomy. The dynamic portion of the
* filter name, $name, refers to the name of the custom taxonomy.
*
* @since 3.0.99
*
* @param array
*/
$this->post_types = apply_filters( "et_core_taxonomy_{$name}_post_types", $this->post_types );
}
/**
* Get the terms for this taxonomy.
*
* @return array|int|WP_Error|WP_Term[]
*/
public function get() {
if ( is_null( $this->terms ) ) {
$this->terms = get_terms( $this->name, array( 'hide_empty' => false ) );
}
return $this->terms;
}
/**
* Get a derived class instance.
*
* @since 3.0.99
*
* @param string $type See {@see self::$wp_type} for accepted values. Default is 'taxonomy'.
* @param string $name The name/slug of the derived object. Default is an empty string.
*
* @return self|null
*/
public static function instance( $type = 'taxonomy', $name = '' ) {
return parent::instance( $type, $name );
}
}

View File

@ -0,0 +1,57 @@
<?php
abstract class ET_Core_Post_Type extends ET_Core_Post_Object {
/**
* The `$args` array used when registering this post type.
*
* @since 3.0.99
* @var array
*/
protected $_args;
/**
* The name of the primary category-style taxonomy for this post type.
*
* @since 3.0.99
* @var string
*/
protected $_category_tax = '';
/**
* The name of the primary tag-style taxonomy for this post type.
*
* @since 3.0.99
* @var string
*/
protected $_tag_tax = '';
/**
* The WP Post Type object for this instance.
*
* @since 3.0.99
* @var WP_Post_Type
*/
protected $_wp_object;
/**
* Post type key.
*
* @since 3.0.99
* @var string
*/
public $name;
/**
* @inheritDoc
*/
public $wp_type = 'cpt';
/**
* @return ET_Core_Post_Query
*/
public function query() {
return new ET_Core_Post_Query( $this->name, $this->_category_tax, $this->_tag_tax );
}
}