Instantly share code, notes, and snippets.
Last active
January 9, 2020 20:09
-
Save wpmudev-sls/6021a6d17b3c9ad6f5b33fbabb55a9da to your computer and use it in GitHub Desktop.
[SmartCrawl] Add author pages to sitemap
This file contains 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: [SmartCrawl] Add author pages to sitemap | |
* Description: [SmartCrawl] Add author pages to sitemap - 1155973042612901 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action( 'after_setup_theme', 'wpmudev_sc_add_author_pages_to_sitemap', 100 ); | |
function wpmudev_sc_add_author_pages_to_sitemap() { | |
// if you want to copy to your child theme's functions.php file, please start here: | |
if ( defined( 'SMARTCRAWL_VERSION' ) && SMARTCRAWL_VERSION && class_exists('Smartcrawl_Settings') ) { | |
$settings = Smartcrawl_Settings::get_specific_options( 'wds_settings_options' ); | |
if( ! empty( $settings['sitemap'] ) ){ | |
class WPMUDEV_SMC_Custom_Sitemap{ | |
/** | |
* Please enter exclude authors here | |
* if it's MU site, you can use this format: | |
* array( | |
* [site-id-1] => array( [author-id-1], [author-id-2] ) | |
* ) | |
* To update the site map, you can go to SmartCrawl -> Sitemap and try to Save Settings again | |
* @var array | |
*/ | |
private $exclude_authors = array(); | |
private static $_author_pages_key = 'wpmudev_smc_author_pages'; | |
public function __construct(){ | |
add_filter( 'option_wds-sitemap-extras', array( $this, 'add_author_pages_to_sitemaps' ) ); | |
add_filter('pre_update_option__wds-sitemap-extras', array( $this, 'remove_author_pages_before_update')); | |
add_action( 'transition_post_status', array( $this, 'maybe_add_author_page_to_sitemap' ), 999, 3 ); | |
add_action( 'update_option__ba_eas_author_base', array( $this, 'after_update_author_base' ) ); | |
add_action( 'update_option_wds_sitemap_options', array( $this, 'clean_cache_author_pages' ), 0 ); | |
add_action( 'update_site_option_wds_sitemap_options', array( $this, 'clean_cache_author_pages' ), 0 ); | |
} | |
public function clean_cache_author_pages(){ | |
delete_transient( self::$_author_pages_key ); | |
} | |
public function remove_author_pages_before_update( $extra_urls ){ | |
$author_pages = $this->get_author_pages(); | |
if( $author_pages ){ | |
$extra_urls = array_diff( $extra_urls, $author_pages ); | |
} | |
return $extra_urls; | |
} | |
public function get_exclude_authors(){ | |
if( $this->exclude_authors ){ | |
if( is_multisite() ){ | |
$current_bog_id = get_current_blog_id(); | |
if( isset( $this->exclude_authors[ $current_bog_id ] ) ){ | |
$this->exclude_authors = $this->exclude_authors[ $current_bog_id ]; | |
} | |
} | |
} | |
return $this->exclude_authors; | |
} | |
public function get_author_pages(){ | |
$author_pages = get_transient( self::$_author_pages_key, array() ); | |
if( ! $author_pages ){ | |
$args = array( | |
'orderby' => 'name', | |
'order' => 'ASC', | |
// 'number' => null, | |
'exclude_admin' => true, | |
'hide_empty' => true, | |
'exclude' => '', | |
'include' => '' | |
); | |
$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) ); | |
$query_args['fields'] = 'ids'; | |
$authors = get_users( $query_args ); | |
if( $args['hide_empty'] ){ | |
$author_count = array(); | |
global $wpdb; | |
foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . ' GROUP BY post_author' ) as $row ) { | |
$author_count[ $row->post_author ] = $row->count; | |
} | |
} | |
$exclude_authors = $this->get_exclude_authors(); | |
foreach ( $authors as $author_id ) { | |
// continue if the author does not allow | |
if( $exclude_authors && in_array( $author_id, $exclude_authors ) ){ | |
continue; | |
} | |
if( $args['hide_empty'] && empty( $author_count[ $author_id ] ) ){ | |
continue; | |
} | |
$author = get_userdata( $author_id ); | |
if ( $args['exclude_admin'] && 'admin' === $author->display_name ) { | |
continue; | |
} | |
$author_url = get_author_posts_url( $author_id ); | |
if( ! in_array( $author_pages[ $author_url ] ) ){ | |
$author_pages[] = $author_url; | |
} | |
} | |
set_transient( self::$_author_pages_key, $author_pages, WEEK_IN_SECONDS ); | |
} | |
return $author_pages; | |
} | |
public function add_author_pages_to_sitemaps( $extra_urls ){ | |
// source https://codex.wordpress.org/Function_Reference/wp_list_authors | |
if( empty( $extra_urls ) ){ | |
$extra_urls = array(); | |
} | |
$author_pages = $this->get_author_pages(); | |
if( $author_pages ){ | |
$extra_urls = array_unique( array_merge( $extra_urls, $author_pages ) ); | |
} | |
return $extra_urls; | |
} | |
public function update_sitemap(){ | |
return Smartcrawl_Controller_Sitemap::get()->invalidate_sitemap_cache(); | |
} | |
public function maybe_add_author_page_to_sitemap( $new_status, $old_status, $post ){ | |
if( 'publish' === $new_status ){ | |
if( $exclude_authors = $this->get_exclude_authors() && in_array( $post->post_author, $exclude_authors ) ){ | |
return; | |
} | |
$post_type_obj = get_post_type_object( $post->post_type ); | |
if( ! empty( $post_type_obj->public ) && ! empty( $post_type_obj->rewrite ) && ! empty( $post_type_obj->show_ui ) ){ | |
$this->add_author_page( $post->post_author ); | |
// update sitemap | |
$this->update_sitemap(); | |
} | |
} | |
} | |
public function add_author_page( $author_id ){ | |
$author_url = get_author_posts_url( $author_id ); | |
if( $author_url ){ | |
$author_pages = get_option('wpmudev_smc_author_pages'); | |
if( ! in_array( $author_url, $author_pages ) ){ | |
$author_pages[] = $author_url; | |
set_transient( self::$_author_pages_key, $author_pages, WEEK_IN_SECONDS ); | |
} | |
} | |
} | |
public function after_update_author_base(){ | |
$this->clean_cache_author_pages(); | |
$this->update_sitemap(); | |
} | |
} | |
$run = new WPMUDEV_SMC_Custom_Sitemap; | |
} | |
} | |
// end here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment