Making WordPress.org


Ignore:
File:
1 edited

Legend:

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

    r10794 r10187  
    11<?php
    2 /**
    3  * Class providing P2/O2 watchlist functionality.
    4  *
    5  * @package handbook
    6  */
    72
    83class WPorg_Handbook_Watchlist {
    94
    10     /**
    11      * Memoized array of handbook post types.
    12      *
    13      * @var array
    14      */
    15     private static $post_types;
     5    private static $post_types = array( 'handbook' );
    166
    17     /**
    18      * Initializes actions.
    19      */
    207    public static function init() {
    21         add_action( 'init', [ __CLASS__, 'on_init' ] );
     8        add_action( 'init', array( __CLASS__, 'on_init' ) );
     9    }
     10
     11    public static function on_init() {
     12        self::$post_types = (array) apply_filters( 'handbook_post_types', self::$post_types );
     13        self::$post_types = array_map( array( __CLASS__, 'append_suffix' ), self::$post_types );
     14
     15        add_action( 'p2_action_links', array(__CLASS__, 'display_action_link'), 100 );
     16        add_filter( 'o2_filter_post_actions', array( __CLASS__, 'add_o2_action_link' ) );
     17        add_filter( 'o2_filter_post_action_html', array( __CLASS__, 'get_o2_action_link' ), 10, 2 );
    2218    }
    2319
    2420    /**
    25      * Performs actions intended to occur during 'init' action.
     21     * Appends '-handbook' to the dynamic post type, if not already 'handbook'.
     22     *
     23     * @param  string $t Hanbook post type name.
     24     * @return string
    2625     */
    27     public static function on_init() {
    28         self::$post_types = WPorg_Handbook_Init::get_post_types();
     26    private static function append_suffix( $t ) {
     27        if ( in_array( $t, array( 'handbook', 'page' ) ) ) {
     28            return $t;
     29        }
    2930
    30         self::o2_register_default_post_action_states();
    31 
    32         add_action( 'p2_action_links',            [ __CLASS__, 'display_action_link' ], 100 );
    33         add_filter( 'o2_filter_post_actions',     [ __CLASS__, 'add_o2_action_link' ] );
    34         add_filter( 'o2_filter_post_action_html', [ __CLASS__, 'get_o2_action_link' ], 10, 2 );
     31        return $t . '-handbook';
    3532    }
    3633
    3734    /**
    38      * Returns default post action info.
    39      *
    40      * @param string $type The post action type. Either 'watch' or 'unwatch'.
    41      * @return array
    42      */
    43     protected static function get_default_post_action_info( $type ) {
    44         $info = [];
    45 
    46         if ( ! in_array( $type, [ 'unwatch', 'watch' ] ) ) {
    47             return $info;
    48         }
    49 
    50         if ( 'watch' === $type ) {
    51             $info = [
    52                 'shortText' => __( 'Watch', 'wporg' ),
    53                 'title'     => __( 'Get notified about changes to this page', 'wporg' ),
    54                 'genericon' => 'genericon-subscribe',
    55                 'classes'   => [ 'genericon', 'genericon-subscribe' ],
    56                 'rel'       => false,
    57             ];
    58         } else {
    59             $info = [
    60                 'shortText' => __( 'Unwatch', 'wporg' ),
    61                 'title'     => __( 'Stop getting notified about changes to this page', 'wporg' ),
    62                 'genericon' => 'genericon-unsubscribe',
    63                 'classes'   => [ 'genericon', 'genericon-unsubscribe' ],
    64                 'rel'       => false,
    65             ];
    66         }
    67 
    68         return $info;
    69     }
    70 
    71     /**
    72      * Registers default post action states.
    73      */
    74     public static function o2_register_default_post_action_states() {
    75         if( ! function_exists( 'o2_register_post_action_states' ) ) {
    76             return;
    77         }
    78 
    79         o2_register_post_action_states( 'watch', [
    80             'unwatch' => self::get_default_post_action_info( 'unwatch' ),
    81             'watch'   => self::get_default_post_action_info( 'watch' ),
    82         ] );
    83     }
    84 
    85     /**
    86      * Adds a 'Watch' action link to O2.
    87      *
    88      * @param array $actions Array of O2 actions.
    89      * @return array
     35     * Adds a 'Watch' action link to O2
    9036     */
    9137    public static function add_o2_action_link( $actions ) {
     
    9945        }
    10046
    101         if ( in_array( $post->post_type, self::$post_types ) && ! is_post_type_archive( self::$post_types ) ) {
     47        if ( 'page' == $post->post_type || ( in_array( $post->post_type, self::$post_types ) && ! is_post_type_archive( self::$post_types ) ) ) {
    10248            $watchlist = get_post_meta( $post->ID, '_wporg_watchlist', true );
    10349
    10450            if ( $watchlist && in_array( get_current_user_id(), $watchlist ) ) {
    105                 $actions[35] = wp_parse_args( [
     51                $actions[35] = [
    10652                    'action'  => 'watch',
     53                    'text'    => __( 'Unwatch', 'wporg' ),
    10754                    'href'    => wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&post_id=' . $post->ID ), 'unwatch-' . $post->ID ),
    108                     'initialState' => 'unwatch',
    109                 ], self::get_default_post_action_info( 'unwatch' ) );
     55                    'title'   => __( 'Stop getting notified about changes to this page', 'wporg' ),
     56                    'classes' => [ 'genericon', 'genericon-unsubscribe' ], 
     57                ];
    11058            } else {
    111                 $actions[35] = wp_parse_args( [
     59                $actions[35] = [
    11260                    'action'  => 'watch',
     61                    'text'    => __( 'Watch', 'wporg' ),
    11362                    'href'    => wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&watch=1&post_id=' . $post->ID ), 'watch-' . $post->ID ),
    114                     'initialState' => 'watch',
    115                 ], self::get_default_post_action_info( 'watch' ) );
     63                    'title'   => __( 'Get notified about changes to this page', 'wporg' ),
     64                    'classes' => [ 'genericon', 'genericon-subscribe' ],
     65                ];
    11666            }
    11767        }
    118 
    11968        return $actions;
    12069    }
    12170
    12271    /**
    123      * Returns the HTML for the watch o2 post action.
    124      *
    125      * @param string $html   The HTML for the given action.
    126      * @param array  $action Data about the action.
    127      * @return string
     72     * Create the HTML for the watch o2 post action.
    12873     */
    12974    public static function get_o2_action_link( $html, $action ) {
     
    13479                $action['title'],
    13580                implode( ' ', $action['classes'] ),
    136                 $action['shortText']
     81                $action['text']
    13782            );
    13883        }
     
    14287
    14388    /**
    144      * Outputs a 'Watch' action link to P2.
     89     * Adds a 'Watch' action link to P2
    14590     */
    14691    public static function display_action_link() {
     92
    14793        if ( ! is_user_logged_in() ) {
    14894            return;
     
    15096
    15197        $post = get_post();
    152         if ( ! $post ) {
    153             return;
    154         }
    15598
    156         if ( in_array( $post->post_type, self::$post_types ) && ! is_post_type_archive( self::$post_types ) ) {
     99        if ( 'page' == $post->post_type || ( in_array( $post->post_type, self::$post_types ) && ! is_post_type_archive( self::$post_types ) ) ) {
     100
    157101            $watchlist = get_post_meta( $post->ID, '_wporg_watchlist', true );
    158102
     
    172116                );
    173117            }
     118
    174119        }
     120
    175121    }
    176122
     
    178124
    179125WPorg_Handbook_Watchlist::init();
     126
Note: See TracChangeset for help on using the changeset viewer.