class.videopress-cli.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  3. /**
  4. * VideoPress command line utilities.
  5. */
  6. class VideoPress_CLI extends WP_CLI_Command {
  7. /**
  8. * Import a VideoPress Video
  9. *
  10. * ## OPTIONS
  11. *
  12. * <guid>: Import the video with the specified guid
  13. *
  14. * ## EXAMPLES
  15. *
  16. * wp videopress import kUJmAcSf
  17. *
  18. */
  19. public function import( $args ) {
  20. $guid = $args[0];
  21. $attachment_id = create_local_media_library_for_videopress_guid( $guid );
  22. if ( $attachment_id && ! is_wp_error( $attachment_id ) ) {
  23. WP_CLI::success( sprintf( __( 'The video has been imported as Attachment ID %d', 'jetpack' ), $attachment_id ) );
  24. } else {
  25. WP_CLI::error( __( 'An error has been encountered.', 'jetpack' ) );
  26. }
  27. }
  28. /**
  29. * Manually runs the job to cleanup videos from the media library that failed during the upload process.
  30. *
  31. * ## EXAMPLES
  32. *
  33. * wp videopress cleanup_videos
  34. */
  35. public function cleanup_videos() {
  36. $num_cleaned = videopress_cleanup_media_library();
  37. WP_CLI::success( sprintf( _n( 'Cleaned up %d video.', 'Cleaned up a total of %d videos.', $num_cleaned, 'jetpack' ), $num_cleaned ) );
  38. }
  39. /**
  40. * List out all of the crons that can be run.
  41. *
  42. * ## EXAMPLES
  43. *
  44. * wp videopress list_crons
  45. */
  46. public function list_crons() {
  47. $scheduler = VideoPress_Scheduler::init();
  48. $crons = $scheduler->get_crons();
  49. $schedules = wp_get_schedules();
  50. if ( count( $crons ) === 0 ) {
  51. WP_CLI::success( __( 'Found no available cron jobs.', 'jetpack' ) );
  52. } else {
  53. WP_CLI::success( sprintf( _n( 'Found %d available cron job.', 'Found %d available cron jobs.', count( $crons ), 'jetpack' ), count( $crons ) ) );
  54. }
  55. foreach ( $crons as $cron_name => $cron ) {
  56. $interval = isset( $schedules[ $cron['interval'] ]['display'] ) ? $schedules[ $cron['interval'] ]['display'] : $cron['interval'];
  57. $runs_next = $scheduler->check_cron( $cron_name );
  58. $status = $runs_next ? sprintf( 'Scheduled - Runs Next at %s GMT', gmdate( 'Y-m-d H:i:s', $runs_next ) ) : 'Not Scheduled';
  59. WP_CLI::log( 'Name: ' . $cron_name );
  60. WP_CLI::log( 'Method: ' . $cron['method'] );
  61. WP_CLI::log( 'Interval: ' . $interval );
  62. WP_CLI::log( 'Status: ' . $status );
  63. }
  64. }
  65. /**
  66. * Checks for the current status of a cron job.
  67. *
  68. * ## OPTIONS
  69. *
  70. * <cron_name>: The name of the cron job to check
  71. *
  72. * ## EXAMPLES
  73. *
  74. * wp videopress cron_status cleanup
  75. */
  76. public function cron_status( $args ) {
  77. if ( ! isset( $args[0] ) ) {
  78. return WP_CLI::error( __( 'You need to provide the name of the cronjob to schedule.', 'jetpack' ) );
  79. }
  80. $scheduler = VideoPress_Scheduler::init();
  81. if ( ! $scheduler->is_cron_valid( $args[0] ) ) {
  82. return WP_CLI::error( sprintf( __( 'There is no cron named %s.', 'jetpack' ), $args[0] ) );
  83. }
  84. $time = $scheduler->check_cron( $args[0] );
  85. if ( ! $time ) {
  86. WP_CLI::success( __( 'The cron is not scheduled to run.', 'jetpack' ) );
  87. } else {
  88. WP_CLI::success( sprintf( __( 'Cron will run at: %s GMT', 'jetpack' ), gmdate( 'Y-m-d H:i:s', $time ) ) );
  89. }
  90. }
  91. /**
  92. * Actives the given cron job
  93. *
  94. * ## OPTIONS
  95. *
  96. * <cron_name>: The name of the cron job to check
  97. *
  98. * ## EXAMPLES
  99. *
  100. * wp videopress activate_cron cleanup
  101. */
  102. public function activate_cron( $args ) {
  103. if ( ! isset( $args[0] ) ) {
  104. WP_CLI::error( __( 'You need to provide the name of the cronjob to schedule.', 'jetpack' ) );
  105. }
  106. $scheduler = VideoPress_Scheduler::init();
  107. if ( ! $scheduler->is_cron_valid( $args[0] ) ) {
  108. return WP_CLI::error( sprintf( __( 'There is no cron named %s.', 'jetpack' ), $args[0] ) );
  109. }
  110. $scheduler->activate_cron( $args[0] );
  111. WP_CLI::success( sprintf( __( 'The cron named `%s` was scheduled.', 'jetpack' ), $args[0] ) );
  112. }
  113. /**
  114. * Actives the given cron job
  115. *
  116. * ## OPTIONS
  117. *
  118. * <cron_name>: The name of the cron job to check
  119. *
  120. * ## EXAMPLES
  121. *
  122. * wp videopress deactivate_cron cleanup
  123. */
  124. public function deactivate_cron( $args ) {
  125. if ( ! isset( $args[0] ) ) {
  126. WP_CLI::error( __( 'You need to provide the name of the cronjob to schedule.', 'jetpack' ) );
  127. }
  128. $scheduler = VideoPress_Scheduler::init();
  129. if ( ! $scheduler->is_cron_valid( $args[0] ) ) {
  130. return WP_CLI::error( sprintf( __( 'There is no cron named %s.', 'jetpack' ), $args[0] ) );
  131. }
  132. $scheduler->deactivate_cron( $args[0] );
  133. WP_CLI::success( sprintf( __( 'The cron named `%s` was removed from the schedule.', 'jetpack' ), $args[0] ) );
  134. }
  135. }
  136. WP_CLI::add_command( 'videopress', 'VideoPress_CLI' );
  137. }