| @@ -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,15 +34,13 @@ | ||
| 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>'; |
| 42 | + | |
| 97 | 43 | return 'noform'; |
| 98 | 44 | } |
| 99 | 45 | |
| 100 | 46 | /** |
| @@ -103,21 +49,12 @@ | ||
| 103 | 49 | public function get_defaults() { |
| 104 | 50 | return array(); |
| 105 | 51 | } |
| 106 | 52 | |
| 107 | - /** | |
| 108 | - * @return array | |
| 109 | - */ | |
| 110 | 53 | public function get_switch_fields() { |
| 111 | 54 | return array(); |
| 112 | 55 | } |
| 113 | 56 | |
| 114 | - /** | |
| 115 | - * @param object $action | |
| 116 | - * @param object $form | |
| 117 | - * | |
| 118 | - * @return object | |
| 119 | - */ | |
| 120 | 57 | public function migrate_values( $action, $form ) { |
| 121 | 58 | return $action; |
| 122 | 59 | } |
| 123 | 60 | |
| @@ -128,14 +65,14 @@ | ||
| 128 | 65 | * |
| 129 | 66 | * @param string $id_base Optional Base ID for the widget, lower case, |
| 130 | 67 | * if left empty a portion of the widget's class name will be used. Has to be unique. |
| 131 | 68 | * @param string $name Name for the widget displayed on the configuration page. |
| 132 | - * @param array $action_options Optional Passed to wp_register_sidebar_widget(). | |
| 133 | - * - description: shown on the configuration page. | |
| 134 | - * - classname. | |
| 135 | - * @param array $control_options Optional Passed to wp_register_widget_control(). | |
| 136 | - * - width: required if more than 250px. | |
| 137 | - * - 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 | |
| 138 | 75 | */ |
| 139 | 76 | public function __construct( $id_base, $name, $action_options = array(), $control_options = array() ) { |
| 140 | 77 | if ( ! defined( 'ABSPATH' ) ) { |
| 141 | 78 | die( 'You are not allowed to call this page directly.' ); |
| @@ -140,17 +77,9 @@ | ||
| 140 | 77 | if ( ! defined( 'ABSPATH' ) ) { |
| 141 | 78 | die( 'You are not allowed to call this page directly.' ); |
| 142 | 79 | } |
| 143 | 80 | |
| 144 | - $this->id_base = strtolower( $id_base ); | |
| 145 | - | |
| 146 | - /** | |
| 147 | - * @since 6.31 | |
| 148 | - * | |
| 149 | - * @param string $name | |
| 150 | - */ | |
| 151 | - $name = apply_filters( 'frm_' . $id_base . '_action_name', $name ); | |
| 152 | - | |
| 81 | + $this->id_base = strtolower( $id_base ); | |
| 153 | 82 | $this->name = $name; |
| 154 | 83 | $this->option_name = 'frm_' . $this->id_base . '_action'; |
| 155 | 84 | |
| 156 | 85 | $default_options = array( |
| @@ -164,12 +93,8 @@ | ||
| 164 | 93 | 'plugin' => $this->id_base, |
| 165 | 94 | 'tooltip' => $name, |
| 166 | 95 | 'group' => $id_base, |
| 167 | 96 | 'color' => '', |
| 168 | - 'keywords' => '', | |
| 169 | - 'description' => '', | |
| 170 | - 'is_new' => false, | |
| 171 | - 'is_beta' => false, | |
| 172 | 97 | ); |
| 173 | 98 | |
| 174 | 99 | $action_options = apply_filters( 'frm_' . $id_base . '_action_options', $action_options ); |
| 175 | 100 | $group = $this->get_group( $action_options ); |
| @@ -175,20 +100,18 @@ | ||
| 175 | 100 | $group = $this->get_group( $action_options ); |
| 176 | 101 | $action_options['group'] = $group['id']; |
| 177 | 102 | |
| 178 | 103 | if ( ! isset( $action_options['color'] ) ) { |
| 179 | - // Deterministic fallback so all instances of the same action type render with the same color across requests. | |
| 180 | - $colors = array( 'green', 'orange', 'purple' ); | |
| 181 | - $index = abs( crc32( $id_base ) ) % count( $colors ); | |
| 182 | - $action_options['color'] = 'var(--' . $colors[ $index ] . ')'; | |
| 104 | + $colors = array( 'green', 'orange', 'purple' ); | |
| 105 | + shuffle( $colors ); | |
| 106 | + $action_options['color'] = 'var(--' . reset( $colors ) . ')'; | |
| 183 | 107 | } |
| 184 | 108 | |
| 185 | - $upgrade_class = isset( $action_options['classes'] ) && $action_options['classes'] === 'frm_show_upgrade'; | |
| 186 | - | |
| 109 | + $upgrade_class = $action_options['classes'] === 'frm_show_upgrade'; | |
| 187 | 110 | if ( $action_options['group'] === $id_base ) { |
| 188 | - $upgrade_class = str_contains( $action_options['classes'], 'frm_show_upgrade' ); | |
| 111 | + $upgrade_class = strpos( $action_options['classes'], 'frm_show_upgrade' ) !== false; | |
| 189 | 112 | $action_options['classes'] = $group['icon']; |
| 190 | - } elseif ( empty( $action_options['classes'] ) || $upgrade_class ) { | |
| 113 | + } elseif ( ! isset( $action_options['classes'] ) || empty( $action_options['classes'] ) || $upgrade_class ) { | |
| 191 | 114 | $action_options['classes'] = $group['icon']; |
| 192 | 115 | } |
| 193 | 116 | |
| 194 | 117 | if ( $upgrade_class ) { |
| @@ -200,13 +123,8 @@ | ||
| 200 | 123 | } |
| 201 | 124 | |
| 202 | 125 | /** |
| 203 | 126 | * @param string $id_base |
| 204 | - * @param string $name | |
| 205 | - * @param array $action_options | |
| 206 | - * @param array $control_options | |
| 207 | - * | |
| 208 | - * @return void | |
| 209 | 127 | */ |
| 210 | 128 | public function FrmFormAction( $id_base, $name, $action_options = array(), $control_options = array() ) { |
| 211 | 129 | self::__construct( $id_base, $name, $action_options, $control_options ); |
| 212 | 130 | } |
| @@ -213,28 +131,22 @@ | ||
| 213 | 131 | |
| 214 | 132 | /** |
| 215 | 133 | * Help to switch old field id by new field id for duplicate form |
| 216 | 134 | * |
| 217 | - * @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 | |
| 218 | 136 | * |
| 219 | 137 | * @return string |
| 220 | 138 | */ |
| 221 | 139 | public function maybe_switch_field_ids( $action ) { |
| 222 | 140 | $updated_action = apply_filters( 'frm_maybe_switch_field_ids', $action ); |
| 223 | - | |
| 224 | 141 | if ( $updated_action === $action ) { |
| 225 | - return FrmFieldsHelper::switch_field_ids( $action ); | |
| 142 | + $updated_action = FrmFieldsHelper::switch_field_ids( $action ); | |
| 226 | 143 | } |
| 227 | - | |
| 228 | 144 | return $updated_action; |
| 229 | 145 | } |
| 230 | 146 | |
| 231 | 147 | /** |
| 232 | 148 | * @since 4.0 |
| 233 | - * | |
| 234 | - * @param array $action_options | |
| 235 | - * | |
| 236 | - * @return array | |
| 237 | 149 | */ |
| 238 | 150 | protected function get_group( $action_options ) { |
| 239 | 151 | $groups = FrmFormActionsController::form_action_groups(); |
| 240 | 152 | $group = 'misc'; |
| @@ -244,9 +156,9 @@ | ||
| 244 | 156 | } elseif ( isset( $groups[ $this->id_base ] ) ) { |
| 245 | 157 | $group = $this->id_base; |
| 246 | 158 | } else { |
| 247 | 159 | foreach ( $groups as $name => $check_group ) { |
| 248 | - if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'], true ) ) { | |
| 160 | + if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'] ) ) { | |
| 249 | 161 | $group = $name; |
| 250 | 162 | break; |
| 251 | 163 | } |
| 252 | 164 | } |
| @@ -260,18 +172,18 @@ | ||
| 260 | 172 | * Constructs name attributes for use in form() fields |
| 261 | 173 | * |
| 262 | 174 | * This function should be used in form() methods to create name attributes for fields to be saved by update() |
| 263 | 175 | * |
| 264 | - * @param string $field_name Field name. | |
| 265 | - * @param string $post_field | |
| 176 | + * @param string $field_name Field name | |
| 266 | 177 | * |
| 267 | 178 | * @return string Name attribute for $field_name |
| 268 | 179 | */ |
| 269 | 180 | public function get_field_name( $field_name, $post_field = 'post_content' ) { |
| 270 | - $name = $this->option_name . '[' . $this->number . ']'; | |
| 271 | - $name .= $post_field ? '[' . $post_field . ']' : ''; | |
| 181 | + $name = $this->option_name . '[' . $this->number . ']'; | |
| 182 | + $name .= ( empty( $post_field ) ? '' : '[' . $post_field . ']' ); | |
| 183 | + $name .= '[' . $field_name . ']'; | |
| 272 | 184 | |
| 273 | - return $name . ( '[' . $field_name . ']' ); | |
| 185 | + return $name; | |
| 274 | 186 | } |
| 275 | 187 | |
| 276 | 188 | /** |
| 277 | 189 | * Constructs id attributes for use in form() fields |
| @@ -277,9 +189,9 @@ | ||
| 277 | 189 | * Constructs id attributes for use in form() fields |
| 278 | 190 | * |
| 279 | 191 | * This function should be used in form() methods to create id attributes for fields to be saved by update() |
| 280 | 192 | * |
| 281 | - * @param string $field_name Field name. | |
| 193 | + * @param string $field_name Field name | |
| 282 | 194 | * |
| 283 | 195 | * @return string ID attribute for $field_name |
| 284 | 196 | */ |
| 285 | 197 | public function get_field_id( $field_name ) { |
| @@ -285,23 +197,15 @@ | ||
| 285 | 197 | public function get_field_id( $field_name ) { |
| 286 | 198 | return $field_name . '_' . $this->number; |
| 287 | 199 | } |
| 288 | 200 | |
| 289 | - /** | |
| 290 | - * @param int|string $number | |
| 291 | - * | |
| 292 | - * @return void | |
| 293 | - */ | |
| 201 | + // Private Function. Don't worry about this. | |
| 202 | + | |
| 294 | 203 | public function _set( $number ) { |
| 295 | 204 | $this->number = $number; |
| 296 | 205 | $this->id = $this->id_base . '-' . $number; |
| 297 | 206 | } |
| 298 | 207 | |
| 299 | - /** | |
| 300 | - * @param false|int|string $form_id | |
| 301 | - * | |
| 302 | - * @return object | |
| 303 | - */ | |
| 304 | 208 | public function prepare_new( $form_id = false ) { |
| 305 | 209 | if ( $form_id ) { |
| 306 | 210 | $this->form_id = $form_id; |
| 307 | 211 | } |
| @@ -308,9 +212,9 @@ | ||
| 308 | 212 | |
| 309 | 213 | $post_content = array(); |
| 310 | 214 | $default_values = $this->get_global_defaults(); |
| 311 | 215 | |
| 312 | - // Fill default values | |
| 216 | + // fill default values | |
| 313 | 217 | $post_content = wp_parse_args( $post_content, $default_values ); |
| 314 | 218 | |
| 315 | 219 | if ( ! isset( $post_content['event'] ) && ! $this->action_options['force_event'] ) { |
| 316 | 220 | $post_content['event'] = array( reset( $this->action_options['event'] ) ); |
| @@ -325,17 +229,13 @@ | ||
| 325 | 229 | 'post_type' => FrmFormActionsController::$action_post_type, |
| 326 | 230 | 'post_name' => $this->form_id . '_' . $this->id_base . '_' . $this->number, |
| 327 | 231 | 'menu_order' => $this->form_id, |
| 328 | 232 | ); |
| 233 | + unset( $post_content ); | |
| 329 | 234 | |
| 330 | 235 | return (object) $form_action; |
| 331 | 236 | } |
| 332 | 237 | |
| 333 | - /** | |
| 334 | - * @param int|string $form_id | |
| 335 | - * | |
| 336 | - * @return int|WP_Error | |
| 337 | - */ | |
| 338 | 238 | public function create( $form_id ) { |
| 339 | 239 | $this->form_id = $form_id; |
| 340 | 240 | |
| 341 | 241 | $action = $this->prepare_new(); |
| @@ -342,18 +242,11 @@ | ||
| 342 | 242 | |
| 343 | 243 | return $this->save_settings( $action ); |
| 344 | 244 | } |
| 345 | 245 | |
| 346 | - /** | |
| 347 | - * @param int|string $form_id | |
| 348 | - * @param int|string $old_id | |
| 349 | - * | |
| 350 | - * @return void | |
| 351 | - */ | |
| 352 | 246 | public function duplicate_form_actions( $form_id, $old_id ) { |
| 353 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 354 | 247 | if ( $form_id == $old_id ) { |
| 355 | - // Don't duplicate the actions if this is a template getting updated | |
| 248 | + // don't duplicate the actions if this is a template getting updated | |
| 356 | 249 | return; |
| 357 | 250 | } |
| 358 | 251 | |
| 359 | 252 | $this->form_id = $old_id; |
| @@ -359,9 +252,8 @@ | ||
| 359 | 252 | $this->form_id = $old_id; |
| 360 | 253 | $actions = $this->get_all( $old_id ); |
| 361 | 254 | |
| 362 | 255 | $this->form_id = $form_id; |
| 363 | - | |
| 364 | 256 | foreach ( $actions as $action ) { |
| 365 | 257 | $this->duplicate_one( $action, $form_id ); |
| 366 | 258 | unset( $action ); |
| 367 | 259 | } |
| @@ -366,35 +258,29 @@ | ||
| 366 | 258 | unset( $action ); |
| 367 | 259 | } |
| 368 | 260 | } |
| 369 | 261 | |
| 370 | - /** | |
| 371 | - * Check if imported action should be created or updated. | |
| 262 | + /* Check if imported action should be created or updated | |
| 372 | 263 | * |
| 373 | 264 | * @since 2.0 |
| 374 | 265 | * |
| 375 | 266 | * @param array $action |
| 376 | - * @param array $forms | |
| 377 | - * | |
| 378 | - * @return int Post ID. | |
| 267 | + * @return integer $post_id | |
| 379 | 268 | */ |
| 380 | 269 | public function maybe_create_action( $action, $forms ) { |
| 381 | - 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' ) { | |
| 382 | 271 | // Update action only |
| 383 | 272 | $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] ); |
| 384 | - return $this->save_settings( $action ); | |
| 273 | + $post_id = $this->save_settings( $action ); | |
| 274 | + } else { | |
| 275 | + // Create action | |
| 276 | + $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] ); | |
| 277 | + $post_id = $this->duplicate_one( (object) $action, $action['menu_order'] ); | |
| 385 | 278 | } |
| 386 | - // Create action | |
| 387 | - $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] ); | |
| 388 | - return $this->duplicate_one( (object) $action, $action['menu_order'] ); | |
| 279 | + | |
| 280 | + return $post_id; | |
| 389 | 281 | } |
| 390 | 282 | |
| 391 | - /** | |
| 392 | - * @param object $action | |
| 393 | - * @param int|string $form_id | |
| 394 | - * | |
| 395 | - * @return int|WP_Error | |
| 396 | - */ | |
| 397 | 283 | public function duplicate_one( $action, $form_id ) { |
| 398 | 284 | global $frm_duplicate_ids; |
| 399 | 285 | |
| 400 | 286 | $action->menu_order = $form_id; |
| @@ -405,9 +291,9 @@ | ||
| 405 | 291 | $action->post_content[ $key ] = $frm_duplicate_ids[ $val ]; |
| 406 | 292 | } elseif ( ! is_array( $val ) ) { |
| 407 | 293 | $action->post_content[ $key ] = FrmFieldsHelper::switch_field_ids( $val ); |
| 408 | 294 | } elseif ( isset( $switch[ $key ] ) && is_array( $switch[ $key ] ) ) { |
| 409 | - // Loop through each value if empty | |
| 295 | + // loop through each value if empty | |
| 410 | 296 | if ( empty( $switch[ $key ] ) ) { |
| 411 | 297 | $switch[ $key ] = array_keys( $val ); |
| 412 | 298 | } |
| 413 | 299 | |
| @@ -422,15 +308,8 @@ | ||
| 422 | 308 | |
| 423 | 309 | return $this->save_settings( $action ); |
| 424 | 310 | } |
| 425 | 311 | |
| 426 | - /** | |
| 427 | - * @param array $action | |
| 428 | - * @param array|string $subkey | |
| 429 | - * @param mixed $val | |
| 430 | - * | |
| 431 | - * @return array | |
| 432 | - */ | |
| 433 | 312 | private function duplicate_array_walk( $action, $subkey, $val ) { |
| 434 | 313 | global $frm_duplicate_ids; |
| 435 | 314 | |
| 436 | 315 | if ( is_array( $subkey ) ) { |
| @@ -446,15 +325,15 @@ | ||
| 446 | 325 | } else { |
| 447 | 326 | foreach ( (array) $val as $ck => $cv ) { |
| 448 | 327 | if ( is_array( $cv ) ) { |
| 449 | 328 | $action[ $ck ] = $this->duplicate_array_walk( $action[ $ck ], $subkey, $cv ); |
| 450 | - } elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 329 | + } elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) { | |
| 451 | 330 | $action[ $ck ] = $frm_duplicate_ids[ $cv ]; |
| 452 | - } elseif ( $ck == $subkey ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 331 | + } elseif ( $ck == $subkey ) { | |
| 453 | 332 | $action[ $ck ] = $this->maybe_switch_field_ids( $action[ $ck ] ); |
| 454 | 333 | } |
| 455 | 334 | } |
| 456 | - }//end if | |
| 335 | + } | |
| 457 | 336 | |
| 458 | 337 | return $action; |
| 459 | 338 | } |
| 460 | 339 | |
| @@ -461,12 +340,8 @@ | ||
| 461 | 340 | /** |
| 462 | 341 | * Deal with changed settings. |
| 463 | 342 | * |
| 464 | 343 | * Do NOT over-ride this function |
| 465 | - * | |
| 466 | - * @param int|string $form_id | |
| 467 | - * | |
| 468 | - * @return array|null | |
| 469 | 344 | */ |
| 470 | 345 | public function update_callback( $form_id ) { |
| 471 | 346 | $this->form_id = $form_id; |
| 472 | 347 | |
| @@ -473,32 +348,31 @@ | ||
| 473 | 348 | $all_instances = $this->get_settings(); |
| 474 | 349 | |
| 475 | 350 | // We need to update the data |
| 476 | 351 | if ( $this->updated ) { |
| 477 | - return null; | |
| 352 | + return; | |
| 478 | 353 | } |
| 479 | 354 | |
| 480 | - // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 481 | - if ( ! isset( $_POST[ $this->option_name ] ) || ! is_array( $_POST[ $this->option_name ] ) ) { | |
| 482 | - return null; | |
| 355 | + if ( isset( $_POST[ $this->option_name ] ) && is_array( $_POST[ $this->option_name ] ) ) { | |
| 356 | + // Sanitizing removes scripts and <email> type of values. | |
| 357 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 358 | + $settings = wp_unslash( $_POST[ $this->option_name ] ); | |
| 359 | + } else { | |
| 360 | + return; | |
| 483 | 361 | } |
| 484 | 362 | |
| 485 | - // Sanitizing removes scripts and <email> type of values. | |
| 486 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing | |
| 487 | - $settings = wp_unslash( $_POST[ $this->option_name ] ); | |
| 488 | - | |
| 489 | 363 | $action_ids = array(); |
| 490 | 364 | |
| 491 | 365 | foreach ( $settings as $number => $new_instance ) { |
| 492 | 366 | $this->_set( $number ); |
| 493 | 367 | |
| 494 | - $old_instance = $all_instances[ $number ] ?? array(); | |
| 368 | + $old_instance = isset( $all_instances[ $number ] ) ? $all_instances[ $number ] : array(); | |
| 495 | 369 | |
| 496 | 370 | if ( ! isset( $new_instance['post_status'] ) ) { |
| 497 | 371 | $new_instance['post_status'] = 'draft'; |
| 498 | 372 | } |
| 499 | 373 | |
| 500 | - // Settings were never opened, so don't update | |
| 374 | + // settings were never opened, so don't update | |
| 501 | 375 | if ( ! isset( $new_instance['post_title'] ) ) { |
| 502 | 376 | $this->maybe_update_status( $new_instance, $old_instance ); |
| 503 | 377 | $action_ids[] = $new_instance['ID']; |
| 504 | 378 | $this->updated = true; |
| @@ -507,11 +381,12 @@ | ||
| 507 | 381 | |
| 508 | 382 | $new_instance['post_type'] = FrmFormActionsController::$action_post_type; |
| 509 | 383 | $new_instance['post_name'] = $this->form_id . '_' . $this->id_base . '_' . $this->number; |
| 510 | 384 | $new_instance['menu_order'] = $this->form_id; |
| 511 | - $new_instance['post_date'] = $old_instance->post_date ?? ''; | |
| 512 | - $instance = $this->update( $new_instance, $old_instance ); | |
| 385 | + $new_instance['post_date'] = isset( $old_instance->post_date ) ? $old_instance->post_date : ''; | |
| 513 | 386 | |
| 387 | + $instance = $this->update( $new_instance, $old_instance ); | |
| 388 | + | |
| 514 | 389 | /** |
| 515 | 390 | * Filter an action's settings before saving. |
| 516 | 391 | * |
| 517 | 392 | * Returning false will effectively short-circuit the widget's ability |
| @@ -518,17 +393,17 @@ | ||
| 518 | 393 | * to update settings. |
| 519 | 394 | * |
| 520 | 395 | * @since 2.0 |
| 521 | 396 | * |
| 522 | - * @param array $instance The current widget instance's settings. | |
| 523 | - * @param array $new_instance Array of new widget settings. | |
| 524 | - * @param array $old_instance Array of old widget settings. | |
| 525 | - * @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. | |
| 526 | 401 | */ |
| 527 | 402 | $instance = apply_filters( 'frm_action_update_callback', $instance, $new_instance, $old_instance, $this ); |
| 528 | 403 | |
| 529 | 404 | $instance['post_content'] = apply_filters( 'frm_before_save_action', $instance['post_content'], $instance, $new_instance, $old_instance, $this ); |
| 530 | - $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 ); | |
| 531 | 406 | |
| 532 | 407 | if ( false !== $instance ) { |
| 533 | 408 | $all_instances[ $number ] = $instance; |
| 534 | 409 | } |
| @@ -535,9 +410,9 @@ | ||
| 535 | 410 | |
| 536 | 411 | $action_ids[] = $this->save_settings( $instance ); |
| 537 | 412 | |
| 538 | 413 | $this->updated = true; |
| 539 | - }//end foreach | |
| 414 | + } | |
| 540 | 415 | |
| 541 | 416 | return $action_ids; |
| 542 | 417 | } |
| 543 | 418 | |
| @@ -544,46 +419,29 @@ | ||
| 544 | 419 | /** |
| 545 | 420 | * If the status of the action has changed, update it |
| 546 | 421 | * |
| 547 | 422 | * @since 3.04 |
| 548 | - * | |
| 549 | - * @param array $new_instance | |
| 550 | - * @param array|stdClass $old_instance | |
| 551 | - * | |
| 552 | - * @return void | |
| 553 | 423 | */ |
| 554 | 424 | protected function maybe_update_status( $new_instance, $old_instance ) { |
| 555 | - if ( ! is_object( $old_instance ) || $new_instance['post_status'] === $old_instance->post_status ) { | |
| 556 | - 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 | + ); | |
| 557 | 433 | } |
| 558 | - | |
| 559 | - self::clear_cache(); | |
| 560 | - wp_update_post( | |
| 561 | - array( | |
| 562 | - 'ID' => $new_instance['ID'], | |
| 563 | - 'post_status' => $new_instance['post_status'], | |
| 564 | - ) | |
| 565 | - ); | |
| 566 | 434 | } |
| 567 | 435 | |
| 568 | - /** | |
| 569 | - * @param array $settings | |
| 570 | - * | |
| 571 | - * @return int|WP_Error | |
| 572 | - */ | |
| 573 | 436 | public function save_settings( $settings ) { |
| 574 | 437 | self::clear_cache(); |
| 438 | + | |
| 575 | 439 | return FrmDb::save_settings( $settings, 'frm_actions' ); |
| 576 | 440 | } |
| 577 | 441 | |
| 578 | - /** | |
| 579 | - * @param int $id | |
| 580 | - * | |
| 581 | - * @return object|null | |
| 582 | - */ | |
| 583 | 442 | public function get_single_action( $id ) { |
| 584 | 443 | $action = get_post( $id ); |
| 585 | - | |
| 586 | 444 | if ( $action ) { |
| 587 | 445 | $action = $this->prepare_action( $action ); |
| 588 | 446 | $this->_set( $id ); |
| 589 | 447 | } |
| @@ -590,43 +448,27 @@ | ||
| 590 | 448 | |
| 591 | 449 | return $action; |
| 592 | 450 | } |
| 593 | 451 | |
| 594 | - /** | |
| 595 | - * @param int|string $form_id | |
| 596 | - * | |
| 597 | - * @return array | |
| 598 | - */ | |
| 599 | 452 | public function get_one( $form_id ) { |
| 600 | 453 | return $this->get_all( $form_id, 1 ); |
| 601 | 454 | } |
| 602 | 455 | |
| 603 | - /** | |
| 604 | - * @param int|string $form_id | |
| 605 | - * @param string $type | |
| 606 | - * @param array $atts | |
| 607 | - * | |
| 608 | - * @return array | |
| 609 | - */ | |
| 610 | 456 | public static function get_action_for_form( $form_id, $type = 'all', $atts = array() ) { |
| 611 | 457 | $action_controls = FrmFormActionsController::get_form_actions( $type ); |
| 612 | - | |
| 613 | - if ( ! $action_controls ) { | |
| 614 | - // Don't continue if there are no available actions | |
| 458 | + if ( empty( $action_controls ) ) { | |
| 459 | + // don't continue if there are no available actions | |
| 615 | 460 | return array(); |
| 616 | 461 | } |
| 617 | 462 | |
| 618 | - if ( 'all' !== $type ) { | |
| 619 | - if ( is_array( $action_controls ) ) { | |
| 620 | - return array(); | |
| 621 | - } | |
| 622 | - | |
| 463 | + if ( 'all' != $type ) { | |
| 623 | 464 | return $action_controls->get_all( $form_id, $atts ); |
| 624 | 465 | } |
| 625 | 466 | |
| 626 | 467 | self::prepare_get_action( $atts ); |
| 627 | 468 | |
| 628 | - $limit = self::get_action_limit( $form_id, $atts['limit'] ); | |
| 469 | + $limit = apply_filters( 'frm_form_action_limit', $atts['limit'], compact( 'type', 'form_id' ) ); | |
| 470 | + | |
| 629 | 471 | $args = self::action_args( $form_id, $limit ); |
| 630 | 472 | $args['post_status'] = $atts['post_status']; |
| 631 | 473 | $actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' ); |
| 632 | 474 | |
| @@ -634,11 +476,10 @@ | ||
| 634 | 476 | return array(); |
| 635 | 477 | } |
| 636 | 478 | |
| 637 | 479 | $settings = array(); |
| 638 | - | |
| 639 | 480 | foreach ( $actions as $action ) { |
| 640 | - // Some plugins/themes are formatting the post_excerpt | |
| 481 | + // some plugins/themes are formatting the post_excerpt | |
| 641 | 482 | $action->post_excerpt = sanitize_title( $action->post_excerpt ); |
| 642 | 483 | |
| 643 | 484 | if ( ! isset( $action_controls[ $action->post_excerpt ] ) ) { |
| 644 | 485 | continue; |
| @@ -651,43 +492,27 @@ | ||
| 651 | 492 | break; |
| 652 | 493 | } |
| 653 | 494 | } |
| 654 | 495 | |
| 655 | - return 1 === $limit ? reset( $settings ) : $settings; | |
| 656 | - } | |
| 496 | + if ( 1 === $limit ) { | |
| 497 | + $settings = reset( $settings ); | |
| 498 | + } | |
| 657 | 499 | |
| 658 | - /** | |
| 659 | - * Get the limit for the number of actions for a single form. By default, this is 99, but | |
| 660 | - * it can be modified with a code snippet. | |
| 661 | - * | |
| 662 | - * @since 6.17 This logic from moved from FrmFormAction::get_action_for_form. | |
| 663 | - * | |
| 664 | - * @param int|string $form_id | |
| 665 | - * @param int|string $limit The unfiltered limit value. | |
| 666 | - * | |
| 667 | - * @return int The filtered limit value. | |
| 668 | - */ | |
| 669 | - public static function get_action_limit( $form_id, $limit = 99 ) { | |
| 670 | - $type = 'all'; | |
| 671 | - return (int) apply_filters( 'frm_form_action_limit', (int) $limit, compact( 'type', 'form_id' ) ); | |
| 500 | + return $settings; | |
| 672 | 501 | } |
| 673 | 502 | |
| 674 | 503 | /** |
| 675 | 504 | * @since 3.04 |
| 676 | - * | |
| 677 | - * @param array|string $args | |
| 678 | - * @param string $default_status | |
| 679 | - * | |
| 680 | - * @return void | |
| 505 | + * @param array $args | |
| 506 | + * @param string $default_status | |
| 681 | 507 | */ |
| 682 | 508 | protected static function prepare_get_action( &$args, $default_status = 'publish' ) { |
| 683 | 509 | if ( is_numeric( $args ) ) { |
| 684 | - // For reverse compatibility. $limit was changed to $args | |
| 510 | + // for reverse compatibility. $limit was changed to $args | |
| 685 | 511 | $args = array( |
| 686 | 512 | 'limit' => $args, |
| 687 | 513 | ); |
| 688 | 514 | } |
| 689 | - | |
| 690 | 515 | $defaults = array( |
| 691 | 516 | 'limit' => 99, |
| 692 | 517 | 'post_status' => $default_status, |
| 693 | 518 | ); |
| @@ -694,21 +519,14 @@ | ||
| 694 | 519 | $args = wp_parse_args( $args, $defaults ); |
| 695 | 520 | } |
| 696 | 521 | |
| 697 | 522 | /** |
| 698 | - * @param int $action_id | |
| 699 | - * @param string $type | |
| 700 | - * | |
| 701 | - * @return bool|object | |
| 523 | + * @param int $action_id | |
| 702 | 524 | */ |
| 703 | 525 | public static function get_single_action_type( $action_id, $type ) { |
| 704 | 526 | if ( ! $type ) { |
| 705 | 527 | return false; |
| 706 | 528 | } |
| 707 | - | |
| 708 | - /** | |
| 709 | - * @var FrmFormAction | |
| 710 | - */ | |
| 711 | 529 | $action_control = FrmFormActionsController::get_form_actions( $type ); |
| 712 | 530 | |
| 713 | 531 | return $action_control->get_single_action( $action_id ); |
| 714 | 532 | } |
| @@ -713,31 +531,20 @@ | ||
| 713 | 531 | return $action_control->get_single_action( $action_id ); |
| 714 | 532 | } |
| 715 | 533 | |
| 716 | 534 | /** |
| 717 | - * @param int $form_id | |
| 718 | - * @param string $type | |
| 535 | + * @param int $form_id | |
| 719 | 536 | * |
| 720 | 537 | * @return bool |
| 721 | 538 | */ |
| 722 | 539 | public static function form_has_action_type( $form_id, $type ) { |
| 723 | - $actions = self::get_action_for_form( $form_id, $type ); | |
| 724 | - return ! empty( $actions ); | |
| 540 | + $payment_actions = self::get_action_for_form( $form_id, $type ); | |
| 541 | + | |
| 542 | + return ! empty( $payment_actions ); | |
| 725 | 543 | } |
| 726 | 544 | |
| 727 | - /** | |
| 728 | - * @param false|int|string $form_id | |
| 729 | - * @param array $atts | |
| 730 | - * | |
| 731 | - * @return array | |
| 732 | - */ | |
| 733 | 545 | public function get_all( $form_id = false, $atts = array() ) { |
| 734 | - if ( is_array( $atts ) && ! isset( $atts['limit'] ) && $this->action_options['limit'] > 99 ) { | |
| 735 | - $atts['limit'] = $this->action_options['limit']; | |
| 736 | - } | |
| 737 | - | |
| 738 | 546 | self::prepare_get_action( $atts, 'any' ); |
| 739 | - | |
| 740 | 547 | $limit = $atts['limit']; |
| 741 | 548 | |
| 742 | 549 | if ( $form_id ) { |
| 743 | 550 | $this->form_id = $form_id; |
| @@ -748,10 +555,10 @@ | ||
| 748 | 555 | global $frm_vars; |
| 749 | 556 | $frm_vars['action_type'] = $type; |
| 750 | 557 | |
| 751 | 558 | add_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' ); |
| 752 | - $query = self::action_args( $form_id, $limit ); | |
| 753 | - $query['post_status'] = $atts['post_status']; | |
| 559 | + $query = self::action_args( $form_id, $limit ); | |
| 560 | + $query['post_status'] = $atts['post_status']; | |
| 754 | 561 | $query['suppress_filters'] = false; |
| 755 | 562 | |
| 756 | 563 | $actions = FrmDb::check_cache( json_encode( $query ) . '_type_' . $type, 'frm_actions', $query, 'get_posts' ); |
| 757 | 564 | unset( $query ); |
| @@ -757,32 +564,30 @@ | ||
| 757 | 564 | unset( $query ); |
| 758 | 565 | |
| 759 | 566 | remove_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' ); |
| 760 | 567 | |
| 761 | - if ( ! $actions ) { | |
| 568 | + if ( empty( $actions ) ) { | |
| 762 | 569 | return array(); |
| 763 | 570 | } |
| 764 | 571 | |
| 765 | 572 | $settings = array(); |
| 766 | - | |
| 767 | 573 | foreach ( $actions as $action ) { |
| 768 | 574 | if ( count( $settings ) >= $limit ) { |
| 769 | 575 | continue; |
| 770 | 576 | } |
| 771 | 577 | |
| 772 | - $action = $this->prepare_action( $action ); | |
| 578 | + $action = $this->prepare_action( $action ); | |
| 579 | + | |
| 773 | 580 | $settings[ $action->ID ] = $action; |
| 774 | 581 | } |
| 775 | 582 | |
| 776 | - return 1 === $limit ? reset( $settings ) : $settings; | |
| 583 | + if ( 1 === $limit ) { | |
| 584 | + $settings = reset( $settings ); | |
| 585 | + } | |
| 586 | + | |
| 587 | + return $settings; | |
| 777 | 588 | } |
| 778 | 589 | |
| 779 | - /** | |
| 780 | - * @param int|string $form_id | |
| 781 | - * @param int $limit | |
| 782 | - * | |
| 783 | - * @return array | |
| 784 | - */ | |
| 785 | 590 | public static function action_args( $form_id = 0, $limit = 99 ) { |
| 786 | 591 | $args = array( |
| 787 | 592 | 'post_type' => FrmFormActionsController::$action_post_type, |
| 788 | 593 | 'post_status' => 'publish', |
| @@ -790,9 +595,9 @@ | ||
| 790 | 595 | 'orderby' => 'title', |
| 791 | 596 | 'order' => 'ASC', |
| 792 | 597 | ); |
| 793 | 598 | |
| 794 | - if ( $form_id && $form_id !== 'all' ) { | |
| 599 | + if ( $form_id && $form_id != 'all' ) { | |
| 795 | 600 | $args['menu_order'] = $form_id; |
| 796 | 601 | } |
| 797 | 602 | |
| 798 | 603 | return $args; |
| @@ -797,13 +602,8 @@ | ||
| 797 | 602 | |
| 798 | 603 | return $args; |
| 799 | 604 | } |
| 800 | 605 | |
| 801 | - /** | |
| 802 | - * @param WP_Post $action | |
| 803 | - * | |
| 804 | - * @return WP_Post | |
| 805 | - */ | |
| 806 | 606 | public function prepare_action( $action ) { |
| 807 | 607 | $action->post_content = (array) FrmAppHelper::maybe_json_decode( $action->post_content ); |
| 808 | 608 | $action->post_excerpt = sanitize_title( $action->post_excerpt ); |
| 809 | 609 | |
| @@ -808,20 +608,18 @@ | ||
| 808 | 608 | $action->post_excerpt = sanitize_title( $action->post_excerpt ); |
| 809 | 609 | |
| 810 | 610 | $default_values = $this->get_global_defaults(); |
| 811 | 611 | |
| 812 | - // Fill default values | |
| 612 | + // fill default values | |
| 813 | 613 | $action->post_content += $default_values; |
| 814 | 614 | |
| 815 | 615 | foreach ( $default_values as $k => $vals ) { |
| 816 | - if ( ! is_array( $vals ) || ! $vals ) { | |
| 817 | - continue; | |
| 616 | + if ( is_array( $vals ) && ! empty( $vals ) ) { | |
| 617 | + if ( 'event' == $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) { | |
| 618 | + continue; | |
| 619 | + } | |
| 620 | + $action->post_content[ $k ] = wp_parse_args( $action->post_content[ $k ], $vals ); | |
| 818 | 621 | } |
| 819 | - | |
| 820 | - if ( 'event' === $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) { | |
| 821 | - continue; | |
| 822 | - } | |
| 823 | - $action->post_content[ $k ] = wp_parse_args( $action->post_content[ $k ], $vals ); | |
| 824 | 622 | } |
| 825 | 623 | |
| 826 | 624 | if ( ! is_array( $action->post_content['event'] ) ) { |
| 827 | 625 | $action->post_content['event'] = explode( ',', $action->post_content['event'] ); |
| @@ -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,14 +633,12 @@ | ||
| 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 | - if ( 'all' !== $type ) { | |
| 640 | + if ( 'all' != $type ) { | |
| 851 | 641 | $query['post_excerpt'] = $this->id_base; |
| 852 | 642 | } |
| 853 | 643 | |
| 854 | 644 | $post_ids = FrmDb::get_col( $wpdb->posts, $query, 'ID' ); |
| @@ -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,43 +678,35 @@ | ||
| 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 | |
| 907 | - return apply_filters( 'frm_global_switch_fields', $switch ); | |
| 686 | + $switch = apply_filters( 'frm_global_switch_fields', $switch ); | |
| 687 | + | |
| 688 | + return $switch; | |
| 908 | 689 | } |
| 909 | 690 | |
| 910 | 691 | /** |
| 911 | 692 | * Migrate settings from form->options into new action. |
| 912 | - * | |
| 913 | - * @param object $form | |
| 914 | - * @param string $update | |
| 915 | - * | |
| 916 | - * @return int|WP_Error | |
| 917 | 693 | */ |
| 918 | 694 | public function migrate_to_2( $form, $update = 'update' ) { |
| 919 | - $action = $this->prepare_new( $form->id ); | |
| 695 | + $action = $this->prepare_new( $form->id ); | |
| 920 | 696 | FrmAppHelper::unserialize_or_decode( $form->options ); |
| 921 | 697 | |
| 922 | - // Fill with existing options | |
| 698 | + // fill with existing options | |
| 923 | 699 | foreach ( $action->post_content as $name => $val ) { |
| 924 | - if ( ! isset( $form->options[ $name ] ) ) { | |
| 925 | - continue; | |
| 700 | + if ( isset( $form->options[ $name ] ) ) { | |
| 701 | + $action->post_content[ $name ] = $form->options[ $name ]; | |
| 702 | + unset( $form->options[ $name ] ); | |
| 926 | 703 | } |
| 927 | - | |
| 928 | - $action->post_content[ $name ] = $form->options[ $name ]; | |
| 929 | - unset( $form->options[ $name ] ); | |
| 930 | 704 | } |
| 931 | 705 | |
| 932 | 706 | $action = $this->migrate_values( $action, $form ); |
| 933 | 707 | |
| 934 | - // Check if action already exists | |
| 708 | + // check if action already exists | |
| 935 | 709 | $post_id = get_posts( |
| 936 | 710 | array( |
| 937 | 711 | 'name' => $action->post_name, |
| 938 | 712 | 'post_type' => FrmFormActionsController::$action_post_type, |
| @@ -940,57 +714,134 @@ | ||
| 940 | 714 | 'numberposts' => 1, |
| 941 | 715 | ) |
| 942 | 716 | ); |
| 943 | 717 | |
| 944 | - if ( ! $post_id ) { | |
| 945 | - // Create action now | |
| 718 | + if ( empty( $post_id ) ) { | |
| 719 | + // create action now | |
| 946 | 720 | $post_id = $this->save_settings( $action ); |
| 947 | 721 | } |
| 948 | 722 | |
| 949 | - if ( ! $post_id || 'update' !== $update ) { | |
| 950 | - return $post_id; | |
| 723 | + if ( $post_id && 'update' == $update ) { | |
| 724 | + global $wpdb; | |
| 725 | + $form->options = maybe_serialize( $form->options ); | |
| 726 | + | |
| 727 | + // update form options | |
| 728 | + $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) ); | |
| 729 | + FrmForm::clear_form_cache(); | |
| 951 | 730 | } |
| 952 | 731 | |
| 953 | - global $wpdb; | |
| 954 | - $form->options = maybe_serialize( $form->options ); | |
| 732 | + return $post_id; | |
| 733 | + } | |
| 955 | 734 | |
| 956 | - // Update form options | |
| 957 | - $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) ); | |
| 958 | - FrmForm::clear_form_cache(); | |
| 735 | + public static function action_conditions_met( $action, $entry ) { | |
| 736 | + if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) { | |
| 737 | + return FrmProFormActionsController::action_conditions_met( $action, $entry ); | |
| 738 | + } | |
| 959 | 739 | |
| 960 | - return $post_id; | |
| 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 | + | |
| 777 | + return $stop; | |
| 961 | 778 | } |
| 962 | 779 | |
| 963 | 780 | /** |
| 964 | - * @param WP_Post $action | |
| 965 | - * @param stdClass $entry | |
| 781 | + * Prepare the logic value for comparison against the entered value | |
| 966 | 782 | * |
| 967 | - * @return bool | |
| 783 | + * @since 2.01.02 | |
| 784 | + * @deprecated 4.06.02 | |
| 785 | + * | |
| 786 | + * @param array|string $logic_value | |
| 968 | 787 | */ |
| 969 | - public static function action_conditions_met( $action, $entry ) { | |
| 970 | - if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) { | |
| 971 | - return FrmProFormActionsController::action_conditions_met( $action, $entry ); | |
| 788 | + private static function prepare_logic_value( &$logic_value, $action, $entry ) { | |
| 789 | + if ( is_array( $logic_value ) ) { | |
| 790 | + $logic_value = reset( $logic_value ); | |
| 972 | 791 | } |
| 973 | - return false; | |
| 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 ); | |
| 974 | 803 | } |
| 975 | 804 | |
| 976 | 805 | /** |
| 977 | - * @param string $class | |
| 806 | + * Get the value from a specific field and entry | |
| 978 | 807 | * |
| 979 | - * @return array | |
| 808 | + * @since 2.01.02 | |
| 809 | + * @deprecated 4.06.02 | |
| 810 | + * | |
| 811 | + * @param object $entry | |
| 812 | + * @param int $field_id | |
| 813 | + * | |
| 814 | + * @return array|bool|mixed|string | |
| 980 | 815 | */ |
| 816 | + private static function get_value_from_entry( $entry, $field_id ) { | |
| 817 | + $observed_value = ''; | |
| 818 | + | |
| 819 | + if ( isset( $entry->metas[ $field_id ] ) ) { | |
| 820 | + $observed_value = $entry->metas[ $field_id ]; | |
| 821 | + } elseif ( $entry->post_id && FrmAppHelper::pro_is_installed() ) { | |
| 822 | + $field = FrmField::getOne( $field_id ); | |
| 823 | + $observed_value = FrmProEntryMetaHelper::get_post_or_meta_value( | |
| 824 | + $entry, | |
| 825 | + $field, | |
| 826 | + array( | |
| 827 | + 'links' => false, | |
| 828 | + 'truncate' => false, | |
| 829 | + ) | |
| 830 | + ); | |
| 831 | + } | |
| 832 | + | |
| 833 | + return $observed_value; | |
| 834 | + } | |
| 835 | + | |
| 981 | 836 | public static function default_action_opts( $class = '' ) { |
| 982 | 837 | return array( |
| 983 | - 'classes' => 'frmfont ' . $class, | |
| 984 | - 'active' => false, | |
| 985 | - 'limit' => 0, | |
| 986 | - 'description' => '', | |
| 838 | + 'classes' => 'frm_icon_font ' . $class, | |
| 839 | + 'active' => false, | |
| 840 | + 'limit' => 0, | |
| 987 | 841 | ); |
| 988 | 842 | } |
| 989 | 843 | |
| 990 | - /** | |
| 991 | - * @return array | |
| 992 | - */ | |
| 993 | 844 | public static function trigger_labels() { |
| 994 | 845 | $triggers = array( |
| 995 | 846 | 'draft' => __( 'Draft is saved', 'formidable' ), |
| 996 | 847 | 'create' => __( 'Entry is created', 'formidable' ), |
| @@ -1001,66 +852,18 @@ | ||
| 1001 | 852 | |
| 1002 | 853 | return apply_filters( 'frm_action_triggers', $triggers ); |
| 1003 | 854 | } |
| 1004 | 855 | |
| 1005 | - /** | |
| 1006 | - * @param string $action_key The unique key for this action instance. | |
| 1007 | - * | |
| 1008 | - * @return void | |
| 1009 | - */ | |
| 1010 | - public function render_conditional_logic_call_to_action( $action_key = '' ) { | |
| 1011 | - $params = array( | |
| 1012 | - 'class' => 'frm-h-stack-xs frm-bt-200 frm-py-md frm_show_upgrade', | |
| 1013 | - 'data-upgrade' => $this->get_upgrade_text(), | |
| 1014 | - 'data-medium' => 'conditional-' . $this->id_base, | |
| 1015 | - 'data-content' => 'conditional-logic', | |
| 1016 | - ); | |
| 1017 | - // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 856 | + public function render_conditional_logic_call_to_action() { | |
| 1018 | 857 | ?> |
| 1019 | - <div <?php FrmAppHelper::array_to_html_params( $params, true ); ?>> | |
| 1020 | - <?php | |
| 1021 | - FrmHtmlHelper::toggle( | |
| 1022 | - 'frm_logic_cta_' . $action_key, | |
| 1023 | - 'frm_logic_cta_' . $action_key, | |
| 1024 | - array( | |
| 1025 | - 'div_class' => 'with_frm_style frm_toggle', | |
| 1026 | - 'checked' => false, | |
| 1027 | - 'echo' => true, | |
| 1028 | - 'disabled' => true, | |
| 1029 | - ) | |
| 1030 | - ); | |
| 1031 | - ?> | |
| 1032 | - <label for="frm_logic_cta_<?php echo esc_attr( $action_key ); ?>" class="frm_noallow"> | |
| 1033 | - <?php esc_html_e( 'Use Conditional Logic', 'formidable' ); ?> | |
| 1034 | - </label> | |
| 1035 | - </div> | |
| 858 | + <h3> | |
| 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 ); ?>"> | |
| 860 | + <?php esc_html_e( 'Use Conditional Logic', 'formidable' ); ?> | |
| 861 | + </a> | |
| 862 | + </h3> | |
| 1036 | 863 | <?php |
| 1037 | - // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 1038 | 864 | } |
| 1039 | 865 | |
| 1040 | - /** | |
| 1041 | - * @return string | |
| 1042 | - */ | |
| 1043 | 866 | protected function get_upgrade_text() { |
| 1044 | 867 | return __( 'Conditional form actions', 'formidable' ); |
| 1045 | - } | |
| 1046 | - | |
| 1047 | - /** | |
| 1048 | - * Gets form fields for form action settings. | |
| 1049 | - * | |
| 1050 | - * @since 6.10 | |
| 1051 | - * | |
| 1052 | - * @param int $form_id Form ID. | |
| 1053 | - * | |
| 1054 | - * @return object[] | |
| 1055 | - */ | |
| 1056 | - protected function get_form_fields( $form_id ) { | |
| 1057 | - // Get form fields, include embedded and repeater child fields. | |
| 1058 | - $form_fields = FrmField::get_all_for_form( $form_id, '', 'include' ); | |
| 1059 | - return array_filter( | |
| 1060 | - $form_fields, | |
| 1061 | - function ( $form_field ) { | |
| 1062 | - return ! FrmField::is_no_save_field( $form_field->type ); | |
| 1063 | - } | |
| 1064 | - ); | |
| 1065 | 868 | } |
| 1066 | 869 | } |