Commit inicial - WordPress Análisis de Precios Unitarios

- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-11-03 21:04:30 -06:00
commit a22573bf0b
24068 changed files with 4993111 additions and 0 deletions

View File

@@ -0,0 +1,132 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay;
use TCB\ConditionalDisplay\Traits\Item;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
abstract class Condition {
use Item;
private $value;
private $operator;
private $um;
private $extra;
public function __construct( $data ) {
$this->prepare_data( $data );
}
public function prepare_data( $data ) {
$this->value = empty( $data['value'] ) ? '' : $data['value'];
if ( ! empty( $data['operator'] ) ) {
$this->operator = $data['operator'];
}
if ( ! empty( $data['extra'] ) ) {
$this->extra = $data['extra'];
}
if ( ! empty( $data['um'] ) ) {
$this->um = $data['um'];
}
}
public function get_value() {
return $this->value;
}
public function get_operator() {
return $this->operator;
}
public function get_um() {
return $this->um;
}
public function get_extra() {
return $this->extra;
}
/**
* @return string
*/
abstract public static function get_key();
abstract public static function get_label();
abstract public function apply( $data );
/**
* Function that should be implemented individually - for situations where the filter has multiple operators, get a list of them
*/
abstract public static function get_operators();
/**
* Allows adding extra operators, for conditions on multiple rows
*
* @return array
*/
public static function get_extra_operators() {
return [];
}
public static function get_control_type() {
return 'input';
}
/**
* This determines whether the select for the operator is shown or not.
*
* @return bool
*/
public static function is_hidden() {
return false;
}
public static function get_display_size() {
return 'small';
}
/**
* @return array
*/
public static function get_data_to_localize() {
$operators = static::get_operators();
return [
'label' => static::get_label(),
'operators' => $operators,
'extra_operators' => static::get_extra_operators(),
'is_hidden' => static::is_hidden(),
'control_type' => static::get_control_type(),
'display_size' => static::get_display_size(),
'validation_data' => static::get_validation_data(),
'is_single_operator' => count( $operators ) === 1,
'has_options_to_preload' => static::has_options_to_preload(),
];
}
/**
* @return array
*/
public static function get_validation_data() {
return [
'min' => 0,
'max' => PHP_INT_MAX,
];
}
public static function has_options_to_preload() {
return false;
}
}

View File

@@ -0,0 +1,130 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay;
use TCB\ConditionalDisplay\Traits\Item;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
abstract class Entity {
use Item;
protected $data;
protected static $fields = [];
/**
* @return array
*/
public static function get_fields() {
$entity_key = static::get_key();
if ( empty( static::$fields[ $entity_key ] ) ) {
foreach ( Field::get( true ) as $field ) {
if ( $field::get_entity() === $entity_key ) {
static::$fields[ $entity_key ][] = $field::get_key();
}
}
}
return static::$fields[ $entity_key ];
}
/**
* @return string
*/
abstract public static function get_key();
/**
* Create an entity from the data provided
*
* @param array $extra_data
*/
public function __construct( $extra_data = [] ) {
$this->data = $this->create_object( $extra_data );
}
/**
* @param $param
*
* @return mixed
*/
abstract public function create_object( $param );
/**
* @param $field_key
*
* @return string
*/
public function get_field_value( $field_key ) {
$value = '';
$field_object = $this->get_field( $field_key );
if ( ! empty( $field_object ) ) {
$value = $field_object->get_value( $this->data );
}
return $value;
}
/**
* @param $field_key
*
* @return Field
*/
public function get_field( $field_key ) {
$field_object = null;
if ( static::validate_field( $field_key ) ) {
$field_object = Field::get_instance( $field_key );
} else {
trigger_error( 'Field ' . $field_key . ' must be an allowed field of ' . static::class );
}
return $field_object;
}
/**
* @param $field
*
* @return bool
*/
public static function validate_field( $field ) {
return in_array( $field, static::get_fields() );
}
/**
* @return array
*/
public static function get_data_to_localize() {
$data = [];
$entities = static::get( true );
foreach ( $entities as $key => $class ) {
$data[ $key ] = [
'label' => $class::get_label(),
];
}
return $data;
}
/**
* Determines the display order in the modal entity select
*
* @return int
*/
public static function get_display_order() {
/* 'User' is 0, other entities are multipliers of 5, everything else that doesn't overwrite this is 100 */
return 100;
}
}

View File

@@ -0,0 +1,111 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay;
use TCB\ConditionalDisplay\Traits\Item;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
abstract class Field {
use Item;
/**
* @return string
*/
abstract public static function get_entity();
abstract public function get_value( $object );
/**
* @return string
*/
abstract public static function get_key();
/**
* @return string
*/
abstract public static function get_label();
/**
* @return array
*/
abstract public static function get_conditions();
/**
* @param array $selected_values
* @param string $search
*
* @return array
*/
public static function get_options( $selected_values = [], $search = '' ) {
return [];
}
public static function filter_options( $id, $label, $selected_values = [], $searched_keyword = '' ) {
return
( empty( $selected_values ) || in_array( $id, $selected_values ) ) && /* if there are pre-selected values, only return the options for those */
( empty( $searched_keyword ) || stripos( $label, $searched_keyword ) !== false ); /* if there is a searched keyword, only return the options that match it */
}
/**
* @return array
*/
public static function get_data_to_localize() {
return [
'label' => static::get_label(),
'is_boolean' => static::is_boolean(),
'autocomplete_placeholder' => static::get_autocomplete_placeholder(), //todo will delete after removing the implementation from the other plugins
'placeholder_text' => static::get_placeholder_text(),
'validation_data' => static::get_validation_data(),
];
}
/**
* Boolean fields can return the condition value directly ( is_logged_in, is_logged_out )
*
* @return bool
*/
public static function is_boolean() {
return false;
}
/**
* @return string
*/
public static function get_autocomplete_placeholder() {
return static::get_placeholder_text();
}
/**
* @return string
*/
public static function get_placeholder_text() {
return '';
}
/**
* @return array
*/
public static function get_validation_data() {
return [
'min' => 0,
'max' => 1000,
];
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 100;
}
}

View File

