-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.php
647 lines (429 loc) · 15.1 KB
/
main.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<?php
/*
Plugin Name: WP-ADD-Video
Plugin URI: http://sabirul-mostofa.blogspot.com
Description: Add video from Youtube At your admin panel
Version: 1.0
Author: Sabirul Mostofa
Author URI: http://sabirul-mostofa.blogspot.com
*/
$wpAddVideo = new wpAddVideo();
if(isset($wpAddVideo)) {
//add_action('init', array($wpAddVideo,'redirect'), 1);
add_action('admin_menu', array($wpAddVideo,'CreateMenu'),50);
if (isset($_POST['addVideoUnique'])) {
}
}
class wpAddVideo{
function __construct(){
add_action('admin_enqueue_scripts' , array($this,'add_scripts'));
add_action( 'wp_ajax_myajax-submit', array($this,'ajax_handle' ));
add_action( 'wp_ajax_ajax_toggle', array($this,'ajax_toggle' ));
add_action( 'wp_ajax_ajax_remove', array($this,'ajax_remove' ));
add_action( 'wp_ajax_show_next', array($this,'ajax_next_page_show'));
add_action( 'wp_ajax_ajax_getId', array($this,'ajax_process_insert'));
register_activation_hook(__FILE__, array($this, 'create_table'));
}
function add_scripts(){
if(preg_match('/wpAddVideo/',$_SERVER['REQUEST_URI']) || preg_match('/wpManageVideo/',$_SERVER['REQUEST_URI'])){
wp_enqueue_script('jquery');
wp_enqueue_script('add_video_script',plugins_url('/' , __FILE__).'js/script.js');
wp_localize_script('add_video_script', 'addVideoSettings',
array(
'ajaxurl'=>admin_url('admin-ajax.php'),
'pluginurl' => plugins_url('/' , __FILE__)
)
);
wp_register_style('add_video_css', plugins_url('/' , __FILE__).'css/style.css', false, '1.0.0');
wp_enqueue_style('add_video_css');
}
}
function CreateMenu(){
add_submenu_page('theme-options.php','Add From YouTube','Add From YouTube','activate_plugins','wpAddVideo',array($this,'OptionsPage'));
add_submenu_page('theme-options.php','Manage Video','Manage Playlist','activate_plugins','wpManageVideo',array($this,'videoManage'));
}
function ajax_handle(){
$id = trim($_REQUEST['id']);
$title = trim($_REQUEST['title']);
global $wpdb;
if(!preg_match('/[a-zA-Z0-9]/', $id))exit;
if($this->exists_in_table($id))exit;
$wpdb->insert( 'wp_video_list',
array( 'video_id' => $id,
'video_title' => $title
),
array( '%s', '%s')
);
print $id;
exit;
}
function ajax_toggle(){
global $wpdb;
$id = $_REQUEST['id'];
$result = $wpdb -> get_results("SELECT video_stat FROM wp_video_list where video_id='$id'",'ARRAY_N' );
if ($result[0][0] == 1){
$wpdb -> update('wp_video_list',
array(
'video_stat' => 2
),
array(
'video_id' => $id
),
array('%d'),
array('%s')
);
}
elseif($result[0][0] == '2'){
$wpdb -> update('wp_video_list',
array(
'video_stat' => 1
),
array(
'video_id' => $id
),
array('%d'),
array('%s')
);
}
//$result = $wpdb -> get_results("SELECT video_stat FROM wp_video_list where video_id='$id'",'ARRAY_N' );
exit;
}
function ajax_remove(){
global $wpdb;
$id = $_REQUEST['id'];
echo $test = $wpdb -> query("delete from wp_video_list where video_id='$id'");
exit;
}
function ajax_next_page_show(){
$num = $_REQUEST['pagenum'];
if($num <1)$num = 1;
$this -> OptionsPage($num,1);
exit;
}
function ajax_process_insert(){
$code = trim($_REQUEST['url']);
if(!preg_match('/[a-zA-Z0-9]/',$code))exit;
//id only
if(!preg_match('?/?',$code)){
echo $id = trim($code,'?');
}
//if url
if(preg_match('?http://www.youtube.com/watch?',$code)){
if(preg_match('/v=+.[^&]+&/',$code,$test))
echo $id = trim(str_replace('v=','',$test[0]),'&');
}
//if shortcode
if(preg_match('/youtu\.be/',$code,$test)){
echo $id = str_replace('http://youtu.be/','',$code);
}
//if iframe
if(preg_match('/<iframe/',$code,$test)){
$dom = new DOMDocument();
$dom->loadHTML($code);
$a= $dom->getElementsByTagName('iframe')->item(0)->getAttribute('src');
preg_match('$/+.[^/]+\?$',$a,$test);
echo $id = trim($test[0],'/?');
}
//if old object
if(preg_match('/<object/',$code)){
$dom = new DOMDocument();
@$dom->loadHTML($code);
$a= $dom->getElementsByTagName('embed')->item(0)->getAttribute('src');
preg_match('$/+.[^/]+\?$',$a,$test);
$id = trim($test[0],'/?');
echo $id;
}
echo $title = $_REQUEST['title'];
global $wpdb;
if($this->exists_in_table($id))exit;
$wpdb->insert( 'wp_video_list',
array( 'video_id' => $id,
'video_title' => $title
),
array( '%s', '%s')
);
//$this -> add_single_video($id);
//http://youtu.be/wagn8Wrmzuc
//<iframe width="560" height="349" src="http://www.youtube.com/embed/wagn8Wrmzuc?rel=0" frameborder="0" allowfullscreen></iframe>
//<object width="560" height="349"><param name="movie" value="http://www.youtube.com/v/wagn8Wrmzuc?fs=1&hl=en_US&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/wagn8Wrmzuc?fs=1&hl=en_US&rel=0" type="application/x-shockwave-flash" width="560" height="349" allowscriptaccess="always" allowfullscreen="true"></embed></object>
//http://www.youtube.com/watch/v=?84343&d
exit;
}
function add_single_video($d){
$id = trim($id);
$url = "http://gdata.youtube.com/feeds/api/videos/{$id}?v=2";
$parser=new DOMDocument();
$parser->load($url);
$title = $parser->getElementsByTagName('title')->item(0)->nodeValue;
echo $title;
}
function create_table(){
$sql = "CREATE TABLE IF NOT EXISTS `wp_video_list` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`video_id` varchar(30) NOT NULL,
`video_title` text not null,
`video_stat` tinyint unsigned NOT NULL default 1,
PRIMARY KEY (`id`),
key `video_id`(`video_id`),
key `video_stat`(`video_stat`)
)";
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
/*
* SUBMENU PAGE Add video
*
* */
function OptionsPage( $pagenum=1, $from_ajax=0 ){
if (isset($_POST['addVideoUnique']))
update_option('videoUserYouTube', trim($_POST['videoUserYouTube'][0]));
$dataA = $this ->getVideo($pagenum);
//--------
if(!$from_ajax):
$this->direct_add();
?>
<div class="wrap"><h3>Insert YouTube UserName to List videos</h3>
<form method="post" action="admin.php?page=wpAddVideo">
<table cellpadding=3>
<tr><td>
<input type="text" name="videoUserYouTube[]" value="<?php echo get_option('videoUserYouTube')?>" style="width:10em" /> </td>
<td>
<p class="submit"><input type="submit" name="addVideoUnique" class="button-primary" value="<?php _e('List Videos') ?>" /></p>
</td>
</tr>
</table>
</form>
</div>
<?php
endif;
//----
if(is_array($dataA)):
$total = $dataA['total'];
$max_page = ceil($total/10);
if(!$from_ajax):
echo $message = ($total==0)?'<div class="error">No Video found for The user</div>':
"<h3>Total {$total} Videos Found - {$max_page} Page(s)</h3>";
echo "<input type='hidden' id='max-page' value='$max_page'/>";
if($total != 0 && $total !=''){
$toShow = ($total<10)? $total : 10;
echo "<div id='videoNumMessage'><h4>Current Page: 1(Showing videos 1-{$toShow})</h4> </div>";
}
?>
<div class="paginate_video"><?php $this->paginate($total,$pagenum); ?></div>
<div id="videoContents">
<?php
endif;
$this->common_ajax($dataA);
if(!$from_ajax):
?>
</div>
<?php
endif;
endif;
}//endof options page
/*
* SUBMENU PAGE Manage Video
*
* */
function videoManage(){
?>
<div style="text-align:center;margin 15px 0"> <h3>Manage Video Playlist</h3></div>
<div class="wrap">
<table class="widefat">
<thead>
<tr>
<th>Video Title</th>
<th>Thumbnail</th>
<th>Status</th>
<th>Action</th>
<th>Remove</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Video Title</th>
<th>Thumbnail</th>
<th>Status</th>
<th>Action</th>
<th>Remove</th>
</tr>
</tfoot>
<tbody>
<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT video_title,video_id,video_stat FROM wp_video_list",ARRAY_N );
foreach($result as $single):
$title = $single[0];
$id= $single[1];
$stat = $single[2];
$image = 'http://i.ytimg.com/vi/'.$id.'/1.jpg';
echo '<td>',$title,'</td>';
echo '<td>','<img src="'.$image.'"/>','</td>';
if($stat == 1)
echo '<td>',Active,'</td>';
else
echo '<td>',Suspended,'</td>';
if($stat == 1)
echo '<td><button id="',$id,'" class="action">Suspend</button></td>';
else
echo '<td><button id="',$id,'" class="action">Add</button></td>';
echo '<td><button class="remove">Remove</button></td></tr>';
endforeach;
?>
</tbody>
</table>
</div>
<?php
}// end of video manage submenu page
function common_ajax($dataA){
?>
<div class="wrap">
<table class="widefat">
<thead>
<tr>
<th>Video Title</th>
<th>Thumbnail</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Video Title</th>
<th>Thumbnail</th>
<th>Action</th>
</tr>
</tfoot>
<tbody>
<?php
$this -> renderVideos($dataA);
?>
</tbody>
</table>
</div>
<?php
}
//direct add from code
function direct_add(){
echo '<h3>Enter Youtube Video ID/Url to Add in the Playlist</h3>';
?>
<table width='70%'>
<tr>
<td><h4>Video Title:</h4> </td>
</tr>
<tr>
<td><input style='width:350px' type='text' id='directTitle' value='Enter Title Here'/> </td>
</tr>
<tr>
<td><h4>Video Id/URL/Embed URL/Short URL:</h4> </td>
</tr>
<tr>
<td>
<?php echo '<textarea id="areaId" rows="5" cols="50"></textarea>'; ?>
</td>
</tr>
<tr>
<td>
<input class='button-primary' type='submit' id='areaSubmit' value='Add To Playslist'/>
</td>
</tr>
</table>
<div class='updated' id='areaMessage'></div>
<?php
}
function getVideo($pagenum=1){
$start=($pagenum==1)?1:($pagenum-1)*10+1;
$user = get_option('videoUserYouTube');
if(!$user)return;
//sample author wataahISg00d
// http://i.ytimg.com/vi/{$user}/1.jpg sample image
$url = ($start>1)?
"http://gdata.youtube.com/feeds/api/videos?author={$user}&start-index={$start}&max-results=10":
"http://gdata.youtube.com/feeds/api/videos?author={$user}&max-results=10"
;
$content_url = 'http://www.youtube.com/v/EgHY53dOZ-U?f=videos&app=youtube_gdata';
$content_type ='video/3gpp' ;
$code = <<<EOF
<object width="425" height="350">
<param name="movie" value="$content_url"></param>
<embed src="$content_url"
type="$content_type" width="425" height="350">
</embed>
</object>
EOF;
$parser=new DOMDocument();
@$parser->load($url);
if(preg_match('/<feed+[^>]+>/',$parser->saveXML(),$a))$feedString=$a[0];
$parser->documentURI;
$nURI=$parser->getElementsByTagName('feed')->item(0)->getAttribute('xmlns:media');
//total video items
$search=$parser->getElementsByTagName('feed')->item(0)->getAttribute('xmlns:openSearch');
$total=0;
$total = $parser -> getElementsByTagNameNS($search,'totalResults')->item(0)->nodeValue;
$entryNumber=$parser->getElementsByTagName('entry')->length;
//fetching data
$dataA = array();
$counter=0;
foreach($parser->getElementsByTagName('entry') as $entry):
$counter++;
$eString=$parser->saveXML($entry);
//creating sudo xml like the parent
$eString=$feedString.$eString.'</feed>';
$eParser=new DOMDocument();
@$eParser->loadXML($eString);
$dataA[$counter-1]['title'] = $eParser->getElementsByTagName('title')->item(0)->nodeValue;
$dataA[$counter-1]['id'] = trim(preg_replace('?http://gdata.youtube.com/feeds/api/videos/?','',$eParser->getElementsByTagName('id')->item(0)->nodeValue),'/');
$data=array('content','thumbnail');
foreach($data as $single):
foreach($eParser->getElementsByTagNameNS($nURI,$single) as $tracker):
if($single == 'thumbnail') $dataA[$counter-1]['thumbnail'] = $tracker->getAttribute('url');
if( $tracker->getAttribute('yt:format') == 5)
$dataA[$counter-1]['url'] = $tracker->getAttribute('url');
endforeach;
endforeach;
endforeach;
$dataA['total'] = $total;
return($dataA);
}//end of getVideo
//pagination
function paginate($total,$items_per_page=10){
// if ($total<=10)return;
//$pages = ceil($total/$items_per_page);
//$current_page = 1;
if($total>10){
$img_right = plugins_url('/' , __FILE__).'images/right.png';
$img_left = plugins_url('/' , __FILE__).'images/left.png';
?>
<input type='image' src='<?php echo $img_left ?>' id="show-prev" style="display:none" value=''/>
<input class='1' type='image' src='<?php echo $img_right ?>' id="show-next" value=''/>
<?php
}
}
function renderVideos($dataA){
array_pop($dataA);
foreach($dataA as $yo){
$image = 'http://i.ytimg.com/vi/'.$yo['id'].'/1.jpg';
echo '<tr><td class="', $yo['id'], '" style="width:400px">',$yo['title'],'</td>';
echo '<td>','<img src="' . $image. '"/>','</td>';
if ($this -> exists_in_table($yo['id']))
echo '<td><b><h3>Added in the playlist</h3></b></td></tr>';
else
echo '<td><button id="',$yo['id'],'" class="primary" name="add">Add to Playlist</button></td></tr>';
}
}//endof renderVideos
//Crude functions
function exists_in_table($video_id){
global $wpdb;
//$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
$result=$wpdb->get_results( "SELECT video_title FROM wp_video_list where video_id='$video_id'" );
if(empty($result))return false;
else return true;
}
function insert(){
}
function delete($vido_id){
}
function suspend(){
}
}
?>