| 1 |
<?php |
| 2 |
|
| 3 |
class WP_Stream_Dashboard_Widget { |
| 4 |
|
| 5 |
public static function load() { |
| 6 |
// Load Dashboard widget |
| 7 |
add_action( 'wp_dashboard_setup', array( __CLASS__, 'stream_activity' ) ); |
| 8 |
|
| 9 |
// Dashboard AJAX pagination |
| 10 |
add_action( 'wp_ajax_stream_activity_dashboard_update', array( __CLASS__, 'stream_activity_update_contents' ) ); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Add Stream Activity widget to the dashboard |
| 15 |
* |
| 16 |
* @action wp_dashboard_setup |
| 17 |
*/ |
| 18 |
public static function stream_activity() { |
| 19 |
if ( ! current_user_can( WP_Stream_Admin::VIEW_CAP ) ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
wp_add_dashboard_widget( |
| 24 |
'dashboard_stream_activity', |
| 25 |
esc_html__( 'Stream Activity', 'stream' ), |
| 26 |
array( __CLASS__, 'stream_activity_initial_contents' ), |
| 27 |
array( __CLASS__, 'stream_activity_options' ) |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
public static function stream_activity_initial_contents() { |
| 32 |
self::stream_activity_contents(); |
| 33 |
} |
| 34 |
|
| 35 |
public static function stream_activity_update_contents() { |
| 36 |
$paged = ! empty( $_POST['stream-paged'] ) ? absint( $_POST['stream-paged'] ) : 1; |
| 37 |
self::stream_activity_contents( $paged ); |
| 38 |
die; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Contents of the Stream Activity dashboard widget |
| 43 |
*/ |
| 44 |
public static function stream_activity_contents( $paged = 1 ) { |
| 45 |
$options = get_option( 'dashboard_stream_activity_options', array() ); |
| 46 |
$records_per_page = isset( $options['records_per_page'] ) ? absint( $options['records_per_page'] ) : 5; |
| 47 |
$args = array( |
| 48 |
'records_per_page' => $records_per_page, |
| 49 |
'paged' => $paged, |
| 50 |
); |
| 51 |
|
| 52 |
$records = wp_stream_query( $args ); |
| 53 |
$total_items = WP_Stream::$db->get_found_rows(); |
| 54 |
|
| 55 |
if ( ! $records ) { |
| 56 |
?> |
| 57 |
<p class="no-records"><?php esc_html_e( 'Sorry, no activity records were found.', 'stream' ) ?></p> |
| 58 |
<?php |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
printf( |
| 63 |
'<ul>%s</ul>', |
| 64 |
implode( '', array_map( array( __CLASS__, 'widget_row' ), $records ) ) |
| 65 |
); |
| 66 |
|
| 67 |
$args = array( |
| 68 |
'current' => $paged, |
| 69 |
'total_pages' => absint( ceil( $total_items / $records_per_page ) ), // Cast as an integer, not a float |
| 70 |
); |
| 71 |
|
| 72 |
self::pagination( $args ); |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* Display pagination links for Dashboard Widget |
| 77 |
* Copied from private class WP_List_Table::pagination() |
| 78 |
*/ |
| 79 |
public static function pagination( $args = array() ) { |
| 80 |
$args = wp_parse_args( |
| 81 |
$args, |
| 82 |
array( |
| 83 |
'current' => 1, |
| 84 |
'total_pages' => 1, |
| 85 |
) |
| 86 |
); |
| 87 |
|
| 88 |
$current = $args['current']; |
| 89 |
$total_pages = $args['total_pages']; |
| 90 |
|
| 91 |
$records_link = add_query_arg( |
| 92 |
array( 'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG ), |
| 93 |
self_admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE ) |
| 94 |
); |
| 95 |
|
| 96 |
$html_view_all = sprintf( |
| 97 |
'<a class="%s" title="%s" href="%s">%s</a>', |
| 98 |
'view-all', |
| 99 |
esc_attr__( 'View all records', 'stream' ), |
| 100 |
esc_url( $records_link ), |
| 101 |
esc_html__( 'View All', 'stream' ) |
| 102 |
); |
| 103 |
|
| 104 |
$page_links = array(); |
| 105 |
$disable_first = ''; |
| 106 |
$disable_last = ''; |
| 107 |
|
| 108 |
if ( 1 === $current ) { |
| 109 |
$disable_first = ' disabled'; |
| 110 |
} |
| 111 |
|
| 112 |
if ( $current === $total_pages ) { |
| 113 |
$disable_last = ' disabled'; |
| 114 |
} |
| 115 |
|
| 116 |
$page_links[] = sprintf( |
| 117 |
'<a class="%s" title="%s" href="%s" data-page="1">%s</a>', |
| 118 |
'first-page' . $disable_first, |
| 119 |
esc_attr__( 'Go to the first page', 'stream' ), |
| 120 |
esc_url( remove_query_arg( 'paged', $records_link ) ), |
| 121 |
'«' |
| 122 |
); |
| 123 |
|
| 124 |
$page_links[] = sprintf( |
| 125 |
'<a class="%s" title="%s" href="%s" data-page="%s">%s</a>', |
| 126 |
'prev-page' . $disable_first, |
| 127 |
esc_attr__( 'Go to the previous page', 'stream' ), |
| 128 |
esc_url( add_query_arg( 'paged', max( 1, $current - 1 ), $records_link ) ), |
| 129 |
max( 1, $current - 1 ), |
| 130 |
'‹' |
| 131 |
); |
| 132 |
|
| 133 |
$html_total_pages = sprintf( '<span class="total-pages">%s</span>', number_format_i18n( $total_pages ) ); |
| 134 |
$page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging', 'stream' ), number_format_i18n( $current ), $html_total_pages ) . '</span>'; |
| 135 |
|
| 136 |
$page_links[] = sprintf( |
| 137 |
'<a class="%s" title="%s" href="%s" data-page="%s">%s</a>', |
| 138 |
'next-page' . $disable_last, |
| 139 |
esc_attr__( 'Go to the next page', 'stream' ), |
| 140 |
esc_url( add_query_arg( 'paged', min( $total_pages, $current + 1 ), $records_link ) ), |
| 141 |
min( $total_pages, $current + 1 ), |
| 142 |
'›' |
| 143 |
); |
| 144 |
|
| 145 |
$page_links[] = sprintf( |
| 146 |
'<a class="%s" title="%s" href="%s" data-page="%s">%s</a>', |
| 147 |
'last-page' . $disable_last, |
| 148 |
esc_attr__( 'Go to the last page', 'stream' ), |
| 149 |
esc_url( add_query_arg( 'paged', $total_pages, $records_link ) ), |
| 150 |
$total_pages, |
| 151 |
'»' |
| 152 |
); |
| 153 |
|
| 154 |
$html_pagination_links = ' |
| 155 |
<div class="tablenav"> |
| 156 |
<div class="tablenav-pages"> |
| 157 |
<span class="pagination-links">' . join( "\n", $page_links ) . '</span> |
| 158 |
</div> |
| 159 |
<div class="clear"></div> |
| 160 |
</div>'; |
| 161 |
|
| 162 |
echo '<div>' . $html_view_all . $html_pagination_links . '</div>'; // xss ok |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Configurable options for the Stream Activity dashboard widget |
| 167 |
*/ |
| 168 |
public static function stream_activity_options() { |
| 169 |
$options = get_option( 'dashboard_stream_activity_options', array() ); |
| 170 |
|
| 171 |
if ( 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['dashboard_stream_activity_options'] ) ) { |
| 172 |
$options['records_per_page'] = absint( $_POST['dashboard_stream_activity_options']['records_per_page'] ); |
| 173 |
$options['live_update'] = isset( $_POST['dashboard_stream_activity_options']['live_update'] ) ? 'on' : 'off';; |
| 174 |
update_option( 'dashboard_stream_activity_options', $options ); |
| 175 |
} |
| 176 |
|
| 177 |
if ( ! isset( $options['records_per_page'] ) ) { |
| 178 |
$options['records_per_page'] = 5; |
| 179 |
} |
| 180 |
|
| 181 |
?> |
| 182 |
<div id="dashboard-stream-activity-options"> |
| 183 |
<p> |
| 184 |
<input type="number" step="1" min="1" max="999" class="screen-per-page" name="dashboard_stream_activity_options[records_per_page]" id="dashboard_stream_activity_options[records_per_page]" value="<?php echo absint( $options['records_per_page'] ) ?>"> |
| 185 |
<label for="dashboard_stream_activity_options[records_per_page]"><?php esc_html_e( 'Records per page', 'stream' ) ?></label> |
| 186 |
</p> |
| 187 |
<?php $value = isset( $options['live_update'] ) ? $options['live_update'] : 'on'; ?> |
| 188 |
<p> |
| 189 |
<input type="checkbox" name="dashboard_stream_activity_options[live_update]" id="dashboard_stream_activity_options[live_update]" value='on' <?php checked( $value, 'on' ) ?> /> |
| 190 |
<label for="dashboard_stream_activity_options[live_update]"><?php esc_html_e( 'Enable live updates', 'stream' ) ?></label> |
| 191 |
</p> |
| 192 |
</div> |
| 193 |
<?php |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Renders rows for Stream Activity Dashboard Widget |
| 198 |
* |
| 199 |
* @param obj Record to be inserted |
| 200 |
* @param int Row number |
| 201 |
* |
| 202 |
* @return string Contents of new row |
| 203 |
*/ |
| 204 |
public static function widget_row( $item ) { |
| 205 |
$author = new WP_Stream_Author( (int) $item->author, (array) $item->author_meta ); |
| 206 |
|
| 207 |
$time_author = sprintf( |
| 208 |
_x( |
| 209 |
'%1$s ago by <a href="%2$s">%3$s</a>', |
| 210 |
'1: Time, 2: User profile URL, 3: User display name', |
| 211 |
'stream' |
| 212 |
), |
| 213 |
human_time_diff( strtotime( $item->created ) ), |
| 214 |
esc_url( $author->get_records_page_url() ), |
| 215 |
esc_html( $author->get_display_name() ) |
| 216 |
); |
| 217 |
|
| 218 |
if ( $author->get_agent() ) { |
| 219 |
$time_author .= sprintf( ' %s', WP_Stream_Author::get_agent_label( $author->get_agent() ) ); |
| 220 |
} |
| 221 |
|
| 222 |
ob_start() |
| 223 |
?> |
| 224 |
<li data-datetime="<?php echo wp_stream_get_iso_8601_extended_date( strtotime( $item->created ) ) ?>"> |
| 225 |
<div class="record-avatar"> |
| 226 |
<a href="<?php echo esc_url( $author->get_records_page_url() ) ?>"> |
| 227 |
<?php echo $author->get_avatar_img( 72 ); // xss ok ?> |
| 228 |
</a> |
| 229 |
</div> |
| 230 |
<span class="record-meta"><?php echo $time_author; // xss ok ?></span> |
| 231 |
<br/> |
| 232 |
<?php echo esc_html( $item->summary ) ?> |
| 233 |
</li> |
| 234 |
<?php |
| 235 |
|
| 236 |
return ob_get_clean(); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Handles Live Updates for Stream Activity Dashboard Widget. |
| 241 |
* |
| 242 |
* @uses gather_updated_items |
| 243 |
* |
| 244 |
* @param array Response to heartbeat |
| 245 |
* @param array Response from heartbeat |
| 246 |
* |
| 247 |
* @return array Data sent to heartbeat |
| 248 |
*/ |
| 249 |
public static function live_update( $response, $data ) { |
| 250 |
if ( ! isset( $data['wp-stream-heartbeat-last-time'] ) ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
$send = array(); |
| 255 |
|
| 256 |
$last_time = $data['wp-stream-heartbeat-last-time']; |
| 257 |
|
| 258 |
$updated_items = WP_Stream_Live_Update::gather_updated_items( $last_time ); |
| 259 |
|
| 260 |
if ( ! empty( $updated_items ) ) { |
| 261 |
ob_start(); |
| 262 |
foreach ( $updated_items as $item ) { |
| 263 |
echo self::widget_row( $item ); //xss okay |
| 264 |
} |
| 265 |
|
| 266 |
$send = ob_get_clean(); |
| 267 |
} |
| 268 |
|
| 269 |
return $send; |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|