Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/Database/AdminNotices.php (1 issue)

1
<?php
2
namespace Elgg\Database;
3
4
/**
5
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
6
 *
7
 * Controls all admin notices in the system.
8
 *
9
 * @access private
10
 *
11
 * @package    Elgg.Core
12
 * @subpackage Database
13
 * @since      1.10.0
14
 */
15
class AdminNotices {
16
	/**
17
	 * Write a persistent message to the admin view.
18
	 * Useful to alert the admin to take a certain action.
19
	 * The id is a unique ID that can be cleared once the admin
20
	 * completes the action.
21
	 *
22
	 * eg: add_admin_notice('twitter_services_no_api',
23
	 * 	'Before your users can use Twitter services on this site, you must set up
24
	 * 	the Twitter API key in the <a href="link">Twitter Services Settings</a>');
25
	 *
26
	 * @param string $id      A unique ID that your plugin can remember
27
	 * @param string $message Body of the message
28
	 *
29
	 * @return \ElggObject|bool
30
	 */
31 2
	public function add($id, $message) {
32 2
		if (!$id || !$message) {
33
			return false;
34
		}
35
		
36 2
		if (elgg_admin_notice_exists($id)) {
37
			return false;
38
		}
39
40
		// need to handle when no one is logged in
41 2
		$old_ia = elgg_set_ignore_access(true);
42
43 2
		$admin_notice = new \ElggObject();
44 2
		$admin_notice->subtype = 'admin_notice';
45
		// admins can see ACCESS_PRIVATE but no one else can.
46 2
		$admin_notice->access_id = ACCESS_PRIVATE;
47 2
		$admin_notice->admin_notice_id = $id;
48 2
		$admin_notice->description = $message;
49
50 2
		$result = $admin_notice->save();
51
52 2
		elgg_set_ignore_access($old_ia);
53
54 2
		if (!$result) {
55
			return false;
56
		}
57
58 2
		return $admin_notice;
59
	}
60
	
61
	/**
62
	 * Remove an admin notice by ID.
63
	 *
64
	 * @param string $id The unique ID assigned in add_admin_notice()
65
	 *
66
	 * @return bool
67
	 */
68 1
	public function delete($id = '') {
69 1
		$result = true;
70
		
71 1
		$notices = $this->find([
72 1
			'metadata_name' => 'admin_notice_id',
73 1
			'metadata_value' => $id,
74
			'limit' => false,
75
			'batch' => true,
76
			'batch_inc_offset' => false,
77
		]);
78
79 1
		$ia = elgg_set_ignore_access(true);
80
81
		// in case a bad plugin adds many, let it remove them all at once.
82 1
		foreach ($notices as $notice) {
83
			$result = ($result && $notice->delete());
84
		}
85
86 1
		elgg_set_ignore_access($ia);
87
88 1
		return $result;
89
	}
90
	
91
	/**
92
	 * Get admin notices. An admin must be logged in since the notices are private.
93
	 *
94
	 * @param array $options Query options
95
	 *
96
	 * @return \ElggObject[] Admin notices
97
	 */
98 2
	public function find(array $options = []) {
99 2
		$options = array_merge($options, [
100 2
			'type' => 'object',
101
			'subtype' => 'admin_notice',
102
		]);
103
104 2
		return Entities::find($options);
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression return Elgg\Database\Entities::find($options) returns the type integer|false which is incompatible with the documented return type ElggObject[].
Loading history...
105
	}
106
	
107
	/**
108
	 * Check if an admin notice is currently active.
109
	 *
110
	 * @param string $id The unique ID used to register the notice.
111
	 *
112
	 * @return bool
113
	 * @since 1.8.0
114
	 */
115 2
	public function exists($id) {
116 2
		$old_ia = elgg_set_ignore_access(true);
117 2
		$notice = elgg_get_entities([
118 2
			'type' => 'object',
119 2
			'subtype' => 'admin_notice',
120 2
			'metadata_name_value_pair' => ['name' => 'admin_notice_id', 'value' => $id],
121
			'count' => true,
122
		]);
123 2
		elgg_set_ignore_access($old_ia);
124
	
125 2
		return ($notice) ? true : false;
126
	}
127
}
128