PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/videopress/class.videopress-cli.php

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