| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SureCart\Controllers\Web; |
| 6 |
|
| 7 |
/** |
| 8 |
* Base Page Controller Abstract class. |
| 9 |
*/ |
| 10 |
abstract class BasePageController { |
| 11 |
/** |
| 12 |
* Specified model. |
| 13 |
* |
| 14 |
* @var object |
| 15 |
*/ |
| 16 |
protected $model; |
| 17 |
|
| 18 |
/** |
| 19 |
* Set the model. |
| 20 |
* |
| 21 |
* @param object $model Model, eg- Product, ProductCollection. |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
protected function setModel( $model ): void { |
| 26 |
$this->model = $model; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Enqueue scripts. |
| 31 |
* Add data for specific store by overriding this method. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function scripts() { |
| 36 |
\SureCart::assets()->enqueueComponents(); |
| 37 |
wp_localize_script( |
| 38 |
'surecart-components', |
| 39 |
'sc', |
| 40 |
[ |
| 41 |
'store' => (object) [], |
| 42 |
] |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Handle fetching error. |
| 48 |
* |
| 49 |
* @param \WP_Error $wp_error WP Error. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function handleError( \WP_Error $wp_error ): void { |
| 54 |
$data = (array) $wp_error->get_error_data(); |
| 55 |
if ( 404 === ( $data['status'] ?? null ) ) { |
| 56 |
$this->notFound(); |
| 57 |
} |
| 58 |
|
| 59 |
wp_die( esc_html( implode( ' ', $wp_error->get_error_messages() ) ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Handle not found error. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function notFound(): void { |
| 68 |
global $wp_query; |
| 69 |
$wp_query->set_404(); |
| 70 |
status_header( 404 ); |
| 71 |
get_template_part( 404 ); |
| 72 |
exit(); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Handle filters. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function filters(): void { |
| 81 |
// set the document title. |
| 82 |
add_filter( 'document_title_parts', [ $this, 'documentTitle' ] ); |
| 83 |
// disallow pre title filter. |
| 84 |
add_filter( 'pre_get_document_title', [ $this, 'disallowPreTitle' ], 214748364 ); |
| 85 |
// add scripts. |
| 86 |
add_action( 'wp_enqueue_scripts', [ $this, 'scripts' ] ); |
| 87 |
// preload image. |
| 88 |
add_action( 'wp_head', [ $this, 'preloadImage' ] ); |
| 89 |
// add meta title and description. |
| 90 |
add_action( 'wp_head', [ $this, 'addSeoMetaData' ] ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Add meta title and description. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function addSeoMetaData(): void { |
| 99 |
?> |
| 100 |
<!-- Primary Meta --> |
| 101 |
<meta name="title" content="<?php echo esc_attr( sanitize_text_field( $this->model->page_title ) ); ?>"> |
| 102 |
<meta name="description" content="<?php echo esc_attr( sanitize_text_field( $this->model->meta_description ) ); ?>"> |
| 103 |
|
| 104 |
<!-- Canonical --> |
| 105 |
<link rel="canonical" href="<?php echo esc_url( $this->model->permalink ); ?>"> |
| 106 |
|
| 107 |
<!-- Open Graph --> |
| 108 |
<meta property="og:locale" content="<?php echo esc_attr( get_locale() ); ?>" /> |
| 109 |
<meta property="og:type" content="website" /> |
| 110 |
<meta property="og:title" content="<?php echo esc_attr( $this->model->page_title ); ?>" /> |
| 111 |
<meta property="og:description" content="<?php echo esc_attr( sanitize_text_field( $this->model->meta_description ) ); ?>" /> |
| 112 |
<meta property="og:url" content="<?php echo esc_url( $this->model->permalink ); ?>" /> |
| 113 |
<meta property="og:site_name" content="<?php bloginfo( 'name' ); ?>" /> |
| 114 |
<?php if ( ! empty( $this->model->getImageUrl( 800 ) ) ) { ?> |
| 115 |
<meta property="og:image" content="<?php echo esc_url( $this->model->getImageUrl( 800 ) ); ?>" /> |
| 116 |
<?php } ?> |
| 117 |
<?php |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Preload the product image. |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public function preloadImage(): void { |
| 126 |
if ( empty( $this->model->image_url ) ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
?> |
| 130 |
<link rel="preload" href="<?php echo esc_url( $this->model->image_url ); ?>" as="image"> |
| 131 |
<?php |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Update the document title name to match the model[eg-product] name. |
| 136 |
* |
| 137 |
* @param array $parts The parts of the document title. |
| 138 |
*/ |
| 139 |
public function documentTitle( $parts ): array { |
| 140 |
$parts['title'] = esc_html( sanitize_text_field( $this->model->page_title ?? $parts['title'] ) ); |
| 141 |
return $parts; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Disallow the pre title. |
| 146 |
* |
| 147 |
* @param string $title The title. |
| 148 |
* |
| 149 |
* @return string |
| 150 |
*/ |
| 151 |
public function disallowPreTitle( $title ) { |
| 152 |
if ( ! empty( $this->model->id ) ) { |
| 153 |
return ''; |
| 154 |
} |
| 155 |
return $title; |
| 156 |
} |
| 157 |
} |
| 158 |
|