Making WordPress.org

Changeset 705


Ignore:
Timestamp:
06/16/2014 11:56:42 PM (10 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

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook
Files:
5 edited

Legend:

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

    r18 r705  
    99require_once dirname( __FILE__ ) . '/inc/email-post-changes.php';
    1010
    11 class WPorg_Handbook_TOC_Pages extends WPorg_Handbook_TOC {
    12     protected $post_type = 'page';
    13 }
    14 new WPorg_Handbook_TOC_Pages;
     11new WPorg_Handbook_TOC( array( 'page' ) );
    1512
    1613add_action( 'widgets_init', 'wporg_handbook_functionality_for_pages_widget', 12 );
  • 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;
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/email-post-changes.php

    r10 r705  
    1515
    1616    function handbook_sidebar() {
    17         if ( ! class_exists( 'P2' ) || ! class_exists( 'Email_Post_Changes' ) )
    18             return;
    1917
    2018        require_once dirname( __FILE__ ) . '/widgets.php';
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/table-of-contents.php

    r609 r705  
    66 */
    77class WPorg_Handbook_TOC {
    8     protected $post_type = 'handbook';
     8    protected $post_types = array();
    99
    1010    protected $styles = '<style> .toc-jump { text-align: right; font-size: 12px; } .page .toc-heading { margin-top: -50px; padding-top: 50px !important; }</style>';
    1111
    12     function __construct() {
     12    function __construct( $post_types ) {
     13        $this->post_types = $post_types;
    1314        add_action( 'template_redirect', array( $this, 'load_filters' ) );
    1415    }
    1516
    1617    function load_filters() {
    17         if ( is_singular( $this->post_type ) )
     18        $this->post_types = array_map( array( $this, 'append_suffix' ), $this->post_types );
     19
     20        if ( is_singular( $this->post_types ) )
    1821            add_filter( 'the_content', array( $this, 'add_toc' ) );
     22    }
     23
     24    function append_suffix( $t ) {
     25        if ( 'handbook' == $t )
     26            return $t;
     27
     28        return $t . '-handbook';
    1929    }
    2030
     
    3242            $pages_header = 'h3';
    3343
    34         if ( $pages = wp_list_pages( array( 'child_of' => get_the_ID(), 'echo' => false, 'title_li' => false, 'post_type' => $this->post_type ) ) ) 
    35             $toc .= "<$pages_header>Pages</$pages_header><ul class=\"items\">$pages</ul>";
     44        if ( $pages = wp_list_pages( array( 'child_of' => get_the_ID(), 'echo' => false, 'title_li' => false, 'post_type' => $this->post_type ) ) )
     45            $toc .= "<$pages_header>Pages</$pages_header><ul class=\"items\">$pages</ul>";
    3646
    3747        if ( $items ) {
    3848            $toc .= $this->styles;
    3949            $toc .= '<div class="table-of-contents">';
    40             $toc .= "<$contents_header>Topics</$contents_header><ul class=\"items\">";
     50            $toc .= "<$contents_header>Contents</$contents_header><ul class=\"items\">";
    4151            $last_item = false;
    4252            foreach ( $items as $item ) {
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/widgets.php

    r540 r705  
    22
    33class WPorg_Handbook_Widget extends WP_Widget {
    4     protected $post_type = 'handbook';
     4
     5    protected $post_types = 'handbook';
    56
    67    function __construct() {
     8        $this->post_types = apply_filters( 'handbook_post_types', $this->post_types );
     9        if ( ! is_array( $this->post_types ) ) {
     10            $this->post_types = (array) $this->post_types;
     11        }
     12        $this->post_types = array_map( array( $this, 'append_suffix' ), $this->post_types );
    713        parent::__construct( 'handbook', 'Handbook Tools', array( 'classname' => 'widget_wporg_handbook', 'description' => 'Shows watch/unwatch links for handbook pages.' ) );
    814    }
     
    1117        if ( ! is_user_logged_in() )
    1218            return;
     19
    1320        $post = get_post();
    14 
    15         switch ( $this->post_type ) {
    16             case 'page' :
    17                 if ( $post->post_type !== 'page' )
    18                     return;
    19                 break;
    20             default :
    21                 if ( $post->post_type !== $this->post_type && ! is_post_type_archive( $this->post_type ) )
    22                     return;
    23                 break;
     21        if ( $post->post_type == 'page' || ( in_array( $post->post_type, $this->post_types ) && ! is_post_type_archive( $this->post_types ) ) ) {
     22            $watchlist = get_post_meta( $post->ID, '_wporg_watchlist', true );
     23            if ( $watchlist && in_array( get_current_user_id(), $watchlist ) ) {
     24                printf( '<p>You are watching this page. <a href="%s">Unwatch</a></p>',
     25                wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&post_id=' . $post->ID ), 'unwatch-' . $post->ID ) );
     26            } else {
     27                printf( '<p><a href="%s">Watch this page</a></p>',
     28                wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&watch=1&post_id=' . $post->ID ), 'watch-' . $post->ID ) );
     29            }
     30        } else {
     31            return;
    2432        }
    2533
    26         $watchlist = get_post_meta( $post->ID, '_wporg_watchlist', true );
    27         if ( $watchlist && in_array( get_current_user_id(), $watchlist ) )
    28             printf( '<p class="handbook-watch">You are watching this page. <a href="%s">Unwatch</a></p>',
    29                 wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&post_id=' . $post->ID ), 'unwatch-' . $post->ID ) );
    30         else
    31             printf( '<p class="handbook-watch"><a href="%s">Watch this page</a></p>',
    32                 wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&watch=1&post_id=' . $post->ID ), 'watch-' . $post->ID ) );
     34    }
     35
     36    function append_suffix( $t ) {
     37        if ( 'handbook' == $t )
     38            return $t;
     39
     40        return $t . '-handbook';
    3341    }
    3442}
    3543
    3644class WPorg_Handbook_Widget_for_Pages extends WPorg_Handbook_Widget {
     45
    3746    protected $post_type = 'page';
    3847
     
    4352
    4453class WPorg_Handbook_Pages_Widget extends WP_Widget_Pages {
    45     protected $post_type = 'handbook';
     54
     55    protected $post_types = 'handbook';
    4656
    4757    function __construct() {
     
    5767
    5868    function handbook_post_type( $args ) {
    59         $args['post_type'] = $this->post_type;
     69        $post = get_post();
     70
     71        $this->post_types = apply_filters( 'handbook_post_types', $this->post_types );
     72        if ( ! is_array( $this->post_types ) ) {
     73            $this->post_types = (array) $this->post_types;
     74        }
     75        $this->post_types = array_map( array( $this, 'append_suffix' ), $this->post_types );
     76
     77        if ( in_array( $post->post_type, $this->post_types ) ) {
     78            $args['post_type'] = $post->post_type;
     79        }
    6080        return $args;
    6181    }
     82
     83    function append_suffix( $t ) {
     84        if ( 'handbook' == $t )
     85            return $t;
     86
     87        return $t . '-handbook';
     88    }
    6289}
    63 
Note: See TracChangeset for help on using the changeset viewer.