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