-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluebillywig.php
363 lines (302 loc) · 12.7 KB
/
bluebillywig.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
/**
* @package Blue Billywig
* @version 0.3.01
*/
/*
Plugin Name: Blue Billywig
Plugin URI: https://github.com/DaanRuiter/BB_wordpress_plugin
Description: Allows for easier embedding of mediaclips and playlists
Author: Daan Ruiter
Version: 0.3.1
Author URI: http://daanruiter.net/
*/
// Include wp interface when testing plugin output
// require_once(dirname( __FILE__ ) . '/inc/wpInterface.php');
define('BB_PLUGIN_VERSION', '0.3.1');
define('BB_PLUGIN_BETA', true);
define('BB_PLUGIN_DIR', dirname( __FILE__ ));
define('BB_PLUGIN_IMG', plugin_dir_url( __FILE__ ) .'/img/');
define('BB_PLUGIN_JS', plugin_dir_url( __FILE__ ) . '/js/');
define('BB_PLUGIN_CSS', plugin_dir_url( __FILE__ ) . '/css/');
define('BB_PLUGIN_INC', BB_PLUGIN_DIR . '/inc/');
define('BB_PLUGIN_LIB', BB_PLUGIN_DIR . '/lib/');
define('BB_PLUGIN_PAGES', BB_PLUGIN_DIR . '/pages/');
define('BB_API_SETTINGS_GROUP', 'bb-api-settings');
define('BB_API_SETTINGS_PUBLICATION', 'bb-api-publication');
define('BB_API_SETTINGS_SECRET', 'bb-api-secret');
define('BB_API_SETTINGS_DEFAULT_PLAYOUT', 'bb-api-defaultPlayout');
define('BB_API_SETTINGS_ID', 'bb-api-id');
define('BB_API_SETTINGS_DEFAULT', array(BB_API_SETTINGS_PUBLICATION => "",
BB_API_SETTINGS_SECRET => "",
BB_API_SETTINGS_ID => 0,
BB_API_SETTINGS_DEFAULT_PLAYOUT => "default"));
define('BB_PLUGIN_SETTING_SUPPRESS_NOTICES', 'bb-plugin-suppress-notices');
define('BB_PLUGIN_SETTING_AUTOPLAY', 'bb-plugin-autoplay');
define('BB_PLUGIN_SETTING_USE_IFRAME_EMBED', 'bb-plugin-iframe-embed');
define('BB_PLUGIN_SETTING_AUTOPUBLISH', 'bb-plugin-autopublish');
define('BB_PLUGIN_SETTINGS_DEFAULT', array( BB_PLUGIN_SETTING_SUPPRESS_NOTICES => 'false',
BB_PLUGIN_SETTING_AUTOPLAY => 'false',
BB_PLUGIN_SETTING_AUTOPUBLISH => 'true',
BB_PLUGIN_SETTING_USE_IFRAME_EMBED => 'false'));
require_once BB_PLUGIN_INC . 'uploader.php';
require_once BB_PLUGIN_INC . "strings.php";
require_once BB_PLUGIN_INC . "content.php";
require_once BB_PLUGIN_INC . 'rpcPackage.php';
require_once BB_PLUGIN_INC . 'util.php';
require_once BB_PLUGIN_INC . 'classes/mediaclip.class.php';
use BlueBillywig\VMSRPC\RPC;
class BlueBillywig
{
private static $instance;
private $apiOptions;
private $pluginOptions;
private $rpc;
//Singleton
public static function instance(){
if ( !self::$instance ){ self::$instance = new BlueBillywig(); error_log('Created instance'); }
return self::$instance;
}
public static function acivate_bb_plugin(){
self::instance()->ensure_api_options();
}
public static function deacivate_bb_plugin(){
}
public static function uninstall_bb_plugin(){
self::instance()->delete_options();
}
public function __construct(){
register_activation_hook( __FILE__, array('BlueBillywig', 'acivate_bb_plugin') );
register_deactivation_hook( __FILE__, array('BlueBillywig', 'deacivate_bb_plugin') );
register_uninstall_hook( __FILE__, array('BlueBillywig', 'uninstall_bb_plugin') );
$this->load_api_options();
self::$instance = $this;
}
//Update the shared secret of the RPC when the API settings were updated
public function update_rpc(){
if(!$this->rpc){ return; }
$this->rpc->host = 'https://' . $this->apiOptions[BB_API_SETTINGS_PUBLICATION] . '.bbvms.com';
$this->rpc->sharedSecret = $this->apiOptions[BB_API_SETTINGS_ID] . '-' . $this->apiOptions[BB_API_SETTINGS_SECRET];
}
//Create our RPC
public function create_rpc($publication, $secret, $id){
$host = 'https://' . $publication . '.bbvms.com';
$sharedSecret = $id . '-' . $secret;
$this->rpc = new RPC($host, null, null, $sharedSecret);
}
//Retreive our RPC
public function get_rpc(){
if( $this->rpc == null){
$this->create_rpc( $this->apiOptions[BB_API_SETTINGS_PUBLICATION],
$this->apiOptions[BB_API_SETTINGS_SECRET],
$this->apiOptions[BB_API_SETTINGS_ID]);
}
return $this->rpc;
}
//Test the stored API key by trying to retreive the publication info
public function test_stored_api_key(){
// Try to fetch a single playout (there is always at least one due to there being a default playout)
$parameters['filterset'] = '[{"filters":[{"type":"playout"}]}]';
$parameters['limit'] = 1;
$playouts = BlueBillywig::get_rpc()->sapi('playout', null, 'GET', null, null, $parameters);
if(!$playouts){
return false;
}
return stripos($playouts, 'Invalid token') === false;
}
//Ensure the plugins option are known to Wordpress
public function ensure_api_options(){
$apiOptions = get_option(BB_API_SETTINGS_GROUP);
if(!$apiOptions){
add_option(BB_API_SETTINGS_GROUP, BB_API_SETTINGS_DEFAULT);
}else if (is_array($apiOptions) && count($apiOptions) !== count(BB_API_SETTINGS_DEFAULT)){
update_option(BB_API_SETTINGS_GROUP, BB_API_SETTINGS_DEFAULT);
}
}
public function ensure_plugin_option($optionName){
$pluginOption = get_option($optionName);
if( !isset($pluginOption) || $pluginOption == NULL ){
add_option($optionName, BB_PLUGIN_SETTINGS_DEFAULT[$optionName]);
return BB_PLUGIN_SETTINGS_DEFAULT[$optionName];
}
return $pluginOption;
}
//Load the plugin's options from Wordpress
public function load_api_options(){
$this->ensure_api_options();
$this->apiOptions = get_option(BB_API_SETTINGS_GROUP);
}
//Get the plugin's options
public function get_api_options(){
$this->ensure_api_options();
return $this->apiOptions;
}
public function get_plugin_option($option){
$value = $this->ensure_plugin_option($option);
// return bools as a boolean instead of string
if($value === 'false')
return false;
if($value === 'true')
return true;
return $value;
}
//Save the plugin's options in Wordpress
public function save_api_options($data){
update_option(BB_API_SETTINGS_GROUP, $data);
$this->load_api_options();
$this->update_rpc();
}
public function save_plugin_option($option, $value){
update_option($option, $value);
}
//Delete the plugin's options from Wordpress
function delete_options(){
delete_option(BB_API_SETTINGS_GROUP);
}
}
//Plugin Menu > Bluebillywig path
function page_home(){
echo '<br/><img class="bb-logo-large" style="width:35%;" src="' . BB_PLUGIN_IMG . 'logo.png">';
echo '<span style="display:inline-block;padding-left:10px">Version ' . BB_PLUGIN_VERSION;
if(BB_PLUGIN_BETA){
echo ' <b>BETA</b>';
}
echo '</span>';
wp_enqueue_script('bb-settings-script', BB_PLUGIN_JS . 'bbSettings.js');
wp_enqueue_style('bb-shortcode-style', BB_PLUGIN_CSS . 'bbSettings.css');
require_once BB_PLUGIN_PAGES . 'settings.php';
}
//Plugin Menu > Generate shortcode path
function page_shortcode(){
//Load JS
$bbAPIOptions = BlueBillywig::instance()->get_api_options();
$autoplay = BlueBillywig::instance()->get_plugin_option(BB_PLUGIN_SETTING_AUTOPLAY) ? 'true' : 'false';
wp_enqueue_script('bb-mediaclip-library', BB_PLUGIN_JS . 'bbMediaclipLibrary.js');
wp_enqueue_script('bb-mediaclip-shortcode', BB_PLUGIN_JS . 'bbShortcodeGenerator.js');
wp_localize_script('bb-mediaclip-shortcode', 'defaultAutoplay', $autoplay);
wp_localize_script('bb-mediaclip-shortcode', 'defaultPlayout', $bbAPIOptions[BB_API_SETTINGS_DEFAULT_PLAYOUT]);
wp_localize_script('bb-mediaclip-library', 'BB_STRINGS', BB_SHORTCODE_STRINGS);
wp_localize_script('bb-mediaclip-library', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
//Load css
wp_enqueue_style('bb-mediaclip-block-style', BB_PLUGIN_CSS . 'bbVideoBlock.css');
wp_enqueue_style('bb-shortcode-style', BB_PLUGIN_CSS . 'bbShortcode.css');
require_once BB_PLUGIN_PAGES . 'shortcode.php';
}
function page_upload(){
$bbAPIOptions = BlueBillywig::instance()->get_api_options();
wp_enqueue_script('aws-sdk', 'https://sdk.amazonaws.com/js/aws-sdk-2.283.1.min.js'); //TODO: dl & include script
wp_enqueue_script('fine-uploader', BB_PLUGIN_JS . 'lib/fineUploader.modified.js');
wp_enqueue_script('bb-upload-script', BB_PLUGIN_JS . 'bbUpload.js', array('fine-uploader'));
wp_localize_script('bb-upload-script', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
wp_localize_script('bb-upload-script', 'publicationStub', $bbAPIOptions[BB_API_SETTINGS_PUBLICATION]);
wp_enqueue_style('bb-shortcode-style', BB_PLUGIN_CSS . 'bbSettings.css');
wp_enqueue_style('bb-upload-style', BB_PLUGIN_CSS . 'bbUpload.css');
require_once BB_PLUGIN_PAGES . 'upload.php';
}
function page_media_library(){
wp_enqueue_style('bb-mediaclip-block-style', BB_PLUGIN_CSS . 'bbVideoBlock.css');
wp_enqueue_style('bb-media-library-style', BB_PLUGIN_CSS . 'bbMediaLibrary.css');
wp_enqueue_style('bb-settings-style', BB_PLUGIN_CSS . 'bbSettings.css');
require_once BB_PLUGIN_PAGES . 'mediaLibrary.php';
}
//Register Wordpress menus
function register_menus() {
add_menu_page( 'Blue Billywig', 'Blue Billywig', 'manage_options', 'bb-plugin', 'page_home', BB_PLUGIN_IMG . 'icon.png' );
add_submenu_page( 'bb-plugin', 'Settings', 'Settings', 'manage_options', 'bb-plugin');
add_submenu_page( 'bb-plugin', 'Media Library', 'Media Library', 'manage_options', 'bb-library', 'page_media_library' );
add_submenu_page( 'bb-plugin', 'Upload Mediaclip', 'Upload Mediaclip', 'manage_options', 'bb-upload', 'page_upload' );
add_submenu_page( 'bb-plugin', 'Generate Shortcode', 'Generate Shortcode', 'manage_options', 'bb-shortcode', 'page_shortcode' );
if( is_plugin_active(get_top_level_plugin_dir_name(dirname(__FILE__)) . '/bluebillywig.php') &&
is_on_plugins_page() && !BlueBillywig::instance()->test_stored_api_key()) {
plugin_needs_configuration_notice();
}
}
//Register the Gutenberg editor embed block
function register_bb_mediaclip_block() {
//Register js
wp_register_script('bb-mediaclip-block', BB_PLUGIN_JS . 'bbVideoBlock.js',
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' )
);
//Register the block
register_block_type( 'bb-plugin/bb-mediaclip-block', array(
'editor_script' => 'bb-mediaclip-block',
) );
add_action('enqueue_block_assets', 'register_bb_mediaclip_block_assets');
}
function register_bb_mediaclip_block_assets(){
//Localize API-options & ajaxURL
$bbAPIOptions = BlueBillywig::instance()->get_api_options();
wp_localize_script('bb-mediaclip-block', 'publication', $bbAPIOptions[BB_API_SETTINGS_PUBLICATION]);
wp_localize_script('bb-mediaclip-block', 'allPlayouts', fetch_all_playouts());
wp_localize_script('bb-mediaclip-block', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
wp_localize_script('bb-mediaclip-block', 'BB_STRINGS', BB_BLOCK_STRINGS );
wp_enqueue_style('bb-mediaclip-block-style', BB_PLUGIN_CSS . 'bbVideoBlock.css');
}
//Register the TinyMCE embed widget
function add_mce_widget() {
if (!current_user_can('edit_posts') && !current_user_can('edit_pages') ) {
return;
}
if (get_user_option('rich_editing') == true) {
add_filter( 'mce_external_plugins', 'add_mce_widget_script' );
add_filter( 'mce_buttons', 'add_mce_widget_button' );
}
}
//Register new button in the TinyMCE editor
function add_mce_widget_button( $buttons ) {
array_push( $buttons, 'bb_mce_mediaclip' );
return $buttons;
}
//Add the TinyMCE widget script and localize it's needed values
function add_mce_widget_script( $plugin_array ) {
$plugin_array['bb_mce_mediaclip'] = BB_PLUGIN_JS . 'bbMediaclipWidget.js';
$playouts = fetch_all_playouts();
?>
<script> //localize data for BB widget script
var iconURL = '<?php echo BB_PLUGIN_IMG . "icon.png"; ?>';
var playouts = '<?php echo $playouts; ?>';
var ajaxURL = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
var BB_STRINGS = <?php echo json_encode(BB_WIDGET_STRINGS); ?>;
</script>
<style>
.mce-bb-widget-no-video-selected{color:red !important}
</style>
<?php
return $plugin_array;
}
function plugin_needs_configuration_notice(){
?>
<div class="notice notice-error is-dismissible">
<p>Before you can start using the <span class="bb-plugin-name-notice">
<img src='<?PHP echo BB_PLUGIN_IMG . 'icon.png' ?>'> Blue Billywig plugin</span> it needs to be
<strong><a href="./admin.php?page=bb-plugin">configured</a></strong>.</p>
</div>
<style>
.bb-plugin-name-notice{
background-color: #7AB1DF;
color: white;
padding: 3px 5px;
padding-top: 4px;
border-radius: 10px;
}
.bb-plugin-name-notice img{
background-color: white;
border-radius: 5px;
}
</style>
<?php
}
add_action( 'admin_head', 'add_mce_widget');
add_action( 'admin_menu', 'register_menus' );
add_action( 'init', 'register_bb_mediaclip_block' );
add_action( 'wp_ajax_search_videos_request', 'search_videos_request' );
add_action( 'wp_ajax_search_playouts_request', 'search_playouts_request' );
add_action( 'wp_ajax_search_single_video_request', 'search_single_video_request' );
add_action( 'wp_ajax_create_media_clip_request', 'create_media_clip_request' );
add_action( 'wp_ajax_fetch_upload_endpoint_request', 'fetch_upload_endpoint_request' );
add_shortcode( 'bbmediaclip', 'bb_mediaclip_shortcode' );
//Initialize plugin
if(class_exists("BlueBillywig")){
$bb = new BlueBillywig();
}
?>