diff --git a/classes/badges/class-badge.php b/classes/badges/class-badge.php index df8179034..62c4f7e13 100644 --- a/classes/badges/class-badge.php +++ b/classes/badges/class-badge.php @@ -46,6 +46,15 @@ public function register_badge() { ); } + /** + * Get the badge ID. + * + * @return string + */ + public function get_id() { + return $this->id; + } + /** * Get the badge name. * diff --git a/classes/class-badges.php b/classes/class-badges.php index 65fd2a8e5..5416917be 100644 --- a/classes/class-badges.php +++ b/classes/class-badges.php @@ -30,6 +30,9 @@ class Badges { * @return void */ public static function register_badge( $badge_id, $args ) { + if ( ! isset( $args['id'] ) ) { + $args['id'] = $badge_id; + } self::$badges[ $badge_id ] = $args; } diff --git a/classes/widgets/class-badge-content.php b/classes/widgets/class-badge-content.php index a72f89d57..b22932e06 100644 --- a/classes/widgets/class-badge-content.php +++ b/classes/widgets/class-badge-content.php @@ -21,6 +21,16 @@ final class Badge_Content extends Widget { */ protected $id = 'badge-content'; + /** + * Whether we should render the widget or not. + * + * @return bool + */ + protected function should_render() { + $details = $this->get_badge_details(); + return ( 100 > (int) $details['progress']['progress'] ); + } + /** * Render the widget content. * @@ -78,7 +88,10 @@ class="prpl-badge-gauge" * @return array */ public function get_badge_details() { - $result = []; + static $result = []; + if ( ! empty( $result ) ) { + return $result; + } $badges = [ 'wonderful-writer', 'bold-blogger', 'awesome-author' ]; // Get the badge to display. diff --git a/classes/widgets/class-badge-streak.php b/classes/widgets/class-badge-streak.php index f2ac3dd1f..41d8820c3 100644 --- a/classes/widgets/class-badge-streak.php +++ b/classes/widgets/class-badge-streak.php @@ -22,6 +22,16 @@ final class Badge_Streak extends Widget { */ protected $id = 'badge-streak'; + /** + * Whether we should render the widget or not. + * + * @return bool + */ + protected function should_render() { + $details = $this->get_badge_details(); + return ( 100 > (int) $details['progress']['progress'] ); + } + /** * Render the widget content. * @@ -79,7 +89,10 @@ class="prpl-badge-gauge" * @return array */ public function get_badge_details() { - $result = []; + static $result = []; + if ( ! empty( $result ) ) { + return $result; + } $badges = [ 'progress-padawan', 'maintenance-maniac', diff --git a/classes/widgets/class-widget.php b/classes/widgets/class-widget.php index 628584837..aa5781df1 100644 --- a/classes/widgets/class-widget.php +++ b/classes/widgets/class-widget.php @@ -58,11 +58,23 @@ public function get_frequency() { * @return void */ protected function render() { + if ( ! $this->should_render() ) { + return; + } echo '
'; } + /** + * Whether we should render the widget or not. + * + * @return bool + */ + protected function should_render() { + return true; + } + /** * Render a big counter. *