| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Maps Block for Gutenberg |
| 4 |
Description: Simple, no-nonsense map block powered by Google Maps for Gutenberg editor. |
| 5 |
Author: WebFactory Ltd |
| 6 |
Version: 1.31 |
| 7 |
Author URI: https://www.webfactoryltd.com/ |
| 8 |
Text Domain: map-block-gutenberg |
| 9 |
|
| 10 |
Copyright 2018-2021 WebFactory Ltd (email: support@webfactoryltd.com) |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify |
| 13 |
it under the terms of the GNU General Public License, version 2, as |
| 14 |
published by the Free Software Foundation. |
| 15 |
|
| 16 |
This program is distributed in the hope that it will be useful, |
| 17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
GNU General Public License for more details. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License |
| 22 |
along with this program; if not, write to the Free Software |
| 23 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 |
*/ |
| 25 |
|
| 26 |
// Exit if accessed directly. |
| 27 |
defined('ABSPATH') || exit; |
| 28 |
|
| 29 |
class wf_map_block { |
| 30 |
static $version; |
| 31 |
|
| 32 |
// get plugin version from header |
| 33 |
static function get_plugin_version() { |
| 34 |
$plugin_data = get_file_data(__FILE__, array('version' => 'Version'), 'plugin'); |
| 35 |
self::$version = $plugin_data['version']; |
| 36 |
|
| 37 |
return $plugin_data['version']; |
| 38 |
} // get_plugin_version |
| 39 |
|
| 40 |
|
| 41 |
// hook things up |
| 42 |
static function init() { |
| 43 |
if (is_admin()) { |
| 44 |
if (false === self::check_gutenberg()) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
add_filter('plugin_action_links_' . basename(dirname(__FILE__)) . '/' . basename(__FILE__), |
| 49 |
array(__CLASS__, 'plugin_action_links')); |
| 50 |
add_filter('plugin_row_meta', array(__CLASS__, 'plugin_meta_links'), 10, 2); |
| 51 |
|
| 52 |
add_action('enqueue_block_editor_assets', array(__CLASS__, 'enqueue_block_editor_assets')); |
| 53 |
|
| 54 |
add_action('wp_ajax_gmw_map_block_save_key', array(__CLASS__, 'save_key')); |
| 55 |
add_action('wp_ajax_nopriv_gmw_map_block_save_key', array(__CLASS__, 'save_key')); |
| 56 |
} |
| 57 |
} // init |
| 58 |
|
| 59 |
|
| 60 |
static function save_key() { |
| 61 |
$key = substr(sanitize_html_class(@$_POST['api_key']), 0, 64); |
| 62 |
update_option('gmw-map-block-key', $key); |
| 63 |
echo $key; |
| 64 |
die(); |
| 65 |
} // save_key |
| 66 |
|
| 67 |
|
| 68 |
// some things have to be loaded earlier |
| 69 |
static function plugins_loaded() { |
| 70 |
self::$version = self::get_plugin_version(); |
| 71 |
} // plugins_loaded |
| 72 |
|
| 73 |
|
| 74 |
// add links to plugins page |
| 75 |
static function plugin_action_links($links) { |
| 76 |
$gutenberg_link = '<a href="' . admin_url('post-new.php?post_type=page') . '" title="' . __('Create a new post using the Gutenberg editor', 'map-block-gutenberg') . '">' . __('Create with Gutenberg', 'map-block-gutenberg') . '</a>'; |
| 77 |
|
| 78 |
array_unshift($links, $gutenberg_link); |
| 79 |
|
| 80 |
return $links; |
| 81 |
} // plugin_action_links |
| 82 |
|
| 83 |
|
| 84 |
// add links to plugin's description in plugins table |
| 85 |
static function plugin_meta_links($links, $file) { |
| 86 |
$support_link = '<a target="_blank" href="https://wordpress.org/support/plugin/map-block-gutenberg" title="' . __('Problems? We are here to help!', 'map-block-gutenberg') . '">' . __('Support', 'map-block-gutenberg') . '</a>'; |
| 87 |
$review_link = '<a target="_blank" href="https://wordpress.org/support/view/plugin-reviews/map-block-gutenberg?filter=5#pages" title="' . __('If you like it, please review the plugin', 'map-block-gutenberg') . '">' . __('Review the plugin', 'map-block-gutenberg') . '</a>'; |
| 88 |
|
| 89 |
if ($file == plugin_basename(__FILE__)) { |
| 90 |
$links[] = $support_link; |
| 91 |
$links[] = $review_link; |
| 92 |
} |
| 93 |
|
| 94 |
return $links; |
| 95 |
} // plugin_meta_links |
| 96 |
|
| 97 |
|
| 98 |
// enqueue block files |
| 99 |
static function enqueue_block_editor_assets() { |
| 100 |
|
| 101 |
// Enqueue the bundled block JS file |
| 102 |
wp_register_script( |
| 103 |
'wf-map-block', |
| 104 |
plugins_url('/assets/js/editor.blocks.js', __FILE__), |
| 105 |
[ 'wp-editor', 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components' ], |
| 106 |
self::$version |
| 107 |
); |
| 108 |
|
| 109 |
$api_key = get_option('gmw-map-block-key')? get_option('gmw-map-block-key'): 'AIzaSyAjyDspiPfzEfjRSS5fQzm-3jHFjHxeXB4'; |
| 110 |
$wf_map_block = array( |
| 111 |
'api_key' => $api_key, |
| 112 |
'_description' => __('Simple yet powerfull map block powered by Google Maps.', 'map-block-gutenberg'), |
| 113 |
'_map' => __('Map', 'map-block-gutenberg'), |
| 114 |
'_map_lc' => __('map', 'map-block-gutenberg'), |
| 115 |
'_location_lc' => __('location', 'map-block-gutenberg'), |
| 116 |
'_address' => __('Address', 'map-block-gutenberg'), |
| 117 |
'_zoom' => __('Zoom', 'map-block-gutenberg'), |
| 118 |
'_height' => __('Height', 'map-block-gutenberg'), |
| 119 |
'_api_key' => __('API Key', 'map-block-gutenberg'), |
| 120 |
'_api_info_start' => __('Please create your own API key on the', 'map-block-gutenberg'), |
| 121 |
'_api_info_console' => __('Google Console', 'map-block-gutenberg'), |
| 122 |
'_api_info_end' => __('This is a requirement enforced by Google.', 'map-block-gutenberg') |
| 123 |
); |
| 124 |
wp_localize_script( 'wf-map-block', 'wf_map_block', $wf_map_block ); |
| 125 |
|
| 126 |
wp_enqueue_script('wf-map-block'); |
| 127 |
|
| 128 |
// Enqueue optional editor only styles |
| 129 |
wp_enqueue_style( |
| 130 |
'wf-map-block', |
| 131 |
plugins_url('/assets/css/blocks.editor.css', __FILE__), |
| 132 |
[ 'wp-editor' ], |
| 133 |
self::$version |
| 134 |
); |
| 135 |
} // enqueue_block_editor_assets |
| 136 |
|
| 137 |
|
| 138 |
// check if Gutenberg is available |
| 139 |
static function check_gutenberg() { |
| 140 |
if (false === defined('GUTENBERG_VERSION') && false === version_compare(get_bloginfo('version'), '5.0', '>=')) { |
| 141 |
add_action('admin_notices', array(__CLASS__, 'notice_gutenberg_missing')); |
| 142 |
return false; |
| 143 |
} |
| 144 |
} // check_gutenberg |
| 145 |
|
| 146 |
|
| 147 |
// complain if Gutenberg is not available |
| 148 |
static function notice_gutenberg_missing() { |
| 149 |
echo '<div class="error"><p><b>Map Block</b> plugin requires the Gutenberg plugin to work. It is after all a block for Gutenberg ;)<br>Install the <a href="https://wordpress.org/plugins/gutenberg/" target="_blank">Gutenberg plugin</a> and this notice will go away.</p></div>'; |
| 150 |
} // notice_gutenberg_missing |
| 151 |
} // class |
| 152 |
|
| 153 |
|
| 154 |
// get the party started |
| 155 |
add_action('init', array('wf_map_block', 'init')); |
| 156 |
add_action('plugins_loaded', array('wf_map_block', 'plugins_loaded')); |
| 157 |
|