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