PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.61
Timetics – Appointment Booking Calendar & Scheduling v1.0.61
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.61, at base/post-model.php

179 lines 3.9 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 /* translators: %s: Property name */
51 throw new \Exception( sprintf( esc_html__( 'Undefined property %s', 'timetics' ), esc_attr( $name ) ) );
52 }
53
54 /**
55 * Set dynamic property
56 *
57 * @param string $key
58 * @param mixed $value
59 *
60 * @return void
61 */
62 public function __set( $key, $value ) {
63 if ( ! isset( $this->data[$key] ) ) {
64 /* translators: %s: Property name */
65 throw new \Exception( sprintf( esc_html__( 'Undefined property %s', 'timetics' ), esc_attr( $key ) ) );
66 }
67
68 $this->data[$key] = $value;
69 }
70
71 /**
72 * Debug info for current object
73 *
74 * @return mixed
75 */
76 public function __debugInfo() {
77 return $this->data;
78 }
79
80 /**
81 * Handle dynamic static method call
82 *
83 * @param string $method Method name for dynamic call
84 * @param mixed $params method params
85 *
86 * @return string
87 */
88 public static function __callStatic( $method, $params ) {
89 return static::forwardCallToStatic( new static , $method, $params );
90 }
91
92 /**
93 * Handle dynamic method call
94 *
95 * @param string $method Method name for dynamic call
96 * @param mixed $params method params
97 *
98 * @return string
99 */
100 public function __call( $method, $params ) {
101 return static::forwardCallToStatic( ( new static ), $method, $params );
102 }
103
104 /**
105 * Get property of current post
106 *
107 * @return mixed
108 */
109 public function get_prop( $name = '' ) {
110 $key = $this->prefix . $name;
111
112 return get_post_meta( $this->id, $key, true );
113 }
114
115 /**
116 * Convert object property to an Array
117 *
118 * @return array
119 */
120 public function toArray() {
121 $items = [];
122 $data = $this->data;
123 $items['id'] = $this->id;
124
125 unset( $data['id'] );
126
127 foreach ( $data as $key => $value ) {
128 $prop = $this->get_prop( $key );
129
130 $items[$key] = '' === $prop ? $value : $prop;
131 }
132
133 return $items;
134 }
135
136 /**
137 * Get all models
138 *
139 * @return array
140 */
141 public static function all( $data = [] ) {
142 $models = [];
143 $model = new static();
144 $meta = [];
145
146 if ( $data ) {
147 foreach ( $data as $key => $value ) {
148 $meta_data = [
149 'key' => $model->prefix . $key,
150 'value' => $value,
151 'compare' => '=',
152 ];
153
154 $meta[] = $meta_data;
155 }
156 }
157
158 $args = [
159 'post_type' => $model->post_type,
160 ];
161
162 if ( $meta ) {
163 $meta['relation'] = 'AND';
164 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query is necessary for filtering posts by meta fields
165 $args['meta_query'] = $meta;
166 }
167
168 $posts = get_posts( $args );
169
170 foreach ( $posts as $post ) {
171 $object = new static();
172 $model->load( $object, $post );
173 $models[] = $object;
174 }
175
176 return $models;
177 }
178 }
179