Completed
Pull Request — dev/5.6 (#145)
by Maria Daniel Deepak
03:03 queued 01:36
created

Admin_Capability_Giver::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
4
5
/**
6
 * Gives capability to admin.
7
 * By default admins should be able to manage Bulk WP.
8
 *
9
 * @since 5.6.0
10
 */
11
class Admin_Capability_Giver {
12
13
	public function load() {
14
		add_filter( 'user_has_cap', array( $this, 'add_cap_to_admin_cap_list' ), 10, 4 );
15
	}
16
17
	/**
18
	 * Add `manage_bulk_wp` capability to admin's list of capabilities during `user_has_cap` check.
19
	 *
20
	 * In new installs this capability will be added to admins on plugin install.
21
	 * But in old installs the admins will not have this capability and that's why we need to add it using the filter.
22
	 *
23
	 * @param array    $allcaps An array of all the user's capabilities.
24
	 * @param array    $caps    Actual capabilities for meta capability.
25
	 * @param array    $args    Optional parameters passed to has_cap(), typically object ID.
26
	 * @param \WP_User $user    The user object.
27
	 *
28
	 * @return array Modified list of user's capabilities.
29
	 */
30
	public function add_cap_to_admin_cap_list( $allcaps, $caps, $args, $user ) {
31
		$bd = BULK_DELETE();
32
33
		if ( ! in_array( 'administrator', $user->roles ) ) {
34
			return $allcaps;
35
		}
36
37
		if ( array_key_exists( $bd::CAPABILITY, $allcaps ) ) {
38
			return $allcaps;
39
		}
40
41
		$allcaps[ $bd::CAPABILITY ] = true;
42
43
		return $allcaps;
44
	}
45
46
	/**
47
	 * Add `manage_bulk_wp` capability to administrator role.
48
	 * This will be called on install.
49
	 */
50
	public function add_cap_to_admin() {
51
		$bd    = BULK_DELETE();
52
		$admin = get_role( 'administrator' );
53
54
		if ( is_null( $admin ) ) {
55
			return;
56
		}
57
58
		$admin->add_cap( $bd::CAPABILITY );
59
	}
60
}
61