@@ -0,0 +1,274 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay;
use TCB\ConditionalDisplay\PostTypes\Conditional_Display_Group;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Hooks {
public static function add_filters() {
add_filter( 'tcb_content_allowed_shortcodes', [ __CLASS__, 'allowed_shortcodes' ] );
add_filter( 'tcb_lazy_load_data', [ __CLASS__, 'load_display_groups' ], 10, 3 );
add_filter( 'tve_frontend_options_data', [ __CLASS__, 'tve_frontend_data' ] );
add_filter( 'tve_dash_frontend_ajax_response', [ __CLASS__, 'lazy_load_response' ] );
add_filter( 'tve_update_symbol_html', [ __CLASS__, 'tve_update_symbol_html' ], 10, 2 );
}
public static function add_actions() {
add_action( 'tcb_ajax_save_post', [ __CLASS__, 'save_post' ] );
add_action( 'tcb_ajax_before', [ __CLASS__, 'save_post' ] );
add_action( 'rest_api_init', [ __CLASS__, 'register_routes' ] );
add_action( 'wp_ajax_nopriv_tcb_conditional_display', [ __CLASS__, 'lazy_load_display_groups' ] );
add_action( 'wp_ajax_tcb_conditional_display', [ __CLASS__, 'lazy_load_display_groups' ] );
add_action( 'wp_ajax_dismiss_conditional_tooltip', [ __CLASS__, 'dismiss_conditional_tooltip' ] );
add_action( 'wp_print_footer_scripts', [ __CLASS__, 'wp_print_footer_scripts' ] );
add_action( 'admin_bar_menu', [ __CLASS__, 'admin_bar_menu' ], 1000 );
add_action( 'after_thrive_clone_item', [ __CLASS__, 'after_thrive_clone_item' ], 10, 3 );
}
/**
* On request for lazy loading other products content we add the conditional display data
*
* @param $response
*
* @return mixed
*/
public static function lazy_load_response( $response ) {
if ( Shortcode::is_preview() && isset( $GLOBALS['conditional_display_preview'] ) ) {
$response['lazy_load_conditional_preview'] = array_values( $GLOBALS['conditional_display_preview'] );
}
return $response;
}
/**
* @param $allowed_shortcodes
*
* @return mixed
*/
public static function allowed_shortcodes( $allowed_shortcodes ) {
if ( is_editor_page_raw( true ) ) {
$allowed_shortcodes[] = Shortcode::NAME;
}
return $allowed_shortcodes;
}
/**
* Lazy load localize display groups inside the editor
*
* @param array $data
*
* @return mixed
*/
public static function load_display_groups( $data ) {
if ( isset( $_POST['conditional-display-groups'] ) && is_array( $_POST['conditional-display-groups'] ) ) {
foreach ( $_POST['conditional-display-groups'] as $display_group_key ) {
$display_group = Conditional_Display_Group::get_instance( $display_group_key );
if ( $display_group !== null ) {
$display_group->localize( false, true );
}
}
$data['conditional-display-groups'] = array_values( $GLOBALS['conditional_display_preview'] );
}
return $data;
}
/**
* Load displays to be displayed in frontend
*
* @return void
*/
public static function lazy_load_display_groups() {
$groups = [];
$external_resources = [
'js' => [],
'css' => [],
];
if ( isset( $_GET['query_vars'] ) ) {
tve_set_query_vars_data( $_GET['query_vars'] );
}
if ( is_array( $_GET['groups'] ) ) {
foreach ( $_GET['groups'] as $display_group_key ) {
$display_group = Conditional_Display_Group::get_instance( $display_group_key );
if ( $display_group !== null ) {
foreach ( $display_group->get_displays() as $display ) {
if ( Shortcode::verify_conditions( $display ) ) {
if ( empty( $display['hide'] ) ) {
$content = Shortcode::parse_content( $display['html'], false );
if ( ! empty( $content ) ) {
$groups[ $display_group_key ] = [
'content' => $content,
];
$external_resources = Conditional_Display_Group::get_external_resources_for_content( $external_resources, $content );
}
}
break;
}
}
}
}
}
ob_start();
/* retrieve animations and actions events and maybe lightboxes if needed */
tve_print_footer_events();
$footer_scripts = ob_get_clean();
wp_send_json( [
'groups' => $groups,
'footer_scripts' => $footer_scripts,
'external_resources' => $external_resources,
]
);
}
public static function dismiss_conditional_tooltip() {
$dismissed_tooltips = (array) get_user_meta( wp_get_current_user()->ID, 'tcb_dismissed_tooltips' );
$dismissed_tooltips[] = 'conditional-display-tooltip';
update_user_meta( wp_get_current_user()->ID, 'tcb_dismissed_tooltips', $dismissed_tooltips );
}
/**
* Save conditional displays
*/
public static function save_post() {
if ( isset( $_REQUEST['conditional-displays'] ) ) {
Conditional_Display_Group::save_groups( $_REQUEST['conditional-displays'] );
}
}
public static function register_routes() {
RestApi\General_Data::register_routes();
RestApi\Global_Sets::register_routes();
}
public static function wp_print_footer_scripts() {
/**
* Make sure that is printed only on initial load not while plugins do lazy load
*/
if ( Shortcode::is_preview() && ! wp_doing_ajax() ) {
if ( empty( $GLOBALS['conditional_display_preview'] ) ) {
$GLOBALS['conditional_display_preview'] = [];
}
echo \TCB_Utils::wrap_content( "var tcb_condition_sets=JSON.parse('" . addslashes( json_encode( array_values( $GLOBALS['conditional_display_preview'] ) ) ) . "');", 'script', '', '', [ 'type' => 'text/javascript' ] ); // phpcs:ignore
}
}
/**
* Add some data to the frontend localized object
*
* @param $data
*
* @return mixed
*/
public static function tve_frontend_data( $data ) {
$dismissed_tooltips = (array) get_user_meta( wp_get_current_user()->ID, 'tcb_dismissed_tooltips', true );
$data['conditional_display'] = [
'is_tooltip_dismissed' => in_array( 'conditional-display-tooltip', $dismissed_tooltips, true ),
];
return $data;
}
/**
* @param \WP_Admin_Bar $wp_admin_bar
*/
public static function admin_bar_menu( $wp_admin_bar ) {
if ( ! Shortcode::is_preview() ) {
return;
}
wp_enqueue_style( 'tve-logged-in-style', tve_editor_css( 'logged-in.css' ), false, TVE_VERSION );
$wp_admin_bar->add_node( [
'id' => 'tve-preview-conditions',
'title' => '<span class="tve-preview-conditions-icon admin-bar"></span>',
'parent' => 'top-secondary',
'meta' => [ 'class' => 'tcb-preview-hidden' ],
] );
$wp_admin_bar->add_node( [
'id' => 'tve-conditions-title',
'title' => '<span class="tve-preview-conditions-icon"></span>' .
'<span class="tve-preview-conditions-title">Preview conditions</span>' .
'<div class="tve-preview-conditions-info">
<div class="tve-preview-conditions-tooltip">
This page contains conditional displays on some content . You can preview how the page looks for users that match different conditions by selecting them below.
<a class="tve-preview-conditions-tooltip-link" target="_blank" href="https://help.thrivethemes.com/en/articles/5814058-how-to-use-the-conditional-display-option">Learn more </a>
</div>
</div> ' .
'<button class="tve-preview-conditions-close"></button> ',
'parent' => 'tve-preview-conditions',
] );
$wp_admin_bar->add_node( [
'id' => 'tve-conditions-tooltip',
'title' => '<div class="tve-preview-conditions-tooltip-text">This page contains conditional displays.</div><div class="tve-preview-conditions-tooltip-text">Click here to change your preview settings.</div> ',
'parent' => 'tve-preview-conditions',
] );
}
/**
* Replace conditional display groups inside the cloned item
*
* @param int $new_post_id
* @param int $original_post_id
* @param array $css_id_map
*
* @return void
*/
public static function after_thrive_clone_item( $new_post_id, $original_post_id, $css_id_map ) {
$content = tve_get_post_meta( $new_post_id, 'tve_updated_post' );
$cloned_result = Conditional_Display_Group::clone_conditional_groups_in_content( $content, $css_id_map );
$content = $cloned_result['content'];
tve_update_post_meta( $new_post_id, 'tve_updated_post', $content );
}
/**
* Conditionals ids for duplicated symbols needs to be regenerated
*
* @param $meta_key
* @param $meta_value
*/
public static function tve_update_symbol_html( $meta_key, $meta_value ) {
/* For the Conditional Display we need to regenerate the ids of the added conditions */
if ( $meta_key === 'tve_updated_post' ) {
$cloned_result = Conditional_Display_Group::clone_conditional_groups_in_content( $meta_value );
$meta_value = $cloned_result['content'];
}
return $meta_value;
}
}

View File

@@ -0,0 +1,118 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay;
use TCB\ConditionalDisplay\PostTypes\Conditional_Display_Group;
use TCB\ConditionalDisplay\PostTypes\Global_Conditional_Set;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Main {
const ABSTRACT_PATH = __DIR__ . '/abstract';
const TRAIT_PATH = __DIR__ . '/traits';
public static $entities = [];
public static function init() {
static::includes();
Conditional_Display_Group::register();
Global_Conditional_Set::register();
Hooks::add_filters();
Hooks::add_actions();
Shortcode::add();
do_action( 'tcb_conditional_display_register' );
}
public static function includes() {
require_once __DIR__ . '/post-types/class-conditional-display-group.php';
require_once __DIR__ . '/post-types/class-global-conditional-set.php';
require_once __DIR__ . '/class-shortcode.php';
require_once __DIR__ . '/class-hooks.php';
require_once __DIR__ . '/rest-api/class-general-data.php';
require_once __DIR__ . '/rest-api/class-global-sets.php';
require_once static::TRAIT_PATH . '/class-item.php';
require_once static::ABSTRACT_PATH . '/class-entity.php';
require_once static::ABSTRACT_PATH . '/class-field.php';
require_once static::ABSTRACT_PATH . '/class-condition.php';
require_once __DIR__ . '/tve-conditional-display-global.php';
static::load_files_from_path( 'entities', Entity::class );
foreach ( [ 'user', 'time', 'request', 'referral' ] as $field_group ) {
static::load_files_from_path( 'fields/' . $field_group, Field::class );
}
static::load_files_from_path( 'conditions', Condition::class );
}
/**
* @param $path
* @param $type_class
*/
public static function load_files_from_path( $path, $type_class ) {
$full_path = __DIR__ . '/' . $path;
/* convert path to namespace: '/fields/user' -> '\\Fields\\User' */
$namespace = implode( '/', array_map( 'ucfirst', explode( '/', $path ) ) );
$namespace = str_replace( '/', '\\', $namespace );
static::include_classes_from_folder_recursive( $full_path, $namespace, $type_class );
}
/**
* @param $path
* @param $namespace
* @param $type_class
*/
public static function include_classes_from_folder_recursive( $path, $namespace, $type_class ) {
$items = array_diff( scandir( $path ), [ '.', '..' ] );
foreach ( $items as $item ) {
$item_path = $path . '/' . $item;
/* if the item is a folder, enter it and do recursion */
if ( is_dir( $item_path ) ) {
static::include_classes_from_folder_recursive( $item_path, $namespace, $type_class );
}
/* if the item is a file, include it */
if ( is_file( $item_path ) && substr( $item_path, - 3 ) === 'php' ) {
require_once $item_path;
/* for each file, dynamically call the init function of the class */
if ( preg_match( '/class-(.*).php/m', $item, $m ) && ! empty( $m[1] ) ) {
$class_name = \TCB_ELEMENTS::capitalize_class_name( $m[1] );
$class = __NAMESPACE__ . '\\' . $namespace . '\\' . $class_name;
$type_class::register( $class );
}
}
}
}
/**
* Get all the data that we want to localize for Conditional Display - the rest is fetched on demand through REST
*
* @return array
*/
public static function get_localized_data() {
return [
'entities' => Entity::get_data_to_localize(),
];
}
}

View File

