class-yoast-plugin-conflict.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. * @since 1.7.0
  7. */
  8. /**
  9. * Base class for handling plugin conflicts.
  10. */
  11. class Yoast_Plugin_Conflict {
  12. /**
  13. * The plugins must be grouped per section.
  14. *
  15. * It's possible to check for each section if there are conflicting plugins.
  16. *
  17. * @var array
  18. */
  19. protected $plugins = [];
  20. /**
  21. * All the current active plugins will be stored in this private var.
  22. *
  23. * @var array
  24. */
  25. protected $all_active_plugins = [];
  26. /**
  27. * After searching for active plugins that are in $this->plugins the active plugins will be stored in this
  28. * property.
  29. *
  30. * @var array
  31. */
  32. protected $active_plugins = [];
  33. /**
  34. * Property for holding instance of itself.
  35. *
  36. * @var Yoast_Plugin_Conflict
  37. */
  38. protected static $instance;
  39. /**
  40. * For the use of singleton pattern. Create instance of itself and return this instance.
  41. *
  42. * @param string $class_name Give the classname to initialize. If classname is
  43. * false (empty) it will use it's own __CLASS__.
  44. *
  45. * @return Yoast_Plugin_Conflict
  46. */
  47. public static function get_instance( $class_name = '' ) {
  48. if ( is_null( self::$instance ) ) {
  49. if ( ! is_string( $class_name ) || $class_name === '' ) {
  50. $class_name = __CLASS__;
  51. }
  52. self::$instance = new $class_name();
  53. }
  54. return self::$instance;
  55. }
  56. /**
  57. * Setting instance, all active plugins and search for active plugins.
  58. *
  59. * Protected constructor to prevent creating a new instance of the
  60. * *Singleton* via the `new` operator from outside of this class.
  61. */
  62. protected function __construct() {
  63. // Set active plugins.
  64. $this->all_active_plugins = get_option( 'active_plugins' );
  65. if ( filter_input( INPUT_GET, 'action' ) === 'deactivate' ) {
  66. $this->remove_deactivated_plugin();
  67. }
  68. // Search for active plugins.
  69. $this->search_active_plugins();
  70. }
  71. /**
  72. * Check if there are conflicting plugins for given $plugin_section.
  73. *
  74. * @param string $plugin_section Type of plugin conflict (such as Open Graph or sitemap).
  75. *
  76. * @return bool
  77. */
  78. public function check_for_conflicts( $plugin_section ) {
  79. static $sections_checked;
  80. if ( $sections_checked === null ) {
  81. $sections_checked = [];
  82. }
  83. if ( ! in_array( $plugin_section, $sections_checked, true ) ) {
  84. $sections_checked[] = $plugin_section;
  85. $has_conflicts = ( ! empty( $this->active_plugins[ $plugin_section ] ) );
  86. return $has_conflicts;
  87. }
  88. return false;
  89. }
  90. /**
  91. * Getting all the conflicting plugins and return them as a string.
  92. *
  93. * This method will loop through all conflicting plugins to get the details of each plugin. The plugin name
  94. * will be taken from the details to parse a comma separated string, which can be use for by example a notice
  95. *
  96. * @param string $plugin_section Plugin conflict type (such as Open Graph or sitemap).
  97. *
  98. * @return string
  99. */
  100. public function get_conflicting_plugins_as_string( $plugin_section ) {
  101. if ( ! function_exists( 'get_plugin_data' ) ) {
  102. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  103. }
  104. // Getting the active plugins by given section.
  105. $plugins = $this->active_plugins[ $plugin_section ];
  106. $plugin_names = [];
  107. foreach ( $plugins as $plugin ) {
  108. $name = WPSEO_Utils::get_plugin_name( $plugin );
  109. if ( ! empty( $name ) ) {
  110. $plugin_names[] = '<em>' . $name . '</em>';
  111. }
  112. }
  113. unset( $plugins, $plugin );
  114. if ( ! empty( $plugin_names ) ) {
  115. return implode( ' &amp; ', $plugin_names );
  116. }
  117. }
  118. /**
  119. * Checks for given $plugin_sections for conflicts.
  120. *
  121. * @param array $plugin_sections Set of sections.
  122. */
  123. public function check_plugin_conflicts( $plugin_sections ) {
  124. foreach ( $plugin_sections as $plugin_section => $readable_plugin_section ) {
  125. // Check for conflicting plugins and show error if there are conflicts.
  126. if ( $this->check_for_conflicts( $plugin_section ) ) {
  127. $this->set_error( $plugin_section, $readable_plugin_section );
  128. }
  129. }
  130. // List of all active sections.
  131. $sections = array_keys( $plugin_sections );
  132. // List of all sections.
  133. $all_plugin_sections = array_keys( $this->plugins );
  134. /*
  135. * Get all sections that are inactive.
  136. * These plugins need to be cleared.
  137. *
  138. * This happens when Sitemaps or OpenGraph implementations toggle active/disabled.
  139. */
  140. $inactive_sections = array_diff( $all_plugin_sections, $sections );
  141. if ( ! empty( $inactive_sections ) ) {
  142. foreach ( $inactive_sections as $section ) {
  143. array_walk( $this->plugins[ $section ], [ $this, 'clear_error' ] );
  144. }
  145. }
  146. // For active sections clear errors for inactive plugins.
  147. foreach ( $sections as $section ) {
  148. // By default clear errors for all plugins of the section.
  149. $inactive_plugins = $this->plugins[ $section ];
  150. // If there are active plugins, filter them from being cleared.
  151. if ( isset( $this->active_plugins[ $section ] ) ) {
  152. $inactive_plugins = array_diff( $this->plugins[ $section ], $this->active_plugins[ $section ] );
  153. }
  154. array_walk( $inactive_plugins, [ $this, 'clear_error' ] );
  155. }
  156. }
  157. /**
  158. * Setting an error on the screen.
  159. *
  160. * @param string $plugin_section Type of conflict group (such as Open Graph or sitemap).
  161. * @param string $readable_plugin_section This is the value for the translation.
  162. */
  163. protected function set_error( $plugin_section, $readable_plugin_section ) {
  164. $notification_center = Yoast_Notification_Center::get();
  165. foreach ( $this->active_plugins[ $plugin_section ] as $plugin_file ) {
  166. $plugin_name = WPSEO_Utils::get_plugin_name( $plugin_file );
  167. $error_message = '';
  168. /* translators: %1$s: 'Facebook & Open Graph' plugin name(s) of possibly conflicting plugin(s), %2$s to Yoast SEO */
  169. $error_message .= '<p>' . sprintf( __( 'The %1$s plugin might cause issues when used in conjunction with %2$s.', 'wordpress-seo' ), '<em>' . $plugin_name . '</em>', 'Yoast SEO' ) . '</p>';
  170. $error_message .= '<p>' . sprintf( $readable_plugin_section, 'Yoast SEO', $plugin_name ) . '</p>';
  171. /* translators: %s: 'Facebook' plugin name of possibly conflicting plugin */
  172. $error_message .= '<a class="button button-primary" href="' . wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $plugin_file . '&amp;plugin_status=all', 'deactivate-plugin_' . $plugin_file ) . '">' . sprintf( __( 'Deactivate %s', 'wordpress-seo' ), WPSEO_Utils::get_plugin_name( $plugin_file ) ) . '</a> ';
  173. $identifier = $this->get_notification_identifier( $plugin_file );
  174. // Add the message to the notifications center.
  175. $notification_center->add_notification(
  176. new Yoast_Notification(
  177. $error_message,
  178. [
  179. 'type' => Yoast_Notification::ERROR,
  180. 'id' => 'wpseo-conflict-' . $identifier,
  181. ]
  182. )
  183. );
  184. }
  185. }
  186. /**
  187. * Clear the notification for a plugin.
  188. *
  189. * @param string $plugin_file Clear the optional notification for this plugin.
  190. */
  191. public function clear_error( $plugin_file ) {
  192. $identifier = $this->get_notification_identifier( $plugin_file );
  193. $notification_center = Yoast_Notification_Center::get();
  194. $notification_center->remove_notification_by_id( 'wpseo-conflict-' . $identifier );
  195. }
  196. /**
  197. * Loop through the $this->plugins to check if one of the plugins is active.
  198. *
  199. * This method will store the active plugins in $this->active_plugins.
  200. */
  201. protected function search_active_plugins() {
  202. foreach ( $this->plugins as $plugin_section => $plugins ) {
  203. $this->check_plugins_active( $plugins, $plugin_section );
  204. }
  205. }
  206. /**
  207. * Loop through plugins and check if each plugin is active.
  208. *
  209. * @param array $plugins Set of plugins.
  210. * @param string $plugin_section Type of conflict group (such as Open Graph or sitemap).
  211. */
  212. protected function check_plugins_active( $plugins, $plugin_section ) {
  213. foreach ( $plugins as $plugin ) {
  214. if ( $this->check_plugin_is_active( $plugin ) ) {
  215. $this->add_active_plugin( $plugin_section, $plugin );
  216. }
  217. }
  218. }
  219. /**
  220. * Check if given plugin exists in array with all_active_plugins.
  221. *
  222. * @param string $plugin Plugin basename string.
  223. *
  224. * @return bool
  225. */
  226. protected function check_plugin_is_active( $plugin ) {
  227. return in_array( $plugin, $this->all_active_plugins, true );
  228. }
  229. /**
  230. * Add plugin to the list of active plugins.
  231. *
  232. * This method will check first if key $plugin_section exists, if not it will create an empty array
  233. * If $plugin itself doesn't exist it will be added.
  234. *
  235. * @param string $plugin_section Type of conflict group (such as Open Graph or sitemap).
  236. * @param string $plugin Plugin basename string.
  237. */
  238. protected function add_active_plugin( $plugin_section, $plugin ) {
  239. if ( ! array_key_exists( $plugin_section, $this->active_plugins ) ) {
  240. $this->active_plugins[ $plugin_section ] = [];
  241. }
  242. if ( ! in_array( $plugin, $this->active_plugins[ $plugin_section ], true ) ) {
  243. $this->active_plugins[ $plugin_section ][] = $plugin;
  244. }
  245. }
  246. /**
  247. * Search in $this->plugins for the given $plugin.
  248. *
  249. * If there is a result it will return the plugin category.
  250. *
  251. * @param string $plugin Plugin basename string.
  252. *
  253. * @return int|string
  254. */
  255. protected function find_plugin_category( $plugin ) {
  256. foreach ( $this->plugins as $plugin_section => $plugins ) {
  257. if ( in_array( $plugin, $plugins, true ) ) {
  258. return $plugin_section;
  259. }
  260. }
  261. }
  262. /**
  263. * When being in the deactivation process the currently deactivated plugin has to be removed.
  264. */
  265. private function remove_deactivated_plugin() {
  266. $deactivated_plugin = filter_input( INPUT_GET, 'plugin' );
  267. $key_to_remove = array_search( $deactivated_plugin, $this->all_active_plugins, true );
  268. if ( $key_to_remove !== false ) {
  269. unset( $this->all_active_plugins[ $key_to_remove ] );
  270. }
  271. }
  272. /**
  273. * Get the identifier from the plugin file.
  274. *
  275. * @param string $plugin_file Plugin file to get Identifier from.
  276. *
  277. * @return string
  278. */
  279. private function get_notification_identifier( $plugin_file ) {
  280. return md5( $plugin_file );
  281. }
  282. }