Skip to content

Commit

Permalink
Hide wpcom features when site is agency-managed or user is local
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar committed Jul 17, 2024
1 parent 7cc96a0 commit 2756596
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 222 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Hide wpcom features when site is agency-managed or user is local
84 changes: 29 additions & 55 deletions projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ public static function init() {

// Shared code for src/features.
require_once self::PKG_DIR . 'src/common/index.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath
require_once __DIR__ . '/utils.php';

// Load features that don't need any special loading considerations.
add_action( 'plugins_loaded', array( __CLASS__, 'load_features' ) );

// Load features that only apply to WordPress.com-connected users.
add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_user_features' ) );

// Load ETK features that need higher priority than the ETK plugin.
add_action( 'plugins_loaded', array( __CLASS__, 'load_etk_features' ), 0 );

Expand All @@ -43,10 +47,6 @@ public static function init() {
add_action( 'plugins_loaded', array( __CLASS__, 'load_coming_soon' ) );
add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_rest_api_endpoints' ) );
add_action( 'plugins_loaded', array( __CLASS__, 'load_block_theme_previews' ) );
add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_command_palette' ) );
add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_admin_interface' ) );
add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_site_management_widget' ) );
add_action( 'plugins_loaded', array( __CLASS__, 'load_replace_site_visibility' ) );

// These features run only on simple sites.
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
Expand Down Expand Up @@ -81,9 +81,6 @@ public static function init() {
* Load features that don't need any special loading considerations.
*/
public static function load_features() {
// Shared features.
require_once __DIR__ . '/features/agency-managed/agency-managed.php';

// Please keep the features in alphabetical order.
require_once __DIR__ . '/features/100-year-plan/enhanced-ownership.php';
require_once __DIR__ . '/features/100-year-plan/locked-mode.php';
Expand All @@ -94,21 +91,14 @@ public static function load_features() {
require_once __DIR__ . '/features/error-reporting/error-reporting.php';
require_once __DIR__ . '/features/first-posts-stream/first-posts-stream-helpers.php';
require_once __DIR__ . '/features/font-smoothing-antialiased/font-smoothing-antialiased.php';
// To avoid potential collisions with ETK.
if ( ! class_exists( 'A8C\FSE\Help_Center' ) ) {
require_once __DIR__ . '/features/help-center/class-help-center.php';
}
require_once __DIR__ . '/features/import-customizations/import-customizations.php';
require_once __DIR__ . '/features/marketplace-products-updater/class-marketplace-products-updater.php';
require_once __DIR__ . '/features/media/heif-support.php';
require_once __DIR__ . '/features/site-editor-dashboard-link/site-editor-dashboard-link.php';
require_once __DIR__ . '/features/wpcom-admin-dashboard/wpcom-admin-dashboard.php';
require_once __DIR__ . '/features/wpcom-admin-bar/wpcom-admin-bar.php';
require_once __DIR__ . '/features/wpcom-admin-menu/wpcom-admin-menu.php';
require_once __DIR__ . '/features/wpcom-block-editor/class-jetpack-wpcom-block-editor.php';
require_once __DIR__ . '/features/wpcom-block-editor/functions.editor-type.php';
require_once __DIR__ . '/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php';
require_once __DIR__ . '/features/wpcom-themes/wpcom-themes.php';
require_once __DIR__ . '/features/wpcom-themes/wpcom-theme-fixes.php';
require_once __DIR__ . '/features/google-analytics/google-analytics.php';

// Initializers, if needed.
Expand All @@ -128,6 +118,30 @@ public static function load_features() {
}
}

/**
* Load features that only apply to WordPress.com users.
*/
public static function load_wpcom_user_features() {
if ( ! is_wpcom_user() ) {
require_once __DIR__ . '/features/replace-site-visibility/hide-site-visibility.php';

return;
}

// To avoid potential collisions with ETK.
if ( ! class_exists( 'A8C\FSE\Help_Center' ) ) {
require_once __DIR__ . '/features/help-center/class-help-center.php';
}
require_once __DIR__ . '/features/replace-site-visibility/replace-site-visibility.php';
require_once __DIR__ . '/features/wpcom-admin-bar/wpcom-admin-bar.php';
require_once __DIR__ . '/features/wpcom-admin-interface/wpcom-admin-interface.php';
require_once __DIR__ . '/features/wpcom-admin-menu/wpcom-admin-menu.php';
require_once __DIR__ . '/features/wpcom-command-palette/wpcom-command-palette.php';
require_once __DIR__ . '/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php';
require_once __DIR__ . '/features/wpcom-site-management-widget/class-wpcom-site-management-widget.php';
require_once __DIR__ . '/features/wpcom-themes/wpcom-themes.php';
}

/**
* Laod ETK features that need higher priority than the ETK plugin.
* Can be moved back to load_features() once the feature no longer exists in the ETK plugin.
Expand Down Expand Up @@ -326,18 +340,6 @@ public static function load_verbum_comments_admin() {
new \Automattic\Jetpack\Verbum_Admin();
}

/**
* Load WPCOM Command Palette.
*
* @return void
*/
public static function load_wpcom_command_palette() {
if ( is_agency_managed_site() || ! current_user_has_wpcom_account() ) {
return;
}
require_once __DIR__ . '/features/wpcom-command-palette/wpcom-command-palette.php';
}

/**
* Load Odyssey Stats in Simple sites.
*/
Expand All @@ -347,34 +349,6 @@ public static function load_wpcom_simple_odyssey_stats() {
}
}

/**
* Load WPCOM Admin Interface.
*
* @return void
*/
public static function load_wpcom_admin_interface() {
require_once __DIR__ . '/features/wpcom-admin-interface/wpcom-admin-interface.php';
}

/**
* Load WPCOM Site Management widget.
*/
public static function load_wpcom_site_management_widget() {
if ( is_agency_managed_site() || ! current_user_has_wpcom_account() || ! current_user_can( 'manage_options' ) ) {
return;
}
if ( get_option( 'wpcom_admin_interface' ) === 'wp-admin' ) {
require_once __DIR__ . '/features/wpcom-site-management-widget/class-wpcom-site-management-widget.php';
}
}

/**
* Load Replace Site Visibility feature.
*/
public static function load_replace_site_visibility() {
require_once __DIR__ . '/features/replace-site-visibility/replace-site-visibility.php';
}

/**
* Load the Jetpack Custom CSS feature.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,3 @@
*
* @package jetpack-mu-wpcom
*/

/**
* Whether to enable "fully managed agency site" features.
* This is primarily used to hide WPCOM links, upsells, and features.
*
* @return bool True if the site is "fully managed agency site", false otherwise.
*/
function is_agency_managed_site() {
return ! empty( get_option( 'is_fully_managed_agency_site' ) );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Hide various settings related to site visibility.
*
* @package automattic/jetpack-mu-wpcom
*/

/**
* Hide the "Site Visibility" setting in Reading Settings.
*/
function wpcom_hide_site_visibility_setting() {
echo '<style>
.option-site-visibility {
display: none !important;
}
</style>';
}
add_action( 'load-options-reading.php', 'wpcom_hide_site_visibility_setting' );

/**
* Remove the "Update Services" section in Writing Settings.
*/
add_filter( 'enable_update_services_configuration', '__return_false' );
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,3 @@ function replace_site_visibility() {
<?php
}
add_action( 'blog_privacy_selector', 'replace_site_visibility' );

/**
* Hide the "Site visibility" setting in Reading Settings if is_agency_managed_site is true.
*/
function wpcom_hide_site_visibility_setting() {
if ( ! is_agency_managed_site() ) {
return;
}
// Check if the current page is the Reading Settings page.
$screen = get_current_screen();
if ( $screen->id === 'options-reading' ) {
echo '<style>
.option-site-visibility {
display: none !important;
}
</style>';
}
}
add_action( 'admin_head', 'wpcom_hide_site_visibility_setting' );

/**
* Remove the "Update Services" section in Writing Settings if is_agency_managed_site is true.
*/
function wpcom_remove_update_services_section() {
return ! is_agency_managed_site();
}
add_filter( 'enable_update_services_configuration', 'wpcom_remove_update_services_section' );
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ function wpcom_enqueue_admin_bar_assets() {
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar core object.
*/
function wpcom_repurpose_wp_logo_as_all_sites_menu( $wp_admin_bar ) {
if ( is_agency_managed_site() ) {
return;
}

foreach ( $wp_admin_bar->get_nodes() as $node ) {
if ( $node->parent === 'wp-logo' || $node->parent === 'wp-logo-external' ) {
$wp_admin_bar->remove_node( $node->id );
Expand Down Expand Up @@ -86,10 +82,6 @@ function wpcom_repurpose_wp_logo_as_all_sites_menu( $wp_admin_bar ) {
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar core object.
*/
function wpcom_add_reader_menu( $wp_admin_bar ) {
if ( is_agency_managed_site() ) {
return;
}

$wp_admin_bar->add_node(
array(
'id' => 'reader',
Expand All @@ -109,10 +101,6 @@ function wpcom_add_reader_menu( $wp_admin_bar ) {
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar core object.
*/
function wpcom_add_my_account_item_to_profile_menu( $wp_admin_bar ) {
if ( is_agency_managed_site() || ! current_user_has_wpcom_account() ) {
return;
}

$logout_node = $wp_admin_bar->get_node( 'logout' );
if ( $logout_node ) {
// Adds the 'My Account' menu item before 'Log Out'.
Expand All @@ -133,20 +121,3 @@ function wpcom_add_my_account_item_to_profile_menu( $wp_admin_bar ) {
}
}
add_action( 'admin_bar_menu', 'wpcom_add_my_account_item_to_profile_menu' );

/**
* Hides the Help Center menu.
*/
function wpcom_hide_help_center_menu() {
if ( ! is_agency_managed_site() ) {
return;
}
?>
<style>
#wp-admin-bar-help-center {
display: none !important;
}
</style>
<?php
}
add_action( 'admin_head', 'wpcom_hide_help_center_menu' );
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* The setting is displayed only if the has the wp-admin interface selected.
*/
function wpcomsh_wpcom_admin_interface_settings_field() {
if ( is_agency_managed_site() ) {
return;
}
add_settings_field( 'wpcom_admin_interface', '', 'wpcom_admin_interface_display', 'general', 'default' );

register_setting( 'general', 'wpcom_admin_interface', array( 'sanitize_callback' => 'esc_attr' ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,13 @@
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Status\Host;

/**
* Checks if the current user has a WordPress.com account connected.
*
* @return bool
*/
function current_user_has_wpcom_account() {
$user_id = get_current_user_id();

if ( function_exists( '\A8C\Billingdaddy\Users\get_wpcom_user' ) ) {
// On Simple sites, use get_wpcom_user function to check if the user has a WordPress.com account.
$user = \A8C\Billingdaddy\Users\get_wpcom_user( $user_id );
$has_account = isset( $user->ID );
} else {
// On Atomic sites, use the Connection Manager to check if the user has a WordPress.com account.
$connection_manager = new Connection_Manager();
$wpcom_user_data = $connection_manager->get_connected_user_data( $user_id );
$has_account = isset( $wpcom_user_data['ID'] );
}

return $has_account;
}

/**
* Adds a Hosting menu.
*/
function wpcom_add_hosting_menu() {
if ( get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) {
return;
}
if ( is_agency_managed_site() ) {
return;
}

/**
* Don't show `Hosting` to administrators without a WordPress.com account being attached,
* as they don't have access to any of the pages.
*/
if ( ! current_user_has_wpcom_account() ) {
return;
}

$parent_slug = 'wpcom-hosting-menu';
$domain = wp_parse_url( home_url(), PHP_URL_HOST );
Expand Down Expand Up @@ -167,14 +134,6 @@ function wpcom_add_jetpack_submenu() {
return;
}

/**
* Don't show to administrators without a WordPress.com account being attached,
* as they don't have access to any of the pages.
*/
if ( ! current_user_has_wpcom_account() ) {
return;
}

/*
* Do not display if we're in Offline mode, or if the user is not connected.
*/
Expand Down Expand Up @@ -289,7 +248,6 @@ function wpcom_add_plugins_menu() {
$is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM;
$is_atomic_site = ! $is_simple_site;
$uses_wp_admin_interface = get_option( 'wpcom_admin_interface' ) === 'wp-admin';
$is_agency_managed_site = is_agency_managed_site();

if ( $is_simple_site ) {
$has_plugins_menu = false;
Expand Down Expand Up @@ -329,10 +287,6 @@ function wpcom_add_plugins_menu() {
}
}

if ( $is_agency_managed_site || ! current_user_has_wpcom_account() ) {
return;
}

$domain = wp_parse_url( home_url(), PHP_URL_HOST );
if ( $uses_wp_admin_interface ) {
add_submenu_page(
Expand All @@ -349,11 +303,6 @@ function wpcom_add_plugins_menu() {

if ( $is_atomic_site ) {
if (
/**
* Don't show `Scheduled Updates` to administrators without a WordPress.com account being attached,
* as they don't have access to any of the pages.
*/
current_user_has_wpcom_account() &&
! get_option( 'wpcom_is_staging_site' ) &&
function_exists( 'wpcom_site_has_feature' ) &&
wpcom_site_has_feature( \WPCOM_Features::SCHEDULED_UPDATES )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ function wpcom_enqueue_sidebar_notice_assets() {
* @return array | null
*/
function wpcom_get_sidebar_notice() {
if ( is_agency_managed_site() ) {
return null;
}
$message_path = 'calypso:sites:sidebar_notice';

if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public static function get_instance() {
* Register widget with WordPress.
*/
public function __construct() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

add_action( 'admin_head', array( $this, 'admin_head' ) );
add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) );
}
Expand Down
Loading

0 comments on commit 2756596

Please sign in to comment.