| @@ -1,46 +1,42 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - die( 'You are not allowed to call this page directly.' ); | |
| 4 | -} | |
| 5 | 2 | |
| 6 | 3 | class FrmDb { |
| 7 | - public $fields; | |
| 8 | - public $forms; | |
| 9 | - public $entries; | |
| 10 | - public $entry_metas; | |
| 4 | + public $fields; | |
| 5 | + public $forms; | |
| 6 | + public $entries; | |
| 7 | + public $entry_metas; | |
| 11 | 8 | |
| 12 | - public function __construct() { | |
| 9 | + public function __construct() { | |
| 13 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 11 | die( 'You are not allowed to call this page directly.' ); |
| 15 | 12 | } |
| 16 | 13 | |
| 17 | 14 | _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 | - } | |
| 15 | + global $wpdb; | |
| 16 | + $this->fields = $wpdb->prefix . 'frm_fields'; | |
| 17 | + $this->forms = $wpdb->prefix . 'frm_forms'; | |
| 18 | + $this->entries = $wpdb->prefix . 'frm_items'; | |
| 19 | + $this->entry_metas = $wpdb->prefix . 'frm_item_metas'; | |
| 20 | + } | |
| 24 | 21 | |
| 25 | - /** | |
| 26 | - * Change array into format $wpdb->prepare can use | |
| 22 | + /** | |
| 23 | + * Change array into format $wpdb->prepare can use | |
| 27 | 24 | * |
| 28 | 25 | * @param array $args |
| 29 | 26 | * @param string $starts_with |
| 30 | - */ | |
| 31 | - public static function get_where_clause_and_values( &$args, $starts_with = ' WHERE ' ) { | |
| 27 | + */ | |
| 28 | + public static function get_where_clause_and_values( &$args, $starts_with = ' WHERE ' ) { | |
| 32 | 29 | if ( empty( $args ) ) { |
| 33 | 30 | // add an arg to prevent prepare from failing |
| 34 | 31 | $args = array( |
| 35 | - 'where' => $starts_with . '1=%d', | |
| 32 | + 'where' => $starts_with . '1=%d', | |
| 36 | 33 | 'values' => array( 1 ), |
| 37 | 34 | ); |
| 38 | - | |
| 39 | 35 | return; |
| 40 | - } | |
| 36 | + } | |
| 41 | 37 | |
| 42 | - $where = ''; | |
| 38 | + $where = ''; | |
| 43 | 39 | $values = array(); |
| 44 | 40 | |
| 45 | 41 | if ( is_array( $args ) ) { |
| 46 | 42 | $base_where = $starts_with; |
| @@ -47,28 +43,28 @@ | ||
| 47 | 43 | self::parse_where_from_array( $args, $base_where, $where, $values ); |
| 48 | 44 | } |
| 49 | 45 | |
| 50 | 46 | $args = compact( 'where', 'values' ); |
| 51 | - } | |
| 47 | + } | |
| 52 | 48 | |
| 53 | - /** | |
| 49 | + /** | |
| 54 | 50 | * @param array $args |
| 55 | - * @param string $base_where | |
| 56 | - * @param string $where | |
| 51 | + * @param string $base_where | |
| 52 | + * @param string $where | |
| 57 | 53 | * @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 | - } | |
| 54 | + */ | |
| 55 | + public static function parse_where_from_array( $args, $base_where, &$where, &$values ) { | |
| 56 | + $condition = ' AND'; | |
| 57 | + if ( isset( $args['or'] ) ) { | |
| 58 | + $condition = ' OR'; | |
| 59 | + unset( $args['or'] ); | |
| 60 | + } | |
| 65 | 61 | |
| 66 | 62 | foreach ( $args as $key => $value ) { |
| 67 | - $where .= empty( $where ) ? $base_where : $condition; | |
| 63 | + $where .= empty( $where ) ? $base_where : $condition; | |
| 68 | 64 | $array_inc_null = ( ! is_numeric( $key ) && is_array( $value ) && in_array( null, $value ) ); |
| 69 | 65 | if ( is_numeric( $key ) || $array_inc_null ) { |
| 70 | - $where .= ' ( '; | |
| 66 | + $where .= ' ( '; | |
| 71 | 67 | $nested_where = ''; |
| 72 | 68 | if ( $array_inc_null ) { |
| 73 | 69 | foreach ( $value as $val ) { |
| 74 | 70 | $parse_where = array( |
| @@ -87,30 +83,30 @@ | ||
| 87 | 83 | } |
| 88 | 84 | } |
| 89 | 85 | } |
| 90 | 86 | |
| 91 | - /** | |
| 92 | - * @param string $key | |
| 87 | + /** | |
| 88 | + * @param string $key | |
| 93 | 89 | * @param string|array $value |
| 94 | - * @param string $where | |
| 90 | + * @param string $where | |
| 95 | 91 | * @param array $values |
| 96 | - */ | |
| 97 | - private static function interpret_array_to_sql( $key, $value, &$where, &$values ) { | |
| 92 | + */ | |
| 93 | + private static function interpret_array_to_sql( $key, $value, &$where, &$values ) { | |
| 98 | 94 | $key = trim( $key ); |
| 99 | 95 | |
| 100 | 96 | 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 { | |
| 97 | + $k = explode( ' ', $key ); | |
| 98 | + $where .= ' DATE_FORMAT(' . reset( $k ) . ', %s) ' . str_replace( reset( $k ), '', $key ); | |
| 99 | + $values[] = '%Y-%m-%d %H:%i:%s'; | |
| 100 | + } else { | |
| 105 | 101 | $where .= ' ' . $key; |
| 106 | - } | |
| 102 | + } | |
| 107 | 103 | |
| 108 | 104 | $lowercase_key = explode( ' ', strtolower( $key ) ); |
| 109 | 105 | $lowercase_key = end( $lowercase_key ); |
| 110 | 106 | |
| 111 | - if ( is_array( $value ) ) { | |
| 112 | - // translate array of values to "in" | |
| 107 | + if ( is_array( $value ) ) { | |
| 108 | + // translate array of values to "in" | |
| 113 | 109 | if ( strpos( $lowercase_key, 'like' ) !== false ) { |
| 114 | 110 | $where = preg_replace( '/' . $key . '$/', '', $where ); |
| 115 | 111 | $where .= '('; |
| 116 | 112 | $start = true; |
| @@ -117,18 +113,18 @@ | ||
| 117 | 113 | foreach ( $value as $v ) { |
| 118 | 114 | if ( ! $start ) { |
| 119 | 115 | $where .= ' OR '; |
| 120 | 116 | } |
| 121 | - $start = false; | |
| 122 | - $where .= $key . ' %s'; | |
| 117 | + $start = false; | |
| 118 | + $where .= $key . ' %s'; | |
| 123 | 119 | $values[] = '%' . self::esc_like( $v ) . '%'; |
| 124 | 120 | } |
| 125 | 121 | $where .= ')'; |
| 126 | - } elseif ( ! empty( $value ) ) { | |
| 127 | - $where .= ' in (' . self::prepare_array_values( $value, '%s' ) . ')'; | |
| 122 | + } else if ( ! empty( $value ) ) { | |
| 123 | + $where .= ' in (' . self::prepare_array_values( $value, '%s' ) . ')'; | |
| 128 | 124 | $values = array_merge( $values, $value ); |
| 129 | 125 | } |
| 130 | - } elseif ( strpos( $lowercase_key, 'like' ) !== false ) { | |
| 126 | + } else if ( strpos( $lowercase_key, 'like' ) !== false ) { | |
| 131 | 127 | /** |
| 132 | 128 | * Allow string to start or end with the value |
| 133 | 129 | * If the key is like% then skip the first % for starts with |
| 134 | 130 | * If the key is %like then skip the last % for ends with |
| @@ -133,26 +129,26 @@ | ||
| 133 | 129 | * If the key is like% then skip the first % for starts with |
| 134 | 130 | * If the key is %like then skip the last % for ends with |
| 135 | 131 | */ |
| 136 | 132 | $start = '%'; |
| 137 | - $end = '%'; | |
| 133 | + $end = '%'; | |
| 138 | 134 | if ( $lowercase_key == 'like%' ) { |
| 139 | 135 | $start = ''; |
| 140 | 136 | $where = rtrim( $where, '%' ); |
| 141 | - } elseif ( $lowercase_key == '%like' ) { | |
| 142 | - $end = ''; | |
| 137 | + } else if ( $lowercase_key == '%like' ) { | |
| 138 | + $end = ''; | |
| 143 | 139 | $where = rtrim( rtrim( $where, '%like' ), '%LIKE' ); |
| 144 | 140 | $where .= 'like'; |
| 145 | 141 | } |
| 146 | 142 | |
| 147 | - $where .= ' %s'; | |
| 143 | + $where .= ' %s'; | |
| 148 | 144 | $values[] = $start . self::esc_like( $value ) . $end; |
| 149 | 145 | |
| 150 | - } elseif ( $value === null ) { | |
| 151 | - $where .= ' IS NULL'; | |
| 152 | - } else { | |
| 146 | + } else if ( $value === null ) { | |
| 147 | + $where .= ' IS NULL'; | |
| 148 | + } else { | |
| 153 | 149 | // allow a - to prevent = from being added |
| 154 | - if ( substr( $key, - 1 ) == '-' ) { | |
| 150 | + if ( substr( $key, -1 ) == '-' ) { | |
| 155 | 151 | $where = rtrim( $where, '-' ); |
| 156 | 152 | } else { |
| 157 | 153 | $where .= '='; |
| 158 | 154 | } |
| @@ -158,22 +154,21 @@ | ||
| 158 | 154 | } |
| 159 | 155 | |
| 160 | 156 | self::add_query_placeholder( $key, $value, $where ); |
| 161 | 157 | |
| 162 | - $values[] = $value; | |
| 163 | - } | |
| 164 | - } | |
| 158 | + $values[] = $value; | |
| 159 | + } | |
| 160 | + } | |
| 165 | 161 | |
| 166 | 162 | /** |
| 167 | 163 | * Add %d, or %s to query |
| 168 | 164 | * |
| 169 | 165 | * @since 2.02.05 |
| 170 | - * | |
| 171 | 166 | * @param string $key |
| 172 | 167 | * @param int|string $value |
| 173 | 168 | * @param string $where |
| 174 | 169 | */ |
| 175 | - private static function add_query_placeholder( $key, $value, &$where ) { | |
| 170 | + private static function add_query_placeholder( $key, $value, &$where ) { | |
| 176 | 171 | if ( is_numeric( $value ) && ( strpos( $key, 'meta_value' ) === false || strpos( $key, '+0' ) !== false ) ) { |
| 177 | 172 | $value = $value + 0; // switch string to number |
| 178 | 173 | $where .= is_float( $value ) ? '%f' : '%d'; |
| 179 | 174 | } else { |
| @@ -180,21 +175,19 @@ | ||
| 180 | 175 | $where .= '%s'; |
| 181 | 176 | } |
| 182 | 177 | } |
| 183 | 178 | |
| 184 | - /** | |
| 185 | - * @param string $table | |
| 179 | + /** | |
| 180 | + * @param string $table | |
| 186 | 181 | * @param array $where |
| 187 | 182 | * @param array $args |
| 188 | - * | |
| 189 | 183 | * @return int |
| 190 | - */ | |
| 191 | - public static function get_count( $table, $where = array(), $args = array() ) { | |
| 192 | - $count = self::get_var( $table, $where, 'COUNT(*)', $args ); | |
| 184 | + */ | |
| 185 | + public static function get_count( $table, $where = array(), $args = array() ) { | |
| 186 | + $count = self::get_var( $table, $where, 'COUNT(*)', $args ); | |
| 187 | + return $count; | |
| 188 | + } | |
| 193 | 189 | |
| 194 | - return (int) $count; | |
| 195 | - } | |
| 196 | - | |
| 197 | 190 | /** |
| 198 | 191 | * @param string $table |
| 199 | 192 | * @param array $where |
| 200 | 193 | * @param string $field |
| @@ -200,27 +193,22 @@ | ||
| 200 | 193 | * @param string $field |
| 201 | 194 | * @param array $args |
| 202 | 195 | * @param string $limit |
| 203 | 196 | * @param string $type |
| 204 | - * | |
| 205 | 197 | * @return array|null|string|object |
| 206 | 198 | */ |
| 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 ); | |
| 199 | + public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) { | |
| 200 | + $group = ''; | |
| 201 | + self::get_group_and_table_name( $table, $group ); | |
| 210 | 202 | self::convert_options_to_array( $args, '', $limit ); |
| 211 | - if ( $type === 'var' && ! isset( $args['limit'] ) ) { | |
| 212 | - $args['limit'] = 1; | |
| 213 | - } | |
| 214 | 203 | |
| 215 | 204 | $query = self::generate_query_string_from_pieces( $field, $table, $where, $args ); |
| 216 | 205 | |
| 217 | 206 | $cache_key = self::generate_cache_key( $where, $args, $field, $type ); |
| 218 | - $results = self::check_cache( $cache_key, $group, $query, 'get_' . $type ); | |
| 207 | + $results = self::check_cache( $cache_key, $group, $query, 'get_' . $type ); | |
| 208 | + return $results; | |
| 209 | + } | |
| 219 | 210 | |
| 220 | - return $results; | |
| 221 | - } | |
| 222 | - | |
| 223 | 211 | /** |
| 224 | 212 | * Generate a cache key from the where query, field, type, and other arguments |
| 225 | 213 | * |
| 226 | 214 | * @since 2.03.07 |
| @@ -231,11 +219,11 @@ | ||
| 231 | 219 | * @param string $type |
| 232 | 220 | * |
| 233 | 221 | * @return string |
| 234 | 222 | */ |
| 235 | - public static function generate_cache_key( $where, $args, $field, $type ) { | |
| 223 | + private static function generate_cache_key( $where, $args, $field, $type ) { | |
| 236 | 224 | $cache_key = ''; |
| 237 | - $where = FrmAppHelper::array_flatten( $where ); | |
| 225 | + $where = FrmAppHelper::array_flatten( $where ); | |
| 238 | 226 | foreach ( $where as $key => $value ) { |
| 239 | 227 | $cache_key .= $key . '_' . $value; |
| 240 | 228 | } |
| 241 | 229 | $cache_key .= implode( '_', $args ) . $field . '_' . $type; |
| @@ -243,52 +231,46 @@ | ||
| 243 | 231 | |
| 244 | 232 | return $cache_key; |
| 245 | 233 | } |
| 246 | 234 | |
| 247 | - /** | |
| 248 | - * @param string $table | |
| 249 | - * @param array $where | |
| 235 | + /** | |
| 236 | + * @param string $table | |
| 237 | + * @param array $where | |
| 250 | 238 | * @param string $field |
| 251 | 239 | * @param array $args |
| 252 | 240 | * @param string $limit |
| 253 | - * | |
| 254 | 241 | * @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 | - } | |
| 242 | + */ | |
| 243 | + public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) { | |
| 244 | + return self::get_var( $table, $where, $field, $args, $limit, 'col' ); | |
| 245 | + } | |
| 259 | 246 | |
| 260 | - /** | |
| 261 | - * @since 2.0 | |
| 262 | - * | |
| 263 | - * @param string $table | |
| 247 | + /** | |
| 248 | + * @since 2.0 | |
| 249 | + * @param string $table | |
| 264 | 250 | * @param array $where |
| 265 | 251 | * @param string $fields |
| 266 | 252 | * @param array $args |
| 267 | - * | |
| 268 | 253 | * @return mixed |
| 269 | - */ | |
| 270 | - public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) { | |
| 271 | - $args['limit'] = 1; | |
| 254 | + */ | |
| 255 | + public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) { | |
| 256 | + $args['limit'] = 1; | |
| 257 | + return self::get_var( $table, $where, $fields, $args, '', 'row' ); | |
| 258 | + } | |
| 272 | 259 | |
| 273 | - return self::get_var( $table, $where, $fields, $args, '', 'row' ); | |
| 274 | - } | |
| 275 | - | |
| 276 | - /** | |
| 277 | - * Prepare a key/value array before DB call | |
| 260 | + /** | |
| 261 | + * Prepare a key/value array before DB call | |
| 278 | 262 | * |
| 279 | - * @since 2.0 | |
| 280 | - * | |
| 281 | - * @param string $table | |
| 263 | + * @since 2.0 | |
| 264 | + * @param string $table | |
| 282 | 265 | * @param array $where |
| 283 | 266 | * @param string $fields |
| 284 | 267 | * @param array $args |
| 285 | - * | |
| 286 | 268 | * @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 | - } | |
| 269 | + */ | |
| 270 | + public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) { | |
| 271 | + return self::get_var( $table, $where, $fields, $args, '', 'results' ); | |
| 272 | + } | |
| 291 | 273 | |
| 292 | 274 | /** |
| 293 | 275 | * Check for like, not like, in, not in, =, !=, >, <, <=, >= |
| 294 | 276 | * Return a value to append to the where array key |
| @@ -293,23 +275,22 @@ | ||
| 293 | 275 | * Check for like, not like, in, not in, =, !=, >, <, <=, >= |
| 294 | 276 | * Return a value to append to the where array key |
| 295 | 277 | * |
| 296 | 278 | * @param string $where_is |
| 297 | - * | |
| 298 | 279 | * @return string |
| 299 | 280 | */ |
| 300 | 281 | public static function append_where_is( $where_is ) { |
| 301 | 282 | $switch_to = array( |
| 302 | - '=' => '', | |
| 303 | - '!=' => '!', | |
| 304 | - '<=' => '<', | |
| 305 | - '>=' => '>', | |
| 306 | - 'like' => 'like', | |
| 283 | + '=' => '', | |
| 284 | + '!=' => '!', | |
| 285 | + '<=' => '<', | |
| 286 | + '>=' => '>', | |
| 287 | + 'like' => 'like', | |
| 307 | 288 | 'not like' => 'not like', |
| 308 | - 'in' => '', | |
| 309 | - 'not in' => 'not', | |
| 310 | - 'like%' => 'like%', | |
| 311 | - '%like' => '%like', | |
| 289 | + 'in' => '', | |
| 290 | + 'not in' => 'not', | |
| 291 | + 'like%' => 'like%', | |
| 292 | + '%like' => '%like', | |
| 312 | 293 | ); |
| 313 | 294 | |
| 314 | 295 | $where_is = strtolower( $where_is ); |
| 315 | 296 | if ( isset( $switch_to[ $where_is ] ) ) { |
| @@ -324,69 +305,58 @@ | ||
| 324 | 305 | // fallback to = if the query is none of these |
| 325 | 306 | return ''; |
| 326 | 307 | } |
| 327 | 308 | |
| 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 ) { | |
| 309 | + /** | |
| 310 | + * Get 'frm_forms' from wp_frm_forms or a longer table param that includes a join | |
| 311 | + * Also add the wpdb->prefix to the table if it's missing | |
| 312 | + * | |
| 313 | + * @param string $table | |
| 314 | + * @param string $group | |
| 315 | + */ | |
| 316 | + private static function get_group_and_table_name( &$table, &$group ) { | |
| 336 | 317 | global $wpdb, $wpmuBaseTablePrefix; |
| 337 | 318 | |
| 338 | 319 | $table_parts = explode( ' ', $table ); |
| 339 | - $group = reset( $table_parts ); | |
| 340 | - self::maybe_remove_prefix( $wpdb->prefix, $group ); | |
| 320 | + $group = reset( $table_parts ); | |
| 321 | + $group = str_replace( $wpdb->prefix, '', $group ); | |
| 341 | 322 | |
| 342 | 323 | $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix : $wpdb->base_prefix; |
| 343 | - self::maybe_remove_prefix( $prefix, $group ); | |
| 324 | + $group = str_replace( $prefix, '', $group ); | |
| 344 | 325 | |
| 345 | - if ( $group == $table ) { | |
| 346 | - $table = $wpdb->prefix . $table; | |
| 347 | - } | |
| 326 | + if ( $group == $table ) { | |
| 327 | + $table = $wpdb->prefix . $table; | |
| 328 | + } | |
| 348 | 329 | |
| 349 | 330 | // switch to singular group name |
| 350 | 331 | $group = rtrim( $group, 's' ); |
| 351 | - } | |
| 332 | + } | |
| 352 | 333 | |
| 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 = '' ) { | |
| 334 | + private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) { | |
| 365 | 335 | if ( ! is_array( $args ) ) { |
| 366 | 336 | $args = array( 'order_by' => $args ); |
| 367 | - } | |
| 337 | + } | |
| 368 | 338 | |
| 369 | - if ( ! empty( $order_by ) ) { | |
| 370 | - $args['order_by'] = $order_by; | |
| 371 | - } | |
| 339 | + if ( ! empty( $order_by ) ) { | |
| 340 | + $args['order_by'] = $order_by; | |
| 341 | + } | |
| 372 | 342 | |
| 373 | - if ( ! empty( $limit ) ) { | |
| 374 | - $args['limit'] = $limit; | |
| 375 | - } | |
| 343 | + if ( ! empty( $limit ) ) { | |
| 344 | + $args['limit'] = $limit; | |
| 345 | + } | |
| 376 | 346 | |
| 377 | - $temp_args = $args; | |
| 378 | - foreach ( $temp_args as $k => $v ) { | |
| 379 | - if ( $v == '' ) { | |
| 347 | + $temp_args = $args; | |
| 348 | + foreach ( $temp_args as $k => $v ) { | |
| 349 | + if ( $v == '' ) { | |
| 380 | 350 | unset( $args[ $k ] ); |
| 381 | - continue; | |
| 382 | - } | |
| 351 | + continue; | |
| 352 | + } | |
| 383 | 353 | |
| 384 | - $db_name = strtoupper( str_replace( '_', ' ', $k ) ); | |
| 385 | - if ( strpos( $v, $db_name ) === false ) { | |
| 354 | + $db_name = strtoupper( str_replace( '_', ' ', $k ) ); | |
| 355 | + if ( strpos( $v, $db_name ) === false ) { | |
| 386 | 356 | $args[ $k ] = $db_name . ' ' . $v; |
| 387 | - } | |
| 388 | - } | |
| 357 | + } | |
| 358 | + } | |
| 389 | 359 | |
| 390 | 360 | // Make sure LIMIT is the last argument |
| 391 | 361 | if ( isset( $args['order_by'] ) && isset( $args['limit'] ) ) { |
| 392 | 362 | $temp_limit = $args['limit']; |
| @@ -392,19 +362,17 @@ | ||
| 392 | 362 | $temp_limit = $args['limit']; |
| 393 | 363 | unset( $args['limit'] ); |
| 394 | 364 | $args['limit'] = $temp_limit; |
| 395 | 365 | } |
| 396 | - } | |
| 366 | + } | |
| 397 | 367 | |
| 398 | 368 | /** |
| 399 | 369 | * Get the associative array results for the given columns, table, and where query |
| 400 | 370 | * |
| 401 | 371 | * @since 2.02.05 |
| 402 | - * | |
| 403 | 372 | * @param string $columns |
| 404 | 373 | * @param string $table |
| 405 | 374 | * @param array $where |
| 406 | - * | |
| 407 | 375 | * @return mixed |
| 408 | 376 | */ |
| 409 | 377 | public static function get_associative_array_results( $columns, $table, $where ) { |
| 410 | 378 | $group = ''; |
| @@ -412,9 +380,9 @@ | ||
| 412 | 380 | |
| 413 | 381 | $query = self::generate_query_string_from_pieces( $columns, $table, $where ); |
| 414 | 382 | |
| 415 | 383 | $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' ); | |
| 384 | + $results = self::check_cache( $cache_key, $group, $query, 'get_associative_results' ); | |
| 417 | 385 | |
| 418 | 386 | return $results; |
| 419 | 387 | } |
| 420 | 388 | |
| @@ -426,9 +394,8 @@ | ||
| 426 | 394 | * @param string $columns |
| 427 | 395 | * @param string $table |
| 428 | 396 | * @param mixed $where |
| 429 | 397 | * @param array $args |
| 430 | - * | |
| 431 | 398 | * @return string |
| 432 | 399 | */ |
| 433 | 400 | private static function generate_query_string_from_pieces( $columns, $table, $where, $args = array() ) { |
| 434 | 401 | $query = 'SELECT ' . $columns . ' FROM ' . $table; |
| @@ -437,9 +404,9 @@ | ||
| 437 | 404 | |
| 438 | 405 | if ( is_array( $where ) || empty( $where ) ) { |
| 439 | 406 | self::get_where_clause_and_values( $where ); |
| 440 | 407 | global $wpdb; |
| 441 | - $query = $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 408 | + $query = $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); // WPCS: unprepared SQL ok. | |
| 442 | 409 | } else { |
| 443 | 410 | /** |
| 444 | 411 | * Allow the $where to be prepared before we recieve it here. |
| 445 | 412 | * This is a fallback for reverse compatibility, but is not recommended |
| @@ -467,26 +434,23 @@ | ||
| 467 | 434 | } |
| 468 | 435 | } |
| 469 | 436 | } |
| 470 | 437 | |
| 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 | - */ | |
| 438 | + /** | |
| 439 | + * Added for < WP 4.0 compatability | |
| 440 | + * | |
| 441 | + * @since 2.05.06 | |
| 442 | + * | |
| 443 | + * @param string $term The value to escape | |
| 444 | + * @return string The escaped value | |
| 445 | + */ | |
| 480 | 446 | public static function esc_like( $term ) { |
| 481 | - global $wpdb; | |
| 482 | - | |
| 447 | + global $wpdb; | |
| 483 | 448 | return $wpdb->esc_like( $term ); |
| 484 | - } | |
| 449 | + } | |
| 485 | 450 | |
| 486 | 451 | /** |
| 487 | 452 | * @since 2.05.06 |
| 488 | - * | |
| 489 | 453 | * @param string $order_query |
| 490 | 454 | */ |
| 491 | 455 | public static function esc_order( $order_query ) { |
| 492 | 456 | if ( empty( $order_query ) ) { |
| @@ -500,9 +464,9 @@ | ||
| 500 | 464 | } |
| 501 | 465 | |
| 502 | 466 | $order_query = explode( ' ', trim( $order_query ) ); |
| 503 | 467 | |
| 504 | - $order = trim( reset( $order_query ) ); | |
| 468 | + $order = trim( reset( $order_query ) ); | |
| 505 | 469 | $safe_order = array( 'count(*)' ); |
| 506 | 470 | if ( ! in_array( strtolower( $order ), $safe_order ) ) { |
| 507 | 471 | $order = preg_replace( '/[^a-zA-Z0-9\-\_\.\+]/', '', $order ); |
| 508 | 472 | } |
| @@ -529,9 +493,8 @@ | ||
| 529 | 493 | } |
| 530 | 494 | |
| 531 | 495 | /** |
| 532 | 496 | * @param string $limit |
| 533 | - * | |
| 534 | 497 | * @since 2.05.06 |
| 535 | 498 | */ |
| 536 | 499 | public static function esc_limit( $limit ) { |
| 537 | 500 | if ( empty( $limit ) ) { |
| @@ -550,9 +513,8 @@ | ||
| 550 | 513 | } |
| 551 | 514 | } |
| 552 | 515 | |
| 553 | 516 | $limit = implode( ',', $limit ); |
| 554 | - | |
| 555 | 517 | return ' LIMIT ' . $limit; |
| 556 | 518 | } |
| 557 | 519 | |
| 558 | 520 | /** |
| @@ -561,9 +523,8 @@ | ||
| 561 | 523 | * @since 2.05.06 |
| 562 | 524 | */ |
| 563 | 525 | public static function prepare_array_values( $array, $type = '%s' ) { |
| 564 | 526 | $placeholders = array_fill( 0, count( $array ), $type ); |
| 565 | - | |
| 566 | 527 | return implode( ', ', $placeholders ); |
| 567 | 528 | } |
| 568 | 529 | |
| 569 | 530 | /** |
| @@ -570,28 +531,20 @@ | ||
| 570 | 531 | * @since 2.05.06 |
| 571 | 532 | */ |
| 572 | 533 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
| 573 | 534 | if ( empty( $where ) ) { |
| 574 | - $where = ''; | |
| 535 | + return ''; | |
| 536 | + } | |
| 537 | + | |
| 538 | + if ( is_array( $where ) ) { | |
| 539 | + global $wpdb; | |
| 540 | + self::get_where_clause_and_values( $where, $starts_with ); | |
| 541 | + $where = $wpdb->prepare( $where['where'], $where['values'] ); // WPCS: unprepared SQL ok. | |
| 575 | 542 | } 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 | - } | |
| 543 | + $where = $starts_with . $where; | |
| 583 | 544 | } |
| 584 | 545 | |
| 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 ); | |
| 546 | + return $where; | |
| 594 | 547 | } |
| 595 | 548 | |
| 596 | 549 | /** |
| 597 | 550 | * Prepare and save settings in styles and actions |
| @@ -601,9 +554,9 @@ | ||
| 601 | 554 | * |
| 602 | 555 | * @since 2.05.06 |
| 603 | 556 | */ |
| 604 | 557 | public static function save_settings( $settings, $group ) { |
| 605 | - $settings = (array) $settings; | |
| 558 | + $settings = (array) $settings; | |
| 606 | 559 | $settings['post_content'] = FrmAppHelper::prepare_and_encode( $settings['post_content'] ); |
| 607 | 560 | |
| 608 | 561 | if ( empty( $settings['ID'] ) ) { |
| 609 | 562 | unset( $settings['ID'] ); |
| @@ -622,17 +575,12 @@ | ||
| 622 | 575 | * |
| 623 | 576 | * Used when saving form actions and styles |
| 624 | 577 | * |
| 625 | 578 | * @since 2.05.06 |
| 626 | - * | |
| 627 | - * @param array $settings | |
| 628 | - * @return int|WP_Error | |
| 629 | 579 | */ |
| 630 | 580 | public static function save_json_post( $settings ) { |
| 631 | 581 | global $wp_filter; |
| 632 | - if ( isset( $wp_filter['content_save_pre'] ) ) { | |
| 633 | - $filters = $wp_filter['content_save_pre']; | |
| 634 | - } | |
| 582 | + $filters = $wp_filter['content_save_pre']; | |
| 635 | 583 | |
| 636 | 584 | // Remove the balanceTags filter in case WordPress is trying to validate the XHTML |
| 637 | 585 | remove_all_filters( 'content_save_pre' ); |
| 638 | 586 | |
| @@ -638,27 +586,24 @@ | ||
| 638 | 586 | |
| 639 | 587 | $post = wp_insert_post( $settings ); |
| 640 | 588 | |
| 641 | 589 | // add the content filters back for views or posts |
| 642 | - if ( isset( $filters ) ) { | |
| 643 | - $wp_filter['content_save_pre'] = $filters; | |
| 644 | - } | |
| 590 | + $wp_filter['content_save_pre'] = $filters; | |
| 645 | 591 | |
| 646 | 592 | return $post; |
| 647 | 593 | } |
| 648 | 594 | |
| 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 | - */ | |
| 595 | + /** | |
| 596 | + * Check cache before fetching values and saving to cache | |
| 597 | + * | |
| 598 | + * @since 2.05.06 | |
| 599 | + * | |
| 600 | + * @param string $cache_key The unique name for this cache | |
| 601 | + * @param string $group The name of the cache group | |
| 602 | + * @param string $query If blank, don't run a db call | |
| 603 | + * @param string $type The wpdb function to use with this query | |
| 604 | + * @return mixed $results The cache or query results | |
| 605 | + */ | |
| 661 | 606 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
| 662 | 607 | $results = wp_cache_get( $cache_key, $group ); |
| 663 | 608 | if ( ! FrmAppHelper::is_empty_value( $results, false ) || empty( $query ) ) { |
| 664 | 609 | return $results; |
| @@ -667,9 +612,9 @@ | ||
| 667 | 612 | if ( 'get_posts' == $type ) { |
| 668 | 613 | $results = get_posts( $query ); |
| 669 | 614 | } elseif ( 'get_associative_results' == $type ) { |
| 670 | 615 | global $wpdb; |
| 671 | - $results = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 616 | + $results = $wpdb->get_results( $query, OBJECT_K ); // WPCS: unprepared SQL ok. | |
| 672 | 617 | } else { |
| 673 | 618 | global $wpdb; |
| 674 | 619 | $results = $wpdb->{$type}( $query ); |
| 675 | 620 | } |
| @@ -695,9 +640,9 @@ | ||
| 695 | 640 | * |
| 696 | 641 | * @since 2.05.06 |
| 697 | 642 | */ |
| 698 | 643 | public static function add_key_to_group_cache( $key, $group ) { |
| 699 | - $cached = self::get_group_cached_keys( $group ); | |
| 644 | + $cached = self::get_group_cached_keys( $group ); | |
| 700 | 645 | $cached[ $key ] = $key; |
| 701 | 646 | wp_cache_set( 'cached_keys', $cached, $group, 300 ); |
| 702 | 647 | } |
| 703 | 648 | |
| @@ -714,9 +659,8 @@ | ||
| 714 | 659 | } |
| 715 | 660 | |
| 716 | 661 | /** |
| 717 | 662 | * @since 2.05.06 |
| 718 | - * | |
| 719 | 663 | * @param string $cache_key |
| 720 | 664 | */ |
| 721 | 665 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
| 722 | 666 | delete_transient( $cache_key ); |
| @@ -722,15 +666,15 @@ | ||
| 722 | 666 | delete_transient( $cache_key ); |
| 723 | 667 | wp_cache_delete( $cache_key, $group ); |
| 724 | 668 | } |
| 725 | 669 | |
| 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 | - */ | |
| 670 | + /** | |
| 671 | + * Delete all caching in a single group | |
| 672 | + * | |
| 673 | + * @since 2.05.06 | |
| 674 | + * | |
| 675 | + * @param string $group The name of the cache group | |
| 676 | + */ | |
| 733 | 677 | public static function cache_delete_group( $group ) { |
| 734 | 678 | $cached_keys = self::get_group_cached_keys( $group ); |
| 735 | 679 | |
| 736 | 680 | if ( ! empty( $cached_keys ) ) { |
| @@ -739,6 +683,30 @@ | ||
| 739 | 683 | } |
| 740 | 684 | |
| 741 | 685 | wp_cache_delete( 'cached_keys', $group ); |
| 742 | 686 | } |
| 687 | + } | |
| 688 | + | |
| 689 | + /** | |
| 690 | + * @deprecated 2.05.06 | |
| 691 | + * @codeCoverageIgnore | |
| 692 | + */ | |
| 693 | + public function upgrade() { | |
| 694 | + FrmDeprecated::upgrade(); | |
| 695 | + } | |
| 696 | + | |
| 697 | + /** | |
| 698 | + * @deprecated 2.05.06 | |
| 699 | + * @codeCoverageIgnore | |
| 700 | + */ | |
| 701 | + public function collation() { | |
| 702 | + return FrmDeprecated::collation(); | |
| 703 | + } | |
| 704 | + | |
| 705 | + /** | |
| 706 | + * @deprecated 2.05.06 | |
| 707 | + * @codeCoverageIgnore | |
| 708 | + */ | |
| 709 | + public function uninstall() { | |
| 710 | + FrmDeprecated::uninstall(); | |
| 743 | 711 | } |
| 744 | 712 | } |