summaryrefslogtreecommitdiff
blob: e1200da09116fb6255e75c274bb89ec76419b47c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

if ( defined( 'WP_CLI' ) && WP_CLI ) {

	/**
	 * VideoPress command line utilities.
	 */
	class VideoPress_CLI extends WP_CLI_Command {
		/**
		 * Import a VideoPress Video
		 *
		 * ## OPTIONS
		 *
		 * <guid>: Import the video with the specified guid
		 *
		 * ## EXAMPLES
		 *
		 * wp videopress import kUJmAcSf
		 */
		public function import( $args ) {
			$guid          = $args[0];
			$attachment_id = create_local_media_library_for_videopress_guid( $guid );
			if ( $attachment_id && ! is_wp_error( $attachment_id ) ) {
				WP_CLI::success( sprintf( __( 'The video has been imported as Attachment ID %d', 'jetpack' ), $attachment_id ) );
			} else {
				WP_CLI::error( __( 'An error has been encountered.', 'jetpack' ) );
			}
		}

		/**
		 * Manually runs the job to cleanup videos from the media library that failed during the upload process.
		 *
		 * ## EXAMPLES
		 *
		 * wp videopress cleanup_videos
		 */
		public function cleanup_videos() {
			$num_cleaned = videopress_cleanup_media_library();

			WP_CLI::success( sprintf( _n( 'Cleaned up %d video.', 'Cleaned up a total of %d videos.', $num_cleaned, 'jetpack' ), $num_cleaned ) );
		}

		/**
		 * List out all of the crons that can be run.
		 *
		 * ## EXAMPLES
		 *
		 * wp videopress list_crons
		 */
		public function list_crons() {

			$scheduler = VideoPress_Scheduler::init();
			$crons     = $scheduler->get_crons();

			$schedules = wp_get_schedules();

			if ( count( $crons ) === 0 ) {
				WP_CLI::success( __( 'Found no available cron jobs.', 'jetpack' ) );

			} else {
				WP_CLI::success( sprintf( _n( 'Found %d available cron job.', 'Found %d available cron jobs.', count( $crons ), 'jetpack' ), count( $crons ) ) );
			}

			foreach ( $crons as $cron_name => $cron ) {
				$interval  = isset( $schedules[ $cron['interval'] ]['display'] ) ? $schedules[ $cron['interval'] ]['display'] : $cron['interval'];
				$runs_next = $scheduler->check_cron( $cron_name );
				$status    = $runs_next ? sprintf( 'Scheduled - Runs Next at %s GMT', gmdate( 'Y-m-d H:i:s', $runs_next ) ) : 'Not Scheduled';

				WP_CLI::log( 'Name: ' . $cron_name );
				WP_CLI::log( 'Method: ' . $cron['method'] );
				WP_CLI::log( 'Interval: ' . $interval );
				WP_CLI::log( 'Status: ' . $status );
			}
		}

		/**
		 * Checks for the current status of a cron job.
		 *
		 * ## OPTIONS
		 *
		 * <cron_name>: The name of the cron job to check
		 *
		 * ## EXAMPLES
		 *
		 * wp videopress cron_status cleanup
		 */
		public function cron_status( $args ) {

			if ( ! isset( $args[0] ) ) {
				return WP_CLI::error( __( 'You need to provide the name of the cronjob to schedule.', 'jetpack' ) );
			}

			$scheduler = VideoPress_Scheduler::init();

			if ( ! $scheduler->is_cron_valid( $args[0] ) ) {
				return WP_CLI::error( sprintf( __( 'There is no cron named %s.', 'jetpack' ), $args[0] ) );
			}

			$time = $scheduler->check_cron( $args[0] );

			if ( ! $time ) {
				WP_CLI::success( __( 'The cron is not scheduled to run.', 'jetpack' ) );

			} else {
				WP_CLI::success( sprintf( __( 'Cron will run at: %s GMT', 'jetpack' ), gmdate( 'Y-m-d H:i:s', $time ) ) );
			}
		}

		/**
		 * Actives the given cron job
		 *
		 * ## OPTIONS
		 *
		 * <cron_name>: The name of the cron job to check
		 *
		 * ## EXAMPLES
		 *
		 * wp videopress activate_cron cleanup
		 */
		public function activate_cron( $args ) {

			if ( ! isset( $args[0] ) ) {
				WP_CLI::error( __( 'You need to provide the name of the cronjob to schedule.', 'jetpack' ) );
			}

			$scheduler = VideoPress_Scheduler::init();

			if ( ! $scheduler->is_cron_valid( $args[0] ) ) {
				return WP_CLI::error( sprintf( __( 'There is no cron named %s.', 'jetpack' ), $args[0] ) );
			}

			$scheduler->activate_cron( $args[0] );

			WP_CLI::success( sprintf( __( 'The cron named `%s` was scheduled.', 'jetpack' ), $args[0] ) );
		}

		/**
		 * Actives the given cron job
		 *
		 * ## OPTIONS
		 *
		 * <cron_name>: The name of the cron job to check
		 *
		 * ## EXAMPLES
		 *
		 * wp videopress deactivate_cron cleanup
		 */
		public function deactivate_cron( $args ) {

			if ( ! isset( $args[0] ) ) {
				WP_CLI::error( __( 'You need to provide the name of the cronjob to schedule.', 'jetpack' ) );
			}

			$scheduler = VideoPress_Scheduler::init();

			if ( ! $scheduler->is_cron_valid( $args[0] ) ) {
				return WP_CLI::error( sprintf( __( 'There is no cron named %s.', 'jetpack' ), $args[0] ) );
			}

			$scheduler->deactivate_cron( $args[0] );

			WP_CLI::success( sprintf( __( 'The cron named `%s` was removed from the schedule.', 'jetpack' ), $args[0] ) );
		}
	}

	WP_CLI::add_command( 'videopress', 'VideoPress_CLI' );
}