-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathacf-yandex-map.php
77 lines (58 loc) · 2.3 KB
/
acf-yandex-map.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/*
Plugin Name: Yandex Map Field for ACF
Plugin URI: https://github.com/constlab/acf-yandex-map
Description: Editing map on page, add geopoints and circles
Version: 1.3
Author: Const Lab
Author URI: https://constlab.ru
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
defined( 'YA_MAP_LANG_DOMAIN' ) or define( 'YA_MAP_LANG_DOMAIN', 'acf-yandex-map' );
defined( 'ACF_YA_MAP_VERSION' ) or define( 'ACF_YA_MAP_VERSION', '1.3.0' );
load_plugin_textdomain( YA_MAP_LANG_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
function include_field_types_yandex_map( $version = false ) {
if ( ! $version ) {
$version = 4;
}
include_once __DIR__ . '/acf-yandex-map-v' . $version . '.php';
}
add_action( 'acf/include_field_types', 'include_field_types_yandex_map' );
add_action( 'acf/register_fields', 'include_field_types_yandex_map' );
/// Function for frontend
if ( ! function_exists( 'the_yandex_map' ) ) {
/**
* @param string $selector
* @param int|bool $post_id
* @param null $data
*/
function the_yandex_map( $selector, $post_id = false, $data = null ) {
$post_id = function_exists( 'acf_get_valid_post_id' ) ? acf_get_valid_post_id( $post_id ) : $post_id;
$value = ( $data !== null ) ? $data : get_field( $selector, $post_id, false );
if ( ! $value ) {
return;
}
$dir = plugin_dir_url( __FILE__ );
wp_register_script( 'yandex-map-api', '//api-maps.yandex.ru/2.1/?lang=' . get_bloginfo( 'language' ), array( 'jquery' ), null );
wp_register_script( 'yandex-map-frontend', "{$dir}js/yandex-map.min.js", array( 'yandex-map-api' ), ACF_YA_MAP_VERSION );
wp_enqueue_script( 'yandex-map-frontend' );
$map_id = uniqid( 'map_' );
wp_localize_script( 'yandex-map-frontend', $map_id, array(
'params' => $value
) );
/**
* Filter the map height for frontend.
*
* @since 1.2.0
*
* @param string $selector Field name
* @param int $post_id Current page id
* @param array $value Map field value
*/
$field = get_field_object( $selector, $post_id );
$field_height = $field ? $field['height'] : 200;
$height_map = apply_filters( 'acf-yandex-map/height', $field_height, $selector, $post_id, $value );
echo sprintf( '<div class="yandex-map" id="%s" style="width:auto;height:%dpx"></div>', $map_id, $height_map );
}
}