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