PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.6
Pods – Custom Content Types and Fields v3.3.6
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 / front / form.php
pods / ui / front Last commit date
display 4 months ago pagination 4 months ago filters.php 4 months ago form.php 4 months ago view.php 4 months ago widgets.php 4 months ago
form.php
233 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 array $fields
12 * @var boolean $fields_only
13 * @var Pods $pod
14 * @var array $params
15 * @var string $label
16 * @var string $thank_you
17 * @var string $output_type
18 * @var string|null $form_key
19 */
20
21 pods_form_enqueue_style( 'pods-form' );
22 pods_form_enqueue_script( 'pods' );
23
24 $pod_name = $pod->pod;
25 $id = $pod->id();
26
27 // Set up fields.
28 foreach ( $fields as $k => $field ) {
29 // Make sure all required array keys exist.
30 if ( ! $field instanceof \Pods\Whatsit\Field ) {
31 $field = wp_parse_args( $field, [
32 'name' => '',
33 'type' => '',
34 'label' => '',
35 'help' => '',
36 ] );
37
38 $fields[ $k ] = $field;
39 }
40
41 if ( in_array( $field['name'], [ 'created', 'modified' ], true ) ) {
42 unset( $fields[ $k ] );
43 } elseif ( ! pods_permission( $field ) ) {
44 if ( pods_v( 'hidden', $field, false ) ) {
45 $fields[ $k ] = pods_form_field_make_hidden( $fields[ $k ] );
46 } elseif ( pods_v_bool( 'read_only_restricted', $field ) ) {
47 $fields[ $k ] = pods_form_field_make_readonly( $fields[ $k ] );
48 } else {
49 unset( $fields[ $k ] );
50
51 continue;
52 }
53 } elseif ( ! pods_has_permissions( $field ) ) {
54 if ( pods_v_bool( 'hidden', $field ) ) {
55 $fields[ $k ] = pods_form_field_make_hidden( $fields[ $k ] );
56 } elseif ( pods_v_bool( 'read_only', $field ) ) {
57 $fields[ $k ] = pods_form_field_make_readonly( $fields[ $k ] );
58 }
59 }
60 }
61
62 $submittable_fields = $fields;
63
64 foreach ( $submittable_fields as $k => $field ) {
65 if ( ! pods_v( 'readonly', $field, false ) ) {
66 continue;
67 }
68
69 unset( $submittable_fields[ $k ] );
70 }
71
72 $uri_hash = wp_create_nonce( 'pods_uri_' . pods_current_path() );
73 $field_hash = wp_create_nonce( 'pods_fields_' . implode( ',', array_keys( $submittable_fields ) ) );
74
75 if ( is_user_logged_in() ) {
76 $uid = 'user_' . get_current_user_id();
77 } else {
78 $uid = pods_session_id();
79 }
80
81 $nonce = wp_create_nonce( 'pods_form_' . $pod_name . '_' . $uid . '_' . $id . '_' . $uri_hash . '_' . $field_hash );
82
83 if ( isset( $_POST['_pods_nonce'] ) ) {
84 try {
85 $id = $pod->api->process_form( $_POST, $pod, $submittable_fields, $thank_you );
86 } catch ( Exception $e ) {
87 pods_message( esc_html( $e->getMessage() ), 'error' );
88 }
89 }
90
91 $field_prefix = '';
92
93 $counter = (int) pods_static_cache_get( $pod->pod . '-counter', 'pods-forms' );
94
95 // Shift counter by 1 so that it always starts at 1.
96 $counter ++;
97
98 // Enforce the counter.
99 PodsForm::$form_counter = $counter;
100
101 pods_static_cache_set( $pod->pod . '-counter', $counter, 'pods-forms' );
102 ?>
103
104 <?php if ( ! $fields_only ) : ?>
105 <?php $field_prefix = 'pods_field_'; ?>
106 <form
107 action=""
108 method="post"
109 class="pods-submittable pods-form pods-form-front pods-form-pod-<?php echo esc_attr( $pod_name ); ?> pods-submittable-ajax"
110 data-location="<?php echo esc_attr( pods_enforce_safe_url( $thank_you ) ); ?>"
111 id="pods-form-<?php echo esc_attr( $pod_name . '-' . $counter ); ?>"
112 data-pods-pod-name="<?php echo esc_attr( $pod_name ); ?>"
113 data-pods-item-id="<?php echo esc_attr( $id ); ?>"
114 data-pods-form-counter="<?php echo esc_attr( $counter ); ?>"
115 >
116 <div class="pods-submittable-fields">
117 <?php PodsForm::output_field( 'action', 'pods_admin', 'hidden' ); ?>
118 <?php PodsForm::output_field( 'method', 'process_form', 'hidden' ); ?>
119 <?php PodsForm::output_field( 'do', ( ! empty( $id ) ? 'save' : 'create' ), 'hidden' ); ?>
120 <?php PodsForm::output_field( '_pods_nonce', $nonce, 'hidden' ); ?>
121 <?php PodsForm::output_field( '_pods_pod', $pod_name, 'hidden' ); ?>
122 <?php PodsForm::output_field( '_pods_id', $id, 'hidden' ); ?>
123 <?php PodsForm::output_field( '_pods_uri', $uri_hash, 'hidden' ); ?>
124 <?php PodsForm::output_field( '_pods_form', implode( ',', array_keys( $submittable_fields ) ), 'hidden' ); ?>
125 <?php PodsForm::output_field( '_pods_form_key', ! empty( $form_key ) ? $form_key : '', 'hidden' ); ?>
126 <?php PodsForm::output_field( '_pods_location', $_SERVER['REQUEST_URI'], 'hidden' ); ?>
127 <?php endif; ?>
128
129 <?php
130 /**
131 * Runs before fields are outputted.
132 *
133 * @params array $fields Fields of the form.
134 * @params object $pod The current Pod object.
135 * @params array $params The form's parameters.
136 *
137 * @since 2.3.19
138 */
139 do_action( 'pods_form_pre_fields', $fields, $pod, $params );
140
141 $field_row_classes = 'pods-field-html-class';
142 $heading_tag = 'h3';
143
144 $pre_callback = static function ( $field_name, $id, $field, $pod ) use ( $fields, $params ) {
145 /**
146 * Runs before a field is output.
147 *
148 * @since 2.3.19
149 *
150 * @param array $field The current field.
151 * @param array $fields All fields of the form.
152 * @param object $pod The current Pod object.
153 * @param array $params The form's parameters.
154 */
155 do_action( 'pods_form_pre_field', $field, $fields, $pod, $params );
156 };
157
158 $post_callback = static function ( $field_name, $id, $field, $pod ) use ( $fields, $params ) {
159 /**
160 * Runs after a field is output.
161 *
162 * @since 2.3.19
163 *
164 * @params array $field The current field.
165 * @params array $fields All fields of the form.
166 * @params object $pod The current Pod object.
167 * @params array $params The form's parameters.
168 */
169 do_action( 'pods_form_after_field', $field, $fields, $pod, $params );
170 };
171
172 $template = 'ui/forms/list-rows.php';
173 $template_before = '<div class="pods-form-fieldset">';
174 $template_after = '</div>';
175
176 if ( 'div' === $output_type ) {
177 $template = 'ui/forms/div-rows.php';
178 } elseif ( 'p' === $output_type ) {
179 $template = 'ui/forms/p-rows.php';
180 } elseif ( 'table' === $output_type ) {
181 $template = 'ui/forms/table-rows.php';
182 $template_before = '<table class="pods-form-fieldset">';
183 $template_after = '</table>';
184 }
185
186 echo wp_kses_post( $template_before );
187
188 pods_view( PODS_DIR . $template, compact( array_keys( get_defined_vars() ) ) );
189
190 echo wp_kses_post( $template_after );
191
192 /**
193 * Runs after all fields are outputted.
194 *
195 * @params array $fields Fields of the form
196 * @params object $pod The current Pod object
197 * @params array $params The form's parameters.
198 *
199 * @since 2.3.19
200 */
201 do_action( 'pods_form_after_fields', $fields, $pod, $params );
202 ?>
203
204 <?php if ( ! $fields_only ) : ?>
205 <p class="pods-submit">
206 <img class="waiting" src="<?php echo esc_url( admin_url( '/images/wpspin_light.gif' ) ); ?>" alt="<?php esc_attr_e( 'Submitting...', 'pods' ); ?>">
207 <input type="submit" value=" <?php echo esc_attr( $label ); ?> " class="pods-submit-button" />
208
209 <?php do_action( 'pods_form_after_submit', $pod, $fields, $params ); ?>
210 </p>
211 </div>
212 </form>
213
214 <script type="text/javascript">
215 if ( 'undefined' === typeof pods_form_init ) {
216 var pods_form_init = true;
217
218 document.addEventListener( "DOMContentLoaded", function () {
219 if ( 'undefined' !== typeof jQuery( document ).Pods ) {
220
221 if ( 'undefined' === typeof ajaxurl ) {
222 window.ajaxurl = '<?php echo esc_url_raw( admin_url( 'admin-ajax.php' ) ); ?>';
223 }
224
225 jQuery( document ).Pods( 'validate' );
226 jQuery( document ).Pods( 'submit' );
227 jQuery( document ).Pods( 'dependency', true ); // Pass `true` to trigger init.
228 }
229 }, false );
230 }
231 </script>
232 <?php endif; ?>
233