You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
10 KiB

<?php
/**
* degruchy-core
*
* @package degruchy-core
* @author Nathan DeGruchy
* @copyright 2020 Nathan DeGruchy
* @license GPL Version 3 or later
*
* @wordpress-plugin
* Plugin Name: DeGruchy Core Plugin
* Plugin URI: https://git.degruchy.link/ndegruchy/degruchy-core
* Description: Core functionality plugin for WordPress on DeGruchy.org.
* Version: 1.5
* Requires at least: 5.8
* Requires PHP: 7.4
* Author: Nathan DeGruchy
* Author URI: https://degruchy.org/
* Text Domain: degruchy-core
* License: GPL Version 3 or Later
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
const CACHELIFETIME = 3600;
// Exit if accessed directly.
if ( !defined( 'ABSPATH' ) )
{
exit;
}
if ( !function_exists( 'degruchy_core_maybe_add_banner' ) )
{
/**
* # Old Post Notifcation
* Display Old Post Notification on content, maybe.
*
* @param string $content The post/page content to, possibly, modify.
*
* @return string The, possibly, modified content
*/
function degruchy_core_maybe_add_banner( string $content ): String
{
if ( !is_singular( 'post' ) ||
in_category( 'garrett-quotes' ) )
{ // Skip if we're not a singe post type, or in the category 'garrett-quotes'
return $content;
}
$postd = get_the_date( 'U' );
$today = date( 'U' );
$oneyr = 60 * 60 * 24 * 365;
$parse = __DIR__ . '/vendor/parsedown/Parsedown.php';
$templ = __DIR__ . '/templates/banner.md';
if ( ( ( $today - $postd ) >= $oneyr ) )
{ // If we're a year or more old
$_banner_cache = wp_cache_get(
'degruchy-core-old-banner',
'degruchy-core'
); // Retrieve cache of the banner to avoid doing it repeatedly
if ( FALSE == $_banner_cache )
{
// If the banner cache is empty, generate it
// Some guard rails in case some part is missing
if ( file_exists( $parse ) &&
file_exists( $templ ) )
{
// Add parsedown.
require_once( $parse );
$Parsedown = new Parsedown;
// Set some options
$Parsedown->setSafeMode( TRUE );
// Load the banner message file
$banner_file = $templ;
$banner = file_get_contents( $banner_file );
$banner = $Parsedown->text( $banner );
$banner = "<section id=\"old\">$banner</section>";
// Cache the result for an hour
wp_cache_set(
'degruchy-core-old-banner',
$banner,
'degruchy-core',
CACHELIFETIME
);
}
else
{
return $content; //abort, something is missing
}
}
else
{
// Cache is warm, use that.
$banner = $_banner_cache;
}
// Show the banner
$content = $banner . $content;
}
return $content;
}
}
// Add banner to the content, maybe.
add_filter( 'the_content', 'degruchy_core_maybe_add_banner', 99 );
/**
* Disable features
*
* - Login error messages
* - Application passwords
* - XML Sitemaps
*/
add_filter('show_recent_comments_widget_style', '__return_false');
/* Change WP-Admin login error message to be more generic */
if ( !function_exists( "degruchy_core_login_invalid" ) )
{
/**
* Hide the original error for an invalid login attempt.
*
* @param String $msg The original error
*
* @return String The resulting message.
*/
function degruchy_core_login_invalid( String $msg ): String
{
global $errors;
$err_codes = $errors->get_error_codes();
if ( in_array( 'invalid_username', $err_codes ) ||
in_array( 'incorrect_password', $err_codes ) )
{
$msg = 'ERROR: Invalid credential.';
}
return $msg;
}
}
add_filter( 'login_errors', 'degruchy_core_login_invalid' );
/**
* Disable plugin/theme editor
*/
if ( !defined( 'DISALLOW_FILE_EDIT' ) ||
FALSE == DISALLOW_FILE_EDIT )
{
define( 'DISALLOW_FILE_EDIT', TRUE );
}
/**
* Enable Media Trash
*/
if ( !defined( 'MEDIA_TRASH' ) ||
FALSE == MEDIA_TRASH )
{
define( 'MEDIA_TRASH', TRUE );
}
/**
* Enable auto core updates
*/
if( !defined( 'WP_AUTO_UPDATE_CORE' ) ||
FALSE == WP_AUTO_UPDATE_CORE )
{
define( 'WP_AUTO_UPDATE_CORE', TRUE );
}
/**
* Lifted from Room34's No Nonsense plugin
*/
if( !function_exists('degruchy_core_remove_admin_wp_logo_and_howdy') )
{
/**
* Removes some stuff from the admin bar
*
* @param $wp_admin_bar object The unmolested admin bar
*
* @return object The admin bar, modified
* @since 1.4.3
*/
function degruchy_core_remove_admin_wp_logo_and_howdy( object $wp_admin_bar ): Object
{
// Remove the WordPress logo menu
$wp_admin_bar->remove_node( 'wp-logo' );
// This section removes the 'howdy' component of the user menu
// Get the user menu node in the menu
$my_account = $wp_admin_bar->get_node( 'my-account' );
// Replace it with a non-howdy message
$wp_admin_bar->add_node( array(
'id' => 'my-account',
'title' => substr( $my_account->title, strpos( $my_account->title, '<span class="display-name">' ) ),
) );
if( !is_admin() )
{
$media = array(
'parent' => 'site-name',
'id' => 'degruchy-core-media',
'title' => 'Media',
'href' => esc_url(admin_url('upload.php'))
);
$plugins = array(
'parent' => 'site-name',
'id' => 'degruchy-core-plugins',
'title' => 'Plugins',
'href' => esc_url(admin_url('plugins.php'))
);
$wp_admin_bar->add_node($plugins);
$wp_admin_bar->add_node($media);
}
return $wp_admin_bar;
}
}
add_action( 'admin_bar_menu', 'degruchy_core_remove_admin_wp_logo_and_howdy', 999, 1 );
if( !function_exists( 'degruchy_core_remove_wp_emoji' ) )
{
/**
* Removes emoji. For realsies.
* @return Void
* @since 1.4.3
*/
function degruchy_core_remove_wp_emoji(): Void
{
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
// Remove WP emoji DNS prefetch
add_filter( 'emoji_svg_url', '__return_false' );
}
}
add_action( 'init', 'degruchy_core_remove_wp_emoji', 10, 0 );
if(!function_exists('degruchy_core_remove_head_garbage'))
{
function degruchy_core_remove_head_garbage()
{
// Remove really simple discovery link.
remove_action('wp_head', 'rsd_link');
// Remove wlwmanifest.xml (needed to support windows live writer).
remove_action('wp_head', 'wlwmanifest_link');
// Remove generator tag from RSS feeds.
remove_action('atom_head', 'the_generator');
remove_action('comments_atom_head', 'the_generator');
remove_action('rss_head', 'the_generator');
remove_action('rss2_head', 'the_generator');
remove_action('commentsrss2_head', 'the_generator');
remove_action('rdf_header', 'the_generator');
remove_action('opml_head', 'the_generator');
remove_action('app_head', 'the_generator');
add_filter('the_generator', '__return_false');
// Remove feeds from categories, tags, search etc.
remove_action('wp_head', 'feed_links_extra', 3);
// Remove the next and previous post links.
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
// Remove the shortlink url from header.
remove_action('wp_head', 'wp_shortlink_wp_head', 10);
remove_action('template_redirect', 'wp_shortlink_header', 11);
// Remove WordPress generator version.
remove_action('wp_head', 'wp_generator');
// Remove the annoying:
// <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>.
add_filter('show_recent_comments_widget_style', '__return_false');
}
}
add_action('init', 'degruchy_core_remove_head_garbage', 10, 0);
add_filter('pre_option_link_manager_enabled', '__return_false');
add_filter('wp_sitemaps_enabled', '__return_true');