index.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use Kubio\Core\Blocks\BlockBase; |
| 6 | use Kubio\Core\Registry; |
| 7 | |
| 8 | class MapBlock extends BlockBase { |
| 9 | |
| 10 | const OUTER = 'outer'; |
| 11 | const WRAPPER = 'wrapper'; |
| 12 | const IFRAME = 'iframe'; |
| 13 | |
| 14 | public function __construct( $block, $autoload = true ) { |
| 15 | parent::__construct( $block, $autoload ); |
| 16 | } |
| 17 | |
| 18 | public function computed() { |
| 19 | return array( |
| 20 | 'address' => $this->getAttribute( 'address' ), |
| 21 | 'zoom' => $this->getAttribute( 'zoom' ), |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | public function mapPropsToElements() { |
| 26 | $address = $this->getAttribute( 'address' ); |
| 27 | $zoom = $this->getAttribute( 'zoom' ); |
| 28 | $key = $this->getAttribute( 'apiKey' ); |
| 29 | if ( ! $address ) { |
| 30 | $address = 'New York'; |
| 31 | } |
| 32 | |
| 33 | $base_url = 'https://www.google.com/maps/embed/v1/place'; |
| 34 | $no_api_base_url = 'https://maps.google.com/maps'; |
| 35 | |
| 36 | if ( $key ) { |
| 37 | $zoomArg = 'zoom'; |
| 38 | $src = add_query_arg( |
| 39 | array( |
| 40 | 'key' => $key, |
| 41 | 'q' => $address, |
| 42 | ), |
| 43 | $base_url |
| 44 | ); |
| 45 | } else { |
| 46 | $zoomArg = 'z'; |
| 47 | $src = add_query_arg( |
| 48 | array( |
| 49 | 'q' => $address, |
| 50 | 'output' => 'embed', |
| 51 | 'iwloc' => 'near', |
| 52 | ), |
| 53 | $no_api_base_url |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | if ( ! empty( $zoom['value'] ) ) { |
| 58 | $src = add_query_arg( array( $zoomArg => $zoom['value'] ), $src ); |
| 59 | } |
| 60 | |
| 61 | return array( |
| 62 | self::OUTER => array(), |
| 63 | self::WRAPPER => array( |
| 64 | 'className' => 'frontend-wrapper', |
| 65 | ), |
| 66 | self::IFRAME => array( |
| 67 | 'src' => esc_url( $src ), |
| 68 | ), |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | Registry::registerBlock( __DIR__, MapBlock::class ); |
| 74 |