| @@ -1,5 +1,6 @@ | ||
| 1 | -<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. | |
| 1 | +<?php if ( ! defined( 'ABSPATH' ) ) { | |
| 2 | + die; } // Cannot access directly. | |
| 2 | 3 | /** |
| 3 | 4 | * |
| 4 | 5 | * Field: map |
| 5 | 6 | * |
| @@ -4,99 +5,96 @@ | ||
| 4 | 5 | * Field: map |
| 5 | 6 | * |
| 6 | 7 | * @since 1.0.0 |
| 7 | 8 | * @version 1.0.0 |
| 8 | - * | |
| 9 | 9 | */ |
| 10 | 10 | if ( ! class_exists( 'ADMINIFY_Field_map' ) ) { |
| 11 | - class ADMINIFY_Field_map extends ADMINIFY_Fields { | |
| 11 | + class ADMINIFY_Field_map extends ADMINIFY_Fields { | |
| 12 | 12 | |
| 13 | - public $version = '1.9.4'; | |
| 14 | - public $cdn_url = ''; | |
| 13 | + public $version = '1.7.1'; | |
| 14 | + public $cdn_url = 'https://cdn.jsdelivr.net/npm/leaflet@'; | |
| 15 | 15 | |
| 16 | - public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { | |
| 17 | - parent::__construct( $field, $value, $unique, $where, $parent ); | |
| 18 | - // Leaflet is bundled locally inside the plugin (no remote CDN). | |
| 19 | - $this->cdn_url = defined( 'PXLBSADMINIFY_ASSETS' ) ? PXLBSADMINIFY_ASSETS . 'vendors/leaflet' : ''; | |
| 20 | - } | |
| 16 | + public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { | |
| 17 | + parent::__construct( $field, $value, $unique, $where, $parent ); | |
| 18 | + } | |
| 21 | 19 | |
| 22 | - public function render() { | |
| 20 | + public function render() { | |
| 21 | + $args = wp_parse_args( | |
| 22 | + $this->field, | |
| 23 | + [ | |
| 24 | + 'placeholder' => esc_html__( 'Search...', 'adminify' ), | |
| 25 | + 'latitude_text' => esc_html__( 'Latitude', 'adminify' ), | |
| 26 | + 'longitude_text' => esc_html__( 'Longitude', 'adminify' ), | |
| 27 | + 'address_field' => '', | |
| 28 | + 'height' => '', | |
| 29 | + ] | |
| 30 | + ); | |
| 23 | 31 | |
| 24 | - $args = wp_parse_args( $this->field, array( | |
| 25 | - 'placeholder' => esc_html__( 'Search...', 'adminify' ), | |
| 26 | - 'latitude_text' => esc_html__( 'Latitude', 'adminify' ), | |
| 27 | - 'longitude_text' => esc_html__( 'Longitude', 'adminify' ), | |
| 28 | - 'address_field' => '', | |
| 29 | - 'height' => '', | |
| 30 | - ) ); | |
| 32 | + $value = wp_parse_args( | |
| 33 | + $this->value, | |
| 34 | + [ | |
| 35 | + 'address' => '', | |
| 36 | + 'latitude' => '20', | |
| 37 | + 'longitude' => '0', | |
| 38 | + 'zoom' => '2', | |
| 39 | + ] | |
| 40 | + ); | |
| 31 | 41 | |
| 32 | - $value = wp_parse_args( $this->value, array( | |
| 33 | - 'address' => '', | |
| 34 | - 'latitude' => '20', | |
| 35 | - 'longitude' => '0', | |
| 36 | - 'zoom' => '2', | |
| 37 | - ) ); | |
| 42 | + $default_settings = [ | |
| 43 | + 'center' => [ $value['latitude'], $value['longitude'] ], | |
| 44 | + 'zoom' => $value['zoom'], | |
| 45 | + 'scrollWheelZoom' => false, | |
| 46 | + ]; | |
| 38 | 47 | |
| 39 | - $default_settings = array( | |
| 40 | - 'center' => array( $value['latitude'], $value['longitude'] ), | |
| 41 | - 'zoom' => $value['zoom'], | |
| 42 | - 'scrollWheelZoom' => false, | |
| 43 | - ); | |
| 48 | + $settings = ( ! empty( $this->field['settings'] ) ) ? $this->field['settings'] : []; | |
| 49 | + $settings = wp_parse_args( $settings, $default_settings ); | |
| 44 | 50 | |
| 45 | - $settings = ( ! empty( $this->field['settings'] ) ) ? $this->field['settings'] : array(); | |
| 46 | - $settings = wp_parse_args( $settings, $default_settings ); | |
| 51 | + $style_attr = ( ! empty( $args['height'] ) ) ? ' style="min-height:' . esc_attr( $args['height'] ) . ';"' : ''; | |
| 52 | + $placeholder = ( ! empty( $args['placeholder'] ) ) ? [ 'placeholder' => $args['placeholder'] ] : ''; | |
| 47 | 53 | |
| 48 | - $style_attr = ( ! empty( $args['height'] ) ) ? ' style="min-height:'. esc_attr( $args['height'] ) .';"' : ''; | |
| 49 | - $placeholder = ( ! empty( $args['placeholder'] ) ) ? array( 'placeholder' => $args['placeholder'] ) : ''; | |
| 54 | + echo wp_kses_post( $this->field_before() ); | |
| 50 | 55 | |
| 51 | - echo wp_kses_post( $this->field_before() ); | |
| 56 | + if ( empty( $args['address_field'] ) ) { | |
| 57 | + echo '<div class="adminify--map-search">'; | |
| 58 | + echo '<input type="text" name="' . esc_attr( $this->field_name( '[address]' ) ) . '" value="' . esc_attr( $value['address'] ) . '"' . wp_kses_post( $this->field_attributes( $placeholder ) ) . ' />'; | |
| 59 | + echo '</div>'; | |
| 60 | + } else { | |
| 61 | + echo '<div class="adminify--address-field" data-address-field="' . esc_attr( $args['address_field'] ) . '"></div>'; | |
| 62 | + } | |
| 52 | 63 | |
| 53 | - if ( empty( $args['address_field'] ) ) { | |
| 54 | - echo '<div class="adminify--map-search">'; | |
| 55 | - echo '<input type="text" name="'. esc_attr( $this->field_name( '[address]' ) ) .'" value="'. esc_attr( $value['address'] ) .'"'. $this->field_attributes( $placeholder ) .' />'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field_attributes() escapes each attribute via esc_attr() | |
| 56 | - echo '</div>'; | |
| 57 | - } else { | |
| 58 | - echo '<div class="adminify--address-field" data-address-field="'. esc_attr( $args['address_field'] ) .'"></div>'; | |
| 59 | - } | |
| 64 | + echo '<div class="adminify--map-osm-wrap"><div class="adminify--map-osm" data-map="' . esc_attr( json_encode( $settings ) ) . '"' . esc_attr( $style_attr ) . '></div></div>'; | |
| 60 | 65 | |
| 61 | - echo '<div class="adminify--map-osm-wrap"><div class="adminify--map-osm" data-map="'. esc_attr( wp_json_encode( $settings ) ) .'"'. $style_attr .'></div></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- attribute value pre-escaped with esc_attr() where built. | |
| 66 | + echo '<div class="adminify--map-inputs">'; | |
| 62 | 67 | |
| 63 | - echo '<div class="adminify--map-inputs">'; | |
| 68 | + echo '<div class="adminify--map-input">'; | |
| 69 | + echo '<label>' . esc_attr( $args['latitude_text'] ) . '</label>'; | |
| 70 | + echo '<input type="text" name="' . esc_attr( $this->field_name( '[latitude]' ) ) . '" value="' . esc_attr( $value['latitude'] ) . '" class="adminify--latitude" />'; | |
| 71 | + echo '</div>'; | |
| 64 | 72 | |
| 65 | - echo '<div class="adminify--map-input">'; | |
| 66 | - echo '<label>'. esc_attr( $args['latitude_text'] ) .'</label>'; | |
| 67 | - echo '<input type="text" name="'. esc_attr( $this->field_name( '[latitude]' ) ) .'" value="'. esc_attr( $value['latitude'] ) .'" class="adminify--latitude" />'; | |
| 68 | - echo '</div>'; | |
| 73 | + echo '<div class="adminify--map-input">'; | |
| 74 | + echo '<label>' . esc_attr( $args['longitude_text'] ) . '</label>'; | |
| 75 | + echo '<input type="text" name="' . esc_attr( $this->field_name( '[longitude]' ) ) . '" value="' . esc_attr( $value['longitude'] ) . '" class="adminify--longitude" />'; | |
| 76 | + echo '</div>'; | |
| 69 | 77 | |
| 70 | - echo '<div class="adminify--map-input">'; | |
| 71 | - echo '<label>'. esc_attr( $args['longitude_text'] ) .'</label>'; | |
| 72 | - echo '<input type="text" name="'. esc_attr( $this->field_name( '[longitude]' ) ) .'" value="'. esc_attr( $value['longitude'] ) .'" class="adminify--longitude" />'; | |
| 73 | - echo '</div>'; | |
| 78 | + echo '</div>'; | |
| 74 | 79 | |
| 75 | - echo '</div>'; | |
| 80 | + echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[zoom]' ) ) . '" value="' . esc_attr( $value['zoom'] ) . '" class="adminify--zoom" />'; | |
| 76 | 81 | |
| 77 | - echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[zoom]' ) ) .'" value="'. esc_attr( $value['zoom'] ) .'" class="adminify--zoom" />'; | |
| 82 | + echo wp_kses_post( $this->field_after() ); | |
| 83 | + } | |
| 78 | 84 | |
| 79 | - echo wp_kses_post( $this->field_after() ); | |
| 85 | + public function enqueue() { | |
| 86 | + if ( ! wp_script_is( 'adminify-leaflet' ) ) { | |
| 87 | + wp_enqueue_script( 'adminify-leaflet', esc_url( $this->cdn_url . $this->version . '/dist/leaflet.js' ), [ 'adminify' ], $this->version, true ); | |
| 88 | + } | |
| 80 | 89 | |
| 81 | - } | |
| 90 | + if ( ! wp_style_is( 'adminify-leaflet' ) ) { | |
| 91 | + wp_enqueue_style( 'adminify-leaflet', esc_url( $this->cdn_url . $this->version . '/dist/leaflet.css' ), [], $this->version ); | |
| 92 | + } | |
| 82 | 93 | |
| 83 | - public function enqueue() { | |
| 94 | + if ( ! wp_script_is( 'jquery-ui-autocomplete' ) ) { | |
| 95 | + wp_enqueue_script( 'jquery-ui-autocomplete' ); | |
| 96 | + } | |
| 97 | + } | |
| 84 | 98 | |
| 85 | - if ( empty( $this->cdn_url ) ) { return; } | |
| 86 | - | |
| 87 | - if ( ! wp_script_is( 'adminify-leaflet' ) ) { | |
| 88 | - wp_enqueue_script( 'adminify-leaflet', esc_url( $this->cdn_url .'/leaflet.js' ), array( 'adminify' ), $this->version, true ); | |
| 89 | - } | |
| 90 | - | |
| 91 | - if ( ! wp_style_is( 'adminify-leaflet' ) ) { | |
| 92 | - wp_enqueue_style( 'adminify-leaflet', esc_url( $this->cdn_url .'/leaflet.css' ), array(), $this->version ); | |
| 93 | - } | |
| 94 | - | |
| 95 | - if ( ! wp_script_is( 'jquery-ui-autocomplete' ) ) { | |
| 96 | - wp_enqueue_script( 'jquery-ui-autocomplete' ); | |
| 97 | - } | |
| 98 | - | |
| 99 | - } | |
| 100 | - | |
| 101 | - } | |
| 99 | + } | |
| 102 | 100 | } |