initial
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/**
|
||||
* The admin-specific functionality of the plugin.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @package automattic/jetpack-boost
|
||||
*/
|
||||
|
||||
namespace Automattic\Jetpack_Boost\Admin;
|
||||
|
||||
use Automattic\Jetpack\Admin_UI\Admin_Menu;
|
||||
use Automattic\Jetpack\Assets;
|
||||
use Automattic\Jetpack\Boost_Speed_Score\Speed_Score;
|
||||
use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
|
||||
use Automattic\Jetpack_Boost\Lib\Analytics;
|
||||
use Automattic\Jetpack_Boost\Lib\Environment_Change_Detector;
|
||||
use Automattic\Jetpack_Boost\Lib\Premium_Features;
|
||||
use Automattic\Jetpack_Boost\Modules\Modules_Setup;
|
||||
|
||||
class Admin {
|
||||
/**
|
||||
* Menu slug.
|
||||
*/
|
||||
const MENU_SLUG = 'jetpack-boost';
|
||||
|
||||
public function init( Modules_Setup $modules ) {
|
||||
Environment_Change_Detector::init();
|
||||
|
||||
// Initiate speed scores.
|
||||
new Speed_Score( $modules->get_ready_active_optimization_modules(), 'boost-plugin' );
|
||||
|
||||
add_action( 'init', array( new Analytics(), 'init' ) );
|
||||
add_filter( 'plugin_action_links_' . JETPACK_BOOST_PLUGIN_BASE, array( $this, 'plugin_page_settings_link' ) );
|
||||
add_action( 'admin_menu', array( $this, 'handle_admin_menu' ), 1 ); // Akismet uses 4, so we use 1 to ensure both menus are added when only they exist.
|
||||
}
|
||||
|
||||
public function handle_admin_menu() {
|
||||
/**
|
||||
* Filters the number of problems shown in the Boost sidebar menu
|
||||
*
|
||||
* @param int $count the number of problems shown.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
$total_problems = apply_filters( 'jetpack_boost_total_problem_count', 0 );
|
||||
$menu_label = 'Boost'; // "Boost" is a product name, do not translate.
|
||||
if ( $total_problems ) {
|
||||
$menu_label .= sprintf( ' <span class="menu-counter count-%d"><span class="count">%d</span></span>', $total_problems, $total_problems );
|
||||
}
|
||||
|
||||
$page_suffix = Admin_Menu::add_menu(
|
||||
__( 'Jetpack Boost - Settings', 'jetpack-boost' ),
|
||||
$menu_label,
|
||||
'manage_options',
|
||||
JETPACK_BOOST_SLUG,
|
||||
array( $this, 'render_settings' ),
|
||||
2
|
||||
);
|
||||
add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts and styles for the admin page.
|
||||
*/
|
||||
public function admin_init() {
|
||||
// Clear premium features cache when the plugin settings page is loaded.
|
||||
Premium_Features::clear_cache();
|
||||
|
||||
add_action( 'admin_enqueue_scripts', array( My_Jetpack_Initializer::class, 'enqueue_scripts' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the JavaScript for the admin area.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function enqueue_scripts() {
|
||||
/**
|
||||
* Filters the internal path to the distributed assets used by the plugin
|
||||
*
|
||||
* @param string $path the path to the assets
|
||||
*/
|
||||
$internal_path = apply_filters( 'jetpack_boost_asset_internal_path', 'app/assets/dist/' );
|
||||
|
||||
$admin_js_handle = 'jetpack-boost-admin';
|
||||
|
||||
$admin_js_dependencies = array(
|
||||
'wp-i18n',
|
||||
'wp-components',
|
||||
'my_jetpack_main_app',
|
||||
);
|
||||
|
||||
Assets::register_script(
|
||||
$admin_js_handle,
|
||||
$internal_path . 'jetpack-boost.js',
|
||||
JETPACK_BOOST_PATH,
|
||||
array(
|
||||
'dependencies' => $admin_js_dependencies,
|
||||
'in_footer' => true,
|
||||
'textdomain' => 'jetpack-boost',
|
||||
'css_path' => $internal_path . 'jetpack-boost.css',
|
||||
)
|
||||
);
|
||||
|
||||
wp_localize_script(
|
||||
$admin_js_handle,
|
||||
'Jetpack_Boost',
|
||||
( new Config() )->constants()
|
||||
);
|
||||
|
||||
Assets::enqueue_script( $admin_js_handle );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get settings link.
|
||||
*
|
||||
* @param array $links the array of links.
|
||||
*/
|
||||
public function plugin_page_settings_link( $links ) {
|
||||
$settings_link = '<a href="' . admin_url( 'admin.php?page=jetpack-boost' ) . '">' . esc_html__( 'Settings', 'jetpack-boost' ) . '</a>';
|
||||
array_unshift( $links, $settings_link );
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the settings page.
|
||||
*/
|
||||
public function render_settings() {
|
||||
wp_localize_script(
|
||||
'jetpack-boost-admin',
|
||||
'wpApiSettings',
|
||||
array(
|
||||
'root' => esc_url_raw( rest_url() ),
|
||||
'nonce' => wp_create_nonce( 'wp_rest' ),
|
||||
)
|
||||
);
|
||||
?>
|
||||
<div id="jb-admin-settings"></div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Automattic\Jetpack_Boost\Admin;
|
||||
|
||||
use Automattic\Jetpack\Boost\App\Contracts\Is_Dev_Feature;
|
||||
use Automattic\Jetpack\Status;
|
||||
use Automattic\Jetpack\Status\Host;
|
||||
use Automattic\Jetpack_Boost\Lib\Cache_Compatibility;
|
||||
use Automattic\Jetpack_Boost\Modules\Features_Index;
|
||||
|
||||
/**
|
||||
* Handle the configuration constants.
|
||||
*
|
||||
* This is a global state of Jetpack Boost and passed on to the front-end.
|
||||
*/
|
||||
class Config {
|
||||
public function constants() {
|
||||
/**
|
||||
* Filters the internal path to the distributed assets used by the plugin
|
||||
*
|
||||
* @param string $path the path to the assets
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
$internal_path = apply_filters( 'jetpack_boost_asset_internal_path', 'app/assets/dist/' );
|
||||
|
||||
$constants = array(
|
||||
'version' => JETPACK_BOOST_VERSION,
|
||||
'pluginDirUrl' => untrailingslashit( JETPACK_BOOST_PLUGINS_DIR_URL ),
|
||||
'assetPath' => plugins_url( $internal_path, JETPACK_BOOST_PATH ),
|
||||
'canResizeImages' => wp_image_editor_supports( array( 'methods' => array( 'resize' ) ) ),
|
||||
'site' => array(
|
||||
'url' => get_home_url(),
|
||||
'domain' => ( new Status() )->get_site_suffix(),
|
||||
'online' => self::is_website_public(),
|
||||
'host' => $this->get_hosting_provider(),
|
||||
'hasCache' => Cache_Compatibility::has_cache(),
|
||||
),
|
||||
'api' => array(
|
||||
'namespace' => JETPACK_BOOST_REST_NAMESPACE,
|
||||
'prefix' => JETPACK_BOOST_REST_PREFIX,
|
||||
),
|
||||
'postTypes' => (object) $this->get_custom_post_types(),
|
||||
'developmentFeatures' => self::get_development_features(),
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the constants so each module can define extra ones
|
||||
*
|
||||
* @param array $constant The array of constants used by the plugin
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
return apply_filters( 'jetpack_boost_js_constants', $constants );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of features that are marked as development features.
|
||||
*
|
||||
* @return array<string, bool> Slugs of features and their status.
|
||||
*/
|
||||
private static function get_development_features() {
|
||||
$features = Features_Index::get_all_features();
|
||||
|
||||
$development_features = array();
|
||||
foreach ( $features as $feature ) {
|
||||
if ( is_subclass_of( $feature, Is_Dev_Feature::class ) ) {
|
||||
$development_features[] = ( new $feature() )->get_slug();
|
||||
}
|
||||
}
|
||||
|
||||
return $development_features;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves custom post types.
|
||||
*
|
||||
* @return array Associative array of custom post types
|
||||
* with their labels as keys and names as values.
|
||||
*/
|
||||
private static function get_custom_post_types() {
|
||||
$post_types = get_post_types(
|
||||
array(
|
||||
'public' => true,
|
||||
'_builtin' => false,
|
||||
),
|
||||
false
|
||||
);
|
||||
unset( $post_types['attachment'] );
|
||||
|
||||
$post_types = array_filter( $post_types, 'is_post_type_viewable' );
|
||||
|
||||
return wp_list_pluck( $post_types, 'label', 'name' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the hosting provider.
|
||||
* We're only interested in 'atomic' or 'woa' for now.
|
||||
*
|
||||
* @since 3.10.0
|
||||
*
|
||||
* @return string The hosting provider.
|
||||
*/
|
||||
public static function get_hosting_provider() {
|
||||
$host = new Host();
|
||||
|
||||
if ( $host->is_woa_site() ) {
|
||||
return 'woa';
|
||||
}
|
||||
|
||||
if ( $host->is_atomic_platform() ) {
|
||||
return 'atomic';
|
||||
}
|
||||
|
||||
return 'other';
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the website is publicly accessible.
|
||||
*
|
||||
* @return bool True if the website is public, false otherwise.
|
||||
*/
|
||||
public static function is_website_public() {
|
||||
return ! ( new Status() )->is_offline_mode() && ! ( new Status() )->is_private_site();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* Admin notice base class. Override this to implement each admin notice Jetpack Boost may show.
|
||||
*
|
||||
* @package automattic/jetpack-boost
|
||||
*/
|
||||
|
||||
namespace Automattic\Jetpack_Boost\Admin;
|
||||
|
||||
use Automattic\Jetpack\Boost_Core\Lib\Transient;
|
||||
|
||||
/**
|
||||
* Admin notice for letting users know they need to regenerate their Critical CSS.
|
||||
*/
|
||||
class Regenerate_Admin_Notice {
|
||||
|
||||
private static $dismissal_key = 'dismiss-critical-css-notice';
|
||||
|
||||
public static function enable() {
|
||||
Transient::set( 'regenerate_admin_notice', true );
|
||||
}
|
||||
|
||||
public static function dismiss() {
|
||||
Transient::delete( 'regenerate_admin_notice' );
|
||||
}
|
||||
|
||||
public static function is_enabled() {
|
||||
return Transient::get( 'regenerate_admin_notice', false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to generate a dismissal link for this message.
|
||||
*/
|
||||
private static function get_dismiss_url() {
|
||||
return add_query_arg(
|
||||
array(
|
||||
self::$dismissal_key => '',
|
||||
'nonce' => wp_create_nonce( 'jb_dismiss_notice' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function maybe_handle_dismissal() {
|
||||
if ( ! is_admin()
|
||||
|| ! current_user_can( 'manage_options' )
|
||||
|| ! isset( $_GET[ self::$dismissal_key ] ) || ! isset( $_GET['nonce'] )
|
||||
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'jb_dismiss_notice' )
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Dismiss the notice that shows up for major changes.
|
||||
static::dismiss();
|
||||
|
||||
wp_safe_redirect( remove_query_arg( array( self::$dismissal_key, 'nonce' ) ) );
|
||||
}
|
||||
|
||||
public static function init() {
|
||||
add_action( 'admin_notices', array( static::class, 'maybe_render' ) );
|
||||
if ( static::is_enabled() ) {
|
||||
static::maybe_handle_dismissal();
|
||||
}
|
||||
}
|
||||
|
||||
public static function maybe_render() {
|
||||
// We're not actually using the GET parameter here, it's only used to find out what page we're on.
|
||||
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
$on_settings_page = is_admin() && isset( $_GET['page'] ) && Admin::MENU_SLUG === $_GET['page'];
|
||||
if ( $on_settings_page || ! current_user_can( 'manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( static::is_enabled() ) {
|
||||
static::render();
|
||||
}
|
||||
}
|
||||
|
||||
public static function render() {
|
||||
?>
|
||||
<div id="jetpack-boost-notice-critical-css-regenerate" class="notice notice-warning is-dismissible">
|
||||
<h3>
|
||||
<?php esc_html_e( 'Jetpack Boost - Action Required', 'jetpack-boost' ); ?>
|
||||
</h3>
|
||||
<p>
|
||||
<?php esc_html_e( 'The Critical CSS generated by Jetpack Boost was cleared due to a change in the site theme.', 'jetpack-boost' ); ?>
|
||||
</p>
|
||||
<p>
|
||||
<?php esc_html_e( 'Please head to the Jetpack Boost dashboard to regenerate your Critical CSS.', 'jetpack-boost' ); ?>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<a class='button button-primary' href="<?php echo esc_url( admin_url( 'admin.php?page=' . Admin::MENU_SLUG ) ); ?>">
|
||||
<strong>
|
||||
<?php esc_html_e( 'Go to Jetpack Boost', 'jetpack-boost' ); ?>
|
||||
</strong>
|
||||
</a>
|
||||
<a class="jb-dismiss-notice" href="<?php echo esc_url( static::get_dismiss_url() ); ?>">
|
||||
<strong>
|
||||
<?php esc_html_e( 'Dismiss notice', 'jetpack-boost' ); ?>
|
||||
</strong>
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit( 0 ); // Exit if accessed directly
|
||||
}
|
||||
?>
|
||||
<main class="jp-plugin-deactivation__dialog__content">
|
||||
<h1><?php esc_html_e( 'Are you sure you want to deactivate?', 'jetpack-boost' ); ?></h1>
|
||||
<p class="big"><?php esc_html_e( 'Before you go...', 'jetpack-boost' ); ?></p>
|
||||
<p><?php esc_html_e( "Thank you for trying Jetpack Boost. Before you go, we'd really love your feedback on our plugin.", 'jetpack-boost' ); ?></p>
|
||||
<p><?php esc_html_e( "Just temporarily deactivating or don't fancy giving feedback? No problem.", 'jetpack-boost' ); ?></p>
|
||||
</main>
|
||||
<footer class="jp-plugin-deactivation__dialog__actions">
|
||||
<button
|
||||
type="button"
|
||||
class="jp-plugin-deactivation__button"
|
||||
data-jp-plugin-deactivation-action="close"
|
||||
><?php esc_html_e( 'Cancel', 'jetpack-boost' ); ?></button>
|
||||
<button
|
||||
type="button"
|
||||
class="jp-plugin-deactivation__button jp-plugin-deactivation__button--outline jp-plugin-deactivation__button--destructive"
|
||||
data-jp-plugin-deactivation-action="deactivate"
|
||||
><?php esc_html_e( 'Just Deactivate', 'jetpack-boost' ); ?></button>
|
||||
<!-- Using an anchor instead of a button for the Deactivate & Give Feedback may trigger browser's popup blocker as it is navigating to two URLs at once. -->
|
||||
<button
|
||||
type="button"
|
||||
class="jp-plugin-deactivation__button jp-plugin-deactivation__button--destructive"
|
||||
data-jp-plugin-deactivation-action="deactivate"
|
||||
onclick="window.open( 'https://jetpack.com/redirect/?source=jetpack-boost-deactivation-feedback', '_blank' )"
|
||||
><?php esc_html_e( 'Deactivate & Give Feedback', 'jetpack-boost' ); ?></button>
|
||||
</footer>
|
||||
|
||||
<style>
|
||||
#jp-plugin-deactivation-jetpack-boost p.big {
|
||||
font-size: 1.7em;
|
||||
}
|
||||
|
||||
#jp-plugin-deactivation-jetpack-boost footer {
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Empty file.
|
||||
*/
|
||||
|
||||
// Silence is golden.
|
||||
Reference in New Issue
Block a user