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