コード例 #1
0
ファイル: class-sitemaps.php プロジェクト: Didox/beminfinito
 /**
  * Hijack requests for potential sitemaps and XSL files.
  *
  * @param \WP_Query $query Main query instance.
  */
 function redirect($query)
 {
     if (!$query->is_main_query()) {
         return;
     }
     $xsl = get_query_var('xsl');
     if (!empty($xsl)) {
         $this->xsl_output($xsl);
         $this->sitemap_close();
     }
     $type = get_query_var('sitemap');
     if (empty($type)) {
         return;
     }
     $this->set_n(get_query_var('sitemap_n'));
     /**
      * Filter: 'wpseo_enable_xml_sitemap_transient_caching' - Allow disabling the transient cache
      *
      * @api bool $unsigned Enable cache or not, defaults to true
      */
     $caching = apply_filters('wpseo_enable_xml_sitemap_transient_caching', true);
     if ($caching) {
         do_action('wpseo_sitemap_stylesheet_cache_' . $type, $this);
         $sitemap_cache_key = WPSEO_Utils::get_sitemap_cache_key($type, $this->n);
         $this->sitemap = get_transient($sitemap_cache_key);
     }
     if (!$this->sitemap || '' == $this->sitemap) {
         $this->build_sitemap($type);
         // 404 for invalid or empty sitemaps.
         if ($this->bad_sitemap) {
             $GLOBALS['wp_query']->set_404();
             status_header(404);
             return;
         }
         if ($caching) {
             /**
              * We need to set a timeout, otherwise the transient is loaded every request!
              *
              * See: https://codex.wordpress.org/Function_Reference/set_transient
              * NB: transients that never expire are autoloaded, whereas transients with an expiration time
              * are not autoloaded. Consider this when adding transients that may not be needed on every
              * page, and thus do not need to be autoloaded, impacting page performance.
              */
             set_transient($sitemap_cache_key, $this->sitemap, DAY_IN_SECONDS);
         }
     } else {
         $this->transient = true;
     }
     $this->output();
     $this->sitemap_close();
 }