PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.7.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.7.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Controllers / Admin / ProductCollections / ProductCollectionsController.php
ProductCollectionsController.php
124 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Controllers\Admin\ProductCollections;
4
5 use SureCart\Controllers\Admin\AdminController;
6 use SureCart\Controllers\Admin\RendersEnhancedAdminView;
7 use SureCart\Models\ProductCollection;
8
9 /**
10 * Handles product collections admin requests.
11 */
12 class ProductCollectionsController extends AdminController {
13 use RendersEnhancedAdminView;
14
15 /**
16 * Render the WP List Table view.
17 */
18 protected function renderWpListView() {
19 $table = new ProductCollectionsListTable();
20 $table->prepare_items();
21
22 $this->withHeader(
23 array(
24 'breadcrumbs' => [
25 'product-collections' => [
26 'title' => __( 'Product Collections', 'surecart' ),
27 ],
28 ],
29 'enhanced_view_promo' => admin_url( 'admin.php?page=sc-product-collections' ),
30 ),
31 );
32
33 return \SureCart::view( 'admin/product-collections/index' )->with(
34 [
35 'table' => $table,
36 ]
37 );
38 }
39
40 /**
41 * Render the SPA view for product collections.
42 */
43 protected function renderSpaView() {
44 $this->enqueueSpaScripts( ProductCollectionsScriptsController::class );
45 return $this->renderSpaShell(
46 'admin/product-collections/spa',
47 'product-collections',
48 __( 'Product Collections', 'surecart' )
49 );
50 }
51
52 /**
53 * Edit a product collection.
54 *
55 * @param \WP_REST_Request $request Request.
56 */
57 public function edit( $request ) {
58 // enqueue needed script.
59 $this->enqueueSpaScripts( ProductCollectionsScriptsController::class );
60
61 $product_collection = null;
62
63 if ( $request->query( 'id' ) ) {
64 $product_collection = ProductCollection::find( $request->query( 'id' ) );
65
66 if ( is_wp_error( $product_collection ) ) {
67 wp_die( implode( ' ', array_map( 'esc_html', $product_collection->get_error_messages() ) ) );
68 }
69
70 if ( ! empty( $product_collection ) ) {
71 $this->preloadAPIRequests( $product_collection );
72 }
73
74 // add product collection link.
75 if ( ! empty( $product_collection ) ) {
76 add_action(
77 'admin_bar_menu',
78 function ( $wp_admin_bar ) use ( $product_collection ) {
79 $wp_admin_bar->add_node(
80 [
81 'id' => 'view-product-collection-page',
82 'title' => __( 'View Collection', 'surecart' ),
83 'href' => esc_url( $product_collection->permalink ?? '#' ),
84 'meta' => [
85 'class' => empty( $product_collection->permalink ) ? 'hidden' : '',
86 ],
87 ]
88 );
89 },
90 99
91 );
92 }
93 }
94
95 return $this->renderSpaShell( 'admin/product-collections/spa' );
96 }
97
98 /**
99 * Preload API Requests.
100 *
101 * @param \SureCart\Models\ProductCollection $product_collection The product collection.
102 *
103 * @return void
104 */
105 public function preloadAPIRequests( ProductCollection $product_collection ): void {
106 $preload_paths = array(
107 array( '/wp/v2/templates', 'OPTIONS' ),
108 '/wp/v2/settings',
109 '/wp/v2/types/wp_template?context=edit',
110 '/wp/v2/types/wp_template-part?context=edit',
111 '/wp/v2/templates?context=edit&per_page=-1',
112 '/wp/v2/template-parts?context=edit&per_page=-1',
113 '/wp/v2/users/me',
114 '/wp/v2/types?context=view',
115 '/wp/v2/types?context=edit',
116 '/wp/v2/templates/' . $product_collection->template_id . '?context=edit',
117 '/wp/v2/template-parts/' . $product_collection->template_part_id . '?context=edit',
118 '/surecart/v1/product_collections/' . $product_collection->id . '?context=edit&expand[0]=media',
119 );
120
121 $this->preloadPaths( $preload_paths );
122 }
123 }
124