<?php

/**
 * Cleaning WordPress
 * @category Functions
 * @author Krik
 */

// Remove weblog client link
remove_action('wp_head', 'rsd_link');

// Remove Windows Live Writer Manifest link
remove_action('wp_head', 'wlwmanifest_link');

// Remove page/post header shortlinks
remove_action('wp_head', 'wp_shortlink_wp_head');

// Remove canonical header links
remove_action('wp_head', 'rel_canonical');

// Remove prev and next header links
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');

// Remove DNS prefetch
remove_action('wp_head', 'wp_resource_hints', 2);

// Remove JSON REST API references
add_filter('json_enabled', '__return_false');
add_filter('json_jsonp_enabled', '__return_false');
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);

// Remove default JSON REST API routes
remove_action('rest_api_init', 'create_initial_rest_routes', 0);

// Disable WordPress emoji support
if (!is_admin()) {
	add_action('init', 'krik_disable_emoji');
}

// Remove WordPress version number
add_filter('the_generator', 'krik_remove_wordpress_version');

// Remove welcome panel in dashboard
remove_action('welcome_panel', 'wp_welcome_panel');

// Remove admin toolbar
if (WP_ENV != 'production') {
	add_filter('show_admin_bar', '__return_false');
}

// Remove login errors
add_filter('login_errors', 'krik_no_login_errors');

// Remove dashboard widgets
add_action('wp_dashboard_setup', 'krik_remove_dashboard_widgets');

// Remove help
add_filter('admin_head', 'krik_remove_wp_help', 999, 3);

// Remove WordPress logo
add_action('admin_bar_menu', 'krik_remove_wp_logo', 999);

// Hide tools
if (WP_ENV != 'development') {
	add_action('admin_menu', 'krik_remove_tools', 99);
}

// Disable editor fullscreen by default
add_action('enqueue_block_editor_assets', 'krik_disable_editor_fullscreen');

// Remove WPML version number
// global $sitepress;
// remove_action('wp_head', array($sitepress, 'meta_generator_tag'));

// Remove WPML CSS
// define('ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS', true);

// Remove WPML JS
// add_action('wp_head', 'krik_remove_wpml_script', 11);

// Remove login language dropdown
add_filter('login_display_language_dropdown', '__return_false');

function krik_disable_emoji()
{
	remove_action('admin_print_styles', 'print_emoji_styles');
	remove_action('wp_head', 'print_emoji_detection_script', 7);
	remove_action('admin_print_scripts', 'print_emoji_detection_script');
	remove_action('wp_print_styles', 'print_emoji_styles');
	remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
	remove_filter('the_content_feed', 'wp_staticize_emoji');
	remove_filter('comment_text_rss', 'wp_staticize_emoji');
}

function krik_remove_wordpress_version()
{
	return '';
}

function krik_no_login_errors()
{
	return 'Something is wrong!';
}

function krik_remove_wpml_script()
{
	 wp_dequeue_script('sitepress');
	 wp_deregister_script('sitepress');
}

function krik_remove_dashboard_widgets()
{
	remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
	remove_meta_box('dashboard_primary', 'dashboard', 'side');
}

function krik_remove_wp_help()
{
    $screen = get_current_screen();
    $screen->remove_help_tabs();
}

function krik_remove_wp_logo($wp_admin_bar)
{
	$wp_admin_bar->remove_node('wp-logo');
}

function krik_remove_tools()
{
	remove_menu_page('tools.php');
}

function krik_disable_editor_fullscreen() {
	$script = "jQuery( window ).load(function() {const isFullscreenMode = wp.data.select( 'core/edit-post' ).isFeatureActive( 'fullscreenMode' ); if ( isFullscreenMode ) { wp.data.dispatch( 'core/edit-post' ).toggleFeature( 'fullscreenMode' ); } });";
	wp_add_inline_script('wp-blocks', $script);
}

