PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / contact-segmentation / conditions.php

conditions.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/contact-segmentation/conditions.php

175 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( ! class_exists( 'Operators' ) ) {
134 require_once __DIR__ . '/operators.php';
135 }
136
137 $condition = self::$conditions[ $condition_name ];
138 $operator = Operators::$operators[ $operator_name ];
139 $extracted_value = $condition->get_value();
140
141 if ( ! is_user_logged_in() && ! isset( $extracted_value ) ) {
142 return new \WP_Error( 'conditions_has_no_value', __( 'Condition has no value', 'sellkit' ) );
143 }
144
145 if ( method_exists( $condition, 'is_valid' ) ) {
146 return $condition->is_valid( $condition_value, $operator_name );
147 }
148
149 if ( empty( $operator->is_valid( $condition->get_value(), $condition_value ) ) ) {
150 return false;
151 }
152
153 return true;
154 }
155
156 /**
157 * Get condition's options.
158 *
159 * @since 1.1.0
160 */
161 public function get_options() {
162 check_ajax_referer( 'sellkit', 'nonce' );
163
164 $condition = sellkit_htmlspecialchars( INPUT_GET, 'condition' );
165
166 if ( empty( $condition ) ) {
167 wp_send_json_error( __( 'Please send a condition name', 'sellkit' ) );
168 }
169
170 $options = $this->get_condition( $condition )->get_options();
171
172 wp_send_json_success( $options );
173 }
174 }
175