| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP; |
| 4 |
|
| 5 |
class Admin { |
| 6 |
|
| 7 |
/** |
| 8 |
* Admin page slug. |
| 9 |
*/ |
| 10 |
public const PAGE_SLUG = 'flywp'; |
| 11 |
|
| 12 |
/** |
| 13 |
* Screen name. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
public const SCREEN_NAME = 'dashboard_page_flywp'; |
| 18 |
|
| 19 |
public $fastcgi = null; |
| 20 |
public $litespeed = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Admin constructor. |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
add_action( 'admin_menu', [ $this, 'register_admin_page' ] ); |
| 27 |
|
| 28 |
$this->fastcgi = new Admin\Fastcgi_Cache(); |
| 29 |
$this->litespeed = new Admin\Litespeed(); |
| 30 |
|
| 31 |
new Admin\Adminbar(); |
| 32 |
new Admin\Opcache(); |
| 33 |
new Admin\Plugins(); |
| 34 |
new Admin\Email(); |
| 35 |
new Admin\Optimizations(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Register admin page. |
| 40 |
*/ |
| 41 |
public function register_admin_page() { |
| 42 |
$hook = add_dashboard_page( |
| 43 |
__( 'FlyWP', 'flywp' ), |
| 44 |
__( 'FlyWP', 'flywp' ), |
| 45 |
'manage_options', |
| 46 |
self::PAGE_SLUG, |
| 47 |
[ $this, 'render_admin_page' ] |
| 48 |
); |
| 49 |
|
| 50 |
add_action( "admin_print_styles-{$hook}", [ $this, 'enqueue_styles' ] ); |
| 51 |
add_action( "admin_print_scripts-{$hook}", [ $this, 'enqueue_js' ] ); |
| 52 |
add_filter( 'removable_query_args', [ $this, 'removable_query_args' ] ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get admin page url. |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function page_url() { |
| 61 |
return admin_url( 'index.php?page=' . self::PAGE_SLUG ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Add query args to removable query args. |
| 66 |
* |
| 67 |
* @param array $args |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function removable_query_args( $args ) { |
| 72 |
$args[] = 'fly-notice'; |
| 73 |
|
| 74 |
return $args; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Enqueue admin styles. |
| 79 |
*/ |
| 80 |
public function enqueue_styles() { |
| 81 |
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 82 |
|
| 83 |
wp_enqueue_style( 'flywp-admin-styles', FLYWP_PLUGIN_URL . '/assets/css/app' . $min . '.css', [], FLYWP_VERSION ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Enqueue admin scripts. |
| 88 |
*/ |
| 89 |
public function enqueue_js() { |
| 90 |
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 91 |
|
| 92 |
wp_enqueue_script( 'flywp-admin-js', FLYWP_PLUGIN_URL . '/assets/js/admin' . $min . '.js', [ 'jquery' ], FLYWP_VERSION, true ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Render admin page. |
| 97 |
*/ |
| 98 |
public function render_admin_page() { |
| 99 |
$tabs = [ |
| 100 |
'cache' => __( 'Caching', 'flywp' ), |
| 101 |
'email' => __( 'Email', 'flywp' ), |
| 102 |
'optimizations' => __( 'Optimizations', 'flywp' ), |
| 103 |
]; |
| 104 |
|
| 105 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 106 |
$tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : ''; |
| 107 |
$active_tab = array_key_exists( $tab, $tabs ) ? $tab : 'cache'; |
| 108 |
$site_info = $this->fetch_site_info(); |
| 109 |
$app_site_url = $this->get_site_url( $site_info ); |
| 110 |
|
| 111 |
include FLYWP_PLUGIN_DIR . '/views/admin.php'; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Fetch site info. |
| 116 |
* |
| 117 |
* @return array|false |
| 118 |
*/ |
| 119 |
private function fetch_site_info() { |
| 120 |
$transient_key = 'flywp_site_info'; |
| 121 |
$site_info = get_transient( $transient_key ); |
| 122 |
|
| 123 |
if ( ! $site_info ) { |
| 124 |
$site_info = flywp()->flyapi->site_info(); |
| 125 |
|
| 126 |
if ( isset( $site_info['error'] ) ) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
set_transient( $transient_key, $site_info, DAY_IN_SECONDS ); |
| 131 |
} |
| 132 |
|
| 133 |
return $site_info; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get site URL. |
| 138 |
* |
| 139 |
* @param array|false $info |
| 140 |
* |
| 141 |
* @return string |
| 142 |
*/ |
| 143 |
private function get_site_url( $info ) { |
| 144 |
if ( ! $info ) { |
| 145 |
return 'https://app.flywp.com'; |
| 146 |
} |
| 147 |
|
| 148 |
return sprintf( |
| 149 |
'https://app.flywp.com/site/%d', |
| 150 |
$info['id'] |
| 151 |
); |
| 152 |
} |
| 153 |
} |
| 154 |
|