/wp-content/plugins/wordpress-seo/inc/class-upgrade.php

https://gitlab.com/iamgraeme/royalmile · PHP · 208 lines · 104 code · 37 blank · 67 comment · 14 complexity · c80ba9f3b59aa199690bf74767efe991 MD5 · raw file

  1. <?php
  2. /**
  3. * @package WPSEO
  4. * @subpackage Internal
  5. */
  6. /**
  7. * This code handles the option upgrades
  8. */
  9. class WPSEO_Upgrade {
  10. /**
  11. * Holds the Yoast SEO options
  12. *
  13. * @var array
  14. */
  15. private $options = array();
  16. /**
  17. * Class constructor
  18. */
  19. public function __construct() {
  20. $this->options = WPSEO_Options::get_option( 'wpseo' );
  21. WPSEO_Options::maybe_set_multisite_defaults( false );
  22. if ( version_compare( $this->options['version'], '1.5.0', '<' ) ) {
  23. $this->upgrade_15( $this->options['version'] );
  24. }
  25. if ( version_compare( $this->options['version'], '2.0', '<' ) ) {
  26. $this->upgrade_20();
  27. }
  28. if ( version_compare( $this->options['version'], '2.1', '<' ) ) {
  29. $this->upgrade_21();
  30. }
  31. if ( version_compare( $this->options['version'], '2.2', '<' ) ) {
  32. $this->upgrade_22();
  33. }
  34. if ( version_compare( $this->options['version'], '2.3', '<' ) ) {
  35. $this->upgrade_23();
  36. }
  37. if ( version_compare( $this->options['version'], '3.0', '<' ) ) {
  38. $this->upgrade_30();
  39. }
  40. if ( version_compare( $this->options['version'], '3.3', '<' ) ) {
  41. $this->upgrade_33();
  42. }
  43. /**
  44. * Filter: 'wpseo_run_upgrade' - Runs the upgrade hook which are dependent on Yoast SEO
  45. *
  46. * @deprecated Since 3.1
  47. *
  48. * @api string - The current version of Yoast SEO
  49. */
  50. do_action( 'wpseo_run_upgrade', $this->options['version'] );
  51. $this->finish_up();
  52. }
  53. /**
  54. * Run the Yoast SEO 1.5 upgrade routine
  55. *
  56. * @param string $version Current plugin version.
  57. */
  58. private function upgrade_15( $version ) {
  59. // Clean up options and meta.
  60. WPSEO_Options::clean_up( null, $version );
  61. WPSEO_Meta::clean_up();
  62. // Add new capabilities on upgrade.
  63. wpseo_add_capabilities();
  64. }
  65. /**
  66. * Moves options that moved position in WPSEO 2.0
  67. */
  68. private function upgrade_20() {
  69. /**
  70. * Clean up stray wpseo_ms options from the options table, option should only exist in the sitemeta table.
  71. * This could have been caused in many version of Yoast SEO, so deleting it for everything below 2.0
  72. */
  73. delete_option( 'wpseo_ms' );
  74. $this->move_pinterest_option();
  75. }
  76. /**
  77. * Detects if taxonomy terms were split and updates the corresponding taxonomy meta's accordingly.
  78. */
  79. private function upgrade_21() {
  80. $taxonomies = get_option( 'wpseo_taxonomy_meta', array() );
  81. if ( ! empty( $taxonomies ) ) {
  82. foreach ( $taxonomies as $taxonomy => $tax_metas ) {
  83. foreach ( $tax_metas as $term_id => $tax_meta ) {
  84. if ( function_exists( 'wp_get_split_term' ) && $new_term_id = wp_get_split_term( $term_id, $taxonomy ) ) {
  85. $taxonomies[ $taxonomy ][ $new_term_id ] = $taxonomies[ $taxonomy ][ $term_id ];
  86. unset( $taxonomies[ $taxonomy ][ $term_id ] );
  87. }
  88. }
  89. }
  90. update_option( 'wpseo_taxonomy_meta', $taxonomies );
  91. }
  92. }
  93. /**
  94. * Performs upgrade functions to Yoast SEO 2.2
  95. */
  96. private function upgrade_22() {
  97. // Unschedule our tracking.
  98. wp_clear_scheduled_hook( 'yoast_tracking' );
  99. // Clear the tracking settings, the seen about setting and the ignore tour setting.
  100. $options = get_option( 'wpseo' );
  101. unset( $options['tracking_popup_done'], $options['yoast_tracking'], $options['seen_about'], $options['ignore_tour'] );
  102. update_option( 'wpseo', $options );
  103. }
  104. /**
  105. * Schedules upgrade function to Yoast SEO 2.3
  106. */
  107. private function upgrade_23() {
  108. add_action( 'wp', array( $this, 'upgrade_23_query' ), 90 );
  109. add_action( 'admin_head', array( $this, 'upgrade_23_query' ), 90 );
  110. }
  111. /**
  112. * Performs upgrade query to Yoast SEO 2.3
  113. */
  114. public function upgrade_23_query() {
  115. $wp_query = new WP_Query( 'post_type=any&meta_key=_yoast_wpseo_sitemap-include&meta_value=never&order=ASC' );
  116. if ( ! empty( $wp_query->posts ) ) {
  117. $options = get_option( 'wpseo_xml' );
  118. $excluded_posts = array();
  119. if ( $options['excluded-posts'] !== '' ) {
  120. $excluded_posts = explode( ',', $options['excluded-posts'] );
  121. }
  122. foreach ( $wp_query->posts as $post ) {
  123. if ( ! in_array( $post->ID, $excluded_posts ) ) {
  124. $excluded_posts[] = $post->ID;
  125. }
  126. }
  127. // Updates the meta value.
  128. $options['excluded-posts'] = implode( ',', $excluded_posts );
  129. // Update the option.
  130. update_option( 'wpseo_xml', $options );
  131. }
  132. // Remove the meta fields.
  133. delete_post_meta_by_key( '_yoast_wpseo_sitemap-include' );
  134. }
  135. /**
  136. * Performs upgrade functions to Yoast SEO 3.0
  137. */
  138. private function upgrade_30() {
  139. // Remove the meta fields for sitemap prio.
  140. delete_post_meta_by_key( '_yoast_wpseo_sitemap-prio' );
  141. }
  142. /**
  143. * Performs upgrade functions to Yoast SEO 3.3
  144. */
  145. private function upgrade_33() {
  146. // Notification dismissals have been moved to User Meta instead of global option.
  147. delete_option( Yoast_Notification_Center::STORAGE_KEY );
  148. }
  149. /**
  150. * Move the pinterest verification option from the wpseo option to the wpseo_social option
  151. */
  152. private function move_pinterest_option() {
  153. $options_social = get_option( 'wpseo_social' );
  154. if ( isset( $option_wpseo['pinterestverify'] ) ) {
  155. $options_social['pinterestverify'] = $option_wpseo['pinterestverify'];
  156. unset( $option_wpseo['pinterestverify'] );
  157. update_option( 'wpseo_social', $options_social );
  158. update_option( 'wpseo', $option_wpseo );
  159. }
  160. }
  161. /**
  162. * Runs the needed cleanup after an update, setting the DB version to latest version, flushing caches etc.
  163. */
  164. private function finish_up() {
  165. $this->options = WPSEO_Options::get_option( 'wpseo' ); // Re-get to make sure we have the latest version.
  166. update_option( 'wpseo', $this->options ); // This also ensures the DB version is equal to WPSEO_VERSION.
  167. add_action( 'shutdown', 'flush_rewrite_rules' ); // Just flush rewrites, always, to at least make them work after an upgrade.
  168. WPSEO_Sitemaps_Cache::clear(); // Flush the sitemap cache.
  169. WPSEO_Options::ensure_options_exist(); // Make sure all our options always exist - issue #1245.
  170. }
  171. }