Last active
June 12, 2024 03:11
-
-
Save wpmudev-sls/958f738080583d31f04cb505ddb0e948 to your computer and use it in GitHub Desktop.
[SmartCrawl Pro] Add Sabai Directory listings 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 Sabai Directory listings to sitemap | |
* Description: Adds a new "directory-listing" item to the sitemap index, which contains URLs from the Sabai Directory listings | |
* Author: Anderson Salas @ WPMUDEV | |
* Task: SLS-6159 | |
* 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( 'SABAI_PACKAGE_DIRECTORY_PATH' ) ) { | |
return; // Sabai Directory is not installed/enabled. | |
} | |
if ( class_exists( 'WPMUDEV_Sabai_Directory_Listing' ) ) { | |
return; // Already loaded. | |
} | |
class WPMUDEV_Sabai_Directory_Listing extends \SmartCrawl\Sitemaps\Query { | |
use \SmartCrawl\Singleton; | |
const TYPE = 'directory-listing'; | |
public static $loaded = FALSE; | |
/** | |
* @return string[] | |
*/ | |
public function get_supported_types() { | |
return array( self::TYPE ); | |
} | |
private function get_listings( $count = false, $page_number = 0 ) { | |
global $wpdb; | |
$p = $wpdb->prefix; | |
$s = ! $count ? 'l.post_slug slug, l.post_created created_at' : 'count(*) c'; | |
$q = "SELECT $s FROM {$p}sabai_content_post l LEFT JOIN {$p}sabai_entity_field_directory_claim c ON c.entity_id = l.post_id | |
WHERE l.post_status = %s AND l.post_entity_bundle_type = %s AND (c.expires_at = 0 OR ISNULL(c.expires_at) OR c.expires_at > %d)"; | |
if ( ! $count ) { | |
$pp = SmartCrawl\Sitemaps\Utils::get_items_per_sitemap(); | |
$q .= ' LIMIT ' . ( ( $page_number - 1 ) * $pp ) . ', ' . $pp; | |
} | |
$q = $wpdb->prepare( $q, [ 'published', 'directory_listing', time() ] ); | |
$r = $wpdb->get_results( $q, ARRAY_A ); | |
return ! $count ? $r : ( $r[0]['c'] ?? 0 ); | |
} | |
public function get_item_count( $type = '' ) { | |
return $this->get_listings( true ); | |
} | |
/** | |
* @return array|Item[] | |
*/ | |
public function get_items( $type = '', $page_number = 0 ) { | |
$listings = $this->get_listings( false, $page_number ); | |
$items = []; | |
$url = get_home_url() . '/directory/listing/'; | |
foreach ( $listings as $listing ) { | |
$item = new \SmartCrawl\Sitemaps\General\Item(); | |
$item->set_location( $url . $listing['slug'] ) | |
->set_last_modified( $listing['created_at'] ) | |
->set_images( [] ); | |
$items[] = $item; | |
} | |
return $items; | |
} | |
/** | |
* @return string | |
*/ | |
public function get_filter_prefix() { | |
return 'wds-sitemap-directory-listings'; | |
} | |
/** | |
* @return array | |
*/ | |
private function get_options() { | |
return \SmartCrawl\Settings::get_options(); | |
} | |
} | |
add_filter( 'smartcrawl_sitemaps_general_get_queries', function( $queries ) { | |
if ( ! WPMUDEV_Sabai_Directory_Listing::$loaded ) { | |
$queries[] = new WPMUDEV_Sabai_Directory_Listing(); | |
WPMUDEV_Sabai_Directory_Listing::$loaded = TRUE; | |
} | |
return $queries; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment