This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelp-class.php
253 lines (247 loc) · 6.43 KB
/
help-class.php
1
<?php/** * @author Conner McCall * @copyright Copyright (c) 2011, Conner McCall, http://connermccall.com * @license http://en.wikipedia.org/wiki/MIT_License The MIT License * @package wp-help * @version 0.1 * @link https://github.com/sloped/Wordpress-Help */class wp_help { /** * Set default post labels used when creating the new post * type for the help. * * @since 0.1 * @access public * @var array */ var $post_labels = array ('name' => 'Help', 'singular_name' => 'Help', 'menu_name' => 'Help', 'add_new' => 'Add Help', 'add_new_item' => 'Add New Help', 'edit' => 'Edit Help', 'edit_item' => 'Edit Help', 'new_item' => 'New Help', 'view' => 'View Help', 'view_item' => 'View Help', 'search_items' => 'Search Help', 'not_found' => 'No Help Found', 'not_found_in_trash' => 'No Help Found in Trash', ); /** * Set default post type used for the help pages * * @since 0.1 * @access public * @var string */ var $post_pages = 'help'; /** * Set default name for the post type used for the help pages * * @since 0.1 * @access public * @var string */ var $post_name = 'Help'; /** * Sets the default capability type used to control access to the help pages * * @since 0.1 * @access public * @var string */ var $capability_type = 'post'; /** * Used to set the post type the metabox and contextual help are displayed on * * @since 0.1 * @access public * @var string */ var $post_type; /** * Used to set the title of the page containing the actual help * * @since 0.1 * @access public * @var string */ var $help_page_name; /** * Controls whether or not the help is displayed as a metabox * * @since 0.1 * @access public * @var bool */ var $on_edit_screen = true; /** * Controls whether or not the help is displayed as contexual help * * @since 0.1 * @access public * @var bool */ var $in_contextual = true; /** * Used to initialize the post content. * * @since 0.1 * @access public * @var bool */ var $page_content = false; /** * Constructor * * @since 0.1 * @access public * @param array[string]string */ function __construct($args = array()) { if(is_array($args)) { if(array_key_exists('post_pages',$args)) $this->post_pages = $args['post_pages']; if(array_key_exists('labels',$args)) $this->post_labels = $args['labels']; } add_action('init', array($this, '_create_post_type')); } /** * Used to create help pages and place on the appropriate post types * * @since 0.1 * @access public * @param array[string]string */ public function create_help($args) { if(is_array($args)) { if(array_key_exists('post_type',$args)) $this->post_type = $args['post_type']; else die('You must pass a post type to display help on'); if(array_key_exists('page_title', $args)) $this->help_page_title = $args['page_title']; else die('You must pass a title for you help page'); if(array_key_exists('page_content', $args)) $this->page_content = $args['page_content']; } add_action('init',array($this, 'create_help_page')); add_action('add_meta_boxes', array($this, 'add_meta_boxes') , 10,2); } /** * Creates the post type for help pages if it does not exist. * * @since 0.1 * @access private * */ function _create_post_type() { if(!post_type_exists($this->post_pages) ) { register_post_type($this->post_pages, array( 'label' => $this->post_name, 'description' => 'Post type for user editable help', 'public' => false, 'show_ui' => true, 'show_in_menu' => true, 'capability_type' => $this->capability_type, 'hierarchical' => false, 'query_var' => true, 'has_archive' => false ,'supports' => array('editor',), 'menu_position' => 12, 'labels' => $this->post_labels,) ); } } /** * Creates the metabox and contextual tab for the help * * @since 0.1 * @access private * * see get_contents() */ function add_meta_boxes($post_type , $post) { if($this->on_edit_screen) { add_meta_box( $this->post_type . '_help', // id of the <div> we'll add 'Help', //title array($this, 'help_box_contents'), // callback function that will echo the box content $this->post_type // where to add the box: on "post", "page", or "link" page ); } if($this->in_contextual) { if ( $this->post_type == $post_type ) { get_current_screen()->add_help_tab( array( 'id' => 'page-help-id', 'title' => __( 'Help' ), 'content' => $this->get_contents(), ) ); } } } /** * Queries the Wordpress Database for the page containing the help. Formating done via wpautop * * @since 0.1 * @access private * */ private function get_contents() { $help = new WP_query(); $args['name'] = $this->help_page_name; $args['post_type'] = $this->post_pages; $help->query( $args ); if($help->post_count > 0) { foreach( $help->posts as $post ) : setup_postdata($post); ?> <?php return wpautop($post->post_content);?> <?php endforeach; } } /** * Outputs the help page contents into the metabox * * @since 0.1 * @access private * */ function help_box_contents() { ?> <div class="custom_dashboard"> <?php echo $this->get_contents();?> </div> <?php } /** * Creates the help page for the help being created. Does not duplicate pages. * * @since 0.1 * @access private * */ function create_help_page() { if($this->wp_exist_post($this->post_type . '_wp_help') == '') { $post = array( 'comment_status' => 'closed', // 'closed' means no comments. 'ping_status' => 'closed', // 'closed' means pingbacks or trackbacks turned off 'post_author' => 1, //The user ID number of the author. 'post_name' => $this->post_type . '_wp_help', // The name (slug) for your post 'post_status' => 'publish', //Set the status of the new post. 'post_title' => $this->help_page_title, //The title of your post. 'post_type' => $this->post_pages, ); if($this->page_content) $post['page_content'] = $this->page_content; $this->page_content = false; wp_insert_post( $post, true ); } } /** * Helper function to determine if a page exists with the given name. * * @since 0.1 * @access private * */ private function wp_exist_post($slug) { global $wpdb; return $wpdb->get_row("SELECT ID FROM wp_posts WHERE post_name = '" . $slug . "'", 'ARRAY_A'); }}