-
Notifications
You must be signed in to change notification settings - Fork 4
/
skiptotimestamp.php
330 lines (294 loc) · 9.39 KB
/
skiptotimestamp.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/*
Plugin Name: Skip to Timestamp
Plugin URI: http://github.com/doytch/SkipToTimestamp
Description: Adds clickable timestamps via shortcode or search-and-replace that skip to a time in a media player.
Version: 1.4.4
Author: Mark Deutsch
Author URI: http://github.com/doytch
License: GPLv2
*/
/* --- Logging --- */
// function qed_stt_logger( $msg, $name = '' )
// {
// // Print the name of the calling function if $name is left empty
// $trace = debug_backtrace();
// $name = ( '' == $name ) ? $trace[1]['function'] : $name;
// $error_dir = '/Applications/MAMP/logs/php_error.log';
// $msg = print_r( $msg, true );
// $log = $name . " | " . $msg . "\n";
// error_log( $log, 3, $error_dir );
// }
/* --- Installation --- */
register_activation_hook(__FILE__, 'qed_stt_install');
function qed_stt_install() {
$default_settings = array(
'only_link_singular' => 1,
'link_audio' => 1,
'link_video' => 1,
'link_youtube' => 1,
'auto_replace_ts' => 0
);
update_option('qed_stt_settings', $default_settings);
}
/* --- Uninstallation --- */
// register_deactivation_hook(__FILE__, 'qed_stt_uninstall')
/* --- Youtube --- */
// Enable Youtube's Javascript API for videos embedded with Wordpress' builtin oembeds
add_filter('oembed_result', 'qed_stt_enable_yt_jsapi');
function qed_stt_enable_yt_jsapi($html) {
if (strstr($html, 'youtube.com/embed/')) {
$html = str_replace('?feature=oembed', '?feature=oembed&enablejsapi=1', $html);
}
return $html;
}
/* --- Shortcode --- */
add_shortcode('skipto', 'qed_stt_shortcode');
function qed_stt_shortcode($attr, $content) {
$options = get_option('qed_stt_settings');
// Only link singular posts if the option is set
if (!is_singular() && $options['only_link_singular']) {
return $content;
}
// Cover a few attributes for ease of use
$time = -1;
if (isset($attr['time'])) {
$time = $attr['time'];
} else if (isset($attr['timestamp'])) {
$time = $attr['timestamp'];
} else if (isset($attr['ts'])) {
$time = $attr['ts'];
}
if ($time == -1) {
return $content;
} else {
return "<a href=\"javascript:void(0)\" class=\"qed_stt_tslink\" data-stt-time=\"{$time}\">{$content}</a>";
}
}
function qed_stt_post_has_shortcode() {
$the_post = get_post(get_the_ID());
if (stripos($the_post->post_content, '[skipto ') !== false) {
return true;
} else {
return false;
}
}
/* --- Admin Page --- */
// Create the settings submenu
add_action('admin_menu', 'qed_stt_create_menu');
function qed_stt_create_menu() {
add_options_page(
'Skip to Timestamp',
'Skip to Timestamp',
'manage_options',
'skiptotimestamp',
'qed_stt_create_settings_page'
);
}
// Draw the settings page
function qed_stt_create_settings_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Skip to Timestamp</h2>
<form action="options.php" method="post">
</div>
<?php settings_fields('qed_stt_settings'); ?>
<?php do_settings_sections('skiptotimestamp'); ?>
<input name="Submit" type="submit" value="Save Changes" />
</form></div>
<?php
}
// Register and define settings
add_action('admin_init', 'qed_stt_admin_init');
function qed_stt_admin_init() {
register_setting(
'qed_stt_settings',
'qed_stt_settings',
'qed_stt_validate_settings'
);
add_settings_section(
'qed_stt_main',
'Skip to Timestamp Settings',
'qed_stt_settings_text',
'skiptotimestamp'
);
add_settings_field(
'qed_stt_only_link_singular',
'Link only on singular posts/pages',
'qed_stt_only_link_singular_select_create',
'skiptotimestamp',
'qed_stt_main'
);
add_settings_field(
'qed_stt_link_audio',
'Skip in embedded audio',
'qed_stt_link_audio_create',
'skiptotimestamp',
'qed_stt_main'
);
add_settings_field(
'qed_stt_link_video',
'Skip in embedded video',
'qed_stt_link_video_create',
'skiptotimestamp',
'qed_stt_main'
);
add_settings_field(
'qed_stt_link_youtube',
'Skip in embedded Youtube videos',
'qed_stt_link_youtube_create',
'skiptotimestamp',
'qed_stt_main'
);
add_settings_field(
'qed_stt_auto_replace_ts',
'Replace Timestamps Automatically',
'qed_stt_auto_replace_ts_create',
'skiptotimestamp',
'qed_stt_main'
);
}
function qed_stt_settings_text() {}
function qed_stt_only_link_singular_select_create() {
$options = get_option('qed_stt_settings');
$only_link_singular = $options['only_link_singular'];
echo "<input name='qed_stt_settings[only_link_singular]' type='checkbox'";
if ($only_link_singular) echo ' checked ';
echo "/>Only generate links on singular posts/pages.";
}
function qed_stt_link_audio_create() {
$options = get_option('qed_stt_settings');
$link_audio = $options['link_audio'];
echo "<input name='qed_stt_settings[link_audio]' type='checkbox'";
if ($link_audio) echo ' checked ';
echo "/>Skip to timestamp in audio embedded with the [audio] shortcode or <audio> HTML5 tag.";
}
function qed_stt_link_video_create() {
$options = get_option('qed_stt_settings');
$link_video = $options['link_video'];
echo "<input name='qed_stt_settings[link_video]' type='checkbox'";
if ($link_video) echo ' checked ';
echo "/>Skip to timestamp in video embedded with the [video] shortcode or <video> HTML5 tag.";
}
function qed_stt_link_youtube_create() {
$options = get_option('qed_stt_settings');
$link_youtube = $options['link_youtube'];
echo "<input name='qed_stt_settings[link_youtube]' type='checkbox'";
if ($link_youtube) echo ' checked ';
echo "/>Skip to timestamp in embedded Youtube videos.";
}
function qed_stt_auto_replace_ts_create() {
$options = get_option('qed_stt_settings');
$auto_replace_ts = $options['auto_replace_ts'];
echo "<input name='qed_stt_settings[auto_replace_ts]' type='checkbox'";
if ($auto_replace_ts) echo ' checked ';
echo "/><br><i>Normally you create hyperlinks using the [skipto] shortcode. Check this and text formatted like";
echo " '3:45' in your posts will get automatically replaced with a hyperlink.</i>";
}
function qed_stt_validate_settings($input) {
$valid = array(
'only_link_singular' => isset($input['only_link_singular']) && true == $input['only_link_singular'] ? true : false,
'link_audio' => isset($input['link_audio']) && true == $input['link_audio'] ? true : false,
'link_video' => isset($input['link_video']) && true == $input['link_video'] ? true : false,
'link_youtube' => isset($input['link_youtube']) && true == $input['link_youtube'] ? true : false,
'auto_replace_ts' => isset($input['auto_replace_ts']) && true == $input['auto_replace_ts'] ? true : false
);
return $valid;
}
/* --- Metaboxes --- */
add_action( 'add_meta_boxes', 'qed_stt_add_metabox' );
add_action( 'save_post', 'qed_stt_save_metabox');
add_action( 'wp_ajax_qed_stt_ajax_get_page_settings', 'qed_stt_get_page_settings');
/**
* Register the metabox
*
*/
function qed_stt_add_metabox($post_type) {
$screens = array( 'post', 'page');
foreach ($screens as $screen) {
add_meta_box(
'stt_post_mb',
__('Skip to Timestamp Configuration', 'stt'),
'qed_stt_create_metabox',
$screen,
'normal',
'high'
);
}
}
function qed_stt_create_metabox($post) {
wp_nonce_field('qed_stt_post_mb', 'qed_stt_post_mb_nonce');
?>
<table id='qed-stt-mb-table' class='form-table'>
<tr valign='top'>
<th scope='row'><?php _e('Disable Automatic Links'); ?></th>
<td>
<input type='checkbox' id='qed-stt-disable-auto-link' name='qed-stt-disable-auto-link' valu
<?php echo get_post_meta(get_the_ID(), 'qed-stt-disable-auto-link', true) ? 'checked' : '' ?>
/>
</td>
</tr>
</table>
<?php
}
function qed_stt_save_metabox($post_id) {
// Verify the nonce to ensure we're getting called from the right spot.
if (!isset($_POST['qed_stt_post_mb_nonce'])) {
return $post_id;
}
$nonce = $_POST['qed_stt_post_mb_nonce'];
if (!wp_verify_nonce($nonce, 'qed_stt_post_mb')) {
return $post_id;
}
update_post_meta($post_id, 'qed-stt-disable-auto-link', isset($_POST['qed-stt-disable-auto-link']));
}
/* --- Automatic Hyperlinking --- */
add_action('plugins_loaded', 'qed_stt_loaded');
function qed_stt_loaded() {
add_filter('the_content', 'qed_stt_hyperlink_timestamps');
}
add_action('wp_enqueue_scripts', 'qed_stt_enqueue_scripts');
function qed_stt_enqueue_scripts() {
wp_enqueue_style( 'thickbox');
wp_enqueue_script('qed-stt-youtube', 'https://www.youtube.com/iframe_api');
wp_enqueue_script('qed-stt-js', plugin_dir_url(__FILE__).'/js/skiptotimestamp.js');
// Expose our settings to our Javascript
wp_localize_script('qed-stt-js', 'STTSettings', get_option('qed_stt_settings'));
}
function qed_stt_hyperlink_timestamps($content) {
// Don't autolink if it's turned off.
$options = get_option('qed_stt_settings');
if (!$options['auto_replace_ts']) {
return $content;
}
// Don't autolink if singular page and only linking on singular pages is turned on.
if (!is_singular() && $options['only_link_singular']) {
return $content;
}
// Don't autolink if they've turned off autolinking for this page.
if (get_post_meta(get_the_ID(), 'qed-stt-disable-auto-link', true)) {
return $content;
}
// Don't allow shortcodes and autolinks in the same post
if (qed_stt_post_has_shortcode()) {
return $content;
}
$content = preg_replace(
"/(?:(?:(?<hh>\d{1,2})[:.])?(?<mm>\d{1,2})[:.])(?<ss>\d{1,2})/",
'<a href="javascript:void()" class="qed_stt_tslink" data-stt-time="$0">$0</a>',
$content
);
return $content;
}
add_action('wp_footer', 'qed_stt_custom_css');
function qed_stt_custom_css() {
?>
<style>
.qed_stt_tslink {
cursor: pointer;
}
</style>
<?php
}
?>