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

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,690 @@
<?php
/**
* Author PhpStorm.
*/
class BWFAN_Dynamic_Option_Base extends BWFAN_Rule_Base {
public $rule_type = '';
public function __construct( $name ) {
parent::__construct( $name );
$this->rule_type = $name;
if ( isset( $_POST['ruletype'] ) && ( strtolower( $name ) === $_POST['ruletype'] ) ) {
add_filter( 'bwfan_select2_ajax_callable', array( $this, 'select2_ajax_callback' ), 10, 2 );
}
}
public function select2_ajax_callback( $callback, $posted ) {
if ( isset( $posted['type'] ) && $this->get_search_type_name() === $posted['type'] ) {
return array( $this, 'get_search_results' );
}
return $callback;
}
public function get_search_type_name() {
return '';
}
public function get_search_results( $term ) {
$array = [];
wp_send_json( array(
'results' => $array,
) );
}
public function conditions_view() {
$condition_input_type = $this->get_condition_input_type();
$values = $this->get_possible_rule_values();
$value_args = array(
'input' => $condition_input_type,
'name' => 'bwfan_rule[<%= groupId %>][<%= ruleId %>][condition]',
'choices' => $values,
'ajax' => true,
'search_type' => $this->get_search_type_name(),
'rule_type' => $this->rule_type,
);
bwfan_Input_Builder::create_input_field( $value_args );
}
public function get_condition_input_type() {
return 'Chosen_Select';
}
public function get_possible_rule_values() {
return [];
}
public function get_default_rule_value() {
return '';
}
}
if ( class_exists( 'WooCommerce' ) ) {
class BWFAN_Rule_Products extends BWFAN_Dynamic_Option_Base {
public function get_condition_values_nice_names( $values ) {
$return = [];
if ( ! is_array( $values ) || count( $values ) === 0 ) {
return $return;
}
foreach ( $values as $coupon_id ) {
$product = wc_get_product( $coupon_id );
if ( ! $product instanceof WC_Product ) {
continue;
}
$return[ $coupon_id ] = rawurldecode( BWFAN_Common::get_formatted_product_name( $product ) );
}
return $return;
}
public function get_search_results( $term, $v2 = false ) {
$this->set_product_types_arr();
$array = BWFAN_Common::product_search( $term, true, true );
if ( $v2 ) {
$return = array();
foreach ( $array as $product ) {
$return[ $product['id'] ] = $product['text'];
}
return $return;
}
wp_send_json( array(
'results' => $array,
) );
}
public function get_search_type_name() {
return 'product_search';
}
/** v2 Methods: Start */
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return $this->return_is_match( false, $rule_data );
}
$operator = $rule_data['rule'];
$data = $rule_data['data'];
if ( ! is_array( $data ) && empty( $data ) ) {
return $this->return_is_match( false, $rule_data );
}
$saved_products = array_map( function ( $product ) {
return $product['key'];
}, $data );
$found_products = $this->get_products( $automation_data );
$result = $this->validate_set( $saved_products, $found_products, $operator );
return $this->return_is_match( $result, $rule_data );
}
/** v2 Methods: END */
public function is_match( $rule_data ) {
$found_products = $this->get_products();
$result = $this->validate_set( $rule_data['condition'], $found_products, $rule_data['operator'] );
return $this->return_is_match( $result, $rule_data );
}
public function validate_set( $products, $found_products, $operator ) {
$result = false;
/** Get product ids with parent */
switch ( $operator ) {
case 'any':
$result = count( array_intersect( $products, $found_products ) ) > 0;
break;
case 'all':
$products = $this->get_product_with_parent( $products, $found_products );
$result = BWFAN_Common::array_equal( $products, $found_products );
break;
case 'none':
$result = count( array_intersect( $products, $found_products ) ) === 0;
break;
}
return $result;
}
public function ui_view() {
esc_html_e( 'Orders Items', 'wp-marketing-automations' );
?>
<% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %> <% var chosen = []; %>
<% _.each(condition, function( value, key ){ %>
<%
if(_.has(uiData, value)) {
chosen.push(uiData[value]);
}
%>
<% }); %>
<%= chosen.join("/ ") %>
<?php
}
public function get_possible_rule_operators() {
return array(
'any' => __( 'matches any of', 'wp-marketing-automations' ),
'all' => __( 'matches all of ', 'wp-marketing-automations' ),
'none' => __( 'matches none of ', 'wp-marketing-automations' ),
);
}
public function set_product_types_arr() {
BWFAN_Common::$offer_product_types = [
'simple',
'variable',
'variation',
'subscription',
'variable-subscription',
'subscription_variation'
];
}
/**
* Add parent id in product array if product has parent product
*
* @param $products
* @param $found_products
*
* @return array
*/
public function get_product_with_parent( $products, $found_products ) {
if ( ! is_array( $products ) || empty( $products ) ) {
return [];
}
$ids = $products;
foreach ( $products as $id ) {
$product_parent = get_post_parent( $id );
if ( $product_parent instanceof WP_Post ) {
$ids[] = $product_parent->ID;
continue;
}
/** If product is parent and child product is in the order */
$product = wc_get_product( $id );
if ( ! $product instanceof WC_Product || empty( $product->get_children() ) ) {
continue;
}
$child_product = array_intersect( $product->get_children(), $found_products );
$ids = array_merge( $ids, $child_product );
}
$ids = array_unique( $ids );
sort( $ids );
return $ids;
}
}
class BWFAN_Rule_Term_Taxonomy extends BWFAN_Dynamic_Option_Base {
public $taxonomy_name = 'product_cat';
public function get_possible_rule_values() {
$result = array();
$terms = get_terms( $this->taxonomy_name, array(
'hide_empty' => false,
) );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$result[ $term->term_id ] = $term->name;
}
}
return $result;
}
public function get_condition_values_nice_names( $values ) {
$return = [];
if ( count( $values ) > 0 ) {
foreach ( $values as $coupon_id ) {
$term = get_term_by( 'id', $coupon_id, $this->taxonomy_name );
$return[ $coupon_id ] = isset( $term->name ) ? $term->name : '';
}
}
return $return;
}
public function get_search_results( $term, $v2 = false ) {
$array = [];
$args = array(
'taxonomy' => array( $this->taxonomy_name ), // taxonomy name
'orderby' => 'id',
'order' => 'ASC',
'number' => 10,
'hide_empty' => false,
'fields' => 'all',
'name__like' => $term,
);
$terms = get_terms( $args );
if ( $v2 ) {
if ( count( $terms ) > 0 ) {
foreach ( $terms as $term ) {
$array[ $term->term_id ] = $term->name;
}
}
return $array;
}
if ( count( $terms ) > 0 ) {
foreach ( $terms as $term ) {
$array[] = array(
'id' => $term->term_id,
'text' => $term->name,
);
}
}
wp_send_json( array(
'results' => $array,
) );
}
public function get_search_type_name() {
return $this->taxonomy_name . '_search';
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return $this->return_is_match( false, $rule_data );
}
$type = $rule_data['rule'];
$data = $rule_data['data'];
if ( ! is_array( $data ) && empty( $data ) ) {
return $this->return_is_match( false, $rule_data );
}
$saved_terms = array_map( function ( $term ) {
return $term['key'];
}, $data );
$all_terms = $this->get_term_ids( $automation_data );
$all_terms = array_filter( $all_terms );
$result = false;
if ( empty( $all_terms ) ) {
$result = ( 'none' === $type ) ? true : false;
return $this->return_is_match( $result, $rule_data );
}
// Get translated term ID.
if ( defined( 'ICL_SITEPRESS_VERSION' ) && class_exists( 'BWFAN_Compatibility_With_WPML' ) && method_exists( 'BWFAN_Compatibility_With_WPML', 'get_translated_term_ids' ) ) {
$saved_terms = BWFAN_Compatibility_With_WPML::get_translated_term_ids( $saved_terms, $this->taxonomy_name, $automation_data );
}
switch ( $type ) {
case 'all':
if ( is_array( $saved_terms ) && is_array( $all_terms ) ) {
$result = count( array_intersect( $saved_terms, $all_terms ) ) === count( $saved_terms );
}
break;
case 'any':
if ( is_array( $saved_terms ) && is_array( $all_terms ) ) {
$result = count( array_intersect( $saved_terms, $all_terms ) ) >= 1;
}
break;
case 'none':
if ( is_array( $saved_terms ) && is_array( $all_terms ) ) {
$result = count( array_intersect( $saved_terms, $all_terms ) ) === 0;
}
break;
}
return $this->return_is_match( $result, $rule_data );
}
public function is_match( $rule_data ) {
$type = $rule_data['operator'];
$all_terms = $this->get_term_ids();
$all_terms = array_filter( $all_terms );
$result = false;
if ( empty( $all_terms ) ) {
$result = ( 'none' === $type ) ? true : false;
return $this->return_is_match( $result, $rule_data );
}
switch ( $type ) {
case 'all':
if ( is_array( $rule_data['condition'] ) && is_array( $all_terms ) ) {
$result = count( array_intersect( $rule_data['condition'], $all_terms ) ) === count( $rule_data['condition'] );
}
break;
case 'any':
if ( is_array( $rule_data['condition'] ) && is_array( $all_terms ) ) {
$result = count( array_intersect( $rule_data['condition'], $all_terms ) ) >= 1;
}
break;
case 'none':
if ( is_array( $rule_data['condition'] ) && is_array( $all_terms ) ) {
$result = count( array_intersect( $rule_data['condition'], $all_terms ) ) === 0;
}
break;
}
return $this->return_is_match( $result, $rule_data );
}
public function get_product_terms( $all_terms, $order, $cart_item ) {
$product = BWFAN_WooCommerce_Compatibility::get_product_from_item( $order, $cart_item );
if ( ! $product instanceof WC_Product ) {
return $all_terms;
}
$product_id = $product->get_id();
$product_id = ( $product->get_parent_id() ) ? $product->get_parent_id() : $product_id;
$terms = wp_get_object_terms( $product_id, $this->taxonomy_name, array(
'fields' => 'ids',
) );
return array_merge( $all_terms, $terms );
}
public function ui_view() {
esc_html_e( 'Order Items Taxonomy', 'wp-marketing-automations' );
?>
<% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %><% var chosen = []; %>
<% _.each(condition, function( value, key ){ %>
<% chosen.push(uiData[value]); %>
<% }); %>
<%= chosen.join("/ ") %>
<?php
}
public function get_possible_rule_operators() {
return array(
'any' => __( 'matches any of', 'wp-marketing-automations' ),
'all' => __( 'matches all of ', 'wp-marketing-automations' ),
);
}
}
}
class BWFAN_Rule_Country extends BWFAN_Rule_Base {
public function get_condition_input_type() {
return 'Chosen_Select';
}
/** v2 Methods: START */
public function get_options( $term = '' ) {
$countries = $this->get_possible_rule_values();
if ( empty( $term ) ) {
return $countries;
}
$array = array_filter( $countries, function ( $country ) use ( $term ) {
return false !== strpos( strtolower( $country ), strtolower( $term ) );
} );
return $array;
}
public function get_possible_rule_values() {
$countries_data = array();
/** get countries using get countries data from woofunnels core */
if ( function_exists( 'bwf_get_countries_data' ) ) {
$countries_data = bwf_get_countries_data();
}
return $countries_data;
}
public function get_rule_type() {
return 'Search';
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return $this->return_is_match( false, $rule_data );
}
$type = $rule_data['rule'];
$data = $rule_data['data'];
if ( ! is_array( $data ) && empty( $data ) ) {
return $this->return_is_match( false, $rule_data );
}
$saved_country = array_map( function ( $country ) {
return $country['key'];
}, $data );
$country = $this->get_objects_country( $automation_data );
if ( ! $country ) {
return $this->return_is_match( false, $rule_data );
}
$result = false;
switch ( $type ) {
case 'any':
if ( is_array( $saved_country ) && is_array( $country ) ) {
$result = count( array_intersect( $saved_country, $country ) ) >= 1;
}
break;
case 'none':
if ( is_array( $saved_country ) && is_array( $country ) ) {
$result = count( array_intersect( $saved_country, $country ) ) === 0;
}
break;
}
return $this->return_is_match( $result, $rule_data );
}
public function get_objects_country() {
if ( ! bwfan_is_woocommerce_active() ) {
return false;
}
$order = BWFAN_Core()->rules->getRulesData( 'wc_order' );
$country = BWFAN_WooCommerce_Compatibility::get_billing_country_from_order( $order );
return empty( $country ) ? false : array( $country );
}
/** v2 Methods: END */
public function is_match( $rule_data ) {
$type = $rule_data['operator'];
$country = $this->get_objects_country();
if ( ! $country ) {
return $country;
}
$result = false;
switch ( $type ) {
case 'any':
if ( is_array( $rule_data['condition'] ) && is_array( $country ) ) {
$result = count( array_intersect( $rule_data['condition'], $country ) ) >= 1;
}
break;
case 'none':
if ( is_array( $rule_data['condition'] ) && is_array( $country ) ) {
$result = count( array_intersect( $rule_data['condition'], $country ) ) === 0;
}
break;
}
return $this->return_is_match( $result, $rule_data );
}
public function ui_view() {
esc_html_e( 'Order Shipping Country', 'wp-marketing-automations' );
?>
<% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %>
<% var chosen = []; %>
<% _.each(condition, function( value, key ){ %>
<% chosen.push(uiData[value]); %>
<% }); %>
<%= chosen.join("/ ") %>
<?php
}
public function get_possible_rule_operators() {
return array(
'any' => __( 'matches any of', 'wp-marketing-automations' ),
'none' => __( 'matches none of ', 'wp-marketing-automations' ),
);
}
}
class BWFAN_Rule_Custom_Field extends BWFAN_Rule_Base {
public function conditions_view() {
$condition_input_type = $this->get_condition_input_type();
$values = $this->get_possible_rule_values();
$value_args = array(
'input' => $condition_input_type,
'name' => 'bwfan_rule[<%= groupId %>][<%= ruleId %>][condition][key]',
'choices' => $values,
'class' => 'bwfan_field_one_half',
'placeholder' => __( 'Key', 'wp-marketing-automations' ),
);
bwfan_Input_Builder::create_input_field( $value_args );
$condition_input_type = $this->get_condition_input_type();
$values = $this->get_possible_rule_values();
$value_args = array(
'input' => $condition_input_type,
'name' => 'bwfan_rule[<%= groupId %>][<%= ruleId %>][condition][value]',
'choices' => $values,
'class' => 'bwfan_field_one_half',
'placeholder' => __( 'Value', 'wp-marketing-automations' ),
);
bwfan_Input_Builder::create_input_field( $value_args );
}
public function get_condition_input_type() {
return 'Text';
}
public function get_possible_rule_values() {
return null;
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return $this->return_is_match( false, $rule_data );
}
$type = $rule_data['rule'];
$data = $rule_data['data'];
if ( ! is_array( $data ) || empty( $data ) ) {
return $this->return_is_match( false, $rule_data );
}
$key = $data[0] ?? '';
$saved_value = strtolower( trim( $data[1] ?? '' ) );
$value = strtolower( trim( $this->get_possible_value( $key, $automation_data ) ) );
if ( empty( $value ) && ! in_array( $type, [ 'is_blank', 'is_not_blank' ] ) ) {
return $this->return_is_match( false, $rule_data );
}
$result = false;
switch ( $type ) {
case 'is':
$result = ( $value === $saved_value );
break;
case 'isnot':
case 'is_not':
$result = ( $value !== $saved_value );
break;
case '>':
$result = ( $value >= $saved_value );
break;
case '<':
$result = ( $value <= $saved_value );
break;
case 'contains':
$result = strpos( $value, $saved_value ) !== false;
break;
case 'not_contains':
$result = strpos( $value, $saved_value ) === false;
break;
case 'starts_with':
$result = strpos( $value, $saved_value ) === 0;
break;
case 'ends_with':
$result = substr( $value, - strlen( $saved_value ) ) === $saved_value;
break;
case 'is_blank':
$result = empty( $value );
break;
case 'is_not_blank':
$result = ! empty( $value );
break;
}
return $this->return_is_match( $result, $rule_data );
}
public function get_possible_value( $key ) {
return __return_empty_string();
}
public function is_match( $rule_data ) {
$type = $rule_data['operator'];
$value = $this->get_possible_value( $rule_data['condition']['key'] );
$result = false;
switch ( $type ) {
case 'is':
$result = ( strtolower( $value ) === strtolower( $rule_data['condition']['value'] ) );
break;
case 'isnot':
case 'is_not':
$result = ( strtolower( $value ) !== strtolower( $rule_data['condition']['value'] ) );
break;
case '>':
$result = ( strtolower( $value ) >= strtolower( $rule_data['condition']['value'] ) );
break;
case '<':
$result = ( strtolower( $value ) <= strtolower( $rule_data['condition']['value'] ) );
break;
}
return $this->return_is_match( $result, $rule_data );
}
public function ui_view() {
?>
Order Custom Field
'<%= condition['key'] %>' <% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %> '<%= condition['value'] %>'
<?php
}
public function get_possible_rule_operators() {
return array(
'is' => __( 'is', 'wp-marketing-automations' ),
'isnot' => __( 'is not', 'wp-marketing-automations' ),
'>' => __( 'greater than', 'wp-marketing-automations' ),
'<' => __( 'less than', 'wp-marketing-automations' ),
);
}
}

