PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.33
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.33
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.33, at classes/controllers/FrmOverlayController.php

190 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 *
46 * @type array $options_data[ '[instance1-option_name_provided_via-->config-option-name]' ] {
47 * @type string $execution-frequency Values example: '1 day' | '10 days' | '1 week' | '10 weeks' | '1 month' | '10 months' | '1 year' | '10 years'
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 /**
57 * @param array $config
58 */
59 public function __construct( $config = array() ) {
60 if ( ! isset( $config['config-option-name'] ) || ! isset( $config['execution-frequency'] ) ) {
61 return;
62 }
63
64 $this->recurring_execution = true;
65 $this->config = $config;
66
67 $this->get_options_data();
68 }
69
70 /**
71 * Get next execution time. Return the next execution timestamp and date.
72 *
73 * @return array
74 */
75 private function get_next_execution() {
76 $next_timestamp = strtotime( '+' . $this->config['execution-frequency'], $this->get_time() );
77 return array(
78 'timestamp' => $next_timestamp,
79 'date' => gmdate( 'Y-m-d H:i:s', $next_timestamp ),
80 );
81 }
82
83 /**
84 * Revise the $options_data by incorporating information about the upcoming execution time, then proceed to store it in the database.
85 *
86 * @return void
87 */
88 private function update_next_execution_time() {
89 $config_option_name = $this->config['config-option-name'];
90
91 $this->options_data[ $config_option_name ] = $this->get_next_execution();
92 $this->update_options_data();
93 }
94
95 /**
96 * Get the time.
97 *
98 * @return int
99 */
100 protected function get_time() {
101 return time();
102 }
103
104 /**
105 * Ascertain whether the overlay needs to be executed or not.
106 *
107 * @return bool
108 */
109 private function is_time_to_execute() {
110 if ( ! isset( $this->options_data[ $this->config['config-option-name'] ] ) ) {
111 return true;
112 }
113
114 $options = $this->options_data[ $this->config['config-option-name'] ];
115
116 return ! ( isset( $options['timestamp'] ) && (int) $options['timestamp'] > $this->get_time() );
117 }
118
119 /**
120 * Get the $options_data.
121 *
122 * @return void
123 */
124 private function get_options_data() {
125 $this->options_data = get_option( $this->option_meta_name, array() );
126 }
127
128 /**
129 * Save the $options_data to db.
130 *
131 * @return void
132 */
133 private function update_options_data() {
134 update_option( $this->option_meta_name, $this->options_data, false );
135 }
136
137 /**
138 * Open overlay.
139 *
140 * @param array $data {
141 * An array containing data for the overlay.
142 *
143 * @type string $hero_image URL of the hero image.
144 * @type string $heading Heading of the overlay.
145 * @type string $copy Copy/content of the overlay.
146 * @type array $buttons Array of button arrays.
147 * @type string $buttons[]['url'] URL for the button.
148 * @type string $buttons[]['target'] Target attribute for the button link.
149 * @type string $buttons[]['label'] Label/text of the button.
150 * }
151 *
152 * @return bool
153 */
154 public function open_overlay( $data = array() ) {
155 if ( true === $this->recurring_execution ) {
156 if ( false === $this->is_time_to_execute() ) {
157 return false;
158 }
159 $this->update_next_execution_time();
160 }
161
162 $this->enqueue_assets();
163 $inline_script = 'frmOverlay.open(' . wp_json_encode( $data ) . ')';
164 wp_add_inline_script( self::$assets_handle_name, $inline_script, 'after' );
165
166 return true;
167 }
168
169 /**
170 * Register controller assets.
171 *
172 * @return void
173 */
174 public static function register_assets() {
175 wp_register_script( self::$assets_handle_name, FrmAppHelper::plugin_url() . '/js/formidable_overlay.js', array(), FrmAppHelper::plugin_version(), true );
176 wp_register_style( self::$assets_handle_name, FrmAppHelper::plugin_url() . '/css/frm_overlay.css', array(), FrmAppHelper::plugin_version() );
177 }
178
179 /**
180 * Enqueue controller assets.
181 *
182 * @return void
183 */
184 private function enqueue_assets() {
185 wp_enqueue_style( 'formidable-animations' );
186 wp_enqueue_style( self::$assets_handle_name );
187 wp_enqueue_script( self::$assets_handle_name );
188 }
189 }
190