PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.49
Timetics – Appointment Booking Calendar & Scheduling v1.0.49
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 / base / post-model.php

post-model.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.49, at base/post-model.php

176 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Model Class
4 *
5 * @package Timetics
6 */
7
8 namespace Timetics\Base;
9
10 use Timetics\Base\ForwardCalls;
11
12 /**
13 * Class Post Model
14 */
15 abstract class PostModel {
16 use ForwardCalls;
17
18 /**
19 * Store post type
20 *
21 * @var string
22 */
23 public $post_type;
24 /**
25 * Store prefix
26 *
27 * @var string
28 */
29 public $prefix;
30
31 /**
32 * Store data for the current post
33 *
34 * @var array
35 */
36 public $data = [];
37
38 /**
39 * Get the post property dynamically
40 *
41 * @param string $name Property name of the current post
42 *
43 * @return mixed
44 */
45 public function __get( $name ) {
46 if ( isset( $this->data[$name] ) ) {
47 return $this->data[$name];
48 }
49
50 throw new \Exception( sprintf( esc_html__( 'Undefined property %s', 'timetics' ), esc_attr( $name ) ) );
51 }
52
53 /**
54 * Set dynamic property
55 *
56 * @param string $key
57 * @param mixed $value
58 *
59 * @return void
60 */
61 public function __set( $key, $value ) {
62 if ( ! isset( $this->data[$key] ) ) {
63 throw new \Exception( sprintf( esc_html__( 'Undefined property %s', 'timetics' ), esc_attr( $key ) ) );
64 }
65
66 $this->data[$key] = $value;
67 }
68
69 /**
70 * Debug info for current object
71 *
72 * @return mixed
73 */
74 public function __debugInfo() {
75 return $this->data;
76 }
77
78 /**
79 * Handle dynamic static method call
80 *
81 * @param string $method Method name for dynamic call
82 * @param mixed $params method params
83 *
84 * @return string
85 */
86 public static function __callStatic( $method, $params ) {
87 return static::forwardCallToStatic( new static , $method, $params );
88 }
89
90 /**
91 * Handle dynamic method call
92 *
93 * @param string $method Method name for dynamic call
94 * @param mixed $params method params
95 *
96 * @return string
97 */
98 public function __call( $method, $params ) {
99 return static::forwardCallToStatic( ( new static ), $method, $params );
100 }
101
102 /**
103 * Get property of current post
104 *
105 * @return mixed
106 */
107 public function get_prop( $name = '' ) {
108 $key = $this->prefix . $name;
109
110 return get_post_meta( $this->id, $key, true );
111 }
112
113 /**
114 * Convert object property to an Array
115 *
116 * @return array
117 */
118 public function toArray() {
119 $items = [];
120 $data = $this->data;
121 $items['id'] = $this->id;
122
123 unset( $data['id'] );
124
125 foreach ( $data as $key => $value ) {
126 $prop = $this->get_prop( $key );
127
128 $items[$key] = '' === $prop ? $value : $prop;
129 }
130
131 return $items;
132 }
133
134 /**
135 * Get all models
136 *
137 * @return array
138 */
139 public static function all( $data = [] ) {
140 $models = [];
141 $model = new static();
142 $meta = [];
143
144 if ( $data ) {
145 foreach ( $data as $key => $value ) {
146 $meta_data = [
147 'key' => $model->prefix . $key,
148 'value' => $value,
149 'compare' => '=',
150 ];
151
152 $meta[] = $meta_data;
153 }
154 }
155
156 $args = [
157 'post_type' => $model->post_type,
158 ];
159
160 if ( $meta ) {
161 $meta['relation'] = 'AND';
162 $args['meta_query'] = $meta;
163 }
164
165 $posts = get_posts( $args );
166
167 foreach ( $posts as $post ) {
168 $object = new static();
169 $model->load( $object, $post );
170 $models[] = $object;
171 }
172
173 return $models;
174 }
175 }
176