class.videopress-ajax.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. class VideoPress_AJAX {
  3. /**
  4. * @var VideoPress_AJAX
  5. **/
  6. private static $instance = null;
  7. /**
  8. * Private VideoPress_AJAX constructor.
  9. *
  10. * Use the VideoPress_AJAX::init() method to get an instance.
  11. */
  12. private function __construct() {
  13. add_action( 'wp_ajax_videopress-get-upload-token', array( $this, 'wp_ajax_videopress_get_upload_token' ) );
  14. add_action( 'wp_ajax_videopress-update-transcoding-status', array(
  15. $this,
  16. 'wp_ajax_update_transcoding_status'
  17. ), -1 );
  18. }
  19. /**
  20. * Initialize the VideoPress_AJAX and get back a singleton instance.
  21. *
  22. * @return VideoPress_AJAX
  23. */
  24. public static function init() {
  25. if ( is_null( self::$instance ) ) {
  26. self::$instance = new VideoPress_AJAX;
  27. }
  28. return self::$instance;
  29. }
  30. /**
  31. * Ajax method that is used by the VideoPress uploader to get a token to upload a file to the wpcom api.
  32. *
  33. * @return void
  34. */
  35. public function wp_ajax_videopress_get_upload_token() {
  36. $options = VideoPress_Options::get_options();
  37. $args = array(
  38. 'method' => 'POST',
  39. // 'sslverify' => false,
  40. );
  41. $endpoint = "sites/{$options['shadow_blog_id']}/media/token";
  42. $result = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $args );
  43. if ( is_wp_error( $result ) ) {
  44. wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
  45. return;
  46. }
  47. $response = json_decode( $result['body'], true );
  48. if ( empty( $response['upload_token'] ) ) {
  49. wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
  50. return;
  51. }
  52. $response['upload_action_url'] = videopress_make_media_upload_path( $options['shadow_blog_id'] );
  53. wp_send_json_success( $response );
  54. }
  55. /**
  56. * Ajax action to update the video transcoding status from the WPCOM API.
  57. *
  58. * @return void
  59. */
  60. public function wp_ajax_update_transcoding_status() {
  61. if ( ! isset( $_POST['post_id'] ) ) {
  62. wp_send_json_error( array( 'message' => __( 'A valid post_id is required.', 'jetpack' ) ) );
  63. return;
  64. }
  65. $post_id = (int) $_POST['post_id'];
  66. if ( ! videopress_update_meta_data( $post_id ) ) {
  67. wp_send_json_error( array( 'message' => __( 'That post does not have a VideoPress video associated to it.', 'jetpack' ) ) );
  68. return;
  69. }
  70. wp_send_json_success( array(
  71. 'message' => __( 'Status updated', 'jetpack' ),
  72. 'status' => videopress_get_transcoding_status( $post_id )
  73. ) );
  74. }
  75. }
  76. // Let's start this thing up.
  77. VideoPress_AJAX::init();