ID, 'dashboard')) { return true; } } return false; } public function setup_dashboard() { wp_add_dashboard_widget('dsn_dashboard' , 'Admin Guide', array($this,'dsn_dashboard')); } public static function dsn_dashboard() { $posts = self::get_notes(); $output = ''; foreach($posts as $post) { $meta = get_post_meta($post->ID, null); $t = $post->post_title; $c = $post->post_content; $c = apply_filters('the_content', $c); $m = self::should_display_post($post->ID); if(self::should_display_post($post->ID)) { $output .= "

{$t}

{$c}
"; } } echo $output; } // returns the current custom post type if applicable, or false if not public static function current_post_type() { if(isset($_GET['post_type'])) { return $_GET['post_type']; } else if(isset($_GET['post'])) { global $post; if(isset($post->ID)) { return get_post_type($post->ID); } } return ''; } // returns the current action public static function current_action() { global $pagenow; switch($pagenow) { case 'index.php': return 'dashboard'; case 'edit.php': return 'search'; case 'post-new.php': return 'new'; case 'post.php': return 'edit'; } } // given a post_id, checks if it should be displayed on the current page public function should_display_post($post_id=null, $on_action=null, $on_content_type=null) { if(!$on_content_type) { $on_content_type = self::current_post_type(); } if(!$on_action) { $on_action = self::current_action(); } if(!$post_id) { global $post; $post_id = $post->ID; } if(!$on_action || !$post_id) { return false; } $meta_field = self::$custom_field_prefix . "loc_"; $meta_field .= $on_action; if($on_content_type) { $meta_field .= "_" . $on_content_type; } $result = get_post_meta($post_id,$meta_field); if(isset($result[0])) { if($result[0]) { return $meta_field; } return false; } return false; } // return all notes public static function get_notes() { $args = array( 'post_type'=>self::$post_type_name, 'orderby'=>'menu_order', 'order'=>'ASC' ); return get_posts($args); } // Called on hook 'all_admin_notices' public function all_admin_notices() { // on the dashboard we print a pretty widget, not a notice if(self::current_action() == 'dashboard') { return; } $posts = self::get_notes(); $output = ''; foreach($posts as $post) { $meta = get_post_meta($post->ID, null); $t = $post->post_title; $c = $post->post_content; $c = apply_filters('the_content', $c); if(self::should_display_post($post->ID)) { $output .= "
{$t}
{$c}
"; } } echo $output; } // Called on wordpress hook 'admin_init' public function admin_init() { add_meta_box('display-location-div', __('When and where to display this message'), array($this,'display_info_metabox'), 'dsn_note', 'normal', 'low'); add_action('save_post', array($this,'save_meta')); $types = get_post_types(); foreach($types as $type=>$type_obj) { if(!in_array($type,self::$exclude_types)) { self::$post_types[$type] = get_post_type_object($type); } } } // Called on wordpress hook 'init' public function init() { self::add_content_type(); } // save all of our meta fields public static function save_meta($post_id) { // prevent wp from killing our custom fields during autosave if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) { return $post_id; } if(is_array($_POST) && count($_POST)) { self::check_and_save_checkbox('loc_dashboard',$post_id); self::check_and_save_checkbox('instructions_exclude',$post_id); foreach(self::$post_types as $type=>$type_obj) { self::check_and_save_checkbox("loc_edit_".$type,$post_id); self::check_and_save_checkbox("loc_new_".$type,$post_id); self::check_and_save_checkbox("loc_search_".$type,$post_id); } } } // saves post data from checkboxes public static function check_and_save_checkbox($key,$post_id = null) { if(!$post_id) { global $post; $post_id = $post->ID; } $key = self::$custom_field_prefix . $key; if(isset($_POST[$key])) { update_post_meta($post_id, $key, 1); } else { update_post_meta($post_id, $key, 0); } } public static $custom; public static function get_checkbox($key, $msg, $class='') { $checked = ''; $key = self::$custom_field_prefix . $key; if(isset(self::$custom[$key][0]) && self::$custom[$key][0] == 1) { $checked = " checked='checked' "; } $ret = ""; $ret .= ""; $ret .= ""; $ret .= ""; return $ret; } public static function display_info_metabox() { global $post; self::$custom = get_post_custom($post->ID); echo "
"; echo "
"; echo self::get_checkbox("loc_dashboard",__("Include in the dashboard widget")); echo "
"; /* TODO: manual action settings echo "
"; echo "
"; echo self::get_checkbox("dsn_loc_manual",__("Include this note on the following pages (eg, options-general.php to appear on the Settings->General page).")); echo "
"; */ echo "
"; echo self::get_checkbox("instructions_exclude",__("Exclude this note from the site instruction manual.")); echo "
"; echo "
"; echo "

" . __("Display this message in the following locations:") . "

"; echo ""; $ct = 0; foreach(self::$post_types as $type=>$type_obj) { if($ct++ % 2 == 0) $class = ' odd '; else $class = ' even '; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
{$type_obj->name}:" . self::get_checkbox("loc_edit_".$type,__("Edit"),'child_check') . "" . self::get_checkbox("loc_new_".$type,__("New"),'child_check') . "" . self::get_checkbox("loc_search_".$type,__("Search"),'child_check') . "" . self::get_checkbox("all_".$type,__("Everywhere"),'parent_check') . "
"; echo "
"; } // Create the site note content type public static function add_content_type() { $labels = array( 'name' => __( 'Site Notes' ), 'singular_name' => __( 'Site Note' ), 'add_new_item' => __( 'Add Site Note' ), 'edit_item' => __( 'Edit Site Note' ), 'new_item' => __( 'New Site Note' ), 'view_item' => __( 'View Site Note' ) ); $supports = array( 'editor'=>true, 'title'=>true, 'page-attributes'=>true, 'hierarchy'=>true ); // TODO: add capability_type for security so non-superadmins can't add/edit notes $args = array( 'labels' => $labels, 'public' => false, 'publicly_queryable' => false, 'show_ui' => self::$has_edit, 'hierarchical' => true, 'page-attributes' => true, 'revisions' => true, //'supports' => array('title', 'editor', 'excerpt', 'page-attributes' ), 'supports' => array('title', 'editor', 'page-attributes' ), 'description' => __('Add helpful notes for site admins') ); register_post_type( self::$post_type_name,$args); } } $dsnmanager = null; function dsn_init_manager() { global $dsnmanager; $dsnmanager = new DSNManager(); } add_action('init','dsn_init_manager', 1);