| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dashboard Handler |
| 4 |
* |
| 5 |
* @package Sky_Addons |
| 6 |
* @since 2.7.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Sky_Addons\Classes; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
use WP_REST_Controller; |
| 16 |
use WP_REST_Server; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_REST_Response; |
| 19 |
use WP_Error; |
| 20 |
|
| 21 |
/** |
| 22 |
* Dashboard Handler |
| 23 |
* |
| 24 |
* @since 2.7.0 |
| 25 |
*/ |
| 26 |
class Dashboard { |
| 27 |
|
| 28 |
private static $instance = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* Namespace |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $namespace; |
| 36 |
|
| 37 |
/** |
| 38 |
* Rest Base |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
|
| 43 |
protected $rest_base; |
| 44 |
|
| 45 |
/** |
| 46 |
* Construct |
| 47 |
*/ |
| 48 |
public function __construct() { |
| 49 |
$this->namespace = 'skyaddons/v1'; |
| 50 |
$this->rest_base = 'dashboard'; |
| 51 |
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Register the routes |
| 56 |
* |
| 57 |
* @since 2.7.0 |
| 58 |
*/ |
| 59 |
public function register_rest_routes() { |
| 60 |
|
| 61 |
register_rest_route( |
| 62 |
$this->namespace, |
| 63 |
'/' . $this->rest_base, |
| 64 |
[ |
| 65 |
'methods' => WP_REST_Server::READABLE, |
| 66 |
'callback' => [ $this, 'handle_dashboard' ], |
| 67 |
// 'permission_callback' => array( $this, 'get_permissions_check' ), |
| 68 |
'permission_callback' => [ $this, 'get_permissions_check' ], |
| 69 |
] |
| 70 |
); |
| 71 |
|
| 72 |
// register_rest_route( |
| 73 |
// $this->namespace, |
| 74 |
// '/' . $this->rest_base, |
| 75 |
// array( |
| 76 |
// 'methods' => WP_REST_Server::EDITABLE, |
| 77 |
// 'callback' => array( $this, 'set_dashboard' ), |
| 78 |
// 'permission_callback' => array( $this, 'update_permissions_check' ), |
| 79 |
// ) |
| 80 |
// ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Check the permissions for getting the settings |
| 85 |
* |
| 86 |
* @since 2.7.0 |
| 87 |
*/ |
| 88 |
public function get_permissions_check() { |
| 89 |
return current_user_can( 'manage_options' ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Check the permissions for updating the settings |
| 94 |
* |
| 95 |
* @since 2.7.0 |
| 96 |
*/ |
| 97 |
public function update_permissions_check() { |
| 98 |
return current_user_can( 'manage_options' ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Set Init |
| 103 |
* |
| 104 |
* @since 2.7.0 |
| 105 |
*/ |
| 106 |
public function handle_dashboard( WP_REST_Request $request ) { |
| 107 |
$params = $request->get_params(); |
| 108 |
return $this->dashboard_welcome(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Dashboard Welcome |
| 113 |
* |
| 114 |
* @return WP_REST_Response |
| 115 |
*/ |
| 116 |
public function dashboard_welcome() { |
| 117 |
$cache_key = 'dci_dashboard_welcome_data'; |
| 118 |
$cache_time = 2 * MINUTE_IN_SECONDS; // Cache for 12 hours |
| 119 |
|
| 120 |
$data = get_transient( $cache_key ); |
| 121 |
|
| 122 |
if ( false === $data ) { |
| 123 |
// $data = array( |
| 124 |
// 'last_30_days_sync' => $this->last_30_days_sync(), |
| 125 |
// ); |
| 126 |
|
| 127 |
// Set the transient |
| 128 |
set_transient( $cache_key, $data, $cache_time ); |
| 129 |
} |
| 130 |
|
| 131 |
return new WP_REST_Response( |
| 132 |
[ |
| 133 |
'message' => 'Welcome to the Dashboard!', |
| 134 |
'data' => $data, |
| 135 |
], |
| 136 |
200 |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* ChartJS BG Colors |
| 143 |
*/ |
| 144 |
public function chartjs_bg_colors( $id ) { |
| 145 |
$bg = [ |
| 146 |
'rgba(255, 99, 132, 0.4)', |
| 147 |
'rgba(54, 162, 235, 0.4)', |
| 148 |
'rgba(255, 206, 86, 0.4)', |
| 149 |
'rgba(75, 192, 192, 0.4)', |
| 150 |
'rgba(153, 102, 255, 0.4)', |
| 151 |
'rgba(255, 159, 64, 0.4)', |
| 152 |
'rgba(54, 162, 235, 0.4)', |
| 153 |
'rgba(104, 132, 245, 0.4)', |
| 154 |
'rgba(255, 99, 132, 0.4)', |
| 155 |
'rgba(54, 162, 235, 0.4)', |
| 156 |
'rgba(255, 206, 86, 0.4)', |
| 157 |
'rgba(75, 192, 192, 0.4)', |
| 158 |
'rgba(153, 102, 255, 0.4)', |
| 159 |
'rgba(255, 159, 64, 0.4)', |
| 160 |
'rgba(54, 162, 235, 0.4)', |
| 161 |
'rgba(255, 206, 86, 0.4)', |
| 162 |
'rgba(75, 192, 192, 0.4)', |
| 163 |
'rgba(153, 102, 255, 0.4)', |
| 164 |
'rgba(255, 159, 64, 0.4)', |
| 165 |
'rgba(54, 162, 235, 0.4)', |
| 166 |
'rgba(255, 206, 86, 0.4)', |
| 167 |
'rgba(75, 192, 192, 0.4)', |
| 168 |
'rgba(153, 102, 255, 0.4)', |
| 169 |
'rgba(255, 159, 64, 0.4)', |
| 170 |
'rgba(54, 162, 235, 0.4)', |
| 171 |
'rgba(255, 206, 86, 0.4)', |
| 172 |
'rgba(75, 192, 192, 0.4)', |
| 173 |
'rgba(153, 102, 255, 0.4)', |
| 174 |
'rgba(255, 159, 64, 0.4)', |
| 175 |
]; |
| 176 |
|
| 177 |
$bg = array_unique( $bg ); |
| 178 |
|
| 179 |
return ( isset( $bg[ $id ] ) ) ? $bg[ $id ] : 'rgba(255, 99, 132, 0.4)'; |
| 180 |
} |
| 181 |
} |
| 182 |
|