1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gamajo Dashboard Glancer, for WP 3.7 and earlier. |
4
|
|
|
* |
5
|
|
|
* @package Gamajo\DashboardGlancer |
6
|
|
|
* @author Gary Jones |
7
|
|
|
* @link https://github.com/gamajo/dashboard-glancer |
8
|
|
|
* @copyright 2013 Gary Jones, Gamajo |
9
|
|
|
* @license GPL-2.0-or-later |
10
|
|
|
* @version 1.0.4 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Easily add items to the Right Now Dashboard widget in WordPress 3.7-. |
15
|
|
|
* |
16
|
|
|
* @package Gamajo\DashboardGlancer |
17
|
|
|
* @author Gary Jones |
18
|
|
|
*/ |
19
|
|
|
class Gamajo_Dashboard_RightNow extends Gamajo_Dashboard_Glancer { |
20
|
|
|
/** |
21
|
|
|
* Automatically show any registered items. |
22
|
|
|
* |
23
|
|
|
* With this, there's no need to explicitly call show() during the |
24
|
|
|
* `dashboard_glance_items` hook, and items can be registered at any time |
25
|
|
|
* before `dashboard_glance_items` priority 20 (including on earlier hooks). |
26
|
|
|
* |
27
|
|
|
* @since 1.0.0 |
28
|
|
|
*/ |
29
|
|
|
public function __construct() { |
30
|
|
|
add_action( 'right_now_content_table_end', array( $this, 'show' ), 20 ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Build and return the data and markup for a single item. |
35
|
|
|
* |
36
|
|
|
* If the item count is zero, return an empty string, to avoid visual clutter. |
37
|
|
|
* |
38
|
|
|
* @since 1.0.0 |
39
|
|
|
* |
40
|
|
|
* @param array $item Registered item. |
41
|
|
|
* @return string Markup, or empty string if item count is zero. |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
protected function get_single_item( array $item ) { |
|
|
|
|
44
|
|
|
$num_posts = wp_count_posts( $item['type'] ); |
45
|
|
|
$count = $num_posts->{$item['status']}; |
46
|
|
|
|
47
|
|
|
if ( ! $count ) { |
48
|
|
|
return ''; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$href = $this->get_link_url( $item ); |
52
|
|
|
$num = $this->maybe_link( number_format_i18n( $count ), $href ); |
53
|
|
|
$text = $this->maybe_link( $this->get_label( $item, $count ), $href ); |
54
|
|
|
|
55
|
|
|
return $this->get_markup( $num . '|' . $text, $item['type'] ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Wrap number and text within table row markup. |
60
|
|
|
* |
61
|
|
|
* @since 1.0.0 |
62
|
|
|
* |
63
|
|
|
* @param string $text Text to display. May be wrapped in a link. |
64
|
|
|
* @param string $post_type Post type. |
65
|
|
|
* @return string Markup for list item. |
66
|
|
|
*/ |
67
|
|
|
protected function get_markup( $text, $post_type ) { |
68
|
|
|
$text_parts = explode( '|', $text ); |
69
|
|
|
|
70
|
|
|
return '<tr> |
71
|
|
|
<td class="first b ' . sanitize_html_class( 'b-' . $post_type ) . '">' . $text_parts[0] . '</td> |
72
|
|
|
<td class="t ' . sanitize_html_class( $post_type ) . '">' . $text_parts[1] . '</td> |
73
|
|
|
</tr>'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.