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