Making WordPress.org


Ignore:
File:
1 edited

Legend:

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

    r10187 r10794  
    11<?php
     2/**
     3 * Class providing P2/O2 watchlist functionality.
     4 *
     5 * @package handbook
     6 */
    27
    38class WPorg_Handbook_Watchlist {
    49
    5     private static $post_types = array( 'handbook' );
     10    /**
     11     * Memoized array of handbook post types.
     12     *
     13     * @var array
     14     */
     15    private static $post_types;
    616
     17    /**
     18     * Initializes actions.
     19     */
    720    public static function 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 );
     21        add_action( 'init', [ __CLASS__, 'on_init' ] );
    1822    }
    1923
    2024    /**
    21      * Appends '-handbook' to the dynamic post type, if not already 'handbook'.
    22      *
    23      * @param  string $t Hanbook post type name.
    24      * @return string
     25     * Performs actions intended to occur during 'init' action.
    2526     */
    26     private static function append_suffix( $t ) {
    27         if ( in_array( $t, array( 'handbook', 'page' ) ) ) {
    28             return $t;
    29         }
     27    public static function on_init() {
     28        self::$post_types = WPorg_Handbook_Init::get_post_types();
    3029
    31         return $t . '-handbook';
     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 );
    3235    }
    3336
    3437    /**
    35      * Adds a 'Watch' action link to O2
     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
    3690     */
    3791    public static function add_o2_action_link( $actions ) {
     
    4599        }
    46100
    47         if ( 'page' == $post->post_type || ( in_array( $post->post_type, self::$post_types ) && ! is_post_type_archive( self::$post_types ) ) ) {
     101        if ( in_array( $post->post_type, self::$post_types ) && ! is_post_type_archive( self::$post_types ) ) {
    48102            $watchlist = get_post_meta( $post->ID, '_wporg_watchlist', true );
    49103
    50104            if ( $watchlist && in_array( get_current_user_id(), $watchlist ) ) {
    51                 $actions[35] = [
     105                $actions[35] = wp_parse_args( [
    52106                    'action'  => 'watch',
    53                     'text'    => __( 'Unwatch', 'wporg' ),
    54107                    'href'    => wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&post_id=' . $post->ID ), 'unwatch-' . $post->ID ),
    55                     'title'   => __( 'Stop getting notified about changes to this page', 'wporg' ),
    56                     'classes' => [ 'genericon', 'genericon-unsubscribe' ], 
    57                 ];
     108                    'initialState' => 'unwatch',
     109                ], self::get_default_post_action_info( 'unwatch' ) );
    58110            } else {
    59                 $actions[35] = [
     111                $actions[35] = wp_parse_args( [
    60112                    'action'  => 'watch',
    61                     'text'    => __( 'Watch', 'wporg' ),
    62113                    'href'    => wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&watch=1&post_id=' . $post->ID ), 'watch-' . $post->ID ),
    63                     'title'   => __( 'Get notified about changes to this page', 'wporg' ),
    64                     'classes' => [ 'genericon', 'genericon-subscribe' ],
    65                 ];
     114                    'initialState' => 'watch',
     115                ], self::get_default_post_action_info( 'watch' ) );
    66116            }
    67117        }
     118
    68119        return $actions;
    69120    }
    70121
    71122    /**
    72      * Create the HTML for the watch o2 post action.
     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
    73128     */
    74129    public static function get_o2_action_link( $html, $action ) {
     
    79134                $action['title'],
    80135                implode( ' ', $action['classes'] ),
    81                 $action['text']
     136                $action['shortText']
    82137            );
    83138        }
     
    87142
    88143    /**
    89      * Adds a 'Watch' action link to P2
     144     * Outputs a 'Watch' action link to P2.
    90145     */
    91146    public static function display_action_link() {
    92 
    93147        if ( ! is_user_logged_in() ) {
    94148            return;
     
    96150
    97151        $post = get_post();
     152        if ( ! $post ) {
     153            return;
     154        }
    98155
    99         if ( 'page' == $post->post_type || ( in_array( $post->post_type, self::$post_types ) && ! is_post_type_archive( self::$post_types ) ) ) {
    100 
     156        if ( in_array( $post->post_type, self::$post_types ) && ! is_post_type_archive( self::$post_types ) ) {
    101157            $watchlist = get_post_meta( $post->ID, '_wporg_watchlist', true );
    102158
     
    116172                );
    117173            }
    118 
    119174        }
    120 
    121175    }
    122176
     
    124178
    125179WPorg_Handbook_Watchlist::init();
    126 
Note: See TracChangeset for help on using the changeset viewer.