View File

@@ -0,0 +1,330 @@
<?php
/**
* Base class for a Conditional_Content rule.
*/
class BWFAN_Rule_Base {
public $supports = array();
public $name = '';
public $description = '';
protected $v2 = false;
protected $v1 = true;
/** v2 Properties: START */
public $event_automation_meta = [];
public $title = '';
/** v2 Properties: END */
public function is_v2() {
return $this->v2;
}
public function is_v1() {
return $this->v1;
}
public function __construct( $name ) {
$this->name = $name;
}
public function get_name() {
return $this->name;
}
/**
* Checks if the conditions defined for this rule object have been met.
*
* @return boolean
*/
public function is_match( $rule_data ) {
return false;
}
/**
* Helper function to wrap the return value from is_match and apply filters or other modifications in sub classes.
*
* @param boolean $result The result that should be returned.
* @param array $rule_data The array config object for the current rule.
*
* @return boolean
*/
public function return_is_match( $result, $rule_data ) {
return apply_filters( 'bwfan_rules_is_match', $result, $rule_data );
}
/*
* Gets the input object type slug for this rule object.
*/
public function supports( $env ) {
return in_array( $env, $this->supports, true );
}
public function operators_view() {
$operators = $this->get_possible_rule_operators();
if ( empty( $operators ) ) {
return;
}
$operator_args = array(
'input' => 'select',
'name' => 'bwfan_rule[<%= groupId %>][<%= ruleId %>][operator]',
'choices' => $operators,
);
bwfan_Input_Builder::create_input_field( $operator_args );
}
/**
* Gets the list of possible rule operators available for this rule object.
*
* Override to return your own list of operators.
*
* @return array
*/
public function get_possible_rule_operators() {
return array(
'==' => __( 'is equal to', 'wp-marketing-automations' ),
'!=' => __( 'is not equal to', 'wp-marketing-automations' ),
);
}
/**
* Get number rule operators
*
* @return array
*/
public function get_possible_number_rule_operators() {
return array(
'==' => __( 'is equal to', 'wp-marketing-automations' ),
'!=' => __( 'is not equal to', 'wp-marketing-automations' ),
'>' => __( 'is greater than', 'wp-marketing-automations' ),
'<' => __( 'is less than', 'wp-marketing-automations' ),
'>=' => __( 'is greater or equal to', 'wp-marketing-automations' ),
'<=' => __( 'is less or equal to', 'wp-marketing-automations' ),
);
}
/**
* Get string rule operators
*
* @return array
*/
public function get_possible_string_rule_operators() {
return array(
'is' => __( 'is', 'wp-marketing-automations' ),
'is_not' => __( 'is not', 'wp-marketing-automations' ),
'contains' => __( 'contains', 'wp-marketing-automations' ),
'not_contains' => __( 'not contains', 'wp-marketing-automations' ),
'starts_with' => __( 'starts with', 'wp-marketing-automations' ),
'ends_with' => __( 'ends with', 'wp-marketing-automations' ),
);
}
public function conditions_view() {
$condition_input_type = $this->get_condition_input_type();
$values = $this->get_possible_rule_values();
$value_args = array(
'input' => $condition_input_type,
'name' => 'bwfan_rule[<%= groupId %>][<%= ruleId %>][condition]',
'choices' => $values,
);
bwfan_Input_Builder::create_input_field( $value_args );
echo $this->add_description(); //phpcs:ignore WordPress.Security.EscapeOutput
}
public function get_condition_input_type() {
return 'Select';
}
/**
* Get's the list of possible values for the rule.
*
* Override to return the correct list of possible values for your rule object.
* @return array
*/
public function get_possible_rule_values() {
return array();
}
public function ui_view() {
esc_html_e( 'Rule Preview here..', 'wp-marketing-automations' );
}
public function get_ui_preview_data() {
return $this->get_possible_rule_values();
}
public function add_description() {
ob_start();
if ( empty( $this->description ) ) {
return ob_get_clean();
}
echo '<div class="clearfix bwfan_field_desc">' . $this->description . '</div>'; //phpcs:ignore WordPress.Security.EscapeOutput
return ob_get_clean();
}
public function validate_matches_set( $array1, $array2, $operator ) {
switch ( $operator ) {
case 'any':
$result = count( array_intersect( $array1, $array2 ) ) > 0;
break;
case 'all':
$result = count( array_intersect( $array1, $array2 ) ) === count( $array1 );
break;
case 'none':
$result = count( array_intersect( $array1, $array2 ) ) === 0;
break;
default:
$result = false;
break;
}
return $result;
}
public function validate_matches_duration_set( $data, $rule_data, $type ) {
$current_time = current_time( 'timestamp' );
if ( 'between' === $type && is_array( $rule_data['data'] ) ) {
$from_data = $rule_data['data']['from'];
$to_data = $rule_data['data']['to'];
$from = strtotime( date( 'Y-m-d', $current_time - ( DAY_IN_SECONDS * absint( $from_data ) ) ) );// excluding time
$to = strtotime( date( 'Y-m-d', $current_time - ( DAY_IN_SECONDS * absint( $to_data ) ) ) );// excluding time
$result = ( ( $data >= $to ) && ( $data <= $from ) );
return $result;
}
$filter_value = strtotime( date( 'Y-m-d', $current_time - ( DAY_IN_SECONDS * absint( $rule_data['data'] ) ) ) );// excluding time
switch ( $type ) {
case 'over':
$result = ( $data < $filter_value );
break;
case 'past':
$result = ( $data >= $filter_value );
break;
default:
$result = false;
break;
}
return $result;
}
public function validate_matches( $operator, $condition_data, $data ) {
$result = false;
switch ( $operator ) {
case 'is':
$result = ( $condition_data === $data );
break;
case 'isnot':
case 'is_not':
$result = ( $condition_data !== $data );
break;
case 'contains':
$result = strpos( $data, $condition_data ) !== false;
break;
case 'not_contains':
$result = strpos( $data, $condition_data ) === false;
break;
case 'starts_with':
$length = strlen( $condition_data );
$result = substr( $data, 0, $length ) === $condition_data;
break;
case 'ends_with':
$length = strlen( $condition_data );
if ( 0 === $length ) {
$result = true;
} else {
$result = substr( $data, - $length ) === $condition_data;
}
break;
case 'is_blank':
$result = empty( $data );
break;
case 'is_not_blank':
$result = ! empty( $data );
break;
default:
break;
}
return $result;
}
public function operator_matches() {
return array(
'is' => __( 'is', 'wp-marketing-automations' ),
'is_not' => __( 'is not', 'wp-marketing-automations' ),
'contains' => __( 'contains', 'wp-marketing-automations' ),
'not_contains' => __( 'does not contain', 'wp-marketing-automations' ),
'starts_with' => __( 'starts with', 'wp-marketing-automations' ),
'ends_with' => __( 'ends with', 'wp-marketing-automations' ),
);
}
/** v2 Methods: START */
public function get_options( $search = '' ) {
return [];
}
public function get_rule_type() {
return 'Text';
}
public function get_value_label() {
return '';
}
public function get_readable_text_schema() {
return '{{key /}} {{rule /}} {{value /}}';
}
public function is_match_v2( $automation_data, $rule_data ) {
return true;
}
public function get_default_rule_value() {
return '';
}
public function get_multiple_select_support() {
return true;
}
/** v2 Methods: END */
public function make_value_as_array( $value ) {
if ( is_array( $value ) ) {
return $value;
}
/** Checking if value contains comma */
if ( strpos( $value, ',' ) !== false ) {
$value = explode( ',', $value );
$value = array_map( 'trim', $value );
} else {
$value = array( $value );
}
return $value;
}
/**
* Return extra properties need in js end
*
* @return array
*/
public function get_extra_props() {
return [];
}
}

