PluginProbe
Getwid – Gutenberg Blocks / 2.1.3
Getwid – Gutenberg Blocks v2.1.3
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 2.1.3, at includes/blocks/google-map.php

150 lines 3.8 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 \Getwid\Blocks\AbstractBlock {
6
7 protected static $blockName = 'getwid/map';
8
9 public function __construct() {
10
11 parent::__construct( self::$blockName );
12
13 register_block_type(
14 'getwid/map',
15 array(
16 'render_callback' => [ $this, 'render_callback' ]
17 )
18 );
19
20 add_action( 'wp_ajax_get_google_api_key', [ $this, 'get_google_api_key'] );
21
22 getwid_maybe_add_option( 'getwid_google_api_key', '', true );
23
24 if ( $this->isEnabled() ) {
25
26 add_filter( 'getwid/editor_blocks_js/dependencies', [ $this, 'block_editor_scripts'] );
27
28 wp_register_script(
29 'unescape',
30 getwid_get_plugin_url( 'vendors/lodash.unescape/unescape.min.js' ),
31 [],
32 '4.0.1',
33 true
34 );
35
36 wp_register_script(
37 'getwid-map-styles',
38 getwid_get_plugin_url( 'vendors/getwid/map-styles.min.js' ),
39 [],
40 '1.0.0',
41 true
42 );
43 }
44 }
45
46 public function getLabel() {
47 return __('Google Maps', 'getwid');
48 }
49
50 public function block_editor_scripts($scripts) {
51
52 //map-styles.min.js
53 if ( ! in_array( 'getwid-map-styles', $scripts ) ) {
54 array_push( $scripts, 'getwid-map-styles' );
55 }
56
57 return $scripts;
58 }
59
60 public function get_google_api_key() {
61
62 $nonce = sanitize_key( $_POST['nonce'] );
63 $action = sanitize_text_field( wp_unslash( $_POST['option'] ) );
64 $data = sanitize_text_field( wp_unslash( $_POST['data'] ) );
65
66 if ( ! wp_verify_nonce( $nonce, 'getwid_nonce_google_api_key' ) ) {
67 wp_send_json_error();
68 }
69
70 if ( ! current_user_can( 'manage_options' ) && in_array( $action, [ 'set', 'delete' ] ) ) {
71 wp_send_json_error( esc_html__( 'You are not allowed to perform this action. Please contact the administrator.', 'getwid' ) );
72 }
73
74 $response = false;
75 if ($action == 'get') {
76 $response = get_option( 'getwid_google_api_key', '');
77 } elseif ($action == 'set') {
78 $response = update_option( 'getwid_google_api_key', $data );
79 } elseif ($action == 'delete') {
80 $response = delete_option( 'getwid_google_api_key' );
81 }
82
83 wp_send_json_success( $response );
84 }
85
86 public function block_frontend_assets( $attributes = [], $content = '' ) {
87
88 if ( is_admin() ) {
89 return;
90 }
91
92 /**
93 * data-map-style="custom"
94 * data-map-style="default"
95 * data-map-style="ultra_light"
96 */
97 $has_custom_style = (
98 false === strpos( $content, 'data-map-style="default"' ) &&
99 false === strpos( $content, 'data-map-style="custom"' )
100 );
101 //map-styles.js
102 if ( $has_custom_style && ! wp_script_is( 'getwid-map-styles', 'enqueued' ) ) {
103 wp_enqueue_script( 'getwid-map-styles' );
104 }
105
106 $api_key = get_option( 'getwid_google_api_key', '' );
107
108 if ( $api_key ) {
109 wp_enqueue_script( 'google_api_key_js', "https://maps.googleapis.com/maps/api/js?key={$api_key}", [], null );
110 }
111
112 //unescape.min.js
113 if ( ! wp_script_is( 'unescape', 'enqueued' ) ) {
114 wp_enqueue_script( 'unescape' );
115 }
116
117 if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
118 return;
119 }
120
121 $rtl = is_rtl() ? '.rtl' : '';
122
123 wp_enqueue_style(
124 self::$blockName,
125 getwid_get_plugin_url( 'assets/blocks/map/style' . $rtl . '.css' ),
126 [],
127 getwid()->settings()->getVersion()
128 );
129
130 wp_enqueue_script(
131 self::$blockName,
132 getwid_get_plugin_url( 'assets/blocks/map/frontend.js' ),
133 [ 'jquery', 'unescape' ],
134 getwid()->settings()->getVersion(),
135 true
136 );
137 }
138
139 public function render_callback( $attributes, $content ) {
140
141 $this->block_frontend_assets( $attributes, $content );
142
143 return $content;
144 }
145 }
146
147 getwid()->blocksManager()->addBlock(
148 new \Getwid\Blocks\GoogleMap()
149 );
150