class-bwt-connect-settings.php
7 months ago
class-bwt-connect.php
4 months ago
class-gsc-connect-settings.php
7 months ago
class-gsc-connect.php
4 months ago
class-main.php
7 months ago
class-sanitize.php
7 months ago
class-sitemap-fields.php
4 months ago
class-sitemap-news-fields.php
4 months ago
class-sitemap-news-settings.php
2 months ago
class-sitemap-news.php
7 months ago
class-sitemap-settings.php
4 months ago
class-sitemap.php
4 months ago
class-main.php
347 lines
| 1 | <?php |
| 2 | /** |
| 3 | * XMLSF Admin CLASS |
| 4 | * |
| 5 | * @package XML Sitemap & Google News |
| 6 | */ |
| 7 | |
| 8 | namespace XMLSF\Admin; |
| 9 | |
| 10 | /** |
| 11 | * XMLSF Admin CLASS |
| 12 | */ |
| 13 | class Main { |
| 14 | /** |
| 15 | * Initialize the admin class. |
| 16 | */ |
| 17 | public static function init() { |
| 18 | self::notices_actions(); |
| 19 | |
| 20 | \add_action( 'update_option_xmlsf_sitemaps', array( __CLASS__, 'update_sitemaps' ) ); |
| 21 | |
| 22 | // ACTION LINK. |
| 23 | \add_filter( 'plugin_action_links_' . XMLSF_BASENAME, array( __CLASS__, 'add_action_link' ) ); |
| 24 | \add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_meta_links' ), 10, 2 ); |
| 25 | |
| 26 | // Shared Admin pages sidebar actions. |
| 27 | \add_action( |
| 28 | 'xmlsf_admin_sidebar', |
| 29 | function () { |
| 30 | include XMLSF_DIR . '/views/admin/sidebar-help.php'; |
| 31 | } |
| 32 | ); |
| 33 | \add_action( |
| 34 | 'xmlsf_admin_sidebar', |
| 35 | function () { |
| 36 | include XMLSF_DIR . '/views/admin/sidebar-contribute.php'; |
| 37 | }, |
| 38 | 20 |
| 39 | ); |
| 40 | |
| 41 | if ( \XMLSF\sitemaps_enabled( 'sitemap' ) ) { |
| 42 | namespace\Sitemap::init(); |
| 43 | } |
| 44 | |
| 45 | if ( \XMLSF\sitemaps_enabled( 'news' ) ) { |
| 46 | namespace\Sitemap_News::init(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Plugin compatibility hooks and filters. |
| 52 | */ |
| 53 | public static function compat() { |
| 54 | // Catch Box Pro compatibility. |
| 55 | if ( function_exists( 'catchbox_is_feed_url_present' ) ) { |
| 56 | \add_action( 'admin_notices', array( '\XMLSF\Compat\Catch_Box_Pro', 'admin_notices' ) ); |
| 57 | } |
| 58 | |
| 59 | if ( \XMLSF\sitemaps_enabled( 'sitemap' ) ) { |
| 60 | Sitemap::compat(); |
| 61 | } |
| 62 | |
| 63 | if ( \XMLSF\sitemaps_enabled( 'news' ) ) { |
| 64 | Sitemap_News::compat(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add options pages |
| 70 | */ |
| 71 | public static function add_options_pages() { |
| 72 | if ( \XMLSF\sitemaps_enabled( 'sitemap' ) ) { |
| 73 | Sitemap::add_options_page(); |
| 74 | } |
| 75 | |
| 76 | if ( \XMLSF\sitemaps_enabled( 'news' ) ) { |
| 77 | Sitemap_News::add_options_page(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * SETTINGS |
| 83 | */ |
| 84 | |
| 85 | /** |
| 86 | * Update actions for Sitemaps |
| 87 | */ |
| 88 | public static function update_sitemaps() { |
| 89 | if ( ! \xmlsf()->using_permalinks() ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Set transients for flushing. |
| 94 | \set_transient( 'xmlsf_sitemaps_updated', true ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Maybe sitemaps option was updated. |
| 99 | * |
| 100 | * Checks $_GET['settings-updated'] and transient 'xmlsf_sitemaps_updated'. Hooked into settings page load actions. |
| 101 | */ |
| 102 | public static function maybe_sitemaps_updated() { |
| 103 | if ( ! empty( $_GET['settings-updated'] ) && delete_transient( 'xmlsf_sitemaps_updated' ) ) { |
| 104 | // Flush rewrite rules. |
| 105 | \flush_rewrite_rules( false ); |
| 106 | |
| 107 | // Check static files. |
| 108 | $sitemaps = (array) \get_option( 'xmlsf_sitemaps' ); |
| 109 | |
| 110 | if ( ! empty( $sitemaps['sitemap'] ) ) { |
| 111 | $slug = \is_object( \xmlsf()->sitemap ) ? \xmlsf()->sitemap->slug() : 'sitemap'; |
| 112 | |
| 113 | if ( \file_exists( \trailingslashit( \get_home_path() ) . $slug . '.xml' ) ) { |
| 114 | \add_settings_error( |
| 115 | 'static_files_notice', |
| 116 | 'static_file_' . $slug, |
| 117 | \sprintf( /* translators: %1$s file name, %2$s is XML Sitemap (linked to options-reading.php) */ |
| 118 | \esc_html__( 'A conflicting static file has been found: %1$s. Either delete it or disable the corresponding %2$s.', 'xml-sitemap-feed' ), |
| 119 | \esc_html( $slug . '.xml' ), |
| 120 | '<a href="' . \esc_url( admin_url( 'options-reading.php' ) ) . '#xmlsf_sitemaps">' . \esc_html__( 'XML Sitemap', 'xml-sitemap-feed' ) . '</a>' |
| 121 | ), |
| 122 | 'warning' |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if ( ! empty( $sitemaps['sitemap-news'] ) ) { |
| 128 | $slug = is_object( \xmlsf()->sitemap_news ) ? \xmlsf()->sitemap_news->slug() : 'sitemap-news'; |
| 129 | |
| 130 | if ( \file_exists( \trailingslashit( \get_home_path() ) . $slug . '.xml' ) ) { |
| 131 | \add_settings_error( |
| 132 | 'static_files_notice', |
| 133 | 'static_file_' . $slug, |
| 134 | \sprintf( /* translators: %1$s file name, %2$s is XML Sitemap (linked to options-reading.php) */ |
| 135 | \esc_html__( 'A conflicting static file has been found: %1$s. Either delete it or disable the corresponding %2$s.', 'xml-sitemap-feed' ), |
| 136 | \esc_html( $slug . '.xml' ), |
| 137 | '<a href="' . \esc_url( admin_url( 'options-reading.php' ) ) . '#xmlsf_sitemaps">' . \esc_html__( 'XML Sitemap', 'xml-sitemap-feed' ) . '</a>' |
| 138 | ), |
| 139 | 'warning' |
| 140 | ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Register settings and add settings fields |
| 148 | */ |
| 149 | public static function register_settings() { |
| 150 | // Sitemaps. |
| 151 | \register_setting( |
| 152 | 'reading', |
| 153 | \get_option( 'blog_public' ) ? 'xmlsf_sitemaps' : '' |
| 154 | ); |
| 155 | \add_settings_field( |
| 156 | 'xmlsf_sitemaps', |
| 157 | __( 'Enable XML sitemaps', 'xml-sitemap-feed' ), |
| 158 | array( __CLASS__, 'sitemaps_settings_field' ), |
| 159 | 'reading' |
| 160 | ); |
| 161 | |
| 162 | // Help tab. |
| 163 | \add_action( 'load-options-reading.php', array( __CLASS__, 'xml_sitemaps_help' ) ); |
| 164 | |
| 165 | // Robots rules. |
| 166 | \register_setting( |
| 167 | 'reading', |
| 168 | 'xmlsf_robots', |
| 169 | 'sanitize_textarea_field' |
| 170 | ); |
| 171 | \add_settings_field( |
| 172 | 'xmlsf_robots', |
| 173 | __( 'Additional robots.txt rules', 'xml-sitemap-feed' ), |
| 174 | array( __CLASS__, 'robots_settings_field' ), |
| 175 | 'reading' |
| 176 | ); |
| 177 | |
| 178 | // Maybe flush rewrite rules. |
| 179 | \add_action( 'load-options-reading.php', array( __CLASS__, 'maybe_sitemaps_updated' ) ); |
| 180 | |
| 181 | if ( \XMLSF\sitemaps_enabled( 'sitemap' ) ) { |
| 182 | namespace\Sitemap::register_settings(); |
| 183 | } |
| 184 | |
| 185 | if ( \XMLSF\sitemaps_enabled( 'news' ) ) { |
| 186 | namespace\Sitemap_News::register_settings(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Sitemaps help tabs |
| 192 | */ |
| 193 | public static function xml_sitemaps_help() { |
| 194 | \ob_start(); |
| 195 | include XMLSF_DIR . '/views/admin/help-tab-sitemaps.php'; |
| 196 | include XMLSF_DIR . '/views/admin/help-tab-support.php'; |
| 197 | $content = \ob_get_clean(); |
| 198 | |
| 199 | \get_current_screen()->add_help_tab( |
| 200 | array( |
| 201 | 'id' => 'sitemap-settings', |
| 202 | 'title' => __( 'Enable XML sitemaps', 'xml-sitemap-feed' ), |
| 203 | 'content' => $content, |
| 204 | 'priority' => 11, |
| 205 | ) |
| 206 | ); |
| 207 | |
| 208 | \ob_start(); |
| 209 | include XMLSF_DIR . '/views/admin/help-tab-robots.php'; |
| 210 | include XMLSF_DIR . '/views/admin/help-tab-support.php'; |
| 211 | $content = \ob_get_clean(); |
| 212 | |
| 213 | \get_current_screen()->add_help_tab( |
| 214 | array( |
| 215 | 'id' => 'robots', |
| 216 | 'title' => __( 'Additional robots.txt rules', 'xml-sitemap-feed' ), |
| 217 | 'content' => $content, |
| 218 | 'priority' => 11, |
| 219 | ) |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Sitemap settings fields |
| 225 | */ |
| 226 | public static function sitemaps_settings_field() { |
| 227 | if ( 1 === (int) \get_option( 'blog_public' ) ) { |
| 228 | $sitemaps = (array) \get_option( 'xmlsf_sitemaps', \XMLSF\get_default_settings( 'sitemaps' ) ); |
| 229 | // The actual fields for data entry. |
| 230 | include XMLSF_DIR . '/views/admin/field-sitemaps.php'; |
| 231 | } else { |
| 232 | \esc_html_e( 'XML Sitemaps are not available because of your site’s visibility settings (above).', 'xml-sitemap-feed' ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * ROBOTS |
| 238 | */ |
| 239 | public static function robots_settings_field() { |
| 240 | global $wp_rewrite; |
| 241 | |
| 242 | $rules = (array) \get_option( 'rewrite_rules' ); |
| 243 | $static = \file_exists( \trailingslashit( \get_home_path() ) . 'robots.txt' ); |
| 244 | |
| 245 | // The actual fields for data entry. |
| 246 | include XMLSF_DIR . '/views/admin/field-robots.php'; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Admin notices actions |
| 251 | */ |
| 252 | public static function notices_actions() { |
| 253 | if ( ! isset( $_POST['_xmlsf_notice_nonce'] ) || ! \wp_verify_nonce( sanitize_key( $_POST['_xmlsf_notice_nonce'] ), XMLSF_BASENAME . '-notice' ) ) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if ( isset( $_POST['xmlsf-dismiss'] ) ) { |
| 258 | // Store user notice dismissal. |
| 259 | $dismissed = \sanitize_key( $_POST['xmlsf-dismiss'] ); |
| 260 | \add_user_meta( |
| 261 | \get_current_user_id(), |
| 262 | 'xmlsf_dismissed', |
| 263 | $dismissed, |
| 264 | false |
| 265 | ); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Add action link |
| 271 | * |
| 272 | * @param array $links Array of links. |
| 273 | */ |
| 274 | public static function add_action_link( $links ) { |
| 275 | $settings_link = '<a href="' . \admin_url( 'options-reading.php' ) . '#xmlsf_sitemaps">' . \translate( 'Settings' ) . '</a>'; // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction |
| 276 | \array_unshift( $links, $settings_link ); |
| 277 | return $links; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Add plugin meta links |
| 282 | * |
| 283 | * @param array $links Array of links. |
| 284 | * @param string $file Plugin file name. |
| 285 | */ |
| 286 | public static function plugin_meta_links( $links, $file ) { |
| 287 | if ( XMLSF_BASENAME === $file ) { |
| 288 | $links[] = '<a target="_blank" href="https://premium.status301.com/support/"><span class="dashicons dashicons-sos" style="color:#d63638"></span>' . \translate( 'Help' ) . '</a>'; // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction |
| 289 | $links[] = '<a target="_blank" href="https://wordpress.org/support/plugin/xml-sitemap-feed/reviews/?filter=5#new-post">' . \__( 'Rate', 'xml-sitemap-feed' ) . ' <span style="color:#dba617">★★★★★</span></a>'; |
| 290 | } |
| 291 | return $links; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Plugin activation |
| 296 | * |
| 297 | * @since 5.4 |
| 298 | * @return void |
| 299 | */ |
| 300 | public static function activate() { |
| 301 | // Load sitemap. |
| 302 | \xmlsf()->get_server( 'sitemap' ); |
| 303 | |
| 304 | // Add core rules if needed. |
| 305 | if ( \function_exists( 'wp_sitemaps_get_server' ) && 'core' === \xmlsf()->sitemap->server_type ) { |
| 306 | $sitemaps = \wp_sitemaps_get_server(); |
| 307 | $sitemaps->register_rewrites(); |
| 308 | } |
| 309 | |
| 310 | // Register new plugin rules. |
| 311 | \xmlsf()->register_rewrites(); |
| 312 | |
| 313 | // Then flush. |
| 314 | \flush_rewrite_rules( false ); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Plugin de-activation |
| 319 | * |
| 320 | * @since 5.0 |
| 321 | * @return void |
| 322 | */ |
| 323 | public static function deactivate() { |
| 324 | // Clear all cache metadata. |
| 325 | // Clear all meta caches... |
| 326 | \delete_metadata( 'post', 0, '_xmlsf_image_attached', '', true ); |
| 327 | \delete_metadata( 'post', 0, '_xmlsf_image_featured', '', true ); |
| 328 | \delete_metadata( 'post', 0, '_xmlsf_comment_date_gmt', '', true ); |
| 329 | \delete_metadata( 'term', 0, 'term_modified', '', true ); |
| 330 | \delete_metadata( 'user', 0, 'user_modified', '', true ); |
| 331 | \delete_transient( 'xmlsf_images_meta_primed' ); |
| 332 | \delete_transient( 'xmlsf_comments_meta_primed' ); |
| 333 | |
| 334 | // Remove old rules. |
| 335 | \xmlsf()->unregister_rewrites(); |
| 336 | |
| 337 | // Re-add core rules. |
| 338 | if ( \function_exists( 'wp_sitemaps_get_server' ) ) { |
| 339 | $sitemaps = \wp_sitemaps_get_server(); |
| 340 | $sitemaps->register_rewrites(); |
| 341 | } |
| 342 | |
| 343 | // Then flush. |
| 344 | \flush_rewrite_rules( false ); |
| 345 | } |
| 346 | } |
| 347 |