-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-interactive-maps.php
More file actions
161 lines (144 loc) · 4.74 KB
/
Copy pathwp-interactive-maps.php
File metadata and controls
161 lines (144 loc) · 4.74 KB
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
<?php
/**
* Plugin Name: WP Interactive Maps
* Plugin URI: https://example.com/wp-interactive-maps
* Description: Create interactive maps with clickable locations (points and areas) for WordPress sites
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-interactive-maps
* Domain Path: /languages
* Requires at least: 5.8
* Requires PHP: 7.4
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Plugin version.
*/
define( 'WIM_VERSION', '1.0.0' );
/**
* Plugin root directory path.
*/
define( 'WIM_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
/**
* Plugin root directory URL.
*/
define( 'WIM_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
/**
* Plugin basename.
*/
define( 'WIM_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
/**
* Activation hook.
* Runs when the plugin is activated.
*/
function wim_activate() {
// Load required files for activation
require_once WIM_PLUGIN_DIR . 'includes/class-sanitization.php';
require_once WIM_PLUGIN_DIR . 'includes/class-post-types.php';
// Trigger post type registration
$post_types = new WIM_Post_Types();
$post_types->register_post_types();
$post_types->register_meta_fields();
// Flush rewrite rules to register custom post types
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'wim_activate' );
/**
* Deactivation hook.
* Runs when the plugin is deactivated.
*/
function wim_deactivate() {
// Flush rewrite rules to clean up custom post types
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'wim_deactivate' );
/**
* Initialize the plugin.
* Load all required files and initialize components.
*/
function wim_init() {
// Load plugin classes
require_once WIM_PLUGIN_DIR . 'includes/class-sanitization.php';
require_once WIM_PLUGIN_DIR . 'includes/class-post-types.php';
require_once WIM_PLUGIN_DIR . 'includes/class-meta-boxes.php';
require_once WIM_PLUGIN_DIR . 'includes/class-rest-api.php';
require_once WIM_PLUGIN_DIR . 'includes/class-shortcode.php';
require_once WIM_PLUGIN_DIR . 'includes/class-gutenberg-block.php';
require_once WIM_PLUGIN_DIR . 'includes/class-settings.php';
// Note: All classes are instantiated within their respective files
// This ensures proper initialization order and hook registration
}
add_action( 'plugins_loaded', 'wim_init' );
/**
* Enqueue admin scripts and styles.
*/
function wim_enqueue_admin_assets( $hook ) {
// Only load on our post type edit screens
global $post_type;
if ( ! in_array( $post_type, array( 'wim_map', 'wim_location' ) ) ) {
return;
}
wp_enqueue_style(
'wim-admin-styles',
WIM_PLUGIN_URL . 'admin/css/admin-styles.css',
array(),
WIM_VERSION
);
}
add_action( 'admin_enqueue_scripts', 'wim_enqueue_admin_assets' );
/**
* Register frontend scripts and styles.
* Only registers assets - they will be enqueued when needed.
*/
function wim_register_frontend_assets() {
if ( ! is_admin() ) {
wp_register_style(
'wim-map-display',
WIM_PLUGIN_URL . 'public/css/map-display.css',
array(),
WIM_VERSION
);
wp_register_script(
'wim-map-display',
WIM_PLUGIN_URL . 'public/js/map-display.js',
array(),
WIM_VERSION,
true
);
}
}
add_action( 'wp_enqueue_scripts', 'wim_register_frontend_assets', 5 );
/**
* Localize frontend script data.
* This runs after scripts are enqueued to ensure localization works properly.
*/
function wim_localize_frontend_script() {
if ( ! is_admin() && wp_script_is( 'wim-map-display', 'enqueued' ) ) {
// Get plugin settings
$settings = WIM_Settings::get_settings();
// Localize script with REST API URL and settings
wp_localize_script(
'wim-map-display',
'wimData',
array(
'restUrl' => rest_url( 'wim/v1' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'settings' => array(
'markerColor' => $settings['marker_color'],
'areaFillColor' => $settings['area_fill_color'],
'areaStrokeColor' => $settings['area_stroke_color'],
'areaFillOpacity' => $settings['area_fill_opacity'] / 100,
'defaultLayout' => $settings['default_layout'],
'customCss' => ! empty( $settings['custom_css'] ) ? $settings['custom_css'] : '',
)
)
);
}
}
add_action( 'wp_enqueue_scripts', 'wim_localize_frontend_script', 20 );