Completed
Push — develop ( 765c38...eb44bf )
by David
03:19
created

Wordlift_Batch_Action::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 4
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: david
5
 * Date: 24.09.18
6
 * Time: 16:57
7
 */
8
9
class Wordlift_Batch_Action {
10
11
12
	public static function process( $post_type, $offset, $query, $callback ) {
13
14
		$posts_per_page = 10;
15
16
		$args = array_merge(
17
			self::get_args( $post_type, $query ),
18
			array(
19
				'offset'         => $offset,
20
				'posts_per_page' => $posts_per_page,
21
			)
22
		);
23
24
		$post_ids = get_posts( $args );
25
26
		foreach ( $post_ids as $post_id ) {
27
			call_user_func( $callback, $post_id );
28
		}
29
30
		return array(
31
			'current' => $offset,
32
			'next'    => $offset + $posts_per_page,
33
			'count'   => self::count( $post_type, $query ),
34
		);
35
	}
36
37
	public static function count( $post_type, $query ) {
38
		$args = array_merge(
39
			self::get_args( $post_type, $query ),
40
			array(
41
				'posts_per_page' => - 1,
42
			)
43
		);
44
45
		return count( get_posts( $args ) );
46
	}
47
48
	private static function get_args( $post_type, $query ) {
49
50
		return array_merge( array(
51
			'fields'        => 'ids',
52
			'post_type'     => $post_type,
53
			'post_status'   => array(
54
				'publish',
55
				'future',
56
				'draft',
57
				'pending',
58
				'private',
59
				'auto-draft',
60
				'inherit',
61
			),
62
			'cache_results' => false,
63
		), $query );
64
	}
65
66
}