Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools: Introduce beta feature toggle. #458

Merged
merged 3 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions HealthCheck/BackCompat/class-wp-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public function enqueues() {
return;
}

wp_enqueue_style( 'health-check', trailingslashit( HEALTH_CHECK_PLUGIN_URL ) . 'build/health-check.css', array(), HEALTH_CHECK_PLUGIN_VERSION );
$health_check = include HEALTH_CHECK_PLUGIN_DIRECTORY . 'build/health-check.asset.php';

wp_enqueue_script( 'health-check', trailingslashit( HEALTH_CHECK_PLUGIN_URL ) . 'build/health-check.js', array( 'jquery' ), HEALTH_CHECK_PLUGIN_VERSION );
wp_enqueue_style( 'health-check', trailingslashit( HEALTH_CHECK_PLUGIN_URL ) . 'build/health-check.css', array(), $health_check['version'] );

wp_enqueue_script( 'health-check', trailingslashit( HEALTH_CHECK_PLUGIN_URL ) . 'build/health-check.js', array( 'jquery' ), $health_check['version'] );

wp_localize_script(
'health-check',
Expand Down
70 changes: 70 additions & 0 deletions HealthCheck/Tools/class-health-check-beta-features.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* Checks if wp_mail() works.
*
* @package Health Check
*/

// Make sure the file is not directly accessible.
if ( ! defined( 'ABSPATH' ) ) {
die( 'We\'re sorry, but you can not directly access this file.' );
}

/**
* Class Mail Check
*/
class Health_Check_Beta_Features extends Health_Check_Tool {

public function __construct() {
$this->label = __( 'Beta features', 'health-check' );
$this->description = __( 'The plugin may contain beta features, which you as the site owner can enable or disable as you wish.', 'health-check' );

parent::__construct();

add_action( 'admin_init', array( $this, 'toggle_beta_features' ) );
}

public function toggle_beta_features() {
if ( ! isset( $_GET['health-check-beta-features'] ) ) {
return;
}

if ( ! isset( $_GET['_wpnonce'] ) ) {
return;
}

if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'health-check-beta-features' ) ) {
return;
}

if ( 'enable' === $_GET['health-check-beta-features'] ) {
update_option( 'health-check-beta-features', true );
} else {
update_option( 'health-check-beta-features', false );
}

wp_safe_redirect( admin_url( 'site-health.php?tab=tools' ) );
}

public function tab_content() {
$feature_status = get_option( 'health-check-beta-features', false );

if ( ! $feature_status ) {
printf(
'<a href="%s" class="button button-primary">%s</a>',
esc_url( wp_nonce_url( add_query_arg( 'health-check-beta-features', 'enable' ), 'health-check-beta-features' ) ),
esc_html__( 'Enable beta features', 'health-check' )
);
} else {
printf(
'<a href="%s" class="button button-primary">%s</a>',
esc_url( wp_nonce_url( add_query_arg( 'health-check-beta-features', 'disable' ), 'health-check-beta-features' ) ),
esc_html__( 'Disable beta features', 'health-check' )
);
}
}

}

new Health_Check_Beta_Features();
256 changes: 256 additions & 0 deletions HealthCheck/class-health-check-screenshots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<?php
/**
* Automate the screenshot process for end users seeking support.
*
* @package Health Check
*/

// Make sure the file is not directly accessible.
if ( ! defined( 'ABSPATH' ) ) {
die( 'We\'re sorry, but you can not directly access this file.' );
}

/**
* Class Health_Check_Screenshots
*/
class Health_Check_Screenshots {

private $allowed_image_mimes = array(
'image/jpeg',
);

private $should_404 = false;

public function __construct() {
$feature_status = get_option( 'health-check-beta-features', false );

if ( $feature_status ) {
add_action( 'admin_init', array( $this, 'delete_screenshot' ) );

add_action( 'init', array( $this, 'display_screenshot' ), 0 );

add_action( 'init', array( $this, 'register_post_type' ) );
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );

add_action( 'admin_bar_menu', array( $this, 'admin_menubar_button' ), 999 );

add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

add_filter( 'site_health_navigation_tabs', array( $this, 'add_site_health_navigation_tabs' ), 20 );
add_action( 'site_health_tab_content', array( $this, 'add_site_health_tab_content' ) );

add_action( 'wp', array( $this, 'maybe_404' ) );
}
}

