Last active
April 16, 2024 23:26
-
-
Save wpmudev-sls/518c85b8a0300d1d820141c55fcd739f to your computer and use it in GitHub Desktop.
[SmartCrawl Pro] Add Ultimate Member user profiles 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 Pro] Add Ultimate Member user profiles to sitemap | |
* Description: Adds a new "members-sitemap" item to the sitemap index, which contains URLs from the Ultimate Member user profiles | |
* Author: Anderson Salas @ WPMUDEV | |
* Task: SLS-6029 | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
add_action( 'plugins_loaded', function() { | |
if ( ! defined( 'SMARTCRAWL_VERSION' ) ) { | |
return; // SmartCrawl is not installed/enabled. | |
} | |
if ( ! defined( 'UM_VERSION' ) ) { | |
return; // Ultimate Member is not installed/enabled. | |
} | |
if ( class_exists( 'WPMUDEV_Ultimate_Member_Profile' ) ) { | |
return; // Already loaded. | |
} | |
class WPMUDEV_Ultimate_Member_Profile extends \SmartCrawl\Sitemaps\Query { | |
use \SmartCrawl\Singleton; | |
const TYPE = 'members'; | |
public static $loaded = FALSE; | |
/** | |
* @return string[] | |
*/ | |
public function get_supported_types() { | |
return array( self::TYPE ); | |
} | |
private function get_users_query( $count = false, $page_number = 0 ) { | |
$args = [ | |
'fields' => [ 'ID', 'user_registered' ], | |
'meta_query' => [ | |
[ | |
'key' => 'account_status', | |
'value' => 'approved', | |
'compare' => '=', | |
], | |
], | |
]; | |
if ( ! $count ) { | |
$args['number'] = SmartCrawl\Sitemaps\Utils::get_items_per_sitemap(); | |
$args['paged'] = $page_number; | |
} | |
return new WP_User_Query( $args ); | |
} | |
public function get_item_count( $type = '' ) { | |
$users = $this->get_users_query( true ); | |
return $users->get_total(); | |
} | |
/** | |
* @return array|Item[] | |
*/ | |
public function get_items( $type = '', $page_number = 0 ) { | |
$query = $this->get_users_query( false, $page_number ); | |
$users = $query->get_results(); | |
$items = []; | |
foreach ( $users as $user ) { | |
if ( UM()->user()->is_profile_noindex( $user->ID ) ) { | |
continue; | |
} | |
$item = new \SmartCrawl\Sitemaps\General\Item(); | |
$item->set_location( um_user_profile_url( $user->ID ) ) | |
->set_last_modified( strtotime( $user->user_registered ) ) | |
->set_images( [] ); | |
$items[] = $item; | |
} | |
return $items; | |
} | |
/** | |
* @return string | |
*/ | |
public function get_filter_prefix() { | |
return 'wds-sitemap-users'; | |
} | |
/** | |
* @return array | |
*/ | |
private function get_options() { | |
return \SmartCrawl\Settings::get_options(); | |
} | |
} | |
add_filter( 'smartcrawl_sitemaps_general_get_queries', function( $queries ) { | |
if ( ! WPMUDEV_Ultimate_Member_Profile::$loaded ) { | |
$queries[] = new WPMUDEV_Ultimate_Member_Profile(); | |
WPMUDEV_Ultimate_Member_Profile::$loaded = TRUE; | |
} | |
return $queries; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment