class-yoast-plugin-conflict.php 9.8 KB

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