| 1 |
<?php |
| 2 |
|
| 3 |
use Imagify\Traits\InstanceGetterTrait; |
| 4 |
|
| 5 |
/** |
| 6 |
* DB class that handles files in "custom folders". |
| 7 |
* |
| 8 |
* @since 1.7 |
| 9 |
* @author Grégory Viguier |
| 10 |
*/ |
| 11 |
class Imagify_Folders_DB extends Imagify_Abstract_DB { |
| 12 |
use InstanceGetterTrait; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class version. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
const VERSION = '1.0.1'; |
| 20 |
|
| 21 |
/** |
| 22 |
* The suffix used in the name of the database table (so, without the wpdb prefix). |
| 23 |
* |
| 24 |
* @var string |
| 25 |
* @since 1.7 |
| 26 |
* @access protected |
| 27 |
*/ |
| 28 |
protected $table = 'imagify_folders'; |
| 29 |
|
| 30 |
/** |
| 31 |
* The version of our database table. |
| 32 |
* |
| 33 |
* @var int |
| 34 |
* @since 1.7 |
| 35 |
* @access protected |
| 36 |
*/ |
| 37 |
protected $table_version = 100; |
| 38 |
|
| 39 |
/** |
| 40 |
* Tell if the table is the same for each site of a Multisite. |
| 41 |
* |
| 42 |
* @var bool |
| 43 |
* @since 1.7 |
| 44 |
* @access protected |
| 45 |
*/ |
| 46 |
protected $table_is_global = true; |
| 47 |
|
| 48 |
/** |
| 49 |
* The name of the primary column. |
| 50 |
* |
| 51 |
* @var string |
| 52 |
* @since 1.7 |
| 53 |
* @access protected |
| 54 |
*/ |
| 55 |
protected $primary_key = 'folder_id'; |
| 56 |
|
| 57 |
/** |
| 58 |
* Whitelist of columns. |
| 59 |
* |
| 60 |
* @since 1.7 |
| 61 |
* @access public |
| 62 |
* @author Grégory Viguier |
| 63 |
* |
| 64 |
* @return array |
| 65 |
*/ |
| 66 |
public function get_columns() { |
| 67 |
return [ |
| 68 |
'folder_id' => '%d', |
| 69 |
'path' => '%s', |
| 70 |
'active' => '%d', |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Default column values. |
| 76 |
* |
| 77 |
* @since 1.7 |
| 78 |
* @access public |
| 79 |
* @author Grégory Viguier |
| 80 |
* |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public function get_column_defaults() { |
| 84 |
return [ |
| 85 |
'folder_id' => 0, |
| 86 |
'path' => '', |
| 87 |
'active' => 0, |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the query to create the table fields. |
| 93 |
* |
| 94 |
* @since 1.7 |
| 95 |
* @access protected |
| 96 |
* @author Grégory Viguier |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
protected function get_table_schema() { |
| 101 |
return " |
| 102 |
folder_id bigint(20) unsigned NOT NULL auto_increment, |
| 103 |
path varchar(191) NOT NULL default '', |
| 104 |
active tinyint(1) unsigned NOT NULL default 0, |
| 105 |
PRIMARY KEY (folder_id), |
| 106 |
UNIQUE KEY path (path), |
| 107 |
KEY active (active)"; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Tell if folders are selected in the plugin settings. |
| 112 |
* |
| 113 |
* @since 1.7 |
| 114 |
* @access public |
| 115 |
* @author Grégory Viguier |
| 116 |
* |
| 117 |
* @return bool |
| 118 |
*/ |
| 119 |
public function has_active_folders() { |
| 120 |
global $wpdb; |
| 121 |
|
| 122 |
$column = esc_sql( $this->get_primary_key() ); |
| 123 |
|
| 124 |
return (bool) $wpdb->get_var( "SELECT $column FROM $this->table_name WHERE active = 1 LIMIT 1;" ); // WPCS: unprepared SQL ok. |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Retrieve active folders (checked in the settings). |
| 129 |
* |
| 130 |
* @since 1.7 |
| 131 |
* @access public |
| 132 |
* @author Grégory Viguier |
| 133 |
* |
| 134 |
* @param string $column_select A column name. |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
public function get_active_folders_column( $column_select ) { |
| 138 |
global $wpdb; |
| 139 |
|
| 140 |
$column = esc_sql( $column_select ); |
| 141 |
|
| 142 |
$result = $wpdb->get_col( "SELECT $column FROM $this->table_name WHERE active = 1;" ); // WPCS: unprepared SQL ok. |
| 143 |
|
| 144 |
return $this->cast_col( $result, $column_select ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Retrieve active folders (checked in the settings) by the specified column / values. |
| 149 |
* |
| 150 |
* @since 1.7 |
| 151 |
* @access public |
| 152 |
* @author Grégory Viguier |
| 153 |
* |
| 154 |
* @param string $column_select A column name. |
| 155 |
* @param string $column_where A column name. |
| 156 |
* @param array $column_values An array of values. |
| 157 |
* @return array |
| 158 |
*/ |
| 159 |
public function get_active_folders_column_in( $column_select, $column_where, $column_values ) { |
| 160 |
global $wpdb; |
| 161 |
|
| 162 |
$column = esc_sql( $column_select ); |
| 163 |
$column_where = esc_sql( $column_where ); |
| 164 |
$column_values = Imagify_DB::prepare_values_list( $column_values ); |
| 165 |
|
| 166 |
$result = $wpdb->get_col( "SELECT $column FROM $this->table_name WHERE $column_where IN ( $column_values ) AND active = 1;" ); // WPCS: unprepared SQL ok. |
| 167 |
|
| 168 |
return $this->cast_col( $result, $column_select ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Retrieve active folders (checked in the settings) by the specified column / values. |
| 173 |
* |
| 174 |
* @since 1.7 |
| 175 |
* @access public |
| 176 |
* @author Grégory Viguier |
| 177 |
* |
| 178 |
* @param string $column_select A column name. |
| 179 |
* @param string $column_where A column name. |
| 180 |
* @param array $column_values An array of values. |
| 181 |
* @return array |
| 182 |
*/ |
| 183 |
public function get_active_folders_column_not_in( $column_select, $column_where, $column_values ) { |
| 184 |
global $wpdb; |
| 185 |
|
| 186 |
$column = esc_sql( $column_select ); |
| 187 |
$column_where = esc_sql( $column_where ); |
| 188 |
$column_values = Imagify_DB::prepare_values_list( $column_values ); |
| 189 |
|
| 190 |
$result = $wpdb->get_col( "SELECT $column FROM $this->table_name WHERE $column_where NOT IN ( $column_values ) AND active = 1;" ); // WPCS: unprepared SQL ok. |
| 191 |
|
| 192 |
return $this->cast_col( $result, $column_select ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Retrieve not active folders (not checked in the settings). |
| 197 |
* |
| 198 |
* @since 1.7 |
| 199 |
* @access public |
| 200 |
* @author Grégory Viguier |
| 201 |
* |
| 202 |
* @param string $column_select A column name. |
| 203 |
* @return array |
| 204 |
*/ |
| 205 |
public function get_inactive_folders_column( $column_select ) { |
| 206 |
global $wpdb; |
| 207 |
|
| 208 |
$column = esc_sql( $column_select ); |
| 209 |
|
| 210 |
$result = $wpdb->get_col( "SELECT $column FROM $this->table_name WHERE active != 1;" ); // WPCS: unprepared SQL ok. |
| 211 |
|
| 212 |
return $this->cast_col( $result, $column_select ); |
| 213 |
} |
| 214 |
} |
| 215 |
|