"; echo "

" . self::$instruction_page_title . "

"; $posts = self::get_notes_by_parent(0); // generate table of contents $output = ''; foreach($posts as $post) { $output .= self::index_with_children($post); } echo "
"; echo "

" . __('Table of Contents') . "

"; echo $output; echo "
"; // generate instructions $output = ''; foreach($posts as $post) { $output .= self::note_with_children($post, 0, true); } echo "
"; echo $output; echo "
"; echo ""; } public static function has_instruction_notes() { $args = array('action'=>'instruction','post_parent'=>0); $posts = self::get_notes($args); if(count($posts)) { return true; } return false; } public static function has_dashboard_notes() { $posts = self::get_notes_by_parent(0); if(count($posts)) { 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_by_parent(0); $output = ''; foreach($posts as $post) { $output .= self::note_with_children($post); } echo $output; } // recursively get the linked post title and it's children public static function index_with_children($post,$depth=0) { if($depth > 64) { // sanity check return "Error: note output aborted, hierarchy depth too deep (>64)"; } $output = "'; return $output; } // recursively get the post and it's children public static function note_with_children($post,$depth=0,$full_post=false) { if($depth > 64) { // sanity check return "Error: note output aborted, hierarchy depth too deep (>64)"; } $output = "'; return $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': if(isset($_GET['page'])) { if($_GET['page']==self::$plugin_id) { return 'instructions'; } break; } return 'dashboard'; case 'edit.php': return 'search'; case 'post-new.php': return 'new'; case 'post.php': return 'edit'; } return ''; } public static function get_current_loc($action=null) { $on_content_type = self::current_post_type(); if($action) { $on_action = 'instructions'; } else { $on_action = self::current_action(); } $loc = "loc_"; $loc .= $on_action; if($on_content_type) { $loc .= "_" . $on_content_type; } return $loc; } public static function get_everywhere_metakey() { return self::$custom_field_prefix . 'loc_all_' . self::current_post_type(); } public static function get_notes_by_parent($which_parent=0) { $args = array('post_parent'=>(int)$which_parent); return self::get_notes($args); } // return all notes public static function get_notes($args=array()) { global $wpdb, $wp_roles, $current_user; if(!isset($current_user->data->wp_capabilities)) { return; } $post_type_name = self::$post_type_name; if(isset($args['action'])) { $which_location = self::get_current_loc($args['action']); } else { $which_location = self::get_current_loc(); } // set up the subquery for role checking $wheres_arr = array(); $pre = self::$custom_field_prefix; $roles = $current_user->data->wp_capabilities; foreach($roles as $role_name=>$role_arr) { $role = $wpdb->escape($role_name); $wheres_arr[] = " (meta_key = '{$pre}role_{$role}' AND meta_value = '1') "; } $where_str = implode(" OR ", $wheres_arr); if(!strlen($where_str)) { return; } $role_query = " SELECT post_id FROM {$wpdb->postmeta} WHERE {$where_str}"; $post_parent = ''; if(isset($args['post_parent'])) { if(is_array($args['post_parent'])) { $p = implode(',',$args['post_parent']); $post_parent = " AND {$wpdb->posts}.post_parent IN '{$p}' "; } else { $post_parent = " AND {$wpdb->posts}.post_parent = '{$args['post_parent']}' "; } } // build the full query if($which_location=='loc_instructions') { $sql = $wpdb->prepare(" SELECT * FROM {$wpdb->postmeta} LEFT JOIN {$wpdb->posts} ON {$wpdb->posts}.id = {$wpdb->postmeta}.post_id WHERE {$wpdb->posts}.post_status = 'publish' AND {$wpdb->posts}.post_type = '%s' AND ({$wpdb->postmeta}.meta_key = '%s' AND {$wpdb->postmeta}.meta_value = '0') AND {$wpdb->posts}.id IN ( {$role_query} ) {$post_parent} GROUP BY {$wpdb->posts}.id ", $post_type_name, self::$custom_field_prefix . 'instructions_exclude'); } else { $sql = $wpdb->prepare(" SELECT * FROM {$wpdb->postmeta} LEFT JOIN {$wpdb->posts} ON {$wpdb->posts}.id = {$wpdb->postmeta}.post_id WHERE {$wpdb->posts}.post_status = 'publish' AND {$wpdb->posts}.post_type = '%s' AND ( ({$wpdb->postmeta}.meta_key = '%s' AND {$wpdb->postmeta}.meta_value = '1') OR ({$wpdb->postmeta}.meta_key = '%s' AND {$wpdb->postmeta}.meta_value = '1') OR ({$wpdb->postmeta}.meta_key = '{$pre}loc_everywhere' AND {$wpdb->postmeta}.meta_value = '1') ) AND {$wpdb->posts}.id IN ( {$role_query} ) {$post_parent} GROUP BY {$wpdb->posts}.id ", $post_type_name, self::$custom_field_prefix . $which_location, self::get_everywhere_metakey() ); } $res = $wpdb->get_results($sql); return $res; } // 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' || self::current_action() == 'instructions' || self::current_action() == '') { return; } $posts = self::get_notes(); $output = ''; if(count($posts)) { foreach($posts as $post) { $t = $post->post_title; $c = $post->post_content; $c = apply_filters('the_content', $c); $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; } // only save these fields on dsn_notes if( (isset($_POST['post_type']) && $_POST['post_type'] != self::$post_type_name ) || (isset($_GET['post_type']) && $_GET['post_type'] != self::$post_type_name )) { 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); self::check_and_save_checkbox('loc_everywhere',$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); self::check_and_save_checkbox("loc_all_".$type,$post_id); } global $wp_roles; $roles = $wp_roles->roles; foreach($roles as $role_name=>$role_arr) { self::check_and_save_checkbox("role_".$role_name,$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 function get_checkbox($key, $msg, $class='', $is_checked=false) { $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 "
"; global $wp_roles; $roles = $wp_roles->roles; echo "
"; echo "

" . __("Show this note for the following roles:") . "

"; foreach($roles as $role_name=>$role_arr) { echo "
"; echo self::get_checkbox("role_".$role_name,$role_arr['name']); echo "
"; } echo "
"; echo "
"; echo "

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

"; echo ""; $ct = 0; echo ""; 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 "
        " . self::get_checkbox("loc_everywhere",__("Everywhere"),'master_check') . "
{$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("loc_all_".$type,__("All"),'parent_check',true) . "
"; 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' ), 'description' => __('Add helpful notes for site admins') ); register_post_type( self::$post_type_name,$args); } // add sickeningly greedy self-serving donate links to the wp plugin page entry public static function extra_plugin_links($data, $page) { if ( $page == self::$base ) { $flattr_url = "http://flattr.com/thing/379485/Dashboard-Site-Notes"; $paypal_url = "https://www.paypal.com/cgi-bin/webscr?business=donate@innerdvations.com&cmd=_donations¤cy_code=EUR&item_name=Donation%%20for%%20Dashboard%%20Site%%20Notes%%20plugin"; $data = array_merge($data,array( sprintf('%s',$flattr_url, esc_html__('Flattr', self::$plugin_id)), sprintf('%s',$paypal_url, esc_html__('Donate', self::$plugin_id)) )); } return $data; } } $dsnmanager = null; function dsn_init_manager() { global $dsnmanager; $dsnmanager = new DSNManager(); } add_action('init','dsn_init_manager', 1);