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