説明なし

Post.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Abstract_Model_Post
  10. */
  11. abstract class PUM_Abstract_Model_Post {
  12. /**
  13. * The current model version.
  14. *
  15. * Used for compatibility testing.
  16. * 1 - v1.0.0
  17. *
  18. * @var int
  19. */
  20. public $model_version = 1;
  21. /**
  22. * The version of the data currently stored for the current item.
  23. *
  24. * 1 - v1.0.0
  25. *
  26. * @var int
  27. */
  28. public $data_version;
  29. /**
  30. * The post ID
  31. */
  32. public $ID = 0;
  33. /**
  34. * Declare the default properties in WP_Post as we can't extend it
  35. */
  36. public $post_author = 0;
  37. /**
  38. * @var string
  39. */
  40. public $post_date = '0000-00-00 00:00:00';
  41. /**
  42. * @var string
  43. */
  44. public $post_date_gmt = '0000-00-00 00:00:00';
  45. /**
  46. * @var string
  47. */
  48. public $post_content = '';
  49. /**
  50. * @var string
  51. */
  52. public $post_title = '';
  53. /**
  54. * @var string
  55. */
  56. public $post_excerpt = '';
  57. /**
  58. * @var string
  59. */
  60. public $post_status = 'publish';
  61. /**
  62. * @var string
  63. */
  64. public $comment_status = 'open';
  65. /**
  66. * @var string
  67. */
  68. public $ping_status = 'open';
  69. /**
  70. * @var string
  71. */
  72. public $post_password = '';
  73. /**
  74. * @var string
  75. */
  76. public $post_name = '';
  77. /**
  78. * @var string
  79. */
  80. public $post_type = '';
  81. /**
  82. * @var string
  83. */
  84. public $to_ping = '';
  85. /**
  86. * @var string
  87. */
  88. public $pinged = '';
  89. /**
  90. * @var string
  91. */
  92. public $post_modified = '0000-00-00 00:00:00';
  93. /**
  94. * @var string
  95. */
  96. public $post_modified_gmt = '0000-00-00 00:00:00';
  97. /**
  98. * @var string
  99. */
  100. public $post_content_filtered = '';
  101. /**
  102. * @var int
  103. */
  104. public $post_parent = 0;
  105. /**
  106. * @var string
  107. */
  108. public $guid = '';
  109. /**
  110. * @var int
  111. */
  112. public $menu_order = 0;
  113. /**
  114. * @var string
  115. */
  116. public $post_mime_type = '';
  117. /**
  118. * @var int
  119. */
  120. public $comment_count = 0;
  121. /**
  122. * @var
  123. */
  124. public $filter;
  125. /**
  126. * @var WP_Post
  127. */
  128. public $post;
  129. /**
  130. * The required post type of the object.
  131. */
  132. protected $required_post_type = false;
  133. /**
  134. * Whether the object is valid.
  135. */
  136. protected $valid = true;
  137. /**
  138. * Get things going
  139. *
  140. * @param WP_Post|int $post
  141. */
  142. public function __construct( $post ) {
  143. if ( ! is_a( $post, 'WP_Post' ) ) {
  144. $post = get_post( $post );
  145. }
  146. $this->setup( $post );
  147. }
  148. /**
  149. * Given the post data, let's set the variables
  150. *
  151. * @param WP_Post $post
  152. */
  153. protected function setup( $post ) {
  154. if ( ! is_a( $post, 'WP_Post' ) || ! $this->is_required_post_type( $post ) ) {
  155. $this->valid = false;
  156. return;
  157. }
  158. $this->post = $post;
  159. foreach ( get_object_vars( $post ) as $key => $value ) {
  160. $this->$key = $value;
  161. }
  162. }
  163. /**
  164. * @param WP_Post $post
  165. *
  166. * @return bool
  167. */
  168. protected function is_required_post_type( $post ) {
  169. if ( $this->required_post_type ) {
  170. if ( is_array( $this->required_post_type ) && ! in_array( $post->post_type, $this->required_post_type ) ) {
  171. return false;
  172. } else if ( is_string( $this->required_post_type ) && $this->required_post_type !== $post->post_type ) {
  173. return false;
  174. }
  175. }
  176. return true;
  177. }
  178. /**
  179. * is triggered when invoking inaccessible methods in an object context.
  180. *
  181. * @param $name string
  182. * @param $arguments array
  183. *
  184. * @return mixed
  185. * @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
  186. */
  187. public function __call( $name, $arguments ) {
  188. if ( method_exists( $this, 'get_' . $name ) ) {
  189. return call_user_func_array( array( $this, 'get_' . $name ), $arguments );
  190. }
  191. }
  192. /**
  193. * Magic __get function to dispatch a call to retrieve a private property
  194. *
  195. * @param $key
  196. *
  197. * @return mixed|WP_Error
  198. */
  199. public function __get( $key ) {
  200. if ( method_exists( $this, 'get_' . $key ) ) {
  201. return call_user_func( array( $this, 'get_' . $key ) );
  202. } else {
  203. $meta = $this->get_meta( $key );
  204. if ( $meta ) {
  205. return $meta;
  206. }
  207. return new WP_Error( 'post-invalid-property', sprintf( __( 'Can\'t get property %s' ), $key ) );
  208. }
  209. }
  210. /**
  211. * Is object valid.
  212. *
  213. * @return bool.
  214. */
  215. public function is_valid() {
  216. return $this->valid;
  217. }
  218. /**
  219. * @param $key
  220. * @param bool $single
  221. *
  222. * @return mixed|false
  223. */
  224. public function get_meta( $key, $single = true ) {
  225. /**
  226. * Checks for remapped meta values. This allows easily adding compatibility layers in the object meta.
  227. */
  228. if ( false !== $remapped_value = $this->remapped_meta( $key ) ) {
  229. return $remapped_value;
  230. }
  231. return get_post_meta( $this->ID, $key, $single );
  232. }
  233. /**
  234. * @param string $key
  235. * @param mixed $value
  236. * @param bool $unique
  237. *
  238. * @return bool|int
  239. */
  240. public function add_meta( $key, $value, $unique = false ) {
  241. return add_post_meta( $this->ID, $key, $value, $unique );
  242. }
  243. /**
  244. * @param string $key
  245. * @param mixed $value
  246. *
  247. * @return bool|int
  248. */
  249. public function update_meta( $key, $value ) {
  250. return update_post_meta( $this->ID, $key, $value );
  251. }
  252. /**
  253. * @param string $key
  254. *
  255. * @return bool
  256. */
  257. public function delete_meta( $key ) {
  258. return delete_post_meta( $this->ID, $key );
  259. }
  260. /**
  261. * Allows for easy backward compatibility layer management in each child class.
  262. *
  263. * @param string $key
  264. *
  265. * @return bool
  266. */
  267. public function remapped_meta( $key = '' ) {
  268. return false;
  269. }
  270. /**
  271. * @return int
  272. */
  273. public function author_id() {
  274. return (int) $this->post_author;
  275. }
  276. /**
  277. * Convert object to array.
  278. *
  279. * @return array Object as array.
  280. */
  281. public function to_array() {
  282. $post = get_object_vars( $this );
  283. return $post;
  284. }
  285. /**
  286. * @return bool
  287. */
  288. public function is_trash() {
  289. return get_post_status( $this->ID ) == 'trash';
  290. }
  291. /**
  292. * @return bool
  293. */
  294. public function is_published() {
  295. return get_post_status( $this->ID ) == 'publish';
  296. }
  297. /**
  298. * @return bool
  299. */
  300. public function is_draft() {
  301. return get_post_status( $this->ID ) == 'draft';
  302. }
  303. /**
  304. * @return bool
  305. */
  306. public function is_private() {
  307. return get_post_status( $this->ID ) == 'private';
  308. }
  309. /**
  310. * @return bool
  311. */
  312. public function is_pending() {
  313. return get_post_status( $this->ID ) == 'pending';
  314. }
  315. }