| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Suggested_Plugins |
| 6 |
*/ |
| 7 |
|
| 8 |
use Yoast\WP\SEO\Conditionals\Conditional; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class WPSEO_Suggested_Plugins |
| 12 |
*/ |
| 13 |
class WPSEO_Suggested_Plugins implements WPSEO_WordPress_Integration { |
| 14 |
|
| 15 |
/** |
| 16 |
* Holds the availability checker. |
| 17 |
* |
| 18 |
* @var WPSEO_Plugin_Availability |
| 19 |
*/ |
| 20 |
protected $availability_checker; |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the notification center. |
| 24 |
* |
| 25 |
* @var Yoast_Notification_Center |
| 26 |
*/ |
| 27 |
protected $notification_center; |
| 28 |
|
| 29 |
/** |
| 30 |
* WPSEO_Suggested_Plugins constructor. |
| 31 |
* |
| 32 |
* @param WPSEO_Plugin_Availability $availability_checker The availability checker to use. |
| 33 |
* @param Yoast_Notification_Center $notification_center The notification center to add notifications to. |
| 34 |
*/ |
| 35 |
public function __construct( WPSEO_Plugin_Availability $availability_checker, Yoast_Notification_Center $notification_center ) { |
| 36 |
$this->availability_checker = $availability_checker; |
| 37 |
$this->notification_center = $notification_center; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Registers all hooks to WordPress. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function register_hooks() { |
| 46 |
add_action( 'admin_init', [ $this->availability_checker, 'register' ] ); |
| 47 |
add_action( 'admin_init', [ $this, 'add_notifications' ] ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Adds notifications (when necessary). |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function add_notifications() { |
| 56 |
$checker = $this->availability_checker; |
| 57 |
|
| 58 |
// Get all Yoast plugins that have dependencies. |
| 59 |
$plugins = $checker->get_plugins_with_dependencies(); |
| 60 |
|
| 61 |
foreach ( $plugins as $plugin_name => $plugin ) { |
| 62 |
$notification_id = 'wpseo-suggested-plugin-' . $plugin_name; |
| 63 |
|
| 64 |
if ( ! $checker->dependencies_are_satisfied( $plugin ) ) { |
| 65 |
$this->notification_center->remove_notification_by_id( $notification_id ); |
| 66 |
|
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! $checker->is_installed( $plugin ) ) { |
| 71 |
$notification = $this->get_yoast_seo_suggested_plugins_notification( $notification_id, $plugin ); |
| 72 |
$this->notification_center->add_notification( $notification ); |
| 73 |
|
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
$this->notification_center->remove_notification_by_id( $notification_id ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Build Yoast SEO suggested plugins notification. |
| 83 |
* |
| 84 |
* @param string $notification_id The id of the notification to be created. |
| 85 |
* @param array<string, string|bool|array<string, Conditional>> $plugin The plugin to retrieve the data from. |
| 86 |
* |
| 87 |
* @return Yoast_Notification The notification containing the suggested plugin. |
| 88 |
*/ |
| 89 |
protected function get_yoast_seo_suggested_plugins_notification( $notification_id, $plugin ) { |
| 90 |
$message = $this->create_install_suggested_plugin_message( $plugin ); |
| 91 |
|
| 92 |
return new Yoast_Notification( |
| 93 |
$message, |
| 94 |
[ |
| 95 |
'id' => $notification_id, |
| 96 |
'type' => Yoast_Notification::WARNING, |
| 97 |
'capabilities' => [ 'install_plugins' ], |
| 98 |
], |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Creates a message to suggest the installation of a particular plugin. |
| 104 |
* |
| 105 |
* @param array $suggested_plugin The suggested plugin. |
| 106 |
* |
| 107 |
* @return string The install suggested plugin message. |
| 108 |
*/ |
| 109 |
protected function create_install_suggested_plugin_message( $suggested_plugin ) { |
| 110 |
/* translators: %1$s expands to an opening strong tag, %2$s expands to the dependency name, %3$s expands to a closing strong tag, %4$s expands to an opening anchor tag, %5$s expands to a closing anchor tag. */ |
| 111 |
$message = __( 'It looks like you aren\'t using our %1$s%2$s addon%3$s. %4$sUpgrade today%5$s to unlock more tools and SEO features to make your products stand out in search results.', 'wordpress-seo' ); |
| 112 |
$install_link = WPSEO_Admin_Utils::get_install_link( $suggested_plugin ); |
| 113 |
|
| 114 |
return sprintf( |
| 115 |
$message, |
| 116 |
'<strong>', |
| 117 |
$install_link, |
| 118 |
'</strong>', |
| 119 |
$this->create_more_information_link( $suggested_plugin['url'], $suggested_plugin['title'] ), |
| 120 |
'</a>', |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Creates a more information link that directs the user to WordPress.org Plugin repository. |
| 126 |
* |
| 127 |
* @param string $url The URL to the plugin's page. |
| 128 |
* @param string $name The name of the plugin. |
| 129 |
* |
| 130 |
* @return string The more information link. |
| 131 |
*/ |
| 132 |
protected function create_more_information_link( $url, $name ) { |
| 133 |
return sprintf( |
| 134 |
'<a href="%s" aria-label="%s" target="_blank" rel="noopener noreferrer">', |
| 135 |
$url, |
| 136 |
/* translators: Hidden accessibility text; %1$s expands to the dependency name */ |
| 137 |
sprintf( __( 'More information about %1$s', 'wordpress-seo' ), $name ), |
| 138 |
); |
| 139 |
} |
| 140 |
} |
| 141 |
|