Skip to content

Commit

Permalink
Merge pull request #328 from humanmade/326-google-api-key
Browse files Browse the repository at this point in the history
Add Google API key argument to gmap field type
  • Loading branch information
mikeselander authored Oct 5, 2016
2 parents 24652b3 + fb17427 commit 6f78ad4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
18 changes: 17 additions & 1 deletion classes.fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,7 @@ public function get_default_args() {
'default_zoom' => '8',
'string-marker-title' => esc_html__( 'Drag to set the exact location', 'cmb' ),
'string-gmaps-api-not-loaded' => esc_html__( 'Google Maps API not loaded.', 'cmb' ),
'google_api_key' => '',
)
);
}
Expand All @@ -1661,7 +1662,22 @@ public function enqueue_scripts() {

parent::enqueue_scripts();

wp_enqueue_script( 'cmb-google-maps', '//maps.google.com/maps/api/js?libraries=places' );
$maps_src = '//maps.google.com/maps/api/js?libraries=places';

// Check for our key with either a field argument or constant.
if ( ! empty( $this->args['google_api_key'] ) ){
$key = $this->args['google_api_key'];
} elseif ( defined( 'CMB_GAPI_KEY' ) ) {
$key = CMB_GAPI_KEY;
}

// Only add the key argument if it's been set.
if ( ! empty( $key ) ) {
$maps_src = add_query_arg( 'key', $key, $maps_src );
}

// Enqueue our scripts.
wp_enqueue_script( 'cmb-google-maps', $maps_src );
wp_enqueue_script( 'cmb-google-maps-script', trailingslashit( CMB_URL ) . 'js/field-gmap.js', array( 'jquery', 'cmb-google-maps' ) );

wp_localize_script( 'cmb-google-maps-script', 'CMBGmaps', array(
Expand Down
2 changes: 1 addition & 1 deletion example-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function cmb_sample_metaboxes( array $meta_boxes ) {

array( 'id' => 'field-22', 'name' => 'Color', 'type' => 'colorpicker' ),

array( 'id' => 'field-23', 'name' => 'Location', 'type' => 'gmap' ),
array( 'id' => 'field-23', 'name' => 'Location', 'type' => 'gmap', 'google_api_key' => '{CUSTOM_KEY}' ),

array( 'id' => 'field-24', 'name' => 'Title Field', 'type' => 'title' ),

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ See [CONTRIBUTING.md](https://github.com/humanmade/Custom-Meta-Boxes/blob/master
* Add hide_on field argument
* Add Composer support
* Enhancement - enable for attachments
* Fix bug with unnatached images on custom post types
* Fix bug with unattached images on custom post types
* Fix error in WYSIWYG
* Fix fields not getting correctly initialized if meta box is collapsed on page load
* Fix bug with page-template restricted meta boxes showing if the post hasn't been saved at all.
Expand Down

0 comments on commit 6f78ad4

Please sign in to comment.