ProductCollectionsController.php
2 years ago
ProductCollectionsListTable.php
2 years ago
ProductCollectionsScriptsController.php
2 years ago
ProductCollectionsController.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\ProductCollections; |
| 4 | |
| 5 | use SureCart\Models\ProductCollection; |
| 6 | |
| 7 | /** |
| 8 | * Handles product collections admin requests. |
| 9 | */ |
| 10 | class ProductCollectionsController { |
| 11 | /** |
| 12 | * Index. |
| 13 | */ |
| 14 | public function index() { |
| 15 | $table = new ProductCollectionsListTable(); |
| 16 | $table->prepare_items(); |
| 17 | return \SureCart::view( 'admin/product-collections/index' )->with( |
| 18 | [ |
| 19 | 'table' => $table, |
| 20 | ] |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Edit a product. |
| 26 | * |
| 27 | * @param \WP_REST_Request $request Request. |
| 28 | */ |
| 29 | public function edit( $request ) { |
| 30 | // enqueue needed script. |
| 31 | add_action( 'admin_enqueue_scripts', \SureCart::closure()->method( ProductCollectionsScriptsController::class, 'enqueue' ) ); |
| 32 | |
| 33 | $product_collection = null; |
| 34 | |
| 35 | if ( $request->query( 'id' ) ) { |
| 36 | $product_collection = ProductCollection::find( $request->query( 'id' ) ); |
| 37 | |
| 38 | if ( is_wp_error( $product_collection ) ) { |
| 39 | wp_die( implode( ' ', array_map( 'esc_html', $product_collection->get_error_messages() ) ) ); |
| 40 | } |
| 41 | |
| 42 | if ( ! empty( $product_collection ) ) { |
| 43 | $this->preloadAPIRequests( $product_collection ); |
| 44 | } |
| 45 | |
| 46 | // add product collection link. |
| 47 | add_action( |
| 48 | 'admin_bar_menu', |
| 49 | function( $wp_admin_bar ) use ( $product_collection ) { |
| 50 | $wp_admin_bar->add_node( |
| 51 | [ |
| 52 | 'id' => 'view-product-collection-page', |
| 53 | 'title' => __( 'View Collection', 'surecart' ), |
| 54 | 'href' => esc_url( $product_collection->permalink ?? '#' ), |
| 55 | 'meta' => [ |
| 56 | 'class' => empty( $product_collection->permalink ) ? 'hidden' : '', |
| 57 | ], |
| 58 | ] |
| 59 | ); |
| 60 | }, |
| 61 | 99 |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | // return view. |
| 66 | return '<div id="app"></div>'; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Preload API Requests. |
| 71 | * |
| 72 | * @param \SureCart\Models\ProductCollection $product_collection The product collection. |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function preloadAPIRequests( ProductCollection $product_collection ): void { |
| 77 | $preload_paths = array( |
| 78 | array( '/wp/v2/templates', 'OPTIONS' ), |
| 79 | '/wp/v2/settings', |
| 80 | '/wp/v2/types/wp_template?context=edit', |
| 81 | '/wp/v2/types/wp_template-part?context=edit', |
| 82 | '/wp/v2/templates?context=edit&per_page=-1', |
| 83 | '/wp/v2/template-parts?context=edit&per_page=-1', |
| 84 | '/wp/v2/users/me', |
| 85 | '/wp/v2/types?context=view', |
| 86 | '/wp/v2/types?context=edit', |
| 87 | '/wp/v2/templates/' . $product_collection->template_id . '?context=edit', |
| 88 | '/wp/v2/template-parts/' . $product_collection->template_part_id . '?context=edit', |
| 89 | '/surecart/v1/product_collections/' . $product_collection->id . '?context=edit&expand[0]=media', |
| 90 | ); |
| 91 | |
| 92 | wp_add_inline_script( |
| 93 | 'wp-api-fetch', |
| 94 | sprintf( |
| 95 | 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 96 | wp_json_encode( |
| 97 | array_reduce( |
| 98 | $preload_paths, |
| 99 | 'rest_preload_api_request', |
| 100 | array() |
| 101 | ) |
| 102 | ) |
| 103 | ), |
| 104 | 'after' |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 |