@@ -0,0 +1,228 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay;
use TCB\ConditionalDisplay\PostTypes\Conditional_Display_Group;
use TCB\ConditionalDisplay\PostTypes\Global_Conditional_Set;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Shortcode {
const NAME = 'tve_conditional_display';
public static function add() {
add_shortcode( static::NAME, [ __CLASS__, 'render' ] );
}
/**
* @param $attr
*
* @return string
*/
public static function render( $attr ) {
$group_key = empty( $attr['group'] ) ? null : $attr['group'];
$is_editor_page = is_editor_page_raw() || isset( $_REQUEST['tar_editor_page'] );
/* check for admin just so we won't show displays to unwanted users */
$during_optimization = \TCB\Lightspeed\Main::is_optimizing() && current_user_can( 'manage_options' );
$display_group = Conditional_Display_Group::get_instance( $group_key );
$content = '';
$all_displays = '';
if ( $display_group !== null ) {
/* We should not localize the groups during optimization or during post list / symbol render */
if ( ! $during_optimization && static::should_localize() ) {
/* Basically localize the data related to conditional display */
$display_group->localize( static::is_preview(), $is_editor_page );
}
if ( ! $during_optimization && ( ! ( $is_editor_page || wp_doing_ajax() ) && $display_group->has_lazy_load() ) ) {
$content = $display_group->lazy_load_placeholder();
} else {
foreach ( $display_group->get_displays( true, $is_editor_page ) as $display ) {
if ( $during_optimization || static::verify_conditions( $display ) ) {
$content = empty( $display['hide'] ) || $is_editor_page ? $display['html'] : '';
/* If we are in preview, and we have a hide content we add a placeholder */
if ( empty( $content ) && static::is_preview() ) {
$content = \TCB_Utils::wrap_content( '', 'span', '', '', [ 'data-display-group' => $group_key ] );
}
$content = static::parse_content( $content, $is_editor_page );
if ( $during_optimization ) {
$all_displays .= $content;
} else {
break;
}
}
}
}
}
return $during_optimization ? $all_displays : $content;
}
/**
* There are cases when we do not want to localize the displays, such as post list render or symbol render during lazy load.
* @return bool
*/
public static function should_localize() {
$should_localize = \TCB_Post_List::is_outside_post_list_render();
if ( wp_doing_ajax() ) {
$should_localize = $should_localize && \TCB_Symbol_Template::is_outside_symbol_render();
}
return $should_localize;
}
/**
* Parse shortcode content - do shortcode, read events, and so on
*
* @param $content
* @param $is_editor_page
*
* @return string
*/
public static function parse_content( $content, $is_editor_page = true ) {
$content = tve_do_wp_shortcodes( $content, $is_editor_page );
$content = tve_thrive_shortcodes( $content, $is_editor_page );
$content = do_shortcode( $content );
if ( ! $is_editor_page ) {
$content = tve_restore_script_tags( $content );
tve_parse_events( $content );
$global_styles = tve_get_shared_styles( $content, '', false, false );
$content = $global_styles . $content;
}
return $content;
}
/**
* Verify conditions for a specific display
*
* @param $display
*
* @return bool
*/
public static function verify_conditions( $display ) {
$is_verified = $display['key'] === 'default';
if ( ! $is_verified && ! empty( $display['conditions'] ) ) {
$conditions = static::parse_condition_config( $display['conditions'] );
/**
* condition = set1 || set2 || set3
* set1 = rule1 && rule2 && rule3
*/
foreach ( $conditions as $set ) {
if ( static::verify_set( $set ) ) {
$is_verified = true;
/* if we have a verified set, we can stop checking the other sets */
break;
}
}
}
return $is_verified;
}
/**
* replace single quotes with double quotes and json_decode
*
* @param $conditions
*
* @return mixed
*/
public static function parse_condition_config( $conditions ) {
$conditions = str_replace( "'", '"', html_entity_decode( $conditions, ENT_QUOTES ) );
return json_decode( $conditions, true );
}
public static function is_preview() {
$is_editor = is_editor_page_raw();
$is_ajax = wp_doing_ajax();
$ajax_action = empty( $GLOBALS['tve_dash_frontend_ajax_load'] ) ? false : $GLOBALS['tve_dash_frontend_ajax_load'];
return current_user_can( 'edit_posts' ) && ! $is_editor && ( ! $is_ajax || ( $is_ajax && $ajax_action ) );
}
/**
* Verify rules from a set
*
* @param $set
*
* @return bool
*/
public static function verify_set( $set ) {
if ( empty( $set['ID'] ) ) {
$rules = $set['rules'];
} else {
$global_set = Global_Conditional_Set::get_instance( $set['ID'] );
$rules = $global_set === null || empty( $global_set->get_post() ) ? [] : $global_set->get_rules();
}
foreach ( $rules as $rule ) {
$is_rule_verified = static::verify_rule( $rule );
$is_set_verified = isset( $is_set_verified ) ? $is_set_verified && $is_rule_verified : $is_rule_verified;
}
return isset( $is_set_verified ) ? $is_set_verified : false;
}
/**
* Check if the rule is valid or not
*
* @param $config
*
* @return boolean
*/
public static function verify_rule( $config ) {
$is_verified = false;
if ( ! empty( $config['entity'] ) && ! empty( $config['field'] ) ) {
$entity = Entity::get_instance( $config['entity'] );
if ( ! empty( $entity ) ) {
$field_value = $entity->get_field_value( $config['field'] );
if ( empty( $config['condition']['key'] ) ) {
$field = Field::get_instance( $config['field'] );
if ( $field::is_boolean() ) {
$is_verified = $field_value;
}
} else {
$condition = Condition::get_instance( $config['condition']['key'], $config['condition'] );
$is_verified = $condition->apply( [ 'field_value' => $field_value ] );
}
}
}
return $is_verified;
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
if ( ! class_exists( 'Autocomplete', false ) ) {
require_once __DIR__ . '/class-autocomplete.php';
}
/**
* This is used by Product_Access in Apprentice
* Because the field name there is called 'Has access to Apprentice Product(s)', it doesn't make sense to have the condition visible, so we hide it.
*/
class Autocomplete_Hidden extends Autocomplete {
/**
* @return string
*/
public static function get_key() {
return 'autocomplete_hidden';
}
public function apply( $data ) {
$field_values = $data['field_value'];
$haystack = $this->get_value();
if ( is_array( $field_values ) ) {
$result = ! empty( array_intersect( $field_values, $haystack ) );
} else {
$result = in_array( $field_values, $haystack, true );
}
return $result;
}
public static function get_operators() {
return [
'autocomplete' => [
'label' => 'is',
],
];
}
public static function is_hidden() {
return true;
}
public static function get_control_type() {
return Autocomplete::get_key(); /* use the 'autocomplete' control because the functionality is the same */
}
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Autocomplete extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'autocomplete';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'Autocomplete', 'thrive-cb' );
}
public static function get_display_size() {
return 'full';
}
public function apply( $data ) {
$result = false;
$compared_values = $this->get_value();
if ( ! empty( $compared_values ) ) {
$field_values = $data['field_value'];
if ( is_array( $field_values ) ) {
$result = ! empty( array_intersect( $field_values, $compared_values ) );
} else {
$result = in_array( $field_values, $compared_values );
}
}
return $this->get_operator() === 'autocomplete' ? $result : ! $result;
}
public static function get_operators() {
return [
'autocomplete' => [
'label' => 'is',
],
'autocomplete_exclude' => [
'label' => 'is not',
],
];
}
public static function get_control_type() {
return static::get_key();
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Checkbox extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'checkbox';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'Checkbox', 'thrive-cb' );
}
public function apply( $data ) {
$field_values = $data['field_value'];
$haystack = $this->get_value();
if ( is_array( $field_values ) ) {
$result = ! empty( array_intersect( $field_values, $haystack ) );
} else {
$result = in_array( $field_values, $haystack );
}
return $this->get_operator() === 'contains' ? $result : ! $result;
}
public static function get_operators() {
return [
'contains' => [
'label' => esc_html__( 'is', 'thrive-cb' ),
],
'not_contains' => [
'label' => esc_html__( 'is not', 'thrive-cb' ),
],
];
}
public static function get_control_type() {
return static::get_key();
}
public static function get_display_size() {
return 'full';
}
public static function has_options_to_preload() {
return true;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Date_And_Time_Picker extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'date_and_time';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'Date and time comparison', 'thrive-cb' );
}
public function apply( $data ) {
$formatted_field_value = strtotime( $data['field_value'] );
$formatted_compared_value = strtotime( $this->get_value() );
switch ( $this->get_operator() ) {
case 'before':
$result = $formatted_field_value < $formatted_compared_value;
break;
case 'after':
$result = $formatted_field_value > $formatted_compared_value;
break;
case 'equals':
$result = strtotime( date( 'Y/m/d', $formatted_field_value ) ) === strtotime( date( 'Y/m/d', $formatted_compared_value ) );
break;
default:
$result = false;
}
return $result;
}
public static function get_operators() {
return [
'equals' => [
'label' => 'equals',
],
'before' => [
'label' => 'before',
],
'after' => [
'label' => 'after',
],
];
}
public static function get_control_type() {
return 'date-and-time';
}
public static function get_display_size() {
return 'medium';
}
/**
* @return array
*/
public static function get_validation_data() {
return [
'min_minutes' => 0,
'max_minutes' => 59,
'min_hours' => 0,
'max_hours' => 23,
];
}
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Date_And_Time_With_Intervals extends Date_And_Time_Picker {
/**
* @return string
*/
public static function get_key() {
return 'date_and_time_with_intervals';
}
public function apply( $data ) {
$result = false;
if ( ! empty( $data['field_value'] ) ) {
$now = current_time( 'timestamp' );
switch ( $this->get_operator() ) {
case 'more':
$result = $now > strtotime( $data['field_value'] ) + $this->get_added_time();
break;
case 'less':
$result = strtotime( $data['field_value'] ) > $now - $this->get_added_time();
break;
default:
$result = parent::apply( $data );
}
}
return $result;
}
public static function get_operators() {
return array_merge(
[
'more' => [
'label' => 'more than',
],
'less' => [
'label' => 'less than',
],
],
parent::get_operators()
);
}
private function get_added_time() {
$um = $this->get_um();
$value = (int) $this->get_value();
switch ( $um ) {
case 'days':
$result = $value * DAY_IN_SECONDS;
break;
case 'hours':
$result = $value * HOUR_IN_SECONDS;
break;
case 'minutes':
$result = $value * MINUTE_IN_SECONDS;
break;
case 'months':
$result = $value * MONTH_IN_SECONDS;
break;
case 'years':
$result = $value * YEAR_IN_SECONDS;
break;
default:
$result = 0;
}
return $result;
}
/**
* @return array
*/
public static function get_validation_data() {
return array_merge( parent::get_validation_data(), [
'min_interval' => 1,
'max_interval' => 1000,
] );
}
}

