Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules/scan
File: class-admin-bar-notice.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* A class that adds the scan notice to the admin bar.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace Automattic\Jetpack\Scan;
[7] Fix | Delete
[8] Fix | Delete
use Automattic\Jetpack\Assets;
[9] Fix | Delete
use Automattic\Jetpack\Redirect;
[10] Fix | Delete
use WP_Admin_Bar;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Class Main
[14] Fix | Delete
*
[15] Fix | Delete
* Responsible for loading the admin bar notice if threats are found.
[16] Fix | Delete
*
[17] Fix | Delete
* @package Automattic\Jetpack\Scan
[18] Fix | Delete
*/
[19] Fix | Delete
class Admin_Bar_Notice {
[20] Fix | Delete
const SCRIPT_NAME = 'jetpack-scan-show-notice';
[21] Fix | Delete
const SCRIPT_VERSION = '1';
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* The singleton instance of this class.
[25] Fix | Delete
*
[26] Fix | Delete
* @var Admin_Bar_Notice
[27] Fix | Delete
*/
[28] Fix | Delete
protected static $instance;
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Get the singleton instance of the class.
[32] Fix | Delete
*
[33] Fix | Delete
* @return Admin_Bar_Notice
[34] Fix | Delete
*/
[35] Fix | Delete
public static function instance() {
[36] Fix | Delete
if ( ! isset( self::$instance ) ) {
[37] Fix | Delete
self::$instance = new Admin_Bar_Notice();
[38] Fix | Delete
self::$instance->init_hooks();
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
return self::$instance;
[42] Fix | Delete
}
[43] Fix | Delete
/**
[44] Fix | Delete
* Initalize the hooks as needed.
[45] Fix | Delete
*/
[46] Fix | Delete
private function init_hooks() {
[47] Fix | Delete
if ( ! $this->should_try_to_display_notice() ) {
[48] Fix | Delete
return;
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_toolbar_script' ) );
[52] Fix | Delete
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_toolbar_script' ) );
[53] Fix | Delete
add_action( 'admin_bar_menu', array( $this, 'add_threats_to_toolbar' ), 999 );
[54] Fix | Delete
[55] Fix | Delete
// Inject the data-ampdevmode attribute into the inline <script> output via wp_localize_script(). To revisit after https://github.com/ampproject/amp-wp/issues/4598.
[56] Fix | Delete
add_filter(
[57] Fix | Delete
'amp_dev_mode_element_xpaths',
[58] Fix | Delete
static function ( $expressions ) {
[59] Fix | Delete
$expressions[] = '//script[ contains( text(), "Jetpack_Scan" ) ]';
[60] Fix | Delete
return $expressions;
[61] Fix | Delete
}
[62] Fix | Delete
);
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* Whether to even try to display the notice or now.
[67] Fix | Delete
*
[68] Fix | Delete
* @return bool
[69] Fix | Delete
*/
[70] Fix | Delete
private function should_try_to_display_notice() {
[71] Fix | Delete
// Jetpack Scan is currently not supported on multisite.
[72] Fix | Delete
if ( is_multisite() ) {
[73] Fix | Delete
return false;
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
// Check if VaultPress is active, the assumtion there is that VaultPress is working.
[77] Fix | Delete
// It has its own notice in the admin bar.
[78] Fix | Delete
if ( class_exists( 'VaultPress' ) ) {
[79] Fix | Delete
return false;
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
// Check if Protect is active.
[83] Fix | Delete
// It has its own notice in the admin bar.
[84] Fix | Delete
if ( class_exists( 'Jetpack_Protect' ) ) {
[85] Fix | Delete
return false;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
// Only show the notice to admins.
[89] Fix | Delete
if ( ! current_user_can( 'manage_options' ) ) {
[90] Fix | Delete
return false;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
return true;
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
/**
[97] Fix | Delete
* Add the inline styles and scripts if they are needed.
[98] Fix | Delete
*/
[99] Fix | Delete
public function enqueue_toolbar_script() {
[100] Fix | Delete
$this->add_inline_styles();
[101] Fix | Delete
[102] Fix | Delete
if ( $this->has_threats() !== null ) {
[103] Fix | Delete
return;
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
// We don't know about threats in the cache lets load the JS that fetches the info and updates the admin bar.
[107] Fix | Delete
Assets::register_script(
[108] Fix | Delete
self::SCRIPT_NAME,
[109] Fix | Delete
'_inc/build/scan/admin-bar-notice.min.js',
[110] Fix | Delete
JETPACK__PLUGIN_FILE,
[111] Fix | Delete
array(
[112] Fix | Delete
'in_footer' => true,
[113] Fix | Delete
'strategy' => 'defer',
[114] Fix | Delete
'nonmin_path' => 'modules/scan/admin-bar-notice.js',
[115] Fix | Delete
'dependencies' => array( 'admin-bar' ),
[116] Fix | Delete
'version' => self::SCRIPT_VERSION,
[117] Fix | Delete
'enqueue' => true,
[118] Fix | Delete
)
[119] Fix | Delete
);
[120] Fix | Delete
$script_data = array(
[121] Fix | Delete
'nonce' => wp_create_nonce( 'wp_rest' ),
[122] Fix | Delete
'scan_endpoint' => get_rest_url( null, 'jetpack/v4/scan' ),
[123] Fix | Delete
'scan_dashboard_url' => Redirect::get_url( 'calypso-scanner' ),
[124] Fix | Delete
/* translators: %s is the alert icon */
[125] Fix | Delete
'singular' => sprintf( esc_html__( '%s Threat found', 'jetpack' ), $this->get_icon() ),
[126] Fix | Delete
/* translators: %s is the alert icon */
[127] Fix | Delete
'multiple' => sprintf( esc_html__( '%s Threats found', 'jetpack' ), $this->get_icon() ),
[128] Fix | Delete
);
[129] Fix | Delete
wp_localize_script( self::SCRIPT_NAME, 'Jetpack_Scan', $script_data );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Adds the inline styles if they are needed.
[134] Fix | Delete
*/
[135] Fix | Delete
public function add_inline_styles() {
[136] Fix | Delete
// We know there are no threats so lets not include any css.
[137] Fix | Delete
if ( false === $this->has_threats() ) {
[138] Fix | Delete
return;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
// We might be showing the threats in the admin bar lets make sure that they look great!
[142] Fix | Delete
$hide_wording_on_mobile = '#wp-admin-bar-jetpack-scan-notice .is-hidden { display:none; } @media screen and (max-width: 959px ) { #wpadminbar #wp-admin-bar-jetpack-scan-notice { width:32px; } #wpadminbar #wp-admin-bar-jetpack-scan-notice a { color: transparent!important; } }';
[143] Fix | Delete
$style = '#wp-admin-bar-jetpack-scan-notice svg { float:left; margin-top: 4px; margin-right: 6px; width: 18px; height: 22px; }' . $hide_wording_on_mobile;
[144] Fix | Delete
if ( is_rtl() ) {
[145] Fix | Delete
$style = '#wp-admin-bar-jetpack-scan-notice svg { float:right; margin-top: 4px; margin-left: 6px; width: 18px; height: 22px; }' . $hide_wording_on_mobile;
[146] Fix | Delete
}
[147] Fix | Delete
wp_add_inline_style( 'admin-bar', $style );
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
/**
[151] Fix | Delete
* Add the link to the admin bar.
[152] Fix | Delete
*
[153] Fix | Delete
* @param WP_Admin_Bar $wp_admin_bar WP Admin Bar class object.
[154] Fix | Delete
*/
[155] Fix | Delete
public function add_threats_to_toolbar( $wp_admin_bar ) {
[156] Fix | Delete
if ( ! $this->should_try_to_display_notice() ) {
[157] Fix | Delete
return;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
$has_threats = $this->has_threats();
[161] Fix | Delete
if ( false === $has_threats ) {
[162] Fix | Delete
return;
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
$node = array(
[166] Fix | Delete
'id' => 'jetpack-scan-notice',
[167] Fix | Delete
'title' => '',
[168] Fix | Delete
'parent' => 'top-secondary',
[169] Fix | Delete
'meta' => array(
[170] Fix | Delete
'title' => esc_attr__( 'View security scan details', 'jetpack' ),
[171] Fix | Delete
'class' => 'error is-hidden',
[172] Fix | Delete
),
[173] Fix | Delete
);
[174] Fix | Delete
[175] Fix | Delete
if ( $has_threats ) {
[176] Fix | Delete
$node['href'] = esc_url( Redirect::get_url( 'calypso-scanner' ) );
[177] Fix | Delete
$node['meta']['onclick'] = 'window.open( this.href ); return false;';
[178] Fix | Delete
$node['meta']['class'] = 'error';
[179] Fix | Delete
$node['title'] = sprintf(
[180] Fix | Delete
esc_html(
[181] Fix | Delete
/* translators: %s is the alert icon */
[182] Fix | Delete
_n( '%s Threat found', '%s Threats found', $this->get_threat_count(), 'jetpack' )
[183] Fix | Delete
),
[184] Fix | Delete
$this->get_icon()
[185] Fix | Delete
);
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
$wp_admin_bar->add_node( $node );
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
/**
[192] Fix | Delete
* Returns the shield icon.
[193] Fix | Delete
*
[194] Fix | Delete
* @return string
[195] Fix | Delete
*/
[196] Fix | Delete
private function get_icon() {
[197] Fix | Delete
return '<svg width="18" height="22" viewBox="0 0 18 22" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M9 0L0 4V10C0 15.55 3.84 20.74 9 22C14.16 20.74 18 15.55 18 10V4L9 0Z" fill="#D63638"/><path d="M7.99121 6.00894H10.0085V11.9968H7.99121V6.00894Z" fill="#FFF"/><path d="M7.99121 14.014H10.0085V15.9911H7.99121V14.014Z" fill="#FFF"/></svg>';
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
/**
[201] Fix | Delete
*
[202] Fix | Delete
* Return Whether boolean cached threats exist or null if the state is unknown.
[203] Fix | Delete
* * @return boolean or null
[204] Fix | Delete
*/
[205] Fix | Delete
public function has_threats() {
[206] Fix | Delete
$scan_state = get_transient( 'jetpack_scan_state' );
[207] Fix | Delete
if ( empty( $scan_state ) ) {
[208] Fix | Delete
return null;
[209] Fix | Delete
}
[210] Fix | Delete
// Return true if there is at least one threat found.
[211] Fix | Delete
return (bool) isset( $scan_state->threats[0] );
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Returns the number of threats found or 0.
[216] Fix | Delete
*
[217] Fix | Delete
* @return int
[218] Fix | Delete
*/
[219] Fix | Delete
public function get_threat_count() {
[220] Fix | Delete
if ( ! $this->has_threats() ) {
[221] Fix | Delete
return 0;
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
$scan_state = get_transient( 'jetpack_scan_state' );
[225] Fix | Delete
return is_array( $scan_state->threats ) ? count( $scan_state->threats ) : 0;
[226] Fix | Delete
}
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function