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,53 @@
<?php
/**
* Helper class to render input field types.
*/
class bwfan_Input_Builder {
/**
* Gets the input object and renders the field.
*
* @param array $field_args Arguments to render the field with.
* @param mixed $value The value, if any, to apply to the field.
*/
public static function create_input_field( $field_args, $value = null ) {
$field_args = apply_filters( 'bwfan_rules_get_input_defaults', self::get_input_field_defaults( $field_args ) );
$field_value = apply_filters( 'bwfan_rules_get_input_value', $value, $field_args );
$input_object = BWFAN_Rules::woocommerce_bwfan_rule_get_input_object( $field_args['input'] );
$input_object->render( $field_args, $field_value );
}
/**
* Helper function to get field defaults.
*
* @param array $field_args Arguments to merge with the defaults.
*
* @return array The merged arguments
*/
public static function get_input_field_defaults( $field_args ) {
// defaults
$defaults = array(
'key' => '',
'label' => '',
'name' => '',
'input' => 'text',
'order_no' => 1,
'instructions' => '',
'required' => 0,
'id' => '',
'class' => '',
);
$field_args = array_merge( $defaults, $field_args );
if ( ! isset( $field_args['id'] ) ) {
$field_args['id'] = sanitize_title( $field_args['name'] );
}
return $field_args;
}
}

View File

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

View File

@@ -0,0 +1,54 @@
<?php
class bwfan_Input_Cart_Category_Select {
public function __construct() {
// vars
$this->type = 'Cart_Category_Select';
$this->defaults = array(
'multiple' => 0,
'allow_null' => 0,
'choices' => array(),
'default_value' => array(),
'class' => '',
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
$current = isset( $value['categories'] ) ? $value['categories'] : array();
$choices = $field['choices'];
?>
<table style="width:100%;">
<tr>
<td style="width:32px;"><?php esc_html_e( 'Quantity', 'wp-marketing-automations' ); ?></td>
<td><?php esc_html_e( 'Categories', 'wp-marketing-automations' ); ?></td>
</tr>
<tr>
<td style="width:32px; vertical-align:top;">
<input type="text" id="<?php echo esc_attr( $field['id'] ); ?>_qty" name="<?php echo $field['name']; //phpcs:ignore WordPress.Security.EscapeOutput ?>[qty]" value="<?php echo isset( $value['qty'] ) ? esc_attr( sanitize_text_field( $value['qty'] ) ) : 1; ?>"/>
</td>
<td>
<select id="<?php echo esc_attr( $field['id'] ); ?>" name="<?php echo $field['name']; //phpcs:ignore WordPress.Security.EscapeOutput ?>[categories][]" class="chosen_select <?php echo esc_attr( $field['class'] ); ?>" multiple="multiple" data-placeholder="<?php echo( isset( $field['placeholder'] ) ? esc_attr( sanitize_text_field( $field['placeholder'] ) ) : esc_html__( 'Search...', 'wp-marketing-automations' ) ); ?>">
<?php
foreach ( $choices as $choice => $title ) {
$selected = in_array( $choice, $current, true );
echo '<option value="' . esc_attr( $choice ) . '" ' . selected( $selected, true, false ) . '">' . esc_html( $title ) . '</option>';
}
?>
</select>
</td>
</tr>
</table>
<?php
}
}
?>

View File

@@ -0,0 +1,70 @@
<?php
#[AllowDynamicProperties]
class BWFAN_Input_Cart_Product_Select {
public function __construct() {
// vars
$this->type = 'Cart_Product_Select';
$this->defaults = array(
'multiple' => false,
'allow_null' => 0,
'choices' => array(),
'default_value' => '',
'ajax' => 'true',
'class' => 'ajax_chosen_select_products',
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
$data_attr = '';
$chosen_class = '';
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
if ( true === $field['ajax'] ) {
$chosen_class = 'bwfan-select2ajax-single'; // bwfan_select2_ajax
$data_attr = 'data-search="product_search" data-search-text="' . esc_attr__( 'Select Product', 'wp-marketing-automations' ) . '"';
}
if ( ! empty( $field['rule_type'] ) ) {
$data_attr .= ' data-rule-type="' . $field['rule_type'] . '"';
}
?>
<table class="bwfan-rules-condition_qty" style="width:100%;">
<tr>
<td style="width:32px;"><?php esc_html_e( 'Quantity', 'wp-marketing-automations' ); ?></td>
<td><?php esc_html_e( 'Products', 'wp-marketing-automations' ); ?></td>
</tr>
<tr>
<td style="width:32px; vertical-align:top;">
<input type="text" id="<?php echo esc_attr( $field['id'] ); ?>_qty" name="<?php echo $field['name']; //phpcs:ignore WordPress.Security.EscapeOutput ?>[qty]" value="<?php echo isset( $value['qty'] ) ? esc_attr( sanitize_text_field( $value['qty'] ) ) : 1; ?>"/>
</td>
<td>
<select <?php echo( $data_attr ); //phpcs:ignore WordPress.Security.EscapeOutput ?> id="<?php echo esc_attr( $field['id'] ); ?>" name="<?php echo $field['name']; //phpcs:ignore WordPress.Security.EscapeOutput ?>[products]" class="<?php echo $chosen_class; ?>" data-placeholder="<?php echo( isset( $field['placeholder'] ) ? esc_attr( sanitize_text_field( $field['placeholder'] ) ) : esc_html__( 'Search...', 'wp-marketing-automations' ) ); ?>">
<option value=""><?php esc_html_e( 'Choose Product', 'wp-marketing-automations' ); ?></option>
<?php
$current = isset( $value['products'] ) ? $value['products'] : array();
$product_ids = ! empty( $current ) ? array_map( 'absint', $current ) : null;
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
$product_name = BWFAN_WooCommerce_Compatibility::woocommerce_get_formatted_product_name( $product );
echo '<option value="' . esc_attr( $product_id ) . '" selected="selected">' . esc_html( $product_name ) . '</option>';
}
}
?>
</select>
</td>
</tr>
</table>
<?php
}
}

