diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b74fb1e --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/class-gamajo-dashboard-glancer.php b/class-gamajo-dashboard-glancer.php index 4a0faf8..220fd1e 100644 --- a/class-gamajo-dashboard-glancer.php +++ b/class-gamajo-dashboard-glancer.php @@ -7,6 +7,7 @@ * @link http://gamajo.com/dashboard-glancer * @copyright 2013 Gary Jones, Gamajo Tech * @license GPL-2.0+ + * @version 1.0.1 */ /** @@ -16,7 +17,6 @@ * @author Gary Jones */ class Gamajo_Dashboard_Glancer { - /** * Hold all of the items to show. * @@ -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 */ @@ -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. @@ -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. ); } } @@ -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 ); } @@ -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 ] ); @@ -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'] ); } @@ -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'] . ')'; } @@ -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']; @@ -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 '' . $text . ''; } + return $text; } @@ -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 '
  • ' . $text . '
  • ' . "\n"; } - } diff --git a/class-gamajo-dashboard-rightnow.php b/class-gamajo-dashboard-rightnow.php index be4f78e..dc4a8ba 100644 --- a/class-gamajo-dashboard-rightnow.php +++ b/class-gamajo-dashboard-rightnow.php @@ -7,6 +7,7 @@ * @link http://gamajo.com/dashboard-glancer * @copyright 2013 Gary Jones, Gamajo Tech * @license GPL-2.0+ + * @version 1.0.1 */ /** @@ -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 */ @@ -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 ' ' . $text_parts[0] . ' ' . $text_parts[1] . '