PluginProbe
FV Player 8 / trunk
FV Player 8 vtrunk
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 / splash-download.php

splash-download.php in FV Player 8 trunk, at models/splash-download.php

152 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 if( !class_exists('FV_Player_Splash_Download') ) :
8
9 class FV_Player_Splash_Download {
10
11 public function __construct() {
12 add_filter('fv_player_meta_data', array( $this, 'splash_data' ), 20, 3);
13 }
14
15 /**
16 * Add splash data to video data
17 *
18 * @param array $video_data
19 * @param int|false $post_id
20 * @param FV_Player_Db_Video $videoObj Used to avoid downloading the splash if the video
21 * already has a splash set.
22 *
23 * @return array
24 */
25 function splash_data($video_data, $post_id, $videoObj = false ) {
26 // Do not download if it's not a player video stored in database
27 if ( !$videoObj ) {
28 return $video_data;
29 }
30
31 // Do not download if video already has splash set
32 if ( $videoObj && method_exists( $videoObj, 'getSplash' ) && $videoObj->getSplash() ) {
33 return $video_data;
34 }
35
36 if( is_array($video_data) && !empty($video_data['thumbnail']) ) {
37 $splash_data = $this->download_splash( $video_data['thumbnail'], isset($video_data['name']) ? $video_data['name'] : false );
38
39 if( !empty( $splash_data ) ) {
40 $video_data['thumbnail'] = $splash_data['url'];
41 $video_data['splash_attachment_id'] = $splash_data['attachment_id'];
42 }
43 }
44
45 return $video_data;
46 }
47
48 /**
49 * Download splash image from url and return attachment id
50 *
51 * @param string $splash_url
52 * @param string $title
53 *
54 * @return array|false
55 */
56 public function download_splash( $splash_url, $title = null ) {
57 $limit = 128 - 5; // .jpeg
58
59 if( empty($title) ) {
60 $arr = explode('/', $splash_url);
61 $title = end($arr);
62
63 if( preg_match( '/\.(png|jpg|jpeg|gif|webp)/', $title, $matches ) ) {
64 $title = pathinfo($title, PATHINFO_FILENAME); // remove file extension
65 }
66 }
67
68 $sanitized_title = sanitize_title($title);
69
70 if( function_exists('mb_strinwidth') ) {
71 $sanitized_title = mb_strimwidth($sanitized_title, 0, $limit, '', 'UTF-8');
72 } else if( strlen( $sanitized_title ) > $limit ) {
73 $sanitized_title = substr($sanitized_title, 0, $limit);
74 }
75
76 $upload_dir = wp_upload_dir();
77 $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
78
79 // if the function its not available, require it
80 if ( ! function_exists( 'download_url' ) ) {
81 require_once ABSPATH . 'wp-admin/includes/file.php';
82 }
83
84 $file_name = $sanitized_title . '.jpg';
85 $file_path = download_url( $splash_url );
86
87 if ( is_wp_error( $file_path ) ) {
88 return false;
89 }
90
91 // Handle upload file
92 if( !function_exists( 'wp_handle_sideload' ) ) {
93 require_once( ABSPATH . 'wp-admin/includes/file.php' );
94 }
95
96 // Debug error
97 if( !function_exists( 'wp_get_current_user' ) ) {
98 require_once( ABSPATH . 'wp-includes/pluggable.php' );
99 }
100
101 // New file
102 $file = array();
103 $file['error'] = '';
104 $file['tmp_name'] = $file_path;
105 $file['name'] = $file_name;
106 $file['type'] = mime_content_type( $file_path );
107 $file['size'] = filesize( $file_path );
108
109 $file_return = wp_handle_sideload( $file, array( 'test_form' => false ) );
110
111 if ( ! empty( $file_return['error'] ) ) {
112 @unlink( $file['tmp_name']);
113 return false;
114 }
115
116 $file_name = $file_return['file'];
117
118 $attachment = array(
119 'post_mime_type' => $file_return['type'],
120 'post_title' => $title,
121 'post_content' => '',
122 'post_status' => 'inherit',
123 'guid' => $upload_dir['url'] . '/' . basename($file_name)
124 );
125
126 $attach_id = wp_insert_attachment( $attachment, $file_name, 0, true );
127
128 if( is_wp_error( $attach_id ) ) {
129 return false;
130 } else {
131
132 require_once(ABSPATH . 'wp-admin/includes/image.php');
133
134 update_post_meta( $attach_id, 'fv_player_original_splash_url', $splash_url ); // store original splash url in attachment meta
135
136 $attach_data = wp_generate_attachment_metadata( $attach_id, $file_name );
137 wp_update_attachment_metadata( $attach_id, $attach_data );
138
139 $img_url = wp_get_attachment_image_url($attach_id, 'full', false);
140
141 return array( 'url' => $img_url, 'attachment_id' => $attach_id ) ;
142 }
143
144 }
145
146 }
147
148 global $FV_Player_Splash_Download;
149 $FV_Player_Splash_Download = new FV_Player_Splash_Download;
150
151 endif;
152