'sanitize_text_field' ); if ( $bulkaction == - 1 ) { $bulkaction = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' ); } if ( ! empty( $bulkaction ) && strpos( $bulkaction, 'bulk_' ) === 0 ) { FrmAppHelper::remove_get_action(); $bulkaction = str_replace( 'bulk_', '', $bulkaction ); } $ids = FrmAppHelper::get_param( 'item-action', '', 'get', 'sanitize_text_field' ); if ( empty( $ids ) ) { $errors[] = __( 'No forms were specified', 'formidable' ); return $errors; } $permission_error = FrmAppHelper::permission_nonce_error( '', '_wpnonce', 'bulk-toplevel_page_formidable' ); if ( $permission_error !== false ) { $errors[] = $permission_error; return $errors; } if ( ! is_array( $ids ) ) { $ids = explode( ',', $ids ); } switch ( $bulkaction ) { case 'delete': $message = self::bulk_destroy( $ids ); break; case 'trash': $message = self::bulk_trash( $ids ); break; case 'untrash': $message = self::bulk_untrash( $ids ); } if ( isset( $message ) && ! empty( $message ) ) { $errors['message'] = $message; } return $errors; } public static function route() { $action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; $vars = array(); FrmAppHelper::include_svg(); if ( isset( $_POST['frm_compact_fields'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing FrmAppHelper::permission_check( 'frm_edit_forms' ); // Javascript needs to be allowed in some field settings. // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing $json_vars = htmlspecialchars_decode( nl2br( str_replace( '"', '"', wp_unslash( $_POST['frm_compact_fields'] ) ) ) ); $json_vars = json_decode( $json_vars, true ); if ( empty( $json_vars ) ) { // json decoding failed so we should return an error message. $action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); if ( 'edit' == $action ) { $action = 'update'; } add_filter( 'frm_validate_form', 'FrmFormsController::json_error' ); } else { $vars = FrmAppHelper::json_to_array( $json_vars ); $action = $vars[ $action ]; unset( $_REQUEST['frm_compact_fields'], $_POST['frm_compact_fields'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing $_REQUEST = array_merge( $_REQUEST, $vars ); // phpcs:ignore WordPress.Security.NonceVerification.Missing $_POST = array_merge( $_POST, $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing } } else { $action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); if ( isset( $_REQUEST['delete_all'] ) ) { // Override the action for this page. $action = 'delete_all'; } } add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' ); FrmAppHelper::trigger_hook_load( 'form' ); switch ( $action ) { case 'new': return self::new_form( $vars ); case 'create': case 'edit': case 'update': case 'trash': case 'untrash': case 'destroy': case 'delete_all': case 'settings': case 'update_settings': return self::$action( $vars ); case 'lite-reports': return self::no_reports( $vars ); case 'views': return self::no_views( $vars ); default: do_action( 'frm_form_action_' . $action ); if ( apply_filters( 'frm_form_stop_action_' . $action, false ) ) { return; } $action = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' ); if ( $action == - 1 ) { $action = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' ); } if ( strpos( $action, 'bulk_' ) === 0 ) { FrmAppHelper::remove_get_action(); self::list_form(); return; } $message = FrmAppHelper::get_param( 'message' ); if ( 'form_duplicate_error' === $message ) { self::display_forms_list( array(), '', array( __( 'There was a problem duplicating the form', 'formidable' ) ) ); return; } self::display_forms_list(); return; } } public static function json_error( $errors ) { $errors['json'] = __( 'Abnormal HTML characters prevented your form from saving correctly', 'formidable' ); return $errors; } /** * Education for premium features. * * @since 4.05 */ public static function add_form_style_tab_options() { include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_form_style_options.php' ); } /** * Add education about views. * * @since 4.07 */ public static function no_views( $values = array() ) { FrmAppHelper::include_svg(); $id = FrmAppHelper::get_param( 'form', '', 'get', 'absint' ); $form = $id ? FrmForm::getOne( $id ) : false; include FrmAppHelper::plugin_path() . '/classes/views/shared/views-info.php'; } /** * Add education about reports. * * @since 4.07 */ public static function no_reports( $values = array() ) { $id = FrmAppHelper::get_param( 'form', '', 'get', 'absint' ); $form = $id ? FrmForm::getOne( $id ) : false; include FrmAppHelper::plugin_path() . '/classes/views/shared/reports-info.php'; } /* FRONT-END FORMS */ public static function admin_bar_css() { if ( is_admin() || ! current_user_can( 'frm_edit_forms' ) ) { return; } self::move_menu_to_footer(); add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' ); FrmAppHelper::load_font_style(); } /** * @since 4.05.02 */ private static function move_menu_to_footer() { $settings = FrmAppHelper::get_settings(); if ( empty( $settings->admin_bar ) ) { remove_action( 'wp_body_open', 'wp_admin_bar_render', 0 ); } } public static function admin_bar_configure() { global $frm_vars; if ( empty( $frm_vars['forms_loaded'] ) ) { return; } $actions = array(); foreach ( $frm_vars['forms_loaded'] as $form ) { if ( is_object( $form ) ) { $actions[ $form->id ] = $form->name; } unset( $form ); } if ( empty( $actions ) ) { return; } self::add_menu_to_admin_bar(); self::add_forms_to_admin_bar( $actions ); } /** * @since 2.05.07 */ public static function add_menu_to_admin_bar() { global $wp_admin_bar; $wp_admin_bar->add_node( array( 'id' => 'frm-forms', 'title' => '' . FrmAppHelper::get_menu_name() . '', 'href' => admin_url( 'admin.php?page=formidable' ), 'meta' => array( 'title' => FrmAppHelper::get_menu_name(), ), ) ); } /** * @since 2.05.07 */ private static function add_forms_to_admin_bar( $actions ) { global $wp_admin_bar; asort( $actions ); foreach ( $actions as $form_id => $name ) { $wp_admin_bar->add_node( array( 'parent' => 'frm-forms', 'id' => 'edit_form_' . $form_id, 'title' => empty( $name ) ? __( '(no title)', 'formidable' ) : $name, 'href' => FrmForm::get_edit_link( $form_id ), ) ); } } /** * The formidable shortcode * * @param array $atts The params from the shortcode. * @return string */ public static function get_form_shortcode( $atts ) { global $frm_vars; if ( isset( $frm_vars['skip_shortcode'] ) && $frm_vars['skip_shortcode'] ) { $sc = '[formidable'; $sc .= FrmAppHelper::array_to_html_params( $atts ); return $sc . ']'; } $shortcode_atts = shortcode_atts( array( 'id' => '', 'key' => '', 'title' => 'auto', 'description' => 'auto', 'readonly' => false, 'entry_id' => false, 'fields' => array(), 'exclude_fields' => array(), 'minimize' => false, ), $atts ); do_action( 'formidable_shortcode_atts', $shortcode_atts, $atts ); return self::show_form( $shortcode_atts['id'], $shortcode_atts['key'], $shortcode_atts['title'], $shortcode_atts['description'], $atts ); } /** * @since 5.2.01 * * @param string|int|false $id * @param string|false $key * @return stdClass|false */ private static function maybe_get_form_by_id_or_key( $id, $key ) { if ( ! $id ) { $id = $key; } return self::maybe_get_form_to_show( $id ); } /** * @param string|int|false $id * @param string|false $key * @param string|int|bool $title may be 'auto', true, false, 'true', 'false', 'yes', '1', 1, '0', 0. * @param string|int|bool $description may be 'auto', true, false, 'true', 'false', 'yes', '1', 1, '0', 0. * @param array $atts * @return string */ public static function show_form( $id = '', $key = '', $title = false, $description = false, $atts = array() ) { $form = self::maybe_get_form_by_id_or_key( $id, $key ); if ( ! $form ) { return __( 'Please select a valid form', 'formidable' ); } if ( 'auto' === $title ) { $title = ! empty( $form->options['show_title'] ); } if ( 'auto' === $description ) { $description = ! empty( $form->options['show_description'] ); } FrmAppController::maybe_update_styles(); add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' ); FrmAppHelper::trigger_hook_load( 'form', $form ); $form = apply_filters( 'frm_pre_display_form', $form ); $frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form->id ) ); if ( self::is_viewable_draft_form( $form ) ) { // don't show a draft form on a page $form = __( 'Please select a valid form', 'formidable' ); } elseif ( ! FrmForm::is_visible_to_user( $form ) ) { $form = do_shortcode( $frm_settings->login_msg ); } else { do_action( 'frm_pre_get_form', $form ); $form = self::get_form( $form, $title, $description, $atts ); /** * Use this shortcode to check for external shortcodes that may span * across multiple fields in the customizable HTML * * @since 2.0.8 */ $form = apply_filters( 'frm_filter_final_form', $form ); } return $form; } /** * @param string|int|false $id * @return stdClass|false */ private static function maybe_get_form_to_show( $id ) { $form = false; if ( ! empty( $id ) ) { // form id or key is set $form = FrmForm::getOne( $id ); if ( ! $form || $form->parent_form_id || $form->status === 'trash' ) { $form = false; } } return $form; } private static function is_viewable_draft_form( $form ) { return $form->status === 'draft' && current_user_can( 'frm_edit_forms' ) && ! FrmAppHelper::is_preview_page(); } public static function get_form( $form, $title, $description, $atts = array() ) { ob_start(); do_action( 'frm_before_get_form', $atts ); self::get_form_contents( $form, $title, $description, $atts ); self::enqueue_scripts( FrmForm::get_params( $form ) ); $contents = ob_get_contents(); ob_end_clean(); self::maybe_minimize_form( $atts, $contents ); return $contents; } public static function enqueue_scripts( $params ) { do_action( 'frm_enqueue_form_scripts', $params ); } public static function get_form_contents( $form, $title, $description, $atts ) { $params = FrmForm::get_params( $form ); $errors = self::get_saved_errors( $form, $params ); $fields = FrmFieldsHelper::get_form_fields( $form->id, $errors ); $reset = false; $pass_args = compact( 'form', 'fields', 'errors', 'title', 'description', 'reset' ); $pass_args['action'] = $params['action']; $handle_process_here = $params['action'] === 'create' && $params['posted_form_id'] == $form->id && $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( ! $handle_process_here ) { FrmFormState::set_initial_value( 'title', $title ); FrmFormState::set_initial_value( 'description', $description ); do_action( 'frm_display_form_action', $params, $fields, $form, $title, $description ); if ( apply_filters( 'frm_continue_to_new', true, $form->id, $params['action'] ) ) { self::show_form_after_submit( $pass_args ); } } elseif ( ! empty( $errors ) ) { self::show_form_after_submit( $pass_args ); } else { do_action( 'frm_validate_form_creation', $params, $fields, $form, $title, $description ); if ( apply_filters( 'frm_continue_to_create', true, $form->id ) ) { $entry_id = self::just_created_entry( $form->id ); $pass_args['entry_id'] = $entry_id; $pass_args['reset'] = true; self::run_on_submit_actions( $pass_args ); do_action( 'frm_after_entry_processed', array( 'entry_id' => $entry_id, 'form' => $form, ) ); } } } /** * If the form was processed earlier (init), get the generated errors * * @since 2.05 */ private static function get_saved_errors( $form, $params ) { global $frm_vars; if ( $params['posted_form_id'] == $form->id && $_POST && isset( $frm_vars['created_entries'][ $form->id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing $errors = $frm_vars['created_entries'][ $form->id ]['errors']; } else { $errors = array(); } /** * Allows modifying the generated errors if the form was processed earlier. * * @since 5.2.03 * * @param array $errors Errors data. Is empty array if no errors found. * @param array $params Form params. See {@see FrmForm::get_params()}. */ return apply_filters( 'frm_saved_errors', $errors, $params ); } /** * @since 2.2.7 */ public static function just_created_entry( $form_id ) { global $frm_vars; return ( isset( $frm_vars['created_entries'] ) && isset( $frm_vars['created_entries'][ $form_id ] ) && isset( $frm_vars['created_entries'][ $form_id ]['entry_id'] ) ) ? $frm_vars['created_entries'][ $form_id ]['entry_id'] : 0; } /** * Gets confirmation method. * * @since 3.0 * @since 6.0 This method can return an array of met On Submit actions. * * @param array $atts { * Atts. * * @type object $form Form object. * @type int $entry_id Entry ID. * } * @return string|array */ private static function get_confirmation_method( $atts ) { $action = FrmOnSubmitHelper::current_event( $atts ); $opt = 'update' === $action ? 'edit_action' : 'success_action'; $method = ( isset( $atts['form']->options[ $opt ] ) && ! empty( $atts['form']->options[ $opt ] ) ) ? $atts['form']->options[ $opt ] : 'message'; if ( ! empty( $atts['entry_id'] ) ) { $met_actions = self::get_met_on_submit_actions( $atts, $action ); if ( $met_actions ) { $method = $met_actions; } } $method = apply_filters( 'frm_success_filter', $method, $atts['form'], $action ); if ( $method != 'message' && ( ! $atts['entry_id'] || ! is_numeric( $atts['entry_id'] ) ) ) { $method = 'message'; } return $method; } public static function maybe_trigger_redirect( $form, $params, $args ) { if ( ! isset( $params['id'] ) ) { global $frm_vars; $params['id'] = $frm_vars['created_entries'][ $form->id ]['entry_id']; } $conf_method = self::get_confirmation_method( array( 'form' => $form, 'entry_id' => $params['id'], 'action' => FrmOnSubmitHelper::current_event( $params ), ) ); self::maybe_trigger_redirect_with_action( $conf_method, $form, $params, $args ); } /** * Maybe trigger redirect with the new Confirmation action. * * @since 6.1.1 * * @param array|string $conf_method Array of confirmation actions or the action type string. * @param object $form Form object. * @param array $params See {@see FrmFormsController::maybe_trigger_redirect()}. * @param array $args See {@see FrmFormsController::maybe_trigger_redirect()}. */ public static function maybe_trigger_redirect_with_action( $conf_method, $form, $params, $args ) { if ( is_array( $conf_method ) && 1 === count( $conf_method ) ) { if ( 'redirect' === FrmOnSubmitHelper::get_action_type( $conf_method[0] ) ) { $event = FrmOnSubmitHelper::current_event( $params ); FrmOnSubmitHelper::populate_on_submit_data( $form->options, $conf_method[0], $event ); $conf_method = 'redirect'; } } if ( 'redirect' === $conf_method ) { self::trigger_redirect( $form, $params, $args ); } } public static function trigger_redirect( $form, $params, $args ) { $success_args = array( 'action' => $params['action'], 'conf_method' => 'redirect', 'form' => $form, 'entry_id' => $params['id'], ); if ( isset( $args['ajax'] ) ) { $success_args['ajax'] = $args['ajax']; } self::run_success_action( $success_args ); } /** * Used when the success action is not 'message' * * @since 2.05 * @since 6.0 `$args['force_delay_redirect']` is added. * * @param array $args { * The args. * * @type string $conf_method The method. * @type object $form Form object. * @type int $entry_id Entry ID. * @type string $action The action event. Accepts `create` or `update`. * @type array $fields The array of fields. * @type int $force_delay_redirect Force to show the message before redirecting in case redirect method runs. * } */ public static function run_success_action( $args ) { global $frm_vars; $extra_args = $args; unset( $extra_args['form'] ); do_action( 'frm_success_action', $args['conf_method'], $args['form'], $args['form']->options, $args['entry_id'], $extra_args ); $opt = ( ! isset( $args['action'] ) || $args['action'] === 'create' ) ? 'success' : 'edit'; $args['success_opt'] = $opt; $args['ajax'] = ! empty( $frm_vars['ajax'] ); if ( $args['conf_method'] === 'page' && is_numeric( $args['form']->options[ $opt . '_page_id' ] ) ) { self::load_page_after_submit( $args ); } elseif ( $args['conf_method'] === 'redirect' ) { self::redirect_after_submit( $args ); } else { self::show_message_after_save( $args ); } } /** * Gets met On Submit actions. * * @since 6.0 * * @param array $args See {@see FrmFormsController::run_success_action()}. * @param string $event Form event. Default is `create`. * @return array Array of actions that meet the conditional logics. */ public static function get_met_on_submit_actions( $args, $event = 'create' ) { if ( ! FrmOnSubmitHelper::form_has_migrated( $args['form'] ) ) { return array(); } // If a redirect action has already opened the URL in a new tab, we show the default message in the currect tab. if ( ! empty( self::$redirected_in_new_tab[ $args['form']->id ] ) ) { return array( FrmOnSubmitHelper::get_fallback_action_after_open_in_new_tab( $event ) ); } $entry = FrmEntry::getOne( $args['entry_id'], true ); $actions = FrmOnSubmitHelper::get_actions( $args['form']->id ); $met_actions = array(); $has_redirect = false; foreach ( $actions as $action ) { if ( ! in_array( $event, $action->post_content['event'], true ) ) { continue; } if ( FrmFormAction::action_conditions_met( $action, $entry ) ) { continue; } $action_type = FrmOnSubmitHelper::get_action_type( $action ); if ( 'redirect' === $action_type ) { if ( $has_redirect ) { // Do not process because we run the first redirect action only. continue; } } if ( ! self::is_valid_on_submit_action( $action, $args, $event ) ) { continue; } if ( 'redirect' === $action_type ) { $has_redirect = true; } $met_actions[] = $action; unset( $action ); } $args['event'] = $event; /** * Filters the On Submit actions that meet the conditional logics. * * @since 6.0 * * @param array $met_actions Actions that meet the conditional logics. * @param array $args See {@see FrmFormsController::run_success_action()}. `$args['event']` is also added. */ $met_actions = apply_filters( 'frm_get_met_on_submit_actions', $met_actions, $args ); if ( empty( $met_actions ) ) { $met_actions = array( FrmOnSubmitHelper::get_fallback_action( $event ) ); } return $met_actions; } /** * Checks if a Confirmation action has the valid data. * * @since 6.1.2 * * @param object $action Form action object. * @param array $args See {@see FrmFormsController::run_success_action()}. * @param string $event Form event. Default is `create`. * @return bool */ private static function is_valid_on_submit_action( $action, $args, $event = 'create' ) { $action_type = FrmOnSubmitHelper::get_action_type( $action ); if ( 'redirect' === $action_type ) { // Run through frm_redirect_url filter. This is used for the valid action check. $action->post_content['success_url'] = apply_filters( 'frm_redirect_url', $action->post_content['success_url'], $args['form'], $args + array( 'action' => $event ) ); return ! empty( $action->post_content['success_url'] ); } if ( 'page' === $action_type ) { if ( empty( $action->post_content['success_page_id'] ) ) { return false; } $page = get_post( $action->post_content['success_page_id'] ); if ( ! $page || 'trash' === $page->post_status ) { return false; } } return true; } /** * Runs On Submit actions. * * @since 6.0 * * @param array $args See inside {@see FrmFormsController::get_form_contents()} method. */ public static function run_on_submit_actions( $args ) { $args['conf_method'] = self::get_confirmation_method( array( 'form' => $args['form'], 'entry_id' => $args['entry_id'], 'action' => FrmOnSubmitHelper::current_event( $args ), ) ); if ( ! is_array( $args['conf_method'] ) ) { self::run_success_action( $args ); return; } // If conf_method is an array, run On Submit actions. if ( ! $args['conf_method'] ) { // Use default message. FrmOnSubmitHelper::populate_on_submit_data( $args['form']->options ); self::run_success_action( $args ); } elseif ( 1 === count( $args['conf_method'] ) ) { FrmOnSubmitHelper::populate_on_submit_data( $args['form']->options, reset( $args['conf_method'] ) ); $args['conf_method'] = $args['form']->options['success_action']; self::run_success_action( $args ); } else { self::run_multi_on_submit_actions( $args ); } } /** * Runs multiple success actions. * * @since 6.0 * * @param array $args See {@see FrmFormsController::run_success_action()}. */ public static function run_multi_on_submit_actions( $args ) { $redirect_action = null; foreach ( $args['conf_method'] as $action ) { if ( 'redirect' === FrmOnSubmitHelper::get_action_type( $action ) ) { // We catch the redirect action to run it last. $redirect_action = $action; continue; } self::run_single_on_submit_action( $args, $action ); unset( $action ); } if ( $redirect_action ) { // Show script to delay the redirection. $args['force_delay_redirect'] = true; self::run_single_on_submit_action( $args, $redirect_action ); } } /** * Runs single On Submit action. * * @since 6.0 * * @param array $args See {@see FrmFormsController::run_success_action()}. * @param object $action On Submit action object. */ public static function run_single_on_submit_action( $args, $action ) { $new_args = self::get_run_success_action_args( $args, $action ); self::run_success_action( $new_args ); } /** * Gets run_success_action() args from the On Submit action. * * @since 6.0 * * @param array $args See {@see FrmFormsController::run_success_action()}. * @param object $action On Submit action object. * @return array */ private static function get_run_success_action_args( $args, $action ) { $new_args = $args; FrmOnSubmitHelper::populate_on_submit_data( $new_args['form']->options, $action, $args['action'] ); $opt = 'update' === $args['action'] ? 'edit_' : 'success_'; $new_args['conf_method'] = $new_args['form']->options[ $opt . 'action' ]; /** * Filters the run success action args. * * @since 6.0 * * @param array $new_args The new args. * @param array $args The old args. See {@see FrmFormsController::run_success_action()}. * @param object $action On Submit action object. */ return apply_filters( 'frm_get_run_success_action_args', $new_args, $args, $action ); } /** * @since 3.0 */ private static function load_page_after_submit( $args ) { global $post; $opt = $args['success_opt']; if ( ! $post || $args['form']->options[ $opt . '_page_id' ] != $post->ID ) { $page = get_post( $args['form']->options[ $opt . '_page_id' ] ); $old_post = $post; $post = $page; $content = apply_filters( 'frm_content', $page->post_content, $args['form'], $args['entry_id'] ); // Fix the On Submit page content doesn't show when previewing In theme. $has_preview_filter = has_filter( 'the_content', 'FrmFormsController::preview_content' ); if ( $has_preview_filter ) { remove_filter( 'the_content', 'FrmFormsController::preview_content', 9999 ); } echo apply_filters( 'the_content', $content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( $has_preview_filter ) { add_filter( 'the_content', 'FrmFormsController::preview_content', 9999 ); } $post = $old_post; } } /** * @since 3.0 * @param array $args See {@see FrmFormsController::run_success_action()}. */ private static function redirect_after_submit( $args ) { add_filter( 'frm_use_wpautop', '__return_false' ); $opt = $args['success_opt']; $success_url = trim( $args['form']->options[ $opt . '_url' ] ); $success_url = apply_filters( 'frm_content', $success_url, $args['form'], $args['entry_id'] ); $success_url = do_shortcode( $success_url ); $args['id'] = $args['entry_id']; FrmEntriesController::delete_entry_before_redirect( $success_url, $args['form'], $args ); add_filter( 'frm_redirect_url', 'FrmEntriesController::prepare_redirect_url' ); $success_url = apply_filters( 'frm_redirect_url', $success_url, $args['form'], $args ); $doing_ajax = FrmAppHelper::doing_ajax(); if ( ! empty( $args['ajax'] ) && $doing_ajax && empty( $args['force_delay_redirect'] ) ) { // Is AJAX submit and there is just one Redirect action runs. echo json_encode( self::get_ajax_redirect_response_data( $args + compact( 'success_url' ) ) ); wp_die(); } if ( ! headers_sent() && empty( $args['force_delay_redirect'] ) ) { // Not AJAX submit, no headers sent, and there is just one Redirect action runs. if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) { self::print_open_in_new_tab_js_with_fallback_handler( $success_url, $args ); self::$redirected_in_new_tab[ $args['form']->id ] = 1; return; } wp_redirect( esc_url_raw( $success_url ) ); die(); // do not use wp_die or redirect fails } // Redirect with a delay. self::redirect_after_submit_using_js( $args + compact( 'success_url', 'doing_ajax' ) ); } /** * Prints open in new tab js with fallback handler. * * @since 6.x * * @param string $success_url Success URL. * @param array $args See {@see FrmFormsController::redirect_after_submit()}. */ private static function print_open_in_new_tab_js_with_fallback_handler( $success_url, $args ) { echo ''; } /** * Gets response data for redirect action when AJAX submitting. * * @since 6.x * * @param array $args See {@see FrmFormsController::run_success_action()}. * @return array */ private static function get_ajax_redirect_response_data( $args ) { $response_data = array( 'redirect' => $args['success_url'] ); if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) { $response_data['openInNewTab'] = 1; $args['message'] = FrmOnSubmitHelper::get_default_new_tab_msg(); $args['form']->options['success_msg'] = $args['message']; $args['form']->options['edit_msg'] = $args['message']; if ( ! isset( $args['fields'] ) ) { $args['fields'] = FrmField::get_all_for_form( $args['form']->id ); } $args['message'] = self::prepare_submit_message( $args['form'], $args['entry_id'], $args ); ob_start(); self::show_lone_success_messsage( $args ); $response_data['content'] = ob_get_clean(); $response_data['fallbackMsg'] = self::get_redirect_fallback_message( $args['success_url'], $args ); } return $response_data; } /** * Redirects after submitting using JS. This is used when showing message before redirecting. * * @since 6.0 * * @param array $args See {@see FrmFormsController::run_success_action()}. */ private static function redirect_after_submit_using_js( $args ) { $success_msg = FrmOnSubmitHelper::get_default_redirect_msg(); $redirect_msg = self::get_redirect_message( $args['success_url'], $success_msg, $args ); $success_url = esc_url_raw( $args['success_url'] ); /** * Filters the delay time before redirecting when On Submit Redirect action is delayed. * * @since 6.0 * * @param int $delay_time Delay time in miliseconds. */ $delay_time = apply_filters( 'frm_redirect_delay_time', 8000 ); if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) { $redirect_js = 'window.open("' . $success_url . '", "_blank")'; } else { $redirect_js = 'window.location="' . $success_url . '";'; } add_filter( 'frm_use_wpautop', '__return_true' ); echo FrmAppHelper::maybe_kses( $redirect_msg ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo ''; } /** * @since 3.0 * * @param string $success_url * @param string $success_msg * @param array $args */ private static function get_redirect_message( $success_url, $success_msg, $args ) { $redirect_msg = '
' . $success_msg . '
' . self::get_redirect_fallback_message( $success_url, $args ) . '
'; $redirect_args = array( 'entry_id' => $args['entry_id'], 'form_id' => $args['form']->id, 'form' => $args['form'], ); return apply_filters( 'frm_redirect_msg', $redirect_msg, $redirect_args ); } /** * Gets fallback message when redirecting failed. * * @since 6.x * * @param string $success_url Redirect URL. * @param array $args Contains `form` object. * @return string */ private static function get_redirect_fallback_message( $success_url, $args ) { $target = ''; if ( ! empty( $args['form']->options['open_in_new_tab'] ) ) { $target = ' target="_blank"'; } return sprintf( /* translators: %1$s: Start link HTML, %2$s: End link HTML */ __( '%1$sClick here%2$s if you are not automatically redirected.', 'formidable' ), '', '' ); } /** * Prepare to show the success message and empty form after submit * * @since 2.05 */ public static function show_message_after_save( $atts ) { $atts['message'] = self::prepare_submit_message( $atts['form'], $atts['entry_id'], $atts ); if ( ! isset( $atts['form']->options['show_form'] ) || $atts['form']->options['show_form'] ) { if ( isset( $atts['action'] ) && 'update' === $atts['action'] && is_callable( array( 'FrmProEntriesController', 'show_front_end_form_with_entry' ) ) ) { $entry = FrmEntry::getOne( $atts['entry_id'] ); if ( $entry ) { // This is copied from the Pro plugin. $atts['conf_message'] = FrmProEntriesController::confirmation( 'message', $atts['form'], $atts['form']->options, $entry->id, $atts ); $atts['show_form'] = FrmProEntriesController::is_form_displayed_after_edit( $atts['form'] ); FrmProEntriesController::show_front_end_form_with_entry( $entry, $atts ); } } else { self::show_form_after_submit( $atts ); } } else { self::show_lone_success_messsage( $atts ); } } /** * Show an empty form * * @since 2.05 */ private static function show_form_after_submit( $args ) { self::fill_atts_for_form_display( $args ); $errors = $args['errors']; $message = $args['message']; $form = $args['form']; $title = $args['title']; $description = $args['description']; if ( empty( $args['fields'] ) ) { $values = array( 'custom_style' => FrmAppHelper::custom_style_value( array() ), ); } else { $values = FrmEntriesHelper::setup_new_vars( $args['fields'], $form, $args['reset'] ); } unset( $args ); $include_form_tag = apply_filters( 'frm_include_form_tag', true, $form ); $frm_settings = FrmAppHelper::get_settings(); $submit = isset( $form->options['submit_value'] ) ? $form->options['submit_value'] : $frm_settings->submit_value; global $frm_vars; self::maybe_load_css( $form, $values['custom_style'], $frm_vars['load_css'] ); $message_placement = self::message_placement( $form, $message ); include FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php'; } /** * @return string - 'before', 'after', or 'submit' * * @since 4.05.02 */ private static function message_placement( $form, $message ) { $place = 'before'; if ( $message && isset( $form->options['form_class'] ) ) { if ( strpos( $form->options['form_class'], 'frm_below_success' ) !== false ) { $place = 'after'; } elseif ( strpos( $form->options['form_class'], 'frm_inline_success' ) !== false ) { $place = 'submit'; } } /** * @return string - 'before' or 'after' * * @since 4.05.02 */ return apply_filters( 'frm_message_placement', $place, compact( 'form', 'message' ) ); } /** * Get all the values needed on the new.php entry page * * @since 2.05 */ private static function fill_atts_for_form_display( &$args ) { if ( ! isset( $args['title'] ) && isset( $args['show_title'] ) ) { $args['title'] = $args['show_title']; } if ( ! isset( $args['description'] ) && isset( $args['show_description'] ) ) { $args['description'] = $args['show_description']; } $defaults = array( 'errors' => array(), 'message' => '', 'fields' => array(), 'form' => array(), 'title' => true, 'description' => false, 'reset' => false, ); $args = wp_parse_args( $args, $defaults ); } /** * Show the success message without the form * * @since 2.05 */ private static function show_lone_success_messsage( $atts ) { global $frm_vars; $values = FrmEntriesHelper::setup_new_vars( $atts['fields'], $atts['form'], true ); self::maybe_load_css( $atts['form'], $values['custom_style'], $frm_vars['load_css'] ); $include_extra_container = 'frm_forms' . FrmFormsHelper::get_form_style_class( $values ); $errors = array(); $form = $atts['form']; $message = $atts['message']; include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/errors.php' ); } /** * Prepare the success message before it's shown * * @since 2.05 * @since 6.0.x Added the third parameter. * * @param object $form Form object. * @param int $entry_id Entry ID. * @param array $args See {@see FrmFormsController::run_success_action()}. * @return string */ private static function prepare_submit_message( $form, $entry_id, $args = array() ) { $frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form->id ) ); $opt = isset( $args['success_opt'] ) ? $args['success_opt'] : 'success'; if ( $entry_id && is_numeric( $entry_id ) ) { $message = isset( $form->options[ $opt . '_msg' ] ) ? $form->options[ $opt . '_msg' ] : $frm_settings->success_msg; $class = 'frm_message'; } else { $message = $frm_settings->failed_msg; $class = FrmFormsHelper::form_error_class(); } $message = FrmFormsHelper::get_success_message( compact( 'message', 'form', 'entry_id', 'class' ) ); return apply_filters( 'frm_main_feedback', $message, $form, $entry_id ); } /** * @return void */ public static function front_head() { $version = FrmAppHelper::plugin_version(); $suffix = FrmAppHelper::js_suffix(); if ( ! empty( $suffix ) && self::has_combo_js_file() ) { wp_register_script( 'formidable', FrmAppHelper::plugin_url() . '/js/frm.min.js', array( 'jquery' ), $version, true ); } else { wp_register_script( 'formidable', FrmAppHelper::plugin_url() . "/js/formidable{$suffix}.js", array( 'jquery' ), $version, true ); } add_filter( 'script_loader_tag', 'FrmFormsController::defer_script_loading', 10, 2 ); if ( FrmAppHelper::is_admin() ) { // don't load this in back-end return; } FrmAppHelper::localize_script( 'front' ); FrmStylesController::enqueue_css( 'register' ); } /** * @since 3.0 */ public static function has_combo_js_file() { return is_readable( FrmAppHelper::plugin_path() . '/js/frm.min.js' ); } public static function maybe_load_css( $form, $this_load, $global_load ) { $load_css = FrmForm::is_form_loaded( $form, $this_load, $global_load ); if ( ! $load_css ) { return; } global $frm_vars; self::footer_js( 'header' ); $frm_vars['css_loaded'] = true; self::load_late_css(); } /** * If css is loaded only on applicable pages, include it before the form loads * to prevent a flash of unstyled form. * * @since 4.01 * * @return void */ private static function load_late_css() { $frm_settings = FrmAppHelper::get_settings(); $late_css = $frm_settings->load_style === 'dynamic'; if ( ! $late_css || ! self::should_load_late() ) { return; } global $wp_styles; if ( is_array( $wp_styles->queue ) && in_array( 'formidable', $wp_styles->queue, true ) ) { wp_print_styles( 'formidable' ); } } /** * Avoid late load if All in One SEO is active because it prevents CSS from loading entirely. * * @since 5.2.03 * * @return bool */ private static function should_load_late() { return ! function_exists( 'aioseo' ); } public static function defer_script_loading( $tag, $handle ) { if ( 'captcha-api' == $handle && ! strpos( $tag, 'defer' ) ) { $tag = str_replace( ' src', ' defer="defer" async="async" src', $tag ); } return $tag; } public static function footer_js( $location = 'footer' ) { global $frm_vars; FrmStylesController::enqueue_css(); if ( ! FrmAppHelper::is_admin() && $location != 'header' && ! empty( $frm_vars['forms_loaded'] ) ) { // load formidable js wp_enqueue_script( 'formidable' ); } } /** * @since 2.0.8 */ private static function maybe_minimize_form( $atts, &$content ) { // check if minimizing is turned on if ( self::is_minification_on( $atts ) ) { $content = str_replace( array( "\r\n", "\r", "\n", "\t", ' ' ), '', $content ); } } /** * @since 2.0.8 * @return boolean */ private static function is_minification_on( $atts ) { return isset( $atts['minimize'] ) && ! empty( $atts['minimize'] ); } /** * @since 5.0.16 * * @return void */ public static function landing_page_preview_option() { $dir = apply_filters( 'frm_landing_page_preview_option', false ); if ( false === $dir || ! file_exists( $dir . 'landing-page-preview-option.php' ) ) { $dir = self::get_form_views_path(); } include $dir . 'landing-page-preview-option.php'; } /** * @since 5.0.16 * * @return string */ private static function get_form_views_path() { return FrmAppHelper::plugin_path() . '/classes/views/frm-forms/'; } /** * Create a page with an embedded formidable Gutenberg block. * * @since 5.2 * * @return never */ public static function create_page_with_shortcode() { if ( ! current_user_can( 'publish_posts' ) ) { die( 0 ); } check_ajax_referer( 'frm_ajax', 'nonce' ); $type = FrmAppHelper::get_post_param( 'type', '', 'sanitize_text_field' ); if ( ! $type || ! in_array( $type, array( 'form', 'view' ), true ) ) { die( 0 ); } $object_id = FrmAppHelper::get_post_param( 'object_id', '', 'absint' ); if ( ! $object_id ) { die( 0 ); } $postarr = array( 'post_type' => 'page' ); if ( 'form' === $type ) { $postarr['post_content'] = self::get_page_shortcode_content_for_form( $object_id ); } else { $postarr['post_content'] = apply_filters( 'frm_create_page_with_' . $type . '_shortcode_content', '', $object_id ); } $name = FrmAppHelper::get_post_param( 'name', '', 'sanitize_text_field' ); if ( $name ) { $postarr['post_title'] = $name; } $success = wp_insert_post( $postarr ); if ( ! is_numeric( $success ) || ! $success ) { die( 0 ); } wp_send_json( array( 'redirect' => get_edit_post_link( $success, 'redirect' ), ) ); } /** * @since 5.3 * * @param string $content * @param int $form_id * @return string */ private static function get_page_shortcode_content_for_form( $form_id ) { $shortcode = '[formidable id="' . $form_id . '"]'; $html_comment_start = ''; $html_comment_end = ''; return $html_comment_start . '
' . $shortcode . '
' . $html_comment_end; } /** * Get page dropdown for AJAX request for embedding form in an existing page. * * @return never */ public static function get_page_dropdown() { if ( ! current_user_can( 'publish_posts' ) ) { die( 0 ); } check_ajax_referer( 'frm_ajax', 'nonce' ); $html = FrmAppHelper::clip( function() { FrmAppHelper::maybe_autocomplete_pages_options( array( 'field_name' => 'frm_page_dropdown', 'page_id' => '', 'placeholder' => __( 'Select a Page', 'formidable' ), ) ); } ); $post_type_object = get_post_type_object( 'page' ); wp_send_json( array( 'html' => $html, 'edit_page_url' => admin_url( sprintf( $post_type_object->_edit_link . '&action=edit', 0 ) ), ) ); } /** * @deprecated 4.0 */ public static function new_form( $values = array() ) { FrmDeprecated::new_form( $values ); } /** * @deprecated 4.0 */ public static function create( $values = array() ) { _deprecated_function( __METHOD__, '4.0', 'FrmFormsController::update' ); self::update( $values ); } /** * @deprecated 3.0 * @codeCoverageIgnore */ public static function bulk_create_template( $ids ) { return FrmDeprecated::bulk_create_template( $ids ); } /** * @deprecated 3.0 * @codeCoverageIgnore */ public static function edit_key() { FrmDeprecated::edit_key(); } /** * @deprecated 3.0 * @codeCoverageIgnore */ public static function edit_description() { FrmDeprecated::edit_description(); } /** * @deprecated 4.08 * @since 3.06 */ public static function add_new() { _deprecated_function( __FUNCTION__, '4.08' ); } }