-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_layer.php
181 lines (164 loc) · 5.07 KB
/
data_layer.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
class Wordpress_Data_Layer_Model {
/**
* Get the data layer and return it in json.
*
*/
public function get() {
$data_layer = (object) array();
$data_layer->site = $this->get_site_data();
$data_layer->page = $this->get_page_data();
$data_layer->user = $this->get_user_data();
if (is_single()) {
$data_layer->post = $this->get_post_data();
}
return json_encode($data_layer);
}
/**
* Get the site details for the data layer.
*
*/
public function get_site_data() {
$site = (object) array();
$site->name = get_bloginfo("name");
$site->description = get_bloginfo("description");
$site->wordpress_version = get_bloginfo("version");
$site->language = get_bloginfo("language");
return $site;
}
/**
* Get the page details for the data layer.
*
*/
public function get_page_data() {
$page = (object) array();
$page->type = $this->get_page_type();
$page->is_paged = is_paged();
return $page;
}
/**
* Get the site details for the data layer.
*
*/
public function get_page_type() {
if (is_home()) {
$type = "home";
} else if (is_page()) {
$type = "page";
} else if (is_single()) {
$type = get_post_type();
} else if (is_category()) {
$type = "category";
} else if (is_tag()) {
$type = "tag";
} else if (is_tax()) {
$type = "taxonomy";
} else if (is_author()) {
$type = "author";
} else if (is_archive()) {
$type = "archive";
} else if (is_search()) {
$type = "search";
} else if (is_404()) {
$type = "404";
} else if (is_attachment()) {
$type = "attachment";
} else if (is_preview()) {
$type = "preview_post";
}
return $type;
}
/**
* Get the user details for the data layer.
*
*/
public function get_user_data() {
$userData = wp_get_current_user();
$user = (object) array();
if ($userData->ID) {
$user->id = $userData->ID;
$user->name = $userData->display_name;
$user->email = $userData->user_email;
$user->registered_date = $userData->user_registered;
$user->logged_in = true;
$user->role = $userData->roles[0];
} else {
$user->logged_in = false;
}
$user->ip = $this->get_user_ip();
return $user;
}
/**
* Get the IP of a user
*
*/
public function get_user_ip() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* Get the post details for the data layer.
*
*/
public function get_post_data() {
$post = (object) array();
$postData = get_post();
$post->id = $postData->ID;
$post->title = $postData->post_title;
$post->publish_date = $postData->post_date;
$post->last_modified_date = $postData->post_modified;
$post->url = get_permalink($post->id);
$post->comment_count = $postData->comment_count;
$post->type = $postData->post_type;
$post->author_id = $postData->post_author;
$post->author_name = get_the_author();
$post->categories = $this->get_post_categories();
$post->images = $this->get_post_images($post);
return $post;
}
/**
* Get the post categories for the post data in the data layer.
*
*/
public function get_post_categories() {
$categoryData = get_the_category();
$categories = array();
foreach($categoryData as $item) {
$category = (object) array();
$category->name = $item->name;
$category->id = $item->cat_ID;
$category->slug = $item->slug;
array_push($categories, $category);
}
return $categories;
}
/**
* Get the post images for the post data in the data layer.
*
*/
public function get_post_images($post) {
$imageData = get_children(array(
"post_parent" => $post->id,
"post_type" => "attachment"
));
$images = array();
foreach ($imageData as $item) {
if (in_array($item->post_mime_type, array("image/png", "image/gif", "image/jpeg"))) {
$image = (object) array();
$image->id = $item->ID;
$image->url = $item->guid;
$image->title = $item->post_title;
$image->publish_date = $item->post_date;
$image->last_modified_date = $item->post_modified;
array_push($images, $image);
}
}
return $images;
}
}