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

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