Completed
Branch FET/10339/exit-modal-for-ee-de... (81712e)
by
unknown
29:29 queued 13:19
created

EE_Messages_Base   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 304
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 304
rs 10
c 0
b 0
f 0
wmc 16
lcom 3
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
_set_admin_pages() 0 1 ?
_set_valid_shortcodes() 0 1 ?
A __construct() 0 5 1
_set_admin_settings_fields() 0 1 ?
A _set_existing_admin_settings() 0 20 4
A get_existing_admin_settings() 0 9 4
A get_valid_shortcodes() 0 10 1
A get_admin_settings_fields() 0 3 1
B _get_admin_page_content() 0 22 4
A excludedFieldsForMessenger() 0 10 1
1
<?php
2
if (!defined('EVENT_ESPRESSO_VERSION') )
3
	exit('NO direct script access allowed');
4
5
/**
6
 * EE_Messages_Base class
7
 *
8
 * Abstract class for setting up messengers and message types parents.
9
 *
10
 * @package			Event Espresso
11
 * @subpackage		includes/core/messages/EE_Messages_Base.core.php
12
 * @author			Darren Ethier
13
 *
14
 * ------------------------------------------------------------------------
15
 */
16
abstract class EE_Messages_Base extends EE_Base {
17
18
	/** DETAILS PROPERTIES **/
19
	/**
20
	 * The following are used to hold details on the type for reference (i.e. on admin screens)
21
	 * and also used by the EE_message_type object to figure out where to get template data.
22
	 */
23
	public $name;
24
	public $description;
25
	protected $_messages_item_type; //messenger OR message_type?
26
27
28
	/**
29
	 * This is an array describing the ui facing labels
30
	 * that will be used whenever the messenger is referenced in the ui
31
	 *
32
	 * array(
33
	 * 	'singular' => __('something'),
34
	 * 	'plural' => __('somethings')
35
	 * )
36
	 *
37
	 * @var array
38
	 */
39
	public $label;
40
41
42
43
44
	/**
45
	 * This property when set will hold the slugs of all EE admin pages that we will need to retrieve fields for
46
	 * (and used to determine which callback method to call from the child class)
47
	 *
48
	 * structure should be
49
	 * array(
50
	 * 'page_action' => true
51
	 * )
52
	 *
53
*@var array
54
	 */
55
	public $admin_registered_pages = array();
56
57
58
59
60
61
62
63
64
	/**
65
	 * this property holds any specific fields for holding any settings related to a messenger (if any needed)
66
	 * @var array
67
	 */
68
	protected $_admin_settings_fields = array();
69
70
71
72
73
74
	/**
75
	 * this property will hold any existing settings that may have been set in the admin.
76
	 * @var array
77
	 */
78
	protected $_existing_admin_settings = array();
79
80
81
82
83
84
	/**
85
	 * this property will hold an array of valid shortcodes for this message type and messengers.
86
	 * #For Message Types:
87
	 * This is an array of strings that correspond to defined EE_Shortcode libraries and per context.
88
	 * For example:
89
	 * array( 'admin' => array('transaction', 'event', 'attendee') )
90
	 * corresponds to 'EE_Transaction_Shortcodes.lib.php, EE_Event_Shortcodes.lib.php, EE_Attendee_Shortcodes.lib.php'
91
	 * for the admin context;
92
	 *
93
	 *
94
	 * #For Messengers:
95
	 * For example:
96
	 * array('subject' => array('transaction', 'event', 'attendee'))
97
	 * corresponds to 'EE_Transaction_Shortcodes.lib.php, EE_Event_Shortcodes.lib.php, EE_Attendee_Shortcodes.lib.php'
98
	 * for the 'subject' field;
99
	 * NOTE:  by default, with messengers, if the valid shortcodes for a field is left blank,
100
	 * that field will inherit whatever are set as valid shortcodes by message_type.
101
	 * This is so messenger can set specific valid codes for fields and leave other
102
	 * valid shortcodes up to the message type matched with the messenger.
103
	 *
104
	 * @access protected
105
	 * @var array
106
	 */
107
	protected $_valid_shortcodes = array();
108
109
110
111
112
113
	public function __construct() {
114
		$this->_set_admin_settings_fields();
115
		$this->_set_valid_shortcodes();
116
		$this->_set_admin_pages();
117
	}
118
119
120
121
122
123
	/**
124
	 * sets the _admin_settings_fields property which needs to be defined by child classes.
125
	 * You will want to set the _admin_settings_fields properties as a multi-dimensional array with the following format
126
	 * array(
127
	 * 		{field_name - also used for setting index} => array(
128
	 * 			'field_type' => {type of field: 'text', 'textarea', 'checkbox'},
129
	 * 			'value_type' => {type of value: 'string', 'int', 'array', 'bool'},
130
	 * 			'required' => {bool, required or not},
131
	 * 			'validation' => {bool, true if we want validation, false if not},
132
	 * 			'format' => {%d, or %s},
133
	 * 			'label' => {label for the field, make sure it's localized},
134
	 * 			'default' => {default value for the setting}
135
	 * 		),
136
	 * );
137
	 *
138
	 * @abstract
139
	 * @access protected
140
	 * @return void
141
	 */
142
	abstract protected function _set_admin_settings_fields();
143
144
145
146
147
148
	/**
149
	 * sets any properties on whether a message type or messenger interface shows up on a ee administration page.
150
	 * Child classes have to define this method but don't necessarily have to set the flags
151
	 * as they will be set to false by default.
152
	 *
153
	 * Child classes use this method to set the `_admin_registered_page` property.
154
	 * That property is to indicate what EE admin pages we have a corresponding callback for in the child class
155
	 * so Message Type/messenger fields/content is included on that admin page.
156
	 *
157
	 * @abstract
158
	 * @access protected
159
	 * @return void
160
	 */
161
	abstract protected function _set_admin_pages();
162
163
164
165
166
167
	/**
168
	 * Child classes must declare the $_valid_shortcodes property using this method.
169
	 * See comments for $_valid_shortcodes property for details on what it is used for.
170
	 *
171
	 * @access protected
172
	 * @return void
173
	 */
174
	abstract protected function _set_valid_shortcodes();
175
176
177
178
	/**
179
	 * sets the _existing_admin_settings property can be overridden by child classes.
180
	 * We do this so we only do database calls if needed.
181
	 *
182
	 * @access protected
183
	 * @param string $messenger
184
	 */
185
	protected function _set_existing_admin_settings( $messenger = '' ) {
186
		/** @var EE_Message_Resource_Manager $Message_Resource_Manager */
187
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
188
		$active_messengers = $Message_Resource_Manager->get_active_messengers_option();
189
		$settings_to_use = $active_messengers;
190
191
		/**
192
		 * This determines what will be used for the getting the settings.
193
		 */
194
		if (
195
			! empty( $messenger )
196
			&& $Message_Resource_Manager->is_message_type_active_for_messenger( $messenger, $this->name )
197
		) {
198
			$settings_to_use = $active_messengers[ $messenger ]['settings'][ $messenger . '-message_types' ];
199
		}
200
201
		$this->_existing_admin_settings = isset( $settings_to_use[ $this->name ]['settings'] )
202
			? $settings_to_use[ $this->name ]['settings']
203
			: null;
204
	}
205
206
207
208
209
210
211
	/**
212
	 * get_existing_admin_settings
213
	 * (if needed) sets and returns the _existing_admin_settings property.
214
	 *
215
	 * @access public
216
	 * @param string $messenger
217
	 * @return array          settings
218
	 */
219
	public function get_existing_admin_settings( $messenger = '' ) {
220
		// if admin_settings property empty lets try setting it.
221
		if ( method_exists($this, '_set_existing_admin_settings') && empty( $this->_existing_admin_settings ) ) {
222
			$this->_set_existing_admin_settings( $messenger );
223
		}
224
		return property_exists( $this, '_existing_admin_settings' )
225
			? $this->_existing_admin_settings
226
			: null;
227
	}
228
229
230
231
232
233
234
	/**
235
	 * This returns the array of valid shortcodes for a message type or messenger as set by the child in the $_valid_shortcode property.
236
	 * @return array   an array of valid shortcodes.
237
	 */
238
	public function get_valid_shortcodes() {
239
		$valid_shortcodes = apply_filters(
240
			'FHEE__' . get_class( $this ) . '__get_valid_shortcodes',
241
			$this->_valid_shortcodes,
242
			$this
243
		);
244
		//The below filter applies to ALL messengers and message types so use with care!
245
		$valid_shortcodes = apply_filters( 'FHEE__EE_Messages_Base__get_valid_shortcodes', $valid_shortcodes, $this );
246
		return $valid_shortcodes;
247
	}
248
249
250
251
252
	/**
253
	 * getter that returns the protected admin_settings_fields property
254
	 *
255
	 * @access public
256
	 * @return array admin settings fields
257
	 */
258
	public function get_admin_settings_fields() {
259
		return $this->_admin_settings_fields;
260
	}
261
262
263
264
265
266
	/**
267
	 * this public method accepts a page slug (for an EE_admin page)
268
	 * and will return the response from the child class callback function
269
	 * if that page is registered via the `_admin_registered_page` property set by the child class.
270
	 *
271
	 * @param string $page the slug of the EE admin page
272
	 * @param array $actives an array of active message type (or messenger) objects.
273
	 * @param string $action the page action (to allow for more specific handling - i.e. edit vs. add pages)
274
	 * @param array $extra This is just an extra argument that can be used
275
	 *                     to pass additional data for setting up page content.
276
	 * @access protected
277
	 * @return string $content for page.
278
	 */
279
	protected function _get_admin_page_content( $page, $action, $extra, $actives ) {
280
		//we can also further refine the context by action (if present).
281
		if ( !empty($action) ) {
282
			$page = $page . '_' . $action;
283
		}
284
285
		if ( !isset( $this->admin_registered_pages[$page]) ){
286
			// todo: a place to throw an exception?
287
			// We need to indicate there is no registered page so this function is not being called correctly.
288
			return false;
289
		}
290
		//k made it here so let's call the method
291
		$content = call_user_func_array(
292
			array( $this, '_get_admin_content_' . $page ),
293
			array( $actives, $extra )
294
		);
295
		if ( $content === false ) {
296
			// todo this needs to be an exception once we've got exceptions in place.
297
			return false;
298
		}
299
		return $content;
300
	}
301
302
303
    /**
304
     * Allows a message type to specifically exclude template fields for the provided messenger.
305
     * Filtered so this can be programmatically altered as well.
306
     * @param string $messenger_name name of messenger
307
     * @return array
308
     */
309
	public function excludedFieldsForMessenger($messenger_name)
310
    {
311
        return apply_filters(
312
            'FHEE__EE_Messages_Base__excludedFieldForMessenger',
313
            array(),
314
            $messenger_name,
315
            $this->name,
316
            $this
317
        );
318
    }
319
}
320