/**
  * @covers WPSEO_Sitemaps::get_last_modified
  */
 public function test_get_last_modified()
 {
     // create and go to post
     $post_id = $this->factory->post->create();
     $this->go_to(get_permalink($post_id));
     $date = self::$class_instance->get_last_modified(array('post'));
     $post = get_post($post_id);
     $this->assertEquals($date, date('c', strtotime($post->post_modified_gmt)));
 }
 /**
  * Tests the main sitemap and also tests the transient cache
  *
  * @covers WPSEO_Sitemaps::redirect
  */
 public function test_main_sitemap()
 {
     self::$class_instance->reset();
     set_query_var('sitemap', 1);
     // Go to the XML sitemap twice, see if transient cache is set
     self::$class_instance->redirect($GLOBALS['wp_the_query']);
     $this->expectOutputContains(array('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', '<sitemap>', '<lastmod>', '</sitemapindex>'));
     self::$class_instance->redirect($GLOBALS['wp_the_query']);
     $this->expectOutputContains(array('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', '<sitemap>', '<lastmod>', '</sitemapindex>', 'Served from transient cache'));
 }
 /**
  * @param int $max_entries Entries per sitemap.
  *
  * @return array
  */
 public function get_index_links($max_entries)
 {
     $taxonomies = get_taxonomies(array('public' => true), 'objects');
     if (empty($taxonomies)) {
         return array();
     }
     $taxonomy_names = array_filter(array_keys($taxonomies), array($this, 'is_valid_taxonomy'));
     $taxonomies = array_intersect_key($taxonomies, array_flip($taxonomy_names));
     // Retrieve all the taxonomies and their terms so we can do a proper count on them.
     /**
      * Filter the setting of excluding empty terms from the XML sitemap.
      *
      * @param boolean $exclude        Defaults to true.
      * @param array   $taxonomy_names Array of names for the taxonomies being processed.
      */
     $hide_empty = apply_filters('wpseo_sitemap_exclude_empty_terms', true, $taxonomy_names);
     $all_taxonomies = array();
     foreach ($taxonomy_names as $taxonomy_name) {
         $taxonomy_terms = get_terms($taxonomy_name, array('hide_empty' => $hide_empty, 'fields' => 'ids'));
         if (count($taxonomy_terms) > 0) {
             $all_taxonomies[$taxonomy_name] = $taxonomy_terms;
         }
     }
     $index = array();
     foreach ($taxonomies as $tax_name => $tax) {
         if (!isset($all_taxonomies[$tax_name])) {
             // No eligible terms found.
             continue;
         }
         $total_count = isset($all_taxonomies[$tax_name]) ? count($all_taxonomies[$tax_name]) : 1;
         $max_pages = 1;
         if ($total_count > $max_entries) {
             $max_pages = (int) ceil($total_count / $max_entries);
         }
         $last_modified_gmt = WPSEO_Sitemaps::get_last_modified_gmt($tax->object_type);
         for ($page_counter = 0; $page_counter < $max_pages; $page_counter++) {
             $current_page = $max_pages > 1 ? $page_counter + 1 : '';
             if (!is_array($tax->object_type) || count($tax->object_type) == 0) {
                 continue;
             }
             $terms = array_splice($all_taxonomies[$tax_name], 0, $max_entries);
             if (!$terms) {
                 continue;
             }
             $args = array('post_type' => $tax->object_type, 'tax_query' => array(array('taxonomy' => $tax_name, 'terms' => $terms)), 'orderby' => 'modified', 'order' => 'DESC', 'posts_per_page' => 1);
             $query = new WP_Query($args);
             if ($query->have_posts()) {
                 $date = $query->posts[0]->post_modified_gmt;
             } else {
                 $date = $last_modified_gmt;
             }
             $index[] = array('loc' => WPSEO_Sitemaps_Router::get_base_url($tax_name . '-sitemap' . $current_page . '.xml'), 'lastmod' => $date);
         }
     }
     return $index;
 }
 /**
  * @param int $max_entries Entries per sitemap.
  *
  * @return array
  */
 public function get_index_links($max_entries)
 {
     global $wpdb;
     $taxonomies = get_taxonomies(array('public' => true), 'objects');
     if (empty($taxonomies)) {
         return array();
     }
     $taxonomy_names = array_filter(array_keys($taxonomies), array($this, 'is_valid_taxonomy'));
     $taxonomies = array_intersect_key($taxonomies, array_flip($taxonomy_names));
     // Retrieve all the taxonomies and their terms so we can do a proper count on them.
     /**
      * Filter the setting of excluding empty terms from the XML sitemap.
      *
      * @param boolean $exclude        Defaults to true.
      * @param array   $taxonomy_names Array of names for the taxonomies being processed.
      */
     $hide_empty = apply_filters('wpseo_sitemap_exclude_empty_terms', true, $taxonomy_names) ? 'count != 0 AND' : '';
     $sql = "\n\t\t\tSELECT taxonomy, term_id\n\t\t\tFROM {$wpdb->term_taxonomy}\n\t\t\tWHERE {$hide_empty} taxonomy IN ('" . implode("','", $taxonomy_names) . "');\n\t\t";
     $all_taxonomy_terms = $wpdb->get_results($sql);
     $all_taxonomies = array();
     foreach ($all_taxonomy_terms as $obj) {
         $all_taxonomies[$obj->taxonomy][] = $obj->term_id;
     }
     unset($hide_empty, $sql, $all_taxonomy_terms, $obj);
     $index = array();
     foreach ($taxonomies as $tax_name => $tax) {
         if (!isset($all_taxonomies[$tax_name])) {
             // No eligible terms found.
             continue;
         }
         $steps = $max_entries;
         $count = isset($all_taxonomies[$tax_name]) ? count($all_taxonomies[$tax_name]) : 1;
         $n = $count > $max_entries ? (int) ceil($count / $max_entries) : 1;
         for ($i = 0; $i < $n; $i++) {
             $count = $n > 1 ? $i + 1 : '';
             if (!is_array($tax->object_type) || count($tax->object_type) == 0) {
                 continue;
             }
             if (empty($count) || $count == $n) {
                 $date = WPSEO_Sitemaps::get_last_modified_gmt($tax->object_type);
             } else {
                 $terms = array_splice($all_taxonomies[$tax_name], 0, $steps);
                 if (!$terms) {
                     continue;
                 }
                 $args = array('post_type' => $tax->object_type, 'tax_query' => array(array('taxonomy' => $tax_name, 'terms' => $terms)), 'orderby' => 'modified', 'order' => 'DESC');
                 $query = new WP_Query($args);
                 if ($query->have_posts()) {
                     $date = $query->posts[0]->post_modified_gmt;
                 } else {
                     $date = WPSEO_Sitemaps::get_last_modified_gmt($tax->object_type);
                 }
                 unset($terms, $args, $query);
             }
             $index[] = array('loc' => WPSEO_Sitemaps_Router::get_base_url($tax_name . '-sitemap' . $count . '.xml'), 'lastmod' => $date);
         }
     }
     return $index;
 }
 /**
  * Produces set of links to prepend at start of first sitemap page.
  *
  * @param string $post_type Post type to produce links for.
  *
  * @return array
  */
 protected function get_first_links($post_type)
 {
     $links = array();
     $needs_archive = true;
     if (!$this->get_page_on_front_id() && ($post_type == 'post' || $post_type == 'page')) {
         $links[] = array('loc' => $this->get_home_url(), 'chf' => 'daily', 'pri' => 1);
         $needs_archive = false;
     } elseif ($this->get_page_on_front_id() && $post_type === 'post' && $this->get_page_for_posts_id()) {
         $page_for_posts_url = get_permalink($this->get_page_for_posts_id());
         $links[] = array('loc' => $page_for_posts_url, 'chf' => 'daily', 'pri' => 1);
         $needs_archive = false;
     }
     if (!$needs_archive) {
         return $links;
     }
     $archive_url = get_post_type_archive_link($post_type);
     /**
      * Filter the URL Yoast SEO uses in the XML sitemap for this post type archive.
      *
      * @param string $archive_url The URL of this archive
      * @param string $post_type   The post type this archive is for.
      */
     $archive_url = apply_filters('wpseo_sitemap_post_type_archive_link', $archive_url, $post_type);
     if ($archive_url) {
         /**
          * Filter the priority of the URL Yoast SEO uses in the XML sitemap.
          *
          * @param float  $priority  The priority for this URL, ranging from 0 to 1
          * @param string $post_type The post type this archive is for.
          */
         $links[] = array('loc' => $archive_url, 'mod' => WPSEO_Sitemaps::get_last_modified_gmt($post_type), 'chf' => 'daily', 'pri' => 1);
     }
     return $links;
 }
 /**
  * Produce array of URL parts for given post object.
  *
  * @param object $post Post object to get URL parts for.
  *
  * @return array|bool
  */
 protected function get_url($post)
 {
     $url = array();
     /**
      * Filter the URL Yoast SEO uses in the XML sitemap.
      *
      * Note that only absolute local URLs are allowed as the check after this removes external URLs.
      *
      * @param string $url  URL to use in the XML sitemap
      * @param object $post Post object for the URL.
      */
     $url['loc'] = apply_filters('wpseo_xml_sitemap_post_url', get_permalink($post), $post);
     /**
      * Do not include external URLs.
      *
      * @see https://wordpress.org/plugins/page-links-to/ can rewrite permalinks to external URLs.
      */
     if (false === strpos($url['loc'], $this->get_home_url())) {
         return false;
     }
     $modified = max($post->post_modified_gmt, $post->post_date_gmt);
     if ($modified !== '0000-00-00 00:00:00') {
         $url['mod'] = $modified;
     }
     $frequency_filter = $post->post_type . '_single';
     $frequency_default = 'weekly';
     if ((int) $post->ID === $this->get_page_for_posts_id()) {
         $frequency_filter = 'blogpage';
         $frequency_default = 'daily';
     }
     if ((int) $post->ID === $this->get_page_on_front_id()) {
         $frequency_filter = 'homepage';
         $frequency_default = 'daily';
     }
     $url['chf'] = WPSEO_Sitemaps::filter_frequency($frequency_filter, $frequency_default, $url['loc']);
     $canonical = WPSEO_Meta::get_value('canonical', $post->ID);
     if ($canonical !== '' && $canonical !== $url['loc']) {
         /*
         Let's assume that if a canonical is set for this page and it's different from
            the URL of this post, that page is either already in the XML sitemap OR is on
            an external site, either way, we shouldn't include it here.
         */
         return false;
     }
     unset($canonical);
     $options = $this->get_options();
     if ($options['trailingslash'] === true && $post->post_type !== 'post') {
         $url['loc'] = trailingslashit($url['loc']);
     }
     $url['pri'] = $this->calculate_priority($post);
     $url['images'] = $this->get_image_parser()->get_images($post);
     return $url;
 }
 /**
  * Get set of sitemap link data.
  *
  * @param string $type         Sitemap type.
  * @param int    $max_entries  Entries per sitemap.
  * @param int    $current_page Current page of the sitemap.
  *
  * @return array
  */
 public function get_sitemap_links($type, $max_entries, $current_page)
 {
     $options = WPSEO_Options::get_all();
     $links = array();
     if ($options['disable-author'] === true || $options['disable_author_sitemap'] === true) {
         return $links;
     }
     $users = $this->get_users(array('offset' => ($current_page - 1) * $max_entries, 'number' => $max_entries));
     $users = $this->exclude_users($users);
     if (empty($users)) {
         $users = array();
     }
     $time = time();
     foreach ($users as $user) {
         $author_link = get_author_posts_url($user->ID);
         if (empty($author_link)) {
             continue;
         }
         $mod = $time;
         if (isset($user->_yoast_wpseo_profile_updated)) {
             $mod = $user->_yoast_wpseo_profile_updated;
         }
         $url = array('loc' => $author_link, 'pri' => 0.8, 'chf' => WPSEO_Sitemaps::filter_frequency('author_archive', 'daily', $author_link), 'mod' => date(DATE_W3C, $mod));
         /** This filter is documented at inc/sitemaps/class-post-type-sitemap-provider.php */
         $url = apply_filters('wpseo_sitemap_entry', $url, 'user', $user);
         if (!empty($url)) {
             $links[] = $url;
         }
     }
     return $links;
 }
