.gitkeep
3 years ago
BasePageController.php
2 years ago
BuyPageController.php
2 years ago
CollectionPageController.php
2 years ago
DashboardController.php
3 years ago
ProductPageController.php
2 years ago
PurchaseController.php
3 years ago
SubscriptionsController.php
3 years ago
WebhookController.php
2 years ago
BasePageController.php
173 lines
| 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 | // maybe add json schema. |
| 90 | add_action( 'wp_head', [ $this, 'displaySchema' ] ); |
| 91 | // add meta title and description. |
| 92 | add_action( 'wp_head', [ $this, 'addSeoMetaData' ] ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Add meta title and description. |
| 97 | * |
| 98 | * @return void |
| 99 | */ |
| 100 | public function addSeoMetaData(): void { |
| 101 | ?> |
| 102 | <!-- Primary Meta --> |
| 103 | <meta name="title" content="<?php echo esc_attr( sanitize_text_field( $this->model->page_title ) ); ?>"> |
| 104 | <meta name="description" content="<?php echo esc_attr( sanitize_text_field( $this->model->meta_description ) ); ?>"> |
| 105 | |
| 106 | <!-- Open Graph --> |
| 107 | <meta property="og:locale" content="<?php echo esc_attr( get_locale() ); ?>" /> |
| 108 | <meta property="og:type" content="website" /> |
| 109 | <meta property="og:title" content="<?php echo esc_attr( $this->model->page_title ); ?>" /> |
| 110 | <meta property="og:description" content="<?php echo esc_attr( sanitize_text_field( $this->model->meta_description ) ); ?>" /> |
| 111 | <meta property="og:url" content="<?php echo esc_url( $this->model->permalink ); ?>" /> |
| 112 | <meta property="og:site_name" content="<?php bloginfo( 'name' ); ?>" /> |
| 113 | <?php if ( ! empty( $this->model->getImageUrl( 800 ) ) ) { ?> |
| 114 | <meta property="og:image" content="<?php echo esc_url( $this->model->getImageUrl( 800 ) ); ?>" /> |
| 115 | <?php } ?> |
| 116 | <?php |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Display the JSON Schema. |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | public function displaySchema(): void { |
| 125 | $schema = $this->model->getJsonSchemaArray() ?? []; |
| 126 | |
| 127 | if ( empty( $schema ) ) { |
| 128 | return; |
| 129 | } |
| 130 | ?> |
| 131 | <script type="application/ld+json"><?php echo wp_json_encode( $schema ); ?></script> |
| 132 | <?php |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Preload the product image. |
| 137 | * |
| 138 | * @return void |
| 139 | */ |
| 140 | public function preloadImage(): void { |
| 141 | if ( empty( $this->model->image_url ) ) { |
| 142 | return; |
| 143 | } |
| 144 | ?> |
| 145 | <link rel="preload" href="<?php echo esc_url( $this->model->image_url ); ?>" as="image"> |
| 146 | <?php |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Update the document title name to match the model[eg-product] name. |
| 151 | * |
| 152 | * @param array $parts The parts of the document title. |
| 153 | */ |
| 154 | public function documentTitle( $parts ): array { |
| 155 | $parts['title'] = esc_html( sanitize_text_field( $this->model->page_title ?? $parts['title'] ) ); |
| 156 | return $parts; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Disallow the pre title. |
| 161 | * |
| 162 | * @param string $title The title. |
| 163 | * |
| 164 | * @return string |
| 165 | */ |
| 166 | public function disallowPreTitle( $title ): string { |
| 167 | if ( ! empty( $this->model->id ) ) { |
| 168 | return ''; |
| 169 | } |
| 170 | return $title; |
| 171 | } |
| 172 | } |
| 173 |