PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.25.0
Independent Analytics – WordPress Analytics Plugin v1.25.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Models / Page.php
independent-analytics / IAWP / Models Last commit date
Campaign.php 3 years ago Current_Traffic.php 3 years ago Geo.php 3 years ago Page.php 3 years ago Page_Author_Archive.php 3 years ago Page_Date_Archive.php 3 years ago Page_Home.php 3 years ago Page_Not_Found.php 3 years ago Page_Post_Type_Archive.php 3 years ago Page_Search.php 3 years ago Page_Singular.php 3 years ago Page_Term_Archive.php 3 years ago Referrer.php 3 years ago View_Stats.php 3 years ago Visitor.php 3 years ago WooCommerce_Stats.php 3 years ago
Page.php
220 lines
1 <?php
2
3 namespace IAWP_SCOPED\IAWP\Models;
4
5 use IAWP_SCOPED\IAWP\Query;
6 use IAWP_SCOPED\IAWP\Utils\Request;
7 abstract class Page
8 {
9 use View_Stats;
10 private $id;
11 private $resource;
12 private $is_deleted;
13 private $cache;
14 private $cached_title;
15 private $cached_type;
16 private $cached_type_label;
17 private $cached_icon;
18 private $cached_author_id;
19 private $cached_author;
20 private $cached_avatar;
21 private $cached_date;
22 private $cached_category;
23 public function __construct($row)
24 {
25 $this->id = $row->id ?? null;
26 $this->resource = $row->resource ?? null;
27 $this->set_view_stats($row);
28 }
29 /**
30 * By default, pages don't have the ability to have comments.
31 * This can be overridden by a subclass to return an actual comments value.
32 *
33 * @return int|null
34 */
35 public function comments() : ?int
36 {
37 return null;
38 }
39 public final function is_resource_type($type) : bool
40 {
41 return $this->resource == $type;
42 }
43 public final function is_deleted() : bool
44 {
45 if (!\is_null($this->is_deleted)) {
46 return $this->is_deleted;
47 }
48 $this->is_deleted = $this->calculate_is_deleted();
49 return $this->is_deleted;
50 }
51 private function use_cache() : bool
52 {
53 if (!\is_null($this->cache)) {
54 return \true;
55 }
56 $deleted = $this->is_deleted();
57 if ($deleted) {
58 $this->cache = $this->get_cache();
59 }
60 return $deleted;
61 }
62 private function get_cache()
63 {
64 global $wpdb;
65 $resources_table = Query::get_table_name(Query::RESOURCES);
66 $resource_key = $this->resource_key();
67 $resource_value = $this->resource_value();
68 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE {$resource_key} = %s", $resource_value);
69 return $wpdb->get_row($query);
70 }
71 public final function update_cache() : void
72 {
73 global $wpdb;
74 $resources_table = Query::get_table_name(Query::RESOURCES);
75 $resource_key = $this->resource_key();
76 $resource_value = $this->resource_value();
77 $query = $wpdb->prepare("UPDATE {$resources_table} SET cached_title = %s, cached_url = %s, cached_type = %s, cached_type_label = %s, cached_author_id = %s, cached_author = %s, cached_date = %s, cached_category = %s WHERE {$resource_key} = %s", $this->calculate_title(), $this->calculate_url(), $this->calculate_type(), $this->calculate_type_label(), $this->calculate_author_id(), $this->calculate_author(), $this->calculate_date(), \implode(', ', $this->calculate_category()), $resource_value);
78 $wpdb->query($query);
79 }
80 public final function id()
81 {
82 return $this->id;
83 }
84 // The goal here is to generate a unique resource key that is *not* the url. This is for internal comparison
85 // purpose only. So a 404 page with be something like not_found_/test/abc and a term archive would be term_12.
86 protected final function unique_resource_id()
87 {
88 return $this->resource . '_' . $this->resource_value();
89 }
90 public final function url($full_url = \false)
91 {
92 if ($this->use_cache()) {
93 $url = $this->cache->cached_url;
94 } else {
95 $url = $this->calculate_url();
96 }
97 if ($full_url) {
98 return $url;
99 } else {
100 return Request::path_relative_to_site_url($url);
101 }
102 }
103 public final function title()
104 {
105 if ($this->use_cache()) {
106 return $this->cache->cached_title;
107 }
108 if (\is_null($this->cached_title)) {
109 $this->cached_title = $this->calculate_title();
110 }
111 return \strlen($this->cached_title) > 0 ? $this->cached_title : '(no title)';
112 }
113 public final function type($raw = \false)
114 {
115 if ($raw) {
116 if ($this->use_cache()) {
117 return $this->cache->cached_type;
118 }
119 if (\is_null($this->cached_type)) {
120 $this->cached_type = $this->calculate_type();
121 }
122 return $this->cached_type;
123 } else {
124 if ($this->use_cache()) {
125 return $this->cache->cached_type_label;
126 }
127 if (\is_null($this->cached_type_label)) {
128 $this->cached_type_label = $this->calculate_type_label();
129 }
130 return $this->cached_type_label;
131 }
132 }
133 public final function icon()
134 {
135 if (\is_null($this->cached_icon)) {
136 $this->cached_icon = $this->calculate_icon();
137 }
138 return $this->cached_icon;
139 }
140 public final function author()
141 {
142 if ($this->use_cache()) {
143 return $this->cache->cached_author;
144 }
145 if (\is_null($this->cached_author)) {
146 $this->cached_author = $this->calculate_author();
147 }
148 return $this->cached_author;
149 }
150 public final function author_id()
151 {
152 if ($this->use_cache()) {
153 return $this->cache->cached_author_id;
154 }
155 if (\is_null($this->cached_author_id)) {
156 $this->cached_author_id = $this->calculate_author_id();
157 }
158 return $this->cached_author_id;
159 }
160 public final function avatar()
161 {
162 if (\is_null($this->cached_avatar)) {
163 $this->cached_avatar = $this->calculate_avatar();
164 }
165 return $this->cached_avatar;
166 }
167 public final function date()
168 {
169 if (\is_null($this->cached_date)) {
170 $this->cached_date = $this->calculate_date();
171 }
172 return $this->cached_date;
173 }
174 public final function formatted_category() : ?string
175 {
176 $categories = $this->category(\false);
177 $category_names = [];
178 if (\count($categories) === 0) {
179 return null;
180 }
181 foreach ($categories as $category_id) {
182 $category = \get_the_category_by_ID($category_id);
183 if (!\is_wp_error($category)) {
184 $category_names[] = $category;
185 }
186 }
187 return \implode(', ', $category_names);
188 }
189 public final function category($formatted = \true)
190 {
191 if ($formatted === \true) {
192 return $this->formatted_category();
193 }
194 if (\is_null($this->cached_category)) {
195 $this->cached_category = $this->calculate_category();
196 }
197 return $this->cached_category;
198 }
199 public function most_popular_subtitle() : ?string
200 {
201 return null;
202 }
203 //
204 // Below are required and optional methods that classes extending page can define
205 //
206 protected abstract function resource_key();
207 protected abstract function resource_value();
208 protected abstract function calculate_is_deleted() : bool;
209 protected abstract function calculate_url();
210 protected abstract function calculate_title();
211 protected abstract function calculate_type();
212 protected abstract function calculate_type_label();
213 protected abstract function calculate_icon();
214 protected abstract function calculate_author();
215 protected abstract function calculate_author_id();
216 protected abstract function calculate_avatar();
217 protected abstract function calculate_date();
218 protected abstract function calculate_category();
219 }
220