| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmFormAction { |
| 7 |
|
| 8 |
public $id_base; // Root id for all actions of this type. |
| 9 |
public $name; // Name for this action type. |
| 10 |
public $option_name; |
| 11 |
public $action_options; // Option array passed to wp_register_sidebar_widget() |
| 12 |
public $control_options; // Option array passed to wp_register_widget_control() |
| 13 |
|
| 14 |
public $form_id; // The ID of the form to evaluate |
| 15 |
public $number = false; // Unique ID number of the current instance. |
| 16 |
public $id = ''; // Unique ID string of the current instance (id_base-number) |
| 17 |
public $updated = false; // Set true when we update the data after a POST submit - makes sure we don't do it twice. |
| 18 |
|
| 19 |
// Member functions that you must over-ride. |
| 20 |
|
| 21 |
/** |
| 22 |
* This function should check that $new_instance is set correctly. |
| 23 |
* The newly calculated value of $instance should be returned. |
| 24 |
* If "false" is returned, the instance won't be saved/updated. |
| 25 |
* |
| 26 |
* @param array $new_instance New settings for this instance as input by the user via form() |
| 27 |
* @param array $old_instance Old settings for this instance |
| 28 |
* |
| 29 |
* @return array Settings to save or bool false to cancel saving |
| 30 |
*/ |
| 31 |
public function update( $new_instance, $old_instance ) { |
| 32 |
return $new_instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Echo the settings update form |
| 37 |
* |
| 38 |
* @param array $instance Current settings |
| 39 |
*/ |
| 40 |
public function form( $instance, $args = array() ) { |
| 41 |
echo '<p class="no-options-widget">' . esc_html__( 'There are no options for this action.', 'formidable' ) . '</p>'; |
| 42 |
|
| 43 |
return 'noform'; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @return array of the default options |
| 48 |
*/ |
| 49 |
public function get_defaults() { |
| 50 |
return array(); |
| 51 |
} |
| 52 |
|
| 53 |
public function get_switch_fields() { |
| 54 |
return array(); |
| 55 |
} |
| 56 |
|
| 57 |
public function migrate_values( $action, $form ) { |
| 58 |
return $action; |
| 59 |
} |
| 60 |
|
| 61 |
// Functions you'll need to call. |
| 62 |
|
| 63 |
/** |
| 64 |
* PHP5 constructor |
| 65 |
* |
| 66 |
* @param string $id_base Optional Base ID for the widget, lower case, |
| 67 |
* if left empty a portion of the widget's class name will be used. Has to be unique. |
| 68 |
* @param string $name Name for the widget displayed on the configuration page. |
| 69 |
* @param array $action_options Optional Passed to wp_register_sidebar_widget() |
| 70 |
* - description: shown on the configuration page |
| 71 |
* - classname |
| 72 |
* @param array $control_options Optional Passed to wp_register_widget_control() |
| 73 |
* - width: required if more than 250px |
| 74 |
* - height: currently not used but may be needed in the future |
| 75 |
*/ |
| 76 |
public function __construct( $id_base, $name, $action_options = array(), $control_options = array() ) { |
| 77 |
if ( ! defined( 'ABSPATH' ) ) { |
| 78 |
die( 'You are not allowed to call this page directly.' ); |
| 79 |
} |
| 80 |
|
| 81 |
$this->id_base = strtolower( $id_base ); |
| 82 |
$this->name = $name; |
| 83 |
$this->option_name = 'frm_' . $this->id_base . '_action'; |
| 84 |
|
| 85 |
$default_options = array( |
| 86 |
'classes' => '', |
| 87 |
'active' => true, |
| 88 |
'event' => array( 'create' ), |
| 89 |
'limit' => 1, |
| 90 |
'force_event' => false, |
| 91 |
'priority' => 20, |
| 92 |
'ajax_load' => true, |
| 93 |
'plugin' => $this->id_base, |
| 94 |
'tooltip' => $name, |
| 95 |
'group' => $id_base, |
| 96 |
'color' => '', |
| 97 |
'keywords' => '', |
| 98 |
); |
| 99 |
|
| 100 |
$action_options = apply_filters( 'frm_' . $id_base . '_action_options', $action_options ); |
| 101 |
$group = $this->get_group( $action_options ); |
| 102 |
$action_options['group'] = $group['id']; |
| 103 |
|
| 104 |
if ( ! isset( $action_options['color'] ) ) { |
| 105 |
$colors = array( 'green', 'orange', 'purple' ); |
| 106 |
shuffle( $colors ); |
| 107 |
$action_options['color'] = 'var(--' . reset( $colors ) . ')'; |
| 108 |
} |
| 109 |
|
| 110 |
$upgrade_class = isset( $action_options['classes'] ) && $action_options['classes'] === 'frm_show_upgrade'; |
| 111 |
if ( $action_options['group'] === $id_base ) { |
| 112 |
$upgrade_class = strpos( $action_options['classes'], 'frm_show_upgrade' ) !== false; |
| 113 |
$action_options['classes'] = $group['icon']; |
| 114 |
} elseif ( ! isset( $action_options['classes'] ) || empty( $action_options['classes'] ) || $upgrade_class ) { |
| 115 |
$action_options['classes'] = $group['icon']; |
| 116 |
} |
| 117 |
|
| 118 |
if ( $upgrade_class ) { |
| 119 |
$action_options['classes'] .= ' frm_show_upgrade'; |
| 120 |
} |
| 121 |
|
| 122 |
$this->action_options = wp_parse_args( $action_options, $default_options ); |
| 123 |
$this->control_options = wp_parse_args( $control_options, array( 'id_base' => $this->id_base ) ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param string $id_base |
| 128 |
*/ |
| 129 |
public function FrmFormAction( $id_base, $name, $action_options = array(), $control_options = array() ) { |
| 130 |
self::__construct( $id_base, $name, $action_options, $control_options ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Help to switch old field id by new field id for duplicate form |
| 135 |
* |
| 136 |
* @param string $action id of the field that needs to be switched |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public function maybe_switch_field_ids( $action ) { |
| 141 |
$updated_action = apply_filters( 'frm_maybe_switch_field_ids', $action ); |
| 142 |
if ( $updated_action === $action ) { |
| 143 |
$updated_action = FrmFieldsHelper::switch_field_ids( $action ); |
| 144 |
} |
| 145 |
return $updated_action; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @since 4.0 |
| 150 |
*/ |
| 151 |
protected function get_group( $action_options ) { |
| 152 |
$groups = FrmFormActionsController::form_action_groups(); |
| 153 |
$group = 'misc'; |
| 154 |
|
| 155 |
if ( isset( $action_options['group'] ) && isset( $groups[ $action_options['group'] ] ) ) { |
| 156 |
$group = $action_options['group']; |
| 157 |
} elseif ( isset( $groups[ $this->id_base ] ) ) { |
| 158 |
$group = $this->id_base; |
| 159 |
} else { |
| 160 |
foreach ( $groups as $name => $check_group ) { |
| 161 |
if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'] ) ) { |
| 162 |
$group = $name; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
$groups[ $group ]['id'] = $group; |
| 169 |
return $groups[ $group ]; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Constructs name attributes for use in form() fields |
| 174 |
* |
| 175 |
* This function should be used in form() methods to create name attributes for fields to be saved by update() |
| 176 |
* |
| 177 |
* @param string $field_name Field name |
| 178 |
* |
| 179 |
* @return string Name attribute for $field_name |
| 180 |
*/ |
| 181 |
public function get_field_name( $field_name, $post_field = 'post_content' ) { |
| 182 |
$name = $this->option_name . '[' . $this->number . ']'; |
| 183 |
$name .= ( empty( $post_field ) ? '' : '[' . $post_field . ']' ); |
| 184 |
$name .= '[' . $field_name . ']'; |
| 185 |
|
| 186 |
return $name; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Constructs id attributes for use in form() fields |
| 191 |
* |
| 192 |
* This function should be used in form() methods to create id attributes for fields to be saved by update() |
| 193 |
* |
| 194 |
* @param string $field_name Field name |
| 195 |
* |
| 196 |
* @return string ID attribute for $field_name |
| 197 |
*/ |
| 198 |
public function get_field_id( $field_name ) { |
| 199 |
return $field_name . '_' . $this->number; |
| 200 |
} |
| 201 |
|
| 202 |
// Private Function. Don't worry about this. |
| 203 |
|
| 204 |
public function _set( $number ) { |
| 205 |
$this->number = $number; |
| 206 |
$this->id = $this->id_base . '-' . $number; |
| 207 |
} |
| 208 |
|
| 209 |
public function prepare_new( $form_id = false ) { |
| 210 |
if ( $form_id ) { |
| 211 |
$this->form_id = $form_id; |
| 212 |
} |
| 213 |
|
| 214 |
$post_content = array(); |
| 215 |
$default_values = $this->get_global_defaults(); |
| 216 |
|
| 217 |
// fill default values |
| 218 |
$post_content = wp_parse_args( $post_content, $default_values ); |
| 219 |
|
| 220 |
if ( ! isset( $post_content['event'] ) && ! $this->action_options['force_event'] ) { |
| 221 |
$post_content['event'] = array( reset( $this->action_options['event'] ) ); |
| 222 |
} |
| 223 |
|
| 224 |
$form_action = array( |
| 225 |
'post_title' => $this->name, |
| 226 |
'post_content' => $post_content, |
| 227 |
'post_excerpt' => $this->id_base, |
| 228 |
'ID' => '', |
| 229 |
'post_status' => 'publish', |
| 230 |
'post_type' => FrmFormActionsController::$action_post_type, |
| 231 |
'post_name' => $this->form_id . '_' . $this->id_base . '_' . $this->number, |
| 232 |
'menu_order' => $this->form_id, |
| 233 |
); |
| 234 |
unset( $post_content ); |
| 235 |
|
| 236 |
return (object) $form_action; |
| 237 |
} |
| 238 |
|
| 239 |
public function create( $form_id ) { |
| 240 |
$this->form_id = $form_id; |
| 241 |
|
| 242 |
$action = $this->prepare_new(); |
| 243 |
|
| 244 |
return $this->save_settings( $action ); |
| 245 |
} |
| 246 |
|
| 247 |
public function duplicate_form_actions( $form_id, $old_id ) { |
| 248 |
if ( $form_id == $old_id ) { |
| 249 |
// don't duplicate the actions if this is a template getting updated |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
$this->form_id = $old_id; |
| 254 |
$actions = $this->get_all( $old_id ); |
| 255 |
|
| 256 |
$this->form_id = $form_id; |
| 257 |
foreach ( $actions as $action ) { |
| 258 |
$this->duplicate_one( $action, $form_id ); |
| 259 |
unset( $action ); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/* Check if imported action should be created or updated |
| 264 |
* |
| 265 |
* @since 2.0 |
| 266 |
* |
| 267 |
* @param array $action |
| 268 |
* @return integer $post_id |
| 269 |
*/ |
| 270 |
public function maybe_create_action( $action, $forms ) { |
| 271 |
if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] == 'updated' ) { |
| 272 |
// Update action only |
| 273 |
$action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] ); |
| 274 |
$post_id = $this->save_settings( $action ); |
| 275 |
} else { |
| 276 |
// Create action |
| 277 |
$action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] ); |
| 278 |
$post_id = $this->duplicate_one( (object) $action, $action['menu_order'] ); |
| 279 |
} |
| 280 |
|
| 281 |
return $post_id; |
| 282 |
} |
| 283 |
|
| 284 |
public function duplicate_one( $action, $form_id ) { |
| 285 |
global $frm_duplicate_ids; |
| 286 |
|
| 287 |
$action->menu_order = $form_id; |
| 288 |
$switch = $this->get_global_switch_fields(); |
| 289 |
|
| 290 |
foreach ( (array) $action->post_content as $key => $val ) { |
| 291 |
if ( is_numeric( $val ) && isset( $frm_duplicate_ids[ $val ] ) ) { |
| 292 |
$action->post_content[ $key ] = $frm_duplicate_ids[ $val ]; |
| 293 |
} elseif ( ! is_array( $val ) ) { |
| 294 |
$action->post_content[ $key ] = FrmFieldsHelper::switch_field_ids( $val ); |
| 295 |
} elseif ( isset( $switch[ $key ] ) && is_array( $switch[ $key ] ) ) { |
| 296 |
// loop through each value if empty |
| 297 |
if ( empty( $switch[ $key ] ) ) { |
| 298 |
$switch[ $key ] = array_keys( $val ); |
| 299 |
} |
| 300 |
|
| 301 |
foreach ( $switch[ $key ] as $subkey ) { |
| 302 |
$action->post_content[ $key ] = $this->duplicate_array_walk( $action->post_content[ $key ], $subkey, $val ); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
unset( $key, $val ); |
| 307 |
} |
| 308 |
unset( $action->ID ); |
| 309 |
|
| 310 |
return $this->save_settings( $action ); |
| 311 |
} |
| 312 |
|
| 313 |
private function duplicate_array_walk( $action, $subkey, $val ) { |
| 314 |
global $frm_duplicate_ids; |
| 315 |
|
| 316 |
if ( is_array( $subkey ) ) { |
| 317 |
foreach ( $subkey as $subkey2 ) { |
| 318 |
foreach ( (array) $val as $ck => $cv ) { |
| 319 |
if ( is_array( $cv ) ) { |
| 320 |
$action[ $ck ] = $this->duplicate_array_walk( $action[ $ck ], $subkey2, $cv ); |
| 321 |
} elseif ( isset( $cv[ $subkey ] ) && is_numeric( $cv[ $subkey ] ) && isset( $frm_duplicate_ids[ $cv[ $subkey ] ] ) ) { |
| 322 |
$action[ $ck ][ $subkey ] = $frm_duplicate_ids[ $cv[ $subkey ] ]; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
} else { |
| 327 |
foreach ( (array) $val as $ck => $cv ) { |
| 328 |
if ( is_array( $cv ) ) { |
| 329 |
$action[ $ck ] = $this->duplicate_array_walk( $action[ $ck ], $subkey, $cv ); |
| 330 |
} elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) { |
| 331 |
$action[ $ck ] = $frm_duplicate_ids[ $cv ]; |
| 332 |
} elseif ( $ck == $subkey ) { |
| 333 |
$action[ $ck ] = $this->maybe_switch_field_ids( $action[ $ck ] ); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
return $action; |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Deal with changed settings. |
| 343 |
* |
| 344 |
* Do NOT over-ride this function |
| 345 |
*/ |
| 346 |
public function update_callback( $form_id ) { |
| 347 |
$this->form_id = $form_id; |
| 348 |
|
| 349 |
$all_instances = $this->get_settings(); |
| 350 |
|
| 351 |
// We need to update the data |
| 352 |
if ( $this->updated ) { |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 357 |
if ( isset( $_POST[ $this->option_name ] ) && is_array( $_POST[ $this->option_name ] ) ) { |
| 358 |
// Sanitizing removes scripts and <email> type of values. |
| 359 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing |
| 360 |
$settings = wp_unslash( $_POST[ $this->option_name ] ); |
| 361 |
} else { |
| 362 |
return; |
| 363 |
} |
| 364 |
|
| 365 |
$action_ids = array(); |
| 366 |
|
| 367 |
foreach ( $settings as $number => $new_instance ) { |
| 368 |
$this->_set( $number ); |
| 369 |
|
| 370 |
$old_instance = isset( $all_instances[ $number ] ) ? $all_instances[ $number ] : array(); |
| 371 |
|
| 372 |
if ( ! isset( $new_instance['post_status'] ) ) { |
| 373 |
$new_instance['post_status'] = 'draft'; |
| 374 |
} |
| 375 |
|
| 376 |
// settings were never opened, so don't update |
| 377 |
if ( ! isset( $new_instance['post_title'] ) ) { |
| 378 |
$this->maybe_update_status( $new_instance, $old_instance ); |
| 379 |
$action_ids[] = $new_instance['ID']; |
| 380 |
$this->updated = true; |
| 381 |
continue; |
| 382 |
} |
| 383 |
|
| 384 |
$new_instance['post_type'] = FrmFormActionsController::$action_post_type; |
| 385 |
$new_instance['post_name'] = $this->form_id . '_' . $this->id_base . '_' . $this->number; |
| 386 |
$new_instance['menu_order'] = $this->form_id; |
| 387 |
$new_instance['post_date'] = isset( $old_instance->post_date ) ? $old_instance->post_date : ''; |
| 388 |
$instance = $this->update( $new_instance, $old_instance ); |
| 389 |
|
| 390 |
/** |
| 391 |
* Filter an action's settings before saving. |
| 392 |
* |
| 393 |
* Returning false will effectively short-circuit the widget's ability |
| 394 |
* to update settings. |
| 395 |
* |
| 396 |
* @since 2.0 |
| 397 |
* |
| 398 |
* @param array $instance The current widget instance's settings. |
| 399 |
* @param array $new_instance Array of new widget settings. |
| 400 |
* @param array $old_instance Array of old widget settings. |
| 401 |
* @param WP_Widget $this The current widget instance. |
| 402 |
*/ |
| 403 |
$instance = apply_filters( 'frm_action_update_callback', $instance, $new_instance, $old_instance, $this ); |
| 404 |
|
| 405 |
$instance['post_content'] = apply_filters( 'frm_before_save_action', $instance['post_content'], $instance, $new_instance, $old_instance, $this ); |
| 406 |
$instance['post_content'] = apply_filters( 'frm_before_save_' . $this->id_base . '_action', $instance['post_content'], $instance, $new_instance, $old_instance, $this ); |
| 407 |
|
| 408 |
if ( false !== $instance ) { |
| 409 |
$all_instances[ $number ] = $instance; |
| 410 |
} |
| 411 |
|
| 412 |
$action_ids[] = $this->save_settings( $instance ); |
| 413 |
|
| 414 |
$this->updated = true; |
| 415 |
} |
| 416 |
|
| 417 |
return $action_ids; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* If the status of the action has changed, update it |
| 422 |
* |
| 423 |
* @since 3.04 |
| 424 |
* |
| 425 |
* @param array $new_instance |
| 426 |
* @param stdClass|array $old_instance |
| 427 |
* @return void |
| 428 |
*/ |
| 429 |
protected function maybe_update_status( $new_instance, $old_instance ) { |
| 430 |
if ( ! is_object( $old_instance ) || $new_instance['post_status'] === $old_instance->post_status ) { |
| 431 |
return; |
| 432 |
} |
| 433 |
|
| 434 |
self::clear_cache(); |
| 435 |
wp_update_post( |
| 436 |
array( |
| 437 |
'ID' => $new_instance['ID'], |
| 438 |
'post_status' => $new_instance['post_status'], |
| 439 |
) |
| 440 |
); |
| 441 |
} |
| 442 |
|
| 443 |
public function save_settings( $settings ) { |
| 444 |
self::clear_cache(); |
| 445 |
|
| 446 |
return FrmDb::save_settings( $settings, 'frm_actions' ); |
| 447 |
} |
| 448 |
|
| 449 |
public function get_single_action( $id ) { |
| 450 |
$action = get_post( $id ); |
| 451 |
if ( $action ) { |
| 452 |
$action = $this->prepare_action( $action ); |
| 453 |
$this->_set( $id ); |
| 454 |
} |
| 455 |
|
| 456 |
return $action; |
| 457 |
} |
| 458 |
|
| 459 |
public function get_one( $form_id ) { |
| 460 |
return $this->get_all( $form_id, 1 ); |
| 461 |
} |
| 462 |
|
| 463 |
public static function get_action_for_form( $form_id, $type = 'all', $atts = array() ) { |
| 464 |
$action_controls = FrmFormActionsController::get_form_actions( $type ); |
| 465 |
if ( empty( $action_controls ) ) { |
| 466 |
// don't continue if there are no available actions |
| 467 |
return array(); |
| 468 |
} |
| 469 |
|
| 470 |
if ( 'all' != $type ) { |
| 471 |
return $action_controls->get_all( $form_id, $atts ); |
| 472 |
} |
| 473 |
|
| 474 |
self::prepare_get_action( $atts ); |
| 475 |
|
| 476 |
$limit = apply_filters( 'frm_form_action_limit', $atts['limit'], compact( 'type', 'form_id' ) ); |
| 477 |
|
| 478 |
$args = self::action_args( $form_id, $limit ); |
| 479 |
$args['post_status'] = $atts['post_status']; |
| 480 |
$actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' ); |
| 481 |
|
| 482 |
if ( ! $actions ) { |
| 483 |
return array(); |
| 484 |
} |
| 485 |
|
| 486 |
$settings = array(); |
| 487 |
foreach ( $actions as $action ) { |
| 488 |
// some plugins/themes are formatting the post_excerpt |
| 489 |
$action->post_excerpt = sanitize_title( $action->post_excerpt ); |
| 490 |
|
| 491 |
if ( ! isset( $action_controls[ $action->post_excerpt ] ) ) { |
| 492 |
continue; |
| 493 |
} |
| 494 |
|
| 495 |
$action = $action_controls[ $action->post_excerpt ]->prepare_action( $action ); |
| 496 |
$settings[ $action->ID ] = $action; |
| 497 |
|
| 498 |
if ( count( $settings ) >= $limit ) { |
| 499 |
break; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
if ( 1 === $limit ) { |
| 504 |
$settings = reset( $settings ); |
| 505 |
} |
| 506 |
|
| 507 |
return $settings; |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* @since 3.04 |
| 512 |
* @param array $args |
| 513 |
* @param string $default_status |
| 514 |
*/ |
| 515 |
protected static function prepare_get_action( &$args, $default_status = 'publish' ) { |
| 516 |
if ( is_numeric( $args ) ) { |
| 517 |
// for reverse compatibility. $limit was changed to $args |
| 518 |
$args = array( |
| 519 |
'limit' => $args, |
| 520 |
); |
| 521 |
} |
| 522 |
$defaults = array( |
| 523 |
'limit' => 99, |
| 524 |
'post_status' => $default_status, |
| 525 |
); |
| 526 |
$args = wp_parse_args( $args, $defaults ); |
| 527 |
} |
| 528 |
|
| 529 |
/** |
| 530 |
* @param int $action_id |
| 531 |
*/ |
| 532 |
public static function get_single_action_type( $action_id, $type ) { |
| 533 |
if ( ! $type ) { |
| 534 |
return false; |
| 535 |
} |
| 536 |
$action_control = FrmFormActionsController::get_form_actions( $type ); |
| 537 |
|
| 538 |
return $action_control->get_single_action( $action_id ); |
| 539 |
} |
| 540 |
|
| 541 |
/** |
| 542 |
* @param int $form_id |
| 543 |
* |
| 544 |
* @return bool |
| 545 |
*/ |
| 546 |
public static function form_has_action_type( $form_id, $type ) { |
| 547 |
$payment_actions = self::get_action_for_form( $form_id, $type ); |
| 548 |
|
| 549 |
return ! empty( $payment_actions ); |
| 550 |
} |
| 551 |
|
| 552 |
public function get_all( $form_id = false, $atts = array() ) { |
| 553 |
self::prepare_get_action( $atts, 'any' ); |
| 554 |
$limit = $atts['limit']; |
| 555 |
|
| 556 |
if ( $form_id ) { |
| 557 |
$this->form_id = $form_id; |
| 558 |
} |
| 559 |
|
| 560 |
$type = $this->id_base; |
| 561 |
|
| 562 |
global $frm_vars; |
| 563 |
$frm_vars['action_type'] = $type; |
| 564 |
|
| 565 |
add_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' ); |
| 566 |
$query = self::action_args( $form_id, $limit ); |
| 567 |
$query['post_status'] = $atts['post_status']; |
| 568 |
$query['suppress_filters'] = false; |
| 569 |
|
| 570 |
$actions = FrmDb::check_cache( json_encode( $query ) . '_type_' . $type, 'frm_actions', $query, 'get_posts' ); |
| 571 |
unset( $query ); |
| 572 |
|
| 573 |
remove_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' ); |
| 574 |
|
| 575 |
if ( empty( $actions ) ) { |
| 576 |
return array(); |
| 577 |
} |
| 578 |
|
| 579 |
$settings = array(); |
| 580 |
foreach ( $actions as $action ) { |
| 581 |
if ( count( $settings ) >= $limit ) { |
| 582 |
continue; |
| 583 |
} |
| 584 |
|
| 585 |
$action = $this->prepare_action( $action ); |
| 586 |
|
| 587 |
$settings[ $action->ID ] = $action; |
| 588 |
} |
| 589 |
|
| 590 |
if ( 1 === $limit ) { |
| 591 |
$settings = reset( $settings ); |
| 592 |
} |
| 593 |
|
| 594 |
return $settings; |
| 595 |
} |
| 596 |
|
| 597 |
public static function action_args( $form_id = 0, $limit = 99 ) { |
| 598 |
$args = array( |
| 599 |
'post_type' => FrmFormActionsController::$action_post_type, |
| 600 |
'post_status' => 'publish', |
| 601 |
'numberposts' => $limit, |
| 602 |
'orderby' => 'title', |
| 603 |
'order' => 'ASC', |
| 604 |
); |
| 605 |
|
| 606 |
if ( $form_id && $form_id != 'all' ) { |
| 607 |
$args['menu_order'] = $form_id; |
| 608 |
} |
| 609 |
|
| 610 |
return $args; |
| 611 |
} |
| 612 |
|
| 613 |
public function prepare_action( $action ) { |
| 614 |
$action->post_content = (array) FrmAppHelper::maybe_json_decode( $action->post_content ); |
| 615 |
$action->post_excerpt = sanitize_title( $action->post_excerpt ); |
| 616 |
|
| 617 |
$default_values = $this->get_global_defaults(); |
| 618 |
|
| 619 |
// fill default values |
| 620 |
$action->post_content += $default_values; |
| 621 |
|
| 622 |
foreach ( $default_values as $k => $vals ) { |
| 623 |
if ( is_array( $vals ) && ! empty( $vals ) ) { |
| 624 |
if ( 'event' == $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) { |
| 625 |
continue; |
| 626 |
} |
| 627 |
$action->post_content[ $k ] = wp_parse_args( $action->post_content[ $k ], $vals ); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
if ( ! is_array( $action->post_content['event'] ) ) { |
| 632 |
$action->post_content['event'] = explode( ',', $action->post_content['event'] ); |
| 633 |
} |
| 634 |
|
| 635 |
return $action; |
| 636 |
} |
| 637 |
|
| 638 |
public function destroy( $form_id = false, $type = 'default' ) { |
| 639 |
global $wpdb; |
| 640 |
|
| 641 |
$this->form_id = $form_id; |
| 642 |
|
| 643 |
$query = array( 'post_type' => FrmFormActionsController::$action_post_type ); |
| 644 |
if ( $form_id ) { |
| 645 |
$query['menu_order'] = $form_id; |
| 646 |
} |
| 647 |
if ( 'all' != $type ) { |
| 648 |
$query['post_excerpt'] = $this->id_base; |
| 649 |
} |
| 650 |
|
| 651 |
$post_ids = FrmDb::get_col( $wpdb->posts, $query, 'ID' ); |
| 652 |
|
| 653 |
foreach ( $post_ids as $id ) { |
| 654 |
wp_delete_post( $id ); |
| 655 |
} |
| 656 |
self::clear_cache(); |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* Delete the action cache when a form action is created, deleted, or updated |
| 661 |
* |
| 662 |
* @since 2.0.5 |
| 663 |
*/ |
| 664 |
public static function clear_cache() { |
| 665 |
FrmDb::cache_delete_group( 'frm_actions' ); |
| 666 |
} |
| 667 |
|
| 668 |
public function get_settings() { |
| 669 |
return self::get_action_for_form( $this->form_id, $this->id_base ); |
| 670 |
} |
| 671 |
|
| 672 |
public function get_global_defaults() { |
| 673 |
$defaults = $this->get_defaults(); |
| 674 |
|
| 675 |
if ( ! isset( $defaults['event'] ) ) { |
| 676 |
$defaults['event'] = array( 'create' ); |
| 677 |
} |
| 678 |
|
| 679 |
if ( ! isset( $defaults['conditions'] ) ) { |
| 680 |
$defaults['conditions'] = array( |
| 681 |
'send_stop' => '', |
| 682 |
'any_all' => '', |
| 683 |
); |
| 684 |
} |
| 685 |
|
| 686 |
return $defaults; |
| 687 |
} |
| 688 |
|
| 689 |
public function get_global_switch_fields() { |
| 690 |
$switch = $this->get_switch_fields(); |
| 691 |
$switch['conditions'] = array( 'hide_field' ); |
| 692 |
|
| 693 |
$switch = apply_filters( 'frm_global_switch_fields', $switch ); |
| 694 |
|
| 695 |
return $switch; |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Migrate settings from form->options into new action. |
| 700 |
*/ |
| 701 |
public function migrate_to_2( $form, $update = 'update' ) { |
| 702 |
$action = $this->prepare_new( $form->id ); |
| 703 |
FrmAppHelper::unserialize_or_decode( $form->options ); |
| 704 |
|
| 705 |
// fill with existing options |
| 706 |
foreach ( $action->post_content as $name => $val ) { |
| 707 |
if ( isset( $form->options[ $name ] ) ) { |
| 708 |
$action->post_content[ $name ] = $form->options[ $name ]; |
| 709 |
unset( $form->options[ $name ] ); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
$action = $this->migrate_values( $action, $form ); |
| 714 |
|
| 715 |
// check if action already exists |
| 716 |
$post_id = get_posts( |
| 717 |
array( |
| 718 |
'name' => $action->post_name, |
| 719 |
'post_type' => FrmFormActionsController::$action_post_type, |
| 720 |
'post_status' => $action->post_status, |
| 721 |
'numberposts' => 1, |
| 722 |
) |
| 723 |
); |
| 724 |
|
| 725 |
if ( empty( $post_id ) ) { |
| 726 |
// create action now |
| 727 |
$post_id = $this->save_settings( $action ); |
| 728 |
} |
| 729 |
|
| 730 |
if ( $post_id && 'update' == $update ) { |
| 731 |
global $wpdb; |
| 732 |
$form->options = maybe_serialize( $form->options ); |
| 733 |
|
| 734 |
// update form options |
| 735 |
$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) ); |
| 736 |
FrmForm::clear_form_cache(); |
| 737 |
} |
| 738 |
|
| 739 |
return $post_id; |
| 740 |
} |
| 741 |
|
| 742 |
public static function action_conditions_met( $action, $entry ) { |
| 743 |
if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) { |
| 744 |
return FrmProFormActionsController::action_conditions_met( $action, $entry ); |
| 745 |
} |
| 746 |
|
| 747 |
// This is here for reverse compatibility. |
| 748 |
$notification = $action->post_content; |
| 749 |
$stop = false; |
| 750 |
$met = array(); |
| 751 |
|
| 752 |
if ( ! isset( $notification['conditions'] ) || empty( $notification['conditions'] ) ) { |
| 753 |
return $stop; |
| 754 |
} |
| 755 |
|
| 756 |
foreach ( $notification['conditions'] as $k => $condition ) { |
| 757 |
if ( ! is_numeric( $k ) ) { |
| 758 |
continue; |
| 759 |
} |
| 760 |
|
| 761 |
if ( $stop && 'any' == $notification['conditions']['any_all'] && 'stop' == $notification['conditions']['send_stop'] ) { |
| 762 |
continue; |
| 763 |
} |
| 764 |
|
| 765 |
self::prepare_logic_value( $condition['hide_opt'], $action, $entry ); |
| 766 |
|
| 767 |
$observed_value = self::get_value_from_entry( $entry, $condition['hide_field'] ); |
| 768 |
|
| 769 |
$stop = FrmFieldsHelper::value_meets_condition( $observed_value, $condition['hide_field_cond'], $condition['hide_opt'] ); |
| 770 |
|
| 771 |
if ( $notification['conditions']['send_stop'] == 'send' ) { |
| 772 |
$stop = $stop ? false : true; |
| 773 |
} |
| 774 |
|
| 775 |
$met[ $stop ] = $stop; |
| 776 |
} |
| 777 |
|
| 778 |
if ( $notification['conditions']['any_all'] == 'all' && ! empty( $met ) && isset( $met[0] ) && isset( $met[1] ) ) { |
| 779 |
$stop = ( $notification['conditions']['send_stop'] == 'send' ); |
| 780 |
} elseif ( $notification['conditions']['any_all'] == 'any' && $notification['conditions']['send_stop'] == 'send' && isset( $met[0] ) ) { |
| 781 |
$stop = false; |
| 782 |
} |
| 783 |
|
| 784 |
return $stop; |
| 785 |
} |
| 786 |
|
| 787 |
/** |
| 788 |
* Prepare the logic value for comparison against the entered value |
| 789 |
* |
| 790 |
* @since 2.01.02 |
| 791 |
* @deprecated 4.06.02 |
| 792 |
* |
| 793 |
* @param array|string $logic_value |
| 794 |
*/ |
| 795 |
private static function prepare_logic_value( &$logic_value, $action, $entry ) { |
| 796 |
if ( is_array( $logic_value ) ) { |
| 797 |
$logic_value = reset( $logic_value ); |
| 798 |
} |
| 799 |
|
| 800 |
if ( $logic_value == 'current_user' ) { |
| 801 |
$logic_value = get_current_user_id(); |
| 802 |
} |
| 803 |
|
| 804 |
$logic_value = apply_filters( 'frm_content', $logic_value, $action->menu_order, $entry ); |
| 805 |
|
| 806 |
/** |
| 807 |
* @since 4.04.05 |
| 808 |
*/ |
| 809 |
$logic_value = apply_filters( 'frm_action_logic_value', $logic_value ); |
| 810 |
} |
| 811 |
|
| 812 |
/** |
| 813 |
* Get the value from a specific field and entry |
| 814 |
* |
| 815 |
* @since 2.01.02 |
| 816 |
* @deprecated 4.06.02 |
| 817 |
* |
| 818 |
* @param object $entry |
| 819 |
* @param int $field_id |
| 820 |
* |
| 821 |
* @return array|bool|mixed|string |
| 822 |
*/ |
| 823 |
private static function get_value_from_entry( $entry, $field_id ) { |
| 824 |
$observed_value = ''; |
| 825 |
|
| 826 |
if ( isset( $entry->metas[ $field_id ] ) ) { |
| 827 |
$observed_value = $entry->metas[ $field_id ]; |
| 828 |
} elseif ( $entry->post_id && FrmAppHelper::pro_is_installed() ) { |
| 829 |
$field = FrmField::getOne( $field_id ); |
| 830 |
$observed_value = FrmProEntryMetaHelper::get_post_or_meta_value( |
| 831 |
$entry, |
| 832 |
$field, |
| 833 |
array( |
| 834 |
'links' => false, |
| 835 |
'truncate' => false, |
| 836 |
) |
| 837 |
); |
| 838 |
} |
| 839 |
|
| 840 |
return $observed_value; |
| 841 |
} |
| 842 |
|
| 843 |
public static function default_action_opts( $class = '' ) { |
| 844 |
return array( |
| 845 |
'classes' => 'frm_icon_font ' . $class, |
| 846 |
'active' => false, |
| 847 |
'limit' => 0, |
| 848 |
); |
| 849 |
} |
| 850 |
|
| 851 |
public static function trigger_labels() { |
| 852 |
$triggers = array( |
| 853 |
'draft' => __( 'Draft is saved', 'formidable' ), |
| 854 |
'create' => __( 'Entry is created', 'formidable' ), |
| 855 |
'update' => __( 'Entry is updated', 'formidable' ), |
| 856 |
'delete' => __( 'Entry is deleted', 'formidable' ), |
| 857 |
'import' => __( 'Entry is imported', 'formidable' ), |
| 858 |
); |
| 859 |
|
| 860 |
return apply_filters( 'frm_action_triggers', $triggers ); |
| 861 |
} |
| 862 |
|
| 863 |
public function render_conditional_logic_call_to_action() { |
| 864 |
?> |
| 865 |
<h3> |
| 866 |
<a href="javascript:void(0)" class="frm_show_upgrade frm_noallow" data-upgrade="<?php echo esc_attr( $this->get_upgrade_text() ); ?>" data-medium="conditional-<?php echo esc_attr( $this->id_base ); ?>"> |
| 867 |
<?php esc_html_e( 'Use Conditional Logic', 'formidable' ); ?> |
| 868 |
</a> |
| 869 |
</h3> |
| 870 |
<?php |
| 871 |
} |
| 872 |
|
| 873 |
protected function get_upgrade_text() { |
| 874 |
return __( 'Conditional form actions', 'formidable' ); |
| 875 |
} |
| 876 |
} |
| 877 |
|