Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Disable direct access/execution to/of the widget code. |
||
| 4 | */ |
||
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
||
| 6 | exit; |
||
| 7 | } |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Jetpack_My_Community_Widget displays community members of this site. |
||
| 11 | * |
||
| 12 | * A community member is a WordPress.com user that liked or commented on an entry or subscribed to the site. |
||
| 13 | * Requires WordPress.com connection to work. Otherwise it won't be visible in Widgets screen in admin. |
||
| 14 | */ |
||
| 15 | class Jetpack_My_Community_Widget extends WP_Widget { |
||
| 16 | /** |
||
| 17 | * Transient expiration time. |
||
| 18 | * |
||
| 19 | * @var int $expiration |
||
| 20 | */ |
||
| 21 | static $expiration = 600; |
||
|
0 ignored issues
–
show
|
|||
| 22 | |||
| 23 | /** |
||
| 24 | * Default widget title. |
||
| 25 | * |
||
| 26 | * @var string $default_title |
||
| 27 | */ |
||
| 28 | var $default_title; |
||
|
0 ignored issues
–
show
The visibility should be declared for property
$default_title.
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using class A {
var $property;
}
the property is implicitly global. To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2. Loading history...
|
|||
| 29 | |||
| 30 | /** |
||
| 31 | * Registers the widget with WordPress. |
||
| 32 | */ |
||
| 33 | function __construct() { |
||
| 34 | parent::__construct( |
||
| 35 | 'jetpack_my_community', // Base ID |
||
| 36 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 37 | apply_filters( 'jetpack_widget_name', esc_html__( 'My Community', 'jetpack' ) ), |
||
| 38 | array( |
||
| 39 | 'description' => esc_html__( 'A sampling of users from your blog.', 'jetpack' ), |
||
| 40 | ) |
||
| 41 | ); |
||
| 42 | |||
| 43 | View Code Duplication | if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) { |
|
| 44 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); |
||
| 45 | } |
||
| 46 | |||
| 47 | $this->default_title = esc_html__( 'Community', 'jetpack' ); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Enqueue stylesheet for grid layout. |
||
| 52 | */ |
||
| 53 | function enqueue_style() { |
||
| 54 | wp_register_style( 'jetpack-my-community-widget', plugins_url( 'my-community/style.css', __FILE__ ), array(), '20160129' ); |
||
| 55 | wp_enqueue_style( 'jetpack-my-community-widget' ); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Back-end widget form. |
||
| 60 | * |
||
| 61 | * @see WP_Widget::form() |
||
| 62 | * |
||
| 63 | * @param array $instance Previously saved values from database. |
||
| 64 | * |
||
| 65 | * @return string|void |
||
| 66 | */ |
||
| 67 | function form( $instance ) { |
||
| 68 | $title = isset( $instance['title' ] ) ? $instance['title'] : false; |
||
| 69 | if ( false === $title ) { |
||
| 70 | $title = $this->default_title; |
||
| 71 | } |
||
| 72 | |||
| 73 | $number = isset( $instance['number'] ) ? $instance['number'] : 10; |
||
| 74 | if ( ! in_array( $number, array( 10, 50 ) ) ) { |
||
| 75 | $number = 10; |
||
| 76 | } |
||
| 77 | |||
| 78 | $include_likers = isset( $instance['include_likers'] ) ? (bool) $instance['include_likers'] : true; |
||
| 79 | $include_followers = isset( $instance['include_followers'] ) ? (bool) $instance['include_followers'] : true; |
||
| 80 | $include_commenters = isset( $instance['include_commenters'] ) ? (bool) $instance['include_commenters'] : true; |
||
| 81 | ?> |
||
| 82 | |||
| 83 | <p> |
||
| 84 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
||
| 85 | <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
||
| 86 | </p> |
||
| 87 | |||
| 88 | <p> |
||
| 89 | <label><?php esc_html_e( 'Show how many users?', 'jetpack' ); ?></label> |
||
| 90 | </p> |
||
| 91 | <ul> |
||
| 92 | <li><label><input id="<?php echo $this->get_field_id( 'number' ); ?>-few" name="<?php echo $this->get_field_name( 'number' ); ?>" type="radio" value="10" <?php checked( '10', $number ); ?> /> <?php esc_html_e( 'A few', 'jetpack' ); ?></label></li> |
||
| 93 | <li><label><input id="<?php echo $this->get_field_id( 'number' ); ?>-lots" name="<?php echo $this->get_field_name( 'number' ); ?>" type="radio" value="50" <?php checked( '50', $number ); ?> /> <?php esc_html_e( 'Lots', 'jetpack' ); ?></label></li> |
||
| 94 | </ul> |
||
| 95 | |||
| 96 | <p> |
||
| 97 | <label for="<?php echo $this->get_field_id( 'include_likers' ); ?>"> |
||
| 98 | <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'include_likers' ); ?>" name="<?php echo $this->get_field_name( 'include_likers' ); ?>" value="1" <?php checked( $include_likers, 1 ); ?> /> |
||
| 99 | <?php esc_html_e( 'Include activity from likers', 'jetpack' ); ?> |
||
| 100 | </label> |
||
| 101 | </p> |
||
| 102 | |||
| 103 | <p> |
||
| 104 | <label for="<?php echo $this->get_field_id( 'include_followers' ); ?>"> |
||
| 105 | <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'include_followers' ); ?>" name="<?php echo $this->get_field_name( 'include_followers' ); ?>" value="1" <?php checked( $include_followers, 1 ); ?> /> |
||
| 106 | <?php esc_html_e( 'Include activity from followers', 'jetpack' ); ?> |
||
| 107 | </label> |
||
| 108 | </p> |
||
| 109 | |||
| 110 | <p> |
||
| 111 | <label for="<?php echo $this->get_field_id( 'include_commenters' ); ?>"> |
||
| 112 | <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'include_commenters' ); ?>" name="<?php echo $this->get_field_name( 'include_commenters' ); ?>" value="1" <?php checked( $include_commenters, 1 ); ?> /> |
||
| 113 | <?php esc_html_e( 'Include activity from commenters', 'jetpack' ); ?> |
||
| 114 | </label> |
||
| 115 | </p> |
||
| 116 | |||
| 117 | <?php |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Sanitize widget form values as they are saved. |
||
| 122 | * |
||
| 123 | * @see WP_Widget::update() |
||
| 124 | * |
||
| 125 | * @param array $new_instance Values just sent to be saved. |
||
| 126 | * @param array $old_instance Previously saved values from database. |
||
| 127 | * |
||
| 128 | * @return array Updated safe values to be saved. |
||
| 129 | */ |
||
| 130 | function update( $new_instance, $old_instance ) { |
||
| 131 | $instance = array(); |
||
| 132 | $instance['title'] = wp_kses( $new_instance['title'], array() ); |
||
| 133 | if ( $instance['title'] === $this->default_title ) { |
||
| 134 | $instance['title'] = false; // Store as false in case of language change |
||
| 135 | } |
||
| 136 | |||
| 137 | $instance['number'] = (int) $new_instance['number']; |
||
| 138 | if ( !in_array( $instance['number'], array( 10, 50 ) ) ) { |
||
| 139 | $instance['number'] = 10; |
||
| 140 | } |
||
| 141 | |||
| 142 | $instance['include_likers'] = (bool) $new_instance['include_likers']; |
||
| 143 | $instance['include_followers'] = (bool) $new_instance['include_followers']; |
||
| 144 | $instance['include_commenters'] = (bool) $new_instance['include_commenters']; |
||
| 145 | |||
| 146 | delete_transient( "$this->id-{$instance['number']}" . (int) $instance['include_likers'] . (int) $instance['include_followers'] . (int) $instance['include_commenters'] ); |
||
| 147 | |||
| 148 | return $instance; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Front-end display of widget. |
||
| 153 | * |
||
| 154 | * @see WP_Widget::widget() |
||
| 155 | * |
||
| 156 | * @param array $args Widget arguments. |
||
| 157 | * @param array $instance Saved values from database. |
||
| 158 | */ |
||
| 159 | function widget( $args, $instance ) { |
||
| 160 | $title = isset( $instance['title' ] ) ? $instance['title'] : false; |
||
| 161 | |||
| 162 | if ( false === $title ) { |
||
| 163 | $title = $this->default_title; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
||
| 167 | $title = apply_filters( 'widget_title', $title ); |
||
| 168 | |||
| 169 | echo $args['before_widget']; |
||
| 170 | |||
| 171 | if ( ! empty( $title ) ) { |
||
| 172 | echo $args['before_title'] . $title . $args['after_title']; |
||
| 173 | } |
||
| 174 | |||
| 175 | $transient_name = "$this->id-{$instance['number']}" . (int) $instance['include_likers'] . (int) $instance['include_followers'] . (int) $instance['include_commenters']; |
||
| 176 | |||
| 177 | $my_community = get_transient( $transient_name ); |
||
| 178 | |||
| 179 | if ( empty( $my_community ) ) { |
||
| 180 | $my_community = $this->get_community( $instance ); |
||
| 181 | |||
| 182 | set_transient( $transient_name, $my_community, self::$expiration ); |
||
| 183 | } |
||
| 184 | |||
| 185 | echo $my_community; |
||
| 186 | |||
| 187 | echo $args['after_widget']; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Initiate request and render the response. |
||
| 192 | * |
||
| 193 | * @since 4.0 |
||
| 194 | * |
||
| 195 | * @param array $query |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | function get_community( $query ) { |
||
| 200 | $members = $this->fetch_remote_community( $query ); |
||
| 201 | |||
| 202 | if ( ! empty( $members ) ) { |
||
| 203 | |||
| 204 | $my_community = '<div class="widgets-multi-column-grid"><ul>'; |
||
| 205 | |||
| 206 | foreach ( $members as $member ) { |
||
| 207 | $my_community .= sprintf( '<li><a href="%s" %s><img alt="" src="%s" class="avatar avatar-240" height="48" width="48" originals="240" scale="1" /></a></li>', |
||
| 208 | $member->profile_URL, |
||
| 209 | empty( $member->name ) ? '' : 'title="' . $member->name . '"', |
||
| 210 | $member->avatar_URL |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | $my_community .= '</ul></div>'; |
||
| 215 | |||
| 216 | } else { |
||
| 217 | if ( current_user_can( 'edit_theme_options' ) ) { |
||
| 218 | $my_community = '<p>' . wp_kses( sprintf( __( 'There are no users to display in this <a href="%1$s">My Community widget</a>. <a href="%2$s">Want more traffic?</a>', 'jetpack' ), |
||
| 219 | admin_url( 'widgets.php' ), |
||
| 220 | 'http://en.support.wordpress.com/getting-more-site-traffic/' |
||
| 221 | ), array( 'a' => array( 'href' => true ) ) ) . '</p>'; |
||
| 222 | } else { |
||
| 223 | $my_community = '<p>' . esc_html__( "I'm just starting out; leave me a comment or a like :)", 'jetpack' ) . '</p>'; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | return $my_community; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Request community members to WordPress.com endpoint. |
||
| 232 | * |
||
| 233 | * @since 4.0 |
||
| 234 | * |
||
| 235 | * @param $query |
||
| 236 | * |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | function fetch_remote_community( $query ) { |
||
| 240 | $jetpack_blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 241 | $url = add_query_arg( |
||
| 242 | array( |
||
| 243 | 'number' => $query['number'], |
||
| 244 | 'likers' => (int) $query['include_likers'], |
||
| 245 | 'followers' => (int) $query['include_followers'], |
||
| 246 | 'commenters' => (int) $query['include_commenters'], |
||
| 247 | ), |
||
| 248 | "https://public-api.wordpress.com/rest/v1.1/sites/$jetpack_blog_id/community" |
||
| 249 | ); |
||
| 250 | $response = wp_remote_get( $url ); |
||
| 251 | $response_body = wp_remote_retrieve_body( $response ); |
||
| 252 | |||
| 253 | if ( empty( $response_body ) ) { |
||
| 254 | return array(); |
||
| 255 | } |
||
| 256 | |||
| 257 | $response_body = json_decode( $response_body ); |
||
| 258 | |||
| 259 | if ( isset( $response_body->users ) ) { |
||
| 260 | return $response_body->users; |
||
| 261 | } |
||
| 262 | |||
| 263 | return array(); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * If site is connected to WordPress.com, register the widget. |
||
| 269 | * |
||
| 270 | * @since 4.0 |
||
| 271 | */ |
||
| 272 | function jetpack_my_community_init() { |
||
| 273 | if ( Jetpack::is_active() ) { |
||
| 274 | register_widget( 'Jetpack_My_Community_Widget' ); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | add_action( 'widgets_init', 'jetpack_my_community_init' ); |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.