Campaign.php
10 months ago
Campaign_Landing_Page.php
9 months ago
Campaign_UTM_Campaign.php
9 months ago
Campaign_UTM_Medium.php
9 months ago
Campaign_UTM_Source.php
9 months ago
ClickWhale_Link_Page.php
1 year ago
Current_Traffic.php
2 years ago
Device.php
10 months ago
Form.php
1 year ago
Geo.php
10 months ago
Journey.php
5 months ago
Link.php
8 months ago
Link_Pattern.php
10 months ago
Model.php
6 months ago
Page.php
10 months 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
1 year ago
Page_Post_Type_Archive.php
1 year ago
Page_Search.php
2 years ago
Page_Singular.php
1 year ago
Page_Term_Archive.php
2 years ago
Page_Virtual.php
1 year ago
Referrer.php
6 months ago
Referrer_Type.php
9 months ago
Universal_Model_Columns.php
1 year ago
Visitor.php
9 months ago
Page.php
289 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Models; |
| 4 | |
| 5 | use IAWP\Illuminate_Builder; |
| 6 | use IAWP\Query; |
| 7 | use IAWP\Tables; |
| 8 | use IAWP\Utils\Request; |
| 9 | use IAWPSCOPED\Illuminate\Support\Str; |
| 10 | /** @internal */ |
| 11 | abstract class Page extends \IAWP\Models\Model |
| 12 | { |
| 13 | use \IAWP\Models\Universal_Model_Columns; |
| 14 | protected $row; |
| 15 | private $id; |
| 16 | private $resource; |
| 17 | private $entrances; |
| 18 | private $exits; |
| 19 | private $exit_percent; |
| 20 | private $is_deleted; |
| 21 | private $cache; |
| 22 | private $cached_title; |
| 23 | private $cached_type; |
| 24 | private $cached_type_label; |
| 25 | private $cached_icon; |
| 26 | private $cached_author_id; |
| 27 | private $cached_author; |
| 28 | private $cached_avatar; |
| 29 | private $cached_date; |
| 30 | private $cached_category; |
| 31 | public function __construct($row) |
| 32 | { |
| 33 | $this->row = $row; |
| 34 | $this->id = $row->id ?? null; |
| 35 | $this->resource = $row->resource ?? null; |
| 36 | $this->entrances = $row->entrances ?? 0; |
| 37 | $this->exits = $row->exits ?? 0; |
| 38 | $this->exit_percent = $row->exit_percent ?? 0; |
| 39 | // If $row is a full row from the database, use that for the cache |
| 40 | // Eventually, I'd like to avoid selecting resources.* and just get the cached_.* |
| 41 | // fields for the rows that are going to be shown. |
| 42 | if (\is_string($row->cached_title ?? null)) { |
| 43 | $this->cache = $row; |
| 44 | } |
| 45 | } |
| 46 | protected abstract function resource_key(); |
| 47 | protected abstract function resource_value(); |
| 48 | protected abstract function calculate_is_deleted() : bool; |
| 49 | protected abstract function calculate_url(); |
| 50 | protected abstract function calculate_title(); |
| 51 | protected abstract function calculate_type(); |
| 52 | protected abstract function calculate_type_label(); |
| 53 | protected abstract function calculate_icon(); |
| 54 | protected abstract function calculate_author(); |
| 55 | protected abstract function calculate_author_id(); |
| 56 | protected abstract function calculate_avatar(); |
| 57 | protected abstract function calculate_date(); |
| 58 | protected abstract function calculate_category(); |
| 59 | public function id() : int |
| 60 | { |
| 61 | return $this->id; |
| 62 | } |
| 63 | public function table_type() : string |
| 64 | { |
| 65 | return 'views'; |
| 66 | } |
| 67 | public function entrances() : int |
| 68 | { |
| 69 | return $this->entrances; |
| 70 | } |
| 71 | public function exits() : int |
| 72 | { |
| 73 | return $this->exits; |
| 74 | } |
| 75 | public function exit_percent() : float |
| 76 | { |
| 77 | return $this->exit_percent; |
| 78 | } |
| 79 | /** |
| 80 | * By default, pages don't have the ability to have comments. |
| 81 | * This can be overridden by a subclass to return an actual comments value. |
| 82 | * |
| 83 | * @return int|null |
| 84 | */ |
| 85 | public function comments() : ?int |
| 86 | { |
| 87 | return null; |
| 88 | } |
| 89 | public function get_singular_id() : ?int |
| 90 | { |
| 91 | if ($this->resource_key() !== 'singular_id') { |
| 92 | return null; |
| 93 | } |
| 94 | return $this->resource_value(); |
| 95 | } |
| 96 | public final function is_deleted() : bool |
| 97 | { |
| 98 | if (!\is_null($this->is_deleted)) { |
| 99 | return $this->is_deleted; |
| 100 | } |
| 101 | $this->is_deleted = $this->calculate_is_deleted(); |
| 102 | return $this->is_deleted; |
| 103 | } |
| 104 | public final function update_cache() : void |
| 105 | { |
| 106 | $url = $this->calculate_url(); |
| 107 | $cache = ['cached_title' => $this->calculate_title(), 'cached_url' => \is_string($url) ? Str::limit($url, 2083) : $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]; |
| 108 | if ($this instanceof \IAWP\Models\Page_Virtual) { |
| 109 | // Allow site owners to override page values such as the title, author, etc |
| 110 | $modified_cache = \apply_filters('iawp_override_virtual_page_details', $cache, $this->resource_value()); |
| 111 | if (\is_array($modified_cache)) { |
| 112 | $cache = $modified_cache; |
| 113 | } |
| 114 | } |
| 115 | try { |
| 116 | Illuminate_Builder::new()->from(Tables::resources())->where($this->resource_key(), '=', $this->resource_value())->update($cache); |
| 117 | } catch (\PDOException $e) { |
| 118 | // Do nothing |
| 119 | } |
| 120 | } |
| 121 | public final function url($full_url = \false) |
| 122 | { |
| 123 | if ($this->use_cache()) { |
| 124 | $url = $this->cache->cached_url; |
| 125 | } else { |
| 126 | $url = $this->calculate_url(); |
| 127 | } |
| 128 | if (\is_null($url)) { |
| 129 | return null; |
| 130 | } |
| 131 | if ($full_url) { |
| 132 | return $url; |
| 133 | } else { |
| 134 | return Request::path_relative_to_site_url($url); |
| 135 | } |
| 136 | } |
| 137 | public final function title() |
| 138 | { |
| 139 | if ($this->use_cache()) { |
| 140 | return $this->cache->cached_title; |
| 141 | } |
| 142 | if (\is_null($this->cached_title)) { |
| 143 | $this->cached_title = $this->calculate_title(); |
| 144 | } |
| 145 | return \strlen($this->cached_title) > 0 ? $this->cached_title : '(no title)'; |
| 146 | } |
| 147 | public final function type($raw = \false) |
| 148 | { |
| 149 | if ($raw) { |
| 150 | if ($this->use_cache()) { |
| 151 | return $this->cache->cached_type; |
| 152 | } |
| 153 | if (\is_null($this->cached_type)) { |
| 154 | $this->cached_type = $this->calculate_type(); |
| 155 | } |
| 156 | return $this->cached_type; |
| 157 | } else { |
| 158 | if ($this->use_cache()) { |
| 159 | return $this->cache->cached_type_label; |
| 160 | } |
| 161 | if (\is_null($this->cached_type_label)) { |
| 162 | $this->cached_type_label = $this->calculate_type_label(); |
| 163 | } |
| 164 | return $this->cached_type_label; |
| 165 | } |
| 166 | } |
| 167 | public final function icon() |
| 168 | { |
| 169 | if (\is_null($this->cached_icon)) { |
| 170 | $this->cached_icon = $this->calculate_icon(); |
| 171 | } |
| 172 | return $this->cached_icon; |
| 173 | } |
| 174 | public final function author() |
| 175 | { |
| 176 | if ($this->use_cache()) { |
| 177 | return $this->cache->cached_author; |
| 178 | } |
| 179 | if (\is_null($this->cached_author)) { |
| 180 | $this->cached_author = $this->calculate_author(); |
| 181 | } |
| 182 | return $this->cached_author; |
| 183 | } |
| 184 | public final function author_id() |
| 185 | { |
| 186 | if ($this->use_cache()) { |
| 187 | return $this->cache->cached_author_id; |
| 188 | } |
| 189 | if (\is_null($this->cached_author_id)) { |
| 190 | $this->cached_author_id = $this->calculate_author_id(); |
| 191 | } |
| 192 | return $this->cached_author_id; |
| 193 | } |
| 194 | public final function avatar() |
| 195 | { |
| 196 | if (\is_null($this->cached_avatar)) { |
| 197 | $this->cached_avatar = $this->calculate_avatar(); |
| 198 | } |
| 199 | return $this->cached_avatar; |
| 200 | } |
| 201 | public final function date() |
| 202 | { |
| 203 | if (\is_null($this->cached_date)) { |
| 204 | $this->cached_date = $this->calculate_date(); |
| 205 | } |
| 206 | return $this->cached_date; |
| 207 | } |
| 208 | public final function formatted_category() : ?string |
| 209 | { |
| 210 | $categories = $this->category(\false); |
| 211 | $category_names = []; |
| 212 | if (\count($categories) === 0) { |
| 213 | return null; |
| 214 | } |
| 215 | foreach ($categories as $category_id) { |
| 216 | $category = \get_the_category_by_ID($category_id); |
| 217 | if (!\is_wp_error($category)) { |
| 218 | $category_names[] = $category; |
| 219 | } |
| 220 | } |
| 221 | return \implode(', ', $category_names); |
| 222 | } |
| 223 | public final function category($formatted = \true) |
| 224 | { |
| 225 | if ($formatted === \true) { |
| 226 | return $this->formatted_category(); |
| 227 | } |
| 228 | if (\is_null($this->cached_category)) { |
| 229 | $this->cached_category = $this->calculate_category(); |
| 230 | } |
| 231 | return $this->cached_category; |
| 232 | } |
| 233 | public function most_popular_subtitle() : ?string |
| 234 | { |
| 235 | return null; |
| 236 | } |
| 237 | public function examiner_title() : ?string |
| 238 | { |
| 239 | return $this->title(); |
| 240 | } |
| 241 | public function examiner_url() : string |
| 242 | { |
| 243 | return \IAWPSCOPED\iawp_dashboard_url(['tab' => 'views', 'examiner' => $this->id()]); |
| 244 | } |
| 245 | private function use_cache() : bool |
| 246 | { |
| 247 | if (!\is_null($this->cache)) { |
| 248 | return \true; |
| 249 | } |
| 250 | $deleted = $this->is_deleted(); |
| 251 | if ($deleted) { |
| 252 | $this->cache = $this->get_cache(); |
| 253 | } |
| 254 | return $deleted; |
| 255 | } |
| 256 | private function get_cache() |
| 257 | { |
| 258 | global $wpdb; |
| 259 | $resources_table = Query::get_table_name(Query::RESOURCES); |
| 260 | $resource_key = $this->resource_key(); |
| 261 | $resource_value = $this->resource_value(); |
| 262 | $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE {$resource_key} = %s", $resource_value); |
| 263 | return $wpdb->get_row($query); |
| 264 | } |
| 265 | public static function from_row(object $row) : \IAWP\Models\Page |
| 266 | { |
| 267 | switch ($row->resource) { |
| 268 | case 'singular': |
| 269 | return new \IAWP\Models\Page_Singular($row); |
| 270 | case 'author_archive': |
| 271 | return new \IAWP\Models\Page_Author_Archive($row); |
| 272 | case 'date_archive': |
| 273 | return new \IAWP\Models\Page_Date_Archive($row); |
| 274 | case 'post_type_archive': |
| 275 | return new \IAWP\Models\Page_Post_Type_Archive($row); |
| 276 | case 'term_archive': |
| 277 | return new \IAWP\Models\Page_Term_Archive($row); |
| 278 | case 'search': |
| 279 | return new \IAWP\Models\Page_Search($row); |
| 280 | case 'home': |
| 281 | return new \IAWP\Models\Page_Home($row); |
| 282 | case 'virtual_page': |
| 283 | return \IAWP\Models\Page_Virtual::from($row); |
| 284 | default: |
| 285 | return new \IAWP\Models\Page_Not_Found($row); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 |