PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 4.7.1.1
Hustle – Email Marketing, Lead Generation, Optins, Popups v4.7.1.1
7.8.14.2 7.8.14 7.8.14.1 7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / inc / class-popup-rule.php
wordpress-popup / inc Last commit date
addons 11 years ago external 11 years ago rules 11 years ago class-popup-admin.php 11 years ago class-popup-base.php 11 years ago class-popup-database.php 11 years ago class-popup-help.php 11 years ago class-popup-item.php 11 years ago class-popup-posttype.php 11 years ago class-popup-public.php 11 years ago class-popup-rule.php 11 years ago config-defaults.php 11 years ago
class-popup-rule.php
452 lines
1 <?php
2
3 /**
4 * Rule-Collection
5 */
6 class IncPopupRules {
7 // List of classes.
8 static public $classes = array();
9
10 // List of all rules.
11 static public $rules = array();
12
13 /**
14 * Register a new rule class.
15 *
16 * @since 4.6
17 * @param string $classname Class-name (not object!)
18 */
19 static public function register( $classname ) {
20 self::$classes[$classname] = new $classname();
21 }
22
23 /**
24 * Checks which php file defines the specified rule-ID
25 *
26 * @since 4.6
27 * @param string $key Rule-ID.
28 * @return string filename of the rule-file.
29 */
30 static public function file_for_rule( $key ) {
31 $file = '';
32
33 foreach ( self::$rules as $prio => $list ) {
34 if ( isset( $list[ $key ] ) ) {
35 $file = $list[ $key ]->filename;
36 break;
37 }
38 }
39
40 return $file;
41 }
42
43 /**
44 * Registers a rule.
45 *
46 * @since 4.6
47 * @param IncPopupRule $obj
48 * @param string $filename
49 * @param string $id
50 * @param string $label
51 * @param string $description
52 * @param string $exclude Optional. Rule-ID that is excluded when this
53 * rule is activated.
54 * @param int $priority Defines if the rule is displayed in the top of
55 * the list (0) or the bottom (100)
56 */
57 static public function add_rule( $obj, $filename, $id, $label, $description, $exclude = '', $priority = 10 ) {
58 if ( ! isset( self::$rules[ $priority ] ) ) {
59 self::$rules[ $priority ] = array();
60 }
61
62 self::$rules[$priority][$id] = (object) array(
63 'obj' => $obj,
64 'filename' => $filename,
65 'label' => $label,
66 'description' => $description,
67 'exclude' => $exclude,
68 );
69
70 ksort( self::$rules, SORT_NUMERIC );
71 }
72
73 /**
74 * Register hooks.
75 *
76 * @since 1.0.0
77 */
78 static public function init() {
79 add_filter(
80 'popup-rule-label',
81 array( __CLASS__, '_label' ),
82 10, 2
83 );
84
85 add_action(
86 'popup-rule-forms',
87 array( __CLASS__, '_admin_rule_form' ),
88 10, 1
89 );
90
91 add_action(
92 'popup-rule-switch',
93 array( __CLASS__, '_admin_rule_list' ),
94 10, 1
95 );
96
97 add_filter(
98 'popup-save-rules',
99 array( __CLASS__, '_save' ),
100 10, 1
101 );
102
103 add_filter(
104 'popup-apply-rules',
105 array( __CLASS__, '_apply' ),
106 10, 2
107 );
108 }
109
110
111 /*----- INTERNAL FUNCTIONS (handlers) ------*/
112
113
114 /**
115 * Filter that returns the rule name if $key is the current rule-ID.
116 * Handles filter `popup-rule-label`
117 *
118 * @since 1.0.0
119 */
120 static public function _label( $rule, $key ) {
121 foreach ( self::$rules as $prio => $list ) {
122 if ( isset( $list[ $key ] ) ) {
123 return $list[ $key ]->label;
124 }
125 }
126 return '';
127 }
128
129 /**
130 * Apply the rule-logic to the specified popup
131 *
132 * @since 1.0.0
133 * @param bool $show Current decission whether popup should be displayed.
134 * @param Object $popup The popup that is evaluated.
135 * @return bool Updated decission to display popup or not.
136 */
137 static public function _apply( $show, $popup ) {
138 if ( ! $show ) { return false; }
139
140 foreach ( self::$rules as $prio => $list ) {
141 foreach ( $list as $key => $rule ) {
142 if ( ! $popup->uses_rule( $key ) ) { continue; }
143
144 if ( ! $rule->obj->_apply( $key, $show, $popup ) ) {
145 return false;
146 }
147 }
148 }
149 return true;
150 }
151
152 /**
153 * Update and return the $settings array to save the form values.
154 *
155 * @since 1.0.0
156 * @param array $data Collection of rule-settings.
157 * @return array The updated rule-settings collection.
158 */
159 static public function _save( $data ) {
160 $data = isset( $_POST['po_rule_data'] ) ? $_POST['po_rule_data'] : array();
161
162 foreach ( self::$rules as $prio => $list ) {
163 foreach ( $list as $key => $rule ) {
164 $data = $rule->obj->_save( $key, $data );
165 }
166 }
167
168 return $data;
169 }
170
171 /**
172 * Display the rule form inside the "active rules" list
173 *
174 * @since 1.0.0
175 * @param InPopupItem $popup
176 */
177 static public function _admin_rule_form( $popup ) {
178 foreach ( self::$rules as $prio => $list ) {
179 foreach ( $list as $key => $rule ) {
180 $rule->obj->_admin_rule_form( $key, $rule, $popup );
181 }
182 }
183 }
184
185 /**
186 * Display the rule-switch in the "all rules" list (no options, only a
187 * function to activate/deactivate the rule)
188 *
189 * @since 1.0.0
190 */
191 static public function _admin_rule_list( $popup ) {
192 foreach ( self::$rules as $prio => $list ) {
193 foreach ( $list as $key => $rule ) {
194 $rule->obj->_admin_rule_list( $key, $rule, $popup );
195 }
196 }
197 }
198 }
199
200 IncPopupRules::init();
201
202
203 /*===============================*\
204 ===================================
205 == ==
206 == RULE BASE ==
207 == ==
208 ===================================
209 \*===============================*/
210
211
212 /**
213 * Base class for PopUp conditions
214 */
215 abstract class IncPopupRule {
216
217 /**
218 * Name of the file (set by the child class)
219 * @var string
220 */
221 public $filename = '';
222
223 /*--------------- OVERWRITABLE functions ----------------*/
224
225 /**
226 * Initialize the rule object.
227 * Overwrite this function!
228 *
229 * @since 4.6
230 */
231 protected function init() {
232 /*
233 // Register new rule
234 $this->add_rule( 'id', 'label', 'description' );
235
236 // Add custom hooks for the rule
237 add_action( 'wp_footer', array( $this, 'footer_code' ) );
238 */
239 }
240
241 /**
242 * Apply the rule-logic to the specified popup:
243 * Create a function with name "apply_" + rule-ID
244 *
245 * Example:
246 * protected function apply_url( $data ) {
247 * return true; // Condition for rule "url"
248 * }
249 *
250 * @since 4.6
251 * @param mixed $data Rule-data which was saved via the save_() handler.
252 * @return bool Decission to display popup or not.
253 */
254 protected function apply_( $data ) {
255 return true;
256 }
257
258 /**
259 * Output the Admin-Form for a single rule:
260 * Create a function with name "form_" + rule-ID
261 *
262 * Example:
263 * protected function form_url( $data ) {
264 * echo 'Form for rule "url"...';
265 * }
266 *
267 * @since 4.6
268 * @param mixed $data Rule-data which was saved via the save_() handler.
269 */
270 protected function form_( $data ) {
271 echo '';
272 }
273
274 /**
275 * Update and return rule-config which should be saved in DB.
276 * Create a function with name "save_" + rule-ID
277 *
278 * Example:
279 * protected function save_url() {
280 * return array();
281 * }
282 *
283 * @since 4.6
284 * @param array $data The contents of $_POST['po_rule_data'].
285 * @return mixed Data collection of this rule.
286 */
287 protected function save_( $data ) {
288 return false;
289 }
290
291 /*--------------- PUBLIC functions ----------------*/
292
293 /**
294 * Registers a rule.
295 *
296 * @since 4.6
297 * @param string $id
298 * @param string $label
299 * @param string $description
300 * @param string $exclude
301 * @param int $priority
302 */
303 protected function add_rule( $id, $label, $description, $exclude = '', $priority = 10 ) {
304 IncPopupRules::add_rule(
305 $this,
306 $this->filename,
307 $id, $label, $description, $exclude, $priority
308 );
309 }
310
311 /*--------------- INTERNAL functions ----------------*/
312
313 /**
314 * Create the rule object.
315 * This is _only_ done by IncPopupRules::register() above!
316 *
317 * @since 1.0.0
318 */
319 public function __construct() {
320 $this->init();
321 }
322
323 /**
324 * Apply the rule-logic to the specified popup
325 *
326 * @since 1.0.0
327 * @param bool $show Current decission whether popup should be displayed.
328 * @param Object $popup The popup that is evaluated.
329 * @return bool Updated decission to display popup or not.
330 */
331 public function _apply( $key, $show, $popup ) {
332 if ( ! $show ) { return $show; }
333
334 // Skip the rule if the popup does not use it.
335 if ( ! in_array( $key, $popup->rule ) ) { return; }
336
337 $method = 'apply_' . $key;
338 if ( method_exists( $this, $method ) ) {
339 if ( isset( $popup->rule_data[$key] ) ) {
340 $data = $popup->rule_data[$key];
341 } else {
342 $data = '';
343 }
344
345 if ( ! $this->$method( $data, $popup ) ) {
346 $show = false;
347 }
348 }
349
350 return $show;
351 }
352
353 /**
354 * Output the Admin-Form for the active rule.
355 *
356 * @since 1.0.0
357 * @param Object $popup The popup that is edited.
358 * @param string $key Rule-ID.
359 */
360 public function _form( $popup, $key ) {
361 $method = 'form_' . $key;
362 if ( method_exists( $this, $method ) ) {
363 if ( isset( $popup->rule_data[$key] ) ) {
364 $data = $popup->rule_data[$key];
365 } else {
366 $data = '';
367 }
368
369 echo '<div class="rule-form">';
370 $this->$method( $data );
371 echo '</div>';
372 }
373 }
374
375 /**
376 * Update and return the $settings array to save the form values.
377 *
378 * @since 1.0.0
379 * @param array $data Collection of rule-settings.
380 * @return array The updated rule-settings collection.
381 */
382 public function _save( $key, $data ) {
383 $method = 'save_' . $key;
384 $data = lib2()->array->get( $data );
385
386 if ( method_exists( $this, $method ) ) {
387 $data[$key] = $this->$method($data);
388 }
389
390 return $data;
391 }
392
393 /**
394 * Display the rule form inside the "active rules" list
395 *
396 * @since 1.0.0
397 * @param InPopupItem $popup
398 */
399 public function _admin_rule_form( $key, $data, $popup ) {
400 $active = $popup->uses_rule( $key );
401 $class = $active ? 'on' : 'off';
402 ?>
403 <li id="po-rule-<?php echo esc_attr( $key ); ?>"
404 class="rule <?php echo esc_attr( $class )?>"
405 data-key="<?php echo esc_attr( $key ); ?>">
406 <div class="rule-title">
407 <?php echo esc_html( $data->label ); ?>
408 </div>
409 <span class="rule-toggle dashicons"></span>
410 <div class="rule-inner">
411 <div class="rule-description">
412 <em><?php echo $data->description; ?></em>
413 </div>
414 <?php $this->_form( $popup, $key ); ?>
415 </div>
416 </li>
417 <?php
418 }
419
420 /**
421 * Display the rule-switch in the "all rules" list (no options, only a
422 * function to activate/deactivate the rule)
423 *
424 * @since 1.0.0
425 */
426 public function _admin_rule_list( $key, $data, $popup ) {
427 $active = $popup->uses_rule( $key );
428 $class = $active ? 'on' : 'off';
429 $exclude = isset( $data->exclude ) ? $data->exclude : '';
430 ?>
431 <li class="rule rule-<?php echo esc_attr( $key ); ?> <?php echo esc_attr( $class ); ?>">
432 <div class="wpmui-toggle">
433 <input type="checkbox"
434 class="wpmui-toggle-checkbox"
435 id="rule-<?php echo esc_attr( $key ); ?>"
436 data-form="#po-rule-<?php echo esc_attr( $key ); ?>"
437 data-exclude="<?php echo esc_attr( $exclude ); ?>"
438 name="po_rule[]"
439 value="<?php echo esc_attr( $key ); ?>"
440 <?php checked( $active ); ?> />
441 <label class="wpmui-toggle-label" for="rule-<?php echo esc_attr( $key ); ?>">
442 <span class="wpmui-toggle-inner"></span>
443 <span class="wpmui-toggle-switch"></span>
444 </label>
445 </div>
446 <?php echo esc_html( $data->label ); ?>
447 </li>
448 <?php
449 }
450
451 };
452