class-suggested-plugins.php 5.4 KB

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