View File

@@ -0,0 +1,232 @@
<?php
if ( bwfan_is_cf7_active() ) {
class BWFAN_Rule_CF7_Form_Field extends BWFAN_Rule_Base {
public function __construct() {
$this->v2 = true;
parent::__construct( 'cf7_form_field' );
}
/** v2 Methods: START */
public function get_options( $term = '' ) {
$meta = $this->event_automation_meta;
$form_id = isset( $meta['bwfan-cf7_form_submit_form_id'] ) ? $meta['bwfan-cf7_form_submit_form_id'] : 0;
if ( empty( $form_id ) ) {
return array();
}
/** @var BWFAN_CF7_Form_Submit $ins */
$ins = BWFAN_CF7_Form_Submit::get_instance();
return $ins->get_form_fields( $form_id );
}
public function get_rule_type() {
return 'key-value';
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return $this->return_is_match( false, $rule_data );
}
$entry = isset( $automation_data['global']['fields'] ) ? $automation_data['global']['fields'] : [];
$type = $rule_data['rule'];
$data = $rule_data['data'];
$key = isset( $data[0] ) ? $data[0] : '';
$saved_value = isset( $data[1] ) ? $data[1] : '';
$value = isset( $entry[ $key ] ) ? $entry[ $key ] : '';
$value = $this->make_value_as_array( $value );
$value = array_map( 'strtolower', $value );
$condition_value = strtolower( trim( $saved_value ) );
/** checking if condition value contains comma */
if ( strpos( $condition_value, ',' ) !== false ) {
$condition_value = explode( ',', $condition_value );
$condition_value = array_map( 'trim', $condition_value );
}
switch ( $type ) {
case 'is':
if ( is_array( $condition_value ) && is_array( $value ) ) {
$result = count( array_intersect( $condition_value, $value ) ) > 0;
} else {
$result = in_array( $condition_value, $value );
}
break;
case 'is_not':
if ( is_array( $condition_value ) && is_array( $value ) ) {
$result = count( array_intersect( $condition_value, $value ) ) === 0;
} else {
$result = ! in_array( $condition_value, $value );
}
break;
case 'contains':
if ( is_array( $value ) ) {
$result = ! empty( array_filter( $value, function ( $element ) use ( $condition_value ) {
return strpos( $element, $condition_value ) !== false;
} ) );
break;
}
$result = strpos( $value, $condition_value ) !== false;
break;
case 'not_contains':
if ( is_array( $value ) ) {
$result = ! empty( array_filter( $value, function ( $element ) use ( $condition_value ) {
return strpos( $element, $condition_value ) === false;
} ) );
break;
}
$result = strpos( $value, $condition_value ) === false;
break;
case 'starts_with':
$value = isset( $value[0] ) && ! empty( $value[0] ) ? $value[0] : '';
$length = strlen( $condition_value );
$result = substr( $value, 0, $length ) === $condition_value;
break;
case 'ends_with':
$value = is_array( $value ) ? end( $value ) : $value;
$length = strlen( $condition_value );
if ( 0 === $length || ( $length > strlen( $value ) ) ) {
$result = false;
break;
}
$result = substr( $value, - $length ) === $condition_value;
break;
case 'is_blank':
$result = empty( $value[0] );
break;
case 'is_not_blank':
$result = ! empty( $value[0] );
break;
default:
$result = false;
break;
}
return $this->return_is_match( $result, $rule_data );
}
/** v2 Methods: END */
public function get_possible_rule_operators() {
$operators = array(
'is' => __( 'is', 'wp-marketing-automations' ),
'is_not' => __( 'is not', 'wp-marketing-automations' ),
'contains' => __( 'contains', 'wp-marketing-automations' ),
'not_contains' => __( 'does not contain', 'wp-marketing-automations' ),
'starts_with' => __( 'starts with', 'wp-marketing-automations' ),
'ends_with' => __( 'ends with', 'wp-marketing-automations' ),
);
return $operators;
}
public function get_condition_input_type() {
return 'Text';
}
public function conditions_view() {
$values = $this->get_possible_rule_values();
$value_args = array(
'input' => 'select',
'name' => 'bwfan_rule[<%= groupId %>][<%= ruleId %>][condition][key]',
'choices' => $values,
'class' => 'bwfan_field_one_half bwfan_cf7_form_fields',
'placeholder' => __( 'Field', 'wp-marketing-automations' ),
);
bwfan_Input_Builder::create_input_field( $value_args );
$condition_input_type = $this->get_condition_input_type();
$values = $this->get_possible_rule_values();
$value_args = array(
'input' => $condition_input_type,
'name' => 'bwfan_rule[<%= groupId %>][<%= ruleId %>][condition][value]',
'choices' => $values,
'class' => 'bwfan_field_one_half',
'placeholder' => __( 'Value', 'wp-marketing-automations' ),
);
bwfan_Input_Builder::create_input_field( $value_args );
}
public function is_match( $rule_data ) {
$entry = BWFAN_Core()->rules->getRulesData( 'fields' );
$type = $rule_data['operator'];
$value = isset( $entry[ $rule_data['condition']['key'] ] ) ? $entry[ $rule_data['condition']['key'] ] : '';
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$value = array_map( 'strtolower', $value );
$condition_value = strtolower( trim( $rule_data['condition']['value'] ) );
/** checking if condition value contains comma */
if ( strpos( $condition_value, ',' ) !== false ) {
$condition_value = explode( ',', $condition_value );
$condition_value = array_map( 'trim', $condition_value );
}
switch ( $type ) {
case 'is':
if ( is_array( $condition_value ) && is_array( $value ) ) {
$result = count( array_intersect( $condition_value, $value ) ) > 0;
} else {
$result = in_array( $condition_value, $value );
}
break;
case 'is_not':
if ( is_array( $condition_value ) && is_array( $value ) ) {
$result = count( array_intersect( $condition_value, $value ) ) === 0;
} else {
$result = ! in_array( $condition_value, $value );
}
break;
case 'contains':
$value = isset( $value[0] ) && ! empty( $value[0] ) ? $value[0] : '';
$result = strpos( $value, $condition_value ) !== false;
break;
case 'not_contains':
$value = isset( $value[0] ) && ! empty( $value[0] ) ? $value[0] : '';
$result = strpos( $value, $condition_value ) === false;
break;
case 'starts_with':
$value = isset( $value[0] ) && ! empty( $value[0] ) ? $value[0] : '';
$length = strlen( $condition_value );
$result = substr( $value, 0, $length ) === $condition_value;
break;
case 'ends_with':
$value = isset( $value[0] ) && ! empty( $value[0] ) ? $value[0] : '';
$length = strlen( $condition_value );
if ( 0 === $length ) {
$result = true;
} else {
$result = substr( $value, - $length ) === $condition_value;
}
break;
default:
$result = false;
break;
}
return $this->return_is_match( $result, $rule_data );
}
public function ui_view() {
?>
Form Field
'<%= bwfan_events_js_data["cf7_form_submit"]["selected_form_fields"][condition['key']] %>' <% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %> '<%= condition['value'] %>'
<?php
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
class BWFAN_Rule_General_Always extends BWFAN_Rule_Base {
public $supports = array( 'cart', 'order' );
public function __construct() {
parent::__construct( 'general_always' );
}
public function get_possible_rule_operators() {
return null;
}
public function get_possible_rule_values() {
return null;
}
public function get_condition_input_type() {
return 'Html_Always';
}
public function is_match( $rule_data ) {
return true;
}
}

View File

@@ -0,0 +1,2 @@
<?php
// Silence is golden.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,240 @@
<?php
if ( ! class_exists( 'BWFAN_Rule_Comment_Count' ) ) {
class BWFAN_Rule_Comment_Count extends BWFAN_Rule_Base {
/**
* BWFAN_Rule_Comment_Count constructor.
*/
public function __construct() {
$this->v2 = true;
parent::__construct( 'comment_count' );
}
/**
* @return string
*/
public function get_rule_type() {
return 'Number';
}
/**
* @param $automation_data
* @param $rule_data
*
* @return bool
*/
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return $this->return_is_match( false, $rule_data );
}
$comment_id = isset( $automation_data['global']['comment_id'] ) ? $automation_data['global']['comment_id'] : 0;
$rating = $comment_id > 0 ? get_comment_meta( $comment_id, 'rating', true ) : 0;
$count = absint( $rating );
$operator = $rule_data['rule'];
$value = absint( $rule_data['data'] );
switch ( $operator ) {
case '==':
$result = $count === $value;
break;
case '!=':
$result = $count !== $value;
break;
case '>':
$result = $count > $value;
break;
case '<':
$result = $count < $value;
break;
case '>=':
$result = $count >= $value;
break;
case '<=':
$result = $count <= $value;
break;
default:
$result = false;
break;
}
return $this->return_is_match( $result, $rule_data );
}
/**
* @return string
*/
public function get_condition_input_type() {
return 'Text';
}
/**
* @param $rule_data
*
* @return bool
*/
public function is_match( $rule_data ) {
$comment_details = BWFAN_Core()->rules->getRulesData( 'wc_comment' );
$comment_rating_count = $comment_details['rating_number'];
$count = absint( $comment_rating_count );
$value = absint( $rule_data['condition'] );
switch ( $rule_data['operator'] ) {
case '==':
$result = $count === $value;
break;
case '!=':
$result = $count !== $value;
break;
case '>':
$result = $count > $value;
break;
case '<':
$result = $count < $value;
break;
case '>=':
$result = $count >= $value;
break;
case '<=':
$result = $count <= $value;
break;
default:
$result = false;
break;
}
return $this->return_is_match( $result, $rule_data );
}
/**
* @return void
*/
public function ui_view() {
esc_html_e( 'Review Rating count', 'wp-marketing-automations' );
?>
<% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %>
<%= condition %>
<?php
}
/**
* @return array
*/
public function get_possible_rule_operators() {
return $this->get_possible_number_rule_operators();
}
}
}
if ( ! class_exists( 'BWFAN_Rule_Comment_Products_Cats' ) ) {
class BWFAN_Rule_Comment_Products_Cats extends BWFAN_Rule_Term_Taxonomy {
/**
* BWFAN_Rule_Comment_Products_Cats constructor.
*/
public function __construct() {
$this->v2 = true;
$this->v1 = false;
parent::__construct( 'comment_products_cats' );
}
/** v2 Methods: START */
/**
* @param $search
*
* @return array
*/
public function get_options( $search = '' ) {
return $this->get_possible_rule_values( $search );
}
/**
* @return string
*/
public function get_rule_type() {
return 'Search';
}
/**
* @param $automation_data
* @param $rule_data
*
* @return bool
*/
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return $this->return_is_match( false, $rule_data );
}
$type = $rule_data['rule'];
$saved_terms = array_column( $rule_data['data'], 'key' );
$comment_id = $automation_data['global']['comment_id'];
if ( ! $comment_id ) {
return $this->return_is_match( false, $rule_data );
}
$comment = get_comment( $comment_id );
if ( ! $comment || ! is_a( $comment, 'WP_Comment' ) ) {
return $this->return_is_match( false, $rule_data );
}
$product_id = $comment->comment_post_ID;
if ( ! $product_id ) {
return $this->return_is_match( false, $rule_data );
}
$product_terms = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
switch ( $type ) {
case 'any':
$result = count( array_intersect( $saved_terms, $product_terms ) ) >= 1;
break;
case 'none':
$result = count( array_intersect( $saved_terms, $product_terms ) ) === 0;
break;
default:
$result = false;
break;
}
return $this->return_is_match( $result, $rule_data );
}
/**
* @return array
*/
public function get_possible_rule_operators() {
return array(
'any' => __( 'matches any of', 'wp-marketing-automations' ),
'none' => __( 'matches none of', 'wp-marketing-automations' ),
);
}
/**
* @param $search
*
* @return array
*/
public function get_possible_rule_values( $search = '' ) {
$result = array();
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
);
if ( ! empty( $search ) ) {
$args['name__like'] = sanitize_text_field( $search );
}
$terms = get_terms( $args );
foreach ( $terms as $term ) {
$result[ $term->term_id ] = $term->name;
}
return $result;
}
}
}

