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