Block.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\Dashboard\CustomerSubscriptions; |
| 4 | |
| 5 | use SureCart\Models\Subscription; |
| 6 | use SureCart\Models\User; |
| 7 | use SureCartBlocks\Blocks\Dashboard\DashboardPage; |
| 8 | use SureCartBlocks\Controllers\SubscriptionController; |
| 9 | |
| 10 | /** |
| 11 | * Checkout block |
| 12 | */ |
| 13 | class Block extends DashboardPage { |
| 14 | /** |
| 15 | * Render the block |
| 16 | * |
| 17 | * @param array $attributes Block attributes. |
| 18 | * @param string $content Post content. |
| 19 | * |
| 20 | * @return function |
| 21 | */ |
| 22 | public function render( $attributes, $content ) { |
| 23 | if ( ! is_user_logged_in() ) { |
| 24 | return false; |
| 25 | } |
| 26 | return ( new SubscriptionController() )->preview( $attributes ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Show and individual checkout session. |
| 31 | * |
| 32 | * @param string $id Session ID. |
| 33 | * |
| 34 | * @return function |
| 35 | */ |
| 36 | public function show( $id ) { |
| 37 | return \SureCart::blocks()->render( |
| 38 | 'web/dashboard/subscriptions/show', |
| 39 | [ |
| 40 | 'id' => $id, |
| 41 | ] |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Show and individual checkout session. |
| 47 | * |
| 48 | * @return function |
| 49 | */ |
| 50 | public function edit() { |
| 51 | $id = isset( $_GET['id'] ) ? sanitize_text_field( wp_unslash( $_GET['id'] ) ) : false; |
| 52 | $tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : false; |
| 53 | $subscription = Subscription::with( [ 'price', 'price.product', 'current_period', 'product.product_group' ] )->find( $id ); |
| 54 | |
| 55 | \SureCart::assets()->addComponentData( |
| 56 | 'sc-subscription', |
| 57 | '#customer-subscription', |
| 58 | [ |
| 59 | 'heading' => $attributes['title'] ?? __( 'Update Plan', 'surecart' ), |
| 60 | 'subscription' => $subscription, |
| 61 | ] |
| 62 | ); |
| 63 | \SureCart::assets()->addComponentData( |
| 64 | 'sc-subscription-switch', |
| 65 | '#customer-subscription-switch', |
| 66 | [ |
| 67 | 'heading' => $attributes['title'] ?? __( 'Update Plan', 'surecart' ), |
| 68 | 'product-group' => $subscription->price->product->product_group ?? null, |
| 69 | 'subscription' => $subscription, |
| 70 | ] |
| 71 | ); |
| 72 | ob_start(); ?> |
| 73 | <sc-spacing style="--spacing: var(--sc-spacing-large)"> |
| 74 | <sc-breadcrumbs> |
| 75 | <sc-breadcrumb href="<?php echo esc_url( add_query_arg( [ 'tab' => $tab ], \SureCart::pages()->url( 'dashboard' ) ) ); ?>"> |
| 76 | <?php esc_html_e( 'Dashboard', 'surecart' ); ?> |
| 77 | </sc-breadcrumb> |
| 78 | <sc-breadcrumb> |
| 79 | <?php esc_html_e( 'Plan', 'surecart' ); ?> |
| 80 | </sc-breadcrumb> |
| 81 | </sc-breadcrumbs> |
| 82 | |
| 83 | <sc-subscription id="customer-subscription"></sc-subscription> |
| 84 | <sc-subscription-switch id="customer-subscription-switch"></sc-subscription-switch> |
| 85 | </sc-spacing> |
| 86 | |
| 87 | <?php |
| 88 | return ob_get_clean(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Show and individual checkout session. |
| 93 | * |
| 94 | * @param array $attributes Block attributes. |
| 95 | * |
| 96 | * @return function |
| 97 | */ |
| 98 | public function index( $attributes ) { |
| 99 | if ( ! is_user_logged_in() ) { |
| 100 | return false; |
| 101 | } |
| 102 | \SureCart::assets()->addComponentData( |
| 103 | 'sc-subscriptions-list', |
| 104 | '#customer-subscriptions-index', |
| 105 | [ |
| 106 | 'heading' => $attributes['title'] ?? __( 'Plans', 'surecart' ), |
| 107 | 'isCustomer' => User::current()->isCustomer(), |
| 108 | 'query' => [ |
| 109 | 'customer_ids' => array_values( User::current()->customerIds() ), |
| 110 | 'status' => [ 'active', 'trialing' ], |
| 111 | 'page' => 1, |
| 112 | 'per_page' => 10, |
| 113 | ], |
| 114 | ] |
| 115 | ); |
| 116 | return '<sc-subscriptions-list id="customer-subscriptions-index"></sc-subscriptions-list>'; |
| 117 | } |
| 118 | } |
| 119 |