helper
3 years ago
importers
3 years ago
list-tables
3 years ago
marketplace-suggestions
3 years ago
meta-boxes
3 years ago
notes
3 years ago
plugin-updates
5 years ago
reports
3 years ago
settings
3 years ago
views
3 years ago
class-wc-admin-addons.php
3 years ago
class-wc-admin-api-keys-table-list.php
6 years ago
class-wc-admin-api-keys.php
6 years ago
class-wc-admin-assets.php
3 years ago
class-wc-admin-attributes.php
3 years ago
class-wc-admin-customize.php
5 years ago
class-wc-admin-dashboard-setup.php
3 years ago
class-wc-admin-dashboard.php
3 years ago
class-wc-admin-duplicate-product.php
5 years ago
class-wc-admin-exporters.php
3 years ago
class-wc-admin-help.php
4 years ago
class-wc-admin-importers.php
3 years ago
class-wc-admin-log-table-list.php
5 years ago
class-wc-admin-menus.php
3 years ago
class-wc-admin-meta-boxes.php
3 years ago
class-wc-admin-notices.php
3 years ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
3 years ago
class-wc-admin-post-types.php
3 years ago
class-wc-admin-profile.php
4 years ago
class-wc-admin-reports.php
5 years ago
class-wc-admin-settings.php
3 years ago
class-wc-admin-setup-wizard.php
4 years ago
class-wc-admin-status.php
3 years ago
class-wc-admin-taxonomies.php
3 years ago
class-wc-admin-webhooks-table-list.php
4 years ago
class-wc-admin-webhooks.php
3 years ago
class-wc-admin.php
4 years ago
wc-admin-functions.php
3 years ago
wc-meta-box-functions.php
3 years ago
class-wc-admin-dashboard-setup.php
202 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Dashboard - Setup |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | * @version 2.1.0 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Constants; |
| 10 | use Automattic\WooCommerce\Admin\Features\Features; |
| 11 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; // Exit if accessed directly. |
| 15 | } |
| 16 | |
| 17 | if ( ! class_exists( 'WC_Admin_Dashboard_Setup', false ) ) : |
| 18 | |
| 19 | /** |
| 20 | * WC_Admin_Dashboard_Setup Class. |
| 21 | */ |
| 22 | class WC_Admin_Dashboard_Setup { |
| 23 | |
| 24 | /** |
| 25 | * Check for task list initialization. |
| 26 | */ |
| 27 | private $initalized = false; |
| 28 | |
| 29 | /** |
| 30 | * The task list. |
| 31 | */ |
| 32 | private $task_list = null; |
| 33 | |
| 34 | /** |
| 35 | * The tasks. |
| 36 | */ |
| 37 | private $tasks = null; |
| 38 | |
| 39 | /** |
| 40 | * # of completed tasks. |
| 41 | * |
| 42 | * @var int |
| 43 | */ |
| 44 | private $completed_tasks_count = 0; |
| 45 | |
| 46 | /** |
| 47 | * WC_Admin_Dashboard_Setup constructor. |
| 48 | */ |
| 49 | public function __construct() { |
| 50 | if ( $this->should_display_widget() ) { |
| 51 | add_meta_box( |
| 52 | 'wc_admin_dashboard_setup', |
| 53 | __( 'WooCommerce Setup', 'woocommerce' ), |
| 54 | array( $this, 'render' ), |
| 55 | 'dashboard', |
| 56 | 'normal', |
| 57 | 'high' |
| 58 | ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Render meta box output. |
| 64 | */ |
| 65 | public function render() { |
| 66 | $version = Constants::get_constant( 'WC_VERSION' ); |
| 67 | wp_enqueue_style( 'wc-dashboard-setup', WC()->plugin_url() . '/assets/css/dashboard-setup.css', array(), $version ); |
| 68 | |
| 69 | $task = $this->get_next_task(); |
| 70 | if ( ! $task ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $button_link = $this->get_button_link( $task ); |
| 75 | $completed_tasks_count = $this->get_completed_tasks_count(); |
| 76 | $step_number = $this->get_completed_tasks_count() + 1; |
| 77 | $tasks_count = count( $this->get_tasks() ); |
| 78 | |
| 79 | // Given 'r' (circle element's r attr), dashoffset = ((100-$desired_percentage)/100) * PI * (r*2). |
| 80 | $progress_percentage = ( $completed_tasks_count / $tasks_count ) * 100; |
| 81 | $circle_r = 6.5; |
| 82 | $circle_dashoffset = ( ( 100 - $progress_percentage ) / 100 ) * ( pi() * ( $circle_r * 2 ) ); |
| 83 | |
| 84 | include __DIR__ . '/views/html-admin-dashboard-setup.php'; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the button link for a given task. |
| 89 | * |
| 90 | * @param Task $task Task. |
| 91 | * @return string |
| 92 | */ |
| 93 | public function get_button_link( $task ) { |
| 94 | $url = $task->get_json()['actionUrl']; |
| 95 | |
| 96 | if ( substr( $url, 0, 4 ) === 'http' ) { |
| 97 | return $url; |
| 98 | } elseif ( $url ) { |
| 99 | return wc_admin_url( '&path=' . $url ); |
| 100 | } |
| 101 | |
| 102 | return admin_url( 'admin.php?page=wc-admin&task=' . $task->get_id() ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get the task list. |
| 107 | * |
| 108 | * @return array |
| 109 | */ |
| 110 | public function get_task_list() { |
| 111 | if ( $this->task_list || $this->initalized ) { |
| 112 | return $this->task_list; |
| 113 | } |
| 114 | |
| 115 | $this->set_task_list( TaskLists::get_list( 'setup' ) ); |
| 116 | $this->initalized = true; |
| 117 | return $this->task_list; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Set the task list. |
| 122 | */ |
| 123 | public function set_task_list( $task_list ) { |
| 124 | return $this->task_list = $task_list; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the tasks. |
| 129 | * |
| 130 | * @return array |
| 131 | */ |
| 132 | public function get_tasks() { |
| 133 | if ( $this->tasks ) { |
| 134 | return $this->tasks; |
| 135 | } |
| 136 | |
| 137 | $this->tasks = $this->get_task_list()->get_viewable_tasks(); |
| 138 | return $this->tasks; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Return # of completed tasks |
| 143 | * |
| 144 | * @return integer |
| 145 | */ |
| 146 | public function get_completed_tasks_count() { |
| 147 | $completed_tasks = array_filter( |
| 148 | $this->get_tasks(), |
| 149 | function( $task ) { |
| 150 | return $task->is_complete(); |
| 151 | } |
| 152 | ); |
| 153 | |
| 154 | return count( $completed_tasks ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get the next task. |
| 159 | * |
| 160 | * @return array|null |
| 161 | */ |
| 162 | private function get_next_task() { |
| 163 | foreach ( $this->get_tasks() as $task ) { |
| 164 | if ( false === $task->is_complete() ) { |
| 165 | return $task; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Check to see if we should display the widget |
| 174 | * |
| 175 | * @return bool |
| 176 | */ |
| 177 | public function should_display_widget() { |
| 178 | if ( ! class_exists( 'Automattic\WooCommerce\Admin\Features\Features' ) || ! class_exists( 'Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists' ) ) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | if ( ! Features::is_enabled( 'onboarding' ) || ! WC()->is_wc_admin_active() ) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if ( ! $this->get_task_list() || $this->get_task_list()->is_hidden() || $this->get_task_list()->is_complete() ) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | endif; |
| 200 | |
| 201 | return new WC_Admin_Dashboard_Setup(); |
| 202 |