Making WordPress.org


Ignore:
Timestamp:
06/16/2014 11:56:42 PM (9 years ago)
Author:
coffee2code
Message:

Handbook plugin: abstract post-type handling to allow for multiple post-type usage, namely for Devhub. props nicolealleyinteractivecom. See #250

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/handbook.php

    r474 r705  
    1010require_once dirname( __FILE__ ) . '/inc/email-post-changes.php';
    1111
    12 WPorg_Handbook_Glossary::init();
    13 new WPorg_Handbook_TOC;
     12//WPorg_Handbook_Glossary::init();
     13
     14/**
     15 * Initialize our handbooks
     16 *
     17 */
     18class WPorg_Handbook_Init {
     19
     20    static function init() {
     21
     22        $post_types = 'handbook';
     23
     24        $post_types = apply_filters( 'handbook_post_types', $post_types );
     25
     26        if ( ! is_array( $post_types ) ) {
     27            $post_types = (array) $post_types;
     28        }
     29
     30        new WPorg_Handbook_TOC( $post_types );
     31
     32        foreach ( $post_types as $type ) {
     33            new WPorg_Handbook( $type );
     34
     35        }
     36    }
     37}
     38add_action( 'after_setup_theme', array( 'WPorg_Handbook_Init', 'init' ) );
    1439
    1540class WPorg_Handbook {
     41
     42    public $post_type = '';
     43
     44    protected $label = '';
    1645
    1746    static function caps() {
     
    3160    }
    3261
    33     function __construct() {
     62    function __construct( $type ) {
     63        if ( 'handbook' != $type )
     64            $this->post_type = $type . '-handbook';
     65
     66        $this->label = ucwords( str_replace( array( '-', '_' ), ' ', $this->post_type ) );
    3467        add_filter( 'user_has_cap', array( $this, 'grant_handbook_caps' ) );
    3568        add_filter( 'init', array( $this, 'register_post_type' ) );
     
    5689
    5790    function register_post_type() {
    58         register_post_type( 'handbook', array(
     91        if ( 'handbook' != $this->post_type ) {
     92            $slug = 'handbook/' . substr( $this->post_type, 0, -9 );
     93        } else {
     94            $slug = 'handbook';
     95        }
     96        register_post_type( $this->post_type, array(
    5997            'labels' => array(
    60                 'name' => 'Handbook Pages',
    61                 'singular_name' => 'Handbook Page',
    62                 'menu_name' => 'Handbook',
     98                'name' => "{$this->label} Pages",
     99                'singular_name' => "{$this->label} Page",
     100                'menu_name' => "{$this->label}",
     101                'all_items' => "{$this->label} Pages",
    63102            ),
    64103            'public' => true,
     
    69108            'hierarchical' => true,
    70109            'menu_position' => 11,
    71             'rewrite' => true,
     110            'rewrite'     => array(
     111                'feeds'      => false,
     112                'slug'       => $slug,
     113                'with_front' => false,
     114            ),
    72115            'delete_with_user' => false,
    73116            'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
     
    77120    function admin_page_access_denied() {
    78121        if ( ! current_user_can( 'read' ) ) {
    79             wp_redirect( admin_url( 'edit.php?post_type=handbook' ) );
     122            wp_redirect( admin_url( "edit.php?post_type={$this->post_type}" ) );
    80123            exit;
    81124        }
     
    83126
    84127    function post_type_link( $link, $post ) {
    85         if ( $post->post_type === 'handbook' && $post->post_name === 'handbook' )
    86             return get_post_type_archive_link( 'handbook' );
     128        if ( $post->post_type === $this->post_type && $post->post_name === $this->post_type )
     129            return get_post_type_archive_link( $this->post_type );
    87130        return $link;
    88131    }
    89132
    90133    function pre_get_posts( $query ) {
    91         if ( $query->is_main_query() && ! $query->is_admin && $query->is_post_type_archive( 'handbook' ) ) {
    92             $query->set( 'handbook', 'handbook' );
     134        if ( $query->is_main_query() && ! $query->is_admin && $query->is_post_type_archive( $this->post_type ) ) {
     135            $query->set( 'handbook', $this->post_type );
    93136        }
    94137    }
    95138
    96139    function handbook_sidebar() {
    97         if ( ! class_exists( 'P2' ) )
    98             return;
    99 
    100         register_sidebar( array( 'id' => 'handbook', 'name' => 'Handbook', 'description' => 'Used on handbook pages' ) );
    101 
     140        register_sidebar( array( 'id' => $this->post_type, 'name' => $this->label, 'description' => "Used on {$this->label} pages" ) );
    102141        require_once dirname( __FILE__ ) . '/inc/widgets.php';
    103142        register_widget( 'WPorg_Handbook_Pages_Widget' );
     
    105144
    106145    function wporg_email_changes_for_post_types( $post_types ) {
    107         if ( ! in_array( 'handbook', $post_types ) )
    108             $post_types[] = 'handbook';
     146        if ( ! in_array( $this->post_type, $post_types ) )
     147            $post_types[] = $this->post_type;
    109148        return $post_types;
    110149    }
    111150}
    112 
    113 new WPorg_Handbook;
Note: See TracChangeset for help on using the changeset viewer.