PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.3
Booktics – Appointment Booking Calendar for Service Businesses v1.0.3
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 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.2 1.0.3 All 31 releases
booktics / trunk / vendor / themewinter / email-notification-sdk / src / Flow / Flow.php

Flow.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.3, at trunk/vendor/themewinter/email-notification-sdk/src/Flow/Flow.php

384 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ENS\Flow;
4
5 use ENS\Base\PostModel;
6 use ENS\Config;
7 use WP_Query;
8
9 /**
10 * Class Flow
11 *
12 * @since 1.0.0
13 *
14 * @package ENS
15 */
16 class Flow extends PostModel {
17
18 /**
19 * Store post type
20 *
21 * @since 1.0.0
22 *
23 * @var string $post_type
24 */
25 public $post_type;
26
27 /**
28 * Store id
29 *
30 * @since 1.0.0
31 *
32 * @var int $id
33 */
34 protected $id;
35
36 /**
37 * Store post metadata
38 *
39 * @since 1.0.0
40 *
41 * @var array $data
42 */
43 public $data = [
44 'name' => '',
45 'trigger' => '',
46 'flow_config' => '',
47 'status' => '',
48 ];
49
50 /**
51 * Store meta key prefix
52 *
53 * @since 1.0.0
54 *
55 * @var string $prefix
56 */
57 public $prefix;
58
59 /**
60 * Flow Constructor
61 *
62 * @since 1.0.0
63 *
64 * @param int $flow Optional. Default 0.
65 * @return void
66 */
67 public function __construct( $flow = 0 ) {
68 if ( $flow instanceof self ) {
69 $this->set_id( $flow->get_id() );
70 } elseif ( !empty( $flow->ID ) ) {
71 $this->set_id( $flow->ID );
72 } elseif ( is_numeric( $flow ) && $flow > 0 ) {
73 $this->set_id( $flow );
74 }
75
76 $prefix_for_cpt = Config::get( 'general_prefix' );
77 $this->post_type = $prefix_for_cpt . '-flow';
78
79 $this->prefix = '_' . $prefix_for_cpt . '_notification_flow_';
80 }
81
82 /**
83 * Get flow id.
84 *
85 * @since 1.0.0
86 *
87 * @return int
88 */
89 public function get_id() {
90 return $this->id;
91 }
92
93 /**
94 * Get flow name.
95 *
96 * @since 1.0.0
97 *
98 * @return string
99 */
100 public function get_name() {
101 return get_post_field( 'post_title', $this->id );
102 }
103
104 /**
105 * Get flow controls.
106 *
107 * @since 1.0.0
108 *
109 * @return string
110 */
111 public function get_trigger() {
112 return $this->get_prop( 'trigger' );
113 }
114
115 /**
116 * Get flow status
117 *
118 * @since 1.0.0
119 *
120 * @return integer
121 */
122 public function get_status() {
123 $post_status = get_post_status( $this->id );
124
125 return $post_status;
126 }
127
128 /**
129 * Get flow config
130 *
131 * @since 1.0.0
132 *
133 * @return integer
134 */
135 public function get_flow_config() {
136 return $this->get_prop( 'flow_config' );
137 }
138
139 /**
140 * Get flow data
141 *
142 * @since 1.0.0
143 *
144 * @param string $prop
145 *
146 * @return mixed
147 */
148 public function get_prop( $prop = '' ) {
149 return $this->get_metadata( $prop );
150 }
151
152 /**
153 * Get metadata
154 *
155 * @since 1.0.0
156 *
157 * @param string $prop Optional. Default empty string.
158 *
159 * @return mixed
160 */
161 private function get_metadata( $prop = '' ) {
162 $meta_key = $this->prefix . $prop;
163 return get_post_meta( $this->id, $meta_key, true );
164 }
165
166 /**
167 * Set flow id
168 *
169 * @since 1.0.0
170 *
171 * @param int $id
172 *
173 * @return void
174 */
175 public function set_id( $id ) {
176 $this->id = $id;
177 }
178
179 /**
180 * Set props
181 *
182 * @since 1.0.0
183 *
184 * @param array $data Metadata array. Default empty array.
185 *
186 * @return void
187 */
188 public function set_props( $data = [] ) {
189 $this->data = $data;
190 }
191
192 /**
193 * Save flow
194 *
195 * @since 1.0.0
196 *
197 * @return void
198 */
199 public function save() {
200 $args = [
201 'post_title' => $this->data['name'],
202 'post_type' => $this->post_type,
203 'post_status' => $this->data['status'],
204 'post_author' => get_current_user_id(),
205 ];
206
207 if ( !empty( $this->id ) ) {
208 $args['ID'] = $this->id;
209 }
210
211 $flow_id = wp_insert_post( $args );
212 if ( !is_wp_error( $flow_id ) ) {
213 $this->set_id( $flow_id );
214 $this->save_metadata();
215 }
216
217 return $flow_id;
218 }
219
220 /**
221 * Update notification flow meta data
222 *
223 * @since 1.0.0
224 *
225 * @return void
226 */
227 private function save_metadata() {
228 foreach ( $this->data as $key => $value ) {
229 if ( !in_array( $key, ['status', 'name'] ) ) {
230 if ( !array_key_exists( $key, $this->data ) ) {
231 continue;
232 }
233
234 $meta_key = $this->prefix . $key;
235
236 if ( !$value ) {
237 $value = $this->get_prop( $key );
238 }
239 update_post_meta( $this->id, $meta_key, $value );
240 }
241 }
242 }
243
244 /**
245 * Delete flow
246 *
247 * @since 1.0.0
248 *
249 * @return bool | WP_Error
250 */
251 public function delete() {
252 return wp_delete_post( $this->id, true );
253 }
254
255 /**
256 * Check the flow is valid or not
257 *
258 * @since 1.0.0
259 *
260 * @return bool
261 */
262 public function is_flow() {
263 $post = get_post( $this->id );
264
265 if ( $post && $this->post_type === $post->post_type ) {
266 return true;
267 }
268
269 return false;
270 }
271
272 /**
273 * Get all flows
274 *
275 * @since 1.0.0
276 *
277 * @param array $args Flow args. Default empty array.
278 *
279 * @return array
280 */
281 public static function all( $args = [] ) {
282 $prefix_for_cpt = '_' . Config::get( 'general_prefix' ) . '_notification_flow_';
283
284 $defaults = [
285 'post_type' => ( new self )->post_type,
286 'posts_per_page' => 20,
287 'paged' => 1,
288 'orderby' => 'ID',
289 'order' => 'DESC',
290 ];
291
292 $args = wp_parse_args( $args, $defaults );
293
294 if ( !empty( $args['trigger'] ) ) {
295 $args['meta_query'][] = [
296 'key' => $prefix_for_cpt . 'trigger',
297 'value' => $args['trigger'],
298 'compare' => '=',
299 ];
300 }
301
302 if ( !empty( $args['search_key'] ) ) {
303 $args['s'] = $args['search_key'];
304 }
305
306 $post = new WP_Query( $args );
307
308 return [
309 'total' => $post->found_posts,
310 'items' => $post->posts,
311 ];
312 }
313
314 /**
315 * clone flow
316 *
317 * @since 1.0.0
318 *
319 * @return void
320 */
321 public function clone () {
322 $this->set_props( $this->get_data() );
323
324 $this->set_id( 0 );
325 $this->save();
326 }
327
328 /**
329 * Fetch status wise total flow count
330 *
331 * @since 1.0.0
332 *
333 * @return array
334 */
335 public static function total_flows_group_by_status() {
336 $args = [
337 'post_type' => ( new self )->post_type,
338 'posts_per_page' => -1,
339 'post_status' => 'any',
340 ];
341
342 $query = new WP_Query( $args );
343
344 $all_status = ens_get_post_status();
345
346 $status_count = [];
347 foreach ( $all_status as $status ) {
348 $status_count[$status] = 0;
349 }
350
351 if ( $query->have_posts() ) {
352 while ( $query->have_posts() ) {
353 $query->the_post();
354 $post_id = get_the_ID();
355 $status = get_post_status( $post_id );
356 if ( isset( $status_count[$status] ) ) {
357 $status_count[$status]++;
358 } else {
359 $status_count[$status] = 1;
360 }
361 }
362
363 wp_reset_postdata();
364 }
365
366 return $status_count;
367 }
368
369 /**
370 * Get all data for a flow
371 *
372 * @since 1.0.0
373 *
374 * @return array
375 */
376 public function get_data() {
377 return [
378 'name' => $this->get_name(),
379 'trigger' => $this->get_trigger(),
380 'flow_config' => $this->get_flow_config(),
381 'status' => $this->get_status(),
382 ];
383 }
384 }