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

146 lines 3.7 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 $response = false;
71 if ($action == 'get') {
72 $response = get_option( 'getwid_google_api_key', '');
73 } elseif ($action == 'set') {
74 $response = update_option( 'getwid_google_api_key', $data );
75 } elseif ($action == 'delete') {
76 $response = delete_option( 'getwid_google_api_key' );
77 }
78
79 wp_send_json_success( $response );
80 }
81
82 public function block_frontend_assets( $attributes = [], $content = '' ) {
83
84 if ( is_admin() ) {
85 return;
86 }
87
88 /**
89 * data-map-style="custom"
90 * data-map-style="default"
91 * data-map-style="ultra_light"
92 */
93 $has_custom_style = (
94 false === strpos( $content, 'data-map-style="default"' ) &&
95 false === strpos( $content, 'data-map-style="custom"' )
96 );
97 //map-styles.js
98 if ( $has_custom_style && ! wp_script_is( 'getwid-map-styles', 'enqueued' ) ) {
99 wp_enqueue_script( 'getwid-map-styles' );
100 }
101
102 $api_key = get_option( 'getwid_google_api_key', '' );
103
104 if ( $api_key ) {
105 wp_enqueue_script( 'google_api_key_js', "https://maps.googleapis.com/maps/api/js?key={$api_key}", [], null );
106 }
107
108 //unescape.min.js
109 if ( ! wp_script_is( 'unescape', 'enqueued' ) ) {
110 wp_enqueue_script( 'unescape' );
111 }
112
113 if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
114 return;
115 }
116
117 $rtl = is_rtl() ? '.rtl' : '';
118
119 wp_enqueue_style(
120 self::$blockName,
121 getwid_get_plugin_url( 'assets/blocks/map/style' . $rtl . '.css' ),
122 [],
123 getwid()->settings()->getVersion()
124 );
125
126 wp_enqueue_script(
127 self::$blockName,
128 getwid_get_plugin_url( 'assets/blocks/map/frontend.js' ),
129 [ 'jquery', 'unescape' ],
130 getwid()->settings()->getVersion(),
131 true
132 );
133 }
134
135 public function render_callback( $attributes, $content ) {
136
137 $this->block_frontend_assets( $attributes, $content );
138
139 return $content;
140 }
141 }
142
143 getwid()->blocksManager()->addBlock(
144 new \Getwid\Blocks\GoogleMap()
145 );
146