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