admin-site-enhancements
Last commit date
assets
3 years ago
classes
3 years ago
LICENSE.md
3 years ago
README.md
3 years ago
admin-site-enhancements.php
3 years ago
bootstrap.php
3 years ago
settings.php
3 years ago
bootstrap.php
224 lines
| 1 | <?php |
| 2 | |
| 3 | // We're using the singleton design pattern |
| 4 | // https://code.tutsplus.com/articles/design-patterns-in-wordpress-the-singleton-pattern--wp-31621 |
| 5 | // https://carlalexander.ca/singletons-in-wordpress/ |
| 6 | // https://torquemag.io/2016/11/singletons-wordpress-good-evil/ |
| 7 | |
| 8 | /** |
| 9 | * Main class of the plugin used to add functionalities |
| 10 | * |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | class Admin_Site_Enhancements { |
| 14 | |
| 15 | // Refers to a single instance of this class |
| 16 | private static $instance = null; |
| 17 | |
| 18 | /** |
| 19 | * Creates or returns a single instance of this class |
| 20 | * |
| 21 | * @return Admin_Site_Enhancements a single instance of this class |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | public static function get_instance() { |
| 25 | |
| 26 | if ( null == self::$instance ) { |
| 27 | |
| 28 | self::$instance = new self; |
| 29 | |
| 30 | } |
| 31 | |
| 32 | return self::$instance; |
| 33 | |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Initialize plugin functionalities |
| 38 | */ |
| 39 | private function __construct() { |
| 40 | |
| 41 | // Setup admin menu, admin page, settings, settings sections, sections fields, admin scripts, plugin action links, etc. |
| 42 | |
| 43 | // Register admin menu and add the settings page. |
| 44 | add_action( 'admin_menu', 'asenha_register_admin_menu' ); |
| 45 | |
| 46 | // Register plugin settings |
| 47 | |
| 48 | // Instantiate object for registration of settings section and fields |
| 49 | $settings = new ASENHA\Classes\Settings_Sections_Fields; |
| 50 | |
| 51 | add_action( 'admin_init', [ $settings, 'register_sections_fields' ] ); |
| 52 | |
| 53 | // Suppress all notices on the plugin's main page. Then add notification for successful settings update. |
| 54 | add_action( 'admin_notices', 'asenha_suppress_notices', 5 ); |
| 55 | |
| 56 | // Enqueue admin scripts and styles only on the plugin's main page |
| 57 | add_action( 'admin_enqueue_scripts', 'asenha_admin_scripts' ); |
| 58 | |
| 59 | // Add action links in plugins page |
| 60 | add_filter( 'plugin_action_links_' . ASENHA_SLUG . '/' . ASENHA_SLUG . '.php', 'asenha_plugin_action_links' ); |
| 61 | |
| 62 | // Update footer text |
| 63 | add_filter( 'admin_footer_text', 'asenha_footer_text' ); |
| 64 | |
| 65 | // Selectively enable enhancements based on options value |
| 66 | |
| 67 | // Get all WP Enhancements options, default to empty array in case it's not been created yet |
| 68 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 69 | |
| 70 | // Instantiate object for Content Management features |
| 71 | $content_management = new ASENHA\Classes\Content_Management; |
| 72 | |
| 73 | // Content Management >> Enable Page and Post Duplication |
| 74 | if ( array_key_exists( 'enable_duplication', $options ) && $options['enable_duplication'] ) { |
| 75 | add_action( 'admin_action_asenha_enable_duplication', [ $content_management, 'asenha_enable_duplication' ] ); |
| 76 | add_filter( 'page_row_actions', [ $content_management, 'add_duplication_action_link' ], 10, 2 ); |
| 77 | add_filter( 'post_row_actions', [ $content_management, 'add_duplication_action_link' ], 10, 2 ); |
| 78 | } |
| 79 | |
| 80 | // Content Management >> Enable Media Replacement |
| 81 | if ( array_key_exists( 'enable_media_replacement', $options ) && $options['enable_media_replacement'] ) { |
| 82 | add_filter( 'media_row_actions', [ $content_management, 'modify_media_list_table_edit_link' ], 10, 2 ); |
| 83 | add_filter( 'attachment_fields_to_edit', [ $content_management, 'add_media_replacement_button' ] ); |
| 84 | add_action( 'edit_attachment', [ $content_management, 'replace_media' ] ); |
| 85 | add_filter( 'post_updated_messages', [ $content_management, 'attachment_updated_custom_message' ] ); |
| 86 | } |
| 87 | |
| 88 | // Content Management >> Enhance List Tables |
| 89 | if ( array_key_exists( 'enhance_list_tables', $options ) && $options['enhance_list_tables'] ) { |
| 90 | |
| 91 | // Show Featured Image Column |
| 92 | if ( array_key_exists( 'show_featured_image_column', $options ) && $options['show_featured_image_column'] ) { |
| 93 | add_action( 'admin_init', [ $content_management, 'show_featured_image_column' ] ); |
| 94 | } |
| 95 | |
| 96 | // Show Excerpt Column |
| 97 | if ( array_key_exists( 'show_excerpt_column', $options ) && $options['show_excerpt_column'] ) { |
| 98 | add_action( 'admin_init', [ $content_management, 'show_excerpt_column' ] ); |
| 99 | } |
| 100 | |
| 101 | // Show ID Column |
| 102 | if ( array_key_exists( 'show_id_column', $options ) && $options['show_id_column'] ) { |
| 103 | add_action( 'admin_init', [ $content_management, 'show_id_column' ] ); |
| 104 | } |
| 105 | |
| 106 | // Hide Comments Column |
| 107 | if ( array_key_exists( 'hide_comments_column', $options ) && $options['hide_comments_column'] ) { |
| 108 | add_action( 'admin_init', [ $content_management, 'hide_comments_column' ] ); |
| 109 | } |
| 110 | |
| 111 | // Hide Post Tags Column |
| 112 | if ( array_key_exists( 'hide_post_tags_column', $options ) && $options['hide_post_tags_column'] ) { |
| 113 | add_action( 'admin_init', [ $content_management, 'hide_post_tags_column' ] ); |
| 114 | } |
| 115 | |
| 116 | // Show Custom Taxonomy Filters |
| 117 | if ( array_key_exists( 'show_custom_taxonomy_filters', $options ) && $options['show_custom_taxonomy_filters'] ) { |
| 118 | add_action( 'restrict_manage_posts', [ $content_management, 'show_custom_taxonomy_filters' ] ); |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | |
| 123 | // Instantiate object for Admin Interface features |
| 124 | $admin_interface = new ASENHA\Classes\Admin_Interface; |
| 125 | |
| 126 | // Admin Interface >> Hide Admin Notices |
| 127 | if ( array_key_exists( 'hide_admin_notices', $options ) && $options['hide_admin_notices'] ) { |
| 128 | add_action( 'all_admin_notices', [ $admin_interface, 'admin_notices_wrapper' ] ); |
| 129 | add_action( 'admin_bar_menu', [ $admin_interface, 'admin_notices_menu' ] ); |
| 130 | add_action( 'admin_enqueue_scripts', [ $admin_interface, 'admin_notices_menu_inline_css' ] ); // wp-admin |
| 131 | } |
| 132 | |
| 133 | // Admin Interface >> Hide Admin Bar |
| 134 | if ( array_key_exists( 'hide_admin_bar', $options ) && $options['hide_admin_bar'] && array_key_exists( 'hide_admin_bar_for', $options ) && isset( $options['hide_admin_bar_for'] ) ) { |
| 135 | add_filter( 'show_admin_bar', [ $admin_interface, 'hide_admin_bar_for_roles' ] ); |
| 136 | } |
| 137 | |
| 138 | // Admin Interface >> View Admin as Role |
| 139 | if ( array_key_exists( 'view_admin_as_role', $options ) && $options['view_admin_as_role'] ) { |
| 140 | add_action( 'admin_bar_menu', [ $admin_interface, 'view_admin_as_admin_bar_menu' ], 8 ); // Priority 8 so it is next to username section |
| 141 | add_action( 'init', [ $admin_interface, 'role_switcher_to_view_admin_as' ] ); |
| 142 | add_action( 'wp_die_handler', [ $admin_interface, 'custom_error_page_on_switch_failure' ] ); |
| 143 | } |
| 144 | |
| 145 | // Admin Interface >> Hide or Modify Elements |
| 146 | if ( array_key_exists( 'hide_modify_elements', $options ) && $options['hide_modify_elements'] ) { |
| 147 | add_filter( 'admin_bar_menu', [ $admin_interface, 'modify_admin_bar_menu' ], 5 ); // priority 5 to execute earlier than the normal 10 |
| 148 | } |
| 149 | |
| 150 | // Admin Interface >> Customize Admin Menu |
| 151 | |
| 152 | // Load jQuery UI sortables scripts regardless of whether Customize Admin Menu is enabled or not. This is needed to enable sorting upon clicking the feature toggle. |
| 153 | add_action( 'admin_enqueue_scripts', [ $admin_interface, 'enqueue_jquery_ui_sortables_scripts' ] ); |
| 154 | |
| 155 | if ( array_key_exists( 'customize_admin_menu', $options ) && $options['customize_admin_menu'] ) { |
| 156 | // add_action( 'wp_ajax_save_custom_menu_order', [ $admin_interface, 'save_custom_menu_order' ] ); |
| 157 | // add_action( 'wp_ajax_save_hidden_menu_items', [ $admin_interface, 'save_hidden_menu_items' ] ); |
| 158 | add_filter( 'custom_menu_order', '__return_true' ); |
| 159 | add_filter( 'menu_order', [ $admin_interface, 'render_custom_menu_order' ] ); |
| 160 | add_action( 'admin_menu', [ $admin_interface, 'hide_menu_items' ], 999 ); |
| 161 | add_action( 'admin_menu', [ $admin_interface, 'add_hidden_menu_toggle' ] ); |
| 162 | add_action( 'admin_enqueue_scripts', [ $admin_interface, 'enqueue_toggle_hidden_menu_script' ] ); |
| 163 | } |
| 164 | |
| 165 | // Instantiate object for Security features |
| 166 | $security = new ASENHA\Classes\Security; |
| 167 | |
| 168 | // Security >> Change Login URL |
| 169 | if ( array_key_exists( 'change_login_url', $options ) && $options['change_login_url'] ) { |
| 170 | if ( array_key_exists( 'custom_login_slug', $options ) && ! empty( $options['custom_login_slug'] ) ) { |
| 171 | add_action( 'login_head', [ $security, 'redirect_on_default_login_urls' ] ); |
| 172 | add_action( 'init', [ $security, 'redirect_on_custom_login_url' ] ); |
| 173 | add_action( 'wp_login_failed', [ $security, 'redirect_to_custom_login_url' ] ); |
| 174 | add_action( 'wp_logout', [ $security, 'redirect_to_custom_login_url' ] ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Security >> Obfuscate Author Slugs |
| 179 | if ( array_key_exists( 'obfuscate_author_slugs', $options ) && $options['obfuscate_author_slugs'] ) { |
| 180 | add_action( 'pre_get_posts', [ $security, 'alter_author_query' ], 10 ); |
| 181 | add_filter( 'author_link', [ $security, 'alter_author_link' ], 10, 3 ); |
| 182 | add_filter( 'rest_prepare_user', [ $security, 'alter_json_users' ], 10, 3 ); |
| 183 | } |
| 184 | |
| 185 | // Instantiate object for Utilities features |
| 186 | $utilities = new ASENHA\Classes\Utilities; |
| 187 | |
| 188 | // Utilities >> Redirect After Login |
| 189 | if ( array_key_exists( 'redirect_after_login', $options ) && $options['redirect_after_login'] ) { |
| 190 | if ( array_key_exists( 'redirect_after_login_to_slug', $options ) && ! empty( $options['redirect_after_login_to_slug'] ) ) { |
| 191 | if ( array_key_exists( 'redirect_after_login_for', $options ) && ! empty( $options['redirect_after_login_for'] ) ) { |
| 192 | add_filter( 'login_redirect', [ $utilities, 'redirect_for_roles_after_login' ], 10, 3 ); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Utilities >> Redirect After Logout |
| 198 | if ( array_key_exists( 'redirect_after_logout', $options ) && $options['redirect_after_logout'] ) { |
| 199 | if ( array_key_exists( 'redirect_after_logout_to_slug', $options ) && ! empty( $options['redirect_after_logout_to_slug'] ) ) { |
| 200 | if ( array_key_exists( 'redirect_after_logout_for', $options ) && ! empty( $options['redirect_after_logout_for'] ) ) { |
| 201 | add_action( 'wp_logout', [ $utilities, 'redirect_after_logout' ], 5, 1 ); // load earlier than Change Login URL add_action |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Utilities >> Redirect 404 to Homepage |
| 207 | if ( array_key_exists( 'redirect_404_to_homepage', $options ) && $options['redirect_404_to_homepage'] ) { |
| 208 | add_filter( 'wp', [ $utilities, 'redirect_404_to_homepage' ] ); |
| 209 | } |
| 210 | |
| 211 | // Instantiate object for Disable Components features |
| 212 | $disable_components = new ASENHA\Classes\Disable_Components; |
| 213 | |
| 214 | // Disable Components >> Disable XML-RPC |
| 215 | if ( array_key_exists( 'disable_xmlrpc', $options ) && $options['disable_xmlrpc'] ) { |
| 216 | add_filter( 'xmlrpc_enabled', '__return_false' ); |
| 217 | add_filter( 'wp_xmlrpc_server_class', [ $disable_components, 'maybe_disable_xmlrpc' ] ); |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | |
| 224 | Admin_Site_Enhancements::get_instance(); |