Changes in sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/watchlist.php [10187:10794]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/watchlist.php
r10187 r10794 1 1 <?php 2 /** 3 * Class providing P2/O2 watchlist functionality. 4 * 5 * @package handbook 6 */ 2 7 3 8 class WPorg_Handbook_Watchlist { 4 9 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; 6 16 17 /** 18 * Initializes actions. 19 */ 7 20 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' ] ); 18 22 } 19 23 20 24 /** 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. 25 26 */ 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(); 30 29 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 ); 32 35 } 33 36 34 37 /** 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 36 90 */ 37 91 public static function add_o2_action_link( $actions ) { … … 45 99 } 46 100 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 ) ) { 48 102 $watchlist = get_post_meta( $post->ID, '_wporg_watchlist', true ); 49 103 50 104 if ( $watchlist && in_array( get_current_user_id(), $watchlist ) ) { 51 $actions[35] = [105 $actions[35] = wp_parse_args( [ 52 106 'action' => 'watch', 53 'text' => __( 'Unwatch', 'wporg' ),54 107 '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' ) ); 58 110 } else { 59 $actions[35] = [111 $actions[35] = wp_parse_args( [ 60 112 'action' => 'watch', 61 'text' => __( 'Watch', 'wporg' ),62 113 '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' ) ); 66 116 } 67 117 } 118 68 119 return $actions; 69 120 } 70 121 71 122 /** 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 73 128 */ 74 129 public static function get_o2_action_link( $html, $action ) { … … 79 134 $action['title'], 80 135 implode( ' ', $action['classes'] ), 81 $action[' text']136 $action['shortText'] 82 137 ); 83 138 } … … 87 142 88 143 /** 89 * Adds a 'Watch' action link to P2144 * Outputs a 'Watch' action link to P2. 90 145 */ 91 146 public static function display_action_link() { 92 93 147 if ( ! is_user_logged_in() ) { 94 148 return; … … 96 150 97 151 $post = get_post(); 152 if ( ! $post ) { 153 return; 154 } 98 155 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 ) ) { 101 157 $watchlist = get_post_meta( $post->ID, '_wporg_watchlist', true ); 102 158 … … 116 172 ); 117 173 } 118 119 174 } 120 121 175 } 122 176 … … 124 178 125 179 WPorg_Handbook_Watchlist::init(); 126
Note: See TracChangeset
for help on using the changeset viewer.