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