Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
<?php
/**
* Plugin Name: UC JavaScript
* Description: Allow super admin's to add arbitrary JavaScript to each site on a site by site basis
* Author: Josh Roy
* Version: 1.0
* Text Domain: ucjs
*/
function ucjs_scripts() {
wp_enqueue_script( 'ucjs-js', plugin_dir_url( __FILE__ ) . '/uc-js.js', array( 'jquery' ));
}
add_action( 'admin_enqueue_scripts', 'ucjs_scripts' );
/**
* Register text domain
*
* @since 1.0
*/
function ucjs_textdomain() {
load_plugin_textdomain( 'ucjs' );
}
add_action( 'init', 'ucjs_textdomain' );
/**
* Delete Options on Uninstall
*
* @since 1.1
*/
function ucjs_uninstall() {
delete_option( 'ucjs_settings' );
}
register_uninstall_hook( __FILE__, 'ucjs_uninstall' );
/**
* Enqueue link to add JS through PHP
*
* This is a typical WP Enqueue statement,
* except that the URL of the script is simply a query var.
* This query var is passed to the URL, and when it is detected by ucjs_add_trigger(),
* It fires ucjs_trigger_check, which writes its PHP/JS to the browser.
*
* Credit for this technique: @Otto http://ottopress.com/2010/dont-include-wp-load-please/
*
* @since 1.1
*/
function ucjs_register_style() {
$options = get_option( 'ucjs_settings' );
if(strlen($options['ucjs-content']) == 0) return;
wp_register_script( 'ucjs', home_url( '/?ucjs=1' ) );
wp_enqueue_script( 'ucjs' );
}
add_action( 'wp_enqueue_scripts', 'ucjs_register_style', 999 );
/**
* Add Query Var Script trigger
*
* Adds a query var to our script, so it can trigger our psuedo-script
*
* @since 1.1
* @param string $vars
* @return array $vars
*/
function ucjs_add_trigger( $vars ) {
$vars[] = 'ucjs';
return $vars;
}
add_filter( 'query_vars', 'ucjs_add_trigger' );
/**
* If trigger (query var) is tripped, load our pseudo-script
*
* I'd prefer to esc $content at the very last moment, but we need to allow the > character.
*
* @since 1.1
*/
function ucjs_trigger_check() {
if ( intval( get_query_var( 'ucjs' ) ) == 1 ) {
ob_start();
header( 'Content-type: text/javascript' );
$options = get_option( 'ucjs_settings' );
$raw_content = isset( $options['ucjs-content'] ) ? $options['ucjs-content'] : '';
$content = str_replace( '&gt;', '>', $raw_content );
echo $content; //xss okay
exit;
ob_clean();
}
}
add_action( 'template_redirect', 'ucjs_trigger_check' );
/**
* Register "Custom JS" submenu in "Appearance" Admin Menu
*
* @since 1.0
*/
function ucjs_register_submenu_page() {
add_theme_page( __( 'JavaScript', 'ucjs' ), __( 'JavaScript', 'ucjs' ), 'manage_network', basename( __FILE__ ), 'ucjs_render_submenu_page' );
}
add_action( 'admin_menu', 'ucjs_register_submenu_page' );
/**
* Register settings
*
* @since 1.0
*/
function ucjs_register_settings() {
register_setting( 'ucjs_settings_group', 'ucjs_settings' );
}
add_action( 'admin_init', 'ucjs_register_settings' );
/**
* Render Admin Menu page
*
* @since 1.0
*/
function ucjs_render_submenu_page() {
$options = get_option( 'ucjs_settings' );
$content = isset( $options['ucjs-content'] ) && ! empty( $options['ucjs-content'] ) ? $options['ucjs-content'] : '/* Enter Your Custom JS Here */';
$content_back = isset( $options['ucjs-content-back'] ) && ! empty( $options['ucjs-content-back'] ) ? $options['ucjs-content-back'] : $options['ucjs-content'];
if ( isset( $_GET['settings-updated'] ) ) : ?>
<div id="message" class="updated"><p><?php _e( 'Custom JS updated successfully.', 'ucjs' ); ?></p></div>
<?php endif; ?>
<div class="wrap">
<h2 style="margin-bottom: 1em;"><?php _e( 'JavaScript', 'ucjs' ); ?></h2>
<form name="ucjs-form" action="options.php" method="post" enctype="multipart/form-data">
<?php settings_fields( 'ucjs_settings_group' ); ?>
<div id="template">
<?php do_action( 'ucjs-form-top' ); ?>
<div>
<textarea cols="70" rows="30" name="ucjs_settings[ucjs-content]" id="ucjs_settings[ucjs-content]" class="ucjs-content" ><?php echo esc_html( $content ); ?></textarea>
<input type="hidden" name="ucjs_settings[ucjs-content-back]" id="ucjs_settings[ucjs-content-back]" value="<?php echo esc_html( $content ); ?>">
<input type="hidden" class="ucjs-content-back" value="<?php echo esc_html( $content_back ); ?>">
</div>
<?php do_action( 'ucjs-textarea-bottom' ); ?>
<div>
<div>
<a href="#" id="revertucjs">Revert to previous version</a>
</div>
<?php submit_button( __( 'Update JavaScript', 'ucjs' ), 'primary', 'submit', true, array('disabled' => true) ); ?>
</div>
<?php do_action( 'ucjs-form-bottom' ); ?>
</div>
</form>
</div>
<?php
}