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
← All changes | includes/blocks/google-map.php +99 -149 2.0.113.0.2 View file →
@@ -1,149 +1,99 @@
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 -);
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 +);