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.php

post.php in Timetics – Appointment Booking Calendar & Scheduling trunk, at base/post.php

147 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main post class
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Base;
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * Class Post
13 */
14 class Post {
15 /**
16 * Store post object
17 *
18 * @var Object
19 */
20 private $post;
21
22 /**
23 * Post class constructor
24 *
25 * @return void
26 */
27 public function __construct( PostModel $post ) {
28 $this->post = $post;
29 }
30
31 /**
32 * Create post
33 *
34 * @param array $args
35 *
36 * @return PostModel
37 */
38 public function create( $args = [] ) {
39 $defaults = [
40 'post_title' => '',
41 'post_type' => $this->post->post_type,
42 'post_status' => 'publish',
43 'post_author' => 1,
44 ];
45
46 $args = wp_parse_args( $args, $defaults );
47 $post_id = wp_insert_post( $args );
48
49 $this->load( $this->post, $post_id );
50 $this->save_meta( $args );
51
52 return $this->post;
53 }
54
55 /**
56 * Update post
57 *
58 * @param array $args
59 *
60 * @return PostModel
61 */
62 public function update( $post_id, $args = [] ) {
63 $defaults = [
64 'post_title' => '',
65 'post_type' => $this->post->post_type,
66 'post_status' => 'publish',
67 'ID' => $post_id,
68 'post_author' => 1,
69 ];
70
71 $args = wp_parse_args( $args, $defaults );
72
73 $post_id = wp_update_post( $args );
74
75 $this->load( $this->post, $post_id );
76 $this->save_meta( $args );
77
78 return $this->post;
79 }
80
81 /**
82 * Delete post
83 *
84 * @return boolean
85 */
86 public function delete( $post_id = 0 ) {
87 return wp_delete_post( $post_id, true );
88 }
89
90 /**
91 * Update post meta data
92 *
93 * @return void
94 */
95 public function save_meta( $props = [] ) {
96 foreach ( $props as $key => $value ) {
97 $meta_key = $this->post->prefix . $key;
98
99 // If a meta key exists on that post then update the post meta.
100 if ( array_key_exists( $key, $this->post->data ) ) {
101 update_post_meta( $this->post->id, $meta_key, $value );
102 }
103 }
104 }
105
106 /**
107 * Load all properties every model
108 *
109 * @param PostModel $object Post model object
110 * @param Object $post WordPress post object
111 *
112 * @return void
113 */
114 public function load( $object, $post ) {
115
116 if ( is_int( $post ) ) {
117 $post = get_post( $post );
118 }
119
120 $data = $object->data;
121 $object->id = $post->ID;
122 unset( $data['id'] );
123
124 foreach ( $data as $key => $value ) {
125 $prop = $object->get_prop( $key );
126 $object->$key = '' === $prop ? $value : $prop;
127 }
128 }
129
130 /**
131 * Get post a single post
132 *
133 * @param integer $post_id
134 *
135 * @return PostModel
136 */
137 public function find( $post_id ) {
138 $post = get_post( $post_id );
139
140 if ( $post ) {
141 $this->load( $this->post, $post );
142 }
143
144 return $this->post;
145 }
146 }
147