View File

@@ -0,0 +1,79 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Date extends Date_And_Time_Picker {
/**
* @return string
*/
public static function get_key() {
return 'date';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'Date comparison', 'thrive-cb' );
}
public function apply( $data ) {
$result = false;
$field_value = $data['field_value'];
if ( ! empty( $field_value ) ) {
$formatted_field_value = strtotime( $data['field_value'] );
$formatted_compared_value = strtotime( $this->get_value() );
switch ( $this->get_operator() ) {
case 'equals':
$result = strtotime( date( 'Y/m/d', $formatted_field_value ) )
===
strtotime( date( 'Y/m/d', $formatted_compared_value ) );
break;
case 'between':
/* reduce the format to date-only */
$formatted_start_interval = strtotime( date( 'Y/m/d', $formatted_compared_value ) );
$formatted_end_interval = strtotime( date( 'Y/m/d', strtotime( $this->get_extra() ) ) );
$formatted_field_value = strtotime( date( 'Y/m/d', $formatted_field_value ) );
$result = $formatted_field_value >= $formatted_start_interval &&
$formatted_field_value <= $formatted_end_interval;
break;
default:
$result = parent::apply( $data );
}
}
return $result;
}
public static function get_operators() {
return array_merge( parent::get_operators(), [
'between' => [
'label' => 'between',
],
] );
}
public static function get_control_type() {
return static::get_key();
}
/**
* @return array
*/
public static function get_validation_data() {
return [];
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Dropdown extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'dropdown';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'Dropdown', 'thrive-cb' );
}
public function apply( $data ) {
$field_value = $data['field_value'];
$compared_value = $this->get_value();
if ( is_numeric( $field_value ) ) {
$compared_value = (int) $compared_value;
}
return $field_value === $compared_value;
}
public static function get_operators() {
return [
'equals' => [
'label' => '=',
],
];
}
public static function get_control_type() {
return static::get_key();
}
public static function is_hidden() {
return true;
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Number_Comparison extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'number_comparison';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'Number comparison', 'thrive-cb' );
}
public function apply( $data ) {
$field_number = (float) $data['field_value'];
$compared_number = (float) $this->get_value();
switch ( $this->get_operator() ) {
case 'less_than':
$result = $field_number < $compared_number;
break;
case 'more_than':
$result = $field_number > $compared_number;
break;
case 'equal':
$result = $field_number == $compared_number;
break;
default:
$result = false;
}
return $result;
}
public static function get_operators() {
return [
'less_than' => [
'label' => 'is less than',
],
'more_than' => [
'label' => 'is more than',
],
'equal' => [
'label' => 'is equal to',
],
];
}
public static function get_control_type() {
return 'input-number';
}
public static function get_display_size() {
return 'medium';
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Number_Equals extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'number_equals';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'Number equals', 'thrive-cb' );
}
public function apply( $data ) {
$a = (int) $data['field_value'];
$b = (int) $this->get_value();
return $a === $b;
}
public static function get_operators() {
return [
'equals' => [
'label' => '=',
],
];
}
public static function get_control_type() {
return 'input-number';
}
public static function is_hidden() {
return true;
}
}

View File

@@ -0,0 +1,102 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class String_Comparison extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'string_comparison';
}
public static function get_control_type() {
return 'string-comparison';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'String comparison', 'thrive-cb' );
}
public function apply( $data ) {
$string_data = $this->get_value();
$string_name = $string_data['string_name'];
$string_operator = $string_data['string_operator'];
$string_value = $string_data['string_value'];
$field_string = isset( $data['field_value'] ) && isset( $data['field_value'][ $string_name ] ) ? sanitize_text_field( urldecode( $data['field_value'][ $string_name ] ) ) : '';
switch ( $string_operator ) {
case 'equals':
$result = $field_string === $string_value;
break;
case 'contains':
$result = strpos( $field_string, $string_value ) !== false;
break;
case 'not_contains':
$result = strpos( $field_string, $string_value ) === false;
break;
case 'exists':
$result = ! empty( $field_string );
break;
default:
$result = false;
}
return $result;
}
public static function get_operators() {
/* The operators are added dynamically, but we keep an array with one element so this will be considered a singular condition */
return [
[
'value' => 'equals',
'label' => 'Equals',
],
];
}
public static function get_extra_operators() {
return [
[
'value' => 'equals',
'label' => __( 'Equals' ),
],
[
'value' => 'contains',
'label' => __( 'Contains' ),
],
[
'value' => 'not_contains',
'label' => __( 'Does not Contain' ),
],
[
'value' => 'exists',
'label' => __( 'Exists' ),
],
];
}
public static function get_display_size() {
return 'medium';
}
public static function is_hidden() {
return true;
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class String_Contains extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'string_contains';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'String contains', 'thrive-cb' );
}
public function apply( $data ) {
$haystack = $data['field_value'];
$needle = $this->get_value();
return strpos( $haystack, $needle ) !== false;
}
public static function get_operators() {
return [
'contains' => [
'label' => '=',
],
];
}
public static function is_hidden() {
return true;
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class String_Equals extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'string_equals';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'String equals', 'thrive-cb' );
}
public function apply( $data ) {
$a = $data['field_value'];
$b = $this->get_value();
return strcmp( $a, $b ) === 0;
}
public static function get_operators() {
return [
'equals' => [
'label' => '=',
],
];
}
public static function is_hidden() {
return true;
}
}

View File

