| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Contact_Segmentation; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Conditions |
| 9 |
* |
| 10 |
* @package Sellkit\Contact_Segmentation\Base |
| 11 |
* @since 1.1.0 |
| 12 |
*/ |
| 13 |
class Conditions { |
| 14 |
|
| 15 |
/** |
| 16 |
* Operations array, the key is the conditions name and value is the operators name. |
| 17 |
* |
| 18 |
* @var array |
| 19 |
* @since 1.1.0 |
| 20 |
*/ |
| 21 |
public static $operators = []; |
| 22 |
|
| 23 |
/** |
| 24 |
* The key is the conditions name and value is the condition's data. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
* @since 1.1.0 |
| 28 |
*/ |
| 29 |
public static $data = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* Conditions instances, the key is conditions name the value is the condition instance. |
| 33 |
* |
| 34 |
* @var array |
| 35 |
* @since 1.1.0 |
| 36 |
*/ |
| 37 |
public static $conditions = []; |
| 38 |
|
| 39 |
/** |
| 40 |
* Conditions constructor. |
| 41 |
* |
| 42 |
* @since 1.1.0 |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
$this->load_conditions(); |
| 46 |
|
| 47 |
add_action( 'wp_ajax_sellkit_conditions_get_options', [ $this, 'get_options' ] ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Loads all of the conditions. |
| 52 |
* |
| 53 |
* @since 1.1.0 |
| 54 |
*/ |
| 55 |
public function load_conditions() { |
| 56 |
sellkit()->load_files( [ |
| 57 |
'contact-segmentation/conditions/condition-base', |
| 58 |
] ); |
| 59 |
|
| 60 |
$path = trailingslashit( sellkit()->plugin_dir() . 'includes/contact-segmentation/conditions' ); |
| 61 |
$file_paths = glob( $path . '*.php' ); |
| 62 |
|
| 63 |
foreach ( $file_paths as $file_path ) { |
| 64 |
if ( ! file_exists( $file_path ) ) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
require_once $file_path; |
| 69 |
|
| 70 |
$file_name = str_replace( '.php', '', basename( $file_path ) ); |
| 71 |
$condition_class = str_replace( '-', ' ', $file_name ); |
| 72 |
$condition_class = str_replace( ' ', '_', ucwords( $condition_class ) ); |
| 73 |
$condition_class = "Sellkit\Contact_Segmentation\Conditions\\{$condition_class}"; |
| 74 |
|
| 75 |
if ( ! class_exists( $condition_class ) || 'condition-base' === $file_name ) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
$condition = new $condition_class(); |
| 80 |
|
| 81 |
if ( false === $condition->is_active() ) { |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
self::$conditions[ $condition->get_name() ] = $condition; |
| 86 |
self::$data[ $condition->get_name() ] = [ |
| 87 |
'title' => $condition->get_title(), |
| 88 |
'type' => $condition->get_type(), |
| 89 |
'isSearchable' => $condition->is_searchable(), |
| 90 |
'openMenuOnClick' => method_exists( $condition, 'open_menu_on_click' ) ? $condition->open_menu_on_click() : false, |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
self::$conditions = apply_filters( 'sellkit_contact_segmentation_conditions', self::$conditions ); |
| 95 |
self::$data = apply_filters( 'sellkit_contact_segmentation_conditions_data', self::$data ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Getting all conditions names. |
| 100 |
* |
| 101 |
* @return array |
| 102 |
* @since 1.1.0 |
| 103 |
*/ |
| 104 |
public function get_names() { |
| 105 |
return self::$names; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Gets all operators based on the conditions name. |
| 110 |
* |
| 111 |
* @since 1.1.0 |
| 112 |
* @param string $condition_name Condition name. |
| 113 |
* @return mixed |
| 114 |
*/ |
| 115 |
public function get_condition( $condition_name ) { |
| 116 |
return self::$conditions[ $condition_name ]; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Check condition validation. |
| 121 |
* |
| 122 |
* @since 1.1.0 |
| 123 |
* @param string $condition_name Condition name. |
| 124 |
* @param string $operator_name Condition operator. |
| 125 |
* @param mixed $condition_value Condition value. |
| 126 |
* @return bool|void|\WP_Error |
| 127 |
*/ |
| 128 |
public static function match( $condition_name, $operator_name, $condition_value ) { |
| 129 |
if ( is_admin() && ! wp_doing_ajax() ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
$condition = self::$conditions[ $condition_name ]; |
| 134 |
$operator = Operators::$operators[ $operator_name ]; |
| 135 |
$extracted_value = $condition->get_value(); |
| 136 |
|
| 137 |
if ( ! is_user_logged_in() && ! isset( $extracted_value ) ) { |
| 138 |
return new \WP_Error( 'conditions_has_no_value', __( 'Condition has no value', 'sellkit' ) ); |
| 139 |
} |
| 140 |
|
| 141 |
if ( method_exists( $condition, 'is_valid' ) ) { |
| 142 |
return $condition->is_valid( $condition_value, $operator_name ); |
| 143 |
} |
| 144 |
|
| 145 |
if ( empty( $operator->is_valid( $condition->get_value(), $condition_value ) ) ) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get condition's options. |
| 154 |
* |
| 155 |
* @since 1.1.0 |
| 156 |
*/ |
| 157 |
public function get_options() { |
| 158 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 159 |
|
| 160 |
$condition = sellkit_htmlspecialchars( INPUT_GET, 'condition' ); |
| 161 |
|
| 162 |
if ( empty( $condition ) ) { |
| 163 |
wp_send_json_error( __( 'Please send a condition name', 'sellkit' ) ); |
| 164 |
} |
| 165 |
|
| 166 |
$options = $this->get_condition( $condition )->get_options(); |
| 167 |
|
| 168 |
wp_send_json_success( $options ); |
| 169 |
} |
| 170 |
} |
| 171 |
|