Making WordPress.org


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.