forked from SimonWaldherr/wp-osf-shownotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shownotes.php
407 lines (363 loc) · 14.4 KB
/
shownotes.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/**
* @package Shownotes
* @version 0.5.2.1
*/
/*
Plugin Name: Shownotes
Plugin URI: http://shownot.es/wp-plugin/
Description: Convert OSF-Shownotes to HTML for your Podcast
Author: Simon Waldherr
Version: 0.5.2.1
Author URI: http://waldherr.eu
License: MIT License
*/
include_once 'snsettings.php';
include_once 'OSFphp/osf.php';
include_once 'micromarkdown/micromarkdown.php';
$shownotes_options = get_option('shownotes_options');
$shownotes_cache = array();
function shownotesshortcode_add_styles() {
global $shownotes_options;
if (!is_feed()) {
if (!isset($shownotes_options['css_id'])) {
return false;
}
if ($shownotes_options['css_id'] == '0') {
return false;
}
$css_styles = array(
'',
'style_one',
'style_two',
'style_three',
'style_four',
'style_five'
);
wp_enqueue_style('shownotesstyle', plugins_url('static/' . $css_styles[$shownotes_options['css_id']] . '.css', __FILE__), array(), '0.5.2.1');
}
}
add_action('wp_print_styles', 'shownotesshortcode_add_styles');
function add_shownotes_textarea($post) {
global $shownotes_options;
$post_id = @get_the_ID();
if ($post_id == '') {
return;
}
if (isset($post_id)) {
$shownotes = get_post_meta($post_id, '_shownotes', true);
if (@get_post_meta($post_id, '_shownotesname', true) != '') {
$shownotesname = get_post_meta($post_id, '_shownotesname', true);
} else {
$shownotesname = '';
}
if ($shownotes == '') {
$shownotes = get_post_meta($post_id, 'shownotes', true);
}
} else {
$shownotes = '';
}
$baseurl = 'http://cdn.simon.waldherr.eu/projects/showpad-api/getPad/?id=$$$';
$baseurlstring = '';
$import_podcastname = false;
if (isset($shownotes_options['import_podcastname'])) {
if (trim($shownotes_options['import_podcastname']) != '') {
$import_podcastname = trim($shownotes_options['import_podcastname']);
}
}
if ($import_podcastname == false) {
$baseurlstring = '<input type="text" id="importId" name="shownotesname" class="form-input-tip" size="16" autocomplete="off" value=""> <input type="button" class="button" onclick="importShownotes(document.getElementById(\'shownotes\'), document.getElementById(\'importId\').value, \'' . $baseurl . '\')" value="Import">';
} else {
$baseurlstring = '<select id="importId" name="shownotesname" size="1"></select> <input type="button" class="button" onclick="importShownotes(document.getElementById(\'shownotes\'), document.getElementById(\'importId\').value, \'' . $baseurl . '\')" value="Import"><script>getPadList(document.getElementById(\'importId\'),\'' . $import_podcastname . '\')</script>';
}
echo '<div id="add_shownotes" class="shownotesdiv"><script>var shownotesname = \'' . $shownotesname . '\';</script><p>You can use the following shortcodes in the textarea above: <code>[shownotes]</code>, <code>[shownotes mode="block"]</code>, <code>[shownotes mode="shownoter"]</code>, <code>[shownotes mode="podcaster"]</code>, ...</p><p>In the textarea below you can enter your <a href="http://shownotes.github.io/OSF-in-a-Nutshell/">OSF Show Notes</a>.</p><p id="snstatus"></p><p><textarea id="shownotes" name="shownotes" style="height:280px" class="large-text" onKeyUp="analyzeShownotes();">' . $shownotes . '</textarea></p> <p>ShowPad Import: ' . $baseurlstring . ' <br/><br/> Preview:
<input type="submit" class="button" onclick="previewPopup(document.getElementById(\'shownotes\'), \'html\', false, \''.plugins_url('' , __FILE__ ).'\'); return false;" value="HTML">
<input type="submit" class="button" onclick="previewPopup(document.getElementById(\'shownotes\'), \'chapter\', false, \''.plugins_url('' , __FILE__ ).'\'); return false;" value="Chapter"> <input type="submit" class="button" onclick="previewPopup(document.getElementById(\'shownotes\'), \'audacity\', true, \''.plugins_url('' , __FILE__ ).'\'); return false;" value="Audacity"> <input type="submit" class="button" onclick="previewPopup(document.getElementById(\'shownotes\'), \'reaper\', true, \''.plugins_url('' , __FILE__ ).'\'); return false;" value="Reaper"> | Download:
<input type="submit" class="button" onclick="previewPopup(document.getElementById(\'shownotes\'), \'chapter\', true, \''.plugins_url('' , __FILE__ ).'\'); return false;" value="Chapter"> </p></div>';
}
function save_shownotes() {
$post_id = @get_the_ID();
if ($post_id == '') {
return;
}
$old = get_post_meta($post_id, '_shownotes', true);
if (isset($_POST['shownotes'])) {
$new = $_POST['shownotes'];
$name = @$_POST['shownotesname'];
} else {
return;
}
$shownotes = $old;
if ($new && $new != $old) {
update_post_meta($post_id, '_shownotes', $new);
update_post_meta($post_id, '_shownotesname', $name);
delete_post_meta($post_id, 'shownotes');
$shownotes = $new;
} elseif ('' == $new && $old) {
delete_post_meta($post_id, '_shownotes', $old);
}
}
add_action('add_meta_boxes', function() {
$screens = array(
'post',
'page',
'podcast'
);
foreach ($screens as $screen) {
add_meta_box('shownotesdiv-', __('Shownotes', 'podlove'), 'add_shownotes_textarea', $screen, 'advanced', 'default');
}
});
add_action('save_post', 'save_shownotes');
function osf_shownotes_shortcode($atts, $content = '') {
global $shownotes_options;
global $shownotes_cache;
$export = '';
$post_id = get_the_ID();
$shownotes = get_post_meta($post_id, '_shownotes', true);
if ($shownotes == '') {
$shownotes = get_post_meta($post_id, 'shownotes', true);
}
if (isset($shownotes_options['main_tags_mode'])) {
$tags_mode = trim($shownotes_options['main_tags_mode']);
} else {
$tags_mode = 'include';
}
if (isset($shownotes_options['main_tags'])) {
$default_tags = trim($shownotes_options['main_tags']);
} else {
$default_tags = '';
}
if (isset($shownotes_options['main_tags'])) {
$feed_tags = trim($shownotes_options['main_tags']);
} else {
$feed_tags = '';
}
extract(shortcode_atts(array(
'template' => $shownotes_options['main_mode'],
'mode' => $shownotes_options['main_mode'],
'tags_mode' => $tags_mode,
'tags' => $default_tags,
'feedtags' => $feed_tags
), $atts));
$randomSupport = rand(0,10);
if (($content !== '') || ($shownotes)) {
if ((isset($shownotes_options['affiliate_amazon']) && $shownotes_options['affiliate_amazon'] != '') && ($randomSupport < 7)) {
$amazon = $shownotes_options['affiliate_amazon'];
} else {
$amazon = 'shownot.es-21';
}
if (isset($shownotes_options['affiliate_thomann']) && $shownotes_options['affiliate_thomann'] != '') {
$thomann = $shownotes_options['affiliate_thomann'];
} else {
$thomann = '93439';
}
if (isset($shownotes_options['affiliate_tradedoubler']) && $shownotes_options['affiliate_tradedoubler'] != '') {
$tradedoubler = $shownotes_options['affiliate_tradedoubler'];
} else {
$tradedoubler = '16248286';
}
$fullmode = 'false';
if (is_feed()) {
$tags = $feedtags;
}
if ($tags == '') {
$fullmode = 'true';
$fullint = 2;
$tags = explode(' ', 'chapter section spoiler topic embed video audio image shopping glossary source app title quote link podcast news');
} else {
$fullint = 1;
$tags = explode(' ', $tags);
}
if (!isset($shownotes_options['main_untagged'])) {
$fullint = 2;
$fullmode = 'true';
} else {
$fullint = 1;
$fullmode = 'false';
}
$data = array(
'amazon' => $amazon,
'thomann' => $thomann,
'tradedoubler' => $tradedoubler,
'fullmode' => $fullmode,
'tagsmode' => $tags_mode,
'tags' => $tags
);
//undo fucking wordpress shortcode cripple shit
if ($content !== '') {
$shownotesString = htmlspecialchars_decode(str_replace('<br />', '', str_replace('<p>', '', str_replace('</p>', '', $content))));
} else {
$shownotesString = "\n" . $shownotes . "\n";
}
//parse shortcode as osf string to html
if ($template !== $shownotes_options['main_mode']) {
$mode = $template;
}
if ($mode == 'block') {
$mode = 'block style';
}
if ($mode == 'list') {
$mode = 'list style';
}
if ($mode == 'osf') {
$mode = 'clean osf';
}
if (isset($shownotes_cache[$post_id])) {
$shownotesArray = $shownotes_cache[$post_id];
} else {
$shownotesArray = osf_parser($shownotesString, $data);
}
if (($mode == 'block style') || ($mode == 'button style')) {
$export = osf_export_block($shownotesArray['export'], $fullint, $mode);
} elseif ($mode == 'list style') {
$export = osf_export_list($shownotesArray['export'], $fullint, $mode);
} elseif ($mode == 'clean osf') {
$export = osf_export_osf($shownotesArray['export'], $fullint, $mode);
} elseif ($mode == 'glossary') {
$export = osf_export_glossary($shownotesArray['export'], $fullint);
} elseif (($mode == 'shownoter') || ($mode == 'podcaster')) {
if (isset($shownotesArray['header'])) {
if ($mode == 'shownoter') {
$export = osf_get_persons('shownoter', $shownotesArray['header']);
$export = $export['html'];
} elseif ($mode == 'podcaster') {
$export = osf_get_persons('podcaster', $shownotesArray['header']);
$export = $export['html'];
}
}
}
if (isset($_GET['debug']) && (!is_feed())) {
$export .= '<textarea>' . json_encode($shownotesArray) . '</textarea><textarea>' . print_r($shownotes_options, true) . htmlspecialchars($shownotesString) . '</textarea>';
}
if (!is_feed()) {
$export .= '<div style="display:none;visibility:hidden;" class="mp4chaps">' . trim(osf_export_chapterlist($shownotesArray['export'])) . '</div>';
}
}
return $export;
}
function md_shownotes_shortcode($atts, $content = '') {
$post_id = get_the_ID();
$shownotes = get_post_meta($post_id, '_shownotes', true);
if ($shownotes == '') {
$shownotes = get_post_meta($post_id, 'shownotes', true);
}
if ($content !== '') {
$shownotesString = htmlspecialchars_decode(str_replace('<br />', '', str_replace('<p>', '', str_replace('</p>', '', $content))));
} else {
$shownotesString = "\n" . $shownotes . "\n";
}
return micromarkdown($shownotesString);
}
if (!isset($shownotes_options['main_osf_shortcode'])) {
$osf_shortcode = 'shownotes';
} else {
$osf_shortcode = $shownotes_options['main_osf_shortcode'];
}
if (!isset($shownotes_options['main_md_shortcode'])) {
$md_shortcode = 'md-shownotes';
} else {
$md_shortcode = $shownotes_options['main_md_shortcode'];
}
add_shortcode($md_shortcode, 'md_shownotes_shortcode');
add_shortcode($osf_shortcode, 'osf_shownotes_shortcode');
if ($osf_shortcode != 'osf-shownotes') {
add_shortcode('osf-shownotes', 'osf_shownotes_shortcode');
}
function shownotesshortcode_add_admin_scripts() {
if (!is_feed()) {
wp_enqueue_script('majax', plugins_url('static/majaX/majax.js', __FILE__), array(), '0.5.2.1', false);
wp_enqueue_script('importPad', plugins_url('static/shownotes_admin.js', __FILE__), array(), '0.5.2.1', false);
wp_enqueue_script('tinyosf', plugins_url('static/tinyOSF/tinyosf.js', __FILE__), array(), '0.5.2.1', false);
wp_enqueue_script('tinyosf_exportmodules', plugins_url('static/tinyOSF/tinyosf_exportmodules.js', __FILE__), array(), '0.5.2.1', false);
}
}
function shownotesshortcode_add_scripts() {
if (!is_feed()) {
wp_enqueue_script('importPad', plugins_url('static/shownotes.js', __FILE__), array(), '0.5.2.1', false);
}
}
if (is_admin()) {
add_action('wp_print_scripts', 'shownotesshortcode_add_admin_scripts');
}
add_action('wp_print_scripts', 'shownotesshortcode_add_scripts');
/*
* deprecated; see below
*/
function custom_search_query( $query ) {
$custom_fields = array(
'_shownotes'
);
$searchterm = $query->query_vars['s'];
$query->query_vars['s'] = '';
if ($searchterm != '') {
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $searchterm,
'compare' => 'LIKE'
));
}
$query->set('meta_query', $meta_query);
}
}
/*
* new search function for the shownotes that doesn't replace the posts query but extends it
*/
function shownotes_search_where($query) {
// if we are on a search page, modify the generated SQL
if (is_search() && !is_admin()) {
global $wpdb;
$custom_fields = array('_shownotes');
$keywords = explode(' ', get_query_var('s')); // build an array from the search string
$shownotes_query = "";
foreach ($custom_fields as $field) {
foreach ($keywords as $word) {
$shownotes_query .= "((mypm1.meta_key = '".$field."')";
$shownotes_query .= " AND (mypm1.meta_value LIKE '%{$word}%')) OR ";
}
}
// if the shownotes query is not an empty string, append it to the existing query
if (!empty($shownotes_query)) {
// add to where clause
$query['where'] = str_replace("(((wp_posts.post_title LIKE '%", "( {$shownotes_query} ((wp_posts.post_title LIKE '%", $query['where']);
$query['join'] = $query['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
}
}
return ($query);
}
/*
* we need this filter to add a grouping to the SQL string - prevents duplicate result rows
*/
function shownotes_groupby($groupby){
global $wpdb;
// group by post id to avoid multiple results in the modified search
$groupby_id = "{$wpdb->posts}.ID";
// if this is not a search or the groupby string already contains our groupby string, just return
if(!is_search() || strpos($groupby, $groupby_id) !== false) {
return $groupby;
}
// if groupby is empty, use ours
if(strlen(trim($groupby)) === 0) {
return $groupby_id;
}
// groupby wasn't empty, append ours
return $groupby.", ".$groupby_id;
}
function add_title_custom_field($postid){
if (isset($_POST['shownotes'])) {
update_post_meta($postid, '_shownotes', $_POST['shownotes']);
}
if (isset($_POST['content'])) {
update_post_meta($postid, 'post_content', $_POST['content']);
}
}
if (isset($shownotes_options['main_snsearch'])) {
//add_filter('pre_get_posts', 'custom_search_query');
}
if (isset($shownotes_options['main_snsearch'])) {
add_filter('posts_clauses', 'shownotes_search_where', 20, 1);
add_filter('posts_groupby', 'shownotes_groupby');
}
add_action('save_post', 'add_title_custom_field');
?>