????
Current Path : /home2/morganrand/backup.morganrand.com/wp-content/themes/wanderfuls/framework/addons/ |
Current File : /home2/morganrand/backup.morganrand.com/wp-content/themes/wanderfuls/framework/addons/custom-js.php |
<?php /** * Creates the admin panel and custom JS output * * @package Wanderfuls WordPress Theme * @subpackage Framework */ // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { exit; } // Start Class if ( ! class_exists( 'TB_Custom_JS' ) ) { class TB_Custom_JS { /** * Start things up * * @since 1.6.0 */ public function __construct() { add_action( 'admin_menu', array( $this, 'add_page' ), 20 ); add_action( 'admin_init', array( $this,'register_settings' ) ); add_action( 'admin_enqueue_scripts',array( $this,'scripts' ) ); add_action( 'admin_notices', array( $this, 'notices' ) ); add_action( 'wp_footer' , array( $this, 'output_js' ), 99 ); } /** * Add sub menu page for the custom JS input * * @since 1.6.0 */ public function add_page() { add_submenu_page( TB_THEME_PANEL_SLUG, __( 'Custom JS', 'tb' ), __( 'Custom JS', 'tb' ), 'administrator', TB_THEME_PANEL_SLUG .'-custom-js', array( $this, 'create_admin_page' ) ); } /** * Load scripts * * @since 1.6.0 */ public function scripts( $hook ) { if ( TB_ADMIN_PANEL_HOOK_PREFIX . '-custom-js' == $hook ) { wp_deregister_script( 'ace-editor' ); wp_enqueue_script( 'tb-ace-editor', TB_FRAMEWORK_DIR_URI .'addons/assets/ace.js', array(), true ); } } /** * Register a setting and its sanitization callback. * * @since 1.6.0 */ public function register_settings() { register_setting( 'tb_custom_js', 'tb_custom_js', array( $this, 'sanitize' ) ); } /** * Displays all messages registered to 'tb-custom_js-notices' * * @since 1.6.0 */ public static function notices() { settings_errors( 'tb_custom_js_notices' ); } /** * Sanitization callback * * @since 1.6.0 */ public static function sanitize( $option ) { // Set option to theme mod set_theme_mod( 'custom_js', $option ); // Return notice add_settings_error( 'tb_custom_js_notices', esc_attr( 'settings_updated' ), __( 'Settings saved.', 'tb' ), 'updated' ); // Lets save the custom JS into a standard option as well for backup return $option; } /** * Settings page output * * @since 1.6.0 */ public static function create_admin_page() { ?> <div class="wrap"> <h2><?php _e( 'Custom JS', 'tb' ); ?></h2> <div> <form method="post" action="options.php"> <?php settings_fields( 'tb_custom_js' ); ?> <table class="form-table"> <tr valign="top"> <td style="padding:0;"> <textarea rows="40" cols="50" id="tb_custom_js" style="display:none;" name="tb_custom_js"><?php echo tb_get_mod( 'custom_js', false ); ?></textarea> <pre id="tb_custom_js_editor" style="width:100%;height:800px;font-size:14px; border: 1px solid #BABABA;"><?php echo tb_get_mod( 'custom_js', false ); ?></pre> </td> </tr> </table> <?php submit_button(); ?> </form> </div> </div><!-- .wrap --> <script> ( function( $ ) { "use strict"; jQuery( document ).ready( function( $ ) { // Start ace editor var $css_editor = $( '#tb_custom_js_editor' ), $css_editor_input = $( '#tb_custom_js' ), $editor = ace.edit( 'tb_custom_js_editor' ); $editor.getSession().setUseWorker(false); $editor.setTheme( "ace/theme/chrome" ); $editor.getSession().setMode( "ace/mode/javascript" ); $editor.find('needle',{ backwards: false, wrap: false, caseSensitive: false, wholeWord: false, regExp: false }); $editor.findNext(); $editor.findPrevious(); // Add val to hidden field $editor.on('input', function() { $css_editor_input.val( $editor.getValue() ); } ); } ); } ) ( jQuery ); </script> <?php } /** * Outputs the custom JS to the wp_head * * @since 1.6.0 */ public static function output_js() { $output = ''; if ( $js = tb_get_mod( 'custom_js', false ) ) { ?> <!-- CUSTOM JS --> <script type="text/javascript"> ( function( $ ) { "use strict"; <?php echo $js; ?> } ) ( jQuery ); </script> <?php } } } } $tb_custom_js = new TB_Custom_JS();