| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\CPT\Pages; |
| 7 |
use FluentCart\App\Hooks\Handlers\GlobalPaymentHandler; |
| 8 |
use FluentCart\App\Models\Product; |
| 9 |
use FluentCart\App\Services\Widgets\DashboardWidget; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
|
| 12 |
class DashboardController extends Controller |
| 13 |
{ |
| 14 |
public function getOnboardingData() |
| 15 |
{ |
| 16 |
$completed = 0; |
| 17 |
$baseUrl = apply_filters('fluent_cart/admin_base_url', admin_url('admin.php?page=fluent-cart#/'), []); |
| 18 |
$steps = [ |
| 19 |
'page_setup' => [ |
| 20 |
'title' => __('Setup Pages', 'fluent-cart'), |
| 21 |
'text' => __("Customers to find what they're looking for by organising.", 'fluent-cart'), |
| 22 |
'icon' => 'Cart', |
| 23 |
'completed' => false, |
| 24 |
'hash_id' => '', |
| 25 |
'url' => $baseUrl . "settings/store-settings/pages_setup" |
| 26 |
], |
| 27 |
'store_info' => [ |
| 28 |
'title' => __('Add Details to Store', 'fluent-cart'), |
| 29 |
'text' => __('Store details such as addresses, company info etc.', 'fluent-cart'), |
| 30 |
'icon' => 'StoreIcon', |
| 31 |
'completed' => false, |
| 32 |
'hash_id' => '', |
| 33 |
'url' => $baseUrl . "settings/store-settings" |
| 34 |
], |
| 35 |
'product_info' => [ |
| 36 |
'title' => __('Add Your First Product', 'fluent-cart'), |
| 37 |
'text' => __('Share your brand story and build trust with customers.', 'fluent-cart'), |
| 38 |
'icon' => 'ShoppingCartIcon', |
| 39 |
'completed' => false, |
| 40 |
'hash_id' => '', |
| 41 |
'url' => $baseUrl . "products" |
| 42 |
], |
| 43 |
|
| 44 |
|
| 45 |
'setup_payments' => [ |
| 46 |
'title' => __('Setup Payment Methods', 'fluent-cart'), |
| 47 |
'text' => __("Choose from fast & secure online and offline payment.", 'fluent-cart'), |
| 48 |
'icon' => 'PaymentIcon', |
| 49 |
'completed' => true, |
| 50 |
'hash_id' => '', |
| 51 |
'url' => $baseUrl . "settings/payments" |
| 52 |
], |
| 53 |
]; |
| 54 |
|
| 55 |
$settings = (new StoreSettings)->get(); |
| 56 |
|
| 57 |
if ($this->isStoreInfoProvided($settings)) { |
| 58 |
$completed++; |
| 59 |
$steps['store_info']['completed'] = true; |
| 60 |
} |
| 61 |
|
| 62 |
if ($this->isProductInfoProvided()) { |
| 63 |
$completed++; |
| 64 |
$steps['product_info']['completed'] = true; |
| 65 |
} |
| 66 |
|
| 67 |
if ($this->isAllPageSetUpDone($settings)) { |
| 68 |
$completed++; |
| 69 |
$steps['page_setup']['completed'] = true; |
| 70 |
} |
| 71 |
|
| 72 |
if (!$this->isAnyPaymentModuleEnabled()) { |
| 73 |
$steps['setup_payments']['completed'] = false; |
| 74 |
} else { |
| 75 |
$completed++; |
| 76 |
} |
| 77 |
|
| 78 |
if (defined('BRICKS_VERSION')) { |
| 79 |
$steps['install_bricks_addon'] = [ |
| 80 |
'title' => __('Install Bricks Addon', 'fluent-cart'), |
| 81 |
'text' => __('Design your store pages with FluentCart elements in Bricks.', 'fluent-cart'), |
| 82 |
'icon' => 'AppsLine', |
| 83 |
'completed' => false, |
| 84 |
'hash_id' => 'fluent-cart-bricks-blocks', |
| 85 |
'url' => $baseUrl . "settings/addons" |
| 86 |
]; |
| 87 |
|
| 88 |
if (defined('FLUENT_CART_BRICKS_BLOCKS_VERSION')) { |
| 89 |
$steps['install_bricks_addon']['completed'] = true; |
| 90 |
$completed++; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (defined('ELEMENTOR_VERSION')) { |
| 95 |
$steps['install_elementor_addon'] = [ |
| 96 |
'title' => __('Install Elementor Addon', 'fluent-cart'), |
| 97 |
'text' => __('Design your store pages with FluentCart widgets in Elementor.', 'fluent-cart'), |
| 98 |
'icon' => 'AppsLine', |
| 99 |
'completed' => false, |
| 100 |
'hash_id' => 'fluent-cart-elementor-blocks', |
| 101 |
'url' => $baseUrl . "settings/addons" |
| 102 |
]; |
| 103 |
|
| 104 |
if (defined('FLUENTCART_ELEMENTOR_BLOCKS_VERSION')) { |
| 105 |
$steps['install_elementor_addon']['completed'] = true; |
| 106 |
$completed++; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (strpos(get_template(), 'Divi') !== false) { |
| 111 |
$steps['install_divi_addon'] = [ |
| 112 |
'title' => __('Install Divi Addon', 'fluent-cart'), |
| 113 |
'text' => __('Design your store pages with FluentCart modules in Divi.', 'fluent-cart'), |
| 114 |
'icon' => 'AppsLine', |
| 115 |
'completed' => false, |
| 116 |
'hash_id' => 'fluent-cart-divi-modules', |
| 117 |
'url' => $baseUrl . "settings/addons" |
| 118 |
]; |
| 119 |
|
| 120 |
// FLUENTCART_DIVI_BLOCKS_VERSION covers installs from before the slug rename |
| 121 |
if (defined('FLUENTCART_DIVI_MODULES_VERSION') || defined('FLUENTCART_DIVI_BLOCKS_VERSION')) { |
| 122 |
$steps['install_divi_addon']['completed'] = true; |
| 123 |
$completed++; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $this->response->json([ |
| 128 |
'data' => [ |
| 129 |
'steps' => $steps, |
| 130 |
'completed' => $completed |
| 131 |
] |
| 132 |
]); |
| 133 |
} |
| 134 |
|
| 135 |
protected function isStoreInfoProvided(array $settings): bool |
| 136 |
{ |
| 137 |
$storeName = Arr::get($settings, 'store_name'); |
| 138 |
$storeLogo = Arr::get($settings, 'store_logo'); |
| 139 |
return !( |
| 140 |
//$storeName === 'Fluent Cart Shop' || |
| 141 |
empty($storeName) || |
| 142 |
empty($storeLogo) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
protected function isProductInfoProvided(): bool |
| 147 |
{ |
| 148 |
return Product::query()->count() > 0; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
private function isAllPageSetUpDone(array $settings): bool |
| 153 |
{ |
| 154 |
$pages = (new Pages())->getGeneratablePage(); |
| 155 |
foreach ($pages as $pageKey => $page) { |
| 156 |
$pageKey = "{$pageKey}_page_id"; |
| 157 |
if (empty(Arr::get($settings, $pageKey))) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
} |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
private function isAnyPaymentModuleEnabled(): bool |
| 165 |
{ |
| 166 |
$gateways = (new GlobalPaymentHandler())->getAll(); |
| 167 |
foreach ($gateways as $gateway) { |
| 168 |
if ($gateway['status']) { |
| 169 |
return true; |
| 170 |
} |
| 171 |
} |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
public function getDashboardStats(): \WP_REST_Response |
| 176 |
{ |
| 177 |
return $this->sendSuccess([ |
| 178 |
'stats' => DashboardWidget::widgets() |
| 179 |
]); |
| 180 |
} |
| 181 |
} |
| 182 |
|