Skip to content

Commit

Permalink
See pewresearch/pewresearch-org@1b84f2c from refs/heads/release/5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
prcdevgitbot committed Feb 6, 2024
1 parent 7348418 commit c762df1
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/on-deploy__build-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Deploy static ./.docs content to GitHub Pages
on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]
branches: ["develop"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down
2 changes: 2 additions & 0 deletions includes/class-platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private function load_dependencies() {
$this->include('schema-meta/class-schema-meta.php');
$this->include('search/class-search.php');
$this->include('short-reads/class-short-reads.php');
$this->include('sitemaps/class-sitemaps.php');
$this->include('slack-bot/class-slack-bot.php');
$this->include('social/class-social.php');
$this->include('staff-bylines/class-staff-bylines.php');
Expand Down Expand Up @@ -192,6 +193,7 @@ function() {
new Schema_Meta( $this->get_version(), $this->get_loader() );
new Search( $this->get_version(), $this->get_loader() );
new Short_Reads( $this->get_version(), $this->get_loader() );
new Sitemaps( $this->get_version(), $this->get_loader() );
new Slack_Bot( $this->get_version(), $this->get_loader() );
new Social( $this->get_version(), $this->get_loader() );
new Staff_Bylines( $this->get_version(), $this->get_loader() );
Expand Down
104 changes: 104 additions & 0 deletions includes/sitemaps/class-sitemap-provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
namespace PRC\Platform;
use WP_Sitemaps_Provider;

class PRC_Sitemaps_Provider extends WP_Sitemaps_Provider {
public $postTypes = array();

public function __construct( $name, $postTypes = array( 'stub' ), $taxQuery = null ) {
$this->name = $name;
$this->postTypes = $postTypes;
$this->object_type = 'post';
$this->tax_query = $taxQuery;
$this->image_mime_types = array( 'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon' );
}

private function queryArgs( $page_num ) {
$args = array(
'post_type' => $this->postTypes,
'post_status' => array( 'inherit', 'publish' ),
'posts_per_page' => ( 'news' === $this->name ) ? 20 : 500,
'orderby' => 'post_date',
'order' => 'DESC',
'paged' => $page_num,
'post_mime_type' => ( 'images' === $this->name ) ? $this->image_mime_types : null,
'tax_query' => ( null !== $this->tax_query ) ? array( $this->tax_query ) : null
);

//Do not return child posts
if ( 'news' === $this->name ) {
$args['post_parent'] = 0;
}

return $args;
}

public function get_base_tags( $post ) {
return array(
'loc' => ( 'stub' === $post->post_type ) ? get_post_meta( $post->ID, '_redirect', true ) : get_the_permalink( $post ),
'lastmod' => get_the_modified_time( 'c', $post ),
'ID' => $post->ID
);
}

public function get_news_tags( $post ) {
return array(
'publication_date' => get_the_date( 'Y-m-d', $post ),
'title' => get_the_title( $post )
);
}

public function get_image_tags( $post ) {
return array(
'loc' => $post->guid,
'caption' => 'Nothing',
'title' => 'Nothing'
);
}

public function get_url_list( $page_num, $post_type = '' ) {
$query = new WP_Query( $this->queryArgs( $page_num ) );
$urlList = array();

if ('images' === $this->name ) {
$imageList = array();
foreach ( $query->posts as $post ) {
if ( 0 !== $post->post_parent ) {
$imageList[$post->post_parent][] = $this->get_image_tags( $post );
}
}
foreach ( $imageList as $k => $v ) {
$post = get_post( $k );
$sitemapEntry = $this->get_base_tags( $post );
$sitemapEntry['image'] = $v;
$sitemapEntry = apply_filters( 'wp_sitemaps_posts_entry', $sitemapEntry, $post, $post_type );
$urlList[] = $sitemapEntry;
}
} else {
foreach ( $query->posts as $post ) {
$sitemapEntry = $this->get_base_tags( $post );

if ( 'news' === $this->name ) {
$sitemapEntry['news'] = $this->get_news_tags( $post );
}

$sitemapEntry = apply_filters( 'wp_sitemaps_posts_entry', $sitemapEntry, $post, $post_type );
$urlList[] = $sitemapEntry;
}
}

return array(
'news' => ('news' === $this->name) ? true : false,
'list' => $urlList
);
}

public function get_max_num_pages( $post_type = '' ) {
$args = $this->queryArgs( 0 );
$args['fields'] = 'ids';
$args['no_found_rows'] = false;

$query = new WP_Query( $args );
return ( 'news' === $this->name ) ? 1 : $query->max_num_pages;
}
}
56 changes: 56 additions & 0 deletions includes/sitemaps/class-sitemap-renderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace PRC\Platform;
use WP_Sitemaps_Renderer;
use SimpleXMLElement;

class PRC_Sitemaps_Renderer extends WP_Sitemaps_Renderer {
/**
* Gets XML for a sitemap.
*
* @since 5.5.0
*
* @param array $url_list Array of URLs for a sitemap.
* @return string|false A well-formed XML string for a sitemap index. False on error.
*/
public function get_sitemap_xml( $url_list ) {
$news_schema = "http://www.google.com/schemas/sitemap-news/0.9";
$urlset = new SimpleXMLElement(
sprintf(
'%1$s%2$s%3$s',
'<?xml version="1.0" encoding="UTF-8" ?>',
$this->stylesheet,
( $url_list['news'] ) ? '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="' . $news_schema . '"/>' :
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />'
)
);

if ( isset( $url_list['list'] ) ) {
$url_list = $url_list['list'];
}

foreach ( $url_list as $url_item ) {
$url = $urlset->addChild( 'url' );

// Add each element as a child node to the <url> entry.
foreach ( $url_item as $name => $value ) {
if ( 'loc' === $name ) {
$url->addChild( $name, esc_url( trailingslashit( set_url_scheme( $value, 'https') ) ) );
//add news-specific tags
} elseif ( 'news' === $name ) {
$news = $url->addChild( 'news:news', null, $news_schema );
$publication = $news->addChild( 'news:publication', null, $news_schema );
$publication->addChild( 'news:name', esc_xml( 'Pew Research Center' ), $news_schema );
$publication->addChild( 'news:language', esc_xml( 'en' ), $news_schema );
$news->addChild( 'news:publication_date', esc_xml( $value['publication_date'] ), $news_schema );
$news->addChild( 'news:title', esc_xml( $value['title'] ), $news_schema );
} elseif ( 'ID' === $name ) {
$url->addAttribute( 'id', esc_xml( $value ) );
} else {
$url->addChild( $name, esc_xml( $value ) );
}
}
}

return $urlset->asXML();
}
}
77 changes: 77 additions & 0 deletions includes/sitemaps/class-sitemaps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
namespace PRC\Platform;

class Sitemaps {
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;

public static $handle = 'prc-platform-sitemaps';

/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $version, $loader ) {
$this->version = $version;
$this->init($loader);
require_once plugin_dir_path( __FILE__ ) . 'class-sitemap-provider.php';
require_once plugin_dir_path( __FILE__ ) . 'class-sitemap-renderer.php';
}

public function init( $loader = null ) {
if ( null !== $loader ) {
$loader->add_filter('wp_sitemaps_enabled', $this, 'enable_sitemap');
$loader->add_filter('wp_sitemaps_taxonomies', $this, 'set_available_taxonomies');
$loader->add_filter('wp_sitemaps_add_provider', $this, 'disable_other_sitemaps', 10, 2);
$loader->add_action('init', $this, 'register_sitemaps');
$loader->add_action('wp_sitemap_init', $this, 'render_sitemaps');
}
}

public function enable_sitemap($enabled) {
return true;
}

public function set_available_taxonomies ( $taxonomies ) {
return array_intersect_key( $taxonomies, array_flip( array( 'category' ) ) );
}

public function disable_other_sitemaps( $provider, $name ) {
if ( !in_array( $name, array( 'primary', 'news', 'taxonomies', 'images' ) ) ) {
return false;
}
return $provider;
}

public function register_sitemaps() {
// Publish News Map
$news_sitemap = new PRC_Sitemaps_Provider(
'news',
array( 'stub' ),
array(
'taxonomy' => 'formats',
'field' => 'slug',
'terms' => array( 'short-read', 'report' )
)
);
wp_register_sitemap_provider( 'news', $news_sitemap );

// Publish Primary Map
$primary_sitemap = new PRC_Sitemaps_Provider( 'primary' );
wp_register_sitemap_provider( 'primary', $primary_sitemap );
}

public function render_sitemaps( $sitemaps ) {
$sitemaps->renderer = new PRC_Sitemaps_Renderer();
}

}

0 comments on commit c762df1

Please sign in to comment.