PluginProbe
FV Player 8 / 8.1
FV Player 8 v8.1
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.1, at models/media-browser.php

248 lines 8.4 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 'validate_file_nonce' => wp_create_nonce( 'fv_flowplayer_validate_file' ),
104 )
105 );
106
107 $this->include_base_uploader_css();
108
109 $this->s3_assets_loaded = true;
110 }
111
112 function get_formatted_assets_data() {
113 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);
114 }
115
116 function get_output() {
117 if( !isset($_POST['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), $this->ajax_action_name ) ) {
118 return array(
119 'items' => array(),
120 'name' => '/',
121 'path' => '/',
122 'type' => 'folder',
123 'err' => 'Invalid nonce'
124 );
125 }
126
127 $output = array(
128 'name' => 'Home',
129 'type' => 'folder',
130 'path' => !empty($_POST['path']) ? sanitize_text_field( $_POST['path'] ) : 'Home/',
131 'items' => array()
132 );
133
134 return $output;
135 }
136
137 function get_metadata( $s3Client, $bucket ) {
138 if( !isset($_POST['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), $this->ajax_action_name ) ) {
139 return array(
140 'items' => array(),
141 'name' => '/',
142 'path' => '/',
143 'type' => 'folder',
144 'err' => 'Invalid nonce'
145 );
146 }
147
148 $args = array(
149 'Bucket' => $bucket,
150 'Delimiter' => '/',
151 );
152
153 $request_path = !empty($_POST['path']) ? str_replace( 'Home/', '', sanitize_text_field( $_POST['path'] ) ) : false;
154
155 if( $request_path ) {
156 $args['Prefix'] = $request_path;
157 }
158
159 $paged = $s3Client->getPaginator('ListObjects',$args);
160
161 $date_format = get_option( 'date_format' );
162
163 return array( $request_path, $paged, $date_format );
164 }
165
166 function get_output_items( $output, $s3Client, $request_path, $paged, $date_format, $bucket, $sum_up = NULL, $custom_domain = NULL ) {
167 foreach( $paged AS $res ) {
168
169 $folders = !empty($res['CommonPrefixes']) ? $res['CommonPrefixes'] : array();
170 $files = $res->get('Contents');
171 if( !$files ) $files = array();
172
173 $objects = array_merge( $folders, $files );
174
175 foreach ( $objects as $object ) {
176 if ( ! isset( $objectarray ) ) {
177 $objectarray = array();
178 }
179
180 $item = array();
181
182 $path = ! empty( $object['Prefix'] ) ? $object['Prefix'] : $object['Key'];
183
184 if( isset($sum_up) && !empty($object['Key']) && preg_match( '~\.ts$~', $object['Key'] ) ) {
185 if( empty($sum_up['ts']) ) $sum_up['ts'] = 0;
186 $sum_up['ts']++;
187 continue;
188 }
189
190 $item['path'] = 'Home/' . $path;
191
192 if( $request_path ) {
193 if( $request_path == $path ) continue; // sometimes the current folder is present in the response, weird
194
195 $item['name'] = str_replace( $request_path, '', $path );
196 } else {
197 $item['name'] = $path;
198 }
199
200 if( !empty($object['Size']) ) {
201 $item['type'] = 'file';
202 $item['size'] = $object['Size'];
203 $item['modified'] = gmdate($date_format, strtotime($object['LastModified']));
204
205 if( isset($object['LastModified']) ) {
206 $item['LastModified'] = $object['LastModified'];
207 }
208
209 $link = (string) $s3Client->getObjectUrl( $bucket, $path );
210
211 $link = $this->decode_link_components( $link );
212
213 // replace link with CloudFront URL, if we have one
214 if( !empty($custom_domain) ) {
215 $link = $this->get_custom_domain_url($link ,$bucket, $custom_domain);
216 }
217
218 $item['link'] = $link;
219
220 if (preg_match('/\.(jpg|jpeg|png|gif)$/i', $item['name'])) {
221 $item['splash'] = htmlspecialchars( apply_filters('fv_flowplayer_splash', $link ) );
222 }
223 } else {
224 $item['type'] = 'folder';
225 $item['items'] = array();
226 }
227
228 $output['items'][] = $item;
229
230 if (strtolower(substr($item['name'], strrpos($item['name'], '.') + 1)) === 'ts') {
231 continue;
232 }
233
234 }
235 }
236
237 return array( $output, $sum_up );
238 }
239
240 function load_assets() {
241 $json_final = $this->get_formatted_assets_data();
242
243 wp_send_json( $json_final );
244 wp_die();
245 }
246
247 }
248