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
visitor.php
3 years ago
page.php
273 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | abstract class Page |
| 6 | { |
| 7 | private $id; |
| 8 | protected $views; |
| 9 | protected $visitors; |
| 10 | private $resource; |
| 11 | private $is_deleted; |
| 12 | private $cache; |
| 13 | private $cached_url; |
| 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 | |
| 24 | public function __construct($row) |
| 25 | { |
| 26 | $this->id = $row->id ?? null; |
| 27 | $this->views = $row->views ?? null; |
| 28 | $this->visitors = $row->visitors ?? null; |
| 29 | $this->resource = $row->resource ?? null; |
| 30 | } |
| 31 | |
| 32 | final public function is_resource_type($type): bool |
| 33 | { |
| 34 | return $this->resource == $type; |
| 35 | } |
| 36 | |
| 37 | final public function is_deleted(): bool |
| 38 | { |
| 39 | if (!is_null($this->is_deleted)) { |
| 40 | return $this->is_deleted; |
| 41 | } |
| 42 | |
| 43 | $this->is_deleted = $this->calculate_is_deleted(); |
| 44 | |
| 45 | return $this->is_deleted; |
| 46 | } |
| 47 | |
| 48 | private function use_cache(): bool |
| 49 | { |
| 50 | if (!is_null($this->cache)) { |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | $deleted = $this->is_deleted(); |
| 55 | |
| 56 | if ($deleted) { |
| 57 | $this->cache = $this->get_cache(); |
| 58 | } |
| 59 | |
| 60 | return $deleted; |
| 61 | } |
| 62 | |
| 63 | private function get_cache() |
| 64 | { |
| 65 | global $wpdb; |
| 66 | $resources_table = DB::resources_table(); |
| 67 | $resource_key = $this->resource_key(); |
| 68 | $resource_value = $this->resource_value(); |
| 69 | $query = $wpdb->prepare( |
| 70 | "SELECT * FROM $resources_table WHERE $resource_key = %s", |
| 71 | $resource_value |
| 72 | ); |
| 73 | |
| 74 | return $wpdb->get_row($query); |
| 75 | } |
| 76 | |
| 77 | final public function update_cache(): void |
| 78 | { |
| 79 | global $wpdb; |
| 80 | $resources_table = DB::resources_table(); |
| 81 | $resource_key = $this->resource_key(); |
| 82 | $resource_value = $this->resource_value(); |
| 83 | $query = $wpdb->prepare( |
| 84 | "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 WHERE $resource_key = %s", |
| 85 | $this->calculate_title(), |
| 86 | $this->calculate_url(), |
| 87 | $this->calculate_type(), |
| 88 | $this->calculate_type_label(), |
| 89 | $this->calculate_author_id(), |
| 90 | $this->calculate_author(), |
| 91 | $this->calculate_date(), |
| 92 | $resource_value |
| 93 | ); |
| 94 | $wpdb->query($query); |
| 95 | } |
| 96 | |
| 97 | final public function id() |
| 98 | { |
| 99 | return $this->id; |
| 100 | } |
| 101 | |
| 102 | // The goal here is to generate a unique resource key that is *not* the url. This is for internal comparison |
| 103 | // purpose only. So a 404 page with be something like not_found_/test/abc and a term archive would be term_12. |
| 104 | final protected function unique_resource_id() |
| 105 | { |
| 106 | return $this->resource . '_' . $this->resource_value(); |
| 107 | } |
| 108 | |
| 109 | final public function views() |
| 110 | { |
| 111 | return $this->views; |
| 112 | } |
| 113 | |
| 114 | final public function visitors() |
| 115 | { |
| 116 | return $this->visitors; |
| 117 | } |
| 118 | |
| 119 | final public function url() |
| 120 | { |
| 121 | if ($this->use_cache()) { |
| 122 | return $this->cache->cached_url; |
| 123 | } |
| 124 | |
| 125 | if (is_null($this->cached_url)) { |
| 126 | $this->cached_url = $this->calculate_url(); |
| 127 | } |
| 128 | |
| 129 | return $this->cached_url; |
| 130 | } |
| 131 | |
| 132 | final public function path() |
| 133 | { |
| 134 | $url = self::url(); |
| 135 | $site_url = site_url(); |
| 136 | |
| 137 | if ($url == $site_url) { |
| 138 | return '/'; |
| 139 | } elseif (substr($url, 0, strlen($site_url)) == $site_url) { |
| 140 | return substr($url, strlen($site_url)); |
| 141 | } else { |
| 142 | return $url; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | final public function title() |
| 147 | { |
| 148 | if ($this->use_cache()) { |
| 149 | return $this->cache->cached_title; |
| 150 | } |
| 151 | |
| 152 | if (is_null($this->cached_title)) { |
| 153 | $this->cached_title = $this->calculate_title(); |
| 154 | } |
| 155 | |
| 156 | return strlen($this->cached_title) > 0 ? $this->cached_title : '(no title)'; |
| 157 | } |
| 158 | |
| 159 | final public function type() |
| 160 | { |
| 161 | if ($this->use_cache()) { |
| 162 | return $this->cache->cached_type; |
| 163 | } |
| 164 | |
| 165 | if (is_null($this->cached_type)) { |
| 166 | $this->cached_type = $this->calculate_type(); |
| 167 | } |
| 168 | |
| 169 | return $this->cached_type; |
| 170 | } |
| 171 | |
| 172 | final public function type_label() |
| 173 | { |
| 174 | if ($this->use_cache()) { |
| 175 | return $this->cache->cached_type_label; |
| 176 | } |
| 177 | |
| 178 | if (is_null($this->cached_type_label)) { |
| 179 | $this->cached_type_label = $this->calculate_type_label(); |
| 180 | } |
| 181 | |
| 182 | return $this->cached_type_label; |
| 183 | } |
| 184 | |
| 185 | final public function icon() |
| 186 | { |
| 187 | if (is_null($this->cached_icon)) { |
| 188 | $this->cached_icon = $this->calculate_icon(); |
| 189 | } |
| 190 | |
| 191 | return $this->cached_icon; |
| 192 | } |
| 193 | |
| 194 | final public function author() |
| 195 | { |
| 196 | if ($this->use_cache()) { |
| 197 | return $this->cache->cached_author; |
| 198 | } |
| 199 | |
| 200 | if (is_null($this->cached_author)) { |
| 201 | $this->cached_author = $this->calculate_author(); |
| 202 | } |
| 203 | |
| 204 | return $this->cached_author; |
| 205 | } |
| 206 | |
| 207 | final public function author_id() |
| 208 | { |
| 209 | if ($this->use_cache()) { |
| 210 | return $this->cache->cached_author_id; |
| 211 | } |
| 212 | |
| 213 | if (is_null($this->cached_author_id)) { |
| 214 | $this->cached_author_id = $this->calculate_author_id(); |
| 215 | } |
| 216 | |
| 217 | return $this->cached_author_id; |
| 218 | } |
| 219 | |
| 220 | final public function avatar() |
| 221 | { |
| 222 | if (is_null($this->cached_avatar)) { |
| 223 | $this->cached_avatar = $this->calculate_avatar(); |
| 224 | } |
| 225 | |
| 226 | return $this->cached_avatar; |
| 227 | } |
| 228 | |
| 229 | final public function date() |
| 230 | { |
| 231 | if (is_null($this->cached_date)) { |
| 232 | $this->cached_date = $this->calculate_date(); |
| 233 | } |
| 234 | |
| 235 | return $this->cached_date; |
| 236 | } |
| 237 | |
| 238 | final public function category(...$args) |
| 239 | { |
| 240 | return $this->calculate_category(...$args); |
| 241 | } |
| 242 | |
| 243 | // |
| 244 | // Below are required and optional methods that classes extending page can define |
| 245 | // |
| 246 | |
| 247 | abstract protected function resource_key(); |
| 248 | |
| 249 | abstract protected function resource_value(); |
| 250 | |
| 251 | abstract protected function calculate_is_deleted(): bool; |
| 252 | |
| 253 | abstract protected function calculate_url(); |
| 254 | |
| 255 | abstract protected function calculate_title(); |
| 256 | |
| 257 | abstract protected function calculate_type(); |
| 258 | |
| 259 | abstract protected function calculate_type_label(); |
| 260 | |
| 261 | abstract protected function calculate_icon(); |
| 262 | |
| 263 | abstract protected function calculate_author(); |
| 264 | |
| 265 | abstract protected function calculate_author_id(); |
| 266 | |
| 267 | abstract protected function calculate_avatar(); |
| 268 | |
| 269 | abstract protected function calculate_date(); |
| 270 | |
| 271 | abstract protected function calculate_category(); |
| 272 | } |
| 273 |