/
import-youtube-videos-as-wp-post.php
337 lines (254 loc) · 10.7 KB
/
import-youtube-videos-as-wp-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
<?php
/*
Plugin Name: Import YouTube videos as WP Posts
Plugin URI: http://www.enguerranweiss.fr
Description: Get a video list from a request to Youtube (free query, playlist ID, channel ID) and add their content to your own Wordpress :)
Version: 2.0
Author: Enguerran Weiss
Author URI: http://www.enguerranweiss.fr
*/
/* -----------------------------------------------------------------------------
* Init functions
---------------------------------------------------------------------------- */
add_action( 'init', 'create_y2p_pt_rejects' );
add_action('admin_init', 'yt_to_posts_feed_init');
add_action('admin_menu', 'yt_to_posts_feed_menu');
add_action('admin_menu', 'yt_to_posts_options_page');
add_filter('yt_to_posts_options_page_render', 'yt_to_posts_register_options');
/* -----------------------------------------------------------------------------
* WP Ajax stuff
---------------------------------------------------------------------------- */
add_action("wp_ajax_yt_to_posts_insertPost", "yt_to_posts_insertPost");
add_action("wp_ajax_nopriv_yt_to_posts_insertPost", "yt_to_posts_insertPost");
add_action("wp_ajax_yt_to_posts_getPostTypeCats", "yt_to_posts_getPostTypeCats");
add_action("wp_ajax_nopriv_yt_to_posts_getPostTypeCats", "yt_to_posts_getPostTypeCats");
add_action("wp_ajax_yt_to_posts_api_call", "yt_to_posts_api_call");
add_action("wp_ajax_nopriv_yt_to_posts_api_call", "yt_to_posts_api_call");
add_action("wp_ajax_yt_to_posts_rejectPost", "yt_to_posts_rejectPost");
add_action("wp_ajax_nopriv_yt_to_posts_rejectPost", "yt_to_posts_rejectPost");
add_action("wp_ajax_yt_to_posts_getAllPostSlug", "yt_to_posts_getAllPostSlug");
add_action("wp_ajax_nopriv_yt_to_posts_getAllPostSlug", "yt_to_posts_getAllPostSlug");
/* -----------------------------------------------------------------------------
* Declare functions
---------------------------------------------------------------------------- */
function yt_to_posts_getStati() {
return get_post_stati(array(), 'names');
}
function yt_to_posts_getAllPostSlug() { // Returns an array of all Youtube ID (to check dupes)
$stati = yt_to_posts_getStati();
$args = array( 'numberposts' => -1, 'post_status' => $stati);
$posts = get_posts($args);
$ids = array();
foreach ($posts as $post) {
if($post->yt_id != ""){
$ids[] = $post->yt_id;
}
}
echo json_encode($ids);
die();
}
function yt_to_posts_getPostTypeCats() { // Returns an array of all Youtube ID (to check dupes)
$type = $_POST['post_type'];
$taxs = get_object_taxonomies($type);
$results = array();
if(empty($taxs)){
echo 'empty';
}
else {
foreach ($taxs as $tax) {
if($tax === "post_tag" || $tax === "post_format"){
}
else {
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'exclude' => array(),
'exclude_tree' => array(),
'include' => array(),
'number' => '',
'fields' => 'all',
'slug' => '',
'parent' => '',
'hierarchical' => true,
'child_of' => 0,
'get' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'offset' => '',
'search' => '',
'cache_domain' => 'core'
);
$tax_terms = get_terms($tax, $args);
// var_dump($tax_terms);
foreach ($tax_terms as $tax_term) {
if($tax_term){
$object = new stdClass();
$object->id = $tax_term->term_id;
$object->name = $tax_term->name;
$results[] = $object;
}
}
}
}
$results = json_encode($results);
echo $results;
}
die();
}
function yt_check_api_settings() { // Check if Youtube API settings are filled
if(get_option('yt_to_posts_ck') === ''){
return false;
}
else {
return true;
}
}
function yt_to_posts_insertPost(){ // Setting and calling wp_insert_post();
$replacers = array('/%title%/', '/%description%/', '/%embed%/', '/%thumbnail%/', '/%query%/', '/%user%/', '/%date%/');
$title = $_POST['title'];
$description = $_POST['content'];
$id = $_POST['id'];
$user = $_POST['username'] === '' ? 'unknown user':$_POST['username'];
$author = get_option('yt_to_posts_author');
$imgSrc = $_POST['imgSrc'];
$query = $_POST['query'];
$status = get_option('yt_to_posts_post_status');
$postType = get_option('yt_to_posts_post_type');
$cat = intval(get_option('yt_to_posts_cat'));
$title_template = get_option('yt_to_posts_title_format');
$content_template = get_option('yt_to_posts_content_format');
$date = $_POST['date'];
$embed = '[embed]'.$_POST['mediaUrl'].'[/embed]';
$content = $embed . ' <br> '. $description;
$templateVals = array($title, $content, $embed, $imgSrc, $query, $user, $date );
// If template selected, construct the title
if($title_template !== ''){
$customTitle = preg_replace( $replacers, $templateVals, $title_template);
$title = $customTitle;
}
if($content_template !== ''){
$customContent = preg_replace($replacers, $templateVals, $content_template);
$content = $customContent;
}
// Creating new post
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => $status,
'post_author' => $author,
'post_type' => $postType,
'post_category' => array($cat)
);
$post_ID = wp_insert_post($my_post);
// updating post meta
// if a media is detected, add its source url
if($imgSrc){
add_post_meta( $post_ID, 'media_url', $imgSrc, true ) || update_post_meta( $post_ID, 'media_url', $imgSrc );
add_post_meta( $post_ID, 'yt_id', $id, true );
// Create and upload thumbnail with $title as description
$image = media_sideload_image($imgSrc, $title);
// then find the last image added to the post attachments
$attachments = get_posts(array('numberposts' => '1', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC', 'orderby' => 'date'));
// Set this attachment as post thumbnail
if(sizeof($attachments) > 0){
set_post_thumbnail($post_ID, $attachments[0]->ID);
}
}
echo 'ok';
die();
}
function yt_to_posts_rejectPost(){ // Reject post : insert
$title = $_POST['title'];
$content = $_POST['content'];
$id = $_POST['id'];
$author = $_POST['author'];
$my_post = array(
'post_name' => $id,
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'tags_input' => $author,
'post_author' => 1,
'post_type' => 't2p_reject'
);
wp_insert_post($my_post);
echo 'ok';
die();
}
function yt_to_posts_feed_init() {
load_plugin_textdomain('youtube-to-posts', false, basename( dirname( __FILE__ ) ) . '/i18n' );
}
function yt_to_posts_feed_menu() {
$page = add_menu_page(
__('Youtube importer', 'yt_to_posts'), // The Menu Title
__('Youtube importer', 'yt_to_posts'), // The Page title
'manage_options', // The capability required for access to this item
'yt_to_posts_feed', // the slug to use for the page in the URL
'yt_to_posts_feed_admin', // The function to call to render the page
"dashicons-format-gallery",58 // Position
);
/* Using registered $page handle to hook script load */
add_action('admin_print_styles-' . $page, 'yt_to_posts_feed_styles');
add_action( 'admin_init', 'yt_to_posts_register_options' );
}
function yt_to_posts_feed_styles() {
/*
* It will be called only on your plugin admin page, enqueue our script here
*/
wp_enqueue_script('yt_to_posts_feed_script', plugins_url('/script.js', __FILE__));
wp_enqueue_style('style', plugins_url('/styles.css', __FILE__));
}
/* -----------------------------------------------------------------------------
* Render the admin page
---------------------------------------------------------------------------- */
function yt_to_posts_feed_admin() {
wp_enqueue_script('yt_to_posts_feed_script', plugins_url('/script.js', __FILE__));
wp_register_script('yt_to_posts_feed_script', plugins_url('/script.js', __FILE__), 'jquery');
require_once(dirname(__FILE__) . '/admin.php');
}
/* -----------------------------------------------------------------------------
* Call to the Tweet API file
---------------------------------------------------------------------------- */
function yt_to_posts_api_call(){
require_once(dirname(__FILE__) ."/youtube-api-interface.php"); // Path to youtube interface
die();
}
/* -----------------------------------------------------------------------------
* Plugin Page options
---------------------------------------------------------------------------- */
function yt_to_posts_options_page() {
add_options_page('Youtube importer', 'Youtube importer', 'manage_options', 'yt-to-posts', 'yt_to_posts_options_page_render');
wp_enqueue_style('style', plugins_url('/styles.css', __FILE__));
}
function yt_to_posts_options_page_render(){
require_once(dirname(__FILE__) . '/options.php');
}
function yt_to_posts_register_options() { //register our settings
register_setting( 'yt_to_posts-query-settings-group', 'yt_to_posts_query' );
register_setting( 'yt_to_posts-query-settings-group', 'yt_to_posts_post_type' );
register_setting( 'yt_to_posts-query-settings-group', 'yt_to_posts_post_status' );
register_setting( 'yt_to_posts-query-settings-group', 'yt_to_posts_cat' );
register_setting( 'yt_to_posts-query-settings-group', 'yt_to_posts_author' );
register_setting( 'yt_to_posts-query-settings-group', 'yt_to_posts_query_type' );
register_setting( 'yt_to_posts-query-settings-group', 'yt_to_posts_number' );
register_setting( 'yt_to_posts-admin-settings-group', 'yt_to_posts_ck' );
register_setting( 'yt_to_posts-admin-settings-group', 'yt_to_posts_title_format' );
register_setting( 'yt_to_posts-admin-settings-group', 'yt_to_posts_content_format' );
}
/* -----------------------------------------------------------------------------
* Register private post type for rejects
---------------------------------------------------------------------------- */
function create_y2p_pt_rejects() {
register_post_type( 'y2p_reject',
array(
'labels' => array(
'name' => __( 'Y2P rejects' ),
'singular_name' => __( 'Y2P rejects' )
),
'public' => false,
'has_archive' => false
)
);
}