public function maybe_404() {
if ( ! $this->should_404 ) {
return;
}

global $wp_query;
$wp_query->set_404();
status_header( 404 );
nocache_headers();
}

public function delete_screenshot() {
if ( ! is_admin() || ! isset( $_GET['health-check-delete-screenshot'] ) || ! $this->user_can_screenshot() ) {
return;
}

// Validate nonces.
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'health-check-delete-screenshot' ) ) {
return;
}

wp_delete_post( $_GET['health-check-delete-screenshot'], true );

wp_safe_redirect( admin_url( 'site-health.php?tab=screenshots' ) );
}

public function display_screenshot() {
if ( ! isset( $_GET['health-check-screenshot'] ) ) {
return;
}

$screenshot_id = $_GET['health-check-screenshot'];
$screenshot = get_posts(
array(
'post_type' => 'health-check-images',
'posts_per_page' => 1,
'meta_key' => 'hash_id',
'meta_value' => $screenshot_id,
)
);

if ( empty( $screenshot ) ) {
$this->should_404 = true;
return;
}

if ( is_array( $screenshot ) ) {
$screenshot = $screenshot[0];
}

$image = $screenshot->screenshot;
$image = explode( ';', $image, 2 );

$image_type = str_replace( 'data:', '', $image[0] );

if ( ! in_array( $image_type, $this->allowed_image_mimes, true ) ) {
return;
}

header( 'Content-Type: ' . $image_type );

if ( isset( $_GET['dl'] ) ) {
header( 'Content-Disposition: attachment; filename="' . sanitize_title( $screenshot->post_title ) . '.jpeg"' );
}

$data = str_replace( 'base64,', '', $image[1] );
echo base64_decode( $data );

die();
}

public function add_site_health_tab_content( $tab ) {
if ( 'screenshots' !== $tab ) {
return;
}

include_once( HEALTH_CHECK_PLUGIN_DIRECTORY . '/pages/screenshots.php' );
}

public function add_site_health_navigation_tabs( $tabs ) {
return array_merge(
$tabs,
array(
'screenshots' => esc_html__( 'Screenshots', 'health-check' ),
)
);
}

public function user_can_screenshot() {
return current_user_can( 'view_site_health_checks' );
}

public function register_post_type() {
register_post_type(
'health-check-images',
array(
'labels' => array(
'name' => __( 'Screenshots', 'health-check' ),
'singular_name' => __( 'Screenshot', 'health-check' ),
),
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'exclude_from_search' => true,
'has_archive' => false,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title' ),
)
);
}

public function register_rest_routes() {
register_rest_route(
'health-check/v1',
'/screenshot',
array(
'methods' => 'POST',
'callback' => array( $this, 'store_screenshot' ),
'permission_callback' => array( $this, 'user_can_screenshot' ),
'args' => array(
'nonce' => array(
'required' => true,
'validate_callback' => function( $param, $request, $key ) {
return wp_verify_nonce( $param, 'health-check-screenshot' );
},
),
'label' => array(
'required' => true,
'validate_callback' => function( $param, $request, $key ) {
return is_string( $param ) && ! empty( $param );
},
),
'screenshot' => array(
'required' => true,
'validate_callback' => function( $param, $request, $key ) {
return is_string( $param ) && 'data:image/jpeg;' === substr( $param, 0, 16 );
},
),
),
)
);
}

public function store_screenshot( \WP_REST_Request $request ) {
// Create a new post in the `health-check-images` post type.
$post_id = wp_insert_post(
array(
'post_type' => 'health-check-images',
'post_title' => sanitize_text_field( $request->get_param( 'label' ) ),
'post_status' => 'publish',
'meta_input' => array(
'screenshot' => $request->get_param( 'screenshot' ),
'hash_id' => wp_hash( $request->get_param( 'screenshot' ) ),
),
)
);
}

