-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAdmin.php
More file actions
157 lines (128 loc) · 4.05 KB
/
Copy pathAdmin.php
File metadata and controls
157 lines (128 loc) · 4.05 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
<?php
namespace FlyWP;
class Admin {
/**
* Admin page slug.
*/
public const PAGE_SLUG = 'flywp';
/**
* Screen name.
*
* @var string
*/
public const SCREEN_NAME = 'dashboard_page_flywp';
public $fastcgi = null;
public $litespeed = null;
/**
* Admin constructor.
*/
public function __construct() {
add_action( 'admin_menu', [ $this, 'register_admin_page' ] );
$this->fastcgi = new Admin\Fastcgi_Cache();
$this->litespeed = new Admin\Litespeed();
new Admin\Adminbar();
new Admin\Opcache();
new Admin\Plugins();
new Admin\Email();
new Admin\Optimizations();
}
/**
* Register admin page.
*/
public function register_admin_page() {
$hook = add_dashboard_page(
__( 'FlyWP', 'flywp' ),
__( 'FlyWP', 'flywp' ),
'manage_options',
self::PAGE_SLUG,
[ $this, 'render_admin_page' ]
);
add_action( "admin_print_styles-{$hook}", [ $this, 'enqueue_styles' ] );
add_action( "admin_print_scripts-{$hook}", [ $this, 'enqueue_js' ] );
add_filter( 'removable_query_args', [ $this, 'removable_query_args' ] );
}
/**
* Get admin page url.
*
* @return string
*/
public function page_url() {
return admin_url( 'index.php?page=' . self::PAGE_SLUG );
}
/**
* Add query args to removable query args.
*
* @param array $args
*
* @return array
*/
public function removable_query_args( $args ) {
$args[] = 'fly-notice';
return $args;
}
/**
* Enqueue admin styles.
*/
public function enqueue_styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_style( 'flywp-admin-styles', FLYWP_PLUGIN_URL . '/assets/css/app' . $min . '.css', [], FLYWP_VERSION );
}
/**
* Enqueue admin scripts.
*/
public function enqueue_js() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script( 'flywp-admin-js', FLYWP_PLUGIN_URL . '/assets/js/admin' . $min . '.js', [ 'jquery' ], FLYWP_VERSION, true );
}
/**
* Render admin page.
*/
public function render_admin_page() {
$tabs = [
'cache' => __( 'Caching', 'flywp' ),
'email' => __( 'Email', 'flywp' ),
'optimizations' => __( 'Optimizations', 'flywp' ),
];
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : '';
$active_tab = array_key_exists( $tab, $tabs ) ? $tab : 'cache';
$site_info = $this->fetch_site_info();
$app_site_url = $this->get_site_url( $site_info );
include FLYWP_PLUGIN_DIR . '/views/admin.php';
}
/**
* Fetch site info.
*
* @return array|false
*/
private function fetch_site_info() {
$transient_key = 'flywp_site_info';
$site_info = get_transient( $transient_key );
if ( ! $site_info ) {
$site_info = flywp()->flyapi->site_info();
if ( isset( $site_info['error'] ) ) {
return false;
}
set_transient( $transient_key, $site_info, DAY_IN_SECONDS );
// Persist site_id so site-specific URLs survive transient expiry or API downtime.
if ( ! empty( $site_info['id'] ) ) {
update_option( 'flywp_site_id', (int) $site_info['id'], false );
}
}
return $site_info;
}
/**
* Get site URL.
*
* @param array|false $info
*
* @return string
*/
private function get_site_url( $info ) {
$site_id = ! empty( $info['id'] ) ? (int) $info['id'] : (int) get_option( 'flywp_site_id', 0 );
if ( ! $site_id ) {
return 'https://app.flywp.com';
}
return sprintf( 'https://app.flywp.com/site/%d', $site_id );
}
}