| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
// Exit if accessed directly |
| 5 |
if ( ! defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class UpStream_Model_Post_Object extends UpStream_Model_Object |
| 10 |
{ |
| 11 |
|
| 12 |
protected $categoryIds = []; |
| 13 |
|
| 14 |
protected $parentId = 0; |
| 15 |
|
| 16 |
protected $postType = 'post'; |
| 17 |
|
| 18 |
/** |
| 19 |
* UpStream_Model_Post_Object constructor. |
| 20 |
*/ |
| 21 |
public function __construct($id, $fields) |
| 22 |
{ |
| 23 |
parent::__construct($id); |
| 24 |
|
| 25 |
if ($id > 0) { |
| 26 |
$this->load($id, $fields); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
protected function load($id, $fields) |
| 31 |
{ |
| 32 |
$post = get_post($id); |
| 33 |
|
| 34 |
if (!$post) { |
| 35 |
throw new UpStream_Model_ArgumentException(sprintf(__('Object with ID %s does not exist.', 'upstream'), $id)); |
| 36 |
} |
| 37 |
|
| 38 |
$metadata = get_post_meta($id); |
| 39 |
if (empty($metadata)) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
if (!isset($post->post_title)) { |
| 44 |
throw new UpStream_Model_ArgumentException(sprintf(__('Object %s title does not exist.', 'upstream'), $id)); |
| 45 |
} |
| 46 |
$this->title = $post->post_title; |
| 47 |
|
| 48 |
if (!isset($post->post_author)) { |
| 49 |
throw new UpStream_Model_ArgumentException(sprintf(__('Object %s author does not exist.', 'upstream'), $id)); |
| 50 |
} |
| 51 |
$this->createdBy = $post->post_author; |
| 52 |
|
| 53 |
if (!isset($post->post_content)) { |
| 54 |
throw new UpStream_Model_ArgumentException(sprintf(__('Object %s content does not exist.', 'upstream'), $id)); |
| 55 |
} |
| 56 |
$this->description = $post->post_content; |
| 57 |
|
| 58 |
foreach ($fields as $field => $input) { |
| 59 |
|
| 60 |
if (is_string($input)) { |
| 61 |
if (!empty($metadata[$input])) { |
| 62 |
|
| 63 |
if (count($metadata[$input]) > 0) { |
| 64 |
$this->{$field} = $metadata[$input][0]; |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
} else if ($input instanceof Closure) { |
| 69 |
$this->{$field} = $input($metadata); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
$dataToLoad = []; |
| 74 |
foreach ($metadata as $key => $val) { |
| 75 |
if (is_array($val)) { |
| 76 |
$dataToLoad[$key] = $val[0]; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$this->additionalFields = apply_filters('upstream_model_load_fields', $this->additionalFields, $dataToLoad, |
| 81 |
$this->type, $this->id); |
| 82 |
} |
| 83 |
|
| 84 |
protected function store() |
| 85 |
{ |
| 86 |
|
| 87 |
$res = null; |
| 88 |
|
| 89 |
if ($this->id > 0) { |
| 90 |
|
| 91 |
$post_arr = [ |
| 92 |
'ID' => $this->id, |
| 93 |
'post_title' => ($this->title == null ? '(New Item)' : $this->title), |
| 94 |
'post_content' => ($this->description == null ? '' : $this->description) |
| 95 |
]; |
| 96 |
|
| 97 |
$res = wp_update_post($post_arr, true); |
| 98 |
|
| 99 |
} else { |
| 100 |
$post_arr = [ |
| 101 |
'post_title' => ($this->title == null ? '(New Item)' : $this->title), |
| 102 |
'post_author' => $this->createdBy, |
| 103 |
'post_parent' => $this->parentId, |
| 104 |
'post_content' => ($this->description == null ? '' : $this->description), |
| 105 |
'post_status' => 'publish', |
| 106 |
'post_type' => $this->postType |
| 107 |
]; |
| 108 |
|
| 109 |
$res = wp_insert_post($post_arr, true); |
| 110 |
} |
| 111 |
|
| 112 |
if ($res instanceof \WP_Error) { |
| 113 |
throw new UpStream_Model_ArgumentException(sprintf(__('Could not load post with ID %s.', 'upstream'), $this->id)); |
| 114 |
} else { |
| 115 |
$this->id = (int)$res; |
| 116 |
} |
| 117 |
|
| 118 |
$dataToStore = []; |
| 119 |
$dataToStore = apply_filters('upstream_model_store_fields', $dataToStore, $this->additionalFields, |
| 120 |
$this->type, $this->id); |
| 121 |
|
| 122 |
foreach ($dataToStore as $key => $value) { |
| 123 |
update_post_meta($this->id, $key, $value); |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
public function __get($property) |
| 129 |
{ |
| 130 |
switch ($property) { |
| 131 |
|
| 132 |
case 'parentId': |
| 133 |
return $this->{$property}; |
| 134 |
|
| 135 |
default: |
| 136 |
return parent::__get($property); |
| 137 |
|
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
public function __set($property, $value) |
| 142 |
{ |
| 143 |
switch ($property) { |
| 144 |
|
| 145 |
case 'id': |
| 146 |
if (!filter_var($value, FILTER_VALIDATE_INT)) |
| 147 |
throw new UpStream_Model_ArgumentException(__('ID must be a valid numeric.', 'upstream')); |
| 148 |
$this->{$property} = $value; |
| 149 |
break; |
| 150 |
|
| 151 |
default: |
| 152 |
parent::__set($property, $value); |
| 153 |
break; |
| 154 |
|
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
} |