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

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