PluginProbe
FV Player 8 / 8.0.21
FV Player 8 v8.0.21
trunk 8.0.18 8.0.19 8.0.20 8.0.21 8.0.25 8.0.27 8.1 8.1.3
fv-player / models / media-browser.php

media-browser.php in FV Player 8 8.0.21, at models/media-browser.php

251 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class FV_Player_Media_Browser {
4
5 public $ajax_action_name = false;
6 public $ajax_action_name_add_new_folder = false;
7 private $s3_assets_loaded = false;
8
9 public function __construct($args) {
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 // load base JS
16 add_action( 'edit_form_after_editor', array($this, 'init_base'), 1 ); // for old WP editor
17 add_action( 'enqueue_block_editor_assets', array($this, 'init_base') ); // for Gutenberg
18 add_action( 'admin_print_scripts-toplevel_page_fv_player', array($this, 'init_base'), 0 ); // wp-admin -> FV Player
19 add_action( 'admin_print_scripts-widgets.php', array($this, 'init_base'), 0 ); // wp-admin -> Widgets
20
21 add_action( 'fvplayer_editor_load', array($this, 'init_base'), 0 ); // Front-end editor
22
23 // TODO: Video encoder class should take care of this
24 add_action( 'admin_print_scripts-fv-player_page_fv_player_coconut', array($this, 'init_base'), 0 ); // wp-admin -> FV Player -> Coconut Jobs
25 add_action( 'admin_print_scripts-fv-player_page_fv_player_bunny_stream', array($this, 'init_base'), 0 ); // wp-admin -> FV Player -> Bunny Stream Jobs
26 add_action( 'fv_player_media_browser_enqueue_base_uploader_css', array( $this, 'include_base_uploader_css' ) );
27
28 if( is_array($args) ) {
29 $args = wp_parse_args($args ,array(
30 'ajax_action_name' => false,
31 'ajax_action_name_add_new_folder' => false,
32 )
33 );
34
35 $this->ajax_action_name = $args['ajax_action_name'];
36 $this->ajax_action_name_add_new_folder = $args['ajax_action_name_add_new_folder'];
37
38 } else {
39 // register extending class WP AJAX action
40 $this->ajax_action_name = $args;
41 }
42
43 $this->register();
44 }
45
46 abstract function init();
47
48 // TODO: should be abstract
49 public function add_folder_ajax() {}
50
51 // TOTO: should be abstract
52 function decode_link_components( $link ) {}
53
54 // TOTO: should be abstract
55 function get_custom_domain_url( $link, $bucket, $custom_domain ) {}
56
57 function init_base() {
58 global $fv_wp_flowplayer_ver;
59 wp_enqueue_media();
60 wp_enqueue_script( 'flowplayer-browser-base', flowplayer::get_plugin_url().'/js/media-library-browser-base.js', array('jquery'), filemtime( dirname( __FILE__ ) . '/../js/media-library-browser-base.js' ), true );
61 wp_enqueue_style('fvwpflowplayer-s3-browser', flowplayer::get_plugin_url().'/css/s3-browser.css','',$fv_wp_flowplayer_ver);
62 wp_localize_script('flowplayer-browser-base', 'fv_flowplayer_browser', array(
63 'ajaxurl' => flowplayer::get_plugin_url().'/controller/s3-ajax.php',
64 )
65 );
66 $this->init();
67 }
68
69 function init_for_gutenberg_base() {
70 add_action( 'admin_footer', array($this, 'init_base'), 1 );
71 }
72
73 function register() {
74 add_action( $this->ajax_action_name, array($this, 'load_assets') );
75 }
76
77 function include_aws_sdk() {
78 if ( ! class_exists( 'Aws\S3\S3Client' ) ) {
79 if ( file_exists( dirname( __FILE__ ) . "/../vendor/autoload.php" ) ) {
80 require_once( dirname( __FILE__ ) . "/../vendor/autoload.php" );
81
82 } else {
83 wp_send_json( array( 'err' => 'AWS SDK not found. please make sure you run <code>composer install --no-dev</code> in FV Player plugin folder or install plugin from WordPress.org.' ) );
84 die();
85 }
86 }
87 }
88
89 function include_base_uploader_css() {
90 wp_enqueue_style( 'fv-player-s3-uploader-css', flowplayer::get_plugin_url() . '/css/s3-uploader.css', array(), filemtime( dirname(__FILE__).'/../css/s3-uploader.css' ) );
91 }
92
93 function include_s3_upload_assets() {
94 if ( $this->s3_assets_loaded ) {
95 return;
96 }
97
98 global $fv_wp_flowplayer_ver;
99
100 wp_enqueue_script( 'fv-player-s3-uploader', flowplayer::get_plugin_url().'/js/s3upload.js', array( 'flowplayer-browser-base' ), $fv_wp_flowplayer_ver );
101 wp_enqueue_script( 'fv-player-s3-uploader-base', flowplayer::get_plugin_url().'/js/s3-upload-base.js', array( 'flowplayer-browser-base' ), $fv_wp_flowplayer_ver );
102 wp_localize_script( 'fv-player-s3-uploader', 'fv_player_s3_uploader', array(
103 'create_multiupload_nonce' => wp_create_nonce( 'fv_flowplayer_create_multiupload' ),
104 'multiupload_send_part_nonce' => wp_create_nonce( 'fv_flowplayer_multiupload_send_part' ),
105 'multiupload_abort_nonce' => wp_create_nonce( 'fv_flowplayer_multiupload_abort' ),
106 'multiupload_complete_nonce' => wp_create_nonce( 'fv_flowplayer_multiupload_complete' ),
107 )
108 );
109
110 $this->include_base_uploader_css();
111
112 $this->s3_assets_loaded = true;
113 }
114
115 function get_formatted_assets_data() {
116 return json_decode('{"items":{"name":"Home","type":"folder","path":"Home\/","items":[{"name":"01 The Beginning.mp3","size":2117536,"type":"file","path":"Home\/01 The Beginning.mp3","link":"http:\/\/sjdua7x04ygyx.cloudfront.net\/01%20The%20Beginning.mp3"},{"name":"Fender_Bass_Guitar_Patent.jpg","size":495756,"type":"file","path":"Home\/Fender_Bass_Guitar_Patent.jpg","link":"http:\/\/sjdua7x04ygyx.cloudfront.net\/Fender_Bass_Guitar_Patent.jpg"}]}}', true);
117 }
118
119 function get_output() {
120 if( !isset($_POST['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), $this->ajax_action_name ) ) {
121 return array(
122 'items' => array(),
123 'name' => '/',
124 'path' => '/',
125 'type' => 'folder',
126 'err' => 'Invalid nonce'
127 );
128 }
129
130 $output = array(
131 'name' => 'Home',
132 'type' => 'folder',
133 'path' => !empty($_POST['path']) ? sanitize_text_field( $_POST['path'] ) : 'Home/',
134 'items' => array()
135 );
136
137 return $output;
138 }
139
140 function get_metadata( $s3Client, $bucket ) {
141 if( !isset($_POST['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), $this->ajax_action_name ) ) {
142 return array(
143 'items' => array(),
144 'name' => '/',
145 'path' => '/',
146 'type' => 'folder',
147 'err' => 'Invalid nonce'
148 );
149 }
150
151 $args = array(
152 'Bucket' => $bucket,
153 'Delimiter' => '/',
154 );
155
156 $request_path = !empty($_POST['path']) ? str_replace( 'Home/', '', sanitize_text_field( $_POST['path'] ) ) : false;
157
158 if( $request_path ) {
159 $args['Prefix'] = $request_path;
160 }
161
162 $paged = $s3Client->getPaginator('ListObjects',$args);
163
164 $date_format = get_option( 'date_format' );
165
166 return array( $request_path, $paged, $date_format );
167 }
168
169 function get_output_items( $output, $s3Client, $request_path, $paged, $date_format, $bucket, $sum_up = NULL, $custom_domain = NULL ) {
170 foreach( $paged AS $res ) {
171
172 $folders = !empty($res['CommonPrefixes']) ? $res['CommonPrefixes'] : array();
173 $files = $res->get('Contents');
174 if( !$files ) $files = array();
175
176 $objects = array_merge( $folders, $files );
177
178 foreach ( $objects as $object ) {
179 if ( ! isset( $objectarray ) ) {
180 $objectarray = array();
181 }
182
183 $item = array();
184
185 $path = ! empty( $object['Prefix'] ) ? $object['Prefix'] : $object['Key'];
186
187 if( isset($sum_up) && !empty($object['Key']) && preg_match( '~\.ts$~', $object['Key'] ) ) {
188 if( empty($sum_up['ts']) ) $sum_up['ts'] = 0;
189 $sum_up['ts']++;
190 continue;
191 }
192
193 $item['path'] = 'Home/' . $path;
194
195 if( $request_path ) {
196 if( $request_path == $path ) continue; // sometimes the current folder is present in the response, weird
197
198 $item['name'] = str_replace( $request_path, '', $path );
199 } else {
200 $item['name'] = $path;
201 }
202
203 if( !empty($object['Size']) ) {
204 $item['type'] = 'file';
205 $item['size'] = $object['Size'];
206 $item['modified'] = gmdate($date_format, strtotime($object['LastModified']));
207
208 if( isset($object['LastModified']) ) {
209 $item['LastModified'] = $object['LastModified'];
210 }
211
212 $link = (string) $s3Client->getObjectUrl( $bucket, $path );
213
214 $link = $this->decode_link_components( $link );
215
216 // replace link with CloudFront URL, if we have one
217 if( !empty($custom_domain) ) {
218 $link = $this->get_custom_domain_url($link ,$bucket, $custom_domain);
219 }
220
221 $item['link'] = $link;
222
223 if (preg_match('/\.(jpg|jpeg|png|gif)$/i', $item['name'])) {
224 $item['splash'] = htmlspecialchars( apply_filters('fv_flowplayer_splash', $link ) );
225 }
226 } else {
227 $item['type'] = 'folder';
228 $item['items'] = array();
229 }
230
231 $output['items'][] = $item;
232
233 if (strtolower(substr($item['name'], strrpos($item['name'], '.') + 1)) === 'ts') {
234 continue;
235 }
236
237 }
238 }
239
240 return array( $output, $sum_up );
241 }
242
243 function load_assets() {
244 $json_final = $this->get_formatted_assets_data();
245
246 wp_send_json( $json_final );
247 wp_die();
248 }
249
250 }
251