PluginProbe
Map Block for Google Maps / 1.30
Map Block for Google Maps v1.30
trunk 1.0 1.1 1.15 1.16 1.2 1.25 1.26 1.27 1.28 1.30 1.31 1.32 1.33 1.34 1.35
map-block-gutenberg / map-block-gutenberg.php

map-block-gutenberg.php in Map Block for Google Maps 1.30, at map-block-gutenberg.php

220 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.30
7 Author URI: https://www.webfactoryltd.com/
8 Text Domain: map-block-gutenberg
9
10 Copyright 2020 Web factory 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 add_filter('install_plugins_table_api_args_featured', array(__CLASS__, 'featured_plugins_tab'));
58 }
59 } // init
60
61
62 static function save_key() {
63 $key = substr(sanitize_html_class(@$_POST['api_key']), 0, 64);
64 update_option('gmw-map-block-key', $key);
65 echo $key;
66 die();
67 } // save_key
68
69
70 // some things have to be loaded earlier
71 static function plugins_loaded() {
72 self::$version = self::get_plugin_version();
73 } // plugins_loaded
74
75
76 // add links to plugins page
77 static function plugin_action_links($links) {
78 $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>';
79
80 array_unshift($links, $gutenberg_link);
81
82 return $links;
83 } // plugin_action_links
84
85
86 // add links to plugin's description in plugins table
87 static function plugin_meta_links($links, $file) {
88 $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>';
89 $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>';
90
91 if ($file == plugin_basename(__FILE__)) {
92 $links[] = $support_link;
93 $links[] = $review_link;
94 }
95
96 return $links;
97 } // plugin_meta_links
98
99
100 // enqueue block files
101 static function enqueue_block_editor_assets() {
102
103 // Enqueue the bundled block JS file
104 wp_register_script(
105 'wf-map-block',
106 plugins_url('/assets/js/editor.blocks.js', __FILE__),
107 [ 'wp-editor', 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components' ],
108 self::$version
109 );
110
111 $api_key = get_option('gmw-map-block-key')? get_option('gmw-map-block-key'): 'AIzaSyAjyDspiPfzEfjRSS5fQzm-3jHFjHxeXB4';
112 $wf_map_block = array(
113 'api_key' => $api_key,
114 '_description' => __('Simple yet powerfull map block powered by Google Maps.', 'map-block-gutenberg'),
115 '_map' => __('Map', 'map-block-gutenberg'),
116 '_map_lc' => __('map', 'map-block-gutenberg'),
117 '_location_lc' => __('location', 'map-block-gutenberg'),
118 '_address' => __('Address', 'map-block-gutenberg'),
119 '_zoom' => __('Zoom', 'map-block-gutenberg'),
120 '_height' => __('Height', 'map-block-gutenberg'),
121 '_api_key' => __('API Key', 'map-block-gutenberg'),
122 '_api_info_start' => __('Please create your own API key on the', 'map-block-gutenberg'),
123 '_api_info_console' => __('Google Console', 'map-block-gutenberg'),
124 '_api_info_end' => __('This is a requirement enforced by Google.', 'map-block-gutenberg')
125 );
126 wp_localize_script( 'wf-map-block', 'wf_map_block', $wf_map_block );
127
128 wp_enqueue_script('wf-map-block');
129
130 // Enqueue optional editor only styles
131 wp_enqueue_style(
132 'wf-map-block',
133 plugins_url('/assets/css/blocks.editor.css', __FILE__),
134 [ 'wp-editor' ],
135 self::$version
136 );
137 } // enqueue_block_editor_assets
138
139
140 // check if Gutenberg is available
141 static function check_gutenberg() {
142 if (false === defined('GUTENBERG_VERSION') && false === version_compare(get_bloginfo('version'), '5.0', '>=')) {
143 add_action('admin_notices', array(__CLASS__, 'notice_gutenberg_missing'));
144 return false;
145 }
146 } // check_gutenberg
147
148
149 // complain if Gutenberg is not available
150 static function notice_gutenberg_missing() {
151 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>';
152 } // notice_gutenberg_missing
153
154 // helper function for adding plugins to fav list
155 static function featured_plugins_tab($args) {
156 add_filter('plugins_api_result', array(__CLASS__, 'plugins_api_result'), 10, 3);
157
158 return $args;
159 } // featured_plugins_tab
160
161
162 // add single plugin to list of favs
163 static function add_plugin_favs($plugin_slug, $res) {
164 if (!isset($res->plugins) || !is_array($res->plugins)) {
165 return $res;
166 }
167
168 if (!empty($res->plugins) && is_array($res->plugins)) {
169 foreach ($res->plugins as $plugin) {
170 if (is_object($plugin) && !empty($plugin->slug) && $plugin->slug == $plugin_slug) {
171 return $res;
172 }
173 } // foreach
174 }
175
176 $plugin_info = get_transient('wf-plugin-info-' . $plugin_slug);
177
178 if (!$plugin_info) {
179 $plugin_info = plugins_api('plugin_information', array(
180 'slug' => $plugin_slug,
181 'is_ssl' => is_ssl(),
182 'fields' => array(
183 'banners' => true,
184 'reviews' => true,
185 'downloaded' => true,
186 'active_installs' => true,
187 'icons' => true,
188 'short_description' => true,
189 )
190 ));
191 if (!is_wp_error($plugin_info)) {
192 set_transient('wf-plugin-info-' . $plugin_slug, $plugin_info, DAY_IN_SECONDS * 7);
193 }
194 }
195
196 if ($plugin_info && !is_wp_error($plugin_info)) {
197 array_unshift($res->plugins, $plugin_info);
198 }
199
200 return $res;
201 } // add_plugin_favs
202
203
204 // add our plugins to recommended list
205 static function plugins_api_result($res, $action, $args) {
206 remove_filter('plugins_api_result', array(__CLASS__, 'plugins_api_result'), 10, 3);
207
208 $res = self::add_plugin_favs('eps-301-redirects', $res);
209 $res = self::add_plugin_favs('sticky-menu-or-anything-on-scroll', $res);
210 $res = self::add_plugin_favs('wp-force-ssl', $res);
211
212 return $res;
213 } // plugins_api_result
214 } // class
215
216
217 // get the party started
218 add_action('init', array('wf_map_block', 'init'));
219 add_action('plugins_loaded', array('wf_map_block', 'plugins_loaded'));
220