-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcp-sc-podcast.php
504 lines (394 loc) · 15.7 KB
/
rcp-sc-podcast.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
<?php
/*
Plugin Name: RCP SoundCloud Podast
Plugin URI: https://toiee.jp
Description: Restrict Content Pro と連動して動作するSoundCloud をPodcast にするためのプラグイン
Version: 0.1
Author: toiee Lab
Author URI: https://toiee.jp
License: GPL2
*/
/* Copyright 2017 toiee Lab (email : desk@toiee.jp)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
$rcp_sc_podcast = new RCP_SC_Podcast();
class RCP_SC_Podcast
{
private $options;
function __construct()
{
// カスタム投稿タイプの追加
add_action('init', array( $this, 'create_post_type') );
// --------- ショートコードの登録
add_shortcode('scpodcast', array($this, 'add_scpodcast_shortcode'));
// --------- カスタム投稿ページに、情報を追加
add_filter( 'manage_scpcast_posts_columns', array($this, 'add_shortcode_fields') );
add_action( 'manage_scpcast_posts_custom_column', array($this, 'custom_shortcode_fields'), 10, 2 );
add_action( 'add_meta_boxes', array($this, 'register_meta_boxes') );
add_action( 'save_post', array($this, 'save_meta_boxes') );
// --------- 設定のため --------------
// メニューの追加
add_action( 'admin_menu', array($this, 'add_plugin_page') );
// 設定の初期化
add_action('admin_init', array($this, 'page_init') );
// --------- podcast 配信のため ----------
add_action('wp_loaded', array( $this, 'podcast' ) );
}
function create_post_type()
{
register_post_type(
'scpcast',
array(
'label' => 'SC Podcast',
'public' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'hierarchical' => false,
'has_archive' => false,
'supports' => array(
'title',
)
)
);
}
//------------------- shortcode
function add_scpodcast_shortcode($atts)
{
if( isset($atts['id']) && is_numeric($atts['id']) )
{
$uid = get_current_user_id();
$embed = get_post_meta($atts['id'], 'rcpscp_embedtag', true);
if($uid == 0){
$token = '';
}
else{
$token = $this->xor_encrypt("toiee,{$uid},lab,{$atts['id']}", NONCE_KEY);
}
$type = isset( $atts['type'] ) ? $atts['type'] : '';
$url = site_url().'/scpcast/?token='. rawurlencode( $token );
switch($type)
{
case 'url':
return '<p><input type="text" value="'.$url.'" style="width:100%" onclick="this.select();"></p>';
case 'link':
return '<p><a href="'.$url.'" target="_blank">'.$url.'</a></p>';
default:
return $embed;
}
}
}
//-------------------- 投稿ページ用
/**
* ショートコードを表示するためのコラムを追加
*/
function add_shortcode_fields( $columns )
{
$columns['shortcode'] = 'Shortcode';
return $columns;
}
/**
* ショートコードを表示するコラムに値を追加
*/
function custom_shortcode_fields( $column, $post_id )
{
switch ( $column )
{
case 'shortcode' :
echo "<input type='text' value='[scpodcast id=\"{$post_id}\" type=\"url\"]' readonly='readonly' onclick='this.select();' />";
break;
}
}
/**
* 投稿ページに「iframe を入れる」場所を作る
*/
function register_meta_boxes()
{
add_meta_box('rcp_scp', 'RCP SoundCloud Playlist to Podcast', array($this, 'display_embedtag_meta_box'), 'scpcast', 'advanced' );
}
function display_embedtag_meta_box( $post )
{
$id = get_the_ID();
// embed
$embed = get_post_meta($id, 'rcpscp_embedtag', true);
// type
$casttype = get_post_meta($id, 'rcpscp_casttype', true);
$casttype = $casttype == '' ? 'audio-seminar' : $casttype;
$select_type = array(
'audio-seminar' => 'オーディオセミナー型',
'podcast' => 'Podcast型',
'campaign' => 'キャンペーン型'
);
// free type
$freetype = get_post_meta($id, 'rcpscp_freetype', true);
$freetype = ($freetype == '') ? 'nolisten' : $freetype;
$select_free = array(
'nolisten' => '全く聞けない',
'latest' => '最新のみ',
);
wp_nonce_field( 'embedtag_meta_box', 'embedtag_meta_box_nonce' );
echo <<<EOD
<p><b>Embed code</b></p>
<p>SoundCloudのプレイリストのEmbed code を貼り付けてください。自動で、iframeだけを保存します。</p>
<textarea name="rcpscp_embedtag" style="width:100%">
{$embed}
</textarea>
<p><b>Podcastタイプを選択</b></p>
<p>Podcastのタイプを選んでください。音声セミナー型は、SoundCloudのプレイリスト順に従い、最新投稿をプレイリストの1番目として設定します。Podcast型はプレイリスト内のオーディオの日付でPodcastを作成します。キャンペーン型は、音声セミナー型でユーザーの登録日時から計算して、1日1つずつオーディオを配信します。</p>
<select name="rcpscp_casttype">
EOD;
foreach( $select_type as $key => $disp)
{
$selected = ($key == $casttype) ? 'selected' : '';
echo "<option value='{$key}' {$selected}>{$disp}</option>";
}
echo <<<EOD
</select>
<p><b>無料ユーザーの扱い</b></p>
<p>「全く聞けない」場合は、完全にオーディオデータをなくします。「最新のみ」は、音声セミナー型の場合は第一話を、Podcast型の場合は最新話をお聞きいただけます。</p>
<select name="rcpscp_freetype">
EOD;
foreach( $select_free as $key => $disp)
{
$selected = ($key == $freetype) ? 'selected' : '';
echo "<option value='{$key}' {$selected}>{$disp}</option>";
}
echo <<<EOD
</select>
EOD;
}
function save_meta_boxes($post_id)
{
// Check if our nonce is set.
if ( ! isset( $_POST['embedtag_meta_box_nonce'] ) ) {
return $post_id;
}
$nonce = $_POST['embedtag_meta_box_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, 'embedtag_meta_box' ) ) {
return $post_id;
}
//embed タグの iframe 部分だけを取り出す
$embed = isset($_POST['rcpscp_embedtag']) ? $_POST['rcpscp_embedtag'] : null;
preg_match('/(<iframe.*?<\/iframe>)/s', $embed, $matches);
$embed = $matches[1];
$embed_before = get_post_meta($post_id, 'rcpscp_embedtag', true);
if($embed)
{
update_post_meta($post_id, 'rcpscp_embedtag', $embed);
}
else
{
delete_post_meta($post_id, 'rcpscp_embedtag', $embed_before);
}
//
$casttype = isset($_POST['rcpscp_casttype']) ? $_POST['rcpscp_casttype'] : 'audio-seminar';
update_post_meta($post_id, 'rcpscp_casttype', $casttype);
$freetype = isset($_POST['rcpscp_freetype']) ? $_POST['rcpscp_freetype'] : 'nolisten';
update_post_meta($post_id, 'rcpscp_freetype', $freetype);
}
//-------------------- 設定用
//設定を保存する
public function add_plugin_page()
{
// add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
// $page_title: 設定ページの<title>部分
// $menu_title: メニュー名
// $capability: 権限 ( 'manage_options' や 'administrator' など)
// $menu_slug : メニューのslug
// $function : 設定ページの出力を行う関数
// $icon_url : メニューに表示するアイコン
// $position : メニューの位置 ( 1 や 99 など )
// 設定のサブメニューとしてメニューを追加する場合は下記のような形にします。
add_options_page( 'RCP SCPodcast', 'RCP SCPodcast', 'manage_options', 'rcpscp_setting', array( $this, 'create_admin_page' ) );
}
public function page_init()
{
// 設定を登録します(入力値チェック用)。
// register_setting( $option_group, $option_name, $sanitize_callback )
// $option_group : 設定のグループ名
// $option_name : 設定項目名(DBに保存する名前)
// $sanitize_callback : 入力値調整をする際に呼ばれる関数
register_setting( 'rcpscp_setting', 'rcpscp_setting', array( $this, 'sanitize' ) );
// 入力項目のセクションを追加します。
// add_settings_section( $id, $title, $callback, $page )
// $id : セクションのID
// $title : セクション名
// $callback : セクションの説明などを出力するための関数
// $page : 設定ページのslug (add_menu_page()の$menu_slugと同じものにする)
add_settings_section( 'rcpscp_setting_section_id', '', '', 'rcpscp_setting' );
// 入力項目のセクションに項目を1つ追加します(今回は「メッセージ」というテキスト項目)。
// add_settings_field( $id, $title, $callback, $page, $section, $args )
// $id : 入力項目のID
// $title : 入力項目名
// $callback : 入力項目のHTMLを出力する関数
// $page : 設定ページのslug (add_menu_page()の$menu_slugと同じものにする)
// $section : セクションのID (add_settings_section()の$idと同じものにする)
// $args : $callbackの追加引数 (必要な場合のみ指定)
add_settings_field( 'sc_cid', 'メッセージ', array( $this, 'sc_cid_callback' ), 'rcpscp_setting', 'rcpscp_setting_section_id' );
}
public function create_admin_page()
{
// 設定値を取得します。
$this->options = get_option( 'rcpscp_setting' );
?>
<div class="wrap">
<h2>SoundCloud CID設定</h2>
<?php
// add_options_page()で設定のサブメニューとして追加している場合は
// 問題ありませんが、add_menu_page()で追加している場合
// options-head.phpが読み込まれずメッセージが出ない(※)ため
// メッセージが出るようにします。
// ※ add_menu_page()の場合親ファイルがoptions-general.phpではない
global $parent_file;
if ( $parent_file != 'options-general.php' ) {
require(ABSPATH . 'wp-admin/options-head.php');
}
?>
<form method="post" action="options.php">
<?php
// 隠しフィールドなどを出力します(register_setting()の$option_groupと同じものを指定)。
settings_fields( 'rcpscp_setting' );
// 入力項目を出力します(設定ページのslugを指定)。
do_settings_sections( 'rcpscp_setting' );
// 送信ボタンを出力します。
submit_button();
?>
</form>
</div>
<?php
}
public function sc_cid_callback()
{
// 値を取得
$sc_cid = isset( $this->options['sc_cid'] ) ? $this->options['sc_cid'] : '';
// nameの[]より前の部分はregister_setting()の$option_nameと同じ名前にします。
?><input type="text" id="sc_cid" name="rcpscp_setting[sc_cid]" value="<?php esc_attr_e( $sc_cid ) ?>" /><?php
}
/**
* 送信された入力値の調整を行います。
*
* @param array $input 設定値
*/
public function sanitize( $input )
{
// DBの設定値を取得します。
$this->options = get_option( 'rcpscp_setting' );
$new_input = array();
// メッセージがある場合値を調整
if( isset( $input['sc_cid'] ) && trim( $input['sc_cid'] ) !== '' ) {
$new_input['sc_cid'] = sanitize_text_field( $input['sc_cid'] );
}
// メッセージがない場合エラーを出力
else {
// add_settings_error( $setting, $code, $sc_cid, $type )
// $setting : 設定のslug
// $code : エラーコードのslug (HTMLで'setting-error-{$code}'のような形でidが設定されます)
// $sc_cid : エラーメッセージの内容
// $type : メッセージのタイプ。'updated' (成功) か 'error' (エラー) のどちらか
add_settings_error( 'rcpscp_setting', 'sc_cid', 'メッセージを入力して下さい。' );
// 値をDBの設定値に戻します。
$new_input['sc_cid'] = isset( $this->options['sc_cid'] ) ? $this->options['sc_cid'] : '';
}
return $new_input;
}
function xor_encrypt($plaintext, $key){
$len = strlen($plaintext);
$enc = "";
for($i = 0; $i < $len; $i++){
$asciin = ord($plaintext[$i]);
$enc .= chr($asciin ^ ord($key[$i]));
}
$enc = base64_encode($enc);
return $enc;
}
function xor_decrypt($encryptedText, $key){
$enc = base64_decode($encryptedText);
$plaintext = "";
$len = strlen($enc);
for($i = 0; $i < $len; $i++){
$asciin = ord($enc[$i]);
$plaintext .= chr($asciin ^ ord($key[$i]));
}
return $plaintext;
}
function podcast()
{
if( preg_match( '|^/scpcast/\?token=.*|', $_SERVER['REQUEST_URI'] ) )
{
require_once dirname(__FILE__).'/sc2podcast.php';
$str = $this->xor_decrypt($_GET['token'], NONCE_KEY);
$arr = explode(',', $str);
$uid = $arr[1];
$pid = $arr[3];
$embed = get_post_meta($pid, 'rcpscp_embedtag', true);
$casttype = get_post_meta($pid, 'rcpscp_casttype', true);
$freetype = get_post_meta($pid, 'rcpscp_freetype', true);
$user = get_userdata($uid);
$email = $user->data->user_email;
$registered_date = strtotime( $user->data->user_registered );
$cid = get_option('rcpscp_setting', false);
$cid = $cid['sc_cid'];
$base_url = site_url().'/wp-content/uploads/sc2podcast/';
$sc2p = new SC2Podcast($cid, $embed, $base_url);
// RCP があるなら
if( function_exists('rcp_user_can_access') )
{
if( rcp_user_can_access($uid, $pid) )
{
switch( $casttype )
{
case 'audio-seminar' :
$sc2p->output_as_audioseminar($email);
break;
case 'podcast' :
$sc2p->output($email);
break;
case 'campaign' :
$sc2p->output_as_campaign($email, $registered_date);
// $sc2p->output_as_campaign($email, time() ); //for test
break;
default:
header("Content-Type:text/plain; charset=utf-8");
echo "L446 : pass rcp_user_can_access({$uid}, {$pid}) \n";
exit;
}
}
else
{
switch( $freetype )
{
case 'nolisten':
//オーディオのURLを全部書き換えるようにする
$sc2p->output_as_free_nolisten($email, $casttype);
break;
case 'latest':
//常に最新話のオーディオ以外のURLを書き換える
$sc2p->output_as_free_listen($email, $casttype);
break;
default:
header("Content-Type:text/plain; charset=utf-8");
echo "L463 : not access rcp_user_can_access({$uid}, {$pid}) \n";
var_dump($casttype, $freetype);
exit;
}
}
}
else
{
//普通にPodcast を配信するかなー
}
exit;
}
}
}