PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / presenters / admin / badge-presenter.php

badge-presenter.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.5, at src/presenters/admin/badge-presenter.php

105 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Presenters\Admin;
4
5 use Yoast\WP\SEO\Config\Badge_Group_Names;
6 use Yoast\WP\SEO\Presenters\Abstract_Presenter;
7
8 /**
9 * Represents the presenter class for "New" badges.
10 */
11 class Badge_Presenter extends Abstract_Presenter {
12
13 /**
14 * Identifier of the badge.
15 *
16 * @var string
17 */
18 private $id;
19
20 /**
21 * Optional link of the badge.
22 *
23 * @var string
24 */
25 private $link;
26
27 /**
28 * Optional group which the badge belongs to.
29 *
30 * Each group has a fixed period after which the group will no longer be considered new and the badges will disappear.
31 *
32 * @var string
33 */
34 private $group;
35
36 /**
37 * Optional object storing the group names and expiration versions.
38 *
39 * The group names set in Yoast SEO are used by default, but they can be overridden to use custom ones for an add-on.
40 *
41 * @var Badge_Group_Names
42 */
43 private $badge_group_names;
44
45 /**
46 * Badge_Presenter constructor.
47 *
48 * @param string $id Id of the badge.
49 * @param string $link Optional link of the badge.
50 * @param string $group Optional group which the badge belongs to.
51 * @param Badge_Group_Names|null $badge_group_names Optional object storing the group names.
52 */
53 public function __construct( $id, $link = '', $group = '', $badge_group_names = null ) {
54 $this->id = $id;
55 $this->link = $link;
56 $this->group = $group;
57
58 if ( ! $badge_group_names instanceof Badge_Group_Names ) {
59 $badge_group_names = new Badge_Group_Names();
60 }
61 $this->badge_group_names = $badge_group_names;
62 }
63
64 /**
65 * Presents the New Badge. If a link has been passed, the badge is presented with the link.
66 * Otherwise a static badge is presented.
67 *
68 * @return string The styled New Badge.
69 */
70 public function present() {
71 if ( ! $this->is_group_still_new() ) {
72 return '';
73 }
74
75 if ( $this->link !== '' ) {
76 return \sprintf(
77 '<a class="yoast-badge yoast-badge__is-link yoast-new-badge" id="%1$s-new-badge" href="%2$s">%3$s</a>',
78 \esc_attr( $this->id ),
79 \esc_url( $this->link ),
80 \esc_html__( 'New', 'wordpress-seo' ),
81 );
82 }
83
84 return \sprintf(
85 '<span class="yoast-badge yoast-new-badge" id="%1$s-new-badge">%2$s</span>',
86 \esc_attr( $this->id ),
87 \esc_html__( 'New', 'wordpress-seo' ),
88 );
89 }
90
91 /**
92 * Check whether the new badge should be shown according to the group it is in.
93 *
94 * @return bool True if still new.
95 */
96 public function is_group_still_new() {
97 // If there's no group configured, the new badge is always active.
98 if ( ! $this->group ) {
99 return true;
100 }
101
102 return $this->badge_group_names->is_still_eligible_for_new_badge( $this->group );
103 }
104 }
105