Completed
Push — fix/4.4.1-typos ( 3db521 )
by
unknown
25s
created

VideoPress_Media_Library   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A init() 0 7 2
A ajax_query_attachments_args() 0 6 2
A media_list_table_query() 0 7 4
A add_status_check_to_meta_query() 0 23 2
1
<?php
2
3
class VideoPress_Media_Library {
4
5
	/**
6
	 * @var VideoPress_Media_Library
7
	 **/
8
	private static $instance = null;
9
10
	/**
11
	 * Private VideoPress_Media_Library constructor.
12
	 *
13
	 * Use the VideoPress_Media_Library::init() method to get an instance.
14
	 */
15
	private function __construct() {
16
		add_filter( 'ajax_query_attachments_args', array( $this, 'ajax_query_attachments_args' ), 10, 1 );
17
		add_action( 'pre_get_posts', array( $this, 'media_list_table_query' ) );
18
	}
19
20
	/**
21
	 * Initialize the VideoPress_Media_Library and get back a singleton instance.
22
	 *
23
	 * @return VideoPress_Media_Library
24
	 */
25
	public static function init() {
26
		if ( is_null( self::$instance ) ) {
27
			self::$instance = new VideoPress_Media_Library;
28
		}
29
30
		return self::$instance;
31
	}
32
33
	/**
34
	 * Media Grid:
35
	 * Filter out any videopress video posters that we've downloaded,
36
	 * so that they don't seem to display twice.
37
	 *
38
	 * @param array $args
39
	 *
40
	 * @return array
41
	 */
42
	public function ajax_query_attachments_args( $args ) {
43
44
		$args['meta_query'] = $this->add_status_check_to_meta_query( isset( $args['meta_query'] ) ? $args['meta_query'] : array() );
45
46
		return $args;
47
	}
48
49
	/**
50
	 * Media List:
51
	 * Do the same as ^^ but for the list view.
52
	 *
53
	 * @param WP_Query $query
54
	 *
55
	 * @return array
56
	 */
57
	public function media_list_table_query( $query ) {
58
		if ( is_admin() && $query->is_main_query() && ( 'upload' === get_current_screen()->id ) ) {
59
			$meta_query = $this->add_status_check_to_meta_query( $query->get( 'meta_query' ) );
60
61
			$query->set( 'meta_query', $meta_query );
62
		}
63
	}
64
65
	/**
66
	 * Add the a videopress_status check to the meta query and if it has a `videopress_status` only include those with
67
	 * a status of 'completed' or 'processing'.
68
	 *
69
	 * @param array $meta_query
70
	 *
71
	 * @return array
72
	 */
73
	protected function add_status_check_to_meta_query( $meta_query ) {
74
75
		if ( ! is_array( $meta_query ) ) {
76
			$meta_query = array();
77
		}
78
79
		$meta_query[] = array(
80
			array(
81
				'relation' => 'OR',
82
				array(
83
					'key'     => 'videopress_status',
84
					'value'   => array( 'completed', 'processing' ),
85
					'compare' => 'IN',
86
				),
87
				array(
88
					'key'     => 'videopress_status',
89
					'compare' => 'NOT EXISTS',
90
				),
91
			),
92
		);
93
94
		return $meta_query;
95
	}
96
}
97
98
// Let's start this thing up.
99
VideoPress_Media_Library::init();