PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.25.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.25.1
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 / FrmTestModeController.php

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

377 lines 10.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 /**
7 * @since 6.25
8 */
9 class FrmTestModeController {
10
11 /**
12 * Maybe add the test mode container.
13 *
14 * @since 6.25
15 *
16 * @param string $html
17 * @return string
18 */
19 public static function maybe_add_test_mode_container( $html ) {
20 if ( '' === $html || ! self::should_add_test_mode_container() ) {
21 return $html;
22 }
23
24 /**
25 * @since 6.25
26 */
27 do_action( 'frm_test_mode_container' );
28
29 if ( false !== strpos( $html, '<div class="frm_form_fields' ) ) {
30 $html = preg_replace(
31 '/<div class="frm_form_fields/',
32 self::get_testing_mode_container() . '<div class="frm_form_fields',
33 $html,
34 1
35 );
36 } else {
37 // If there no form, add before an error message.
38 $html = '<div class="with_frm_style">' . self::get_testing_mode_container() . '</div>' . $html;
39 }
40
41 return $html;
42 }
43
44 /**
45 * @since 6.25
46 *
47 * @return bool
48 */
49 public static function should_add_test_mode_container() {
50 if ( ! current_user_can( 'frm_edit_forms' ) ) {
51 return false;
52 }
53
54 /**
55 * Filter this so the add-on can enable it when applicable in other cases (like when submitting with AJAX).
56 *
57 * @since 6.25
58 *
59 * @param bool $is_test_mode
60 */
61 return (bool) apply_filters( 'frm_test_mode', (bool) FrmAppHelper::simple_get( 'testmode' ) );
62 }
63
64 /**
65 * Get the testing mode container.
66 *
67 * @since 6.25
68 *
69 * @return string
70 */
71 private static function get_testing_mode_container() {
72 return FrmAppHelper::clip(
73 function () {
74 self::render_testing_mode_container();
75 }
76 );
77 }
78
79 /**
80 * Render the testing mode container.
81 *
82 * @since 6.25
83 *
84 * @return void
85 */
86 private static function render_testing_mode_container() {
87 $form_key = self::get_form_key_from_request();
88 if ( ! $form_key ) {
89 return;
90 }
91
92 $form = FrmForm::getOne( $form_key );
93 if ( ! $form ) {
94 return;
95 }
96
97 if ( ! empty( $form->options['chat'] ) ) {
98 echo '<div class="frm_note_style">' . esc_html__( 'Test Mode is currently not supported for conversational forms.', 'formidable' ) . '</div>';
99 return;
100 }
101
102 $enabled = self::test_mode_addon_exists();
103 $ai_enabled = class_exists( 'FrmAIAppHelper' );
104 $roles = self::get_roles();
105 $selected_role = self::get_selected_role();
106 $pagination = apply_filters( 'frm_test_mode_pagination_buttons', false );
107 $disabled_required_fields_toggle_args = self::get_disabled_required_fields_toggle_args();
108 $show_all_hidden_fields_toggle_args = self::get_show_all_hidden_fields_toggle_args();
109 $form_id = is_numeric( $form_key ) ? $form_key : FrmForm::get_id_by_key( $form_key );
110 $should_show_upsell = self::should_show_upsell();
111 $should_suggest_test_mode_install = ! $enabled && ! $should_show_upsell;
112 $should_suggest_ai_install = $enabled && ! $ai_enabled;
113 $should_show_warning = $should_suggest_test_mode_install || $should_suggest_ai_install;
114 $form_actions = FrmFormAction::get_action_for_form( $form_id );
115 $enabled_form_actions = self::get_enabled_form_action_ids( $form_actions );
116 $test_mode_install_span_attrs = array(
117 'data-upgrade' => __( 'Test Mode Controls', 'formidable' ),
118 'data-content' => 'test-mode',
119 'data-medium' => 'test-mode',
120 'data-requires' => 'Business',
121 'style' => 'margin-left: auto;',
122 );
123 $ai_install_span_attrs = array(
124 'data-upgrade' => __( 'Autofilled forms with AI', 'formidable' ),
125 'data-content' => 'ai-autofill',
126 'data-medium' => 'test-mode',
127 'data-requires' => 'Business',
128 'style' => 'margin-left: auto;',
129 );
130
131 $oneclick_data = FrmAddonsController::install_link( 'ai' );
132 if ( isset( $oneclick_data['url'] ) ) {
133 $ai_install_span_attrs['data-oneclick'] = json_encode( $oneclick_data );
134 }
135
136 $oneclick_data = FrmAddonsController::install_link( 'test-mode' );
137 if ( isset( $oneclick_data['url'] ) ) {
138 $test_mode_install_span_attrs['data-oneclick'] = json_encode( $oneclick_data );
139 }
140
141 self::include_svg();
142
143 include FrmAppHelper::plugin_path() . '/classes/views/test-mode/container.php';
144 }
145
146 /**
147 * This is required for the speaker icon in the upsell to appear,
148 * and for the lock icon in the upgrade modals.
149 * It is also required for the tooltip icon used for the enabled form actions setting.
150 *
151 * @since 6.25
152 *
153 * @return void
154 */
155 private static function include_svg() {
156 FrmAppHelper::include_svg();
157 }
158
159 /**
160 * Check GET and POST to determine the current form key.
161 *
162 * @since 6.25
163 *
164 * @return false|string
165 */
166 private static function get_form_key_from_request() {
167 $form_key = FrmAppHelper::simple_get( 'form' );
168 if ( $form_key ) {
169 return $form_key;
170 }
171
172 $form_key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_text_field' );
173 if ( $form_key ) {
174 return $form_key;
175 }
176
177 $form_id = FrmAppHelper::get_post_param( 'form_id', '', 'sanitize_text_field' );
178 if ( $form_id && is_numeric( $form_id ) ) {
179 return FrmForm::get_key_by_id( $form_id );
180 }
181
182 return false;
183 }
184
185 /**
186 * Check the request data to determine which action IDs are currently enabled.
187 *
188 * @since 6.25
189 *
190 * @param array $form_actions
191 * @return array
192 */
193 private static function get_enabled_form_action_ids( $form_actions ) {
194 $all_form_action_ids = wp_list_pluck( $form_actions, 'ID' );
195
196 /**
197 * Filters the list of enabled form action IDs.
198 * This way the add-on can modify it when required.
199 *
200 * @since 6.25
201 *
202 * @param array $all_form_action_ids
203 */
204 return apply_filters( 'frm_test_mode_enabled_form_action_ids', $all_form_action_ids );
205 }
206
207 /**
208 * Determine if the upsell should be shown.
209 *
210 * @since 6.25
211 *
212 * @return bool
213 */
214 private static function should_show_upsell() {
215 if ( self::test_mode_addon_exists() ) {
216 return false;
217 }
218
219 return ! in_array( FrmAddonsController::license_type(), array( 'plus', 'business', 'elite' ) );
220 }
221
222 /**
223 * Determine if the Test Mode add-on is installed and active.
224 *
225 * @since 6.25
226 *
227 * @return bool
228 */
229 private static function test_mode_addon_exists() {
230 if ( ! function_exists( 'load_formidable_test_mode' ) ) {
231 return false;
232 }
233
234 return FrmAppHelper::pro_is_installed();
235 }
236
237 /**
238 * Get the list of roles that can be selected in the test mode container.
239 *
240 * @since 6.25
241 *
242 * @return array
243 */
244 private static function get_roles() {
245 if ( ! function_exists( 'get_editable_roles' ) ) {
246 require_once ABSPATH . 'wp-admin/includes/user.php';
247 }
248
249 $roles = get_editable_roles();
250 $roles['loggedout'] = array(
251 'name' => __( 'Logged Out', 'formidable' ),
252 );
253 return $roles;
254 }
255
256 /**
257 * Get the selected role for test mode.
258 *
259 * @since 6.25
260 *
261 * @return string
262 */
263 private static function get_selected_role() {
264 $selected_role = '';
265
266 /**
267 * Filters the selected role for test mode so the add-on can modify it when required.
268 *
269 * @since 6.25
270 *
271 * @param string $selected_role
272 */
273 return apply_filters( 'frm_test_mode_selected_role', $selected_role );
274 }
275
276 /**
277 * Get the arguments for the disabled required fields toggle.
278 *
279 * @since 6.25
280 *
281 * @return array
282 */
283 private static function get_disabled_required_fields_toggle_args() {
284 /**
285 * Filters the arguments for the disabled required fields toggle so the add-on can modify it.
286 *
287 * @since 6.25
288 *
289 * @param array $args
290 */
291 return (array) apply_filters(
292 'frm_test_mode_disable_required_fields_toggle_args',
293 array(
294 'echo' => true,
295 'off_label' => __( 'Disable Required Fields', 'formidable' ),
296 'show_labels' => true,
297 'disabled' => true,
298 )
299 );
300 }
301
302 /**
303 * Get the arguments for the show all hidden fields toggle.
304 *
305 * @since 6.25
306 *
307 * @return array
308 */
309 private static function get_show_all_hidden_fields_toggle_args() {
310 /**
311 * Filters the arguments for the show all hidden fields toggle so the add-on can modify it.
312 *
313 * @since 6.25
314 *
315 * @param array $args
316 */
317 return (array) apply_filters(
318 'frm_test_mode_show_all_hidden_fields_toggle_args',
319 array(
320 'echo' => true,
321 'off_label' => __( 'Show All Hidden Fields', 'formidable' ),
322 'show_labels' => true,
323 'disabled' => true,
324 )
325 );
326 }
327
328 /**
329 * Register and enqueue the required scripts for the test mode container Lite functionality.
330 *
331 * @since 6.25
332 *
333 * @return void
334 */
335 public static function register_and_enqueue_required_scripts() {
336 // These are used for the upgrade pop-up.
337 FrmAppController::enqueue_dialog_assets();
338 FrmAppController::upgrade_overlay_html();
339
340 $version = FrmAppHelper::plugin_version();
341
342 wp_enqueue_style( 'frm_testing_mode', FrmAppHelper::plugin_url() . '/css/frm_testing_mode.css', array(), $version );
343 wp_enqueue_script( 'frm_testing_mode', FrmAppHelper::plugin_url() . '/js/frm_testing_mode.js', array( 'jquery', 'formidable_dom' ), $version, true );
344
345 // These are used in addon-state.js.
346 $admin_script_strings = array(
347 'active' => __( 'Active', 'formidable' ),
348 'installed' => __( 'Installed', 'formidable' ),
349 'not_installed' => __( 'Not Installed', 'formidable' ),
350 );
351 wp_localize_script( 'frm_testing_mode', 'frm_admin_js', $admin_script_strings );
352
353 self::register_and_enqueue_multiselect_dropdown_requirements();
354 }
355
356 /**
357 * Register and enqueue the required scripts for the multiselect dropdown.
358 *
359 * @since 6.25
360 *
361 * @return void
362 */
363 private static function register_and_enqueue_multiselect_dropdown_requirements() {
364 // Enqueue multiselect dropdown requirements.
365 $plugin_url = FrmAppHelper::plugin_url();
366 $version = FrmAppHelper::plugin_version();
367
368 wp_register_script( 'popper', FrmAppHelper::plugin_url() . '/js/popper.min.js', array( 'jquery' ), '1.16.0', true );
369 wp_register_script( 'bootstrap_tooltip', $plugin_url . '/js/bootstrap.min.js', array( 'jquery', 'popper' ), '4.6.1', true );
370 wp_register_script( 'bootstrap-multiselect', $plugin_url . '/js/bootstrap-multiselect.js', array( 'jquery', 'bootstrap_tooltip', 'popper' ), '1.1.1', true );
371 wp_register_script( 'formidable_dom', $plugin_url . '/js/admin/dom.js', array( 'jquery', 'jquery-ui-dialog', 'wp-i18n' ), $version, true );
372
373 wp_enqueue_script( 'bootstrap-multiselect' );
374 wp_enqueue_script( 'formidable_dom' );
375 }
376 }
377