PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 1.07.11
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v1.07.11
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmEntry.php +467 -1039 6.41.07.11 View file →
@@ -1,1086 +1,514 @@
1 1 <?php
2 -if ( ! defined( 'ABSPATH' ) ) {
3 - die( 'You are not allowed to call this page directly.' );
4 -}
2 +if(!defined('ABSPATH')) die(__('You are not allowed to call this page directly.', 'formidable'));
5 3
6 -class FrmEntry {
4 +if(class_exists('FrmEntry'))
5 + return;
7 6
8 - /**
9 - * Create a new entry
10 - *
11 - * @param array $values
12 - *
13 - * @return int | boolean $entry_id
14 - */
15 - public static function create( $values ) {
16 - $entry_id = self::create_entry( $values, 'standard' );
7 +class FrmEntry{
17 8
18 - return $entry_id;
19 - }
9 + function create( $values ){
10 + global $wpdb, $frm_entry_meta;
11 +
12 + $new_values = array(
13 + 'item_key' => FrmAppHelper::get_unique_key($values['item_key'], $wpdb->prefix .'frm_items', 'item_key'),
14 + 'name' => isset($values['name']) ? $values['name'] : $values['item_key'],
15 + 'ip' => $_SERVER['REMOTE_ADDR'],
16 + 'is_draft' => ( ( isset($values['frm_saving_draft']) && $values['frm_saving_draft'] == 1 ) || ( isset($values['is_draft']) && $values['is_draft'] == 1) ) ? 1 : 0,
17 + 'form_id' => isset($values['form_id']) ? (int) $values['form_id']: null,
18 + 'post_id' => isset($values['post_id']) ? (int) $values['post_id']: null,
19 + 'created_at' => isset($values['created_at']) ? $values['created_at'] : current_time('mysql', 1),
20 + 'updated_at' => isset($values['updated_at']) ? $values['updated_at'] : ( isset($values['created_at']) ? $values['created_at'] : current_time('mysql', 1) ),
21 + );
22 +
23 + if(is_array($new_values['name']))
24 + $new_values['name'] = reset($new_values['name']);
25 +
26 + if(isset($values['description']) and !empty($values['description'])){
27 + $new_values['description'] = maybe_serialize($values['description']);
28 + }else{
29 + $referrerinfo = FrmAppHelper::get_referer_info();
30 +
31 + $new_values['description'] = serialize(array('browser' => $_SERVER['HTTP_USER_AGENT'],
32 + 'referrer' => $referrerinfo));
33 + }
34 +
35 + //if(isset($values['id']) and is_numeric($values['id']))
36 + // $new_values['id'] = $values['id'];
37 +
38 + if(isset($values['frm_user_id']) and (is_numeric($values['frm_user_id']) or (is_admin() and !defined('DOING_AJAX')))){
39 + $new_values['user_id'] = $values['frm_user_id'];
40 + }else{
41 + $user_ID = get_current_user_id();
42 + $new_values['user_id'] = $user_ID ? $user_ID : 0;
43 + }
44 +
45 + $new_values['updated_by'] = isset($values['updated_by']) ? $values['updated_by'] : $new_values['user_id'];
46 +
47 + //check for duplicate entries created in the last 5 minutes
48 + if(!defined('WP_IMPORTING')){
49 + $create_entry = true;
50 +
51 + $check_val = $new_values;
52 + $check_val['created_at >'] = date('Y-m-d H:i:s', (strtotime($new_values['created_at']) - (60*5)));
53 + unset($check_val['created_at']);
54 + unset($check_val['updated_at']);
55 + unset($check_val['is_draft']);
56 + unset($check_val['id']);
57 + unset($check_val['item_key']);
58 + if($new_values['item_key'] == $new_values['name'])
59 + unset($check_val['name']);
60 +
61 + global $frmdb;
62 + $entry_exists = $frmdb->get_records($wpdb->prefix .'frm_items', $check_val, 'created_at DESC', '', 'id');
63 + unset($frmdb);
64 +
65 + if($entry_exists and !empty($entry_exists)){
66 + foreach($entry_exists as $entry_exist){
67 + if($create_entry){
68 + $create_entry = false;
69 + //add more checks here to make sure it's a duplicate
70 + if (isset($values['item_meta'])){
71 + $metas = $frm_entry_meta->get_entry_meta_info($entry_exist->id);
72 + $field_metas = array();
73 + foreach($metas as $meta)
74 + $field_metas[$meta->field_id] = $meta->meta_value;
20 75
21 - /**
22 - * Create a new entry with some differences depending on type
23 - *
24 - * @param array $values
25 - * @param string $type
26 - *
27 - * @return int | boolean $entry_id
28 - */
29 - private static function create_entry( $values, $type ) {
30 - $new_values = self::before_insert_entry_in_database( $values, $type );
76 + $diff = array_diff_assoc($field_metas, array_map('maybe_serialize', $values['item_meta']));
77 + foreach($diff as $field_id => $meta_value){
78 + if(!empty($meta_value) and !$create_entry)
79 + $create_entry = true;
80 + }
81 + }
82 + }
83 + }
84 + }
85 +
86 + if ( !$create_entry ) {
87 + return false;
88 + }
89 + }
90 +
91 + $query_results = $wpdb->insert( $wpdb->prefix .'frm_items', $new_values );
92 +
93 + if ( $query_results ) {
94 + $entry_id = $wpdb->insert_id;
95 +
96 + global $frm_vars;
97 + if(!isset($frm_vars['saved_entries']))
98 + $frm_vars['saved_entries'] = array();
99 + $frm_vars['saved_entries'][] = (int)$entry_id;
100 +
101 + if ( isset($values['item_meta']) ) {
102 + $frm_entry_meta->update_entry_metas($entry_id, $values['item_meta']);
103 + }
104 +
105 + do_action('frm_after_create_entry', $entry_id, $new_values['form_id']);
106 + do_action('frm_after_create_entry_'. $new_values['form_id'], $entry_id);
107 + return $entry_id;
108 + } else {
109 + return false;
110 + }
111 + }
112 +
113 + function duplicate( $id ){
114 + global $wpdb, $frm_entry, $frm_entry_meta;
31 115
32 - // Don't check XML entries for duplicates
33 - if ( $type != 'xml' && self::is_duplicate( $new_values, $values ) ) {
34 - return false;
35 - }
116 + $values = $frm_entry->getOne( $id );
36 117
37 - $entry_id = self::continue_to_create_entry( $values, $new_values );
118 + $new_values = array();
119 + $new_values['item_key'] = FrmAppHelper::get_unique_key('', $wpdb->prefix .'frm_items', 'item_key');
120 + $new_values['name'] = $values->name;
121 + $new_values['is_draft'] = $values->is_draft;
122 + $new_values['user_id'] = $new_values['updated_by'] = (int)$values->user_id;
123 + $new_values['form_id'] = $values->form_id ? (int)$values->form_id: null;
124 + $new_values['created_at'] = $new_values['updated_at'] = current_time('mysql', 1);
38 125
39 - return $entry_id;
40 - }
126 + $query_results = $wpdb->insert( $wpdb->prefix .'frm_items', $new_values );
127 + if($query_results){
128 + $entry_id = $wpdb->insert_id;
129 +
130 + global $frm_vars;
131 + if(!isset($frm_vars['saved_entries']))
132 + $frm_vars['saved_entries'] = array();
133 + $frm_vars['saved_entries'][] = (int)$entry_id;
134 +
135 + $frm_entry_meta->duplicate_entry_metas($id, $entry_id);
136 +
137 + do_action('frm_after_duplicate_entry', $entry_id, $new_values['form_id']);
138 + return $entry_id;
139 + }else
140 + return false;
141 + }
41 142
42 - /**
43 - * Check for duplicate entries created in the last minute
44 - *
45 - * @return boolean
46 - */
47 - public static function is_duplicate( $new_values, $values ) {
48 - $duplicate_entry_time = apply_filters( 'frm_time_to_check_duplicates', 60, $new_values );
143 + function update( $id, $values ){
144 + global $wpdb, $frm_entry_meta, $frm_field, $frm_vars;
145 + if(isset($frm_vars['saved_entries']) && is_array($frm_vars['saved_entries']) && in_array((int)$id, (array)$frm_vars['saved_entries']))
146 + return;
49 147
50 - if ( false === self::is_duplicate_check_needed( $values, $duplicate_entry_time ) ) {
51 - return false;
52 - }
148 + $user_ID = get_current_user_id();
149 +
150 + $new_values = array(
151 + 'name' => isset($values['name']) ? $values['name'] : '',
152 + 'form_id' => isset($values['form_id']) ? (int) $values['form_id'] : null,
153 + 'is_draft' => ( ( isset($values['frm_saving_draft']) && $values['frm_saving_draft'] == 1 ) || ( isset($values['is_draft']) && $values['is_draft'] == 1) ) ? 1 : 0,
154 + 'updated_at' => current_time('mysql', 1),
155 + 'updated_by' => isset($values['updated_by']) ? $values['updated_by'] : $user_ID,
156 + );
157 +
158 + if ( isset($values['post_id']) ) {
159 + $new_values['post_id'] = (int) $values['post_id'];
160 + }
53 161
54 - $check_val = $new_values;
55 - $check_val['created_at >'] = gmdate( 'Y-m-d H:i:s', ( strtotime( $new_values['created_at'] ) - absint( $duplicate_entry_time ) ) );
162 + if ( isset($values['item_key']) ) {
163 + $new_values['item_key'] = FrmAppHelper::get_unique_key($values['item_key'], $wpdb->prefix .'frm_items', 'item_key', $id);
164 + }
165 +
166 + if(isset($values['frm_user_id']) and is_numeric($values['frm_user_id']))
167 + $new_values['user_id'] = $values['frm_user_id'];
56 168
57 - unset( $check_val['created_at'], $check_val['updated_at'], $check_val['is_draft'], $check_val['id'], $check_val['item_key'] );
169 + $new_values = apply_filters('frm_update_entry', $new_values, $id);
170 + $query_results = $wpdb->update( $wpdb->prefix .'frm_items', $new_values, compact('id') );
171 + if($query_results)
172 + wp_cache_delete( $id, 'frm_entry');
173 +
174 + if(!isset($frm_vars['saved_entries']))
175 + $frm_vars['saved_entries'] = array();
176 +
177 + $frm_vars['saved_entries'][] = (int)$id;
178 +
179 + if (isset($values['item_meta']))
180 + $frm_entry_meta->update_entry_metas($id, $values['item_meta']);
181 + do_action('frm_after_update_entry', $id, $new_values['form_id']);
182 + do_action('frm_after_update_entry_'. $new_values['form_id'], $id);
183 + return $query_results;
184 + }
58 185
59 - if ( $new_values['item_key'] == $new_values['name'] ) {
60 - unset( $check_val['name'] );
61 - }
186 + function &destroy( $id ){
187 + global $wpdb;
188 + $id = (int)$id;
189 +
190 + $entry = $this->getOne($id);
191 + if ( !$entry ) {
192 + $result = false;
193 + return $result;
194 + }
195 +
196 + do_action('frm_before_destroy_entry', $id, $entry);
197 +
198 + wp_cache_delete( $id, 'frm_entry');
199 + $wpdb->query('DELETE FROM ' . $wpdb->prefix .'frm_item_metas WHERE item_id=' . $id);
200 + $result = $wpdb->query('DELETE FROM ' . $wpdb->prefix .'frm_items WHERE id=' . $id);
201 + return $result;
202 + }
203 +
204 + function &update_form( $id, $value, $form_id ){
205 + global $wpdb;
206 + $form_id = isset($value) ? $form_id : NULL;
207 + $result = $wpdb->update( $wpdb->prefix .'frm_items', array('form_id' => $form_id), array( 'id' => $id ) );
208 + if($result)
209 + wp_cache_delete( $id, 'frm_entry');
210 + return $result;
211 + }
212 +
213 + function getOne( $id, $meta=false){
214 + global $wpdb;
215 +
216 + $entry = wp_cache_get( $id, 'frm_entry' );
217 + if($entry)
218 + return stripslashes_deep($entry);
62 219
63 - $check_val = apply_filters( 'frm_duplicate_check_val', $check_val );
64 -
65 - global $wpdb;
66 - $entry_exists = FrmDb::get_col( $wpdb->prefix . 'frm_items', $check_val, 'id', array( 'order_by' => 'created_at DESC' ) );
67 -
68 - if ( ! $entry_exists || ! isset( $values['item_meta'] ) ) {
69 - return false;
70 - }
71 -
72 - global $frm_vars;
73 - $frm_vars['checking_duplicates'] = true;
74 -
75 - $is_duplicate = false;
76 - foreach ( $entry_exists as $entry_exist ) {
77 - $is_duplicate = true;
78 -
79 - // make sure it's a duplicate
80 - $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist );
81 - $field_metas = array();
82 - foreach ( $metas as $meta ) {
83 - $field_metas[ $meta->field_id ] = $meta->meta_value;
84 - }
85 -
86 - $filtered_vals = array_filter( $values['item_meta'] );
87 - $filtered_vals = self::convert_values_to_their_saved_value( $filtered_vals, $entry_exist );
88 - $field_metas = array_filter( $field_metas );
89 -
90 - // If prev entry is empty and current entry is not, they are not duplicates
91 - if ( empty( $field_metas ) && ! empty( $filtered_vals ) ) {
92 - return false;
93 - }
94 -
95 - // compare serialized values and not arrays
96 - $new_meta = array_map( 'maybe_serialize', $filtered_vals );
97 -
98 - if ( $field_metas === $new_meta ) {
99 - $is_duplicate = true;
100 - break;
101 - }
102 -
103 - if ( count( $field_metas ) !== count( $new_meta ) ) {
104 - // TODO: compare values saved in the post also
105 - $is_duplicate = false;
106 - continue;
107 - }
108 -
109 - $diff = array_diff_assoc( $field_metas, $new_meta );
110 - foreach ( $diff as $field_id => $meta_value ) {
111 - if ( ! empty( $meta_value ) ) {
112 - $is_duplicate = false;
113 - continue;
114 - }
115 - }
116 -
117 - if ( $is_duplicate ) {
118 - break;
119 - }
120 - }
121 -
122 - $frm_vars['checking_duplicates'] = false;
123 -
124 - return $is_duplicate;
125 - }
126 -
127 - /**
128 - * Convert form data to the actual value that would be saved into the database.
129 - * This is important for the duplicate check as something like 'a:2:{s:5:"typed";s:0:"";s:6:"output";s:0:"";}' (a signature value) is actually an empty string and does not get saved.
130 - *
131 - * @param array $filter_vals
132 - * @param int $entry_id
133 - * @return array
134 - */
135 - private static function convert_values_to_their_saved_value( $filter_vals, $entry_id ) {
136 - $reduced = array();
137 - foreach ( $filter_vals as $field_id => $value ) {
138 - $field = FrmFieldFactory::get_field_object( $field_id );
139 - $reduced[ $field_id ] = $field->get_value_to_save( $value, array( 'entry_id' => $entry_id ) );
140 - $reduced[ $field_id ] = $field->set_value_before_save( $reduced[ $field_id ] );
141 - if ( '' === $reduced[ $field_id ] || ( is_array( $reduced[ $field_id ] ) && 0 === count( $reduced[ $field_id ] ) ) ) {
142 - unset( $reduced[ $field_id ] );
143 - }
144 - }
145 - return $reduced;
146 - }
147 -
148 - /**
149 - * Determine if an entry needs to be checked as a possible duplicate
150 - *
151 - * @since 2.0.23
152 - *
153 - * @param array $values
154 - * @param int $duplicate_entry_time
155 - *
156 - * @return bool
157 - */
158 - private static function is_duplicate_check_needed( $values, $duplicate_entry_time ) {
159 - // If time for checking duplicates is set to an empty value, don't check for duplicates
160 - if ( empty( $duplicate_entry_time ) ) {
161 - return false;
162 - }
163 -
164 - // If CSV is importing, don't check for duplicates
165 - if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
166 - return false;
167 - }
168 -
169 - // If repeating field entries are getting created, don't check for duplicates
170 - if ( isset( $values['parent_form_id'] ) && $values['parent_form_id'] ) {
171 - return false;
172 - }
173 -
174 - return true;
175 - }
176 -
177 - public static function duplicate( $id ) {
178 - global $wpdb;
179 -
180 - $values = self::getOne( $id );
181 -
182 - $new_values = array();
183 - $new_values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
184 - $new_values['name'] = $values->name;
185 - $new_values['is_draft'] = $values->is_draft;
186 - $new_values['user_id'] = (int) $values->user_id;
187 - $new_values['updated_by'] = (int) $values->user_id;
188 - $new_values['form_id'] = $values->form_id ? (int) $values->form_id : null;
189 - $new_values['created_at'] = current_time( 'mysql', 1 );
190 - $new_values['updated_at'] = $new_values['created_at'];
191 -
192 - $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
193 - if ( ! $query_results ) {
194 - return false;
195 - }
196 -
197 - $entry_id = $wpdb->insert_id;
198 -
199 - global $frm_vars;
200 - if ( ! isset( $frm_vars['saved_entries'] ) ) {
201 - $frm_vars['saved_entries'] = array();
202 - }
203 - $frm_vars['saved_entries'][] = (int) $entry_id;
204 -
205 - FrmEntryMeta::duplicate_entry_metas( $id, $entry_id );
206 - self::clear_cache();
207 -
208 - do_action( 'frm_after_duplicate_entry', $entry_id, $new_values['form_id'], array( 'old_id' => $id ) );
209 -
210 - return $entry_id;
211 - }
212 -
213 - /**
214 - * Update an entry (not via XML)
215 - *
216 - * @param int $id
217 - * @param array $values
218 - *
219 - * @return boolean|int $update_results
220 - */
221 - public static function update( $id, $values ) {
222 - $update_results = self::update_entry( $id, $values, 'standard' );
223 -
224 - return $update_results;
225 - }
226 -
227 - /**
228 - * Update an entry with some differences depending on the update type
229 - *
230 - * @since 2.0.16
231 - *
232 - * @param int $id
233 - * @param array $values
234 - *
235 - * @return boolean|int $query_results
236 - */
237 - private static function update_entry( $id, $values, $update_type ) {
238 - global $wpdb;
239 -
240 - $update = self::before_update_entry( $id, $values, $update_type );
241 - if ( ! $update ) {
242 - return false;
243 - }
244 -
245 - $new_values = self::package_entry_to_update( $id, $values );
246 -
247 - $query_results = $wpdb->update( $wpdb->prefix . 'frm_items', $new_values, compact( 'id' ) );
248 -
249 - self::after_update_entry( $query_results, $id, $values, $new_values );
250 -
251 - return $query_results;
252 - }
253 -
254 - /**
255 - * Delete an entry.
256 - *
257 - * @param string|int $id
258 - * @return bool True on success, false if nothing was deleted.
259 - */
260 - public static function destroy( $id ) {
261 - global $wpdb;
262 - $id = (int) $id;
263 -
264 - $entry = self::getOne( $id, true ); // Item meta is required for conditional logic in actions with 'delete' events.
265 - if ( ! $entry ) {
266 - $result = false;
267 - return $result;
268 - }
269 -
270 - /**
271 - * Trigger an action to run custom logic before the entry is deleted.
272 - *
273 - * @param int $id The id of the entry that was destroyed.
274 - * @param stdClass $entry The entry object.
275 - */
276 - do_action( 'frm_before_destroy_entry', $id, $entry );
277 -
278 - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE item_id=%d', $id ) );
279 - $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $id ) );
280 -
281 - self::clear_cache();
282 -
283 - /**
284 - * Trigger an action to run custom logic after the entry is deleted.
285 - * Use this hook if you need to update caching after an entry is deleted.
286 - *
287 - * @since 5.4.1
288 - *
289 - * @param int $id The id of the entry that was destroyed.
290 - * @param stdClass $entry The entry object.
291 - */
292 - do_action( 'frm_after_destroy_entry', $id, $entry );
293 -
294 - return $result;
295 - }
296 -
297 - public static function update_form( $id, $value, $form_id ) {
298 - global $wpdb;
299 - $form_id = isset( $value ) ? $form_id : null;
300 - $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) );
301 - if ( $result ) {
302 - self::clear_cache();
303 - }
304 -
305 - return $result;
306 - }
307 -
308 - /**
309 - * Clear entry caching
310 - * Called when an entry is changed
311 - *
312 - * @since 2.0.5
313 - */
314 - public static function clear_cache() {
315 - FrmDb::cache_delete_group( 'frm_entry' );
316 - FrmDb::cache_delete_group( 'frm_item' );
317 - FrmDb::cache_delete_group( 'frm_entry_meta' );
318 - FrmDb::cache_delete_group( 'frm_item_meta' );
319 - }
320 -
321 - /**
322 - * After switching to the wp_loaded hook for processing entries,
323 - * we can no longer use 'name', but check it as a fallback
324 - *
325 - * @since 2.0.11
326 - */
327 - public static function get_new_entry_name( $values, $default = '' ) {
328 - $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default );
329 - if ( is_array( $name ) ) {
330 - $name = reset( $name );
331 - }
332 -
333 - return $name;
334 - }
335 -
336 - /**
337 - * If $entry is numeric, get the entry object
338 - *
339 - * @param int|object $entry by reference
340 - *
341 - * @since 2.0.9
342 - */
343 - public static function maybe_get_entry( &$entry ) {
344 - if ( $entry && is_numeric( $entry ) ) {
345 - $entry = self::getOne( $entry );
346 - } elseif ( empty( $entry ) || 'false' === $entry ) {
347 - $entry = false;
348 - }
349 - }
350 -
351 - public static function getOne( $id, $meta = false ) {
352 - global $wpdb;
353 -
354 - $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
220 + $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
355 221 LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id WHERE ";
222 +
223 + $query .= $wpdb->prepare( is_numeric($id) ? 'it.id=%d' : 'it.item_key=%s', $id);
356 224
357 - $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
358 - $query_args = array( $id );
359 - $query = $wpdb->prepare( $query, $query_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
225 + $entry = $wpdb->get_row($query);
360 226
361 - if ( ! $meta ) {
362 - $entry = FrmDb::check_cache( $id . '_nometa', 'frm_entry', $query, 'get_row' );
363 - self::prepare_entry( $entry );
364 - return $entry;
365 - }
227 + if($meta and $entry){
228 + $metas = $wpdb->get_results($wpdb->prepare("SELECT field_id, meta_value, field_key FROM {$wpdb->prefix}frm_item_metas m LEFT JOIN {$wpdb->prefix}frm_fields f ON m.field_id=f.id WHERE item_id=%d and field_id != %d", $entry->id, 0));
229 +
230 + $entry_metas = array();
231 +
232 + foreach($metas as $meta_val){
233 + $entry_metas[$meta_val->field_id] = $entry_metas[$meta_val->field_key] = maybe_unserialize($meta_val->meta_value);
234 + unset($meta_val);
235 + }
236 + unset($metas);
366 237
367 - $entry = FrmDb::check_cache( $id, 'frm_entry' );
368 - if ( $entry !== false ) {
369 - self::prepare_entry( $entry );
370 - return $entry;
371 - }
238 + $entry->metas = $entry_metas;
372 239
373 - $entry = $wpdb->get_row( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
374 - $entry = self::get_meta( $entry );
375 - self::prepare_entry( $entry );
240 + wp_cache_set( $entry->id, $entry, 'frm_entry');
241 + }
376 242
377 - return $entry;
378 - }
243 + return stripslashes_deep($entry);
244 + }
245 +
246 + function &exists( $id ){
247 + global $wpdb;
248 +
249 + if(wp_cache_get( $id, 'frm_entry' )){
250 + $exists = true;
251 + return $exists;
252 + }
253 +
254 + $where = (is_numeric($id)) ? 'id=%d' : 'item_key=%s';
379 255
380 - /**
381 - * @since 4.02.03
382 - *
383 - * @param object $entry
384 - */
385 - private static function prepare_entry( &$entry ) {
386 - if ( empty( $entry ) ) {
387 - return;
388 - }
256 + $id = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$wpdb->prefix}frm_items WHERE $where", $id));
257 +
258 + $exists = ($id && $id > 0) ? true : false;
259 + return $exists;
260 + }
389 261
390 - FrmAppHelper::unserialize_or_decode( $entry->description );
391 - $entry = wp_unslash( $entry ); // TODO: Remove slashes on input only, not output.
392 - }
393 -
394 - /**
395 - * @since 4.02.03
396 - *
397 - * @param array $entries
398 - */
399 - private static function prepare_entries( &$entries ) {
400 - foreach ( $entries as $k => $entry ) {
401 - self::prepare_entry( $entry );
402 - $entries[ $k ] = $entry;
262 + function getAll($where = '', $order_by = '', $limit = '', $meta=false, $inc_form=true){
263 + global $wpdb;
264 +
265 + if(is_numeric($limit))
266 + $limit = " LIMIT {$limit}";
267 +
268 + if($inc_form){
269 + $query = "SELECT it.*, fr.name as form_name,fr.form_key as form_key
270 + FROM {$wpdb->prefix}frm_items it LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id" .
271 + FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
272 + }else{
273 + $query = "SELECT it.id, it.item_key, it.name, it.ip, it.form_id, it.post_id, it.user_id, it.updated_by,
274 + it.created_at, it.updated_at, it.is_draft FROM {$wpdb->prefix}frm_items it" .
275 + FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
276 + }
277 +
278 + if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
279 + // sort by a requested field
280 + $query = str_replace( " FROM {$wpdb->prefix}frm_items ", ", (SELECT meta_value FROM {$wpdb->prefix}frm_item_metas WHERE field_id = {$order_matches[1]} AND item_id = it.id) as meta_{$order_matches[1]} FROM {$wpdb->prefix}frm_items ", $query );
403 281 }
404 - }
282 +
283 + $entries = $wpdb->get_results($query, OBJECT_K);
284 + unset($query);
285 +
286 + if($meta and $entries){
287 + if($limit == '' and !is_array($where) and preg_match('/^it\.form_id=\d+$/', $where)){
288 + $meta_where = $wpdb->prepare('fi.form_id=%d', substr($where, 11));
289 + }else if($limit == '' and is_array($where) and count($where) == 1 and isset($where['it.form_id'])){
290 + $meta_where = $wpdb->prepare('fi.form_id=%d', $where['it.form_id']);
291 + }else{
292 + $meta_where = "item_id in (". implode(',', array_filter(array_keys($entries), 'is_numeric')) .")";
293 + }
294 + $query = "SELECT item_id, meta_value, field_id, field_key FROM {$wpdb->prefix}frm_item_metas it
295 + LEFT OUTER JOIN {$wpdb->prefix}frm_fields fi ON it.field_id=fi.id
296 + WHERE $meta_where and field_id != 0";
297 +
298 + $metas = $wpdb->get_results($query);
299 + unset($query);
300 +
301 + if($metas){
302 + foreach($metas as $m_key => $meta_val){
303 + if(!isset($entries[$meta_val->item_id]))
304 + continue;
305 +
306 + if(!isset($entries[$meta_val->item_id]->metas))
307 + $entries[$meta_val->item_id]->metas = array();
308 +
309 + $entries[$meta_val->item_id]->metas[$meta_val->field_id] = $entries[$meta_val->item_id]->metas[$meta_val->field_key] = maybe_unserialize($meta_val->meta_value);
310 + }
311 +
312 + foreach($entries as $entry){
313 + wp_cache_set( $entry->id, $entry, 'frm_entry');
314 + unset($entry);
315 + }
316 + }
317 + }
318 +
319 + return stripslashes_deep($entries);
320 + }
405 321
406 - public static function get_meta( $entry ) {
407 - if ( ! $entry ) {
408 - return $entry;
409 - }
322 + // Pagination Methods
323 + function getRecordCount($where=''){
324 + global $wpdb;
325 + if(is_numeric($where)){
326 + $query = "SELECT COUNT(*) FROM {$wpdb->prefix}frm_items WHERE form_id=". $where;
327 + }else{
328 + $query = "SELECT COUNT(*) FROM {$wpdb->prefix}frm_items it LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id" .
329 + FrmAppHelper::prepend_and_or_where(' WHERE ', $where);
330 + }
331 + return $wpdb->get_var($query);
332 + }
410 333
411 - global $wpdb;
412 - $metas = FrmDb::get_results(
413 - $wpdb->prefix . 'frm_item_metas m LEFT JOIN ' . $wpdb->prefix . 'frm_fields f ON m.field_id=f.id',
414 - array(
415 - 'item_id' => $entry->id,
416 - 'field_id !' => 0,
417 - ),
418 - 'field_id, meta_value, field_key, item_id, f.type'
419 - );
334 + function getPageCount($p_size, $where=''){
335 + if(is_numeric($where))
336 + return ceil((int)$where / (int)$p_size);
337 + else
338 + return ceil((int)$this->getRecordCount($where) / (int)$p_size);
339 + }
420 340
421 - $entry->metas = array();
341 + function validate( $values, $exclude=false ){
342 + global $wpdb, $frm_field, $frm_entry_meta, $frm_settings;
343 +
344 + $errors = array();
345 +
346 + if ( ! isset($values['form_id']) || ! isset($values['item_meta']) ) {
347 + $errors['form'] = __('There was a problem with your submission. Please try again.', 'formidable');
348 + return $errors;
349 + }
350 +
351 + if ( is_admin() && is_user_logged_in() && ( ! isset($values['frm_submit_entry_'. $values['form_id']]) || ! wp_verify_nonce($values['frm_submit_entry_'. $values['form_id']], 'frm_submit_entry_nonce') ) ) {
352 + $errors['form'] = __('You do not have permission to do that', 'formidable');
353 + }
354 +
355 + if( !isset($values['item_key']) or $values['item_key'] == '' ){
356 + $_POST['item_key'] = $values['item_key'] = FrmAppHelper::get_unique_key('', $wpdb->prefix .'frm_items', 'item_key');
357 + }
358 +
359 + $where = apply_filters('frm_posted_field_ids', 'fi.form_id='. (int)$values['form_id']);
360 + if($exclude)
361 + $where .= " and fi.type not in ('". implode("','", array_filter($exclude, 'esc_sql')) ."')";
362 +
363 + $posted_fields = $frm_field->getAll($where, 'field_order');
364 +
365 + foreach($posted_fields as $posted_field){
366 + $posted_field->field_options = maybe_unserialize($posted_field->field_options);
367 + $value = '';
368 + if (isset($values['item_meta'][$posted_field->id]))
369 + $value = $values['item_meta'][$posted_field->id];
370 +
371 + if (isset($posted_field->field_options['default_blank']) and $posted_field->field_options['default_blank'] and $value == $posted_field->default_value)
372 + $value = '';
373 +
374 + if(is_array($value) and count($value) === 1)
375 + $value = reset($value);
376 +
377 + if($posted_field->type == 'rte' and !is_array($value) and (trim($value) == '<br>'))
378 + $value = '';
379 +
380 + if ($posted_field->required == '1' and !is_array($value) and trim($value) == ''){
381 + $errors['field'. $posted_field->id] = (!isset($posted_field->field_options['blank']) or $posted_field->field_options['blank'] == '' or $posted_field->field_options['blank'] == 'Untitled cannot be blank') ? $frm_settings->blank_msg : $posted_field->field_options['blank'];
382 + }else if ($posted_field->type == 'text' and !isset($_POST['name'])){
383 + $_POST['name'] = $value;
384 + }
385 +
386 + $_POST['item_meta'][$posted_field->id] = $value;
387 +
388 + if ($posted_field->type == 'captcha' and isset($_POST['recaptcha_challenge_field'])){
389 + global $frm_settings;
422 390
423 - $include_key = apply_filters( 'frm_include_meta_keys', false, array( 'form_id' => $entry->form_id ) );
424 - foreach ( $metas as $meta_val ) {
425 - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
391 + if(!function_exists('recaptcha_check_answer'))
392 + require(FrmAppHelper::plugin_path().'/classes/recaptchalib.php');
426 393
427 - if ( $meta_val->item_id == $entry->id ) {
428 - $entry->metas[ $meta_val->field_id ] = $meta_val->meta_value;
429 - if ( $include_key ) {
430 - $entry->metas[ $meta_val->field_key ] = $entry->metas[ $meta_val->field_id ];
431 - }
432 - continue;
433 - }
394 + $response = recaptcha_check_answer($frm_settings->privkey,
395 + $_SERVER['REMOTE_ADDR'],
396 + $_POST['recaptcha_challenge_field'],
397 + $_POST['recaptcha_response_field']);
434 398
435 - // include sub entries in an array
436 - if ( ! isset( $entry->metas[ $meta_val->field_id ] ) ) {
437 - $entry->metas[ $meta_val->field_id ] = array();
438 - }
399 + if (!$response->is_valid) {
400 + // What happens when the CAPTCHA was entered incorrectly
401 + $errors['captcha-'. $response->error] = $errors['field'. $posted_field->id] = (!isset($posted_field->field_options['invalid']) or $posted_field->field_options['invalid'] == '') ? $frm_settings->re_msg : $posted_field->field_options['invalid'];
402 + }
439 403
440 - $entry->metas[ $meta_val->field_id ][] = $meta_val->meta_value;
404 + }
405 +
406 + $errors = apply_filters('frm_validate_field_entry', $errors, $posted_field, $value);
407 +
408 + }
409 +
410 +
411 + // check for spam
412 + if ( empty($exclude) && isset($values['item_meta']) && !empty($values['item_meta']) && empty($errors) ) {
413 + global $wpcom_api_key;
414 + if ( (function_exists( 'akismet_http_post' ) || is_callable('Akismet::http_post')) && ((get_option('wordpress_api_key') || $wpcom_api_key)) && $this->akismet($values) ) {
415 + $frm_form = new FrmForm();
416 + $form = $frm_form->getOne($values['form_id']);
417 +
418 + if ( isset($form->options['akismet']) && !empty($form->options['akismet']) && ($form->options['akismet'] != 'logged' || !is_user_logged_in()) ) {
419 + $errors['spam'] = __('Your entry appears to be spam!', 'formidable');
420 + }
421 + }
422 +
423 + // check for blacklist keys
424 + if ( $this->blacklist_check($values) ) {
425 + $errors['spam'] = __('Your entry appears to be spam!', 'formidable');
426 + }
427 + }
441 428
442 - unset( $meta_val );
443 - }
444 - unset( $metas );
429 +
430 + $errors = apply_filters('frm_validate_entry', $errors, $values);
431 + return $errors;
432 + }
433 +
434 + // check the blacklisted words
435 + function blacklist_check( $values ) {
436 + if ( ! apply_filters('frm_check_blacklist', true, $values) ) {
437 + return false;
438 + }
439 +
440 + $mod_keys = trim( get_option( 'blacklist_keys' ) );
445 441
446 - FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
447 -
448 - return $entry;
449 - }
450 -
451 - /**
452 - * @param string $id
453 - */
454 - public static function exists( $id ) {
455 - global $wpdb;
456 -
457 - if ( FrmDb::check_cache( $id, 'frm_entry' ) ) {
458 - $exists = true;
459 -
460 - return $exists;
442 + if ( empty( $mod_keys ) ) {
443 + return false;
444 + }
445 +
446 + $content = FrmEntriesHelper::entry_array_to_string($values);
447 +
448 + if ( empty($content) ) {
449 + return false;
461 450 }
462 451
463 - if ( is_numeric( $id ) ) {
464 - $where = array( 'id' => $id );
465 - } else {
466 - $where = array( 'item_key' => $id );
467 - }
468 - $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
452 + $words = explode( "\n", $mod_keys );
469 453
470 - return ( $id && $id > 0 );
471 - }
454 + foreach ( (array) $words as $word ) {
455 + $word = trim( $word );
472 456
473 - public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
474 - global $wpdb;
457 + if ( empty($word) ) {
458 + continue;
459 + }
475 460
476 - $limit = FrmDb::esc_limit( $limit );
461 + if ( preg_match('#' . preg_quote( $word, '#' ) . '#', $content) ) {
462 + return true;
463 + }
464 + }
477 465
478 - $cache_key = FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit . $inc_form;
479 - $entries = wp_cache_get( $cache_key, 'frm_entry' );
480 -
481 - if ( false === $entries ) {
482 - $fields = 'it.id, it.item_key, it.name, it.ip, it.form_id, it.post_id, it.user_id, it.parent_item_id, it.updated_by, it.created_at, it.updated_at, it.is_draft, it.description';
483 - $table = $wpdb->prefix . 'frm_items it ';
484 -
485 - if ( $inc_form ) {
486 - $fields = 'it.*, fr.name as form_name,fr.form_key as form_key';
487 - $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
488 - }
489 -
490 - if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
491 - $fields .= self::sort_by_field( $order_matches[1] );
492 - unset( $order_matches );
493 - }
494 -
495 - // prepare the query
496 - $query = 'SELECT ' . $fields . ' FROM ' . $table . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
497 -
498 - $entries = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
499 - unset( $query );
500 -
501 - FrmDb::set_cache( $cache_key, $entries, 'frm_entry' );
466 + return false;
467 + }
468 +
469 + //Check entries for spam -- returns true if is spam
470 + function akismet($values) {
471 + $content = FrmEntriesHelper::entry_array_to_string($values);
472 +
473 + if ( empty($content) ) {
474 + return false;
502 475 }
476 +
477 + $datas = array();
478 + $datas['blog'] = FrmAppHelper::site_url();
479 + $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
480 + $datas['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
481 + $datas['referrer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : false;
482 + $datas['comment_type'] = 'formidable';
483 + if ( $permalink = get_permalink() )
484 + $datas['permalink'] = $permalink;
503 485
504 - if ( ! $meta || ! $entries ) {
505 - self::prepare_entries( $entries );
506 - return $entries;
507 - }
508 - unset( $meta );
486 + $datas['comment_content'] = $content;
509 487
510 - if ( ! is_array( $where ) && preg_match( '/^it\.form_id=\d+$/', $where ) ) {
511 - $where = array( 'it.form_id' => substr( $where, 11 ) );
512 - }
513 -
514 - $meta_where = array( 'field_id !' => 0 );
515 - if ( $limit == '' && is_array( $where ) && count( $where ) == 1 && isset( $where['it.form_id'] ) ) {
516 - $meta_where['fi.form_id'] = $where['it.form_id'];
517 - } else {
518 - $meta_where['item_id'] = array_keys( $entries );
519 - }
520 -
521 - $metas = FrmDb::get_results(
522 - $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id = fi.id',
523 - $meta_where,
524 - 'item_id, meta_value, field_id, field_key, form_id, fi.type'
525 - );
526 -
527 - unset( $meta_where );
528 -
529 - if ( ! $metas ) {
530 - self::prepare_entries( $entries );
531 - return $entries;
532 - }
533 -
534 - foreach ( $metas as $m_key => $meta_val ) {
535 - if ( ! isset( $entries[ $meta_val->item_id ] ) ) {
536 - continue;
488 + foreach ( $_SERVER as $key => $value ) {
489 + if ( !in_array($key, array('HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW')) && is_string($value) ) {
490 + $datas["$key"] = $value;
491 + } else {
492 + $datas["$key"] = '';
537 493 }
538 -
539 - if ( ! isset( $entries[ $meta_val->item_id ]->metas ) ) {
540 - $entries[ $meta_val->item_id ]->metas = array();
541 - }
542 -
543 - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
544 - $entries[ $meta_val->item_id ]->metas[ $meta_val->field_id ] = $meta_val->meta_value;
545 - unset( $m_key, $meta_val );
494 +
495 + unset($key, $value);
546 496 }
547 497
548 - if ( ! FrmAppHelper::prevent_caching() ) {
549 - foreach ( $entries as $entry ) {
550 - FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
551 - unset( $entry );
552 - }
498 + $query_string = '';
499 + foreach ( $datas as $key => $data ) {
500 + $query_string .= $key . '=' . urlencode( stripslashes( $data ) ) . '&';
501 + unset($key, $data);
553 502 }
554 503
555 - self::prepare_entries( $entries );
556 - return $entries;
557 - }
558 -
559 - /**
560 - * @param int $field_id
561 - * @return string
562 - */
563 - private static function sort_by_field( $field_id ) {
564 - global $wpdb;
565 - $field_id = (int) $field_id;
566 -
567 - $field_options = FrmDb::get_var( 'frm_fields', array( 'id' => $field_id ), 'field_options' );
568 - FrmAppHelper::unserialize_or_decode( $field_options );
569 -
570 - if ( empty( $field_options['post_field'] ) ) {
571 - $sort = ', (SELECT meta_value FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id = ' . $field_id . ' AND item_id = it.id) as meta_' . $field_id;
572 - } else {
573 - $sort = '';
574 - }
575 -
576 - return apply_filters( 'frm_handle_field_column_sort', $sort, $field_id, $field_options );
577 - }
578 -
579 - // Pagination Methods
580 - /**
581 - * @param int|array|string If int, use the form id.
582 - */
583 - public static function getRecordCount( $where = '' ) {
584 - global $wpdb;
585 - $table_join = $wpdb->prefix . 'frm_items it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
586 -
587 - if ( is_numeric( $where ) ) {
588 - $table_join = 'frm_items';
589 - $where = array( 'form_id' => $where );
590 - }
591 -
592 - if ( is_array( $where ) ) {
593 - $count = FrmDb::get_count( $table_join, $where );
594 - } else {
595 - $cache_key = 'count_' . FrmAppHelper::maybe_json_encode( $where );
596 - $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where );
597 - $count = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' );
598 - }
599 -
600 - return $count;
601 - }
602 -
603 - public static function getPageCount( $p_size, $where = '' ) {
604 - $p_size = (int) $p_size;
605 - $count = 1;
606 - if ( $p_size ) {
607 - if ( ! is_numeric( $where ) ) {
608 - $where = self::getRecordCount( $where );
609 - }
610 - $count = ceil( (int) $where / $p_size );
611 - }
612 -
613 - return $count;
614 - }
615 -
616 - /**
617 - * Prepare the data before inserting it into the database
618 - *
619 - * @since 2.0.16
620 - *
621 - * @param array $values
622 - * @param string $type
623 - *
624 - * @return array $new_values
625 - */
626 - private static function before_insert_entry_in_database( &$values, $type ) {
627 -
628 - self::sanitize_entry_post( $values );
629 -
630 - if ( $type != 'xml' ) {
631 - $values = apply_filters( 'frm_pre_create_entry', $values );
632 - }
633 -
634 - $new_values = self::package_entry_data( $values );
635 -
636 - return $new_values;
637 - }
638 -
639 - /**
640 - * Create an entry and perform after create actions
641 - *
642 - * @since 2.0.16
643 - *
644 - * @param array $values
645 - * @param array $new_values
646 - *
647 - * @return boolean|int $entry_id
648 - */
649 - private static function continue_to_create_entry( $values, $new_values ) {
650 - $entry_id = self::insert_entry_into_database( $new_values );
651 - if ( ! $entry_id ) {
652 - return false;
653 - }
654 -
655 - self::after_insert_entry_in_database( $values, $new_values, $entry_id );
656 -
657 - return $entry_id;
658 - }
659 -
660 - /**
661 - * Sanitize the POST values before we use them
662 - *
663 - * @since 2.0
664 - *
665 - * @param array $values The POST values by reference
666 - */
667 - public static function sanitize_entry_post( &$values ) {
668 - $sanitize_method = array(
669 - 'form_id' => 'absint',
670 - 'frm_action' => 'sanitize_title',
671 - 'form_key' => 'sanitize_title',
672 - 'item_key' => 'sanitize_title',
673 - 'item_name' => 'sanitize_text_field',
674 - 'frm_saving_draft' => 'absint',
675 - 'is_draft' => 'absint',
676 - 'post_id' => 'absint',
677 - 'parent_item_id' => 'absint',
678 - 'created_at' => 'sanitize_text_field',
679 - 'updated_at' => 'sanitize_text_field',
680 - );
681 -
682 - FrmAppHelper::sanitize_request( $sanitize_method, $values );
683 - }
684 -
685 - /**
686 - * Prepare the new values for inserting into the database
687 - *
688 - * @since 2.0.16
689 - *
690 - * @param array $values
691 - *
692 - * @return array $new_values
693 - */
694 - private static function package_entry_data( &$values ) {
695 - global $wpdb;
696 -
697 - if ( ! isset( $values['item_key'] ) ) {
698 - $values['item_key'] = '';
699 - }
700 -
701 - $item_name = self::get_new_entry_name( $values, $values['item_key'] );
702 - $new_values = array(
703 - 'item_key' => FrmAppHelper::get_unique_key( $values['item_key'], $wpdb->prefix . 'frm_items', 'item_key' ),
704 - 'name' => FrmAppHelper::truncate( $item_name, 255, 1, '' ),
705 - 'ip' => self::get_ip( $values ),
706 - 'is_draft' => self::get_is_draft_value( $values ),
707 - 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
708 - 'post_id' => (int) self::get_entry_value( $values, 'post_id', 0 ),
709 - 'parent_item_id' => (int) self::get_entry_value( $values, 'parent_item_id', 0 ),
710 - 'created_at' => self::get_created_at( $values ),
711 - 'updated_at' => self::get_updated_at( $values ),
712 - 'description' => self::get_entry_description( $values ),
713 - 'user_id' => self::get_entry_user_id( $values ),
714 - );
715 -
716 - $new_values['updated_by'] = isset( $values['updated_by'] ) ? $values['updated_by'] : $new_values['user_id'];
717 -
718 - return $new_values;
719 - }
720 -
721 - private static function get_entry_value( $values, $name, $default ) {
722 - return isset( $values[ $name ] ) ? $values[ $name ] : $default;
723 - }
724 -
725 - /**
726 - * Get the ip for a new entry.
727 - * Allow the import to override the value.
728 - *
729 - * @since 2.03.10
730 - *
731 - * @param array $values
732 - *
733 - * @return string
734 - */
735 - private static function get_ip( $values ) {
736 - if ( ! FrmAppHelper::ips_saved() ) {
737 - return '';
738 - }
739 -
740 - $ip = FrmAppHelper::get_ip_address();
741 - if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
742 - $ip = self::get_entry_value( $values, 'ip', $ip );
743 - }
744 -
745 - return $ip;
746 - }
747 -
748 - /**
749 - * Get the is_draft value for a new entry
750 - *
751 - * @since 2.0.16
752 - *
753 - * @param array $values
754 - *
755 - * @return int
756 - */
757 - private static function get_is_draft_value( $values ) {
758 - return ( ( isset( $values['frm_saving_draft'] ) && $values['frm_saving_draft'] == 1 ) || ( isset( $values['is_draft'] ) && $values['is_draft'] == 1 ) ) ? 1 : 0;
759 - }
760 -
761 - /**
762 - * Get the created_at value for a new entry
763 - *
764 - * @since 2.0.16
765 - *
766 - * @param array $values
767 - *
768 - * @return string
769 - */
770 - private static function get_created_at( $values ) {
771 - return self::get_entry_value( $values, 'created_at', current_time( 'mysql', 1 ) );
772 - }
773 -
774 - /**
775 - * Get the updated_at value for a new entry
776 - *
777 - * @since 2.0.16
778 - *
779 - * @param array $values
780 - *
781 - * @return string
782 - */
783 - private static function get_updated_at( $values ) {
784 - if ( isset( $values['updated_at'] ) ) {
785 - $updated_at = $values['updated_at'];
786 - } else {
787 - $updated_at = self::get_created_at( $values );
788 - }
789 -
790 - return $updated_at;
791 - }
792 -
793 - /**
794 - * Get the description value for a new entry
795 - *
796 - * @since 2.0.16
797 - *
798 - * @param array $values
799 - *
800 - * @return string
801 - */
802 - private static function get_entry_description( $values ) {
803 - if ( isset( $values['description'] ) && ! empty( $values['description'] ) ) {
804 - $description = FrmAppHelper::maybe_json_encode( $values['description'] );
805 - } else {
806 - $description = json_encode(
807 - array(
808 - 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ),
809 - 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ),
810 - )
811 - );
812 - }
813 -
814 - return $description;
815 - }
816 -
817 - /**
818 - * Get the user_id value for a new entry
819 - *
820 - * @since 2.0.16
821 - *
822 - * @param array $values
823 - *
824 - * @return int
825 - */
826 - private static function get_entry_user_id( $values ) {
827 - if ( isset( $values['frm_user_id'] ) && ( is_numeric( $values['frm_user_id'] ) || FrmAppHelper::is_admin() ) ) {
828 - $user_id = $values['frm_user_id'];
829 - } else {
830 - $current_user_id = get_current_user_id();
831 - $user_id = $current_user_id ? $current_user_id : 0;
832 - }
833 -
834 - return $user_id;
835 - }
836 -
837 - /**
838 - * Insert new entry into the database
839 - *
840 - * @since 2.0.16
841 - *
842 - * @param array $new_values
843 - *
844 - * @return int | boolean $entry_id
845 - */
846 - private static function insert_entry_into_database( $new_values ) {
847 - global $wpdb;
848 -
849 - $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
850 -
851 - if ( ! $query_results ) {
852 - $entry_id = false;
853 - } else {
854 - $entry_id = $wpdb->insert_id;
855 - }
856 -
857 - return $entry_id;
858 - }
859 -
860 - /**
861 - * Add the new entry to global $frm_vars
862 - *
863 - * @since 2.0.16
864 - *
865 - * @param int $entry_id
866 - */
867 - private static function add_new_entry_to_frm_vars( $entry_id ) {
868 - global $frm_vars;
869 -
870 - if ( ! isset( $frm_vars['saved_entries'] ) ) {
871 - $frm_vars['saved_entries'] = array();
872 - }
873 -
874 - $frm_vars['saved_entries'][] = (int) $entry_id;
875 - }
876 -
877 - /**
878 - * Add entry metas, if there are any
879 - *
880 - * @since 2.0.16
881 - *
882 - * @param array $values
883 - * @param int $entry_id
884 - * @return void
885 - */
886 - private static function maybe_add_entry_metas( $values, $entry_id ) {
887 - if ( isset( $values['item_meta'] ) ) {
888 - FrmEntryMeta::update_entry_metas( $entry_id, $values['item_meta'] );
889 - }
890 - self::maybe_add_captcha_meta( (int) $values['form_id'], (int) $entry_id );
891 - }
892 -
893 - /**
894 - * @since 5.0.15
895 - *
896 - * @param int $form_id
897 - * @param int $entry_id
898 - * @return void
899 - */
900 - private static function maybe_add_captcha_meta( $form_id, $entry_id ) {
901 - global $frm_vars;
902 - if ( array_key_exists( 'captcha_scores', $frm_vars ) && array_key_exists( $form_id, $frm_vars['captcha_scores'] ) ) {
903 - $captcha_score_meta = array( 'captcha_score' => $frm_vars['captcha_scores'][ $form_id ] );
904 - FrmEntryMeta::add_entry_meta( $entry_id, 0, '', maybe_serialize( $captcha_score_meta ) );
905 - }
906 - }
907 -
908 - /**
909 - * Trigger frm_after_create_entry hooks
910 - *
911 - * @since 2.0.16
912 - *
913 - * @param int $entry_id
914 - * @param array $new_values
915 - */
916 - private static function after_entry_created_actions( $entry_id, $values, $new_values ) {
917 - // this is a child entry
918 - $is_child = isset( $values['parent_form_id'] ) && isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
919 -
920 - do_action( 'frm_after_create_entry', $entry_id, $new_values['form_id'], compact( 'is_child' ) );
921 - do_action( 'frm_after_create_entry_' . $new_values['form_id'], $entry_id, compact( 'is_child' ) );
922 - }
923 -
924 - /**
925 - * Actions to perform immediately after an entry is inserted in the frm_items database
926 - *
927 - * @since 2.0.16
928 - *
929 - * @param array $values
930 - * @param array $new_values
931 - * @param int $entry_id
932 - */
933 - private static function after_insert_entry_in_database( $values, $new_values, $entry_id ) {
934 -
935 - self::add_new_entry_to_frm_vars( $entry_id );
936 -
937 - self::maybe_add_entry_metas( $values, $entry_id );
938 -
939 - self::clear_cache();
940 -
941 - self::after_entry_created_actions( $entry_id, $values, $new_values );
942 - }
943 -
944 - /**
945 - * Perform some actions right before updating an entry
946 - *
947 - * @since 2.0.16
948 - *
949 - * @param int $id
950 - * @param array $values
951 - * @param string $update_type
952 - *
953 - * @return boolean $update
954 - */
955 - private static function before_update_entry( $id, &$values, $update_type ) {
956 - $update = true;
957 -
958 - global $frm_vars;
959 -
960 - if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, (array) $frm_vars['saved_entries'] ) ) {
961 - $update = false;
962 - }
963 -
964 - if ( $update && $update_type != 'xml' ) {
965 - $values = apply_filters( 'frm_pre_update_entry', $values, $id );
966 - }
967 -
968 - return $update;
969 - }
970 -
971 - /**
972 - * Package the entry data for updating
973 - *
974 - * @since 2.0.16
975 - *
976 - * @param int $id
977 - * @param array $values
978 - *
979 - * @return array $new_values
980 - */
981 - private static function package_entry_to_update( $id, $values ) {
982 - global $wpdb;
983 -
984 - $new_values = array(
985 - 'name' => self::get_new_entry_name( $values ),
986 - 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
987 - 'is_draft' => self::get_is_draft_value( $values ),
988 - 'updated_at' => current_time( 'mysql', 1 ),
989 - 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(),
990 - );
991 -
992 - if ( isset( $values['post_id'] ) ) {
993 - $new_values['post_id'] = (int) $values['post_id'];
994 - }
995 -
996 - if ( isset( $values['item_key'] ) ) {
997 - $new_values['item_key'] = FrmAppHelper::get_unique_key( $values['item_key'], $wpdb->prefix . 'frm_items', 'item_key', $id );
998 - }
999 -
1000 - if ( isset( $values['parent_item_id'] ) ) {
1001 - $new_values['parent_item_id'] = (int) $values['parent_item_id'];
1002 - }
1003 -
1004 - if ( isset( $values['frm_user_id'] ) && is_numeric( $values['frm_user_id'] ) ) {
1005 - $new_values['user_id'] = $values['frm_user_id'];
1006 - }
1007 -
1008 - $new_values = apply_filters( 'frm_update_entry', $new_values, $id );
1009 -
1010 - return $new_values;
1011 - }
1012 -
1013 - /**
1014 - * Perform some actions right after updating an entry
1015 - *
1016 - * @since 2.0.16
1017 - *
1018 - * @param boolean|int $query_results
1019 - * @param int $id
1020 - * @param array $values
1021 - * @param array $new_values
1022 - */
1023 - private static function after_update_entry( $query_results, $id, $values, $new_values ) {
1024 - if ( $query_results ) {
1025 - self::clear_cache();
1026 - }
1027 -
1028 - global $frm_vars;
1029 - if ( ! isset( $frm_vars['saved_entries'] ) ) {
1030 - $frm_vars['saved_entries'] = array();
1031 - }
1032 -
1033 - $frm_vars['saved_entries'][] = (int) $id;
1034 -
1035 - if ( isset( $values['item_meta'] ) ) {
1036 - FrmEntryMeta::update_entry_metas( $id, $values['item_meta'] );
1037 - }
1038 -
1039 - do_action( 'frm_after_update_entry', $id, $new_values['form_id'] );
1040 - do_action( 'frm_after_update_entry_' . $new_values['form_id'], $id );
1041 - }
1042 -
1043 - /**
1044 - * Create entry from an XML import
1045 - * Certain actions aren't necessary when importing (like saving sub entries, checking for duplicates, etc.)
1046 - *
1047 - * @since 2.0.16
1048 - *
1049 - * @param array $values
1050 - *
1051 - * @return int | boolean $entry_id
1052 - */
1053 - public static function create_entry_from_xml( $values ) {
1054 - $entry_id = self::create_entry( $values, 'xml' );
1055 -
1056 - return $entry_id;
1057 - }
1058 -
1059 - /**
1060 - * Update entry from an XML import
1061 - * Certain actions aren't necessary when importing (like saving sub entries and modifying other vals)
1062 - *
1063 - * @since 2.0.16
1064 - *
1065 - * @param int $id
1066 - * @param array $values
1067 - *
1068 - * @return int | boolean $updated
1069 - */
1070 - public static function update_entry_from_xml( $id, $values ) {
1071 - $updated = self::update_entry( $id, $values, 'xml' );
1072 -
1073 - return $updated;
1074 - }
1075 -
1076 - /**
1077 - * @param string $key
1078 - *
1079 - * @return int entry_id
1080 - */
1081 - public static function get_id_by_key( $key ) {
1082 - $entry_id = FrmDb::get_var( 'frm_items', array( 'item_key' => sanitize_title( $key ) ) );
1083 -
1084 - return (int) $entry_id;
1085 - }
504 + if ( is_callable('Akismet::http_post') ) {
505 + $response = Akismet::http_post($query_string, 'comment-check');
506 + } else {
507 + global $akismet_api_host, $akismet_api_port;
508 + $response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
509 + }
510 +
511 + return ( is_array($response) and $response[1] == 'true' ) ? true : false;
512 + }
513 +
1086 514 }