ProductScriptsController.php
3 years ago
ProductsController.php
2 years ago
ProductsListTable.php
2 years ago
ProductsController.php
138 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\Products; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\AdminController; |
| 6 | use SureCart\Models\Product; |
| 7 | use SureCartCore\Responses\RedirectResponse; |
| 8 | use SureCart\Controllers\Admin\Products\ProductsListTable; |
| 9 | |
| 10 | /** |
| 11 | * Handles product admin requests. |
| 12 | */ |
| 13 | class ProductsController extends AdminController { |
| 14 | /** |
| 15 | * Products index. |
| 16 | */ |
| 17 | public function index() { |
| 18 | $table = new ProductsListTable(); |
| 19 | $table->prepare_items(); |
| 20 | $this->withHeader( |
| 21 | [ |
| 22 | 'products' => [ |
| 23 | 'title' => __( 'Products', 'surecart' ), |
| 24 | ], |
| 25 | ] |
| 26 | ); |
| 27 | return \SureCart::view( 'admin/products/index' )->with( [ 'table' => $table ] ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Edit a product. |
| 32 | */ |
| 33 | public function edit( $request ) { |
| 34 | // enqueue needed script. |
| 35 | add_action( 'admin_enqueue_scripts', \SureCart::closure()->method( ProductScriptsController::class, 'enqueue' ) ); |
| 36 | |
| 37 | $product = null; |
| 38 | |
| 39 | if ( $request->query( 'id' ) ) { |
| 40 | $product = Product::find( $request->query( 'id' ) ); |
| 41 | |
| 42 | if ( is_wp_error( $product ) ) { |
| 43 | wp_die( implode( ' ', array_map( 'esc_html', $product->get_error_messages() ) ) ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if ( ! empty( $product ) ) { |
| 48 | $this->preloadPaths( |
| 49 | [ |
| 50 | [ '/wp/v2/templates', 'OPTIONS' ], |
| 51 | '/wp/v2/settings', |
| 52 | '/wp/v2/types/wp_template?context=edit', |
| 53 | '/wp/v2/types/wp_template-part?context=edit', |
| 54 | '/wp/v2/templates?context=edit&per_page=-1', |
| 55 | '/wp/v2/template-parts?context=edit&per_page=-1', |
| 56 | '/wp/v2/users/me', |
| 57 | '/wp/v2/types?context=view', |
| 58 | '/wp/v2/types?context=edit', |
| 59 | '/wp/v2/templates/' . $product->template_id . '?context=edit', |
| 60 | '/wp/v2/template-parts/' . $product->template_part_id . '?context=edit', |
| 61 | '/surecart/v1/products/' . $product->id . '?context=edit', |
| 62 | // '/surecart/v1/product_medias?context=edit&product_ids[0]=' . $product->id . '&per_page=100', |
| 63 | // '/surecart/v1/prices?context=edit&product_ids[0]=' . $product->id . '&per_page=100', |
| 64 | '/surecart/v1/integrations?context=edit&model_ids[0]=' . $product->id . '&per_page=50', |
| 65 | '/surecart/v1/integration_providers?context=edit', |
| 66 | '/surecart/v1/integration_provider_items?context=edit', |
| 67 | ] |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | // add product link. |
| 72 | add_action( |
| 73 | 'admin_bar_menu', |
| 74 | function( $wp_admin_bar ) use ( $product ) { |
| 75 | $wp_admin_bar->add_node( |
| 76 | [ |
| 77 | 'id' => 'view-product-page', |
| 78 | 'title' => __( 'View Product', 'surecart' ), |
| 79 | 'href' => esc_url( $product->permalink ?? '#' ), |
| 80 | 'meta' => [ |
| 81 | 'class' => empty( $product->permalink ) ? 'hidden' : '', |
| 82 | ], |
| 83 | ] |
| 84 | ); |
| 85 | }, |
| 86 | 99 |
| 87 | ); |
| 88 | |
| 89 | // return view. |
| 90 | return '<div id="app"></div>'; |
| 91 | } |
| 92 | |
| 93 | public function addInstantCheckoutLink( $wp_admin_bar ) { |
| 94 | $wp_admin_bar->add_node( |
| 95 | [ |
| 96 | 'id' => 'edit', |
| 97 | 'title' => __( 'Edit Product', 'surecart' ), |
| 98 | 'href' => '#', |
| 99 | ] |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Change the archived attribute in the model |
| 105 | * |
| 106 | * @param \SureCartCore\Requests\RequestInterface $request Request. |
| 107 | * @return void |
| 108 | */ |
| 109 | public function toggleArchive( $request ) { |
| 110 | $product = Product::find( $request->query( 'id' ) ); |
| 111 | |
| 112 | if ( is_wp_error( $product ) ) { |
| 113 | wp_die( implode( ' ', array_map( 'esc_html', $product->get_error_messages() ) ) ); |
| 114 | } |
| 115 | |
| 116 | $updated = $product->update( |
| 117 | [ |
| 118 | 'archived' => ! (bool) $product->archived, |
| 119 | ] |
| 120 | ); |
| 121 | |
| 122 | if ( is_wp_error( $updated ) ) { |
| 123 | wp_die( implode( ' ', array_map( 'esc_html', $updated->get_error_messages() ) ) ); |
| 124 | } |
| 125 | |
| 126 | \SureCart::flash()->add( |
| 127 | 'success', |
| 128 | $updated->archived ? __( 'Product archived.', 'surecart' ) : __( 'Product restored.', 'surecart' ) |
| 129 | ); |
| 130 | |
| 131 | return $this->redirectBack( $request ); |
| 132 | } |
| 133 | |
| 134 | public function redirectBack( $request ) { |
| 135 | return ( new RedirectResponse( $request ) )->back(); |
| 136 | } |
| 137 | } |
| 138 |