class-wpml-custom-field-setting-query.php
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
class WPML_Custom_Field_Setting_Query {
/** @var wpdb $wpdb */
private $wpdb;
/** @var array $excluded_keys */
private $excluded_keys;
/** @var string $table */
private $table;
/**
* @param wpdb $wpdb
* @param array $excluded_keys
* @param string $table
*/
public function __construct( wpdb $wpdb, array $excluded_keys, $table ) {
$this->wpdb = $wpdb;
$this->excluded_keys = $excluded_keys;
$this->table = $table;
}
/**
* @param array $args
*
* @return array
*/
public function get( array $args ) {
$args = array_merge(
array(
'search' => null,
'hide_system_fields' => false,
'items_per_page' => null,
'page' => null,
),
$args
);
$where = ' WHERE 1=1';
$where .= $this->add_AND_excluded_fields_condition();
$where .= $this->add_AND_search_condition( $args['search'] );
$where .= $this->add_AND_system_fields_condition( $args['hide_system_fields'] );
$limit_offset = $this->get_limit_offset( $args );
$query = "SELECT SQL_CALC_FOUND_ROWS DISTINCT meta_key FROM {$this->table}" . $where . $limit_offset;
return $this->wpdb->get_col( $query );
}
/**
* @return int
*/
public function get_total_rows() {
return (int) $this->wpdb->get_var( 'SELECT FOUND_ROWS();' );
}
/**
* @return string
*/
private function add_AND_excluded_fields_condition() {
if ( $this->excluded_keys ) {
return ' AND meta_key NOT IN(' . wpml_prepare_in( $this->excluded_keys ) . ')';
}
return '';
}
/**
* @param string $search
*
* @return string
*/
private function add_AND_search_condition( $search ) {
return $search ? $this->wpdb->prepare( " AND meta_key LIKE '%s'", '%' . $search . '%' ) : '';
}
/**
* @param bool $hide_system_fields
*
* @return string
*/
private function add_AND_system_fields_condition( $hide_system_fields ) {
return $hide_system_fields ? " AND meta_key NOT LIKE '\_%'" : '';
}
/**
* @param array $args
*
* @return string
*/
private function get_limit_offset( array $args ) {
$limit_offset = '';
if ( $args['items_per_page'] && 0 < (int) $args['page'] ) {
$limit_offset = $this->wpdb->prepare(
' LIMIT %d OFFSET %d',
$args['items_per_page'],
( $args['page'] - 1 ) * $args['items_per_page']
);
}
return $limit_offset;
}
}