| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Alerts\Application\Indexables_Disabled; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 7 |
use Yoast\WP\SEO\Helpers\Indexable_Helper; |
| 8 |
use Yoast\WP\SEO\Helpers\Short_Link_Helper; |
| 9 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 10 |
use Yoast_Notification; |
| 11 |
use Yoast_Notification_Center; |
| 12 |
|
| 13 |
/** |
| 14 |
* Indexables_Disabled_Alert class. |
| 15 |
*/ |
| 16 |
class Indexables_Disabled_Alert implements Integration_Interface { |
| 17 |
|
| 18 |
public const NOTIFICATION_ID = 'wpseo-indexables-disabled'; |
| 19 |
|
| 20 |
/** |
| 21 |
* The notifications center. |
| 22 |
* |
| 23 |
* @var Yoast_Notification_Center |
| 24 |
*/ |
| 25 |
private $notification_center; |
| 26 |
|
| 27 |
/** |
| 28 |
* The indexable helper. |
| 29 |
* |
| 30 |
* @var Indexable_Helper |
| 31 |
*/ |
| 32 |
private $indexable_helper; |
| 33 |
|
| 34 |
/** |
| 35 |
* The short link helper. |
| 36 |
* |
| 37 |
* @var Short_Link_Helper |
| 38 |
*/ |
| 39 |
private $short_link_helper; |
| 40 |
|
| 41 |
/** |
| 42 |
* Indexables_Disabled_Alert constructor. |
| 43 |
* |
| 44 |
* @param Yoast_Notification_Center $notification_center The notification center. |
| 45 |
* @param Indexable_Helper $indexable_helper The indexable helper. |
| 46 |
* @param Short_Link_Helper $short_link_helper The short link helper. |
| 47 |
*/ |
| 48 |
public function __construct( |
| 49 |
Yoast_Notification_Center $notification_center, |
| 50 |
Indexable_Helper $indexable_helper, |
| 51 |
Short_Link_Helper $short_link_helper |
| 52 |
) { |
| 53 |
$this->notification_center = $notification_center; |
| 54 |
$this->indexable_helper = $indexable_helper; |
| 55 |
$this->short_link_helper = $short_link_helper; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the conditionals based on which this loadable should be active. |
| 60 |
* |
| 61 |
* @return array<string> |
| 62 |
*/ |
| 63 |
public static function get_conditionals() { |
| 64 |
return [ Admin_Conditional::class ]; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Initializes the integration. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function register_hooks() { |
| 73 |
\add_action( 'admin_init', [ $this, 'add_notifications' ] ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Adds or removes notification based on whether indexables are disabled. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function add_notifications() { |
| 82 |
if ( $this->indexable_helper->should_index_indexables() ) { |
| 83 |
$this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID ); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$notification = $this->get_indexables_disabled_notification(); |
| 88 |
|
| 89 |
$this->notification_center->add_notification( $notification ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Builds the indexables-disabled notification. |
| 94 |
* |
| 95 |
* @return Yoast_Notification The indexables-disabled notification. |
| 96 |
*/ |
| 97 |
private function get_indexables_disabled_notification(): Yoast_Notification { |
| 98 |
$message = $this->get_message(); |
| 99 |
|
| 100 |
return new Yoast_Notification( |
| 101 |
$message, |
| 102 |
[ |
| 103 |
'id' => self::NOTIFICATION_ID, |
| 104 |
'type' => Yoast_Notification::WARNING, |
| 105 |
'capabilities' => [ 'wpseo_manage_options' ], |
| 106 |
], |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns the notification message as an HTML string. |
| 112 |
* |
| 113 |
* @return string The HTML string representation of the notification. |
| 114 |
*/ |
| 115 |
private function get_message(): string { |
| 116 |
$shortlink = $this->short_link_helper->get( 'https://yoa.st/indexables-disabled' ); |
| 117 |
|
| 118 |
$message = \sprintf( |
| 119 |
/* translators: %1$s expands to "Yoast", %2$s expands to an opening anchor tag, %3$s expands to a closing anchor tag. */ |
| 120 |
\esc_html__( '%1$s indexables are disabled because your site is in a non-production environment or custom code is blocking them. This may affect your SEO features. %2$sLearn more about this%3$s.', 'wordpress-seo' ), |
| 121 |
'Yoast', |
| 122 |
'<a href="' . \esc_url( $shortlink ) . '" target="_blank">', |
| 123 |
'</a>', |
| 124 |
); |
| 125 |
|
| 126 |
return $message; |
| 127 |
} |
| 128 |
} |
| 129 |
|