PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.25
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.25
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / controllers / FrmOverlayController.php

FrmOverlayController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.25, at classes/controllers/FrmOverlayController.php

186 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmOverlayController {
7
8 /**
9 * Handle name used for registering controller scripts and style.
10 *
11 * @var string Handle name used for wp_register_script|wp_register_style
12 */
13 public static $assets_handle_name = 'formidable-overlay';
14
15 /**
16 * Check if the overlay will run again periodically.
17 *
18 * @var bool If the condition is true, the overlay will execute only following a specified time interval.
19 */
20 private $recurring_execution = false;
21
22 /**
23 * The controller configs passed through construct. If empty the overlay will run normally, it will open every time when open_overlay is called.
24 *
25 * @var array Config {
26 *
27 * @type string $execution-frequency Example values: '1 day', '10 days', '1 week', etc.
28 * @type string $config-option-name A handle name that will be used to store the controller options_data.
29 *
30 * }
31 */
32 private $config = array();
33
34 /**
35 * The WordPress option meta name that will store the overlay options.
36 *
37 * @var string
38 */
39 private $option_meta_name = 'frm-overlay-options';
40
41 /**
42 * The controller data. It will handle the options from multiple instances of the controller
43 *
44 * @var array Option data {
45 * @type array $options_data[ '[instance1-option_name_provided_via-->config-option-name]' ] {
46 * @type string $execution-frequency Values example: '1 day' | '10 days' | '1 week' | '10 weeks' | '1 month' | '10 months' | '1 year' | '10 years'
47 * }
48 *
49 * @type array $options_data[ '[instance2-option_name_provided_via-->config-option-name]' ] {
50 * @type string $execution-frequency Values example: '1 day' | '10 days' | '1 week' | '10 weeks' | '1 month' | '10 months' | '1 year' | '10 years'
51 * }
52 * }
53 */
54 private $options_data = array();
55
56 public function __construct( $config = array() ) {
57 if ( ! isset( $config['config-option-name'] ) || ! isset( $config['execution-frequency'] ) ) {
58 return;
59 }
60
61 $this->recurring_execution = true;
62 $this->config = $config;
63
64 $this->get_options_data();
65 }
66
67 /**
68 * Get next execution time. Return the next execution timestamp and date.
69 *
70 * @return array
71 */
72 private function get_next_execution() {
73 $next_timestamp = strtotime( '+' . $this->config['execution-frequency'], $this->get_time() );
74 return array(
75 'timestamp' => $next_timestamp,
76 'date' => gmdate( 'Y-m-d H:i:s', $next_timestamp ),
77 );
78 }
79
80 /**
81 * Revise the $options_data by incorporating information about the upcoming execution time, then proceed to store it in the database.
82 *
83 * @return void
84 */
85 private function update_next_execution_time() {
86 $config_option_name = $this->config['config-option-name'];
87
88 $this->options_data[ $config_option_name ] = $this->get_next_execution();
89 $this->update_options_data();
90 }
91
92 /**
93 * Get the time.
94 *
95 * @return int
96 */
97 protected function get_time() {
98 return time();
99 }
100
101 /**
102 * Ascertain whether the overlay needs to be executed or not.
103 *
104 * @return bool
105 */
106 private function is_time_to_execute() {
107 if ( ! isset( $this->options_data[ $this->config['config-option-name'] ] ) ) {
108 return true;
109 }
110
111 $options = $this->options_data[ $this->config['config-option-name'] ];
112
113 return ! ( isset( $options['timestamp'] ) && (int) $options['timestamp'] > $this->get_time() );
114 }
115
116 /**
117 * Get the $options_data.
118 *
119 * @return void
120 */
121 private function get_options_data() {
122 $this->options_data = get_option( $this->option_meta_name, array() );
123 }
124
125 /**
126 * Save the $options_data to db.
127 *
128 * @return void
129 */
130 private function update_options_data() {
131 update_option( $this->option_meta_name, $this->options_data, 'no' );
132 }
133
134 /**
135 * Open overlay.
136 *
137 * @param array $data {
138 * An array containing data for the overlay.
139 *
140 * @type string $hero_image URL of the hero image.
141 * @type string $heading Heading of the overlay.
142 * @type string $copy Copy/content of the overlay.
143 * @type array $buttons Array of button arrays.
144 * @type string $buttons[]['url'] URL for the button.
145 * @type string $buttons[]['target'] Target attribute for the button link.
146 * @type string $buttons[]['label'] Label/text of the button.
147 * }
148 * @return bool
149 */
150 public function open_overlay( $data = array() ) {
151 if ( true === $this->recurring_execution ) {
152 if ( false === $this->is_time_to_execute() ) {
153 return false;
154 }
155 $this->update_next_execution_time();
156 }
157
158 $this->enqueue_assets();
159 $inline_script = 'frmOverlay.open(' . wp_json_encode( $data ) . ')';
160 wp_add_inline_script( self::$assets_handle_name, $inline_script, 'after' );
161
162 return true;
163 }
164
165 /**
166 * Register controller assets.
167 *
168 * @return void
169 */
170 public static function register_assets() {
171 wp_register_script( self::$assets_handle_name, FrmAppHelper::plugin_url() . '/js/formidable_overlay.js', array(), FrmAppHelper::plugin_version(), true );
172 wp_register_style( self::$assets_handle_name, FrmAppHelper::plugin_url() . '/css/frm_overlay.css', array(), FrmAppHelper::plugin_version() );
173 }
174
175 /**
176 * Enqueue controller assets.
177 *
178 * @return void
179 */
180 private function enqueue_assets() {
181 wp_enqueue_style( 'formidable-animations' );
182 wp_enqueue_style( self::$assets_handle_name );
183 wp_enqueue_script( self::$assets_handle_name );
184 }
185 }
186