Created
February 16, 2014 12:17
-
-
Save nikolov-tmw/9033376 to your computer and use it in GitHub Desktop.
Extension to the Google XML Sitemaps plugin(version 4.0+) that lists all media attachments in the sitemap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Google XML Sitemaps - Media support | |
Description: This plugin will display all of your media in the automatically generated sitemap created by the Google XML Sitemaps plugin v 4.0+ | |
Version: 1 | |
Author: Nikola Nikolov | |
Author URI: http://paiyakdev.com/ | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
return; | |
} | |
add_action( 'sm_build_index', 'gsmg_register_extension', 10 ); | |
add_action( 'sm_build_content', 'gsmg_register_extension', 10 ); | |
function gsmg_register_extension() { | |
static $extension = null; | |
if ( is_null( $extension ) && class_exists( 'GoogleSitemapGeneratorStandardBuilder' ) ) { | |
/** | |
* | |
*/ | |
class GSMG_Media extends GoogleSitemapGeneratorStandardBuilder { | |
/** | |
* Override the default construct method for GoogleSitemapGeneratorStandardBuilder | |
* | |
* This way we add our actions with lower(higher index) priority, | |
* so that the media list comes last | |
*/ | |
function __construct() { | |
add_action( "sm_build_index", array( $this, "main" ), 15, 1 ); | |
add_action( "sm_build_content", array( $this, "render_attachments" ), 15, 3 ); | |
} | |
public function main( $gsg ) { | |
global $wpdb; | |
$wpdb->show_errors(); | |
$q = "SELECT | |
YEAR(p.post_date) AS `year`, | |
MONTH(p.post_date) AS `month`, | |
COUNT(p.ID) AS `numposts`, | |
MAX(p.post_date) as `last_mod` | |
FROM | |
{$wpdb->posts} p | |
WHERE | |
p.post_password = '' | |
AND p.post_type = 'attachment' | |
AND p.post_status = 'inherit' | |
AND ( | |
p.post_parent = 0 OR 'publish' = ( SELECT post_status FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_parent=p.ID ) | |
) | |
GROUP BY | |
YEAR(p.post_date), | |
MONTH(p.post_date) | |
ORDER BY | |
p.post_date DESC"; | |
$posts = $wpdb->get_results( $q ); | |
if ( $posts ) { | |
foreach ( $posts as $post ) { | |
$gsg->AddSitemap( "media", sprintf( "%04d-%02d", $post->year, $post->month ), $gsg->GetTimestampFromMySql( $post->last_mod ) ); | |
} | |
} | |
} | |
public function render_attachments( $gsg, $type, $params ) { | |
if ( 'media' == $type && preg_match( '/^([0-9]{4})\-([0-9]{2})$/', $params, $matches ) ) { | |
$year = $matches[1]; | |
$month = $matches[2]; | |
//All comments as an asso. Array (postID=>commentCount) | |
$comments = array(); | |
//Full number of comments | |
$commentCount = 0; | |
$qp = $this->BuildPostQuery( $gsg, 'attachment' ); | |
$qp['year'] = $year; | |
$qp['monthnum'] = $month; | |
//Don't retrieve and update meta values and taxonomy terms if they are not used in the permalink | |
$struct = get_option('permalink_structure'); | |
if ( strpos( $struct, "%category%" ) === false && strpos( $struct, "%tag%" ) === false ) { | |
$qp['update_post_term_cache'] = false; | |
} | |
$qp['update_post_meta_cache'] = false; | |
//Add filter to remove password protected posts | |
add_filter( 'posts_search', array( $this, 'FilterPassword' ), 10, 1 ); | |
//Add filter to filter the fields | |
add_filter( 'posts_fields', array( $this, 'FilterFields' ), 10, 1 ); | |
$posts = get_posts( $qp ); | |
//Remove the filter again | |
remove_filter( 'posts_where', array( $this, 'FilterPassword' ), 10, 1 ); | |
remove_filter( 'posts_fields', array( $this, 'FilterFields' ), 10, 1 ); | |
if ( $postCount = count( $posts ) > 0 ) { | |
$prioProvider = NULL; | |
if ( '' != $gsg->GetOption( "b_prio_provider" ) ) { | |
$providerClass = $gsg->GetOption( 'b_prio_provider' ); | |
$prioProvider = new $providerClass( 0, $postCount ); | |
} | |
//Default priorities | |
$default_prio_posts = $gsg->GetOption( 'pr_posts' ); | |
//Change frequencies | |
$cf_posts = $gsg->GetOption( 'cf_posts' ); | |
//Minimum priority | |
$minPrio = $gsg->GetOption( 'pr_posts_min' ); | |
foreach ( $posts as $post ) { | |
$permalink = get_permalink( $post->ID ); | |
$parent_status = 0 == $post->post_parent ? 'publish' : get_post_status( $post->post_parent ); | |
//Exclude the home page and placeholder items by some plugins... | |
if ( ! empty( $permalink ) && $permalink != "#" && 'publish' == $parent_status ) { | |
//Default Priority if auto calc is disabled | |
$prio = $default_prio_posts; | |
$time = $post->post_modified_gmt && $post->post_modified_gmt != '0000-00-00 00:00:00' ? $post->post_modified_gmt : $post->post_date_gmt; | |
$gsg->AddUrl( $permalink, $gsg->GetTimestampFromMySql( $time ), $cf_posts, $prio, $post->ID ); | |
} | |
} | |
} | |
} | |
} | |
} | |
$extension = new GSMG_Media(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment