Skip to content
This repository has been archived by the owner. It is now read-only.

Intial commit of UC JS #1

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md

This file was deleted.

9 changes: 9 additions & 0 deletions uc-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
jQuery(document).ready(function($){
$('#revertucjs').click(function(){
$('.ucjs-content').val($('.ucjs-content-back').val());
$('input[name=submit]').attr('disabled', false);
});
$('.ucjs-content').keyup(function(){
$('input[name=submit]').attr('disabled', false);
});
});
154 changes: 154 additions & 0 deletions uc-js.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?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
}