PluginProbe
Countdown Timer – Widget Countdown / trunk
Countdown Timer – Widget Countdown vtrunk
2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 All 182 releases
widget-countdown / includes / Repository / TimerRepository.php

TimerRepository.php in Countdown Timer – Widget Countdown trunk, at includes/Repository/TimerRepository.php

152 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit;
3
4 /**
5 * Timer Repository — ALL timer database operations in one place.
6 *
7 * Every file that needs timer data (admin, frontend, widget, integrations)
8 * goes through this class. No more scattered $wpdb calls.
9 */
10 class Wpda_Countdown_Timer_Repository {
11
12 private $table;
13
14 public function __construct() {
15 global $wpdb;
16 $this->table = $wpdb->prefix . 'wpda_contdown_timer';
17 }
18
19 public function get_table_name() {
20 return $this->table;
21 }
22
23 /**
24 * Get a single timer by ID. Returns null if the table doesn't exist yet.
25 */
26 public function find( $id ) {
27 if ( ! $this->table_exists() ) return null;
28 global $wpdb;
29 return $wpdb->get_row(
30 $wpdb->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id )
31 );
32 }
33
34 /**
35 * Get decoded timer data merged with defaults.
36 *
37 * @return array|null Merged settings array, or null if not found.
38 */
39 public function find_data( $id ) {
40 $row = $this->find( $id );
41 if ( ! $row ) return null;
42 $data = json_decode( $row->option_value, true );
43 return is_array( $data ) ? $data : array();
44 }
45
46 /**
47 * Get all timers (raw rows). Returns [] if the table doesn't exist yet.
48 */
49 public function all() {
50 if ( ! $this->table_exists() ) return array();
51 global $wpdb;
52 $rows = $wpdb->get_results( "SELECT id, name, option_value FROM {$this->table} ORDER BY id ASC" );
53 return is_array( $rows ) ? $rows : array();
54 }
55
56 /**
57 * Get id => name map for dropdowns.
58 *
59 * @return array [ id => name, ... ]
60 */
61 public function all_names() {
62 $list = array();
63 foreach ( $this->all() as $row ) {
64 $list[ $row->id ] = $row->name;
65 }
66 return $list;
67 }
68
69 /**
70 * Insert a new timer.
71 *
72 * @param string $name Timer display name.
73 * @param array $data Settings array (will be JSON-encoded).
74 * @return int Inserted row ID, or 0 on failure.
75 */
76 public function insert( $name, array $data ) {
77 global $wpdb;
78 $ok = $wpdb->insert(
79 $this->table,
80 array(
81 'name' => $name ?: 'Unnamed',
82 'option_value' => wp_json_encode( $data ),
83 ),
84 array( '%s', '%s' )
85 );
86 return $ok ? $wpdb->insert_id : 0;
87 }
88
89 /**
90 * Update an existing timer.
91 *
92 * @return int|false Rows affected, 0 if data unchanged, false on DB error.
93 */
94 public function update( $id, $name, array $data ) {
95 global $wpdb;
96 return $wpdb->update(
97 $this->table,
98 array(
99 'name' => $name,
100 'option_value' => wp_json_encode( $data ),
101 ),
102 array( 'id' => $id ),
103 array( '%s', '%s' ),
104 array( '%d' )
105 );
106 }
107
108 /**
109 * Save — insert or update depending on whether $id is provided.
110 *
111 * @return int The row ID on success, 0 on failure.
112 */
113 public function save( $id, $name, array $data ) {
114 if ( $id ) {
115 return $this->update( $id, $name, $data ) !== false ? (int) $id : 0;
116 }
117 return $this->insert( $name, $data );
118 }
119
120 /**
121 * Delete a timer by ID.
122 */
123 public function delete( $id ) {
124 global $wpdb;
125 return (bool) $wpdb->delete( $this->table, array( 'id' => $id ), array( '%d' ) );
126 }
127
128 /**
129 * Count total timers.
130 */
131 public function count() {
132 global $wpdb;
133 return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$this->table}" );
134 }
135
136 /**
137 * Get the highest ID (used after insert to redirect to edit).
138 */
139 public function max_id() {
140 global $wpdb;
141 return (int) $wpdb->get_var( "SELECT MAX(id) FROM {$this->table}" );
142 }
143
144 /**
145 * Check if the table exists in the database.
146 */
147 public function table_exists() {
148 global $wpdb;
149 return $wpdb->get_var( "SHOW TABLES LIKE '{$this->table}'" ) === $this->table;
150 }
151 }
152