PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.63
Timetics – Appointment Booking Calendar & Scheduling v1.0.63
1.0.62 1.0.63 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 All 64 releases
timetics / vendor / themewinter / email-notification-sdk / src / Flow / Flow.php

Flow.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.63, at vendor/themewinter/email-notification-sdk/src/Flow/Flow.php

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