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