> 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.3.1
*/
/*
Copyright (c) 2010-2014 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' ) ) :
require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'c2c-plugin.php' );
class c2c_AddAdminJavaScript extends C2C_Plugin_038 {
/**
* @var c2c_AddAdminJavaScript The one true instance
*/
private static $instance;
/**
* @var string To hold memoized jQuery code
*/
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.3.1', '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 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 function uninstall() {
delete_option( 'c2c_add_admin_javascript' );
}
/**
* Initializes the plugin's configuration and localizable text variables.
*/
public function load_config() {
$this->name = __( 'Add Admin JavaScript', $this->textdomain );
$this->menu_name = __( 'Admin JavaScript', $this->textdomain );
$this->config = array(
'files' => array( 'input' => 'inline_textarea', 'default' => '', 'datatype' => 'array',
'label' => __( 'Admin JavaScript Files', $this->textdomain ),
'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.', $this->textdomain ),
'input_attributes' => 'style="width: 98%; white-space: nowrap;" rows="8" cols="40"'
),
'js_head' => array( 'input' => 'inline_textarea', 'default' => '', 'datatype' => 'text',
'label' => __( 'Admin JavaScript (in head)', $this->textdomain ),
'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.', $this->textdomain ),
'input_attributes' => 'style="width: 98%; white-space: nowrap;" rows="8" cols="40"'
),
'js_foot' => array( 'input' => 'inline_textarea', 'default' => '', 'datatype' => 'text',
'label' => __( 'Admin JavaScript (in footer)', $this->textdomain ),
'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.', $this->textdomain ),
'input_attributes' => 'style="width: 98%; white-space: nowrap;" rows="8" cols="40"'
),
'js_jq' => array( 'input' => 'inline_textarea', 'default' => '', 'datatype' => 'text',
'label' => __( 'Admin jQuery JavaScript', $this->textdomain ),
'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.', $this->textdomain ),
'input_attributes' => 'style="width: 98%; white-space: nowrap;" 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_head', array( $this, 'add_js_to_head' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'add_js_to_foot' ) );
}
/**
* Outputs the text above the setting form
*
* @param string $localized_heading_text (optional) Localized page heading text.
* @return void (Text will be echoed.)
*/
public function options_page_description( $localized_heading_text = '' ) {
parent::options_page_description( __( 'Add Admin JavaScript Settings', $this->textdomain ) );
echo '
' . __( 'Add additional JavaScript to your admin pages.', $this->textdomain ) . '
'; echo '' . __( 'See the "Advanced Tips" tab in the "Help" section above for info on how to use the plugin to programmatically customize 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', $this->textdomain ), '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) * @return void (Text is echoed) */ 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:', $this->textdomain ) . '
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:', $this->textdomain ) . '
add_filter( 'c2c_add_admin_js_files', 'my_admin_js_files' );
function my_admin_js_files( \$files ) {
\$files[] = 'http://ajax.googleapis.com/ajax/libs/yui/2.8.1/build/yuiloader/yuiloader-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.', $this->textdomain ) . "