map.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: map
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_map' ) ) {
  11. class CSF_Field_map extends CSF_Fields {
  12. public $version = '1.7.1';
  13. public $cdn_url = 'https://lib.baomitu.com/leaflet/';
  14. public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
  15. parent::__construct( $field, $value, $unique, $where, $parent );
  16. }
  17. public function render() {
  18. $args = wp_parse_args( $this->field, array(
  19. 'placeholder' => esc_html__( 'Search...', 'sakurairo_csf' ),
  20. 'latitude_text' => esc_html__( 'Latitude', 'sakurairo_csf' ),
  21. 'longitude_text' => esc_html__( 'Longitude', 'sakurairo_csf' ),
  22. 'address_field' => '',
  23. 'height' => '',
  24. ) );
  25. $value = wp_parse_args( $this->value, array(
  26. 'address' => '',
  27. 'latitude' => '20',
  28. 'longitude' => '0',
  29. 'zoom' => '2',
  30. ) );
  31. $default_settings = array(
  32. 'center' => array( $value['latitude'], $value['longitude'] ),
  33. 'zoom' => $value['zoom'],
  34. 'scrollWheelZoom' => false,
  35. );
  36. $settings = ( ! empty( $this->field['settings'] ) ) ? $this->field['settings'] : array();
  37. $settings = wp_parse_args( $settings, $default_settings );
  38. $style_attr = ( ! empty( $args['height'] ) ) ? ' style="min-height:'. esc_attr( $args['height'] ) .';"' : '';
  39. $placeholder = ( ! empty( $args['placeholder'] ) ) ? array( 'placeholder' => $args['placeholder'] ) : '';
  40. echo $this->field_before();
  41. if ( empty( $args['address_field'] ) ) {
  42. echo '<div class="csf--map-search">';
  43. echo '<input type="text" name="'. esc_attr( $this->field_name( '[address]' ) ) .'" value="'. esc_attr( $value['address'] ) .'"'. $this->field_attributes( $placeholder ) .' />';
  44. echo '</div>';
  45. } else {
  46. echo '<div class="csf--address-field" data-address-field="'. esc_attr( $args['address_field'] ) .'"></div>';
  47. }
  48. echo '<div class="csf--map-osm-wrap"><div class="csf--map-osm" data-map="'. esc_attr( json_encode( $settings ) ) .'"'. $style_attr .'></div></div>';
  49. echo '<div class="csf--map-inputs">';
  50. echo '<div class="csf--map-input">';
  51. echo '<label>'. esc_attr( $args['latitude_text'] ) .'</label>';
  52. echo '<input type="text" name="'. esc_attr( $this->field_name( '[latitude]' ) ) .'" value="'. esc_attr( $value['latitude'] ) .'" class="csf--latitude" />';
  53. echo '</div>';
  54. echo '<div class="csf--map-input">';
  55. echo '<label>'. esc_attr( $args['longitude_text'] ) .'</label>';
  56. echo '<input type="text" name="'. esc_attr( $this->field_name( '[longitude]' ) ) .'" value="'. esc_attr( $value['longitude'] ) .'" class="csf--longitude" />';
  57. echo '</div>';
  58. echo '</div>';
  59. echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[zoom]' ) ) .'" value="'. esc_attr( $value['zoom'] ) .'" class="csf--zoom" />';
  60. echo $this->field_after();
  61. }
  62. public function enqueue() {
  63. if ( ! wp_script_is( 'csf-leaflet' ) ) {
  64. wp_enqueue_script( 'csf-leaflet', esc_url( $this->cdn_url . $this->version .'/leaflet.js' ), array( 'sakurairo_csf' ), $this->version, true );
  65. }
  66. if ( ! wp_style_is( 'csf-leaflet' ) ) {
  67. wp_enqueue_style( 'csf-leaflet', esc_url( $this->cdn_url . $this->version .'/leaflet.css' ), array(), $this->version );
  68. }
  69. if ( ! wp_script_is( 'jquery-ui-autocomplete' ) ) {
  70. wp_enqueue_script( 'jquery-ui-autocomplete' );
  71. }
  72. }
  73. }
  74. }