| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
/** |
| 5 |
* Theme Repository — ALL theme database operations in one place. |
| 6 |
*/ |
| 7 |
class Wpda_Countdown_Theme_Repository { |
| 8 |
|
| 9 |
private $table; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
global $wpdb; |
| 13 |
$this->table = $wpdb->prefix . 'wpda_contdown_theme'; |
| 14 |
} |
| 15 |
|
| 16 |
public function get_table_name() { |
| 17 |
return $this->table; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Get a single theme by ID. Returns null if the table doesn't exist yet. |
| 22 |
*/ |
| 23 |
public function find( $id ) { |
| 24 |
if ( ! $this->table_exists() ) return null; |
| 25 |
global $wpdb; |
| 26 |
return $wpdb->get_row( |
| 27 |
$wpdb->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id ) |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
public function find_data( $id ) { |
| 32 |
$row = $this->find( $id ); |
| 33 |
if ( ! $row ) return null; |
| 34 |
$data = json_decode( $row->option_value, true ); |
| 35 |
return is_array( $data ) ? $data : array(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get the default theme row. Returns null if the table doesn't exist yet. |
| 40 |
*/ |
| 41 |
public function find_default() { |
| 42 |
if ( ! $this->table_exists() ) return null; |
| 43 |
global $wpdb; |
| 44 |
return $wpdb->get_row( "SELECT * FROM {$this->table} WHERE `default` = 1" ); |
| 45 |
} |
| 46 |
|
| 47 |
public function find_or_default( $id ) { |
| 48 |
$theme = $this->find( $id ); |
| 49 |
if ( ! $theme ) { |
| 50 |
$theme = $this->find_default(); |
| 51 |
} |
| 52 |
return $theme; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get all themes (raw rows). Returns [] if the table doesn't exist yet. |
| 57 |
*/ |
| 58 |
public function all() { |
| 59 |
if ( ! $this->table_exists() ) return array(); |
| 60 |
global $wpdb; |
| 61 |
$rows = $wpdb->get_results( "SELECT id, name, option_value, `default` FROM {$this->table} ORDER BY id ASC" ); |
| 62 |
return is_array( $rows ) ? $rows : array(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get id => "Name (Type)" map for dropdowns. |
| 67 |
* |
| 68 |
* The type suffix helps users identify what design each theme uses |
| 69 |
* (Standard / Vertical / Circle / Flip) without having to open each one. |
| 70 |
* Stored names in the DB are unchanged — this is display-only. |
| 71 |
*/ |
| 72 |
public function all_names() { |
| 73 |
static $type_labels = array( |
| 74 |
'standart' => 'Standard', |
| 75 |
'vertical' => 'Vertical', |
| 76 |
'circle' => 'Circle', |
| 77 |
'flip' => 'Flip', |
| 78 |
); |
| 79 |
$list = array(); |
| 80 |
foreach ( $this->all() as $row ) { |
| 81 |
$data = json_decode( $row->option_value, true ); |
| 82 |
$type = ( is_array( $data ) && isset( $data['countdown_type'] ) ) ? $data['countdown_type'] : 'standart'; |
| 83 |
$label = isset( $type_labels[ $type ] ) ? $type_labels[ $type ] : 'Standard'; |
| 84 |
$list[ $row->id ] = $row->name . ' (' . $label . ')'; |
| 85 |
} |
| 86 |
return $list; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Insert a new theme. |
| 91 |
* |
| 92 |
* @param string $name Theme display name. |
| 93 |
* @param array $data Settings array (will be JSON-encoded). |
| 94 |
* @param bool $default Whether this is the default theme. |
| 95 |
* @return int Inserted row ID, or 0 on failure. |
| 96 |
*/ |
| 97 |
public function insert( $name, array $data, $default = false ) { |
| 98 |
global $wpdb; |
| 99 |
$ok = $wpdb->insert( |
| 100 |
$this->table, |
| 101 |
array( |
| 102 |
'name' => $name ?: 'Unnamed', |
| 103 |
'option_value' => wp_json_encode( $data ), |
| 104 |
'default' => $default ? 1 : 0, |
| 105 |
), |
| 106 |
array( '%s', '%s', '%d' ) |
| 107 |
); |
| 108 |
return $ok ? $wpdb->insert_id : 0; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Update an existing theme. |
| 113 |
* |
| 114 |
* @return int|false Rows affected, 0 if data unchanged, false on DB error. |
| 115 |
*/ |
| 116 |
public function update( $id, $name, array $data ) { |
| 117 |
global $wpdb; |
| 118 |
return $wpdb->update( |
| 119 |
$this->table, |
| 120 |
array( |
| 121 |
'name' => $name, |
| 122 |
'option_value' => wp_json_encode( $data ), |
| 123 |
), |
| 124 |
array( 'id' => $id ), |
| 125 |
array( '%s', '%s' ), |
| 126 |
array( '%d' ) |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Save — insert or update depending on whether $id is provided. |
| 132 |
* |
| 133 |
* @return int The row ID on success, 0 on failure. |
| 134 |
*/ |
| 135 |
public function save( $id, $name, array $data ) { |
| 136 |
if ( $id ) { |
| 137 |
return $this->update( $id, $name, $data ) !== false ? (int) $id : 0; |
| 138 |
} |
| 139 |
return $this->insert( $name, $data ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Delete a theme by ID. |
| 144 |
*/ |
| 145 |
public function delete( $id ) { |
| 146 |
global $wpdb; |
| 147 |
return (bool) $wpdb->delete( $this->table, array( 'id' => $id ), array( '%d' ) ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Set a theme as default (clears previous default). |
| 152 |
*/ |
| 153 |
public function set_default( $id ) { |
| 154 |
global $wpdb; |
| 155 |
$wpdb->update( $this->table, array( 'default' => 0 ), array( 'default' => 1 ) ); |
| 156 |
return (bool) $wpdb->update( $this->table, array( 'default' => 1 ), array( 'id' => $id ) ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Check if any default theme exists. Returns its ID or false. |
| 161 |
*/ |
| 162 |
public function get_default_id() { |
| 163 |
global $wpdb; |
| 164 |
$id = $wpdb->get_var( "SELECT id FROM {$this->table} WHERE `default` = 1" ); |
| 165 |
return $id ? (int) $id : false; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Count total themes. |
| 170 |
*/ |
| 171 |
public function count() { |
| 172 |
global $wpdb; |
| 173 |
return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$this->table}" ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Get the highest ID. |
| 178 |
*/ |
| 179 |
public function max_id() { |
| 180 |
global $wpdb; |
| 181 |
return (int) $wpdb->get_var( "SELECT MAX(id) FROM {$this->table}" ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Check if the table exists. |
| 186 |
*/ |
| 187 |
public function table_exists() { |
| 188 |
global $wpdb; |
| 189 |
return $wpdb->get_var( "SHOW TABLES LIKE '{$this->table}'" ) === $this->table; |
| 190 |
} |
| 191 |
} |
| 192 |
|