-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnelio-maps.php
192 lines (148 loc) · 4.59 KB
/
nelio-maps.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* The plugin bootstrap file
*
* @wordpress-plugin
* Plugin Name: Nelio Maps
* Plugin URI: https://neliosoftware.com
* Description: Simple and beautiful Google Maps block for WordPress.
* Version: 1.0.2
*
* Author: Nelio Software
* Author URI: http://neliosoftware.com
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*
* Text Domain: nelio-maps
*
* @package Nelio_Maps
* @author David Aguilera <[email protected]>
* @since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}//end if
define( 'NELIO_MAPS', true );
function nelio_maps() {
return Nelio_Maps::instance();
}//end nelio_maps()
/**
* Nelio_Maps
*/
class Nelio_Maps {
private static $instance = null;
public $plugin_path;
public $plugin_url;
public $plugin_name;
public $plugin_version;
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
self::$instance->init_options();
self::$instance->init_hooks();
}//end if
return self::$instance;
}//end instance()
public function init_options() {
$this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
$this->plugin_url = untrailingslashit( plugin_dir_url( __FILE__ ) );
// load textdomain.
load_plugin_textdomain( 'nelio-maps' );
}//end init_options()
public function init_hooks() {
add_action( 'admin_init', [ $this, 'admin_init' ] );
if ( ! function_exists( 'register_block_type' ) ) {
return;
}//end if
add_action( 'init', [ $this, 'register_google_maps_api_key_option' ] );
add_filter( 'block_categories', [ $this, 'add_extra_category' ], 99 );
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ], 9 );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_block_assets' ] );
if ( is_admin() ) {
require_once $this->plugin_path . '/options.php';
}//end if
}//end init_hooks()
public function add_extra_category( $categories ) {
if ( count( array_filter( $categories, function( $category ) { return 'extra' === $category['slug']; } ) ) ) {
return $categories;
}//end if
return array_merge(
$categories,
array(
array(
'slug' => 'extra',
'title' => _x( 'Extra', 'text (block category)', 'nelio-maps' ),
),
)
);
}//end add_extra_category()
public function enqueue_block_editor_assets() {
$script_meta = require $this->plugin_path . '/assets/dist/blocks.asset.php';
wp_enqueue_script(
'nelio-maps-blocks',
untrailingslashit( plugin_dir_url( __FILE__ ) ) . '/assets/dist/blocks.js',
$script_meta['dependencies'],
$script_meta['version'],
true
);
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( 'nelio-maps-blocks', 'nelio-maps' );
}//end if
wp_enqueue_style(
'nelio-maps-blocks',
untrailingslashit( plugin_dir_url( __FILE__ ) ) . '/assets/dist/blocks.css',
[],
$this->plugin_version
);
wp_localize_script(
'nelio-maps-blocks',
'NelioMaps',
array(
'googleMapsApiKey' => get_option( 'nelio_maps_api_key_option', '' ),
'optionsPageUrl' => admin_url( 'options-general.php?page=nelio-maps' ),
)
);
}//end enqueue_block_editor_assets()
public function enqueue_block_assets() {
wp_register_script(
'google-maps',
add_query_arg(
array(
'key' => get_option( 'nelio_maps_api_key_option', '' ),
'libraries' => 'geometry,drawing,places',
),
'https://maps.googleapis.com/maps/api/js'
),
[],
$this->plugin_version,
true
);
wp_enqueue_style(
'nelio-maps-gutenberg',
$this->plugin_url . '/assets/dist/blocks.css',
[],
$this->plugin_version
);
$script_meta = require $this->plugin_path . '/assets/dist/public.asset.php';
wp_enqueue_script(
'nelio-maps',
$this->plugin_url . '/assets/dist/public.js',
array_merge( $script_meta['dependencies'], [ 'google-maps' ] ),
$script_meta['version'],
true
);
}//end enqueue_block_assets()
public function admin_init() {
$data = get_file_data( __FILE__, [ 'Plugin Name', 'Version' ], 'plugin' );
$this->plugin_name = $data[0];
$this->plugin_version = $data[1];
$this->plugin_slug = plugin_basename( __FILE__, '.php' );
$this->plugin_name_sanitized = basename( __FILE__, '.php' );
}//end admin_init()
public function register_google_maps_api_key_option() {
$api_key = get_option( 'nelio_maps_api_key_option', '' );
update_option( 'nelio_maps_api_key_option', $api_key );
}//end register_google_maps_api_key_option()
}//end class
// Start plugin.
nelio_maps();