Completed
Push — master ( 888ca4...f03a0a )
by Steve
29:19 queued 20:32
created

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

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 bool
30
	 */
31
	function add($id, $message) {
32
		if (!$id || !$message) {
33
			return false;
34
		}
35
		
36
		if (elgg_admin_notice_exists($id)) {
37
			return false;
38
		}
39
40
		// need to handle when no one is logged in
41
		$old_ia = elgg_set_ignore_access(true);
42
43
		$admin_notice = new \ElggObject();
44
		$admin_notice->subtype = 'admin_notice';
45
		// admins can see ACCESS_PRIVATE but no one else can.
46
		$admin_notice->access_id = ACCESS_PRIVATE;
47
		$admin_notice->admin_notice_id = $id;
48
		$admin_notice->description = $message;
49
50
		$result = $admin_notice->save();
51
52
		elgg_set_ignore_access($old_ia);
53
54
		return (bool) $result;
55
	}
56
	
57
	/**
58
	 * Remove an admin notice by ID.
59
	 *
60
	 * eg In actions/twitter_service/save_settings:
61
	 * 	if (is_valid_twitter_api_key()) {
62
	 * 		delete_admin_notice('twitter_services_no_api');
63
	 * 	}
64
	 *
65
	 * @param string $id The unique ID assigned in add_admin_notice()
66
	 *
67
	 * @return bool
68
	 */
69
	function delete($id) {
70
		if (!$id) {
71
			return false;
72
		}
73
		
74
		$result = true;
75
		
76
		$notices = elgg_get_entities_from_metadata([
77
			'type' => 'object',
78
			'subtype' => 'admin_notice',
79
			'metadata_name' => 'admin_notice_id',
80
			'metadata_value' => $id,
81
			'limit' => false,
82
			'batch' => true,
83
			'batch_inc_offset' => false,
84
		]);
85
86
		// in case a bad plugin adds many, let it remove them all at once.
87
		foreach ($notices as $notice) {
88
			$result = ($result && $notice->delete());
89
		}
90
		return $result;
91
	}
92
	
93
	/**
94
	 * Get admin notices. An admin must be logged in since the notices are private.
95
	 *
96
	 * @param array $options Query options
97
	 *
98
	 * @return \ElggObject[] Admin notices
99
	 */
100
	function find(array $options = []) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
101
		$options = array_merge($options, [
102
			'type' => 'object',
103
			'subtype' => 'admin_notice',
104
		]);
105
106
		return _elgg_services()->metadataTable->getEntities($options);
107
	}
108
	
109
	/**
110
	 * Check if an admin notice is currently active.
111
	 *
112
	 * @param string $id The unique ID used to register the notice.
113
	 *
114
	 * @return bool
115
	 * @since 1.8.0
116
	 */
117
	function exists($id) {
118
		$old_ia = elgg_set_ignore_access(true);
119
		$notice = elgg_get_entities_from_metadata([
120
			'type' => 'object',
121
			'subtype' => 'admin_notice',
122
			'metadata_name_value_pair' => ['name' => 'admin_notice_id', 'value' => $id],
123
			'count' => true,
124
		]);
125
		elgg_set_ignore_access($old_ia);
126
	
127
		return ($notice) ? true : false;
128
	}
129
}
130