From 64f88574738fc870172e3bda7306024d62529323 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 4 Jul 2024 08:58:30 +0300 Subject: [PATCH 1/3] Skip rendering badges that are complete --- classes/badges/class-badge.php | 9 +++++++++ classes/class-badges.php | 3 +++ classes/widgets/class-badge-content.php | 21 ++++++++++++++++++++- classes/widgets/class-badge-streak.php | 21 ++++++++++++++++++++- classes/widgets/class-widget.php | 12 ++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/classes/badges/class-badge.php b/classes/badges/class-badge.php index df8179034..e48be1339 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..c26594855 100644 --- a/classes/widgets/class-badge-content.php +++ b/classes/widgets/class-badge-content.php @@ -21,6 +21,22 @@ 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(); + if ( + 'awesome-author' === $details['badge']['id'] + && 100 === (int) $details['progress']['progress'] + ) { + return false; + } + return true; + } + /** * Render the widget content. * @@ -78,7 +94,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..be78c9bfb 100644 --- a/classes/widgets/class-badge-streak.php +++ b/classes/widgets/class-badge-streak.php @@ -22,6 +22,22 @@ 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(); + if ( + 'super-site-specialist' === $details['badge']['id'] + && 100 === (int) $details['progress']['progress'] + ) { + return false; + } + return true; + } + /** * Render the widget content. * @@ -79,7 +95,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..1e0aa249f 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 '
'; $this->the_content(); echo '
'; } + /** + * Whether we should render the widget or not. + * + * @return bool + */ + protected function should_render() { + return true; + } + /** * Render a big counter. * From fdd04d3d7bf064fe7e3b8a8f6e311839622d4b47 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 4 Jul 2024 09:27:07 +0300 Subject: [PATCH 2/3] CS fixes --- classes/badges/class-badge.php | 2 +- classes/widgets/class-badge-content.php | 2 +- classes/widgets/class-badge-streak.php | 6 +++--- classes/widgets/class-widget.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/classes/badges/class-badge.php b/classes/badges/class-badge.php index e48be1339..62c4f7e13 100644 --- a/classes/badges/class-badge.php +++ b/classes/badges/class-badge.php @@ -48,7 +48,7 @@ public function register_badge() { /** * Get the badge ID. - * + * * @return string */ public function get_id() { diff --git a/classes/widgets/class-badge-content.php b/classes/widgets/class-badge-content.php index c26594855..90ca1a6a9 100644 --- a/classes/widgets/class-badge-content.php +++ b/classes/widgets/class-badge-content.php @@ -23,7 +23,7 @@ final class Badge_Content extends Widget { /** * Whether we should render the widget or not. - * + * * @return bool */ protected function should_render() { diff --git a/classes/widgets/class-badge-streak.php b/classes/widgets/class-badge-streak.php index be78c9bfb..60d1d8db4 100644 --- a/classes/widgets/class-badge-streak.php +++ b/classes/widgets/class-badge-streak.php @@ -22,9 +22,9 @@ final class Badge_Streak extends Widget { */ protected $id = 'badge-streak'; -/** + /** * Whether we should render the widget or not. - * + * * @return bool */ protected function should_render() { @@ -37,7 +37,7 @@ protected function should_render() { } return true; } - + /** * Render the widget content. * diff --git a/classes/widgets/class-widget.php b/classes/widgets/class-widget.php index 1e0aa249f..aa5781df1 100644 --- a/classes/widgets/class-widget.php +++ b/classes/widgets/class-widget.php @@ -68,7 +68,7 @@ protected function render() { /** * Whether we should render the widget or not. - * + * * @return bool */ protected function should_render() { From 36cbae88e02c0f8192b0864b40960fcc60452f0f Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 4 Jul 2024 11:06:09 +0300 Subject: [PATCH 3/3] Simplify check --- classes/widgets/class-badge-content.php | 8 +------- classes/widgets/class-badge-streak.php | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/classes/widgets/class-badge-content.php b/classes/widgets/class-badge-content.php index 90ca1a6a9..b22932e06 100644 --- a/classes/widgets/class-badge-content.php +++ b/classes/widgets/class-badge-content.php @@ -28,13 +28,7 @@ final class Badge_Content extends Widget { */ protected function should_render() { $details = $this->get_badge_details(); - if ( - 'awesome-author' === $details['badge']['id'] - && 100 === (int) $details['progress']['progress'] - ) { - return false; - } - return true; + return ( 100 > (int) $details['progress']['progress'] ); } /** diff --git a/classes/widgets/class-badge-streak.php b/classes/widgets/class-badge-streak.php index 60d1d8db4..41d8820c3 100644 --- a/classes/widgets/class-badge-streak.php +++ b/classes/widgets/class-badge-streak.php @@ -29,13 +29,7 @@ final class Badge_Streak extends Widget { */ protected function should_render() { $details = $this->get_badge_details(); - if ( - 'super-site-specialist' === $details['badge']['id'] - && 100 === (int) $details['progress']['progress'] - ) { - return false; - } - return true; + return ( 100 > (int) $details['progress']['progress'] ); } /**