PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.9.1
Pods – Custom Content Types and Fields v3.3.9.1
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
270 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 // 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().
221 if ( $obj instanceof PodsUI ) {
222 PodsForm::output_field( $obj->num_prefix . '_wpnonce' . $obj->num, wp_create_nonce( 'pods-ui-save' ), 'hidden' );
223 }
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 json_encode( esc_url_raw( 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 json_encode( esc_url_raw( 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 json_encode( esc_url_raw( $thank_you ) ); ?>;
247 var thank_you_alt = <?php echo json_encode( 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