settings_name = 'cjtoolbox_settings'; $this->cjdata_name = 'cjtoolbox_data'; // Load saved settings $this->getSettings(); // Start this plugin once all other plugins are fully loaded add_action( 'plugins_loaded', array(&$this, 'start_plugin') ); } function __destruct() { // Nothing to do now... } // Save new settings function saveSettings() { update_option($this->settings_name, $this->settings); } // Get back all saved settings function getSettings() { $this->settings = get_option($this->settings_name); } // Save/Get data function saveData() { $this->cjdata = array_values($this->cjdata); update_option($this->cjdata_name, $this->cjdata); } function getData() { $this->cjdata = get_option($this->cjdata_name); } function start_plugin() { if (is_admin() ) { // Load for admin panel add_action('admin_menu', array(&$this, 'add_plugin_menu')); //register the callback been used if options of page been submitted and needs to be processed add_action('admin_post_save_cjtoolbox', array(&$this, 'on_save_changes')); // register ajax save function add_action('wp_ajax_cjtoolbox_save', array(&$this, 'ajax_save_changes')); add_action('wp_ajax_cjtoolbox_save_newcode', array(&$this, 'ajax_save_newcode')); add_action('wp_ajax_cjtoolbox_form', array(&$this, 'ajax_show_form')); add_action('wp_ajax_cjtoolbox_get_code', array(&$this, 'ajax_get_code')); add_action('wp_ajax_cjtoolbox_delete_code', array(&$this, 'ajax_delete_code')); add_action('wp_ajax_cjtoolbox_add_block', array(&$this, 'ajax_add_block')); // Create the tables and sample data } else { // Add the script and style files to footer add_action('wp_head', array(&$this, 'cjtoolbox_insertcode')); } } function cjtoolbox_insertcode() { global $post; // Home page displays a page. $check_for = ''; if(is_home()) { // The blog page. It will be either same as front page or will be a page. $check_for = 'allposts'; } if(is_front_page()) { $check_for = 'frontpage'; } if(is_page()) { $check_for = $post->ID; } if(is_single()) { $check_for = 'allposts'; } $this->getData(); $data = $this->cjdata; foreach($data as $key => $value) { $page_list = $data[$key]['page']; if(is_array($page_list)) { if(is_page() && in_array('allpages', $page_list)) { echo stripslashes($data[$key]['code']); continue; } else if(in_array($check_for, $page_list)) { echo stripslashes($data[$key]['code']); continue; } } if(is_category()) { $this_category = get_query_var('cat'); $category_list = $data[$key]['category']; if(is_array($category_list)) { if(in_array($this_category, $category_list)) { echo stripslashes($data[$key]['code']); continue; } } } $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } $links = stripslashes($data[$key]['links']); $link_list = explode("\n", $links); if(in_array($pageURL, $link_list)) { echo stripslashes($data[$key]['code']); continue; } } } // To add sub page & meta boxes function add_plugin_menu() { $this->hook_manage = add_options_page('CSS & JavaScript Toolbox' ,'CSS & JavaScript Toolbox', '10', 'cjtoolbox', array(&$this, 'admin_display')); // register callback gets call prior your own page gets rendered add_action('load-'.$this->hook_manage, array(&$this, 'admin_load_page')); // register callback to show styles needed for the admin page add_action( 'admin_print_styles-' . $this->hook_manage, array(&$this, 'admin_print_styles') ); // Load scripts for admin panel working add_action('admin_print_scripts-' . $this->hook_manage, array(&$this, 'admin_print_scripts')); } //will be executed if wordpress core detects this page has to be rendered function admin_load_page() { //ensure, that the needed javascripts been loaded to allow drag/drop, expand/collapse and hide/show of boxes wp_enqueue_script('common'); wp_enqueue_script('wp-lists'); wp_enqueue_script('postbox'); wp_enqueue_script('thickbox'); wp_enqueue_script('jquery-ui-tabs'); //add several metaboxes now, all metaboxes registered during load page can be switched off/on at "Screen Options" automatically, nothing special to do therefore // add_meta_box('cjtoolbox-contentbox-1', 'CSS & JavaScript Block 1', array(&$this, 'on_contentbox_1_content'), $this->hook_manage, 'normal', 'core'); } // Apply our stylesheet function admin_print_styles() { wp_enqueue_style('thickbox'); wp_enqueue_style( 'cjtoolbox', CJTOOLBOX_URL . 'media/admin.css', '', CJTOOLBOX_VERSION, 'all' ); } // Add javascripts function admin_print_scripts() { wp_enqueue_style('jquery'); wp_print_scripts('jquery'); ?> getData(); $count = count($this->cjdata); /** RESET the metabox order to normal, we dont want to retain the order now. Just boxes with data are sufficient.*/ $page = $this->hook_manage; $neworder = array(); for($i = 1; $i <= $count; $i++) { $neworder[] = 'cjtoolbox-' . $i; } $order['normal'] = implode(',', $neworder); $user = wp_get_current_user(); update_user_option($user->ID, "meta-box-order_$page", $order, true); /*Block END*/ if($count <= 0) $count = 1; // Atleast have one block by default. for($i = 1; $i <= $count; $i++) { add_meta_box('cjtoolbox-'.$i, 'CSS & JavaScript Block #'.$i, array(&$this, 'cjtoolbox_unit'), $this->hook_manage, 'normal', 'core', $i - 1); } echo '
'; echo '

