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