/
PostDataFactory.php
159 lines (145 loc) · 5.43 KB
/
PostDataFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace NestedPages\Entities\Post;
use NestedPages\Entities\PluginIntegration\IntegrationFactory;
/**
* Build Post Data Object
*/
class PostDataFactory
{
/**
* Post Data
* @var object
*/
private $post_data;
/**
* Plugin Integrations
* @var object
*/
private $integrations;
/**
* Build the Object
*/
public function build($post, $h_taxonomies = null, $f_taxonomies = null)
{
if ( is_int($post) ) $post = get_post($post);
$this->integrations = new IntegrationFactory;
$this->post_data = new \WP_Post($post);
$this->addPostVars($post);
$this->addPostMeta($post);
$this->addOriginalLink($post);
$this->addDate($post);
$this->author($post);
if ( $h_taxonomies || $f_taxonomies ) $this->addTaxonomies($post, $h_taxonomies, $f_taxonomies);
return $this->post_data;
}
/**
* Post Items
*/
public function addPostVars($post)
{
$this->post_data->id = $post->ID;
$this->post_data->ID = $post->ID;
$this->post_data->parent_id = $post->post_parent;
$this->post_data->title = $post->post_title;
$this->post_data->password = $post->post_password;
$this->post_data->status = $post->post_status;
$this->post_data->type = $post->post_type;
$this->post_data->comment_status = $post->comment_status;
$this->post_data->content = $post->post_content;
$this->post_data->hierarchical = is_post_type_hierarchical($post->post_type);
$this->post_data->post_type = $post->post_type;
$this->post_data->link = get_the_permalink($post->ID);
}
/**
* Post Meta
*/
public function addPostMeta($post)
{
$meta = get_metadata('post', $post->ID);
$this->post_data->meta = $meta;
$this->post_data->nav_title = ( isset($meta['_np_nav_title'][0]) ) ? $meta['_np_nav_title'][0] : null;
$this->post_data->link_target = ( isset($meta['_np_link_target'][0]) ) ? $meta['_np_link_target'][0] : null;
$this->post_data->nav_title_attr = ( isset($meta['_np_title_attribute'][0]) ) ? $meta['_np_title_attribute'][0] : null;
$this->post_data->nav_css = ( isset($meta['_np_nav_css_classes'][0]) ) ? $meta['_np_nav_css_classes'][0] : null;
$this->post_data->nav_object = ( isset($meta['_np_nav_menu_item_object'][0]) ) ? $meta['_np_nav_menu_item_object'][0] : null;
$this->post_data->nav_object_id = ( isset($meta['_np_nav_menu_item_object_id'][0]) ) ? $meta['_np_nav_menu_item_object_id'][0] : null;
$this->post_data->nav_type = ( isset($meta['_np_nav_menu_item_type'][0]) ) ? $meta['_np_nav_menu_item_type'][0] : null;
$this->post_data->nav_status = ( isset($meta['_np_nav_status'][0]) && $meta['_np_nav_status'][0] == 'hide' ) ? 'hide' : 'show';
$this->post_data->np_status = ( isset($meta['_nested_pages_status'][0]) && $meta['_nested_pages_status'][0] == 'hide' ) ? 'hide' : 'show';
$this->post_data->nav_custom_url = ( isset($meta['_np_nav_custom_url'][0]) && $meta['_np_nav_custom_url'][0] !== '' ) ? $meta['_np_nav_custom_url'][0] : null;
$this->post_data->template = ( isset($meta['_wp_page_template'][0]) ) ? $meta['_wp_page_template'][0] : false;
// Yoast Score
if ( $this->integrations->plugins->yoast->installed ) {
$this->post_data->score = $this->integrations->plugins->yoast->getScore($post->ID);
}
}
/**
* Add original item/link to link
*/
private function addOriginalLink($post)
{
if ( $post->post_type !== 'np-redirect' ) {
$this->post_data->nav_original_link = null;
$this->post_data->nav_original_type = null;
return;
}
if ( $this->post_data->nav_type && $this->post_data->nav_type == 'taxonomy' ){
$term = get_term_by('id', $this->post_data->nav_object_id, $this->post_data->nav_object);
$this->post_data->nav_original_link = get_term_link($term);
$this->post_data->nav_original_title = $term->name;
return;
}
if ( $this->post_data->nav_type && $this->post_data->nav_type == 'post_type_archive' ){
$post_type = get_post_type_object($this->post_data->nav_object);
$this->post_data->nav_original_link = get_post_type_archive_link($this->post_data->nav_object);
$this->post_data->nav_original_title = sprintf(__('%s (Archive)', 'wp-nested-pages'), $post_type->labels->name);
return;
}
$id = $this->post_data->nav_object_id;
$this->post_data->nav_original_link = get_the_permalink($id);
$this->post_data->nav_original_title = get_the_title($id);
}
/**
* Date Vars
*/
private function addDate($post)
{
$this->post_data->date = new \stdClass();
$time = get_the_time('U', $post->ID);
$this->post_data->date->d = date('d', $time);
$this->post_data->date->month = date('m', $time);
$this->post_data->date->y = date('Y', $time);
$this->post_data->date->h = date('H', $time);
$this->post_data->date->m = date('i', $time);
$this->post_data->date->datepicker = $time;
}
/**
* Add Author Info
*/
private function author($post)
{
$this->post_data->author = get_the_author_meta('display_name', $post->post_author);
$this->post_data->author_link = admin_url('edit.php?post_type=' . $post->post_type . '&author=' . $post->post_author);
}
/**
* Add taxonomies
*/
public function addTaxonomies($post, $h_taxonomies, $f_taxonomies)
{
// Add taxonomies
if ( count($h_taxonomies) > 0 ) {
foreach($h_taxonomies as $tax){
$taxname = $tax->name;
if ( !isset($post->$taxname) ) continue;
$this->post_data->$taxname = explode(',', $post->$taxname);
}
}
if ( count($f_taxonomies) > 0 ) {
foreach($f_taxonomies as $tax){
$taxname = $tax->name;
if ( !isset($post->$taxname) ) continue;
$this->post_data->$taxname = explode(',', $post->$taxname);
}
}
}
}