| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Plugin_Table_Models { |
| 4 |
|
| 5 |
use WPDataAccess\WPDA; |
| 6 |
|
| 7 |
class WPDA_App_Model extends WPDA_Plugin_Table_Base_Model { |
| 8 |
|
| 9 |
const BASE_TABLE_NAME = 'wpda_app'; |
| 10 |
|
| 11 |
public static function get_by_id( $app_id ) { |
| 12 |
|
| 13 |
global $wpdb; |
| 14 |
$dataset = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table |
| 15 |
$wpdb->prepare( |
| 16 |
'SELECT * FROM `%1s` WHERE app_id = %d', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 17 |
array( |
| 18 |
WPDA::remove_backticks( self::get_base_table_name() ), |
| 19 |
$app_id, |
| 20 |
) |
| 21 |
), // db call ok; no-cache ok. |
| 22 |
'ARRAY_A' |
| 23 |
); |
| 24 |
|
| 25 |
return 1 === $wpdb->num_rows ? $dataset : false; |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
public static function get_by_name( $app_name ) { |
| 30 |
|
| 31 |
global $wpdb; |
| 32 |
$dataset = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table |
| 33 |
$wpdb->prepare( |
| 34 |
'SELECT * FROM `%1s` WHERE app_name = %s', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 35 |
array( |
| 36 |
WPDA::remove_backticks( self::get_base_table_name() ), |
| 37 |
$app_name, |
| 38 |
) |
| 39 |
), // db call ok; no-cache ok. |
| 40 |
'ARRAY_A' |
| 41 |
); |
| 42 |
|
| 43 |
return 1 === $wpdb->num_rows ? $dataset : false; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
public static function list() { |
| 48 |
|
| 49 |
global $wpdb; |
| 50 |
return $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table |
| 51 |
$wpdb->prepare( |
| 52 |
'SELECT * FROM `%1s` ORDER BY app_name', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 53 |
array( |
| 54 |
WPDA::remove_backticks( self::get_base_table_name() ), |
| 55 |
) |
| 56 |
), // db call ok; no-cache ok. |
| 57 |
'ARRAY_A' |
| 58 |
); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
public static function has_any() { |
| 63 |
|
| 64 |
global $wpdb; |
| 65 |
$wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table |
| 66 |
$wpdb->prepare( |
| 67 |
'SELECT 1 FROM `%1s` LIMIT 1', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 68 |
array( |
| 69 |
WPDA::remove_backticks( self::get_base_table_name() ), |
| 70 |
) |
| 71 |
), // db call ok; no-cache ok. |
| 72 |
'ARRAY_A' |
| 73 |
); |
| 74 |
|
| 75 |
return 1 === $wpdb->num_rows; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
public static function add_to_dashboard_menu() { |
| 80 |
|
| 81 |
global $wpdb; |
| 82 |
return $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table |
| 83 |
$wpdb->prepare( |
| 84 |
'SELECT * FROM `%1s` WHERE `app_add_to_menu` = 1 ORDER BY app_name', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 85 |
array( |
| 86 |
WPDA::remove_backticks( self::get_base_table_name() ), |
| 87 |
) |
| 88 |
), // db call ok; no-cache ok. |
| 89 |
'ARRAY_A' |
| 90 |
); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
public static function create( |
| 95 |
$app_name, |
| 96 |
$app_title, |
| 97 |
$app_type, |
| 98 |
$app_settings |
| 99 |
) { |
| 100 |
|
| 101 |
global $wpdb; |
| 102 |
if ( 1 === $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- plugin table |
| 103 |
static::get_base_table_name(), |
| 104 |
array( |
| 105 |
'app_name' => $app_name, |
| 106 |
'app_title' => $app_title, |
| 107 |
'app_type' => $app_type, |
| 108 |
'app_settings' => $app_settings, |
| 109 |
) |
| 110 |
) |
| 111 |
) { |
| 112 |
return array( |
| 113 |
'app_id' => $wpdb->insert_id, |
| 114 |
'msg' => '', |
| 115 |
); |
| 116 |
} else { |
| 117 |
return array( |
| 118 |
'app_id' => false, |
| 119 |
'msg' => $wpdb->last_error, |
| 120 |
); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public static function delete( $app_id ) { |
| 125 |
|
| 126 |
global $wpdb; |
| 127 |
return $wpdb->delete( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table |
| 128 |
static::get_base_table_name(), |
| 129 |
array( |
| 130 |
'app_id' => $app_id, |
| 131 |
) |
| 132 |
); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
public static function update( |
| 137 |
$app_id, |
| 138 |
$app_name, |
| 139 |
$app_title, |
| 140 |
$app_type, |
| 141 |
$app_settings, |
| 142 |
$app_add_to_menu |
| 143 |
) { |
| 144 |
|
| 145 |
global $wpdb; |
| 146 |
$wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table |
| 147 |
static::get_base_table_name(), |
| 148 |
array( |
| 149 |
'app_name' => $app_name, |
| 150 |
'app_title' => $app_title, |
| 151 |
'app_type' => $app_type, |
| 152 |
'app_settings' => $app_settings, |
| 153 |
'app_add_to_menu' => $app_add_to_menu, |
| 154 |
), |
| 155 |
array( |
| 156 |
'app_id' => $app_id, |
| 157 |
) |
| 158 |
); |
| 159 |
|
| 160 |
return $wpdb->last_error; |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
public static function copy_all( |
| 165 |
$app |
| 166 |
) { |
| 167 |
|
| 168 |
$app_id = $app[0]['app_id']; |
| 169 |
$app_name = $app[0]['app_name']; |
| 170 |
|
| 171 |
// Determine new app name |
| 172 |
$copy_nr = 1; |
| 173 |
$new_app_name = "{$app_name}_{$copy_nr} (copy)"; |
| 174 |
$app_exists = self::get_by_name( $new_app_name ); |
| 175 |
while ( false !== $app_exists ) { |
| 176 |
$copy_nr++; |
| 177 |
$new_app_name = "{$app_name}_{$copy_nr} (copy)"; |
| 178 |
$app_exists = self::get_by_name( $new_app_name ); |
| 179 |
} |
| 180 |
|
| 181 |
// Copy app |
| 182 |
global $wpdb; |
| 183 |
if ( 1 === $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- plugin table |
| 184 |
static::get_base_table_name(), |
| 185 |
array( |
| 186 |
'app_name' => $new_app_name, |
| 187 |
'app_title' => $app[0]['app_title'], |
| 188 |
'app_type' => $app[0]['app_type'], |
| 189 |
'app_settings' => $app[0]['app_settings'], |
| 190 |
'app_theme' => $app[0]['app_theme'], |
| 191 |
) |
| 192 |
) |
| 193 |
) { |
| 194 |
$new_app_id = $wpdb->insert_id; |
| 195 |
|
| 196 |
// Copy containers |
| 197 |
WPDA_App_Container_Model::copy( $app_id, $new_app_id ); |
| 198 |
|
| 199 |
if ( 5 === $app[0]['app_type'] || '5' === $app[0]['app_type'] ) { |
| 200 |
// App container, copy details |
| 201 |
$apps = WPDA_App_Apps_Model::select_all( $app_id ); |
| 202 |
foreach ( $apps as $app ) { |
| 203 |
$detail_app = self::get_by_id( $app['app_id_detail'] ); |
| 204 |
if ( false !== $detail_app ) { |
| 205 |
$detail_app_ids = static::copy_all($detail_app); |
| 206 |
if ( false !== $detail_app_ids['app_id'] ) { |
| 207 |
WPDA_App_Apps_Model::create($new_app_id, $detail_app_ids['app_id'], $app['seq_nr']); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return array( |
| 214 |
'app_id' => $new_app_id, |
| 215 |
'msg' => '', |
| 216 |
); |
| 217 |
} else { |
| 218 |
return array( |
| 219 |
'app_id' => false, |
| 220 |
'msg' => $wpdb->last_error, |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
public static function copy( |
| 227 |
$app_id |
| 228 |
) { |
| 229 |
|
| 230 |
$app = self::get_by_id( $app_id ); |
| 231 |
if ( false === $app ) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
return static::copy_all( $app ); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
public static function update_theme( |
| 240 |
$app_id, |
| 241 |
$app_theme |
| 242 |
) { |
| 243 |
|
| 244 |
global $wpdb; |
| 245 |
$wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table |
| 246 |
static::get_base_table_name(), |
| 247 |
array( |
| 248 |
'app_theme' => $app_theme, |
| 249 |
), |
| 250 |
array( |
| 251 |
'app_id' => $app_id, |
| 252 |
) |
| 253 |
); |
| 254 |
|
| 255 |
return $wpdb->last_error; |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
} |