@@ -0,0 +1,75 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Time extends Date_And_Time_Picker {
/**
* @return string
*/
public static function get_key() {
return 'time';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'Time comparison', 'thrive-cb' );
}
public function apply( $data ) {
$compared_value = $this->get_value();
$formatted_field_value = strtotime( $data['field_value'] );
$formatted_compared_value = strtotime( $compared_value['hours'] . ':' . $compared_value['minutes'] );
switch ( $this->get_operator() ) {
case 'before':
$result = $formatted_field_value <= $formatted_compared_value;
break;
case 'after':
$result = $formatted_field_value >= $formatted_compared_value;
break;
case 'between':
$formatted_start_interval = $formatted_compared_value;
$formatted_end_interval = strtotime( $this->get_extra()['hours'] . ':' . $this->get_extra()['minutes'] );
$result = $formatted_field_value >= $formatted_start_interval &&
$formatted_field_value <= $formatted_end_interval;
break;
default:
$result = false;
}
return $result;
}
public static function get_control_type() {
return static::get_key();
}
public static function get_operators() {
return [
'before' => [
'label' => 'before',
],
'after' => [
'label' => 'after',
],
'between' => [
'label' => 'between',
],
];
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Conditions;
use TCB\ConditionalDisplay\Condition;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class URL_Comparison extends Condition {
/**
* @return string
*/
public static function get_key() {
return 'url_comparison';
}
/**
* @return string
*/
public static function get_label() {
return esc_html__( 'URL comparison', 'thrive-cb' );
}
public function apply( $data ) {
$field_value = $data['field_value'];
$compared_value = $this->get_value();
if ( strpos( $compared_value, '*' ) === false ) {
$result = strcmp( $field_value, $compared_value ) === 0;
} else {
/* if a wildcard '*' is found, convert it into the regex format then run a preg_match */
$compared_value = str_replace( '\*', '.*', preg_quote( $compared_value, '/' ) );
preg_match_all( '/' . $compared_value . '/m', $field_value, $matches, PREG_SET_ORDER );
$result = ! empty( $matches );
}
return $result;
}
public static function get_operators() {
return [
'equals' => [
'label' => '=',
],
];
}
public static function is_hidden() {
return true;
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Entities;
use TCB\ConditionalDisplay\Entity;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Referral extends Entity {
/**
* @return string
*/
public static function get_key() {
return 'referral_data';
}
public static function get_label() {
return esc_html__( 'Referral', 'thrive-cb' );
}
/**
* The referrer is normally taken from $_SERVER['HTTP_REFERER'].
* When it's not available ( this happens during ajax requests ), it is sent as a parameter from there.
*
* @param $param
*
* @return array|mixed
*/
public function create_object( $param ) {
$referrer_data = [];
if ( wp_doing_ajax() && ! empty( $_REQUEST['referrer'] ) ) {
$url = $_REQUEST['referrer'];
} else if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
$url = $_SERVER['HTTP_REFERER'];
}
if ( ! empty( $url ) ) {
$referrer_data['url'] = $url;
/* if the URL is from this site, store the post data */
if ( strpos( $url, home_url() ) !== false ) {
$referrer_data['post'] = get_post( url_to_postid( $url ) );
}
}
return $referrer_data;
}
/**
* Determines the display order in the modal entity select
*
* @return int
*/
public static function get_display_order() {
return 25;
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Entities;
use TCB\ConditionalDisplay\Entity;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Request extends Entity {
/**
* @return string
*/
public static function get_key() {
return 'request_data';
}
public static function get_label() {
return esc_html__( 'Request data', 'thrive-cb' );
}
public function create_object( $param ) {
return '';
}
/**
* Determines the display order in the modal entity select
*
* @return int
*/
public static function get_display_order() {
return 20;
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Entities;
use TCB\ConditionalDisplay\Entity;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Time extends Entity {
/**
* @return string
*/
public static function get_key() {
return 'time_data';
}
public static function get_label() {
return esc_html__( 'Time and date', 'thrive-cb' );
}
public function create_object( $param ) {
return '';
}
/**
* Determines the display order in the modal entity select
*
* @return int
*/
public static function get_display_order() {
return 15;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Entities;
use TCB\ConditionalDisplay\Entity;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class User extends Entity {
/**
* @return string
*/
public static function get_key() {
return 'user_data';
}
public static function get_label() {
return esc_html__( 'User', 'thrive-cb' );
}
public function create_object( $param ) {
$user_id = get_current_user_id();
return get_userdata( $user_id );
}
/**
* Determines the display order in the modal entity select
*
* @return int
*/
public static function get_display_order() {
return 0;
}
}

View File

@@ -0,0 +1,81 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Referral;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Category extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'referral_data';
}
/**
* @return string
*/
public static function get_key() {
return 'referral_category_id';
}
public static function get_label() {
return esc_html__( 'Category', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'autocomplete' ];
}
public function get_value( $referral_data ) {
$categories = [];
if ( ! empty( $referral_data['post'] ) ) {
foreach ( get_the_category( $referral_data['post'] ) as $category ) {
$categories[] = $category->term_id;
}
}
return $categories;
}
public static function get_options( $selected_values = [], $searched_keyword = '' ) {
$categories = [];
foreach ( get_categories() as $category ) {
if ( static::filter_options( $category->term_id, $category->name, $selected_values, $searched_keyword ) ) {
$categories[] = [
'value' => (string) $category->term_id,
'label' => $category->name,
];
}
}
return $categories;
}
/**
* @return string
*/
public static function get_placeholder_text() {
return esc_html__( 'Search categories', 'thrive-cb' );
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 20;
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Referral;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Domain extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'referral_data';
}
/**
* @return string
*/
public static function get_key() {
return 'referral_domain';
}
public static function get_label() {
return esc_html__( 'Domain', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'string_contains' ];
}
public function get_value( $referral_data ) {
return empty( $referral_data['url'] ) ? '' : parse_url( $referral_data['url'], PHP_URL_HOST );
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 5;
}
/**
* @return string
*/
public static function get_placeholder_text() {
return esc_html__( 'Enter a domain URL', 'thrive-cb' );
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Referral;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
if ( ! class_exists( 'Referral_Post', false ) ) {
require_once __DIR__ . '/class-post.php';
}
class Page extends Post {
/**
* @return string
*/
public static function get_key() {
return 'referral_page_id';
}
public static function get_label() {
return esc_html__( 'Page', 'thrive-cb' );
}
public static function get_post_type() {
return 'page';
}
/**
* @return string
*/
public static function get_placeholder_text() {
return esc_html__( 'Search pages', 'thrive-cb' );
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 15;
}
}

View File

@@ -0,0 +1,92 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Referral;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Post extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'referral_data';
}
/**
* @return string
*/
public static function get_key() {
return 'referral_post_id';
}
public static function get_label() {
return esc_html__( 'Post', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'autocomplete' ];
}
public function get_value( $referral_data ) {
return empty( $referral_data['post'] ) ? '' : $referral_data['post']->ID;
}
public static function get_options( $selected_values = [], $searched_keyword = '' ) {
$query = [
'posts_per_page' => empty( $selected_values ) ? min( 100, max( 20, strlen( $searched_keyword ) * 3 ) ) : - 1,
'post_type' => static::get_post_type(),
'orderby' => 'title',
'order' => 'ASC',
];
if ( ! empty( $searched_keyword ) ) {
$query['s'] = $searched_keyword;
}
if ( ! empty( $selected_values ) ) {
$query['include'] = $selected_values;
}
$posts = [];
foreach ( get_posts( $query ) as $post ) {
if ( static::filter_options( $post->ID, $post->post_title, $selected_values, $searched_keyword ) ) {
$posts[] = [
'value' => (string) $post->ID,
'label' => $post->post_title,
];
}
}
return $posts;
}
public static function get_post_type() {
return 'post';
}
/**
* @return string
*/
public static function get_placeholder_text() {
return esc_html__( 'Search posts', 'thrive-cb' );
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 10;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Referral;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Tag extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'referral_data';
}
/**
* @return string
*/
public static function get_key() {
return 'referral_tag_id';
}
public static function get_label() {
return esc_html__( 'Tag', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'autocomplete' ];
}
public function get_value( $referral_data ) {
$tags = [];
if ( ! empty( $referral_data['post'] ) ) {
$post_tags = get_the_tags( $referral_data['post'] );
if ( ! empty( $post_tags ) ) {
foreach ( $post_tags as $tag ) {
$tags[] = $tag->term_id;
}
}
}
return $tags;
}
public static function get_options( $selected_values = [], $searched_keyword = '' ) {
$tags = [];
foreach ( get_tags() as $tag ) {
if ( static::filter_options( $tag->term_id, $tag->name, $selected_values, $searched_keyword ) ) {
$tags[] = [
'value' => (string) $tag->term_id,
'label' => $tag->name,
];
}
}
return $tags;
}
/**
* @return string
*/
public static function get_placeholder_text() {
return esc_html__( 'Search tags', 'thrive-cb' );
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 25;
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Referral;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Url extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'referral_data';
}
/**
* @return string
*/
public static function get_key() {
return 'referral_url';
}
public static function get_label() {
return esc_html__( 'URL', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'url_comparison' ];
}
public function get_value( $referral_data ) {
return empty( $referral_data['url'] ) ? '' : $referral_data['url'];
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 0;
}
/**
* @return string
*/
public static function get_placeholder_text() {
return esc_html__( 'Enter a URL. Use an asterisk as a wildcard', 'thrive-cb' );
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Request;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Cookie extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'request_data';
}
/**
* @return string
*/
public static function get_key() {
return 'cookie';
}
public static function get_label() {
return esc_html__( 'Cookie', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'string_comparison' ];
}
public function get_value( $request_data ) {
return $_COOKIE;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Request;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Post_Variable extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'request_data';
}
/**
* @return string
*/
public static function get_key() {
return 'post_variable';
}
public static function get_label() {
return esc_html__( 'POST variable ', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'string_comparison' ];
}
public function get_value( $request_data ) {
return wp_doing_ajax() && isset( $_GET['post_variables'] ) ? $_GET['post_variables'] : $_POST;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Request;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Url_Query_String extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'request_data';
}
/**
* @return string
*/
public static function get_key() {
return 'url_query_string';
}
public static function get_label() {
return esc_html__( 'URL Query String', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'string_comparison' ];
}
public function get_value( $request_data ) {
return wp_doing_ajax() && isset( $_GET['query_strings'] ) ? $_GET['query_strings'] : $_GET;
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Time;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Current_Date extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'time_data';
}
/**
* @return string
*/
public static function get_key() {
return 'current_date';
}
public static function get_label() {
return esc_html__( 'Date', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'date' ];
}
public function get_value( $data ) {
return current_time( 'Y/m/d H:i' );
}
/**
* @return int
*/
public static function get_display_order() {
return 0;
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Time;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Current_Time extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'time_data';
}
/**
* @return string
*/
public static function get_key() {
return 'current_time';
}
public static function get_label() {
return esc_html__( 'Time', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'time' ];
}
public function get_value( $data ) {
return current_time( 'H:i' );
}
/**
* @return int
*/
public static function get_display_order() {
return 5;
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Time;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Day_Of_Month extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'time_data';
}
/**
* @return string
*/
public static function get_key() {
return 'day_of_month';
}
public static function get_label() {
return esc_html__( 'Day of month', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'number_equals' ];
}
public function get_value( $data ) {
return (int) date( 'd' );
}
/**
* @return array
*/
public static function get_validation_data() {
return [
'min' => 1,
'max' => 31,
];
}
/**
* @return int
*/
public static function get_display_order() {
return 15;
}
}

View File

@@ -0,0 +1,82 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Time;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Day_Of_Week extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'time_data';
}
/**
* @return string
*/
public static function get_key() {
return 'day_of_week';
}
public static function get_label() {
return esc_html__( 'Day of week', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'checkbox' ];
}
public function get_value( $data ) {
return strtolower( date( 'l' ) );
}
public static function get_options( $selected_values = [], $search = '' ) {
return [
[
'value' => 'monday',
'label' => __( 'Monday' ),
],
[
'value' => 'tuesday',
'label' => __( 'Tuesday' ),
],
[
'value' => 'wednesday',
'label' => __( 'Wednesday' ),
],
[
'value' => 'thursday',
'label' => __( 'Thursday' ),
],
[
'value' => 'friday',
'label' => __( 'Friday' ),
],
[
'value' => 'saturday',
'label' => __( 'Saturday' ),
],
[
'value' => 'sunday',
'label' => __( 'Sunday' ),
],
];
}
/**
* @return int
*/
public static function get_display_order() {
return 10;
}
}

View File

@@ -0,0 +1,102 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\Time;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Month extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'time_data';
}
/**
* @return string
*/
public static function get_key() {
return 'month';
}
public static function get_label() {
return esc_html__( 'Month', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'checkbox' ];
}
public function get_value( $data ) {
return (int) date( 'n' );
}
public static function get_options( $selected_values = [], $search = '' ) {
return [
[
'value' => '1',
'label' => __( 'January' ),
],
[
'value' => '2',
'label' => __( 'February' ),
],
[
'value' => '3',
'label' => __( 'March' ),
],
[
'value' => '4',
'label' => __( 'April' ),
],
[
'value' => '5',
'label' => __( 'May' ),
],
[
'value' => '6',
'label' => __( 'June' ),
],
[
'value' => '7',
'label' => __( 'July' ),
],
[
'value' => '8',
'label' => __( 'August' ),
],
[
'value' => '9',
'label' => __( 'September' ),
],
[
'value' => '10',
'label' => __( 'October' ),
],
[
'value' => '11',
'label' => __( 'November' ),
],
[
'value' => '12',
'label' => __( 'December' ),
],
];
}
/**
* @return int
*/
public static function get_display_order() {
return 20;
}
}

View File

@@ -0,0 +1,61 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\User;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Last_Logged_In extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'user_data';
}
/**
* @return string
*/
public static function get_key() {
return 'last_logged_in';
}
public static function get_label() {
return esc_html__( 'Last logged in', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'date_and_time_with_intervals' ];
}
public function get_value( $user_data ) {
$last_logged_in = '';
if ( $user_data ) {
$user_meta = get_user_meta( $user_data->ID );
if ( isset( $user_meta['tve_last_login'] ) && is_array( $user_meta['tve_last_login'] ) ) {
$last_logged_in = date( 'Y-m-d H:i:s', (int) $user_meta['tve_last_login'][0] );
}
}
return $last_logged_in;
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 10;
}
}

