| @@ -4,19 +4,71 @@ | ||
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | class FrmFormAction { |
| 7 | 7 | |
| 8 | - public $id_base; // Root id for all actions of this type. | |
| 9 | - public $name; // Name for this action type. | |
| 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 | + */ | |
| 10 | 27 | 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 | 28 | |
| 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. | |
| 29 | + /** | |
| 30 | + * Option array passed to wp_register_sidebar_widget(). | |
| 31 | + * | |
| 32 | + * @var array | |
| 33 | + */ | |
| 34 | + public $action_options; | |
| 18 | 35 | |
| 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 | + | |
| 19 | 71 | // Member functions that you must over-ride. |
| 20 | 72 | |
| 21 | 73 | /** |
| 22 | 74 | * This function should check that $new_instance is set correctly. |
| @@ -22,10 +74,10 @@ | ||
| 22 | 74 | * This function should check that $new_instance is set correctly. |
| 23 | 75 | * The newly calculated value of $instance should be returned. |
| 24 | 76 | * If "false" is returned, the instance won't be saved/updated. |
| 25 | 77 | * |
| 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 | |
| 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. | |
| 28 | 80 | * |
| 29 | 81 | * @return array Settings to save or bool false to cancel saving |
| 30 | 82 | */ |
| 31 | 83 | public function update( $new_instance, $old_instance ) { |
| @@ -34,9 +86,9 @@ | ||
| 34 | 86 | |
| 35 | 87 | /** |
| 36 | 88 | * Echo the settings update form |
| 37 | 89 | * |
| 38 | - * @param WP_Post $instance Current settings | |
| 90 | + * @param WP_Post $instance Current settings. | |
| 39 | 91 | * @param array $args |
| 40 | 92 | */ |
| 41 | 93 | public function form( $instance, $args = array() ) { |
| 42 | 94 | echo '<p class="no-options-widget">' . esc_html__( 'There are no options for this action.', 'formidable' ) . '</p>'; |
| @@ -69,14 +121,14 @@ | ||
| 69 | 121 | * |
| 70 | 122 | * @param string $id_base Optional Base ID for the widget, lower case, |
| 71 | 123 | * if left empty a portion of the widget's class name will be used. Has to be unique. |
| 72 | 124 | * @param string $name Name for the widget displayed on the configuration page. |
| 73 | - * @param array $action_options Optional Passed to wp_register_sidebar_widget() | |
| 74 | - * - description: shown on the configuration page | |
| 75 | - * - classname | |
| 76 | - * @param array $control_options Optional Passed to wp_register_widget_control() | |
| 77 | - * - width: required if more than 250px | |
| 78 | - * - height: currently not used but may be needed in the future | |
| 125 | + * @param array $action_options Optional Passed to wp_register_sidebar_widget(). | |
| 126 | + * - description: shown on the configuration page. | |
| 127 | + * - classname. | |
| 128 | + * @param array $control_options Optional Passed to wp_register_widget_control(). | |
| 129 | + * - width: required if more than 250px. | |
| 130 | + * - height: currently not used but may be needed in the future. | |
| 79 | 131 | */ |
| 80 | 132 | public function __construct( $id_base, $name, $action_options = array(), $control_options = array() ) { |
| 81 | 133 | if ( ! defined( 'ABSPATH' ) ) { |
| 82 | 134 | die( 'You are not allowed to call this page directly.' ); |
| @@ -112,11 +164,11 @@ | ||
| 112 | 164 | } |
| 113 | 165 | |
| 114 | 166 | $upgrade_class = isset( $action_options['classes'] ) && $action_options['classes'] === 'frm_show_upgrade'; |
| 115 | 167 | if ( $action_options['group'] === $id_base ) { |
| 116 | - $upgrade_class = strpos( $action_options['classes'], 'frm_show_upgrade' ) !== false; | |
| 168 | + $upgrade_class = strpos( $action_options['classes'], 'frm_show_upgrade' ) !== false; | |
| 117 | 169 | $action_options['classes'] = $group['icon']; |
| 118 | - } elseif ( ! isset( $action_options['classes'] ) || empty( $action_options['classes'] ) || $upgrade_class ) { | |
| 170 | + } elseif ( empty( $action_options['classes'] ) || $upgrade_class ) { | |
| 119 | 171 | $action_options['classes'] = $group['icon']; |
| 120 | 172 | } |
| 121 | 173 | |
| 122 | 174 | if ( $upgrade_class ) { |
| @@ -136,9 +188,9 @@ | ||
| 136 | 188 | |
| 137 | 189 | /** |
| 138 | 190 | * Help to switch old field id by new field id for duplicate form |
| 139 | 191 | * |
| 140 | - * @param string $action id of the field that needs to be switched | |
| 192 | + * @param string $action id of the field that needs to be switched. | |
| 141 | 193 | * |
| 142 | 194 | * @return string |
| 143 | 195 | */ |
| 144 | 196 | public function maybe_switch_field_ids( $action ) { |
| @@ -177,14 +229,14 @@ | ||
| 177 | 229 | * Constructs name attributes for use in form() fields |
| 178 | 230 | * |
| 179 | 231 | * This function should be used in form() methods to create name attributes for fields to be saved by update() |
| 180 | 232 | * |
| 181 | - * @param string $field_name Field name | |
| 233 | + * @param string $field_name Field name. | |
| 182 | 234 | * |
| 183 | 235 | * @return string Name attribute for $field_name |
| 184 | 236 | */ |
| 185 | 237 | public function get_field_name( $field_name, $post_field = 'post_content' ) { |
| 186 | - $name = $this->option_name . '[' . $this->number . ']'; | |
| 238 | + $name = $this->option_name . '[' . $this->number . ']'; | |
| 187 | 239 | $name .= ( empty( $post_field ) ? '' : '[' . $post_field . ']' ); |
| 188 | 240 | $name .= '[' . $field_name . ']'; |
| 189 | 241 | |
| 190 | 242 | return $name; |
| @@ -194,9 +246,9 @@ | ||
| 194 | 246 | * Constructs id attributes for use in form() fields |
| 195 | 247 | * |
| 196 | 248 | * This function should be used in form() methods to create id attributes for fields to be saved by update() |
| 197 | 249 | * |
| 198 | - * @param string $field_name Field name | |
| 250 | + * @param string $field_name Field name. | |
| 199 | 251 | * |
| 200 | 252 | * @return string ID attribute for $field_name |
| 201 | 253 | */ |
| 202 | 254 | public function get_field_id( $field_name ) { |
| @@ -271,17 +323,18 @@ | ||
| 271 | 323 | unset( $action ); |
| 272 | 324 | } |
| 273 | 325 | } |
| 274 | 326 | |
| 275 | - /* Check if imported action should be created or updated | |
| 327 | + /** | |
| 328 | + * Check if imported action should be created or updated. | |
| 276 | 329 | * |
| 277 | 330 | * @since 2.0 |
| 278 | 331 | * |
| 279 | 332 | * @param array $action |
| 280 | - * @return integer $post_id | |
| 333 | + * @return int $post_id | |
| 281 | 334 | */ |
| 282 | 335 | public function maybe_create_action( $action, $forms ) { |
| 283 | - if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] == 'updated' ) { | |
| 336 | + if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] === 'updated' ) { | |
| 284 | 337 | // Update action only |
| 285 | 338 | $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] ); |
| 286 | 339 | $post_id = $this->save_settings( $action ); |
| 287 | 340 | } else { |
| @@ -347,9 +400,9 @@ | ||
| 347 | 400 | } elseif ( $ck == $subkey ) { |
| 348 | 401 | $action[ $ck ] = $this->maybe_switch_field_ids( $action[ $ck ] ); |
| 349 | 402 | } |
| 350 | 403 | } |
| 351 | - } | |
| 404 | + }//end if | |
| 352 | 405 | |
| 353 | 406 | return $action; |
| 354 | 407 | } |
| 355 | 408 | |
| @@ -399,9 +452,9 @@ | ||
| 399 | 452 | $new_instance['post_type'] = FrmFormActionsController::$action_post_type; |
| 400 | 453 | $new_instance['post_name'] = $this->form_id . '_' . $this->id_base . '_' . $this->number; |
| 401 | 454 | $new_instance['menu_order'] = $this->form_id; |
| 402 | 455 | $new_instance['post_date'] = isset( $old_instance->post_date ) ? $old_instance->post_date : ''; |
| 403 | - $instance = $this->update( $new_instance, $old_instance ); | |
| 456 | + $instance = $this->update( $new_instance, $old_instance ); | |
| 404 | 457 | |
| 405 | 458 | /** |
| 406 | 459 | * Filter an action's settings before saving. |
| 407 | 460 | * |
| @@ -409,12 +462,12 @@ | ||
| 409 | 462 | * to update settings. |
| 410 | 463 | * |
| 411 | 464 | * @since 2.0 |
| 412 | 465 | * |
| 413 | - * @param array $instance The current widget instance's settings. | |
| 414 | - * @param array $new_instance Array of new widget settings. | |
| 415 | - * @param array $old_instance Array of old widget settings. | |
| 416 | - * @param WP_Widget $this The current widget instance. | |
| 466 | + * @param array $instance The current widget instance's settings. | |
| 467 | + * @param array $new_instance Array of new widget settings. | |
| 468 | + * @param array $old_instance Array of old widget settings. | |
| 469 | + * @param FrmFormAction $form_action FrmFormAction instance. | |
| 417 | 470 | */ |
| 418 | 471 | $instance = apply_filters( 'frm_action_update_callback', $instance, $new_instance, $old_instance, $this ); |
| 419 | 472 | |
| 420 | 473 | $instance['post_content'] = apply_filters( 'frm_before_save_action', $instance['post_content'], $instance, $new_instance, $old_instance, $this ); |
| @@ -426,9 +479,9 @@ | ||
| 426 | 479 | |
| 427 | 480 | $action_ids[] = $this->save_settings( $instance ); |
| 428 | 481 | |
| 429 | 482 | $this->updated = true; |
| 430 | - } | |
| 483 | + }//end foreach | |
| 431 | 484 | |
| 432 | 485 | return $action_ids; |
| 433 | 486 | } |
| 434 | 487 | |
| @@ -437,9 +490,9 @@ | ||
| 437 | 490 | * |
| 438 | 491 | * @since 3.04 |
| 439 | 492 | * |
| 440 | 493 | * @param array $new_instance |
| 441 | - * @param stdClass|array $old_instance | |
| 494 | + * @param array|stdClass $old_instance | |
| 442 | 495 | * @return void |
| 443 | 496 | */ |
| 444 | 497 | protected function maybe_update_status( $new_instance, $old_instance ) { |
| 445 | 498 | if ( ! is_object( $old_instance ) || $new_instance['post_status'] === $old_instance->post_status ) { |
| @@ -454,8 +507,12 @@ | ||
| 454 | 507 | ) |
| 455 | 508 | ); |
| 456 | 509 | } |
| 457 | 510 | |
| 511 | + /** | |
| 512 | + * @param array $settings | |
| 513 | + * @return int|WP_Error | |
| 514 | + */ | |
| 458 | 515 | public function save_settings( $settings ) { |
| 459 | 516 | self::clear_cache(); |
| 460 | 517 | |
| 461 | 518 | return FrmDb::save_settings( $settings, 'frm_actions' ); |
| @@ -481,15 +538,15 @@ | ||
| 481 | 538 | // don't continue if there are no available actions |
| 482 | 539 | return array(); |
| 483 | 540 | } |
| 484 | 541 | |
| 485 | - if ( 'all' != $type ) { | |
| 542 | + if ( 'all' !== $type ) { | |
| 486 | 543 | return $action_controls->get_all( $form_id, $atts ); |
| 487 | 544 | } |
| 488 | 545 | |
| 489 | 546 | self::prepare_get_action( $atts ); |
| 490 | 547 | |
| 491 | - $limit = apply_filters( 'frm_form_action_limit', $atts['limit'], compact( 'type', 'form_id' ) ); | |
| 548 | + $limit = self::get_action_limit( $form_id, $atts['limit'] ); | |
| 492 | 549 | |
| 493 | 550 | $args = self::action_args( $form_id, $limit ); |
| 494 | 551 | $args['post_status'] = $atts['post_status']; |
| 495 | 552 | $actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' ); |
| @@ -522,8 +579,24 @@ | ||
| 522 | 579 | return $settings; |
| 523 | 580 | } |
| 524 | 581 | |
| 525 | 582 | /** |
| 583 | + * Get the limit for the number of actions for a single form. By default, this is 99, but | |
| 584 | + * it can be modified with a code snippet. | |
| 585 | + * | |
| 586 | + * @since 6.17 This logic from moved from FrmFormAction::get_action_for_form. | |
| 587 | + * | |
| 588 | + * @param int|string $form_id | |
| 589 | + * @param int|string $limit The unfiltered limit value. | |
| 590 | + * | |
| 591 | + * @return int The filtered limit value. | |
| 592 | + */ | |
| 593 | + public static function get_action_limit( $form_id, $limit = 99 ) { | |
| 594 | + $type = 'all'; | |
| 595 | + return (int) apply_filters( 'frm_form_action_limit', (int) $limit, compact( 'type', 'form_id' ) ); | |
| 596 | + } | |
| 597 | + | |
| 598 | + /** | |
| 526 | 599 | * @since 3.04 |
| 527 | 600 | * |
| 528 | 601 | * @param array $args |
| 529 | 602 | * @param string $default_status |
| @@ -550,8 +623,12 @@ | ||
| 550 | 623 | public static function get_single_action_type( $action_id, $type ) { |
| 551 | 624 | if ( ! $type ) { |
| 552 | 625 | return false; |
| 553 | 626 | } |
| 627 | + | |
| 628 | + /** | |
| 629 | + * @var FrmFormAction | |
| 630 | + */ | |
| 554 | 631 | $action_control = FrmFormActionsController::get_form_actions( $type ); |
| 555 | 632 | |
| 556 | 633 | return $action_control->get_single_action( $action_id ); |
| 557 | 634 | } |
| @@ -585,10 +662,10 @@ | ||
| 585 | 662 | global $frm_vars; |
| 586 | 663 | $frm_vars['action_type'] = $type; |
| 587 | 664 | |
| 588 | 665 | add_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' ); |
| 589 | - $query = self::action_args( $form_id, $limit ); | |
| 590 | - $query['post_status'] = $atts['post_status']; | |
| 666 | + $query = self::action_args( $form_id, $limit ); | |
| 667 | + $query['post_status'] = $atts['post_status']; | |
| 591 | 668 | $query['suppress_filters'] = false; |
| 592 | 669 | |
| 593 | 670 | $actions = FrmDb::check_cache( json_encode( $query ) . '_type_' . $type, 'frm_actions', $query, 'get_posts' ); |
| 594 | 671 | unset( $query ); |
| @@ -636,9 +713,9 @@ | ||
| 636 | 713 | return $args; |
| 637 | 714 | } |
| 638 | 715 | |
| 639 | 716 | /** |
| 640 | - * @param WP_Post|array $action | |
| 717 | + * @param array|WP_Post $action | |
| 641 | 718 | */ |
| 642 | 719 | public function prepare_action( $action ) { |
| 643 | 720 | $action->post_content = (array) FrmAppHelper::maybe_json_decode( $action->post_content ); |
| 644 | 721 | $action->post_excerpt = sanitize_title( $action->post_excerpt ); |
| @@ -735,9 +812,9 @@ | ||
| 735 | 812 | /** |
| 736 | 813 | * Migrate settings from form->options into new action. |
| 737 | 814 | */ |
| 738 | 815 | public function migrate_to_2( $form, $update = 'update' ) { |
| 739 | - $action = $this->prepare_new( $form->id ); | |
| 816 | + $action = $this->prepare_new( $form->id ); | |
| 740 | 817 | FrmAppHelper::unserialize_or_decode( $form->options ); |
| 741 | 818 | |
| 742 | 819 | // fill with existing options |
| 743 | 820 | foreach ( $action->post_content as $name => $val ) { |
| @@ -785,9 +862,9 @@ | ||
| 785 | 862 | $notification = $action->post_content; |
| 786 | 863 | $stop = false; |
| 787 | 864 | $met = array(); |
| 788 | 865 | |
| 789 | - if ( ! isset( $notification['conditions'] ) || empty( $notification['conditions'] ) ) { | |
| 866 | + if ( empty( $notification['conditions'] ) ) { | |
| 790 | 867 | return $stop; |
| 791 | 868 | } |
| 792 | 869 | |
| 793 | 870 | foreach ( $notification['conditions'] as $k => $condition ) { |
| @@ -804,18 +881,18 @@ | ||
| 804 | 881 | $observed_value = self::get_value_from_entry( $entry, $condition['hide_field'] ); |
| 805 | 882 | |
| 806 | 883 | $stop = FrmFieldsHelper::value_meets_condition( $observed_value, $condition['hide_field_cond'], $condition['hide_opt'] ); |
| 807 | 884 | |
| 808 | - if ( $notification['conditions']['send_stop'] == 'send' ) { | |
| 885 | + if ( $notification['conditions']['send_stop'] === 'send' ) { | |
| 809 | 886 | $stop = $stop ? false : true; |
| 810 | 887 | } |
| 811 | 888 | |
| 812 | 889 | $met[ $stop ] = $stop; |
| 813 | - } | |
| 890 | + }//end foreach | |
| 814 | 891 | |
| 815 | - if ( $notification['conditions']['any_all'] == 'all' && ! empty( $met ) && isset( $met[0] ) && isset( $met[1] ) ) { | |
| 816 | - $stop = ( $notification['conditions']['send_stop'] == 'send' ); | |
| 817 | - } elseif ( $notification['conditions']['any_all'] == 'any' && $notification['conditions']['send_stop'] == 'send' && isset( $met[0] ) ) { | |
| 892 | + if ( $notification['conditions']['any_all'] === 'all' && ! empty( $met ) && isset( $met[0] ) && isset( $met[1] ) ) { | |
| 893 | + $stop = ( $notification['conditions']['send_stop'] === 'send' ); | |
| 894 | + } elseif ( $notification['conditions']['any_all'] === 'any' && $notification['conditions']['send_stop'] === 'send' && isset( $met[0] ) ) { | |
| 818 | 895 | $stop = false; |
| 819 | 896 | } |
| 820 | 897 | |
| 821 | 898 | return $stop; |
| @@ -825,10 +902,8 @@ | ||
| 825 | 902 | * Prepare the logic value for comparison against the entered value |
| 826 | 903 | * |
| 827 | 904 | * @since 2.01.02 |
| 828 | 905 | * |
| 829 | - * @deprecated 4.06.02 | |
| 830 | - * | |
| 831 | 906 | * @param array|string $logic_value |
| 832 | 907 | * |
| 833 | 908 | * @return void |
| 834 | 909 | */ |
| @@ -836,9 +911,9 @@ | ||
| 836 | 911 | if ( is_array( $logic_value ) ) { |
| 837 | 912 | $logic_value = reset( $logic_value ); |
| 838 | 913 | } |
| 839 | 914 | |
| 840 | - if ( $logic_value == 'current_user' ) { | |
| 915 | + if ( $logic_value === 'current_user' ) { | |
| 841 | 916 | $logic_value = get_current_user_id(); |
| 842 | 917 | } |
| 843 | 918 | |
| 844 | 919 | $logic_value = apply_filters( 'frm_content', $logic_value, $action->menu_order, $entry ); |
| @@ -852,12 +927,11 @@ | ||
| 852 | 927 | /** |
| 853 | 928 | * Get the value from a specific field and entry |
| 854 | 929 | * |
| 855 | 930 | * @since 2.01.02 |
| 856 | - * @deprecated 4.06.02 | |
| 857 | 931 | * |
| 858 | 932 | * @param object $entry |
| 859 | - * @param int $field_id | |
| 933 | + * @param int $field_id | |
| 860 | 934 | * |
| 861 | 935 | * @return array|bool|mixed|string |
| 862 | 936 | */ |
| 863 | 937 | private static function get_value_from_entry( $entry, $field_id ) { |
| @@ -922,6 +996,25 @@ | ||
| 922 | 996 | * @return string |
| 923 | 997 | */ |
| 924 | 998 | protected function get_upgrade_text() { |
| 925 | 999 | return __( 'Conditional form actions', 'formidable' ); |
| 1000 | + } | |
| 1001 | + | |
| 1002 | + /** | |
| 1003 | + * Gets form fields for form action settings. | |
| 1004 | + * | |
| 1005 | + * @since 6.10 | |
| 1006 | + * | |
| 1007 | + * @param int $form_id Form ID. | |
| 1008 | + * @return object[] | |
| 1009 | + */ | |
| 1010 | + protected function get_form_fields( $form_id ) { | |
| 1011 | + // Get form fields, include embedded and repeater child fields. | |
| 1012 | + $form_fields = FrmField::get_all_for_form( $form_id, '', 'include' ); | |
| 1013 | + return array_filter( | |
| 1014 | + $form_fields, | |
| 1015 | + function ( $form_field ) { | |
| 1016 | + return ! FrmField::is_no_save_field( $form_field->type ); | |
| 1017 | + } | |
| 1018 | + ); | |
| 926 | 1019 | } |
| 927 | 1020 | } |