> Read the accompanying readme.txt file for instructions and documentation.
* =>> Also, visit the plugin's homepage for additional information and updates.
* =>> Or visit: https://wordpress.org/plugins/add-admin-javascript/
*
* @package Add_Admin_JavaScript
* @author Scott Reilly
* @version 1.7
*/
/*
* TODO:
* - Improve documentation within the help pane.
*/
/*
Copyright (c) 2010-2019 by Scott Reilly (aka coffee2code)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
defined( 'ABSPATH' ) or die();
if ( is_admin() && ! class_exists( 'c2c_AddAdminJavaScript_Plugin_049' ) ) :
require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'c2c-plugin.php' );
final class c2c_AddAdminJavaScript extends c2c_AddAdminJavaScript_Plugin_049 {
/**
* Name of plugin's setting.
*
* @since 1.7
* @var string
*/
const SETTING_NAME = 'c2c_add_admin_javascript';
/**
* Name of query parameter for disabling JS output.
*
* @since 1.7
* @var string
*/
const NO_JS_QUERY_PARAM = 'c2c-no-js';
/**
* The one true instance.
*
* @var c2c_AddAdminJavaScript
*/
private static $instance;
/**
* Holds memoized jQuery code.
*
* @var string
*/
protected $jq = false;
/**
* Get singleton instance.
*
* @since 1.2
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
protected function __construct() {
parent::__construct( '1.7', 'add-admin-javascript', 'c2c', __FILE__, array() );
register_activation_hook( __FILE__, array( __CLASS__, 'activation' ) );
return self::$instance = $this;
}
/**
* Resets the object to its initial state.
*
* @since 1.3
*/
public function reset() {
$this->jq = false;
}
/**
* Handles activation tasks, such as registering the uninstall hook.
*
* @since 1.1
*/
public static function activation() {
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
}
/**
* Handles uninstallation tasks, such as deleting plugin options.
*
* This can be overridden.
*
* @since 1.1
*/
public static function uninstall() {
delete_option( self::SETTING_NAME );
}
/**
* Initializes the plugin's configuration and localizable text variables.
*/
public function load_config() {
$this->name = __( 'Add Admin JavaScript', 'add-admin-javascript' );
$this->menu_name = __( 'Admin JavaScript', 'add-admin-javascript' );
$this->config = array(
'files' => array(
'input' => 'inline_textarea',
'default' => '',
'datatype' => 'array',
'label' => __( 'Admin JavaScript Files', 'add-admin-javascript' ),
'help' => __( 'List one URL per line. The reference can be relative to the root of your site, or a full, absolute URL. These will be listed in the order listed, and appear in the <head> before the JS defined below.', 'add-admin-javascript' ),
'input_attributes' => 'style="width: 98%; white-space: pre; word-wrap: normal; overflow-x: scroll;" rows="8" cols="40"'
),
'js_head' => array(
'input' => 'inline_textarea',
'default' => '',
'datatype' => 'text',
'label' => __( 'Admin JavaScript (in head)', 'add-admin-javascript' ),
'help' => __( 'Note that the above JavaScript will be added to all admin pages and apply for all admin users. To speed up page load, it is recommended that inline JavaScript be added to the footer instead of the head.', 'add-admin-javascript' ),
'input_attributes' => 'style="width: 98%; white-space: pre; word-wrap: normal; overflow-x: scroll;" rows="8" cols="40"'
),
'js_foot' => array(
'input' => 'inline_textarea',
'default' => '',
'datatype' => 'text',
'label' => __( 'Admin JavaScript (in footer)', 'add-admin-javascript' ),
'help' => __( 'Note that the above JavaScript will be added to all admin pages and apply for all admin users. To speed up page load, it is recommended that inline JavaScript be added to the footer instead of the head.', 'add-admin-javascript' ),
'input_attributes' => 'style="width: 98%; white-space: pre; word-wrap: normal; overflow-x: scroll;" rows="8" cols="40"'
),
'js_jq' => array(
'input' => 'inline_textarea',
'default' => '',
'datatype' => 'text',
'label' => __( 'Admin jQuery JavaScript', 'add-admin-javascript' ),
'help' => __( 'This will be put in a jQuery(document).ready(function($)) {} in the footer. Note that the above JavaScript will be added to all admin pages and apply for all admin users.', 'add-admin-javascript' ),
'input_attributes' => 'style="width: 98%; white-space: pre; word-wrap: normal; overflow-x: scroll;" rows="8" cols="40"'
)
);
}
/**
* Override the plugin framework's register_filters() to register actions and filters.
*/
public function register_filters() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_js' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'add_codemirror' ) );
add_action( 'admin_head', array( $this, 'add_js_to_head' ) );
add_action( 'admin_notices', array( $this, 'recovery_mode_notice' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'add_js_to_foot' ) );
add_filter( 'wp_redirect', array( $this, 'remove_query_param_from_redirects' ) );
}
/**
* Outputs the text above the setting form.
*
* @param string $localized_heading_text (optional) Localized page heading text.
*/
public function options_page_description( $localized_heading_text = '' ) {
parent::options_page_description( __( 'Add Admin JavaScript Settings', 'add-admin-javascript' ) );
echo '
' . __( 'Add additional JavaScript to your admin pages.', 'add-admin-javascript' ) . '
'; echo '' . __( 'See the "Advanced Tips" tab in the "Help" section above for info on how to use the plugin to programmatically customize JavaScript.', 'add-admin-javascript' ) . '
'; } /** * Configures help tabs content. * * @since 1.2 */ public function help_tabs_content( $screen ) { $screen->add_help_tab( array( 'id' => 'c2c-advanced-tips-' . $this->id_base, 'title' => __( 'Advanced Tips', 'add-admin-javascript' ), 'content' => self::contextual_help( '', $this->options_page ) ) ); parent::help_tabs_content( $screen ); } /** * Outputs advanced tips text. * * @since 1.2 * * @param string $contextual_help The default contextual help * @param int $screen_id The screen ID * @param object $screen The screen object (only supplied in WP 3.0) */ public function contextual_help( $contextual_help, $screen_id, $screen = null ) { if ( $screen_id != $this->options_page ) { return $contextual_help; } $help = '' . __( 'You can also programmatically add to or customize any JavaScript defined in the "Admin JavaScript" field via the c2c_add_admin_js_jq filter, like so:', 'add-admin-javascript' ) . '
add_filter( 'c2c_add_admin_js_jq', 'my_admin_js_jq' );
function my_admin_js_jq( \$js ) {
\$js .= "\$('.hide_me').hide();";
return \$js;
}
HTML;
$help .= '' . __( 'You can also programmatically add to or customize any referenced JavaScript files defined in the "Admin JS Files" field via the c2c_add_admin_js_files filter, like so:', 'add-admin-javascript' ) . '
add_filter( 'c2c_add_admin_js_files', 'my_admin_js_files' );
function my_admin_js_files( \$files ) {
\$files[] = 'https://ajax.googleapis.com/ajax/libs/mootools/1.6.0/mootools.min.js';
return \$files;
}
HTML;
$help .= '' . __( 'In addition, the "Admin JavaScript (in head)" and "Admin JavaScript (in footer)" can be filtered via c2c_add_admin_js_head and c2c_add_admin_js_footer respectively.', 'add-admin-javascript' ) . "
%s constant.", 'add-admin-javascript' ),
'C2C_ADD_ADMIN_JAVASCRIPT_DISABLED'
);
} else {
$msg = __( "RECOVERY MODE ENABLED: JavaScript output for this plugin is disabled on this page view.", 'add-admin-javascript' );;
}
echo <<
{$msg}
HTML; } } /** * Obtain the jQuery JavaScript, if any. Needed since it is requested in two * functions so it should be memoizable. */ public function get_jq() { $options = $this->get_options(); if ( $this->jq === false || empty( $this->jq ) ) { /** * Filters the JavaScript that should be added directly to the admin page * footer within a jQuery document ready function. * * @since 1.0 * * @param string $files JavaScript code (without ` "; } } /** * Outputs JavaScript into footer as regular JavaScript and/or within a jQuery ready * * @return void (Text will be echoed.) */ public function add_js_to_foot() { if ( ! $this->can_show_js() ) { return; } $options = $this->get_options(); /** * Filters the JavaScript that should be added directly to the admin footer. * * @since 1.0 * * @param string $files JavaScript code (without ` "; } $js = $this->get_jq(); if ( ! empty( $js ) ) { echo " "; } } /** * Initializes CodeMirror for the JavaScript textareas. * * @since 1.7 */ public function add_codemirror() { // Bail if not on the plugin setting page. if ( $this->options_page !== get_current_screen()->id ) { return; } // Bail if the code editor script hasn't been registered. if ( ! wp_script_is( 'code-editor', 'registered' ) ) { return; } // Enqueue code editor and settings for manipulating JS. $settings = wp_enqueue_code_editor( array( 'type' => 'text/javascript' ) ); // Bail if user disabled CodeMirror. if ( false === $settings ) { return; } // Inline the CodeMirror code. $json_settings = wp_json_encode( $settings ); wp_add_inline_script( 'code-editor', sprintf( 'jQuery( function() { wp.codeEditor.initialize( "js_head", %s ); wp.codeEditor.initialize( "js_foot", %s ); wp.codeEditor.initialize( "js_jq", %s ); } );', $json_settings, $json_settings, $json_settings ) ); } } // end c2c_AddAdminJavaScript add_action( 'plugins_loaded', array( 'c2c_AddAdminJavaScript', 'instance' ) ); endif; // end if !class_exists()