VideoPress_AJAX

The WordPress Core VideoPress AJAX class.

Defined (1)

The class is defined in the following location(s).

/modules/videopress/class.videopress-ajax.php  
  1. class VideoPress_AJAX { 
  2.  
  3. /** 
  4. * @var VideoPress_AJAX 
  5. **/ 
  6. private static $instance = null; 
  7.  
  8. /** 
  9. * Private VideoPress_AJAX constructor. 
  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.  
  15. add_action( 'wp_ajax_videopress-update-transcoding-status', array( 
  16. $this,  
  17. 'wp_ajax_update_transcoding_status' 
  18. ), -1 ); 
  19.  
  20. /** 
  21. * Initialize the VideoPress_AJAX and get back a singleton instance. 
  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. * @return void 
  33. */ 
  34. public function wp_ajax_videopress_get_upload_token() { 
  35.  
  36. $options = VideoPress_Options::get_options(); 
  37.  
  38. $args = array( 
  39. 'method' => 'POST',  
  40. // 'sslverify' => false,  
  41. ); 
  42.  
  43. $endpoint = "sites/{$options['shadow_blog_id']}/media/token"; 
  44. $result = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $args ); 
  45.  
  46. if ( is_wp_error( $result ) ) { 
  47. wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) ); 
  48. return; 
  49.  
  50. $response = json_decode( $result['body'], true ); 
  51.  
  52. if ( empty( $response['upload_token'] ) ) { 
  53. wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) ); 
  54. return; 
  55.  
  56. $response['upload_action_url'] = videopress_make_media_upload_path( $options['shadow_blog_id'] ); 
  57.  
  58. wp_send_json_success( $response ); 
  59.  
  60. /** 
  61. * Ajax action to update the video transcoding status from the WPCOM API. 
  62. * @return void 
  63. */ 
  64. public function wp_ajax_update_transcoding_status() { 
  65. if ( ! isset( $_POST['post_id'] ) ) { 
  66. wp_send_json_error( array( 'message' => __( 'A valid post_id is required.', 'jetpack' ) ) ); 
  67. return; 
  68.  
  69. $post_id = (int) $_POST['post_id']; 
  70.  
  71. if ( ! videopress_update_meta_data( $post_id ) ) { 
  72. wp_send_json_error( array( 'message' => __( 'That post does not have a VideoPress video associated to it.', 'jetpack' ) ) ); 
  73. return; 
  74.  
  75. wp_send_json_success( array( 
  76. 'message' => __( 'Status updated', 'jetpack' ),  
  77. 'status' => videopress_get_transcoding_status( $post_id ) 
  78. ) );