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/FrmDb.php +297 -713 6.4 → 1.07.11 View file →
@@ -1,744 +1,328 @@
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 FrmDb {
7 - public $fields;
8 - public $forms;
9 - public $entries;
10 - public $entry_metas;
4 +if(class_exists('FrmDb'))
5 + return;
11 6
12 - public function __construct() {
13 - if ( ! defined( 'ABSPATH' ) ) {
14 - die( 'You are not allowed to call this page directly.' );
15 - }
7 +class FrmDb{
8 +
9 + function __construct(){
10 + global $wpdb;
11 + $this->fields = $wpdb->prefix . "frm_fields";
12 + $this->forms = $wpdb->prefix . "frm_forms";
13 + $this->entries = $wpdb->prefix . "frm_items";
14 + $this->entry_metas = $wpdb->prefix . "frm_item_metas";
15 + }
16 +
17 + function upgrade($old_db_version=false){
18 + global $wpdb;
19 + //$frm_db_version is the version of the database we're moving to
20 + $frm_db_version = FrmAppHelper::$db_version;
21 + $old_db_version = (float)$old_db_version;
22 + if(!$old_db_version)
23 + $old_db_version = get_option('frm_db_version');
16 24
17 - _deprecated_function( __METHOD__, '2.05.06', 'FrmMigrate' );
18 - global $wpdb;
19 - $this->fields = $wpdb->prefix . 'frm_fields';
20 - $this->forms = $wpdb->prefix . 'frm_forms';
21 - $this->entries = $wpdb->prefix . 'frm_items';
22 - $this->entry_metas = $wpdb->prefix . 'frm_item_metas';
23 - }
25 + if ($frm_db_version != $old_db_version){
26 + require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
27 +
28 + $charset_collate = '';
29 + if( $wpdb->has_cap( 'collation' ) ){
30 + if( !empty($wpdb->charset) )
31 + $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
32 + if( !empty($wpdb->collate) )
33 + $charset_collate .= " COLLATE $wpdb->collate";
34 + }
24 35
25 - /**
26 - * Change array into format $wpdb->prepare can use
27 - *
28 - * @param array $args
29 - * @param string $starts_with
30 - */
31 - public static function get_where_clause_and_values( &$args, $starts_with = ' WHERE ' ) {
32 - if ( empty( $args ) ) {
33 - // add an arg to prevent prepare from failing
34 - $args = array(
35 - 'where' => $starts_with . '1=%d',
36 - 'values' => array( 1 ),
37 - );
36 + /* Create/Upgrade Fields Table */
37 + $sql = "CREATE TABLE {$this->fields} (
38 + id int(11) NOT NULL auto_increment,
39 + field_key varchar(255) default NULL,
40 + name text default NULL,
41 + description text default NULL,
42 + type text default NULL,
43 + default_value longtext default NULL,
44 + options longtext default NULL,
45 + field_order int(11) default 0,
46 + required int(1) default NULL,
47 + field_options longtext default NULL,
48 + form_id int(11) default NULL,
49 + created_at datetime NOT NULL,
50 + PRIMARY KEY (id),
51 + KEY form_id (form_id),
52 + UNIQUE KEY field_key (field_key)
53 + ) {$charset_collate};";
38 54
39 - return;
40 - }
55 + dbDelta($sql);
56 +
57 + /* Create/Upgrade Forms Table */
58 + $sql = "CREATE TABLE {$this->forms} (
59 + id int(11) NOT NULL auto_increment,
60 + form_key varchar(255) default NULL,
61 + name varchar(255) default NULL,
62 + description text default NULL,
63 + parent_form_id int(11) default NULL,
64 + logged_in tinyint(1) default NULL,
65 + editable tinyint(1) default NULL,
66 + is_template tinyint(1) default 0,
67 + default_template tinyint(1) default 0,
68 + status varchar(255) default NULL,
69 + prli_link_id int(11) default NULL,
70 + options longtext default NULL,
71 + created_at datetime NOT NULL,
72 + PRIMARY KEY (id),
73 + UNIQUE KEY form_key (form_key)
74 + ) {$charset_collate};";
41 75
42 - $where = '';
43 - $values = array();
76 + dbDelta($sql);
44 77
45 - if ( is_array( $args ) ) {
46 - $base_where = $starts_with;
47 - self::parse_where_from_array( $args, $base_where, $where, $values );
48 - }
78 + /* Create/Upgrade Items Table */
79 + $sql = "CREATE TABLE {$this->entries} (
80 + id int(11) NOT NULL auto_increment,
81 + item_key varchar(255) default NULL,
82 + name varchar(255) default NULL,
83 + description text default NULL,
84 + ip text default NULL,
85 + form_id int(11) default NULL,
86 + post_id int(11) default NULL,
87 + user_id int(11) default NULL,
88 + parent_item_id int(11) default NULL,
89 + is_draft tinyint(1) default 0,
90 + updated_by int(11) default NULL,
91 + created_at datetime NOT NULL,
92 + updated_at datetime NOT NULL,
93 + PRIMARY KEY (id),
94 + KEY form_id (form_id),
95 + KEY post_id (post_id),
96 + KEY user_id (user_id),
97 + KEY parent_item_id (parent_item_id),
98 + UNIQUE KEY item_key (item_key)
99 + ) {$charset_collate};";
49 100
50 - $args = compact( 'where', 'values' );
51 - }
101 + dbDelta($sql);
52 102
53 - /**
54 - * @param array $args
55 - * @param string $base_where
56 - * @param string $where
57 - * @param array $values
58 - */
59 - public static function parse_where_from_array( $args, $base_where, &$where, &$values ) {
60 - $condition = ' AND';
61 - if ( isset( $args['or'] ) ) {
62 - $condition = ' OR';
63 - unset( $args['or'] );
64 - }
103 + /* Create/Upgrade Meta Table */
104 + $sql = "CREATE TABLE {$this->entry_metas} (
105 + id int(11) NOT NULL auto_increment,
106 + meta_value longtext default NULL,
107 + field_id int(11) NOT NULL,
108 + item_id int(11) NOT NULL,
109 + created_at datetime NOT NULL,
110 + PRIMARY KEY (id),
111 + KEY field_id (field_id),
112 + KEY item_id (item_id)
113 + ) {$charset_collate};";
65 114
66 - foreach ( $args as $key => $value ) {
67 - $where .= empty( $where ) ? $base_where : $condition;
68 - $array_inc_null = ( ! is_numeric( $key ) && is_array( $value ) && in_array( null, $value ) );
69 - if ( is_numeric( $key ) || $array_inc_null ) {
70 - $where .= ' ( ';
71 - $nested_where = '';
72 - if ( $array_inc_null ) {
73 - foreach ( $value as $val ) {
74 - $parse_where = array(
75 - $key => $val,
76 - 'or' => 1,
77 - );
78 - self::parse_where_from_array( $parse_where, '', $nested_where, $values );
79 - }
80 - } else {
81 - self::parse_where_from_array( $value, '', $nested_where, $values );
82 - }
83 - $where .= $nested_where;
84 - $where .= ' ) ';
85 - } else {
86 - self::interpret_array_to_sql( $key, $value, $where, $values );
87 - }
88 - }
89 - }
115 + dbDelta($sql);
116 +
117 + /**** MIGRATE DATA ****/
118 + if ($frm_db_version >= 1.03 and $old_db_version < 1.03){
119 + global $frm_entry;
120 + $all_entries = $frm_entry->getAll();
121 + foreach($all_entries as $ent){
122 + $opts = maybe_unserialize($ent->description);
123 + if(is_array($opts) and in_array($opts['ip']))
124 + $wpdb->update( $this->entries, array('ip' => $opts['ip']), array( 'id' => $ent->id ) );
125 + }
126 + }
127 +
128 + if($frm_db_version >= 4 and $old_db_version < 4){
129 + global $frm_entry_meta;
130 + $user_ids = $frm_entry_meta->getAll("fi.type='user_id'");
131 + foreach($user_ids as $user_id)
132 + $wpdb->update( $this->entries, array('user_id' => $user_id->meta_value), array('id' => $user_id->item_id) );
133 + }
134 +
135 + if($frm_db_version >= 6 and $old_db_version < 6){
136 + $fields = $wpdb->get_results("SELECT id, field_options FROM $this->fields WHERE type not in ('hidden', 'user_id', 'break', 'divider', 'html', 'captcha', 'form')");
137 + $default_html = <<<DEFAULT_HTML
138 +<div id="frm_field_[id]_container" class="form-field [required_class] [error_class]">
139 + <label class="frm_pos_[label_position]">[field_name]
140 + <span class="frm_required">[required_label]</span>
141 + </label>
142 + [input]
143 + [if description]<div class="frm_description">[description]</div>[/if description]
144 +</div>
145 +DEFAULT_HTML;
146 + $old_default_html = <<<DEFAULT_HTML
147 +<div id="frm_field_[id]_container" class="form-field [required_class] [error_class]">
148 + <label class="frm_pos_[label_position]">[field_name]
149 + <span class="frm_required">[required_label]</span>
150 + </label>
151 + [input]
152 + [if description]<p class="frm_description">[description]</p>[/if description]
153 +</div>
154 +DEFAULT_HTML;
155 + $new_default_html = FrmFieldsHelper::get_default_html('text');
156 + foreach($fields as $field){
157 + $field->field_options = maybe_unserialize($field->field_options);
158 + if ( !isset($field->field_options['custom_html']) || empty($field->field_options['custom_html']) || $field->field_options['custom_html'] == $default_html || $field->field_options['custom_html'] == $old_default_html ) {
159 + $field->field_options['custom_html'] = $new_default_html;
160 + $wpdb->update($this->fields, array('field_options' => maybe_serialize($field->field_options)), array( 'id' => $field->id ));
161 + }
162 + unset($field);
163 + }
164 + unset($default_html);
165 + unset($old_default_html);
166 + unset($fields);
167 + }
168 +
169 + if($frm_db_version >= 11 and $old_db_version < 11){
170 + $forms = $wpdb->get_results("SELECT id, options FROM $this->forms");
171 + $sending = __('Sending', 'formidable');
172 + $img = FrmAppHelper::plugin_url() .'/images/ajax_loader.gif';
173 + $old_default_html = <<<DEFAULT_HTML
174 +<div class="frm_submit">
175 +[if back_button]<input type="submit" value="[back_label]" name="frm_prev_page" formnovalidate="formnovalidate" [back_hook] />[/if back_button]
176 +<input type="submit" value="[button_label]" [button_action] />
177 +<img class="frm_ajax_loading" src="$img" alt="$sending" style="visibility:hidden;" />
178 +</div>
179 +DEFAULT_HTML;
180 + unset($sending);
181 + unset($img);
182 +
183 + $new_default_html = FrmFormsHelper::get_default_html('submit');
184 + $draft_link = FrmFormsHelper::get_draft_link();
185 + foreach($forms as $form){
186 + $form->options = maybe_unserialize($form->options);
187 + if(!isset($form->options['submit_html']) or empty($form->options['submit_html']))
188 + continue;
189 +
190 + if ( $form->options['submit_html'] != $new_default_html && $form->options['submit_html'] == $old_default_html ) {
191 + $form->options['submit_html'] = $new_default_html;
192 + $wpdb->update($this->forms, array('options' => serialize($form->options)), array( 'id' => $form->id ));
193 + }else if(!strpos($form->options['submit_html'], 'save_draft')){
194 + $form->options['submit_html'] = preg_replace('~\<\/div\>(?!.*\<\/div\>)~', $draft_link ."\r\n</div>", $form->options['submit_html']);
195 + $wpdb->update($this->forms, array('options' => serialize($form->options)), array( 'id' => $form->id ));
196 + }
197 + unset($form);
198 + }
199 + unset($forms);
200 + }
201 +
202 +
90 203
91 - /**
92 - * @param string $key
93 - * @param string|array $value
94 - * @param string $where
95 - * @param array $values
96 - */
97 - private static function interpret_array_to_sql( $key, $value, &$where, &$values ) {
98 - $key = trim( $key );
204 + /**** ADD/UPDATE DEFAULT TEMPLATES ****/
205 + if ( class_exists('FrmXMLController') ) {
206 + FrmXMLController::add_default_templates();
207 + }
99 208
100 - if ( strpos( $key, 'created_at' ) !== false || strpos( $key, 'updated_at' ) !== false ) {
101 - $k = explode( ' ', $key );
102 - $where .= ' DATE_FORMAT(' . reset( $k ) . ', %s) ' . str_replace( reset( $k ), '', $key );
103 - $values[] = '%Y-%m-%d %H:%i:%s';
104 - } else {
105 - $where .= ' ' . $key;
106 - }
209 +
210 + /***** SAVE DB VERSION *****/
211 + update_option('frm_db_version', $frm_db_version);
212 + }
107 213
108 - $lowercase_key = explode( ' ', strtolower( $key ) );
109 - $lowercase_key = end( $lowercase_key );
214 + do_action('frm_after_install');
215 + }
216 +
217 + function get_count($table, $args=array()){
218 + global $wpdb;
219 + extract(FrmDb::get_where_clause_and_values( $args ));
110 220
111 - if ( is_array( $value ) ) {
112 - // translate array of values to "in"
113 - if ( strpos( $lowercase_key, 'like' ) !== false ) {
114 - $where = preg_replace( '/' . $key . '$/', '', $where );
115 - $where .= '(';
116 - $start = true;
117 - foreach ( $value as $v ) {
118 - if ( ! $start ) {
119 - $where .= ' OR ';
120 - }
121 - $start = false;
122 - $where .= $key . ' %s';
123 - $values[] = '%' . self::esc_like( $v ) . '%';
124 - }
125 - $where .= ')';
126 - } elseif ( ! empty( $value ) ) {
127 - $where .= ' in (' . self::prepare_array_values( $value, '%s' ) . ')';
128 - $values = array_merge( $values, $value );
129 - }
130 - } elseif ( strpos( $lowercase_key, 'like' ) !== false ) {
131 - /**
132 - * Allow string to start or end with the value
133 - * If the key is like% then skip the first % for starts with
134 - * If the key is %like then skip the last % for ends with
135 - */
136 - $start = '%';
137 - $end = '%';
138 - if ( $lowercase_key == 'like%' ) {
139 - $start = '';
140 - $where = rtrim( $where, '%' );
141 - } elseif ( $lowercase_key == '%like' ) {
142 - $end = '';
143 - $where = rtrim( rtrim( $where, '%like' ), '%LIKE' );
144 - $where .= 'like';
145 - }
221 + $query = "SELECT COUNT(*) FROM {$table}{$where}";
222 + $query = $wpdb->prepare($query, $values);
223 + return $wpdb->get_var($query);
224 + }
146 225
147 - $where .= ' %s';
148 - $values[] = $start . self::esc_like( $value ) . $end;
226 + function get_where_clause_and_values( $args ){
227 + $where = '';
228 + $values = array();
229 + if(is_array($args)){
230 + foreach($args as $key => $value){
231 + $where .= (!empty($where)) ? ' AND' : ' WHERE';
232 + $where .= " {$key}=";
233 + $where .= is_numeric($value) ? (strpos($value, ".") !== false ? '%f' : '%d') : '%s';
234 +
235 + $values[] = $value;
236 + }
237 + }
149 238
150 - } elseif ( $value === null ) {
151 - $where .= ' IS NULL';
152 - } else {
153 - // allow a - to prevent = from being added
154 - if ( substr( $key, - 1 ) == '-' ) {
155 - $where = rtrim( $where, '-' );
156 - } else {
157 - $where .= '=';
158 - }
239 + return compact('where', 'values');
240 + }
241 +
242 + function get_var($table, $args=array(), $field='id', $order_by=''){
243 + global $wpdb;
159 244
160 - self::add_query_placeholder( $key, $value, $where );
245 + extract(FrmDb::get_where_clause_and_values( $args ));
246 + if(!empty($order_by))
247 + $order_by = " ORDER BY {$order_by}";
161 248
162 - $values[] = $value;
163 - }
164 - }
249 + $query = $wpdb->prepare("SELECT {$field} FROM {$table}{$where}{$order_by} LIMIT 1", $values);
250 + return $wpdb->get_var($query);
251 + }
252 +
253 + function get_col($table, $args=array(), $field='id', $order_by=''){
254 + global $wpdb;
165 255
166 - /**
167 - * Add %d, or %s to query
168 - *
169 - * @since 2.02.05
170 - *
171 - * @param string $key
172 - * @param int|string $value
173 - * @param string $where
174 - */
175 - private static function add_query_placeholder( $key, $value, &$where ) {
176 - if ( is_numeric( $value ) && ( strpos( $key, 'meta_value' ) === false || strpos( $key, '+0' ) !== false ) ) {
177 - $value = $value + 0; // switch string to number
178 - $where .= is_float( $value ) ? '%f' : '%d';
179 - } else {
180 - $where .= '%s';
181 - }
182 - }
256 + extract(FrmDb::get_where_clause_and_values( $args ));
257 + if(!empty($order_by))
258 + $order_by = " ORDER BY {$order_by}";
183 259
184 - /**
185 - * @param string $table
186 - * @param array $where
187 - * @param array $args
188 - *
189 - * @return int
190 - */
191 - public static function get_count( $table, $where = array(), $args = array() ) {
192 - $count = self::get_var( $table, $where, 'COUNT(*)', $args );
260 + $query = $wpdb->prepare("SELECT {$field} FROM {$table}{$where}{$order_by}", $values);
261 + return $wpdb->get_col($query);
262 + }
193 263
194 - return (int) $count;
195 - }
264 + function get_one_record($table, $args=array(), $fields='*', $order_by=''){
265 + global $wpdb;
196 266
197 - /**
198 - * @param string $table
199 - * @param array $where
200 - * @param string $field
201 - * @param array $args
202 - * @param string $limit
203 - * @param string $type
204 - *
205 - * @return array|null|string|object
206 - */
207 - public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) {
208 - $group = '';
209 - self::get_group_and_table_name( $table, $group );
210 - self::convert_options_to_array( $args, '', $limit );
211 - if ( $type === 'var' && ! isset( $args['limit'] ) ) {
212 - $args['limit'] = 1;
213 - }
267 + extract(FrmDb::get_where_clause_and_values( $args ));
268 +
269 + if(!empty($order_by))
270 + $order_by = " ORDER BY {$order_by}";
214 271
215 - $query = self::generate_query_string_from_pieces( $field, $table, $where, $args );
272 + $query = "SELECT {$fields} FROM {$table}{$where} {$order_by} LIMIT 1";
273 + $query = $wpdb->prepare($query, $values);
274 + return $wpdb->get_row($query);
275 + }
216 276
217 - $cache_key = self::generate_cache_key( $where, $args, $field, $type );
218 - $results = self::check_cache( $cache_key, $group, $query, 'get_' . $type );
277 + function get_records($table, $args=array(), $order_by='', $limit='', $fields='*'){
278 + global $wpdb;
219 279
220 - return $results;
221 - }
280 + extract(FrmDb::get_where_clause_and_values( $args ));
222 281
223 - /**
224 - * Generate a cache key from the where query, field, type, and other arguments
225 - *
226 - * @since 2.03.07
227 - *
228 - * @param array $where
229 - * @param array $args
230 - * @param string $field
231 - * @param string $type
232 - *
233 - * @return string
234 - */
235 - public static function generate_cache_key( $where, $args, $field, $type ) {
236 - $cache_key = '';
237 - $where = FrmAppHelper::array_flatten( $where );
238 - foreach ( $where as $key => $value ) {
239 - $cache_key .= $key . '_' . $value;
240 - }
241 - $cache_key .= implode( '_', $args ) . $field . '_' . $type;
242 - $cache_key = str_replace( array( ' ', ',' ), '_', $cache_key );
282 + if ( !empty($order_by) && strpos($order_by, ' ORDER BY ') === false ) {
283 + $order_by = " ORDER BY {$order_by}";
284 + }
243 285
244 - return $cache_key;
245 - }
286 + if ( !empty($limit) && strpos($order_by, ' LIMIT ') === false ) {
287 + $limit = " LIMIT {$limit}";
288 + }
246 289
247 - /**
248 - * @param string $table
249 - * @param array $where
250 - * @param string $field
251 - * @param array $args
252 - * @param string $limit
253 - *
254 - * @return mixed
255 - */
256 - public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) {
257 - return self::get_var( $table, $where, $field, $args, $limit, 'col' );
258 - }
259 -
260 - /**
261 - * @since 2.0
262 - *
263 - * @param string $table
264 - * @param array $where
265 - * @param string $fields
266 - * @param array $args
267 - *
268 - * @return mixed
269 - */
270 - public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) {
271 - $args['limit'] = 1;
272 -
273 - return self::get_var( $table, $where, $fields, $args, '', 'row' );
274 - }
275 -
276 - /**
277 - * Prepare a key/value array before DB call
278 - *
279 - * @since 2.0
280 - *
281 - * @param string $table
282 - * @param array $where
283 - * @param string $fields
284 - * @param array $args
285 - *
286 - * @return mixed
287 - */
288 - public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) {
289 - return self::get_var( $table, $where, $fields, $args, '', 'results' );
290 - }
291 -
292 - /**
293 - * Check for like, not like, in, not in, =, !=, >, <, <=, >=
294 - * Return a value to append to the where array key
295 - *
296 - * @param string $where_is
297 - *
298 - * @return string
299 - */
300 - public static function append_where_is( $where_is ) {
301 - $switch_to = array(
302 - '=' => '',
303 - '!=' => '!',
304 - '<=' => '<',
305 - '>=' => '>',
306 - 'like' => 'like',
307 - 'not like' => 'not like',
308 - 'in' => '',
309 - 'not in' => 'not',
310 - 'like%' => 'like%',
311 - '%like' => '%like',
312 - );
313 -
314 - $where_is = strtolower( $where_is );
315 - if ( isset( $switch_to[ $where_is ] ) ) {
316 - return ' ' . $switch_to[ $where_is ];
290 + $query = "SELECT {$fields} FROM {$table}{$where}{$order_by}{$limit}";
291 + $query = $wpdb->prepare($query, $values);
292 + return $wpdb->get_results($query);
293 + }
294 +
295 + function uninstall(){
296 + if ( !current_user_can('administrator') ) {
297 + global $frm_settings;
298 + wp_die($frm_settings->admin_permission);
299 + }
300 +
301 + global $wpdb, $wp_roles;
302 +
303 + $wpdb->query('DROP TABLE IF EXISTS '. $this->fields);
304 + $wpdb->query('DROP TABLE IF EXISTS '. $this->forms);
305 + $wpdb->query('DROP TABLE IF EXISTS '. $this->entries);
306 + $wpdb->query('DROP TABLE IF EXISTS '. $this->entry_metas);
307 +
308 + delete_option('frm_options');
309 + delete_option('frm_db_version');
310 +
311 + //delete roles
312 + $frm_roles = FrmAppHelper::frm_capabilities();
313 + $roles = get_editable_roles();
314 + foreach($frm_roles as $frm_role => $frm_role_description){
315 + foreach ($roles as $role => $details){
316 + $wp_roles->remove_cap( $role, $frm_role );
317 + unset($role);
318 + unset($details);
319 + }
320 + unset($frm_role);
321 + unset($frm_role_description);
317 322 }
318 -
319 - // > and < need a little more work since we don't want them switched to >= and <=
320 - if ( $where_is == '>' || $where_is == '<' ) {
321 - return ' ' . $where_is . '-'; // the - indicates that the = should not be added later
322 - }
323 -
324 - // fallback to = if the query is none of these
325 - return '';
326 - }
327 -
328 - /**
329 - * Get 'frm_forms' from wp_frm_forms or a longer table param that includes a join
330 - * Also add the wpdb->prefix to the table if it's missing
331 - *
332 - * @param string $table
333 - * @param string $group
334 - */
335 - private static function get_group_and_table_name( &$table, &$group ) {
336 - global $wpdb, $wpmuBaseTablePrefix;
337 -
338 - $table_parts = explode( ' ', $table );
339 - $group = reset( $table_parts );
340 - self::maybe_remove_prefix( $wpdb->prefix, $group );
341 -
342 - $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix : $wpdb->base_prefix;
343 - self::maybe_remove_prefix( $prefix, $group );
344 -
345 - if ( $group == $table ) {
346 - $table = $wpdb->prefix . $table;
347 - }
348 -
349 - // switch to singular group name
350 - $group = rtrim( $group, 's' );
351 - }
352 -
353 - /**
354 - * Only remove the db prefix when at the beginning.
355 - *
356 - * @since 4.04.02
357 - */
358 - private static function maybe_remove_prefix( $prefix, &$name ) {
359 - if ( substr( $name, 0, strlen( $prefix ) ) === $prefix ) {
360 - $name = substr( $name, strlen( $prefix ) );
361 - }
362 - }
363 -
364 - private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) {
365 - if ( ! is_array( $args ) ) {
366 - $args = array( 'order_by' => $args );
367 - }
368 -
369 - if ( ! empty( $order_by ) ) {
370 - $args['order_by'] = $order_by;
371 - }
372 -
373 - if ( ! empty( $limit ) ) {
374 - $args['limit'] = $limit;
375 - }
376 -
377 - $temp_args = $args;
378 - foreach ( $temp_args as $k => $v ) {
379 - if ( $v == '' ) {
380 - unset( $args[ $k ] );
381 - continue;
382 - }
383 -
384 - $db_name = strtoupper( str_replace( '_', ' ', $k ) );
385 - if ( strpos( $v, $db_name ) === false ) {
386 - $args[ $k ] = $db_name . ' ' . $v;
387 - }
388 - }
389 -
390 - // Make sure LIMIT is the last argument
391 - if ( isset( $args['order_by'] ) && isset( $args['limit'] ) ) {
392 - $temp_limit = $args['limit'];
393 - unset( $args['limit'] );
394 - $args['limit'] = $temp_limit;
395 - }
396 - }
397 -
398 - /**
399 - * Get the associative array results for the given columns, table, and where query
400 - *
401 - * @since 2.02.05
402 - *
403 - * @param string $columns
404 - * @param string $table
405 - * @param array $where
406 - *
407 - * @return mixed
408 - */
409 - public static function get_associative_array_results( $columns, $table, $where ) {
410 - $group = '';
411 - self::get_group_and_table_name( $table, $group );
412 -
413 - $query = self::generate_query_string_from_pieces( $columns, $table, $where );
414 -
415 - $cache_key = str_replace( array( ' ', ',' ), '_', trim( implode( '_', FrmAppHelper::array_flatten( $where ) ) . $columns . '_results_ARRAY_A', ' WHERE' ) );
416 - $results = self::check_cache( $cache_key, $group, $query, 'get_associative_results' );
417 -
418 - return $results;
419 - }
420 -
421 - /**
422 - * Combine the pieces of a query to form a full, prepared query
423 - *
424 - * @since 2.02.05
425 - *
426 - * @param string $columns
427 - * @param string $table
428 - * @param mixed $where
429 - * @param array $args
430 - *
431 - * @return string
432 - */
433 - private static function generate_query_string_from_pieces( $columns, $table, $where, $args = array() ) {
434 - $query = 'SELECT ' . $columns . ' FROM ' . $table;
435 -
436 - self::esc_query_args( $args );
437 -
438 - if ( is_array( $where ) || empty( $where ) ) {
439 - self::get_where_clause_and_values( $where );
440 - global $wpdb;
441 - $query = $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
442 - } else {
443 - /**
444 - * Allow the $where to be prepared before we recieve it here.
445 - * This is a fallback for reverse compatibility, but is not recommended
446 - */
447 - _deprecated_argument( 'where', '2.0', esc_html__( 'Use the query in an array format so it can be properly prepared.', 'formidable' ) );
448 - $query .= $where . ' ' . implode( ' ', $args );
449 - }
450 -
451 - return $query;
452 - }
453 -
454 - /**
455 - * @since 2.05.07
456 - */
457 - private static function esc_query_args( &$args ) {
458 - foreach ( $args as $param => $value ) {
459 - if ( $param == 'order_by' ) {
460 - $args[ $param ] = self::esc_order( $value );
461 - } elseif ( $param == 'limit' ) {
462 - $args[ $param ] = self::esc_limit( $value );
463 - }
464 -
465 - if ( $args[ $param ] == '' ) {
466 - unset( $args[ $param ] );
467 - }
468 - }
469 - }
470 -
471 - /**
472 - * Added for < WP 4.0 compatability
473 - *
474 - * @since 2.05.06
475 - *
476 - * @param string $term The value to escape
477 - *
478 - * @return string The escaped value
479 - */
480 - public static function esc_like( $term ) {
481 - global $wpdb;
482 -
483 - return $wpdb->esc_like( $term );
484 - }
485 -
486 - /**
487 - * @since 2.05.06
488 - *
489 - * @param string $order_query
490 - */
491 - public static function esc_order( $order_query ) {
492 - if ( empty( $order_query ) ) {
493 - return '';
494 - }
495 -
496 - // remove ORDER BY before santizing
497 - $order_query = strtolower( $order_query );
498 - if ( strpos( $order_query, 'order by' ) !== false ) {
499 - $order_query = str_replace( 'order by', '', $order_query );
500 - }
501 -
502 - $order_query = explode( ' ', trim( $order_query ) );
503 -
504 - $order = trim( reset( $order_query ) );
505 - $safe_order = array( 'count(*)' );
506 - if ( ! in_array( strtolower( $order ), $safe_order ) ) {
507 - $order = preg_replace( '/[^a-zA-Z0-9\-\_\.\+]/', '', $order );
508 - }
509 -
510 - $order_by = '';
511 - if ( count( $order_query ) > 1 ) {
512 - $order_by = end( $order_query );
513 - self::esc_order_by( $order_by );
514 - }
515 -
516 - return ' ORDER BY ' . $order . ' ' . $order_by;
517 - }
518 -
519 - /**
520 - * Make sure this is ordering by either ASC or DESC
521 - *
522 - * @since 2.05.06
523 - */
524 - public static function esc_order_by( &$order_by ) {
525 - $sort_options = array( 'asc', 'desc' );
526 - if ( ! in_array( strtolower( $order_by ), $sort_options ) ) {
527 - $order_by = 'asc';
528 - }
529 - }
530 -
531 - /**
532 - * @param string $limit
533 - *
534 - * @since 2.05.06
535 - */
536 - public static function esc_limit( $limit ) {
537 - if ( empty( $limit ) ) {
538 - return '';
539 - }
540 -
541 - $limit = trim( str_replace( 'limit ', '', strtolower( $limit ) ) );
542 - if ( is_numeric( $limit ) ) {
543 - return ' LIMIT ' . $limit;
544 - }
545 -
546 - $limit = explode( ',', trim( $limit ) );
547 - foreach ( $limit as $k => $l ) {
548 - if ( is_numeric( $l ) ) {
549 - $limit[ $k ] = $l;
550 - }
551 - }
552 -
553 - $limit = implode( ',', $limit );
554 -
555 - return ' LIMIT ' . $limit;
556 - }
557 -
558 - /**
559 - * Get an array of values ready to go through $wpdb->prepare
560 - *
561 - * @since 2.05.06
562 - */
563 - public static function prepare_array_values( $array, $type = '%s' ) {
564 - $placeholders = array_fill( 0, count( $array ), $type );
565 -
566 - return implode( ', ', $placeholders );
567 - }
568 -
569 - /**
570 - * @since 2.05.06
571 - */
572 - public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) {
573 - if ( empty( $where ) ) {
574 - $where = '';
575 - } else {
576 - if ( is_array( $where ) ) {
577 - global $wpdb;
578 - self::get_where_clause_and_values( $where, $starts_with );
579 - $where = $wpdb->prepare( $where['where'], $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
580 - } else {
581 - $where = $starts_with . $where;
582 - }
583 - }
584 -
585 - /**
586 - * Allows modifying where clause when using FrmDb::prepend_and_or_where() method.
587 - *
588 - * @since 5.0.16
589 - *
590 - * @param string $where Where string.
591 - * @param string $starts_with The start of where string.
592 - */
593 - return apply_filters( 'frm_prepend_and_or_where', $where, $starts_with );
594 - }
595 -
596 - /**
597 - * Prepare and save settings in styles and actions
598 - *
599 - * @param array $settings
600 - * @param string $group
601 - *
602 - * @since 2.05.06
603 - */
604 - public static function save_settings( $settings, $group ) {
605 - $settings = (array) $settings;
606 - $settings['post_content'] = FrmAppHelper::prepare_and_encode( $settings['post_content'] );
607 -
608 - if ( empty( $settings['ID'] ) ) {
609 - unset( $settings['ID'] );
610 - }
611 -
612 - // delete all caches for this group
613 - self::cache_delete_group( $group );
614 -
615 - return self::save_json_post( $settings );
616 - }
617 -
618 - /**
619 - * Since actions are JSON encoded, we don't want any filters messing with it.
620 - * Remove the filters and then add them back in case any posts or views are
621 - * also being imported.
622 - *
623 - * Used when saving form actions and styles
624 - *
625 - * @since 2.05.06
626 - *
627 - * @param array $settings
628 - * @return int|WP_Error
629 - */
630 - public static function save_json_post( $settings ) {
631 - global $wp_filter;
632 - if ( isset( $wp_filter['content_save_pre'] ) ) {
633 - $filters = $wp_filter['content_save_pre'];
634 - }
635 -
636 - // Remove the balanceTags filter in case WordPress is trying to validate the XHTML
637 - remove_all_filters( 'content_save_pre' );
638 -
639 - $post = wp_insert_post( $settings );
640 -
641 - // add the content filters back for views or posts
642 - if ( isset( $filters ) ) {
643 - $wp_filter['content_save_pre'] = $filters;
644 - }
645 -
646 - return $post;
647 - }
648 -
649 - /**
650 - * Check cache before fetching values and saving to cache
651 - *
652 - * @since 2.05.06
653 - *
654 - * @param string $cache_key The unique name for this cache
655 - * @param string $group The name of the cache group
656 - * @param string $query If blank, don't run a db call
657 - * @param string $type The wpdb function to use with this query
658 - *
659 - * @return mixed $results The cache or query results
660 - */
661 - public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) {
662 - $results = wp_cache_get( $cache_key, $group );
663 - if ( ! FrmAppHelper::is_empty_value( $results, false ) || empty( $query ) ) {
664 - return $results;
665 - }
666 -
667 - if ( 'get_posts' == $type ) {
668 - $results = get_posts( $query );
669 - } elseif ( 'get_associative_results' == $type ) {
670 - global $wpdb;
671 - $results = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
672 - } else {
673 - global $wpdb;
674 - $results = $wpdb->{$type}( $query );
675 - }
676 -
677 - self::set_cache( $cache_key, $results, $group, $time );
678 -
679 - return $results;
680 - }
681 -
682 - /**
683 - * @since 2.05.06
684 - */
685 - public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) {
686 - if ( ! FrmAppHelper::prevent_caching() ) {
687 - self::add_key_to_group_cache( $cache_key, $group );
688 - wp_cache_set( $cache_key, $results, $group, $time );
689 - }
690 - }
691 -
692 - /**
693 - * Keep track of the keys cached in each group so they can be deleted
694 - * in Redis and Memcache
695 - *
696 - * @since 2.05.06
697 - */
698 - public static function add_key_to_group_cache( $key, $group ) {
699 - $cached = self::get_group_cached_keys( $group );
700 - $cached[ $key ] = $key;
701 - wp_cache_set( 'cached_keys', $cached, $group, 300 );
702 - }
703 -
704 - /**
705 - * @since 2.05.06
706 - */
707 - public static function get_group_cached_keys( $group ) {
708 - $cached = wp_cache_get( 'cached_keys', $group );
709 - if ( ! $cached || ! is_array( $cached ) ) {
710 - $cached = array();
711 - }
712 -
713 - return $cached;
714 - }
715 -
716 - /**
717 - * @since 2.05.06
718 - *
719 - * @param string $cache_key
720 - */
721 - public static function delete_cache_and_transient( $cache_key, $group = 'default' ) {
722 - delete_transient( $cache_key );
723 - wp_cache_delete( $cache_key, $group );
724 - }
725 -
726 - /**
727 - * Delete all caching in a single group
728 - *
729 - * @since 2.05.06
730 - *
731 - * @param string $group The name of the cache group
732 - */
733 - public static function cache_delete_group( $group ) {
734 - $cached_keys = self::get_group_cached_keys( $group );
735 -
736 - if ( ! empty( $cached_keys ) ) {
737 - foreach ( $cached_keys as $key ) {
738 - wp_cache_delete( $key, $group );
739 - }
740 -
741 - wp_cache_delete( 'cached_keys', $group );
742 - }
743 - }
323 + unset($roles);
324 + unset($frm_roles);
325 +
326 + do_action('frm_after_uninstall');
327 + }
744 328 }