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

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