forked from podlove/podlove-publisher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
282 lines (246 loc) · 11.1 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
<?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 );
add_action( 'delete_blog', '\Podlove\delete_blog', 10, 2 );
function activate_for_current_blog() {
Model\Feed::build();
Model\FileType::build();
Model\EpisodeAsset::build();
Model\MediaFile::build();
Model\Episode::build();
Model\Template::build();
Model\DownloadIntent::build();
Model\DownloadIntentClean::build();
Model\UserAgent::build();
Model\GeoArea::build();
Model\GeoAreaName::build();
\podlove_init_capabilities();
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' => 'mp4' ),
array( 'name' => 'M4V Video (Apple)', 'type' => 'video', 'mime_type' => 'video/x-m4v', '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/ogg;codecs=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' => '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' => 'chapters.txt' ),
array( 'name' => 'Podlove Simple Chapters','type' => 'chapters', 'mime_type' => 'application/xml', 'extension' => 'psc' ),
array( 'name' => 'Subrip Captions', 'type' => 'captions', 'mime_type' => 'application/x-subrip', 'extension' => 'srt' ),
array( 'name' => 'WebVTT Captions', 'type' => 'captions', 'mime_type' => 'text/vtt', 'extension' => 'vtt' ),
array( 'name' => 'Auphonic Production Description', 'type' => 'metadata', 'mime_type' => 'application/json', 'extension' => 'json' ),
);
foreach ( $default_types as $file_type ) {
$f = new Model\FileType;
foreach ( $file_type as $key => $value ) {
$f->{$key} = $value;
}
$f->save();
}
}
$podcast = Model\Podcast::get();
if (!$podcast->limit_items) {
$podcast->limit_items = Model\Feed::ITEMS_NO_LIMIT;
}
$podcast->save();
// required for all module hooks to fire correctly
add_option('podlove_active_modules', []);
// set default modules
$default_modules = array( 'logging', 'podlove_web_player', 'open_graph', 'asset_validation', 'oembed', 'feed_validation', 'import_export', 'subscribe_button' );
foreach ( $default_modules as $module ) {
\Podlove\Modules\Base::activate( $module );
}
// set default expert settings
$settings = get_option( 'podlove', array() );
if ( $settings === array() ) {
$settings = array(
'merge_episodes' => 'on',
'hide_wp_feed_discovery' => 'off',
'use_post_permastruct' => 'on',
'episode_archive' => 'on',
'episode_archive_slug' => '/podcast/',
'custom_episode_slug' => '/podcast/%podcast%/'
);
update_option( 'podlove', $settings );
}
// set default template
if (!$template = Model\Template::find_one_by_property('title', 'default')) {
$template = new Model\Template;
$template->title = 'default';
$template->content = <<<EOT
{% if not is_feed() %}
{# display web player for episode #}
{{ episode.player }}
{# display download menu for episode #}
{% include "@core/shortcode/downloads-select.twig" %}
{% endif %}
EOT;
$template->save();
$assignment = Model\TemplateAssignment::get_instance();
$assignment->top = $template->title;
$assignment->save();
}
}
/**
* 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 ) {
switch_to_blog( $blog_id );
$plugin_base_path = implode(DIRECTORY_SEPARATOR, array_slice(explode(DIRECTORY_SEPARATOR, PLUGIN_FILE), -2));
if ( is_plugin_active_for_network( $plugin_base_path ) ) {
activate_for_current_blog();
}
restore_current_blog();
}
/**
* Fires before a blog is deleted.
*
* @param int $blog_id The blog ID.
* @param bool $drop True if blog's table should be dropped.
*/
function delete_blog($blog_id, $drop) {
if ($drop) {
uninstall_for_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($network_wide) {
global $wpdb;
if ( $network_wide ) {
set_time_limit(0); // may take a while, depending on network size
$blogids = $wpdb->get_col( "SELECT blog_id FROM " . $wpdb->blogs );
foreach ( $blogids as $blog_id ) {
\Podlove\with_blog_scope($blog_id, function() {
activate_for_current_blog();
});
}
} else {
activate_for_current_blog();
}
set_transient( 'podlove_needs_to_flush_rewrite_rules', true );
podlove_run_system_report();
}
function deactivate() {
flush_rewrite_rules();
}
/**
* 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( "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() {
global $wpdb;
Model\Feed::destroy();
Model\FileType::destroy();
Model\EpisodeAsset::destroy();
Model\MediaFile::destroy();
Model\Episode::destroy();
Model\Template::destroy();
Model\DownloadIntent::destroy();
Model\DownloadIntentClean::destroy();
Model\UserAgent::destroy();
Model\GeoArea::destroy();
Model\GeoAreaName::destroy();
do_action('podlove_uninstall_plugin');
// trash all episodes
$query = new \WP_Query([ 'post_type' => 'podcast' ]);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
wp_trash_post(get_the_ID());
}
}
wp_reset_postdata();
// delete everything from wp_options
$wpdb->query('DELETE FROM `' . $wpdb->options . '` WHERE option_name LIKE "%podlove%"');
}
/**
* Activate internal modules.
*/
add_action( 'init', array( '\Podlove\Custom_Guid', 'init' ) );
add_action( 'init', array( '\Podlove\Downloads', 'init' ) );
add_action( 'init', array( '\Podlove\Geo_Ip', 'init' ) );
add_action( 'init', array( '\Podlove\DuplicatePost', 'init' ) );
add_action( 'init', array( '\Podlove\Analytics\EpisodeDownloadAverage', 'init' ) );
add_action( 'init', array( '\Podlove\Analytics\DownloadIntentCleanup', 'init' ) );
add_action( 'admin_init', array( '\Podlove\Repair', 'init' ) );
add_action( 'admin_init', array( '\Podlove\PhpDeprecationWarning', 'init' ) );
// init cache (after plugins_loaded, so modules have a chance to hook)
add_action( 'init', array( '\Podlove\Cache\TemplateCache', 'get_instance' ) );
// require_once \Podlove\PLUGIN_DIR . 'includes/about.php';
require_once \Podlove\PLUGIN_DIR . 'includes/cache.php';
require_once \Podlove\PLUGIN_DIR . 'includes/capabilities.php';
require_once \Podlove\PLUGIN_DIR . 'includes/chapters.php';
require_once \Podlove\PLUGIN_DIR . 'includes/cover_art.php';
require_once \Podlove\PLUGIN_DIR . 'includes/deprecations.php';
require_once \Podlove\PLUGIN_DIR . 'includes/detect_duplicate_slugs.php';
require_once \Podlove\PLUGIN_DIR . 'includes/downloads.php';
require_once \Podlove\PLUGIN_DIR . 'includes/explicit_content.php';
require_once \Podlove\PLUGIN_DIR . 'includes/extras.php';
require_once \Podlove\PLUGIN_DIR . 'includes/feed_discovery.php';
require_once \Podlove\PLUGIN_DIR . 'includes/frontend_styles.php';
require_once \Podlove\PLUGIN_DIR . 'includes/http.php';
require_once \Podlove\PLUGIN_DIR . 'includes/images.php';
require_once \Podlove\PLUGIN_DIR . 'includes/import.php';
require_once \Podlove\PLUGIN_DIR . 'includes/jetpack.php';
require_once \Podlove\PLUGIN_DIR . 'includes/license.php';
require_once \Podlove\PLUGIN_DIR . 'includes/merge_episodes.php';
require_once \Podlove\PLUGIN_DIR . 'includes/modules.php';
require_once \Podlove\PLUGIN_DIR . 'includes/no_enclosure_autodiscovery.php';
require_once \Podlove\PLUGIN_DIR . 'includes/permalinks.php';
require_once \Podlove\PLUGIN_DIR . 'includes/recording_date.php';
require_once \Podlove\PLUGIN_DIR . 'includes/redirects.php';
require_once \Podlove\PLUGIN_DIR . 'includes/scripts_and_styles.php';
require_once \Podlove\PLUGIN_DIR . 'includes/search.php';
require_once \Podlove\PLUGIN_DIR . 'includes/system_report.php';
require_once \Podlove\PLUGIN_DIR . 'includes/templates.php';
require_once \Podlove\PLUGIN_DIR . 'includes/theme_helper.php';
require_once \Podlove\PLUGIN_DIR . 'includes/trash.php';
require_once \Podlove\PLUGIN_DIR . 'includes/user_agent_refresh.php';
// @todo: change to internal module
new \Podlove\AJAX\Ajax();