Skip to content

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryJones committed Aug 12, 2015
2 parents eb108c5 + 6481057 commit 8c380a5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 45 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Change Log for Gamajo Dashboard Glancer

All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][unreleased]

_No changes._

## [1.0.1] - 2015-08-12

- Added PHP 7 compatibility.
- Documentation and code standards.

## 1.0.0 - 2014-02-17

- Initial release.

[unreleased]: https://github.com/GaryJones/Gamajo-Dashboard-Glancer/compare/1.0.0...HEAD
[1.0.1]: https://github.com/GaryJones/Gamajo-Dashboard-Glancer/compare/1.0.0...1.0.1
69 changes: 35 additions & 34 deletions class-gamajo-dashboard-glancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @link http://gamajo.com/dashboard-glancer
* @copyright 2013 Gary Jones, Gamajo Tech
* @license GPL-2.0+
* @version 1.0.1
*/

/**
Expand All @@ -16,7 +17,6 @@
* @author Gary Jones
*/
class Gamajo_Dashboard_Glancer {

/**
* Hold all of the items to show.
*
Expand All @@ -29,8 +29,9 @@ class Gamajo_Dashboard_Glancer {
/**
* Automatically show any registered items.
*
* With this, there's no need to explicitly call show() during the dashboard_glance_items hook,
* and items can be registered at any time before dashboard_glance_items priority 20 (including on earlier hooks).
* With this, there's no need to explicitly call show() during the
* `dashboard_glance_items` hook, and items can be registered at any time
* before `dashboard_glance_items` priority 20 (including on earlier hooks).
*
* @since 1.0.0
*/
Expand All @@ -44,9 +45,11 @@ public function __construct() {
* @since 1.0.0
*
* @param array|string $post_types Post type name, or array of post type names.
* @param array|string $statuses Post status or array of different post type statuses
*
* @return Return early if action hook has already passed, or no valid post types were given.
* @param array|string $statuses Optional. Post status or array of
* different post type statuses. Default is
* `publish`.
* @return null Return early if action hook has already passed, or no valid
* post types were given.
*/
public function add( $post_types, $statuses = 'publish' ) {
// If relevant output action hook has already passed, then no point in proceeding.
Expand All @@ -57,17 +60,17 @@ public function add( $post_types, $statuses = 'publish' ) {

$post_types = $this->unset_invalid_post_types( (array) $post_types );

// If all given post types were invalid, bail now
// If all given post types were invalid, bail now.
if ( ! $post_types ) {
return;
}

// Register each combination of given post type and status
foreach( $post_types as $post_type ) {
// Register each combination of given post type and status.
foreach ( $post_types as $post_type ) {
foreach ( (array) $statuses as $status ) {
$this->items[] = array(
'type' => $post_type,
'status' => $status, // No checks yet to see if status is valid
'status' => $status, // No checks yet to see if status is valid.
);
}
}
Expand All @@ -80,9 +83,9 @@ public function add( $post_types, $statuses = 'publish' ) {
*/
public function show() {
foreach ( $this->items as $item ) {
echo $this->get_single_item( $item );
echo wp_kses_post( $this->get_single_item( $item ) );
}
// Reset items, so items aren't shown again if show() is re-called
// Reset items, so items aren't shown again if show() is re-called.
unset( $this->items );
}

Expand All @@ -92,11 +95,10 @@ public function show() {
* @since 1.0.0
*
* @param array $post_types Each of the post types to check.
*
* @return array List of the given post types that are valid.
*/
protected function unset_invalid_post_types( array $post_types ) {
foreach( $post_types as $index => $post_type ) {
foreach ( $post_types as $index => $post_type ) {
$post_type_object = get_post_type_object( $post_type );
if ( is_null( $post_type_object ) ) {
unset( $post_types[ $index ] );
Expand All @@ -113,21 +115,20 @@ protected function unset_invalid_post_types( array $post_types ) {
*
* @since 1.0.0
*
* @param array $item Registered item.
*
* @param array $item Registered item.
* @return string Markup, or empty string if item count is zero.
*/
protected function get_single_item( array $item ) {
$num_posts = wp_count_posts( $item['type'] );
$count = (int) $num_posts->$item['status'];
$count = (int) $num_posts->{$item['status']};

if ( ! $count ) {
return '';
}

$href = $this->get_link_url( $item );
$text = number_format_i18n( $count ) . ' ' . $this->get_label( $item, $count );
$text = $this->maybe_link( $text, $href );
$href = $this->get_link_url( $item );
$text = number_format_i18n( $count ) . ' ' . $this->get_label( $item, $count );
$text = $this->maybe_link( $text, $href );

return $this->get_markup( $text, $item['type'] );
}
Expand All @@ -137,16 +138,16 @@ protected function get_single_item( array $item ) {
*
* @since 1.0.0
*
* @param array $item Registered item.
* @param int $count Number of items present in WP.
*
* @param array $item Registered item.
* @param int $count Number of items present in WP.
* @return string
*/
protected function get_label( array $item, $count ) {
$post_type_object = get_post_type_object( $item['type'] );
$label = 1 === $count ? $post_type_object->labels->singular_name : $post_type_object->labels->name;
$labels = $post->labels;
$label = 1 === $count ? $labels->singular_name : $labels->name;

// Append status for non-publish statuses for disambiguation
// Append status for non-publish statuses for disambiguation.
if ( 'publish' !== $item['status'] ) {
$label .= ' (' . $item['status'] . ')';
}
Expand All @@ -159,9 +160,8 @@ protected function get_label( array $item, $count ) {
*
* @since 1.0.0
*
* @param array $item Registered item.
*
* @return string Admin URL to view the entries of the given post type with the given status
* @param array $item Registered item.
* @return string Admin URL to view the entries of the given post type with the given status
*/
public function get_link_url( array $item ) {
return 'edit.php?post_status=' . $item['status'] . '&post_type=' . $item['type'];
Expand All @@ -172,15 +172,15 @@ public function get_link_url( array $item ) {
*
* @since 1.0.0
*
* @param string $text Text to potentially wrap in a link.
* @param string $href Link target.
*
* @return string Text wrapped in a link if current user can edit posts, or original text otherwise.
* @param string $text Text to potentially wrap in a link.
* @param string $href Link target.
* @return string Text wrapped in a link if current user can edit posts, or original text otherwise.
*/
protected function maybe_link( $text, $href ) {
if ( current_user_can( 'edit_posts' ) ) {
return '<a href="' . esc_url( $href ) . '">' . $text . '</a>';
}

return $text;
}

Expand All @@ -189,10 +189,11 @@ protected function maybe_link( $text, $href ) {
*
* @since 1.0.0
*
* @param string $text Text to display. May be wrapped in a link.
* @param string $text Text to display. May be wrapped in a link.
* @param string $post_type Post type.
* @return string Markup for list item.
*/
protected function get_markup( $text, $post_type ) {
return '<li class="' . sanitize_html_class( $post_type . '-count' ) . '">' . $text . '</li>' . "\n";
}

}
26 changes: 15 additions & 11 deletions class-gamajo-dashboard-rightnow.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @link http://gamajo.com/dashboard-glancer
* @copyright 2013 Gary Jones, Gamajo Tech
* @license GPL-2.0+
* @version 1.0.1
*/

/**
Expand All @@ -16,12 +17,12 @@
* @author Gary Jones
*/
class Gamajo_Dashboard_RightNow extends Gamajo_Dashboard_Glancer {

/**
* Automatically show any registered items.
*
* With this, there's no need to explicitly call show() during the dashboard_glance_items hook,
* and items can be registered at any time before dashboard_glance_items priority 20 (including on earlier hooks).
* With this, there's no need to explicitly call show() during the
* `dashboard_glance_items` hook, and items can be registered at any time
* before `dashboard_glance_items` priority 20 (including on earlier hooks).
*
* @since 1.0.0
*/
Expand All @@ -36,33 +37,36 @@ public function __construct() {
*
* @since 1.0.0
*
* @param array $item Registered item.
*
* @param array $item Registered item.
* @return string Markup, or empty string if item count is zero.
*/
protected function get_single_item( array $item ) {
$num_posts = wp_count_posts( $item['type'] );
$count = $num_posts->$item['status'];
$count = $num_posts->{$item['status']};

if ( ! $count ) {
return '';
}

$href = $this->get_link_url( $item );
$num = $this->maybe_link( number_format_i18n( $count ), $href );
$text = $this->maybe_link( $this->get_label( $item, $count ), $href );
$href = $this->get_link_url( $item );
$num = $this->maybe_link( number_format_i18n( $count ), $href );
$text = $this->maybe_link( $this->get_label( $item, $count ), $href );

return $this->get_markup( $num . '|' . $text, $item['type'] );
}

/**
* Wrap number and text within list item markup.
* Wrap number and text within table row markup.
*
* @since 1.0.0
*
* @param string $text Text to display. May be wrapped in a link.
* @param string $text Text to display. May be wrapped in a link.
* @param string $post_type Post type.
* @return string Markup for list item.
*/
protected function get_markup( $text, $post_type ) {
$text_parts = explode( '|', $text );

return '<tr>
<td class="first b ' . sanitize_html_class( 'b-' . $post_type ) . '">' . $text_parts[0] . '</td>
<td class="t ' . sanitize_html_class( $post_type ) . '">' . $text_parts[1] . '</td>
Expand Down

0 comments on commit 8c380a5

Please sign in to comment.