| 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 |
|