class-suggested-plugins.php 5.5 KB

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