PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.28.0
Independent Analytics – WordPress Analytics Plugin v1.28.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 public final function update_cache() : void
52 {
53 global $wpdb;
54 $resources_table = Query::get_table_name(Query::RESOURCES);
55 $resource_key = $this->resource_key();
56 $resource_value = $this->resource_value();
57 $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);
58 $wpdb->query($query);
59 }
60 public final function id()
61 {
62 return $this->id;
63 }
64 public final function url($full_url = \false)
65 {
66 if ($this->use_cache()) {
67 $url = $this->cache->cached_url;
68 } else {
69 $url = $this->calculate_url();
70 }
71 if ($full_url) {
72 return $url;
73 } else {
74 return Request::path_relative_to_site_url($url);
75 }
76 }
77 public final function title()
78 {
79 if ($this->use_cache()) {
80 return $this->cache->cached_title;
81 }
82 if (\is_null($this->cached_title)) {
83 $this->cached_title = $this->calculate_title();
84 }
85 return \strlen($this->cached_title) > 0 ? $this->cached_title : '(no title)';
86 }
87 public final function type($raw = \false)
88 {
89 if ($raw) {
90 if ($this->use_cache()) {
91 return $this->cache->cached_type;
92 }
93 if (\is_null($this->cached_type)) {
94 $this->cached_type = $this->calculate_type();
95 }
96 return $this->cached_type;
97 } else {
98 if ($this->use_cache()) {
99 return $this->cache->cached_type_label;
100 }
101 if (\is_null($this->cached_type_label)) {
102 $this->cached_type_label = $this->calculate_type_label();
103 }
104 return $this->cached_type_label;
105 }
106 }
107 public final function icon()
108 {
109 if (\is_null($this->cached_icon)) {
110 $this->cached_icon = $this->calculate_icon();
111 }
112 return $this->cached_icon;
113 }
114 public final function author()
115 {
116 if ($this->use_cache()) {
117 return $this->cache->cached_author;
118 }
119 if (\is_null($this->cached_author)) {
120 $this->cached_author = $this->calculate_author();
121 }
122 return $this->cached_author;
123 }
124 public final function author_id()
125 {
126 if ($this->use_cache()) {
127 return $this->cache->cached_author_id;
128 }
129 if (\is_null($this->cached_author_id)) {
130 $this->cached_author_id = $this->calculate_author_id();
131 }
132 return $this->cached_author_id;
133 }
134 public final function avatar()
135 {
136 if (\is_null($this->cached_avatar)) {
137 $this->cached_avatar = $this->calculate_avatar();
138 }
139 return $this->cached_avatar;
140 }
141 public final function date()
142 {
143 if (\is_null($this->cached_date)) {
144 $this->cached_date = $this->calculate_date();
145 }
146 return $this->cached_date;
147 }
148 public final function formatted_category() : ?string
149 {
150 $categories = $this->category(\false);
151 $category_names = [];
152 if (\count($categories) === 0) {
153 return null;
154 }
155 foreach ($categories as $category_id) {
156 $category = \get_the_category_by_ID($category_id);
157 if (!\is_wp_error($category)) {
158 $category_names[] = $category;
159 }
160 }
161 return \implode(', ', $category_names);
162 }
163 public final function category($formatted = \true)
164 {
165 if ($formatted === \true) {
166 return $this->formatted_category();
167 }
168 if (\is_null($this->cached_category)) {
169 $this->cached_category = $this->calculate_category();
170 }
171 return $this->cached_category;
172 }
173 public function most_popular_subtitle() : ?string
174 {
175 return null;
176 }
177 // The goal here is to generate a unique resource key that is *not* the url. This is for internal comparison
178 // purpose only. So a 404 page with be something like not_found_/test/abc and a term archive would be term_12.
179 protected final function unique_resource_id()
180 {
181 return $this->resource . '_' . $this->resource_value();
182 }
183 //
184 // Below are required and optional methods that classes extending page can define
185 //
186 protected abstract function resource_key();
187 protected abstract function resource_value();
188 protected abstract function calculate_is_deleted() : bool;
189 protected abstract function calculate_url();
190 protected abstract function calculate_title();
191 protected abstract function calculate_type();
192 protected abstract function calculate_type_label();
193 protected abstract function calculate_icon();
194 protected abstract function calculate_author();
195 protected abstract function calculate_author_id();
196 protected abstract function calculate_avatar();
197 protected abstract function calculate_date();
198 protected abstract function calculate_category();
199 private function use_cache() : bool
200 {
201 if (!\is_null($this->cache)) {
202 return \true;
203 }
204 $deleted = $this->is_deleted();
205 if ($deleted) {
206 $this->cache = $this->get_cache();
207 }
208 return $deleted;
209 }
210 private function get_cache()
211 {
212 global $wpdb;
213 $resources_table = Query::get_table_name(Query::RESOURCES);
214 $resource_key = $this->resource_key();
215 $resource_value = $this->resource_value();
216 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE {$resource_key} = %s", $resource_value);
217 return $wpdb->get_row($query);
218 }
219 }
220