PluginProbe
UpStream: a Project Management Plugin for WordPress / 2.1.0
UpStream: a Project Management Plugin for WordPress v2.1.0
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / model / class-upstream-model-post-object.php

class-upstream-model-post-object.php in UpStream: a Project Management Plugin for WordPress 2.1.0, at includes/model/class-upstream-model-post-object.php

252 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UpStream_Model_Post_Object
4 *
5 * WordPress Coding Standart (WCS) note:
6 * All camelCase methods and object properties on this file are not converted to snake_case,
7 * because it being used (heavily) on another add-on plugins.
8 *
9 * @package UpStream
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class UpStream_Model_Post_Object.
19 */
20 class UpStream_Model_Post_Object extends UpStream_Model_Object {
21
22 /**
23 * CategoryIds
24 *
25 * @var array
26 */
27 protected $categoryIds = array(); // phpcs:ignore
28
29 /**
30 * ParentId
31 *
32 * @var int
33 */
34 protected $parentId = 0; // phpcs:ignore
35
36 /**
37 * PostType
38 *
39 * @var string
40 */
41 protected $postType = 'post'; // phpcs:ignore
42
43 /**
44 * UpStream_Model_Post_Object constructor.
45 *
46 * @param mixed $id id.
47 * @param mixed $fields fields.
48 * @return void
49 */
50 public function __construct( $id, $fields ) {
51 parent::__construct( $id );
52
53 if ( $id > 0 ) {
54 $this->load( $id, $fields );
55 }
56 }
57
58 /**
59 * Load
60 *
61 * @param mixed $id id.
62 * @param mixed $fields fields.
63 * @throws UpStream_Model_ArgumentException Exception.
64 * @return void
65 */
66 protected function load( $id, $fields ) {
67 $post = get_post( $id );
68
69 if ( ! $post ) {
70 throw new UpStream_Model_ArgumentException(
71 sprintf(
72 // translators: %s: object id.
73 __( 'Object with ID %s does not exist.', 'upstream' ),
74 $id
75 )
76 );
77 }
78
79 $metadata = get_post_meta( $id );
80 if ( empty( $metadata ) ) {
81 return;
82 }
83
84 if ( ! isset( $post->post_title ) ) {
85 throw new UpStream_Model_ArgumentException(
86 sprintf(
87 // translators: %s: object name.
88 __( 'Object %s title does not exist.', 'upstream' ),
89 $id
90 )
91 );
92 }
93 $this->title = $post->post_title;
94
95 if ( ! isset( $post->post_author ) ) {
96 throw new UpStream_Model_ArgumentException(
97 sprintf(
98 // translators: %s: object author.
99 __( 'Object %s author does not exist.', 'upstream' ),
100 $id
101 )
102 );
103 }
104 $this->createdBy = $post->post_author; // phpcs:ignore
105
106 if ( ! isset( $post->post_content ) ) {
107 throw new UpStream_Model_ArgumentException(
108 sprintf(
109 // translators: %s: object name.
110 __( 'Object %s content does not exist.', 'upstream' ),
111 $id
112 )
113 );
114 }
115 $this->description = $post->post_content;
116
117 foreach ( $fields as $field => $input ) {
118 if ( is_string( $input ) ) {
119 if ( ! empty( $metadata[ $input ] ) ) {
120
121 if ( count( $metadata[ $input ] ) > 0 ) {
122 $this->{$field} = $metadata[ $input ][0];
123 }
124 }
125 } elseif ( $input instanceof Closure ) {
126 $this->{$field} = $input( $metadata );
127 }
128 }
129
130 $data_to_load = array();
131 foreach ( $metadata as $key => $val ) {
132 if ( is_array( $val ) ) {
133 $data_to_load[ $key ] = $val[0];
134 }
135 }
136
137 $this->additionalFields = apply_filters( // phpcs:ignore
138 'upstream_model_load_fields',
139 $this->additionalFields, // phpcs:ignore
140 $data_to_load,
141 $this->type,
142 $this->id
143 );
144 }
145
146 /**
147 * Store
148 *
149 * @throws UpStream_Model_ArgumentException Exception.
150 * @return void
151 */
152 protected function store() {
153 $res = null;
154
155 if ( $this->id > 0 ) {
156
157 $post_arr = array(
158 'ID' => $this->id,
159 'post_title' => ( null === $this->title ? '(New Item)' : $this->title ),
160 'post_content' => ( null === $this->description ? '' : $this->description ),
161 );
162
163 $res = wp_update_post( $post_arr, true );
164
165 } else {
166 $post_arr = array(
167 'post_title' => ( null === $this->title ? '(New Item)' : $this->title ),
168 'post_author' => $this->createdBy, // phpcs:ignore
169 'post_parent' => $this->parentId, // phpcs:ignore
170 'post_content' => ( null === $this->description ? '' : $this->description ),
171 'post_status' => 'publish',
172 'post_type' => $this->postType, // phpcs:ignore
173 );
174
175 $res = wp_insert_post( $post_arr, true );
176 }
177
178 if ( $res instanceof \WP_Error ) {
179 throw new UpStream_Model_ArgumentException(
180 sprintf(
181 // translators: %s: post id.
182 __( 'Could not load post with ID %s.', 'upstream' ),
183 $this->id
184 )
185 );
186 } else {
187 $this->id = (int) $res;
188 }
189
190 $data_to_store = array();
191 $data_to_store = apply_filters(
192 'upstream_model_store_fields',
193 $data_to_store,
194 $this->additionalFields, // phpcs:ignore
195 $this->type,
196 $this->id
197 );
198
199 foreach ( $data_to_store as $key => $value ) {
200 update_post_meta( $this->id, $key, $value );
201 }
202
203 }
204
205 /**
206 * Get
207 *
208 * @param mixed $property property.
209 */
210 public function __get( $property ) {
211 $property = apply_filters( 'upstream_wcs_model_variable', $property );
212
213 switch ( $property ) {
214
215 case 'parentId':
216 return $this->{$property};
217
218 default:
219 return parent::__get( $property );
220
221 }
222 }
223
224 /**
225 * Set
226 *
227 * @param mixed $property property.
228 * @param mixed $value value.
229 * @throws UpStream_Model_ArgumentException Exception.
230 * @return void
231 */
232 public function __set( $property, $value ) {
233 $property = apply_filters( 'upstream_wcs_model_variable', $property );
234
235 switch ( $property ) {
236
237 case 'id':
238 if ( ! filter_var( $value, FILTER_VALIDATE_INT ) ) {
239 throw new UpStream_Model_ArgumentException( __( 'ID must be a valid numeric.', 'upstream' ) );
240 }
241 $this->{$property} = $value;
242 break;
243
244 default:
245 parent::__set( $property, $value );
246 break;
247
248 }
249 }
250
251 }
252