class-wp-revisions-control-bulk-actions.php 2.95 KB
Newer Older
Erick Hitter's avatar
Erick Hitter committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
/**
 * Bulk actions.
 *
 * @package WP_Revisions_Control
 */

/**
 * Class WP_Revisions_Control_Bulk_Actions.
 */
class WP_Revisions_Control_Bulk_Actions {
	/**
	 * Supported post types.
	 *
	 * @var array
	 */
	protected $post_types;

19
20
21
22
23
24
25
	/**
	 * Base for bulk action names.
	 *
	 * @var string
	 */
	protected $action_base = 'wp_rev_ctl_bulk_';

Erick Hitter's avatar
Erick Hitter committed
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
	/**
	 * Custom bulk actions.
	 *
	 * @var array
	 */
	protected $actions;

	/**
	 * Constructor.
	 *
	 * @param array $post_types Supported post types.
	 */
	public function __construct( $post_types ) {
		if ( empty( $post_types ) || ! is_array( $post_types ) ) {
			return;
		}

		$this->post_types = $post_types;
		$this->register_actions();

		add_action( 'load-edit.php', array( $this, 'setup' ) );
	}

	/**
	 * Register custom actions.
	 */
	protected function register_actions() {
53
		$actions = array();
Erick Hitter's avatar
Erick Hitter committed
54

55
56
		$actions[ $this->action_base . 'purge_excess' ] = __( 'Purge excess revisions', 'wp_revisions_control' );
		$actions[ $this->action_base . 'purge_all' ]    = __( 'Purge ALL revisions', 'wp_revisions_control' );
Erick Hitter's avatar
Erick Hitter committed
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

		$this->actions = $actions;
	}

	/**
	 * Register various hooks.
	 */
	public function setup() {
		$screen = get_current_screen();

		if ( null === $screen ) {
			return;
		}

		$post_types = array_keys( $this->post_types );

		if ( ! in_array( $screen->post_type, $post_types, true ) ) {
			return;
		}

		if ( 'edit' !== $screen->base ) {
			return;
		}

		add_filter( 'bulk_actions-' . $screen->id, array( $this, 'add_actions' ) );
		add_filter( 'handle_bulk_actions-' . $screen->id, array( $this, 'handle_action' ), 10, 3 );
83
		// TODO: messages.
Erick Hitter's avatar
Erick Hitter committed
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
	}

	/**
	 * Add our actions.
	 *
	 * @param string[] $actions Array of available actions.
	 * @return array
	 */
	public function add_actions( $actions ) {
		return array_merge( $actions, $this->actions );
	}

	/**
	 * Handle our bulk actions.
	 *
	 * @param string $redirect_to Redirect URL.
	 * @param string $action      Bulk action being taken.
	 * @param array  $ids         Object IDs to manipulate.
	 * @return string
	 */
	public function handle_action( $redirect_to, $action, $ids ) {
		if ( ! array_key_exists( $action, $this->actions ) ) {
			return $redirect_to;
		}

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
		$action = str_replace( $this->action_base, '', $action );

		switch ( $action ) {
			case 'purge_all':
				$this->purge_all( $ids );
				break;

			case 'purge_excess':
				$this->purge_excess( $ids );
				break;

			default:
				break;
		}

Erick Hitter's avatar
Erick Hitter committed
124
125
126
		// TODO: implement and add a query string to trigger a message.
		return $redirect_to;
	}
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

	/**
	 * Remove all revisions from the given IDs.
	 *
	 * @param array $ids Object IDs.
	 */
	protected function purge_all( $ids ) {
		foreach ( $ids as $id ) {
			WP_Revisions_Control::get_instance()->do_purge_all( $id );
		}
	}

	/**
	 * Remove excess revisions from the given IDs.
	 *
	 * @param array $ids Object IDs.
	 */
	protected function purge_excess( $ids ) {
		foreach ( $ids as $id ) {
			WP_Revisions_Control::get_instance()->do_purge_excess( $id );
		}
	}
Erick Hitter's avatar
Erick Hitter committed
149
}