View File

@@ -0,0 +1,295 @@
<?php
class BWFAN_Rule_Users_Role extends BWFAN_Rule_Base {
public function __construct() {
$this->v2 = true;
parent::__construct( 'users_role' );
}
/** v2 Methods: START */
public function get_options( $term = '' ) {
return $this->get_possible_rule_values( $term );
}
public function get_rule_type() {
return 'Search';
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return false;
}
$user_id = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
$email = isset( $automation_data['global']['email'] ) ? trim( $automation_data['global']['email'] ) : 0;;
if ( 0 === absint( $user_id ) || ! is_email( $email ) ) {
return false;
}
$user = ! empty( $user_id ) ? get_user_by( 'id', $user_id ) : ( is_email( $email ) ? get_user_by( 'email', $email ) : '' );
if ( ! $user instanceof WP_User ) {
return false;
}
$operator = $rule_data['rule'];
$data = $rule_data['data'];
if ( ! is_array( $data ) && empty( $data ) ) {
return false;
}
$saved_roles = array_map( function ( $role ) {
return isset( $role['key'] ) ? $role['key'] : '';
}, $data );
$result = false;
$role = array_intersect( (array) $user->roles, $saved_roles );
if ( ! empty( $role ) ) {
$result = true;
}
return ( 'in' === $operator ) ? $result : ! $result;
}
/** v2 Methods: END */
public function get_possible_rule_operators() {
$operators = array(
'in' => __( 'is', 'wp-marketing-automations' ),
'notin' => __( 'is not', 'wp-marketing-automations' ),
);
return $operators;
}
public function get_possible_rule_values( $term = '' ) {
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
$editable_roles = get_editable_roles();
if ( empty( $editable_roles ) || ! is_array( $editable_roles ) ) {
return [];
}
$result = array();
foreach ( $editable_roles as $role => $details ) {
$name = translate_user_role( $details['name'] );
if ( empty( $term ) ) {
$result[ $role ] = $name;
continue;
}
if ( stripos( $name, $term ) !== false ) {
$result[ $role ] = $name;
}
}
return $result;
}
public function get_condition_input_type() {
return 'Chosen_Select';
}
public function is_match( $rule_data ) {
$user_id = BWFAN_Core()->rules->getRulesData( 'user_id' );
$email = BWFAN_Core()->rules->getRulesData( 'email' );
$user = ! empty( $user_id ) ? get_user_by( 'id', $user_id ) : ( is_email( $email ) ? get_user_by( 'email', $email ) : '' );
$user = ! $user instanceof WP_User ? BWFAN_Core()->rules->getRulesData( 'wp_user' ) : $user;
if ( ! $user instanceof WP_User ) {
return $this->return_is_match( false, $rule_data );
}
$result = false;
$role = [];
if ( $rule_data['condition'] && is_array( $rule_data['condition'] ) ) {
$role = array_intersect( (array) $user->roles, $rule_data['condition'] );
}
if ( ! empty( $role ) ) {
$result = true;
}
$result = ( 'in' === $rule_data['operator'] ) ? $result : ! $result;
return $this->return_is_match( $result, $rule_data );
}
public function sort_attribute_taxonomies( $taxa, $taxb ) {
return strcmp( $taxa->attribute_name, $taxb->attribute_name );
}
public function ui_view() {
esc_html_e( 'User Role', 'wp-marketing-automations' );
?>
<% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %>
<% var chosen = []; %>
<% _.each(condition, function( value, key ){ %>
<% chosen.push(uiData[value]); %>
<% }); %>
<%= chosen.join("/ ") %>
<?php
}
}
class BWFAN_Rule_Users_User extends BWFAN_Dynamic_Option_Base {
public function __construct() {
$this->v2 = true;
parent::__construct( 'users_user' );
}
/** v2 Methods: START */
public function get_options( $term = '' ) {
return $this->get_possible_values( $term, true );
}
public function get_rule_type() {
return 'Search';
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return false;
}
$user_id = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
if ( empty( $user_id ) ) {
$email = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : 0;
$user = get_user_by( 'email', $email );
if ( ! $user instanceof WP_User ) {
return false;
}
$user_id = $user->ID;
}
$operator = $rule_data['rule'];
$data = $rule_data['data'];
if ( ! is_array( $data ) && empty( $data ) ) {
return false;
}
$saved_users = array_map( function ( $user ) {
return isset( $user['key'] ) ? absint( $user['key'] ) : 0;
}, $data );
$result = false;
$result = in_array( $user_id, $saved_users, true );
return ( 'in' === $operator ) ? $result : ! $result;
}
public function get_default_rule_value() {
return 'yes';
}
/** v2 Methods: END */
public function get_search_type_name() {
return 'wp_users';
}
public function get_condition_values_nice_names( $values ) {
$return = [];
if ( is_array( $values ) && count( $values ) > 0 ) {
foreach ( $values as $user ) {
$userdata = get_userdata( $user );
$return[ $user ] = $userdata->display_name;
}
}
return $return;
}
public function get_possible_values( $term, $v2 = false ) {
$array = array();
$users = new WP_User_Query( array(
'search' => '*' . esc_attr( $term ) . '*',
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
'user_url',
),
) );
$users_found = $users->get_results();
$return = array();
foreach ( $users_found as $user ) {
array_push( $array, array(
'id' => $user->ID,
'text' => $user->data->display_name,
) );
if ( $v2 ) {
$return[ $user->ID ] = $user->data->display_name;
}
}
if ( $v2 ) {
return $return;
}
wp_send_json( array(
'results' => $array,
) );
}
public function get_possible_rule_operators() {
$operators = array(
'in' => __( 'is', 'wp-marketing-automations' ),
'notin' => __( 'is not', 'wp-marketing-automations' ),
);
return $operators;
}
public function is_match( $rule_data ) {
$user_id = BWFAN_Core()->rules->getRulesData( 'user_id' );
if ( empty( $user_id ) ) {
$email = BWFAN_Core()->rules->getRulesData( 'email' );
$user = ! is_email( $email ) ? get_user_by( 'email', $email ) : BWFAN_Core()->rules->getRulesData( 'wp_user' );
if ( ! $user instanceof WP_User ) {
return $this->return_is_match( false, $rule_data );
}
$user_id = $user->ID;
}
$rule_data['condition'] = array_map( 'intval', $rule_data['condition'] );
$result = in_array( $user_id, $rule_data['condition'], true );
$result = ( 'in' === $rule_data['operator'] ) ? $result : ! $result;
return $this->return_is_match( $result, $rule_data );
}
public function ui_view() {
esc_html_e( 'User', 'wp-marketing-automations' );
?>
<% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %>
<% var chosen = []; %>
<% _.each(condition, function( value, key ){ %>
<% chosen.push(uiData[value]); %>
<% }); %>
<%= chosen.join("/ ") %>
<?php
}
}

