-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmp3-to-post.php
363 lines (322 loc) · 12.2 KB
/
mp3-to-post.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
<?php
/*
Plugin Name: MP3 to Post
Plugin URI: http://www.fractured-state.com/2011/09/mp3-to-post-plugin/
Description: Creates posts using ID3 information in MP3 files.
Author: Paul Sheldrake
Version: 1.3.0
Author URI: http://www.fractured-state.com
*/
/**
* Variables, store them in the options array to grab as necessary
*/
$uploadsDetails = wp_upload_dir();
$mp3FolderName = 'mp3-to-post';
$folderPath = $uploadsDetails['basedir'] . '/' . $mp3FolderName;
$base_path = parse_url($uploadsDetails['baseurl'], PHP_URL_PATH);
$mp3ToPostOptions = array(
'folder_name' => $mp3FolderName,
'folder_path' => $folderPath,
'base_url_path' => $base_path,
);
update_option('mp3-to-post', serialize($mp3ToPostOptions));
/* create the menu item and link to to an admin function */
function mp3_admin_actions() {
add_options_page(__('MP3 to Post','mp3-to-post'), __('MP3 to Post','mp3-to-post'), 1, "mp3-to-post", "mp3_admin");
}
/* add the menu item */
add_action('admin_menu', 'mp3_admin_actions');
/**
* Creates the admin page for the plugin
*
*/
function mp3_admin() {
/**
* Add the ID3 library. Adding it here so it's only used as needed
* http://wordpress.org/support/topic/plugin-blubrry-powerpress-podcasting-plugin-conflict-with-mp3-to-post-plugin?replies=1#post-2833002
*/
require_once('getid3/getid3.php');
?>
<div class="wrap">
<h2>MP3 to Post</h2>
<?php
// load our variables in to an array
$mp3ToPostOptions = unserialize(get_option('mp3-to-post'));
?>
<p><?php _e('This plugin will scan for MP3 files in the directory below and then add them as posts. It takes the ID3v2 title and comment and sets it as the post title and content respectively. It also takes the file and attaches it to the post and adds a link to the post content.','mp3-to-post'); ?></p>
<p><?php _e('The way the ID3 information is processed, <strong>the file needs to have the title and comment set in v1 and v2</strong>','mp3-to-post'); ?></p>
<p><?php _e('If the genre is set on the file, that will be turned in to the category. If more than one genre is set in the ID3 information MP3 to Post only takes the first one. If the genre is not set the category on the post is set to the default option.','mp3-to-post'); ?></p>
<?php create_folder($mp3ToPostOptions['folder_path']); ?>
<p><?php _e('Upload your files here:','mp3-to-post'); ?>
<?php echo $mp3ToPostOptions['base_url_path'] . '/' .
$mp3ToPostOptions['folder_name']; ?> </p>
<form method="post" action="">
<table class="form-table">
<tbody>
<tr>
<th scope="row">Settings</th>
<td>
<label>
<input type="checkbox" name="publish-automatically" value="1" />
<?php _e('Publish post(s) automatically (leave unchecked to set post(s) to draft)','mp3-to-post') ?>
</label>
</td>
</tr>
<tr>
<th scope="row"></th>
<td>
<input type="submit" class="button-primary" name="create-all-posts" value="<?php _e('Create All Posts','mp3-to-post') ?>" />
<input type="submit" class="button-primary" name="create-first-post" value="<?php _e('Create 1st Post','mp3-to-post') ?>" />
</td>
</tr>
</tbody>
</table>
</form>
<?php
// create some posts already!
if (isset($_POST['create-all-posts'])) {
echo '<pre>';
print_r(mp3_to_post('all', $mp3ToPostOptions['folder_path']));
echo '</pre>';
}
if (isset($_POST['create-first-post'])) {
echo '<pre>';
print_r(mp3_to_post(1, $mp3ToPostOptions['folder_path']));
echo '</pre>';
}
// end POST check
?>
<hr />
<h3><?php _e('Files listed in the order they will be added','mp3-to-post'); ?></h3>
<ol>
<?php
// get files
$mp3Files = mp3_array($mp3ToPostOptions['folder_path']);
// list files and details
foreach ($mp3Files as $file) {
$filePath = $mp3ToPostOptions['folder_path'].'/'.$file;
$id3Details = get_ID3($filePath);
echo '<li>
<strong>' . $file . '</strong>
<ul>
<li><strong>' . __('Title:', 'mp3-to-post') . '</strong> '.$id3Details['title'].'</li>
<li><strong>' . __('Category:', 'mp3-to-post') . '</strong> '.$id3Details['category'].'</li>
<li><strong>' . __('Comment:', 'mp3-to-post') . '</strong> '.$id3Details['comment'].'</li>
</ul>
</li>';
}
?>
</ol>
</div>
<?php
}
// end mp3_admin
/**
* Adds a select query that lets you search for titles more easily using WP Query
*/
function title_like_posts_where($where, &$wp_query) {
global $wpdb;
if ($post_title_like = $wp_query->get('post_title_like')) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' .
esc_sql(like_escape($post_title_like)) . '%\'';
}
return $where;
}
add_filter('posts_where', 'title_like_posts_where', 10, 2);
/**
* Takes a string and only returns it if it has '.mp3' in it.
*
* @param $string
* A string, possibly containing .mp3
*
* @return
* Returns a string. Only if it contains '.mp3' or it returns FALSE
*/
function mp3_only($filename) {
$findme = '.mp3';
$pos = strpos($filename, $findme);
if ($pos !== false) {
return $filename;
} else {
return FALSE;
}
}
/**
* Creates a post from an mp3 file.
*
* @param $limit
* Limits the number of items created at one time. Use an intager
*
* @param $path
* The base path to the folder containing the mp3s to convert to posts
*
* @return $array
* Will provide an array of messages
*/
function mp3_to_post($limit = 'all', $folderPath) {
$messages = array();
// get an array of mp3 files
$mp3Files = mp3_array($folderPath);
// check of there are files to process
if(count($mp3Files) == 0){
array_push($messages, _e('There are no files to process', 'mp3-to-post'));
return $messages;
}
// Initialize getID3 engine
$getID3 = new getID3;
// loop through all the files and create posts
$i = 0;
if ($limit == 'all') {
$limit = count($mp3Files) - 1;
} else {
$limit--; // subtract one to work with arrays
}
while ($i <= $limit):
// Analyze file and store returned data in $ThisFileInfo
$filePath = $folderPath . '/' . $mp3Files[$i];
$ThisFileInfo = $getID3->analyze($filePath);
/*
Optional: copies data from all subarrays of [tags] into [comments] so
metadata is all available in one location for all tag formats
metainformation is always available under [tags] even if this is not called
*/
getid3_lib::CopyTagsToComments($ThisFileInfo);
$title = $ThisFileInfo['tags']['id3v2']['title'][0];
$category = $ThisFileInfo['tags']['id3v2']['genre'][0];
$comment = $ThisFileInfo['tags']['id3v2']['comments'][0];
// check if we have a title and a comment
if ($title && $comment){
// check if post exists by search for one with the same title
$searchArgs = array(
'post_title_like' => $title
);
$titleSearchResult = new WP_Query($searchArgs);
// If there are no posts with the title of the mp3 then make the post
if ($titleSearchResult->post_count == 0) {
// create basic post with info from ID3 details
$my_post = array(
'post_title' => $title,
'post_content' => $comment,
'post_author' => 1,
'post_name' => $title,
);
// Insert the post!!
$postID = wp_insert_post($my_post);
// If the category/genre is set then update the post
if(!empty($category)){
$category_ID = get_cat_ID($category);
// if a category exists
if($category_ID) {
$categories_array = array($category_ID);
wp_set_post_categories($postID, $categories_array);
}
// if it doesn't exist then create a new category
else {
$new_category_ID = wp_create_category($category);
$categories_array = array($new_category_ID);
wp_set_post_categories($postID, $categories_array);
}
}
// move the file to the right month/date directory in wordpress
$wpFileInfo = wp_upload_bits(basename($filePath), null, file_get_contents($filePath));
// if moved correctly delete the original
if (empty($wpFileInfo['error'])) {
unlink($filePath);
}
// add the mp3 file to the post as an attachment
$wp_filetype = wp_check_filetype(basename($wpFileInfo['file']), null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($wpFileInfo['file'])),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $wpFileInfo['file'], $postID);
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $wpFileInfo['file']);
wp_update_attachment_metadata($attach_id, $attach_data);
// add the link to the attachment to the post
$attachmentLink = wp_get_attachment_link($attach_id, 'thumbnail', FALSE, FALSE, 'Download file');
$updatePost = get_post($postID);
$updated_post = array();
$updated_post['ID'] = $postID;
$updated_post['post_content'] = $updatePost->post_content . '<p>' . $attachmentLink . '</p>';
if($_POST['publish-automatically']){
$updated_post['post_status'] = 'publish';
}
wp_update_post($updated_post);
//
array_push($messages, _e('Post created:', 'mp3-to-post') . ' ' . $title);
} else {
array_push($messages, _e('Post already exists:', 'mp3-to-post') . ' ' . $title);
}
} else {
array_push($messages, _e('Either the title or comments are not set in the ID3 information. Make sure they are both set for v1 and v2.', 'mp3-to-post'));
}
$i++;
endwhile; //
// return the messages
return $messages;
}
/**
* Creates a folder based on the path provided
*
* @param $folderpath
*/
function create_folder($folderPath){
// check if directory exists and makes it if it isn't
if (!is_dir($folderPath)) {
if (!mkdir($folderPath, 0777)) {
echo '<p><strong>Couldnt make the folder for you to put your files in, please check your permissions.</strong></p>';
}
}
}
/**
* Gives an array of mp3 files to turn in to posts
*
* @param $folderPath
*
* @return $array
* Returns an array of mp3 file names from the directory created by the plugin
*/
function mp3_array($folderPath){
// scan folders for files and get id3 info
$mp3Files = array_slice(scandir($folderPath), 2); // cut out the dots..
// filter out all the non mp3 files
$mp3Files = array_filter($mp3Files, "mp3_only");
// sort the files
sort($mp3Files);
return $mp3Files;
}
/**
* Gets the ID3 info of a file
*
* @param $filePath
* String, base path to the mp3 file
*
* @return array
* Keyed array with title, comment and category as keys.
*/
function get_ID3($filePath) {
// Initialize getID3 engine
$get_ID3 = new getID3;
$ThisFileInfo = $get_ID3->analyze($filePath);
/**
* Optional: copies data from all subarrays of [tags] into [comments] so
* metadata is all available in one location for all tag formats
* metainformation is always available under [tags] even if this is not called
*/
getid3_lib::CopyTagsToComments($ThisFileInfo);
$title = $ThisFileInfo['tags']['id3v2']['title'][0];
$comment = $ThisFileInfo['tags']['id3v2']['comments'][0];
$category = $ThisFileInfo['tags']['id3v2']['genre'][0];
$details = array(
'title' => $title,
'comment' => $comment,
'category' => $category,
);
return $details;
}
?>