redirect-to-custom-url.php
3 months ago
register-a-new-user.php
10 months ago
set-custom-response.php
3 months ago
set-custom-response.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SetCustomResponse. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category SetCustomResponse |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\FluentForm\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * SetCustomResponse |
| 22 | * |
| 23 | * @category SetCustomResponse |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class SetCustomResponse extends AutomateAction { |
| 31 | |
| 32 | /** |
| 33 | * Integration type. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $integration = 'FluentForm'; |
| 38 | |
| 39 | /** |
| 40 | * Action name. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $action = 'fluentform_set_custom_response'; |
| 45 | |
| 46 | use SingletonLoader; |
| 47 | |
| 48 | /** |
| 49 | * Register a action. |
| 50 | * |
| 51 | * @param array $actions actions. |
| 52 | * @return array |
| 53 | */ |
| 54 | public function register( $actions ) { |
| 55 | |
| 56 | $actions[ $this->integration ][ $this->action ] = [ |
| 57 | 'label' => __( 'Set Custom Response', 'suretriggers' ), |
| 58 | 'action' => $this->action, |
| 59 | 'function' => [ $this, 'action_listener' ], |
| 60 | ]; |
| 61 | |
| 62 | return $actions; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Action listener. |
| 67 | * |
| 68 | * @param int $user_id user_id. |
| 69 | * @param int $automation_id automation_id. |
| 70 | * @param array $fields fields. |
| 71 | * @param array $selected_options selected options. |
| 72 | * |
| 73 | * @return array |
| 74 | * |
| 75 | * @throws Exception Exception. |
| 76 | */ |
| 77 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 78 | if ( ! defined( 'FLUENTFORM' ) ) { |
| 79 | throw new Exception( 'Fluent Forms plugin is not active.' ); |
| 80 | } |
| 81 | |
| 82 | $form_id = isset( $selected_options['form_id'] ) ? absint( $selected_options['form_id'] ) : 0; |
| 83 | |
| 84 | if ( empty( $form_id ) ) { |
| 85 | return [ |
| 86 | 'status' => 'error', |
| 87 | 'message' => 'Form ID is required.', |
| 88 | ]; |
| 89 | } |
| 90 | |
| 91 | $custom_message = isset( $selected_options['custom_message'] ) ? $selected_options['custom_message'] : ''; |
| 92 | |
| 93 | if ( empty( $custom_message ) ) { |
| 94 | return [ |
| 95 | 'status' => 'error', |
| 96 | 'message' => 'Custom response message is required.', |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | $form_behavior = isset( $selected_options['form_behavior'] ) ? sanitize_text_field( $selected_options['form_behavior'] ) : 'hide_form'; |
| 101 | if ( ! in_array( $form_behavior, [ 'hide_form', 'reset_form' ], true ) ) { |
| 102 | $form_behavior = 'hide_form'; |
| 103 | } |
| 104 | |
| 105 | $override = [ |
| 106 | 'type' => 'custom_message', |
| 107 | 'message' => wp_kses_post( $custom_message ), |
| 108 | 'form_behavior' => $form_behavior, |
| 109 | ]; |
| 110 | |
| 111 | \SureTriggers\Integrations\FluentForm\ottokit_ff_save_override( $form_id, $override ); |
| 112 | |
| 113 | return [ |
| 114 | 'status' => 'success', |
| 115 | 'message' => 'Custom response has been set for form.', |
| 116 | 'form_id' => $form_id, |
| 117 | 'response' => $override['message'], |
| 118 | 'form_behavior' => $form_behavior, |
| 119 | ]; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | SetCustomResponse::get_instance(); |
| 124 |