| @@ -1,145 +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 | - $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 | -); | |
| 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 | +); | |