bricksbuilder-submit-form.php
229 lines
| 1 | <?php |
| 2 | /** |
| 3 | * UserSubmitsBricksBuilderForm. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category UserSubmitsBricksBuilderForm |
| 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\BricksBuilder\Triggers; |
| 15 | |
| 16 | use SureTriggers\Controllers\AutomationController; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | |
| 19 | if ( ! class_exists( 'UserSubmitsBricksBuilderForm' ) ) : |
| 20 | |
| 21 | /** |
| 22 | * UserSubmitsBricksBuilderForm |
| 23 | * |
| 24 | * @category UserSubmitsBricksBuilderForm |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class UserSubmitsBricksBuilderForm { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'BricksBuilder'; |
| 39 | |
| 40 | /** |
| 41 | * Trigger name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $trigger = 'user_submits_bricks_builder_form'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Constructor |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public function __construct() { |
| 55 | add_filter( 'bricks/form/response', [ $this, 'register_form_submit_action' ], 10, 2 ); |
| 56 | add_filter( 'sure_trigger_register_trigger', [ $this, 'register' ] ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Register action. |
| 61 | * |
| 62 | * @param array $triggers trigger data. |
| 63 | * @return array |
| 64 | */ |
| 65 | public function register( $triggers ) { |
| 66 | $triggers[ $this->integration ][ $this->trigger ] = [ |
| 67 | 'label' => __( 'Form Submitted', 'suretriggers' ), |
| 68 | 'action' => 'user_submits_bricks_builder_form', |
| 69 | 'common_action' => 'bricksbuilder_after_form_submit', |
| 70 | 'function' => [ $this, 'trigger_listener' ], |
| 71 | 'priority' => 10, |
| 72 | 'accepted_args' => 2, |
| 73 | ]; |
| 74 | |
| 75 | return $triggers; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Adds custom action hook for Bricks Builder form submit. |
| 80 | * |
| 81 | * @param array $response The response object. |
| 82 | * @param array $obj The Bricks Builder object. |
| 83 | * |
| 84 | * @return array |
| 85 | */ |
| 86 | public function register_form_submit_action( $response, $obj ) { |
| 87 | do_action( 'bricksbuilder_after_form_submit', $response, $obj ); |
| 88 | return $response; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Change field label. |
| 93 | * |
| 94 | * @param string $label label. |
| 95 | * @return string |
| 96 | */ |
| 97 | public function modify_field_label( $label ) { |
| 98 | $label = trim( $label ); |
| 99 | if ( strpos( $label, ' ' ) !== false ) { |
| 100 | $label_str = explode( ' ', $label ); |
| 101 | $result_str = array_map( |
| 102 | function ( $val ) { |
| 103 | return strtolower( $val ); |
| 104 | }, |
| 105 | $label_str |
| 106 | ); |
| 107 | $label = implode( '_', $result_str ); |
| 108 | } else { |
| 109 | $label = strtolower( $label ); |
| 110 | } |
| 111 | return $label; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Trigger listener |
| 116 | * |
| 117 | * @param array $response The response object. |
| 118 | * @param array $obj Bricks Form Object. |
| 119 | * @since 1.0.0 |
| 120 | * |
| 121 | * @return void |
| 122 | */ |
| 123 | public function trigger_listener( $response, $obj ) { |
| 124 | if ( ! check_ajax_referer( 'bricks-nonce-form', 'nonce', false ) ) { |
| 125 | return; |
| 126 | } |
| 127 | $post_data = sanitize_post( $_POST ); |
| 128 | |
| 129 | if ( is_object( $obj ) ) { |
| 130 | $files_data = $obj->get_uploaded_files(); |
| 131 | } |
| 132 | $context = []; |
| 133 | if ( ! empty( $post_data ) ) { |
| 134 | $form_id = ( isset( $post_data['formId'] ) ) ? sanitize_text_field( $post_data['formId'] ) : 0; |
| 135 | $context['form_id'] = $form_id; |
| 136 | $fields = []; |
| 137 | $form_fields = []; |
| 138 | $file_fields = []; |
| 139 | $file_field_labels = []; |
| 140 | foreach ( $post_data as $key => $value ) { |
| 141 | if ( str_contains( $key, 'form-field-' ) ) { |
| 142 | $field_id = str_replace( 'form-field-', '', $key ); |
| 143 | $fields[ $field_id ] = $value; |
| 144 | } else { |
| 145 | $fields[ $key ] = $value; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | $bricks_settings = (array) get_option( BRICKS_DB_GLOBAL_SETTINGS ); |
| 150 | if ( array_key_exists( 'postTypes', $bricks_settings ) ) { |
| 151 | $bricks_posts = $bricks_settings['postTypes']; |
| 152 | } else { |
| 153 | $bricks_posts = []; |
| 154 | } |
| 155 | $bricks_posts[] = 'bricks_template'; |
| 156 | |
| 157 | $args = [ |
| 158 | 'post_type' => $bricks_posts, |
| 159 | 'post_status' => 'publish', |
| 160 | 'posts_per_page' => -1, |
| 161 | ]; |
| 162 | |
| 163 | $templates = get_posts( $args ); |
| 164 | |
| 165 | if ( ! empty( $templates ) && is_array( $templates ) ) { // Check if submitted form has fields. |
| 166 | foreach ( $templates as $template ) { |
| 167 | $bb_contents = get_post_meta( $template->ID, BRICKS_DB_PAGE_CONTENT, true ); // Fetch form contents. |
| 168 | if ( ! empty( $bb_contents ) && is_array( $bb_contents ) ) { |
| 169 | foreach ( $bb_contents as $content ) { |
| 170 | if ( $form_id === $content['id'] ) { |
| 171 | $context['template_name'] = html_entity_decode( get_the_title( $template->ID ), ENT_QUOTES, 'UTF-8' ); |
| 172 | $form_fields = ( isset( $content['settings']['fields'] ) ) ? $content['settings']['fields'] : []; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if ( ! empty( $form_fields ) && is_array( $form_fields ) ) { |
| 179 | foreach ( $form_fields as $field ) { |
| 180 | $field_name = ''; |
| 181 | if ( is_array( $field ) && isset( $field['name'] ) ) { |
| 182 | $field_name = str_replace( ' ', '_', $field['name'] ); |
| 183 | } |
| 184 | if ( isset( $fields[ $field['id'] ] ) ) { |
| 185 | $fd_label = ! empty( $field['label'] ) ? $field['label'] : $field['id']; |
| 186 | $context[ $this->modify_field_label( $fd_label ) ] = $fields[ $field['id'] ]; |
| 187 | } elseif ( is_array( $field ) && isset( $fields[ (string) $field_name ] ) ) { |
| 188 | $fd_label = ! empty( $field['label'] ) ? $field['label'] : $field_name; |
| 189 | $context[ $this->modify_field_label( $fd_label ) ] = $fields[ (string) $field_name ]; |
| 190 | } else { |
| 191 | $file_fields[] = $field['id']; |
| 192 | $file_field_labels[ $field['id'] ] = ! empty( $field['label'] ) ? $field['label'] : $field['id']; |
| 193 | $file_field_labels[ $field['id'] ] = isset( $field['name'] ) ? $field['name'] : ( ! empty( $field['label'] ) ? $field['label'] : $field['id'] ); |
| 194 | } |
| 195 | } |
| 196 | if ( ! empty( $file_fields ) ) { |
| 197 | foreach ( $file_fields as $file_field ) { |
| 198 | $key = 'form-field-' . $file_field; |
| 199 | $label = $file_field_labels[ $file_field ]; |
| 200 | $urls = []; |
| 201 | if ( isset( $files_data[ $key ] ) && is_array( $files_data[ $key ] ) ) { |
| 202 | foreach ( $files_data[ $key ] as $value ) { |
| 203 | $urls[] = $value['url']; |
| 204 | } |
| 205 | } elseif ( isset( $files_data[ $label ] ) && is_array( $files_data[ $label ] ) ) { |
| 206 | foreach ( $files_data[ $label ] as $value ) { |
| 207 | $urls[] = $value['url']; |
| 208 | } |
| 209 | } |
| 210 | $context[ $this->modify_field_label( $label ) ] = $urls; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | AutomationController::sure_trigger_handle_trigger( |
| 218 | [ |
| 219 | 'trigger' => $this->trigger, |
| 220 | 'context' => $context, |
| 221 | ] |
| 222 | ); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | UserSubmitsBricksBuilderForm::get_instance(); |
| 227 | |
| 228 | endif; |
| 229 |