| 1 |
<?php |
| 2 |
namespace Falcon; |
| 3 |
|
| 4 |
use WP_Query; |
| 5 |
|
| 6 |
class General extends Base { |
| 7 |
protected $features = [ |
| 8 |
'no_gutenberg', |
| 9 |
'no_heartbeat', |
| 10 |
'no_embeds', |
| 11 |
'no_revisions', |
| 12 |
'no_self_pings', |
| 13 |
'no_privacy', |
| 14 |
'no_cron', |
| 15 |
'no_auto_updates', |
| 16 |
'no_external_requests', |
| 17 |
'no_comments', |
| 18 |
'search_posts_only', |
| 19 |
'no_comment_url', |
| 20 |
'no_texturize', |
| 21 |
'maintenance_mode', |
| 22 |
]; |
| 23 |
|
| 24 |
public function no_gutenberg() { |
| 25 |
// Disable Gutenberg on the back end. |
| 26 |
add_filter( 'use_block_editor_for_post', '__return_false' ); |
| 27 |
add_filter( 'use_block_editor_for_post_type', '__return_false' ); |
| 28 |
|
| 29 |
// Disable Gutenberg for widgets. |
| 30 |
add_filter( 'use_widgets_block_editor', '__return_false' ); |
| 31 |
|
| 32 |
add_action( 'wp_enqueue_scripts', [ $this, 'remove_gutenberg_assets' ], 20 ); |
| 33 |
} |
| 34 |
|
| 35 |
public function remove_gutenberg_assets() { |
| 36 |
// Remove CSS on the front end. |
| 37 |
wp_dequeue_style( 'wp-block-library' ); |
| 38 |
|
| 39 |
// Remove Gutenberg theme. |
| 40 |
wp_dequeue_style( 'wp-block-library-theme' ); |
| 41 |
|
| 42 |
// Remove inline global CSS on the front end. |
| 43 |
wp_dequeue_style( 'global-styles' ); |
| 44 |
|
| 45 |
// Remove classic-themes CSS for backwards compatibility for button blocks. |
| 46 |
wp_dequeue_style( 'classic-theme-styles' ); |
| 47 |
} |
| 48 |
|
| 49 |
public function no_heartbeat() { |
| 50 |
add_action( 'init', [ $this, 'remove_heartbeat_script' ], 1 ); |
| 51 |
} |
| 52 |
|
| 53 |
public function remove_heartbeat_script() { |
| 54 |
wp_deregister_script( 'heartbeat' ); |
| 55 |
} |
| 56 |
|
| 57 |
public function no_embeds() { |
| 58 |
// Remove the REST API endpoint. |
| 59 |
remove_action( 'rest_api_init', 'wp_oembed_register_route' ); |
| 60 |
|
| 61 |
// Turn off oEmbed auto discovery. |
| 62 |
add_filter( 'embed_oembed_discover', '__return_false' ); |
| 63 |
|
| 64 |
// Don't filter oEmbed results. |
| 65 |
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result' ); |
| 66 |
|
| 67 |
// Remove oEmbed discovery links. |
| 68 |
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); |
| 69 |
|
| 70 |
// Remove oEmbed JavaScript from the front-end and back-end. |
| 71 |
remove_action( 'wp_head', 'wp_oembed_add_host_js' ); |
| 72 |
} |
| 73 |
|
| 74 |
public function no_revisions() { |
| 75 |
add_filter( 'wp_revisions_to_keep', '__return_zero' ); |
| 76 |
} |
| 77 |
|
| 78 |
public function no_self_pings() { |
| 79 |
add_action( 'pre_ping', [ $this, 'remove_self_pings' ] ); |
| 80 |
} |
| 81 |
|
| 82 |
public function no_privacy() { |
| 83 |
add_action( 'admin_menu', [ $this, 'remove_menu' ] ); |
| 84 |
} |
| 85 |
|
| 86 |
public function remove_menu() { |
| 87 |
remove_submenu_page( 'options-general.php', 'options-privacy.php' ); |
| 88 |
remove_submenu_page( 'tools.php', 'export-personal-data.php' ); |
| 89 |
remove_submenu_page( 'tools.php', 'erase-personal-data.php' ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Remove pings to the website itself. |
| 94 |
* @link http://wordpress.stackexchange.com/a/1852 |
| 95 |
*/ |
| 96 |
public function remove_self_pings( &$links ) { |
| 97 |
$home_url = home_url(); |
| 98 |
$links = array_filter( $links, function ( $link ) use ( $home_url ) { |
| 99 |
return false === strpos( $link, $home_url ); |
| 100 |
} ); |
| 101 |
} |
| 102 |
|
| 103 |
public function no_cron(): void { |
| 104 |
if ( ! defined( 'DISABLE_WP_CRON' ) ) { |
| 105 |
define( 'DISABLE_WP_CRON', true ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public function no_auto_updates() { |
| 110 |
add_filter( 'automatic_updater_disabled', '__return_true' ); |
| 111 |
} |
| 112 |
|
| 113 |
public function no_external_requests() { |
| 114 |
if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) ) { |
| 115 |
define( 'WP_HTTP_BLOCK_EXTERNAL', true ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public function no_comments() { |
| 120 |
new Components\DisableComments; |
| 121 |
} |
| 122 |
|
| 123 |
public function search_posts_only(): void { |
| 124 |
add_filter( 'pre_get_posts', function ( WP_Query $query ): void { |
| 125 |
// Bypass Rest API & admin search. |
| 126 |
if ( defined( 'REST_REQUEST' ) || is_admin() ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
if ( $query->is_main_query() && $query->is_search() ) { |
| 130 |
$query->set( 'post_type', 'post' ); |
| 131 |
} |
| 132 |
} ); |
| 133 |
} |
| 134 |
|
| 135 |
public function no_comment_url() { |
| 136 |
add_filter( 'comment_form_default_fields', function ( array $fields ): array { |
| 137 |
unset( $fields['url'] ); |
| 138 |
return $fields; |
| 139 |
} ); |
| 140 |
} |
| 141 |
|
| 142 |
public function no_texturize() { |
| 143 |
add_filter( 'run_wptexturize', '__return_false' ); |
| 144 |
} |
| 145 |
|
| 146 |
public function maintenance_mode(): void { |
| 147 |
add_action( 'template_redirect', function () { |
| 148 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 149 |
// Translators: %1$s - Header, %2$s - Message. |
| 150 |
$html = sprintf( |
| 151 |
'<h1>%1$s</h1><br>%2$s', |
| 152 |
__( 'Maintenance mode', 'falcon' ), |
| 153 |
__( 'The website is under maintenance, please come back later. Sorry for the inconvenience.', 'falcon' ) |
| 154 |
); |
| 155 |
wp_die( wp_kses_post( $html ), esc_html__( 'Maintenance mode', 'falcon' ), 503 ); |
| 156 |
} |
| 157 |
} ); |
| 158 |
} |
| 159 |
} |
| 160 |
|