'; switch ($_GET['page']){ case 'cjtoolbox': default: echo '

CSS & JavaScript Toolbox

'; $this->cjtoolbox_manage(); break; } echo '
'; } // Display the admin page to manage custom css and javscripts function cjtoolbox_manage() { $this->getData(); $count = count($this->cjdata); /* print_r($this->cjdata); echo "
";
$page = $this->hook_manage;
$sorted = get_user_option( "meta-box-order_$page" );
print_r($sorted);
echo "
"; */ ?>
Like this plugin? Please support our work
Options Updated
Options Reset
Working...
hook_manage, 'normal', $this->cjdata); ?>

getData(); ?>
show_page_list($boxid, $this->cjdata[$boxid]['page'], $this->cjdata[$boxid]['category']); ?>

CSS Template show_dropdown_box('css', $boxid);?>

Insert Code Delete Code New

JS Template show_dropdown_box('js', $boxid);?>

Insert Code Delete Code New

Click for Hints & Tips

Delete This Block

Add this CSS/JS code to ?

Add this CSS/JS code to category page?

0, 'exclude' => '', 'hide_empty' => false, 'hierarchical' => 1, 'include' => '', 'include_last_update_time' => false, 'number' => 9999, 'order' => 'ASC', 'orderby' => 'name', 'pad_counts' => false, ); $terms = get_terms( $taxonomy_name, $args ); if ( ! $terms || is_wp_error($terms) ) { // No items return; } $db_fields = false; if ( is_taxonomy_hierarchical( $taxonomy_name ) ) { $db_fields = array( 'parent' => 'parent', 'id' => 'term_id' ); } $walker = new cj_Walker_Nav_Menu_Checklist( $db_fields, $boxid, 'category', $taxonomy_selected ); $args['walker'] = $walker; echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $terms), 0, (object) $args ); } function show_pages_with_checkbox($boxid, $pages_selected) { $post_type_name = 'page'; $args = array( 'order' => 'ASC', 'orderby' => 'title', 'posts_per_page' => 9999, 'post_type' => $post_type_name, 'suppress_filters' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false ); // @todo transient caching of these results with proper invalidation on updating of a post of this type $get_posts = new WP_Query; $posts = $get_posts->query( $args ); if ( ! $get_posts->post_count ) { // No items return; } $db_fields = false; if ( is_post_type_hierarchical( $post_type_name ) ) { $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' ); } $walker = new cj_Walker_Nav_Menu_Checklist( $db_fields, $boxid, 'page', $pages_selected ); $post_type_object = get_post_type_object($post_type_name); $args['walker'] = $walker; $checkbox_items = walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $posts), 0, (object) $args ); echo $checkbox_items; } //executed if the post arrives initiated by pressing the submit button of form function on_save_changes() { //user permission check if ( !current_user_can('manage_options') ) wp_die( __('Cheatin’ uh?') ); //cross check the given referer check_admin_referer('cjtoolbox'); // TODO: This is not coded now because we use AJAX. Should be coded to make the plugin work even if javascript is disabled. //process here your on $_POST validation and / or option saving //print_r($_POST); //die(); //lets redirect the post request into get request (you may add additional params at the url, if you need to show save results wp_redirect(add_query_arg( array('updated' => 'true'), $_POST['_wp_http_referer'])); } function ajax_add_block() { check_ajax_referer('cjtoolbox-admin', 'security'); $count = (int) $_POST['count']; if($count == 0) $count = 1; $args = array(); $args['args'] = $count; ?>

CSS & JavaScript Block #

cjtoolbox_unit('', $args); ?>
add_cjdata($type, $title, $code); if($response != false) { die($response); } die('Oops, unable to save '.$type.' code template! Please try again!!!'); } function ajax_save_changes() { check_ajax_referer('cjtoolbox-admin', 'security'); if($_POST['action'] == 'cjtoolbox_save') { // Save data and return 1 on success $cjdata = $_POST['cjtoolbox']; foreach($cjdata as $key => $value) { if($value['code'] == '') { unset($cjdata[$key]); } } $this->cjdata = $cjdata; $this->saveData(); return 1; die('1'); } return 0; // Something wrong! die(); // this is required to return a proper result } function ajax_delete_code() { check_ajax_referer('cjtoolbox-admin', 'security'); $type = $_POST['type']; $id = (int) $_POST['id']; if($id <=0 || ($type != 'js' && $type != 'css')) return 'Invalid Request: Unable to process the request!'; $this->delete_cjdata($type, $id); die('1'); } function ajax_get_code() { check_ajax_referer('cjtoolbox-admin', 'security'); $type = $_POST['type']; $id = (int) $_POST['id']; if($id <=0 || ($type != 'js' && $type != 'css')) return 'Invalid Request: Unable to process the request!'; $code = $this->get_cjdata($type, $id); die($code); } function ajax_show_form() { $type = ''; switch($_GET['type']) { case 'js': $type = 'js'; break; case 'css': default: $type = 'css'; break; } ?>

prepare("SELECT id, title FROM {$wpdb->prefix}cjtoolbox_cjdata WHERE type = '{$type}'"); $list = $wpdb->get_results($query); if(count($list)) { echo ''; } } function add_cjdata($type, $title, $code) { global $wpdb; if($type == '' || $title == '' || $code == '') return false; $query = $wpdb->prepare("INSERT INTO {$wpdb->prefix}cjtoolbox_cjdata (type,title,code) VALUES ('%s', '%s', '%s')", $type, $title, $code); $wpdb->query($query); // Get inserted id $lastid = $wpdb->get_var("SELECT id FROM {$wpdb->prefix}cjtoolbox_cjdata ORDER BY id DESC LIMIT 0,1"); return $lastid; } function delete_cjdata($type, $id) { global $wpdb; if($type == '' || $id <= 0) return false; $query = $wpdb->prepare("DELETE FROM {$wpdb->prefix}cjtoolbox_cjdata WHERE type = '%s' AND id = '%d' LIMIT 1", $type, $id); $wpdb->query($query); return true; } function get_cjdata($type, $id) { global $wpdb; if($type == '' || $id <= 0) return false; $query = $wpdb->prepare("SELECT code FROM {$wpdb->prefix}cjtoolbox_cjdata WHERE type = '%s' AND id = '%d' LIMIT 1", $type, $id); $code = $wpdb->get_var($query); return $code; } function activate_plugin() { global $wpdb; $database_version = CJTOOLBOX_VERSION; $installed_db = get_option('cjtoolbox_db_version'); if($database_version != $installed_db) { require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); // Create the table structure $sql = "CREATE TABLE `{$wpdb->prefix}cjtoolbox_cjdata` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT , `type` VARCHAR( 15 ) NOT NULL , `title` TINYTEXT NOT NULL , `code` MEDIUMTEXT NOT NULL , PRIMARY KEY ( `id` , `type` ) )"; dbDelta($sql); update_option( "cjtoolbox_db_version", $database_version ); // Add sample code $count = $wpdb->get_var("SELECT count(*) FROM {$wpdb->prefix}cjtoolbox_cjdata WHERE type='css'"); if($count == 0) { $wpdb->query("INSERT INTO {$wpdb->prefix}cjtoolbox_cjdata (type,title,code) VALUES ('css','Inline CSS Declaration','')"); $wpdb->query("INSERT INTO {$wpdb->prefix}cjtoolbox_cjdata (type,title,code) VALUES ('css','External Stylesheet','')"); } $count = $wpdb->get_var("SELECT count(*) FROM {$wpdb->prefix}cjtoolbox_cjdata WHERE type='js'"); if($count == 0) { $wpdb->query("INSERT INTO {$wpdb->prefix}cjtoolbox_cjdata (type,title,code) VALUES ('js','Inline JavaScript Declaration','')"); $wpdb->query("INSERT INTO {$wpdb->prefix}cjtoolbox_cjdata (type,title,code) VALUES ('js','External JavaScript','')"); } } } }// END Class // Let's start the plugin global $cssJSToolbox; $cssJSToolbox = new cssJSToolbox(); // Activation register_activation_hook(__FILE__, array(&$cssJSToolbox, "activate_plugin")); } /** Following code copied from WordPress core */ /** * Create HTML list of nav menu input items. * * @package WordPress * @since 3.0.0 * @uses Walker_Nav_Menu */ class cj_Walker_Nav_Menu_Checklist extends Walker_Nav_Menu { function __construct( $fields = false, $boxid = 0, $type = 'page', $selected = array() ) { if ( $fields ) { $this->db_fields = $fields; } $this->boxid = $boxid; $this->selected = $selected; $this->type = $type; } function start_lvl( &$output, $depth ) { $indent = str_repeat( "\t", $depth ); $output .= "\n$indent"; } /** * @see Walker::start_el() * @since 3.0.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $item Menu item data object. * @param int $depth Depth of menu item. Used for padding. * @param object $args */ function start_el(&$output, $item, $depth, $args) { $possible_object_id = $item->object_id; $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $output .= $indent . '
  • '; $output .= ' '; } }