Created
October 16, 2018 20:33
-
-
Save tripflex/a13cf9ece1d6cec01c16e3bf8be08721 to your computer and use it in GitHub Desktop.
WP Job Manager slow geocode existing listings missing geolocation data to overcome OVER_QUERY_LIMIT
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 | |
$smyles_enable_slow_geo = true; | |
function smyles_add_30_min_cron_schedule( $schedules ) { | |
$schedules['every_thirty_minutes'] = array( | |
'interval' => 1800, | |
'display' => __( 'Every 30 Minutes' ) | |
); | |
return $schedules; | |
} | |
add_filter( 'cron_schedules', 'smyles_add_30_min_cron_schedule' ); | |
if ( $smyles_enable_slow_geo && ! wp_next_scheduled( 'job_manager_slow_geocode' ) ) { | |
wp_schedule_event( time(), 'every_thirty_minutes', 'job_manager_slow_geocode' ); | |
} elseif( ! $smyles_enable_slow_geo ){ | |
$timestamp = wp_next_scheduled( 'job_manager_slow_geocode' ); | |
wp_unschedule_event( $timestamp, 'job_manager_slow_geocode' ); | |
} | |
add_action( 'job_manager_slow_geocode', 'smyles_do_geocoding_update' ); | |
//delete_option( 'smyles_job_manager_slow_geo_total_remaining' ); | |
//delete_option( 'smyles_slow_geo_in_progress' ); | |
function smyles_do_geocoding_update(){ | |
global $wpdb; | |
//if( wp_doing_ajax() && ! array_key_exists( 'action', $_POST ) && $_POST['action'] !== 'acm_execute_task' ){ | |
// return; | |
//} | |
$over_limit = get_transient( 'jm_geocode_over_query_limit' ); | |
$progress = get_option( 'smyles_slow_geo_in_progress', false ); | |
$total_remaining = get_option( 'smyles_job_manager_slow_geo_total_remaining', 1 ); | |
if( ! $progress ){ | |
$in_process = false; | |
} else { | |
$in_process = strtotime( '-10 minutes' ) < strtotime( $progress ); | |
} | |
// We were over API limit last time cron was run | |
if( $over_limit || $in_process || empty( $total_remaining ) ){ | |
return; | |
} | |
$prefix = $wpdb->prefix; | |
$posts = $wpdb->get_results( "SELECT {$prefix}_posts.* FROM {$prefix}_posts LEFT JOIN {$prefix}_postmeta ON ({$prefix}_posts.ID = {$prefix}_postmeta.post_id AND {$prefix}_postmeta.meta_key = 'geolocation_street' ) WHERE 1=1 AND ( {$prefix}_postmeta.post_id IS NULL ) AND {$prefix}_posts.post_type = 'job_listing' AND (({$prefix}_posts.post_status = 'publish')) GROUP BY {$prefix}_posts.ID ORDER BY {$prefix}_posts.post_date DESC" ); | |
$total_remaining = count( $posts ); | |
update_option( 'smyles_job_manager_slow_geo_total_remaining', $total_remaining ); | |
if( empty( $posts ) ){ | |
$timestamp = wp_next_scheduled( 'job_manager_slow_geocode' ); | |
wp_unschedule_event( $timestamp, 'job_manager_slow_geocode' ); | |
update_option( 'smyles_job_manager_slow_geo_total_remaining', 0 ); | |
} | |
foreach( $posts as $post ){ | |
$meta_value = get_post_meta( $post->ID, '_job_location', true ); | |
// Looks like an invalid job_location value, empty value, or does not contain a comma (which at least one should exist) | |
if( ! $meta_value || empty( $meta_value ) || strpos( $meta_value, ',' ) === FALSE ){ | |
$lat = get_post_meta( $post->ID, 'geolocation_lat', true ); | |
$long = get_post_meta( $post->ID, 'geolocation_long', true ); | |
// Verify we have valid geolocation lat and long values | |
if ( $lat && is_numeric( $lat ) && $long && is_numeric( $long ) ) { | |
// Set to array value for geocoding handling | |
$meta_value = array( | |
'lat' => $lat, | |
'long' => $long | |
); | |
} | |
} | |
update_option( 'smyles_slow_geo_in_progress', time() ); | |
do_action( 'job_manager_job_location_edited', $post->ID, $meta_value ); | |
$total_remaining--; | |
update_option( 'smyles_job_manager_slow_geo_total_remaining', $total_remaining ); | |
} | |
delete_option( 'smyles_slow_geo_in_progress' ); | |
} | |
add_filter( 'job_manager_geolocation_get_location_data', 'smyles_update_geolocated_count', 10, 2 ); | |
function smyles_update_geolocated_count( $address, $geo ){ | |
$existing = get_option( 'smyles_job_manager_slow_geo_count', 0 ); | |
update_option( 'smyles_job_manager_slow_geo_count', $existing + 1 ); | |
return $address; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is provided as a courtesy and for reference for others to use. Commonly WP Job Manager (especially when importing large number of listings), the geocoding stalls due to
OVER_QUERY_LIMIT
and basically all listings end up not having any geolocation data.This code will loop through every 30 minutes attempting to geocode any listings that do not have valid geolocation data on them. I do not provide support with this, and I only posted this to help other devs and help them to not have to figure out a way to do this themselves (although you do need to know PHP to know what this is doing and how to use it)