get_option( 'advanced_maps_block_api_key', '' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => $nonce ] ); } // Register script for frontend. if ( ! is_admin() ) { wp_register_script( 'advanced-maps-block-frontend-js', plugins_url( '/src/frontend.js', dirname( __FILE__ ) ), array(), '0.1.0', true ); // Register Google Maps API for frontend and backend. wp_register_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=' . $key . $google_maps_params, array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor', 'advanced-maps-block-frontend-js' ), '0.1.0', true ); } /** * Register block on server-side. * * Register the block on server-side to ensure that the block * scripts and styles for both frontend and backend are * enqueued when the editor loads. * * @link https://wordpress.org/gutenberg/handbook/blocks/writing-your-first-block-type#enqueuing-block-scripts * @since 1.16.0 */ register_block_type( 'amb/advanced-maps-block', array( // Enqueue blocks.build.js in the editor only. 'editor_script' => 'advanced-maps-block-block-js', // Enqueue blocks.editor.build.css in the editor only. 'editor_style' => 'advanced-maps-block-block-editor-css', // Enqueue frontend.build.js on frontend. 'script' => 'advanced-maps-block-frontend-js', // Enqueue Google Maps API on frontend. 'script' => 'google-maps', ) ); } add_action( 'init', 'advanced_maps_block_block_assets' ); /** * AJAX script to update Google Maps API Key option. */ function advanced_maps_block_update_api_key( $response ) { check_ajax_referer( 'advanced_maps_block_api_key_nonce' ); $advanced_maps_block_api_key = sanitize_text_field( $_GET['advanced_maps_block_api_key'] ); // maybe want to send as post update_option( 'advanced_maps_block_api_key', $advanced_maps_block_api_key ); $response = json_encode( 'success' ); echo $response; die(); } add_action( 'wp_ajax_advanced_maps_block_update_api_key', 'advanced_maps_block_update_api_key' );