View File

@@ -0,0 +1,54 @@
<?php
#[AllowDynamicProperties]
class bwfan_Input_Chosen_Select {
public function __construct() {
// vars
$this->type = 'Chosen_Select';
$this->defaults = array(
'multiple' => 1,
'allow_null' => 0,
'choices' => array(),
'default_value' => array(),
'class' => '',
'ajax' => false,
'rule_type' => '',
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
$current = $value ? $value : array();
$choices = $field['choices'];
$chosen_class = 'bwfan_select2 ';
$data_attr = '';
$multiple = $field['multiple'] ? 'multiple' : '';
if ( true === $field['ajax'] ) {
$chosen_class = 'bwfan_select2_ajax ';
$data_attr = 'data-search-type="' . $field['search_type'] . '"';
}
if ( ! empty( $field['rule_type'] ) ) {
$data_attr .= ' data-rule-type="' . $field['rule_type'] . '"';
}
?>
<select <?php echo( $data_attr ); //phpcs:ignore WordPress.Security.EscapeOutput ?> id="<?php echo esc_attr( $field['id'] ); ?>" name="<?php echo $field['name']; //phpcs:ignore WordPress.Security.EscapeOutput ?>[]" class="<?php echo esc_attr( $chosen_class ) . esc_attr( $field['class'] ); ?>" <?php echo esc_attr( $multiple ); ?> data-placeholder="<?php echo( isset( $field['placeholder'] ) ? esc_attr( sanitize_text_field( $field['placeholder'] ) ) : esc_html__( 'Search...', 'wp-marketing-automations' ) ); ?>">
<?php
foreach ( $choices as $choice => $title ) {
$selected = in_array( $choice, $current, true );
echo '<option value="' . ( esc_attr( $choice ) ) . '" ' . selected( $selected, true, false ) . '>' . esc_html( $title ) . '</option>';
}
?>
</select>
<?php
}
}
?>

View File

@@ -0,0 +1,20 @@
<?php
class bwfan_Input_Date extends bwfan_Input_Text {
public function __construct() {
$this->type = 'Date';
parent::__construct();
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
echo '<input name="' . $field['name'] . '" type="text" id="' . esc_attr( $field['id'] ) . '" class="bwfan-date-picker-field' . esc_attr( $field['class'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . $value . '" />'; //phpcs:ignore WordPress.Security.EscapeOutput
}
}

View File

@@ -0,0 +1,44 @@
<?php
class bwfan_Input_Geo_Postal_Code_Entry {
public function __construct() {
// vars
$this->type = 'Geo_Postal_Code_Entry';
$this->defaults = array(
'multiple' => 0,
'allow_null' => 0,
'choices' => array(),
'default_value' => '',
'class' => '',
'placeholder' => '',
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
?>
<table style="width:100%;">
<tr>
<td style="width:162px;"><?php esc_html_e( 'Distance ( km )', 'wp-marketing-automations' ); ?></td>
<td><?php esc_html_e( 'Zip/Postalcode ( One per line )', 'wp-marketing-automations' ); ?></td>
</tr>
<tr>
<td style="width:162px; vertical-align:top;">
<input type="text" id="<?php echo esc_attr( $field['id'] ); ?>_qty" name="<?php echo $field['name']; //phpcs:ignore WordPress.Security.EscapeOutput ?>[qty]" value="<?php echo isset( $value['qty'] ) ? esc_attr( sanitize_text_field( $value['qty'] ) ) : 1; ?>"/>
</td>
<td>
<?php echo '<textarea style="width:100%" rows="20" name="' . $field['name'] . '[codes]" type="text" id="' . esc_attr( $field['id'] ) . '" class="' . esc_attr( $field['class'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '">' . esc_textarea( $value['codes'] ) . '</textarea>'; //phpcs:ignore WordPress.Security.EscapeOutput ?>
</td>
</td>
</tr>
</table>
<?php
}
}

View File

@@ -0,0 +1,19 @@
<?php
class bwfan_Input_Html_Always {
public function __construct() {
// vars
$this->type = 'Html_Always';
$this->defaults = array(
'default_value' => '',
'class' => '',
'placeholder' => '',
);
}
public function render( $field, $value = null ) {
esc_html_e( 'Funnel will run on every order on your store. This will override any other rule you define.', 'wp-marketing-automations' );
}
}

View File

@@ -0,0 +1,19 @@
<?php
class bwfan_Input_Html_Rule_Is_Downgrade {
public function __construct() {
// vars
$this->type = 'Html_Rule_Is_Downgrade';
$this->defaults = array(
'default_value' => '',
'class' => '',
'placeholder' => '',
);
}
public function render( $field, $value = null ) {
esc_html_e( 'This Funnel will initiate on orders that have downgraded subscriptions.', 'wp-marketing-automations' );
}
}

View File

@@ -0,0 +1,19 @@
<?php
class bwfan_Input_Html_Rule_Is_First_Order {
public function __construct() {
// vars
$this->type = 'Html_Rule_Is_First_Order';
$this->defaults = array(
'default_value' => '',
'class' => '',
'placeholder' => '',
);
}
public function render( $field, $value = null ) {
esc_html_e( 'This Funnel will initiate on very first order for the customer.', 'wp-marketing-automations' );
}
}

View File

@@ -0,0 +1,19 @@
<?php
class bwfan_Input_Html_Rule_Is_Guest {
public function __construct() {
// vars
$this->type = 'Html_Rule_Is_Guest';
$this->defaults = array(
'default_value' => '',
'class' => '',
'placeholder' => '',
);
}
public function render( $field, $value = null ) {
esc_html_e( 'This Funnel will initiate on guest orders.', 'wp-marketing-automations' );
}
}

View File

@@ -0,0 +1,19 @@
<?php
class bwfan_Input_Html_Rule_Is_Renewal {
public function __construct() {
// vars
$this->type = 'Html_Rule_Is_Renewal';
$this->defaults = array(
'default_value' => '',
'class' => '',
'placeholder' => '',
);
}
public function render( $field, $value = null ) {
esc_html_e( 'This Funnel will initiate on orders that are renewals.', 'wp-marketing-automations' );
}
}

View File

@@ -0,0 +1,19 @@
<?php
class bwfan_Input_Html_Rule_Is_Upgrade {
public function __construct() {
// vars
$this->type = 'Html_Rule_Is_Upgrade';
$this->defaults = array(
'default_value' => '',
'class' => '',
'placeholder' => '',
);
}
public function render( $field, $value = null ) {
esc_html_e( 'This Page will show on orders that have upgraded subscriptions.', 'wp-marketing-automations' );
}
}

View File

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

View File

@@ -0,0 +1,38 @@
<?php
class bwfan_Input_Order_State_Select {
public function __construct() {
// vars
$this->type = 'Order_State_Select';
$this->defaults = array(
'multiple' => 0,
'allow_null' => 0,
'choices' => array(),
'default_value' => array(),
'class' => '',
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
$chosen_states = $value;
?>
<select id="<?php echo esc_attr( $field['id'] ); ?>" name="<?php echo $field['name']; //phpcs:ignore WordPress.Security.EscapeOutput ?>[states][]" class="chosen_select <?php echo esc_attr( $field['class'] ); ?>" multiple="multiple" data-placeholder="<?php echo( isset( $field['placeholder'] ) ? esc_attr( sanitize_text_field( $field['placeholder'] ) ) : esc_html__( 'Search...', 'wp-marketing-automations' ) ); ?>">
<?php
WC()->countries->country_dropdown_options( '', $chosen_states );
?>
</select>
<?php
}
}
?>

View File

@@ -0,0 +1,40 @@
<?php
class bwfan_Input_Page_Select extends bwfan_Input_Text {
public function __construct() {
// vars
$this->type = 'Page_Select';
$this->defaults = array(
'multiple' => 0,
'allow_null' => 0,
'choices' => array(),
'default_value' => '',
'class' => 'ajax_chosen_select_products',
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
$args = array(
'name' => $field['name'],
'id' => $field['id'],
'sort_column' => 'menu_order',
'sort_order' => 'ASC',
'show_option_none' => ' ',
'class' => '',
'echo' => false,
'selected' => absint( $value ),
);
echo wp_dropdown_pages( $args ); //phpcs:ignore WordPress.Security.EscapeOutput
}
}

View File

@@ -0,0 +1,49 @@
<?php
class bwfan_Input_Product_Select {
public function __construct() {
// vars
$this->type = 'Product_Select';
$this->defaults = array(
'multiple' => 0,
'allow_null' => 0,
'choices' => array(),
'default_value' => '',
'class' => 'ajax_chosen_select_products',
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
?>
<select id="<?php echo esc_attr( $field['id'] ); ?>" name="<?php echo esc_attr( $field['name'] ); //phpcs:ignore WordPress.Security.EscapeOutput ?>[]" class="ajax_chosen_select_products" data-placeholder="<?php esc_html_e( 'Search for a product&hellip;', 'woocommerce' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch ?>">
<?php
$current = $value ? $value : array();
$product_ids = ! empty( $current ) ? array_map( 'absint', $current ) : null;
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
$product_name = BWFAN_WooCommerce_Compatibility::woocommerce_get_formatted_product_name( $product );
echo '<option value="' . esc_attr( $product_id ) . '" selected="selected">' . esc_html( $product_name ) . '</option>';
}
}
?>
</select>
<?php
}
}
?>

View File

@@ -0,0 +1,98 @@
<?php
#[AllowDynamicProperties]
class bwfan_Input_Select {
public function __construct() {
// vars
$this->type = 'Select';
$this->defaults = array(
'multiple' => 0,
'allow_null' => 0,
'choices' => array(),
'default_value' => '',
'class' => '',
'null_text' => __( '- Select -', 'wp-marketing-automations' ),
'disabled' => false,
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
$field['value'] = $value;
$optgroup = false;
// determine if choices are grouped (2 levels of array)
if ( is_array( $field['choices'] ) ) {
foreach ( $field['choices'] as $v ) {
if ( is_array( $v ) ) {
$optgroup = true;
}
}
}
// value must be array
if ( ! is_array( $field['value'] ) ) {
// perhaps this is a default value with new lines in it?
if ( ! is_null( $field['value'] ) && strpos( $field['value'], "\n" ) !== false ) {
// found multiple lines, explode it
$field['value'] = explode( "\n", $field['value'] );
} else {
$field['value'] = array( $field['value'] );
}
}
// trim value
$field['value'] = array_map( function ( $value ) {
return ! is_null( $value ) ? trim( $value ) : $value;
}, $field['value'] );
$multiple = '';
if ( $field['multiple'] ) {
$multiple = ' multiple="multiple" size="5" ';
$field['name'] .= '[]';
}
$disabled = '';
if ( true === $field['disabled'] ) {
$disabled = 'disabled';
}
echo '<select ' . esc_attr( $disabled ) . ' id="' . esc_attr( $field['id'] ) . '" class="' . esc_attr( $field['class'] ) . '" name="' . $field['name'] . '" ' . $multiple . ' >'; //phpcs:ignore WordPress.Security.EscapeOutput
// null
if ( $field['allow_null'] ) {
echo '<option value="null"> ' . esc_attr( $field['null_text'] ) . ' </option>';
}
// loop through values and add them as options
if ( is_array( $field['choices'] ) ) {
foreach ( $field['choices'] as $key => $value ) {
if ( $optgroup ) {
// this select is grouped with optgroup
if ( '' !== $key ) {
echo '<optgroup label="' . esc_attr( $key ) . '">';
}
if ( is_array( $value ) ) {
foreach ( $value as $id => $label ) {
$selected = in_array( $id, $field['value'], true ) ? 'selected="selected"' : '';
echo '<option value="' . esc_attr( $id ) . '" ' . $selected . '>' . esc_html( $label ) . '</option>'; //phpcs:ignore WordPress.Security.EscapeOutput
}
}
if ( '' !== $key ) {
echo '</optgroup>';
}
} else {
$selected = in_array( $key, $field['value'], true ) ? 'selected="selected"' : '';
echo '<option value="' . esc_attr( $key ) . '" ' . $selected . '>' . esc_html( $value ) . '</option>'; //phpcs:ignore WordPress.Security.EscapeOutput
}
}
}
echo '</select>';
}
}

View File

@@ -0,0 +1,39 @@
<?php
class bwfan_Input_Term_Select extends bwfan_Input_Text {
public function __construct() {
// vars
$this->type = 'Term_Select';
$this->defaults = array(
'multiple' => 0,
'allow_null' => 0,
'choices' => array(),
'default_value' => '',
'class' => '',
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
$args = array(
'name' => $field['name'],
'id' => $field['id'],
'show_option_none' => __( 'Select category', 'wp-marketing-automations' ),
'show_count' => 0,
'orderby' => 'name',
'echo' => 0,
'taxonomy' => 'product_cat',
'selected' => absint( $value ),
);
echo wp_dropdown_categories( $args ); //phpcs:ignore WordPress.Security.EscapeOutput
}
}

View File

@@ -0,0 +1,33 @@
<?php
#[AllowDynamicProperties]
class bwfan_Input_Text {
public function __construct() {
// vars
$this->type = 'Text';
$this->defaults = array(
'default_value' => '',
'class' => '',
'placeholder' => '',
'disabled' => false,
);
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
$disabled = '';
if ( true === $field['disabled'] ) {
$disabled = 'disabled';
}
/** Don't add any escaping method on name field as it will break the dynamic backbone string */
echo '<input ' . esc_attr( $disabled ) . ' name="' . $field['name'] . '" type="text" id="' . esc_attr( $field['id'] ) . '" class="' . esc_html( $field['class'] ) . '" placeholder="' . esc_html( $field['placeholder'] ) . '" value="' . $value . '" />'; //phpcs:ignore WordPress.Security.EscapeOutput, WordPress.Security.EscapeOutput
}
}

View File

@@ -0,0 +1,20 @@
<?php
class bwfan_Input_Time extends bwfan_Input_Text {
public function __construct() {
$this->type = 'Time';
parent::__construct();
}
public function render( $field, $value = null ) {
$field = array_merge( $this->defaults, $field );
if ( ! isset( $field['id'] ) ) {
$field['id'] = sanitize_title( $field['id'] );
}
$value = ( null === $value ) ? '00:00' : $value;
echo '<input placeholder="For eg: 23:59" name="' . $field['name'] . '" type="time" id="' . esc_attr( $field['id'] ) . '" class="bwfan-time-picker-field' . esc_attr( $field['class'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . $value . '" />'; //phpcs:ignore WordPress.Security.EscapeOutput, WordPress.Security.EscapeOutput
}
}

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' ),
);
}
}

View File

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

View File

@@ -0,0 +1,36 @@
<td class="remove">
<span>IF</span>
<a href="#" class="bwfan-remove-rule bwfan-button-remove">Delete</a>
</td>
<td class="rule-type">
<?php
$types = apply_filters( 'bwfan_rule_get_rule_types', array() );
// create field
$args = array(
'input' => 'select',
'name' => 'bwfan_rule[<%= groupId %>][<%= ruleId %>][rule_type]',
'class' => 'rule_type',
'choices' => $types,
'allow_null' => true,
'null_text' => __( 'Select a rule', 'wp-marketing-automations' ),
);
?>
<select id="" class="rule_type" name="bwfan_rule[<%= groupId %>][<%= ruleId %>][rule_type]">
<option value="null"> Select a rule</option>
<% _(ruleSet).each(function(k,v) { %>
<optgroup label="<%= typeof rulesGroups[v] !== 'undefined' ? rulesGroups[v].title : v %>">
<% _(k).each(function(k,v) { %>
<option value="<%= v %>"><%= k %></option>
<% }) %>
</optgroup>
<% }) %>
</select>
</td>
<td class="loading" colspan="2" style="display:none;"><?php esc_html_e( 'Loading...', 'wp-marketing-automations' ); ?></td>
<td class="add"><a href="#" class="bwfan-add-rule button"><?php esc_html_e( 'AND', 'wp-marketing-automations' ); ?></a></td>

View File

@@ -0,0 +1,91 @@
<?php
$automation_id = BWFAN_Core()->automations->get_automation_id();
$groups = [];
if ( empty( $groups ) ) {
$default_rule_id = 'rule' . uniqid();
$groups = array(
'group' . ( time() ) => array(
$default_rule_id => array(
'rule_type' => 'general_always',
'operator' => '==',
'condition' => '',
),
),
);
}
?>
<div class="bwfan-rules-builder woocommerce_options_panel">
<div class="label">
<h4><?php esc_html_e( 'Advanced Rules', 'wp-marketing-automations' ); ?></h4>
</div>
<div id="bwfan-rules-groups" class="bwfan_rules_common">
<div class="bwfan-rule-group-target">
<?php if ( is_array( $groups ) ) : ?>
<?php
$group_counter = 0;
foreach ( $groups as $group_id => $group ) :
if ( empty( $group_id ) ) {
$group_id = 'group' . $group_id;
}
?>
<div class="bwfan-rule-group-container" data-groupid="<?php echo esc_attr( $group_id ); ?>">
<div class="bwfan-rule-group-header">
<?php if ( 0 === $group_counter ) : ?>
<h4><?php esc_html_e( 'Initiate this automation when these conditions are matched:', 'wp-marketing-automations' ); ?></h4>
<?php endif; ?>
</div>
<?php
if ( is_array( $group ) ) :
?>
<table class="bwfan-rules" data-groupid="<?php echo esc_attr( $group_id ); ?>">
<tbody>
<?php
foreach ( $group as $rule_id => $rule ) :
if ( empty( $rule_id ) ) {
$rule_id = 'rule' . $rule_id;
}
?>
<tr data-ruleid="<?php echo esc_attr( $rule_id ); ?>" class="bwfan-rule">
<td class="rule-type">
<?php
// allow custom location rules
$types = apply_filters( 'bwfan_rule_get_rule_types', array() );
// create field
$args = array(
'input' => 'select',
'name' => 'bwfan_rule[' . $group_id . '][' . $rule_id . '][rule_type]',
'class' => 'rule_type',
'choices' => $types,
'allow_null' => true,
'null_text' => __( 'Select a rule', 'wp-marketing-automations' ),
);
bwfan_Input_Builder::create_input_field( $args, $rule['rule_type'] );
?>
</td>
<td class="condition"></td>
<td class="loading" colspan="2" style="display:none;"><?php esc_html_e( 'Loading...', 'wp-marketing-automations' ); ?></td>
<td class="add">
<a href="#"
class="bwfan-add-rule button"><?php esc_html_e( 'AND', 'wp-marketing-automations' ); ?></a>
</td>
<td class="remove">
<a href="#" class="bwfan-remove-rule bwfan-button-remove"
title="<?php esc_attr_e( 'Remove condition', 'wp-marketing-automations' ); ?>"></a>
</td>
</tr><?php endforeach; ?></tbody>
</table>
<?php endif; ?>
</div>
<?php $group_counter ++; ?>
<?php endforeach; ?>
</div>
<button class="button button-primary bwfan-add-rule-group" title="<?php esc_attr_e( 'Add a set of conditions', 'wp-marketing-automations' ); ?>"><?php esc_html_e( 'OR', 'wp-marketing-automations' ); ?></button>
<?php endif; ?>
</div>
</div>