View File

@@ -0,0 +1,543 @@
<?php
class BWFAN_Rule_Is_First_Order extends BWFAN_Rule_Base {
public $supports = array( 'order' );
public function __construct() {
$this->v2 = true;
parent::__construct( 'is_first_order' );
}
/** v2 Methods: START */
public function get_options( $term = '' ) {
return $this->get_possible_rule_values();
}
public function get_rule_type() {
return 'Select';
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return false;
}
$email = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : 0;
$is_first = false;
if ( empty( $email ) || ! is_email( $email ) ) {
$order_id = isset( $automation_data['global']['order_id'] ) ? $automation_data['global']['order_id'] : 0;
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order ) {
return false;
}
$email = BWFAN_WooCommerce_Compatibility::get_order_data( $order, '_billing_email' );
}
$orders = wc_get_orders( array(
'customer' => $email,
'limit' => 2,
'return' => 'ids',
) );
if ( count( $orders ) === 1 ) {
$is_first = true;
}
$operator = $rule_data['data'];
return ( 'yes' === $operator ) ? $is_first : ! $is_first;
}
/** v2 Methods: END */
public function get_possible_rule_operators() {
return null;
}
public function get_possible_rule_values() {
return array(
'yes' => __( 'Yes', 'wp-marketing-automations' ),
'no' => __( 'No', 'wp-marketing-automations' ),
);
}
public function get_condition_input_type() {
return 'Select';
}
public function is_match( $rule_data ) {
$is_first = false;
$data = BWFAN_Core()->rules->getRulesData();
if ( empty( $data ) ) {
return $is_first;
}
$billing_email = ( isset( $data['email'] ) ) ? $data['email'] : '';
if ( empty( $billing_email ) && isset( $data['wc_order'] ) ) {
$order = $data['wc_order'];
/** check for order instance */
if ( $order instanceof WC_Order ) {
$billing_email = BWFAN_WooCommerce_Compatibility::get_order_data( $order, '_billing_email' );
}
}
if ( empty( $billing_email ) ) {
return $is_first;
}
$orders = wc_get_orders( array(
'customer' => $billing_email,
'limit' => 2,
'return' => 'ids',
) );
if ( count( $orders ) === 1 ) {
$is_first = true;
}
return ( 'yes' === $rule_data['condition'] ) ? $is_first : ! $is_first;
}
public function ui_view() {
esc_html_e( 'Order', 'wp-marketing-automations' );
?>
<% if (condition == "yes") { %> is <% } %>
<% if (condition == "no") { %> is not <% } %>
<?php
esc_html_e( 'a First Order', 'wp-marketing-automations' );
}
}
class BWFAN_Rule_Is_Guest extends BWFAN_Rule_Base {
public $supports = array( 'order' );
public function __construct() {
$this->v2 = true;
parent::__construct( 'is_guest' );
}
/** v2 Methods: START */
public function get_options( $term = '' ) {
return $this->get_possible_rule_values();
}
public function get_rule_type() {
return 'Select';
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return false;
}
$order_id = isset( $automation_data['global']['order_id'] ) ? $automation_data['global']['order_id'] : 0;
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order ) {
return false;
}
if ( ! empty( $order ) ) {
$result = ( $order->get_user_id() === 0 );
return ( 'yes' === $rule_data['data'] ) ? $result : ! $result;
}
$email = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
if ( ! empty( $email ) ) {
$result = true;
$user = get_user_by( 'user_email', $email );
if ( $user instanceof WP_User ) {
$result = false;
}
return ( 'yes' === $rule_data['data'] ) ? $result : ! $result;
}
/** Checking user logged in value only if order or email value not passed */
return ! is_user_logged_in();
}
/** v2 Methods: END */
public function get_possible_rule_operators() {
return null;
}
public function get_possible_rule_values() {
return array(
'yes' => __( 'Yes', 'wp-marketing-automations' ),
'no' => __( 'No', 'wp-marketing-automations' ),
);
}
public function is_match( $rule_data ) {
$order = BWFAN_Core()->rules->getRulesData( 'wc_order' );
if ( ! empty( $order ) ) {
$result = ( $order->get_user_id() === 0 );
return ( 'yes' === $rule_data['condition'] ) ? $result : ! $result;
}
$email = BWFAN_Core()->rules->get_environment_var( 'email' );
if ( ! empty( $email ) ) {
$result = true;
$user = get_user_by( 'user_email', $email );
if ( $user instanceof WP_User ) {
$result = false;
}
return ( 'yes' === $rule_data['condition'] ) ? $result : ! $result;
}
/** Checking user logged in value only if order or email value not passed */
return ! is_user_logged_in();
}
public function ui_view() {
esc_html_e( 'Order', 'wp-marketing-automations' );
?>
<% if (condition == "yes") { %> is <% } %>
<% if (condition == "no") { %> is not <% } %>
<?php
esc_html_e( 'a Guest Order', 'wp-marketing-automations' );
}
}
class BWFAN_Rule_Customer_User extends BWFAN_Dynamic_Option_Base {
public $supports = array( 'order' );
public function __construct() {
$this->v2 = true;
parent::__construct( 'customer_user' );
}
/** v2 Methods: START */
public function get_options( $term = '' ) {
return $this->get_possible_values( $term, true );
}
public function get_rule_type() {
return 'Search';
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return false;
}
$id = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
if ( empty( $id ) ) {
$order_id = isset( $automation_data['global']['order_id'] ) ? $automation_data['global']['order_id'] : 0;
$order = wc_get_order( $order_id );
$id = $order->get_user_id();
}
$data = $rule_data['data'];
$users = array_map( function ( $user ) {
return absint( $user['key'] );
}, $data );
$result = in_array( $id, $users, true );
$result = ( 'in' === $rule_data['rule'] ) ? $result : ! $result;
return $this->return_is_match( $result, $rule_data );
}
public function get_search_type_name() {
return 'wp_users';
}
public function get_condition_values_nice_names( $values ) {
$return = [];
if ( count( $values ) > 0 ) {
foreach ( $values as $user ) {
$userdata = get_userdata( $user );
$return[ $user ] = $userdata->display_name;
}
}
return $return;
}
public function get_possible_values( $term, $v2 = false ) {
$array = array();
$users = new WP_User_Query( array(
'search' => '*' . esc_attr( $term ) . '*',
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
'user_url',
),
) );
$users_found = $users->get_results();
$return = array();
foreach ( $users_found as $user ) {
array_push( $array, array(
'id' => $user->ID,
'text' => $user->data->display_name,
) );
if ( $v2 ) {
$return[ $user->ID ] = $user->data->display_name;
}
}
if ( $v2 ) {
return $return;
}
wp_send_json( array(
'results' => $array,
) );
}
public function is_match( $rule_data ) {
$id = BWFAN_Core()->rules->getRulesData( 'user_id' );
if ( empty( $id ) ) {
$order = BWFAN_Core()->rules->getRulesData( 'wc_order' );
$id = $order->get_user_id();
}
$rule_data['condition'] = array_map( 'intval', $rule_data['condition'] );
$result = in_array( $id, $rule_data['condition'], true );
$result = ( 'in' === $rule_data['operator'] ) ? $result : ! $result;
return $this->return_is_match( $result, $rule_data );
}
public function ui_view() {
esc_html_e( 'User', 'wp-marketing-automations' );
?>
<% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %>
<% var chosen = []; %>
<% _.each(condition, function( value, key ){ %>
<% chosen.push(uiData[value]); %>
<% }); %>
<%= chosen.join("/ ") %>
<?php
}
public function get_possible_rule_operators() {
return array(
'in' => __( 'is', 'wp-marketing-automations' ),
'notin' => __( 'is not', 'wp-marketing-automations' ),
);
}
}
class BWFAN_Rule_Customer_Role extends BWFAN_Rule_Base {
public $supports = array( 'order' );
public function __construct() {
$this->v2 = true;
parent::__construct( 'customer_role' );
}
/** v2 Methods: START */
public function get_options( $term = '' ) {
return $this->get_possible_rule_values( $term );
}
public function get_rule_type() {
return 'Search';
}
public function is_match_v2( $automation_data, $rule_data ) {
if ( ! isset( $automation_data['global'] ) || ! is_array( $automation_data['global'] ) ) {
return false;
}
$id = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
$abandoned_data = isset( $automation_data['global']['abandoned_data'] ) ? $automation_data['global']['abandoned_data'] : [];
$contact_id = isset( $automation_data['global']['contact_id'] ) ? $automation_data['global']['contact_id'] : 0;
/** If contact_id available */
if ( empty( $id ) && ! empty( $contact_id ) ) {
$contact = new WooFunnels_Contact( '', '', '', $contact_id );
$id = $contact->get_wpid();
}
/** If WooCommerce active and order object available */
if ( empty( $id ) && class_exists( 'WooCommerce' ) ) {
$order_id = isset( $automation_data['global']['order_id'] ) ? $automation_data['global']['order_id'] : 0;
$order = wc_get_order( $order_id );
$id = $order instanceof WC_Order ? $order->get_user_id() : $id;
}
/** If email available */
if ( empty( $id ) ) {
$email = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
$email = is_email( $email ) ? $email : ( isset( $abandoned_data['email'] ) ? $abandoned_data['email'] : false );
if ( ! is_email( $email ) ) {
return false;
}
$contact_db = WooFunnels_DB_Operations::get_instance();
$contact_obj = $contact_db->get_contact_by_email( $email );
if ( isset( $contact_obj->wpid ) && absint( $contact_obj->wpid ) > 0 ) {
$id = absint( $contact_obj->wpid );
}
/** Get WP user by email */
if ( empty( $id ) ) {
$user_data = get_user_by( 'email', $email );
$id = $user_data instanceof WP_User ? $user_data->ID : false;
}
}
/** If no user, return false */
if ( empty( $id ) ) {
return $this->return_is_match( false, $rule_data );
}
$data = $rule_data['data'];
if ( ! is_array( $data ) && empty( $data ) ) {
return false;
}
$saved_roles = array_map( function ( $role ) {
return $role['key'];
}, $data );
$result = false;
$user = get_user_by( 'id', $id );
$role = array_intersect( (array) $user->roles, $saved_roles );
$result = ! empty( $role ) ? true : false;
return $this->return_is_match( ( 'in' === $rule_data['rule'] ) ? $result : ! $result, $rule_data );
}
/** v2 Methods: END */
public function get_possible_rule_values( $term = '' ) {
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
$editable_roles = get_editable_roles();
if ( empty( $editable_roles ) || ! is_array( $editable_roles ) ) {
return [];
}
$result = array();
foreach ( $editable_roles as $role => $details ) {
$name = translate_user_role( $details['name'] );
if ( empty( $term ) ) {
$result[ $role ] = $name;
continue;
}
if ( stripos( $name, $term ) !== false ) {
$result[ $role ] = $name;
}
}
return $result;
}
public function get_condition_input_type() {
return 'Chosen_Select';
}
public function is_match( $rule_data ) {
$id = BWFAN_Core()->rules->getRulesData( 'user_id' );
$abandoned_data = BWFAN_Core()->rules->getRulesData( 'abandoned_data' );
$contact_id = BWFAN_Core()->rules->getRulesData( 'contact_id' );
/** If contact_id available */
if ( empty( $id ) && ! empty( $contact_id ) ) {
$contact = new WooFunnels_Contact( '', '', '', $contact_id );
$id = $contact->get_wpid();
}
/** If WooCommerce active and order object available */
if ( empty( $id ) && class_exists( 'WooCommerce' ) ) {
$order = BWFAN_Core()->rules->getRulesData( 'wc_order' );
$id = $order instanceof WC_Order ? $order->get_user_id() : $id;
}
/** If email available */
if ( empty( $id ) ) {
$email = BWFAN_Core()->rules->getRulesData( 'email' );
$email = is_email( $email ) ? $email : ( isset( $abandoned_data['email'] ) ? $abandoned_data['email'] : false );
if ( ! is_email( $email ) ) {
return false;
}
$contact_db = WooFunnels_DB_Operations::get_instance();
$contact_obj = $contact_db->get_contact_by_email( $email );
if ( isset( $contact_obj->wpid ) && absint( $contact_obj->wpid ) > 0 ) {
$id = absint( $contact_obj->wpid );
}
/** Get WP user by email */
if ( empty( $id ) ) {
$user_data = get_user_by( 'email', $email );
$id = $user_data instanceof WP_User ? $user_data->ID : false;
}
}
/** If no user, return false */
if ( empty( $id ) ) {
return $this->return_is_match( false, $rule_data );
}
$result = false;
if ( $rule_data['condition'] && is_array( $rule_data['condition'] ) ) {
$user = get_user_by( 'id', $id );
foreach ( $rule_data['condition'] as $role ) {
if ( in_array( $role, $user->roles ) ) {
$result = true;
break;
}
}
}
if ( 'in' === $rule_data['operator'] ) {
return $this->return_is_match( $result, $rule_data );
} else {
return $this->return_is_match( ! $result, $rule_data );
}
}
public function ui_view() {
esc_html_e( 'User Role', 'wp-marketing-automations' );
?>
<% var ops = JSON.parse('<?php echo wp_json_encode( $this->get_possible_rule_operators() ); ?>'); %>
<%= ops[operator] %>
<% var chosen = []; %>
<% _.each(condition, function( value, key ){ %>
<% chosen.push(uiData[value]); %>
<% }); %>
<%= chosen.join("/ ") %>
<?php
}
public function get_possible_rule_operators() {
return array(
'in' => __( 'is', 'wp-marketing-automations' ),
'notin' => __( 'is not', 'wp-marketing-automations' ),
);
}
}