PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / includes / admin / class-wc-admin-dashboard-setup.php
class-wc-admin-dashboard-setup.php
241 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\OnboardingTasks\Task;
11 use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
12 use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 if ( ! class_exists( 'WC_Admin_Dashboard_Setup', false ) ) :
19
20 /**
21 * WC_Admin_Dashboard_Setup Class.
22 */
23 class WC_Admin_Dashboard_Setup {
24
25 /**
26 * Check for task list initialization.
27 */
28 private bool $initialized = false;
29
30 /**
31 * The task list.
32 */
33 private $task_list = null;
34
35 /**
36 * The tasks.
37 */
38 private $tasks = null;
39
40 /**
41 * # of completed tasks.
42 *
43 * @var int
44 */
45 private $completed_tasks_count = 0;
46
47 /**
48 * WC_Admin_Dashboard_Setup constructor.
49 */
50 public function __construct() {
51 if ( $this->should_display_widget() ) {
52 add_meta_box(
53 'wc_admin_dashboard_setup',
54 __( 'WooCommerce Setup', 'woocommerce' ),
55 array( $this, 'render' ),
56 'dashboard',
57 'normal',
58 'high'
59 );
60 }
61 }
62
63 /**
64 * Render meta box output.
65 */
66 public function render() {
67 $version = Constants::get_constant( 'WC_VERSION' );
68 wp_enqueue_style( 'wc-dashboard-setup', WC()->plugin_url() . '/assets/css/dashboard-setup.css', array(), $version );
69
70 $task = $this->get_next_task();
71 if ( ! $task ) {
72 return;
73 }
74
75 $button_link = $this->get_button_link( $task );
76 $task_header = $this->get_task_header( $task );
77 $task_is_in_progress = $task->is_in_progress();
78 $task_in_progress_label = $task_is_in_progress ? $task->in_progress_label() : '';
79 $completed_tasks_count = $this->get_completed_tasks_count();
80 $step_number = $completed_tasks_count + 1;
81 $tasks_count = count( $this->get_tasks() );
82
83 // Given 'r' (circle element's r attr), dashoffset = ((100-$desired_percentage)/100) * PI * (r*2).
84 $progress_percentage = ( $completed_tasks_count / $tasks_count ) * 100;
85 $circle_r = 6.5;
86 $circle_dashoffset = ( ( 100 - $progress_percentage ) / 100 ) * ( pi() * ( $circle_r * 2 ) );
87
88 include __DIR__ . '/views/html-admin-dashboard-setup.php';
89 }
90
91 /**
92 * Get dashboard widget header data for a task.
93 *
94 * @param Task $task Task.
95 * @return array
96 */
97 private function get_task_header( $task ) {
98 $asset_url = WC()->plugin_url() . '/assets/';
99 $task_json = $task->get_json();
100
101 $image_url = method_exists( $task, 'get_image_url' ) ? (string) $task->get_image_url() : '';
102 $image_alt = method_exists( $task, 'get_image_alt' ) ? (string) $task->get_image_alt() : '';
103
104 // Fallback to the generic WooCommerce setup illustration.
105 if ( '' === $image_url ) {
106 $image_url = $asset_url . 'images/dashboard-widget-setup.png';
107 $image_alt = __( 'WooCommerce setup illustration', 'woocommerce' );
108 }
109
110 return array(
111 'title' => $task->get_title(),
112 'content' => $task_json['content'] ?? '',
113 'button_label' => $task_json['actionLabel'] ?? $task->get_title(),
114 'image_url' => $image_url,
115 'image_alt' => $image_alt,
116 );
117 }
118
119 /**
120 * Get the button link for a given task.
121 *
122 * @param Task $task Task.
123 * @return string
124 */
125 public function get_button_link( $task ) {
126 // Check if core profiler needs completion and redirect to it.
127 if ( class_exists( OnboardingProfile::class ) ) {
128 if ( OnboardingProfile::needs_completion() ) {
129 return wc_admin_url( '&path=/setup-wizard' );
130 }
131 }
132
133 $url = (string) $task->get_json()['actionUrl'];
134
135 if ( substr( $url, 0, 4 ) === 'http' ) {
136 return $url;
137 } elseif ( $url ) {
138 return wc_admin_url( '&path=' . $url );
139 }
140
141 return admin_url( 'admin.php?page=wc-admin&task=' . $task->get_id() );
142 }
143
144 /**
145 * Get the task list.
146 *
147 * @return array
148 */
149 public function get_task_list() {
150 if ( $this->task_list || $this->initialized ) {
151 return $this->task_list;
152 }
153
154 $this->set_task_list( TaskLists::get_list( 'setup' ) );
155 $this->initialized = true;
156 return $this->task_list;
157 }
158
159 /**
160 * Set the task list.
161 */
162 public function set_task_list( $task_list ) {
163 return $this->task_list = $task_list;
164 }
165
166 /**
167 * Get the tasks.
168 *
169 * @return array
170 */
171 public function get_tasks() {
172 if ( $this->tasks ) {
173 return $this->tasks;
174 }
175
176 $this->tasks = $this->get_task_list()->get_viewable_tasks();
177 return $this->tasks;
178 }
179
180 /**
181 * Return # of completed tasks
182 *
183 * @return integer
184 */
185 public function get_completed_tasks_count() {
186 $completed_tasks = array_filter(
187 $this->get_tasks(),
188 function( $task ) {
189 return $task->is_complete();
190 }
191 );
192
193 return count( $completed_tasks );
194 }
195
196 /**
197 * Get the next task.
198 *
199 * @return Task|null
200 */
201 private function get_next_task() {
202 foreach ( $this->get_tasks() as $task ) {
203 if ( false === $task->is_complete() ) {
204 return $task;
205 }
206 }
207
208 return null;
209 }
210
211 /**
212 * Check to see if we should display the widget
213 *
214 * @return bool
215 */
216 public function should_display_widget() {
217 if ( ! class_exists( 'Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists' ) ) {
218 return false;
219 }
220
221 if ( ! WC()->is_wc_admin_active() ) {
222 return false;
223 }
224
225 if ( ! current_user_can( 'manage_woocommerce' ) ) {
226 return false;
227 }
228
229 if ( ! $this->get_task_list() || $this->get_task_list()->is_hidden() || $this->get_task_list()->is_complete() ) {
230 return false;
231 }
232
233 return true;
234 }
235
236 }
237
238 endif;
239
240 return new WC_Admin_Dashboard_Setup();
241