.gitkeep
3 years ago
BasePageController.php
2 years ago
BuyPageController.php
2 years ago
CheckoutFormsController.php
2 years ago
CollectionPageController.php
2 years ago
DashboardController.php
2 years ago
ProductPageController.php
2 years ago
PurchaseController.php
3 years ago
SubscriptionsController.php
3 years ago
UpsellPageController.php
2 years ago
WebhookController.php
2 years ago
CollectionPageController.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace SureCart\Controllers\Web; |
| 6 | |
| 7 | use SureCart\Models\Form; |
| 8 | |
| 9 | /** |
| 10 | * Handles Frontend Collection Pages. |
| 11 | */ |
| 12 | class CollectionPageController extends BasePageController { |
| 13 | /** |
| 14 | * Show the product collection page. |
| 15 | * |
| 16 | * @param \SureCartCore\Requests\RequestInterface $request Request. |
| 17 | * @param \SureCartCore\View $view View. |
| 18 | * |
| 19 | * @return function |
| 20 | */ |
| 21 | public function show( $request, $view ) { |
| 22 | // get the collection from the query var. |
| 23 | $collection_page_id = get_query_var( 'sc_collection_page_id' ); |
| 24 | |
| 25 | // fetch the collection by id/slug. |
| 26 | $this->model = \SureCart\Models\ProductCollection::with( [ 'image' ] )->find( $collection_page_id ); |
| 27 | if ( is_wp_error( $this->model ) ) { |
| 28 | return $this->handleError( $this->model ); |
| 29 | } |
| 30 | |
| 31 | // slug changed or we are using the id, redirect. |
| 32 | if ( $this->model->slug !== $collection_page_id ) { |
| 33 | return \SureCart::redirect()->to( $this->model->permalink ); |
| 34 | } |
| 35 | |
| 36 | set_query_var( 'sc_collection_page_id', $this->model->id ); |
| 37 | |
| 38 | // add the filters. |
| 39 | $this->filters(); |
| 40 | |
| 41 | // handle block theme. |
| 42 | if ( wp_is_block_theme() ) { |
| 43 | global $_wp_current_template_content; |
| 44 | $_wp_current_template_content = $this->model->template->content ?? ''; |
| 45 | } |
| 46 | |
| 47 | // include the default view. |
| 48 | include $view; |
| 49 | |
| 50 | return \SureCart::response(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Handle filters. |
| 55 | * |
| 56 | * @return void |
| 57 | */ |
| 58 | public function filters(): void { |
| 59 | parent::filters(); |
| 60 | |
| 61 | // add edit product collection link. |
| 62 | add_action( 'admin_bar_menu', [ $this, 'addEditCollectionLink' ], 99 ); |
| 63 | |
| 64 | // add data needed for collection to load. |
| 65 | add_filter( |
| 66 | 'surecart-components/scData', |
| 67 | function( $data ) { |
| 68 | $form = \SureCart::forms()->getDefault(); |
| 69 | |
| 70 | $data['collection_data'] = [ |
| 71 | 'collection' => $this->model, |
| 72 | 'form' => $form, |
| 73 | 'mode' => Form::getMode( $form->ID ), |
| 74 | 'checkout_link' => \SureCart::pages()->url( 'checkout' ), |
| 75 | ]; |
| 76 | |
| 77 | return $data; |
| 78 | } |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Add edit product collection link. |
| 84 | * |
| 85 | * @param \WP_Admin_Bar $wp_admin_bar Admin bar. |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | public function addEditCollectionLink( $wp_admin_bar ): void { |
| 90 | $wp_admin_bar->add_node( |
| 91 | [ |
| 92 | 'id' => 'edit-collection', |
| 93 | 'title' => __( 'Edit Product Collection', 'surecart' ), |
| 94 | 'href' => esc_url( \SureCart::getUrl()->edit( 'product_collections', $this->model->id ) ), |
| 95 | ] |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 |