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