PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
3.0.2 3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / blocks / google-map.php

google-map.php in Getwid – Gutenberg Blocks 3.0.2, at includes/blocks/google-map.php

100 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Getwid\Blocks;
4
5 class GoogleMap extends AbstractBlock {
6
7 public function __construct() {
8
9 parent::__construct( 'getwid/map' );
10
11 add_action( 'wp_ajax_get_google_api_key', array( $this, 'get_google_api_key' ) );
12
13 getwid_maybe_add_option( 'getwid_google_api_key', '', true );
14
15 wp_register_script(
16 'getwid-map-styles',
17 getwid_get_plugin_url( 'vendors/getwid/map-styles.min.js' ),
18 array(),
19 '1.0.0',
20 true
21 );
22
23 register_block_type(
24 getwid_get_plugin_path( 'assets/blocks/map' ),
25 array(
26 'render_callback' => array( $this, 'render_callback' ),
27 )
28 );
29 }
30
31 public function get_label() {
32 return __( 'Google Maps', 'getwid' );
33 }
34
35 public function get_google_api_key() {
36
37 $nonce = sanitize_key( $_POST['nonce'] );
38 $action = sanitize_text_field( wp_unslash( $_POST['option'] ) );
39 $data = sanitize_text_field( wp_unslash( $_POST['data'] ) );
40
41 if ( ! wp_verify_nonce( $nonce, 'getwid_nonce_google_api_key' ) ) {
42 wp_send_json_error();
43 }
44
45 if ( ! current_user_can( 'manage_options' ) && in_array( $action, array( 'set', 'delete' ), true ) ) {
46 wp_send_json_error( esc_html__( 'You are not allowed to perform this action. Please contact the administrator.', 'getwid' ) );
47 }
48
49 $response = false;
50 if ( 'get' === $action ) {
51 $response = get_option( 'getwid_google_api_key', '' );
52 } elseif ( 'set' === $action ) {
53 $response = update_option( 'getwid_google_api_key', $data );
54 } elseif ( 'delete' === $action ) {
55 $response = delete_option( 'getwid_google_api_key' );
56 }
57
58 wp_send_json_success( $response );
59 }
60
61 public function block_frontend_assets( $content = '' ) {
62
63 if ( is_admin() ) {
64 return;
65 }
66
67 $has_preset_style = (
68 false === strpos( $content, 'data-map-style="default"' ) &&
69 false === strpos( $content, 'data-map-style="custom"' )
70 );
71
72 if ( $has_preset_style && ! wp_script_is( 'getwid-map-styles', 'enqueued' ) ) {
73 wp_enqueue_script( 'getwid-map-styles' );
74 }
75
76 $api_key = get_option( 'getwid_google_api_key', '' );
77
78 if ( $api_key ) {
79 wp_enqueue_script(
80 'google_api_key_js',
81 'https://maps.googleapis.com/maps/api/js?key=' . rawurlencode( $api_key ),
82 array(),
83 null,
84 true
85 );
86 }
87 }
88
89 public function render_callback( $attributes, $content ) {
90
91 $this->block_frontend_assets( $content );
92
93 return $content;
94 }
95 }
96
97 getwid()->blocksManager()->addBlock(
98 new GoogleMap()
99 );
100