
How Should You Prepare for Autonomous Cars?
Many insurance companies recognize the challenges that self-driving cars pose to the insurance industry, but most have made only tentative efforts to address the…
Read Morephp
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( !post_type_exists('location') ) {
function locations_init() {
$labels = array(
'name' => _x( 'Locations', 'post type general name' ),
'singular_name' => _x( 'Location', 'post type singular name' ),
'add_new' => _x( 'Add New', 'location' ),
'add_new_item' => __( 'Add New Location' ),
'edit_item' => __( 'Edit Location' ),
'new_item' => __( 'New Location' ),
'all_items' => __( 'All Locations' ),
'view_item' => __( 'View Location' ),
'search_items' => __( 'Search Locations' ),
'not_found' => __( 'No locations found' ),
'not_found_in_trash' => __( 'No locations found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Locations'
);
$args = array(
'labels' => $labels,
'description' => 'PrideStaff Locations',
'public' => true,
'menu_icon' => 'dashicons-location',
'menu_position' => 5,
'supports' => array( 'title', 'editor' ),
'has_archive' => false,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => false,
'capability_type' => 'post',
'hierarchical' => false,
);
register_post_type( 'location', $args );
}
add_action( 'init', 'locations_init' ); }
function location_rewrites() {
add_rewrite_rule( '([^/]*)', 'index.php?post_type=location&name=$matches[1]', 'bottom'); } add_action( 'init', 'location_rewrites' );
function location_permalinks( $post_link, $post, $leavename ) {
if ( isset( $post->post_type ) && 'location' == $post->post_type ) {
$post_link = home_url( $post->post_name );
}
return $post_link;
}
add_filter( 'post_type_link', 'location_permalinks', 10, 3 );
function location_prevent_slug_duplicates( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
$check_post_types = array(
'post',
'page',
'location'
);
if ( ! in_array( $post_type, $check_post_types ) ) {
return $slug;
}
if ( 'location' == $post_type ) {
// Saving a location post, check for duplicates in POST or PAGE post types
$post_match = get_page_by_path( $slug, 'OBJECT', 'post' );
$page_match = get_page_by_path( $slug, 'OBJECT', 'page' );
if ( $post_match || $page_match ) {
$slug .= '-duplicate';
}
} else {
// Saving a POST or PAGE, check for duplicates in location post type
$location_match = get_page_by_path( $slug, 'OBJECT', 'location' );
if ( $location_match ) {
$slug .= '-duplicate';
}
}
return $slug;
}
add_filter( 'wp_unique_post_slug', 'location_prevent_slug_duplicates', 10, 6 );
function locations_register_acf() {
acf_add_local_field_group(
array (
'key' => 'location_post_group',
'title' => 'Location Details',
'menu_order' => 0,
'fields' => array (
array( # Location ID
'key' => 'location_id',
'label' => 'Location ID',
'name' => 'location_id',
'type' => 'text',
), # End Location ID
array( # Haleymail ID
'key' => 'haleymail_id',
'label' => 'Haleymail ID',
'name' => 'haleymail_id',
'type' => 'text',
), # End Haleymail ID
array( # Haleymail URL (Talent)
'key' => 'haleymail_url',
'label' => 'Haleymail URL (Talent)',
'name' => 'haleymail_url',
'type' => 'text',
), # End Haleymail URL (Talent)
array( # Haleymail URL (Client)
'key' => 'haleymail_url_client',
'label' => 'Haleymail URL (Client)',
'name' => 'haleymail_url_client',
'type' => 'text',
), # End Haleymail URL (Client)
array( # Repeater
'key' => 'badges_repeater',
'label' => 'Badges',
'name' => 'badges_repeater',
'type' => 'repeater',
'layout' => 'block',
'button_label' => 'Add Badge',
'sub_fields' => array( # sub_fields
array( # Image
'key' => 'badge_image',
'label' => 'Badge Image',
'name' => 'badge_image',
'type' => 'image',
'preview_size' => 'thumbnail',
'return_format' => 'id',
), # End Image
), # End sub_fields repeater
), # End repeater
array( # Featured Image
'key' => 'featured_image',
'label' => 'Featured Image',
'name' => 'featured_image',
'type' => 'image',
'preview_size' => 'blog-thumbnail',
'return_format' => 'id',
), # End Featured Image
array( # Street 1
'key' => 'location_street1',
'label' => 'Street 1',
'name' => 'location_street1',
'type' => 'text',
), # End Street 1
array( # Street 2
'key' => 'location_street2',
'label' => 'Street 2',
'name' => 'location_street2',
'type' => 'text',
), # End Street 2
array( # City
'key' => 'location_city',
'label' => 'City',
'name' => 'location_city',
'type' => 'text',
), # End City
array( # State
'key' => 'location_state',
'label' => 'State',
'name' => 'location_state',
'type' => 'select',
'choices' => array(
'AL'=>'Alabama',
'AK'=>'Alaska',
'AZ'=>'Arizona',
'AR'=>'Arkansas',
'CA'=>'California',
'CO'=>'Colorado',
'CT'=>'Connecticut',
'DE'=>'Delaware',
'DC'=>'District of Columbia',
'FL'=>'Florida',
'GA'=>'Georgia',
'HI'=>'Hawaii',
'ID'=>'Idaho',
'IL'=>'Illinois',
'IN'=>'Indiana',
'IA'=>'Iowa',
'KS'=>'Kansas',
'KY'=>'Kentucky',
'LA'=>'Louisiana',
'ME'=>'Maine',
'MD'=>'Maryland',
'MA'=>'Massachusetts',
'MI'=>'Michigan',
'MN'=>'Minnesota',
'MS'=>'Mississippi',
'MO'=>'Missouri',
'MT'=>'Montana',
'NE'=>'Nebraska',
'NV'=>'Nevada',
'NH'=>'New Hampshire',
'NJ'=>'New Jersey',
'NM'=>'New Mexico',
'NY'=>'New York',
'NC'=>'North Carolina',
'ND'=>'North Dakota',
'OH'=>'Ohio',
'OK'=>'Oklahoma',
'OR'=>'Oregon',
'PA'=>'Pennsylvania',
'RI'=>'Rhode Island',
'SC'=>'South Carolina',
'SD'=>'South Dakota',
'TN'=>'Tennessee',
'TX'=>'Texas',
'UT'=>'Utah',
'VT'=>'Vermont',
'VA'=>'Virginia',
'WA'=>'Washington',
'WV'=>'West Virginia',
'WI'=>'Wisconsin',
'WY'=>'Wyoming',
),
), # End State
array( # Zip
'key' => 'location_zip',
'label' => 'Zip',
'name' => 'location_zip',
'type' => 'text',
), # End Zip
array( # Latitude
'key' => 'location_lat',
'label' => 'Latitude',
'name' => 'location_lat',
'type' => 'text',
), # End Latitude
array( # Longitude
'key' => 'location_lon',
'label' => 'Longitude',
'name' => 'location_lon',
'type' => 'text',
), # End Longitude
array( # Re-geocode
'key' => 'location_geocode',
'label' => 'Geocode',
'name' => 'location_geocode',
'type' => 'true_false',
'message' => 'Geocode address',
), # End Re-geocode
array( # Phone
'key' => 'location_phone',
'label' => 'Phone',
'name' => 'location_phone',
'type' => 'text',
), # End Phone
array( # Email
'key' => 'location_email',
'label' => 'Email',
'name' => 'location_email',
'type' => 'text',
), # End Email
array( # Contact Heading
'key' => 'location_contact_heading',
'label' => 'Contact Heading',
'name' => 'location_contact_heading',
'type' => 'text',
), # End Contact Heading
array( # Contact Content
'key' => 'location_contact_content',
'label' => 'Contact Content',
'name' => 'location_contact_content',
'type' => 'wysiwyg',
), # End Contact Content
array( # Jobs button label
'key' => 'jobs_button_label',
'name' => 'jobs_button_label',
'label' => 'Jobs Button Label',
'type' => 'text',
), # End Jobs button label
array( # Jobs button url
'key' => 'jobs_button_url',
'name' => 'jobs_button_url',
'label' => 'Jobs Button URL',
'type' => 'text',
), # End Jobs button url
array( # Talent Community Label
'key' => 'tn_button_label',
'name' => 'tn_button_label',
'label' => 'Talent Community Label',
'type' => 'text',
), # End Talent Community Label
array( # Talent Community Button Link
'key' => 'tn_button_url',
'name' => 'tn_button_url',
'label' => 'Talent Community Button Link',
'type' => 'text',
), # End Talent Community Button Link
array( # Financial button url
'key' => 'financial_button_url',
'name' => 'financial_button_url',
'label' => 'Financial Button URL',
'type' => 'text',
), # End Financial button url
array( # Microsite button label
'key' => 'ms_button_label',
'name' => 'ms_button_label',
'label' => 'Microsite Button Label',
'type' => 'text',
), # End Microsite button label
array( # Microsite button url
'key' => 'ms_button_url',
'name' => 'ms_button_url',
'label' => 'Microsite Button URL',
'type' => 'text',
), # End Microsite button url
array( # Social Media content
'key' => 'social_button_url',
'name' => 'social_button_url',
'label' => 'Social Media Content',
'type' => 'textarea',
), # End Social Media content
), # END fields
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'location'
),
),
), # END location
'options' => array(
'position' => 'normal',
'hide_on_screen' => array(
)
) # END options
)
);
}
add_action( 'acf/init', 'locations_register_acf' );
/* Post save hook for location post type */ function save_location_callback($post_id) {
global $post;
if ($post->post_type != 'location'){
return;
}
// Stitch the address strings
$address = get_field('location_street1');
if ( get_field('location_street2') ) {
$address .= ', ' . get_field('location_street2');
}
$address .= ', ' . get_field('location_city');
$address .= ', ' . get_field('location_state');
$address .= ' ' . get_field('location_zip');
if ( ( get_field('location_geocode') || !get_field('location_lat') || !get_field('location_lon') ) && $address ) {
// Geocode the address
$encoded_address = urlencode($address);
$gecode_url = "https://maps.googleapis.com/maps/api/geocode/json?address={$encoded_address}&key=AIzaSyAc2Jpb2O8T3mvZ-LLUWBmgruj-DcUtJ5U";
$resp_json = file_get_contents($gecode_url);
$resp = json_decode( $resp_json, true );
// response status will be 'OK', if able to geocode given address
if ( $resp['status'] == 'OK' ) {
$lat = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : '';
$lon = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : '';
$formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : '';
// Save coordinates to ACF fields
if ( $lat ) {
update_field('location_lat', $lat, $post_id);
}
if ( $lon ) {
update_field('location_lon', $lon, $post_id);
}
}
}
update_field('location_geocode', '0', $post_id); } add_action('acf/save_post', 'save_location_callback', 100);
/* Locations ajax request */
function pridestaff_get_locations_ajax() {
$args = array (
'post_type' => 'location',
'posts_per_page' => '-1',
'post_status' => 'publish'
);
$posts = new WP_Query($args);
$coordinates = array();
while ( $posts->have_posts() ) {
$posts->the_post();
if ( get_field('location_lat') && get_field('location_lon') ) {
$address = get_field('location_street1');
if ( get_field('location_street2') ) {
$address .= '
' . get_field('location_street2');
}
$address .= '
' . get_field('location_city');
$address .= ', ' . get_field('location_state');
$address .= ' ' . get_field('location_zip');
$encoded_address = urlencode(str_replace('
', ' ', $address));
$coordinates[] = array(
'coord' => array(
'lat' => floatval(get_field('location_lat')),
'lng' => floatval(get_field('location_lon'))
),
'info' => ''.get_the_title().'
'.$address.'
View Location »
Get Directions »',
);
}
}
echo json_encode($coordinates);
die();
}
add_action('wp_ajax_get_locations_ajax', 'pridestaff_get_locations_ajax'); add_action('wp_ajax_nopriv_get_locations_ajax', 'pridestaff_get_locations_ajax');
/* Newsletter ajax request */
function pridestaff_get_newsletter_ajax() {
$args = array (
'post_type' => 'location',
'posts_per_page' => '-1',
'post_status' => 'publish',
'meta_key' => 'location_state',
'orderby' => array('meta_value', 'title'),
'order' => 'ASC',
);
$posts = new WP_Query($args);
$json_object = array();
$json_object['haley'] = array();
$json_object['client'] = array();
$json_object['groups'] = array();
while ( $posts->have_posts() ) {
$posts->the_post();
if ( get_field('haleymail_id') && get_field('haleymail_url') ) {
$json_object['haley'][get_field('haleymail_id')] = addScheme(get_field('haleymail_url'));
$json_object['client'][get_field('haleymail_id')] = addScheme(get_field('haleymail_url_client'));
if ( get_field('location_state') ) {
$state_field = get_field_object('location_state');
$value = $state_field['value'];
$state = $state_field['choices'][ $value ];
if ( empty($json_object['groups'][$state]) ) $json_object['groups'][$state] = array();
if (get_field('haleymail_id'))
// $json_object['groups'][$state][get_field('haleymail_id')] = '';
$json_object['groups'][$state][] = array(get_field('haleymail_id'), '');
}
}
}
wp_reset_postdata();
ksort($json_object['groups']);
foreach ($json_object['groups'] as $k => $a) {
usort($a, "cmp_city_opts");
$json_object['groups'][$k] = $a;
}
echo json_encode($json_object);
die();
}
add_action('wp_ajax_get_newsletter_ajax', 'pridestaff_get_newsletter_ajax');
add_action('wp_ajax_nopriv_get_newsletter_ajax', 'pridestaff_get_newsletter_ajax');
function addScheme($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "https://" . $url;
}
return $url;
}
function cmp_city_opts($a, $b) {
$textA = getTextBetweenTags($a[1], "option");
$textB = getTextBetweenTags($b[1], "option");
if ($textA == $textB) return 0;
return (strcasecmp($textA, $textB) < 0) ? -1 : 1; }
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
The Premium Chat blog is where everyone comes for the latest info. It’s full of workplace tips and trends related to employee retention, engagement, interviewing and more.