| 1 |
<?php |
| 2 |
/** |
| 3 |
* On Submit action helper |
| 4 |
* |
| 5 |
* @package Formidable |
| 6 |
* @since 6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
die( 'You are not allowed to call this page directly.' ); |
| 11 |
} |
| 12 |
|
| 13 |
class FrmOnSubmitHelper { |
| 14 |
|
| 15 |
/** |
| 16 |
* Shows message settings. |
| 17 |
* |
| 18 |
* @param array $args { |
| 19 |
* The args. |
| 20 |
* |
| 21 |
* @type object $form_action Form action post data. |
| 22 |
* @type FrmFormAction $action_control Form action object. |
| 23 |
* @type object $form Form data. |
| 24 |
* @type string $action_key Action key. |
| 25 |
* @type array $values Contains `fields` (form fields) and `id` (form ID). |
| 26 |
* } |
| 27 |
*/ |
| 28 |
public static function show_message_settings( $args ) { |
| 29 |
$id_attr = $args['action_control']->get_field_id( 'success_msg' ); |
| 30 |
?> |
| 31 |
<div class="frm_form_field frm_has_shortcodes"> |
| 32 |
<label for="<?php echo esc_attr( $id_attr ); ?>" class="screen-reader-text"> |
| 33 |
<?php esc_html_e( 'Message on submit', 'formidable' ); ?> |
| 34 |
</label> |
| 35 |
|
| 36 |
<?php |
| 37 |
wp_editor( |
| 38 |
$args['form_action']->post_content['success_msg'], |
| 39 |
$id_attr, |
| 40 |
array( |
| 41 |
'textarea_name' => $args['action_control']->get_field_name( 'success_msg' ), |
| 42 |
'textarea_rows' => 4, |
| 43 |
'editor_class' => 'frm_not_email_message', |
| 44 |
) |
| 45 |
); |
| 46 |
?> |
| 47 |
</div> |
| 48 |
|
| 49 |
<?php |
| 50 |
$id_attr = $args['action_control']->get_field_id( 'show_form' ); |
| 51 |
$name_attr = $args['action_control']->get_field_name( 'show_form' ); |
| 52 |
?> |
| 53 |
<div class="frm_form_field"> |
| 54 |
<?php |
| 55 |
FrmHtmlHelper::toggle( |
| 56 |
$id_attr, |
| 57 |
$name_attr, |
| 58 |
array( |
| 59 |
'div_class' => 'with_frm_style frm_toggle', |
| 60 |
'checked' => ! empty( $args['form_action']->post_content['show_form'] ), |
| 61 |
'echo' => true, |
| 62 |
) |
| 63 |
); |
| 64 |
?> |
| 65 |
<label for="<?php echo esc_attr( $id_attr ); ?>"> |
| 66 |
<?php esc_html_e( 'Show the form with the confirmation message', 'formidable' ); ?> |
| 67 |
</label> |
| 68 |
</div> |
| 69 |
<?php |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Shows redirect settings. |
| 74 |
* |
| 75 |
* @param array $args { |
| 76 |
* The args. |
| 77 |
* |
| 78 |
* @type object $form_action Form action post data. |
| 79 |
* @type FrmFormAction $action_control Form action object. |
| 80 |
* @type object $form Form data. |
| 81 |
* @type string $action_key Action key. |
| 82 |
* @type array $values Contains `fields` (form fields) and `id` (form ID). |
| 83 |
* } |
| 84 |
*/ |
| 85 |
public static function show_redirect_settings( $args ) { |
| 86 |
$id_attr = $args['action_control']->get_field_id( 'success_url' ); |
| 87 |
?> |
| 88 |
<div class="frm_form_field frm_has_shortcodes"> |
| 89 |
<label for="<?php echo esc_attr( $id_attr ); ?>"><?php esc_html_e( 'Redirect URL', 'formidable' ); ?></label> |
| 90 |
<input |
| 91 |
type="text" |
| 92 |
id="<?php echo esc_attr( $id_attr ); ?>" |
| 93 |
name="<?php echo esc_attr( $args['action_control']->get_field_name( 'success_url' ) ); ?>" |
| 94 |
value="<?php echo esc_attr( $args['form_action']->post_content['success_url'] ); ?>" |
| 95 |
/> |
| 96 |
</div> |
| 97 |
<?php |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Shows page settings. |
| 102 |
* |
| 103 |
* @param array $args { |
| 104 |
* The args. |
| 105 |
* |
| 106 |
* @type object $form_action Form action post data. |
| 107 |
* @type FrmFormAction $action_control Form action object. |
| 108 |
* @type object $form Form data. |
| 109 |
* @type string $action_key Action key. |
| 110 |
* @type array $values Contains `fields` (form fields) and `id` (form ID). |
| 111 |
* } |
| 112 |
*/ |
| 113 |
public static function show_page_settings( $args ) { |
| 114 |
$name_attr = $args['action_control']->get_field_name( 'success_page_id' ); |
| 115 |
?> |
| 116 |
<div class="frm_form_field"> |
| 117 |
<label for="<?php echo esc_attr( $name_attr ); ?>" class="screen-reader-text"> |
| 118 |
<?php esc_html_e( 'Select a page', 'formidable' ); ?> |
| 119 |
</label> |
| 120 |
<?php |
| 121 |
FrmAppHelper::maybe_autocomplete_pages_options( |
| 122 |
array( |
| 123 |
'field_name' => $name_attr, |
| 124 |
'page_id' => $args['form_action']->post_content['success_page_id'], |
| 125 |
'placeholder' => __( 'Select a Page', 'formidable' ), |
| 126 |
) |
| 127 |
); |
| 128 |
?> |
| 129 |
</div> |
| 130 |
<?php |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Gets all active On Submit form actions for form. |
| 135 |
* This checks and gets data from WordPress cache group `frm_actions` first. It prevents the cache issue if form |
| 136 |
* action is created during run time. If you use another cache method, please remember to delete the cache in |
| 137 |
* {@see FrmFormAction::save_settings()}. |
| 138 |
* |
| 139 |
* @param int $form_id Form ID. |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public static function get_actions( $form_id ) { |
| 143 |
$cache_key = 'frm_on_submit_actions_' . $form_id; |
| 144 |
$actions = wp_cache_get( $cache_key, 'frm_actions' ); |
| 145 |
if ( false !== $actions ) { |
| 146 |
return $actions; |
| 147 |
} |
| 148 |
|
| 149 |
$actions = FrmFormAction::get_action_for_form( $form_id, FrmOnSubmitAction::$slug, array( 'post_status' => 'publish' ) ); |
| 150 |
wp_cache_set( $cache_key, 'frm_actions' ); |
| 151 |
return $actions; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Gets On Submit action type (message, redirect or page). |
| 156 |
* |
| 157 |
* @param object $action Form action object. |
| 158 |
* @return string |
| 159 |
*/ |
| 160 |
public static function get_action_type( $action ) { |
| 161 |
if ( isset( $action->post_content['success_action'] ) ) { |
| 162 |
return $action->post_content['success_action']; |
| 163 |
} |
| 164 |
return self::get_default_action_type(); |
| 165 |
} |
| 166 |
|
| 167 |
public static function get_default_action_type() { |
| 168 |
return 'message'; |
| 169 |
} |
| 170 |
|
| 171 |
public static function get_default_msg() { |
| 172 |
$msg = FrmAppHelper::get_settings()->success_msg; |
| 173 |
return $msg ? $msg : __( 'Your responses were successfully submitted. Thank you!', 'formidable' ); |
| 174 |
} |
| 175 |
|
| 176 |
public static function get_default_redirect_msg() { |
| 177 |
return __( 'Please wait while you are redirected.', 'formidable' ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Adds the first On Submit action data to the form options to be saved. |
| 182 |
* |
| 183 |
* @param int $form_id Form ID. |
| 184 |
*/ |
| 185 |
public static function save_on_submit_settings( $form_id ) { |
| 186 |
$actions = self::get_actions( $form_id ); |
| 187 |
$first_create_action = null; |
| 188 |
$first_edit_action = null; |
| 189 |
foreach ( $actions as $action ) { |
| 190 |
if ( ! $first_create_action && in_array( 'create', $action->post_content['event'], true ) ) { |
| 191 |
$first_create_action = $action; |
| 192 |
} |
| 193 |
if ( ! $first_edit_action && in_array( 'update', $action->post_content['event'], true ) ) { |
| 194 |
$first_edit_action = $action; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
$form_options = array(); |
| 199 |
self::populate_on_submit_data( $form_options, $first_create_action ); |
| 200 |
if ( method_exists( 'FrmProFormActionsController', 'change_on_submit_action_ops' ) && FrmAppHelper::pro_is_connected() ) { |
| 201 |
$form_editable = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'editable' ); |
| 202 |
if ( $form_editable ) { |
| 203 |
self::populate_on_submit_data( $form_options, $first_edit_action, 'update' ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if ( ! empty( $form_options ) ) { |
| 208 |
$_POST['options'] += $form_options; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Populates the On Submit data to form options. |
| 214 |
* |
| 215 |
* @param array $form_options Form options. |
| 216 |
* @param object $action Optional. The On Submit action object. |
| 217 |
* @param string $event Form event. Default is `create`. |
| 218 |
*/ |
| 219 |
public static function populate_on_submit_data( &$form_options, $action = null, $event = 'create' ) { |
| 220 |
$opt = 'update' === $event ? 'edit_' : 'success_'; |
| 221 |
if ( ! $action || ! is_object( $action ) ) { |
| 222 |
$form_options[ $opt . 'action' ] = self::get_default_action_type(); |
| 223 |
$form_options[ $opt . 'msg' ] = self::get_default_msg(); |
| 224 |
|
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
$form_options[ $opt . 'action' ] = isset( $action->post_content['success_action'] ) ? $action->post_content['success_action'] : 'message'; |
| 229 |
|
| 230 |
switch ( $form_options[ $opt . 'action' ] ) { |
| 231 |
case 'redirect': |
| 232 |
$form_options[ $opt . 'url' ] = isset( $action->post_content['success_url'] ) ? $action->post_content['success_url'] : ''; |
| 233 |
break; |
| 234 |
|
| 235 |
case 'page': |
| 236 |
$form_options[ $opt . 'page_id' ] = isset( $action->post_content['success_page_id'] ) ? $action->post_content['success_page_id'] : ''; |
| 237 |
break; |
| 238 |
|
| 239 |
default: |
| 240 |
$form_options[ $opt . 'msg' ] = ! empty( $action->post_content['success_msg'] ) ? $action->post_content['success_msg'] : self::get_default_msg(); |
| 241 |
$form_options['show_form'] = ! empty( $action->post_content['show_form'] ); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|