forked from podlove/podlove-publisher
/
plugin.php
426 lines (351 loc) · 13.3 KB
/
plugin.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
namespace Podlove;
register_activation_hook( PLUGIN_FILE, __NAMESPACE__ . '\activate' );
register_deactivation_hook( PLUGIN_FILE, __NAMESPACE__ . '\deactivate' );
register_uninstall_hook( PLUGIN_FILE, __NAMESPACE__ . '\uninstall' );
add_action( 'wpmu_new_blog', '\Podlove\create_new_blog', 10, 6 );
function activate_for_current_blog() {
Model\Feed::build();
Model\FileType::build();
Model\EpisodeAsset::build();
Model\MediaFile::build();
Model\Show::build();
Model\Episode::build();
Model\Release::build();
if ( ! Model\FileType::has_entries() ) {
$default_types = array(
array( 'name' => 'MP3 Audio', 'type' => 'audio', 'mime_type' => 'audio/mpeg', 'extension' => 'mp3' ),
array( 'name' => 'BitTorrent (MP3 Audio)', 'type' => 'audio', 'mime_type' => 'application/x-bittorrent', 'extension' => 'mp3.torrent' ),
array( 'name' => 'MPEG-1 Video', 'type' => 'video', 'mime_type' => 'video/mpeg', 'extension' => 'mpg' ),
array( 'name' => 'MPEG-4 AAC Audio', 'type' => 'audio', 'mime_type' => 'audio/mp4', 'extension' => 'm4a' ),
array( 'name' => 'MPEG-4 ALAC Audio', 'type' => 'audio', 'mime_type' => 'audio/mp4', 'extension' => 'm4a' ),
array( 'name' => 'MPEG-4 Video', 'type' => 'video', 'mime_type' => 'video/mp4', 'extension' => 'm4v' ),
array( 'name' => 'Ogg Vorbis Audio', 'type' => 'audio', 'mime_type' => 'audio/ogg', 'extension' => 'oga' ),
array( 'name' => 'Ogg Vorbis Audio', 'type' => 'audio', 'mime_type' => 'audio/ogg', 'extension' => 'ogg' ),
array( 'name' => 'Ogg Theora Video', 'type' => 'video', 'mime_type' => 'video/ogg', 'extension' => 'ogv' ),
array( 'name' => 'WebM Audio', 'type' => 'audio', 'mime_type' => 'audio/webm', 'extension' => 'webm' ),
array( 'name' => 'WebM Video', 'type' => 'video', 'mime_type' => 'video/webm', 'extension' => 'webm' ),
array( 'name' => 'FLAC Audio', 'type' => 'audio', 'mime_type' => 'audio/flac', 'extension' => 'flac' ),
array( 'name' => 'Opus Audio', 'type' => 'audio', 'mime_type' => 'audio/opus', 'extension' => 'opus' ),
array( 'name' => 'Matroska Audio', 'type' => 'audio', 'mime_type' => 'audio/x-matroska', 'extension' => 'mka' ),
array( 'name' => 'Matroska Video', 'type' => 'video', 'mime_type' => 'video/x-matroska', 'extension' => 'mkv' ),
array( 'name' => 'Matroska Video', 'type' => 'video', 'mime_type' => 'video/x-matroska', 'extension' => 'mkv' ),
array( 'name' => 'PDF Document', 'type' => 'ebook', 'mime_type' => 'application/pdf', 'extension' => 'pdf' ),
array( 'name' => 'ePub Document', 'type' => 'ebook', 'mime_type' => 'application/epub+zip', 'extension' => 'epub' ),
array( 'name' => 'PNG Image', 'type' => 'image', 'mime_type' => 'image/png', 'extension' => 'png' ),
array( 'name' => 'JPEG Image', 'type' => 'image', 'mime_type' => 'image/jpeg', 'extension' => 'jpg' ),
array( 'name' => 'mp4chaps Chapter File', 'type' => 'chapters', 'mime_type' => 'text/plain', 'extension' => 'txt' ),
);
foreach ( $default_types as $file_type ) {
$f = new Model\FileType;
foreach ( $file_type as $key => $value ) {
$f->{$key} = $value;
}
$f->save();
}
}
$default_modules = array( 'podlove_web_player', 'episode_assistant', 'open_graph' );
foreach ( $default_modules as $module ) {
\Podlove\Modules\Base::activate( $module );
}
}
/**
* Hook: Create a new blog in a multisite environment.
*
* When a new blog is created, we have to trigger the activation function
* for in the scope of that blog.
*/
function create_new_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
global $wpdb;
// something like 'podlove/podlove.php'
$plugin_file = basename( dirname( __FILE__ ) ) . '/' . basename( __FILE__ );
if ( is_plugin_active_for_network( $plugin_file ) ) {
$current_blog = $wpdb->blogid;
switch_to_blog( $blog_id );
activate_for_current_blog();
switch_to_blog( $current_blog );
}
}
/**
* Hook: Activate the plugin.
*
* In a single blog install, just call activate_for_current_blog().
* However, in a multisite install, iterate over all blogs and call the activate
* function for each of them.
*/
function activate() {
global $wpdb;
if ( is_multisite() ) {
if ( isset( $_GET['networkwide'] ) && ( $_GET['networkwide'] == 1 ) ) {
$current_blog = $wpdb->blogid;
$blogids = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM " . $wpdb->blogs ) );
foreach ( $blogids as $blog_id ) {
switch_to_blog($blog_id);
activate_for_current_blog();
}
switch_to_blog($current_blog);
} else {
activate_for_current_blog();
}
} else {
activate_for_current_blog();
}
set_transient( 'podlove_needs_to_flush_rewrite_rules', true );
}
/**
* Hackish workaround to flush rewrite rules.
*
* flush_rewrite_rules() is expensive, so it should only be called once.
* However, calling it on activaton doesn't work. So I add a temporary flag
* and call it when the flag exists. Not pretty but it does the job.
*/
add_action( 'admin_init', function () {
if ( delete_transient( 'podlove_needs_to_flush_rewrite_rules' ) )
flush_rewrite_rules();
} );
function deactivate() {
}
/**
* Hook: Uninstall the plugin.
*
* In a single blog install, just call uninstall_for_current_blog().
* However, in a multisite install, iterate over all blogs and call the
* uninstall function for each of them.
*/
function uninstall() {
global $wpdb;
if ( is_multisite() ) {
if ( isset( $_GET['networkwide'] ) && ( $_GET['networkwide'] == 1 ) ) {
$current_blog = $wpdb->blogid;
$blogids = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM " . $wpdb->blogs ) );
foreach ( $blogids as $blog_id ) {
switch_to_blog($blog_id);
uninstall_for_current_blog();
}
switch_to_blog($current_blog);
} else {
activate_for_current_blog();
}
} else {
uninstall_for_current_blog();
}
}
function uninstall_for_current_blog() {
Model\Feed::destroy();
Model\FileType::destroy();
Model\EpisodeAsset::destroy();
Model\MediaFile::destroy();
Model\Show::destroy();
Model\Episode::destroy();
Model\Release::destroy();
}
/**
* Adds feed discover links to WordPress head.
*
* @todo find a better place for this function
*/
function add_feed_discoverability() {
if ( is_admin() )
return;
$feeds = \Podlove\Model\Feed::find_all_by_discoverable( 1 );
foreach ( $feeds as $feed ) {
echo '<link rel="alternate" type="' . $feed->get_content_type() . '" title="' . esc_attr( $feed->title_for_discovery() ) . '" href="' . $feed->get_subscribe_url() . "\" />\n";
}
}
add_action( 'init', function () {
new Podcast_Post_Type();
// priority 2 so they are placed below the WordPress default discovery links
add_action( 'wp_head', '\Podlove\add_feed_discoverability', 2 );
// hide WordPress default link discovery
if ( \Podlove\get_setting( 'hide_wp_feed_discovery' ) === 'on' ) {
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
}
});
add_action( 'init', function () {
if ( is_admin() )
return;
wp_register_style(
'podlove-frontend-css',
plugins_url( 'css/frontend.css', __FILE__ ),
array(),
'1.0'
);
wp_enqueue_style( 'podlove-frontend-css' );
} );
// apply domain mapping plugin where it's essential
add_action( 'plugins_loaded', function () {
if ( function_exists( 'domain_mapping_post_content' ) ) {
add_filter( 'feed_link', 'domain_mapping_post_content', 20 );
add_filter( 'podlove_subscribe_url', 'domain_mapping_post_content', 20 );
}
} );
/**
* This helps to get your blog tidy.
* It's all about "Settings > Reading > Front page displays"
*
* Default: Check "Your latest posts" and we won't change anything.
* However, if you check "A static page", we assume you'd like to separate
* blog and podcast by moving your blog away and the podcast directory to "/".
* That's what we do here.
*
* It's magic. Okay, I should probably document this publicly at some point.
*/
add_filter( 'pre_get_posts', function ( $wp_query ) {
if ( is_home() && $wp_query->is_main_query() && \Podlove\get_setting( 'merge_episodes' ) === 'on' ) {
$wp_query->set( 'post_type', array( 'post', 'podcast' ) );
return $wp_query;
}
if ( get_option( 'show_on_front' ) === 'posts' )
return $wp_query;
if ( $wp_query->get( 'page_id' ) == get_option( 'page_on_front' ) ) {
$wp_query->set( 'post_type', array( 'podcast' ) );
// fix conditional functions
$wp_query->set( 'page_id', '' );
$wp_query->is_page = 0;
$wp_query->is_singular = 0;
}
return $wp_query;
} );
// init modules
add_action( 'plugins_loaded', function () {
$modules = Modules\Base::get_active_module_names();
if ( empty( $modules ) )
return;
foreach ( $modules as $module_name ) {
$class = Modules\Base::get_class_by_module_name( $module_name );
if ( class_exists( $class ) ) {
$class::instance()->load();
} else {
Modules\Base::deactivate( $module_name );
add_action( 'admin_notices', function () use ( $module_name ) {
?>
<div id="message" class="error">
<p>
<strong><?php echo __( 'Warning' ) ?></strong>
<?php echo sprintf( __( 'Podlove Module "%s" could not be found and has been deactivated.', 'podlove' ), $module_name ); ?>
</p>
</div>
<?php
} );
}
}
} );
/**
* Simple method to allow support for multiple urls per post.
*
* Add custom post meta 'podlove_alternate_url' with old url part to match.
*/
function override404() {
global $wpdb, $wp_query;
if ( ! $wp_query->is_404 )
return;
$rows = $wpdb->get_results( "
SELECT
post_id, meta_value url
FROM
" . $wpdb->postmeta . "
WHERE
meta_key = 'podlove_alternate_url'
", ARRAY_A );
foreach ( $rows as $row ) {
if ( false !== stripos( $_SERVER['REQUEST_URI'], $row['url'] ) ) {
status_header( 301 );
$wp_query->is_404 = false;
\wp_redirect( \get_permalink( $row['post_id'] ), 301 );
exit;
}
}
}
add_filter( 'template_redirect', '\Podlove\override404' );
function clear_all_caches() {
// clear WP Super Cache
if ( function_exists( 'wp_cache_clear_cache' ) )
wp_cache_clear_cache();
}
namespace Podlove\AJAX;
use \Podlove\Model;
function validate_file() {
$file_id = $_REQUEST['file_id'];
$file = \Podlove\Model\MediaFile::find_by_id( $file_id );
$info = $file->curl_get_header();
$result = array();
$result['file_id'] = $file_id;
$result['reachable'] = ( $info['http_code'] >= 200 && $info['http_code'] < 300 );
$result['file_size'] = $info['download_content_length'];
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode($result);
die();
}
add_action( 'wp_ajax_podlove-validate-file', '\Podlove\AJAX\validate_file' );
function update_file() {
$file_id = $_REQUEST['file_id'];
$file = \Podlove\Model\MediaFile::find_by_id( $file_id );
$info = $file->determine_file_size();
$file->save();
$result = array();
$result['file_id'] = $file_id;
$result['reachable'] = ( $info['http_code'] >= 200 && $info['http_code'] < 300 );
$result['file_size'] = $info['download_content_length'];
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode($result);
die();
}
add_action( 'wp_ajax_podlove-update-file', '\Podlove\AJAX\update_file' );
function create_file() {
$episode_id = $_REQUEST['episode_id'];
$episode_asset_id = $_REQUEST['episode_asset_id'];
if ( ! $episode_id || ! $episode_asset_id )
die();
$file = Model\MediaFile::find_or_create_by_episode_id_and_episode_asset_id( $episode_id, $episode_asset_id );
$result = array();
$result['file_id'] = $file->id;
$result['file_size'] = $file->size;
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode($result);
die();
}
add_action( 'wp_ajax_podlove-create-file', '\Podlove\AJAX\create_file' );
function create_episode() {
$slug = isset( $_REQUEST['slug'] ) ? $_REQUEST['slug'] : NULL;
$title = isset( $_REQUEST['title'] ) ? $_REQUEST['title'] : NULL;
if ( ! $slug || ! $title )
die();
$args = array(
'post_type' => 'podcast',
'post_title' => $title,
'post_content' => \Podlove\Podcast_Post_Type::$default_post_content
);
// create post
$post_id = wp_insert_post( $args );
// link episode and release
$episode = \Podlove\Model\Episode::find_or_create_by_post_id( $post_id );
$episode->slug = $slug;
$episode->enable = true;
$episode->active = true;
$episode->save();
// activate all media files
$episode_assets = Model\EpisodeAsset::all();
foreach ( $episode_assets as $episode_asset ) {
$media_file = new \Podlove\Model\MediaFile();
$media_file->episode_id = $episode->id;
$media_file->episode_asset_id = $episode_asset->id;
$media_file->save();
}
// generate response
$result = array();
$result['post_id'] = $post_id;
$result['post_edit_url'] = get_edit_post_link( $post_id );
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode( $result );
die();
}
add_action( 'wp_ajax_podlove-create-episode', '\Podlove\AJAX\create_episode' );