Making WordPress.org

Changeset 10794


Ignore:
Timestamp:
03/07/2021 08:41:31 PM (4 years ago)
Author:
coffee2code
Message:

Handbooks, Watchlist: Play nicer with o2 to avoid error_log messages when creating a watch/unwatch action link

  • Call o2_register_post_action_states() when function exists to register config for the two watch states
  • Introduce get_default_post_action_info() as getter for config info for each watch state
  • Configure more action attributes expected by o2 (e.g. initialState, genericon, rel, shortText)
  • Prevent "no post action states have been registered for action watch" error_log message
File:
1 edited

Legend:

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

    r10793 r10794  
    2828        self::$post_types = WPorg_Handbook_Init::get_post_types();
    2929
     30        self::o2_register_default_post_action_states();
     31
    3032        add_action( 'p2_action_links',            [ __CLASS__, 'display_action_link' ], 100 );
    3133        add_filter( 'o2_filter_post_actions',     [ __CLASS__, 'add_o2_action_link' ] );
    3234        add_filter( 'o2_filter_post_action_html', [ __CLASS__, 'get_o2_action_link' ], 10, 2 );
     35    }
     36
     37    /**
     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        ] );
    3383    }
    3484
     
    53103
    54104            if ( $watchlist && in_array( get_current_user_id(), $watchlist ) ) {
    55                 $actions[35] = [
     105                $actions[35] = wp_parse_args( [
    56106                    'action'  => 'watch',
    57                     'text'    => __( 'Unwatch', 'wporg' ),
    58107                    'href'    => wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&post_id=' . $post->ID ), 'unwatch-' . $post->ID ),
    59                     'title'   => __( 'Stop getting notified about changes to this page', 'wporg' ),
    60                     'classes' => [ 'genericon', 'genericon-unsubscribe' ], 
    61                 ];
     108                    'initialState' => 'unwatch',
     109                ], self::get_default_post_action_info( 'unwatch' ) );
    62110            } else {
    63                 $actions[35] = [
     111                $actions[35] = wp_parse_args( [
    64112                    'action'  => 'watch',
    65                     'text'    => __( 'Watch', 'wporg' ),
    66113                    'href'    => wp_nonce_url( admin_url( 'admin-post.php?action=wporg_watchlist&watch=1&post_id=' . $post->ID ), 'watch-' . $post->ID ),
    67                     'title'   => __( 'Get notified about changes to this page', 'wporg' ),
    68                     'classes' => [ 'genericon', 'genericon-subscribe' ],
    69                 ];
     114                    'initialState' => 'watch',
     115                ], self::get_default_post_action_info( 'watch' ) );
    70116            }
    71117        }
     
    88134                $action['title'],
    89135                implode( ' ', $action['classes'] ),
    90                 $action['text']
     136                $action['shortText']
    91137            );
    92138        }
Note: See TracChangeset for help on using the changeset viewer.