PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.11.9
Independent Analytics – WordPress Analytics Plugin v2.11.9
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 2 years ago Click.php 1 year ago ClickWhale_Link_Page.php 1 year ago Current_Traffic.php 2 years ago Device.php 2 years ago Form.php 1 year ago Geo.php 2 years ago Page.php 1 year ago Page_Author_Archive.php 2 years ago Page_Date_Archive.php 2 years ago Page_Home.php 2 years ago Page_Not_Found.php 1 year ago Page_Post_Type_Archive.php 1 year ago Page_Search.php 2 years ago Page_Singular.php 1 year ago Page_Term_Archive.php 2 years ago Page_Virtual.php 1 year ago Referrer.php 2 years ago Universal_Model_Columns.php 1 year ago Visitor.php 1 year ago
Page.php
267 lines
1 <?php
2
3 namespace IAWP\Models;
4
5 use IAWP\Illuminate_Builder;
6 use IAWP\Query;
7 use IAWP\Utils\Request;
8 use IAWPSCOPED\Illuminate\Support\Str;
9 /** @internal */
10 abstract class Page
11 {
12 use \IAWP\Models\Universal_Model_Columns;
13 protected $row;
14 private $id;
15 private $resource;
16 private $entrances;
17 private $exits;
18 private $exit_percent;
19 private $is_deleted;
20 private $cache;
21 private $cached_title;
22 private $cached_type;
23 private $cached_type_label;
24 private $cached_icon;
25 private $cached_author_id;
26 private $cached_author;
27 private $cached_avatar;
28 private $cached_date;
29 private $cached_category;
30 public function __construct($row)
31 {
32 $this->row = $row;
33 $this->id = $row->id ?? null;
34 $this->resource = $row->resource ?? null;
35 $this->entrances = $row->entrances ?? 0;
36 $this->exits = $row->exits ?? 0;
37 $this->exit_percent = $row->exit_percent ?? 0;
38 // If $row is a full row from the database, use that for the cache
39 // Eventually, I'd like to avoid selecting resources.* and just get the cached_.*
40 // fields for the rows that are going to be shown.
41 if (\is_string($row->cached_title ?? null)) {
42 $this->cache = $row;
43 }
44 }
45 protected abstract function resource_key();
46 protected abstract function resource_value();
47 protected abstract function calculate_is_deleted() : bool;
48 protected abstract function calculate_url();
49 protected abstract function calculate_title();
50 protected abstract function calculate_type();
51 protected abstract function calculate_type_label();
52 protected abstract function calculate_icon();
53 protected abstract function calculate_author();
54 protected abstract function calculate_author_id();
55 protected abstract function calculate_avatar();
56 protected abstract function calculate_date();
57 protected abstract function calculate_category();
58 public function entrances() : int
59 {
60 return $this->entrances;
61 }
62 public function exits() : int
63 {
64 return $this->exits;
65 }
66 public function exit_percent() : float
67 {
68 return $this->exit_percent;
69 }
70 /**
71 * By default, pages don't have the ability to have comments.
72 * This can be overridden by a subclass to return an actual comments value.
73 *
74 * @return int|null
75 */
76 public function comments() : ?int
77 {
78 return null;
79 }
80 public function get_singular_id() : ?int
81 {
82 if ($this->resource_key() !== 'singular_id') {
83 return null;
84 }
85 return $this->resource_value();
86 }
87 public final function is_deleted() : bool
88 {
89 if (!\is_null($this->is_deleted)) {
90 return $this->is_deleted;
91 }
92 $this->is_deleted = $this->calculate_is_deleted();
93 return $this->is_deleted;
94 }
95 public final function update_cache() : void
96 {
97 $resources_table = Query::get_table_name(Query::RESOURCES);
98 $resource_key = $this->resource_key();
99 $resource_value = $this->resource_value();
100 $url = $this->calculate_url();
101 Illuminate_Builder::new()->from($resources_table)->where($resource_key, '=', $resource_value)->update(['cached_title' => $this->calculate_title(), 'cached_url' => \is_string($url) ? Str::limit($url, 2083) : $url, 'cached_type' => $this->calculate_type(), 'cached_type_label' => $this->calculate_type_label(), 'cached_author_id' => $this->calculate_author_id(), 'cached_author' => $this->calculate_author(), 'cached_date' => $this->calculate_date(), 'cached_category' => !empty($this->calculate_category()) ? \implode(', ', $this->calculate_category()) : null]);
102 }
103 public final function id()
104 {
105 return $this->id;
106 }
107 public final function url($full_url = \false)
108 {
109 if ($this->use_cache()) {
110 $url = $this->cache->cached_url;
111 } else {
112 $url = $this->calculate_url();
113 }
114 if (\is_null($url)) {
115 return null;
116 }
117 if ($full_url) {
118 return $url;
119 } else {
120 return Request::path_relative_to_site_url($url);
121 }
122 }
123 public final function title()
124 {
125 if ($this->use_cache()) {
126 return $this->cache->cached_title;
127 }
128 if (\is_null($this->cached_title)) {
129 $this->cached_title = $this->calculate_title();
130 }
131 return \strlen($this->cached_title) > 0 ? $this->cached_title : '(no title)';
132 }
133 public final function type($raw = \false)
134 {
135 if ($raw) {
136 if ($this->use_cache()) {
137 return $this->cache->cached_type;
138 }
139 if (\is_null($this->cached_type)) {
140 $this->cached_type = $this->calculate_type();
141 }
142 return $this->cached_type;
143 } else {
144 if ($this->use_cache()) {
145 return $this->cache->cached_type_label;
146 }
147 if (\is_null($this->cached_type_label)) {
148 $this->cached_type_label = $this->calculate_type_label();
149 }
150 return $this->cached_type_label;
151 }
152 }
153 public final function icon()
154 {
155 if (\is_null($this->cached_icon)) {
156 $this->cached_icon = $this->calculate_icon();
157 }
158 return $this->cached_icon;
159 }
160 public final function author()
161 {
162 if ($this->use_cache()) {
163 return $this->cache->cached_author;
164 }
165 if (\is_null($this->cached_author)) {
166 $this->cached_author = $this->calculate_author();
167 }
168 return $this->cached_author;
169 }
170 public final function author_id()
171 {
172 if ($this->use_cache()) {
173 return $this->cache->cached_author_id;
174 }
175 if (\is_null($this->cached_author_id)) {
176 $this->cached_author_id = $this->calculate_author_id();
177 }
178 return $this->cached_author_id;
179 }
180 public final function avatar()
181 {
182 if (\is_null($this->cached_avatar)) {
183 $this->cached_avatar = $this->calculate_avatar();
184 }
185 return $this->cached_avatar;
186 }
187 public final function date()
188 {
189 if (\is_null($this->cached_date)) {
190 $this->cached_date = $this->calculate_date();
191 }
192 return $this->cached_date;
193 }
194 public final function formatted_category() : ?string
195 {
196 $categories = $this->category(\false);
197 $category_names = [];
198 if (\count($categories) === 0) {
199 return null;
200 }
201 foreach ($categories as $category_id) {
202 $category = \get_the_category_by_ID($category_id);
203 if (!\is_wp_error($category)) {
204 $category_names[] = $category;
205 }
206 }
207 return \implode(', ', $category_names);
208 }
209 public final function category($formatted = \true)
210 {
211 if ($formatted === \true) {
212 return $this->formatted_category();
213 }
214 if (\is_null($this->cached_category)) {
215 $this->cached_category = $this->calculate_category();
216 }
217 return $this->cached_category;
218 }
219 public function most_popular_subtitle() : ?string
220 {
221 return null;
222 }
223 private function use_cache() : bool
224 {
225 if (!\is_null($this->cache)) {
226 return \true;
227 }
228 $deleted = $this->is_deleted();
229 if ($deleted) {
230 $this->cache = $this->get_cache();
231 }
232 return $deleted;
233 }
234 private function get_cache()
235 {
236 global $wpdb;
237 $resources_table = Query::get_table_name(Query::RESOURCES);
238 $resource_key = $this->resource_key();
239 $resource_value = $this->resource_value();
240 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE {$resource_key} = %s", $resource_value);
241 return $wpdb->get_row($query);
242 }
243 public static function from_row(object $row) : \IAWP\Models\Page
244 {
245 switch ($row->resource) {
246 case 'singular':
247 return new \IAWP\Models\Page_Singular($row);
248 case 'author_archive':
249 return new \IAWP\Models\Page_Author_Archive($row);
250 case 'date_archive':
251 return new \IAWP\Models\Page_Date_Archive($row);
252 case 'post_type_archive':
253 return new \IAWP\Models\Page_Post_Type_Archive($row);
254 case 'term_archive':
255 return new \IAWP\Models\Page_Term_Archive($row);
256 case 'search':
257 return new \IAWP\Models\Page_Search($row);
258 case 'home':
259 return new \IAWP\Models\Page_Home($row);
260 case 'virtual_page':
261 return \IAWP\Models\Page_Virtual::from($row);
262 default:
263 return new \IAWP\Models\Page_Not_Found($row);
264 }
265 }
266 }
267