Skip to content

Commit

Permalink
polylang fix plus new filter xmlsf_excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
RavanH committed Jul 5, 2016
1 parent d09f030 commit 37a46ba
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 65 deletions.
143 changes: 90 additions & 53 deletions includes/class-xmlsitemapfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class XMLSitemapFeed {
private $lastmodified; // unused at the moment
private $postmodified = array();
private $termmodified = array();
private $blogpage;
private $frontpages = null;
private $blogpages = null;
private $images = array();

// make some private parts public ;)
Expand Down Expand Up @@ -370,33 +371,70 @@ public function do_tags( $type = 'post' )
) ? (array)$return[$type]['tags'] : array();
}

public function is_home($id)
private function get_translations( $post_id )
{
if ( empty($this->blogpage) ) {
$blogpage = get_option('page_for_posts');
$translation_ids = array();
// Polylang compat
if ( function_exists('pll_get_post_translations') ) {
$translations = pll_get_post_translations($post_id);
if ( is_array($translations) )
foreach ( $translations as $slug => $id )
$translation_ids[] = $id;
}
// WPML compat
global $sitepress;
if ( isset($sitepress) && is_object($sitepress) && method_exists($sitepress, 'get_languages') && method_exists($sitepress, 'get_object_id') ) {
foreach ( array_keys ( $sitepress->get_languages(false,true) ) as $term ) {
$id = $sitepress->get_object_id($post_id,'page',false,$term);
if ( $post_id != $id ) $translation_ids[] = $id;
}
}

return $translation_ids;
}

private function get_blogpages()
{
if ( null === $this->blogpages ) :
$blogpages = array();
if ( 'page' == get_option('show_on_front') ) {
$blogpage = get_option('page_for_posts');
if ( !empty($blogpage) ) {
global $polylang,$sitepress; // Polylang and WPML compat
if ( isset($polylang) && is_object($polylang) && isset($polylang->model) && is_object($polylang->model) && method_exists($polylang->model, 'get_translations') )
$this->blogpage = $polylang->model->get_translations('post', $blogpage);
if ( isset($sitepress) && is_object($sitepress) && method_exists($sitepress, 'get_languages') && method_exists($sitepress, 'get_object_id') )
foreach ( array_keys ( $sitepress->get_languages(false,true) ) as $term )
$this->blogpage[] = $sitepress->get_object_id($id,'page',false,$term);
else
$this->blogpage = array($blogpage);
} else {
$this->blogpage = array('-1');
$blogpages[] = $blogpage;
$blogpages += $this->get_translations($blogpage);
}
}
$this->blogpages = $blogpages;
endif;

return $this->blogpages;
}

private function get_frontpages()
{
if ( null === $this->frontpages ) :
$frontpages = array();
if ( 'page' == get_option('show_on_front') ) {
$frontpage = get_option('page_on_front');
$frontpages[] = $frontpage;
$frontpages += $this->get_translations($frontpage);
}
$this->frontpages = $frontpages;
endif;

return $this->frontpages;
}

return in_array($id,$this->blogpage);
private function is_home( $id )
{
return in_array( $id, $this->get_blogpages() );
}

/**
* TEMPLATE FUNCTIONS
*/

public function modified($sitemap = 'post_type', $term = '')
public function modified( $sitemap = 'post_type', $term = '' )
{
if ('post_type' == $sitemap) :

Expand Down Expand Up @@ -471,7 +509,7 @@ public function modified($sitemap = 'post_type', $term = '')
endif;
}

public function get_images($sitemap = '')
public function get_images( $sitemap = '' )
{
global $post;
if ( empty($this->images[$post->ID]) ) {
Expand Down Expand Up @@ -510,13 +548,13 @@ public function get_images($sitemap = '')
return ( isset($this->images[$post->ID]) ) ? $this->images[$post->ID] : false;
}

public function get_lastmod($sitemap = 'post_type', $term = '')
public function get_lastmod( $sitemap = 'post_type', $term = '' )
{
$return = trim(mysql2date('Y-m-d\TH:i:s+00:00', $this->modified($sitemap,$term), false));
return !empty($return) ? "\t<lastmod>".$return."</lastmod>\r\n\t" : '';
}

public function get_changefreq($sitemap = 'post_type', $term = '')
public function get_changefreq( $sitemap = 'post_type', $term = '' )
{
$modified = trim($this->modified($sitemap,$term));

Expand All @@ -540,7 +578,7 @@ public function get_changefreq($sitemap = 'post_type', $term = '')
return $changefreq;
}

public function get_priority($sitemap = 'post_type', $term = '')
public function get_priority( $sitemap = 'post_type', $term = '' )
{
if ( 'post_type' == $sitemap ) :
global $post;
Expand Down Expand Up @@ -571,7 +609,7 @@ public function get_priority($sitemap = 'post_type', $term = '')
$priority_value = floatval($defaults[$post->post_type]['priority']);

// reduce by age
// NOTE : home/blog page gets same treatment as sticky post
// NOTE : home/blog page gets same treatment as sticky post, i.e. no reduction by age
if ( is_sticky($post->ID) || $this->is_home($post->ID) )
$priority = $priority_value;
else
Expand Down Expand Up @@ -622,39 +660,41 @@ public function get_home_urls()
{
$urls = array();

global $polylang,$sitepress; // Polylang and WPML compat

if ( isset($polylang) && is_object($polylang) && method_exists($polylang, 'get_languages') && method_exists($polylang, 'get_home_url') )
foreach ($polylang->get_languages_list() as $term)
$urls[] = $polylang->get_home_url($term);
elseif ( isset($sitepress) && is_object($sitepress) && method_exists($sitepress, 'get_languages') && method_exists($sitepress, 'language_url') )
global $sitepress; // Polylang and WPML compat
if ( function_exists('pll_the_languages') ) {
$languages = pll_the_languages( array( 'raw' => 1 ) );
if ( is_array($languages) )
foreach ( $languages as $language )
$urls[] = pll_home_url( $language['slug'] );
else
$urls[] = home_url();
} elseif ( isset($sitepress) && is_object($sitepress) && method_exists($sitepress, 'get_languages') && method_exists($sitepress, 'language_url') ) {
foreach ( array_keys ( $sitepress->get_languages(false,true) ) as $term )
$urls[] = $sitepress->language_url($term);
else
} else {
$urls[] = home_url();
}

return $urls;
}

public function get_excluded($post_type)
public function is_excluded( $post_id = null )
{
$exclude = array();

if ( $post_type == 'page' and $id = get_option('page_on_front') ) { // use 'and' here for precedence of the assignement operator, thanks @kitchin
global $polylang,$sitepress; // Polylang and WPML compat
if ( isset($polylang) && is_object($polylang) && isset($polylang->model) && is_object($polylang->model) && method_exists($polylang->model, 'get_translations') )
$exclude += $polylang->model->get_translations('post', $id);
if ( isset($sitepress) && is_object($sitepress) && method_exists($sitepress, 'get_languages') && method_exists($sitepress, 'get_object_id') )
foreach ( array_keys ( $sitepress->get_languages(false,true) ) as $term )
$exclude[] = $sitepress->get_object_id($id,'page',false,$term);
// no ID, try and get it from global post object
if ( null == $post_id ) {
global $post;
if ( is_object($post) && isset($post->ID))
$post_id = $post->ID;
else
$exclude[] = $id;
return false;
}

return $exclude;
$excluded = get_post_meta($post_id,'_xmlsf_exclude',true) || in_array($post_id,$this->get_frontpages()) ? true : false;

return apply_filters( 'xmlsf_excluded', $excluded, $post_id );
}

public function is_allowed_domain($url)
public function is_allowed_domain( $url )
{
$domains = $this->get_domains();
$return = false;
Expand All @@ -669,7 +709,7 @@ public function is_allowed_domain($url)
}
}

return apply_filters( 'xmlsf_allowed_domain', $return );
return apply_filters( 'xmlsf_allowed_domain', $return, $url );
}

public function get_index_url( $sitemap = 'home', $type = false, $param = false )
Expand Down Expand Up @@ -715,18 +755,15 @@ public function get_language( $id )
// WPML compat
global $sitepress;
if ( isset($sitepress) && is_object($sitepress) && method_exists($sitepress, 'get_language_for_element') ) {
$post_type = get_query_var( 'post_type', 'post' );
$post_type = get_query_var( 'post_type', 'post' ); // is $post_type always an array here??
$language = $sitepress->get_language_for_element( $id, 'post_'.$post_type[0] );
//apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $id, 'element_type' => $post_type ) );
}

// Polylang
if ( taxonomy_exists('language') ) {
$lang = get_the_terms($id,'language');
if ( is_array($lang) ) {
$lang = reset($lang);
$language = is_object($lang) ? $lang->slug : $language;
}
if ( function_exists('pll_get_post_language') ) {
$lang = pll_get_post_language( $id, 'slug' );
$language = !empty($lang) ? $lang : $language;
}

return !empty($language) ? $language : $this->blog_language;
Expand Down Expand Up @@ -942,7 +979,7 @@ public function load_template_news()
load_template( dirname( __FILE__ ) . '/feed-sitemap-news.php' );
}

// set up the news sitemap template
// set up the custom sitemap template
public function load_template_custom()
{
load_template( dirname( __FILE__ ) . '/feed-sitemap-custom.php' );
Expand Down Expand Up @@ -1075,7 +1112,7 @@ public function clear_settings()
}
}

function cache_flush($new_status, $old_status)
function cache_flush( $new_status, $old_status )
{
// are we moving the post in or out of published status?
if ( $new_status == 'publish' || $old_status == 'publish' ) {
Expand Down Expand Up @@ -1142,7 +1179,7 @@ public function nginx_helper_purge_urls( $urls = array(), $redis = false )
* INITIALISATION
*/

public function upgrade($old_version)
public function upgrade( $old_version )
{
// rewrite rules not available on plugins_loaded
// and don't flush rules from init as Polylang chokes on that
Expand Down Expand Up @@ -1292,7 +1329,7 @@ public function admin_init()
include_once( dirname( __FILE__ ) . '/class-xmlsitemapfeed-admin.php' );
}

public function flush_rules($hard = false)
public function flush_rules( $hard = false )
{
// did you flush already?
if ($this->yes_mother)
Expand Down
12 changes: 3 additions & 9 deletions includes/feed-sitemap-post_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ';

global $xmlsf;
$post_type = get_query_var('post_type');

foreach ( $xmlsf->do_tags($post_type) as $tag => $setting )
foreach ( $xmlsf->do_tags( get_query_var('post_type') ) as $tag => $setting )
$$tag = $setting;

echo !empty($image) ? '
Expand All @@ -37,9 +36,6 @@
echo '">
';

// any ID's we need to exclude?
$excluded = $xmlsf->get_excluded($post_type);

// set empty sitemap flag
$have_posts = false;

Expand All @@ -48,11 +44,9 @@
while ( have_posts() ) :
the_post();

// check if page is in the exclusion list (like front page)
// check if page is in the exclusion list (like front page or post meta)
// or if we are not dealing with an external URL :: Thanks to Francois Deschenes :)
// or if post meta says "exclude me please"
$exclude = get_post_meta( $post->ID, '_xmlsf_exclude', true );
if ( !empty($exclude) || !$xmlsf->is_allowed_domain(get_permalink()) || in_array($post->ID, $excluded) )
if ( $xmlsf->is_excluded($post->ID)) || !$xmlsf->is_allowed_domain(get_permalink()) )
continue;

$have_posts = true;
Expand Down
8 changes: 5 additions & 3 deletions xml-sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin URI: http://status301.net/wordpress-plugins/xml-sitemap-feed/
Description: Feed the hungry spiders in compliance with the XML Sitemap and Google News protocols. Happy with the results? Please leave me a <strong><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=ravanhagen%40gmail%2ecom&item_name=XML%20Sitemap%20Feed&item_number=4%2e0&no_shipping=0&tax=0&bn=PP%2dDonationsBF&charset=UTF%2d8&lc=us">tip</a></strong> for continued development and support. Thanks :)
Text Domain: xml-sitemap-feed
Version: 4.7.2
Version: 4.7.3
Author: RavanH
Author URI: http://status301.net/
*/
Expand All @@ -30,7 +30,9 @@
* FILTERS
* xmlsf_defaults -> Filters the default array values for different option groups.
* xmlsf_allowed_domain -> Filters the response when checking the url against allowed domains.
* Can be true or false.
* Passes variable $url; must return true or false.
* xmlsf_excluded -> Filters the response when checking the post for exclusion flags.
* Passes variable $post_id; must return true or false.
* the_title_xmlsitemap -> Filters the Google News publication name, title and keywords
* plus the Image title and caption tags
* xmlsf_custom_urls -> Filters the custom urls array
Expand All @@ -50,7 +52,7 @@
* CONSTANTS
* -------------------- */

define('XMLSF_VERSION', '4.7.2');
define('XMLSF_VERSION', '4.7.3');

define('XMLSF_PLUGIN_BASENAME', plugin_basename(__FILE__));

Expand Down

0 comments on commit 37a46ba

Please sign in to comment.