PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/class-yoast-plugin-conflict.php

https://gitlab.com/najomie/fit-hippie
PHP | 333 lines | 141 code | 52 blank | 140 comment | 18 complexity | 2ad1390865d1b404915fb62d4cc60513 MD5 | raw file
  1. <?php
  2. /**
  3. * @package WPSEO\Admin
  4. * @since 1.7.0
  5. */
  6. /**
  7. * Base class for handling plugin conflicts.
  8. */
  9. class Yoast_Plugin_Conflict {
  10. /**
  11. * The plugins must be grouped per section.
  12. *
  13. * It's possible to check for each section if there are conflicting plugin
  14. *
  15. * @var array
  16. */
  17. protected $plugins = array();
  18. /**
  19. * All the current active plugins will be stored in this private var
  20. *
  21. * @var array
  22. */
  23. protected $all_active_plugins = array();
  24. /**
  25. * After searching for active plugins that are in $this->plugins the active plugins will be stored in this
  26. * property
  27. *
  28. * @var array
  29. */
  30. protected $active_plugins = array();
  31. /**
  32. * Property for holding instance of itself
  33. *
  34. * @var Yoast_Plugin_Conflict
  35. */
  36. protected static $instance;
  37. /**
  38. * For the use of singleton pattern. Create instance of itself and return his instance
  39. *
  40. * @param string $class_name Give the classname to initialize. If classname is false (empty) it will use it's own __CLASS__.
  41. *
  42. * @return Yoast_Plugin_Conflict
  43. */
  44. public static function get_instance( $class_name = '' ) {
  45. if ( is_null( self::$instance ) ) {
  46. if ( ! is_string( $class_name ) || $class_name === '' ) {
  47. $class_name = __CLASS__;
  48. }
  49. self::$instance = new $class_name();
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * Setting instance, all active plugins and search for active plugins
  55. *
  56. * Protected constructor to prevent creating a new instance of the
  57. * *Singleton* via the `new` operator from outside of this class.
  58. */
  59. protected function __construct() {
  60. // Set active plugins.
  61. $this->all_active_plugins = get_option( 'active_plugins' );
  62. if ( filter_input( INPUT_GET, 'action' ) === 'deactivate' ) {
  63. $this->remove_deactivated_plugin();
  64. }
  65. // Search for active plugins.
  66. $this->search_active_plugins();
  67. }
  68. /**
  69. * Check if there are conflicting plugins for given $plugin_section
  70. *
  71. * @param string $plugin_section Type of plugin conflict (such as Open Graph or sitemap).
  72. *
  73. * @return bool
  74. */
  75. public function check_for_conflicts( $plugin_section ) {
  76. static $sections_checked;
  77. if ( $sections_checked === null ) {
  78. $sections_checked = array();
  79. }
  80. if ( ! in_array( $plugin_section, $sections_checked ) ) {
  81. $sections_checked[] = $plugin_section;
  82. $has_conflicts = ( ! empty( $this->active_plugins[ $plugin_section ] ) );
  83. return $has_conflicts;
  84. }
  85. return false;
  86. }
  87. /**
  88. * Getting all the conflicting plugins and return them as a string.
  89. *
  90. * This method will loop through all conflicting plugins to get the details of each plugin. The plugin name
  91. * will be taken from the details to parse a comma separated string, which can be use for by example a notice
  92. *
  93. * @param string $plugin_section Plugin conflict type (such as Open Graph or sitemap).
  94. *
  95. * @return string
  96. */
  97. public function get_conflicting_plugins_as_string( $plugin_section ) {
  98. if ( ! function_exists( 'get_plugin_data' ) ) {
  99. require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  100. }
  101. // Getting the active plugins by given section.
  102. $plugins = $this->active_plugins[ $plugin_section ];
  103. $plugin_names = array();
  104. foreach ( $plugins as $plugin ) {
  105. if ( $name = WPSEO_Utils::get_plugin_name( $plugin ) ) {
  106. $plugin_names[] = '<em>' . $name . '</em>';
  107. }
  108. }
  109. unset( $plugins, $plugin );
  110. if ( ! empty( $plugin_names ) ) {
  111. return implode( ' &amp; ', $plugin_names );
  112. }
  113. }
  114. /**
  115. * Checks for given $plugin_sections for conflicts
  116. *
  117. * @param array $plugin_sections Set of sections.
  118. */
  119. public function check_plugin_conflicts( $plugin_sections ) {
  120. foreach ( $plugin_sections as $plugin_section => $readable_plugin_section ) {
  121. // Check for conflicting plugins and show error if there are conflicts.
  122. if ( $this->check_for_conflicts( $plugin_section ) ) {
  123. $this->set_error( $plugin_section, $readable_plugin_section );
  124. }
  125. }
  126. // List of all active sections.
  127. $sections = array_keys( $plugin_sections );
  128. // List of all sections.
  129. $all_plugin_sections = array_keys( $this->plugins );
  130. /*
  131. * Get all sections that are inactive.
  132. * These plugins need to be cleared.
  133. *
  134. * This happens when Sitemaps or OpenGraph implementations toggle active/disabled.
  135. */
  136. $inactive_sections = array_diff( $all_plugin_sections, $sections );
  137. if ( ! empty( $inactive_sections ) ) {
  138. foreach ( $inactive_sections as $section ) {
  139. array_walk( $this->plugins[ $section ], array( $this, 'clear_error' ) );
  140. }
  141. }
  142. // For active sections clear errors for inactive plugins.
  143. foreach ( $sections as $section ) {
  144. // By default clear errors for all plugins of the section.
  145. $inactive_plugins = $this->plugins[ $section ];
  146. // If there are active plugins, filter them from being cleared.
  147. if ( isset( $this->active_plugins[ $section ] ) ) {
  148. $inactive_plugins = array_diff( $this->plugins[ $section ], $this->active_plugins[ $section ] );
  149. }
  150. array_walk( $inactive_plugins, array( $this, 'clear_error' ) );
  151. }
  152. }
  153. /**
  154. * Setting an error on the screen
  155. *
  156. * @param string $plugin_section Type of conflict group (such as Open Graph or sitemap).
  157. * @param string $readable_plugin_section This is the value for the translation.
  158. */
  159. protected function set_error( $plugin_section, $readable_plugin_section ) {
  160. $notification_center = Yoast_Notification_Center::get();
  161. foreach ( $this->active_plugins[ $plugin_section ] as $plugin_file ) {
  162. $plugin_name = WPSEO_Utils::get_plugin_name( $plugin_file );
  163. $error_message = '';
  164. /* translators: %1$s: 'Facebook & Open Graph' plugin name(s) of possibly conflicting plugin(s), %2$s to Yoast SEO */
  165. $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>';
  166. $error_message .= '<p>' . sprintf( $readable_plugin_section, 'Yoast SEO', $plugin_name ) . '</p>';
  167. /* translators: %s: 'Facebook' plugin name of possibly conflicting plugin */
  168. $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> ';
  169. $identifier = $this->get_notification_identifier( $plugin_file );
  170. // Add the message to the notifications center.
  171. $notification_center->add_notification(
  172. new Yoast_Notification(
  173. $error_message,
  174. array(
  175. 'type' => Yoast_Notification::ERROR,
  176. 'id' => 'wpseo-conflict-' . $identifier,
  177. )
  178. )
  179. );
  180. }
  181. }
  182. /**
  183. * Clear the notification for a plugin
  184. *
  185. * @param string $plugin_file Clear the optional notification for this plugin.
  186. */
  187. protected function clear_error( $plugin_file ) {
  188. $identifier = $this->get_notification_identifier( $plugin_file );
  189. $notification_center = Yoast_Notification_Center::get();
  190. $notification = $notification_center->get_notification_by_id( 'wpseo-conflict-' . $identifier );
  191. if ( $notification ) {
  192. $notification_center->remove_notification( $notification );
  193. }
  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 );
  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 ] ) ) {
  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 ) ) {
  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 );
  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. }