PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.10
Timetics – Appointment Booking Calendar & Scheduling v1.0.10
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / core / services / service.php

service.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.10, at core/services/service.php

303 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Service
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Services;
8
9 use WP_Term_Query;
10
11 class Service {
12
13 /**
14 * Store service taxonomy
15 *
16 * @var string
17 */
18 protected $taxonomy = 'timetics-service';
19
20 /**
21 * Store service taxonomy meta
22 *
23 * @var string
24 */
25 protected $meta_prefix = '_tt_service_';
26
27 /**
28 * Store taxonomy id
29 *
30 * @var integer
31 */
32 protected $id;
33
34 /**
35 * Store service category data
36 *
37 * @var array
38 */
39 protected $data = [
40 'name' => '',
41 'parent' => 0,
42 'duration' => '',
43 'location' => '',
44 'staff' => '',
45 'seat' => '',
46 'price' => 0,
47 'color' => '',
48 ];
49
50 /**
51 * Store WP_Error
52 *
53 * @var Object
54 */
55 public $error;
56
57 /**
58 * Service Constructor
59 *
60 * @return void
61 */
62 public function __construct( $service = 0 ) {
63 if ( $service instanceof self ) {
64 $this->set_id( $service->get_id() );
65 } elseif ( ! empty( $service->ID ) ) {
66 $this->set_id( $service->ID );
67 } elseif ( is_numeric( $service ) && $service > 0 ) {
68 $this->set_id( $service );
69 }
70 }
71
72 // Getter.
73
74 /**
75 * Get service id
76 *
77 * @return integer
78 */
79 public function get_id() {
80 return $this->id;
81 }
82
83 /**
84 * Get service name
85 *
86 * @return string
87 */
88 public function get_name() {
89 return $this->get_prop( 'name' );
90 }
91
92 /**
93 * Get service parent id
94 *
95 * @return integer
96 */
97 public function get_parent() {
98 return $this->get_prop( 'parent' );
99 }
100
101 /**
102 * Get duration
103 *
104 * @return string
105 */
106 public function get_duration() {
107 return $this->get_prop( 'duration' );
108 }
109
110 /**
111 * Get location
112 *
113 * @return string
114 */
115 public function get_location() {
116 return $this->get_prop( 'location' );
117 }
118
119 /**
120 * Get service price
121 *
122 * @return float
123 */
124 public function get_price() {
125 return $this->get_prop( 'price' );
126 }
127
128 /**
129 * Get seat
130 *
131 * @return string
132 */
133 public function get_seat() {
134 return $this->get_prop( 'seat' );
135 }
136
137 /**
138 * Get staff id
139 *
140 * @return integer
141 */
142 public function get_staff() {
143 return $this->get_prop( 'staff' );
144 }
145
146 /**
147 * Get service color
148 *
149 * @return string
150 */
151 public function get_color() {
152 return $this->get_prop( 'color' );
153 }
154
155 /**
156 * Get link
157 *
158 * @return string
159 */
160 public function get_link() {
161 return get_term_link( $this->id );
162 }
163
164 /**
165 * Get single data.
166 *
167 * @param string $prop Service meta key
168 *
169 * @return mixed
170 */
171 private function get_prop( $prop ) {
172 return $this->get_metadata( $prop );
173 }
174
175 /**
176 * Get service meta data
177 *
178 * @param string $prop Service metadata
179 *
180 * @return mixed
181 */
182 private function get_metadata( $prop ) {
183 $key = $this->meta_prefix . $prop;
184
185 return get_term_meta( $this->id, $key, true );
186 }
187
188 /**
189 * Get all services
190 *
191 * @param array
192 *
193 * @return array
194 */
195 public static function all( $args = [] ) {
196 $term = new WP_Term_Query(
197 [
198 'taxonomy' => 'timetics-service',
199 'number' => $args['per_page'],
200 'offset' => $args['paged'] - 1,
201 'hide_empty' => false,
202 'count' => true,
203 ]
204 );
205
206 return $term->terms ? $term->terms : [];
207 }
208
209 // Setter.
210
211 /**
212 * Set service id
213 *
214 * @param integer $id
215 *
216 * @return void
217 */
218 public function set_id( $id ) {
219 $this->id = $id;
220 }
221
222 /**
223 * Set props
224 *
225 * @param array $args Service args
226 *
227 * @return void
228 */
229 public function set_props( $args = [] ) {
230 $this->data = wp_parse_args( $args, $this->data );
231 }
232
233 /**
234 * Save service
235 *
236 * @return void
237 */
238 public function save() {
239 if ( $this->id ) {
240 $term = wp_update_term(
241 $this->id, $this->taxonomy, [
242 'name' => $this->data['name'],
243 'parent' => $this->data['parent'],
244 ]
245 );
246 } else {
247 $term = wp_insert_term(
248 $this->data['name'], $this->taxonomy, [
249 'parent' => $this->data['parent'],
250 ]
251 );
252 }
253
254 if ( is_wp_error( $term ) ) {
255 $this->error = $term;
256 } else {
257 $this->set_id( $term['term_id'] );
258 $this->save_metadata();
259 }
260 }
261
262 /**
263 * Save service meta data
264 *
265 * @return void
266 */
267 public function save_metadata() {
268 foreach ( $this->data as $key => $value ) {
269 $meta_key = $this->meta_prefix . $key;
270
271 update_term_meta( $this->id, $meta_key, $value );
272 }
273 }
274
275 /**
276 * Delete service
277 *
278 * @return bool
279 */
280 public function delete() {
281 $delete = wp_delete_term( $this->id, $this->taxonomy );
282
283 if ( is_wp_error( $delete ) ) {
284 $this->error = $delete;
285 }
286
287 return $delete;
288 }
289
290 /**
291 * Check a service is valid
292 *
293 * @return bool
294 */
295 public function is_service() {
296 if ( term_exists( $this->id, $this->taxonomy ) ) {
297 return true;
298 }
299
300 return false;
301 }
302 }
303