View File

@@ -0,0 +1,67 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\User;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Number_Of_Comments extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'user_data';
}
/**
* @return string
*/
public static function get_key() {
return 'number_of_comments';
}
public static function get_label() {
return esc_html__( 'Number of comments', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'number_comparison' ];
}
public function get_value( $user_data ) {
$comments_count = 0;
if ( ! empty( $user_data ) ) {
$args = [
'author_email' => $user_data->user_email,
'no_found_rows' => false,
'number' => 10,
'status' => 'all,spam,trash',
];
$query = new \WP_Comment_Query;
$query->query( $args );
$comments_count = (int) $query->found_comments;
}
return $comments_count;
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 25;
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\User;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class User_Id extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'user_data';
}
/**
* @return string
*/
public static function get_key() {
return 'user_id';
}
public static function get_label() {
return esc_html__( 'Username', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'autocomplete' ];
}
public function get_value( $user_data ) {
return empty( $user_data ) ? '' : $user_data->ID;
}
public static function get_options( $selected_values = [], $searched_keyword = '' ) {
$users = [];
foreach ( get_users() as $user ) {
if ( static::filter_options( $user->ID, $user->data->user_login, $selected_values, $searched_keyword ) ) {
$users[] = [
'value' => (string) $user->ID,
'label' => $user->data->user_login,
];
}
}
return $users;
}
/**
* @return string
*/
public static function get_placeholder_text() {
return esc_html__( 'Search users', 'thrive-cb' );
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 20;
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\User;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class User_Is_Logged_In extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'user_data';
}
/**
* @return string
*/
public static function get_key() {
return 'is_logged_in';
}
public static function get_label() {
return esc_html__( 'Is logged in', 'thrive-cb' );
}
public static function get_conditions() {
return [];
}
public function get_value( $user_data ) {
return ! empty( $user_data );
}
public static function is_boolean() {
return true;
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 0;
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\User;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class User_Is_Not_Logged_In extends User_Is_Logged_In {
/**
* @return string
*/
public static function get_entity() {
return 'user_data';
}
/**
* @return string
*/
public static function get_key() {
return 'is_not_logged_in';
}
public static function get_label() {
return esc_html__( 'Is not logged in', 'thrive-cb' );
}
public function get_value( $user_data ) {
return empty( $user_data );
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 1;
}
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\User;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class User_Registration_Date extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'user_data';
}
/**
* @return string
*/
public static function get_key() {
return 'user_registration_date';
}
public static function get_label() {
return esc_html__( 'Registration date', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'date_and_time_with_intervals' ];
}
public function get_value( $user_data ) {
return empty( $user_data ) ? '' : $user_data->user_registered;
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 15;
}
}

View File

@@ -0,0 +1,67 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Fields\User;
use TCB\ConditionalDisplay\Field;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class User_Role extends Field {
/**
* @return string
*/
public static function get_entity() {
return 'user_data';
}
/**
* @return string
*/
public static function get_key() {
return 'user_role';
}
public static function get_label() {
return esc_html__( 'Role', 'thrive-cb' );
}
public static function get_conditions() {
return [ 'checkbox' ];
}
public function get_value( $user_data ) {
return empty( $user_data ) ? '' : $user_data->roles;
}
public static function get_options( $selected_values = [], $search = '' ) {
$roles = [];
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . '/wp-admin/includes/user.php';
}
foreach ( get_editable_roles() as $key => $role ) {
$roles[ $key ] = [
'label' => $role['name'],
'value' => $key,
];
}
return $roles;
}
/**
* Determines the display order in the modal field select
*
* @return int
*/
public static function get_display_order() {
return 5;
}
}

View File

