This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jmr06005/master
Intial commit of UC JS
- Loading branch information
Showing
3 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( '>', '>', $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 | ||
} |