-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnxt-timeline.php
More file actions
178 lines (154 loc) · 6.06 KB
/
nxt-timeline.php
File metadata and controls
178 lines (154 loc) · 6.06 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/*
Plugin Name: Animated Timeline
Description: Creates an animated timeline with SVG and JavaScript
Version: 1.2
Author: nexTab & Unleashed Design
Author URI: https://nextab.de
Text Domain: nxt-timeline
*/
#region Enqueue Scripts in frontend
function nxt_timeline_enqueue_scripts() {
global $post;
if (nxt_timeline_should_enqueue()) {
wp_register_script('nxt-timeline', plugin_dir_url(__FILE__) . 'js/nxt-timeline.js', false, filemtime(plugin_dir_path(__FILE__) . 'js/nxt-timeline.js'), true);
$options = get_option('nxt_timeline_options');
if (!empty($options['custom_svg_id'])) {
$options['custom_svg_url'] = wp_get_attachment_url($options['custom_svg_id']);
}
// Per-page selector override
if ($post) {
$page_selector = get_post_meta($post->ID, '_nxt_timeline_selector', true);
if (!empty($page_selector)) {
$options['target_selector'] = $page_selector;
}
}
wp_localize_script('nxt-timeline', 'nxtTimelineOptions', $options);
wp_enqueue_script('nxt-timeline');
}
}
add_action('wp_enqueue_scripts', 'nxt_timeline_enqueue_scripts', 999999);
function nxt_timeline_should_enqueue() {
global $post;
$options = get_option('nxt_timeline_options');
// Check manual enqueue settings first
if (!empty($options['manual_enqueue_enabled'])) {
// Check for all posts
if (!empty($options['enqueue_all_posts']) && is_single()) {
return true;
}
// Check for all pages
if (!empty($options['enqueue_all_pages']) && is_page()) {
return true;
}
// Check for all archives
if (!empty($options['enqueue_all_archives']) && is_archive()) {
return true;
}
// Check for specific posts
if (!empty($options['enqueue_specific_posts']) && is_single()) {
$specific_posts = array_map('intval', array_map('trim', explode(',', $options['enqueue_specific_posts'])));
if (in_array($post->ID, $specific_posts, true)) {
return true;
}
}
// Check for specific pages
if (!empty($options['enqueue_specific_pages']) && is_page()) {
$specific_pages = array_map('intval', array_map('trim', explode(',', $options['enqueue_specific_pages'])));
if (in_array($post->ID, $specific_pages, true)) {
return true;
}
}
// If manual enqueue is enabled but no conditions match, don't enqueue
return false;
}
// Fall back to original content-based detection
if (!$post) return false;
$target_selector = isset($options['target_selector']) ? $options['target_selector'] : '.svg-target';
// Extract class name from selector for content detection (remove dots, spaces, etc.)
$target_class = preg_replace('/[^a-zA-Z0-9_-]/', '', $target_selector);
// Also check for old target_class for backward compatibility
if (empty($target_class) && isset($options['target_class'])) {
$target_class = $options['target_class'];
}
if (empty($target_class)) {
$target_class = 'svg-target';
}
return (strpos($post->post_content, $target_class) !== false);
}
#endregion Enqueue Scripts in frontend
#region Enqueue Scripts in WP backend
function nxt_timeline_enqueue_admin_scripts($hook_suffix) {
if ('settings_page_nxt_timeline' !== $hook_suffix) {
return;
}
wp_enqueue_media();
wp_enqueue_style('wp-color-picker');
wp_enqueue_script('wp-color-picker');
$plugin_dir = plugin_dir_path(__FILE__);
$plugin_url = plugin_dir_url(__FILE__);
// Enqueue admin CSS
wp_enqueue_style(
'nxt-timeline-admin-css',
$plugin_url . 'css/nxt-timeline-admin.css',
array(),
filemtime($plugin_dir . 'css/nxt-timeline-admin.css')
);
wp_enqueue_script(
'nxt-timeline-color-picker',
$plugin_url . 'js/nxt-timeline-color-picker.js',
array('jquery', 'wp-color-picker'),
filemtime($plugin_dir . 'js/nxt-timeline-color-picker.js'),
true
);
wp_enqueue_script(
'nxt-timeline-admin',
$plugin_url . 'js/nxt-timeline-admin.js',
array('jquery', 'wp-color-picker', 'media-upload', 'thickbox'),
filemtime($plugin_dir . 'js/nxt-timeline-admin.js'),
true
);
// Enqueue admin enqueue script
wp_enqueue_script(
'nxt-timeline-admin-enqueue',
$plugin_url . 'js/nxt-timeline-admin-enqueue.js',
array(),
filemtime($plugin_dir . 'js/nxt-timeline-admin-enqueue.js'),
true
);
// Localize script for AJAX
wp_localize_script('nxt-timeline-admin-enqueue', 'nxtTimelineAdmin', array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('nxt_timeline_nonce')
));
}
add_action('admin_enqueue_scripts', 'nxt_timeline_enqueue_admin_scripts');
#endregion Enqueue Scripts in WP backend
// Include admin page functionality
if (is_admin()) {
require_once plugin_dir_path(__FILE__) . 'includes/admin-page.php';
}
#region Per-page selector meta field
function nxt_timeline_register_meta() {
register_post_meta('', '_nxt_timeline_selector', [
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'auth_callback' => function() {
return current_user_can('edit_posts');
},
'sanitize_callback' => 'sanitize_text_field',
]);
}
add_action('init', 'nxt_timeline_register_meta');
function nxt_timeline_enqueue_block_editor_assets() {
wp_enqueue_script(
'nxt-timeline-meta-box',
plugin_dir_url(__FILE__) . 'js/nxt-timeline-meta-box.js',
['wp-plugins', 'wp-edit-post', 'wp-editor', 'wp-components', 'wp-data', 'wp-element'],
filemtime(plugin_dir_path(__FILE__) . 'js/nxt-timeline-meta-box.js'),
true
);
}
add_action('enqueue_block_editor_assets', 'nxt_timeline_enqueue_block_editor_assets');
#endregion Per-page selector meta field