type
4 months ago
div-row.php
4 months ago
div-rows.php
4 months ago
form.php
4 months ago
list-row.php
4 months ago
list-rows.php
4 months ago
p-row.php
4 months ago
p-rows.php
4 months ago
table-row.php
4 months ago
table-rows.php
4 months ago
form.php
265 lines
| 1 | <?php |
| 2 | |
| 3 | // Don't load directly. |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | die( '-1' ); |
| 6 | } |
| 7 | |
| 8 | // phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound |
| 9 | |
| 10 | /** |
| 11 | * @var string $form_type |
| 12 | * @var PodsUI $obj |
| 13 | * @var Pods $pod |
| 14 | * @var null|string $thank_you |
| 15 | * @var null|string $thank_you_alt |
| 16 | * @var null|string $label |
| 17 | * @var string|null $form_key |
| 18 | */ |
| 19 | |
| 20 | use Pods\Static_Cache; |
| 21 | |
| 22 | pods_form_enqueue_script( 'pods' ); |
| 23 | pods_form_enqueue_style( 'pods-form' ); |
| 24 | |
| 25 | $is_settings_pod = 'settings' === $pod->pod_data['type']; |
| 26 | |
| 27 | if ( empty( $fields ) || ! is_array( $fields ) ) { |
| 28 | $fields = $obj->pod->fields; |
| 29 | } |
| 30 | |
| 31 | if ( ! isset( $duplicate ) || $is_settings_pod ) { |
| 32 | $duplicate = false; |
| 33 | } else { |
| 34 | $duplicate = (boolean) $duplicate; |
| 35 | } |
| 36 | |
| 37 | $groups = PodsInit::$meta->groups_get( $pod->pod_data['type'], $pod->pod_data['name'], $fields ); |
| 38 | |
| 39 | if ( 1 === count( $groups ) ) { |
| 40 | $first_group = current( $groups ); |
| 41 | |
| 42 | if ( 0 === count( $first_group['fields'] ) ) { |
| 43 | $groups = []; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | $pod_name = $pod->pod_data['name']; |
| 48 | $pod_options = $pod->pod_data['options']; |
| 49 | $pod_options = apply_filters( "pods_advanced_content_type_pod_data_{$pod_name}", $pod_options, $pod->pod_data['name'] ); |
| 50 | $pod_options = apply_filters( 'pods_advanced_content_type_pod_data', $pod_options, $pod->pod_data['name'] ); |
| 51 | |
| 52 | $group_fields = array(); |
| 53 | $submittable_fields = array(); |
| 54 | |
| 55 | foreach ( $groups as $g => $group ) { |
| 56 | // unset fields |
| 57 | foreach ( $group['fields'] as $k => $field ) { |
| 58 | if ( in_array( $field['name'], array( 'created', 'modified' ), true ) ) { |
| 59 | unset( $group['fields'][ $k ] ); |
| 60 | |
| 61 | continue; |
| 62 | } elseif ( ! pods_permission( $field ) ) { |
| 63 | if ( pods_v_bool( 'hidden', $field['options'] ) ) { |
| 64 | $group['fields'][ $k ] = pods_form_field_make_hidden( $group['fields'][ $k ] ); |
| 65 | } elseif ( pods_v_bool( 'read_only_restricted', $group['fields'][ $k ]['options'] ) ) { |
| 66 | $group['fields'][ $k ] = pods_form_field_make_readonly( $group['fields'][ $k ] ); |
| 67 | } else { |
| 68 | unset( $group['fields'][ $k ] ); |
| 69 | |
| 70 | continue; |
| 71 | } |
| 72 | } elseif ( ! pods_has_permissions( $field ) ) { |
| 73 | if ( pods_v_bool( 'hidden', $field['options'] ) ) { |
| 74 | $group['fields'][ $k ] = pods_form_field_make_hidden( $group['fields'][ $k ] ); |
| 75 | } elseif ( pods_v_bool( 'read_only', $field['options'] ) ) { |
| 76 | $group['fields'][ $k ] = pods_form_field_make_readonly( $group['fields'][ $k ] ); |
| 77 | } |
| 78 | }//end if |
| 79 | |
| 80 | if ( ! pods_v_bool( 'readonly', $field ) ) { |
| 81 | $submittable_fields[ $field['name'] ] = $group['fields'][ $k ]; |
| 82 | } |
| 83 | |
| 84 | $group_fields[ $field['name'] ] = $group['fields'][ $k ]; |
| 85 | }//end foreach |
| 86 | $groups[ $g ] = $group; |
| 87 | }//end foreach |
| 88 | |
| 89 | if ( ! isset( $thank_you_alt ) ) { |
| 90 | $thank_you_alt = $thank_you; |
| 91 | } |
| 92 | |
| 93 | $uri_hash = wp_create_nonce( 'pods_uri_' . pods_current_path() ); |
| 94 | $field_hash = wp_create_nonce( 'pods_fields_' . implode( ',', array_keys( $submittable_fields ) ) ); |
| 95 | |
| 96 | if ( is_user_logged_in() ) { |
| 97 | $uid = 'user_' . get_current_user_id(); |
| 98 | } else { |
| 99 | $uid = pods_session_id(); |
| 100 | } |
| 101 | |
| 102 | $item_id = $duplicate ? 0 : $pod->id(); |
| 103 | |
| 104 | $nonce = wp_create_nonce( 'pods_form_' . $pod->pod . '_' . $uid . '_' . $item_id . '_' . $uri_hash . '_' . $field_hash ); |
| 105 | |
| 106 | $submit_result = null; |
| 107 | |
| 108 | if ( isset( $_POST['_pods_nonce'] ) ) { |
| 109 | try { |
| 110 | $params = pods_unslash( (array) $_POST ); |
| 111 | |
| 112 | $submit_result = 0 !== $pod->api->process_form( $params, $pod, $submittable_fields, $thank_you ); |
| 113 | } catch ( Exception $e ) { |
| 114 | $obj->error( wp_kses_post( $e->getMessage() ) ); |
| 115 | } |
| 116 | } elseif ( isset( $_GET['do'] ) ) { |
| 117 | $submit_result = 0 !== $pod->id(); |
| 118 | } |
| 119 | |
| 120 | if ( null !== $submit_result ) { |
| 121 | $messages = [ |
| 122 | 'success' => __( 'Success', 'pods' ), |
| 123 | 'error' => __( 'Error', 'pods' ), |
| 124 | // translators: %s: The singular item label. |
| 125 | 'view_item' => __( 'View %s', 'pods' ), |
| 126 | // translators: %s: The singular item label. |
| 127 | 'success_saved' => _x( '%s saved successfully', 'The success message shown after saving form', 'pods' ), |
| 128 | // translators: %s: The singular item label. |
| 129 | 'success_created' => _x( '%s created successfully', 'The success message shown after saving form', 'pods' ), |
| 130 | // translators: %s: The singular item label. |
| 131 | 'success_duplicated' => _x( '%s duplicated successfully', 'The success message shown after saving form', 'pods' ), |
| 132 | // translators: %s: The singular item label. |
| 133 | 'error_saved' => _x( '%s not saved', 'The error message shown after saving form', 'pods' ), |
| 134 | // translators: %s: The singular item label. |
| 135 | 'error_created' => _x( '%s not created', 'The error message shown after saving form', 'pods' ), |
| 136 | // translators: %s: The singular item label. |
| 137 | 'error_duplicated' => _x( '%s not duplicated', 'The error message shown after saving form', 'pods' ), |
| 138 | ]; |
| 139 | |
| 140 | $success_message = sprintf( '<strong>%1$s:</strong> %2$s.', $messages['success'], $messages['success_saved'] ); |
| 141 | $error_message = sprintf( '<strong>%1$s:</strong> %2$s.', $messages['error'], $messages['error_saved'] ); |
| 142 | |
| 143 | if ( ! $is_settings_pod ) { |
| 144 | if ( 'create' === pods_v( 'do', 'post', pods_v( 'do', 'get', 'save' ) ) ) { |
| 145 | $success_message = sprintf( '<strong>%1$s:</strong> %2$s.', $messages['success'], $messages['success_created'] ); |
| 146 | $error_message = sprintf( '<strong>%1$s:</strong> %2$s.', $messages['error'], $messages['error_created'] ); |
| 147 | } elseif ( 'duplicate' === pods_v( 'do', 'get', 'save' ) ) { |
| 148 | $success_message = sprintf( '<strong>%1$s:</strong> %2$s.', $messages['success'], $messages['success_duplicated'] ); |
| 149 | $error_message = sprintf( '<strong>%1$s:</strong> %2$s.', $messages['error'], $messages['error_duplicated'] ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if ( $submit_result ) { |
| 154 | $message = sprintf( $success_message, $obj->item ); |
| 155 | |
| 156 | if ( ! $is_settings_pod && ! empty( $pod_options['detail_url'] ) ) { |
| 157 | $message_view = |
| 158 | $message .= sprintf( |
| 159 | ' <a target="_blank" rel="noopener noreferrer" href="%1$s">%2$s</a>', |
| 160 | esc_url( $pod->field( 'detail_url' ) ), |
| 161 | esc_html( sprintf( $messages['view_item'], $obj->item ) ) |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | $obj->message( wp_kses_post( $message ) ); |
| 166 | } else { |
| 167 | $error = sprintf( $error_message, $obj->item ); |
| 168 | |
| 169 | $obj->error( wp_kses_post( $error ) ); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if ( ! isset( $label ) ) { |
| 174 | $label = __( 'Save', 'pods' ); |
| 175 | } |
| 176 | |
| 177 | $do = 'create'; |
| 178 | |
| 179 | if ( 0 < $pod->id() ) { |
| 180 | if ( $duplicate ) { |
| 181 | $do = 'duplicate'; |
| 182 | } else { |
| 183 | $do = 'save'; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | $counter = (int) pods_static_cache_get( $pod->pod . '-counter', 'pods-forms' ); |
| 188 | |
| 189 | // Shift counter by 1 so that it always starts at 1. |
| 190 | $counter ++; |
| 191 | |
| 192 | // Enforce the counter. |
| 193 | PodsForm::$form_counter = $counter; |
| 194 | |
| 195 | pods_static_cache_set( $pod->pod . '-counter', $counter, 'pods-forms' ); |
| 196 | ?> |
| 197 | |
| 198 | <form |
| 199 | action="" |
| 200 | method="post" |
| 201 | class="pods-submittable pods-form pods-form-pod-<?php echo esc_attr( $pod->pod ); ?> pods-submittable-ajax" |
| 202 | id="pods-form-<?php echo esc_attr( $pod->pod . '-' . $counter ); ?>" |
| 203 | data-pods-pod-name="<?php echo esc_attr( $pod->pod ); ?>" |
| 204 | data-pods-item-id="<?php echo esc_attr( $item_id ); ?>" |
| 205 | data-pods-form-counter="<?php echo esc_attr( $counter ); ?>" |
| 206 | > |
| 207 | <div class="pods-submittable-fields"> |
| 208 | <?php |
| 209 | PodsForm::output_field( 'action', 'pods_admin', 'hidden' ); |
| 210 | PodsForm::output_field( 'method', 'process_form', 'hidden' ); |
| 211 | PodsForm::output_field( 'do', $do, 'hidden' ); |
| 212 | PodsForm::output_field( '_pods_nonce', $nonce, 'hidden' ); |
| 213 | PodsForm::output_field( '_pods_pod', $pod->pod, 'hidden' ); |
| 214 | PodsForm::output_field( '_pods_id', $item_id, 'hidden' ); |
| 215 | PodsForm::output_field( '_pods_uri', $uri_hash, 'hidden' ); |
| 216 | PodsForm::output_field( '_pods_form', implode( ',', array_keys( $submittable_fields ) ), 'hidden' ); |
| 217 | PodsForm::output_field( '_pods_form_key', ! empty( $form_key ) ? $form_key : '', 'hidden' ); |
| 218 | PodsForm::output_field( '_pods_location', $_SERVER['REQUEST_URI'], 'hidden' ); |
| 219 | |
| 220 | pods_view( PODS_DIR . 'ui/forms/type/' . sanitize_title( $form_type ) . '.php', compact( array_keys( get_defined_vars() ) ) ); |
| 221 | ?> |
| 222 | </div> |
| 223 | </form> |
| 224 | |
| 225 | <script type="text/javascript"> |
| 226 | if ( 'undefined' == typeof ajaxurl ) { |
| 227 | var ajaxurl = '<?php echo esc_url_raw( admin_url( 'admin-ajax.php' ) ); ?>'; |
| 228 | } |
| 229 | |
| 230 | if ( 'undefined' == typeof pods_form_thank_you ) { |
| 231 | var pods_form_thank_you = null; |
| 232 | } |
| 233 | |
| 234 | <?php if ( $is_settings_pod ) : ?> |
| 235 | var pods_admin_submit_callback = function ( id ) { |
| 236 | document.location = '<?php echo esc_url_raw( pods_query_arg( array( 'do' => $do ) ) ); ?>'; |
| 237 | } |
| 238 | <?php else : ?> |
| 239 | var pods_admin_submit_callback = function ( id ) { |
| 240 | id = parseInt( id, 10 ); |
| 241 | var thank_you = '<?php echo esc_url_raw( $thank_you ); ?>'; |
| 242 | var thank_you_alt = '<?php echo esc_url_raw( $thank_you_alt ); ?>'; |
| 243 | |
| 244 | if ( 'undefined' != typeof pods_form_thank_you && null !== pods_form_thank_you ) { |
| 245 | thank_you = pods_form_thank_you; |
| 246 | } |
| 247 | |
| 248 | if ( isNaN( id ) ) { |
| 249 | document.location = thank_you_alt.replace( 'X_ID_X', String( 0 ) ); |
| 250 | } |
| 251 | else { |
| 252 | document.location = thank_you.replace( 'X_ID_X', String( id ) ); |
| 253 | } |
| 254 | } |
| 255 | <?php endif; ?> |
| 256 | |
| 257 | jQuery( function ( $ ) { |
| 258 | $( document ).Pods( 'validate' ); |
| 259 | $( document ).Pods( 'submit' ); |
| 260 | $( document ).Pods( 'dependency', true ); |
| 261 | $( document ).Pods( 'confirm' ); |
| 262 | $( document ).Pods( 'exit_confirm' ); |
| 263 | } ); |
| 264 | </script> |
| 265 |