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