xml-sitemap-feed
Last commit date
assets
1 month ago
inc
1 month ago
views
1 month ago
LICENSE
3 years ago
readme.txt
1 month ago
uninstall.php
1 month ago
upgrade.php
1 month ago
xml-sitemap.php
1 month ago
upgrade.php
229 lines
| 1 | <?php |
| 2 | /** |
| 3 | * XML Sitemap Feed upgrade routines |
| 4 | * |
| 5 | * @package XML Sitemap & Google News |
| 6 | * |
| 7 | * @since 5.1 |
| 8 | */ |
| 9 | |
| 10 | defined( 'WPINC' ) || die; |
| 11 | |
| 12 | if ( $db_version ) { |
| 13 | xmlsf_upgrade( $db_version ); |
| 14 | } else { |
| 15 | xmlsf_install(); |
| 16 | } |
| 17 | |
| 18 | update_option( 'xmlsf_version', XMLSF_VERSION ); |
| 19 | |
| 20 | add_action( 'init', 'flush_rewrite_rules', 11 ); |
| 21 | |
| 22 | /** |
| 23 | * Update from defaults. |
| 24 | * |
| 25 | * @since 5.1 |
| 26 | * |
| 27 | * @param bool $update Wether to add or update options. |
| 28 | */ |
| 29 | function xmlsf_update_from_defaults( $update = true ) { |
| 30 | // Options that need not be autoloaded. |
| 31 | $not_autoload = array( 'robots' ); |
| 32 | $defaults = (array) XMLSF\get_default_settings(); |
| 33 | |
| 34 | foreach ( $defaults as $option => $default ) { |
| 35 | if ( $update ) { |
| 36 | update_option( 'xmlsf_' . $option, $default, '', ! in_array( $option, $not_autoload, true ) ); |
| 37 | } else { |
| 38 | add_option( 'xmlsf_' . $option, $default, '', ! in_array( $option, $not_autoload, true ) ); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Set up default plugin data. |
| 45 | * |
| 46 | * @since 5.1 |
| 47 | */ |
| 48 | function xmlsf_install() { |
| 49 | // Add or update all defaults. |
| 50 | xmlsf_update_from_defaults(); |
| 51 | |
| 52 | do_action( 'xmlsf_install' ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Upgrade plugin data. |
| 57 | * |
| 58 | * @param int $db_version Database version. |
| 59 | * @since 5.1 |
| 60 | */ |
| 61 | function xmlsf_upgrade( $db_version ) { |
| 62 | if ( version_compare( '4.4', $db_version, '>' ) ) { |
| 63 | // Remove robots.txt rules blocking stylesheets. |
| 64 | $robot_rules = get_option( 'xmlsf_robots' ); |
| 65 | if ( ! empty( $robot_rules ) ) { |
| 66 | $robot_rules = str_replace( array( 'Disallow: */wp-content/', 'Allow: */wp-content/uploads/' ), '', $robot_rules ); |
| 67 | delete_option( 'xmlsf_robots' ); |
| 68 | add_option( 'xmlsf_robots', $robot_rules, '', false ); |
| 69 | } |
| 70 | |
| 71 | // Make sure custom sitemaps is an array. |
| 72 | $urls = get_option( 'xmlsf_custom_sitemaps' ); |
| 73 | if ( ! is_array( $urls ) ) { |
| 74 | $urls = explode( PHP_EOL, $urls ); |
| 75 | update_option( 'xmlsf_custom_sitemaps', $urls ); |
| 76 | } |
| 77 | |
| 78 | // Register location taxonomies then delete all terms. |
| 79 | $taxonomies = array( 'gn-location-3', 'gn-location-2', 'gn-location-1' ); |
| 80 | foreach ( $taxonomies as $taxonomy ) { |
| 81 | register_taxonomy( $taxonomy, null ); |
| 82 | $terms = get_terms( |
| 83 | array( |
| 84 | 'taxonomy' => $taxonomy, |
| 85 | 'hide_empty' => false, |
| 86 | ) |
| 87 | ); |
| 88 | foreach ( (array) $terms as $term ) { |
| 89 | wp_delete_term( $term->term_id, 'gn-location-3' ); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if ( version_compare( '5.0.1', $db_version, '>' ) ) { |
| 95 | // Delete all taxonomy terms. |
| 96 | register_taxonomy( 'gn-genre', null ); |
| 97 | |
| 98 | $terms = get_terms( |
| 99 | array( |
| 100 | 'taxonomy' => 'gn-genre', |
| 101 | 'hide_empty' => false, |
| 102 | ) |
| 103 | ); |
| 104 | |
| 105 | foreach ( (array) $terms as $term ) { |
| 106 | wp_delete_term( $term->term_id, 'gn-genre' ); |
| 107 | } |
| 108 | |
| 109 | // New taxonomy settings. |
| 110 | $taxonomies = get_option( 'xmlsf_taxonomies' ); |
| 111 | if ( empty( $taxonomies ) ) { |
| 112 | $active = ''; |
| 113 | } else { |
| 114 | $available = 0; |
| 115 | $checked = count( $taxonomies ); |
| 116 | foreach ( (array) get_option( 'xmlsf_post_types' ) as $post_type => $settings ) { |
| 117 | if ( empty( $settings['active'] ) ) { |
| 118 | continue; |
| 119 | } |
| 120 | $taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 121 | // Check each tax public flag and term count and append name to array. |
| 122 | foreach ( $taxonomies as $taxonomy ) { |
| 123 | if ( ! empty( $taxonomy->public ) && ! in_array( $taxonomy->name, xmlsf()->disabled_taxonomies(), true ) ) { |
| 124 | ++$available; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | if ( $checked === $available ) { |
| 129 | update_option( 'xmlsf_taxonomies', '' ); |
| 130 | } |
| 131 | $active = '1'; |
| 132 | } |
| 133 | |
| 134 | $taxonomy_settings = array( |
| 135 | 'active' => $active, |
| 136 | 'priority' => '0.3', |
| 137 | 'dynamic_priority' => '1', |
| 138 | 'limit' => '5000', |
| 139 | ); |
| 140 | add_option( 'xmlsf_taxonomy_settings', $taxonomy_settings ); |
| 141 | |
| 142 | // Make sure no pong option remains. |
| 143 | delete_option( 'xmlsf_pong' ); |
| 144 | |
| 145 | // Update or create robots option. |
| 146 | $robots = get_option( 'xmlsf_robots', '' ); |
| 147 | delete_option( 'xmlsf_robots' ); |
| 148 | add_option( 'xmlsf_robots', $robots, '', false ); |
| 149 | } |
| 150 | |
| 151 | if ( version_compare( '5.4', $db_version, '>' ) ) { |
| 152 | // Delete old transients. |
| 153 | delete_transient( 'xmlsf_ping_google_sitemap_news' ); |
| 154 | delete_transient( 'xmlsf_ping_google_sitemap' ); |
| 155 | delete_transient( 'xmlsf_ping_bing_sitemap' ); |
| 156 | delete_transient( 'xmlsf_flush_rewrite_rules' ); |
| 157 | delete_transient( 'xmlsf_check_static_files' ); |
| 158 | delete_transient( 'xmlsf_prefetch_post_meta_failed' ); |
| 159 | // Remove term meta term_modified_gmt. |
| 160 | delete_metadata( 'term', 0, 'term_modified_gmt', '', true ); |
| 161 | // Remove comments meta _xmlsf_comment_date. |
| 162 | delete_metadata( 'post', 0, '_xmlsf_comment_date', '', true ); |
| 163 | |
| 164 | $author_settings = (array) get_option( 'xmlsf_author_settings', array() ); |
| 165 | $tax_settings = (array) get_option( 'xmlsf_taxonomy_settings', array() ); |
| 166 | // Do not switch to core sitemap when upgrading. |
| 167 | add_option( 'xmlsf_server', 'plugin' ); |
| 168 | // Set include array. |
| 169 | $disabled = array(); |
| 170 | if ( empty( $tax_settings['active'] ) ) { |
| 171 | $disabled[] = 'taxonomies'; |
| 172 | } |
| 173 | if ( empty( $author_settings['active'] ) ) { |
| 174 | $disabled[] = 'users'; |
| 175 | } |
| 176 | // Add general settings option. |
| 177 | add_option( 'xmlsf_disabled_providers', ! empty( $disabled ) ? $disabled : '' ); |
| 178 | // Update taxonomy terms limit. |
| 179 | $tax_settings['limit'] = isset( $tax_settings['term_limit'] ) ? $tax_settings['term_limit'] : '3000'; |
| 180 | unset( $tax_settings['term_limit'] ); |
| 181 | update_option( 'xmlsf_taxonomy_settings', $tax_settings ); |
| 182 | // Update users limit. |
| 183 | $author_settings['limit'] = isset( $author_settings['term_limit'] ) ? $author_settings['term_limit'] : '1000'; |
| 184 | unset( $author_settings['term_limit'] ); |
| 185 | update_option( 'xmlsf_author_settings', $author_settings ); |
| 186 | |
| 187 | // Delete old settings. |
| 188 | delete_option( 'xmlsf_ping' ); |
| 189 | delete_option( 'xmlsf_permalinks_flushed' ); |
| 190 | delete_option( 'xmlsf_domains' ); |
| 191 | set_transient( 'xmlsf_images_meta_primed', get_option( 'xmlsf_images_meta_primed' ) ); |
| 192 | set_transient( 'xmlsf_comments_meta_primed', get_option( 'xmlsf_comments_meta_primed' ) ); |
| 193 | delete_option( 'xmlsf_images_meta_primed' ); |
| 194 | delete_option( 'xmlsf_comments_meta_primed' ); |
| 195 | |
| 196 | // Remove deprecated transient. |
| 197 | delete_transient( 'xmlsf_static_files' ); |
| 198 | } |
| 199 | |
| 200 | if ( version_compare( '5.5', $db_version, '>' ) ) { |
| 201 | $post_type_settings = get_option( 'xmlsf_post_types' ); |
| 202 | update_option( 'xmlsf_post_type_settings', $post_type_settings ); |
| 203 | |
| 204 | $post_types = array(); |
| 205 | foreach ( $post_type_settings as $post_type => $settings ) { |
| 206 | if ( ! empty( $settings['active'] ) ) { |
| 207 | $post_types[] = $post_type; |
| 208 | } |
| 209 | } |
| 210 | update_option( 'xmlsf_post_types', $post_types ); |
| 211 | } |
| 212 | |
| 213 | if ( version_compare( '5.7', $db_version, '>' ) ) { |
| 214 | // Drop priority meta data. |
| 215 | delete_metadata( 'post', 0, '_xmlsf_priority', '', true ); |
| 216 | delete_transient( 'sitemap_notifier_access_token' ); |
| 217 | delete_transient( 'sitemap_notifier_submission' ); |
| 218 | } elseif ( version_compare( '5.7.2', $db_version, '>' ) ) { |
| 219 | // Fix 5.7 and 5.7.1 broken transients upgrade routine. |
| 220 | delete_transient( 'sitemap_notifier_google_access_token' ); |
| 221 | delete_transient( 'sitemap_notifier_google_submission' ); |
| 222 | } |
| 223 | |
| 224 | // Add possible missing new defaults. |
| 225 | xmlsf_update_from_defaults( false ); |
| 226 | |
| 227 | do_action( 'xmlsf_upgrade', $db_version ); |
| 228 | } |
| 229 |