PluginProbe
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 3.6.1
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v3.6.1
3.6.1 3.6.0 3.5.3 3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 All 165 releases
everest-forms / includes / evf-entry-functions.php
evf-entry-functions.php
338 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * EverestForms Entry Functions - FIXED for All Forms View
4 *
5 * @package EverestForms\Functions
6 * @since 1.1.0
7 */
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * Get entry.
13 *
14 * @param int|EVF_Entry $id Entry ID or object.
15 * @param bool $with_fields True if empty data should be present.
16 * @param array $args Additional arguments.
17 * @return EVF_Entry|null
18 */
19 function evf_get_entry( $id, $with_fields = false, $args = array() ) {
20 global $wpdb;
21
22 if ( ! isset( $args['cap'] ) && ( is_admin() && ! wp_doing_ajax() ) ) {
23 $args['cap'] = 'everest_forms_view_entry';
24 }
25
26 if ( ! empty( $args['cap'] ) && ! current_user_can( $args['cap'], $id ) ) {
27 return null;
28 }
29
30 $entry = wp_cache_get( $id, 'evf-entry' );
31 if ( false === $entry ) {
32 $entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}evf_entries WHERE entry_id = %d LIMIT 1;", $id ) ); // WPCS: cache ok, DB call ok.
33 wp_cache_add( $id, $entry, 'evf-entry' );
34 }
35
36 if ( is_null( $entry->fields ) && empty( $entry->viewed ) ) {
37 $is_viewed = $wpdb->update(
38 $wpdb->prefix . 'evf_entries',
39 array(
40 'viewed' => 1,
41 'fields' => '{}',
42 ),
43 array(
44 'entry_id' => $entry->entry_id,
45 )
46 );
47
48 if ( $is_viewed ) {
49 $entry->viewed = 1;
50 }
51 }
52
53 $fields = evf_decode( $entry->fields );
54
55 if ( $with_fields && ! empty( $fields ) ) {
56 foreach ( $fields as $field ) {
57 if ( isset( $field['meta_key'], $field['value'] ) ) {
58 $entry->meta[ $field['meta_key'] ] = maybe_serialize( $field['value'] );
59 }
60 }
61 } elseif ( apply_filters( 'everest_forms_get_entry_metadata', true ) ) {
62 $results = wp_cache_get( $id, 'evf-entrymeta' );
63
64 if ( false === $results ) {
65 $results = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->prefix}evf_entrymeta WHERE entry_id = %d", $id ), ARRAY_A );
66 wp_cache_add( $id, $results, 'evf-entrymeta' );
67 }
68
69 if ( $entry && is_object( $entry ) ) {
70 if ( ! empty( $results ) && is_array( $results ) ) {
71 $entry->meta = wp_list_pluck( $results, 'meta_value', 'meta_key' );
72 } else {
73 $entry->meta = array();
74 }
75 } else {
76 $logger = evf_get_logger();
77 $logger->critical(
78 $entry . PHP_EOL,
79 array(
80 'source' => 'fatal-errors',
81 )
82 );
83 }
84 }
85
86 return 0 !== $entry ? $entry : null;
87 }
88
89 /**
90 * Get all entries IDs.
91 *
92 * @param int $form_id Form ID.
93 * @return int[]
94 */
95 function evf_get_entries_ids( $form_id ) {
96 global $wpdb;
97
98 $results = wp_cache_get( $form_id, 'evf-entries-ids' );
99 if ( false === $results ) {
100 $results = $wpdb->get_results( $wpdb->prepare( "SELECT entry_id FROM {$wpdb->prefix}evf_entries WHERE form_id = %d", $form_id ) ); // WPCS: cache ok, DB call ok.
101 wp_cache_add( $form_id, $results, 'evf-entries-ids' );
102 }
103
104 return array_map( 'intval', wp_list_pluck( $results, 'entry_id' ) );
105 }
106
107 /**
108 * Get entry statuses.
109 *
110 * @param array $form_data Form data.
111 *
112 * @return array
113 */
114 function evf_get_entry_statuses( $form_data = array() ) {
115 return apply_filters(
116 'everest_forms_entry_statuses',
117 array(
118 'publish' => esc_html__( 'Published', 'everest-forms' ),
119 'trash' => esc_html__( 'Trash', 'everest-forms' ),
120 'spam' => esc_html__( 'Spam', 'everest-forms' ),
121 ),
122 $form_data
123 );
124 }
125
126
127 /**
128 * Search entries.
129 *
130 * @param array $args Search arguments.
131 * @return array Array of entry IDs.
132 */
133 function evf_search_entries( $args ) {
134 global $wpdb;
135
136 $args = wp_parse_args(
137 $args,
138 array(
139 'limit' => 10,
140 'form_id' => 0,
141 'offset' => 0,
142 'order' => 'DESC',
143 'orderby' => 'entry_id',
144 )
145 );
146
147 if ( ! isset( $args['cap'] ) ) {
148 $args['cap'] = 'everest_forms_view_form_entries';
149 }
150
151 if ( ! empty( $args['form_id'] ) ) {
152 $all_forms = evf_get_all_forms();
153
154 if ( ! array_key_exists( $args['form_id'], $all_forms ) ) {
155 return array();
156 }
157
158 if ( ! empty( $args['cap'] ) && ! current_user_can( $args['cap'], $args['form_id'] ) ) {
159 return array();
160 }
161 }
162
163 $where = array(
164 'default' => "{$wpdb->prefix}evf_entries.entry_id = {$wpdb->prefix}evf_entrymeta.entry_id",
165 );
166
167 $allowed_form_ids = evf()->form->get(
168 '',
169 array(
170 'fields' => 'ids',
171 'cap' => $args['cap'],
172 )
173 );
174
175 $allowed_forms = implode( ',', array_map( 'intval', (array) $allowed_form_ids ) );
176
177 if ( ! empty( $allowed_forms ) ) {
178 $where['arg_form_id'] = "{$wpdb->prefix}evf_entries.form_id IN ( {$allowed_forms} )";
179 } else {
180 $where = array( 'return_empty' => '1=0' );
181 }
182
183 $where = (array) apply_filters( 'everest_forms_search_entries_where', $where, $args );
184 $where_sql = implode( ' AND ', $where );
185 $query = array();
186 $query[] = "SELECT DISTINCT {$wpdb->prefix}evf_entries.entry_id
187 FROM {$wpdb->prefix}evf_entries
188 INNER JOIN {$wpdb->prefix}evf_entrymeta
189 WHERE {$where_sql}";
190
191 if ( ! empty( $args['search'] ) ) {
192 $like = '%' . $wpdb->esc_like( $args['search'] ) . '%';
193 $query[] = $wpdb->prepare( 'AND meta_value LIKE %s', $like );
194 }
195
196 if ( ! empty( $args['form_id'] ) ) {
197 $query[] = $wpdb->prepare( 'AND form_id = %d', absint( $args['form_id'] ) );
198 }
199
200 if ( ! empty( $args['status'] ) ) {
201 if ( 'unread' === $args['status'] ) {
202 $query[] = $wpdb->prepare( 'AND `status` != %s AND `viewed` = 0', 'trash' );
203 } elseif ( 'starred' === $args['status'] ) {
204 $query[] = $wpdb->prepare( 'AND `status` != %s AND `starred` = 1', 'trash' );
205 } elseif ( 'read' === $args['status'] ) {
206 $query[] = $wpdb->prepare( 'AND `status` != %s AND `viewed` = 1', 'trash' );
207 } elseif ( 'pending' === $args['status'] ) {
208 $query[] = $wpdb->prepare( 'AND `status` = %s', 'pending' );
209 } elseif ( 'approved' === $args['status'] ) {
210 $query[] = $wpdb->prepare( 'AND `status` = %s', 'approved' );
211 } elseif ( 'denied' === $args['status'] ) {
212 $query[] = $wpdb->prepare( 'AND `status` = %s', 'denied' );
213 } elseif ( 'spam' === $args['status'] ) {
214 $query[] = $wpdb->prepare( 'AND `status` = %s', 'spam' );
215 } elseif ( 'unspam' === $args['status'] ) {
216 $query[] = $wpdb->prepare( 'AND `status` = %s', 'publish' );
217 } else {
218 $query[] = $wpdb->prepare( 'AND `status` = %s', $args['status'] );
219 }
220 }
221
222 if ( empty( $args['status'] ) || 'draft' !== $args['status'] ) {
223 $query[] = $wpdb->prepare( 'AND `status` <> %s', 'draft' );
224 }
225
226 $valid_fields = array( 'date', 'form_id', 'title', 'status' );
227 $orderby = in_array( $args['orderby'], $valid_fields, true ) ? $args['orderby'] : 'entry_id';
228 $order = 'DESC' === strtoupper( $args['order'] ) ? 'DESC' : 'ASC';
229 $orderby_sql = sanitize_sql_orderby( "{$orderby} {$order}" );
230
231 $query[] = "ORDER BY {$orderby_sql}";
232
233 if ( -1 < $args['limit'] ) {
234 $query[] = $wpdb->prepare( 'LIMIT %d', absint( $args['limit'] ) );
235 }
236
237 if ( 0 < $args['offset'] ) {
238 $query[] = $wpdb->prepare( 'OFFSET %d', absint( $args['offset'] ) );
239 }
240
241 $results = $wpdb->get_results( implode( ' ', $query ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
242
243 return wp_list_pluck( $results, 'entry_id' );
244 }
245
246
247 /**
248 * Get total entries counts by status.
249 *
250 * @param int $form_id Form ID.
251 * @return array
252 */
253 function evf_get_count_entries_by_status( $form_id ) {
254 $form_data = evf()->form->get( $form_id, array( 'content_only' => true ) );
255 $statuses = array_keys( evf_get_entry_statuses( $form_data ) );
256 $counts = array();
257
258 foreach ( $statuses as $status ) {
259 $count = count(
260 evf_search_entries(
261 array(
262 'limit' => -1,
263 'status' => $status,
264 'form_id' => $form_id,
265 )
266 )
267 );
268
269 $counts[ $status ] = $count;
270 }
271
272 return $counts;
273 }
274
275 /**
276 * Get total next entries counts by last entry.
277 *
278 * @since 1.5.0
279 *
280 * @param int $form_id Form ID.
281 * @param int $last_entry Last Form ID.
282 * @return int[]
283 */
284 function evf_get_count_entries_by_last_entry( $form_id, $last_entry ) {
285 global $wpdb;
286
287 $results = wp_cache_get( $form_id, 'evf-last-entries-count' );
288
289 if ( false === $results ) {
290 $results = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(entry_id) FROM {$wpdb->prefix}evf_entries WHERE form_id = %d AND entry_id > %d", $form_id, $last_entry ) );
291 wp_cache_add( $form_id, $results, 'evf-last-entries-count' );
292 }
293
294 return $results;
295 }
296
297 /**
298 * Get all the entries by form id between the start and end date.
299 *
300 * @since 1.7.0
301 *
302 * @param int $form_id Form ID.
303 * @param string $start_date Start date.
304 * @param string $end_date End date.
305 * @param bool $hide_trashed Exclude trashed entries.
306 *
307 * @return array of entries by form ID.
308 */
309 function evf_get_entries_by_form_id( $form_id, $start_date = '', $end_date = '', $hide_trashed = false ) {
310 global $wpdb;
311
312 $query = array();
313 $query[] = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}evf_entries WHERE form_id=%s", $form_id );
314
315 if ( ! empty( $start_date ) ) {
316 $query[] = $wpdb->prepare( 'AND date_created >= %s', $start_date );
317 }
318
319 if ( ! empty( $end_date ) ) {
320 $query[] = $wpdb->prepare( 'AND date_created <= %s', $end_date );
321 }
322
323 $query[] = $wpdb->prepare( 'AND status != %s', 'draft' );
324
325 if ( $hide_trashed ) {
326 $query[] = $wpdb->prepare( 'AND status != %s', 'trash' );
327 }
328
329 $results = wp_cache_get( $form_id, 'evf-search-entries' );
330
331 if ( false === $results ) {
332 $results = $wpdb->get_results( implode( ' ', $query ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
333 wp_cache_add( $form_id, $results, 'evf-search-entries' );
334 }
335
336 return $results;
337 }
338