Nessuna descrizione

class.videopress-ajax.php 2.6KB

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