No Description

class-wc-admin-dashboard-setup.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Admin Dashboard - Setup
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 2.1.0
  7. */
  8. use Automattic\Jetpack\Constants;
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. exit; // Exit if accessed directly.
  11. }
  12. if ( ! class_exists( 'WC_Admin_Dashboard_Setup', false ) ) :
  13. /**
  14. * WC_Admin_Dashboard_Setup Class.
  15. */
  16. class WC_Admin_Dashboard_Setup {
  17. /**
  18. * List of tasks.
  19. *
  20. * @var array
  21. */
  22. private $tasks = array(
  23. 'store_details' => array(
  24. 'completed' => false,
  25. 'button_link' => 'admin.php?page=wc-admin&path=%2Fsetup-wizard',
  26. ),
  27. 'products' => array(
  28. 'completed' => false,
  29. 'button_link' => 'admin.php?page=wc-admin&task=products',
  30. ),
  31. 'woocommerce-payments' => array(
  32. 'completed' => false,
  33. 'button_link' => 'admin.php?page=wc-admin&path=%2Fpayments%2Fconnect',
  34. ),
  35. 'payments' => array(
  36. 'completed' => false,
  37. 'button_link' => 'admin.php?page=wc-admin&task=payments',
  38. ),
  39. 'tax' => array(
  40. 'completed' => false,
  41. 'button_link' => 'admin.php?page=wc-admin&task=tax',
  42. ),
  43. 'shipping' => array(
  44. 'completed' => false,
  45. 'button_link' => 'admin.php?page=wc-admin&task=shipping',
  46. ),
  47. 'appearance' => array(
  48. 'completed' => false,
  49. 'button_link' => 'admin.php?page=wc-admin&task=appearance',
  50. ),
  51. );
  52. /**
  53. * # of completed tasks.
  54. *
  55. * @var int
  56. */
  57. private $completed_tasks_count = 0;
  58. /**
  59. * WC_Admin_Dashboard_Setup constructor.
  60. */
  61. public function __construct() {
  62. if ( $this->should_display_widget() ) {
  63. $this->populate_general_tasks();
  64. $this->populate_payment_tasks();
  65. $this->completed_tasks_count = $this->get_completed_tasks_count();
  66. add_meta_box(
  67. 'wc_admin_dashboard_setup',
  68. __( 'WooCommerce Setup', 'woocommerce' ),
  69. array( $this, 'render' ),
  70. 'dashboard',
  71. 'normal',
  72. 'high'
  73. );
  74. }
  75. }
  76. /**
  77. * Render meta box output.
  78. */
  79. public function render() {
  80. $version = Constants::get_constant( 'WC_VERSION' );
  81. wp_enqueue_style( 'wc-dashboard-setup', WC()->plugin_url() . '/assets/css/dashboard-setup.css', array(), $version );
  82. $task = $this->get_next_task();
  83. if ( ! $task ) {
  84. return;
  85. }
  86. $button_link = $task['button_link'];
  87. $completed_tasks_count = $this->completed_tasks_count;
  88. $tasks_count = count( $this->tasks );
  89. // Given 'r' (circle element's r attr), dashoffset = ((100-$desired_percentage)/100) * PI * (r*2).
  90. $progress_percentage = ( $completed_tasks_count / $tasks_count ) * 100;
  91. $circle_r = 6.5;
  92. $circle_dashoffset = ( ( 100 - $progress_percentage ) / 100 ) * ( pi() * ( $circle_r * 2 ) );
  93. include __DIR__ . '/views/html-admin-dashboard-setup.php';
  94. }
  95. /**
  96. * Populate tasks from the database.
  97. */
  98. private function populate_general_tasks() {
  99. $tasks = get_option( 'woocommerce_task_list_tracked_completed_tasks', array() );
  100. foreach ( $tasks as $task ) {
  101. if ( isset( $this->tasks[ $task ] ) ) {
  102. $this->tasks[ $task ]['completed'] = true;
  103. $this->tasks[ $task ]['button_link'] = wc_admin_url( $this->tasks[ $task ]['button_link'] );
  104. }
  105. }
  106. }
  107. /**
  108. * Getter for $tasks
  109. *
  110. * @return array
  111. */
  112. public function get_tasks() {
  113. return $this->tasks;
  114. }
  115. /**
  116. * Return # of completed tasks
  117. */
  118. public function get_completed_tasks_count() {
  119. $completed_tasks = array_filter(
  120. $this->tasks,
  121. function( $task ) {
  122. return $task['completed'];
  123. }
  124. );
  125. return count( $completed_tasks );
  126. }
  127. /**
  128. * Get the next task.
  129. *
  130. * @return array|null
  131. */
  132. private function get_next_task() {
  133. foreach ( $this->get_tasks() as $task ) {
  134. if ( false === $task['completed'] ) {
  135. return $task;
  136. }
  137. }
  138. return null;
  139. }
  140. /**
  141. * Check to see if we should display the widget
  142. *
  143. * @return bool
  144. */
  145. private function should_display_widget() {
  146. return WC()->is_wc_admin_active() &&
  147. 'yes' !== get_option( 'woocommerce_task_list_complete' ) &&
  148. 'yes' !== get_option( 'woocommerce_task_list_hidden' );
  149. }
  150. /**
  151. * Populate payment tasks's visibility and completion
  152. */
  153. private function populate_payment_tasks() {
  154. $is_woo_payment_installed = is_plugin_active( 'woocommerce-payments/woocommerce-payments.php' );
  155. $country = explode( ':', get_option( 'woocommerce_default_country', 'US:CA' ) )[0];
  156. // woocommerce-payments requires its plugin activated and country must be US.
  157. if ( ! $is_woo_payment_installed || 'US' !== $country ) {
  158. unset( $this->tasks['woocommerce-payments'] );
  159. }
  160. // payments can't be used when woocommerce-payments exists and country is US.
  161. if ( $is_woo_payment_installed && 'US' === $country ) {
  162. unset( $this->tasks['payments'] );
  163. }
  164. if ( isset( $this->tasks['payments'] ) ) {
  165. $gateways = WC()->payment_gateways->get_available_payment_gateways();
  166. $enabled_gateways = array_filter(
  167. $gateways,
  168. function ( $gateway ) {
  169. return 'yes' === $gateway->enabled;
  170. }
  171. );
  172. $this->tasks['payments']['completed'] = ! empty( $enabled_gateways );
  173. }
  174. if ( isset( $this->tasks['woocommerce-payments'] ) ) {
  175. $wc_pay_is_connected = false;
  176. if ( class_exists( '\WC_Payments' ) ) {
  177. $wc_payments_gateway = \WC_Payments::get_gateway();
  178. $wc_pay_is_connected = method_exists( $wc_payments_gateway, 'is_connected' )
  179. ? $wc_payments_gateway->is_connected()
  180. : false;
  181. }
  182. $this->tasks['woocommerce-payments']['completed'] = $wc_pay_is_connected;
  183. }
  184. }
  185. }
  186. endif;
  187. return new WC_Admin_Dashboard_Setup();