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
wc_stats.php
3 years ago
page.php
259 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | abstract class Page |
| 6 | { |
| 7 | use View_Stats; |
| 8 | |
| 9 | private $id; |
| 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->resource = $row->resource ?? null; |
| 28 | $this->set_view_stats($row); |
| 29 | } |
| 30 | |
| 31 | final public function is_resource_type($type): bool |
| 32 | { |
| 33 | return $this->resource == $type; |
| 34 | } |
| 35 | |
| 36 | final public function is_deleted(): bool |
| 37 | { |
| 38 | if (!is_null($this->is_deleted)) { |
| 39 | return $this->is_deleted; |
| 40 | } |
| 41 | |
| 42 | $this->is_deleted = $this->calculate_is_deleted(); |
| 43 | |
| 44 | return $this->is_deleted; |
| 45 | } |
| 46 | |
| 47 | private function use_cache(): bool |
| 48 | { |
| 49 | if (!is_null($this->cache)) { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | $deleted = $this->is_deleted(); |
| 54 | |
| 55 | if ($deleted) { |
| 56 | $this->cache = $this->get_cache(); |
| 57 | } |
| 58 | |
| 59 | return $deleted; |
| 60 | } |
| 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( |
| 69 | "SELECT * FROM $resources_table WHERE $resource_key = %s", |
| 70 | $resource_value |
| 71 | ); |
| 72 | |
| 73 | return $wpdb->get_row($query); |
| 74 | } |
| 75 | |
| 76 | final public function update_cache(): void |
| 77 | { |
| 78 | global $wpdb; |
| 79 | $resources_table = Query::get_table_name(Query::RESOURCES); |
| 80 | $resource_key = $this->resource_key(); |
| 81 | $resource_value = $this->resource_value(); |
| 82 | $query = $wpdb->prepare( |
| 83 | "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", |
| 84 | $this->calculate_title(), |
| 85 | $this->calculate_url(), |
| 86 | $this->calculate_type(), |
| 87 | $this->calculate_type_label(), |
| 88 | $this->calculate_author_id(), |
| 89 | $this->calculate_author(), |
| 90 | $this->calculate_date(), |
| 91 | $this->calculate_category(), |
| 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 url($full_url = false) |
| 110 | { |
| 111 | if ($this->use_cache()) { |
| 112 | $url = $this->cache->cached_url; |
| 113 | } else { |
| 114 | $url = $this->calculate_url(); |
| 115 | } |
| 116 | |
| 117 | if ($full_url) { |
| 118 | return $url; |
| 119 | } else { |
| 120 | return Request::path_relative_to_site_url($url); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | final public function title() |
| 125 | { |
| 126 | if ($this->use_cache()) { |
| 127 | return $this->cache->cached_title; |
| 128 | } |
| 129 | |
| 130 | if (is_null($this->cached_title)) { |
| 131 | $this->cached_title = $this->calculate_title(); |
| 132 | } |
| 133 | |
| 134 | return strlen($this->cached_title) > 0 ? $this->cached_title : '(no title)'; |
| 135 | } |
| 136 | |
| 137 | final public function type($raw = false) |
| 138 | { |
| 139 | if ($raw) { |
| 140 | if ($this->use_cache()) { |
| 141 | return $this->cache->cached_type; |
| 142 | } |
| 143 | |
| 144 | if (is_null($this->cached_type)) { |
| 145 | $this->cached_type = $this->calculate_type(); |
| 146 | } |
| 147 | |
| 148 | return $this->cached_type; |
| 149 | } else { |
| 150 | if ($this->use_cache()) { |
| 151 | return $this->cache->cached_type_label; |
| 152 | } |
| 153 | |
| 154 | if (is_null($this->cached_type_label)) { |
| 155 | $this->cached_type_label = $this->calculate_type_label(); |
| 156 | } |
| 157 | |
| 158 | return $this->cached_type_label; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | final public function icon() |
| 163 | { |
| 164 | if (is_null($this->cached_icon)) { |
| 165 | $this->cached_icon = $this->calculate_icon(); |
| 166 | } |
| 167 | |
| 168 | return $this->cached_icon; |
| 169 | } |
| 170 | |
| 171 | final public function author() |
| 172 | { |
| 173 | if ($this->use_cache()) { |
| 174 | return $this->cache->cached_author; |
| 175 | } |
| 176 | |
| 177 | if (is_null($this->cached_author)) { |
| 178 | $this->cached_author = $this->calculate_author(); |
| 179 | } |
| 180 | |
| 181 | return $this->cached_author; |
| 182 | } |
| 183 | |
| 184 | final public function author_id() |
| 185 | { |
| 186 | if ($this->use_cache()) { |
| 187 | return $this->cache->cached_author_id; |
| 188 | } |
| 189 | |
| 190 | if (is_null($this->cached_author_id)) { |
| 191 | $this->cached_author_id = $this->calculate_author_id(); |
| 192 | } |
| 193 | |
| 194 | return $this->cached_author_id; |
| 195 | } |
| 196 | |
| 197 | final public function avatar() |
| 198 | { |
| 199 | if (is_null($this->cached_avatar)) { |
| 200 | $this->cached_avatar = $this->calculate_avatar(); |
| 201 | } |
| 202 | |
| 203 | return $this->cached_avatar; |
| 204 | } |
| 205 | |
| 206 | final public function date() |
| 207 | { |
| 208 | if (is_null($this->cached_date)) { |
| 209 | $this->cached_date = $this->calculate_date(); |
| 210 | } |
| 211 | |
| 212 | return $this->cached_date; |
| 213 | } |
| 214 | |
| 215 | final public function category(...$args) |
| 216 | { |
| 217 | if (is_null($this->cached_category)) { |
| 218 | $this->cached_category = $this->calculate_category(...$args); |
| 219 | } |
| 220 | |
| 221 | return $this->cached_category; |
| 222 | } |
| 223 | |
| 224 | public function most_popular_subtitle(): ?string |
| 225 | { |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | // |
| 230 | // Below are required and optional methods that classes extending page can define |
| 231 | // |
| 232 | |
| 233 | abstract protected function resource_key(); |
| 234 | |
| 235 | abstract protected function resource_value(); |
| 236 | |
| 237 | abstract protected function calculate_is_deleted(): bool; |
| 238 | |
| 239 | abstract protected function calculate_url(); |
| 240 | |
| 241 | abstract protected function calculate_title(); |
| 242 | |
| 243 | abstract protected function calculate_type(); |
| 244 | |
| 245 | abstract protected function calculate_type_label(); |
| 246 | |
| 247 | abstract protected function calculate_icon(); |
| 248 | |
| 249 | abstract protected function calculate_author(); |
| 250 | |
| 251 | abstract protected function calculate_author_id(); |
| 252 | |
| 253 | abstract protected function calculate_avatar(); |
| 254 | |
| 255 | abstract protected function calculate_date(); |
| 256 | |
| 257 | abstract protected function calculate_category(); |
| 258 | } |
| 259 |