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