| @@ -88,12 +88,13 @@ | ||
| 88 | 88 | * Echo the settings update form |
| 89 | 89 | * |
| 90 | 90 | * @param WP_Post $instance Current settings. |
| 91 | 91 | * @param array $args |
| 92 | + * | |
| 93 | + * @return string | |
| 92 | 94 | */ |
| 93 | 95 | public function form( $instance, $args = array() ) { |
| 94 | 96 | echo '<p class="no-options-widget">' . esc_html__( 'There are no options for this action.', 'formidable' ) . '</p>'; |
| 95 | - | |
| 96 | 97 | return 'noform'; |
| 97 | 98 | } |
| 98 | 99 | |
| 99 | 100 | /** |
| @@ -109,8 +110,14 @@ | ||
| 109 | 110 | public function get_switch_fields() { |
| 110 | 111 | return array(); |
| 111 | 112 | } |
| 112 | 113 | |
| 114 | + /** | |
| 115 | + * @param object $action | |
| 116 | + * @param object $form | |
| 117 | + * | |
| 118 | + * @return object | |
| 119 | + */ | |
| 113 | 120 | public function migrate_values( $action, $form ) { |
| 114 | 121 | return $action; |
| 115 | 122 | } |
| 116 | 123 | |
| @@ -133,9 +140,17 @@ | ||
| 133 | 140 | if ( ! defined( 'ABSPATH' ) ) { |
| 134 | 141 | die( 'You are not allowed to call this page directly.' ); |
| 135 | 142 | } |
| 136 | 143 | |
| 137 | - $this->id_base = strtolower( $id_base ); | |
| 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 | + | |
| 138 | 153 | $this->name = $name; |
| 139 | 154 | $this->option_name = 'frm_' . $this->id_base . '_action'; |
| 140 | 155 | |
| 141 | 156 | $default_options = array( |
| @@ -150,8 +165,11 @@ | ||
| 150 | 165 | 'tooltip' => $name, |
| 151 | 166 | 'group' => $id_base, |
| 152 | 167 | 'color' => '', |
| 153 | 168 | 'keywords' => '', |
| 169 | + 'description' => '', | |
| 170 | + 'is_new' => false, | |
| 171 | + 'is_beta' => false, | |
| 154 | 172 | ); |
| 155 | 173 | |
| 156 | 174 | $action_options = apply_filters( 'frm_' . $id_base . '_action_options', $action_options ); |
| 157 | 175 | $group = $this->get_group( $action_options ); |
| @@ -157,16 +175,18 @@ | ||
| 157 | 175 | $group = $this->get_group( $action_options ); |
| 158 | 176 | $action_options['group'] = $group['id']; |
| 159 | 177 | |
| 160 | 178 | if ( ! isset( $action_options['color'] ) ) { |
| 161 | - $colors = array( 'green', 'orange', 'purple' ); | |
| 162 | - shuffle( $colors ); | |
| 163 | - $action_options['color'] = 'var(--' . reset( $colors ) . ')'; | |
| 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 ] . ')'; | |
| 164 | 183 | } |
| 165 | 184 | |
| 166 | 185 | $upgrade_class = isset( $action_options['classes'] ) && $action_options['classes'] === 'frm_show_upgrade'; |
| 186 | + | |
| 167 | 187 | if ( $action_options['group'] === $id_base ) { |
| 168 | - $upgrade_class = strpos( $action_options['classes'], 'frm_show_upgrade' ) !== false; | |
| 188 | + $upgrade_class = str_contains( $action_options['classes'], 'frm_show_upgrade' ); | |
| 169 | 189 | $action_options['classes'] = $group['icon']; |
| 170 | 190 | } elseif ( empty( $action_options['classes'] ) || $upgrade_class ) { |
| 171 | 191 | $action_options['classes'] = $group['icon']; |
| 172 | 192 | } |
| @@ -180,8 +200,13 @@ | ||
| 180 | 200 | } |
| 181 | 201 | |
| 182 | 202 | /** |
| 183 | 203 | * @param string $id_base |
| 204 | + * @param string $name | |
| 205 | + * @param array $action_options | |
| 206 | + * @param array $control_options | |
| 207 | + * | |
| 208 | + * @return void | |
| 184 | 209 | */ |
| 185 | 210 | public function FrmFormAction( $id_base, $name, $action_options = array(), $control_options = array() ) { |
| 186 | 211 | self::__construct( $id_base, $name, $action_options, $control_options ); |
| 187 | 212 | } |
| @@ -194,16 +219,22 @@ | ||
| 194 | 219 | * @return string |
| 195 | 220 | */ |
| 196 | 221 | public function maybe_switch_field_ids( $action ) { |
| 197 | 222 | $updated_action = apply_filters( 'frm_maybe_switch_field_ids', $action ); |
| 223 | + | |
| 198 | 224 | if ( $updated_action === $action ) { |
| 199 | - $updated_action = FrmFieldsHelper::switch_field_ids( $action ); | |
| 225 | + return FrmFieldsHelper::switch_field_ids( $action ); | |
| 200 | 226 | } |
| 227 | + | |
| 201 | 228 | return $updated_action; |
| 202 | 229 | } |
| 203 | 230 | |
| 204 | 231 | /** |
| 205 | 232 | * @since 4.0 |
| 233 | + * | |
| 234 | + * @param array $action_options | |
| 235 | + * | |
| 236 | + * @return array | |
| 206 | 237 | */ |
| 207 | 238 | protected function get_group( $action_options ) { |
| 208 | 239 | $groups = FrmFormActionsController::form_action_groups(); |
| 209 | 240 | $group = 'misc'; |
| @@ -213,9 +244,9 @@ | ||
| 213 | 244 | } elseif ( isset( $groups[ $this->id_base ] ) ) { |
| 214 | 245 | $group = $this->id_base; |
| 215 | 246 | } else { |
| 216 | 247 | foreach ( $groups as $name => $check_group ) { |
| 217 | - if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'] ) ) { | |
| 248 | + if ( isset( $check_group['actions'] ) && in_array( $this->id_base, $check_group['actions'], true ) ) { | |
| 218 | 249 | $group = $name; |
| 219 | 250 | break; |
| 220 | 251 | } |
| 221 | 252 | } |
| @@ -230,17 +261,17 @@ | ||
| 230 | 261 | * |
| 231 | 262 | * This function should be used in form() methods to create name attributes for fields to be saved by update() |
| 232 | 263 | * |
| 233 | 264 | * @param string $field_name Field name. |
| 265 | + * @param string $post_field | |
| 234 | 266 | * |
| 235 | 267 | * @return string Name attribute for $field_name |
| 236 | 268 | */ |
| 237 | 269 | public function get_field_name( $field_name, $post_field = 'post_content' ) { |
| 238 | 270 | $name = $this->option_name . '[' . $this->number . ']'; |
| 239 | - $name .= ( empty( $post_field ) ? '' : '[' . $post_field . ']' ); | |
| 240 | - $name .= '[' . $field_name . ']'; | |
| 271 | + $name .= $post_field ? '[' . $post_field . ']' : ''; | |
| 241 | 272 | |
| 242 | - return $name; | |
| 273 | + return $name . ( '[' . $field_name . ']' ); | |
| 243 | 274 | } |
| 244 | 275 | |
| 245 | 276 | /** |
| 246 | 277 | * Constructs id attributes for use in form() fields |
| @@ -256,8 +287,9 @@ | ||
| 256 | 287 | } |
| 257 | 288 | |
| 258 | 289 | /** |
| 259 | 290 | * @param int|string $number |
| 291 | + * | |
| 260 | 292 | * @return void |
| 261 | 293 | */ |
| 262 | 294 | public function _set( $number ) { |
| 263 | 295 | $this->number = $number; |
| @@ -264,8 +296,10 @@ | ||
| 264 | 296 | $this->id = $this->id_base . '-' . $number; |
| 265 | 297 | } |
| 266 | 298 | |
| 267 | 299 | /** |
| 300 | + * @param false|int|string $form_id | |
| 301 | + * | |
| 268 | 302 | * @return object |
| 269 | 303 | */ |
| 270 | 304 | public function prepare_new( $form_id = false ) { |
| 271 | 305 | if ( $form_id ) { |
| @@ -274,9 +308,9 @@ | ||
| 274 | 308 | |
| 275 | 309 | $post_content = array(); |
| 276 | 310 | $default_values = $this->get_global_defaults(); |
| 277 | 311 | |
| 278 | - // fill default values | |
| 312 | + // Fill default values | |
| 279 | 313 | $post_content = wp_parse_args( $post_content, $default_values ); |
| 280 | 314 | |
| 281 | 315 | if ( ! isset( $post_content['event'] ) && ! $this->action_options['force_event'] ) { |
| 282 | 316 | $post_content['event'] = array( reset( $this->action_options['event'] ) ); |
| @@ -291,13 +325,17 @@ | ||
| 291 | 325 | 'post_type' => FrmFormActionsController::$action_post_type, |
| 292 | 326 | 'post_name' => $this->form_id . '_' . $this->id_base . '_' . $this->number, |
| 293 | 327 | 'menu_order' => $this->form_id, |
| 294 | 328 | ); |
| 295 | - unset( $post_content ); | |
| 296 | 329 | |
| 297 | 330 | return (object) $form_action; |
| 298 | 331 | } |
| 299 | 332 | |
| 333 | + /** | |
| 334 | + * @param int|string $form_id | |
| 335 | + * | |
| 336 | + * @return int|WP_Error | |
| 337 | + */ | |
| 300 | 338 | public function create( $form_id ) { |
| 301 | 339 | $this->form_id = $form_id; |
| 302 | 340 | |
| 303 | 341 | $action = $this->prepare_new(); |
| @@ -305,13 +343,17 @@ | ||
| 305 | 343 | return $this->save_settings( $action ); |
| 306 | 344 | } |
| 307 | 345 | |
| 308 | 346 | /** |
| 347 | + * @param int|string $form_id | |
| 348 | + * @param int|string $old_id | |
| 349 | + * | |
| 309 | 350 | * @return void |
| 310 | 351 | */ |
| 311 | 352 | public function duplicate_form_actions( $form_id, $old_id ) { |
| 353 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 312 | 354 | if ( $form_id == $old_id ) { |
| 313 | - // don't duplicate the actions if this is a template getting updated | |
| 355 | + // Don't duplicate the actions if this is a template getting updated | |
| 314 | 356 | return; |
| 315 | 357 | } |
| 316 | 358 | |
| 317 | 359 | $this->form_id = $old_id; |
| @@ -317,8 +359,9 @@ | ||
| 317 | 359 | $this->form_id = $old_id; |
| 318 | 360 | $actions = $this->get_all( $old_id ); |
| 319 | 361 | |
| 320 | 362 | $this->form_id = $form_id; |
| 363 | + | |
| 321 | 364 | foreach ( $actions as $action ) { |
| 322 | 365 | $this->duplicate_one( $action, $form_id ); |
| 323 | 366 | unset( $action ); |
| 324 | 367 | } |
| @@ -329,26 +372,28 @@ | ||
| 329 | 372 | * |
| 330 | 373 | * @since 2.0 |
| 331 | 374 | * |
| 332 | 375 | * @param array $action |
| 333 | - * @return int $post_id | |
| 376 | + * @param array $forms | |
| 377 | + * | |
| 378 | + * @return int Post ID. | |
| 334 | 379 | */ |
| 335 | 380 | public function maybe_create_action( $action, $forms ) { |
| 336 | 381 | if ( isset( $action['ID'] ) && is_numeric( $action['ID'] ) && isset( $forms[ $action['menu_order'] ] ) && $forms[ $action['menu_order'] ] === 'updated' ) { |
| 337 | 382 | // Update action only |
| 338 | 383 | $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] ); |
| 339 | - $post_id = $this->save_settings( $action ); | |
| 340 | - } else { | |
| 341 | - // Create action | |
| 342 | - $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] ); | |
| 343 | - $post_id = $this->duplicate_one( (object) $action, $action['menu_order'] ); | |
| 384 | + return $this->save_settings( $action ); | |
| 344 | 385 | } |
| 345 | - | |
| 346 | - return $post_id; | |
| 386 | + // Create action | |
| 387 | + $action['post_content'] = FrmAppHelper::maybe_json_decode( $action['post_content'] ); | |
| 388 | + return $this->duplicate_one( (object) $action, $action['menu_order'] ); | |
| 347 | 389 | } |
| 348 | 390 | |
| 349 | 391 | /** |
| 350 | - * @param object $action | |
| 392 | + * @param object $action | |
| 393 | + * @param int|string $form_id | |
| 394 | + * | |
| 395 | + * @return int|WP_Error | |
| 351 | 396 | */ |
| 352 | 397 | public function duplicate_one( $action, $form_id ) { |
| 353 | 398 | global $frm_duplicate_ids; |
| 354 | 399 | |
| @@ -360,9 +405,9 @@ | ||
| 360 | 405 | $action->post_content[ $key ] = $frm_duplicate_ids[ $val ]; |
| 361 | 406 | } elseif ( ! is_array( $val ) ) { |
| 362 | 407 | $action->post_content[ $key ] = FrmFieldsHelper::switch_field_ids( $val ); |
| 363 | 408 | } elseif ( isset( $switch[ $key ] ) && is_array( $switch[ $key ] ) ) { |
| 364 | - // loop through each value if empty | |
| 409 | + // Loop through each value if empty | |
| 365 | 410 | if ( empty( $switch[ $key ] ) ) { |
| 366 | 411 | $switch[ $key ] = array_keys( $val ); |
| 367 | 412 | } |
| 368 | 413 | |
| @@ -377,8 +422,15 @@ | ||
| 377 | 422 | |
| 378 | 423 | return $this->save_settings( $action ); |
| 379 | 424 | } |
| 380 | 425 | |
| 426 | + /** | |
| 427 | + * @param array $action | |
| 428 | + * @param array|string $subkey | |
| 429 | + * @param mixed $val | |
| 430 | + * | |
| 431 | + * @return array | |
| 432 | + */ | |
| 381 | 433 | private function duplicate_array_walk( $action, $subkey, $val ) { |
| 382 | 434 | global $frm_duplicate_ids; |
| 383 | 435 | |
| 384 | 436 | if ( is_array( $subkey ) ) { |
| @@ -394,11 +446,11 @@ | ||
| 394 | 446 | } else { |
| 395 | 447 | foreach ( (array) $val as $ck => $cv ) { |
| 396 | 448 | if ( is_array( $cv ) ) { |
| 397 | 449 | $action[ $ck ] = $this->duplicate_array_walk( $action[ $ck ], $subkey, $cv ); |
| 398 | - } elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) { | |
| 450 | + } elseif ( $ck == $subkey && isset( $frm_duplicate_ids[ $cv ] ) ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 399 | 451 | $action[ $ck ] = $frm_duplicate_ids[ $cv ]; |
| 400 | - } elseif ( $ck == $subkey ) { | |
| 452 | + } elseif ( $ck == $subkey ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 401 | 453 | $action[ $ck ] = $this->maybe_switch_field_ids( $action[ $ck ] ); |
| 402 | 454 | } |
| 403 | 455 | } |
| 404 | 456 | }//end if |
| @@ -409,8 +461,12 @@ | ||
| 409 | 461 | /** |
| 410 | 462 | * Deal with changed settings. |
| 411 | 463 | * |
| 412 | 464 | * Do NOT over-ride this function |
| 465 | + * | |
| 466 | + * @param int|string $form_id | |
| 467 | + * | |
| 468 | + * @return array|null | |
| 413 | 469 | */ |
| 414 | 470 | public function update_callback( $form_id ) { |
| 415 | 471 | $this->form_id = $form_id; |
| 416 | 472 | |
| @@ -417,20 +473,20 @@ | ||
| 417 | 473 | $all_instances = $this->get_settings(); |
| 418 | 474 | |
| 419 | 475 | // We need to update the data |
| 420 | 476 | if ( $this->updated ) { |
| 421 | - return; | |
| 477 | + return null; | |
| 422 | 478 | } |
| 423 | 479 | |
| 424 | 480 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 425 | - if ( isset( $_POST[ $this->option_name ] ) && is_array( $_POST[ $this->option_name ] ) ) { | |
| 426 | - // Sanitizing removes scripts and <email> type of values. | |
| 427 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing | |
| 428 | - $settings = wp_unslash( $_POST[ $this->option_name ] ); | |
| 429 | - } else { | |
| 430 | - return; | |
| 481 | + if ( ! isset( $_POST[ $this->option_name ] ) || ! is_array( $_POST[ $this->option_name ] ) ) { | |
| 482 | + return null; | |
| 431 | 483 | } |
| 432 | 484 | |
| 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 | + | |
| 433 | 489 | $action_ids = array(); |
| 434 | 490 | |
| 435 | 491 | foreach ( $settings as $number => $new_instance ) { |
| 436 | 492 | $this->_set( $number ); |
| @@ -440,9 +496,9 @@ | ||
| 440 | 496 | if ( ! isset( $new_instance['post_status'] ) ) { |
| 441 | 497 | $new_instance['post_status'] = 'draft'; |
| 442 | 498 | } |
| 443 | 499 | |
| 444 | - // settings were never opened, so don't update | |
| 500 | + // Settings were never opened, so don't update | |
| 445 | 501 | if ( ! isset( $new_instance['post_title'] ) ) { |
| 446 | 502 | $this->maybe_update_status( $new_instance, $old_instance ); |
| 447 | 503 | $action_ids[] = $new_instance['ID']; |
| 448 | 504 | $this->updated = true; |
| @@ -491,8 +547,9 @@ | ||
| 491 | 547 | * @since 3.04 |
| 492 | 548 | * |
| 493 | 549 | * @param array $new_instance |
| 494 | 550 | * @param array|stdClass $old_instance |
| 551 | + * | |
| 495 | 552 | * @return void |
| 496 | 553 | */ |
| 497 | 554 | protected function maybe_update_status( $new_instance, $old_instance ) { |
| 498 | 555 | if ( ! is_object( $old_instance ) || $new_instance['post_status'] === $old_instance->post_status ) { |
| @@ -509,18 +566,24 @@ | ||
| 509 | 566 | } |
| 510 | 567 | |
| 511 | 568 | /** |
| 512 | 569 | * @param array $settings |
| 570 | + * | |
| 513 | 571 | * @return int|WP_Error |
| 514 | 572 | */ |
| 515 | 573 | public function save_settings( $settings ) { |
| 516 | 574 | self::clear_cache(); |
| 517 | - | |
| 518 | 575 | return FrmDb::save_settings( $settings, 'frm_actions' ); |
| 519 | 576 | } |
| 520 | 577 | |
| 578 | + /** | |
| 579 | + * @param int $id | |
| 580 | + * | |
| 581 | + * @return object|null | |
| 582 | + */ | |
| 521 | 583 | public function get_single_action( $id ) { |
| 522 | 584 | $action = get_post( $id ); |
| 585 | + | |
| 523 | 586 | if ( $action ) { |
| 524 | 587 | $action = $this->prepare_action( $action ); |
| 525 | 588 | $this->_set( $id ); |
| 526 | 589 | } |
| @@ -527,16 +590,29 @@ | ||
| 527 | 590 | |
| 528 | 591 | return $action; |
| 529 | 592 | } |
| 530 | 593 | |
| 594 | + /** | |
| 595 | + * @param int|string $form_id | |
| 596 | + * | |
| 597 | + * @return array | |
| 598 | + */ | |
| 531 | 599 | public function get_one( $form_id ) { |
| 532 | 600 | return $this->get_all( $form_id, 1 ); |
| 533 | 601 | } |
| 534 | 602 | |
| 603 | + /** | |
| 604 | + * @param int|string $form_id | |
| 605 | + * @param string $type | |
| 606 | + * @param array $atts | |
| 607 | + * | |
| 608 | + * @return array | |
| 609 | + */ | |
| 535 | 610 | public static function get_action_for_form( $form_id, $type = 'all', $atts = array() ) { |
| 536 | 611 | $action_controls = FrmFormActionsController::get_form_actions( $type ); |
| 537 | - if ( empty( $action_controls ) ) { | |
| 538 | - // don't continue if there are no available actions | |
| 612 | + | |
| 613 | + if ( ! $action_controls ) { | |
| 614 | + // Don't continue if there are no available actions | |
| 539 | 615 | return array(); |
| 540 | 616 | } |
| 541 | 617 | |
| 542 | 618 | if ( 'all' !== $type ) { |
| @@ -542,15 +618,15 @@ | ||
| 542 | 618 | if ( 'all' !== $type ) { |
| 543 | 619 | if ( is_array( $action_controls ) ) { |
| 544 | 620 | return array(); |
| 545 | 621 | } |
| 622 | + | |
| 546 | 623 | return $action_controls->get_all( $form_id, $atts ); |
| 547 | 624 | } |
| 548 | 625 | |
| 549 | 626 | self::prepare_get_action( $atts ); |
| 550 | 627 | |
| 551 | - $limit = self::get_action_limit( $form_id, $atts['limit'] ); | |
| 552 | - | |
| 628 | + $limit = self::get_action_limit( $form_id, $atts['limit'] ); | |
| 553 | 629 | $args = self::action_args( $form_id, $limit ); |
| 554 | 630 | $args['post_status'] = $atts['post_status']; |
| 555 | 631 | $actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' ); |
| 556 | 632 | |
| @@ -558,10 +634,11 @@ | ||
| 558 | 634 | return array(); |
| 559 | 635 | } |
| 560 | 636 | |
| 561 | 637 | $settings = array(); |
| 638 | + | |
| 562 | 639 | foreach ( $actions as $action ) { |
| 563 | - // some plugins/themes are formatting the post_excerpt | |
| 640 | + // Some plugins/themes are formatting the post_excerpt | |
| 564 | 641 | $action->post_excerpt = sanitize_title( $action->post_excerpt ); |
| 565 | 642 | |
| 566 | 643 | if ( ! isset( $action_controls[ $action->post_excerpt ] ) ) { |
| 567 | 644 | continue; |
| @@ -574,13 +651,9 @@ | ||
| 574 | 651 | break; |
| 575 | 652 | } |
| 576 | 653 | } |
| 577 | 654 | |
| 578 | - if ( 1 === $limit ) { | |
| 579 | - $settings = reset( $settings ); | |
| 580 | - } | |
| 581 | - | |
| 582 | - return $settings; | |
| 655 | + return 1 === $limit ? reset( $settings ) : $settings; | |
| 583 | 656 | } |
| 584 | 657 | |
| 585 | 658 | /** |
| 586 | 659 | * Get the limit for the number of actions for a single form. By default, this is 99, but |
| @@ -593,9 +666,9 @@ | ||
| 593 | 666 | * |
| 594 | 667 | * @return int The filtered limit value. |
| 595 | 668 | */ |
| 596 | 669 | public static function get_action_limit( $form_id, $limit = 99 ) { |
| 597 | - $type = 'all'; | |
| 670 | + $type = 'all'; | |
| 598 | 671 | return (int) apply_filters( 'frm_form_action_limit', (int) $limit, compact( 'type', 'form_id' ) ); |
| 599 | 672 | } |
| 600 | 673 | |
| 601 | 674 | /** |
| @@ -600,20 +673,21 @@ | ||
| 600 | 673 | |
| 601 | 674 | /** |
| 602 | 675 | * @since 3.04 |
| 603 | 676 | * |
| 604 | - * @param array $args | |
| 605 | - * @param string $default_status | |
| 677 | + * @param array|string $args | |
| 678 | + * @param string $default_status | |
| 606 | 679 | * |
| 607 | 680 | * @return void |
| 608 | 681 | */ |
| 609 | 682 | protected static function prepare_get_action( &$args, $default_status = 'publish' ) { |
| 610 | 683 | if ( is_numeric( $args ) ) { |
| 611 | - // for reverse compatibility. $limit was changed to $args | |
| 684 | + // For reverse compatibility. $limit was changed to $args | |
| 612 | 685 | $args = array( |
| 613 | 686 | 'limit' => $args, |
| 614 | 687 | ); |
| 615 | 688 | } |
| 689 | + | |
| 616 | 690 | $defaults = array( |
| 617 | 691 | 'limit' => 99, |
| 618 | 692 | 'post_status' => $default_status, |
| 619 | 693 | ); |
| @@ -620,9 +694,12 @@ | ||
| 620 | 694 | $args = wp_parse_args( $args, $defaults ); |
| 621 | 695 | } |
| 622 | 696 | |
| 623 | 697 | /** |
| 624 | - * @param int $action_id | |
| 698 | + * @param int $action_id | |
| 699 | + * @param string $type | |
| 700 | + * | |
| 701 | + * @return bool|object | |
| 625 | 702 | */ |
| 626 | 703 | public static function get_single_action_type( $action_id, $type ) { |
| 627 | 704 | if ( ! $type ) { |
| 628 | 705 | return false; |
| @@ -636,18 +713,24 @@ | ||
| 636 | 713 | return $action_control->get_single_action( $action_id ); |
| 637 | 714 | } |
| 638 | 715 | |
| 639 | 716 | /** |
| 640 | - * @param int $form_id | |
| 717 | + * @param int $form_id | |
| 718 | + * @param string $type | |
| 641 | 719 | * |
| 642 | 720 | * @return bool |
| 643 | 721 | */ |
| 644 | 722 | public static function form_has_action_type( $form_id, $type ) { |
| 645 | - $payment_actions = self::get_action_for_form( $form_id, $type ); | |
| 646 | - | |
| 647 | - return ! empty( $payment_actions ); | |
| 723 | + $actions = self::get_action_for_form( $form_id, $type ); | |
| 724 | + return ! empty( $actions ); | |
| 648 | 725 | } |
| 649 | 726 | |
| 727 | + /** | |
| 728 | + * @param false|int|string $form_id | |
| 729 | + * @param array $atts | |
| 730 | + * | |
| 731 | + * @return array | |
| 732 | + */ | |
| 650 | 733 | public function get_all( $form_id = false, $atts = array() ) { |
| 651 | 734 | if ( is_array( $atts ) && ! isset( $atts['limit'] ) && $this->action_options['limit'] > 99 ) { |
| 652 | 735 | $atts['limit'] = $this->action_options['limit']; |
| 653 | 736 | } |
| @@ -674,31 +757,30 @@ | ||
| 674 | 757 | unset( $query ); |
| 675 | 758 | |
| 676 | 759 | remove_filter( 'posts_where', 'FrmFormActionsController::limit_by_type' ); |
| 677 | 760 | |
| 678 | - if ( empty( $actions ) ) { | |
| 761 | + if ( ! $actions ) { | |
| 679 | 762 | return array(); |
| 680 | 763 | } |
| 681 | 764 | |
| 682 | 765 | $settings = array(); |
| 766 | + | |
| 683 | 767 | foreach ( $actions as $action ) { |
| 684 | 768 | if ( count( $settings ) >= $limit ) { |
| 685 | 769 | continue; |
| 686 | 770 | } |
| 687 | 771 | |
| 688 | - $action = $this->prepare_action( $action ); | |
| 689 | - | |
| 772 | + $action = $this->prepare_action( $action ); | |
| 690 | 773 | $settings[ $action->ID ] = $action; |
| 691 | 774 | } |
| 692 | 775 | |
| 693 | - if ( 1 === $limit ) { | |
| 694 | - $settings = reset( $settings ); | |
| 695 | - } | |
| 696 | - | |
| 697 | - return $settings; | |
| 776 | + return 1 === $limit ? reset( $settings ) : $settings; | |
| 698 | 777 | } |
| 699 | 778 | |
| 700 | 779 | /** |
| 780 | + * @param int|string $form_id | |
| 781 | + * @param int $limit | |
| 782 | + * | |
| 701 | 783 | * @return array |
| 702 | 784 | */ |
| 703 | 785 | public static function action_args( $form_id = 0, $limit = 99 ) { |
| 704 | 786 | $args = array( |
| @@ -708,9 +790,9 @@ | ||
| 708 | 790 | 'orderby' => 'title', |
| 709 | 791 | 'order' => 'ASC', |
| 710 | 792 | ); |
| 711 | 793 | |
| 712 | - if ( $form_id && $form_id != 'all' ) { | |
| 794 | + if ( $form_id && $form_id !== 'all' ) { | |
| 713 | 795 | $args['menu_order'] = $form_id; |
| 714 | 796 | } |
| 715 | 797 | |
| 716 | 798 | return $args; |
| @@ -716,9 +798,11 @@ | ||
| 716 | 798 | return $args; |
| 717 | 799 | } |
| 718 | 800 | |
| 719 | 801 | /** |
| 720 | - * @param array|WP_Post $action | |
| 802 | + * @param WP_Post $action | |
| 803 | + * | |
| 804 | + * @return WP_Post | |
| 721 | 805 | */ |
| 722 | 806 | public function prepare_action( $action ) { |
| 723 | 807 | $action->post_content = (array) FrmAppHelper::maybe_json_decode( $action->post_content ); |
| 724 | 808 | $action->post_excerpt = sanitize_title( $action->post_excerpt ); |
| @@ -724,18 +808,20 @@ | ||
| 724 | 808 | $action->post_excerpt = sanitize_title( $action->post_excerpt ); |
| 725 | 809 | |
| 726 | 810 | $default_values = $this->get_global_defaults(); |
| 727 | 811 | |
| 728 | - // fill default values | |
| 812 | + // Fill default values | |
| 729 | 813 | $action->post_content += $default_values; |
| 730 | 814 | |
| 731 | 815 | foreach ( $default_values as $k => $vals ) { |
| 732 | - if ( is_array( $vals ) && ! empty( $vals ) ) { | |
| 733 | - if ( 'event' === $k && ! $this->action_options['force_event'] && ! empty( $action->post_content[ $k ] ) ) { | |
| 734 | - continue; | |
| 735 | - } | |
| 736 | - $action->post_content[ $k ] = wp_parse_args( $action->post_content[ $k ], $vals ); | |
| 816 | + if ( ! is_array( $vals ) || ! $vals ) { | |
| 817 | + continue; | |
| 737 | 818 | } |
| 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 ); | |
| 738 | 824 | } |
| 739 | 825 | |
| 740 | 826 | if ( ! is_array( $action->post_content['event'] ) ) { |
| 741 | 827 | $action->post_content['event'] = explode( ',', $action->post_content['event'] ); |
| @@ -744,8 +830,11 @@ | ||
| 744 | 830 | return $action; |
| 745 | 831 | } |
| 746 | 832 | |
| 747 | 833 | /** |
| 834 | + * @param false|int|string $form_id | |
| 835 | + * @param string $type | |
| 836 | + * | |
| 748 | 837 | * @return void |
| 749 | 838 | */ |
| 750 | 839 | public function destroy( $form_id = false, $type = 'default' ) { |
| 751 | 840 | global $wpdb; |
| @@ -752,12 +841,14 @@ | ||
| 752 | 841 | |
| 753 | 842 | $this->form_id = $form_id; |
| 754 | 843 | |
| 755 | 844 | $query = array( 'post_type' => FrmFormActionsController::$action_post_type ); |
| 845 | + | |
| 756 | 846 | if ( $form_id ) { |
| 757 | 847 | $query['menu_order'] = $form_id; |
| 758 | 848 | } |
| 759 | - if ( 'all' != $type ) { | |
| 849 | + | |
| 850 | + if ( 'all' !== $type ) { | |
| 760 | 851 | $query['post_excerpt'] = $this->id_base; |
| 761 | 852 | } |
| 762 | 853 | |
| 763 | 854 | $post_ids = FrmDb::get_col( $wpdb->posts, $query, 'ID' ); |
| @@ -778,8 +869,11 @@ | ||
| 778 | 869 | public static function clear_cache() { |
| 779 | 870 | FrmDb::cache_delete_group( 'frm_actions' ); |
| 780 | 871 | } |
| 781 | 872 | |
| 873 | + /** | |
| 874 | + * @return array | |
| 875 | + */ | |
| 782 | 876 | public function get_settings() { |
| 783 | 877 | return self::get_action_for_form( $this->form_id, $this->id_base ); |
| 784 | 878 | } |
| 785 | 879 | |
| @@ -802,35 +896,43 @@ | ||
| 802 | 896 | |
| 803 | 897 | return $defaults; |
| 804 | 898 | } |
| 805 | 899 | |
| 900 | + /** | |
| 901 | + * @return array | |
| 902 | + */ | |
| 806 | 903 | public function get_global_switch_fields() { |
| 807 | 904 | $switch = $this->get_switch_fields(); |
| 808 | 905 | $switch['conditions'] = array( 'hide_field' ); |
| 809 | 906 | |
| 810 | - $switch = apply_filters( 'frm_global_switch_fields', $switch ); | |
| 811 | - | |
| 812 | - return $switch; | |
| 907 | + return apply_filters( 'frm_global_switch_fields', $switch ); | |
| 813 | 908 | } |
| 814 | 909 | |
| 815 | 910 | /** |
| 816 | 911 | * Migrate settings from form->options into new action. |
| 912 | + * | |
| 913 | + * @param object $form | |
| 914 | + * @param string $update | |
| 915 | + * | |
| 916 | + * @return int|WP_Error | |
| 817 | 917 | */ |
| 818 | 918 | public function migrate_to_2( $form, $update = 'update' ) { |
| 819 | 919 | $action = $this->prepare_new( $form->id ); |
| 820 | 920 | FrmAppHelper::unserialize_or_decode( $form->options ); |
| 821 | 921 | |
| 822 | - // fill with existing options | |
| 922 | + // Fill with existing options | |
| 823 | 923 | foreach ( $action->post_content as $name => $val ) { |
| 824 | - if ( isset( $form->options[ $name ] ) ) { | |
| 825 | - $action->post_content[ $name ] = $form->options[ $name ]; | |
| 826 | - unset( $form->options[ $name ] ); | |
| 924 | + if ( ! isset( $form->options[ $name ] ) ) { | |
| 925 | + continue; | |
| 827 | 926 | } |
| 927 | + | |
| 928 | + $action->post_content[ $name ] = $form->options[ $name ]; | |
| 929 | + unset( $form->options[ $name ] ); | |
| 828 | 930 | } |
| 829 | 931 | |
| 830 | 932 | $action = $this->migrate_values( $action, $form ); |
| 831 | 933 | |
| 832 | - // check if action already exists | |
| 934 | + // Check if action already exists | |
| 833 | 935 | $post_id = get_posts( |
| 834 | 936 | array( |
| 835 | 937 | 'name' => $action->post_name, |
| 836 | 938 | 'post_type' => FrmFormActionsController::$action_post_type, |
| @@ -838,22 +940,24 @@ | ||
| 838 | 940 | 'numberposts' => 1, |
| 839 | 941 | ) |
| 840 | 942 | ); |
| 841 | 943 | |
| 842 | - if ( empty( $post_id ) ) { | |
| 843 | - // create action now | |
| 944 | + if ( ! $post_id ) { | |
| 945 | + // Create action now | |
| 844 | 946 | $post_id = $this->save_settings( $action ); |
| 845 | 947 | } |
| 846 | 948 | |
| 847 | - if ( $post_id && 'update' == $update ) { | |
| 848 | - global $wpdb; | |
| 849 | - $form->options = maybe_serialize( $form->options ); | |
| 949 | + if ( ! $post_id || 'update' !== $update ) { | |
| 950 | + return $post_id; | |
| 951 | + } | |
| 850 | 952 | |
| 851 | - // update form options | |
| 852 | - $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) ); | |
| 853 | - FrmForm::clear_form_cache(); | |
| 854 | - } | |
| 953 | + global $wpdb; | |
| 954 | + $form->options = maybe_serialize( $form->options ); | |
| 855 | 955 | |
| 956 | + // Update form options | |
| 957 | + $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => $form->options ), array( 'id' => $form->id ) ); | |
| 958 | + FrmForm::clear_form_cache(); | |
| 959 | + | |
| 856 | 960 | return $post_id; |
| 857 | 961 | } |
| 858 | 962 | |
| 859 | 963 | /** |
| @@ -858,8 +962,9 @@ | ||
| 858 | 962 | |
| 859 | 963 | /** |
| 860 | 964 | * @param WP_Post $action |
| 861 | 965 | * @param stdClass $entry |
| 966 | + * | |
| 862 | 967 | * @return bool |
| 863 | 968 | */ |
| 864 | 969 | public static function action_conditions_met( $action, $entry ) { |
| 865 | 970 | if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) { |
| @@ -864,44 +969,12 @@ | ||
| 864 | 969 | public static function action_conditions_met( $action, $entry ) { |
| 865 | 970 | if ( is_callable( 'FrmProFormActionsController::action_conditions_met' ) ) { |
| 866 | 971 | return FrmProFormActionsController::action_conditions_met( $action, $entry ); |
| 867 | 972 | } |
| 868 | - | |
| 869 | - $stop = false; | |
| 870 | - return $stop; | |
| 973 | + return false; | |
| 871 | 974 | } |
| 872 | 975 | |
| 873 | 976 | /** |
| 874 | - * Get the value from a specific field and entry | |
| 875 | - * | |
| 876 | - * @since 2.01.02 | |
| 877 | - * | |
| 878 | - * @param object $entry | |
| 879 | - * @param int $field_id | |
| 880 | - * | |
| 881 | - * @return array|bool|mixed|string | |
| 882 | - */ | |
| 883 | - private static function get_value_from_entry( $entry, $field_id ) { | |
| 884 | - $observed_value = ''; | |
| 885 | - | |
| 886 | - if ( isset( $entry->metas[ $field_id ] ) ) { | |
| 887 | - $observed_value = $entry->metas[ $field_id ]; | |
| 888 | - } elseif ( $entry->post_id && FrmAppHelper::pro_is_installed() ) { | |
| 889 | - $field = FrmField::getOne( $field_id ); | |
| 890 | - $observed_value = FrmProEntryMetaHelper::get_post_or_meta_value( | |
| 891 | - $entry, | |
| 892 | - $field, | |
| 893 | - array( | |
| 894 | - 'links' => false, | |
| 895 | - 'truncate' => false, | |
| 896 | - ) | |
| 897 | - ); | |
| 898 | - } | |
| 899 | - | |
| 900 | - return $observed_value; | |
| 901 | - } | |
| 902 | - | |
| 903 | - /** | |
| 904 | 977 | * @param string $class |
| 905 | 978 | * |
| 906 | 979 | * @return array |
| 907 | 980 | */ |
| @@ -906,14 +979,18 @@ | ||
| 906 | 979 | * @return array |
| 907 | 980 | */ |
| 908 | 981 | public static function default_action_opts( $class = '' ) { |
| 909 | 982 | return array( |
| 910 | - 'classes' => 'frm_icon_font ' . $class, | |
| 911 | - 'active' => false, | |
| 912 | - 'limit' => 0, | |
| 983 | + 'classes' => 'frmfont ' . $class, | |
| 984 | + 'active' => false, | |
| 985 | + 'limit' => 0, | |
| 986 | + 'description' => '', | |
| 913 | 987 | ); |
| 914 | 988 | } |
| 915 | 989 | |
| 990 | + /** | |
| 991 | + * @return array | |
| 992 | + */ | |
| 916 | 993 | public static function trigger_labels() { |
| 917 | 994 | $triggers = array( |
| 918 | 995 | 'draft' => __( 'Draft is saved', 'formidable' ), |
| 919 | 996 | 'create' => __( 'Entry is created', 'formidable' ), |
| @@ -925,18 +1002,40 @@ | ||
| 925 | 1002 | return apply_filters( 'frm_action_triggers', $triggers ); |
| 926 | 1003 | } |
| 927 | 1004 | |
| 928 | 1005 | /** |
| 1006 | + * @param string $action_key The unique key for this action instance. | |
| 1007 | + * | |
| 929 | 1008 | * @return void |
| 930 | 1009 | */ |
| 931 | - public function render_conditional_logic_call_to_action() { | |
| 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 | |
| 932 | 1018 | ?> |
| 933 | - <h3> | |
| 934 | - <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 ); ?>"> | |
| 935 | - <?php esc_html_e( 'Use Conditional Logic', 'formidable' ); ?> | |
| 936 | - </a> | |
| 937 | - </h3> | |
| 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> | |
| 938 | 1036 | <?php |
| 1037 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 939 | 1038 | } |
| 940 | 1039 | |
| 941 | 1040 | /** |
| 942 | 1041 | * @return string |
| @@ -950,8 +1049,9 @@ | ||
| 950 | 1049 | * |
| 951 | 1050 | * @since 6.10 |
| 952 | 1051 | * |
| 953 | 1052 | * @param int $form_id Form ID. |
| 1053 | + * | |
| 954 | 1054 | * @return object[] |
| 955 | 1055 | */ |
| 956 | 1056 | protected function get_form_fields( $form_id ) { |
| 957 | 1057 | // Get form fields, include embedded and repeater child fields. |