Exemplo n.º 8
0
/**
 * Notify search engines of the updated sitemap.
 *
 * @deprecated
 * @see WPSEO_Sitemaps::ping_search_engines()
 *
 * @param string|null $sitemapurl Optional URL to make the ping for.
 */
function wpseo_ping_search_engines($sitemapurl = null)
{
    WPSEO_Sitemaps::ping_search_engines($sitemapurl);
}
 /**
  * After import finished, walk through imported post_types and update info.
  */
 public function status_transition_bulk_finished()
 {
     if (!defined('WP_IMPORTING')) {
         return;
     }
     if (empty($this->importing_post_types)) {
         return;
     }
     $options = WPSEO_Options::get_option('wpseo_xml');
     $ping_search_engines = false;
     foreach ($this->importing_post_types as $post_type) {
         wp_cache_delete('lastpostmodified:gmt:' . $post_type, 'timeinfo');
         // #17455.
         // Just have the cache deleted for nav_menu_item.
         if ('nav_menu_item' === $post_type) {
             continue;
         }
         $option = sprintf('post_types-%s-not_in_sitemap', $post_type);
         if (!isset($options[$option]) || $options[$option] === false) {
             $ping_search_engines = true;
         }
     }
     // Nothing to do.
     if (false === $ping_search_engines) {
         return;
     }
     if (WP_CACHE) {
         do_action('wpseo_hit_sitemap_index');
     }
     WPSEO_Sitemaps::ping_search_engines();
 }
 /**
  * Get set of sitemap link data.
  *
  * @param string $type         Sitemap type.
  * @param int    $max_entries  Entries per sitemap.
  * @param int    $current_page Current page of the sitemap.
  *
  * @return array
  */
 public function get_sitemap_links($type, $max_entries, $current_page)
 {
     $options = WPSEO_Options::get_all();
     $links = array();
     if ($options['disable-author'] === true || $options['disable_author_sitemap'] === true) {
         return $links;
     }
     $steps = $max_entries;
     $offset = $current_page > 1 ? ($current_page - 1) * $max_entries : 0;
     // Initial query to fill in missing usermeta with the current timestamp.
     $users = get_users(array('who' => 'authors', 'meta_query' => array(array('key' => '_yoast_wpseo_profile_updated', 'value' => 'needs-a-value-anyway', 'compare' => 'NOT EXISTS'))));
     foreach ($users as $user) {
         update_user_meta($user->ID, '_yoast_wpseo_profile_updated', time());
     }
     unset($users, $user);
     // Query for users with this meta.
     $users = get_users(array('who' => 'authors', 'offset' => $offset, 'number' => $steps, 'meta_key' => '_yoast_wpseo_profile_updated', 'orderby' => 'meta_value_num', 'order' => 'ASC'));
     /**
      * Filter the authors, included in XML sitemap.
      *
      * @param array $users Array of user objects to filter.
      */
     $users = apply_filters('wpseo_sitemap_exclude_author', $users);
     // TODO deduplicate filter. R.
     if (empty($users)) {
         $users = array();
     }
     // Ascending sort.
     usort($users, array($this, 'user_map_sorter'));
     foreach ($users as $user) {
         $author_link = get_author_posts_url($user->ID);
         if (empty($author_link)) {
             continue;
         }
         $url = array('loc' => $author_link, 'pri' => 0.8, 'chf' => WPSEO_Sitemaps::filter_frequency('author_archive', 'daily', $author_link), 'mod' => date('c', isset($user->_yoast_wpseo_profile_updated) ? $user->_yoast_wpseo_profile_updated : time()));
         /** This filter is documented at inc/sitemaps/class-post-type-sitemap-provider.php */
         $url = apply_filters('wpseo_sitemap_entry', $url, 'user', $user);
         if (!empty($url)) {
             $links[] = $url;
         }
     }
     unset($user, $author_link, $url);
     return $links;
 }
 /**
  * Get set of sitemap link data.
  *
  * @param string $type         Sitemap type.
  * @param int    $max_entries  Entries per sitemap.
  * @param int    $current_page Current page of the sitemap.
  *
  * @return array
  */
 public function get_sitemap_links($type, $max_entries, $current_page)
 {
     global $wpdb;
     $links = array();
     $taxonomy = get_taxonomy($type);
     if ($taxonomy === false || !$this->is_valid_taxonomy($taxonomy->name) || !$taxonomy->public) {
         return $links;
     }
     $options = $this->get_options();
     $steps = $max_entries;
     $offset = $current_page > 1 ? ($current_page - 1) * $max_entries : 0;
     /** This filter is documented in inc/sitemaps/class-taxonomy-sitemap-provider.php */
     $hide_empty = apply_filters('wpseo_sitemap_exclude_empty_terms', true, $taxonomy);
     $terms = get_terms($taxonomy->name, array('hide_empty' => $hide_empty));
     $terms = array_splice($terms, $offset, $steps);
     if (empty($terms)) {
         $terms = array();
     }
     // Grab last modified date.
     $sql = "\n\t\t\tSELECT MAX(p.post_modified_gmt) AS lastmod\n\t\t\tFROM\t{$wpdb->posts} AS p\n\t\t\tINNER JOIN {$wpdb->term_relationships} AS term_rel\n\t\t\t\tON\t\tterm_rel.object_id = p.ID\n\t\t\tINNER JOIN {$wpdb->term_taxonomy} AS term_tax\n\t\t\t\tON\t\tterm_tax.term_taxonomy_id = term_rel.term_taxonomy_id\n\t\t\t\tAND\t\tterm_tax.taxonomy = %s\n\t\t\t\tAND\t\tterm_tax.term_id = %d\n\t\t\tWHERE\tp.post_status IN ('publish','inherit')\n\t\t\t\tAND\t\tp.post_password = ''\n\t\t";
     foreach ($terms as $term) {
         $url = array();
         $tax_noindex = WPSEO_Taxonomy_Meta::get_term_meta($term, $term->taxonomy, 'noindex');
         $tax_sitemap_inc = WPSEO_Taxonomy_Meta::get_term_meta($term, $term->taxonomy, 'sitemap_include');
         if ($tax_noindex === 'noindex' && $tax_sitemap_inc !== 'always') {
             continue;
         }
         if ($tax_sitemap_inc === 'never') {
             continue;
         }
         $url['loc'] = WPSEO_Taxonomy_Meta::get_term_meta($term, $term->taxonomy, 'canonical');
         if (!is_string($url['loc']) || $url['loc'] === '') {
             $url['loc'] = get_term_link($term, $term->taxonomy);
             if ($options['trailingslash'] === true) {
                 $url['loc'] = trailingslashit($url['loc']);
             }
         }
         if ($term->count > 10) {
             $url['pri'] = 0.6;
         } elseif ($term->count > 3) {
             $url['pri'] = 0.4;
         } else {
             $url['pri'] = 0.2;
         }
         $url['mod'] = $wpdb->get_var($wpdb->prepare($sql, $term->taxonomy, $term->term_id));
         $url['chf'] = WPSEO_Sitemaps::filter_frequency($term->taxonomy . '_term', 'weekly', $url['loc']);
         $url['images'] = $this->get_image_parser()->get_term_images($term);
         /** This filter is documented at inc/sitemaps/class-post-type-sitemap-provider.php */
         $url = apply_filters('wpseo_sitemap_entry', $url, 'term', $term);
         if (!empty($url)) {
             $links[] = $url;
         }
     }
     return $links;
 }