@@ -0,0 +1,473 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\PostTypes;
use TCB\ConditionalDisplay\Shortcode;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Conditional_Display_Group {
const NAME = 'tve_cond_display';
const DISPLAYS_META_KEY = '_tve_conditional_displays';
const LAZY_LOAD_META_KEY = '_tve_lazy_load';
const UNIFORM_HEIGHT_META_KEY = '_tve_uniform_height';
const DISPLAY_HEIGHTS_META_KEY = '_tve_display_heights';
const PLACEHOLDER_CSS_META_KEY = '_tve_placeholder_css';
const PLACEHOLDER_HTML_META_KEY = '_tve_placeholder_html';
const INHERIT_BACKGROUND_META_KEY = '_tve_inherit_background';
/** @var \WP_Post */
private $post;
/** @var string|null */
private $key;
/** @var array|null */
private $displays;
public static function title() {
return __( 'Conditional display group', 'thrive-cb' );
}
public static function register() {
register_post_type(
static::NAME,
[
'public' => isset( $_GET[ TVE_EDITOR_FLAG ] ),
'publicly_queryable' => is_user_logged_in(),
'query_var' => false,
'exclude_from_search' => true,
'rewrite' => false,
'_edit_link' => 'post.php?post=%d',
'map_meta_cap' => true,
'label' => static::title(),
'capabilities' => [
'edit_others_posts' => 'tve-edit-cpt',
'edit_published_posts' => 'tve-edit-cpt',
],
'show_in_nav_menus' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'has_archive' => false,
] );
}
/**
* @param null $key
*
* @return Conditional_Display_Group|null
*/
public static function get_instance( $key = null ) {
if ( $key === null ) {
return null;
}
$posts = get_posts( [
'post_type' => static::NAME,
'meta_query' => [
[
'key' => 'key',
'value' => $key,
],
],
] );
if ( empty( $posts ) ) {
$post = null;
} else {
$post = $posts[0];
}
return new self( $post, $key );
}
private function __construct( $post = null, $key = null ) {
$this->post = $post;
$this->key = $key;
}
public function create() {
$post_id = wp_insert_post( [
'post_title' => static::title(),
'post_type' => static::NAME,
'post_status' => 'publish',
'meta_input' => [
'key' => $this->key,
],
] );
$this->post = get_post( $post_id );
}
public function update( $data ) {
if ( $this->post === null ) {
$this->create();
}
foreach ( static::get_data_attributes() as $key => $meta_key ) {
if ( isset( $data[ $key ] ) ) {
$this->update_meta_key( $meta_key, $data[ $key ] );
}
}
}
/**
* @return string
*/
public function get_key() {
return $this->key;
}
/**
* @param $meta_key
* @param $default
*
* @return mixed|null
*/
public function get_meta_value( $meta_key, $default = null ) {
return $this->post === null ? $default : get_post_meta( $this->post->ID, $meta_key, true );
}
/**
* @param $meta_key
* @param $meta_value
*
* @return void
*/
public function update_meta_key( $meta_key, $meta_value ) {
update_post_meta( $this->post->ID, $meta_key, $meta_value );
}
/**
* Clone group and displays and return the new type
*
* @return Conditional_Display_Group
*/
public function clone_group( $css_id_map = [] ) {
$new_group_key = uniqid( 'tve-dg', false );
$new_group = new Conditional_Display_Group( null, $new_group_key );
$new_group->create();
$old_group_key = $this->get_key();
foreach ( static::get_data_attributes() as $meta_key ) {
$meta_value = $this->get_meta_value( $meta_key );
if ( $meta_key === static::DISPLAYS_META_KEY && is_array( $meta_value ) ) {
/* generate new keys for each display */
foreach ( $meta_value as $index => $display ) {
if ( $display['key'] !== 'default' ) {
$old_display_key = $display['key'];
$new_display_key = uniqid( '', false );
$display['key'] = $new_display_key;
/* replace old display key with the new one */
$display['html'] = str_replace( $old_display_key, $new_display_key, $display['html'] );
}
/* replace old group key with the new one */
$display['html'] = str_replace( $old_group_key, $new_group_key, $display['html'] );
/* replace css ids with newly generated ones */
$display['html'] = str_replace( array_keys( $css_id_map ), array_values( $css_id_map ), $display['html'] );
/* replace other groups inside the display */
$cloned_content = static::clone_conditional_groups_in_content( $display['html'], $css_id_map );
$display['html'] = $cloned_content['content'];
$meta_value[ $index ] = $display;
}
} elseif ( \is_string( $meta_value ) ) {
/* just replace the group key in every string */
$meta_value = str_replace( $old_group_key, $new_group_key, $meta_value );
/* also replace css ids */
$meta_value = str_replace( array_keys( $css_id_map ), array_values( $css_id_map ), $meta_value );
}
$new_group->update_meta_key( $meta_key, $meta_value );
}
return $new_group;
}
/**
* Data attributes
*
* @return string[]
*/
public static function get_data_attributes() {
return [
'displays' => static::DISPLAYS_META_KEY,
'lazy-load' => static::LAZY_LOAD_META_KEY,
'uniform-heights' => static::UNIFORM_HEIGHT_META_KEY,
'display-heights' => static::DISPLAY_HEIGHTS_META_KEY,
'placeholder-css' => static::PLACEHOLDER_CSS_META_KEY,
'placeholder-html' => static::PLACEHOLDER_HTML_META_KEY,
'inherit-bg' => static::INHERIT_BACKGROUND_META_KEY,
];
}
/**
* Return an array of displays from the group
*
* @param boolean $ordered
* @param boolean $is_editor_page
*
* @return array|mixed
*/
public function get_displays( $ordered = true, $is_editor_page = false ) {
if ( empty( $this->post ) ) {
$displays = [];
} else {
$displays = $this->get_meta_value( static::DISPLAYS_META_KEY );
if ( empty( $displays ) ) {
$displays = [];
} elseif ( $ordered ) {
$default_display_index = array_search( 'default', array_column( $displays, 'key' ), true );
$displays[ $default_display_index ]['order'] = $is_editor_page ? - 1 : PHP_INT_MAX;
uasort( $displays, static function ( $a, $b ) {
return (int) $a['order'] - (int) $b['order'];
} );
/* re-index after sorting */
$displays = array_values( $displays );
}
}
return $displays;
}
/**
* Localize display group data
*
* @param $for_preview boolean for preview we need more data
*
* @return array
*/
public function localize( $for_preview = false, $is_editor_page = false ) {
if ( empty( $GLOBALS['conditional_display_preview'] ) ) {
$GLOBALS['conditional_display_preview'] = [];
}
if ( ! isset( $GLOBALS['conditional_display_preview'][ $this->key ] ) ) {
$group_data = [
'key' => $this->key,
'lazy' => $this->has_lazy_load() ? 1 : 0,
'displays' => [],
'uniform-heights' => $this->has_uniformed_heights() ? 1 : 0,
'display-heights' => $this->get_display_heights(),
'inherit-bg' => $this->get_inherit_background(),
];
$external_resources = [
'js' => [],
'css' => [],
];
foreach ( $this->get_displays() as $display ) {
$display['html'] = Shortcode::parse_content( $display['html'], $is_editor_page );
$external_resources = static::get_external_resources_for_content( $external_resources, $display['html'] );
if ( $for_preview ) {
$display['sets'] = [];
$conditions = Shortcode::parse_condition_config( $display['conditions'] );
if ( ! empty( $conditions ) ) {
foreach ( $conditions as $set ) {
if ( empty( $set['ID'] ) ) {
$name = $set['label'];
} else {
$global_set = Global_Conditional_Set::get_instance( $set['ID'] );
$name = $global_set === null ? '' : $global_set->get_label();
}
$is_valid = Shortcode::verify_set( $set );
$display['sets'][] = [
'is_default' => false,
'is_checked' => $is_valid,
'is_global' => empty( $set['ID'] ) ? false : $set['ID'],
'name' => $name,
];
}
} elseif ( $display['key'] === 'default' ) {
$display['sets'][] = [ 'is_default' => true ];
}
}
$group_data['displays'][] = $display;
}
static::enqueue_external_resources( $external_resources );
$GLOBALS['conditional_display_preview'][ $this->key ] = $group_data;
}
return $GLOBALS['conditional_display_preview'][ $this->key ];
}
/**
* Check if we should load external resources for this content
*
* @param array $external_resources
* @param string $content
*
* @return array
*/
public static function get_external_resources_for_content( $external_resources, $content ) {
return apply_filters( 'tcb_external_resources_for_content', $external_resources, $content );
}
/**
* @param array $resources
*/
public static function enqueue_external_resources( $resources ) {
foreach ( $resources['js'] as $id => $script ) {
if ( ! wp_script_is( $id ) ) {
tve_enqueue_script( $id, $script['url'], $script['dependencies'], false, true );
}
}
foreach ( $resources['css'] as $id => $style ) {
if ( ! wp_style_is( $id ) ) {
tve_enqueue_style( $id, $style );
}
}
}
/**
* Check if the display group uses lazy load or not
*
* @return bool
*/
public function has_lazy_load() {
return $this->get_meta_value( static::LAZY_LOAD_META_KEY ) === '1';
}
/**
* @return bool
*/
public function has_uniformed_heights() {
return $this->get_meta_value( static::UNIFORM_HEIGHT_META_KEY ) === '1';
}
/**
* @return mixed|null
*/
public function get_display_heights() {
return $this->get_meta_value( static::DISPLAY_HEIGHTS_META_KEY, [] );
}
/**
* @return mixed|null
*/
public function get_inherit_background() {
return $this->get_meta_value( static::INHERIT_BACKGROUND_META_KEY, '' );
}
/**
* Check if the display group uses lazy load or not
*
* @return bool
*/
public function get_placeholder_css() {
$css = '';
if ( $this->post ) {
$css = $this->get_meta_value( static::PLACEHOLDER_CSS_META_KEY );
}
if ( ! empty( $css ) ) {
$css = \TCB_Utils::wrap_content( $css, 'style', '', 'tve-cd-placeholder-css-' . $this->key );
}
return $css;
}
public function lazy_load_placeholder() {
$placeholder_css = $this->get_placeholder_css();
$placeholder_html = $this->get_meta_value( static::PLACEHOLDER_HTML_META_KEY );
return empty( $placeholder_html ) ? '<div class="tcb-conditional-display-placeholder" data-group="' . $this->key . '">' . $placeholder_css . '</div>' : ( $placeholder_css . $placeholder_html );
}
/**
* @param array|string $groups
*
* @return void
*/
public static function save_groups( $groups = [] ) {
/* content templates save the groups in the JSON format, so we must decode first */
if ( ! empty( $groups ) && ! is_array( $groups ) ) {
$groups = json_decode( stripslashes( $groups ), true );
}
if ( is_array( $groups ) ) {
foreach ( $groups as $group ) {
if ( isset( $group['key'] ) ) {
$display_group = static::get_instance( (string) $group['key'] );
if ( $display_group !== null ) {
$display_group->update( $group );
}
}
}
}
}
/**
* Clone conditional groups from content and replace them with new ids
*
* @param string $content
* @param array $css_id_map
*
* @return array
*/
public static function clone_conditional_groups_in_content( $content, $css_id_map = [] ) {
$display_group_keys = [];
/* match display group key */
preg_match_all( '/\[' . Shortcode::NAME . " group='([^']*)'/m", $content, $matches );
if ( $matches !== null ) {
foreach ( $matches[1] as $display_group_key ) {
$display_group = static::get_instance( $display_group_key );
if ( $display_group !== null ) {
$new_display_group = $display_group->clone_group( $css_id_map );
$content = str_replace( $display_group_key, $new_display_group->get_key(), $content );
/* Store the mapping of old key to new key */
$display_group_keys[ $display_group_key ] = $new_display_group->get_key();
}
}
}
return [
'content' => $content,
'display_group_keys' => $display_group_keys,
];
}
}

View File

@@ -0,0 +1,130 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\PostTypes;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
class Global_Conditional_Set {
const NAME = 'tve_global_cond_set';
/** @var \WP_Post */
private $post;
public static function title() {
return __( 'Global condition set', 'thrive-cb' );
}
public static function register() {
register_post_type( static::NAME, [
'public' => isset( $_GET[ TVE_EDITOR_FLAG ] ),
'publicly_queryable' => is_user_logged_in(),
'query_var' => false,
'exclude_from_search' => true,
'rewrite' => false,
'_edit_link' => 'post.php?post=%d',
'map_meta_cap' => true,
'label' => static::title(),
'capabilities' => [
'edit_others_posts' => 'tve-edit-cpt',
'edit_published_posts' => 'tve-edit-cpt',
],
'show_in_nav_menus' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'has_archive' => false,
] );
}
/**
* @param $post_id
*
* @return Global_Conditional_Set|null
*/
public static function get_instance( $post_id = null ) {
if ( ! empty( $post_id ) ) {
$post = get_post( $post_id );
}
if ( empty( $post ) ) {
$post = null;
}
return new self( $post );
}
private function __construct( $post = null ) {
$this->post = $post;
}
public function get_post() {
return $this->post;
}
public function create( $rules, $label ) {
$post_id = wp_insert_post( [
'post_title' => static::title(),
'post_type' => static::NAME,
'post_status' => 'publish',
'meta_input' => [
'rules' => $rules,
'label' => $label,
],
]
);
$this->post = get_post( $post_id );
return $post_id;
}
public function update( $rules, $label ) {
update_post_meta( $this->post->ID, 'rules', $rules );
update_post_meta( $this->post->ID, 'label', $label );
}
public function remove() {
wp_delete_post( $this->post->ID, true );
}
public function get_rules() {
return get_post_meta( $this->post->ID, 'rules', true );
}
public function get_label() {
return empty( $this->post ) ? '' : get_post_meta( $this->post->ID, 'label', true );
}
/**
* @param string $searched_keyword
* @param bool $strict
*
* @return array[]
*/
public static function get_sets_by_name( $searched_keyword, $strict = false ) {
$posts = get_posts( [
'post_type' => static::NAME,
'posts_per_page' => 20,
'meta_query' => [
[
'key' => 'label',
'value' => $searched_keyword,
'compare' => $strict ? '=' : 'LIKE',
],
],
] );
return array_map( function ( $item ) {
return [
'value' => $item->ID,
'label' => get_post_meta( $item->ID, 'label' ),
];
}, $posts );
}
}

View File

