class.jetpack-videopress.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * VideoPress in Jetpack
  4. *
  5. */
  6. class Jetpack_VideoPress {
  7. /** @var string */
  8. public $module = 'videopress';
  9. /** @var int */
  10. public $version = 5;
  11. /**
  12. * Singleton
  13. */
  14. public static function init() {
  15. static $instance = false;
  16. if ( ! $instance ) {
  17. $instance = new Jetpack_VideoPress;
  18. }
  19. return $instance;
  20. }
  21. /**
  22. * Jetpack_VideoPress constructor.
  23. *
  24. * Sets up the initializer and makes sure that videopress activates and deactivates properly.
  25. */
  26. private function __construct() {
  27. //$this->version = time(); // <s>ghost</s> cache busters!
  28. add_action( 'init', array( $this, 'on_init' ) );
  29. add_action( 'jetpack_deactivate_module_videopress', array( $this, 'jetpack_module_deactivated' ) );
  30. }
  31. /**
  32. * Fires on init
  33. */
  34. public function on_init() {
  35. add_action( 'wp_enqueue_media', array( $this, 'enqueue_admin_scripts' ) );
  36. add_filter( 'plupload_default_settings', array( $this, 'videopress_pluploder_config' ) );
  37. add_filter( 'wp_get_attachment_url', array( $this, 'update_attachment_url_for_videopress' ), 10, 2 );
  38. if ( Jetpack::active_plan_supports( 'videopress' ) ) {
  39. add_filter( 'upload_mimes', array( $this, 'add_video_upload_mimes' ), 999 );
  40. }
  41. add_action( 'admin_print_footer_scripts', array( $this, 'print_in_footer_open_media_add_new' ) );
  42. add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) );
  43. add_filter( 'wp_mime_type_icon', array( $this, 'wp_mime_type_icon' ), 10, 3 );
  44. add_filter( 'wp_video_extensions', array( $this, 'add_videopress_extenstion' ) );
  45. VideoPress_Scheduler::init();
  46. VideoPress_XMLRPC::init();
  47. }
  48. /**
  49. * Runs when the VideoPress module is deactivated.
  50. */
  51. public function jetpack_module_deactivated() {
  52. VideoPress_Options::delete_options();
  53. }
  54. /**
  55. * A can of coke
  56. *
  57. * Similar to current_user_can, but internal to VideoPress. Returns
  58. * true if the given VideoPress capability is allowed by the given user.
  59. */
  60. public function can( $cap, $user_id = false ) {
  61. if ( ! $user_id ) {
  62. $user_id = get_current_user_id();
  63. }
  64. // Connection owners are allowed to do all the things.
  65. if ( $this->is_connection_owner( $user_id ) ) {
  66. return true;
  67. }
  68. // Additional and internal caps checks
  69. if ( ! user_can( $user_id, 'upload_files' ) ) {
  70. return false;
  71. }
  72. if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) ) {
  73. return false;
  74. }
  75. if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) ) {
  76. return false;
  77. }
  78. return true;
  79. }
  80. /**
  81. * Returns true if the provided user is the Jetpack connection owner.
  82. */
  83. public function is_connection_owner( $user_id = false ) {
  84. if ( ! $user_id ) {
  85. $user_id = get_current_user_id();
  86. }
  87. $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
  88. return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id;
  89. }
  90. /**
  91. * Register and enqueue VideoPress admin styles.
  92. */
  93. public function enqueue_admin_styles() {
  94. wp_register_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
  95. wp_enqueue_style( 'videopress-admin' );
  96. }
  97. /**
  98. * Register VideoPress admin scripts.
  99. */
  100. public function enqueue_admin_scripts() {
  101. if ( did_action( 'videopress_enqueue_admin_scripts' ) ) {
  102. return;
  103. }
  104. if ( $this->should_override_media_uploader() ) {
  105. wp_enqueue_script(
  106. 'videopress-plupload',
  107. Jetpack::get_file_url_for_environment(
  108. '_inc/build/videopress/js/videopress-plupload.min.js',
  109. 'modules/videopress/js/videopress-plupload.js'
  110. ),
  111. array(
  112. 'jquery',
  113. 'wp-plupload'
  114. ),
  115. $this->version
  116. );
  117. wp_enqueue_script(
  118. 'videopress-uploader',
  119. Jetpack::get_file_url_for_environment(
  120. '_inc/build/videopress/js/videopress-uploader.min.js',
  121. 'modules/videopress/js/videopress-uploader.js'
  122. ),
  123. array(
  124. 'videopress-plupload'
  125. ),
  126. $this->version
  127. );
  128. wp_enqueue_script(
  129. 'media-video-widget-extensions',
  130. Jetpack::get_file_url_for_environment(
  131. '_inc/build/videopress/js/media-video-widget-extensions.min.js',
  132. 'modules/videopress/js/media-video-widget-extensions.js'
  133. ),
  134. array(),
  135. $this->version,
  136. true
  137. );
  138. }
  139. /**
  140. * Fires after VideoPress scripts are enqueued in the dashboard.
  141. *
  142. * @since 2.5.0
  143. */
  144. do_action( 'videopress_enqueue_admin_scripts' );
  145. }
  146. /**
  147. * An override for the attachment url, which returns back the WPCOM VideoPress processed url.
  148. *
  149. * This is an action proxy to the videopress_get_attachment_url() utility function.
  150. *
  151. * @param string $url
  152. * @param int $post_id
  153. *
  154. * @return string
  155. */
  156. public function update_attachment_url_for_videopress( $url, $post_id ) {
  157. if ( $videopress_url = videopress_get_attachment_url( $post_id ) ) {
  158. return $videopress_url;
  159. }
  160. return $url;
  161. }
  162. /**
  163. * Modify the default plupload config to turn on videopress specific filters.
  164. */
  165. public function videopress_pluploder_config( $config ) {
  166. if ( ! isset( $config['filters']['max_file_size'] ) ) {
  167. $config['filters']['max_file_size'] = wp_max_upload_size() . 'b';
  168. }
  169. $config['filters']['videopress_check_uploads'] = $config['filters']['max_file_size'];
  170. // We're doing our own check in the videopress_check_uploads filter.
  171. unset( $config['filters']['max_file_size'] );
  172. return $config;
  173. }
  174. /**
  175. * Helper function to determine if the media uploader should be overridden.
  176. *
  177. * The rules are simple, only try to load the script when on the edit post or new post pages.
  178. *
  179. * @return bool
  180. */
  181. protected function should_override_media_uploader() {
  182. global $pagenow;
  183. // Only load in the admin
  184. if ( ! is_admin() ) {
  185. return false;
  186. }
  187. $acceptable_pages = array(
  188. 'post-new.php',
  189. 'post.php',
  190. 'upload.php',
  191. 'customize.php',
  192. );
  193. // Only load on the post, new post, or upload pages.
  194. if ( !in_array( $pagenow, $acceptable_pages ) ) {
  195. return false;
  196. }
  197. $options = VideoPress_Options::get_options();
  198. return $options['shadow_blog_id'] > 0;
  199. }
  200. /**
  201. * A work-around / hack to make it possible to go to the media library with the add new box open.
  202. *
  203. * @return bool
  204. */
  205. public function print_in_footer_open_media_add_new() {
  206. global $pagenow;
  207. // Only load in the admin
  208. if ( ! is_admin() ) {
  209. return false;
  210. }
  211. if ( $pagenow !== 'upload.php' ) {
  212. return false;
  213. }
  214. if ( ! isset ( $_GET['action'] ) || $_GET['action'] !== 'add-new' ) {
  215. return false;
  216. }
  217. ?>
  218. <script type="text/javascript">
  219. ( function( $ ) {
  220. window.setTimeout( function() {
  221. $('#wp-media-grid .page-title-action').click();
  222. }, 500 );
  223. }( jQuery ) );
  224. </script>
  225. <?php
  226. }
  227. /**
  228. * Makes sure that all video mimes are added in, as multi site installs can remove them.
  229. *
  230. * @param array $existing_mimes
  231. * @return array
  232. */
  233. public function add_video_upload_mimes( $existing_mimes = array() ) {
  234. $mime_types = wp_get_mime_types();
  235. $video_types = array_filter( $mime_types, array( $this, 'filter_video_mimes' ) );
  236. foreach ( $video_types as $key => $value ) {
  237. $existing_mimes[ $key ] = $value;
  238. }
  239. // Make sure that videopress mimes are considered videos.
  240. $existing_mimes['videopress'] = 'video/videopress';
  241. return $existing_mimes;
  242. }
  243. /**
  244. * Filter designed to get rid of non video mime types.
  245. *
  246. * @param string $value
  247. * @return int
  248. */
  249. public function filter_video_mimes( $value ) {
  250. return preg_match( '@^video/@', $value );
  251. }
  252. /**
  253. * @param string $icon
  254. * @param string $mime
  255. * @param int $post_id
  256. *
  257. * @return string
  258. */
  259. public function wp_mime_type_icon( $icon, $mime, $post_id ) {
  260. if ( $mime !== 'video/videopress' ) {
  261. return $icon;
  262. }
  263. $status = get_post_meta( $post_id, 'videopress_status', true );
  264. if ( $status === 'complete' ) {
  265. return $icon;
  266. }
  267. return 'https://wordpress.com/wp-content/mu-plugins/videopress/images/media-video-processing-icon.png';
  268. }
  269. /**
  270. * @param array $extensions
  271. *
  272. * @return array
  273. */
  274. public function add_videopress_extenstion( $extensions ) {
  275. $extensions[] = 'videopress';
  276. return $extensions;
  277. }
  278. }
  279. // Initialize the module.
  280. Jetpack_VideoPress::init();