Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: double-term
- Description: Custom code for oieniuz's auto sales installation. Sets up sitemap links using two taxonomy terms. Requires Yoast SEO plugin to be active.
- Author: bcworkz
- Version: 0.10 beta
- License: GPL 2.0
- */
- /**
- * Requires Yoast WP SEO plugin to be installed and activated. This is not verified.
- * Adds custom sitemaps to Yoast sitemaps that include links with double terms. This sitemap type
- * in Yoast is 'dblterm', the sitemap URLS are something like dblterm-sitemap3.xml . The '3' could
- * be any integer. They are referenced in the root sitemap-index.xml generated by Yoast
- * Sitemap URLs are shown to be published 1/2 week ago, regardless of actual publish dates.
- */
- // Constants for use in custom Yoast sitemaps - adjust values as required
- // This plugin ignores the max. items set in Yoast. The value here is used.
- // To flush Yoast sitemap cache, change the Max items on Yoast's sitemap screen, then save
- // You must also reload the retrieved sitemap to flush browser cached versions
- /**
- * Max items. Per sitemap specifications, max is 50,000 minus $location_count
- * - maybe assume 48k max ?? (there may also be memory constraints)
- * - script will output more than specified to complete a full set of locations per model
- */
- define( 'MAX_ITEMS', 1000 );
- // Taxonomies for term sources
- define('MODELS', 'vehicle_model');
- define('LOCATIONS', 'vehicle_location');
- // Register "dblterm" sitemap type with Yoast sitemap object
- // this allows Yoast to handle redirects and sitemap caching :)
- add_action('init', 'dt_register_map');
- function dt_register_map() {
- if ( array_key_exists('wpseo_sitemaps', $GLOBALS )) {
- $sitemaps = $GLOBALS['wpseo_sitemaps'];
- $sitemaps->register_sitemap( 'dblterm', 'dt_gen_sitemap_dbl_terms');
- }
- }
- /**
- * generates XML sitemap content from 2 taxonomies/terms
- * used as callback to action 'wpseo_do_sitemap_dblterm'
- * ignores Yoast max. entries - The max. is hardcoded as constant MAX_ITEMS above.
- * filename to request: dblterm-sitemap#.xml where # is page number (any places: #, ##, ###, ...)
- */
- function dt_gen_sitemap_dbl_terms() {
- // assign constants to local vars
- $max_items = MAX_ITEMS;
- $vehicle_model = MODELS;
- $vehicle_location = LOCATIONS;
- $page = get_query_var( 'sitemap_n' ); //Yoast sitemap page # taken from filename requested
- if ( '' == $page ) $page = 1; // Assume page 1 if no query var
- // Yoast will send headers, set <?xml version, encoding, stylesheets, templates ? >
- $pages = '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
- // get terms through SQL queries
- $models = dt_alt_get_terms( $vehicle_model ); // each hides empty terms by default
- $locations = dt_alt_get_terms( $vehicle_location );
- $model_ct = count( $models );
- $location_ct = count( $locations );
- // determine parameters so page breaks occur after full set of locations are output
- $models_per_page = floor(($max_items/$location_ct) + 1 );
- $offset = ($page-1) * $models_per_page;
- if ( 0 > $offset ) $offset = 0; // safety function just in case something odd happens
- $limit = $offset + ($models_per_page * $location_ct);
- // do not surpass number of models available
- $limit = ( $model_ct < $limit ) ? $model_ct: $limit;
- $count = 0;
- // Loop through all term permutaions
- for ( $i=$offset; $i<$limit; $i++ ) {
- $model = $models[$i];
- foreach ( $locations as $location ) {
- $location2 = home_url( '/used-cars/location/' . $location . '/model/' . $model .'/' );
- // Assume all published 1/2 week ago
- $date = date_i18n('o-m-d\TH:i:sP', current_time('timestamp')-( 3.5 * DAY_IN_SECONDS ));
- $pages .= "\t<url>\n\t\t<loc>$location2</loc>\n\t\t<lastmod>$date</lastmod>\n\t</url>\n";
- $count++;
- }
- if ( $count >= $max_items) break;
- }
- // Get Yoast sitemap object and give it our sitemap
- $sitemaps = $GLOBALS['wpseo_sitemaps'];
- $sitemaps->set_sitemap( $pages . '</urlset>');
- return;
- }
- // Add dblterm-sitemap links to sitemap-index
- add_filter( 'wpseo_sitemap_index', 'dt_add_to_index' );
- function dt_add_to_index() {
- // assign constants to local vars
- $max_items = MAX_ITEMS;
- $vehicle_model = MODELS;
- $vehicle_location = LOCATIONS;
- // get term counts through SQL queries
- $models = dt_alt_get_terms( $vehicle_model ); // each hides empty terms by default
- $locations = dt_alt_get_terms( $vehicle_location );
- $model_ct = count( $models );
- $location_ct = count( $locations );
- // determine parameters so page breaks occur after full set of locations are output
- $models_per_page = floor(( $max_items / $location_ct ) + 1 );
- $max_page_idx = floor(( $model_ct / $models_per_page ) + 1 );
- $links = '';
- // Assume all published 1/2 week ago
- $date = date_i18n('o-m-d\TH:i:sP', current_time('timestamp')-( 3.5 * DAY_IN_SECONDS ));
- for ( $i=1; $i<=$max_page_idx; $i++ ) {
- $url = get_site_url( null, '/dblterm-sitemap' . (string)$i . '.xml');
- $links .= "\t<sitemap>\n\t\t<loc>$url</loc>\n\t\t<lastmod>$date</lastmod>\n\t</sitemap>\n";
- }
- return $links;
- }
- /**
- * Gets all terms for taxonomy $tax
- * Hides empty terms unless hiding algorithm is commented out (@ line 144)
- * Returns an array of term slugs
- */
- function dt_alt_get_terms( $tax ) {
- /* // The original alt_get_terms(). Direct query ought to be a bit faster??
- $hide_empty = true;
- $term_query = new WP_Term_Query(['taxonomy'=> $tax, 'fields'=>'id=>slug', 'hide_empty'=> $hide_empty,]);
- $terms = $term_query->get_terms();
- $no_empties = array();
- foreach ( $terms as $term ) { $no_empties[] = $term->slug; }
- */
- global $wpdb;
- $terms = $wpdb->get_results( $wpdb->prepare("SELECT t.term_id, t.slug, tt.count, tt.taxonomy FROM $wpdb->terms AS t
- INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
- WHERE tt.taxonomy IN ('%s') ORDER BY t.slug ASC;",
- $tax
- ));
- // Remove empty terms unless children are not empty (hide_empty = true)
- // Algorithm copied from WordPress core code (it's OK, WP is GPL licensed)
- // To show empties add astrisk between two slashes at beginning of this line
- if ( is_array( $terms ) ) {
- foreach ( $terms as $k => $term ) {
- if ( ! $term->count ) {
- $children = get_term_children( $term->term_id, $term->taxonomy );
- if ( is_array( $children ) ) {
- foreach ( $children as $child_id ) {
- $child = get_term( $child_id, $term->taxonomy );
- if ( $child->count ) {
- continue 2;
- }
- }
- }
- // It really is empty.
- unset( $terms[ $k ] );
- }
- }
- }
- // end remove empty terms------------------------ */
- //make array indices sequential, save value of slug only
- $no_empties = array();
- foreach ( $terms as $term ) { $no_empties[] = $term->slug; }
- return $no_empties;
- }
- /**
- * Transient Garbage Collection - WP only does this on DB upgrades
- *
- * Deletes all expired transients.
- * The multi-table delete syntax is used to delete the transient record from table a,
- * and the corresponding transient_timeout record from table b, though in fact both
- * are in the same table.
- */
- // only setup event on activation
- register_activation_hook(__FILE__, 'dt_activation');
- function dt_activation() {
- if (! wp_next_scheduled ( 'dt_event' )) {
- wp_schedule_event(time(), 'daily', 'dt_event');
- }
- }
- add_action('dt_event', 'dt_do_this_daily');
- function dt_do_this_daily() {
- global $wpdb;
- $time = time();
- //from upgrade_network()
- $wpdb->query("DELETE a, b FROM $wpdb->options a, $wpdb->options b WHERE
- a.option_name LIKE '\_transient\_%' AND
- a.option_name NOT LIKE '\_transient\_timeout\_%' AND
- b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
- AND b.option_value < $time");
- return;
- }
- // remove event hook on deactivation
- register_deactivation_hook(__FILE__, 'dt_deactivation');
- function dt_deactivation() {
- wp_clear_scheduled_hook('dt_event');
- }
Advertisement
Add Comment
Please, Sign In to add comment