public function enqueue_scripts() {
if ( ! $this->user_can_screenshot() ) {
return;
}

$asset = include HEALTH_CHECK_PLUGIN_DIRECTORY . 'build/health-check-global.asset.php';

wp_enqueue_script( 'health-check-tools', trailingslashit( HEALTH_CHECK_PLUGIN_URL ) . 'build/health-check-global.js', array( 'jquery', 'wp-a11y' ), $asset['version'] );

wp_localize_script(
'health-check-tools',
'HealthCheckTools',
array(
'nonce' => array(
'rest' => wp_create_nonce( 'wp_rest' ),
'screenshot' => wp_create_nonce( 'health-check-screenshot' ),
),
'rest' => array(
'screenshot' => rest_url( 'health-check/v1/screenshot' ),
),
)
);
}

public function admin_menubar_button( $wp_menu ) {
if ( ! $this->user_can_screenshot() ) {
return;
}

if ( ! is_admin() ) {
require_once( trailingslashit( ABSPATH ) . 'wp-admin/includes/plugin.php' );
}

// Add top-level menu item.
$wp_menu->add_menu(
array(
'id' => 'health-check-screenshot',
'title' => esc_html__( 'Take screenshot', 'health-check' ),
'href' => '#',
'meta' => array(
'class' => 'health-check-take-screenshot',
),
)
);
}

}

new Health_Check_Screenshots();
8 changes: 6 additions & 2 deletions HealthCheck/class-health-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ public function enqueues() {
return;
}

wp_enqueue_style( 'health-check', trailingslashit( HEALTH_CHECK_PLUGIN_URL ) . 'build/health-check.css', array(), HEALTH_CHECK_PLUGIN_VERSION );
$health_check = include HEALTH_CHECK_PLUGIN_DIRECTORY . 'build/health-check.asset.php';

wp_enqueue_style( 'health-check', trailingslashit( HEALTH_CHECK_PLUGIN_URL ) . 'build/health-check.css', array(), $health_check['version'] );

// If the WordPress 5.2+ version of Site Health is used, do some extra checks to not mess with core scripts and styles.
if ( 'site-health' === $screen->id ) {
Expand All @@ -246,7 +248,9 @@ public function enqueues() {
}
}

wp_enqueue_script( 'health-check-tools', trailingslashit( HEALTH_CHECK_PLUGIN_URL ) . 'build/health-check-tools.js', array( 'jquery' ), HEALTH_CHECK_PLUGIN_VERSION );
$health_check_tools = include HEALTH_CHECK_PLUGIN_DIRECTORY . 'build/health-check-tools.asset.php';

wp_enqueue_script( 'health-check-tools', trailingslashit( HEALTH_CHECK_PLUGIN_URL ) . 'build/health-check-tools.js', array( 'jquery' ), $health_check_tools['version'] );

wp_localize_script(
'health-check-tools',
Expand Down
2 changes: 2 additions & 0 deletions health-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
// Include class-files used by our plugin.
require_once( dirname( __FILE__ ) . '/HealthCheck/class-health-check.php' );
require_once( dirname( __FILE__ ) . '/HealthCheck/class-health-check-loopback.php' );
require_once( dirname( __FILE__ ) . '/HealthCheck/class-health-check-screenshots.php' );
require_once( dirname( __FILE__ ) . '/HealthCheck/class-health-check-troubleshoot.php' );

// Tools section.
Expand All @@ -72,6 +73,7 @@
require_once( dirname( __FILE__ ) . '/HealthCheck/Tools/class-health-check-phpinfo.php' );
require_once( dirname( __FILE__ ) . '/HealthCheck/Tools/class-health-check-htaccess.php' );
require_once( dirname( __FILE__ ) . '/HealthCheck/Tools/class-health-check-robotstxt.php' );
require_once( dirname( __FILE__ ) . '/HealthCheck/Tools/class-health-check-beta-features.php' );

// Initialize our plugin.
new Health_Check();
Expand Down
Loading