PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.8.3
Pods – Custom Content Types and Fields v3.2.8.3
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
275 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 // 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().
226 if ( $obj instanceof PodsUI ) {
227 PodsForm::output_field( $obj->num_prefix . '_wpnonce' . $obj->num, wp_create_nonce( 'pods-ui-save' ), 'hidden' );
228 }
229
230 pods_view( PODS_DIR . 'ui/forms/type/' . sanitize_title( $form_type ) . '.php', compact( array_keys( get_defined_vars() ) ) );
231 ?>
232 </div>
233 </form>
234
235 <script type="text/javascript">
236 if ( 'undefined' == typeof ajaxurl ) {
237 var ajaxurl = '<?php echo pods_slash( admin_url( 'admin-ajax.php' ) ); ?>';
238 }
239
240 if ( 'undefined' == typeof pods_form_thank_you ) {
241 var pods_form_thank_you = null;
242 }
243
244 <?php if ( $is_settings_pod ) : ?>
245 var pods_admin_submit_callback = function ( id ) {
246 document.location = '<?php echo pods_slash( pods_query_arg( array( 'do' => $do ) ) ); ?>';
247 }
248 <?php else : ?>
249 var pods_admin_submit_callback = function ( id ) {
250 id = parseInt( id, 10 );
251 var thank_you = '<?php echo esc_url_raw( $thank_you ); ?>';
252 var thank_you_alt = '<?php echo esc_url_raw( $thank_you_alt ); ?>';
253
254 if ( 'undefined' != typeof pods_form_thank_you && null !== pods_form_thank_you ) {
255 thank_you = pods_form_thank_you;
256 }
257
258 if ( isNaN( id ) ) {
259 document.location = thank_you_alt.replace( 'X_ID_X', String( 0 ) );
260 }
261 else {
262 document.location = thank_you.replace( 'X_ID_X', String( id ) );
263 }
264 }
265 <?php endif; ?>
266
267 jQuery( function ( $ ) {
268 $( document ).Pods( 'validate' );
269 $( document ).Pods( 'submit' );
270 $( document ).Pods( 'dependency', true );
271 $( document ).Pods( 'confirm' );
272 $( document ).Pods( 'exit_confirm' );
273 } );
274 </script>
275