map-field.php
189 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Compatibility\Jet_Engine\Blocks; |
| 5 | |
| 6 | use Jet_Engine\Modules\Maps_Listings\Base_Provider; |
| 7 | use Jet_Engine\Modules\Maps_Listings\Module; |
| 8 | use Jet_Form_Builder\Blocks\Render\Base as RenderBase; |
| 9 | use Jet_Form_Builder\Blocks\Types\Base; |
| 10 | use JFB_Compatibility\Jet_Engine\Preset_Sources\Preset_Source_Options_Page; |
| 11 | use Jet_Form_Builder\Presets\Sources\Base_Source; |
| 12 | use JFB_Components\Compatibility\Compatibility_Tools; |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | if ( ! defined( 'WPINC' ) ) { |
| 16 | die; |
| 17 | } |
| 18 | |
| 19 | class Map_Field extends Base { |
| 20 | |
| 21 | const HANDLE = 'jet-fb-map-field'; |
| 22 | const DEFAULT_ZOOM = 14; |
| 23 | |
| 24 | /** |
| 25 | * @return string |
| 26 | */ |
| 27 | public function get_name() { |
| 28 | return 'map-field'; |
| 29 | } |
| 30 | |
| 31 | public function get_path_metadata_block() { |
| 32 | return Compatibility_Tools::get_dir( |
| 33 | 'jet-engine', |
| 34 | "blocks-metadata/{$this->get_name()}" |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | public function get_field_settings(): array { |
| 39 | return array( |
| 40 | 'height' => $this->block_attrs['height'] ?? 300, |
| 41 | 'format' => $this->block_attrs['format'] ?? Map_Tools::STRING, |
| 42 | 'field_prefix' => $this->block_attrs['name'] ?? '', |
| 43 | 'zoom' => $this->block_attrs['zoom'] ?? 14, |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | public function get_extra_fields( Base_Source $source ): array { |
| 48 | if ( is_a( $source, Preset_Source_Options_Page::class ) ) { |
| 49 | return array( |
| 50 | 'self' => '%prop%', |
| 51 | 'lat' => '%prop|md5%_lat', |
| 52 | 'lng' => '%prop|md5%_lng', |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | return array( |
| 57 | 'self' => '%key%', |
| 58 | 'lat' => '%key|md5%_lat', |
| 59 | 'lng' => '%key|md5%_lng', |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | public function set_preset() { |
| 64 | parent::set_preset(); |
| 65 | |
| 66 | $this->apply_plain_coords(); |
| 67 | $this->sanitize_zoom(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Allows you to specify default coordinates in the following format: |
| 72 | * 50.4496017860376, 30.52537590265274. |
| 73 | * |
| 74 | * Where the first number is lat (Latitude), and the second is lng (Longitude) |
| 75 | * |
| 76 | * @since 3.2.0 |
| 77 | */ |
| 78 | private function apply_plain_coords() { |
| 79 | if ( empty( $this->block_attrs['default'] ) || |
| 80 | ! is_string( $this->block_attrs['default'] ) |
| 81 | ) { |
| 82 | $this->apply_plain_preset(); |
| 83 | |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | $this->try_to_split_coords( $this->block_attrs['default'] ); |
| 88 | } |
| 89 | |
| 90 | private function apply_plain_preset() { |
| 91 | if ( empty( $this->block_attrs['default']['self'] ) || |
| 92 | ! is_string( $this->block_attrs['default']['self'] ) || |
| 93 | ! empty( $this->block_attrs['default']['lat'] ) || |
| 94 | ! empty( $this->block_attrs['default']['lng'] ) |
| 95 | ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $this->try_to_split_coords( $this->block_attrs['default']['self'] ); |
| 100 | } |
| 101 | |
| 102 | private function try_to_split_coords( string $plain_coords ) { |
| 103 | $parts = array_map( 'trim', explode( ',', $plain_coords ) ); |
| 104 | |
| 105 | if ( 2 !== count( $parts ) ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | foreach ( $parts as $coordinate ) { |
| 110 | if ( ! is_numeric( $coordinate ) ) { |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $this->block_attrs['default'] = array( |
| 116 | 'self' => $plain_coords, |
| 117 | 'lat' => $parts[0], |
| 118 | 'lng' => $parts[1], |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | private function sanitize_zoom() { |
| 123 | if ( ! array_key_exists( 'zoom', $this->block_attrs ) ) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | $zoom = &$this->block_attrs['zoom']; |
| 128 | |
| 129 | if ( ! is_numeric( $zoom ) || |
| 130 | $zoom < 1 || 18 < $zoom |
| 131 | ) { |
| 132 | $zoom = self::DEFAULT_ZOOM; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public function expected_preset_type(): array { |
| 137 | return array( self::PRESET_EXACTLY ); |
| 138 | } |
| 139 | |
| 140 | protected function get_value_from_repeater( array $row, string $name ) { |
| 141 | if ( ! isset( $row[ $name ] ) ) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return array( |
| 146 | 'self' => $row[ $name ], |
| 147 | 'lat' => $row[ $name . '_lat' ], |
| 148 | 'lng' => $row[ $name . '_lng' ], |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | protected function render_field( array $attrs, $content = null, $wp_block = null ): string { |
| 153 | if ( ! Map_Tools::is_supported() ) { |
| 154 | return Map_Tools::get_help_message(); |
| 155 | } |
| 156 | |
| 157 | return parent::render_field( $attrs, $content, $wp_block ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @param null $wp_block |
| 162 | * |
| 163 | * @return mixed |
| 164 | */ |
| 165 | public function get_block_renderer( $wp_block = null ) { |
| 166 | /** @var Base_Provider $provider */ |
| 167 | $provider = Module::instance()->providers->get_active_map_provider(); |
| 168 | |
| 169 | $provider->register_public_assets(); |
| 170 | $provider->public_assets( null, array( 'marker_clustering' => false ), null ); |
| 171 | |
| 172 | return ( new class( $this ) extends RenderBase { |
| 173 | public function get_name() { |
| 174 | return 'map-field'; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | protected function get_fields_label_tag(): string { |
| 183 | return 'div'; |
| 184 | } |
| 185 | } )->render(); |
| 186 | } |
| 187 | |
| 188 | } |
| 189 |