Skip to content

Commit

Permalink
CANTINA-944: For private sites, force disable blog_public UI toggle b…
Browse files Browse the repository at this point in the history
…uttons in Reading Settings to avoid confusion (#4890)

* For private sites, force disable blog_public UI toggle buttons in Reading Settings to avoid confusion

* Change description to let user know why UI is disabled

* Address feedback to simplify JS

* Add translation

* Add some moar protections to ensure it only loads on admin and only on the one screen

* Update security/class-private-sites.php

Co-authored-by: Volodymyr Kolesnykov <[email protected]>

* Remove piece of testing code that snuck in

* Move stuff around

---------

Co-authored-by: Volodymyr Kolesnykov <[email protected]>
  • Loading branch information
rebeccahum and sjinks authored Sep 28, 2023
1 parent d4acfcc commit 8de9938
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions security/class-private-sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public function init() {

add_filter( 'jetpack_active_modules', array( $this, 'filter_jetpack_active_modules' ) );
add_filter( 'jetpack_get_available_modules', array( $this, 'filter_jetpack_get_available_modules' ) );

// Force the blog_public option to be -1 and disable UI
add_filter( 'option_blog_public', array( $this, 'filter_restrict_blog_public' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'disable_blog_public_ui' ) );

$this->disable_core_feeds();
$this->block_unnecessary_access();
Expand All @@ -71,6 +74,52 @@ public function disable_core_feeds() {
add_action( 'do_feed_atom', array( $this, 'action_do_feed' ), -1 );
}

/**
* Disable checkbox/radio UI in Reading Settings
*/
public function disable_blog_public_ui() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

$screen = get_current_screen();
if ( 'options-reading' !== $screen->base ) {
return;
}

wp_register_script( 'vip-disable-blog-public-option-ui', false, array(), '0.1', true );
wp_enqueue_script( 'vip-disable-blog-public-option-ui' );
$js_code = <<<JS
function onContentLoaded(callback) {
if (document.readyState !== 'loading') {
callback();
} else {
document.addEventListener('DOMContentLoaded', callback);
}
}
onContentLoaded(function() {
function updateProperty(selector, property, value) {
const element = document.querySelector(selector);
if (element) {
element[property] = value;
}
}
var checkbox = 'tr.option-site-visibility input#blog_public[type="checkbox"]';
if (document.querySelector(checkbox)) {
updateProperty(checkbox, 'disabled', true);
} else {
updateProperty('tr.option-site-visibility input#blog-public[type="radio"]', 'disabled', true);
updateProperty('tr.option-site-visibility input#blog-norobots[type="radio"]', 'disabled', true);
}
updateProperty('tr.option-site-visibility p.description', 'textContent', '%s');
});
JS;
$description = esc_html__( 'This option is disabled when the constant VIP_JETPACK_IS_PRIVATE is enabled.', 'vip' );
$final_js_code = sprintf( $js_code, $description );
wp_add_inline_script( 'vip-disable-blog-public-option-ui', $final_js_code );
}

/*
* Block the entire site for the feedbot
*
Expand Down

0 comments on commit 8de9938

Please sign in to comment.