Plugin Directory

Changeset 1596720


Ignore:
Timestamp:
02/15/2017 05:43:36 PM (9 years ago)
Author:
ionutmorariu
Message:

CHG: code documentation and organization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • scale-lite-tools/trunk/inc/sl-google-maps/render-cpt.php

    r1596719 r1596720  
    66*/
    77
     8// Register Google Maps callback function for the backend
    89function scale_lite_admin_google_maps() {
    910  global $scale_lite_tools, $scale_lite_root_path, $scale_lite_url_path;
    1011  wp_enqueue_script(
    1112    'scale-lite-google-maps-admin-js',
    12     $scale_lite_url_path. '/assets/js/admin.sl-maps.js',
    13     array('jquery'),
     13    $scale_lite_url_path.'/assets/js/admin.sl-maps.js',
     14    array(),
    1415    '1.0.5',
    1516    1
     
    1819add_action('admin_enqueue_scripts', 'scale_lite_admin_google_maps');
    1920
    20 // Register Google Maps callback function
     21// Register Google maps JS v3 for the backend
     22function scale_lite_load_gmaps_v3() {
     23  global $scale_lite_url_path;
     24  wp_enqueue_script(
     25    'scale-lite-google-maps-js',
     26    "https://maps.googleapis.com/maps/api/js",
     27    array(),
     28    '',
     29    true
     30  );
     31  wp_enqueue_style(
     32    'scale_lite_google_maps_css',
     33    $scale_lite_url_path . 'assets/css/fronted.google-maps.css',
     34    array(),
     35    '1.0.5',
     36    'all'
     37  );
     38}
     39add_action('admin_enqueue_scripts', 'scale_lite_load_gmaps_v3', 50 );
     40
     41// Register Google Maps callback function for the frontend
    2142function scale_lite_load_sl_maps_js() {
    2243  global $scale_lite_tools, $scale_lite_root_path, $scale_lite_url_path;
     
    2950}
    3051
    31 
    32 // store all the maps data
     52// store all the markers in an array
    3353$markersArray = array();
    34 function scale_lite_build_markers_array()
    35 {
     54function scale_lite_build_markers_array() {
    3655  $args = array(
    37     // Select maps that are assigned to category slug my-locations
     56    // Fetch all markers
    3857    'post_type' => 'slt_gmap_marker'
    3958  );
     
    6079    endwhile; wp_reset_postdata(); // Restore original post data.
    6180  else:
    62     echo 'No post foundd!';
     81    echo 'No marker foundd!';
    6382  endif;
     83}
    6484
     85// Save all markers in JS var and pass it to sl-google-maps-frontend
     86function register_markers_array() {
     87  global $markersArray;
     88  wp_localize_script('sl-google-maps-frontend', 'scaleLiteMarkersBuffer',$markersArray);
     89}
     90
     91// add async and defer attributs to google maps script google maps JS
     92add_filter('clean_url','unclean_url',10,3);
     93function unclean_url( $good_protocol_url, $original_url, $_context){
     94  $slt_gmaps_settings_options = get_option( 'slt_gmaps_settings_option_name' ); // Array of All Options
     95  $google_maps_api_key_0 = $slt_gmaps_settings_options['google_maps_api_key_0']; // Google Maps API Key
     96  if (false !== strpos($original_url, 'maps.googleapis.com/maps/api/js')){
     97    remove_filter('clean_url','unclean_url',10,3);
     98    return wp_specialchars_decode($good_protocol_url).
     99    '?key='.$google_maps_api_key_0."&callback=slt_gmaps_init' async='async' defer='defer";
     100  }
     101  return $good_protocol_url;
    65102}
    66103
    67104
    68 function register_markers_array() {
    69   global $markersArray;
    70   wp_localize_script('sl-google-maps-frontend', 'scaleLiteMarkersBuffer',
    71     $markersArray
    72   );
    73 
     105// Enque scripts only if shortcode is used
     106function scale_lite_load_maps_if_shortcode() {
     107  global $post;
     108  if(has_shortcode($post->post_content,'sl_gmaps')) {
     109    // execute code only if there is a sl_gmaps shortcode on the page
     110    add_action('wp_enqueue_scripts', 'scale_lite_build_markers_array', 9 );
     111    add_action('wp_enqueue_scripts', 'register_markers_array', 10 );
     112    add_action('wp_enqueue_scripts', 'scale_lite_load_gmaps_v3', 50 );
     113    add_action('wp_enqueue_scripts', 'scale_lite_load_sl_maps_js', 9 );
     114  }
    74115}
     116add_action('wp_enqueue_scripts','scale_lite_load_maps_if_shortcode',0);
    75117
    76118
     
    145187  $o = ob_get_contents();
    146188  ob_end_clean();
    147 
    148189  return $o;
    149190}
    150191// Register google_maps shortcode
    151192add_shortcode('sl_gmaps', 'scale_lite_google_maps_shortcode');
    152 
    153 // Register Google maps JS v3
    154 function scale_lite_load_gmaps_v3() {
    155   global $scale_lite_url_path;
    156   wp_enqueue_script(
    157     'scale-lite-google-maps-js',
    158     "https://maps.googleapis.com/maps/api/js",
    159     array(),
    160     '',
    161     true
    162   );
    163   wp_enqueue_style(
    164     'scale_lite_google_maps_css',
    165     $scale_lite_url_path . 'assets/css/fronted.google-maps.css',
    166     array(),
    167     '1.0.5',
    168     'all'
    169   );
    170 
    171 }
    172 add_action('admin_enqueue_scripts', 'scale_lite_load_gmaps_v3', 50 );
    173 
    174 
    175 // add async and defer attributs to google maps script google maps JS
    176 add_filter('clean_url','unclean_url',10,3);
    177 function unclean_url( $good_protocol_url, $original_url, $_context){
    178   $slt_gmaps_settings_options = get_option( 'slt_gmaps_settings_option_name' ); // Array of All Options
    179   $google_maps_api_key_0 = $slt_gmaps_settings_options['google_maps_api_key_0']; // Google Maps API Key
    180   if (false !== strpos($original_url, 'maps.googleapis.com/maps/api/js')){
    181     remove_filter('clean_url','unclean_url',10,3);
    182     return wp_specialchars_decode($good_protocol_url).'?key='.$google_maps_api_key_0."&callback=slt_gmaps_init' async='async' defer='defer";
    183   }
    184   return $good_protocol_url;
    185 }
    186 
    187 
    188 
    189 function scale_lite_load_maps_if_shortcode() {
    190   global $post;
    191   if(has_shortcode($post->post_content,'sl_gmaps')) {
    192     // execute code only if there is a sl_gmaps shortcode on the page
    193     add_action('wp_enqueue_scripts', 'scale_lite_build_markers_array', 9 );
    194     add_action('wp_enqueue_scripts', 'register_markers_array', 10 );
    195     add_action('wp_enqueue_scripts', 'scale_lite_load_gmaps_v3', 50 );
    196     add_action('wp_enqueue_scripts', 'scale_lite_load_sl_maps_js', 9 );
    197   }
    198 }
    199 add_action('wp_enqueue_scripts','scale_lite_load_maps_if_shortcode',0);
    200 
    201 
    202 
    203 
Note: See TracChangeset for help on using the changeset viewer.