@@ -0,0 +1,198 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\RestApi;
use TCB\ConditionalDisplay\Condition;
use TCB\ConditionalDisplay\Entity;
use TCB\ConditionalDisplay\Field;
use TCB\ConditionalDisplay\PostTypes\Conditional_Display_Group;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
/**
* Class General_Data
*
* @package TCB\ConditionalDisplay\RestApi
*/
class General_Data {
const REST_NAMESPACE = 'tcb/v1';
const REST_ROUTE = 'conditional-display/';
public static function register_routes() {
register_rest_route( static::REST_NAMESPACE, static::REST_ROUTE . 'groups', [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'get_groups' ],
'permission_callback' => [ __CLASS__, 'route_permission' ],
'args' => [
'groups' => [
'type' => 'array',
'required' => true,
],
],
],
] );
register_rest_route( static::REST_NAMESPACE, static::REST_ROUTE . 'fields', [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'get_fields' ],
'permission_callback' => [ __CLASS__, 'route_permission' ],
'args' => [
'entity' => [
'type' => 'string',
'required' => true,
],
],
],
] );
register_rest_route( static::REST_NAMESPACE, static::REST_ROUTE . 'conditions', [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'get_conditions' ],
'permission_callback' => [ __CLASS__, 'route_permission' ],
'args' => [
'field' => [
'type' => 'string',
'required' => true,
],
],
],
] );
register_rest_route( static::REST_NAMESPACE, static::REST_ROUTE . 'field-options', [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'get_field_options' ],
'permission_callback' => [ __CLASS__, 'route_permission' ],
'args' => [
'field' => [
'type' => 'string',
'required' => true,
],
],
],
] );
}
/**
* @param $request \WP_REST_Request
*
* @return array
*/
public static function get_groups( $request ) {
$group_keys = $request->get_param( 'groups' );
$query_vars = $request->get_param( 'query_vars' );
if ( ! empty( $query_vars ) ) {
tve_set_query_vars_data( $query_vars );
}
if ( is_array( $group_keys ) ) {
foreach ( $group_keys as $display_group_key ) {
$display_group = Conditional_Display_Group::get_instance( $display_group_key );
if ( $display_group !== null ) {
$display_group->localize( false, true );
}
}
$groups = array_values( $GLOBALS['conditional_display_preview'] );
} else {
$groups = [];
}
return $groups;
}
/**
* @param $request \WP_REST_Request
*
* @return \WP_REST_Response
*/
public static function get_fields( $request ) {
$entity_key = $request->get_param( 'entity' );
$field_data = [];
$entity = Entity::get();
$fields = $entity[ $entity_key ]::get_fields();
if ( ! empty( $fields ) ) {
foreach ( $fields as $field_key ) {
$field_class = Field::get()[ $field_key ];
$field_data[ $field_key ] = $field_class::get_data_to_localize();
}
}
return new \WP_REST_Response( $field_data );
}
/**
* @param \WP_REST_Request
*
* @return \WP_REST_Response
*/
public static function get_conditions( $request ) {
$field_key = $request->get_param( 'field' );
$condition_data = [];
$field = Field::get();
if ( ! empty( $field[ $field_key ] ) ) {
$conditions = $field[ $field_key ]::get_conditions();
if ( ! empty( $conditions ) ) {
foreach ( $conditions as $condition_key ) {
$condition_class = Condition::get()[ $condition_key ];
$condition_data[ $condition_key ] = $condition_class::get_data_to_localize();
}
}
}
return new \WP_REST_Response( $condition_data );
}
/**
* @param \WP_REST_Request
*
* @return \WP_REST_Response
*/
public static function get_field_options( $request ) {
$options = [];
$field_key = $request->get_param( 'field' );
if ( ! empty( Field::get()[ $field_key ] ) ) {
$selected_values = $request->get_param( 'values' );
$search = $request->get_param( 'search' );
$field = Field::get();
$options = $field[ $field_key ]::get_options( $selected_values, $search );
}
return new \WP_REST_Response( $options );
}
/**
* Check if a given request has access to route
*
* @return \WP_Error|bool
*/
public static function route_permission() {
$post_id = isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null;
return \TCB_Product::has_external_access( $post_id );
}
}

View File

@@ -0,0 +1,182 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\RestApi;
use TCB\ConditionalDisplay\PostTypes\Global_Conditional_Set;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
/**
* Class Global_Sets
*
* @package TCB\ConditionalDisplay\RestApi
*/
class Global_Sets {
const REST_NAMESPACE = 'tcb/v1';
const REST_ROUTE = 'conditional-display/global-set';
public static function register_routes() {
register_rest_route( static::REST_NAMESPACE, static::REST_ROUTE, [
[
'methods' => \WP_REST_Server::CREATABLE,
'callback' => [ __CLASS__, 'update_global_set' ],
'permission_callback' => [ __CLASS__, 'route_permission' ],
'args' => [
'label' => [
'type' => 'string',
'required' => true,
],
'rules' => [
'type' => 'array',
'required' => true,
],
],
],
] );
register_rest_route( static::REST_NAMESPACE, static::REST_ROUTE . '/(?P<id>[\d]+)', [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'get_global_set_data' ],
'permission_callback' => [ __CLASS__, 'route_permission' ],
],
[
'methods' => \WP_REST_Server::EDITABLE,
'callback' => [ __CLASS__, 'update_global_set' ],
'permission_callback' => [ __CLASS__, 'route_permission' ],
'args' => [
'label' => [
'type' => 'string',
'required' => true,
],
'rules' => [
'type' => 'array',
'required' => true,
],
],
],
[
'methods' => \WP_REST_Server::DELETABLE,
'callback' => [ __CLASS__, 'remove_global_set' ],
'permission_callback' => [ __CLASS__, 'route_permission' ],
],
] );
register_rest_route( static::REST_NAMESPACE, static::REST_ROUTE . '/get-all', [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'get_global_sets' ],
'permission_callback' => [ __CLASS__, 'route_permission' ],
'args' => [
'search' => [
'type' => 'string',
'required' => false,
],
],
],
] );
}
/**
* @param \WP_REST_Request
*
* @return \WP_REST_Response
*/
public static function get_global_set_data( $request ) {
$data = [];
$post_id = $request->get_param( 'id' );
$global_set = Global_Conditional_Set::get_instance( $post_id );
if ( ! empty( $global_set->get_post() ) ) {
$data = [
'id' => $post_id,
'rules' => $global_set->get_rules(),
'label' => $global_set->get_label(),
];
}
return new \WP_REST_Response( $data );
}
/**
* @param \WP_REST_Request
*
* @return \WP_REST_Response
*/
public static function get_global_sets( $request ) {
$searched_keyword = $request->get_param( 'search' );
$global_sets = Global_Conditional_Set::get_sets_by_name( $searched_keyword );
return new \WP_REST_Response( $global_sets );
}
/**
* @param \WP_REST_Request
*
* @return \WP_REST_Response|\WP_Error
*/
public static function update_global_set( $request ) {
$post_id = $request->get_param( 'id' );
$label = $request->get_param( 'label' );
$sets_with_this_label = Global_Conditional_Set::get_sets_by_name( $label, true );
if (
! empty( $sets_with_this_label ) &&
(
empty( $post_id ) || /* case 1: no post ID means we're adding a new global set and the label already exists */
count( $sets_with_this_label ) > 1 /* case 2: if there is a post ID: the only global set found for this label should be the current one */
)
) {
return new \WP_Error( 'tcb_error', __( 'A global set with this name already exists!', 'thrive-cb' ), [ 'status' => 409 ] );
}
$rules = $request->get_param( 'rules' );
if ( empty( $post_id ) ) {
$global_set = Global_Conditional_Set::get_instance();
$post_id = $global_set->create( $rules, $label );
} else {
$global_set = Global_Conditional_Set::get_instance( $post_id );
$global_set->update( $rules, $label );
}
return new \WP_REST_Response( $post_id );
}
/**
*
* @param \WP_REST_Request
*
* @return \WP_REST_Response
*/
public static function remove_global_set( $request ) {
$post_id = $request->get_param( 'id' );
$global_set = Global_Conditional_Set::get_instance( $post_id );
$global_set->remove();
return new \WP_REST_Response( $post_id );
}
/**
* Check if a given request has access to route
*
* @return \WP_Error|bool
*/
public static function route_permission() {
$post_id = isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null;
return \TCB_Product::has_external_access( $post_id );
}
}

View File

@@ -0,0 +1,75 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
namespace TCB\ConditionalDisplay\Traits;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
trait Item {
protected static $registered_items = [];
/**
* Get a list of all the registered items
*
* @param bool $sort_by_order
*
* @return static[]
*/
public static function get( $sort_by_order = false ) {
$items = static::$registered_items;
if ( $sort_by_order ) {
uasort( $items, static function ( $item1, $item2 ) {
/* @var Item $item1 , $item2 */
return $item1::get_display_order() - $item2::get_display_order();
} );
}
return $items;
}
/**
* @param $item
*/
public static function register( $item ) {
if ( is_subclass_of( $item, static::class ) ) {
if ( ! empty( $item::get_key() ) ) {
static::$registered_items[ $item::get_key() ] = $item;
} else {
trigger_error( $item . ' does not have an ID.' );
}
} else {
trigger_error( 'Argument ' . $item . ' must be a subclass of ' . static::class );
}
}
/**
* @param string $key
* @param array $extra_data
*
* @return mixed
*/
public static function get_instance( $key = '', $extra_data = [] ) {
$items = static::get();
$instance = null;
if ( isset( $items[ $key ] ) ) {
$instance = new $items[ $key ]( $extra_data );
}
return $instance;
}
/**
* @return string
*/
public function __toString() {
return __CLASS__;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* Thrive Themes - https://thrivethemes.com
*
* @package thrive-visual-editor
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden!
}
/**
* Register a new condition entity
*
* @param \TCB\ConditionalDisplay\Entity|string $entity
*/
function tve_register_condition_entity( $entity ) {
TCB\ConditionalDisplay\Entity::register( $entity );
}
/**
* Register a new condition field
*
* @param \TCB\ConditionalDisplay\Field|string $field
*/
function tve_register_condition_field( $field ) {
TCB\ConditionalDisplay\Field::register( $field );
}
/**
* Register a new condition
*
* @param \TCB\ConditionalDisplay\Condition|string $condition
*/
function tve_register_condition( $condition ) {
TCB\ConditionalDisplay\Condition::register( $condition );
}