@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 2 | - exit('NO direct script access allowed'); |
|
| 2 | + exit('NO direct script access allowed'); |
|
| 3 | 3 | } |
| 4 | 4 | |
| 5 | 5 | /** |
@@ -17,2222 +17,2222 @@ discard block |
||
| 17 | 17 | class Messages_Admin_Page extends EE_Admin_Page |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @type EE_Message_Resource_Manager $_message_resource_manager |
|
| 22 | - */ |
|
| 23 | - protected $_message_resource_manager; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @type string $_active_message_type_name |
|
| 27 | - */ |
|
| 28 | - protected $_active_message_type_name = ''; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @type EE_messenger $_active_messenger |
|
| 32 | - */ |
|
| 33 | - protected $_active_messenger; |
|
| 34 | - protected $_activate_state; |
|
| 35 | - protected $_activate_meta_box_type; |
|
| 36 | - protected $_current_message_meta_box; |
|
| 37 | - protected $_current_message_meta_box_object; |
|
| 38 | - protected $_context_switcher; |
|
| 39 | - protected $_shortcodes = array(); |
|
| 40 | - protected $_active_messengers = array(); |
|
| 41 | - protected $_active_message_types = array(); |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var EE_Message_Template_Group $_message_template_group |
|
| 45 | - */ |
|
| 46 | - protected $_message_template_group; |
|
| 47 | - protected $_m_mt_settings = array(); |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * This is set via the _set_message_template_group method and holds whatever the template pack for the group is. |
|
| 52 | - * IF there is no group then it gets automatically set to the Default template pack. |
|
| 53 | - * |
|
| 54 | - * @since 4.5.0 |
|
| 55 | - * |
|
| 56 | - * @var EE_Messages_Template_Pack |
|
| 57 | - */ |
|
| 58 | - protected $_template_pack; |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * This is set via the _set_message_template_group method and holds whatever the template pack variation for the |
|
| 63 | - * group is. If there is no group then it automatically gets set to default. |
|
| 64 | - * |
|
| 65 | - * @since 4.5.0 |
|
| 66 | - * |
|
| 67 | - * @var string |
|
| 68 | - */ |
|
| 69 | - protected $_variation; |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param bool $routing |
|
| 74 | - */ |
|
| 75 | - public function __construct($routing = true) |
|
| 76 | - { |
|
| 77 | - //make sure messages autoloader is running |
|
| 78 | - EED_Messages::set_autoloaders(); |
|
| 79 | - parent::__construct($routing); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - protected function _init_page_props() |
|
| 84 | - { |
|
| 85 | - $this->page_slug = EE_MSG_PG_SLUG; |
|
| 86 | - $this->page_label = __('Messages Settings', 'event_espresso'); |
|
| 87 | - $this->_admin_base_url = EE_MSG_ADMIN_URL; |
|
| 88 | - $this->_admin_base_path = EE_MSG_ADMIN; |
|
| 89 | - |
|
| 90 | - $this->_activate_state = isset($this->_req_data['activate_state']) ? (array)$this->_req_data['activate_state'] : array(); |
|
| 91 | - |
|
| 92 | - $this->_active_messenger = isset($this->_req_data['messenger']) ? $this->_req_data['messenger'] : null; |
|
| 93 | - $this->_load_message_resource_manager(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * loads messenger objects into the $_active_messengers property (so we can access the needed methods) |
|
| 99 | - * |
|
| 100 | - * |
|
| 101 | - * @throws EE_Error |
|
| 102 | - */ |
|
| 103 | - protected function _load_message_resource_manager() |
|
| 104 | - { |
|
| 105 | - $this->_message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @deprecated 4.9.9.rc.014 |
|
| 111 | - * @return array |
|
| 112 | - */ |
|
| 113 | - public function get_messengers_for_list_table() |
|
| 114 | - { |
|
| 115 | - EE_Error::doing_it_wrong( |
|
| 116 | - __METHOD__, |
|
| 117 | - __('This method is no longer in use. There is no replacement for it. The method was used to generate a set of |
|
| 20 | + /** |
|
| 21 | + * @type EE_Message_Resource_Manager $_message_resource_manager |
|
| 22 | + */ |
|
| 23 | + protected $_message_resource_manager; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @type string $_active_message_type_name |
|
| 27 | + */ |
|
| 28 | + protected $_active_message_type_name = ''; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @type EE_messenger $_active_messenger |
|
| 32 | + */ |
|
| 33 | + protected $_active_messenger; |
|
| 34 | + protected $_activate_state; |
|
| 35 | + protected $_activate_meta_box_type; |
|
| 36 | + protected $_current_message_meta_box; |
|
| 37 | + protected $_current_message_meta_box_object; |
|
| 38 | + protected $_context_switcher; |
|
| 39 | + protected $_shortcodes = array(); |
|
| 40 | + protected $_active_messengers = array(); |
|
| 41 | + protected $_active_message_types = array(); |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var EE_Message_Template_Group $_message_template_group |
|
| 45 | + */ |
|
| 46 | + protected $_message_template_group; |
|
| 47 | + protected $_m_mt_settings = array(); |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * This is set via the _set_message_template_group method and holds whatever the template pack for the group is. |
|
| 52 | + * IF there is no group then it gets automatically set to the Default template pack. |
|
| 53 | + * |
|
| 54 | + * @since 4.5.0 |
|
| 55 | + * |
|
| 56 | + * @var EE_Messages_Template_Pack |
|
| 57 | + */ |
|
| 58 | + protected $_template_pack; |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * This is set via the _set_message_template_group method and holds whatever the template pack variation for the |
|
| 63 | + * group is. If there is no group then it automatically gets set to default. |
|
| 64 | + * |
|
| 65 | + * @since 4.5.0 |
|
| 66 | + * |
|
| 67 | + * @var string |
|
| 68 | + */ |
|
| 69 | + protected $_variation; |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param bool $routing |
|
| 74 | + */ |
|
| 75 | + public function __construct($routing = true) |
|
| 76 | + { |
|
| 77 | + //make sure messages autoloader is running |
|
| 78 | + EED_Messages::set_autoloaders(); |
|
| 79 | + parent::__construct($routing); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + protected function _init_page_props() |
|
| 84 | + { |
|
| 85 | + $this->page_slug = EE_MSG_PG_SLUG; |
|
| 86 | + $this->page_label = __('Messages Settings', 'event_espresso'); |
|
| 87 | + $this->_admin_base_url = EE_MSG_ADMIN_URL; |
|
| 88 | + $this->_admin_base_path = EE_MSG_ADMIN; |
|
| 89 | + |
|
| 90 | + $this->_activate_state = isset($this->_req_data['activate_state']) ? (array)$this->_req_data['activate_state'] : array(); |
|
| 91 | + |
|
| 92 | + $this->_active_messenger = isset($this->_req_data['messenger']) ? $this->_req_data['messenger'] : null; |
|
| 93 | + $this->_load_message_resource_manager(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * loads messenger objects into the $_active_messengers property (so we can access the needed methods) |
|
| 99 | + * |
|
| 100 | + * |
|
| 101 | + * @throws EE_Error |
|
| 102 | + */ |
|
| 103 | + protected function _load_message_resource_manager() |
|
| 104 | + { |
|
| 105 | + $this->_message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @deprecated 4.9.9.rc.014 |
|
| 111 | + * @return array |
|
| 112 | + */ |
|
| 113 | + public function get_messengers_for_list_table() |
|
| 114 | + { |
|
| 115 | + EE_Error::doing_it_wrong( |
|
| 116 | + __METHOD__, |
|
| 117 | + __('This method is no longer in use. There is no replacement for it. The method was used to generate a set of |
|
| 118 | 118 | values for use in creating a messenger filter dropdown which is now generated differently via |
| 119 | 119 | Messages_Admin_Page::get_messengers_select_input', 'event_espresso'), |
| 120 | - '4.9.9.rc.014' |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - $m_values = array(); |
|
| 124 | - $active_messengers = EEM_Message::instance()->get_all(array('group_by' => 'MSG_messenger')); |
|
| 125 | - //setup messengers for selects |
|
| 126 | - $i = 1; |
|
| 127 | - foreach ($active_messengers as $active_messenger) { |
|
| 128 | - if ($active_messenger instanceof EE_Message) { |
|
| 129 | - $m_values[$i]['id'] = $active_messenger->messenger(); |
|
| 130 | - $m_values[$i]['text'] = ucwords($active_messenger->messenger_label()); |
|
| 131 | - $i++; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - return $m_values; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @deprecated 4.9.9.rc.014 |
|
| 141 | - * @return array |
|
| 142 | - */ |
|
| 143 | - public function get_message_types_for_list_table() |
|
| 144 | - { |
|
| 145 | - EE_Error::doing_it_wrong( |
|
| 146 | - __METHOD__, |
|
| 147 | - __('This method is no longer in use. There is no replacement for it. The method was used to generate a set of |
|
| 120 | + '4.9.9.rc.014' |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + $m_values = array(); |
|
| 124 | + $active_messengers = EEM_Message::instance()->get_all(array('group_by' => 'MSG_messenger')); |
|
| 125 | + //setup messengers for selects |
|
| 126 | + $i = 1; |
|
| 127 | + foreach ($active_messengers as $active_messenger) { |
|
| 128 | + if ($active_messenger instanceof EE_Message) { |
|
| 129 | + $m_values[$i]['id'] = $active_messenger->messenger(); |
|
| 130 | + $m_values[$i]['text'] = ucwords($active_messenger->messenger_label()); |
|
| 131 | + $i++; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + return $m_values; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @deprecated 4.9.9.rc.014 |
|
| 141 | + * @return array |
|
| 142 | + */ |
|
| 143 | + public function get_message_types_for_list_table() |
|
| 144 | + { |
|
| 145 | + EE_Error::doing_it_wrong( |
|
| 146 | + __METHOD__, |
|
| 147 | + __('This method is no longer in use. There is no replacement for it. The method was used to generate a set of |
|
| 148 | 148 | values for use in creating a message type filter dropdown which is now generated differently via |
| 149 | 149 | Messages_Admin_Page::get_message_types_select_input', 'event_espresso'), |
| 150 | - '4.9.9.rc.014' |
|
| 151 | - ); |
|
| 152 | - |
|
| 153 | - $mt_values = array(); |
|
| 154 | - $active_messages = EEM_Message::instance()->get_all(array('group_by' => 'MSG_message_type')); |
|
| 155 | - $i = 1; |
|
| 156 | - foreach ($active_messages as $active_message) { |
|
| 157 | - if ($active_message instanceof EE_Message) { |
|
| 158 | - $mt_values[$i]['id'] = $active_message->message_type(); |
|
| 159 | - $mt_values[$i]['text'] = ucwords($active_message->message_type_label()); |
|
| 160 | - $i++; |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return $mt_values; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @deprecated 4.9.9.rc.014 |
|
| 170 | - * @return array |
|
| 171 | - */ |
|
| 172 | - public function get_contexts_for_message_types_for_list_table() |
|
| 173 | - { |
|
| 174 | - EE_Error::doing_it_wrong( |
|
| 175 | - __METHOD__, |
|
| 176 | - __('This method is no longer in use. There is no replacement for it. The method was used to generate a set of |
|
| 150 | + '4.9.9.rc.014' |
|
| 151 | + ); |
|
| 152 | + |
|
| 153 | + $mt_values = array(); |
|
| 154 | + $active_messages = EEM_Message::instance()->get_all(array('group_by' => 'MSG_message_type')); |
|
| 155 | + $i = 1; |
|
| 156 | + foreach ($active_messages as $active_message) { |
|
| 157 | + if ($active_message instanceof EE_Message) { |
|
| 158 | + $mt_values[$i]['id'] = $active_message->message_type(); |
|
| 159 | + $mt_values[$i]['text'] = ucwords($active_message->message_type_label()); |
|
| 160 | + $i++; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return $mt_values; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @deprecated 4.9.9.rc.014 |
|
| 170 | + * @return array |
|
| 171 | + */ |
|
| 172 | + public function get_contexts_for_message_types_for_list_table() |
|
| 173 | + { |
|
| 174 | + EE_Error::doing_it_wrong( |
|
| 175 | + __METHOD__, |
|
| 176 | + __('This method is no longer in use. There is no replacement for it. The method was used to generate a set of |
|
| 177 | 177 | values for use in creating a message type context filter dropdown which is now generated differently via |
| 178 | 178 | Messages_Admin_Page::get_contexts_for_message_types_select_input', 'event_espresso'), |
| 179 | - '4.9.9.rc.014' |
|
| 180 | - ); |
|
| 181 | - |
|
| 182 | - $contexts = array(); |
|
| 183 | - $active_message_contexts = EEM_Message::instance()->get_all(array('group_by' => 'MSG_context')); |
|
| 184 | - foreach ($active_message_contexts as $active_message) { |
|
| 185 | - if ($active_message instanceof EE_Message) { |
|
| 186 | - $message_type = $active_message->message_type_object(); |
|
| 187 | - if ($message_type instanceof EE_message_type) { |
|
| 188 | - $message_type_contexts = $message_type->get_contexts(); |
|
| 189 | - foreach ($message_type_contexts as $context => $context_details) { |
|
| 190 | - $contexts[$context] = $context_details['label']; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - return $contexts; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Generate select input with provided messenger options array. |
|
| 202 | - * |
|
| 203 | - * @param array $messenger_options Array of messengers indexed by messenger slug and values are the messenger |
|
| 204 | - * labels. |
|
| 205 | - * |
|
| 206 | - * @return string |
|
| 207 | - */ |
|
| 208 | - public function get_messengers_select_input($messenger_options) |
|
| 209 | - { |
|
| 210 | - //if empty or just one value then just return an empty string |
|
| 211 | - if (empty($messenger_options) |
|
| 212 | - || ! is_array($messenger_options) |
|
| 213 | - || count($messenger_options) === 1 |
|
| 214 | - ) { |
|
| 215 | - return ''; |
|
| 216 | - } |
|
| 217 | - //merge in default |
|
| 218 | - $messenger_options = array_merge( |
|
| 219 | - array('none_selected' => __('Show All Messengers', 'event_espresso')), |
|
| 220 | - $messenger_options |
|
| 221 | - ); |
|
| 222 | - $input = new EE_Select_Input( |
|
| 223 | - $messenger_options, |
|
| 224 | - array( |
|
| 225 | - 'html_name' => 'ee_messenger_filter_by', |
|
| 226 | - 'html_id' => 'ee_messenger_filter_by', |
|
| 227 | - 'html_class' => 'wide', |
|
| 228 | - 'default' => isset($this->_req_data['ee_messenger_filter_by']) |
|
| 229 | - ? sanitize_title($this->_req_data['ee_messenger_filter_by']) |
|
| 230 | - : 'none_selected' |
|
| 231 | - ) |
|
| 232 | - ); |
|
| 233 | - |
|
| 234 | - return $input->get_html_for_input(); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Generate select input with provided message type options array. |
|
| 240 | - * |
|
| 241 | - * @param array $message_type_options Array of message types indexed by message type slug, and values are the |
|
| 242 | - * message type labels |
|
| 243 | - * |
|
| 244 | - * @return string |
|
| 245 | - */ |
|
| 246 | - public function get_message_types_select_input($message_type_options) |
|
| 247 | - { |
|
| 248 | - //if empty or count of options is 1 then just return an empty string |
|
| 249 | - if (empty($message_type_options) |
|
| 250 | - || ! is_array($message_type_options) |
|
| 251 | - || count($message_type_options) === 1 |
|
| 252 | - ) { |
|
| 253 | - return ''; |
|
| 254 | - } |
|
| 255 | - //merge in default |
|
| 256 | - $message_type_options = array_merge( |
|
| 257 | - array('none_selected' => __('Show All Message Types', 'event_espresso')), |
|
| 258 | - $message_type_options |
|
| 259 | - ); |
|
| 260 | - $input = new EE_Select_Input( |
|
| 261 | - $message_type_options, |
|
| 262 | - array( |
|
| 263 | - 'html_name' => 'ee_message_type_filter_by', |
|
| 264 | - 'html_id' => 'ee_message_type_filter_by', |
|
| 265 | - 'html_class' => 'wide', |
|
| 266 | - 'default' => isset($this->_req_data['ee_message_type_filter_by']) |
|
| 267 | - ? sanitize_title($this->_req_data['ee_message_type_filter_by']) |
|
| 268 | - : 'none_selected', |
|
| 269 | - ) |
|
| 270 | - ); |
|
| 271 | - |
|
| 272 | - return $input->get_html_for_input(); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Generate select input with provide message type contexts array. |
|
| 278 | - * |
|
| 279 | - * @param array $context_options Array of message type contexts indexed by context slug, and values are the |
|
| 280 | - * context label. |
|
| 281 | - * |
|
| 282 | - * @return string |
|
| 283 | - */ |
|
| 284 | - public function get_contexts_for_message_types_select_input($context_options) |
|
| 285 | - { |
|
| 286 | - //if empty or count of options is one then just return empty string |
|
| 287 | - if (empty($context_options) |
|
| 288 | - || ! is_array($context_options) |
|
| 289 | - || count($context_options) === 1 |
|
| 290 | - ) { |
|
| 291 | - return ''; |
|
| 292 | - } |
|
| 293 | - //merge in default |
|
| 294 | - $context_options = array_merge( |
|
| 295 | - array('none_selected' => __('Show all Contexts', 'event_espresso')), |
|
| 296 | - $context_options |
|
| 297 | - ); |
|
| 298 | - $input = new EE_Select_Input( |
|
| 299 | - $context_options, |
|
| 300 | - array( |
|
| 301 | - 'html_name' => 'ee_context_filter_by', |
|
| 302 | - 'html_id' => 'ee_context_filter_by', |
|
| 303 | - 'html_class' => 'wide', |
|
| 304 | - 'default' => isset($this->_req_data['ee_context_filter_by']) |
|
| 305 | - ? sanitize_title($this->_req_data['ee_context_filter_by']) |
|
| 306 | - : 'none_selected', |
|
| 307 | - ) |
|
| 308 | - ); |
|
| 309 | - |
|
| 310 | - return $input->get_html_for_input(); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - |
|
| 314 | - protected function _ajax_hooks() |
|
| 315 | - { |
|
| 316 | - add_action('wp_ajax_activate_messenger', array($this, 'activate_messenger_toggle')); |
|
| 317 | - add_action('wp_ajax_activate_mt', array($this, 'activate_mt_toggle')); |
|
| 318 | - add_action('wp_ajax_ee_msgs_save_settings', array($this, 'save_settings')); |
|
| 319 | - add_action('wp_ajax_ee_msgs_update_mt_form', array($this, 'update_mt_form')); |
|
| 320 | - add_action('wp_ajax_switch_template_pack', array($this, 'switch_template_pack')); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - |
|
| 324 | - protected function _define_page_props() |
|
| 325 | - { |
|
| 326 | - $this->_admin_page_title = $this->page_label; |
|
| 327 | - $this->_labels = array( |
|
| 328 | - 'buttons' => array( |
|
| 329 | - 'add' => __('Add New Message Template', 'event_espresso'), |
|
| 330 | - 'edit' => __('Edit Message Template', 'event_espresso'), |
|
| 331 | - 'delete' => __('Delete Message Template', 'event_espresso') |
|
| 332 | - ), |
|
| 333 | - 'publishbox' => __('Update Actions', 'event_espresso') |
|
| 334 | - ); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * an array for storing key => value pairs of request actions and their corresponding methods |
|
| 340 | - * @access protected |
|
| 341 | - * @return void |
|
| 342 | - */ |
|
| 343 | - protected function _set_page_routes() |
|
| 344 | - { |
|
| 345 | - $grp_id = ! empty($this->_req_data['GRP_ID']) && ! is_array($this->_req_data['GRP_ID']) |
|
| 346 | - ? $this->_req_data['GRP_ID'] |
|
| 347 | - : 0; |
|
| 348 | - $grp_id = empty($grp_id) && ! empty($this->_req_data['id']) |
|
| 349 | - ? $this->_req_data['id'] |
|
| 350 | - : $grp_id; |
|
| 351 | - $msg_id = ! empty($this->_req_data['MSG_ID']) && ! is_array($this->_req_data['MSG_ID']) |
|
| 352 | - ? $this->_req_data['MSG_ID'] |
|
| 353 | - : 0; |
|
| 354 | - |
|
| 355 | - $this->_page_routes = array( |
|
| 356 | - 'default' => array( |
|
| 357 | - 'func' => '_message_queue_list_table', |
|
| 358 | - 'capability' => 'ee_read_global_messages' |
|
| 359 | - ), |
|
| 360 | - 'global_mtps' => array( |
|
| 361 | - 'func' => '_ee_default_messages_overview_list_table', |
|
| 362 | - 'capability' => 'ee_read_global_messages' |
|
| 363 | - ), |
|
| 364 | - 'custom_mtps' => array( |
|
| 365 | - 'func' => '_custom_mtps_preview', |
|
| 366 | - 'capability' => 'ee_read_messages' |
|
| 367 | - ), |
|
| 368 | - 'add_new_message_template' => array( |
|
| 369 | - 'func' => '_add_message_template', |
|
| 370 | - 'capability' => 'ee_edit_messages', |
|
| 371 | - 'noheader' => true |
|
| 372 | - ), |
|
| 373 | - 'edit_message_template' => array( |
|
| 374 | - 'func' => '_edit_message_template', |
|
| 375 | - 'capability' => 'ee_edit_message', |
|
| 376 | - 'obj_id' => $grp_id |
|
| 377 | - ), |
|
| 378 | - 'preview_message' => array( |
|
| 379 | - 'func' => '_preview_message', |
|
| 380 | - 'capability' => 'ee_read_message', |
|
| 381 | - 'obj_id' => $grp_id, |
|
| 382 | - 'noheader' => true, |
|
| 383 | - 'headers_sent_route' => 'display_preview_message' |
|
| 384 | - ), |
|
| 385 | - 'display_preview_message' => array( |
|
| 386 | - 'func' => '_display_preview_message', |
|
| 387 | - 'capability' => 'ee_read_message', |
|
| 388 | - 'obj_id' => $grp_id |
|
| 389 | - ), |
|
| 390 | - 'insert_message_template' => array( |
|
| 391 | - 'func' => '_insert_or_update_message_template', |
|
| 392 | - 'capability' => 'ee_edit_messages', |
|
| 393 | - 'args' => array('new_template' => true), |
|
| 394 | - 'noheader' => true |
|
| 395 | - ), |
|
| 396 | - 'update_message_template' => array( |
|
| 397 | - 'func' => '_insert_or_update_message_template', |
|
| 398 | - 'capability' => 'ee_edit_message', |
|
| 399 | - 'obj_id' => $grp_id, |
|
| 400 | - 'args' => array('new_template' => false), |
|
| 401 | - 'noheader' => true |
|
| 402 | - ), |
|
| 403 | - 'trash_message_template' => array( |
|
| 404 | - 'func' => '_trash_or_restore_message_template', |
|
| 405 | - 'capability' => 'ee_delete_message', |
|
| 406 | - 'obj_id' => $grp_id, |
|
| 407 | - 'args' => array('trash' => true, 'all' => true), |
|
| 408 | - 'noheader' => true |
|
| 409 | - ), |
|
| 410 | - 'trash_message_template_context' => array( |
|
| 411 | - 'func' => '_trash_or_restore_message_template', |
|
| 412 | - 'capability' => 'ee_delete_message', |
|
| 413 | - 'obj_id' => $grp_id, |
|
| 414 | - 'args' => array('trash' => true), |
|
| 415 | - 'noheader' => true |
|
| 416 | - ), |
|
| 417 | - 'restore_message_template' => array( |
|
| 418 | - 'func' => '_trash_or_restore_message_template', |
|
| 419 | - 'capability' => 'ee_delete_message', |
|
| 420 | - 'obj_id' => $grp_id, |
|
| 421 | - 'args' => array('trash' => false, 'all' => true), |
|
| 422 | - 'noheader' => true |
|
| 423 | - ), |
|
| 424 | - 'restore_message_template_context' => array( |
|
| 425 | - 'func' => '_trash_or_restore_message_template', |
|
| 426 | - 'capability' => 'ee_delete_message', |
|
| 427 | - 'obj_id' => $grp_id, |
|
| 428 | - 'args' => array('trash' => false), |
|
| 429 | - 'noheader' => true |
|
| 430 | - ), |
|
| 431 | - 'delete_message_template' => array( |
|
| 432 | - 'func' => '_delete_message_template', |
|
| 433 | - 'capability' => 'ee_delete_message', |
|
| 434 | - 'obj_id' => $grp_id, |
|
| 435 | - 'noheader' => true |
|
| 436 | - ), |
|
| 437 | - 'reset_to_default' => array( |
|
| 438 | - 'func' => '_reset_to_default_template', |
|
| 439 | - 'capability' => 'ee_edit_message', |
|
| 440 | - 'obj_id' => $grp_id, |
|
| 441 | - 'noheader' => true |
|
| 442 | - ), |
|
| 443 | - 'settings' => array( |
|
| 444 | - 'func' => '_settings', |
|
| 445 | - 'capability' => 'manage_options' |
|
| 446 | - ), |
|
| 447 | - 'update_global_settings' => array( |
|
| 448 | - 'func' => '_update_global_settings', |
|
| 449 | - 'capability' => 'manage_options', |
|
| 450 | - 'noheader' => true |
|
| 451 | - ), |
|
| 452 | - 'generate_now' => array( |
|
| 453 | - 'func' => '_generate_now', |
|
| 454 | - 'capability' => 'ee_send_message', |
|
| 455 | - 'noheader' => true |
|
| 456 | - ), |
|
| 457 | - 'generate_and_send_now' => array( |
|
| 458 | - 'func' => '_generate_and_send_now', |
|
| 459 | - 'capability' => 'ee_send_message', |
|
| 460 | - 'noheader' => true |
|
| 461 | - ), |
|
| 462 | - 'queue_for_resending' => array( |
|
| 463 | - 'func' => '_queue_for_resending', |
|
| 464 | - 'capability' => 'ee_send_message', |
|
| 465 | - 'noheader' => true |
|
| 466 | - ), |
|
| 467 | - 'send_now' => array( |
|
| 468 | - 'func' => '_send_now', |
|
| 469 | - 'capability' => 'ee_send_message', |
|
| 470 | - 'noheader' => true |
|
| 471 | - ), |
|
| 472 | - 'delete_ee_message' => array( |
|
| 473 | - 'func' => '_delete_ee_messages', |
|
| 474 | - 'capability' => 'ee_delete_messages', |
|
| 475 | - 'noheader' => true |
|
| 476 | - ), |
|
| 477 | - 'delete_ee_messages' => array( |
|
| 478 | - 'func' => '_delete_ee_messages', |
|
| 479 | - 'capability' => 'ee_delete_messages', |
|
| 480 | - 'noheader' => true, |
|
| 481 | - 'obj_id' => $msg_id |
|
| 482 | - ) |
|
| 483 | - ); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - |
|
| 487 | - protected function _set_page_config() |
|
| 488 | - { |
|
| 489 | - $this->_page_config = array( |
|
| 490 | - 'default' => array( |
|
| 491 | - 'nav' => array( |
|
| 492 | - 'label' => __('Message Activity', 'event_espresso'), |
|
| 493 | - 'order' => 10 |
|
| 494 | - ), |
|
| 495 | - 'list_table' => 'EE_Message_List_Table', |
|
| 496 | - // 'qtips' => array( 'EE_Message_List_Table_Tips' ), |
|
| 497 | - 'require_nonce' => false |
|
| 498 | - ), |
|
| 499 | - 'global_mtps' => array( |
|
| 500 | - 'nav' => array( |
|
| 501 | - 'label' => __('Default Message Templates', 'event_espresso'), |
|
| 502 | - 'order' => 20 |
|
| 503 | - ), |
|
| 504 | - 'list_table' => 'Messages_Template_List_Table', |
|
| 505 | - 'help_tabs' => array( |
|
| 506 | - 'messages_overview_help_tab' => array( |
|
| 507 | - 'title' => __('Messages Overview', 'event_espresso'), |
|
| 508 | - 'filename' => 'messages_overview' |
|
| 509 | - ), |
|
| 510 | - 'messages_overview_messages_table_column_headings_help_tab' => array( |
|
| 511 | - 'title' => __('Messages Table Column Headings', 'event_espresso'), |
|
| 512 | - 'filename' => 'messages_overview_table_column_headings' |
|
| 513 | - ), |
|
| 514 | - 'messages_overview_messages_filters_help_tab' => array( |
|
| 515 | - 'title' => __('Message Filters', 'event_espresso'), |
|
| 516 | - 'filename' => 'messages_overview_filters' |
|
| 517 | - ), |
|
| 518 | - 'messages_overview_messages_views_help_tab' => array( |
|
| 519 | - 'title' => __('Message Views', 'event_espresso'), |
|
| 520 | - 'filename' => 'messages_overview_views' |
|
| 521 | - ), |
|
| 522 | - 'message_overview_message_types_help_tab' => array( |
|
| 523 | - 'title' => __('Message Types', 'event_espresso'), |
|
| 524 | - 'filename' => 'messages_overview_types' |
|
| 525 | - ), |
|
| 526 | - 'messages_overview_messengers_help_tab' => array( |
|
| 527 | - 'title' => __('Messengers', 'event_espresso'), |
|
| 528 | - 'filename' => 'messages_overview_messengers', |
|
| 529 | - ), |
|
| 530 | - ), |
|
| 531 | - 'help_tour' => array('Messages_Overview_Help_Tour'), |
|
| 532 | - 'require_nonce' => false |
|
| 533 | - ), |
|
| 534 | - 'custom_mtps' => array( |
|
| 535 | - 'nav' => array( |
|
| 536 | - 'label' => __('Custom Message Templates', 'event_espresso'), |
|
| 537 | - 'order' => 30 |
|
| 538 | - ), |
|
| 539 | - 'help_tabs' => array(), |
|
| 540 | - 'help_tour' => array(), |
|
| 541 | - 'require_nonce' => false |
|
| 542 | - ), |
|
| 543 | - 'add_new_message_template' => array( |
|
| 544 | - 'nav' => array( |
|
| 545 | - 'label' => __('Add New Message Templates', 'event_espresso'), |
|
| 546 | - 'order' => 5, |
|
| 547 | - 'persistent' => false |
|
| 548 | - ), |
|
| 549 | - 'require_nonce' => false |
|
| 550 | - ), |
|
| 551 | - 'edit_message_template' => array( |
|
| 552 | - 'labels' => array( |
|
| 553 | - 'buttons' => array( |
|
| 554 | - 'reset' => __('Reset Templates'), |
|
| 555 | - ), |
|
| 556 | - 'publishbox' => __('Update Actions', 'event_espresso') |
|
| 557 | - ), |
|
| 558 | - 'nav' => array( |
|
| 559 | - 'label' => __('Edit Message Templates', 'event_espresso'), |
|
| 560 | - 'order' => 5, |
|
| 561 | - 'persistent' => false, |
|
| 562 | - 'url' => '' |
|
| 563 | - ), |
|
| 564 | - 'metaboxes' => array('_publish_post_box', '_register_edit_meta_boxes'), |
|
| 565 | - 'has_metaboxes' => true, |
|
| 566 | - 'help_tour' => array('Message_Templates_Edit_Help_Tour'), |
|
| 567 | - 'help_tabs' => array( |
|
| 568 | - 'edit_message_template' => array( |
|
| 569 | - 'title' => __('Message Template Editor', 'event_espresso'), |
|
| 570 | - 'callback' => 'edit_message_template_help_tab' |
|
| 571 | - ), |
|
| 572 | - 'message_templates_help_tab' => array( |
|
| 573 | - 'title' => __('Message Templates', 'event_espresso'), |
|
| 574 | - 'filename' => 'messages_templates' |
|
| 575 | - ), |
|
| 576 | - 'message_template_shortcodes' => array( |
|
| 577 | - 'title' => __('Message Shortcodes', 'event_espresso'), |
|
| 578 | - 'callback' => 'message_template_shortcodes_help_tab' |
|
| 579 | - ), |
|
| 580 | - 'message_preview_help_tab' => array( |
|
| 581 | - 'title' => __('Message Preview', 'event_espresso'), |
|
| 582 | - 'filename' => 'messages_preview' |
|
| 583 | - ), |
|
| 584 | - 'messages_overview_other_help_tab' => array( |
|
| 585 | - 'title' => __('Messages Other', 'event_espresso'), |
|
| 586 | - 'filename' => 'messages_overview_other', |
|
| 587 | - ), |
|
| 588 | - ), |
|
| 589 | - 'require_nonce' => false |
|
| 590 | - ), |
|
| 591 | - 'display_preview_message' => array( |
|
| 592 | - 'nav' => array( |
|
| 593 | - 'label' => __('Message Preview', 'event_espresso'), |
|
| 594 | - 'order' => 5, |
|
| 595 | - 'url' => '', |
|
| 596 | - 'persistent' => false |
|
| 597 | - ), |
|
| 598 | - 'help_tabs' => array( |
|
| 599 | - 'preview_message' => array( |
|
| 600 | - 'title' => __('About Previews', 'event_espresso'), |
|
| 601 | - 'callback' => 'preview_message_help_tab' |
|
| 602 | - ) |
|
| 603 | - ), |
|
| 604 | - 'require_nonce' => false |
|
| 605 | - ), |
|
| 606 | - 'settings' => array( |
|
| 607 | - 'nav' => array( |
|
| 608 | - 'label' => __('Settings', 'event_espresso'), |
|
| 609 | - 'order' => 40 |
|
| 610 | - ), |
|
| 611 | - 'metaboxes' => array('_messages_settings_metaboxes'), |
|
| 612 | - 'help_tabs' => array( |
|
| 613 | - 'messages_settings_help_tab' => array( |
|
| 614 | - 'title' => __('Messages Settings', 'event_espresso'), |
|
| 615 | - 'filename' => 'messages_settings' |
|
| 616 | - ), |
|
| 617 | - 'messages_settings_message_types_help_tab' => array( |
|
| 618 | - 'title' => __('Activating / Deactivating Message Types', 'event_espresso'), |
|
| 619 | - 'filename' => 'messages_settings_message_types' |
|
| 620 | - ), |
|
| 621 | - 'messages_settings_messengers_help_tab' => array( |
|
| 622 | - 'title' => __('Activating / Deactivating Messengers', 'event_espresso'), |
|
| 623 | - 'filename' => 'messages_settings_messengers' |
|
| 624 | - ), |
|
| 625 | - ), |
|
| 626 | - 'help_tour' => array('Messages_Settings_Help_Tour'), |
|
| 627 | - 'require_nonce' => false |
|
| 628 | - ) |
|
| 629 | - ); |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - |
|
| 633 | - protected function _add_screen_options() |
|
| 634 | - { |
|
| 635 | - //todo |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - |
|
| 639 | - protected function _add_screen_options_global_mtps() |
|
| 640 | - { |
|
| 641 | - /** |
|
| 642 | - * Note: the reason for the value swap here on $this->_admin_page_title is because $this->_per_page_screen_options |
|
| 643 | - * uses the $_admin_page_title property and we want different outputs in the different spots. |
|
| 644 | - */ |
|
| 645 | - $page_title = $this->_admin_page_title; |
|
| 646 | - $this->_admin_page_title = __('Global Message Templates', 'event_espresso'); |
|
| 647 | - $this->_per_page_screen_option(); |
|
| 648 | - $this->_admin_page_title = $page_title; |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - |
|
| 652 | - protected function _add_screen_options_default() |
|
| 653 | - { |
|
| 654 | - $this->_admin_page_title = __('Message Activity', 'event_espresso'); |
|
| 655 | - $this->_per_page_screen_option(); |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - |
|
| 659 | - //none of the below group are currently used for Messages |
|
| 660 | - protected function _add_feature_pointers() |
|
| 661 | - { |
|
| 662 | - } |
|
| 663 | - |
|
| 664 | - public function admin_init() |
|
| 665 | - { |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - public function admin_notices() |
|
| 669 | - { |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - public function admin_footer_scripts() |
|
| 673 | - { |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - |
|
| 677 | - public function messages_help_tab() |
|
| 678 | - { |
|
| 679 | - EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_help_tab.template.php'); |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - |
|
| 683 | - public function messengers_help_tab() |
|
| 684 | - { |
|
| 685 | - EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messenger_help_tab.template.php'); |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - |
|
| 689 | - public function message_types_help_tab() |
|
| 690 | - { |
|
| 691 | - EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_type_help_tab.template.php'); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - |
|
| 695 | - public function messages_overview_help_tab() |
|
| 696 | - { |
|
| 697 | - EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_overview_help_tab.template.php'); |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - |
|
| 701 | - public function message_templates_help_tab() |
|
| 702 | - { |
|
| 703 | - EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_templates_help_tab.template.php'); |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - |
|
| 707 | - public function edit_message_template_help_tab() |
|
| 708 | - { |
|
| 709 | - $args['img1'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/editor.png' . '" alt="' . esc_attr__('Editor Title', |
|
| 710 | - 'event_espresso') . '" />'; |
|
| 711 | - $args['img2'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/switch-context.png' . '" alt="' . esc_attr__('Context Switcher and Preview', |
|
| 712 | - 'event_espresso') . '" />'; |
|
| 713 | - $args['img3'] = '<img class="left" src="' . EE_MSG_ASSETS_URL . 'images/form-fields.png' . '" alt="' . esc_attr__('Message Template Form Fields', |
|
| 714 | - 'event_espresso') . '" />'; |
|
| 715 | - $args['img4'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/shortcodes-metabox.png' . '" alt="' . esc_attr__('Shortcodes Metabox', |
|
| 716 | - 'event_espresso') . '" />'; |
|
| 717 | - $args['img5'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/publish-meta-box.png' . '" alt="' . esc_attr__('Publish Metabox', |
|
| 718 | - 'event_espresso') . '" />'; |
|
| 719 | - EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_templates_editor_help_tab.template.php', |
|
| 720 | - $args); |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - |
|
| 724 | - public function message_template_shortcodes_help_tab() |
|
| 725 | - { |
|
| 726 | - $this->_set_shortcodes(); |
|
| 727 | - $args['shortcodes'] = $this->_shortcodes; |
|
| 728 | - EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_shortcodes_help_tab.template.php', |
|
| 729 | - $args); |
|
| 730 | - } |
|
| 179 | + '4.9.9.rc.014' |
|
| 180 | + ); |
|
| 181 | + |
|
| 182 | + $contexts = array(); |
|
| 183 | + $active_message_contexts = EEM_Message::instance()->get_all(array('group_by' => 'MSG_context')); |
|
| 184 | + foreach ($active_message_contexts as $active_message) { |
|
| 185 | + if ($active_message instanceof EE_Message) { |
|
| 186 | + $message_type = $active_message->message_type_object(); |
|
| 187 | + if ($message_type instanceof EE_message_type) { |
|
| 188 | + $message_type_contexts = $message_type->get_contexts(); |
|
| 189 | + foreach ($message_type_contexts as $context => $context_details) { |
|
| 190 | + $contexts[$context] = $context_details['label']; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + return $contexts; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Generate select input with provided messenger options array. |
|
| 202 | + * |
|
| 203 | + * @param array $messenger_options Array of messengers indexed by messenger slug and values are the messenger |
|
| 204 | + * labels. |
|
| 205 | + * |
|
| 206 | + * @return string |
|
| 207 | + */ |
|
| 208 | + public function get_messengers_select_input($messenger_options) |
|
| 209 | + { |
|
| 210 | + //if empty or just one value then just return an empty string |
|
| 211 | + if (empty($messenger_options) |
|
| 212 | + || ! is_array($messenger_options) |
|
| 213 | + || count($messenger_options) === 1 |
|
| 214 | + ) { |
|
| 215 | + return ''; |
|
| 216 | + } |
|
| 217 | + //merge in default |
|
| 218 | + $messenger_options = array_merge( |
|
| 219 | + array('none_selected' => __('Show All Messengers', 'event_espresso')), |
|
| 220 | + $messenger_options |
|
| 221 | + ); |
|
| 222 | + $input = new EE_Select_Input( |
|
| 223 | + $messenger_options, |
|
| 224 | + array( |
|
| 225 | + 'html_name' => 'ee_messenger_filter_by', |
|
| 226 | + 'html_id' => 'ee_messenger_filter_by', |
|
| 227 | + 'html_class' => 'wide', |
|
| 228 | + 'default' => isset($this->_req_data['ee_messenger_filter_by']) |
|
| 229 | + ? sanitize_title($this->_req_data['ee_messenger_filter_by']) |
|
| 230 | + : 'none_selected' |
|
| 231 | + ) |
|
| 232 | + ); |
|
| 233 | + |
|
| 234 | + return $input->get_html_for_input(); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Generate select input with provided message type options array. |
|
| 240 | + * |
|
| 241 | + * @param array $message_type_options Array of message types indexed by message type slug, and values are the |
|
| 242 | + * message type labels |
|
| 243 | + * |
|
| 244 | + * @return string |
|
| 245 | + */ |
|
| 246 | + public function get_message_types_select_input($message_type_options) |
|
| 247 | + { |
|
| 248 | + //if empty or count of options is 1 then just return an empty string |
|
| 249 | + if (empty($message_type_options) |
|
| 250 | + || ! is_array($message_type_options) |
|
| 251 | + || count($message_type_options) === 1 |
|
| 252 | + ) { |
|
| 253 | + return ''; |
|
| 254 | + } |
|
| 255 | + //merge in default |
|
| 256 | + $message_type_options = array_merge( |
|
| 257 | + array('none_selected' => __('Show All Message Types', 'event_espresso')), |
|
| 258 | + $message_type_options |
|
| 259 | + ); |
|
| 260 | + $input = new EE_Select_Input( |
|
| 261 | + $message_type_options, |
|
| 262 | + array( |
|
| 263 | + 'html_name' => 'ee_message_type_filter_by', |
|
| 264 | + 'html_id' => 'ee_message_type_filter_by', |
|
| 265 | + 'html_class' => 'wide', |
|
| 266 | + 'default' => isset($this->_req_data['ee_message_type_filter_by']) |
|
| 267 | + ? sanitize_title($this->_req_data['ee_message_type_filter_by']) |
|
| 268 | + : 'none_selected', |
|
| 269 | + ) |
|
| 270 | + ); |
|
| 271 | + |
|
| 272 | + return $input->get_html_for_input(); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Generate select input with provide message type contexts array. |
|
| 278 | + * |
|
| 279 | + * @param array $context_options Array of message type contexts indexed by context slug, and values are the |
|
| 280 | + * context label. |
|
| 281 | + * |
|
| 282 | + * @return string |
|
| 283 | + */ |
|
| 284 | + public function get_contexts_for_message_types_select_input($context_options) |
|
| 285 | + { |
|
| 286 | + //if empty or count of options is one then just return empty string |
|
| 287 | + if (empty($context_options) |
|
| 288 | + || ! is_array($context_options) |
|
| 289 | + || count($context_options) === 1 |
|
| 290 | + ) { |
|
| 291 | + return ''; |
|
| 292 | + } |
|
| 293 | + //merge in default |
|
| 294 | + $context_options = array_merge( |
|
| 295 | + array('none_selected' => __('Show all Contexts', 'event_espresso')), |
|
| 296 | + $context_options |
|
| 297 | + ); |
|
| 298 | + $input = new EE_Select_Input( |
|
| 299 | + $context_options, |
|
| 300 | + array( |
|
| 301 | + 'html_name' => 'ee_context_filter_by', |
|
| 302 | + 'html_id' => 'ee_context_filter_by', |
|
| 303 | + 'html_class' => 'wide', |
|
| 304 | + 'default' => isset($this->_req_data['ee_context_filter_by']) |
|
| 305 | + ? sanitize_title($this->_req_data['ee_context_filter_by']) |
|
| 306 | + : 'none_selected', |
|
| 307 | + ) |
|
| 308 | + ); |
|
| 309 | + |
|
| 310 | + return $input->get_html_for_input(); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + |
|
| 314 | + protected function _ajax_hooks() |
|
| 315 | + { |
|
| 316 | + add_action('wp_ajax_activate_messenger', array($this, 'activate_messenger_toggle')); |
|
| 317 | + add_action('wp_ajax_activate_mt', array($this, 'activate_mt_toggle')); |
|
| 318 | + add_action('wp_ajax_ee_msgs_save_settings', array($this, 'save_settings')); |
|
| 319 | + add_action('wp_ajax_ee_msgs_update_mt_form', array($this, 'update_mt_form')); |
|
| 320 | + add_action('wp_ajax_switch_template_pack', array($this, 'switch_template_pack')); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + |
|
| 324 | + protected function _define_page_props() |
|
| 325 | + { |
|
| 326 | + $this->_admin_page_title = $this->page_label; |
|
| 327 | + $this->_labels = array( |
|
| 328 | + 'buttons' => array( |
|
| 329 | + 'add' => __('Add New Message Template', 'event_espresso'), |
|
| 330 | + 'edit' => __('Edit Message Template', 'event_espresso'), |
|
| 331 | + 'delete' => __('Delete Message Template', 'event_espresso') |
|
| 332 | + ), |
|
| 333 | + 'publishbox' => __('Update Actions', 'event_espresso') |
|
| 334 | + ); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * an array for storing key => value pairs of request actions and their corresponding methods |
|
| 340 | + * @access protected |
|
| 341 | + * @return void |
|
| 342 | + */ |
|
| 343 | + protected function _set_page_routes() |
|
| 344 | + { |
|
| 345 | + $grp_id = ! empty($this->_req_data['GRP_ID']) && ! is_array($this->_req_data['GRP_ID']) |
|
| 346 | + ? $this->_req_data['GRP_ID'] |
|
| 347 | + : 0; |
|
| 348 | + $grp_id = empty($grp_id) && ! empty($this->_req_data['id']) |
|
| 349 | + ? $this->_req_data['id'] |
|
| 350 | + : $grp_id; |
|
| 351 | + $msg_id = ! empty($this->_req_data['MSG_ID']) && ! is_array($this->_req_data['MSG_ID']) |
|
| 352 | + ? $this->_req_data['MSG_ID'] |
|
| 353 | + : 0; |
|
| 354 | + |
|
| 355 | + $this->_page_routes = array( |
|
| 356 | + 'default' => array( |
|
| 357 | + 'func' => '_message_queue_list_table', |
|
| 358 | + 'capability' => 'ee_read_global_messages' |
|
| 359 | + ), |
|
| 360 | + 'global_mtps' => array( |
|
| 361 | + 'func' => '_ee_default_messages_overview_list_table', |
|
| 362 | + 'capability' => 'ee_read_global_messages' |
|
| 363 | + ), |
|
| 364 | + 'custom_mtps' => array( |
|
| 365 | + 'func' => '_custom_mtps_preview', |
|
| 366 | + 'capability' => 'ee_read_messages' |
|
| 367 | + ), |
|
| 368 | + 'add_new_message_template' => array( |
|
| 369 | + 'func' => '_add_message_template', |
|
| 370 | + 'capability' => 'ee_edit_messages', |
|
| 371 | + 'noheader' => true |
|
| 372 | + ), |
|
| 373 | + 'edit_message_template' => array( |
|
| 374 | + 'func' => '_edit_message_template', |
|
| 375 | + 'capability' => 'ee_edit_message', |
|
| 376 | + 'obj_id' => $grp_id |
|
| 377 | + ), |
|
| 378 | + 'preview_message' => array( |
|
| 379 | + 'func' => '_preview_message', |
|
| 380 | + 'capability' => 'ee_read_message', |
|
| 381 | + 'obj_id' => $grp_id, |
|
| 382 | + 'noheader' => true, |
|
| 383 | + 'headers_sent_route' => 'display_preview_message' |
|
| 384 | + ), |
|
| 385 | + 'display_preview_message' => array( |
|
| 386 | + 'func' => '_display_preview_message', |
|
| 387 | + 'capability' => 'ee_read_message', |
|
| 388 | + 'obj_id' => $grp_id |
|
| 389 | + ), |
|
| 390 | + 'insert_message_template' => array( |
|
| 391 | + 'func' => '_insert_or_update_message_template', |
|
| 392 | + 'capability' => 'ee_edit_messages', |
|
| 393 | + 'args' => array('new_template' => true), |
|
| 394 | + 'noheader' => true |
|
| 395 | + ), |
|
| 396 | + 'update_message_template' => array( |
|
| 397 | + 'func' => '_insert_or_update_message_template', |
|
| 398 | + 'capability' => 'ee_edit_message', |
|
| 399 | + 'obj_id' => $grp_id, |
|
| 400 | + 'args' => array('new_template' => false), |
|
| 401 | + 'noheader' => true |
|
| 402 | + ), |
|
| 403 | + 'trash_message_template' => array( |
|
| 404 | + 'func' => '_trash_or_restore_message_template', |
|
| 405 | + 'capability' => 'ee_delete_message', |
|
| 406 | + 'obj_id' => $grp_id, |
|
| 407 | + 'args' => array('trash' => true, 'all' => true), |
|
| 408 | + 'noheader' => true |
|
| 409 | + ), |
|
| 410 | + 'trash_message_template_context' => array( |
|
| 411 | + 'func' => '_trash_or_restore_message_template', |
|
| 412 | + 'capability' => 'ee_delete_message', |
|
| 413 | + 'obj_id' => $grp_id, |
|
| 414 | + 'args' => array('trash' => true), |
|
| 415 | + 'noheader' => true |
|
| 416 | + ), |
|
| 417 | + 'restore_message_template' => array( |
|
| 418 | + 'func' => '_trash_or_restore_message_template', |
|
| 419 | + 'capability' => 'ee_delete_message', |
|
| 420 | + 'obj_id' => $grp_id, |
|
| 421 | + 'args' => array('trash' => false, 'all' => true), |
|
| 422 | + 'noheader' => true |
|
| 423 | + ), |
|
| 424 | + 'restore_message_template_context' => array( |
|
| 425 | + 'func' => '_trash_or_restore_message_template', |
|
| 426 | + 'capability' => 'ee_delete_message', |
|
| 427 | + 'obj_id' => $grp_id, |
|
| 428 | + 'args' => array('trash' => false), |
|
| 429 | + 'noheader' => true |
|
| 430 | + ), |
|
| 431 | + 'delete_message_template' => array( |
|
| 432 | + 'func' => '_delete_message_template', |
|
| 433 | + 'capability' => 'ee_delete_message', |
|
| 434 | + 'obj_id' => $grp_id, |
|
| 435 | + 'noheader' => true |
|
| 436 | + ), |
|
| 437 | + 'reset_to_default' => array( |
|
| 438 | + 'func' => '_reset_to_default_template', |
|
| 439 | + 'capability' => 'ee_edit_message', |
|
| 440 | + 'obj_id' => $grp_id, |
|
| 441 | + 'noheader' => true |
|
| 442 | + ), |
|
| 443 | + 'settings' => array( |
|
| 444 | + 'func' => '_settings', |
|
| 445 | + 'capability' => 'manage_options' |
|
| 446 | + ), |
|
| 447 | + 'update_global_settings' => array( |
|
| 448 | + 'func' => '_update_global_settings', |
|
| 449 | + 'capability' => 'manage_options', |
|
| 450 | + 'noheader' => true |
|
| 451 | + ), |
|
| 452 | + 'generate_now' => array( |
|
| 453 | + 'func' => '_generate_now', |
|
| 454 | + 'capability' => 'ee_send_message', |
|
| 455 | + 'noheader' => true |
|
| 456 | + ), |
|
| 457 | + 'generate_and_send_now' => array( |
|
| 458 | + 'func' => '_generate_and_send_now', |
|
| 459 | + 'capability' => 'ee_send_message', |
|
| 460 | + 'noheader' => true |
|
| 461 | + ), |
|
| 462 | + 'queue_for_resending' => array( |
|
| 463 | + 'func' => '_queue_for_resending', |
|
| 464 | + 'capability' => 'ee_send_message', |
|
| 465 | + 'noheader' => true |
|
| 466 | + ), |
|
| 467 | + 'send_now' => array( |
|
| 468 | + 'func' => '_send_now', |
|
| 469 | + 'capability' => 'ee_send_message', |
|
| 470 | + 'noheader' => true |
|
| 471 | + ), |
|
| 472 | + 'delete_ee_message' => array( |
|
| 473 | + 'func' => '_delete_ee_messages', |
|
| 474 | + 'capability' => 'ee_delete_messages', |
|
| 475 | + 'noheader' => true |
|
| 476 | + ), |
|
| 477 | + 'delete_ee_messages' => array( |
|
| 478 | + 'func' => '_delete_ee_messages', |
|
| 479 | + 'capability' => 'ee_delete_messages', |
|
| 480 | + 'noheader' => true, |
|
| 481 | + 'obj_id' => $msg_id |
|
| 482 | + ) |
|
| 483 | + ); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + |
|
| 487 | + protected function _set_page_config() |
|
| 488 | + { |
|
| 489 | + $this->_page_config = array( |
|
| 490 | + 'default' => array( |
|
| 491 | + 'nav' => array( |
|
| 492 | + 'label' => __('Message Activity', 'event_espresso'), |
|
| 493 | + 'order' => 10 |
|
| 494 | + ), |
|
| 495 | + 'list_table' => 'EE_Message_List_Table', |
|
| 496 | + // 'qtips' => array( 'EE_Message_List_Table_Tips' ), |
|
| 497 | + 'require_nonce' => false |
|
| 498 | + ), |
|
| 499 | + 'global_mtps' => array( |
|
| 500 | + 'nav' => array( |
|
| 501 | + 'label' => __('Default Message Templates', 'event_espresso'), |
|
| 502 | + 'order' => 20 |
|
| 503 | + ), |
|
| 504 | + 'list_table' => 'Messages_Template_List_Table', |
|
| 505 | + 'help_tabs' => array( |
|
| 506 | + 'messages_overview_help_tab' => array( |
|
| 507 | + 'title' => __('Messages Overview', 'event_espresso'), |
|
| 508 | + 'filename' => 'messages_overview' |
|
| 509 | + ), |
|
| 510 | + 'messages_overview_messages_table_column_headings_help_tab' => array( |
|
| 511 | + 'title' => __('Messages Table Column Headings', 'event_espresso'), |
|
| 512 | + 'filename' => 'messages_overview_table_column_headings' |
|
| 513 | + ), |
|
| 514 | + 'messages_overview_messages_filters_help_tab' => array( |
|
| 515 | + 'title' => __('Message Filters', 'event_espresso'), |
|
| 516 | + 'filename' => 'messages_overview_filters' |
|
| 517 | + ), |
|
| 518 | + 'messages_overview_messages_views_help_tab' => array( |
|
| 519 | + 'title' => __('Message Views', 'event_espresso'), |
|
| 520 | + 'filename' => 'messages_overview_views' |
|
| 521 | + ), |
|
| 522 | + 'message_overview_message_types_help_tab' => array( |
|
| 523 | + 'title' => __('Message Types', 'event_espresso'), |
|
| 524 | + 'filename' => 'messages_overview_types' |
|
| 525 | + ), |
|
| 526 | + 'messages_overview_messengers_help_tab' => array( |
|
| 527 | + 'title' => __('Messengers', 'event_espresso'), |
|
| 528 | + 'filename' => 'messages_overview_messengers', |
|
| 529 | + ), |
|
| 530 | + ), |
|
| 531 | + 'help_tour' => array('Messages_Overview_Help_Tour'), |
|
| 532 | + 'require_nonce' => false |
|
| 533 | + ), |
|
| 534 | + 'custom_mtps' => array( |
|
| 535 | + 'nav' => array( |
|
| 536 | + 'label' => __('Custom Message Templates', 'event_espresso'), |
|
| 537 | + 'order' => 30 |
|
| 538 | + ), |
|
| 539 | + 'help_tabs' => array(), |
|
| 540 | + 'help_tour' => array(), |
|
| 541 | + 'require_nonce' => false |
|
| 542 | + ), |
|
| 543 | + 'add_new_message_template' => array( |
|
| 544 | + 'nav' => array( |
|
| 545 | + 'label' => __('Add New Message Templates', 'event_espresso'), |
|
| 546 | + 'order' => 5, |
|
| 547 | + 'persistent' => false |
|
| 548 | + ), |
|
| 549 | + 'require_nonce' => false |
|
| 550 | + ), |
|
| 551 | + 'edit_message_template' => array( |
|
| 552 | + 'labels' => array( |
|
| 553 | + 'buttons' => array( |
|
| 554 | + 'reset' => __('Reset Templates'), |
|
| 555 | + ), |
|
| 556 | + 'publishbox' => __('Update Actions', 'event_espresso') |
|
| 557 | + ), |
|
| 558 | + 'nav' => array( |
|
| 559 | + 'label' => __('Edit Message Templates', 'event_espresso'), |
|
| 560 | + 'order' => 5, |
|
| 561 | + 'persistent' => false, |
|
| 562 | + 'url' => '' |
|
| 563 | + ), |
|
| 564 | + 'metaboxes' => array('_publish_post_box', '_register_edit_meta_boxes'), |
|
| 565 | + 'has_metaboxes' => true, |
|
| 566 | + 'help_tour' => array('Message_Templates_Edit_Help_Tour'), |
|
| 567 | + 'help_tabs' => array( |
|
| 568 | + 'edit_message_template' => array( |
|
| 569 | + 'title' => __('Message Template Editor', 'event_espresso'), |
|
| 570 | + 'callback' => 'edit_message_template_help_tab' |
|
| 571 | + ), |
|
| 572 | + 'message_templates_help_tab' => array( |
|
| 573 | + 'title' => __('Message Templates', 'event_espresso'), |
|
| 574 | + 'filename' => 'messages_templates' |
|
| 575 | + ), |
|
| 576 | + 'message_template_shortcodes' => array( |
|
| 577 | + 'title' => __('Message Shortcodes', 'event_espresso'), |
|
| 578 | + 'callback' => 'message_template_shortcodes_help_tab' |
|
| 579 | + ), |
|
| 580 | + 'message_preview_help_tab' => array( |
|
| 581 | + 'title' => __('Message Preview', 'event_espresso'), |
|
| 582 | + 'filename' => 'messages_preview' |
|
| 583 | + ), |
|
| 584 | + 'messages_overview_other_help_tab' => array( |
|
| 585 | + 'title' => __('Messages Other', 'event_espresso'), |
|
| 586 | + 'filename' => 'messages_overview_other', |
|
| 587 | + ), |
|
| 588 | + ), |
|
| 589 | + 'require_nonce' => false |
|
| 590 | + ), |
|
| 591 | + 'display_preview_message' => array( |
|
| 592 | + 'nav' => array( |
|
| 593 | + 'label' => __('Message Preview', 'event_espresso'), |
|
| 594 | + 'order' => 5, |
|
| 595 | + 'url' => '', |
|
| 596 | + 'persistent' => false |
|
| 597 | + ), |
|
| 598 | + 'help_tabs' => array( |
|
| 599 | + 'preview_message' => array( |
|
| 600 | + 'title' => __('About Previews', 'event_espresso'), |
|
| 601 | + 'callback' => 'preview_message_help_tab' |
|
| 602 | + ) |
|
| 603 | + ), |
|
| 604 | + 'require_nonce' => false |
|
| 605 | + ), |
|
| 606 | + 'settings' => array( |
|
| 607 | + 'nav' => array( |
|
| 608 | + 'label' => __('Settings', 'event_espresso'), |
|
| 609 | + 'order' => 40 |
|
| 610 | + ), |
|
| 611 | + 'metaboxes' => array('_messages_settings_metaboxes'), |
|
| 612 | + 'help_tabs' => array( |
|
| 613 | + 'messages_settings_help_tab' => array( |
|
| 614 | + 'title' => __('Messages Settings', 'event_espresso'), |
|
| 615 | + 'filename' => 'messages_settings' |
|
| 616 | + ), |
|
| 617 | + 'messages_settings_message_types_help_tab' => array( |
|
| 618 | + 'title' => __('Activating / Deactivating Message Types', 'event_espresso'), |
|
| 619 | + 'filename' => 'messages_settings_message_types' |
|
| 620 | + ), |
|
| 621 | + 'messages_settings_messengers_help_tab' => array( |
|
| 622 | + 'title' => __('Activating / Deactivating Messengers', 'event_espresso'), |
|
| 623 | + 'filename' => 'messages_settings_messengers' |
|
| 624 | + ), |
|
| 625 | + ), |
|
| 626 | + 'help_tour' => array('Messages_Settings_Help_Tour'), |
|
| 627 | + 'require_nonce' => false |
|
| 628 | + ) |
|
| 629 | + ); |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + |
|
| 633 | + protected function _add_screen_options() |
|
| 634 | + { |
|
| 635 | + //todo |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + |
|
| 639 | + protected function _add_screen_options_global_mtps() |
|
| 640 | + { |
|
| 641 | + /** |
|
| 642 | + * Note: the reason for the value swap here on $this->_admin_page_title is because $this->_per_page_screen_options |
|
| 643 | + * uses the $_admin_page_title property and we want different outputs in the different spots. |
|
| 644 | + */ |
|
| 645 | + $page_title = $this->_admin_page_title; |
|
| 646 | + $this->_admin_page_title = __('Global Message Templates', 'event_espresso'); |
|
| 647 | + $this->_per_page_screen_option(); |
|
| 648 | + $this->_admin_page_title = $page_title; |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + |
|
| 652 | + protected function _add_screen_options_default() |
|
| 653 | + { |
|
| 654 | + $this->_admin_page_title = __('Message Activity', 'event_espresso'); |
|
| 655 | + $this->_per_page_screen_option(); |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + |
|
| 659 | + //none of the below group are currently used for Messages |
|
| 660 | + protected function _add_feature_pointers() |
|
| 661 | + { |
|
| 662 | + } |
|
| 663 | + |
|
| 664 | + public function admin_init() |
|
| 665 | + { |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + public function admin_notices() |
|
| 669 | + { |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + public function admin_footer_scripts() |
|
| 673 | + { |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + |
|
| 677 | + public function messages_help_tab() |
|
| 678 | + { |
|
| 679 | + EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_help_tab.template.php'); |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + |
|
| 683 | + public function messengers_help_tab() |
|
| 684 | + { |
|
| 685 | + EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messenger_help_tab.template.php'); |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + |
|
| 689 | + public function message_types_help_tab() |
|
| 690 | + { |
|
| 691 | + EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_type_help_tab.template.php'); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + |
|
| 695 | + public function messages_overview_help_tab() |
|
| 696 | + { |
|
| 697 | + EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_overview_help_tab.template.php'); |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + |
|
| 701 | + public function message_templates_help_tab() |
|
| 702 | + { |
|
| 703 | + EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_templates_help_tab.template.php'); |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + |
|
| 707 | + public function edit_message_template_help_tab() |
|
| 708 | + { |
|
| 709 | + $args['img1'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/editor.png' . '" alt="' . esc_attr__('Editor Title', |
|
| 710 | + 'event_espresso') . '" />'; |
|
| 711 | + $args['img2'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/switch-context.png' . '" alt="' . esc_attr__('Context Switcher and Preview', |
|
| 712 | + 'event_espresso') . '" />'; |
|
| 713 | + $args['img3'] = '<img class="left" src="' . EE_MSG_ASSETS_URL . 'images/form-fields.png' . '" alt="' . esc_attr__('Message Template Form Fields', |
|
| 714 | + 'event_espresso') . '" />'; |
|
| 715 | + $args['img4'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/shortcodes-metabox.png' . '" alt="' . esc_attr__('Shortcodes Metabox', |
|
| 716 | + 'event_espresso') . '" />'; |
|
| 717 | + $args['img5'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/publish-meta-box.png' . '" alt="' . esc_attr__('Publish Metabox', |
|
| 718 | + 'event_espresso') . '" />'; |
|
| 719 | + EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_templates_editor_help_tab.template.php', |
|
| 720 | + $args); |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + |
|
| 724 | + public function message_template_shortcodes_help_tab() |
|
| 725 | + { |
|
| 726 | + $this->_set_shortcodes(); |
|
| 727 | + $args['shortcodes'] = $this->_shortcodes; |
|
| 728 | + EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_shortcodes_help_tab.template.php', |
|
| 729 | + $args); |
|
| 730 | + } |
|
| 731 | 731 | |
| 732 | 732 | |
| 733 | - public function preview_message_help_tab() |
|
| 734 | - { |
|
| 735 | - EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_preview_help_tab.template.php'); |
|
| 736 | - } |
|
| 733 | + public function preview_message_help_tab() |
|
| 734 | + { |
|
| 735 | + EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_preview_help_tab.template.php'); |
|
| 736 | + } |
|
| 737 | 737 | |
| 738 | - |
|
| 739 | - public function settings_help_tab() |
|
| 740 | - { |
|
| 741 | - $args['img1'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-active.png' . '" alt="' . esc_attr__('Active Email Tab', |
|
| 742 | - 'event_espresso') . '" />'; |
|
| 743 | - $args['img2'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-inactive.png' . '" alt="' . esc_attr__('Inactive Email Tab', |
|
| 744 | - 'event_espresso') . '" />'; |
|
| 745 | - $args['img3'] = '<div class="switch"><input id="ee-on-off-toggle-on" class="ee-on-off-toggle ee-toggle-round-flat" type="checkbox" checked="checked"><label for="ee-on-off-toggle-on"></label>'; |
|
| 746 | - $args['img4'] = '<div class="switch"><input id="ee-on-off-toggle-on" class="ee-on-off-toggle ee-toggle-round-flat" type="checkbox"><label for="ee-on-off-toggle-on"></label>'; |
|
| 747 | - EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_settings_help_tab.template.php', $args); |
|
| 748 | - } |
|
| 738 | + |
|
| 739 | + public function settings_help_tab() |
|
| 740 | + { |
|
| 741 | + $args['img1'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-active.png' . '" alt="' . esc_attr__('Active Email Tab', |
|
| 742 | + 'event_espresso') . '" />'; |
|
| 743 | + $args['img2'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-inactive.png' . '" alt="' . esc_attr__('Inactive Email Tab', |
|
| 744 | + 'event_espresso') . '" />'; |
|
| 745 | + $args['img3'] = '<div class="switch"><input id="ee-on-off-toggle-on" class="ee-on-off-toggle ee-toggle-round-flat" type="checkbox" checked="checked"><label for="ee-on-off-toggle-on"></label>'; |
|
| 746 | + $args['img4'] = '<div class="switch"><input id="ee-on-off-toggle-on" class="ee-on-off-toggle ee-toggle-round-flat" type="checkbox"><label for="ee-on-off-toggle-on"></label>'; |
|
| 747 | + EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_settings_help_tab.template.php', $args); |
|
| 748 | + } |
|
| 749 | 749 | |
| 750 | 750 | |
| 751 | - public function load_scripts_styles() |
|
| 752 | - { |
|
| 753 | - wp_register_style('espresso_ee_msg', EE_MSG_ASSETS_URL . 'ee_message_admin.css', EVENT_ESPRESSO_VERSION); |
|
| 754 | - wp_enqueue_style('espresso_ee_msg'); |
|
| 751 | + public function load_scripts_styles() |
|
| 752 | + { |
|
| 753 | + wp_register_style('espresso_ee_msg', EE_MSG_ASSETS_URL . 'ee_message_admin.css', EVENT_ESPRESSO_VERSION); |
|
| 754 | + wp_enqueue_style('espresso_ee_msg'); |
|
| 755 | 755 | |
| 756 | - wp_register_script('ee-messages-settings', EE_MSG_ASSETS_URL . 'ee-messages-settings.js', |
|
| 757 | - array('jquery-ui-droppable', 'ee-serialize-full-array'), EVENT_ESPRESSO_VERSION, true); |
|
| 758 | - wp_register_script('ee-msg-list-table-js', EE_MSG_ASSETS_URL . 'ee_message_admin_list_table.js', |
|
| 759 | - array('ee-dialog'), EVENT_ESPRESSO_VERSION); |
|
| 760 | - } |
|
| 756 | + wp_register_script('ee-messages-settings', EE_MSG_ASSETS_URL . 'ee-messages-settings.js', |
|
| 757 | + array('jquery-ui-droppable', 'ee-serialize-full-array'), EVENT_ESPRESSO_VERSION, true); |
|
| 758 | + wp_register_script('ee-msg-list-table-js', EE_MSG_ASSETS_URL . 'ee_message_admin_list_table.js', |
|
| 759 | + array('ee-dialog'), EVENT_ESPRESSO_VERSION); |
|
| 760 | + } |
|
| 761 | 761 | |
| 762 | 762 | |
| 763 | - public function load_scripts_styles_default() |
|
| 764 | - { |
|
| 765 | - wp_enqueue_script('ee-msg-list-table-js'); |
|
| 766 | - } |
|
| 763 | + public function load_scripts_styles_default() |
|
| 764 | + { |
|
| 765 | + wp_enqueue_script('ee-msg-list-table-js'); |
|
| 766 | + } |
|
| 767 | 767 | |
| 768 | 768 | |
| 769 | - public function wp_editor_css($mce_css) |
|
| 770 | - { |
|
| 771 | - //if we're on the edit_message_template route |
|
| 772 | - if ($this->_req_action == 'edit_message_template' && $this->_active_messenger instanceof EE_messenger) { |
|
| 773 | - $message_type_name = $this->_active_message_type_name; |
|
| 769 | + public function wp_editor_css($mce_css) |
|
| 770 | + { |
|
| 771 | + //if we're on the edit_message_template route |
|
| 772 | + if ($this->_req_action == 'edit_message_template' && $this->_active_messenger instanceof EE_messenger) { |
|
| 773 | + $message_type_name = $this->_active_message_type_name; |
|
| 774 | 774 | |
| 775 | - //we're going to REPLACE the existing mce css |
|
| 776 | - //we need to get the css file location from the active messenger |
|
| 777 | - $mce_css = $this->_active_messenger->get_variation($this->_template_pack, $message_type_name, true, |
|
| 778 | - 'wpeditor', $this->_variation); |
|
| 779 | - } |
|
| 775 | + //we're going to REPLACE the existing mce css |
|
| 776 | + //we need to get the css file location from the active messenger |
|
| 777 | + $mce_css = $this->_active_messenger->get_variation($this->_template_pack, $message_type_name, true, |
|
| 778 | + 'wpeditor', $this->_variation); |
|
| 779 | + } |
|
| 780 | 780 | |
| 781 | - return $mce_css; |
|
| 782 | - } |
|
| 781 | + return $mce_css; |
|
| 782 | + } |
|
| 783 | 783 | |
| 784 | 784 | |
| 785 | - public function load_scripts_styles_edit_message_template() |
|
| 786 | - { |
|
| 785 | + public function load_scripts_styles_edit_message_template() |
|
| 786 | + { |
|
| 787 | 787 | |
| 788 | - $this->_set_shortcodes(); |
|
| 788 | + $this->_set_shortcodes(); |
|
| 789 | 789 | |
| 790 | - EE_Registry::$i18n_js_strings['confirm_default_reset'] = sprintf( |
|
| 791 | - __('Are you sure you want to reset the %s %s message templates? Remember continuing will reset the templates for all contexts in this messenger and message type group.', |
|
| 792 | - 'event_espresso'), |
|
| 793 | - $this->_message_template_group->messenger_obj()->label['singular'], |
|
| 794 | - $this->_message_template_group->message_type_obj()->label['singular'] |
|
| 795 | - ); |
|
| 796 | - EE_Registry::$i18n_js_strings['confirm_switch_template_pack'] = __('Switching the template pack for a messages template will reset the content for the template so the new layout is loaded. Any custom content in the existing template will be lost. Are you sure you wish to do this?', |
|
| 797 | - 'event_espresso'); |
|
| 790 | + EE_Registry::$i18n_js_strings['confirm_default_reset'] = sprintf( |
|
| 791 | + __('Are you sure you want to reset the %s %s message templates? Remember continuing will reset the templates for all contexts in this messenger and message type group.', |
|
| 792 | + 'event_espresso'), |
|
| 793 | + $this->_message_template_group->messenger_obj()->label['singular'], |
|
| 794 | + $this->_message_template_group->message_type_obj()->label['singular'] |
|
| 795 | + ); |
|
| 796 | + EE_Registry::$i18n_js_strings['confirm_switch_template_pack'] = __('Switching the template pack for a messages template will reset the content for the template so the new layout is loaded. Any custom content in the existing template will be lost. Are you sure you wish to do this?', |
|
| 797 | + 'event_espresso'); |
|
| 798 | 798 | |
| 799 | - wp_register_script('ee_msgs_edit_js', EE_MSG_ASSETS_URL . 'ee_message_editor.js', array('jquery'), |
|
| 800 | - EVENT_ESPRESSO_VERSION); |
|
| 799 | + wp_register_script('ee_msgs_edit_js', EE_MSG_ASSETS_URL . 'ee_message_editor.js', array('jquery'), |
|
| 800 | + EVENT_ESPRESSO_VERSION); |
|
| 801 | 801 | |
| 802 | - wp_enqueue_script('ee_admin_js'); |
|
| 803 | - wp_enqueue_script('ee_msgs_edit_js'); |
|
| 802 | + wp_enqueue_script('ee_admin_js'); |
|
| 803 | + wp_enqueue_script('ee_msgs_edit_js'); |
|
| 804 | 804 | |
| 805 | - //add in special css for tiny_mce |
|
| 806 | - add_filter('mce_css', array($this, 'wp_editor_css')); |
|
| 807 | - } |
|
| 805 | + //add in special css for tiny_mce |
|
| 806 | + add_filter('mce_css', array($this, 'wp_editor_css')); |
|
| 807 | + } |
|
| 808 | 808 | |
| 809 | 809 | |
| 810 | - public function load_scripts_styles_display_preview_message() |
|
| 811 | - { |
|
| 810 | + public function load_scripts_styles_display_preview_message() |
|
| 811 | + { |
|
| 812 | 812 | |
| 813 | - $this->_set_message_template_group(); |
|
| 813 | + $this->_set_message_template_group(); |
|
| 814 | 814 | |
| 815 | - if (isset($this->_req_data['messenger'])) { |
|
| 816 | - $this->_active_messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']); |
|
| 817 | - } |
|
| 815 | + if (isset($this->_req_data['messenger'])) { |
|
| 816 | + $this->_active_messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']); |
|
| 817 | + } |
|
| 818 | 818 | |
| 819 | - $message_type_name = isset($this->_req_data['message_type']) ? $this->_req_data['message_type'] : ''; |
|
| 819 | + $message_type_name = isset($this->_req_data['message_type']) ? $this->_req_data['message_type'] : ''; |
|
| 820 | 820 | |
| 821 | 821 | |
| 822 | - wp_enqueue_style('espresso_preview_css', |
|
| 823 | - $this->_active_messenger->get_variation($this->_template_pack, $message_type_name, true, 'preview', |
|
| 824 | - $this->_variation)); |
|
| 825 | - } |
|
| 822 | + wp_enqueue_style('espresso_preview_css', |
|
| 823 | + $this->_active_messenger->get_variation($this->_template_pack, $message_type_name, true, 'preview', |
|
| 824 | + $this->_variation)); |
|
| 825 | + } |
|
| 826 | 826 | |
| 827 | 827 | |
| 828 | - public function load_scripts_styles_settings() |
|
| 829 | - { |
|
| 830 | - wp_register_style('ee-message-settings', EE_MSG_ASSETS_URL . 'ee_message_settings.css', array(), |
|
| 831 | - EVENT_ESPRESSO_VERSION); |
|
| 832 | - wp_enqueue_style('ee-text-links'); |
|
| 833 | - wp_enqueue_style('ee-message-settings'); |
|
| 828 | + public function load_scripts_styles_settings() |
|
| 829 | + { |
|
| 830 | + wp_register_style('ee-message-settings', EE_MSG_ASSETS_URL . 'ee_message_settings.css', array(), |
|
| 831 | + EVENT_ESPRESSO_VERSION); |
|
| 832 | + wp_enqueue_style('ee-text-links'); |
|
| 833 | + wp_enqueue_style('ee-message-settings'); |
|
| 834 | 834 | |
| 835 | - wp_enqueue_script('ee-messages-settings'); |
|
| 836 | - } |
|
| 835 | + wp_enqueue_script('ee-messages-settings'); |
|
| 836 | + } |
|
| 837 | 837 | |
| 838 | 838 | |
| 839 | - /** |
|
| 840 | - * set views array for List Table |
|
| 841 | - */ |
|
| 842 | - public function _set_list_table_views_global_mtps() |
|
| 843 | - { |
|
| 844 | - $this->_views = array( |
|
| 845 | - 'in_use' => array( |
|
| 846 | - 'slug' => 'in_use', |
|
| 847 | - 'label' => __('In Use', 'event_espresso'), |
|
| 848 | - 'count' => 0, |
|
| 849 | - ) |
|
| 850 | - ); |
|
| 851 | - } |
|
| 839 | + /** |
|
| 840 | + * set views array for List Table |
|
| 841 | + */ |
|
| 842 | + public function _set_list_table_views_global_mtps() |
|
| 843 | + { |
|
| 844 | + $this->_views = array( |
|
| 845 | + 'in_use' => array( |
|
| 846 | + 'slug' => 'in_use', |
|
| 847 | + 'label' => __('In Use', 'event_espresso'), |
|
| 848 | + 'count' => 0, |
|
| 849 | + ) |
|
| 850 | + ); |
|
| 851 | + } |
|
| 852 | 852 | |
| 853 | 853 | |
| 854 | - /** |
|
| 855 | - * Set views array for the Custom Template List Table |
|
| 856 | - */ |
|
| 857 | - public function _set_list_table_views_custom_mtps() |
|
| 858 | - { |
|
| 859 | - $this->_set_list_table_views_global_mtps(); |
|
| 860 | - $this->_views['in_use']['bulk_action'] = array( |
|
| 861 | - 'trash_message_template' => esc_html__('Move to Trash', 'event_espresso') |
|
| 862 | - ); |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - |
|
| 866 | - /** |
|
| 867 | - * set views array for message queue list table |
|
| 868 | - */ |
|
| 869 | - public function _set_list_table_views_default() |
|
| 870 | - { |
|
| 871 | - EE_Registry::instance()->load_helper('Template'); |
|
| 872 | - |
|
| 873 | - $common_bulk_actions = EE_Registry::instance()->CAP->current_user_can('ee_send_message', |
|
| 874 | - 'message_list_table_bulk_actions') |
|
| 875 | - ? array( |
|
| 876 | - 'generate_now' => __('Generate Now', 'event_espresso'), |
|
| 877 | - 'generate_and_send_now' => __('Generate and Send Now', 'event_espresso'), |
|
| 878 | - 'queue_for_resending' => __('Queue for Resending', 'event_espresso'), |
|
| 879 | - 'send_now' => __('Send Now', 'event_espresso') |
|
| 880 | - ) |
|
| 881 | - : array(); |
|
| 882 | - |
|
| 883 | - $delete_bulk_action = EE_Registry::instance()->CAP->current_user_can('ee_delete_messages', |
|
| 884 | - 'message_list_table_bulk_actions') |
|
| 885 | - ? array('delete_ee_messages' => __('Delete Messages', 'event_espresso')) |
|
| 886 | - : array(); |
|
| 887 | - |
|
| 888 | - |
|
| 889 | - $this->_views = array( |
|
| 890 | - 'all' => array( |
|
| 891 | - 'slug' => 'all', |
|
| 892 | - 'label' => __('All', 'event_espresso'), |
|
| 893 | - 'count' => 0, |
|
| 894 | - 'bulk_action' => array_merge($common_bulk_actions, $delete_bulk_action) |
|
| 895 | - ) |
|
| 896 | - ); |
|
| 897 | - |
|
| 898 | - |
|
| 899 | - foreach (EEM_Message::instance()->all_statuses() as $status) { |
|
| 900 | - if ($status === EEM_Message::status_debug_only && ! EEM_Message::debug()) { |
|
| 901 | - continue; |
|
| 902 | - } |
|
| 903 | - $status_bulk_actions = $common_bulk_actions; |
|
| 904 | - //unset bulk actions not applying to status |
|
| 905 | - if (! empty($status_bulk_actions)) { |
|
| 906 | - switch ($status) { |
|
| 907 | - case EEM_Message::status_idle: |
|
| 908 | - case EEM_Message::status_resend: |
|
| 909 | - $status_bulk_actions['send_now'] = $common_bulk_actions['send_now']; |
|
| 910 | - break; |
|
| 854 | + /** |
|
| 855 | + * Set views array for the Custom Template List Table |
|
| 856 | + */ |
|
| 857 | + public function _set_list_table_views_custom_mtps() |
|
| 858 | + { |
|
| 859 | + $this->_set_list_table_views_global_mtps(); |
|
| 860 | + $this->_views['in_use']['bulk_action'] = array( |
|
| 861 | + 'trash_message_template' => esc_html__('Move to Trash', 'event_espresso') |
|
| 862 | + ); |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + |
|
| 866 | + /** |
|
| 867 | + * set views array for message queue list table |
|
| 868 | + */ |
|
| 869 | + public function _set_list_table_views_default() |
|
| 870 | + { |
|
| 871 | + EE_Registry::instance()->load_helper('Template'); |
|
| 872 | + |
|
| 873 | + $common_bulk_actions = EE_Registry::instance()->CAP->current_user_can('ee_send_message', |
|
| 874 | + 'message_list_table_bulk_actions') |
|
| 875 | + ? array( |
|
| 876 | + 'generate_now' => __('Generate Now', 'event_espresso'), |
|
| 877 | + 'generate_and_send_now' => __('Generate and Send Now', 'event_espresso'), |
|
| 878 | + 'queue_for_resending' => __('Queue for Resending', 'event_espresso'), |
|
| 879 | + 'send_now' => __('Send Now', 'event_espresso') |
|
| 880 | + ) |
|
| 881 | + : array(); |
|
| 882 | + |
|
| 883 | + $delete_bulk_action = EE_Registry::instance()->CAP->current_user_can('ee_delete_messages', |
|
| 884 | + 'message_list_table_bulk_actions') |
|
| 885 | + ? array('delete_ee_messages' => __('Delete Messages', 'event_espresso')) |
|
| 886 | + : array(); |
|
| 887 | + |
|
| 888 | + |
|
| 889 | + $this->_views = array( |
|
| 890 | + 'all' => array( |
|
| 891 | + 'slug' => 'all', |
|
| 892 | + 'label' => __('All', 'event_espresso'), |
|
| 893 | + 'count' => 0, |
|
| 894 | + 'bulk_action' => array_merge($common_bulk_actions, $delete_bulk_action) |
|
| 895 | + ) |
|
| 896 | + ); |
|
| 897 | + |
|
| 898 | + |
|
| 899 | + foreach (EEM_Message::instance()->all_statuses() as $status) { |
|
| 900 | + if ($status === EEM_Message::status_debug_only && ! EEM_Message::debug()) { |
|
| 901 | + continue; |
|
| 902 | + } |
|
| 903 | + $status_bulk_actions = $common_bulk_actions; |
|
| 904 | + //unset bulk actions not applying to status |
|
| 905 | + if (! empty($status_bulk_actions)) { |
|
| 906 | + switch ($status) { |
|
| 907 | + case EEM_Message::status_idle: |
|
| 908 | + case EEM_Message::status_resend: |
|
| 909 | + $status_bulk_actions['send_now'] = $common_bulk_actions['send_now']; |
|
| 910 | + break; |
|
| 911 | 911 | |
| 912 | - case EEM_Message::status_failed: |
|
| 913 | - case EEM_Message::status_debug_only: |
|
| 914 | - case EEM_Message::status_messenger_executing: |
|
| 915 | - $status_bulk_actions = array(); |
|
| 916 | - break; |
|
| 912 | + case EEM_Message::status_failed: |
|
| 913 | + case EEM_Message::status_debug_only: |
|
| 914 | + case EEM_Message::status_messenger_executing: |
|
| 915 | + $status_bulk_actions = array(); |
|
| 916 | + break; |
|
| 917 | 917 | |
| 918 | - case EEM_Message::status_incomplete: |
|
| 919 | - unset($status_bulk_actions['queue_for_resending'], $status_bulk_actions['send_now']); |
|
| 920 | - break; |
|
| 918 | + case EEM_Message::status_incomplete: |
|
| 919 | + unset($status_bulk_actions['queue_for_resending'], $status_bulk_actions['send_now']); |
|
| 920 | + break; |
|
| 921 | 921 | |
| 922 | - case EEM_Message::status_retry: |
|
| 923 | - case EEM_Message::status_sent: |
|
| 924 | - unset($status_bulk_actions['generate_now'], $status_bulk_actions['generate_and_send_now']); |
|
| 925 | - break; |
|
| 926 | - } |
|
| 927 | - } |
|
| 922 | + case EEM_Message::status_retry: |
|
| 923 | + case EEM_Message::status_sent: |
|
| 924 | + unset($status_bulk_actions['generate_now'], $status_bulk_actions['generate_and_send_now']); |
|
| 925 | + break; |
|
| 926 | + } |
|
| 927 | + } |
|
| 928 | 928 | |
| 929 | - //skip adding messenger executing status to views because it will be included with the Failed view. |
|
| 930 | - if ( $status === EEM_Message::status_messenger_executing ) { |
|
| 931 | - continue; |
|
| 932 | - } |
|
| 929 | + //skip adding messenger executing status to views because it will be included with the Failed view. |
|
| 930 | + if ( $status === EEM_Message::status_messenger_executing ) { |
|
| 931 | + continue; |
|
| 932 | + } |
|
| 933 | 933 | |
| 934 | - $this->_views[strtolower($status)] = array( |
|
| 935 | - 'slug' => strtolower($status), |
|
| 936 | - 'label' => EEH_Template::pretty_status($status, false, 'sentence'), |
|
| 937 | - 'count' => 0, |
|
| 938 | - 'bulk_action' => array_merge($status_bulk_actions, $delete_bulk_action) |
|
| 939 | - ); |
|
| 940 | - } |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - |
|
| 944 | - protected function _ee_default_messages_overview_list_table() |
|
| 945 | - { |
|
| 946 | - $this->_admin_page_title = __('Default Message Templates', 'event_espresso'); |
|
| 947 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 948 | - } |
|
| 949 | - |
|
| 950 | - |
|
| 951 | - protected function _message_queue_list_table() |
|
| 952 | - { |
|
| 953 | - $this->_search_btn_label = __('Message Activity', 'event_espresso'); |
|
| 954 | - $this->_template_args['per_column'] = 6; |
|
| 955 | - $this->_template_args['after_list_table'] = $this->_display_legend($this->_message_legend_items()); |
|
| 956 | - $this->_template_args['before_list_table'] = '<h3>' . EEM_Message::instance()->get_pretty_label_for_results() . '</h3>'; |
|
| 957 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 958 | - } |
|
| 959 | - |
|
| 960 | - |
|
| 961 | - protected function _message_legend_items() |
|
| 962 | - { |
|
| 963 | - |
|
| 964 | - $action_css_classes = EEH_MSG_Template::get_message_action_icons(); |
|
| 965 | - $action_items = array(); |
|
| 966 | - |
|
| 967 | - foreach ($action_css_classes as $action_item => $action_details) { |
|
| 968 | - if ($action_item === 'see_notifications_for') { |
|
| 969 | - continue; |
|
| 970 | - } |
|
| 971 | - $action_items[$action_item] = array( |
|
| 972 | - 'class' => $action_details['css_class'], |
|
| 973 | - 'desc' => $action_details['label'] |
|
| 974 | - ); |
|
| 975 | - } |
|
| 976 | - |
|
| 977 | - /** @type array $status_items status legend setup */ |
|
| 978 | - $status_items = array( |
|
| 979 | - 'sent_status' => array( |
|
| 980 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_sent, |
|
| 981 | - 'desc' => EEH_Template::pretty_status(EEM_Message::status_sent, false, 'sentence') |
|
| 982 | - ), |
|
| 983 | - 'idle_status' => array( |
|
| 984 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_idle, |
|
| 985 | - 'desc' => EEH_Template::pretty_status(EEM_Message::status_idle, false, 'sentence') |
|
| 986 | - ), |
|
| 987 | - 'failed_status' => array( |
|
| 988 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_failed, |
|
| 989 | - 'desc' => EEH_Template::pretty_status(EEM_Message::status_failed, false, 'sentence') |
|
| 990 | - ), |
|
| 991 | - 'messenger_executing_status' => array( |
|
| 992 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_messenger_executing, |
|
| 993 | - 'desc' => EEH_Template::pretty_status(EEM_Message::status_messenger_executing, false, 'sentence') |
|
| 994 | - ), |
|
| 995 | - 'resend_status' => array( |
|
| 996 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_resend, |
|
| 997 | - 'desc' => EEH_Template::pretty_status(EEM_Message::status_resend, false, 'sentence') |
|
| 998 | - ), |
|
| 999 | - 'incomplete_status' => array( |
|
| 1000 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_incomplete, |
|
| 1001 | - 'desc' => EEH_Template::pretty_status(EEM_Message::status_incomplete, false, 'sentence') |
|
| 1002 | - ), |
|
| 1003 | - 'retry_status' => array( |
|
| 1004 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_retry, |
|
| 1005 | - 'desc' => EEH_Template::pretty_status(EEM_Message::status_retry, false, 'sentence') |
|
| 1006 | - ) |
|
| 1007 | - ); |
|
| 1008 | - if (EEM_Message::debug()) { |
|
| 1009 | - $status_items['debug_only_status'] = array( |
|
| 1010 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_debug_only, |
|
| 1011 | - 'desc' => EEH_Template::pretty_status(EEM_Message::status_debug_only, false, 'sentence') |
|
| 1012 | - ); |
|
| 1013 | - } |
|
| 1014 | - |
|
| 1015 | - return array_merge($action_items, $status_items); |
|
| 1016 | - } |
|
| 1017 | - |
|
| 1018 | - |
|
| 1019 | - protected function _custom_mtps_preview() |
|
| 1020 | - { |
|
| 1021 | - $this->_admin_page_title = __('Custom Message Templates (Preview)', 'event_espresso'); |
|
| 1022 | - $this->_template_args['preview_img'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/custom_mtps_preview.png" alt="' . esc_attr__('Preview Custom Message Templates screenshot', |
|
| 1023 | - 'event_espresso') . '" />'; |
|
| 1024 | - $this->_template_args['preview_text'] = '<strong>' . __('Custom Message Templates is a feature that is only available in the premium version of Event Espresso 4 which is available with a support license purchase on EventEspresso.com. With the Custom Message Templates feature, you are able to create custom message templates and assign them on a per-event basis.', |
|
| 1025 | - 'event_espresso') . '</strong>'; |
|
| 1026 | - $this->display_admin_caf_preview_page('custom_message_types', false); |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - |
|
| 1030 | - /** |
|
| 1031 | - * get_message_templates |
|
| 1032 | - * This gets all the message templates for listing on the overview list. |
|
| 1033 | - * |
|
| 1034 | - * @access public |
|
| 1035 | - * |
|
| 1036 | - * @param int $perpage the amount of templates groups to show per page |
|
| 1037 | - * @param string $type the current _view we're getting templates for |
|
| 1038 | - * @param bool $count return count? |
|
| 1039 | - * @param bool $all disregard any paging info (get all data); |
|
| 1040 | - * @param bool $global whether to return just global (true) or custom templates (false) |
|
| 1041 | - * |
|
| 1042 | - * @return array |
|
| 1043 | - */ |
|
| 1044 | - public function get_message_templates($perpage = 10, $type = 'in_use', $count = false, $all = false, $global = true) |
|
| 1045 | - { |
|
| 1046 | - |
|
| 1047 | - $MTP = EEM_Message_Template_Group::instance(); |
|
| 1048 | - |
|
| 1049 | - $this->_req_data['orderby'] = empty($this->_req_data['orderby']) ? 'GRP_ID' : $this->_req_data['orderby']; |
|
| 1050 | - $orderby = $this->_req_data['orderby']; |
|
| 1051 | - |
|
| 1052 | - $order = (isset($this->_req_data['order']) && ! empty($this->_req_data['order'])) ? $this->_req_data['order'] : 'ASC'; |
|
| 1053 | - |
|
| 1054 | - $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged']) ? $this->_req_data['paged'] : 1; |
|
| 1055 | - $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage']) ? $this->_req_data['perpage'] : $perpage; |
|
| 1056 | - |
|
| 1057 | - $offset = ($current_page - 1) * $per_page; |
|
| 1058 | - $limit = $all ? null : array($offset, $per_page); |
|
| 1059 | - |
|
| 1060 | - |
|
| 1061 | - //options will match what is in the _views array property |
|
| 1062 | - switch ($type) { |
|
| 934 | + $this->_views[strtolower($status)] = array( |
|
| 935 | + 'slug' => strtolower($status), |
|
| 936 | + 'label' => EEH_Template::pretty_status($status, false, 'sentence'), |
|
| 937 | + 'count' => 0, |
|
| 938 | + 'bulk_action' => array_merge($status_bulk_actions, $delete_bulk_action) |
|
| 939 | + ); |
|
| 940 | + } |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + |
|
| 944 | + protected function _ee_default_messages_overview_list_table() |
|
| 945 | + { |
|
| 946 | + $this->_admin_page_title = __('Default Message Templates', 'event_espresso'); |
|
| 947 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 948 | + } |
|
| 949 | + |
|
| 950 | + |
|
| 951 | + protected function _message_queue_list_table() |
|
| 952 | + { |
|
| 953 | + $this->_search_btn_label = __('Message Activity', 'event_espresso'); |
|
| 954 | + $this->_template_args['per_column'] = 6; |
|
| 955 | + $this->_template_args['after_list_table'] = $this->_display_legend($this->_message_legend_items()); |
|
| 956 | + $this->_template_args['before_list_table'] = '<h3>' . EEM_Message::instance()->get_pretty_label_for_results() . '</h3>'; |
|
| 957 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 958 | + } |
|
| 959 | + |
|
| 960 | + |
|
| 961 | + protected function _message_legend_items() |
|
| 962 | + { |
|
| 963 | + |
|
| 964 | + $action_css_classes = EEH_MSG_Template::get_message_action_icons(); |
|
| 965 | + $action_items = array(); |
|
| 966 | + |
|
| 967 | + foreach ($action_css_classes as $action_item => $action_details) { |
|
| 968 | + if ($action_item === 'see_notifications_for') { |
|
| 969 | + continue; |
|
| 970 | + } |
|
| 971 | + $action_items[$action_item] = array( |
|
| 972 | + 'class' => $action_details['css_class'], |
|
| 973 | + 'desc' => $action_details['label'] |
|
| 974 | + ); |
|
| 975 | + } |
|
| 976 | + |
|
| 977 | + /** @type array $status_items status legend setup */ |
|
| 978 | + $status_items = array( |
|
| 979 | + 'sent_status' => array( |
|
| 980 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_sent, |
|
| 981 | + 'desc' => EEH_Template::pretty_status(EEM_Message::status_sent, false, 'sentence') |
|
| 982 | + ), |
|
| 983 | + 'idle_status' => array( |
|
| 984 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_idle, |
|
| 985 | + 'desc' => EEH_Template::pretty_status(EEM_Message::status_idle, false, 'sentence') |
|
| 986 | + ), |
|
| 987 | + 'failed_status' => array( |
|
| 988 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_failed, |
|
| 989 | + 'desc' => EEH_Template::pretty_status(EEM_Message::status_failed, false, 'sentence') |
|
| 990 | + ), |
|
| 991 | + 'messenger_executing_status' => array( |
|
| 992 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_messenger_executing, |
|
| 993 | + 'desc' => EEH_Template::pretty_status(EEM_Message::status_messenger_executing, false, 'sentence') |
|
| 994 | + ), |
|
| 995 | + 'resend_status' => array( |
|
| 996 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_resend, |
|
| 997 | + 'desc' => EEH_Template::pretty_status(EEM_Message::status_resend, false, 'sentence') |
|
| 998 | + ), |
|
| 999 | + 'incomplete_status' => array( |
|
| 1000 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_incomplete, |
|
| 1001 | + 'desc' => EEH_Template::pretty_status(EEM_Message::status_incomplete, false, 'sentence') |
|
| 1002 | + ), |
|
| 1003 | + 'retry_status' => array( |
|
| 1004 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_retry, |
|
| 1005 | + 'desc' => EEH_Template::pretty_status(EEM_Message::status_retry, false, 'sentence') |
|
| 1006 | + ) |
|
| 1007 | + ); |
|
| 1008 | + if (EEM_Message::debug()) { |
|
| 1009 | + $status_items['debug_only_status'] = array( |
|
| 1010 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_debug_only, |
|
| 1011 | + 'desc' => EEH_Template::pretty_status(EEM_Message::status_debug_only, false, 'sentence') |
|
| 1012 | + ); |
|
| 1013 | + } |
|
| 1014 | + |
|
| 1015 | + return array_merge($action_items, $status_items); |
|
| 1016 | + } |
|
| 1017 | + |
|
| 1018 | + |
|
| 1019 | + protected function _custom_mtps_preview() |
|
| 1020 | + { |
|
| 1021 | + $this->_admin_page_title = __('Custom Message Templates (Preview)', 'event_espresso'); |
|
| 1022 | + $this->_template_args['preview_img'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/custom_mtps_preview.png" alt="' . esc_attr__('Preview Custom Message Templates screenshot', |
|
| 1023 | + 'event_espresso') . '" />'; |
|
| 1024 | + $this->_template_args['preview_text'] = '<strong>' . __('Custom Message Templates is a feature that is only available in the premium version of Event Espresso 4 which is available with a support license purchase on EventEspresso.com. With the Custom Message Templates feature, you are able to create custom message templates and assign them on a per-event basis.', |
|
| 1025 | + 'event_espresso') . '</strong>'; |
|
| 1026 | + $this->display_admin_caf_preview_page('custom_message_types', false); |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + |
|
| 1030 | + /** |
|
| 1031 | + * get_message_templates |
|
| 1032 | + * This gets all the message templates for listing on the overview list. |
|
| 1033 | + * |
|
| 1034 | + * @access public |
|
| 1035 | + * |
|
| 1036 | + * @param int $perpage the amount of templates groups to show per page |
|
| 1037 | + * @param string $type the current _view we're getting templates for |
|
| 1038 | + * @param bool $count return count? |
|
| 1039 | + * @param bool $all disregard any paging info (get all data); |
|
| 1040 | + * @param bool $global whether to return just global (true) or custom templates (false) |
|
| 1041 | + * |
|
| 1042 | + * @return array |
|
| 1043 | + */ |
|
| 1044 | + public function get_message_templates($perpage = 10, $type = 'in_use', $count = false, $all = false, $global = true) |
|
| 1045 | + { |
|
| 1046 | + |
|
| 1047 | + $MTP = EEM_Message_Template_Group::instance(); |
|
| 1048 | + |
|
| 1049 | + $this->_req_data['orderby'] = empty($this->_req_data['orderby']) ? 'GRP_ID' : $this->_req_data['orderby']; |
|
| 1050 | + $orderby = $this->_req_data['orderby']; |
|
| 1051 | + |
|
| 1052 | + $order = (isset($this->_req_data['order']) && ! empty($this->_req_data['order'])) ? $this->_req_data['order'] : 'ASC'; |
|
| 1053 | + |
|
| 1054 | + $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged']) ? $this->_req_data['paged'] : 1; |
|
| 1055 | + $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage']) ? $this->_req_data['perpage'] : $perpage; |
|
| 1056 | + |
|
| 1057 | + $offset = ($current_page - 1) * $per_page; |
|
| 1058 | + $limit = $all ? null : array($offset, $per_page); |
|
| 1059 | + |
|
| 1060 | + |
|
| 1061 | + //options will match what is in the _views array property |
|
| 1062 | + switch ($type) { |
|
| 1063 | 1063 | |
| 1064 | - case 'in_use': |
|
| 1065 | - $templates = $MTP->get_all_active_message_templates($orderby, $order, $limit, $count, $global, true); |
|
| 1066 | - break; |
|
| 1064 | + case 'in_use': |
|
| 1065 | + $templates = $MTP->get_all_active_message_templates($orderby, $order, $limit, $count, $global, true); |
|
| 1066 | + break; |
|
| 1067 | 1067 | |
| 1068 | - default: |
|
| 1069 | - $templates = $MTP->get_all_trashed_grouped_message_templates($orderby, $order, $limit, $count, $global); |
|
| 1068 | + default: |
|
| 1069 | + $templates = $MTP->get_all_trashed_grouped_message_templates($orderby, $order, $limit, $count, $global); |
|
| 1070 | 1070 | |
| 1071 | - } |
|
| 1072 | - |
|
| 1073 | - return $templates; |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - |
|
| 1077 | - /** |
|
| 1078 | - * filters etc might need a list of installed message_types |
|
| 1079 | - * @return array an array of message type objects |
|
| 1080 | - */ |
|
| 1081 | - public function get_installed_message_types() |
|
| 1082 | - { |
|
| 1083 | - $installed_message_types = $this->_message_resource_manager->installed_message_types(); |
|
| 1084 | - $installed = array(); |
|
| 1085 | - |
|
| 1086 | - foreach ($installed_message_types as $message_type) { |
|
| 1087 | - $installed[$message_type->name] = $message_type; |
|
| 1088 | - } |
|
| 1089 | - |
|
| 1090 | - return $installed; |
|
| 1091 | - } |
|
| 1092 | - |
|
| 1093 | - |
|
| 1094 | - /** |
|
| 1095 | - * _add_message_template |
|
| 1096 | - * |
|
| 1097 | - * This is used when creating a custom template. All Custom Templates start based off another template. |
|
| 1098 | - * |
|
| 1099 | - * @param string $message_type |
|
| 1100 | - * @param string $messenger |
|
| 1101 | - * @param string $GRP_ID |
|
| 1102 | - * |
|
| 1103 | - * @throws EE_error |
|
| 1104 | - */ |
|
| 1105 | - protected function _add_message_template($message_type = '', $messenger = '', $GRP_ID = '') |
|
| 1106 | - { |
|
| 1107 | - //set values override any request data |
|
| 1108 | - $message_type = ! empty($message_type) ? $message_type : ''; |
|
| 1109 | - $message_type = empty($message_type) && ! empty($this->_req_data['message_type']) ? $this->_req_data['message_type'] : $message_type; |
|
| 1110 | - |
|
| 1111 | - $messenger = ! empty($messenger) ? $messenger : ''; |
|
| 1112 | - $messenger = empty($messenger) && ! empty($this->_req_data['messenger']) ? $this->_req_data['messenger'] : $messenger; |
|
| 1113 | - |
|
| 1114 | - $GRP_ID = ! empty($GRP_ID) ? $GRP_ID : ''; |
|
| 1115 | - $GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : $GRP_ID; |
|
| 1116 | - |
|
| 1117 | - //we need messenger and message type. They should be coming from the event editor. If not here then return error |
|
| 1118 | - if (empty($message_type) || empty($messenger)) { |
|
| 1119 | - throw new EE_error(__('Sorry, but we can\'t create new templates because we\'re missing the messenger or message type', |
|
| 1120 | - 'event_espresso')); |
|
| 1121 | - } |
|
| 1122 | - |
|
| 1123 | - //we need the GRP_ID for the template being used as the base for the new template |
|
| 1124 | - if (empty($GRP_ID)) { |
|
| 1125 | - throw new EE_Error(__('In order to create a custom message template the GRP_ID of the template being used as a base is needed', |
|
| 1126 | - 'event_espresso')); |
|
| 1127 | - } |
|
| 1128 | - |
|
| 1129 | - //let's just make sure the template gets generated! |
|
| 1130 | - |
|
| 1131 | - //we need to reassign some variables for what the insert is expecting |
|
| 1132 | - $this->_req_data['MTP_messenger'] = $messenger; |
|
| 1133 | - $this->_req_data['MTP_message_type'] = $message_type; |
|
| 1134 | - $this->_req_data['GRP_ID'] = $GRP_ID; |
|
| 1135 | - $this->_insert_or_update_message_template(true); |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - |
|
| 1139 | - /** |
|
| 1140 | - * public wrapper for the _add_message_template method |
|
| 1141 | - * |
|
| 1142 | - * @param string $message_type message type slug |
|
| 1143 | - * @param string $messenger messenger slug |
|
| 1144 | - * @param int $GRP_ID GRP_ID for the related message template group this new template will be based |
|
| 1145 | - * off of. |
|
| 1146 | - */ |
|
| 1147 | - public function add_message_template($message_type, $messenger, $GRP_ID) |
|
| 1148 | - { |
|
| 1149 | - $this->_add_message_template($message_type, $messenger, $GRP_ID); |
|
| 1150 | - } |
|
| 1151 | - |
|
| 1152 | - |
|
| 1153 | - /** |
|
| 1154 | - * _edit_message_template |
|
| 1155 | - * |
|
| 1156 | - * @access protected |
|
| 1157 | - * @return void |
|
| 1158 | - */ |
|
| 1159 | - protected function _edit_message_template() |
|
| 1160 | - { |
|
| 1161 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 1162 | - $template_fields = ''; |
|
| 1163 | - $sidebar_fields = ''; |
|
| 1164 | - //we filter the tinyMCE settings to remove the validation since message templates by their nature will not have valid html in the templates. |
|
| 1165 | - add_filter('tiny_mce_before_init', array($this, 'filter_tinymce_init'), 10, 2); |
|
| 1166 | - |
|
| 1167 | - $GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id']) |
|
| 1168 | - ? absint($this->_req_data['id']) |
|
| 1169 | - : false; |
|
| 1170 | - |
|
| 1171 | - $this->_set_shortcodes(); //this also sets the _message_template property. |
|
| 1172 | - $message_template_group = $this->_message_template_group; |
|
| 1173 | - $c_label = $message_template_group->context_label(); |
|
| 1174 | - $c_config = $message_template_group->contexts_config(); |
|
| 1175 | - |
|
| 1176 | - reset($c_config); |
|
| 1177 | - $context = isset($this->_req_data['context']) && ! empty($this->_req_data['context']) |
|
| 1178 | - ? strtolower($this->_req_data['context']) |
|
| 1179 | - : key($c_config); |
|
| 1180 | - |
|
| 1181 | - |
|
| 1182 | - if (empty($GRP_ID)) { |
|
| 1183 | - $action = 'insert_message_template'; |
|
| 1184 | - //$button_both = false; |
|
| 1185 | - //$button_text = array( __( 'Save','event_espresso') ); |
|
| 1186 | - //$button_actions = array('something_different'); |
|
| 1187 | - //$referrer = false; |
|
| 1188 | - $edit_message_template_form_url = add_query_arg( |
|
| 1189 | - array('action' => $action, 'noheader' => true), |
|
| 1190 | - EE_MSG_ADMIN_URL |
|
| 1191 | - ); |
|
| 1192 | - } else { |
|
| 1193 | - $action = 'update_message_template'; |
|
| 1194 | - //$button_both = true; |
|
| 1195 | - //$button_text = array(); |
|
| 1196 | - //$button_actions = array(); |
|
| 1197 | - //$referrer = $this->_admin_base_url; |
|
| 1198 | - $edit_message_template_form_url = add_query_arg( |
|
| 1199 | - array('action' => $action, 'noheader' => true), |
|
| 1200 | - EE_MSG_ADMIN_URL |
|
| 1201 | - ); |
|
| 1202 | - } |
|
| 1203 | - |
|
| 1204 | - //set active messenger for this view |
|
| 1205 | - $this->_active_messenger = $this->_message_resource_manager->get_active_messenger( |
|
| 1206 | - $message_template_group->messenger() |
|
| 1207 | - ); |
|
| 1208 | - $this->_active_message_type_name = $message_template_group->message_type(); |
|
| 1209 | - |
|
| 1210 | - |
|
| 1211 | - //Do we have any validation errors? |
|
| 1212 | - $validators = $this->_get_transient(); |
|
| 1213 | - $v_fields = ! empty($validators) ? array_keys($validators) : array(); |
|
| 1214 | - |
|
| 1215 | - |
|
| 1216 | - //we need to assemble the title from Various details |
|
| 1217 | - $context_label = sprintf( |
|
| 1218 | - __('(%s %s)', 'event_espresso'), |
|
| 1219 | - $c_config[$context]['label'], |
|
| 1220 | - ucwords($c_label['label']) |
|
| 1221 | - ); |
|
| 1222 | - |
|
| 1223 | - $title = sprintf( |
|
| 1224 | - __(' %s %s Template %s', 'event_espresso'), |
|
| 1225 | - ucwords($message_template_group->messenger_obj()->label['singular']), |
|
| 1226 | - ucwords($message_template_group->message_type_obj()->label['singular']), |
|
| 1227 | - $context_label |
|
| 1228 | - ); |
|
| 1229 | - |
|
| 1230 | - $this->_template_args['GRP_ID'] = $GRP_ID; |
|
| 1231 | - $this->_template_args['message_template'] = $message_template_group; |
|
| 1232 | - $this->_template_args['is_extra_fields'] = false; |
|
| 1233 | - |
|
| 1234 | - |
|
| 1235 | - //let's get EEH_MSG_Template so we can get template form fields |
|
| 1236 | - $template_field_structure = EEH_MSG_Template::get_fields( |
|
| 1237 | - $message_template_group->messenger(), |
|
| 1238 | - $message_template_group->message_type() |
|
| 1239 | - ); |
|
| 1240 | - |
|
| 1241 | - if ( ! $template_field_structure) { |
|
| 1242 | - $template_field_structure = false; |
|
| 1243 | - $template_fields = __('There was an error in assembling the fields for this display (you should see an error message)', |
|
| 1244 | - 'event_espresso'); |
|
| 1245 | - } |
|
| 1246 | - |
|
| 1247 | - |
|
| 1248 | - $message_templates = $message_template_group->context_templates(); |
|
| 1249 | - |
|
| 1250 | - |
|
| 1251 | - //if we have the extra key.. then we need to remove the content index from the template_field_structure as it will get handled in the "extra" array. |
|
| 1252 | - if (is_array($template_field_structure[$context]) && isset($template_field_structure[$context]['extra'])) { |
|
| 1253 | - foreach ($template_field_structure[$context]['extra'] as $reference_field => $new_fields) { |
|
| 1254 | - unset($template_field_structure[$context][$reference_field]); |
|
| 1255 | - } |
|
| 1256 | - } |
|
| 1257 | - |
|
| 1258 | - //let's loop through the template_field_structure and actually assemble the input fields! |
|
| 1259 | - if ( ! empty($template_field_structure)) { |
|
| 1260 | - foreach ($template_field_structure[$context] as $template_field => $field_setup_array) { |
|
| 1261 | - //if this is an 'extra' template field then we need to remove any existing fields that are keyed up in the extra array and reset them. |
|
| 1262 | - if ($template_field == 'extra') { |
|
| 1263 | - $this->_template_args['is_extra_fields'] = true; |
|
| 1264 | - foreach ($field_setup_array as $reference_field => $new_fields_array) { |
|
| 1265 | - $message_template = $message_templates[$context][$reference_field]; |
|
| 1266 | - $content = $message_template instanceof EE_Message_Template |
|
| 1267 | - ? $message_template->get('MTP_content') |
|
| 1268 | - : ''; |
|
| 1269 | - foreach ($new_fields_array as $extra_field => $extra_array) { |
|
| 1270 | - //let's verify if we need this extra field via the shortcodes parameter. |
|
| 1271 | - $continue = false; |
|
| 1272 | - if (isset($extra_array['shortcodes_required'])) { |
|
| 1273 | - foreach ((array)$extra_array['shortcodes_required'] as $shortcode) { |
|
| 1274 | - if ( ! array_key_exists($shortcode, $this->_shortcodes)) { |
|
| 1275 | - $continue = true; |
|
| 1276 | - } |
|
| 1277 | - } |
|
| 1278 | - if ($continue) { |
|
| 1279 | - continue; |
|
| 1280 | - } |
|
| 1281 | - } |
|
| 1071 | + } |
|
| 1072 | + |
|
| 1073 | + return $templates; |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + |
|
| 1077 | + /** |
|
| 1078 | + * filters etc might need a list of installed message_types |
|
| 1079 | + * @return array an array of message type objects |
|
| 1080 | + */ |
|
| 1081 | + public function get_installed_message_types() |
|
| 1082 | + { |
|
| 1083 | + $installed_message_types = $this->_message_resource_manager->installed_message_types(); |
|
| 1084 | + $installed = array(); |
|
| 1085 | + |
|
| 1086 | + foreach ($installed_message_types as $message_type) { |
|
| 1087 | + $installed[$message_type->name] = $message_type; |
|
| 1088 | + } |
|
| 1089 | + |
|
| 1090 | + return $installed; |
|
| 1091 | + } |
|
| 1092 | + |
|
| 1093 | + |
|
| 1094 | + /** |
|
| 1095 | + * _add_message_template |
|
| 1096 | + * |
|
| 1097 | + * This is used when creating a custom template. All Custom Templates start based off another template. |
|
| 1098 | + * |
|
| 1099 | + * @param string $message_type |
|
| 1100 | + * @param string $messenger |
|
| 1101 | + * @param string $GRP_ID |
|
| 1102 | + * |
|
| 1103 | + * @throws EE_error |
|
| 1104 | + */ |
|
| 1105 | + protected function _add_message_template($message_type = '', $messenger = '', $GRP_ID = '') |
|
| 1106 | + { |
|
| 1107 | + //set values override any request data |
|
| 1108 | + $message_type = ! empty($message_type) ? $message_type : ''; |
|
| 1109 | + $message_type = empty($message_type) && ! empty($this->_req_data['message_type']) ? $this->_req_data['message_type'] : $message_type; |
|
| 1110 | + |
|
| 1111 | + $messenger = ! empty($messenger) ? $messenger : ''; |
|
| 1112 | + $messenger = empty($messenger) && ! empty($this->_req_data['messenger']) ? $this->_req_data['messenger'] : $messenger; |
|
| 1113 | + |
|
| 1114 | + $GRP_ID = ! empty($GRP_ID) ? $GRP_ID : ''; |
|
| 1115 | + $GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : $GRP_ID; |
|
| 1116 | + |
|
| 1117 | + //we need messenger and message type. They should be coming from the event editor. If not here then return error |
|
| 1118 | + if (empty($message_type) || empty($messenger)) { |
|
| 1119 | + throw new EE_error(__('Sorry, but we can\'t create new templates because we\'re missing the messenger or message type', |
|
| 1120 | + 'event_espresso')); |
|
| 1121 | + } |
|
| 1122 | + |
|
| 1123 | + //we need the GRP_ID for the template being used as the base for the new template |
|
| 1124 | + if (empty($GRP_ID)) { |
|
| 1125 | + throw new EE_Error(__('In order to create a custom message template the GRP_ID of the template being used as a base is needed', |
|
| 1126 | + 'event_espresso')); |
|
| 1127 | + } |
|
| 1128 | + |
|
| 1129 | + //let's just make sure the template gets generated! |
|
| 1130 | + |
|
| 1131 | + //we need to reassign some variables for what the insert is expecting |
|
| 1132 | + $this->_req_data['MTP_messenger'] = $messenger; |
|
| 1133 | + $this->_req_data['MTP_message_type'] = $message_type; |
|
| 1134 | + $this->_req_data['GRP_ID'] = $GRP_ID; |
|
| 1135 | + $this->_insert_or_update_message_template(true); |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + |
|
| 1139 | + /** |
|
| 1140 | + * public wrapper for the _add_message_template method |
|
| 1141 | + * |
|
| 1142 | + * @param string $message_type message type slug |
|
| 1143 | + * @param string $messenger messenger slug |
|
| 1144 | + * @param int $GRP_ID GRP_ID for the related message template group this new template will be based |
|
| 1145 | + * off of. |
|
| 1146 | + */ |
|
| 1147 | + public function add_message_template($message_type, $messenger, $GRP_ID) |
|
| 1148 | + { |
|
| 1149 | + $this->_add_message_template($message_type, $messenger, $GRP_ID); |
|
| 1150 | + } |
|
| 1151 | + |
|
| 1152 | + |
|
| 1153 | + /** |
|
| 1154 | + * _edit_message_template |
|
| 1155 | + * |
|
| 1156 | + * @access protected |
|
| 1157 | + * @return void |
|
| 1158 | + */ |
|
| 1159 | + protected function _edit_message_template() |
|
| 1160 | + { |
|
| 1161 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 1162 | + $template_fields = ''; |
|
| 1163 | + $sidebar_fields = ''; |
|
| 1164 | + //we filter the tinyMCE settings to remove the validation since message templates by their nature will not have valid html in the templates. |
|
| 1165 | + add_filter('tiny_mce_before_init', array($this, 'filter_tinymce_init'), 10, 2); |
|
| 1166 | + |
|
| 1167 | + $GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id']) |
|
| 1168 | + ? absint($this->_req_data['id']) |
|
| 1169 | + : false; |
|
| 1170 | + |
|
| 1171 | + $this->_set_shortcodes(); //this also sets the _message_template property. |
|
| 1172 | + $message_template_group = $this->_message_template_group; |
|
| 1173 | + $c_label = $message_template_group->context_label(); |
|
| 1174 | + $c_config = $message_template_group->contexts_config(); |
|
| 1175 | + |
|
| 1176 | + reset($c_config); |
|
| 1177 | + $context = isset($this->_req_data['context']) && ! empty($this->_req_data['context']) |
|
| 1178 | + ? strtolower($this->_req_data['context']) |
|
| 1179 | + : key($c_config); |
|
| 1180 | + |
|
| 1181 | + |
|
| 1182 | + if (empty($GRP_ID)) { |
|
| 1183 | + $action = 'insert_message_template'; |
|
| 1184 | + //$button_both = false; |
|
| 1185 | + //$button_text = array( __( 'Save','event_espresso') ); |
|
| 1186 | + //$button_actions = array('something_different'); |
|
| 1187 | + //$referrer = false; |
|
| 1188 | + $edit_message_template_form_url = add_query_arg( |
|
| 1189 | + array('action' => $action, 'noheader' => true), |
|
| 1190 | + EE_MSG_ADMIN_URL |
|
| 1191 | + ); |
|
| 1192 | + } else { |
|
| 1193 | + $action = 'update_message_template'; |
|
| 1194 | + //$button_both = true; |
|
| 1195 | + //$button_text = array(); |
|
| 1196 | + //$button_actions = array(); |
|
| 1197 | + //$referrer = $this->_admin_base_url; |
|
| 1198 | + $edit_message_template_form_url = add_query_arg( |
|
| 1199 | + array('action' => $action, 'noheader' => true), |
|
| 1200 | + EE_MSG_ADMIN_URL |
|
| 1201 | + ); |
|
| 1202 | + } |
|
| 1203 | + |
|
| 1204 | + //set active messenger for this view |
|
| 1205 | + $this->_active_messenger = $this->_message_resource_manager->get_active_messenger( |
|
| 1206 | + $message_template_group->messenger() |
|
| 1207 | + ); |
|
| 1208 | + $this->_active_message_type_name = $message_template_group->message_type(); |
|
| 1209 | + |
|
| 1210 | + |
|
| 1211 | + //Do we have any validation errors? |
|
| 1212 | + $validators = $this->_get_transient(); |
|
| 1213 | + $v_fields = ! empty($validators) ? array_keys($validators) : array(); |
|
| 1214 | + |
|
| 1215 | + |
|
| 1216 | + //we need to assemble the title from Various details |
|
| 1217 | + $context_label = sprintf( |
|
| 1218 | + __('(%s %s)', 'event_espresso'), |
|
| 1219 | + $c_config[$context]['label'], |
|
| 1220 | + ucwords($c_label['label']) |
|
| 1221 | + ); |
|
| 1222 | + |
|
| 1223 | + $title = sprintf( |
|
| 1224 | + __(' %s %s Template %s', 'event_espresso'), |
|
| 1225 | + ucwords($message_template_group->messenger_obj()->label['singular']), |
|
| 1226 | + ucwords($message_template_group->message_type_obj()->label['singular']), |
|
| 1227 | + $context_label |
|
| 1228 | + ); |
|
| 1229 | + |
|
| 1230 | + $this->_template_args['GRP_ID'] = $GRP_ID; |
|
| 1231 | + $this->_template_args['message_template'] = $message_template_group; |
|
| 1232 | + $this->_template_args['is_extra_fields'] = false; |
|
| 1233 | + |
|
| 1234 | + |
|
| 1235 | + //let's get EEH_MSG_Template so we can get template form fields |
|
| 1236 | + $template_field_structure = EEH_MSG_Template::get_fields( |
|
| 1237 | + $message_template_group->messenger(), |
|
| 1238 | + $message_template_group->message_type() |
|
| 1239 | + ); |
|
| 1240 | + |
|
| 1241 | + if ( ! $template_field_structure) { |
|
| 1242 | + $template_field_structure = false; |
|
| 1243 | + $template_fields = __('There was an error in assembling the fields for this display (you should see an error message)', |
|
| 1244 | + 'event_espresso'); |
|
| 1245 | + } |
|
| 1246 | + |
|
| 1247 | + |
|
| 1248 | + $message_templates = $message_template_group->context_templates(); |
|
| 1249 | + |
|
| 1250 | + |
|
| 1251 | + //if we have the extra key.. then we need to remove the content index from the template_field_structure as it will get handled in the "extra" array. |
|
| 1252 | + if (is_array($template_field_structure[$context]) && isset($template_field_structure[$context]['extra'])) { |
|
| 1253 | + foreach ($template_field_structure[$context]['extra'] as $reference_field => $new_fields) { |
|
| 1254 | + unset($template_field_structure[$context][$reference_field]); |
|
| 1255 | + } |
|
| 1256 | + } |
|
| 1257 | + |
|
| 1258 | + //let's loop through the template_field_structure and actually assemble the input fields! |
|
| 1259 | + if ( ! empty($template_field_structure)) { |
|
| 1260 | + foreach ($template_field_structure[$context] as $template_field => $field_setup_array) { |
|
| 1261 | + //if this is an 'extra' template field then we need to remove any existing fields that are keyed up in the extra array and reset them. |
|
| 1262 | + if ($template_field == 'extra') { |
|
| 1263 | + $this->_template_args['is_extra_fields'] = true; |
|
| 1264 | + foreach ($field_setup_array as $reference_field => $new_fields_array) { |
|
| 1265 | + $message_template = $message_templates[$context][$reference_field]; |
|
| 1266 | + $content = $message_template instanceof EE_Message_Template |
|
| 1267 | + ? $message_template->get('MTP_content') |
|
| 1268 | + : ''; |
|
| 1269 | + foreach ($new_fields_array as $extra_field => $extra_array) { |
|
| 1270 | + //let's verify if we need this extra field via the shortcodes parameter. |
|
| 1271 | + $continue = false; |
|
| 1272 | + if (isset($extra_array['shortcodes_required'])) { |
|
| 1273 | + foreach ((array)$extra_array['shortcodes_required'] as $shortcode) { |
|
| 1274 | + if ( ! array_key_exists($shortcode, $this->_shortcodes)) { |
|
| 1275 | + $continue = true; |
|
| 1276 | + } |
|
| 1277 | + } |
|
| 1278 | + if ($continue) { |
|
| 1279 | + continue; |
|
| 1280 | + } |
|
| 1281 | + } |
|
| 1282 | 1282 | |
| 1283 | - $field_id = $reference_field . '-' . $extra_field . '-content'; |
|
| 1284 | - $template_form_fields[$field_id] = $extra_array; |
|
| 1285 | - $template_form_fields[$field_id]['name'] = 'MTP_template_fields[' . $reference_field . '][content][' . $extra_field . ']'; |
|
| 1286 | - $css_class = isset($extra_array['css_class']) ? $extra_array['css_class'] : ''; |
|
| 1283 | + $field_id = $reference_field . '-' . $extra_field . '-content'; |
|
| 1284 | + $template_form_fields[$field_id] = $extra_array; |
|
| 1285 | + $template_form_fields[$field_id]['name'] = 'MTP_template_fields[' . $reference_field . '][content][' . $extra_field . ']'; |
|
| 1286 | + $css_class = isset($extra_array['css_class']) ? $extra_array['css_class'] : ''; |
|
| 1287 | 1287 | |
| 1288 | - $template_form_fields[$field_id]['css_class'] = ! empty($v_fields) |
|
| 1289 | - && in_array($extra_field, $v_fields) |
|
| 1290 | - && |
|
| 1291 | - ( |
|
| 1292 | - is_array($validators[$extra_field]) |
|
| 1293 | - && isset($validators[$extra_field]['msg']) |
|
| 1294 | - ) |
|
| 1295 | - ? 'validate-error ' . $css_class |
|
| 1296 | - : $css_class; |
|
| 1288 | + $template_form_fields[$field_id]['css_class'] = ! empty($v_fields) |
|
| 1289 | + && in_array($extra_field, $v_fields) |
|
| 1290 | + && |
|
| 1291 | + ( |
|
| 1292 | + is_array($validators[$extra_field]) |
|
| 1293 | + && isset($validators[$extra_field]['msg']) |
|
| 1294 | + ) |
|
| 1295 | + ? 'validate-error ' . $css_class |
|
| 1296 | + : $css_class; |
|
| 1297 | 1297 | |
| 1298 | - $template_form_fields[$field_id]['value'] = ! empty($message_templates) && isset($content[$extra_field]) |
|
| 1299 | - ? stripslashes(html_entity_decode($content[$extra_field], ENT_QUOTES, "UTF-8")) |
|
| 1300 | - : ''; |
|
| 1298 | + $template_form_fields[$field_id]['value'] = ! empty($message_templates) && isset($content[$extra_field]) |
|
| 1299 | + ? stripslashes(html_entity_decode($content[$extra_field], ENT_QUOTES, "UTF-8")) |
|
| 1300 | + : ''; |
|
| 1301 | 1301 | |
| 1302 | - //do we have a validation error? if we do then let's use that value instead |
|
| 1303 | - $template_form_fields[$field_id]['value'] = isset($validators[$extra_field]) ? $validators[$extra_field]['value'] : $template_form_fields[$field_id]['value']; |
|
| 1302 | + //do we have a validation error? if we do then let's use that value instead |
|
| 1303 | + $template_form_fields[$field_id]['value'] = isset($validators[$extra_field]) ? $validators[$extra_field]['value'] : $template_form_fields[$field_id]['value']; |
|
| 1304 | 1304 | |
| 1305 | 1305 | |
| 1306 | - $template_form_fields[$field_id]['db-col'] = 'MTP_content'; |
|
| 1306 | + $template_form_fields[$field_id]['db-col'] = 'MTP_content'; |
|
| 1307 | 1307 | |
| 1308 | - //shortcode selector |
|
| 1309 | - $field_name_to_use = $extra_field == 'main' ? 'content' : $extra_field; |
|
| 1310 | - $template_form_fields[$field_id]['append_content'] = $this->_get_shortcode_selector( |
|
| 1311 | - $field_name_to_use, |
|
| 1312 | - $field_id |
|
| 1313 | - ); |
|
| 1308 | + //shortcode selector |
|
| 1309 | + $field_name_to_use = $extra_field == 'main' ? 'content' : $extra_field; |
|
| 1310 | + $template_form_fields[$field_id]['append_content'] = $this->_get_shortcode_selector( |
|
| 1311 | + $field_name_to_use, |
|
| 1312 | + $field_id |
|
| 1313 | + ); |
|
| 1314 | 1314 | |
| 1315 | - if (isset($extra_array['input']) && $extra_array['input'] == 'wp_editor') { |
|
| 1316 | - //we want to decode the entities |
|
| 1317 | - $template_form_fields[$field_id]['value'] = stripslashes( |
|
| 1318 | - html_entity_decode($template_form_fields[$field_id]['value'], ENT_QUOTES, "UTF-8") |
|
| 1319 | - ); |
|
| 1315 | + if (isset($extra_array['input']) && $extra_array['input'] == 'wp_editor') { |
|
| 1316 | + //we want to decode the entities |
|
| 1317 | + $template_form_fields[$field_id]['value'] = stripslashes( |
|
| 1318 | + html_entity_decode($template_form_fields[$field_id]['value'], ENT_QUOTES, "UTF-8") |
|
| 1319 | + ); |
|
| 1320 | 1320 | |
| 1321 | - }/**/ |
|
| 1322 | - } |
|
| 1323 | - $templatefield_MTP_id = $reference_field . '-MTP_ID'; |
|
| 1324 | - $templatefield_templatename_id = $reference_field . '-name'; |
|
| 1321 | + }/**/ |
|
| 1322 | + } |
|
| 1323 | + $templatefield_MTP_id = $reference_field . '-MTP_ID'; |
|
| 1324 | + $templatefield_templatename_id = $reference_field . '-name'; |
|
| 1325 | 1325 | |
| 1326 | - $template_form_fields[$templatefield_MTP_id] = array( |
|
| 1327 | - 'name' => 'MTP_template_fields[' . $reference_field . '][MTP_ID]', |
|
| 1328 | - 'label' => null, |
|
| 1329 | - 'input' => 'hidden', |
|
| 1330 | - 'type' => 'int', |
|
| 1331 | - 'required' => false, |
|
| 1332 | - 'validation' => false, |
|
| 1333 | - 'value' => ! empty($message_templates) ? $message_template->ID() : '', |
|
| 1334 | - 'css_class' => '', |
|
| 1335 | - 'format' => '%d', |
|
| 1336 | - 'db-col' => 'MTP_ID' |
|
| 1337 | - ); |
|
| 1326 | + $template_form_fields[$templatefield_MTP_id] = array( |
|
| 1327 | + 'name' => 'MTP_template_fields[' . $reference_field . '][MTP_ID]', |
|
| 1328 | + 'label' => null, |
|
| 1329 | + 'input' => 'hidden', |
|
| 1330 | + 'type' => 'int', |
|
| 1331 | + 'required' => false, |
|
| 1332 | + 'validation' => false, |
|
| 1333 | + 'value' => ! empty($message_templates) ? $message_template->ID() : '', |
|
| 1334 | + 'css_class' => '', |
|
| 1335 | + 'format' => '%d', |
|
| 1336 | + 'db-col' => 'MTP_ID' |
|
| 1337 | + ); |
|
| 1338 | 1338 | |
| 1339 | - $template_form_fields[$templatefield_templatename_id] = array( |
|
| 1340 | - 'name' => 'MTP_template_fields[' . $reference_field . '][name]', |
|
| 1341 | - 'label' => null, |
|
| 1342 | - 'input' => 'hidden', |
|
| 1343 | - 'type' => 'string', |
|
| 1344 | - 'required' => false, |
|
| 1345 | - 'validation' => true, |
|
| 1346 | - 'value' => $reference_field, |
|
| 1347 | - 'css_class' => '', |
|
| 1348 | - 'format' => '%s', |
|
| 1349 | - 'db-col' => 'MTP_template_field' |
|
| 1350 | - ); |
|
| 1351 | - } |
|
| 1352 | - continue; //skip the next stuff, we got the necessary fields here for this dataset. |
|
| 1353 | - } else { |
|
| 1354 | - $field_id = $template_field . '-content'; |
|
| 1355 | - $template_form_fields[$field_id] = $field_setup_array; |
|
| 1356 | - $template_form_fields[$field_id]['name'] = 'MTP_template_fields[' . $template_field . '][content]'; |
|
| 1357 | - $message_template = isset($message_templates[$context][$template_field]) |
|
| 1358 | - ? $message_templates[$context][$template_field] |
|
| 1359 | - : null; |
|
| 1360 | - $template_form_fields[$field_id]['value'] = ! empty($message_templates) |
|
| 1361 | - && is_array($message_templates[$context]) |
|
| 1362 | - && $message_template instanceof EE_Message_Template |
|
| 1363 | - ? $message_template->get('MTP_content') |
|
| 1364 | - : ''; |
|
| 1339 | + $template_form_fields[$templatefield_templatename_id] = array( |
|
| 1340 | + 'name' => 'MTP_template_fields[' . $reference_field . '][name]', |
|
| 1341 | + 'label' => null, |
|
| 1342 | + 'input' => 'hidden', |
|
| 1343 | + 'type' => 'string', |
|
| 1344 | + 'required' => false, |
|
| 1345 | + 'validation' => true, |
|
| 1346 | + 'value' => $reference_field, |
|
| 1347 | + 'css_class' => '', |
|
| 1348 | + 'format' => '%s', |
|
| 1349 | + 'db-col' => 'MTP_template_field' |
|
| 1350 | + ); |
|
| 1351 | + } |
|
| 1352 | + continue; //skip the next stuff, we got the necessary fields here for this dataset. |
|
| 1353 | + } else { |
|
| 1354 | + $field_id = $template_field . '-content'; |
|
| 1355 | + $template_form_fields[$field_id] = $field_setup_array; |
|
| 1356 | + $template_form_fields[$field_id]['name'] = 'MTP_template_fields[' . $template_field . '][content]'; |
|
| 1357 | + $message_template = isset($message_templates[$context][$template_field]) |
|
| 1358 | + ? $message_templates[$context][$template_field] |
|
| 1359 | + : null; |
|
| 1360 | + $template_form_fields[$field_id]['value'] = ! empty($message_templates) |
|
| 1361 | + && is_array($message_templates[$context]) |
|
| 1362 | + && $message_template instanceof EE_Message_Template |
|
| 1363 | + ? $message_template->get('MTP_content') |
|
| 1364 | + : ''; |
|
| 1365 | 1365 | |
| 1366 | - //do we have a validator error for this field? if we do then we'll use that value instead |
|
| 1367 | - $template_form_fields[$field_id]['value'] = isset($validators[$template_field]) |
|
| 1368 | - ? $validators[$template_field]['value'] |
|
| 1369 | - : $template_form_fields[$field_id]['value']; |
|
| 1366 | + //do we have a validator error for this field? if we do then we'll use that value instead |
|
| 1367 | + $template_form_fields[$field_id]['value'] = isset($validators[$template_field]) |
|
| 1368 | + ? $validators[$template_field]['value'] |
|
| 1369 | + : $template_form_fields[$field_id]['value']; |
|
| 1370 | 1370 | |
| 1371 | 1371 | |
| 1372 | - $template_form_fields[$field_id]['db-col'] = 'MTP_content'; |
|
| 1373 | - $css_class = isset($field_setup_array['css_class']) ? $field_setup_array['css_class'] : ''; |
|
| 1374 | - $template_form_fields[$field_id]['css_class'] = ! empty($v_fields) |
|
| 1375 | - && in_array($template_field, $v_fields) |
|
| 1376 | - && isset($validators[$template_field]['msg']) |
|
| 1377 | - ? 'validate-error ' . $css_class |
|
| 1378 | - : $css_class; |
|
| 1372 | + $template_form_fields[$field_id]['db-col'] = 'MTP_content'; |
|
| 1373 | + $css_class = isset($field_setup_array['css_class']) ? $field_setup_array['css_class'] : ''; |
|
| 1374 | + $template_form_fields[$field_id]['css_class'] = ! empty($v_fields) |
|
| 1375 | + && in_array($template_field, $v_fields) |
|
| 1376 | + && isset($validators[$template_field]['msg']) |
|
| 1377 | + ? 'validate-error ' . $css_class |
|
| 1378 | + : $css_class; |
|
| 1379 | 1379 | |
| 1380 | - //shortcode selector |
|
| 1381 | - $template_form_fields[$field_id]['append_content'] = $this->_get_shortcode_selector( |
|
| 1382 | - $template_field, $field_id |
|
| 1383 | - ); |
|
| 1384 | - } |
|
| 1380 | + //shortcode selector |
|
| 1381 | + $template_form_fields[$field_id]['append_content'] = $this->_get_shortcode_selector( |
|
| 1382 | + $template_field, $field_id |
|
| 1383 | + ); |
|
| 1384 | + } |
|
| 1385 | 1385 | |
| 1386 | - //k took care of content field(s) now let's take care of others. |
|
| 1386 | + //k took care of content field(s) now let's take care of others. |
|
| 1387 | 1387 | |
| 1388 | - $templatefield_MTP_id = $template_field . '-MTP_ID'; |
|
| 1389 | - $templatefield_field_templatename_id = $template_field . '-name'; |
|
| 1388 | + $templatefield_MTP_id = $template_field . '-MTP_ID'; |
|
| 1389 | + $templatefield_field_templatename_id = $template_field . '-name'; |
|
| 1390 | 1390 | |
| 1391 | - //foreach template field there are actually two form fields created |
|
| 1392 | - $template_form_fields[$templatefield_MTP_id] = array( |
|
| 1393 | - 'name' => 'MTP_template_fields[' . $template_field . '][MTP_ID]', |
|
| 1394 | - 'label' => null, |
|
| 1395 | - 'input' => 'hidden', |
|
| 1396 | - 'type' => 'int', |
|
| 1397 | - 'required' => false, |
|
| 1398 | - 'validation' => true, |
|
| 1399 | - 'value' => $message_template instanceof EE_Message_Template ? $message_template->ID() : '', |
|
| 1400 | - 'css_class' => '', |
|
| 1401 | - 'format' => '%d', |
|
| 1402 | - 'db-col' => 'MTP_ID' |
|
| 1403 | - ); |
|
| 1391 | + //foreach template field there are actually two form fields created |
|
| 1392 | + $template_form_fields[$templatefield_MTP_id] = array( |
|
| 1393 | + 'name' => 'MTP_template_fields[' . $template_field . '][MTP_ID]', |
|
| 1394 | + 'label' => null, |
|
| 1395 | + 'input' => 'hidden', |
|
| 1396 | + 'type' => 'int', |
|
| 1397 | + 'required' => false, |
|
| 1398 | + 'validation' => true, |
|
| 1399 | + 'value' => $message_template instanceof EE_Message_Template ? $message_template->ID() : '', |
|
| 1400 | + 'css_class' => '', |
|
| 1401 | + 'format' => '%d', |
|
| 1402 | + 'db-col' => 'MTP_ID' |
|
| 1403 | + ); |
|
| 1404 | 1404 | |
| 1405 | - $template_form_fields[$templatefield_field_templatename_id] = array( |
|
| 1406 | - 'name' => 'MTP_template_fields[' . $template_field . '][name]', |
|
| 1407 | - 'label' => null, |
|
| 1408 | - 'input' => 'hidden', |
|
| 1409 | - 'type' => 'string', |
|
| 1410 | - 'required' => false, |
|
| 1411 | - 'validation' => true, |
|
| 1412 | - 'value' => $template_field, |
|
| 1413 | - 'css_class' => '', |
|
| 1414 | - 'format' => '%s', |
|
| 1415 | - 'db-col' => 'MTP_template_field' |
|
| 1416 | - ); |
|
| 1405 | + $template_form_fields[$templatefield_field_templatename_id] = array( |
|
| 1406 | + 'name' => 'MTP_template_fields[' . $template_field . '][name]', |
|
| 1407 | + 'label' => null, |
|
| 1408 | + 'input' => 'hidden', |
|
| 1409 | + 'type' => 'string', |
|
| 1410 | + 'required' => false, |
|
| 1411 | + 'validation' => true, |
|
| 1412 | + 'value' => $template_field, |
|
| 1413 | + 'css_class' => '', |
|
| 1414 | + 'format' => '%s', |
|
| 1415 | + 'db-col' => 'MTP_template_field' |
|
| 1416 | + ); |
|
| 1417 | 1417 | |
| 1418 | - } |
|
| 1418 | + } |
|
| 1419 | 1419 | |
| 1420 | - //add other fields |
|
| 1421 | - $template_form_fields['ee-msg-current-context'] = array( |
|
| 1422 | - 'name' => 'MTP_context', |
|
| 1423 | - 'label' => null, |
|
| 1424 | - 'input' => 'hidden', |
|
| 1425 | - 'type' => 'string', |
|
| 1426 | - 'required' => false, |
|
| 1427 | - 'validation' => true, |
|
| 1428 | - 'value' => $context, |
|
| 1429 | - 'css_class' => '', |
|
| 1430 | - 'format' => '%s', |
|
| 1431 | - 'db-col' => 'MTP_context' |
|
| 1432 | - ); |
|
| 1420 | + //add other fields |
|
| 1421 | + $template_form_fields['ee-msg-current-context'] = array( |
|
| 1422 | + 'name' => 'MTP_context', |
|
| 1423 | + 'label' => null, |
|
| 1424 | + 'input' => 'hidden', |
|
| 1425 | + 'type' => 'string', |
|
| 1426 | + 'required' => false, |
|
| 1427 | + 'validation' => true, |
|
| 1428 | + 'value' => $context, |
|
| 1429 | + 'css_class' => '', |
|
| 1430 | + 'format' => '%s', |
|
| 1431 | + 'db-col' => 'MTP_context' |
|
| 1432 | + ); |
|
| 1433 | 1433 | |
| 1434 | - $template_form_fields['ee-msg-grp-id'] = array( |
|
| 1435 | - 'name' => 'GRP_ID', |
|
| 1436 | - 'label' => null, |
|
| 1437 | - 'input' => 'hidden', |
|
| 1438 | - 'type' => 'int', |
|
| 1439 | - 'required' => false, |
|
| 1440 | - 'validation' => true, |
|
| 1441 | - 'value' => $GRP_ID, |
|
| 1442 | - 'css_class' => '', |
|
| 1443 | - 'format' => '%d', |
|
| 1444 | - 'db-col' => 'GRP_ID' |
|
| 1445 | - ); |
|
| 1434 | + $template_form_fields['ee-msg-grp-id'] = array( |
|
| 1435 | + 'name' => 'GRP_ID', |
|
| 1436 | + 'label' => null, |
|
| 1437 | + 'input' => 'hidden', |
|
| 1438 | + 'type' => 'int', |
|
| 1439 | + 'required' => false, |
|
| 1440 | + 'validation' => true, |
|
| 1441 | + 'value' => $GRP_ID, |
|
| 1442 | + 'css_class' => '', |
|
| 1443 | + 'format' => '%d', |
|
| 1444 | + 'db-col' => 'GRP_ID' |
|
| 1445 | + ); |
|
| 1446 | 1446 | |
| 1447 | - $template_form_fields['ee-msg-messenger'] = array( |
|
| 1448 | - 'name' => 'MTP_messenger', |
|
| 1449 | - 'label' => null, |
|
| 1450 | - 'input' => 'hidden', |
|
| 1451 | - 'type' => 'string', |
|
| 1452 | - 'required' => false, |
|
| 1453 | - 'validation' => true, |
|
| 1454 | - 'value' => $message_template_group->messenger(), |
|
| 1455 | - 'css_class' => '', |
|
| 1456 | - 'format' => '%s', |
|
| 1457 | - 'db-col' => 'MTP_messenger' |
|
| 1458 | - ); |
|
| 1447 | + $template_form_fields['ee-msg-messenger'] = array( |
|
| 1448 | + 'name' => 'MTP_messenger', |
|
| 1449 | + 'label' => null, |
|
| 1450 | + 'input' => 'hidden', |
|
| 1451 | + 'type' => 'string', |
|
| 1452 | + 'required' => false, |
|
| 1453 | + 'validation' => true, |
|
| 1454 | + 'value' => $message_template_group->messenger(), |
|
| 1455 | + 'css_class' => '', |
|
| 1456 | + 'format' => '%s', |
|
| 1457 | + 'db-col' => 'MTP_messenger' |
|
| 1458 | + ); |
|
| 1459 | 1459 | |
| 1460 | - $template_form_fields['ee-msg-message-type'] = array( |
|
| 1461 | - 'name' => 'MTP_message_type', |
|
| 1462 | - 'label' => null, |
|
| 1463 | - 'input' => 'hidden', |
|
| 1464 | - 'type' => 'string', |
|
| 1465 | - 'required' => false, |
|
| 1466 | - 'validation' => true, |
|
| 1467 | - 'value' => $message_template_group->message_type(), |
|
| 1468 | - 'css_class' => '', |
|
| 1469 | - 'format' => '%s', |
|
| 1470 | - 'db-col' => 'MTP_message_type' |
|
| 1471 | - ); |
|
| 1460 | + $template_form_fields['ee-msg-message-type'] = array( |
|
| 1461 | + 'name' => 'MTP_message_type', |
|
| 1462 | + 'label' => null, |
|
| 1463 | + 'input' => 'hidden', |
|
| 1464 | + 'type' => 'string', |
|
| 1465 | + 'required' => false, |
|
| 1466 | + 'validation' => true, |
|
| 1467 | + 'value' => $message_template_group->message_type(), |
|
| 1468 | + 'css_class' => '', |
|
| 1469 | + 'format' => '%s', |
|
| 1470 | + 'db-col' => 'MTP_message_type' |
|
| 1471 | + ); |
|
| 1472 | 1472 | |
| 1473 | - $sidebar_form_fields['ee-msg-is-global'] = array( |
|
| 1474 | - 'name' => 'MTP_is_global', |
|
| 1475 | - 'label' => __('Global Template', 'event_espresso'), |
|
| 1476 | - 'input' => 'hidden', |
|
| 1477 | - 'type' => 'int', |
|
| 1478 | - 'required' => false, |
|
| 1479 | - 'validation' => true, |
|
| 1480 | - 'value' => $message_template_group->get('MTP_is_global'), |
|
| 1481 | - 'css_class' => '', |
|
| 1482 | - 'format' => '%d', |
|
| 1483 | - 'db-col' => 'MTP_is_global' |
|
| 1484 | - ); |
|
| 1473 | + $sidebar_form_fields['ee-msg-is-global'] = array( |
|
| 1474 | + 'name' => 'MTP_is_global', |
|
| 1475 | + 'label' => __('Global Template', 'event_espresso'), |
|
| 1476 | + 'input' => 'hidden', |
|
| 1477 | + 'type' => 'int', |
|
| 1478 | + 'required' => false, |
|
| 1479 | + 'validation' => true, |
|
| 1480 | + 'value' => $message_template_group->get('MTP_is_global'), |
|
| 1481 | + 'css_class' => '', |
|
| 1482 | + 'format' => '%d', |
|
| 1483 | + 'db-col' => 'MTP_is_global' |
|
| 1484 | + ); |
|
| 1485 | 1485 | |
| 1486 | - $sidebar_form_fields['ee-msg-is-override'] = array( |
|
| 1487 | - 'name' => 'MTP_is_override', |
|
| 1488 | - 'label' => __('Override all custom', 'event_espresso'), |
|
| 1489 | - 'input' => $message_template_group->is_global() ? 'checkbox' : 'hidden', |
|
| 1490 | - 'type' => 'int', |
|
| 1491 | - 'required' => false, |
|
| 1492 | - 'validation' => true, |
|
| 1493 | - 'value' => $message_template_group->get('MTP_is_override'), |
|
| 1494 | - 'css_class' => '', |
|
| 1495 | - 'format' => '%d', |
|
| 1496 | - 'db-col' => 'MTP_is_override' |
|
| 1497 | - ); |
|
| 1486 | + $sidebar_form_fields['ee-msg-is-override'] = array( |
|
| 1487 | + 'name' => 'MTP_is_override', |
|
| 1488 | + 'label' => __('Override all custom', 'event_espresso'), |
|
| 1489 | + 'input' => $message_template_group->is_global() ? 'checkbox' : 'hidden', |
|
| 1490 | + 'type' => 'int', |
|
| 1491 | + 'required' => false, |
|
| 1492 | + 'validation' => true, |
|
| 1493 | + 'value' => $message_template_group->get('MTP_is_override'), |
|
| 1494 | + 'css_class' => '', |
|
| 1495 | + 'format' => '%d', |
|
| 1496 | + 'db-col' => 'MTP_is_override' |
|
| 1497 | + ); |
|
| 1498 | 1498 | |
| 1499 | - $sidebar_form_fields['ee-msg-is-active'] = array( |
|
| 1500 | - 'name' => 'MTP_is_active', |
|
| 1501 | - 'label' => __('Active Template', 'event_espresso'), |
|
| 1502 | - 'input' => 'hidden', |
|
| 1503 | - 'type' => 'int', |
|
| 1504 | - 'required' => false, |
|
| 1505 | - 'validation' => true, |
|
| 1506 | - 'value' => $message_template_group->is_active(), |
|
| 1507 | - 'css_class' => '', |
|
| 1508 | - 'format' => '%d', |
|
| 1509 | - 'db-col' => 'MTP_is_active' |
|
| 1510 | - ); |
|
| 1499 | + $sidebar_form_fields['ee-msg-is-active'] = array( |
|
| 1500 | + 'name' => 'MTP_is_active', |
|
| 1501 | + 'label' => __('Active Template', 'event_espresso'), |
|
| 1502 | + 'input' => 'hidden', |
|
| 1503 | + 'type' => 'int', |
|
| 1504 | + 'required' => false, |
|
| 1505 | + 'validation' => true, |
|
| 1506 | + 'value' => $message_template_group->is_active(), |
|
| 1507 | + 'css_class' => '', |
|
| 1508 | + 'format' => '%d', |
|
| 1509 | + 'db-col' => 'MTP_is_active' |
|
| 1510 | + ); |
|
| 1511 | 1511 | |
| 1512 | - $sidebar_form_fields['ee-msg-deleted'] = array( |
|
| 1513 | - 'name' => 'MTP_deleted', |
|
| 1514 | - 'label' => null, |
|
| 1515 | - 'input' => 'hidden', |
|
| 1516 | - 'type' => 'int', |
|
| 1517 | - 'required' => false, |
|
| 1518 | - 'validation' => true, |
|
| 1519 | - 'value' => $message_template_group->get('MTP_deleted'), |
|
| 1520 | - 'css_class' => '', |
|
| 1521 | - 'format' => '%d', |
|
| 1522 | - 'db-col' => 'MTP_deleted' |
|
| 1523 | - ); |
|
| 1524 | - $sidebar_form_fields['ee-msg-author'] = array( |
|
| 1525 | - 'name' => 'MTP_user_id', |
|
| 1526 | - 'label' => __('Author', 'event_espresso'), |
|
| 1527 | - 'input' => 'hidden', |
|
| 1528 | - 'type' => 'int', |
|
| 1529 | - 'required' => false, |
|
| 1530 | - 'validation' => false, |
|
| 1531 | - 'value' => $message_template_group->user(), |
|
| 1532 | - 'format' => '%d', |
|
| 1533 | - 'db-col' => 'MTP_user_id' |
|
| 1534 | - ); |
|
| 1512 | + $sidebar_form_fields['ee-msg-deleted'] = array( |
|
| 1513 | + 'name' => 'MTP_deleted', |
|
| 1514 | + 'label' => null, |
|
| 1515 | + 'input' => 'hidden', |
|
| 1516 | + 'type' => 'int', |
|
| 1517 | + 'required' => false, |
|
| 1518 | + 'validation' => true, |
|
| 1519 | + 'value' => $message_template_group->get('MTP_deleted'), |
|
| 1520 | + 'css_class' => '', |
|
| 1521 | + 'format' => '%d', |
|
| 1522 | + 'db-col' => 'MTP_deleted' |
|
| 1523 | + ); |
|
| 1524 | + $sidebar_form_fields['ee-msg-author'] = array( |
|
| 1525 | + 'name' => 'MTP_user_id', |
|
| 1526 | + 'label' => __('Author', 'event_espresso'), |
|
| 1527 | + 'input' => 'hidden', |
|
| 1528 | + 'type' => 'int', |
|
| 1529 | + 'required' => false, |
|
| 1530 | + 'validation' => false, |
|
| 1531 | + 'value' => $message_template_group->user(), |
|
| 1532 | + 'format' => '%d', |
|
| 1533 | + 'db-col' => 'MTP_user_id' |
|
| 1534 | + ); |
|
| 1535 | 1535 | |
| 1536 | - $sidebar_form_fields['ee-msg-route'] = array( |
|
| 1537 | - 'name' => 'action', |
|
| 1538 | - 'input' => 'hidden', |
|
| 1539 | - 'type' => 'string', |
|
| 1540 | - 'value' => $action |
|
| 1541 | - ); |
|
| 1536 | + $sidebar_form_fields['ee-msg-route'] = array( |
|
| 1537 | + 'name' => 'action', |
|
| 1538 | + 'input' => 'hidden', |
|
| 1539 | + 'type' => 'string', |
|
| 1540 | + 'value' => $action |
|
| 1541 | + ); |
|
| 1542 | 1542 | |
| 1543 | - $sidebar_form_fields['ee-msg-id'] = array( |
|
| 1544 | - 'name' => 'id', |
|
| 1545 | - 'input' => 'hidden', |
|
| 1546 | - 'type' => 'int', |
|
| 1547 | - 'value' => $GRP_ID |
|
| 1548 | - ); |
|
| 1549 | - $sidebar_form_fields['ee-msg-evt-nonce'] = array( |
|
| 1550 | - 'name' => $action . '_nonce', |
|
| 1551 | - 'input' => 'hidden', |
|
| 1552 | - 'type' => 'string', |
|
| 1553 | - 'value' => wp_create_nonce($action . '_nonce') |
|
| 1554 | - ); |
|
| 1543 | + $sidebar_form_fields['ee-msg-id'] = array( |
|
| 1544 | + 'name' => 'id', |
|
| 1545 | + 'input' => 'hidden', |
|
| 1546 | + 'type' => 'int', |
|
| 1547 | + 'value' => $GRP_ID |
|
| 1548 | + ); |
|
| 1549 | + $sidebar_form_fields['ee-msg-evt-nonce'] = array( |
|
| 1550 | + 'name' => $action . '_nonce', |
|
| 1551 | + 'input' => 'hidden', |
|
| 1552 | + 'type' => 'string', |
|
| 1553 | + 'value' => wp_create_nonce($action . '_nonce') |
|
| 1554 | + ); |
|
| 1555 | 1555 | |
| 1556 | - if (isset($this->_req_data['template_switch']) && $this->_req_data['template_switch']) { |
|
| 1557 | - $sidebar_form_fields['ee-msg-template-switch'] = array( |
|
| 1558 | - 'name' => 'template_switch', |
|
| 1559 | - 'input' => 'hidden', |
|
| 1560 | - 'type' => 'int', |
|
| 1561 | - 'value' => 1 |
|
| 1562 | - ); |
|
| 1563 | - } |
|
| 1556 | + if (isset($this->_req_data['template_switch']) && $this->_req_data['template_switch']) { |
|
| 1557 | + $sidebar_form_fields['ee-msg-template-switch'] = array( |
|
| 1558 | + 'name' => 'template_switch', |
|
| 1559 | + 'input' => 'hidden', |
|
| 1560 | + 'type' => 'int', |
|
| 1561 | + 'value' => 1 |
|
| 1562 | + ); |
|
| 1563 | + } |
|
| 1564 | 1564 | |
| 1565 | 1565 | |
| 1566 | - $template_fields = $this->_generate_admin_form_fields($template_form_fields); |
|
| 1567 | - $sidebar_fields = $this->_generate_admin_form_fields($sidebar_form_fields); |
|
| 1566 | + $template_fields = $this->_generate_admin_form_fields($template_form_fields); |
|
| 1567 | + $sidebar_fields = $this->_generate_admin_form_fields($sidebar_form_fields); |
|
| 1568 | 1568 | |
| 1569 | 1569 | |
| 1570 | - } //end if ( !empty($template_field_structure) ) |
|
| 1570 | + } //end if ( !empty($template_field_structure) ) |
|
| 1571 | 1571 | |
| 1572 | - //set extra content for publish box |
|
| 1573 | - $this->_template_args['publish_box_extra_content'] = $sidebar_fields; |
|
| 1574 | - $this->_set_publish_post_box_vars( |
|
| 1575 | - 'id', |
|
| 1576 | - $GRP_ID, |
|
| 1577 | - false, |
|
| 1578 | - add_query_arg( |
|
| 1579 | - array('action' => 'global_mtps'), |
|
| 1580 | - $this->_admin_base_url |
|
| 1581 | - ) |
|
| 1582 | - ); |
|
| 1583 | - |
|
| 1584 | - //add preview button |
|
| 1585 | - $preview_url = parent::add_query_args_and_nonce( |
|
| 1586 | - array( |
|
| 1587 | - 'message_type' => $message_template_group->message_type(), |
|
| 1588 | - 'messenger' => $message_template_group->messenger(), |
|
| 1589 | - 'context' => $context, |
|
| 1590 | - 'GRP_ID' => $GRP_ID, |
|
| 1591 | - 'action' => 'preview_message' |
|
| 1592 | - ), |
|
| 1593 | - $this->_admin_base_url |
|
| 1594 | - ); |
|
| 1595 | - $preview_button = '<a href="' . $preview_url . '" class="button-secondary messages-preview-button">' . __('Preview', |
|
| 1596 | - 'event_espresso') . '</a>'; |
|
| 1597 | - |
|
| 1598 | - |
|
| 1599 | - //setup context switcher |
|
| 1600 | - $context_switcher_args = array( |
|
| 1601 | - 'page' => 'espresso_messages', |
|
| 1602 | - 'action' => 'edit_message_template', |
|
| 1603 | - 'id' => $GRP_ID, |
|
| 1604 | - 'context' => $context, |
|
| 1605 | - 'extra' => $preview_button |
|
| 1606 | - ); |
|
| 1607 | - $this->_set_context_switcher($message_template_group, $context_switcher_args); |
|
| 1608 | - |
|
| 1609 | - |
|
| 1610 | - //main box |
|
| 1611 | - $this->_template_args['template_fields'] = $template_fields; |
|
| 1612 | - $this->_template_args['sidebar_box_id'] = 'details'; |
|
| 1613 | - $this->_template_args['action'] = $action; |
|
| 1614 | - $this->_template_args['context'] = $context; |
|
| 1615 | - $this->_template_args['edit_message_template_form_url'] = $edit_message_template_form_url; |
|
| 1616 | - $this->_template_args['learn_more_about_message_templates_link'] = $this->_learn_more_about_message_templates_link(); |
|
| 1617 | - |
|
| 1618 | - |
|
| 1619 | - $this->_template_args['before_admin_page_content'] = $this->add_context_switcher(); |
|
| 1620 | - $this->_template_args['before_admin_page_content'] .= $this->_add_form_element_before(); |
|
| 1621 | - $this->_template_args['after_admin_page_content'] = $this->_add_form_element_after(); |
|
| 1622 | - |
|
| 1623 | - $this->_template_path = $this->_template_args['GRP_ID'] |
|
| 1624 | - ? EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_edit_meta_box.template.php' |
|
| 1625 | - : EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_add_meta_box.template.php'; |
|
| 1626 | - |
|
| 1627 | - //send along EE_Message_Template_Group object for further template use. |
|
| 1628 | - $this->_template_args['MTP'] = $message_template_group; |
|
| 1629 | - |
|
| 1630 | - $this->_template_args['admin_page_content'] = EEH_Template::display_template($this->_template_path, |
|
| 1631 | - $this->_template_args, true); |
|
| 1632 | - |
|
| 1633 | - |
|
| 1634 | - //finally, let's set the admin_page title |
|
| 1635 | - $this->_admin_page_title = sprintf(__('Editing %s', 'event_espresso'), $title); |
|
| 1636 | - |
|
| 1637 | - |
|
| 1638 | - //we need to take care of setting the shortcodes property for use elsewhere. |
|
| 1639 | - $this->_set_shortcodes(); |
|
| 1640 | - |
|
| 1641 | - |
|
| 1642 | - //final template wrapper |
|
| 1643 | - $this->display_admin_page_with_sidebar(); |
|
| 1644 | - } |
|
| 1645 | - |
|
| 1646 | - |
|
| 1647 | - public function filter_tinymce_init($mceInit, $editor_id) |
|
| 1648 | - { |
|
| 1649 | - return $mceInit; |
|
| 1650 | - } |
|
| 1651 | - |
|
| 1652 | - |
|
| 1653 | - public function add_context_switcher() |
|
| 1654 | - { |
|
| 1655 | - return $this->_context_switcher; |
|
| 1656 | - } |
|
| 1657 | - |
|
| 1658 | - public function _add_form_element_before() |
|
| 1659 | - { |
|
| 1660 | - return '<form method="post" action="' . $this->_template_args["edit_message_template_form_url"] . '" id="ee-msg-edit-frm">'; |
|
| 1661 | - } |
|
| 1662 | - |
|
| 1663 | - public function _add_form_element_after() |
|
| 1664 | - { |
|
| 1665 | - return '</form>'; |
|
| 1666 | - } |
|
| 1667 | - |
|
| 1668 | - |
|
| 1669 | - /** |
|
| 1670 | - * This executes switching the template pack for a message template. |
|
| 1671 | - * |
|
| 1672 | - * @since 4.5.0 |
|
| 1673 | - * |
|
| 1674 | - */ |
|
| 1675 | - public function switch_template_pack() |
|
| 1676 | - { |
|
| 1677 | - $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0; |
|
| 1678 | - $template_pack = ! empty($this->_req_data['template_pack']) ? $this->_req_data['template_pack'] : ''; |
|
| 1679 | - |
|
| 1680 | - //verify we have needed values. |
|
| 1681 | - if (empty($GRP_ID) || empty($template_pack)) { |
|
| 1682 | - $this->_template_args['error'] = true; |
|
| 1683 | - EE_Error::add_error(__('The required date for switching templates is not available.', 'event_espresso'), |
|
| 1684 | - __FILE__, __FUNCTION__, __LINE__); |
|
| 1685 | - } else { |
|
| 1686 | - //get template, set the new template_pack and then reset to default |
|
| 1687 | - /** @type EE_Message_Template_Group $message_template_group */ |
|
| 1688 | - $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID); |
|
| 1572 | + //set extra content for publish box |
|
| 1573 | + $this->_template_args['publish_box_extra_content'] = $sidebar_fields; |
|
| 1574 | + $this->_set_publish_post_box_vars( |
|
| 1575 | + 'id', |
|
| 1576 | + $GRP_ID, |
|
| 1577 | + false, |
|
| 1578 | + add_query_arg( |
|
| 1579 | + array('action' => 'global_mtps'), |
|
| 1580 | + $this->_admin_base_url |
|
| 1581 | + ) |
|
| 1582 | + ); |
|
| 1583 | + |
|
| 1584 | + //add preview button |
|
| 1585 | + $preview_url = parent::add_query_args_and_nonce( |
|
| 1586 | + array( |
|
| 1587 | + 'message_type' => $message_template_group->message_type(), |
|
| 1588 | + 'messenger' => $message_template_group->messenger(), |
|
| 1589 | + 'context' => $context, |
|
| 1590 | + 'GRP_ID' => $GRP_ID, |
|
| 1591 | + 'action' => 'preview_message' |
|
| 1592 | + ), |
|
| 1593 | + $this->_admin_base_url |
|
| 1594 | + ); |
|
| 1595 | + $preview_button = '<a href="' . $preview_url . '" class="button-secondary messages-preview-button">' . __('Preview', |
|
| 1596 | + 'event_espresso') . '</a>'; |
|
| 1597 | + |
|
| 1598 | + |
|
| 1599 | + //setup context switcher |
|
| 1600 | + $context_switcher_args = array( |
|
| 1601 | + 'page' => 'espresso_messages', |
|
| 1602 | + 'action' => 'edit_message_template', |
|
| 1603 | + 'id' => $GRP_ID, |
|
| 1604 | + 'context' => $context, |
|
| 1605 | + 'extra' => $preview_button |
|
| 1606 | + ); |
|
| 1607 | + $this->_set_context_switcher($message_template_group, $context_switcher_args); |
|
| 1608 | + |
|
| 1609 | + |
|
| 1610 | + //main box |
|
| 1611 | + $this->_template_args['template_fields'] = $template_fields; |
|
| 1612 | + $this->_template_args['sidebar_box_id'] = 'details'; |
|
| 1613 | + $this->_template_args['action'] = $action; |
|
| 1614 | + $this->_template_args['context'] = $context; |
|
| 1615 | + $this->_template_args['edit_message_template_form_url'] = $edit_message_template_form_url; |
|
| 1616 | + $this->_template_args['learn_more_about_message_templates_link'] = $this->_learn_more_about_message_templates_link(); |
|
| 1617 | + |
|
| 1618 | + |
|
| 1619 | + $this->_template_args['before_admin_page_content'] = $this->add_context_switcher(); |
|
| 1620 | + $this->_template_args['before_admin_page_content'] .= $this->_add_form_element_before(); |
|
| 1621 | + $this->_template_args['after_admin_page_content'] = $this->_add_form_element_after(); |
|
| 1622 | + |
|
| 1623 | + $this->_template_path = $this->_template_args['GRP_ID'] |
|
| 1624 | + ? EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_edit_meta_box.template.php' |
|
| 1625 | + : EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_add_meta_box.template.php'; |
|
| 1626 | + |
|
| 1627 | + //send along EE_Message_Template_Group object for further template use. |
|
| 1628 | + $this->_template_args['MTP'] = $message_template_group; |
|
| 1629 | + |
|
| 1630 | + $this->_template_args['admin_page_content'] = EEH_Template::display_template($this->_template_path, |
|
| 1631 | + $this->_template_args, true); |
|
| 1632 | + |
|
| 1633 | + |
|
| 1634 | + //finally, let's set the admin_page title |
|
| 1635 | + $this->_admin_page_title = sprintf(__('Editing %s', 'event_espresso'), $title); |
|
| 1636 | + |
|
| 1637 | + |
|
| 1638 | + //we need to take care of setting the shortcodes property for use elsewhere. |
|
| 1639 | + $this->_set_shortcodes(); |
|
| 1640 | + |
|
| 1641 | + |
|
| 1642 | + //final template wrapper |
|
| 1643 | + $this->display_admin_page_with_sidebar(); |
|
| 1644 | + } |
|
| 1645 | + |
|
| 1646 | + |
|
| 1647 | + public function filter_tinymce_init($mceInit, $editor_id) |
|
| 1648 | + { |
|
| 1649 | + return $mceInit; |
|
| 1650 | + } |
|
| 1651 | + |
|
| 1652 | + |
|
| 1653 | + public function add_context_switcher() |
|
| 1654 | + { |
|
| 1655 | + return $this->_context_switcher; |
|
| 1656 | + } |
|
| 1657 | + |
|
| 1658 | + public function _add_form_element_before() |
|
| 1659 | + { |
|
| 1660 | + return '<form method="post" action="' . $this->_template_args["edit_message_template_form_url"] . '" id="ee-msg-edit-frm">'; |
|
| 1661 | + } |
|
| 1662 | + |
|
| 1663 | + public function _add_form_element_after() |
|
| 1664 | + { |
|
| 1665 | + return '</form>'; |
|
| 1666 | + } |
|
| 1667 | + |
|
| 1668 | + |
|
| 1669 | + /** |
|
| 1670 | + * This executes switching the template pack for a message template. |
|
| 1671 | + * |
|
| 1672 | + * @since 4.5.0 |
|
| 1673 | + * |
|
| 1674 | + */ |
|
| 1675 | + public function switch_template_pack() |
|
| 1676 | + { |
|
| 1677 | + $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0; |
|
| 1678 | + $template_pack = ! empty($this->_req_data['template_pack']) ? $this->_req_data['template_pack'] : ''; |
|
| 1679 | + |
|
| 1680 | + //verify we have needed values. |
|
| 1681 | + if (empty($GRP_ID) || empty($template_pack)) { |
|
| 1682 | + $this->_template_args['error'] = true; |
|
| 1683 | + EE_Error::add_error(__('The required date for switching templates is not available.', 'event_espresso'), |
|
| 1684 | + __FILE__, __FUNCTION__, __LINE__); |
|
| 1685 | + } else { |
|
| 1686 | + //get template, set the new template_pack and then reset to default |
|
| 1687 | + /** @type EE_Message_Template_Group $message_template_group */ |
|
| 1688 | + $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID); |
|
| 1689 | 1689 | |
| 1690 | - $message_template_group->set_template_pack_name($template_pack); |
|
| 1691 | - $this->_req_data['msgr'] = $message_template_group->messenger(); |
|
| 1692 | - $this->_req_data['mt'] = $message_template_group->message_type(); |
|
| 1690 | + $message_template_group->set_template_pack_name($template_pack); |
|
| 1691 | + $this->_req_data['msgr'] = $message_template_group->messenger(); |
|
| 1692 | + $this->_req_data['mt'] = $message_template_group->message_type(); |
|
| 1693 | 1693 | |
| 1694 | - $query_args = $this->_reset_to_default_template(); |
|
| 1694 | + $query_args = $this->_reset_to_default_template(); |
|
| 1695 | 1695 | |
| 1696 | - if (empty($query_args['id'])) { |
|
| 1697 | - EE_Error::add_error( |
|
| 1698 | - __( |
|
| 1699 | - 'Something went wrong with switching the template pack. Please try again or contact EE support', |
|
| 1700 | - 'event_espresso' |
|
| 1701 | - ), |
|
| 1702 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1703 | - ); |
|
| 1704 | - $this->_template_args['error'] = true; |
|
| 1705 | - } else { |
|
| 1706 | - $template_label = $message_template_group->get_template_pack()->label; |
|
| 1707 | - $template_pack_labels = $message_template_group->messenger_obj()->get_supports_labels(); |
|
| 1708 | - EE_Error::add_success( |
|
| 1709 | - sprintf( |
|
| 1710 | - __( |
|
| 1711 | - 'This message template has been successfully switched to use the %1$s %2$s. Please wait while the page reloads with your new template.', |
|
| 1712 | - 'event_espresso' |
|
| 1713 | - ), |
|
| 1714 | - $template_label, |
|
| 1715 | - $template_pack_labels->template_pack |
|
| 1716 | - ) |
|
| 1717 | - ); |
|
| 1718 | - //generate the redirect url for js. |
|
| 1719 | - $url = self::add_query_args_and_nonce($query_args, |
|
| 1720 | - $this->_admin_base_url); |
|
| 1721 | - $this->_template_args['data']['redirect_url'] = $url; |
|
| 1722 | - $this->_template_args['success'] = true; |
|
| 1723 | - } |
|
| 1696 | + if (empty($query_args['id'])) { |
|
| 1697 | + EE_Error::add_error( |
|
| 1698 | + __( |
|
| 1699 | + 'Something went wrong with switching the template pack. Please try again or contact EE support', |
|
| 1700 | + 'event_espresso' |
|
| 1701 | + ), |
|
| 1702 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1703 | + ); |
|
| 1704 | + $this->_template_args['error'] = true; |
|
| 1705 | + } else { |
|
| 1706 | + $template_label = $message_template_group->get_template_pack()->label; |
|
| 1707 | + $template_pack_labels = $message_template_group->messenger_obj()->get_supports_labels(); |
|
| 1708 | + EE_Error::add_success( |
|
| 1709 | + sprintf( |
|
| 1710 | + __( |
|
| 1711 | + 'This message template has been successfully switched to use the %1$s %2$s. Please wait while the page reloads with your new template.', |
|
| 1712 | + 'event_espresso' |
|
| 1713 | + ), |
|
| 1714 | + $template_label, |
|
| 1715 | + $template_pack_labels->template_pack |
|
| 1716 | + ) |
|
| 1717 | + ); |
|
| 1718 | + //generate the redirect url for js. |
|
| 1719 | + $url = self::add_query_args_and_nonce($query_args, |
|
| 1720 | + $this->_admin_base_url); |
|
| 1721 | + $this->_template_args['data']['redirect_url'] = $url; |
|
| 1722 | + $this->_template_args['success'] = true; |
|
| 1723 | + } |
|
| 1724 | 1724 | |
| 1725 | - $this->_return_json(); |
|
| 1725 | + $this->_return_json(); |
|
| 1726 | 1726 | |
| 1727 | - } |
|
| 1728 | - } |
|
| 1729 | - |
|
| 1730 | - |
|
| 1731 | - /** |
|
| 1732 | - * This handles resetting the template for the given messenger/message_type so that users can start from scratch if |
|
| 1733 | - * they want. |
|
| 1734 | - * |
|
| 1735 | - * @access protected |
|
| 1736 | - * @return array|null |
|
| 1737 | - */ |
|
| 1738 | - protected function _reset_to_default_template() |
|
| 1739 | - { |
|
| 1740 | - |
|
| 1741 | - $templates = array(); |
|
| 1742 | - $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0; |
|
| 1743 | - //we need to make sure we've got the info we need. |
|
| 1744 | - if ( ! isset($this->_req_data['msgr'], $this->_req_data['mt'], $this->_req_data['GRP_ID'])) { |
|
| 1745 | - EE_Error::add_error( |
|
| 1746 | - __( |
|
| 1747 | - 'In order to reset the template to its default we require the messenger, message type, and message template GRP_ID to know what is being reset. At least one of these is missing.', |
|
| 1748 | - 'event_espresso' |
|
| 1749 | - ), |
|
| 1750 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1751 | - ); |
|
| 1752 | - } |
|
| 1753 | - |
|
| 1754 | - // all templates will be reset to whatever the defaults are |
|
| 1755 | - // for the global template matching the messenger and message type. |
|
| 1756 | - $success = ! empty($GRP_ID) ? true : false; |
|
| 1757 | - |
|
| 1758 | - if ($success) { |
|
| 1727 | + } |
|
| 1728 | + } |
|
| 1729 | + |
|
| 1730 | + |
|
| 1731 | + /** |
|
| 1732 | + * This handles resetting the template for the given messenger/message_type so that users can start from scratch if |
|
| 1733 | + * they want. |
|
| 1734 | + * |
|
| 1735 | + * @access protected |
|
| 1736 | + * @return array|null |
|
| 1737 | + */ |
|
| 1738 | + protected function _reset_to_default_template() |
|
| 1739 | + { |
|
| 1740 | + |
|
| 1741 | + $templates = array(); |
|
| 1742 | + $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0; |
|
| 1743 | + //we need to make sure we've got the info we need. |
|
| 1744 | + if ( ! isset($this->_req_data['msgr'], $this->_req_data['mt'], $this->_req_data['GRP_ID'])) { |
|
| 1745 | + EE_Error::add_error( |
|
| 1746 | + __( |
|
| 1747 | + 'In order to reset the template to its default we require the messenger, message type, and message template GRP_ID to know what is being reset. At least one of these is missing.', |
|
| 1748 | + 'event_espresso' |
|
| 1749 | + ), |
|
| 1750 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1751 | + ); |
|
| 1752 | + } |
|
| 1753 | + |
|
| 1754 | + // all templates will be reset to whatever the defaults are |
|
| 1755 | + // for the global template matching the messenger and message type. |
|
| 1756 | + $success = ! empty($GRP_ID) ? true : false; |
|
| 1757 | + |
|
| 1758 | + if ($success) { |
|
| 1759 | 1759 | |
| 1760 | - //let's first determine if the incoming template is a global template, |
|
| 1761 | - // if it isn't then we need to get the global template matching messenger and message type. |
|
| 1762 | - //$MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID( $GRP_ID ); |
|
| 1760 | + //let's first determine if the incoming template is a global template, |
|
| 1761 | + // if it isn't then we need to get the global template matching messenger and message type. |
|
| 1762 | + //$MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID( $GRP_ID ); |
|
| 1763 | 1763 | |
| 1764 | 1764 | |
| 1765 | - //note this is ONLY deleting the template fields (Message Template rows) NOT the message template group. |
|
| 1766 | - $success = $this->_delete_mtp_permanently($GRP_ID, false); |
|
| 1765 | + //note this is ONLY deleting the template fields (Message Template rows) NOT the message template group. |
|
| 1766 | + $success = $this->_delete_mtp_permanently($GRP_ID, false); |
|
| 1767 | 1767 | |
| 1768 | - if ($success) { |
|
| 1769 | - // if successfully deleted, lets generate the new ones. |
|
| 1770 | - // Note. We set GLOBAL to true, because resets on ANY template |
|
| 1771 | - // will use the related global template defaults for regeneration. |
|
| 1772 | - // This means that if a custom template is reset it resets to whatever the related global template is. |
|
| 1773 | - // HOWEVER, we DO keep the template pack and template variation set |
|
| 1774 | - // for the current custom template when resetting. |
|
| 1775 | - $templates = $this->_generate_new_templates( |
|
| 1776 | - $this->_req_data['msgr'], |
|
| 1777 | - $this->_req_data['mt'], |
|
| 1778 | - $GRP_ID, |
|
| 1779 | - true |
|
| 1780 | - ); |
|
| 1781 | - } |
|
| 1768 | + if ($success) { |
|
| 1769 | + // if successfully deleted, lets generate the new ones. |
|
| 1770 | + // Note. We set GLOBAL to true, because resets on ANY template |
|
| 1771 | + // will use the related global template defaults for regeneration. |
|
| 1772 | + // This means that if a custom template is reset it resets to whatever the related global template is. |
|
| 1773 | + // HOWEVER, we DO keep the template pack and template variation set |
|
| 1774 | + // for the current custom template when resetting. |
|
| 1775 | + $templates = $this->_generate_new_templates( |
|
| 1776 | + $this->_req_data['msgr'], |
|
| 1777 | + $this->_req_data['mt'], |
|
| 1778 | + $GRP_ID, |
|
| 1779 | + true |
|
| 1780 | + ); |
|
| 1781 | + } |
|
| 1782 | 1782 | |
| 1783 | - } |
|
| 1784 | - |
|
| 1785 | - //any error messages? |
|
| 1786 | - if ( ! $success) { |
|
| 1787 | - EE_Error::add_error( |
|
| 1788 | - __('Something went wrong with deleting existing templates. Unable to reset to default', |
|
| 1789 | - 'event_espresso'), |
|
| 1790 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1791 | - ); |
|
| 1792 | - } |
|
| 1793 | - |
|
| 1794 | - //all good, let's add a success message! |
|
| 1795 | - if ($success && ! empty($templates)) { |
|
| 1796 | - $templates = $templates[0]; //the info for the template we generated is the first element in the returned array. |
|
| 1797 | - EE_Error::overwrite_success(); |
|
| 1798 | - EE_Error::add_success(__('Templates have been reset to defaults.', 'event_espresso')); |
|
| 1799 | - } |
|
| 1800 | - |
|
| 1801 | - |
|
| 1802 | - $query_args = array( |
|
| 1803 | - 'id' => isset($templates['GRP_ID']) ? $templates['GRP_ID'] : null, |
|
| 1804 | - 'context' => isset($templates['MTP_context']) ? $templates['MTP_context'] : null, |
|
| 1805 | - 'action' => isset($templates['GRP_ID']) ? 'edit_message_template' : 'global_mtps' |
|
| 1806 | - ); |
|
| 1807 | - |
|
| 1808 | - //if called via ajax then we return query args otherwise redirect |
|
| 1809 | - if (defined('DOING_AJAX') && DOING_AJAX) { |
|
| 1810 | - return $query_args; |
|
| 1811 | - } else { |
|
| 1812 | - $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 1783 | + } |
|
| 1784 | + |
|
| 1785 | + //any error messages? |
|
| 1786 | + if ( ! $success) { |
|
| 1787 | + EE_Error::add_error( |
|
| 1788 | + __('Something went wrong with deleting existing templates. Unable to reset to default', |
|
| 1789 | + 'event_espresso'), |
|
| 1790 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1791 | + ); |
|
| 1792 | + } |
|
| 1793 | + |
|
| 1794 | + //all good, let's add a success message! |
|
| 1795 | + if ($success && ! empty($templates)) { |
|
| 1796 | + $templates = $templates[0]; //the info for the template we generated is the first element in the returned array. |
|
| 1797 | + EE_Error::overwrite_success(); |
|
| 1798 | + EE_Error::add_success(__('Templates have been reset to defaults.', 'event_espresso')); |
|
| 1799 | + } |
|
| 1800 | + |
|
| 1801 | + |
|
| 1802 | + $query_args = array( |
|
| 1803 | + 'id' => isset($templates['GRP_ID']) ? $templates['GRP_ID'] : null, |
|
| 1804 | + 'context' => isset($templates['MTP_context']) ? $templates['MTP_context'] : null, |
|
| 1805 | + 'action' => isset($templates['GRP_ID']) ? 'edit_message_template' : 'global_mtps' |
|
| 1806 | + ); |
|
| 1807 | + |
|
| 1808 | + //if called via ajax then we return query args otherwise redirect |
|
| 1809 | + if (defined('DOING_AJAX') && DOING_AJAX) { |
|
| 1810 | + return $query_args; |
|
| 1811 | + } else { |
|
| 1812 | + $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 1813 | 1813 | |
| 1814 | - return null; |
|
| 1815 | - } |
|
| 1816 | - } |
|
| 1817 | - |
|
| 1818 | - |
|
| 1819 | - /** |
|
| 1820 | - * Retrieve and set the message preview for display. |
|
| 1821 | - * |
|
| 1822 | - * @param bool $send if TRUE then we are doing an actual TEST send with the results of the preview. |
|
| 1823 | - * |
|
| 1824 | - * @return string |
|
| 1825 | - */ |
|
| 1826 | - public function _preview_message($send = false) |
|
| 1827 | - { |
|
| 1828 | - //first make sure we've got the necessary parameters |
|
| 1829 | - if ( |
|
| 1830 | - ! isset( |
|
| 1831 | - $this->_req_data['message_type'], |
|
| 1832 | - $this->_req_data['messenger'], |
|
| 1833 | - $this->_req_data['messenger'], |
|
| 1834 | - $this->_req_data['GRP_ID'] |
|
| 1835 | - ) |
|
| 1836 | - ) { |
|
| 1837 | - EE_Error::add_error( |
|
| 1838 | - __('Missing necessary parameters for displaying preview', 'event_espresso'), |
|
| 1839 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1840 | - ); |
|
| 1841 | - } |
|
| 1842 | - |
|
| 1843 | - EE_Registry::instance()->REQ->set('GRP_ID', $this->_req_data['GRP_ID']); |
|
| 1844 | - |
|
| 1845 | - |
|
| 1846 | - //get the preview! |
|
| 1847 | - $preview = EED_Messages::preview_message($this->_req_data['message_type'], $this->_req_data['context'], |
|
| 1848 | - $this->_req_data['messenger'], $send); |
|
| 1849 | - |
|
| 1850 | - if ($send) { |
|
| 1851 | - return $preview; |
|
| 1852 | - } |
|
| 1853 | - |
|
| 1854 | - //let's add a button to go back to the edit view |
|
| 1855 | - $query_args = array( |
|
| 1856 | - 'id' => $this->_req_data['GRP_ID'], |
|
| 1857 | - 'context' => $this->_req_data['context'], |
|
| 1858 | - 'action' => 'edit_message_template' |
|
| 1859 | - ); |
|
| 1860 | - $go_back_url = parent::add_query_args_and_nonce($query_args, $this->_admin_base_url); |
|
| 1861 | - $preview_button = '<a href="' . $go_back_url . '" class="button-secondary messages-preview-go-back-button">' . __('Go Back to Edit', |
|
| 1862 | - 'event_espresso') . '</a>'; |
|
| 1863 | - $message_types = $this->get_installed_message_types(); |
|
| 1864 | - $active_messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']); |
|
| 1865 | - $active_messenger_label = $active_messenger instanceof EE_messenger |
|
| 1866 | - ? ucwords($active_messenger->label['singular']) |
|
| 1867 | - : esc_html__('Unknown Messenger', 'event_espresso'); |
|
| 1868 | - //let's provide a helpful title for context |
|
| 1869 | - $preview_title = sprintf( |
|
| 1870 | - __('Viewing Preview for %s %s Message Template', 'event_espresso'), |
|
| 1871 | - $active_messenger_label, |
|
| 1872 | - ucwords($message_types[$this->_req_data['message_type']]->label['singular']) |
|
| 1873 | - ); |
|
| 1874 | - //setup display of preview. |
|
| 1875 | - $this->_admin_page_title = $preview_title; |
|
| 1876 | - $this->_template_args['admin_page_content'] = $preview_button . '<br />' . stripslashes($preview); |
|
| 1877 | - $this->_template_args['data']['force_json'] = true; |
|
| 1878 | - |
|
| 1879 | - return ''; |
|
| 1880 | - } |
|
| 1881 | - |
|
| 1882 | - |
|
| 1883 | - /** |
|
| 1884 | - * The initial _preview_message is on a no headers route. It will optionally call this if necessary otherwise it |
|
| 1885 | - * gets called automatically. |
|
| 1886 | - * |
|
| 1887 | - * @since 4.5.0 |
|
| 1888 | - * |
|
| 1889 | - * @return string |
|
| 1890 | - */ |
|
| 1891 | - protected function _display_preview_message() |
|
| 1892 | - { |
|
| 1893 | - $this->display_admin_page_with_no_sidebar(); |
|
| 1894 | - } |
|
| 1895 | - |
|
| 1896 | - |
|
| 1897 | - /** |
|
| 1898 | - * registers metaboxes that should show up on the "edit_message_template" page |
|
| 1899 | - * |
|
| 1900 | - * @access protected |
|
| 1901 | - * @return void |
|
| 1902 | - */ |
|
| 1903 | - protected function _register_edit_meta_boxes() |
|
| 1904 | - { |
|
| 1905 | - add_meta_box('mtp_valid_shortcodes', __('Valid Shortcodes', 'event_espresso'), |
|
| 1906 | - array($this, 'shortcode_meta_box'), $this->_current_screen->id, 'side', 'default'); |
|
| 1907 | - add_meta_box('mtp_extra_actions', __('Extra Actions', 'event_espresso'), array($this, 'extra_actions_meta_box'), |
|
| 1908 | - $this->_current_screen->id, 'side', 'high'); |
|
| 1909 | - add_meta_box('mtp_templates', __('Template Styles', 'event_espresso'), array($this, 'template_pack_meta_box'), |
|
| 1910 | - $this->_current_screen->id, 'side', 'high'); |
|
| 1911 | - } |
|
| 1912 | - |
|
| 1913 | - |
|
| 1914 | - /** |
|
| 1915 | - * metabox content for all template pack and variation selection. |
|
| 1916 | - * |
|
| 1917 | - * @since 4.5.0 |
|
| 1918 | - * |
|
| 1919 | - * @return string |
|
| 1920 | - */ |
|
| 1921 | - public function template_pack_meta_box() |
|
| 1922 | - { |
|
| 1923 | - $this->_set_message_template_group(); |
|
| 1924 | - |
|
| 1925 | - $tp_collection = EEH_MSG_Template::get_template_pack_collection(); |
|
| 1926 | - |
|
| 1927 | - $tp_select_values = array(); |
|
| 1928 | - |
|
| 1929 | - foreach ($tp_collection as $tp) { |
|
| 1930 | - //only include template packs that support this messenger and message type! |
|
| 1931 | - $supports = $tp->get_supports(); |
|
| 1932 | - if ( |
|
| 1933 | - ! isset($supports[$this->_message_template_group->messenger()]) |
|
| 1934 | - || ! in_array( |
|
| 1935 | - $this->_message_template_group->message_type(), |
|
| 1936 | - $supports[$this->_message_template_group->messenger()] |
|
| 1937 | - ) |
|
| 1938 | - ) { |
|
| 1939 | - //not supported |
|
| 1940 | - continue; |
|
| 1941 | - } |
|
| 1814 | + return null; |
|
| 1815 | + } |
|
| 1816 | + } |
|
| 1817 | + |
|
| 1818 | + |
|
| 1819 | + /** |
|
| 1820 | + * Retrieve and set the message preview for display. |
|
| 1821 | + * |
|
| 1822 | + * @param bool $send if TRUE then we are doing an actual TEST send with the results of the preview. |
|
| 1823 | + * |
|
| 1824 | + * @return string |
|
| 1825 | + */ |
|
| 1826 | + public function _preview_message($send = false) |
|
| 1827 | + { |
|
| 1828 | + //first make sure we've got the necessary parameters |
|
| 1829 | + if ( |
|
| 1830 | + ! isset( |
|
| 1831 | + $this->_req_data['message_type'], |
|
| 1832 | + $this->_req_data['messenger'], |
|
| 1833 | + $this->_req_data['messenger'], |
|
| 1834 | + $this->_req_data['GRP_ID'] |
|
| 1835 | + ) |
|
| 1836 | + ) { |
|
| 1837 | + EE_Error::add_error( |
|
| 1838 | + __('Missing necessary parameters for displaying preview', 'event_espresso'), |
|
| 1839 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1840 | + ); |
|
| 1841 | + } |
|
| 1842 | + |
|
| 1843 | + EE_Registry::instance()->REQ->set('GRP_ID', $this->_req_data['GRP_ID']); |
|
| 1844 | + |
|
| 1845 | + |
|
| 1846 | + //get the preview! |
|
| 1847 | + $preview = EED_Messages::preview_message($this->_req_data['message_type'], $this->_req_data['context'], |
|
| 1848 | + $this->_req_data['messenger'], $send); |
|
| 1849 | + |
|
| 1850 | + if ($send) { |
|
| 1851 | + return $preview; |
|
| 1852 | + } |
|
| 1853 | + |
|
| 1854 | + //let's add a button to go back to the edit view |
|
| 1855 | + $query_args = array( |
|
| 1856 | + 'id' => $this->_req_data['GRP_ID'], |
|
| 1857 | + 'context' => $this->_req_data['context'], |
|
| 1858 | + 'action' => 'edit_message_template' |
|
| 1859 | + ); |
|
| 1860 | + $go_back_url = parent::add_query_args_and_nonce($query_args, $this->_admin_base_url); |
|
| 1861 | + $preview_button = '<a href="' . $go_back_url . '" class="button-secondary messages-preview-go-back-button">' . __('Go Back to Edit', |
|
| 1862 | + 'event_espresso') . '</a>'; |
|
| 1863 | + $message_types = $this->get_installed_message_types(); |
|
| 1864 | + $active_messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']); |
|
| 1865 | + $active_messenger_label = $active_messenger instanceof EE_messenger |
|
| 1866 | + ? ucwords($active_messenger->label['singular']) |
|
| 1867 | + : esc_html__('Unknown Messenger', 'event_espresso'); |
|
| 1868 | + //let's provide a helpful title for context |
|
| 1869 | + $preview_title = sprintf( |
|
| 1870 | + __('Viewing Preview for %s %s Message Template', 'event_espresso'), |
|
| 1871 | + $active_messenger_label, |
|
| 1872 | + ucwords($message_types[$this->_req_data['message_type']]->label['singular']) |
|
| 1873 | + ); |
|
| 1874 | + //setup display of preview. |
|
| 1875 | + $this->_admin_page_title = $preview_title; |
|
| 1876 | + $this->_template_args['admin_page_content'] = $preview_button . '<br />' . stripslashes($preview); |
|
| 1877 | + $this->_template_args['data']['force_json'] = true; |
|
| 1878 | + |
|
| 1879 | + return ''; |
|
| 1880 | + } |
|
| 1881 | + |
|
| 1882 | + |
|
| 1883 | + /** |
|
| 1884 | + * The initial _preview_message is on a no headers route. It will optionally call this if necessary otherwise it |
|
| 1885 | + * gets called automatically. |
|
| 1886 | + * |
|
| 1887 | + * @since 4.5.0 |
|
| 1888 | + * |
|
| 1889 | + * @return string |
|
| 1890 | + */ |
|
| 1891 | + protected function _display_preview_message() |
|
| 1892 | + { |
|
| 1893 | + $this->display_admin_page_with_no_sidebar(); |
|
| 1894 | + } |
|
| 1895 | + |
|
| 1896 | + |
|
| 1897 | + /** |
|
| 1898 | + * registers metaboxes that should show up on the "edit_message_template" page |
|
| 1899 | + * |
|
| 1900 | + * @access protected |
|
| 1901 | + * @return void |
|
| 1902 | + */ |
|
| 1903 | + protected function _register_edit_meta_boxes() |
|
| 1904 | + { |
|
| 1905 | + add_meta_box('mtp_valid_shortcodes', __('Valid Shortcodes', 'event_espresso'), |
|
| 1906 | + array($this, 'shortcode_meta_box'), $this->_current_screen->id, 'side', 'default'); |
|
| 1907 | + add_meta_box('mtp_extra_actions', __('Extra Actions', 'event_espresso'), array($this, 'extra_actions_meta_box'), |
|
| 1908 | + $this->_current_screen->id, 'side', 'high'); |
|
| 1909 | + add_meta_box('mtp_templates', __('Template Styles', 'event_espresso'), array($this, 'template_pack_meta_box'), |
|
| 1910 | + $this->_current_screen->id, 'side', 'high'); |
|
| 1911 | + } |
|
| 1912 | + |
|
| 1913 | + |
|
| 1914 | + /** |
|
| 1915 | + * metabox content for all template pack and variation selection. |
|
| 1916 | + * |
|
| 1917 | + * @since 4.5.0 |
|
| 1918 | + * |
|
| 1919 | + * @return string |
|
| 1920 | + */ |
|
| 1921 | + public function template_pack_meta_box() |
|
| 1922 | + { |
|
| 1923 | + $this->_set_message_template_group(); |
|
| 1924 | + |
|
| 1925 | + $tp_collection = EEH_MSG_Template::get_template_pack_collection(); |
|
| 1926 | + |
|
| 1927 | + $tp_select_values = array(); |
|
| 1928 | + |
|
| 1929 | + foreach ($tp_collection as $tp) { |
|
| 1930 | + //only include template packs that support this messenger and message type! |
|
| 1931 | + $supports = $tp->get_supports(); |
|
| 1932 | + if ( |
|
| 1933 | + ! isset($supports[$this->_message_template_group->messenger()]) |
|
| 1934 | + || ! in_array( |
|
| 1935 | + $this->_message_template_group->message_type(), |
|
| 1936 | + $supports[$this->_message_template_group->messenger()] |
|
| 1937 | + ) |
|
| 1938 | + ) { |
|
| 1939 | + //not supported |
|
| 1940 | + continue; |
|
| 1941 | + } |
|
| 1942 | 1942 | |
| 1943 | - $tp_select_values[] = array( |
|
| 1944 | - 'text' => $tp->label, |
|
| 1945 | - 'id' => $tp->dbref |
|
| 1946 | - ); |
|
| 1947 | - } |
|
| 1948 | - |
|
| 1949 | - //if empty $tp_select_values then we make sure default is set because EVERY message type should be supported by the default template pack. This still allows for the odd template pack to override. |
|
| 1950 | - if (empty($tp_select_values)) { |
|
| 1951 | - $tp_select_values[] = array( |
|
| 1952 | - 'text' => __('Default', 'event_espresso'), |
|
| 1953 | - 'id' => 'default' |
|
| 1954 | - ); |
|
| 1955 | - } |
|
| 1956 | - |
|
| 1957 | - //setup variation select values for the currently selected template. |
|
| 1958 | - $variations = $this->_message_template_group->get_template_pack()->get_variations( |
|
| 1959 | - $this->_message_template_group->messenger(), |
|
| 1960 | - $this->_message_template_group->message_type() |
|
| 1961 | - ); |
|
| 1962 | - $variations_select_values = array(); |
|
| 1963 | - foreach ($variations as $variation => $label) { |
|
| 1964 | - $variations_select_values[] = array( |
|
| 1965 | - 'text' => $label, |
|
| 1966 | - 'id' => $variation |
|
| 1967 | - ); |
|
| 1968 | - } |
|
| 1969 | - |
|
| 1970 | - $template_pack_labels = $this->_message_template_group->messenger_obj()->get_supports_labels(); |
|
| 1971 | - |
|
| 1972 | - $template_args['template_packs_selector'] = EEH_Form_Fields::select_input( |
|
| 1973 | - 'MTP_template_pack', |
|
| 1974 | - $tp_select_values, |
|
| 1975 | - $this->_message_template_group->get_template_pack_name() |
|
| 1976 | - ); |
|
| 1977 | - $template_args['variations_selector'] = EEH_Form_Fields::select_input( |
|
| 1978 | - 'MTP_template_variation', |
|
| 1979 | - $variations_select_values, |
|
| 1980 | - $this->_message_template_group->get_template_pack_variation() |
|
| 1981 | - ); |
|
| 1982 | - $template_args['template_pack_label'] = $template_pack_labels->template_pack; |
|
| 1983 | - $template_args['template_variation_label'] = $template_pack_labels->template_variation; |
|
| 1984 | - $template_args['template_pack_description'] = $template_pack_labels->template_pack_description; |
|
| 1985 | - $template_args['template_variation_description'] = $template_pack_labels->template_variation_description; |
|
| 1986 | - |
|
| 1987 | - $template = EE_MSG_TEMPLATE_PATH . 'template_pack_and_variations_metabox.template.php'; |
|
| 1988 | - |
|
| 1989 | - EEH_Template::display_template($template, $template_args); |
|
| 1990 | - } |
|
| 1991 | - |
|
| 1992 | - |
|
| 1993 | - /** |
|
| 1994 | - * This meta box holds any extra actions related to Message Templates |
|
| 1995 | - * For now, this includes Resetting templates to defaults and sending a test email. |
|
| 1996 | - * |
|
| 1997 | - * @access public |
|
| 1998 | - * @return void |
|
| 1999 | - * @throws \EE_Error |
|
| 2000 | - */ |
|
| 2001 | - public function extra_actions_meta_box() |
|
| 2002 | - { |
|
| 2003 | - $template_form_fields = array(); |
|
| 2004 | - |
|
| 2005 | - $extra_args = array( |
|
| 2006 | - 'msgr' => $this->_message_template_group->messenger(), |
|
| 2007 | - 'mt' => $this->_message_template_group->message_type(), |
|
| 2008 | - 'GRP_ID' => $this->_message_template_group->GRP_ID() |
|
| 2009 | - ); |
|
| 2010 | - //first we need to see if there are any fields |
|
| 2011 | - $fields = $this->_message_template_group->messenger_obj()->get_test_settings_fields(); |
|
| 2012 | - |
|
| 2013 | - if ( ! empty($fields)) { |
|
| 2014 | - //yup there be fields |
|
| 2015 | - foreach ($fields as $field => $config) { |
|
| 2016 | - $field_id = $this->_message_template_group->messenger() . '_' . $field; |
|
| 2017 | - $existing = $this->_message_template_group->messenger_obj()->get_existing_test_settings(); |
|
| 2018 | - $default = isset($config['default']) ? $config['default'] : ''; |
|
| 2019 | - $default = isset($config['value']) ? $config['value'] : $default; |
|
| 1943 | + $tp_select_values[] = array( |
|
| 1944 | + 'text' => $tp->label, |
|
| 1945 | + 'id' => $tp->dbref |
|
| 1946 | + ); |
|
| 1947 | + } |
|
| 1948 | + |
|
| 1949 | + //if empty $tp_select_values then we make sure default is set because EVERY message type should be supported by the default template pack. This still allows for the odd template pack to override. |
|
| 1950 | + if (empty($tp_select_values)) { |
|
| 1951 | + $tp_select_values[] = array( |
|
| 1952 | + 'text' => __('Default', 'event_espresso'), |
|
| 1953 | + 'id' => 'default' |
|
| 1954 | + ); |
|
| 1955 | + } |
|
| 1956 | + |
|
| 1957 | + //setup variation select values for the currently selected template. |
|
| 1958 | + $variations = $this->_message_template_group->get_template_pack()->get_variations( |
|
| 1959 | + $this->_message_template_group->messenger(), |
|
| 1960 | + $this->_message_template_group->message_type() |
|
| 1961 | + ); |
|
| 1962 | + $variations_select_values = array(); |
|
| 1963 | + foreach ($variations as $variation => $label) { |
|
| 1964 | + $variations_select_values[] = array( |
|
| 1965 | + 'text' => $label, |
|
| 1966 | + 'id' => $variation |
|
| 1967 | + ); |
|
| 1968 | + } |
|
| 1969 | + |
|
| 1970 | + $template_pack_labels = $this->_message_template_group->messenger_obj()->get_supports_labels(); |
|
| 1971 | + |
|
| 1972 | + $template_args['template_packs_selector'] = EEH_Form_Fields::select_input( |
|
| 1973 | + 'MTP_template_pack', |
|
| 1974 | + $tp_select_values, |
|
| 1975 | + $this->_message_template_group->get_template_pack_name() |
|
| 1976 | + ); |
|
| 1977 | + $template_args['variations_selector'] = EEH_Form_Fields::select_input( |
|
| 1978 | + 'MTP_template_variation', |
|
| 1979 | + $variations_select_values, |
|
| 1980 | + $this->_message_template_group->get_template_pack_variation() |
|
| 1981 | + ); |
|
| 1982 | + $template_args['template_pack_label'] = $template_pack_labels->template_pack; |
|
| 1983 | + $template_args['template_variation_label'] = $template_pack_labels->template_variation; |
|
| 1984 | + $template_args['template_pack_description'] = $template_pack_labels->template_pack_description; |
|
| 1985 | + $template_args['template_variation_description'] = $template_pack_labels->template_variation_description; |
|
| 1986 | + |
|
| 1987 | + $template = EE_MSG_TEMPLATE_PATH . 'template_pack_and_variations_metabox.template.php'; |
|
| 1988 | + |
|
| 1989 | + EEH_Template::display_template($template, $template_args); |
|
| 1990 | + } |
|
| 1991 | + |
|
| 1992 | + |
|
| 1993 | + /** |
|
| 1994 | + * This meta box holds any extra actions related to Message Templates |
|
| 1995 | + * For now, this includes Resetting templates to defaults and sending a test email. |
|
| 1996 | + * |
|
| 1997 | + * @access public |
|
| 1998 | + * @return void |
|
| 1999 | + * @throws \EE_Error |
|
| 2000 | + */ |
|
| 2001 | + public function extra_actions_meta_box() |
|
| 2002 | + { |
|
| 2003 | + $template_form_fields = array(); |
|
| 2004 | + |
|
| 2005 | + $extra_args = array( |
|
| 2006 | + 'msgr' => $this->_message_template_group->messenger(), |
|
| 2007 | + 'mt' => $this->_message_template_group->message_type(), |
|
| 2008 | + 'GRP_ID' => $this->_message_template_group->GRP_ID() |
|
| 2009 | + ); |
|
| 2010 | + //first we need to see if there are any fields |
|
| 2011 | + $fields = $this->_message_template_group->messenger_obj()->get_test_settings_fields(); |
|
| 2012 | + |
|
| 2013 | + if ( ! empty($fields)) { |
|
| 2014 | + //yup there be fields |
|
| 2015 | + foreach ($fields as $field => $config) { |
|
| 2016 | + $field_id = $this->_message_template_group->messenger() . '_' . $field; |
|
| 2017 | + $existing = $this->_message_template_group->messenger_obj()->get_existing_test_settings(); |
|
| 2018 | + $default = isset($config['default']) ? $config['default'] : ''; |
|
| 2019 | + $default = isset($config['value']) ? $config['value'] : $default; |
|
| 2020 | 2020 | |
| 2021 | - // if type is hidden and the value is empty |
|
| 2022 | - // something may have gone wrong so let's correct with the defaults |
|
| 2023 | - $fix = $config['input'] === 'hidden' && isset($existing[$field]) && empty($existing[$field]) |
|
| 2024 | - ? $default |
|
| 2025 | - : ''; |
|
| 2026 | - $existing[$field] = isset($existing[$field]) && empty($fix) |
|
| 2027 | - ? $existing[$field] |
|
| 2028 | - : $fix; |
|
| 2021 | + // if type is hidden and the value is empty |
|
| 2022 | + // something may have gone wrong so let's correct with the defaults |
|
| 2023 | + $fix = $config['input'] === 'hidden' && isset($existing[$field]) && empty($existing[$field]) |
|
| 2024 | + ? $default |
|
| 2025 | + : ''; |
|
| 2026 | + $existing[$field] = isset($existing[$field]) && empty($fix) |
|
| 2027 | + ? $existing[$field] |
|
| 2028 | + : $fix; |
|
| 2029 | 2029 | |
| 2030 | - $template_form_fields[$field_id] = array( |
|
| 2031 | - 'name' => 'test_settings_fld[' . $field . ']', |
|
| 2032 | - 'label' => $config['label'], |
|
| 2033 | - 'input' => $config['input'], |
|
| 2034 | - 'type' => $config['type'], |
|
| 2035 | - 'required' => $config['required'], |
|
| 2036 | - 'validation' => $config['validation'], |
|
| 2037 | - 'value' => isset($existing[$field]) ? $existing[$field] : $default, |
|
| 2038 | - 'css_class' => $config['css_class'], |
|
| 2039 | - 'options' => isset($config['options']) ? $config['options'] : array(), |
|
| 2040 | - 'default' => $default, |
|
| 2041 | - 'format' => $config['format'] |
|
| 2042 | - ); |
|
| 2043 | - } |
|
| 2044 | - } |
|
| 2045 | - |
|
| 2046 | - $test_settings_fields = ! empty($template_form_fields) |
|
| 2047 | - ? $this->_generate_admin_form_fields($template_form_fields, 'string', 'ee_tst_settings_flds') |
|
| 2048 | - : ''; |
|
| 2049 | - |
|
| 2050 | - $test_settings_html = ''; |
|
| 2051 | - //print out $test_settings_fields |
|
| 2052 | - if ( ! empty($test_settings_fields)) { |
|
| 2053 | - echo $test_settings_fields; |
|
| 2054 | - $test_settings_html = '<input type="submit" class="button-primary mtp-test-button alignright" '; |
|
| 2055 | - $test_settings_html .= 'name="test_button" value="'; |
|
| 2056 | - $test_settings_html .= __('Test Send', 'event_espresso'); |
|
| 2057 | - $test_settings_html .= '" /><div style="clear:both"></div>'; |
|
| 2058 | - } |
|
| 2059 | - |
|
| 2060 | - //and button |
|
| 2061 | - $test_settings_html .= '<p>' . __('Need to reset this message type and start over?', 'event_espresso') . '</p>'; |
|
| 2062 | - $test_settings_html .= '<div class="publishing-action alignright resetbutton">'; |
|
| 2063 | - $test_settings_html .= $this->get_action_link_or_button( |
|
| 2064 | - 'reset_to_default', |
|
| 2065 | - 'reset', |
|
| 2066 | - $extra_args, |
|
| 2067 | - 'button-primary reset-default-button' |
|
| 2068 | - ); |
|
| 2069 | - $test_settings_html .= '</div><div style="clear:both"></div>'; |
|
| 2070 | - echo $test_settings_html; |
|
| 2071 | - } |
|
| 2072 | - |
|
| 2073 | - |
|
| 2074 | - /** |
|
| 2075 | - * This returns the shortcode selector skeleton for a given context and field. |
|
| 2076 | - * |
|
| 2077 | - * @since 4.9.rc.000 |
|
| 2078 | - * |
|
| 2079 | - * @param string $field The name of the field retrieving shortcodes for. |
|
| 2080 | - * @param string $linked_input_id The css id of the input that the shortcodes get added to. |
|
| 2081 | - * |
|
| 2082 | - * @return string |
|
| 2083 | - */ |
|
| 2084 | - protected function _get_shortcode_selector($field, $linked_input_id) |
|
| 2085 | - { |
|
| 2086 | - $template_args = array( |
|
| 2087 | - 'shortcodes' => $this->_get_shortcodes(array($field), true), |
|
| 2088 | - 'fieldname' => $field, |
|
| 2089 | - 'linked_input_id' => $linked_input_id |
|
| 2090 | - ); |
|
| 2091 | - |
|
| 2092 | - return EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'shortcode_selector_skeleton.template.php', |
|
| 2093 | - $template_args, true); |
|
| 2094 | - } |
|
| 2095 | - |
|
| 2096 | - |
|
| 2097 | - /** |
|
| 2098 | - * This just takes care of returning the meta box content for shortcodes (only used on the edit message template |
|
| 2099 | - * page) |
|
| 2100 | - * |
|
| 2101 | - * @access public |
|
| 2102 | - * @return void |
|
| 2103 | - */ |
|
| 2104 | - public function shortcode_meta_box() |
|
| 2105 | - { |
|
| 2106 | - $shortcodes = $this->_get_shortcodes(array(), false); //just make sure shortcodes property is set |
|
| 2107 | - //$messenger = $this->_message_template_group->messenger_obj(); |
|
| 2108 | - //now let's set the content depending on the status of the shortcodes array |
|
| 2109 | - if (empty($shortcodes)) { |
|
| 2110 | - $content = '<p>' . __('There are no valid shortcodes available', 'event_espresso') . '</p>'; |
|
| 2111 | - echo $content; |
|
| 2112 | - } else { |
|
| 2113 | - //$alt = 0; |
|
| 2114 | - ?> |
|
| 2030 | + $template_form_fields[$field_id] = array( |
|
| 2031 | + 'name' => 'test_settings_fld[' . $field . ']', |
|
| 2032 | + 'label' => $config['label'], |
|
| 2033 | + 'input' => $config['input'], |
|
| 2034 | + 'type' => $config['type'], |
|
| 2035 | + 'required' => $config['required'], |
|
| 2036 | + 'validation' => $config['validation'], |
|
| 2037 | + 'value' => isset($existing[$field]) ? $existing[$field] : $default, |
|
| 2038 | + 'css_class' => $config['css_class'], |
|
| 2039 | + 'options' => isset($config['options']) ? $config['options'] : array(), |
|
| 2040 | + 'default' => $default, |
|
| 2041 | + 'format' => $config['format'] |
|
| 2042 | + ); |
|
| 2043 | + } |
|
| 2044 | + } |
|
| 2045 | + |
|
| 2046 | + $test_settings_fields = ! empty($template_form_fields) |
|
| 2047 | + ? $this->_generate_admin_form_fields($template_form_fields, 'string', 'ee_tst_settings_flds') |
|
| 2048 | + : ''; |
|
| 2049 | + |
|
| 2050 | + $test_settings_html = ''; |
|
| 2051 | + //print out $test_settings_fields |
|
| 2052 | + if ( ! empty($test_settings_fields)) { |
|
| 2053 | + echo $test_settings_fields; |
|
| 2054 | + $test_settings_html = '<input type="submit" class="button-primary mtp-test-button alignright" '; |
|
| 2055 | + $test_settings_html .= 'name="test_button" value="'; |
|
| 2056 | + $test_settings_html .= __('Test Send', 'event_espresso'); |
|
| 2057 | + $test_settings_html .= '" /><div style="clear:both"></div>'; |
|
| 2058 | + } |
|
| 2059 | + |
|
| 2060 | + //and button |
|
| 2061 | + $test_settings_html .= '<p>' . __('Need to reset this message type and start over?', 'event_espresso') . '</p>'; |
|
| 2062 | + $test_settings_html .= '<div class="publishing-action alignright resetbutton">'; |
|
| 2063 | + $test_settings_html .= $this->get_action_link_or_button( |
|
| 2064 | + 'reset_to_default', |
|
| 2065 | + 'reset', |
|
| 2066 | + $extra_args, |
|
| 2067 | + 'button-primary reset-default-button' |
|
| 2068 | + ); |
|
| 2069 | + $test_settings_html .= '</div><div style="clear:both"></div>'; |
|
| 2070 | + echo $test_settings_html; |
|
| 2071 | + } |
|
| 2072 | + |
|
| 2073 | + |
|
| 2074 | + /** |
|
| 2075 | + * This returns the shortcode selector skeleton for a given context and field. |
|
| 2076 | + * |
|
| 2077 | + * @since 4.9.rc.000 |
|
| 2078 | + * |
|
| 2079 | + * @param string $field The name of the field retrieving shortcodes for. |
|
| 2080 | + * @param string $linked_input_id The css id of the input that the shortcodes get added to. |
|
| 2081 | + * |
|
| 2082 | + * @return string |
|
| 2083 | + */ |
|
| 2084 | + protected function _get_shortcode_selector($field, $linked_input_id) |
|
| 2085 | + { |
|
| 2086 | + $template_args = array( |
|
| 2087 | + 'shortcodes' => $this->_get_shortcodes(array($field), true), |
|
| 2088 | + 'fieldname' => $field, |
|
| 2089 | + 'linked_input_id' => $linked_input_id |
|
| 2090 | + ); |
|
| 2091 | + |
|
| 2092 | + return EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'shortcode_selector_skeleton.template.php', |
|
| 2093 | + $template_args, true); |
|
| 2094 | + } |
|
| 2095 | + |
|
| 2096 | + |
|
| 2097 | + /** |
|
| 2098 | + * This just takes care of returning the meta box content for shortcodes (only used on the edit message template |
|
| 2099 | + * page) |
|
| 2100 | + * |
|
| 2101 | + * @access public |
|
| 2102 | + * @return void |
|
| 2103 | + */ |
|
| 2104 | + public function shortcode_meta_box() |
|
| 2105 | + { |
|
| 2106 | + $shortcodes = $this->_get_shortcodes(array(), false); //just make sure shortcodes property is set |
|
| 2107 | + //$messenger = $this->_message_template_group->messenger_obj(); |
|
| 2108 | + //now let's set the content depending on the status of the shortcodes array |
|
| 2109 | + if (empty($shortcodes)) { |
|
| 2110 | + $content = '<p>' . __('There are no valid shortcodes available', 'event_espresso') . '</p>'; |
|
| 2111 | + echo $content; |
|
| 2112 | + } else { |
|
| 2113 | + //$alt = 0; |
|
| 2114 | + ?> |
|
| 2115 | 2115 | <div |
| 2116 | 2116 | style="float:right; margin-top:10px"><?php echo $this->_get_help_tab_link('message_template_shortcodes'); ?></div> |
| 2117 | 2117 | <p class="small-text"><?php printf(__('You can view the shortcodes usable in your template by clicking the %s icon next to each field.', |
| 2118 | - 'event_espresso'), '<span class="dashicons dashicons-menu"></span>'); ?></p> |
|
| 2118 | + 'event_espresso'), '<span class="dashicons dashicons-menu"></span>'); ?></p> |
|
| 2119 | 2119 | <?php |
| 2120 | - } |
|
| 2121 | - |
|
| 2122 | - |
|
| 2123 | - } |
|
| 2124 | - |
|
| 2125 | - |
|
| 2126 | - /** |
|
| 2127 | - * used to set the $_shortcodes property for when its needed elsewhere. |
|
| 2128 | - * |
|
| 2129 | - * @access protected |
|
| 2130 | - * @return void |
|
| 2131 | - */ |
|
| 2132 | - protected function _set_shortcodes() |
|
| 2133 | - { |
|
| 2134 | - |
|
| 2135 | - //no need to run this if the property is already set |
|
| 2136 | - if ( ! empty($this->_shortcodes)) { |
|
| 2137 | - return; |
|
| 2138 | - } |
|
| 2139 | - |
|
| 2140 | - $this->_shortcodes = $this->_get_shortcodes(); |
|
| 2141 | - } |
|
| 2142 | - |
|
| 2143 | - |
|
| 2144 | - /** |
|
| 2145 | - * get's all shortcodes for a given template group. (typically used by _set_shortcodes to set the $_shortcodes |
|
| 2146 | - * property) |
|
| 2147 | - * |
|
| 2148 | - * @access protected |
|
| 2149 | - * |
|
| 2150 | - * @param array $fields include an array of specific field names that you want to be used to get the shortcodes |
|
| 2151 | - * for. Defaults to all (for the given context) |
|
| 2152 | - * @param boolean $merged Whether to merge all the shortcodes into one list of unique shortcodes |
|
| 2153 | - * |
|
| 2154 | - * @return array Shortcodes indexed by fieldname and the an array of shortcode/label pairs OR if merged is |
|
| 2155 | - * true just an array of shortcode/label pairs. |
|
| 2156 | - */ |
|
| 2157 | - protected function _get_shortcodes($fields = array(), $merged = true) |
|
| 2158 | - { |
|
| 2159 | - $this->_set_message_template_group(); |
|
| 2160 | - |
|
| 2161 | - //we need the messenger and message template to retrieve the valid shortcodes array. |
|
| 2162 | - $GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id']) ? absint($this->_req_data['id']) : false; |
|
| 2163 | - $context = isset($this->_req_data['context']) ? $this->_req_data['context'] : key($this->_message_template_group->contexts_config()); |
|
| 2164 | - |
|
| 2165 | - return ! empty($GRP_ID) ? $this->_message_template_group->get_shortcodes($context, $fields, $merged) : array(); |
|
| 2166 | - } |
|
| 2167 | - |
|
| 2168 | - |
|
| 2169 | - /** |
|
| 2170 | - * This sets the _message_template property (containing the called message_template object) |
|
| 2171 | - * |
|
| 2172 | - * @access protected |
|
| 2173 | - * @return void |
|
| 2174 | - */ |
|
| 2175 | - protected function _set_message_template_group() |
|
| 2176 | - { |
|
| 2177 | - |
|
| 2178 | - if ( ! empty($this->_message_template_group)) { |
|
| 2179 | - return; |
|
| 2180 | - } //get out if this is already set. |
|
| 2181 | - |
|
| 2182 | - $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? absint($this->_req_data['GRP_ID']) : false; |
|
| 2183 | - $GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['id']) ? $this->_req_data['id'] : $GRP_ID; |
|
| 2184 | - |
|
| 2185 | - //let's get the message templates |
|
| 2186 | - $MTP = EEM_Message_Template_Group::instance(); |
|
| 2187 | - |
|
| 2188 | - if (empty($GRP_ID)) { |
|
| 2189 | - $this->_message_template_group = $MTP->create_default_object(); |
|
| 2190 | - } else { |
|
| 2191 | - $this->_message_template_group = $MTP->get_one_by_ID($GRP_ID); |
|
| 2192 | - } |
|
| 2193 | - |
|
| 2194 | - $this->_template_pack = $this->_message_template_group->get_template_pack(); |
|
| 2195 | - $this->_variation = $this->_message_template_group->get_template_pack_variation(); |
|
| 2196 | - |
|
| 2197 | - } |
|
| 2198 | - |
|
| 2199 | - |
|
| 2200 | - /** |
|
| 2201 | - * sets up a context switcher for edit forms |
|
| 2202 | - * |
|
| 2203 | - * @access protected |
|
| 2204 | - * |
|
| 2205 | - * @param EE_Message_Template_Group $template_group_object the template group object being displayed on the form |
|
| 2206 | - * @param array $args various things the context switcher needs. |
|
| 2207 | - * |
|
| 2208 | - */ |
|
| 2209 | - protected function _set_context_switcher(EE_Message_Template_Group $template_group_object, $args) |
|
| 2210 | - { |
|
| 2211 | - $context_details = $template_group_object->contexts_config(); |
|
| 2212 | - $context_label = $template_group_object->context_label(); |
|
| 2213 | - ob_start(); |
|
| 2214 | - ?> |
|
| 2120 | + } |
|
| 2121 | + |
|
| 2122 | + |
|
| 2123 | + } |
|
| 2124 | + |
|
| 2125 | + |
|
| 2126 | + /** |
|
| 2127 | + * used to set the $_shortcodes property for when its needed elsewhere. |
|
| 2128 | + * |
|
| 2129 | + * @access protected |
|
| 2130 | + * @return void |
|
| 2131 | + */ |
|
| 2132 | + protected function _set_shortcodes() |
|
| 2133 | + { |
|
| 2134 | + |
|
| 2135 | + //no need to run this if the property is already set |
|
| 2136 | + if ( ! empty($this->_shortcodes)) { |
|
| 2137 | + return; |
|
| 2138 | + } |
|
| 2139 | + |
|
| 2140 | + $this->_shortcodes = $this->_get_shortcodes(); |
|
| 2141 | + } |
|
| 2142 | + |
|
| 2143 | + |
|
| 2144 | + /** |
|
| 2145 | + * get's all shortcodes for a given template group. (typically used by _set_shortcodes to set the $_shortcodes |
|
| 2146 | + * property) |
|
| 2147 | + * |
|
| 2148 | + * @access protected |
|
| 2149 | + * |
|
| 2150 | + * @param array $fields include an array of specific field names that you want to be used to get the shortcodes |
|
| 2151 | + * for. Defaults to all (for the given context) |
|
| 2152 | + * @param boolean $merged Whether to merge all the shortcodes into one list of unique shortcodes |
|
| 2153 | + * |
|
| 2154 | + * @return array Shortcodes indexed by fieldname and the an array of shortcode/label pairs OR if merged is |
|
| 2155 | + * true just an array of shortcode/label pairs. |
|
| 2156 | + */ |
|
| 2157 | + protected function _get_shortcodes($fields = array(), $merged = true) |
|
| 2158 | + { |
|
| 2159 | + $this->_set_message_template_group(); |
|
| 2160 | + |
|
| 2161 | + //we need the messenger and message template to retrieve the valid shortcodes array. |
|
| 2162 | + $GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id']) ? absint($this->_req_data['id']) : false; |
|
| 2163 | + $context = isset($this->_req_data['context']) ? $this->_req_data['context'] : key($this->_message_template_group->contexts_config()); |
|
| 2164 | + |
|
| 2165 | + return ! empty($GRP_ID) ? $this->_message_template_group->get_shortcodes($context, $fields, $merged) : array(); |
|
| 2166 | + } |
|
| 2167 | + |
|
| 2168 | + |
|
| 2169 | + /** |
|
| 2170 | + * This sets the _message_template property (containing the called message_template object) |
|
| 2171 | + * |
|
| 2172 | + * @access protected |
|
| 2173 | + * @return void |
|
| 2174 | + */ |
|
| 2175 | + protected function _set_message_template_group() |
|
| 2176 | + { |
|
| 2177 | + |
|
| 2178 | + if ( ! empty($this->_message_template_group)) { |
|
| 2179 | + return; |
|
| 2180 | + } //get out if this is already set. |
|
| 2181 | + |
|
| 2182 | + $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? absint($this->_req_data['GRP_ID']) : false; |
|
| 2183 | + $GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['id']) ? $this->_req_data['id'] : $GRP_ID; |
|
| 2184 | + |
|
| 2185 | + //let's get the message templates |
|
| 2186 | + $MTP = EEM_Message_Template_Group::instance(); |
|
| 2187 | + |
|
| 2188 | + if (empty($GRP_ID)) { |
|
| 2189 | + $this->_message_template_group = $MTP->create_default_object(); |
|
| 2190 | + } else { |
|
| 2191 | + $this->_message_template_group = $MTP->get_one_by_ID($GRP_ID); |
|
| 2192 | + } |
|
| 2193 | + |
|
| 2194 | + $this->_template_pack = $this->_message_template_group->get_template_pack(); |
|
| 2195 | + $this->_variation = $this->_message_template_group->get_template_pack_variation(); |
|
| 2196 | + |
|
| 2197 | + } |
|
| 2198 | + |
|
| 2199 | + |
|
| 2200 | + /** |
|
| 2201 | + * sets up a context switcher for edit forms |
|
| 2202 | + * |
|
| 2203 | + * @access protected |
|
| 2204 | + * |
|
| 2205 | + * @param EE_Message_Template_Group $template_group_object the template group object being displayed on the form |
|
| 2206 | + * @param array $args various things the context switcher needs. |
|
| 2207 | + * |
|
| 2208 | + */ |
|
| 2209 | + protected function _set_context_switcher(EE_Message_Template_Group $template_group_object, $args) |
|
| 2210 | + { |
|
| 2211 | + $context_details = $template_group_object->contexts_config(); |
|
| 2212 | + $context_label = $template_group_object->context_label(); |
|
| 2213 | + ob_start(); |
|
| 2214 | + ?> |
|
| 2215 | 2215 | <div class="ee-msg-switcher-container"> |
| 2216 | 2216 | <form method="get" action="<?php echo EE_MSG_ADMIN_URL; ?>" id="ee-msg-context-switcher-frm"> |
| 2217 | 2217 | <?php |
| 2218 | - foreach ($args as $name => $value) { |
|
| 2219 | - if ($name == 'context' || empty($value) || $name == 'extra') { |
|
| 2220 | - continue; |
|
| 2221 | - } |
|
| 2222 | - ?> |
|
| 2218 | + foreach ($args as $name => $value) { |
|
| 2219 | + if ($name == 'context' || empty($value) || $name == 'extra') { |
|
| 2220 | + continue; |
|
| 2221 | + } |
|
| 2222 | + ?> |
|
| 2223 | 2223 | <input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/> |
| 2224 | 2224 | <?php |
| 2225 | - } |
|
| 2226 | - //setup nonce_url |
|
| 2227 | - wp_nonce_field($args['action'] . '_nonce', $args['action'] . '_nonce', false); |
|
| 2228 | - ?> |
|
| 2225 | + } |
|
| 2226 | + //setup nonce_url |
|
| 2227 | + wp_nonce_field($args['action'] . '_nonce', $args['action'] . '_nonce', false); |
|
| 2228 | + ?> |
|
| 2229 | 2229 | <select name="context"> |
| 2230 | 2230 | <?php |
| 2231 | - $context_templates = $template_group_object->context_templates(); |
|
| 2232 | - if (is_array($context_templates)) : |
|
| 2233 | - foreach ($context_templates as $context => $template_fields) : |
|
| 2234 | - $checked = ($context == $args['context']) ? 'selected="selected"' : ''; |
|
| 2235 | - ?> |
|
| 2231 | + $context_templates = $template_group_object->context_templates(); |
|
| 2232 | + if (is_array($context_templates)) : |
|
| 2233 | + foreach ($context_templates as $context => $template_fields) : |
|
| 2234 | + $checked = ($context == $args['context']) ? 'selected="selected"' : ''; |
|
| 2235 | + ?> |
|
| 2236 | 2236 | <option value="<?php echo $context; ?>" <?php echo $checked; ?>> |
| 2237 | 2237 | <?php echo $context_details[$context]['label']; ?> |
| 2238 | 2238 | </option> |
@@ -2245,1584 +2245,1584 @@ discard block |
||
| 2245 | 2245 | <?php echo $args['extra']; ?> |
| 2246 | 2246 | </div> <!-- end .ee-msg-switcher-container --> |
| 2247 | 2247 | <?php |
| 2248 | - $output = ob_get_contents(); |
|
| 2249 | - ob_clean(); |
|
| 2250 | - $this->_context_switcher = $output; |
|
| 2251 | - } |
|
| 2252 | - |
|
| 2253 | - |
|
| 2254 | - /** |
|
| 2255 | - * utility for sanitizing new values coming in. |
|
| 2256 | - * Note: this is only used when updating a context. |
|
| 2257 | - * |
|
| 2258 | - * @access protected |
|
| 2259 | - * |
|
| 2260 | - * @param int $index This helps us know which template field to select from the request array. |
|
| 2261 | - * |
|
| 2262 | - * @return array |
|
| 2263 | - */ |
|
| 2264 | - protected function _set_message_template_column_values($index) |
|
| 2265 | - { |
|
| 2266 | - if (is_array($this->_req_data['MTP_template_fields'][$index]['content'])) { |
|
| 2267 | - foreach ($this->_req_data['MTP_template_fields'][$index]['content'] as $field => $value) { |
|
| 2268 | - $this->_req_data['MTP_template_fields'][$index]['content'][$field] = $value; |
|
| 2269 | - } |
|
| 2270 | - } /*else { |
|
| 2248 | + $output = ob_get_contents(); |
|
| 2249 | + ob_clean(); |
|
| 2250 | + $this->_context_switcher = $output; |
|
| 2251 | + } |
|
| 2252 | + |
|
| 2253 | + |
|
| 2254 | + /** |
|
| 2255 | + * utility for sanitizing new values coming in. |
|
| 2256 | + * Note: this is only used when updating a context. |
|
| 2257 | + * |
|
| 2258 | + * @access protected |
|
| 2259 | + * |
|
| 2260 | + * @param int $index This helps us know which template field to select from the request array. |
|
| 2261 | + * |
|
| 2262 | + * @return array |
|
| 2263 | + */ |
|
| 2264 | + protected function _set_message_template_column_values($index) |
|
| 2265 | + { |
|
| 2266 | + if (is_array($this->_req_data['MTP_template_fields'][$index]['content'])) { |
|
| 2267 | + foreach ($this->_req_data['MTP_template_fields'][$index]['content'] as $field => $value) { |
|
| 2268 | + $this->_req_data['MTP_template_fields'][$index]['content'][$field] = $value; |
|
| 2269 | + } |
|
| 2270 | + } /*else { |
|
| 2271 | 2271 | $this->_req_data['MTP_template_fields'][$index]['content'] = $this->_req_data['MTP_template_fields'][$index]['content']; |
| 2272 | 2272 | }*/ |
| 2273 | 2273 | |
| 2274 | 2274 | |
| 2275 | - $set_column_values = array( |
|
| 2276 | - 'MTP_ID' => absint($this->_req_data['MTP_template_fields'][$index]['MTP_ID']), |
|
| 2277 | - 'GRP_ID' => absint($this->_req_data['GRP_ID']), |
|
| 2278 | - 'MTP_user_id' => absint($this->_req_data['MTP_user_id']), |
|
| 2279 | - 'MTP_messenger' => strtolower($this->_req_data['MTP_messenger']), |
|
| 2280 | - 'MTP_message_type' => strtolower($this->_req_data['MTP_message_type']), |
|
| 2281 | - 'MTP_template_field' => strtolower($this->_req_data['MTP_template_fields'][$index]['name']), |
|
| 2282 | - 'MTP_context' => strtolower($this->_req_data['MTP_context']), |
|
| 2283 | - 'MTP_content' => $this->_req_data['MTP_template_fields'][$index]['content'], |
|
| 2284 | - 'MTP_is_global' => isset($this->_req_data['MTP_is_global']) |
|
| 2285 | - ? absint($this->_req_data['MTP_is_global']) |
|
| 2286 | - : 0, |
|
| 2287 | - 'MTP_is_override' => isset($this->_req_data['MTP_is_override']) |
|
| 2288 | - ? absint($this->_req_data['MTP_is_override']) |
|
| 2289 | - : 0, |
|
| 2290 | - 'MTP_deleted' => absint($this->_req_data['MTP_deleted']), |
|
| 2291 | - 'MTP_is_active' => absint($this->_req_data['MTP_is_active']) |
|
| 2292 | - ); |
|
| 2293 | - |
|
| 2294 | - |
|
| 2295 | - return $set_column_values; |
|
| 2296 | - } |
|
| 2297 | - |
|
| 2298 | - |
|
| 2299 | - protected function _insert_or_update_message_template($new = false) |
|
| 2300 | - { |
|
| 2301 | - |
|
| 2302 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 2303 | - $success = 0; |
|
| 2304 | - $override = false; |
|
| 2305 | - |
|
| 2306 | - //setup notices description |
|
| 2307 | - $messenger_slug = ! empty($this->_req_data['MTP_messenger']) ? $this->_req_data['MTP_messenger'] : ''; |
|
| 2308 | - |
|
| 2309 | - //need the message type and messenger objects to be able to use the labels for the notices |
|
| 2310 | - $messenger_object = $this->_message_resource_manager->get_messenger($messenger_slug); |
|
| 2311 | - $messenger_label = $messenger_object instanceof EE_messenger ? ucwords($messenger_object->label['singular']) : ''; |
|
| 2312 | - |
|
| 2313 | - $message_type_slug = ! empty($this->_req_data['MTP_message_type']) ? $this->_req_data['MTP_message_type'] : ''; |
|
| 2314 | - $message_type_object = $this->_message_resource_manager->get_message_type($message_type_slug); |
|
| 2315 | - |
|
| 2316 | - $message_type_label = $message_type_object instanceof EE_message_type |
|
| 2317 | - ? ucwords($message_type_object->label['singular']) |
|
| 2318 | - : ''; |
|
| 2319 | - |
|
| 2320 | - $context_slug = ! empty($this->_req_data['MTP_context']) |
|
| 2321 | - ? $this->_req_data['MTP_context'] |
|
| 2322 | - : ''; |
|
| 2323 | - $context = ucwords(str_replace('_', ' ', $context_slug)); |
|
| 2324 | - |
|
| 2325 | - $item_desc = $messenger_label && $message_type_label ? $messenger_label . ' ' . $message_type_label . ' ' . $context . ' ' : ''; |
|
| 2326 | - $item_desc .= 'Message Template'; |
|
| 2327 | - $query_args = array(); |
|
| 2328 | - $edit_array = array(); |
|
| 2329 | - $action_desc = ''; |
|
| 2330 | - |
|
| 2331 | - //if this is "new" then we need to generate the default contexts for the selected messenger/message_type for user to edit. |
|
| 2332 | - if ($new) { |
|
| 2333 | - $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0; |
|
| 2334 | - if ($edit_array = $this->_generate_new_templates($messenger_slug, $message_type_slug, $GRP_ID)) { |
|
| 2335 | - if (empty($edit_array)) { |
|
| 2336 | - $success = 0; |
|
| 2337 | - } else { |
|
| 2338 | - $success = 1; |
|
| 2339 | - $edit_array = $edit_array[0]; |
|
| 2340 | - $query_args = array( |
|
| 2341 | - 'id' => $edit_array['GRP_ID'], |
|
| 2342 | - 'context' => $edit_array['MTP_context'], |
|
| 2343 | - 'action' => 'edit_message_template' |
|
| 2344 | - ); |
|
| 2345 | - } |
|
| 2346 | - } |
|
| 2347 | - $action_desc = 'created'; |
|
| 2348 | - } else { |
|
| 2349 | - $MTPG = EEM_Message_Template_Group::instance(); |
|
| 2350 | - $MTP = EEM_Message_Template::instance(); |
|
| 2275 | + $set_column_values = array( |
|
| 2276 | + 'MTP_ID' => absint($this->_req_data['MTP_template_fields'][$index]['MTP_ID']), |
|
| 2277 | + 'GRP_ID' => absint($this->_req_data['GRP_ID']), |
|
| 2278 | + 'MTP_user_id' => absint($this->_req_data['MTP_user_id']), |
|
| 2279 | + 'MTP_messenger' => strtolower($this->_req_data['MTP_messenger']), |
|
| 2280 | + 'MTP_message_type' => strtolower($this->_req_data['MTP_message_type']), |
|
| 2281 | + 'MTP_template_field' => strtolower($this->_req_data['MTP_template_fields'][$index]['name']), |
|
| 2282 | + 'MTP_context' => strtolower($this->_req_data['MTP_context']), |
|
| 2283 | + 'MTP_content' => $this->_req_data['MTP_template_fields'][$index]['content'], |
|
| 2284 | + 'MTP_is_global' => isset($this->_req_data['MTP_is_global']) |
|
| 2285 | + ? absint($this->_req_data['MTP_is_global']) |
|
| 2286 | + : 0, |
|
| 2287 | + 'MTP_is_override' => isset($this->_req_data['MTP_is_override']) |
|
| 2288 | + ? absint($this->_req_data['MTP_is_override']) |
|
| 2289 | + : 0, |
|
| 2290 | + 'MTP_deleted' => absint($this->_req_data['MTP_deleted']), |
|
| 2291 | + 'MTP_is_active' => absint($this->_req_data['MTP_is_active']) |
|
| 2292 | + ); |
|
| 2293 | + |
|
| 2294 | + |
|
| 2295 | + return $set_column_values; |
|
| 2296 | + } |
|
| 2297 | + |
|
| 2298 | + |
|
| 2299 | + protected function _insert_or_update_message_template($new = false) |
|
| 2300 | + { |
|
| 2301 | + |
|
| 2302 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 2303 | + $success = 0; |
|
| 2304 | + $override = false; |
|
| 2305 | + |
|
| 2306 | + //setup notices description |
|
| 2307 | + $messenger_slug = ! empty($this->_req_data['MTP_messenger']) ? $this->_req_data['MTP_messenger'] : ''; |
|
| 2308 | + |
|
| 2309 | + //need the message type and messenger objects to be able to use the labels for the notices |
|
| 2310 | + $messenger_object = $this->_message_resource_manager->get_messenger($messenger_slug); |
|
| 2311 | + $messenger_label = $messenger_object instanceof EE_messenger ? ucwords($messenger_object->label['singular']) : ''; |
|
| 2312 | + |
|
| 2313 | + $message_type_slug = ! empty($this->_req_data['MTP_message_type']) ? $this->_req_data['MTP_message_type'] : ''; |
|
| 2314 | + $message_type_object = $this->_message_resource_manager->get_message_type($message_type_slug); |
|
| 2315 | + |
|
| 2316 | + $message_type_label = $message_type_object instanceof EE_message_type |
|
| 2317 | + ? ucwords($message_type_object->label['singular']) |
|
| 2318 | + : ''; |
|
| 2319 | + |
|
| 2320 | + $context_slug = ! empty($this->_req_data['MTP_context']) |
|
| 2321 | + ? $this->_req_data['MTP_context'] |
|
| 2322 | + : ''; |
|
| 2323 | + $context = ucwords(str_replace('_', ' ', $context_slug)); |
|
| 2324 | + |
|
| 2325 | + $item_desc = $messenger_label && $message_type_label ? $messenger_label . ' ' . $message_type_label . ' ' . $context . ' ' : ''; |
|
| 2326 | + $item_desc .= 'Message Template'; |
|
| 2327 | + $query_args = array(); |
|
| 2328 | + $edit_array = array(); |
|
| 2329 | + $action_desc = ''; |
|
| 2330 | + |
|
| 2331 | + //if this is "new" then we need to generate the default contexts for the selected messenger/message_type for user to edit. |
|
| 2332 | + if ($new) { |
|
| 2333 | + $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0; |
|
| 2334 | + if ($edit_array = $this->_generate_new_templates($messenger_slug, $message_type_slug, $GRP_ID)) { |
|
| 2335 | + if (empty($edit_array)) { |
|
| 2336 | + $success = 0; |
|
| 2337 | + } else { |
|
| 2338 | + $success = 1; |
|
| 2339 | + $edit_array = $edit_array[0]; |
|
| 2340 | + $query_args = array( |
|
| 2341 | + 'id' => $edit_array['GRP_ID'], |
|
| 2342 | + 'context' => $edit_array['MTP_context'], |
|
| 2343 | + 'action' => 'edit_message_template' |
|
| 2344 | + ); |
|
| 2345 | + } |
|
| 2346 | + } |
|
| 2347 | + $action_desc = 'created'; |
|
| 2348 | + } else { |
|
| 2349 | + $MTPG = EEM_Message_Template_Group::instance(); |
|
| 2350 | + $MTP = EEM_Message_Template::instance(); |
|
| 2351 | 2351 | |
| 2352 | 2352 | |
| 2353 | - //run update for each template field in displayed context |
|
| 2354 | - if ( ! isset($this->_req_data['MTP_template_fields']) && empty($this->_req_data['MTP_template_fields'])) { |
|
| 2355 | - EE_Error::add_error( |
|
| 2356 | - __('There was a problem saving the template fields from the form because I didn\'t receive any actual template field data.', |
|
| 2357 | - 'event_espresso'), |
|
| 2358 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 2359 | - ); |
|
| 2360 | - $success = 0; |
|
| 2353 | + //run update for each template field in displayed context |
|
| 2354 | + if ( ! isset($this->_req_data['MTP_template_fields']) && empty($this->_req_data['MTP_template_fields'])) { |
|
| 2355 | + EE_Error::add_error( |
|
| 2356 | + __('There was a problem saving the template fields from the form because I didn\'t receive any actual template field data.', |
|
| 2357 | + 'event_espresso'), |
|
| 2358 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 2359 | + ); |
|
| 2360 | + $success = 0; |
|
| 2361 | 2361 | |
| 2362 | - } else { |
|
| 2363 | - //first validate all fields! |
|
| 2364 | - $validates = $MTPG->validate($this->_req_data['MTP_template_fields'], $context_slug, $messenger_slug, |
|
| 2365 | - $message_type_slug); |
|
| 2362 | + } else { |
|
| 2363 | + //first validate all fields! |
|
| 2364 | + $validates = $MTPG->validate($this->_req_data['MTP_template_fields'], $context_slug, $messenger_slug, |
|
| 2365 | + $message_type_slug); |
|
| 2366 | 2366 | |
| 2367 | - //if $validate returned error messages (i.e. is_array()) then we need to process them and setup an appropriate response. HMM, dang this isn't correct, $validates will ALWAYS be an array. WE need to make sure there is no actual error messages in validates. |
|
| 2368 | - if (is_array($validates) && ! empty($validates)) { |
|
| 2369 | - //add the transient so when the form loads we know which fields to highlight |
|
| 2370 | - $this->_add_transient('edit_message_template', $validates); |
|
| 2367 | + //if $validate returned error messages (i.e. is_array()) then we need to process them and setup an appropriate response. HMM, dang this isn't correct, $validates will ALWAYS be an array. WE need to make sure there is no actual error messages in validates. |
|
| 2368 | + if (is_array($validates) && ! empty($validates)) { |
|
| 2369 | + //add the transient so when the form loads we know which fields to highlight |
|
| 2370 | + $this->_add_transient('edit_message_template', $validates); |
|
| 2371 | 2371 | |
| 2372 | - $success = 0; |
|
| 2372 | + $success = 0; |
|
| 2373 | 2373 | |
| 2374 | - //setup notices |
|
| 2375 | - foreach ($validates as $field => $error) { |
|
| 2376 | - if (isset($error['msg'])) { |
|
| 2377 | - EE_Error::add_error($error['msg'], __FILE__, __FUNCTION__, __LINE__); |
|
| 2378 | - } |
|
| 2379 | - } |
|
| 2374 | + //setup notices |
|
| 2375 | + foreach ($validates as $field => $error) { |
|
| 2376 | + if (isset($error['msg'])) { |
|
| 2377 | + EE_Error::add_error($error['msg'], __FILE__, __FUNCTION__, __LINE__); |
|
| 2378 | + } |
|
| 2379 | + } |
|
| 2380 | 2380 | |
| 2381 | - } else { |
|
| 2382 | - $set_column_values = array(); |
|
| 2383 | - foreach ($this->_req_data['MTP_template_fields'] as $template_field => $content) { |
|
| 2384 | - $set_column_values = $this->_set_message_template_column_values($template_field); |
|
| 2381 | + } else { |
|
| 2382 | + $set_column_values = array(); |
|
| 2383 | + foreach ($this->_req_data['MTP_template_fields'] as $template_field => $content) { |
|
| 2384 | + $set_column_values = $this->_set_message_template_column_values($template_field); |
|
| 2385 | 2385 | |
| 2386 | - $where_cols_n_values = array( |
|
| 2387 | - 'MTP_ID' => $this->_req_data['MTP_template_fields'][$template_field]['MTP_ID'] |
|
| 2388 | - ); |
|
| 2386 | + $where_cols_n_values = array( |
|
| 2387 | + 'MTP_ID' => $this->_req_data['MTP_template_fields'][$template_field]['MTP_ID'] |
|
| 2388 | + ); |
|
| 2389 | 2389 | |
| 2390 | - $message_template_fields = array( |
|
| 2391 | - 'GRP_ID' => $set_column_values['GRP_ID'], |
|
| 2392 | - 'MTP_template_field' => $set_column_values['MTP_template_field'], |
|
| 2393 | - 'MTP_context' => $set_column_values['MTP_context'], |
|
| 2394 | - 'MTP_content' => $set_column_values['MTP_content'] |
|
| 2395 | - ); |
|
| 2396 | - if ($updated = $MTP->update($message_template_fields, array($where_cols_n_values))) { |
|
| 2397 | - if ($updated === false) { |
|
| 2398 | - EE_Error::add_error( |
|
| 2399 | - sprintf( |
|
| 2400 | - __('%s field was NOT updated for some reason', 'event_espresso'), |
|
| 2401 | - $template_field |
|
| 2402 | - ), |
|
| 2403 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 2404 | - ); |
|
| 2405 | - } else { |
|
| 2406 | - $success = 1; |
|
| 2407 | - } |
|
| 2408 | - } |
|
| 2409 | - $action_desc = 'updated'; |
|
| 2410 | - } |
|
| 2390 | + $message_template_fields = array( |
|
| 2391 | + 'GRP_ID' => $set_column_values['GRP_ID'], |
|
| 2392 | + 'MTP_template_field' => $set_column_values['MTP_template_field'], |
|
| 2393 | + 'MTP_context' => $set_column_values['MTP_context'], |
|
| 2394 | + 'MTP_content' => $set_column_values['MTP_content'] |
|
| 2395 | + ); |
|
| 2396 | + if ($updated = $MTP->update($message_template_fields, array($where_cols_n_values))) { |
|
| 2397 | + if ($updated === false) { |
|
| 2398 | + EE_Error::add_error( |
|
| 2399 | + sprintf( |
|
| 2400 | + __('%s field was NOT updated for some reason', 'event_espresso'), |
|
| 2401 | + $template_field |
|
| 2402 | + ), |
|
| 2403 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 2404 | + ); |
|
| 2405 | + } else { |
|
| 2406 | + $success = 1; |
|
| 2407 | + } |
|
| 2408 | + } |
|
| 2409 | + $action_desc = 'updated'; |
|
| 2410 | + } |
|
| 2411 | 2411 | |
| 2412 | - //we can use the last set_column_values for the MTPG update (because its the same for all of these specific MTPs) |
|
| 2413 | - $mtpg_fields = array( |
|
| 2414 | - 'MTP_user_id' => $set_column_values['MTP_user_id'], |
|
| 2415 | - 'MTP_messenger' => $set_column_values['MTP_messenger'], |
|
| 2416 | - 'MTP_message_type' => $set_column_values['MTP_message_type'], |
|
| 2417 | - 'MTP_is_global' => $set_column_values['MTP_is_global'], |
|
| 2418 | - 'MTP_is_override' => $set_column_values['MTP_is_override'], |
|
| 2419 | - 'MTP_deleted' => $set_column_values['MTP_deleted'], |
|
| 2420 | - 'MTP_is_active' => $set_column_values['MTP_is_active'], |
|
| 2421 | - 'MTP_name' => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_name']) |
|
| 2422 | - ? $this->_req_data['ee_msg_non_global_fields']['MTP_name'] |
|
| 2423 | - : '', |
|
| 2424 | - 'MTP_description' => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_description']) |
|
| 2425 | - ? $this->_req_data['ee_msg_non_global_fields']['MTP_description'] |
|
| 2426 | - : '' |
|
| 2427 | - ); |
|
| 2412 | + //we can use the last set_column_values for the MTPG update (because its the same for all of these specific MTPs) |
|
| 2413 | + $mtpg_fields = array( |
|
| 2414 | + 'MTP_user_id' => $set_column_values['MTP_user_id'], |
|
| 2415 | + 'MTP_messenger' => $set_column_values['MTP_messenger'], |
|
| 2416 | + 'MTP_message_type' => $set_column_values['MTP_message_type'], |
|
| 2417 | + 'MTP_is_global' => $set_column_values['MTP_is_global'], |
|
| 2418 | + 'MTP_is_override' => $set_column_values['MTP_is_override'], |
|
| 2419 | + 'MTP_deleted' => $set_column_values['MTP_deleted'], |
|
| 2420 | + 'MTP_is_active' => $set_column_values['MTP_is_active'], |
|
| 2421 | + 'MTP_name' => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_name']) |
|
| 2422 | + ? $this->_req_data['ee_msg_non_global_fields']['MTP_name'] |
|
| 2423 | + : '', |
|
| 2424 | + 'MTP_description' => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_description']) |
|
| 2425 | + ? $this->_req_data['ee_msg_non_global_fields']['MTP_description'] |
|
| 2426 | + : '' |
|
| 2427 | + ); |
|
| 2428 | 2428 | |
| 2429 | - $mtpg_where = array('GRP_ID' => $set_column_values['GRP_ID']); |
|
| 2430 | - $updated = $MTPG->update($mtpg_fields, array($mtpg_where)); |
|
| 2429 | + $mtpg_where = array('GRP_ID' => $set_column_values['GRP_ID']); |
|
| 2430 | + $updated = $MTPG->update($mtpg_fields, array($mtpg_where)); |
|
| 2431 | 2431 | |
| 2432 | - if ($updated === false) { |
|
| 2433 | - EE_Error::add_error( |
|
| 2434 | - sprintf( |
|
| 2435 | - __('The Message Template Group (%d) was NOT updated for some reason', 'event_espresso'), |
|
| 2436 | - $set_column_values['GRP_ID'] |
|
| 2437 | - ), |
|
| 2438 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 2439 | - ); |
|
| 2440 | - } else { |
|
| 2441 | - //k now we need to ensure the template_pack and template_variation fields are set. |
|
| 2442 | - $template_pack = ! empty($this->_req_data['MTP_template_pack']) |
|
| 2443 | - ? $this->_req_data['MTP_template_pack'] |
|
| 2444 | - : 'default'; |
|
| 2432 | + if ($updated === false) { |
|
| 2433 | + EE_Error::add_error( |
|
| 2434 | + sprintf( |
|
| 2435 | + __('The Message Template Group (%d) was NOT updated for some reason', 'event_espresso'), |
|
| 2436 | + $set_column_values['GRP_ID'] |
|
| 2437 | + ), |
|
| 2438 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 2439 | + ); |
|
| 2440 | + } else { |
|
| 2441 | + //k now we need to ensure the template_pack and template_variation fields are set. |
|
| 2442 | + $template_pack = ! empty($this->_req_data['MTP_template_pack']) |
|
| 2443 | + ? $this->_req_data['MTP_template_pack'] |
|
| 2444 | + : 'default'; |
|
| 2445 | 2445 | |
| 2446 | - $template_variation = ! empty($this->_req_data['MTP_template_variation']) |
|
| 2447 | - ? $this->_req_data['MTP_template_variation'] |
|
| 2448 | - : 'default'; |
|
| 2446 | + $template_variation = ! empty($this->_req_data['MTP_template_variation']) |
|
| 2447 | + ? $this->_req_data['MTP_template_variation'] |
|
| 2448 | + : 'default'; |
|
| 2449 | 2449 | |
| 2450 | - $mtpg_obj = $MTPG->get_one_by_ID($set_column_values['GRP_ID']); |
|
| 2451 | - if ($mtpg_obj instanceof EE_Message_Template_Group) { |
|
| 2452 | - $mtpg_obj->set_template_pack_name($template_pack); |
|
| 2453 | - $mtpg_obj->set_template_pack_variation($template_variation); |
|
| 2454 | - } |
|
| 2455 | - $success = 1; |
|
| 2456 | - } |
|
| 2457 | - } |
|
| 2458 | - } |
|
| 2450 | + $mtpg_obj = $MTPG->get_one_by_ID($set_column_values['GRP_ID']); |
|
| 2451 | + if ($mtpg_obj instanceof EE_Message_Template_Group) { |
|
| 2452 | + $mtpg_obj->set_template_pack_name($template_pack); |
|
| 2453 | + $mtpg_obj->set_template_pack_variation($template_variation); |
|
| 2454 | + } |
|
| 2455 | + $success = 1; |
|
| 2456 | + } |
|
| 2457 | + } |
|
| 2458 | + } |
|
| 2459 | 2459 | |
| 2460 | - } |
|
| 2461 | - |
|
| 2462 | - //we return things differently if doing ajax |
|
| 2463 | - if (defined('DOING_AJAX') && DOING_AJAX) { |
|
| 2464 | - $this->_template_args['success'] = $success; |
|
| 2465 | - $this->_template_args['error'] = ! $success ? true : false; |
|
| 2466 | - $this->_template_args['content'] = ''; |
|
| 2467 | - $this->_template_args['data'] = array( |
|
| 2468 | - 'grpID' => $edit_array['GRP_ID'], |
|
| 2469 | - 'templateName' => $edit_array['template_name'] |
|
| 2470 | - ); |
|
| 2471 | - if ($success) { |
|
| 2472 | - EE_Error::overwrite_success(); |
|
| 2473 | - EE_Error::add_success(__('The new template has been created and automatically selected for this event. You can edit the new template by clicking the edit button. Note before this template is assigned to this event, the event must be saved.', |
|
| 2474 | - 'event_espresso')); |
|
| 2475 | - } |
|
| 2460 | + } |
|
| 2461 | + |
|
| 2462 | + //we return things differently if doing ajax |
|
| 2463 | + if (defined('DOING_AJAX') && DOING_AJAX) { |
|
| 2464 | + $this->_template_args['success'] = $success; |
|
| 2465 | + $this->_template_args['error'] = ! $success ? true : false; |
|
| 2466 | + $this->_template_args['content'] = ''; |
|
| 2467 | + $this->_template_args['data'] = array( |
|
| 2468 | + 'grpID' => $edit_array['GRP_ID'], |
|
| 2469 | + 'templateName' => $edit_array['template_name'] |
|
| 2470 | + ); |
|
| 2471 | + if ($success) { |
|
| 2472 | + EE_Error::overwrite_success(); |
|
| 2473 | + EE_Error::add_success(__('The new template has been created and automatically selected for this event. You can edit the new template by clicking the edit button. Note before this template is assigned to this event, the event must be saved.', |
|
| 2474 | + 'event_espresso')); |
|
| 2475 | + } |
|
| 2476 | 2476 | |
| 2477 | - $this->_return_json(); |
|
| 2478 | - } |
|
| 2479 | - |
|
| 2480 | - |
|
| 2481 | - //was a test send triggered? |
|
| 2482 | - if (isset($this->_req_data['test_button'])) { |
|
| 2483 | - EE_Error::overwrite_success(); |
|
| 2484 | - $this->_do_test_send($context_slug, $messenger_slug, $message_type_slug); |
|
| 2485 | - $override = true; |
|
| 2486 | - } |
|
| 2487 | - |
|
| 2488 | - if (empty($query_args)) { |
|
| 2489 | - $query_args = array( |
|
| 2490 | - 'id' => $this->_req_data['GRP_ID'], |
|
| 2491 | - 'context' => $context_slug, |
|
| 2492 | - 'action' => 'edit_message_template' |
|
| 2493 | - ); |
|
| 2494 | - } |
|
| 2495 | - |
|
| 2496 | - $this->_redirect_after_action($success, $item_desc, $action_desc, $query_args, $override); |
|
| 2497 | - } |
|
| 2498 | - |
|
| 2499 | - |
|
| 2500 | - /** |
|
| 2501 | - * processes a test send request to do an actual messenger delivery test for the given message template being tested |
|
| 2502 | - * |
|
| 2503 | - * @param string $context what context being tested |
|
| 2504 | - * @param string $messenger messenger being tested |
|
| 2505 | - * @param string $message_type message type being tested |
|
| 2506 | - * |
|
| 2507 | - */ |
|
| 2508 | - protected function _do_test_send($context, $messenger, $message_type) |
|
| 2509 | - { |
|
| 2510 | - //set things up for preview |
|
| 2511 | - $this->_req_data['messenger'] = $messenger; |
|
| 2512 | - $this->_req_data['message_type'] = $message_type; |
|
| 2513 | - $this->_req_data['context'] = $context; |
|
| 2514 | - $this->_req_data['GRP_ID'] = isset($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : ''; |
|
| 2515 | - $active_messenger = $this->_message_resource_manager->get_active_messenger($messenger); |
|
| 2516 | - |
|
| 2517 | - //let's save any existing fields that might be required by the messenger |
|
| 2518 | - if ( |
|
| 2519 | - isset($this->_req_data['test_settings_fld']) |
|
| 2520 | - && $active_messenger instanceof EE_messenger |
|
| 2521 | - && apply_filters( |
|
| 2522 | - 'FHEE__Messages_Admin_Page__do_test_send__set_existing_test_settings', |
|
| 2523 | - true, |
|
| 2524 | - $this->_req_data['test_settings_fld'], |
|
| 2525 | - $active_messenger |
|
| 2526 | - ) |
|
| 2527 | - ) { |
|
| 2528 | - $active_messenger->set_existing_test_settings($this->_req_data['test_settings_fld']); |
|
| 2529 | - } |
|
| 2530 | - |
|
| 2531 | - $success = $this->_preview_message(true); |
|
| 2532 | - |
|
| 2533 | - if ($success) { |
|
| 2534 | - EE_Error::add_success(__('Test message sent', 'event_espresso')); |
|
| 2535 | - } else { |
|
| 2536 | - EE_Error::add_error(__('The test message was not sent', 'event_espresso'), __FILE__, __FUNCTION__, |
|
| 2537 | - __LINE__); |
|
| 2538 | - } |
|
| 2539 | - } |
|
| 2540 | - |
|
| 2541 | - |
|
| 2542 | - /** |
|
| 2543 | - * _generate_new_templates |
|
| 2544 | - * This will handle the messenger, message_type selection when "adding a new custom template" for an event and will |
|
| 2545 | - * automatically create the defaults for the event. The user would then be redirected to edit the default context |
|
| 2546 | - * for the event. |
|
| 2547 | - * |
|
| 2548 | - * |
|
| 2549 | - * @param string $messenger the messenger we are generating templates for |
|
| 2550 | - * @param array $message_types array of message types that the templates are generated for. |
|
| 2551 | - * @param int $GRP_ID If this is a custom template being generated then a GRP_ID needs to be included to |
|
| 2552 | - * indicate the message_template_group being used as the base. |
|
| 2553 | - * |
|
| 2554 | - * @param bool $global |
|
| 2555 | - * |
|
| 2556 | - * @return array|bool array of data required for the redirect to the correct edit page or bool if |
|
| 2557 | - * encountering problems. |
|
| 2558 | - * @throws \EE_Error |
|
| 2559 | - */ |
|
| 2560 | - protected function _generate_new_templates($messenger, $message_types, $GRP_ID = 0, $global = false) |
|
| 2561 | - { |
|
| 2562 | - |
|
| 2563 | - //if no $message_types are given then that's okay... this may be a messenger that just adds shortcodes, so we just don't generate any templates. |
|
| 2564 | - if (empty($message_types)) { |
|
| 2565 | - return true; |
|
| 2566 | - } |
|
| 2567 | - |
|
| 2568 | - return EEH_MSG_Template::generate_new_templates($messenger, $message_types, $GRP_ID, $global); |
|
| 2569 | - } |
|
| 2570 | - |
|
| 2571 | - |
|
| 2572 | - /** |
|
| 2573 | - * [_trash_or_restore_message_template] |
|
| 2574 | - * |
|
| 2575 | - * @param boolean $trash whether to move an item to trash/restore (TRUE) or restore it (FALSE) |
|
| 2576 | - * @param boolean $all whether this is going to trash/restore all contexts within a template group (TRUE) OR just |
|
| 2577 | - * an individual context (FALSE). |
|
| 2578 | - * |
|
| 2579 | - * @return void |
|
| 2580 | - */ |
|
| 2581 | - protected function _trash_or_restore_message_template($trash = true, $all = false) |
|
| 2582 | - { |
|
| 2583 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 2584 | - $MTP = EEM_Message_Template_Group::instance(); |
|
| 2585 | - |
|
| 2586 | - $success = 1; |
|
| 2587 | - |
|
| 2588 | - //incoming GRP_IDs |
|
| 2589 | - if ($all) { |
|
| 2590 | - //Checkboxes |
|
| 2591 | - if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 2592 | - //if array has more than one element then success message should be plural. |
|
| 2593 | - //todo: what about nonce? |
|
| 2594 | - $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 2477 | + $this->_return_json(); |
|
| 2478 | + } |
|
| 2479 | + |
|
| 2480 | + |
|
| 2481 | + //was a test send triggered? |
|
| 2482 | + if (isset($this->_req_data['test_button'])) { |
|
| 2483 | + EE_Error::overwrite_success(); |
|
| 2484 | + $this->_do_test_send($context_slug, $messenger_slug, $message_type_slug); |
|
| 2485 | + $override = true; |
|
| 2486 | + } |
|
| 2487 | + |
|
| 2488 | + if (empty($query_args)) { |
|
| 2489 | + $query_args = array( |
|
| 2490 | + 'id' => $this->_req_data['GRP_ID'], |
|
| 2491 | + 'context' => $context_slug, |
|
| 2492 | + 'action' => 'edit_message_template' |
|
| 2493 | + ); |
|
| 2494 | + } |
|
| 2495 | + |
|
| 2496 | + $this->_redirect_after_action($success, $item_desc, $action_desc, $query_args, $override); |
|
| 2497 | + } |
|
| 2498 | + |
|
| 2499 | + |
|
| 2500 | + /** |
|
| 2501 | + * processes a test send request to do an actual messenger delivery test for the given message template being tested |
|
| 2502 | + * |
|
| 2503 | + * @param string $context what context being tested |
|
| 2504 | + * @param string $messenger messenger being tested |
|
| 2505 | + * @param string $message_type message type being tested |
|
| 2506 | + * |
|
| 2507 | + */ |
|
| 2508 | + protected function _do_test_send($context, $messenger, $message_type) |
|
| 2509 | + { |
|
| 2510 | + //set things up for preview |
|
| 2511 | + $this->_req_data['messenger'] = $messenger; |
|
| 2512 | + $this->_req_data['message_type'] = $message_type; |
|
| 2513 | + $this->_req_data['context'] = $context; |
|
| 2514 | + $this->_req_data['GRP_ID'] = isset($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : ''; |
|
| 2515 | + $active_messenger = $this->_message_resource_manager->get_active_messenger($messenger); |
|
| 2516 | + |
|
| 2517 | + //let's save any existing fields that might be required by the messenger |
|
| 2518 | + if ( |
|
| 2519 | + isset($this->_req_data['test_settings_fld']) |
|
| 2520 | + && $active_messenger instanceof EE_messenger |
|
| 2521 | + && apply_filters( |
|
| 2522 | + 'FHEE__Messages_Admin_Page__do_test_send__set_existing_test_settings', |
|
| 2523 | + true, |
|
| 2524 | + $this->_req_data['test_settings_fld'], |
|
| 2525 | + $active_messenger |
|
| 2526 | + ) |
|
| 2527 | + ) { |
|
| 2528 | + $active_messenger->set_existing_test_settings($this->_req_data['test_settings_fld']); |
|
| 2529 | + } |
|
| 2530 | + |
|
| 2531 | + $success = $this->_preview_message(true); |
|
| 2532 | + |
|
| 2533 | + if ($success) { |
|
| 2534 | + EE_Error::add_success(__('Test message sent', 'event_espresso')); |
|
| 2535 | + } else { |
|
| 2536 | + EE_Error::add_error(__('The test message was not sent', 'event_espresso'), __FILE__, __FUNCTION__, |
|
| 2537 | + __LINE__); |
|
| 2538 | + } |
|
| 2539 | + } |
|
| 2540 | + |
|
| 2541 | + |
|
| 2542 | + /** |
|
| 2543 | + * _generate_new_templates |
|
| 2544 | + * This will handle the messenger, message_type selection when "adding a new custom template" for an event and will |
|
| 2545 | + * automatically create the defaults for the event. The user would then be redirected to edit the default context |
|
| 2546 | + * for the event. |
|
| 2547 | + * |
|
| 2548 | + * |
|
| 2549 | + * @param string $messenger the messenger we are generating templates for |
|
| 2550 | + * @param array $message_types array of message types that the templates are generated for. |
|
| 2551 | + * @param int $GRP_ID If this is a custom template being generated then a GRP_ID needs to be included to |
|
| 2552 | + * indicate the message_template_group being used as the base. |
|
| 2553 | + * |
|
| 2554 | + * @param bool $global |
|
| 2555 | + * |
|
| 2556 | + * @return array|bool array of data required for the redirect to the correct edit page or bool if |
|
| 2557 | + * encountering problems. |
|
| 2558 | + * @throws \EE_Error |
|
| 2559 | + */ |
|
| 2560 | + protected function _generate_new_templates($messenger, $message_types, $GRP_ID = 0, $global = false) |
|
| 2561 | + { |
|
| 2562 | + |
|
| 2563 | + //if no $message_types are given then that's okay... this may be a messenger that just adds shortcodes, so we just don't generate any templates. |
|
| 2564 | + if (empty($message_types)) { |
|
| 2565 | + return true; |
|
| 2566 | + } |
|
| 2567 | + |
|
| 2568 | + return EEH_MSG_Template::generate_new_templates($messenger, $message_types, $GRP_ID, $global); |
|
| 2569 | + } |
|
| 2570 | + |
|
| 2571 | + |
|
| 2572 | + /** |
|
| 2573 | + * [_trash_or_restore_message_template] |
|
| 2574 | + * |
|
| 2575 | + * @param boolean $trash whether to move an item to trash/restore (TRUE) or restore it (FALSE) |
|
| 2576 | + * @param boolean $all whether this is going to trash/restore all contexts within a template group (TRUE) OR just |
|
| 2577 | + * an individual context (FALSE). |
|
| 2578 | + * |
|
| 2579 | + * @return void |
|
| 2580 | + */ |
|
| 2581 | + protected function _trash_or_restore_message_template($trash = true, $all = false) |
|
| 2582 | + { |
|
| 2583 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 2584 | + $MTP = EEM_Message_Template_Group::instance(); |
|
| 2585 | + |
|
| 2586 | + $success = 1; |
|
| 2587 | + |
|
| 2588 | + //incoming GRP_IDs |
|
| 2589 | + if ($all) { |
|
| 2590 | + //Checkboxes |
|
| 2591 | + if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 2592 | + //if array has more than one element then success message should be plural. |
|
| 2593 | + //todo: what about nonce? |
|
| 2594 | + $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 2595 | 2595 | |
| 2596 | - //cycle through checkboxes |
|
| 2597 | - while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 2598 | - $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID); |
|
| 2599 | - if ( ! $trashed_or_restored) { |
|
| 2600 | - $success = 0; |
|
| 2601 | - } |
|
| 2602 | - } |
|
| 2603 | - } else { |
|
| 2604 | - //grab single GRP_ID and handle |
|
| 2605 | - $GRP_ID = isset($this->_req_data['id']) ? absint($this->_req_data['id']) : 0; |
|
| 2606 | - if ( ! empty($GRP_ID)) { |
|
| 2607 | - $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID); |
|
| 2608 | - if ( ! $trashed_or_restored) { |
|
| 2609 | - $success = 0; |
|
| 2610 | - } |
|
| 2611 | - } else { |
|
| 2612 | - $success = 0; |
|
| 2613 | - } |
|
| 2614 | - } |
|
| 2596 | + //cycle through checkboxes |
|
| 2597 | + while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 2598 | + $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID); |
|
| 2599 | + if ( ! $trashed_or_restored) { |
|
| 2600 | + $success = 0; |
|
| 2601 | + } |
|
| 2602 | + } |
|
| 2603 | + } else { |
|
| 2604 | + //grab single GRP_ID and handle |
|
| 2605 | + $GRP_ID = isset($this->_req_data['id']) ? absint($this->_req_data['id']) : 0; |
|
| 2606 | + if ( ! empty($GRP_ID)) { |
|
| 2607 | + $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID); |
|
| 2608 | + if ( ! $trashed_or_restored) { |
|
| 2609 | + $success = 0; |
|
| 2610 | + } |
|
| 2611 | + } else { |
|
| 2612 | + $success = 0; |
|
| 2613 | + } |
|
| 2614 | + } |
|
| 2615 | 2615 | |
| 2616 | - } |
|
| 2616 | + } |
|
| 2617 | 2617 | |
| 2618 | - $action_desc = $trash ? __('moved to the trash', 'event_espresso') : __('restored', 'event_espresso'); |
|
| 2618 | + $action_desc = $trash ? __('moved to the trash', 'event_espresso') : __('restored', 'event_espresso'); |
|
| 2619 | 2619 | |
| 2620 | - $action_desc = ! empty($this->_req_data['template_switch']) ? __('switched') : $action_desc; |
|
| 2620 | + $action_desc = ! empty($this->_req_data['template_switch']) ? __('switched') : $action_desc; |
|
| 2621 | 2621 | |
| 2622 | - $item_desc = $all ? _n('Message Template Group', 'Message Template Groups', $success, |
|
| 2623 | - 'event_espresso') : _n('Message Template Context', 'Message Template Contexts', $success, 'event_espresso'); |
|
| 2622 | + $item_desc = $all ? _n('Message Template Group', 'Message Template Groups', $success, |
|
| 2623 | + 'event_espresso') : _n('Message Template Context', 'Message Template Contexts', $success, 'event_espresso'); |
|
| 2624 | 2624 | |
| 2625 | - $item_desc = ! empty($this->_req_data['template_switch']) ? _n('template', 'templates', $success, |
|
| 2626 | - 'event_espresso') : $item_desc; |
|
| 2625 | + $item_desc = ! empty($this->_req_data['template_switch']) ? _n('template', 'templates', $success, |
|
| 2626 | + 'event_espresso') : $item_desc; |
|
| 2627 | 2627 | |
| 2628 | - $this->_redirect_after_action($success, $item_desc, $action_desc, array()); |
|
| 2628 | + $this->_redirect_after_action($success, $item_desc, $action_desc, array()); |
|
| 2629 | 2629 | |
| 2630 | - } |
|
| 2630 | + } |
|
| 2631 | 2631 | |
| 2632 | 2632 | |
| 2633 | - /** |
|
| 2634 | - * [_delete_message_template] |
|
| 2635 | - * NOTE: this handles not only the deletion of the groups but also all the templates belonging to that group. |
|
| 2636 | - * @return void |
|
| 2637 | - */ |
|
| 2638 | - protected function _delete_message_template() |
|
| 2639 | - { |
|
| 2640 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 2633 | + /** |
|
| 2634 | + * [_delete_message_template] |
|
| 2635 | + * NOTE: this handles not only the deletion of the groups but also all the templates belonging to that group. |
|
| 2636 | + * @return void |
|
| 2637 | + */ |
|
| 2638 | + protected function _delete_message_template() |
|
| 2639 | + { |
|
| 2640 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 2641 | 2641 | |
| 2642 | - //checkboxes |
|
| 2643 | - if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 2644 | - //if array has more than one element then success message should be plural |
|
| 2645 | - $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 2642 | + //checkboxes |
|
| 2643 | + if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 2644 | + //if array has more than one element then success message should be plural |
|
| 2645 | + $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 2646 | 2646 | |
| 2647 | - //cycle through bulk action checkboxes |
|
| 2648 | - while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 2649 | - $success = $this->_delete_mtp_permanently($GRP_ID); |
|
| 2650 | - } |
|
| 2651 | - } else { |
|
| 2652 | - //grab single grp_id and delete |
|
| 2653 | - $GRP_ID = absint($this->_req_data['id']); |
|
| 2654 | - $success = $this->_delete_mtp_permanently($GRP_ID); |
|
| 2655 | - } |
|
| 2656 | - |
|
| 2657 | - $this->_redirect_after_action($success, 'Message Templates', 'deleted', array()); |
|
| 2658 | - |
|
| 2659 | - } |
|
| 2660 | - |
|
| 2661 | - |
|
| 2662 | - /** |
|
| 2663 | - * helper for permanently deleting a mtP group and all related message_templates |
|
| 2664 | - * |
|
| 2665 | - * @param int $GRP_ID The group being deleted |
|
| 2666 | - * @param bool $include_group whether to delete the Message Template Group as well. |
|
| 2667 | - * |
|
| 2668 | - * @return bool boolean to indicate the success of the deletes or not. |
|
| 2669 | - */ |
|
| 2670 | - private function _delete_mtp_permanently($GRP_ID, $include_group = true) |
|
| 2671 | - { |
|
| 2672 | - $success = 1; |
|
| 2673 | - $MTPG = EEM_Message_Template_Group::instance(); |
|
| 2674 | - //first let's GET this group |
|
| 2675 | - $MTG = $MTPG->get_one_by_ID($GRP_ID); |
|
| 2676 | - //then delete permanently all the related Message Templates |
|
| 2677 | - $deleted = $MTG->delete_related_permanently('Message_Template'); |
|
| 2678 | - |
|
| 2679 | - if ($deleted === 0) { |
|
| 2680 | - $success = 0; |
|
| 2681 | - } |
|
| 2682 | - |
|
| 2683 | - //now delete permanently this particular group |
|
| 2684 | - |
|
| 2685 | - if ($include_group && ! $MTG->delete_permanently()) { |
|
| 2686 | - $success = 0; |
|
| 2687 | - } |
|
| 2688 | - |
|
| 2689 | - return $success; |
|
| 2690 | - } |
|
| 2691 | - |
|
| 2692 | - |
|
| 2693 | - /** |
|
| 2694 | - * _learn_more_about_message_templates_link |
|
| 2695 | - * @access protected |
|
| 2696 | - * @return string |
|
| 2697 | - */ |
|
| 2698 | - protected function _learn_more_about_message_templates_link() |
|
| 2699 | - { |
|
| 2700 | - return '<a class="hidden" style="margin:0 20px; cursor:pointer; font-size:12px;" >' . __('learn more about how message templates works', |
|
| 2701 | - 'event_espresso') . '</a>'; |
|
| 2702 | - } |
|
| 2703 | - |
|
| 2704 | - |
|
| 2705 | - /** |
|
| 2706 | - * Used for setting up messenger/message type activation. This loads up the initial view. The rest is handled by |
|
| 2707 | - * ajax and other routes. |
|
| 2708 | - * @return void |
|
| 2709 | - */ |
|
| 2710 | - protected function _settings() |
|
| 2711 | - { |
|
| 2712 | - |
|
| 2713 | - |
|
| 2714 | - $this->_set_m_mt_settings(); |
|
| 2715 | - |
|
| 2716 | - $selected_messenger = isset($this->_req_data['selected_messenger']) ? $this->_req_data['selected_messenger'] : 'email'; |
|
| 2717 | - |
|
| 2718 | - //let's setup the messenger tabs |
|
| 2719 | - $this->_template_args['admin_page_header'] = EEH_Tabbed_Content::tab_text_links($this->_m_mt_settings['messenger_tabs'], |
|
| 2720 | - 'messenger_links', '|', $selected_messenger); |
|
| 2721 | - $this->_template_args['before_admin_page_content'] = '<div class="ui-widget ui-helper-clearfix">'; |
|
| 2722 | - $this->_template_args['after_admin_page_content'] = '</div><!-- end .ui-widget -->'; |
|
| 2723 | - |
|
| 2724 | - $this->display_admin_page_with_sidebar(); |
|
| 2725 | - |
|
| 2726 | - } |
|
| 2727 | - |
|
| 2728 | - |
|
| 2729 | - /** |
|
| 2730 | - * This sets the $_m_mt_settings property for when needed (used on the Messages settings page) |
|
| 2731 | - * |
|
| 2732 | - * @access protected |
|
| 2733 | - * @return void |
|
| 2734 | - */ |
|
| 2735 | - protected function _set_m_mt_settings() |
|
| 2736 | - { |
|
| 2737 | - //first if this is already set then lets get out no need to regenerate data. |
|
| 2738 | - if ( ! empty($this->_m_mt_settings)) { |
|
| 2739 | - return; |
|
| 2740 | - } |
|
| 2741 | - |
|
| 2742 | - //$selected_messenger = isset( $this->_req_data['selected_messenger'] ) ? $this->_req_data['selected_messenger'] : 'email'; |
|
| 2743 | - |
|
| 2744 | - //get all installed messengers and message_types |
|
| 2745 | - /** @type EE_messenger[] $messengers */ |
|
| 2746 | - $messengers = $this->_message_resource_manager->installed_messengers(); |
|
| 2747 | - /** @type EE_message_type[] $message_types */ |
|
| 2748 | - $message_types = $this->_message_resource_manager->installed_message_types(); |
|
| 2749 | - |
|
| 2750 | - |
|
| 2751 | - //assemble the array for the _tab_text_links helper |
|
| 2752 | - |
|
| 2753 | - foreach ($messengers as $messenger) { |
|
| 2754 | - $this->_m_mt_settings['messenger_tabs'][$messenger->name] = array( |
|
| 2755 | - 'label' => ucwords($messenger->label['singular']), |
|
| 2756 | - 'class' => $this->_message_resource_manager->is_messenger_active($messenger->name) ? 'messenger-active' : '', |
|
| 2757 | - 'href' => $messenger->name, |
|
| 2758 | - 'title' => __('Modify this Messenger', 'event_espresso'), |
|
| 2759 | - 'slug' => $messenger->name, |
|
| 2760 | - 'obj' => $messenger |
|
| 2761 | - ); |
|
| 2647 | + //cycle through bulk action checkboxes |
|
| 2648 | + while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 2649 | + $success = $this->_delete_mtp_permanently($GRP_ID); |
|
| 2650 | + } |
|
| 2651 | + } else { |
|
| 2652 | + //grab single grp_id and delete |
|
| 2653 | + $GRP_ID = absint($this->_req_data['id']); |
|
| 2654 | + $success = $this->_delete_mtp_permanently($GRP_ID); |
|
| 2655 | + } |
|
| 2656 | + |
|
| 2657 | + $this->_redirect_after_action($success, 'Message Templates', 'deleted', array()); |
|
| 2658 | + |
|
| 2659 | + } |
|
| 2660 | + |
|
| 2661 | + |
|
| 2662 | + /** |
|
| 2663 | + * helper for permanently deleting a mtP group and all related message_templates |
|
| 2664 | + * |
|
| 2665 | + * @param int $GRP_ID The group being deleted |
|
| 2666 | + * @param bool $include_group whether to delete the Message Template Group as well. |
|
| 2667 | + * |
|
| 2668 | + * @return bool boolean to indicate the success of the deletes or not. |
|
| 2669 | + */ |
|
| 2670 | + private function _delete_mtp_permanently($GRP_ID, $include_group = true) |
|
| 2671 | + { |
|
| 2672 | + $success = 1; |
|
| 2673 | + $MTPG = EEM_Message_Template_Group::instance(); |
|
| 2674 | + //first let's GET this group |
|
| 2675 | + $MTG = $MTPG->get_one_by_ID($GRP_ID); |
|
| 2676 | + //then delete permanently all the related Message Templates |
|
| 2677 | + $deleted = $MTG->delete_related_permanently('Message_Template'); |
|
| 2678 | + |
|
| 2679 | + if ($deleted === 0) { |
|
| 2680 | + $success = 0; |
|
| 2681 | + } |
|
| 2682 | + |
|
| 2683 | + //now delete permanently this particular group |
|
| 2684 | + |
|
| 2685 | + if ($include_group && ! $MTG->delete_permanently()) { |
|
| 2686 | + $success = 0; |
|
| 2687 | + } |
|
| 2688 | + |
|
| 2689 | + return $success; |
|
| 2690 | + } |
|
| 2691 | + |
|
| 2692 | + |
|
| 2693 | + /** |
|
| 2694 | + * _learn_more_about_message_templates_link |
|
| 2695 | + * @access protected |
|
| 2696 | + * @return string |
|
| 2697 | + */ |
|
| 2698 | + protected function _learn_more_about_message_templates_link() |
|
| 2699 | + { |
|
| 2700 | + return '<a class="hidden" style="margin:0 20px; cursor:pointer; font-size:12px;" >' . __('learn more about how message templates works', |
|
| 2701 | + 'event_espresso') . '</a>'; |
|
| 2702 | + } |
|
| 2703 | + |
|
| 2704 | + |
|
| 2705 | + /** |
|
| 2706 | + * Used for setting up messenger/message type activation. This loads up the initial view. The rest is handled by |
|
| 2707 | + * ajax and other routes. |
|
| 2708 | + * @return void |
|
| 2709 | + */ |
|
| 2710 | + protected function _settings() |
|
| 2711 | + { |
|
| 2712 | + |
|
| 2713 | + |
|
| 2714 | + $this->_set_m_mt_settings(); |
|
| 2715 | + |
|
| 2716 | + $selected_messenger = isset($this->_req_data['selected_messenger']) ? $this->_req_data['selected_messenger'] : 'email'; |
|
| 2717 | + |
|
| 2718 | + //let's setup the messenger tabs |
|
| 2719 | + $this->_template_args['admin_page_header'] = EEH_Tabbed_Content::tab_text_links($this->_m_mt_settings['messenger_tabs'], |
|
| 2720 | + 'messenger_links', '|', $selected_messenger); |
|
| 2721 | + $this->_template_args['before_admin_page_content'] = '<div class="ui-widget ui-helper-clearfix">'; |
|
| 2722 | + $this->_template_args['after_admin_page_content'] = '</div><!-- end .ui-widget -->'; |
|
| 2723 | + |
|
| 2724 | + $this->display_admin_page_with_sidebar(); |
|
| 2725 | + |
|
| 2726 | + } |
|
| 2727 | + |
|
| 2728 | + |
|
| 2729 | + /** |
|
| 2730 | + * This sets the $_m_mt_settings property for when needed (used on the Messages settings page) |
|
| 2731 | + * |
|
| 2732 | + * @access protected |
|
| 2733 | + * @return void |
|
| 2734 | + */ |
|
| 2735 | + protected function _set_m_mt_settings() |
|
| 2736 | + { |
|
| 2737 | + //first if this is already set then lets get out no need to regenerate data. |
|
| 2738 | + if ( ! empty($this->_m_mt_settings)) { |
|
| 2739 | + return; |
|
| 2740 | + } |
|
| 2741 | + |
|
| 2742 | + //$selected_messenger = isset( $this->_req_data['selected_messenger'] ) ? $this->_req_data['selected_messenger'] : 'email'; |
|
| 2743 | + |
|
| 2744 | + //get all installed messengers and message_types |
|
| 2745 | + /** @type EE_messenger[] $messengers */ |
|
| 2746 | + $messengers = $this->_message_resource_manager->installed_messengers(); |
|
| 2747 | + /** @type EE_message_type[] $message_types */ |
|
| 2748 | + $message_types = $this->_message_resource_manager->installed_message_types(); |
|
| 2749 | + |
|
| 2750 | + |
|
| 2751 | + //assemble the array for the _tab_text_links helper |
|
| 2752 | + |
|
| 2753 | + foreach ($messengers as $messenger) { |
|
| 2754 | + $this->_m_mt_settings['messenger_tabs'][$messenger->name] = array( |
|
| 2755 | + 'label' => ucwords($messenger->label['singular']), |
|
| 2756 | + 'class' => $this->_message_resource_manager->is_messenger_active($messenger->name) ? 'messenger-active' : '', |
|
| 2757 | + 'href' => $messenger->name, |
|
| 2758 | + 'title' => __('Modify this Messenger', 'event_espresso'), |
|
| 2759 | + 'slug' => $messenger->name, |
|
| 2760 | + 'obj' => $messenger |
|
| 2761 | + ); |
|
| 2762 | 2762 | |
| 2763 | 2763 | |
| 2764 | - $message_types_for_messenger = $messenger->get_valid_message_types(); |
|
| 2764 | + $message_types_for_messenger = $messenger->get_valid_message_types(); |
|
| 2765 | 2765 | |
| 2766 | - foreach ($message_types as $message_type) { |
|
| 2767 | - //first we need to verify that this message type is valid with this messenger. Cause if it isn't then it shouldn't show in either the inactive OR active metabox. |
|
| 2768 | - if ( ! in_array($message_type->name, $message_types_for_messenger)) { |
|
| 2769 | - continue; |
|
| 2770 | - } |
|
| 2766 | + foreach ($message_types as $message_type) { |
|
| 2767 | + //first we need to verify that this message type is valid with this messenger. Cause if it isn't then it shouldn't show in either the inactive OR active metabox. |
|
| 2768 | + if ( ! in_array($message_type->name, $message_types_for_messenger)) { |
|
| 2769 | + continue; |
|
| 2770 | + } |
|
| 2771 | 2771 | |
| 2772 | - $a_or_i = $this->_message_resource_manager->is_message_type_active_for_messenger($messenger->name, |
|
| 2773 | - $message_type->name) ? 'active' : 'inactive'; |
|
| 2772 | + $a_or_i = $this->_message_resource_manager->is_message_type_active_for_messenger($messenger->name, |
|
| 2773 | + $message_type->name) ? 'active' : 'inactive'; |
|
| 2774 | 2774 | |
| 2775 | - $this->_m_mt_settings['message_type_tabs'][$messenger->name][$a_or_i][$message_type->name] = array( |
|
| 2776 | - 'label' => ucwords($message_type->label['singular']), |
|
| 2777 | - 'class' => 'message-type-' . $a_or_i, |
|
| 2778 | - 'slug_id' => $message_type->name . '-messagetype-' . $messenger->name, |
|
| 2779 | - 'mt_nonce' => wp_create_nonce($message_type->name . '_nonce'), |
|
| 2780 | - 'href' => 'espresso_' . $message_type->name . '_message_type_settings', |
|
| 2781 | - 'title' => $a_or_i == 'active' |
|
| 2782 | - ? __('Drag this message type to the Inactive window to deactivate', 'event_espresso') |
|
| 2783 | - : __('Drag this message type to the messenger to activate', 'event_espresso'), |
|
| 2784 | - 'content' => $a_or_i == 'active' |
|
| 2785 | - ? $this->_message_type_settings_content($message_type, $messenger, true) |
|
| 2786 | - : $this->_message_type_settings_content($message_type, $messenger), |
|
| 2787 | - 'slug' => $message_type->name, |
|
| 2788 | - 'active' => $a_or_i == 'active' ? true : false, |
|
| 2789 | - 'obj' => $message_type |
|
| 2790 | - ); |
|
| 2791 | - } |
|
| 2792 | - } |
|
| 2793 | - } |
|
| 2794 | - |
|
| 2795 | - |
|
| 2796 | - /** |
|
| 2797 | - * This just prepares the content for the message type settings |
|
| 2798 | - * |
|
| 2799 | - * @param object $message_type The message type object |
|
| 2800 | - * @param object $messenger The messenger object |
|
| 2801 | - * @param boolean $active Whether the message type is active or not |
|
| 2802 | - * |
|
| 2803 | - * @return string html output for the content |
|
| 2804 | - */ |
|
| 2805 | - protected function _message_type_settings_content($message_type, $messenger, $active = false) |
|
| 2806 | - { |
|
| 2807 | - //get message type fields |
|
| 2808 | - $fields = $message_type->get_admin_settings_fields(); |
|
| 2809 | - $settings_template_args['template_form_fields'] = ''; |
|
| 2810 | - |
|
| 2811 | - if ( ! empty($fields) && $active) { |
|
| 2775 | + $this->_m_mt_settings['message_type_tabs'][$messenger->name][$a_or_i][$message_type->name] = array( |
|
| 2776 | + 'label' => ucwords($message_type->label['singular']), |
|
| 2777 | + 'class' => 'message-type-' . $a_or_i, |
|
| 2778 | + 'slug_id' => $message_type->name . '-messagetype-' . $messenger->name, |
|
| 2779 | + 'mt_nonce' => wp_create_nonce($message_type->name . '_nonce'), |
|
| 2780 | + 'href' => 'espresso_' . $message_type->name . '_message_type_settings', |
|
| 2781 | + 'title' => $a_or_i == 'active' |
|
| 2782 | + ? __('Drag this message type to the Inactive window to deactivate', 'event_espresso') |
|
| 2783 | + : __('Drag this message type to the messenger to activate', 'event_espresso'), |
|
| 2784 | + 'content' => $a_or_i == 'active' |
|
| 2785 | + ? $this->_message_type_settings_content($message_type, $messenger, true) |
|
| 2786 | + : $this->_message_type_settings_content($message_type, $messenger), |
|
| 2787 | + 'slug' => $message_type->name, |
|
| 2788 | + 'active' => $a_or_i == 'active' ? true : false, |
|
| 2789 | + 'obj' => $message_type |
|
| 2790 | + ); |
|
| 2791 | + } |
|
| 2792 | + } |
|
| 2793 | + } |
|
| 2794 | + |
|
| 2795 | + |
|
| 2796 | + /** |
|
| 2797 | + * This just prepares the content for the message type settings |
|
| 2798 | + * |
|
| 2799 | + * @param object $message_type The message type object |
|
| 2800 | + * @param object $messenger The messenger object |
|
| 2801 | + * @param boolean $active Whether the message type is active or not |
|
| 2802 | + * |
|
| 2803 | + * @return string html output for the content |
|
| 2804 | + */ |
|
| 2805 | + protected function _message_type_settings_content($message_type, $messenger, $active = false) |
|
| 2806 | + { |
|
| 2807 | + //get message type fields |
|
| 2808 | + $fields = $message_type->get_admin_settings_fields(); |
|
| 2809 | + $settings_template_args['template_form_fields'] = ''; |
|
| 2810 | + |
|
| 2811 | + if ( ! empty($fields) && $active) { |
|
| 2812 | 2812 | |
| 2813 | - $existing_settings = $message_type->get_existing_admin_settings($messenger->name); |
|
| 2813 | + $existing_settings = $message_type->get_existing_admin_settings($messenger->name); |
|
| 2814 | 2814 | |
| 2815 | - foreach ($fields as $fldname => $fldprops) { |
|
| 2816 | - $field_id = $messenger->name . '-' . $message_type->name . '-' . $fldname; |
|
| 2817 | - $template_form_field[$field_id] = array( |
|
| 2818 | - 'name' => 'message_type_settings[' . $fldname . ']', |
|
| 2819 | - 'label' => $fldprops['label'], |
|
| 2820 | - 'input' => $fldprops['field_type'], |
|
| 2821 | - 'type' => $fldprops['value_type'], |
|
| 2822 | - 'required' => $fldprops['required'], |
|
| 2823 | - 'validation' => $fldprops['validation'], |
|
| 2824 | - 'value' => isset($existing_settings[$fldname]) ? $existing_settings[$fldname] : $fldprops['default'], |
|
| 2825 | - 'options' => isset($fldprops['options']) ? $fldprops['options'] : array(), |
|
| 2826 | - 'default' => isset($existing_settings[$fldname]) ? $existing_settings[$fldname] : $fldprops['default'], |
|
| 2827 | - 'css_class' => 'no-drag', |
|
| 2828 | - 'format' => $fldprops['format'] |
|
| 2829 | - ); |
|
| 2830 | - } |
|
| 2815 | + foreach ($fields as $fldname => $fldprops) { |
|
| 2816 | + $field_id = $messenger->name . '-' . $message_type->name . '-' . $fldname; |
|
| 2817 | + $template_form_field[$field_id] = array( |
|
| 2818 | + 'name' => 'message_type_settings[' . $fldname . ']', |
|
| 2819 | + 'label' => $fldprops['label'], |
|
| 2820 | + 'input' => $fldprops['field_type'], |
|
| 2821 | + 'type' => $fldprops['value_type'], |
|
| 2822 | + 'required' => $fldprops['required'], |
|
| 2823 | + 'validation' => $fldprops['validation'], |
|
| 2824 | + 'value' => isset($existing_settings[$fldname]) ? $existing_settings[$fldname] : $fldprops['default'], |
|
| 2825 | + 'options' => isset($fldprops['options']) ? $fldprops['options'] : array(), |
|
| 2826 | + 'default' => isset($existing_settings[$fldname]) ? $existing_settings[$fldname] : $fldprops['default'], |
|
| 2827 | + 'css_class' => 'no-drag', |
|
| 2828 | + 'format' => $fldprops['format'] |
|
| 2829 | + ); |
|
| 2830 | + } |
|
| 2831 | 2831 | |
| 2832 | 2832 | |
| 2833 | - $settings_template_args['template_form_fields'] = ! empty($template_form_field) ? $this->_generate_admin_form_fields($template_form_field, |
|
| 2834 | - 'string', 'ee_mt_activate_form') : ''; |
|
| 2835 | - } |
|
| 2836 | - |
|
| 2837 | - $settings_template_args['description'] = $message_type->description; |
|
| 2838 | - //we also need some hidden fields |
|
| 2839 | - $settings_template_args['hidden_fields'] = array( |
|
| 2840 | - 'message_type_settings[messenger]' => array( |
|
| 2841 | - 'type' => 'hidden', |
|
| 2842 | - 'value' => $messenger->name |
|
| 2843 | - ), |
|
| 2844 | - 'message_type_settings[message_type]' => array( |
|
| 2845 | - 'type' => 'hidden', |
|
| 2846 | - 'value' => $message_type->name |
|
| 2847 | - ), |
|
| 2848 | - 'type' => array( |
|
| 2849 | - 'type' => 'hidden', |
|
| 2850 | - 'value' => 'message_type' |
|
| 2851 | - ) |
|
| 2852 | - ); |
|
| 2853 | - |
|
| 2854 | - $settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields($settings_template_args['hidden_fields'], |
|
| 2855 | - 'array'); |
|
| 2856 | - $settings_template_args['show_form'] = empty($settings_template_args['template_form_fields']) ? ' hidden' : ''; |
|
| 2857 | - |
|
| 2858 | - |
|
| 2859 | - $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_mt_settings_content.template.php'; |
|
| 2860 | - $content = EEH_Template::display_template($template, $settings_template_args, true); |
|
| 2861 | - |
|
| 2862 | - return $content; |
|
| 2863 | - } |
|
| 2864 | - |
|
| 2865 | - |
|
| 2866 | - /** |
|
| 2867 | - * Generate all the metaboxes for the message types and register them for the messages settings page. |
|
| 2868 | - * |
|
| 2869 | - * @access protected |
|
| 2870 | - * @return void |
|
| 2871 | - */ |
|
| 2872 | - protected function _messages_settings_metaboxes() |
|
| 2873 | - { |
|
| 2874 | - $this->_set_m_mt_settings(); |
|
| 2875 | - $m_boxes = $mt_boxes = array(); |
|
| 2876 | - $m_template_args = $mt_template_args = array(); |
|
| 2877 | - |
|
| 2878 | - $selected_messenger = isset($this->_req_data['selected_messenger']) ? $this->_req_data['selected_messenger'] : 'email'; |
|
| 2879 | - |
|
| 2880 | - if (isset($this->_m_mt_settings['messenger_tabs'])) { |
|
| 2881 | - foreach ($this->_m_mt_settings['messenger_tabs'] as $messenger => $tab_array) { |
|
| 2882 | - $hide_on_message = $this->_message_resource_manager->is_messenger_active($messenger) ? '' : 'hidden'; |
|
| 2883 | - $hide_off_message = $this->_message_resource_manager->is_messenger_active($messenger) ? 'hidden' : ''; |
|
| 2884 | - //messenger meta boxes |
|
| 2885 | - $active = $selected_messenger == $messenger ? true : false; |
|
| 2886 | - $active_mt_tabs = isset($this->_m_mt_settings['message_type_tabs'][$messenger]['active']) |
|
| 2887 | - ? $this->_m_mt_settings['message_type_tabs'][$messenger]['active'] |
|
| 2888 | - : ''; |
|
| 2889 | - $m_boxes[$messenger . '_a_box'] = sprintf( |
|
| 2890 | - __('%s Settings', 'event_espresso'), |
|
| 2891 | - $tab_array['label'] |
|
| 2892 | - ); |
|
| 2893 | - $m_template_args[$messenger . '_a_box'] = array( |
|
| 2894 | - 'active_message_types' => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '', |
|
| 2895 | - 'inactive_message_types' => isset($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']) |
|
| 2896 | - ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']) |
|
| 2897 | - : '', |
|
| 2898 | - 'content' => $this->_get_messenger_box_content($tab_array['obj']), |
|
| 2899 | - 'hidden' => $active ? '' : ' hidden', |
|
| 2900 | - 'hide_on_message' => $hide_on_message, |
|
| 2901 | - 'messenger' => $messenger, |
|
| 2902 | - 'active' => $active |
|
| 2903 | - ); |
|
| 2904 | - // message type meta boxes |
|
| 2905 | - // (which is really just the inactive container for each messenger |
|
| 2906 | - // showing inactive message types for that messenger) |
|
| 2907 | - $mt_boxes[$messenger . '_i_box'] = __('Inactive Message Types', 'event_espresso'); |
|
| 2908 | - $mt_template_args[$messenger . '_i_box'] = array( |
|
| 2909 | - 'active_message_types' => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '', |
|
| 2910 | - 'inactive_message_types' => isset($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']) |
|
| 2911 | - ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']) |
|
| 2912 | - : '', |
|
| 2913 | - 'hidden' => $active ? '' : ' hidden', |
|
| 2914 | - 'hide_on_message' => $hide_on_message, |
|
| 2915 | - 'hide_off_message' => $hide_off_message, |
|
| 2916 | - 'messenger' => $messenger, |
|
| 2917 | - 'active' => $active |
|
| 2918 | - ); |
|
| 2919 | - } |
|
| 2920 | - } |
|
| 2921 | - |
|
| 2922 | - |
|
| 2923 | - //register messenger metaboxes |
|
| 2924 | - $m_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_mt_meta_box.template.php'; |
|
| 2925 | - foreach ($m_boxes as $box => $label) { |
|
| 2926 | - $callback_args = array('template_path' => $m_template_path, 'template_args' => $m_template_args[$box]); |
|
| 2927 | - $msgr = str_replace('_a_box', '', $box); |
|
| 2928 | - add_meta_box( |
|
| 2929 | - 'espresso_' . $msgr . '_settings', |
|
| 2930 | - $label, |
|
| 2931 | - function ($post, $metabox) { |
|
| 2932 | - echo EEH_Template::display_template($metabox["args"]["template_path"], |
|
| 2933 | - $metabox["args"]["template_args"], true); |
|
| 2934 | - }, |
|
| 2935 | - $this->_current_screen->id, |
|
| 2936 | - 'normal', |
|
| 2937 | - 'high', |
|
| 2938 | - $callback_args |
|
| 2939 | - ); |
|
| 2940 | - } |
|
| 2941 | - |
|
| 2942 | - //register message type metaboxes |
|
| 2943 | - $mt_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_meta_box.template.php'; |
|
| 2944 | - foreach ($mt_boxes as $box => $label) { |
|
| 2945 | - $callback_args = array( |
|
| 2946 | - 'template_path' => $mt_template_path, |
|
| 2947 | - 'template_args' => $mt_template_args[$box] |
|
| 2948 | - ); |
|
| 2949 | - $mt = str_replace('_i_box', '', $box); |
|
| 2950 | - add_meta_box( |
|
| 2951 | - 'espresso_' . $mt . '_inactive_mts', |
|
| 2952 | - $label, |
|
| 2953 | - function ($post, $metabox) { |
|
| 2954 | - echo EEH_Template::display_template($metabox["args"]["template_path"], |
|
| 2955 | - $metabox["args"]["template_args"], true); |
|
| 2956 | - }, |
|
| 2957 | - $this->_current_screen->id, |
|
| 2958 | - 'side', |
|
| 2959 | - 'high', |
|
| 2960 | - $callback_args |
|
| 2961 | - ); |
|
| 2962 | - } |
|
| 2963 | - |
|
| 2964 | - //register metabox for global messages settings but only when on the main site. On single site installs this will |
|
| 2965 | - //always result in the metabox showing, on multisite installs the metabox will only show on the main site. |
|
| 2966 | - if (is_main_site()) { |
|
| 2967 | - add_meta_box( |
|
| 2968 | - 'espresso_global_message_settings', |
|
| 2969 | - __('Global Message Settings', 'event_espresso'), |
|
| 2970 | - array($this, 'global_messages_settings_metabox_content'), |
|
| 2971 | - $this->_current_screen->id, |
|
| 2972 | - 'normal', |
|
| 2973 | - 'low', |
|
| 2974 | - array() |
|
| 2975 | - ); |
|
| 2976 | - } |
|
| 2977 | - |
|
| 2978 | - } |
|
| 2979 | - |
|
| 2980 | - |
|
| 2981 | - /** |
|
| 2982 | - * This generates the content for the global messages settings metabox. |
|
| 2983 | - * @return string |
|
| 2984 | - */ |
|
| 2985 | - public function global_messages_settings_metabox_content() |
|
| 2986 | - { |
|
| 2987 | - $form = $this->_generate_global_settings_form(); |
|
| 2988 | - echo $form->form_open( |
|
| 2989 | - $this->add_query_args_and_nonce(array('action' => 'update_global_settings'), EE_MSG_ADMIN_URL), |
|
| 2990 | - 'POST' |
|
| 2991 | - ) |
|
| 2992 | - . $form->get_html() |
|
| 2993 | - . $form->form_close(); |
|
| 2994 | - } |
|
| 2995 | - |
|
| 2996 | - |
|
| 2997 | - /** |
|
| 2998 | - * This generates and returns the form object for the global messages settings. |
|
| 2999 | - * @return EE_Form_Section_Proper |
|
| 3000 | - */ |
|
| 3001 | - protected function _generate_global_settings_form() |
|
| 3002 | - { |
|
| 3003 | - EE_Registry::instance()->load_helper('HTML'); |
|
| 3004 | - /** @var EE_Network_Core_Config $network_config */ |
|
| 3005 | - $network_config = EE_Registry::instance()->NET_CFG->core; |
|
| 3006 | - |
|
| 3007 | - return new EE_Form_Section_Proper( |
|
| 3008 | - array( |
|
| 3009 | - 'name' => 'global_messages_settings', |
|
| 3010 | - 'html_id' => 'global_messages_settings', |
|
| 3011 | - 'html_class' => 'form-table', |
|
| 3012 | - 'layout_strategy' => new EE_Admin_Two_Column_Layout(), |
|
| 3013 | - 'subsections' => apply_filters( |
|
| 3014 | - 'FHEE__Messages_Admin_Page__global_messages_settings_metabox_content__form_subsections', |
|
| 3015 | - array( |
|
| 3016 | - 'do_messages_on_same_request' => new EE_Select_Input( |
|
| 3017 | - array( |
|
| 3018 | - true => esc_html__("On the same request", "event_espresso"), |
|
| 3019 | - false => esc_html__("On a separate request", "event_espresso") |
|
| 3020 | - ), |
|
| 3021 | - array( |
|
| 3022 | - 'default' => $network_config->do_messages_on_same_request, |
|
| 3023 | - 'html_label_text' => esc_html__('Generate and send all messages:', 'event_espresso'), |
|
| 3024 | - 'html_help_text' => esc_html__('By default the messages system uses a more efficient means of processing messages on separate requests and utilizes the wp-cron scheduling system. This makes things execute faster for people registering for your events. However, if the wp-cron system is disabled on your site and there is no alternative in place, then you can change this so messages are always executed on the same request.', |
|
| 3025 | - 'event_espresso'), |
|
| 3026 | - ) |
|
| 3027 | - ), |
|
| 3028 | - 'delete_threshold' => new EE_Select_Input( |
|
| 3029 | - array( |
|
| 3030 | - 0 => esc_html__('Forever', 'event_espresso'), |
|
| 3031 | - 3 => esc_html__('3 Months', 'event_espresso'), |
|
| 3032 | - 6 => esc_html__('6 Months', 'event_espresso'), |
|
| 3033 | - 9 => esc_html__('9 Months', 'event_espresso'), |
|
| 3034 | - 12 => esc_html__('12 Months', 'event_espresso'), |
|
| 3035 | - 24 => esc_html__('24 Months', 'event_espresso'), |
|
| 3036 | - 36 => esc_html__('36 Months', 'event_espresso') |
|
| 3037 | - ), |
|
| 3038 | - array( |
|
| 3039 | - 'default' => EE_Registry::instance()->CFG->messages->delete_threshold, |
|
| 3040 | - 'html_label_text' => esc_html__('Cleanup of old messages:', 'event_espresso'), |
|
| 3041 | - 'html_help_text' => esc_html__('You can control how long a record of processed messages is kept |
|
| 2833 | + $settings_template_args['template_form_fields'] = ! empty($template_form_field) ? $this->_generate_admin_form_fields($template_form_field, |
|
| 2834 | + 'string', 'ee_mt_activate_form') : ''; |
|
| 2835 | + } |
|
| 2836 | + |
|
| 2837 | + $settings_template_args['description'] = $message_type->description; |
|
| 2838 | + //we also need some hidden fields |
|
| 2839 | + $settings_template_args['hidden_fields'] = array( |
|
| 2840 | + 'message_type_settings[messenger]' => array( |
|
| 2841 | + 'type' => 'hidden', |
|
| 2842 | + 'value' => $messenger->name |
|
| 2843 | + ), |
|
| 2844 | + 'message_type_settings[message_type]' => array( |
|
| 2845 | + 'type' => 'hidden', |
|
| 2846 | + 'value' => $message_type->name |
|
| 2847 | + ), |
|
| 2848 | + 'type' => array( |
|
| 2849 | + 'type' => 'hidden', |
|
| 2850 | + 'value' => 'message_type' |
|
| 2851 | + ) |
|
| 2852 | + ); |
|
| 2853 | + |
|
| 2854 | + $settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields($settings_template_args['hidden_fields'], |
|
| 2855 | + 'array'); |
|
| 2856 | + $settings_template_args['show_form'] = empty($settings_template_args['template_form_fields']) ? ' hidden' : ''; |
|
| 2857 | + |
|
| 2858 | + |
|
| 2859 | + $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_mt_settings_content.template.php'; |
|
| 2860 | + $content = EEH_Template::display_template($template, $settings_template_args, true); |
|
| 2861 | + |
|
| 2862 | + return $content; |
|
| 2863 | + } |
|
| 2864 | + |
|
| 2865 | + |
|
| 2866 | + /** |
|
| 2867 | + * Generate all the metaboxes for the message types and register them for the messages settings page. |
|
| 2868 | + * |
|
| 2869 | + * @access protected |
|
| 2870 | + * @return void |
|
| 2871 | + */ |
|
| 2872 | + protected function _messages_settings_metaboxes() |
|
| 2873 | + { |
|
| 2874 | + $this->_set_m_mt_settings(); |
|
| 2875 | + $m_boxes = $mt_boxes = array(); |
|
| 2876 | + $m_template_args = $mt_template_args = array(); |
|
| 2877 | + |
|
| 2878 | + $selected_messenger = isset($this->_req_data['selected_messenger']) ? $this->_req_data['selected_messenger'] : 'email'; |
|
| 2879 | + |
|
| 2880 | + if (isset($this->_m_mt_settings['messenger_tabs'])) { |
|
| 2881 | + foreach ($this->_m_mt_settings['messenger_tabs'] as $messenger => $tab_array) { |
|
| 2882 | + $hide_on_message = $this->_message_resource_manager->is_messenger_active($messenger) ? '' : 'hidden'; |
|
| 2883 | + $hide_off_message = $this->_message_resource_manager->is_messenger_active($messenger) ? 'hidden' : ''; |
|
| 2884 | + //messenger meta boxes |
|
| 2885 | + $active = $selected_messenger == $messenger ? true : false; |
|
| 2886 | + $active_mt_tabs = isset($this->_m_mt_settings['message_type_tabs'][$messenger]['active']) |
|
| 2887 | + ? $this->_m_mt_settings['message_type_tabs'][$messenger]['active'] |
|
| 2888 | + : ''; |
|
| 2889 | + $m_boxes[$messenger . '_a_box'] = sprintf( |
|
| 2890 | + __('%s Settings', 'event_espresso'), |
|
| 2891 | + $tab_array['label'] |
|
| 2892 | + ); |
|
| 2893 | + $m_template_args[$messenger . '_a_box'] = array( |
|
| 2894 | + 'active_message_types' => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '', |
|
| 2895 | + 'inactive_message_types' => isset($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']) |
|
| 2896 | + ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']) |
|
| 2897 | + : '', |
|
| 2898 | + 'content' => $this->_get_messenger_box_content($tab_array['obj']), |
|
| 2899 | + 'hidden' => $active ? '' : ' hidden', |
|
| 2900 | + 'hide_on_message' => $hide_on_message, |
|
| 2901 | + 'messenger' => $messenger, |
|
| 2902 | + 'active' => $active |
|
| 2903 | + ); |
|
| 2904 | + // message type meta boxes |
|
| 2905 | + // (which is really just the inactive container for each messenger |
|
| 2906 | + // showing inactive message types for that messenger) |
|
| 2907 | + $mt_boxes[$messenger . '_i_box'] = __('Inactive Message Types', 'event_espresso'); |
|
| 2908 | + $mt_template_args[$messenger . '_i_box'] = array( |
|
| 2909 | + 'active_message_types' => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '', |
|
| 2910 | + 'inactive_message_types' => isset($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']) |
|
| 2911 | + ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']) |
|
| 2912 | + : '', |
|
| 2913 | + 'hidden' => $active ? '' : ' hidden', |
|
| 2914 | + 'hide_on_message' => $hide_on_message, |
|
| 2915 | + 'hide_off_message' => $hide_off_message, |
|
| 2916 | + 'messenger' => $messenger, |
|
| 2917 | + 'active' => $active |
|
| 2918 | + ); |
|
| 2919 | + } |
|
| 2920 | + } |
|
| 2921 | + |
|
| 2922 | + |
|
| 2923 | + //register messenger metaboxes |
|
| 2924 | + $m_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_mt_meta_box.template.php'; |
|
| 2925 | + foreach ($m_boxes as $box => $label) { |
|
| 2926 | + $callback_args = array('template_path' => $m_template_path, 'template_args' => $m_template_args[$box]); |
|
| 2927 | + $msgr = str_replace('_a_box', '', $box); |
|
| 2928 | + add_meta_box( |
|
| 2929 | + 'espresso_' . $msgr . '_settings', |
|
| 2930 | + $label, |
|
| 2931 | + function ($post, $metabox) { |
|
| 2932 | + echo EEH_Template::display_template($metabox["args"]["template_path"], |
|
| 2933 | + $metabox["args"]["template_args"], true); |
|
| 2934 | + }, |
|
| 2935 | + $this->_current_screen->id, |
|
| 2936 | + 'normal', |
|
| 2937 | + 'high', |
|
| 2938 | + $callback_args |
|
| 2939 | + ); |
|
| 2940 | + } |
|
| 2941 | + |
|
| 2942 | + //register message type metaboxes |
|
| 2943 | + $mt_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_meta_box.template.php'; |
|
| 2944 | + foreach ($mt_boxes as $box => $label) { |
|
| 2945 | + $callback_args = array( |
|
| 2946 | + 'template_path' => $mt_template_path, |
|
| 2947 | + 'template_args' => $mt_template_args[$box] |
|
| 2948 | + ); |
|
| 2949 | + $mt = str_replace('_i_box', '', $box); |
|
| 2950 | + add_meta_box( |
|
| 2951 | + 'espresso_' . $mt . '_inactive_mts', |
|
| 2952 | + $label, |
|
| 2953 | + function ($post, $metabox) { |
|
| 2954 | + echo EEH_Template::display_template($metabox["args"]["template_path"], |
|
| 2955 | + $metabox["args"]["template_args"], true); |
|
| 2956 | + }, |
|
| 2957 | + $this->_current_screen->id, |
|
| 2958 | + 'side', |
|
| 2959 | + 'high', |
|
| 2960 | + $callback_args |
|
| 2961 | + ); |
|
| 2962 | + } |
|
| 2963 | + |
|
| 2964 | + //register metabox for global messages settings but only when on the main site. On single site installs this will |
|
| 2965 | + //always result in the metabox showing, on multisite installs the metabox will only show on the main site. |
|
| 2966 | + if (is_main_site()) { |
|
| 2967 | + add_meta_box( |
|
| 2968 | + 'espresso_global_message_settings', |
|
| 2969 | + __('Global Message Settings', 'event_espresso'), |
|
| 2970 | + array($this, 'global_messages_settings_metabox_content'), |
|
| 2971 | + $this->_current_screen->id, |
|
| 2972 | + 'normal', |
|
| 2973 | + 'low', |
|
| 2974 | + array() |
|
| 2975 | + ); |
|
| 2976 | + } |
|
| 2977 | + |
|
| 2978 | + } |
|
| 2979 | + |
|
| 2980 | + |
|
| 2981 | + /** |
|
| 2982 | + * This generates the content for the global messages settings metabox. |
|
| 2983 | + * @return string |
|
| 2984 | + */ |
|
| 2985 | + public function global_messages_settings_metabox_content() |
|
| 2986 | + { |
|
| 2987 | + $form = $this->_generate_global_settings_form(); |
|
| 2988 | + echo $form->form_open( |
|
| 2989 | + $this->add_query_args_and_nonce(array('action' => 'update_global_settings'), EE_MSG_ADMIN_URL), |
|
| 2990 | + 'POST' |
|
| 2991 | + ) |
|
| 2992 | + . $form->get_html() |
|
| 2993 | + . $form->form_close(); |
|
| 2994 | + } |
|
| 2995 | + |
|
| 2996 | + |
|
| 2997 | + /** |
|
| 2998 | + * This generates and returns the form object for the global messages settings. |
|
| 2999 | + * @return EE_Form_Section_Proper |
|
| 3000 | + */ |
|
| 3001 | + protected function _generate_global_settings_form() |
|
| 3002 | + { |
|
| 3003 | + EE_Registry::instance()->load_helper('HTML'); |
|
| 3004 | + /** @var EE_Network_Core_Config $network_config */ |
|
| 3005 | + $network_config = EE_Registry::instance()->NET_CFG->core; |
|
| 3006 | + |
|
| 3007 | + return new EE_Form_Section_Proper( |
|
| 3008 | + array( |
|
| 3009 | + 'name' => 'global_messages_settings', |
|
| 3010 | + 'html_id' => 'global_messages_settings', |
|
| 3011 | + 'html_class' => 'form-table', |
|
| 3012 | + 'layout_strategy' => new EE_Admin_Two_Column_Layout(), |
|
| 3013 | + 'subsections' => apply_filters( |
|
| 3014 | + 'FHEE__Messages_Admin_Page__global_messages_settings_metabox_content__form_subsections', |
|
| 3015 | + array( |
|
| 3016 | + 'do_messages_on_same_request' => new EE_Select_Input( |
|
| 3017 | + array( |
|
| 3018 | + true => esc_html__("On the same request", "event_espresso"), |
|
| 3019 | + false => esc_html__("On a separate request", "event_espresso") |
|
| 3020 | + ), |
|
| 3021 | + array( |
|
| 3022 | + 'default' => $network_config->do_messages_on_same_request, |
|
| 3023 | + 'html_label_text' => esc_html__('Generate and send all messages:', 'event_espresso'), |
|
| 3024 | + 'html_help_text' => esc_html__('By default the messages system uses a more efficient means of processing messages on separate requests and utilizes the wp-cron scheduling system. This makes things execute faster for people registering for your events. However, if the wp-cron system is disabled on your site and there is no alternative in place, then you can change this so messages are always executed on the same request.', |
|
| 3025 | + 'event_espresso'), |
|
| 3026 | + ) |
|
| 3027 | + ), |
|
| 3028 | + 'delete_threshold' => new EE_Select_Input( |
|
| 3029 | + array( |
|
| 3030 | + 0 => esc_html__('Forever', 'event_espresso'), |
|
| 3031 | + 3 => esc_html__('3 Months', 'event_espresso'), |
|
| 3032 | + 6 => esc_html__('6 Months', 'event_espresso'), |
|
| 3033 | + 9 => esc_html__('9 Months', 'event_espresso'), |
|
| 3034 | + 12 => esc_html__('12 Months', 'event_espresso'), |
|
| 3035 | + 24 => esc_html__('24 Months', 'event_espresso'), |
|
| 3036 | + 36 => esc_html__('36 Months', 'event_espresso') |
|
| 3037 | + ), |
|
| 3038 | + array( |
|
| 3039 | + 'default' => EE_Registry::instance()->CFG->messages->delete_threshold, |
|
| 3040 | + 'html_label_text' => esc_html__('Cleanup of old messages:', 'event_espresso'), |
|
| 3041 | + 'html_help_text' => esc_html__('You can control how long a record of processed messages is kept |
|
| 3042 | 3042 | via this option.', 'event_espresso'), |
| 3043 | - ) |
|
| 3044 | - ), |
|
| 3045 | - 'update_settings' => new EE_Submit_Input( |
|
| 3046 | - array( |
|
| 3047 | - 'default' => esc_html__('Update', 'event_espresso'), |
|
| 3048 | - 'html_label_text' => ' ' |
|
| 3049 | - ) |
|
| 3050 | - ) |
|
| 3051 | - ) |
|
| 3052 | - ) |
|
| 3053 | - ) |
|
| 3054 | - ); |
|
| 3055 | - } |
|
| 3056 | - |
|
| 3057 | - |
|
| 3058 | - /** |
|
| 3059 | - * This handles updating the global settings set on the admin page. |
|
| 3060 | - * @throws \EE_Error |
|
| 3061 | - */ |
|
| 3062 | - protected function _update_global_settings() |
|
| 3063 | - { |
|
| 3064 | - /** @var EE_Network_Core_Config $network_config */ |
|
| 3065 | - $network_config = EE_Registry::instance()->NET_CFG->core; |
|
| 3066 | - $messages_config = EE_Registry::instance()->CFG->messages; |
|
| 3067 | - $form = $this->_generate_global_settings_form(); |
|
| 3068 | - if ($form->was_submitted()) { |
|
| 3069 | - $form->receive_form_submission(); |
|
| 3070 | - if ($form->is_valid()) { |
|
| 3071 | - $valid_data = $form->valid_data(); |
|
| 3072 | - foreach ($valid_data as $property => $value) { |
|
| 3073 | - $setter = 'set_' . $property; |
|
| 3074 | - if (method_exists($network_config, $setter)) { |
|
| 3075 | - $network_config->{$setter}($value); |
|
| 3076 | - } else if ( |
|
| 3077 | - property_exists($network_config, $property) |
|
| 3078 | - && $network_config->{$property} !== $value |
|
| 3079 | - ) { |
|
| 3080 | - $network_config->{$property} = $value; |
|
| 3081 | - } else if ( |
|
| 3082 | - property_exists($messages_config, $property) |
|
| 3083 | - && $messages_config->{$property} !== $value |
|
| 3084 | - ) { |
|
| 3085 | - $messages_config->{$property} = $value; |
|
| 3086 | - } |
|
| 3087 | - } |
|
| 3088 | - //only update if the form submission was valid! |
|
| 3089 | - EE_Registry::instance()->NET_CFG->update_config(true, false); |
|
| 3090 | - EE_Registry::instance()->CFG->update_espresso_config(); |
|
| 3091 | - EE_Error::overwrite_success(); |
|
| 3092 | - EE_Error::add_success(__('Global message settings were updated', 'event_espresso')); |
|
| 3093 | - } |
|
| 3094 | - } |
|
| 3095 | - $this->_redirect_after_action(0, '', '', array('action' => 'settings'), true); |
|
| 3096 | - } |
|
| 3097 | - |
|
| 3098 | - |
|
| 3099 | - /** |
|
| 3100 | - * this prepares the messenger tabs that can be dragged in and out of messenger boxes to activate/deactivate |
|
| 3101 | - * |
|
| 3102 | - * @param array $tab_array This is an array of message type tab details used to generate the tabs |
|
| 3103 | - * |
|
| 3104 | - * @return string html formatted tabs |
|
| 3105 | - */ |
|
| 3106 | - protected function _get_mt_tabs($tab_array) |
|
| 3107 | - { |
|
| 3108 | - $tab_array = (array)$tab_array; |
|
| 3109 | - $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_mt_settings_tab_item.template.php'; |
|
| 3110 | - $tabs = ''; |
|
| 3111 | - |
|
| 3112 | - foreach ($tab_array as $tab) { |
|
| 3113 | - $tabs .= EEH_Template::display_template($template, $tab, true); |
|
| 3114 | - } |
|
| 3115 | - |
|
| 3116 | - return $tabs; |
|
| 3117 | - } |
|
| 3118 | - |
|
| 3119 | - |
|
| 3120 | - /** |
|
| 3121 | - * This prepares the content of the messenger meta box admin settings |
|
| 3122 | - * |
|
| 3123 | - * @param EE_messenger $messenger The messenger we're setting up content for |
|
| 3124 | - * |
|
| 3125 | - * @return string html formatted content |
|
| 3126 | - */ |
|
| 3127 | - protected function _get_messenger_box_content(EE_messenger $messenger) |
|
| 3128 | - { |
|
| 3129 | - |
|
| 3130 | - $fields = $messenger->get_admin_settings_fields(); |
|
| 3131 | - $settings_template_args['template_form_fields'] = ''; |
|
| 3132 | - |
|
| 3133 | - //is $messenger active? |
|
| 3134 | - $settings_template_args['active'] = $this->_message_resource_manager->is_messenger_active($messenger->name); |
|
| 3135 | - |
|
| 3136 | - |
|
| 3137 | - if ( ! empty($fields)) { |
|
| 3043 | + ) |
|
| 3044 | + ), |
|
| 3045 | + 'update_settings' => new EE_Submit_Input( |
|
| 3046 | + array( |
|
| 3047 | + 'default' => esc_html__('Update', 'event_espresso'), |
|
| 3048 | + 'html_label_text' => ' ' |
|
| 3049 | + ) |
|
| 3050 | + ) |
|
| 3051 | + ) |
|
| 3052 | + ) |
|
| 3053 | + ) |
|
| 3054 | + ); |
|
| 3055 | + } |
|
| 3056 | + |
|
| 3057 | + |
|
| 3058 | + /** |
|
| 3059 | + * This handles updating the global settings set on the admin page. |
|
| 3060 | + * @throws \EE_Error |
|
| 3061 | + */ |
|
| 3062 | + protected function _update_global_settings() |
|
| 3063 | + { |
|
| 3064 | + /** @var EE_Network_Core_Config $network_config */ |
|
| 3065 | + $network_config = EE_Registry::instance()->NET_CFG->core; |
|
| 3066 | + $messages_config = EE_Registry::instance()->CFG->messages; |
|
| 3067 | + $form = $this->_generate_global_settings_form(); |
|
| 3068 | + if ($form->was_submitted()) { |
|
| 3069 | + $form->receive_form_submission(); |
|
| 3070 | + if ($form->is_valid()) { |
|
| 3071 | + $valid_data = $form->valid_data(); |
|
| 3072 | + foreach ($valid_data as $property => $value) { |
|
| 3073 | + $setter = 'set_' . $property; |
|
| 3074 | + if (method_exists($network_config, $setter)) { |
|
| 3075 | + $network_config->{$setter}($value); |
|
| 3076 | + } else if ( |
|
| 3077 | + property_exists($network_config, $property) |
|
| 3078 | + && $network_config->{$property} !== $value |
|
| 3079 | + ) { |
|
| 3080 | + $network_config->{$property} = $value; |
|
| 3081 | + } else if ( |
|
| 3082 | + property_exists($messages_config, $property) |
|
| 3083 | + && $messages_config->{$property} !== $value |
|
| 3084 | + ) { |
|
| 3085 | + $messages_config->{$property} = $value; |
|
| 3086 | + } |
|
| 3087 | + } |
|
| 3088 | + //only update if the form submission was valid! |
|
| 3089 | + EE_Registry::instance()->NET_CFG->update_config(true, false); |
|
| 3090 | + EE_Registry::instance()->CFG->update_espresso_config(); |
|
| 3091 | + EE_Error::overwrite_success(); |
|
| 3092 | + EE_Error::add_success(__('Global message settings were updated', 'event_espresso')); |
|
| 3093 | + } |
|
| 3094 | + } |
|
| 3095 | + $this->_redirect_after_action(0, '', '', array('action' => 'settings'), true); |
|
| 3096 | + } |
|
| 3097 | + |
|
| 3098 | + |
|
| 3099 | + /** |
|
| 3100 | + * this prepares the messenger tabs that can be dragged in and out of messenger boxes to activate/deactivate |
|
| 3101 | + * |
|
| 3102 | + * @param array $tab_array This is an array of message type tab details used to generate the tabs |
|
| 3103 | + * |
|
| 3104 | + * @return string html formatted tabs |
|
| 3105 | + */ |
|
| 3106 | + protected function _get_mt_tabs($tab_array) |
|
| 3107 | + { |
|
| 3108 | + $tab_array = (array)$tab_array; |
|
| 3109 | + $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_mt_settings_tab_item.template.php'; |
|
| 3110 | + $tabs = ''; |
|
| 3111 | + |
|
| 3112 | + foreach ($tab_array as $tab) { |
|
| 3113 | + $tabs .= EEH_Template::display_template($template, $tab, true); |
|
| 3114 | + } |
|
| 3115 | + |
|
| 3116 | + return $tabs; |
|
| 3117 | + } |
|
| 3118 | + |
|
| 3119 | + |
|
| 3120 | + /** |
|
| 3121 | + * This prepares the content of the messenger meta box admin settings |
|
| 3122 | + * |
|
| 3123 | + * @param EE_messenger $messenger The messenger we're setting up content for |
|
| 3124 | + * |
|
| 3125 | + * @return string html formatted content |
|
| 3126 | + */ |
|
| 3127 | + protected function _get_messenger_box_content(EE_messenger $messenger) |
|
| 3128 | + { |
|
| 3129 | + |
|
| 3130 | + $fields = $messenger->get_admin_settings_fields(); |
|
| 3131 | + $settings_template_args['template_form_fields'] = ''; |
|
| 3132 | + |
|
| 3133 | + //is $messenger active? |
|
| 3134 | + $settings_template_args['active'] = $this->_message_resource_manager->is_messenger_active($messenger->name); |
|
| 3135 | + |
|
| 3136 | + |
|
| 3137 | + if ( ! empty($fields)) { |
|
| 3138 | 3138 | |
| 3139 | - $existing_settings = $messenger->get_existing_admin_settings(); |
|
| 3139 | + $existing_settings = $messenger->get_existing_admin_settings(); |
|
| 3140 | 3140 | |
| 3141 | - foreach ($fields as $fldname => $fldprops) { |
|
| 3142 | - $field_id = $messenger->name . '-' . $fldname; |
|
| 3143 | - $template_form_field[$field_id] = array( |
|
| 3144 | - 'name' => 'messenger_settings[' . $field_id . ']', |
|
| 3145 | - 'label' => $fldprops['label'], |
|
| 3146 | - 'input' => $fldprops['field_type'], |
|
| 3147 | - 'type' => $fldprops['value_type'], |
|
| 3148 | - 'required' => $fldprops['required'], |
|
| 3149 | - 'validation' => $fldprops['validation'], |
|
| 3150 | - 'value' => isset($existing_settings[$field_id]) |
|
| 3151 | - ? $existing_settings[$field_id] |
|
| 3152 | - : $fldprops['default'], |
|
| 3153 | - 'css_class' => '', |
|
| 3154 | - 'format' => $fldprops['format'] |
|
| 3155 | - ); |
|
| 3156 | - } |
|
| 3141 | + foreach ($fields as $fldname => $fldprops) { |
|
| 3142 | + $field_id = $messenger->name . '-' . $fldname; |
|
| 3143 | + $template_form_field[$field_id] = array( |
|
| 3144 | + 'name' => 'messenger_settings[' . $field_id . ']', |
|
| 3145 | + 'label' => $fldprops['label'], |
|
| 3146 | + 'input' => $fldprops['field_type'], |
|
| 3147 | + 'type' => $fldprops['value_type'], |
|
| 3148 | + 'required' => $fldprops['required'], |
|
| 3149 | + 'validation' => $fldprops['validation'], |
|
| 3150 | + 'value' => isset($existing_settings[$field_id]) |
|
| 3151 | + ? $existing_settings[$field_id] |
|
| 3152 | + : $fldprops['default'], |
|
| 3153 | + 'css_class' => '', |
|
| 3154 | + 'format' => $fldprops['format'] |
|
| 3155 | + ); |
|
| 3156 | + } |
|
| 3157 | 3157 | |
| 3158 | 3158 | |
| 3159 | - $settings_template_args['template_form_fields'] = ! empty($template_form_field) |
|
| 3160 | - ? $this->_generate_admin_form_fields($template_form_field, 'string', 'ee_m_activate_form') |
|
| 3161 | - : ''; |
|
| 3162 | - } |
|
| 3163 | - |
|
| 3164 | - //we also need some hidden fields |
|
| 3165 | - $settings_template_args['hidden_fields'] = array( |
|
| 3166 | - 'messenger_settings[messenger]' => array( |
|
| 3167 | - 'type' => 'hidden', |
|
| 3168 | - 'value' => $messenger->name |
|
| 3169 | - ), |
|
| 3170 | - 'type' => array( |
|
| 3171 | - 'type' => 'hidden', |
|
| 3172 | - 'value' => 'messenger' |
|
| 3173 | - ) |
|
| 3174 | - ); |
|
| 3175 | - |
|
| 3176 | - //make sure any active message types that are existing are included in the hidden fields |
|
| 3177 | - if (isset($this->_m_mt_settings['message_type_tabs'][$messenger->name]['active'])) { |
|
| 3178 | - foreach ($this->_m_mt_settings['message_type_tabs'][$messenger->name]['active'] as $mt => $values) { |
|
| 3179 | - $settings_template_args['hidden_fields']['messenger_settings[message_types][' . $mt . ']'] = array( |
|
| 3180 | - 'type' => 'hidden', |
|
| 3181 | - 'value' => $mt |
|
| 3182 | - ); |
|
| 3183 | - } |
|
| 3184 | - } |
|
| 3185 | - $settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields( |
|
| 3186 | - $settings_template_args['hidden_fields'], |
|
| 3187 | - 'array' |
|
| 3188 | - ); |
|
| 3189 | - $active = $this->_message_resource_manager->is_messenger_active($messenger->name); |
|
| 3190 | - |
|
| 3191 | - $settings_template_args['messenger'] = $messenger->name; |
|
| 3192 | - $settings_template_args['description'] = $messenger->description; |
|
| 3193 | - $settings_template_args['show_hide_edit_form'] = $active ? '' : ' hidden'; |
|
| 3194 | - |
|
| 3195 | - |
|
| 3196 | - $settings_template_args['show_hide_edit_form'] = $this->_message_resource_manager->is_messenger_active($messenger->name) |
|
| 3197 | - ? $settings_template_args['show_hide_edit_form'] |
|
| 3198 | - : ' hidden'; |
|
| 3199 | - |
|
| 3200 | - $settings_template_args['show_hide_edit_form'] = empty($settings_template_args['template_form_fields']) |
|
| 3201 | - ? ' hidden' |
|
| 3202 | - : $settings_template_args['show_hide_edit_form']; |
|
| 3203 | - |
|
| 3204 | - |
|
| 3205 | - $settings_template_args['on_off_action'] = $active ? 'messenger-off' : 'messenger-on'; |
|
| 3206 | - $settings_template_args['nonce'] = wp_create_nonce('activate_' . $messenger->name . '_toggle_nonce'); |
|
| 3207 | - $settings_template_args['on_off_status'] = $active ? true : false; |
|
| 3208 | - $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_m_settings_content.template.php'; |
|
| 3209 | - $content = EEH_Template::display_template($template, $settings_template_args, |
|
| 3210 | - true); |
|
| 3211 | - |
|
| 3212 | - return $content; |
|
| 3213 | - } |
|
| 3214 | - |
|
| 3215 | - |
|
| 3216 | - /** |
|
| 3217 | - * used by ajax on the messages settings page to activate|deactivate the messenger |
|
| 3218 | - */ |
|
| 3219 | - public function activate_messenger_toggle() |
|
| 3220 | - { |
|
| 3221 | - $success = true; |
|
| 3222 | - $this->_prep_default_response_for_messenger_or_message_type_toggle(); |
|
| 3223 | - //let's check that we have required data |
|
| 3224 | - if ( ! isset($this->_req_data['messenger'])) { |
|
| 3225 | - EE_Error::add_error( |
|
| 3226 | - __('Messenger name needed to toggle activation. None given', 'event_espresso'), |
|
| 3227 | - __FILE__, |
|
| 3228 | - __FUNCTION__, |
|
| 3229 | - __LINE__ |
|
| 3230 | - ); |
|
| 3231 | - $success = false; |
|
| 3232 | - } |
|
| 3233 | - |
|
| 3234 | - //do a nonce check here since we're not arriving via a normal route |
|
| 3235 | - $nonce = isset($this->_req_data['activate_nonce']) ? sanitize_text_field($this->_req_data['activate_nonce']) : ''; |
|
| 3236 | - $nonce_ref = 'activate_' . $this->_req_data['messenger'] . '_toggle_nonce'; |
|
| 3237 | - |
|
| 3238 | - $this->_verify_nonce($nonce, $nonce_ref); |
|
| 3239 | - |
|
| 3240 | - |
|
| 3241 | - if ( ! isset($this->_req_data['status'])) { |
|
| 3242 | - EE_Error::add_error( |
|
| 3243 | - __( |
|
| 3244 | - 'Messenger status needed to know whether activation or deactivation is happening. No status is given', |
|
| 3245 | - 'event_espresso' |
|
| 3246 | - ), |
|
| 3247 | - __FILE__, |
|
| 3248 | - __FUNCTION__, |
|
| 3249 | - __LINE__ |
|
| 3250 | - ); |
|
| 3251 | - $success = false; |
|
| 3252 | - } |
|
| 3253 | - |
|
| 3254 | - //do check to verify we have a valid status. |
|
| 3255 | - $status = $this->_req_data['status']; |
|
| 3256 | - |
|
| 3257 | - if ($status != 'off' && $status != 'on') { |
|
| 3258 | - EE_Error::add_error( |
|
| 3259 | - sprintf( |
|
| 3260 | - __('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'), |
|
| 3261 | - $this->_req_data['status'] |
|
| 3262 | - ), |
|
| 3263 | - __FILE__, |
|
| 3264 | - __FUNCTION__, |
|
| 3265 | - __LINE__ |
|
| 3266 | - ); |
|
| 3267 | - $success = false; |
|
| 3268 | - } |
|
| 3269 | - |
|
| 3270 | - if ($success) { |
|
| 3271 | - //made it here? Stop dawdling then!! |
|
| 3272 | - $success = $status == 'off' |
|
| 3273 | - ? $this->_deactivate_messenger($this->_req_data['messenger']) |
|
| 3274 | - : $this->_activate_messenger($this->_req_data['messenger']); |
|
| 3275 | - } |
|
| 3276 | - |
|
| 3277 | - $this->_template_args['success'] = $success; |
|
| 3278 | - |
|
| 3279 | - //no special instructions so let's just do the json return (which should automatically do all the special stuff). |
|
| 3280 | - $this->_return_json(); |
|
| 3281 | - |
|
| 3282 | - } |
|
| 3283 | - |
|
| 3284 | - |
|
| 3285 | - /** |
|
| 3286 | - * used by ajax from the messages settings page to activate|deactivate a message type |
|
| 3287 | - * |
|
| 3288 | - */ |
|
| 3289 | - public function activate_mt_toggle() |
|
| 3290 | - { |
|
| 3291 | - $success = true; |
|
| 3292 | - $this->_prep_default_response_for_messenger_or_message_type_toggle(); |
|
| 3293 | - |
|
| 3294 | - //let's make sure we have the necessary data |
|
| 3295 | - if ( ! isset($this->_req_data['message_type'])) { |
|
| 3296 | - EE_Error::add_error( |
|
| 3297 | - __('Message Type name needed to toggle activation. None given', 'event_espresso'), |
|
| 3298 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 3299 | - ); |
|
| 3300 | - $success = false; |
|
| 3301 | - } |
|
| 3302 | - |
|
| 3303 | - if ( ! isset($this->_req_data['messenger'])) { |
|
| 3304 | - EE_Error::add_error( |
|
| 3305 | - __('Messenger name needed to toggle activation. None given', 'event_espresso'), |
|
| 3306 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 3307 | - ); |
|
| 3308 | - $success = false; |
|
| 3309 | - } |
|
| 3310 | - |
|
| 3311 | - if ( ! isset($this->_req_data['status'])) { |
|
| 3312 | - EE_Error::add_error( |
|
| 3313 | - __('Messenger status needed to know whether activation or deactivation is happening. No status is given', |
|
| 3314 | - 'event_espresso'), |
|
| 3315 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 3316 | - ); |
|
| 3317 | - $success = false; |
|
| 3318 | - } |
|
| 3319 | - |
|
| 3320 | - |
|
| 3321 | - //do check to verify we have a valid status. |
|
| 3322 | - $status = $this->_req_data['status']; |
|
| 3323 | - |
|
| 3324 | - if ($status != 'activate' && $status != 'deactivate') { |
|
| 3325 | - EE_Error::add_error( |
|
| 3326 | - sprintf( |
|
| 3327 | - __('The given status (%s) is not valid. Must be "active" or "inactive"', 'event_espresso'), |
|
| 3328 | - $this->_req_data['status'] |
|
| 3329 | - ), |
|
| 3330 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 3331 | - ); |
|
| 3332 | - $success = false; |
|
| 3333 | - } |
|
| 3334 | - |
|
| 3335 | - |
|
| 3336 | - //do a nonce check here since we're not arriving via a normal route |
|
| 3337 | - $nonce = isset($this->_req_data['mt_nonce']) ? sanitize_text_field($this->_req_data['mt_nonce']) : ''; |
|
| 3338 | - $nonce_ref = $this->_req_data['message_type'] . '_nonce'; |
|
| 3339 | - |
|
| 3340 | - $this->_verify_nonce($nonce, $nonce_ref); |
|
| 3341 | - |
|
| 3342 | - if ($success) { |
|
| 3343 | - //made it here? um, what are you waiting for then? |
|
| 3344 | - $success = $status == 'deactivate' |
|
| 3345 | - ? $this->_deactivate_message_type_for_messenger($this->_req_data['messenger'], |
|
| 3346 | - $this->_req_data['message_type']) |
|
| 3347 | - : $this->_activate_message_type_for_messenger($this->_req_data['messenger'], |
|
| 3348 | - $this->_req_data['message_type']); |
|
| 3349 | - } |
|
| 3350 | - |
|
| 3351 | - $this->_template_args['success'] = $success; |
|
| 3352 | - $this->_return_json(); |
|
| 3353 | - } |
|
| 3354 | - |
|
| 3355 | - |
|
| 3356 | - /** |
|
| 3357 | - * Takes care of processing activating a messenger and preparing the appropriate response. |
|
| 3358 | - * |
|
| 3359 | - * @param string $messenger_name The name of the messenger being activated |
|
| 3360 | - * |
|
| 3361 | - * @return bool |
|
| 3362 | - */ |
|
| 3363 | - protected function _activate_messenger($messenger_name) |
|
| 3364 | - { |
|
| 3365 | - /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */ |
|
| 3366 | - $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name); |
|
| 3367 | - $message_types_to_activate = $active_messenger instanceof EE_Messenger ? $active_messenger->get_default_message_types() : array(); |
|
| 3368 | - |
|
| 3369 | - //ensure is active |
|
| 3370 | - $this->_message_resource_manager->activate_messenger($messenger_name, $message_types_to_activate); |
|
| 3371 | - |
|
| 3372 | - //set response_data for reload |
|
| 3373 | - foreach ($message_types_to_activate as $message_type_name) { |
|
| 3374 | - /** @var EE_message_type $message_type */ |
|
| 3375 | - $message_type = $this->_message_resource_manager->get_message_type($message_type_name); |
|
| 3376 | - if ($this->_message_resource_manager->is_message_type_active_for_messenger($messenger_name, |
|
| 3377 | - $message_type_name) |
|
| 3378 | - && $message_type instanceof EE_message_type |
|
| 3379 | - ) { |
|
| 3380 | - $this->_template_args['data']['active_mts'][] = $message_type_name; |
|
| 3381 | - if ($message_type->get_admin_settings_fields()) { |
|
| 3382 | - $this->_template_args['data']['mt_reload'][] = $message_type_name; |
|
| 3383 | - } |
|
| 3384 | - } |
|
| 3385 | - } |
|
| 3386 | - |
|
| 3387 | - //add success message for activating messenger |
|
| 3388 | - return $this->_setup_response_message_for_activating_messenger_with_message_types($active_messenger); |
|
| 3389 | - |
|
| 3390 | - } |
|
| 3391 | - |
|
| 3392 | - |
|
| 3393 | - /** |
|
| 3394 | - * Takes care of processing deactivating a messenger and preparing the appropriate response. |
|
| 3395 | - * |
|
| 3396 | - * @param string $messenger_name The name of the messenger being activated |
|
| 3397 | - * |
|
| 3398 | - * @return bool |
|
| 3399 | - */ |
|
| 3400 | - protected function _deactivate_messenger($messenger_name) |
|
| 3401 | - { |
|
| 3402 | - /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */ |
|
| 3403 | - $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name); |
|
| 3404 | - $this->_message_resource_manager->deactivate_messenger($messenger_name); |
|
| 3405 | - |
|
| 3406 | - return $this->_setup_response_message_for_deactivating_messenger_with_message_types($active_messenger); |
|
| 3407 | - } |
|
| 3408 | - |
|
| 3409 | - |
|
| 3410 | - /** |
|
| 3411 | - * Takes care of processing activating a message type for a messenger and preparing the appropriate response. |
|
| 3412 | - * |
|
| 3413 | - * @param string $messenger_name The name of the messenger the message type is being activated for. |
|
| 3414 | - * @param string $message_type_name The name of the message type being activated for the messenger |
|
| 3415 | - * |
|
| 3416 | - * @return bool |
|
| 3417 | - */ |
|
| 3418 | - protected function _activate_message_type_for_messenger($messenger_name, $message_type_name) |
|
| 3419 | - { |
|
| 3420 | - /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */ |
|
| 3421 | - $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name); |
|
| 3422 | - /** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */ |
|
| 3423 | - $message_type_to_activate = $this->_message_resource_manager->get_message_type($message_type_name); |
|
| 3424 | - |
|
| 3425 | - //ensure is active |
|
| 3426 | - $this->_message_resource_manager->activate_messenger($messenger_name, $message_type_name); |
|
| 3427 | - |
|
| 3428 | - //set response for load |
|
| 3429 | - if ($this->_message_resource_manager->is_message_type_active_for_messenger($messenger_name, |
|
| 3430 | - $message_type_name) |
|
| 3431 | - ) { |
|
| 3432 | - $this->_template_args['data']['active_mts'][] = $message_type_name; |
|
| 3433 | - if ($message_type_to_activate->get_admin_settings_fields()) { |
|
| 3434 | - $this->_template_args['data']['mt_reload'][] = $message_type_name; |
|
| 3435 | - } |
|
| 3436 | - } |
|
| 3437 | - |
|
| 3438 | - return $this->_setup_response_message_for_activating_messenger_with_message_types($active_messenger, |
|
| 3439 | - $message_type_to_activate); |
|
| 3440 | - } |
|
| 3441 | - |
|
| 3442 | - |
|
| 3443 | - /** |
|
| 3444 | - * Takes care of processing deactivating a message type for a messenger and preparing the appropriate response. |
|
| 3445 | - * |
|
| 3446 | - * @param string $messenger_name The name of the messenger the message type is being deactivated for. |
|
| 3447 | - * @param string $message_type_name The name of the message type being deactivated for the messenger |
|
| 3448 | - * |
|
| 3449 | - * @return bool |
|
| 3450 | - */ |
|
| 3451 | - protected function _deactivate_message_type_for_messenger($messenger_name, $message_type_name) |
|
| 3452 | - { |
|
| 3453 | - /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */ |
|
| 3454 | - $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name); |
|
| 3455 | - /** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */ |
|
| 3456 | - $message_type_to_deactivate = $this->_message_resource_manager->get_message_type($message_type_name); |
|
| 3457 | - $this->_message_resource_manager->deactivate_message_type_for_messenger($message_type_name, $messenger_name); |
|
| 3458 | - |
|
| 3459 | - return $this->_setup_response_message_for_deactivating_messenger_with_message_types($active_messenger, |
|
| 3460 | - $message_type_to_deactivate); |
|
| 3461 | - } |
|
| 3462 | - |
|
| 3463 | - |
|
| 3464 | - /** |
|
| 3465 | - * This just initializes the defaults for activating messenger and message type responses. |
|
| 3466 | - */ |
|
| 3467 | - protected function _prep_default_response_for_messenger_or_message_type_toggle() |
|
| 3468 | - { |
|
| 3469 | - $this->_template_args['data']['active_mts'] = array(); |
|
| 3470 | - $this->_template_args['data']['mt_reload'] = array(); |
|
| 3471 | - } |
|
| 3472 | - |
|
| 3473 | - |
|
| 3474 | - /** |
|
| 3475 | - * Setup appropriate response for activating a messenger and/or message types |
|
| 3476 | - * |
|
| 3477 | - * @param EE_messenger $messenger |
|
| 3478 | - * @param EE_message_type|null $message_type |
|
| 3479 | - * |
|
| 3480 | - * @return bool |
|
| 3481 | - * @throws EE_Error |
|
| 3482 | - */ |
|
| 3483 | - protected function _setup_response_message_for_activating_messenger_with_message_types( |
|
| 3484 | - $messenger, |
|
| 3485 | - EE_Message_Type $message_type = null |
|
| 3486 | - ) { |
|
| 3487 | - //if $messenger isn't a valid messenger object then get out. |
|
| 3488 | - if ( ! $messenger instanceof EE_Messenger) { |
|
| 3489 | - EE_Error::add_error( |
|
| 3490 | - __('The messenger being activated is not a valid messenger', 'event_espresso'), |
|
| 3491 | - __FILE__, |
|
| 3492 | - __FUNCTION__, |
|
| 3493 | - __LINE__ |
|
| 3494 | - ); |
|
| 3159 | + $settings_template_args['template_form_fields'] = ! empty($template_form_field) |
|
| 3160 | + ? $this->_generate_admin_form_fields($template_form_field, 'string', 'ee_m_activate_form') |
|
| 3161 | + : ''; |
|
| 3162 | + } |
|
| 3163 | + |
|
| 3164 | + //we also need some hidden fields |
|
| 3165 | + $settings_template_args['hidden_fields'] = array( |
|
| 3166 | + 'messenger_settings[messenger]' => array( |
|
| 3167 | + 'type' => 'hidden', |
|
| 3168 | + 'value' => $messenger->name |
|
| 3169 | + ), |
|
| 3170 | + 'type' => array( |
|
| 3171 | + 'type' => 'hidden', |
|
| 3172 | + 'value' => 'messenger' |
|
| 3173 | + ) |
|
| 3174 | + ); |
|
| 3175 | + |
|
| 3176 | + //make sure any active message types that are existing are included in the hidden fields |
|
| 3177 | + if (isset($this->_m_mt_settings['message_type_tabs'][$messenger->name]['active'])) { |
|
| 3178 | + foreach ($this->_m_mt_settings['message_type_tabs'][$messenger->name]['active'] as $mt => $values) { |
|
| 3179 | + $settings_template_args['hidden_fields']['messenger_settings[message_types][' . $mt . ']'] = array( |
|
| 3180 | + 'type' => 'hidden', |
|
| 3181 | + 'value' => $mt |
|
| 3182 | + ); |
|
| 3183 | + } |
|
| 3184 | + } |
|
| 3185 | + $settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields( |
|
| 3186 | + $settings_template_args['hidden_fields'], |
|
| 3187 | + 'array' |
|
| 3188 | + ); |
|
| 3189 | + $active = $this->_message_resource_manager->is_messenger_active($messenger->name); |
|
| 3190 | + |
|
| 3191 | + $settings_template_args['messenger'] = $messenger->name; |
|
| 3192 | + $settings_template_args['description'] = $messenger->description; |
|
| 3193 | + $settings_template_args['show_hide_edit_form'] = $active ? '' : ' hidden'; |
|
| 3194 | + |
|
| 3195 | + |
|
| 3196 | + $settings_template_args['show_hide_edit_form'] = $this->_message_resource_manager->is_messenger_active($messenger->name) |
|
| 3197 | + ? $settings_template_args['show_hide_edit_form'] |
|
| 3198 | + : ' hidden'; |
|
| 3199 | + |
|
| 3200 | + $settings_template_args['show_hide_edit_form'] = empty($settings_template_args['template_form_fields']) |
|
| 3201 | + ? ' hidden' |
|
| 3202 | + : $settings_template_args['show_hide_edit_form']; |
|
| 3203 | + |
|
| 3204 | + |
|
| 3205 | + $settings_template_args['on_off_action'] = $active ? 'messenger-off' : 'messenger-on'; |
|
| 3206 | + $settings_template_args['nonce'] = wp_create_nonce('activate_' . $messenger->name . '_toggle_nonce'); |
|
| 3207 | + $settings_template_args['on_off_status'] = $active ? true : false; |
|
| 3208 | + $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_m_settings_content.template.php'; |
|
| 3209 | + $content = EEH_Template::display_template($template, $settings_template_args, |
|
| 3210 | + true); |
|
| 3211 | + |
|
| 3212 | + return $content; |
|
| 3213 | + } |
|
| 3214 | + |
|
| 3215 | + |
|
| 3216 | + /** |
|
| 3217 | + * used by ajax on the messages settings page to activate|deactivate the messenger |
|
| 3218 | + */ |
|
| 3219 | + public function activate_messenger_toggle() |
|
| 3220 | + { |
|
| 3221 | + $success = true; |
|
| 3222 | + $this->_prep_default_response_for_messenger_or_message_type_toggle(); |
|
| 3223 | + //let's check that we have required data |
|
| 3224 | + if ( ! isset($this->_req_data['messenger'])) { |
|
| 3225 | + EE_Error::add_error( |
|
| 3226 | + __('Messenger name needed to toggle activation. None given', 'event_espresso'), |
|
| 3227 | + __FILE__, |
|
| 3228 | + __FUNCTION__, |
|
| 3229 | + __LINE__ |
|
| 3230 | + ); |
|
| 3231 | + $success = false; |
|
| 3232 | + } |
|
| 3233 | + |
|
| 3234 | + //do a nonce check here since we're not arriving via a normal route |
|
| 3235 | + $nonce = isset($this->_req_data['activate_nonce']) ? sanitize_text_field($this->_req_data['activate_nonce']) : ''; |
|
| 3236 | + $nonce_ref = 'activate_' . $this->_req_data['messenger'] . '_toggle_nonce'; |
|
| 3237 | + |
|
| 3238 | + $this->_verify_nonce($nonce, $nonce_ref); |
|
| 3239 | + |
|
| 3240 | + |
|
| 3241 | + if ( ! isset($this->_req_data['status'])) { |
|
| 3242 | + EE_Error::add_error( |
|
| 3243 | + __( |
|
| 3244 | + 'Messenger status needed to know whether activation or deactivation is happening. No status is given', |
|
| 3245 | + 'event_espresso' |
|
| 3246 | + ), |
|
| 3247 | + __FILE__, |
|
| 3248 | + __FUNCTION__, |
|
| 3249 | + __LINE__ |
|
| 3250 | + ); |
|
| 3251 | + $success = false; |
|
| 3252 | + } |
|
| 3253 | + |
|
| 3254 | + //do check to verify we have a valid status. |
|
| 3255 | + $status = $this->_req_data['status']; |
|
| 3256 | + |
|
| 3257 | + if ($status != 'off' && $status != 'on') { |
|
| 3258 | + EE_Error::add_error( |
|
| 3259 | + sprintf( |
|
| 3260 | + __('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'), |
|
| 3261 | + $this->_req_data['status'] |
|
| 3262 | + ), |
|
| 3263 | + __FILE__, |
|
| 3264 | + __FUNCTION__, |
|
| 3265 | + __LINE__ |
|
| 3266 | + ); |
|
| 3267 | + $success = false; |
|
| 3268 | + } |
|
| 3269 | + |
|
| 3270 | + if ($success) { |
|
| 3271 | + //made it here? Stop dawdling then!! |
|
| 3272 | + $success = $status == 'off' |
|
| 3273 | + ? $this->_deactivate_messenger($this->_req_data['messenger']) |
|
| 3274 | + : $this->_activate_messenger($this->_req_data['messenger']); |
|
| 3275 | + } |
|
| 3276 | + |
|
| 3277 | + $this->_template_args['success'] = $success; |
|
| 3278 | + |
|
| 3279 | + //no special instructions so let's just do the json return (which should automatically do all the special stuff). |
|
| 3280 | + $this->_return_json(); |
|
| 3281 | + |
|
| 3282 | + } |
|
| 3283 | + |
|
| 3284 | + |
|
| 3285 | + /** |
|
| 3286 | + * used by ajax from the messages settings page to activate|deactivate a message type |
|
| 3287 | + * |
|
| 3288 | + */ |
|
| 3289 | + public function activate_mt_toggle() |
|
| 3290 | + { |
|
| 3291 | + $success = true; |
|
| 3292 | + $this->_prep_default_response_for_messenger_or_message_type_toggle(); |
|
| 3293 | + |
|
| 3294 | + //let's make sure we have the necessary data |
|
| 3295 | + if ( ! isset($this->_req_data['message_type'])) { |
|
| 3296 | + EE_Error::add_error( |
|
| 3297 | + __('Message Type name needed to toggle activation. None given', 'event_espresso'), |
|
| 3298 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 3299 | + ); |
|
| 3300 | + $success = false; |
|
| 3301 | + } |
|
| 3302 | + |
|
| 3303 | + if ( ! isset($this->_req_data['messenger'])) { |
|
| 3304 | + EE_Error::add_error( |
|
| 3305 | + __('Messenger name needed to toggle activation. None given', 'event_espresso'), |
|
| 3306 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 3307 | + ); |
|
| 3308 | + $success = false; |
|
| 3309 | + } |
|
| 3310 | + |
|
| 3311 | + if ( ! isset($this->_req_data['status'])) { |
|
| 3312 | + EE_Error::add_error( |
|
| 3313 | + __('Messenger status needed to know whether activation or deactivation is happening. No status is given', |
|
| 3314 | + 'event_espresso'), |
|
| 3315 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 3316 | + ); |
|
| 3317 | + $success = false; |
|
| 3318 | + } |
|
| 3319 | + |
|
| 3320 | + |
|
| 3321 | + //do check to verify we have a valid status. |
|
| 3322 | + $status = $this->_req_data['status']; |
|
| 3323 | + |
|
| 3324 | + if ($status != 'activate' && $status != 'deactivate') { |
|
| 3325 | + EE_Error::add_error( |
|
| 3326 | + sprintf( |
|
| 3327 | + __('The given status (%s) is not valid. Must be "active" or "inactive"', 'event_espresso'), |
|
| 3328 | + $this->_req_data['status'] |
|
| 3329 | + ), |
|
| 3330 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 3331 | + ); |
|
| 3332 | + $success = false; |
|
| 3333 | + } |
|
| 3334 | + |
|
| 3335 | + |
|
| 3336 | + //do a nonce check here since we're not arriving via a normal route |
|
| 3337 | + $nonce = isset($this->_req_data['mt_nonce']) ? sanitize_text_field($this->_req_data['mt_nonce']) : ''; |
|
| 3338 | + $nonce_ref = $this->_req_data['message_type'] . '_nonce'; |
|
| 3339 | + |
|
| 3340 | + $this->_verify_nonce($nonce, $nonce_ref); |
|
| 3341 | + |
|
| 3342 | + if ($success) { |
|
| 3343 | + //made it here? um, what are you waiting for then? |
|
| 3344 | + $success = $status == 'deactivate' |
|
| 3345 | + ? $this->_deactivate_message_type_for_messenger($this->_req_data['messenger'], |
|
| 3346 | + $this->_req_data['message_type']) |
|
| 3347 | + : $this->_activate_message_type_for_messenger($this->_req_data['messenger'], |
|
| 3348 | + $this->_req_data['message_type']); |
|
| 3349 | + } |
|
| 3350 | + |
|
| 3351 | + $this->_template_args['success'] = $success; |
|
| 3352 | + $this->_return_json(); |
|
| 3353 | + } |
|
| 3354 | + |
|
| 3355 | + |
|
| 3356 | + /** |
|
| 3357 | + * Takes care of processing activating a messenger and preparing the appropriate response. |
|
| 3358 | + * |
|
| 3359 | + * @param string $messenger_name The name of the messenger being activated |
|
| 3360 | + * |
|
| 3361 | + * @return bool |
|
| 3362 | + */ |
|
| 3363 | + protected function _activate_messenger($messenger_name) |
|
| 3364 | + { |
|
| 3365 | + /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */ |
|
| 3366 | + $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name); |
|
| 3367 | + $message_types_to_activate = $active_messenger instanceof EE_Messenger ? $active_messenger->get_default_message_types() : array(); |
|
| 3368 | + |
|
| 3369 | + //ensure is active |
|
| 3370 | + $this->_message_resource_manager->activate_messenger($messenger_name, $message_types_to_activate); |
|
| 3371 | + |
|
| 3372 | + //set response_data for reload |
|
| 3373 | + foreach ($message_types_to_activate as $message_type_name) { |
|
| 3374 | + /** @var EE_message_type $message_type */ |
|
| 3375 | + $message_type = $this->_message_resource_manager->get_message_type($message_type_name); |
|
| 3376 | + if ($this->_message_resource_manager->is_message_type_active_for_messenger($messenger_name, |
|
| 3377 | + $message_type_name) |
|
| 3378 | + && $message_type instanceof EE_message_type |
|
| 3379 | + ) { |
|
| 3380 | + $this->_template_args['data']['active_mts'][] = $message_type_name; |
|
| 3381 | + if ($message_type->get_admin_settings_fields()) { |
|
| 3382 | + $this->_template_args['data']['mt_reload'][] = $message_type_name; |
|
| 3383 | + } |
|
| 3384 | + } |
|
| 3385 | + } |
|
| 3386 | + |
|
| 3387 | + //add success message for activating messenger |
|
| 3388 | + return $this->_setup_response_message_for_activating_messenger_with_message_types($active_messenger); |
|
| 3389 | + |
|
| 3390 | + } |
|
| 3391 | + |
|
| 3392 | + |
|
| 3393 | + /** |
|
| 3394 | + * Takes care of processing deactivating a messenger and preparing the appropriate response. |
|
| 3395 | + * |
|
| 3396 | + * @param string $messenger_name The name of the messenger being activated |
|
| 3397 | + * |
|
| 3398 | + * @return bool |
|
| 3399 | + */ |
|
| 3400 | + protected function _deactivate_messenger($messenger_name) |
|
| 3401 | + { |
|
| 3402 | + /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */ |
|
| 3403 | + $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name); |
|
| 3404 | + $this->_message_resource_manager->deactivate_messenger($messenger_name); |
|
| 3405 | + |
|
| 3406 | + return $this->_setup_response_message_for_deactivating_messenger_with_message_types($active_messenger); |
|
| 3407 | + } |
|
| 3408 | + |
|
| 3409 | + |
|
| 3410 | + /** |
|
| 3411 | + * Takes care of processing activating a message type for a messenger and preparing the appropriate response. |
|
| 3412 | + * |
|
| 3413 | + * @param string $messenger_name The name of the messenger the message type is being activated for. |
|
| 3414 | + * @param string $message_type_name The name of the message type being activated for the messenger |
|
| 3415 | + * |
|
| 3416 | + * @return bool |
|
| 3417 | + */ |
|
| 3418 | + protected function _activate_message_type_for_messenger($messenger_name, $message_type_name) |
|
| 3419 | + { |
|
| 3420 | + /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */ |
|
| 3421 | + $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name); |
|
| 3422 | + /** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */ |
|
| 3423 | + $message_type_to_activate = $this->_message_resource_manager->get_message_type($message_type_name); |
|
| 3424 | + |
|
| 3425 | + //ensure is active |
|
| 3426 | + $this->_message_resource_manager->activate_messenger($messenger_name, $message_type_name); |
|
| 3427 | + |
|
| 3428 | + //set response for load |
|
| 3429 | + if ($this->_message_resource_manager->is_message_type_active_for_messenger($messenger_name, |
|
| 3430 | + $message_type_name) |
|
| 3431 | + ) { |
|
| 3432 | + $this->_template_args['data']['active_mts'][] = $message_type_name; |
|
| 3433 | + if ($message_type_to_activate->get_admin_settings_fields()) { |
|
| 3434 | + $this->_template_args['data']['mt_reload'][] = $message_type_name; |
|
| 3435 | + } |
|
| 3436 | + } |
|
| 3437 | + |
|
| 3438 | + return $this->_setup_response_message_for_activating_messenger_with_message_types($active_messenger, |
|
| 3439 | + $message_type_to_activate); |
|
| 3440 | + } |
|
| 3441 | + |
|
| 3442 | + |
|
| 3443 | + /** |
|
| 3444 | + * Takes care of processing deactivating a message type for a messenger and preparing the appropriate response. |
|
| 3445 | + * |
|
| 3446 | + * @param string $messenger_name The name of the messenger the message type is being deactivated for. |
|
| 3447 | + * @param string $message_type_name The name of the message type being deactivated for the messenger |
|
| 3448 | + * |
|
| 3449 | + * @return bool |
|
| 3450 | + */ |
|
| 3451 | + protected function _deactivate_message_type_for_messenger($messenger_name, $message_type_name) |
|
| 3452 | + { |
|
| 3453 | + /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */ |
|
| 3454 | + $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name); |
|
| 3455 | + /** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */ |
|
| 3456 | + $message_type_to_deactivate = $this->_message_resource_manager->get_message_type($message_type_name); |
|
| 3457 | + $this->_message_resource_manager->deactivate_message_type_for_messenger($message_type_name, $messenger_name); |
|
| 3458 | + |
|
| 3459 | + return $this->_setup_response_message_for_deactivating_messenger_with_message_types($active_messenger, |
|
| 3460 | + $message_type_to_deactivate); |
|
| 3461 | + } |
|
| 3462 | + |
|
| 3463 | + |
|
| 3464 | + /** |
|
| 3465 | + * This just initializes the defaults for activating messenger and message type responses. |
|
| 3466 | + */ |
|
| 3467 | + protected function _prep_default_response_for_messenger_or_message_type_toggle() |
|
| 3468 | + { |
|
| 3469 | + $this->_template_args['data']['active_mts'] = array(); |
|
| 3470 | + $this->_template_args['data']['mt_reload'] = array(); |
|
| 3471 | + } |
|
| 3472 | + |
|
| 3473 | + |
|
| 3474 | + /** |
|
| 3475 | + * Setup appropriate response for activating a messenger and/or message types |
|
| 3476 | + * |
|
| 3477 | + * @param EE_messenger $messenger |
|
| 3478 | + * @param EE_message_type|null $message_type |
|
| 3479 | + * |
|
| 3480 | + * @return bool |
|
| 3481 | + * @throws EE_Error |
|
| 3482 | + */ |
|
| 3483 | + protected function _setup_response_message_for_activating_messenger_with_message_types( |
|
| 3484 | + $messenger, |
|
| 3485 | + EE_Message_Type $message_type = null |
|
| 3486 | + ) { |
|
| 3487 | + //if $messenger isn't a valid messenger object then get out. |
|
| 3488 | + if ( ! $messenger instanceof EE_Messenger) { |
|
| 3489 | + EE_Error::add_error( |
|
| 3490 | + __('The messenger being activated is not a valid messenger', 'event_espresso'), |
|
| 3491 | + __FILE__, |
|
| 3492 | + __FUNCTION__, |
|
| 3493 | + __LINE__ |
|
| 3494 | + ); |
|
| 3495 | 3495 | |
| 3496 | - return false; |
|
| 3497 | - } |
|
| 3498 | - //activated |
|
| 3499 | - if ($this->_template_args['data']['active_mts']) { |
|
| 3500 | - EE_Error::overwrite_success(); |
|
| 3501 | - //activated a message type with the messenger |
|
| 3502 | - if ($message_type instanceof EE_message_type) { |
|
| 3503 | - EE_Error::add_success( |
|
| 3504 | - sprintf( |
|
| 3505 | - __('%s message type has been successfully activated with the %s messenger', 'event_espresso'), |
|
| 3506 | - ucwords($message_type->label['singular']), |
|
| 3507 | - ucwords($messenger->label['singular']) |
|
| 3508 | - ) |
|
| 3509 | - ); |
|
| 3496 | + return false; |
|
| 3497 | + } |
|
| 3498 | + //activated |
|
| 3499 | + if ($this->_template_args['data']['active_mts']) { |
|
| 3500 | + EE_Error::overwrite_success(); |
|
| 3501 | + //activated a message type with the messenger |
|
| 3502 | + if ($message_type instanceof EE_message_type) { |
|
| 3503 | + EE_Error::add_success( |
|
| 3504 | + sprintf( |
|
| 3505 | + __('%s message type has been successfully activated with the %s messenger', 'event_espresso'), |
|
| 3506 | + ucwords($message_type->label['singular']), |
|
| 3507 | + ucwords($messenger->label['singular']) |
|
| 3508 | + ) |
|
| 3509 | + ); |
|
| 3510 | 3510 | |
| 3511 | - //if message type was invoice then let's make sure we activate the invoice payment method. |
|
| 3512 | - if ($message_type->name == 'invoice') { |
|
| 3513 | - EE_Registry::instance()->load_lib('Payment_Method_Manager'); |
|
| 3514 | - $pm = EE_Payment_Method_Manager::instance()->activate_a_payment_method_of_type('Invoice'); |
|
| 3515 | - if ($pm instanceof EE_Payment_Method) { |
|
| 3516 | - EE_Error::add_attention(__('Activating the invoice message type also automatically activates the invoice payment method. If you do not wish the invoice payment method to be active, or to change its settings, visit the payment method admin page.', |
|
| 3517 | - 'event_espresso')); |
|
| 3518 | - } |
|
| 3519 | - } |
|
| 3520 | - //just toggles the entire messenger |
|
| 3521 | - } else { |
|
| 3522 | - EE_Error::add_success( |
|
| 3523 | - sprintf( |
|
| 3524 | - __('%s messenger has been successfully activated', 'event_espresso'), |
|
| 3525 | - ucwords($messenger->label['singular']) |
|
| 3526 | - ) |
|
| 3527 | - ); |
|
| 3528 | - } |
|
| 3511 | + //if message type was invoice then let's make sure we activate the invoice payment method. |
|
| 3512 | + if ($message_type->name == 'invoice') { |
|
| 3513 | + EE_Registry::instance()->load_lib('Payment_Method_Manager'); |
|
| 3514 | + $pm = EE_Payment_Method_Manager::instance()->activate_a_payment_method_of_type('Invoice'); |
|
| 3515 | + if ($pm instanceof EE_Payment_Method) { |
|
| 3516 | + EE_Error::add_attention(__('Activating the invoice message type also automatically activates the invoice payment method. If you do not wish the invoice payment method to be active, or to change its settings, visit the payment method admin page.', |
|
| 3517 | + 'event_espresso')); |
|
| 3518 | + } |
|
| 3519 | + } |
|
| 3520 | + //just toggles the entire messenger |
|
| 3521 | + } else { |
|
| 3522 | + EE_Error::add_success( |
|
| 3523 | + sprintf( |
|
| 3524 | + __('%s messenger has been successfully activated', 'event_espresso'), |
|
| 3525 | + ucwords($messenger->label['singular']) |
|
| 3526 | + ) |
|
| 3527 | + ); |
|
| 3528 | + } |
|
| 3529 | 3529 | |
| 3530 | - return true; |
|
| 3530 | + return true; |
|
| 3531 | 3531 | |
| 3532 | - //possible error condition. This will happen when our active_mts data is empty because it is validated for actual active |
|
| 3533 | - //message types after the activation process. However its possible some messengers don't HAVE any default_message_types |
|
| 3534 | - //in which case we just give a success message for the messenger being successfully activated. |
|
| 3535 | - } else { |
|
| 3536 | - if ( ! $messenger->get_default_message_types()) { |
|
| 3537 | - //messenger doesn't have any default message types so still a success. |
|
| 3538 | - EE_Error::add_success( |
|
| 3539 | - sprintf( |
|
| 3540 | - __('%s messenger was successfully activated.', 'event_espresso'), |
|
| 3541 | - ucwords($messenger->label['singular']) |
|
| 3542 | - ) |
|
| 3543 | - ); |
|
| 3532 | + //possible error condition. This will happen when our active_mts data is empty because it is validated for actual active |
|
| 3533 | + //message types after the activation process. However its possible some messengers don't HAVE any default_message_types |
|
| 3534 | + //in which case we just give a success message for the messenger being successfully activated. |
|
| 3535 | + } else { |
|
| 3536 | + if ( ! $messenger->get_default_message_types()) { |
|
| 3537 | + //messenger doesn't have any default message types so still a success. |
|
| 3538 | + EE_Error::add_success( |
|
| 3539 | + sprintf( |
|
| 3540 | + __('%s messenger was successfully activated.', 'event_espresso'), |
|
| 3541 | + ucwords($messenger->label['singular']) |
|
| 3542 | + ) |
|
| 3543 | + ); |
|
| 3544 | 3544 | |
| 3545 | - return true; |
|
| 3546 | - } else { |
|
| 3547 | - EE_Error::add_error( |
|
| 3548 | - $message_type instanceof EE_message_type |
|
| 3549 | - ? sprintf( |
|
| 3550 | - __('%s message type was not successfully activated with the %s messenger', 'event_espresso'), |
|
| 3551 | - ucwords($message_type->label['singular']), |
|
| 3552 | - ucwords($messenger->label['singular']) |
|
| 3553 | - ) |
|
| 3554 | - : sprintf( |
|
| 3555 | - __('%s messenger was not successfully activated', 'event_espresso'), |
|
| 3556 | - ucwords($messenger->label['singular']) |
|
| 3557 | - ), |
|
| 3558 | - __FILE__, |
|
| 3559 | - __FUNCTION__, |
|
| 3560 | - __LINE__ |
|
| 3561 | - ); |
|
| 3545 | + return true; |
|
| 3546 | + } else { |
|
| 3547 | + EE_Error::add_error( |
|
| 3548 | + $message_type instanceof EE_message_type |
|
| 3549 | + ? sprintf( |
|
| 3550 | + __('%s message type was not successfully activated with the %s messenger', 'event_espresso'), |
|
| 3551 | + ucwords($message_type->label['singular']), |
|
| 3552 | + ucwords($messenger->label['singular']) |
|
| 3553 | + ) |
|
| 3554 | + : sprintf( |
|
| 3555 | + __('%s messenger was not successfully activated', 'event_espresso'), |
|
| 3556 | + ucwords($messenger->label['singular']) |
|
| 3557 | + ), |
|
| 3558 | + __FILE__, |
|
| 3559 | + __FUNCTION__, |
|
| 3560 | + __LINE__ |
|
| 3561 | + ); |
|
| 3562 | 3562 | |
| 3563 | - return false; |
|
| 3564 | - } |
|
| 3565 | - } |
|
| 3566 | - } |
|
| 3567 | - |
|
| 3568 | - |
|
| 3569 | - /** |
|
| 3570 | - * This sets up the appropriate response for deactivating a messenger and/or message type. |
|
| 3571 | - * |
|
| 3572 | - * @param EE_messenger $messenger |
|
| 3573 | - * @param EE_message_type|null $message_type |
|
| 3574 | - * |
|
| 3575 | - * @return bool |
|
| 3576 | - */ |
|
| 3577 | - protected function _setup_response_message_for_deactivating_messenger_with_message_types( |
|
| 3578 | - $messenger, |
|
| 3579 | - EE_message_type $message_type = null |
|
| 3580 | - ) { |
|
| 3581 | - EE_Error::overwrite_success(); |
|
| 3582 | - |
|
| 3583 | - //if $messenger isn't a valid messenger object then get out. |
|
| 3584 | - if ( ! $messenger instanceof EE_Messenger) { |
|
| 3585 | - EE_Error::add_error( |
|
| 3586 | - __('The messenger being deactivated is not a valid messenger', 'event_espresso'), |
|
| 3587 | - __FILE__, |
|
| 3588 | - __FUNCTION__, |
|
| 3589 | - __LINE__ |
|
| 3590 | - ); |
|
| 3563 | + return false; |
|
| 3564 | + } |
|
| 3565 | + } |
|
| 3566 | + } |
|
| 3567 | + |
|
| 3568 | + |
|
| 3569 | + /** |
|
| 3570 | + * This sets up the appropriate response for deactivating a messenger and/or message type. |
|
| 3571 | + * |
|
| 3572 | + * @param EE_messenger $messenger |
|
| 3573 | + * @param EE_message_type|null $message_type |
|
| 3574 | + * |
|
| 3575 | + * @return bool |
|
| 3576 | + */ |
|
| 3577 | + protected function _setup_response_message_for_deactivating_messenger_with_message_types( |
|
| 3578 | + $messenger, |
|
| 3579 | + EE_message_type $message_type = null |
|
| 3580 | + ) { |
|
| 3581 | + EE_Error::overwrite_success(); |
|
| 3582 | + |
|
| 3583 | + //if $messenger isn't a valid messenger object then get out. |
|
| 3584 | + if ( ! $messenger instanceof EE_Messenger) { |
|
| 3585 | + EE_Error::add_error( |
|
| 3586 | + __('The messenger being deactivated is not a valid messenger', 'event_espresso'), |
|
| 3587 | + __FILE__, |
|
| 3588 | + __FUNCTION__, |
|
| 3589 | + __LINE__ |
|
| 3590 | + ); |
|
| 3591 | 3591 | |
| 3592 | - return false; |
|
| 3593 | - } |
|
| 3594 | - |
|
| 3595 | - if ($message_type instanceof EE_message_type) { |
|
| 3596 | - $message_type_name = $message_type->name; |
|
| 3597 | - EE_Error::add_success( |
|
| 3598 | - sprintf( |
|
| 3599 | - __('%s message type has been successfully deactivated for the %s messenger.', 'event_espresso'), |
|
| 3600 | - ucwords($message_type->label['singular']), |
|
| 3601 | - ucwords($messenger->label['singular']) |
|
| 3602 | - ) |
|
| 3603 | - ); |
|
| 3604 | - } else { |
|
| 3605 | - $message_type_name = ''; |
|
| 3606 | - EE_Error::add_success( |
|
| 3607 | - sprintf( |
|
| 3608 | - __('%s messenger has been successfully deactivated.', 'event_espresso'), |
|
| 3609 | - ucwords($messenger->label['singular']) |
|
| 3610 | - ) |
|
| 3611 | - ); |
|
| 3612 | - } |
|
| 3613 | - |
|
| 3614 | - //if messenger was html or message type was invoice then let's make sure we deactivate invoice payment method. |
|
| 3615 | - if ($messenger->name == 'html' || $message_type_name == 'invoice') { |
|
| 3616 | - EE_Registry::instance()->load_lib('Payment_Method_Manager'); |
|
| 3617 | - $count_updated = EE_Payment_Method_Manager::instance()->deactivate_payment_method('invoice'); |
|
| 3618 | - if ($count_updated > 0) { |
|
| 3619 | - $msg = $message_type_name == 'invoice' |
|
| 3620 | - ? __('Deactivating the invoice message type also automatically deactivates the invoice payment method. In order for invoices to be generated the invoice message type must be active. If you completed this action by mistake, simply reactivate the invoice message type and then visit the payment methods admin page to reactivate the invoice payment method.', |
|
| 3621 | - 'event_espresso') |
|
| 3622 | - : __('Deactivating the html messenger also automatically deactivates the invoice payment method. In order for invoices to be generated the html messenger must be be active. If you completed this action by mistake, simply reactivate the html messenger, then visit the payment methods admin page to reactivate the invoice payment method.', |
|
| 3623 | - 'event_espresso'); |
|
| 3624 | - EE_Error::add_attention($msg); |
|
| 3625 | - } |
|
| 3626 | - } |
|
| 3627 | - |
|
| 3628 | - return true; |
|
| 3629 | - } |
|
| 3630 | - |
|
| 3631 | - |
|
| 3632 | - /** |
|
| 3633 | - * handles updating a message type form on messenger activation IF the message type has settings fields. (via ajax) |
|
| 3634 | - */ |
|
| 3635 | - public function update_mt_form() |
|
| 3636 | - { |
|
| 3637 | - if ( ! isset($this->_req_data['messenger']) || ! isset($this->_req_data['message_type'])) { |
|
| 3638 | - EE_Error::add_error(__('Require message type or messenger to send an updated form'), __FILE__, __FUNCTION__, |
|
| 3639 | - __LINE__); |
|
| 3640 | - $this->_return_json(); |
|
| 3641 | - } |
|
| 3642 | - |
|
| 3643 | - $message_types = $this->get_installed_message_types(); |
|
| 3644 | - |
|
| 3645 | - $message_type = $message_types[$this->_req_data['message_type']]; |
|
| 3646 | - $messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']); |
|
| 3647 | - |
|
| 3648 | - $content = $this->_message_type_settings_content($message_type, $messenger, true); |
|
| 3649 | - $this->_template_args['success'] = true; |
|
| 3650 | - $this->_template_args['content'] = $content; |
|
| 3651 | - $this->_return_json(); |
|
| 3652 | - } |
|
| 3653 | - |
|
| 3654 | - |
|
| 3655 | - /** |
|
| 3656 | - * this handles saving the settings for a messenger or message type |
|
| 3657 | - * |
|
| 3658 | - */ |
|
| 3659 | - public function save_settings() |
|
| 3660 | - { |
|
| 3661 | - if ( ! isset($this->_req_data['type'])) { |
|
| 3662 | - EE_Error::add_error(__('Cannot save settings because type is unknown (messenger settings or messsage type settings?)', |
|
| 3663 | - 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 3664 | - $this->_template_args['error'] = true; |
|
| 3665 | - $this->_return_json(); |
|
| 3666 | - } |
|
| 3667 | - |
|
| 3668 | - |
|
| 3669 | - if ($this->_req_data['type'] == 'messenger') { |
|
| 3670 | - $settings = $this->_req_data['messenger_settings']; //this should be an array. |
|
| 3671 | - $messenger = $settings['messenger']; |
|
| 3672 | - //let's setup the settings data |
|
| 3673 | - foreach ($settings as $key => $value) { |
|
| 3674 | - switch ($key) { |
|
| 3675 | - case 'messenger' : |
|
| 3676 | - unset($settings['messenger']); |
|
| 3677 | - break; |
|
| 3678 | - case 'message_types' : |
|
| 3679 | - unset($settings['message_types']); |
|
| 3680 | - break; |
|
| 3681 | - default : |
|
| 3682 | - $settings[$key] = $value; |
|
| 3683 | - break; |
|
| 3684 | - } |
|
| 3685 | - } |
|
| 3686 | - $this->_message_resource_manager->add_settings_for_messenger($messenger, $settings); |
|
| 3687 | - } else if ($this->_req_data['type'] == 'message_type') { |
|
| 3688 | - $settings = $this->_req_data['message_type_settings']; |
|
| 3689 | - $messenger = $settings['messenger']; |
|
| 3690 | - $message_type = $settings['message_type']; |
|
| 3592 | + return false; |
|
| 3593 | + } |
|
| 3594 | + |
|
| 3595 | + if ($message_type instanceof EE_message_type) { |
|
| 3596 | + $message_type_name = $message_type->name; |
|
| 3597 | + EE_Error::add_success( |
|
| 3598 | + sprintf( |
|
| 3599 | + __('%s message type has been successfully deactivated for the %s messenger.', 'event_espresso'), |
|
| 3600 | + ucwords($message_type->label['singular']), |
|
| 3601 | + ucwords($messenger->label['singular']) |
|
| 3602 | + ) |
|
| 3603 | + ); |
|
| 3604 | + } else { |
|
| 3605 | + $message_type_name = ''; |
|
| 3606 | + EE_Error::add_success( |
|
| 3607 | + sprintf( |
|
| 3608 | + __('%s messenger has been successfully deactivated.', 'event_espresso'), |
|
| 3609 | + ucwords($messenger->label['singular']) |
|
| 3610 | + ) |
|
| 3611 | + ); |
|
| 3612 | + } |
|
| 3613 | + |
|
| 3614 | + //if messenger was html or message type was invoice then let's make sure we deactivate invoice payment method. |
|
| 3615 | + if ($messenger->name == 'html' || $message_type_name == 'invoice') { |
|
| 3616 | + EE_Registry::instance()->load_lib('Payment_Method_Manager'); |
|
| 3617 | + $count_updated = EE_Payment_Method_Manager::instance()->deactivate_payment_method('invoice'); |
|
| 3618 | + if ($count_updated > 0) { |
|
| 3619 | + $msg = $message_type_name == 'invoice' |
|
| 3620 | + ? __('Deactivating the invoice message type also automatically deactivates the invoice payment method. In order for invoices to be generated the invoice message type must be active. If you completed this action by mistake, simply reactivate the invoice message type and then visit the payment methods admin page to reactivate the invoice payment method.', |
|
| 3621 | + 'event_espresso') |
|
| 3622 | + : __('Deactivating the html messenger also automatically deactivates the invoice payment method. In order for invoices to be generated the html messenger must be be active. If you completed this action by mistake, simply reactivate the html messenger, then visit the payment methods admin page to reactivate the invoice payment method.', |
|
| 3623 | + 'event_espresso'); |
|
| 3624 | + EE_Error::add_attention($msg); |
|
| 3625 | + } |
|
| 3626 | + } |
|
| 3627 | + |
|
| 3628 | + return true; |
|
| 3629 | + } |
|
| 3630 | + |
|
| 3631 | + |
|
| 3632 | + /** |
|
| 3633 | + * handles updating a message type form on messenger activation IF the message type has settings fields. (via ajax) |
|
| 3634 | + */ |
|
| 3635 | + public function update_mt_form() |
|
| 3636 | + { |
|
| 3637 | + if ( ! isset($this->_req_data['messenger']) || ! isset($this->_req_data['message_type'])) { |
|
| 3638 | + EE_Error::add_error(__('Require message type or messenger to send an updated form'), __FILE__, __FUNCTION__, |
|
| 3639 | + __LINE__); |
|
| 3640 | + $this->_return_json(); |
|
| 3641 | + } |
|
| 3642 | + |
|
| 3643 | + $message_types = $this->get_installed_message_types(); |
|
| 3644 | + |
|
| 3645 | + $message_type = $message_types[$this->_req_data['message_type']]; |
|
| 3646 | + $messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']); |
|
| 3647 | + |
|
| 3648 | + $content = $this->_message_type_settings_content($message_type, $messenger, true); |
|
| 3649 | + $this->_template_args['success'] = true; |
|
| 3650 | + $this->_template_args['content'] = $content; |
|
| 3651 | + $this->_return_json(); |
|
| 3652 | + } |
|
| 3653 | + |
|
| 3654 | + |
|
| 3655 | + /** |
|
| 3656 | + * this handles saving the settings for a messenger or message type |
|
| 3657 | + * |
|
| 3658 | + */ |
|
| 3659 | + public function save_settings() |
|
| 3660 | + { |
|
| 3661 | + if ( ! isset($this->_req_data['type'])) { |
|
| 3662 | + EE_Error::add_error(__('Cannot save settings because type is unknown (messenger settings or messsage type settings?)', |
|
| 3663 | + 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 3664 | + $this->_template_args['error'] = true; |
|
| 3665 | + $this->_return_json(); |
|
| 3666 | + } |
|
| 3667 | + |
|
| 3668 | + |
|
| 3669 | + if ($this->_req_data['type'] == 'messenger') { |
|
| 3670 | + $settings = $this->_req_data['messenger_settings']; //this should be an array. |
|
| 3671 | + $messenger = $settings['messenger']; |
|
| 3672 | + //let's setup the settings data |
|
| 3673 | + foreach ($settings as $key => $value) { |
|
| 3674 | + switch ($key) { |
|
| 3675 | + case 'messenger' : |
|
| 3676 | + unset($settings['messenger']); |
|
| 3677 | + break; |
|
| 3678 | + case 'message_types' : |
|
| 3679 | + unset($settings['message_types']); |
|
| 3680 | + break; |
|
| 3681 | + default : |
|
| 3682 | + $settings[$key] = $value; |
|
| 3683 | + break; |
|
| 3684 | + } |
|
| 3685 | + } |
|
| 3686 | + $this->_message_resource_manager->add_settings_for_messenger($messenger, $settings); |
|
| 3687 | + } else if ($this->_req_data['type'] == 'message_type') { |
|
| 3688 | + $settings = $this->_req_data['message_type_settings']; |
|
| 3689 | + $messenger = $settings['messenger']; |
|
| 3690 | + $message_type = $settings['message_type']; |
|
| 3691 | 3691 | |
| 3692 | - foreach ($settings as $key => $value) { |
|
| 3693 | - switch ($key) { |
|
| 3694 | - case 'messenger' : |
|
| 3695 | - unset($settings['messenger']); |
|
| 3696 | - break; |
|
| 3697 | - case 'message_type' : |
|
| 3698 | - unset($settings['message_type']); |
|
| 3699 | - break; |
|
| 3700 | - default : |
|
| 3701 | - $settings[$key] = $value; |
|
| 3702 | - break; |
|
| 3703 | - } |
|
| 3704 | - } |
|
| 3692 | + foreach ($settings as $key => $value) { |
|
| 3693 | + switch ($key) { |
|
| 3694 | + case 'messenger' : |
|
| 3695 | + unset($settings['messenger']); |
|
| 3696 | + break; |
|
| 3697 | + case 'message_type' : |
|
| 3698 | + unset($settings['message_type']); |
|
| 3699 | + break; |
|
| 3700 | + default : |
|
| 3701 | + $settings[$key] = $value; |
|
| 3702 | + break; |
|
| 3703 | + } |
|
| 3704 | + } |
|
| 3705 | 3705 | |
| 3706 | - $this->_message_resource_manager->add_settings_for_message_type($messenger, $message_type, $settings); |
|
| 3707 | - } |
|
| 3708 | - |
|
| 3709 | - //okay we should have the data all setup. Now we just update! |
|
| 3710 | - $success = $this->_message_resource_manager->update_active_messengers_option(); |
|
| 3711 | - |
|
| 3712 | - if ($success) { |
|
| 3713 | - EE_Error::add_success(__('Settings updated', 'event_espresso')); |
|
| 3714 | - } else { |
|
| 3715 | - EE_Error::add_error(__('Settings did not get updated', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 3716 | - } |
|
| 3717 | - |
|
| 3718 | - $this->_template_args['success'] = $success; |
|
| 3719 | - $this->_return_json(); |
|
| 3720 | - } |
|
| 3721 | - |
|
| 3722 | - |
|
| 3723 | - |
|
| 3724 | - |
|
| 3725 | - /** EE MESSAGE PROCESSING ACTIONS **/ |
|
| 3726 | - |
|
| 3727 | - |
|
| 3728 | - /** |
|
| 3729 | - * This immediately generates any EE_Message ID's that are selected that are EEM_Message::status_incomplete |
|
| 3730 | - * However, this does not send immediately, it just queues for sending. |
|
| 3731 | - * |
|
| 3732 | - * @since 4.9.0 |
|
| 3733 | - */ |
|
| 3734 | - protected function _generate_now() |
|
| 3735 | - { |
|
| 3736 | - $msg_ids = $this->_get_msg_ids_from_request(); |
|
| 3737 | - EED_Messages::generate_now($msg_ids); |
|
| 3738 | - $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3739 | - } |
|
| 3740 | - |
|
| 3741 | - |
|
| 3742 | - /** |
|
| 3743 | - * This immediately generates AND sends any EE_Message's selected that are EEM_Message::status_incomplete or that |
|
| 3744 | - * are EEM_Message::status_resend or EEM_Message::status_idle |
|
| 3745 | - * |
|
| 3746 | - * @since 4.9.0 |
|
| 3747 | - * |
|
| 3748 | - */ |
|
| 3749 | - protected function _generate_and_send_now() |
|
| 3750 | - { |
|
| 3751 | - $this->_generate_now(); |
|
| 3752 | - $this->_send_now(); |
|
| 3753 | - $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3754 | - } |
|
| 3755 | - |
|
| 3756 | - |
|
| 3757 | - /** |
|
| 3758 | - * This queues any EEM_Message::status_sent EE_Message ids in the request for resending. |
|
| 3759 | - * |
|
| 3760 | - * @since 4.9.0 |
|
| 3761 | - */ |
|
| 3762 | - protected function _queue_for_resending() |
|
| 3763 | - { |
|
| 3764 | - $msg_ids = $this->_get_msg_ids_from_request(); |
|
| 3765 | - EED_Messages::queue_for_resending($msg_ids); |
|
| 3766 | - $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3767 | - } |
|
| 3768 | - |
|
| 3769 | - |
|
| 3770 | - /** |
|
| 3771 | - * This sends immediately any EEM_Message::status_idle or EEM_Message::status_resend messages in the queue |
|
| 3772 | - * |
|
| 3773 | - * @since 4.9.0 |
|
| 3774 | - */ |
|
| 3775 | - protected function _send_now() |
|
| 3776 | - { |
|
| 3777 | - $msg_ids = $this->_get_msg_ids_from_request(); |
|
| 3778 | - EED_Messages::send_now($msg_ids); |
|
| 3779 | - $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3780 | - } |
|
| 3781 | - |
|
| 3782 | - |
|
| 3783 | - /** |
|
| 3784 | - * Deletes EE_messages for IDs in the request. |
|
| 3785 | - * |
|
| 3786 | - * @since 4.9.0 |
|
| 3787 | - */ |
|
| 3788 | - protected function _delete_ee_messages() |
|
| 3789 | - { |
|
| 3790 | - $msg_ids = $this->_get_msg_ids_from_request(); |
|
| 3791 | - $deleted_count = 0; |
|
| 3792 | - foreach ($msg_ids as $msg_id) { |
|
| 3793 | - if (EEM_Message::instance()->delete_by_ID($msg_id)) { |
|
| 3794 | - $deleted_count++; |
|
| 3795 | - } |
|
| 3796 | - } |
|
| 3797 | - if ($deleted_count) { |
|
| 3798 | - $this->_redirect_after_action( |
|
| 3799 | - true, |
|
| 3800 | - _n('message', 'messages', $deleted_count, 'event_espresso'), |
|
| 3801 | - __('deleted', 'event_espresso') |
|
| 3802 | - ); |
|
| 3803 | - } else { |
|
| 3804 | - EE_Error::add_error( |
|
| 3805 | - _n('The message was not deleted.', 'The messages were not deleted', count($msg_ids), 'event_espresso'), |
|
| 3806 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 3807 | - ); |
|
| 3808 | - $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3809 | - } |
|
| 3810 | - } |
|
| 3811 | - |
|
| 3812 | - |
|
| 3813 | - /** |
|
| 3814 | - * This looks for 'MSG_ID' key in the request and returns an array of MSG_ID's if present. |
|
| 3815 | - * @since 4.9.0 |
|
| 3816 | - * @return array |
|
| 3817 | - */ |
|
| 3818 | - protected function _get_msg_ids_from_request() |
|
| 3819 | - { |
|
| 3820 | - if ( ! isset($this->_req_data['MSG_ID'])) { |
|
| 3821 | - return array(); |
|
| 3822 | - } |
|
| 3823 | - |
|
| 3824 | - return is_array($this->_req_data['MSG_ID']) ? array_keys($this->_req_data['MSG_ID']) : array($this->_req_data['MSG_ID']); |
|
| 3825 | - } |
|
| 3706 | + $this->_message_resource_manager->add_settings_for_message_type($messenger, $message_type, $settings); |
|
| 3707 | + } |
|
| 3708 | + |
|
| 3709 | + //okay we should have the data all setup. Now we just update! |
|
| 3710 | + $success = $this->_message_resource_manager->update_active_messengers_option(); |
|
| 3711 | + |
|
| 3712 | + if ($success) { |
|
| 3713 | + EE_Error::add_success(__('Settings updated', 'event_espresso')); |
|
| 3714 | + } else { |
|
| 3715 | + EE_Error::add_error(__('Settings did not get updated', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 3716 | + } |
|
| 3717 | + |
|
| 3718 | + $this->_template_args['success'] = $success; |
|
| 3719 | + $this->_return_json(); |
|
| 3720 | + } |
|
| 3721 | + |
|
| 3722 | + |
|
| 3723 | + |
|
| 3724 | + |
|
| 3725 | + /** EE MESSAGE PROCESSING ACTIONS **/ |
|
| 3726 | + |
|
| 3727 | + |
|
| 3728 | + /** |
|
| 3729 | + * This immediately generates any EE_Message ID's that are selected that are EEM_Message::status_incomplete |
|
| 3730 | + * However, this does not send immediately, it just queues for sending. |
|
| 3731 | + * |
|
| 3732 | + * @since 4.9.0 |
|
| 3733 | + */ |
|
| 3734 | + protected function _generate_now() |
|
| 3735 | + { |
|
| 3736 | + $msg_ids = $this->_get_msg_ids_from_request(); |
|
| 3737 | + EED_Messages::generate_now($msg_ids); |
|
| 3738 | + $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3739 | + } |
|
| 3740 | + |
|
| 3741 | + |
|
| 3742 | + /** |
|
| 3743 | + * This immediately generates AND sends any EE_Message's selected that are EEM_Message::status_incomplete or that |
|
| 3744 | + * are EEM_Message::status_resend or EEM_Message::status_idle |
|
| 3745 | + * |
|
| 3746 | + * @since 4.9.0 |
|
| 3747 | + * |
|
| 3748 | + */ |
|
| 3749 | + protected function _generate_and_send_now() |
|
| 3750 | + { |
|
| 3751 | + $this->_generate_now(); |
|
| 3752 | + $this->_send_now(); |
|
| 3753 | + $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3754 | + } |
|
| 3755 | + |
|
| 3756 | + |
|
| 3757 | + /** |
|
| 3758 | + * This queues any EEM_Message::status_sent EE_Message ids in the request for resending. |
|
| 3759 | + * |
|
| 3760 | + * @since 4.9.0 |
|
| 3761 | + */ |
|
| 3762 | + protected function _queue_for_resending() |
|
| 3763 | + { |
|
| 3764 | + $msg_ids = $this->_get_msg_ids_from_request(); |
|
| 3765 | + EED_Messages::queue_for_resending($msg_ids); |
|
| 3766 | + $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3767 | + } |
|
| 3768 | + |
|
| 3769 | + |
|
| 3770 | + /** |
|
| 3771 | + * This sends immediately any EEM_Message::status_idle or EEM_Message::status_resend messages in the queue |
|
| 3772 | + * |
|
| 3773 | + * @since 4.9.0 |
|
| 3774 | + */ |
|
| 3775 | + protected function _send_now() |
|
| 3776 | + { |
|
| 3777 | + $msg_ids = $this->_get_msg_ids_from_request(); |
|
| 3778 | + EED_Messages::send_now($msg_ids); |
|
| 3779 | + $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3780 | + } |
|
| 3781 | + |
|
| 3782 | + |
|
| 3783 | + /** |
|
| 3784 | + * Deletes EE_messages for IDs in the request. |
|
| 3785 | + * |
|
| 3786 | + * @since 4.9.0 |
|
| 3787 | + */ |
|
| 3788 | + protected function _delete_ee_messages() |
|
| 3789 | + { |
|
| 3790 | + $msg_ids = $this->_get_msg_ids_from_request(); |
|
| 3791 | + $deleted_count = 0; |
|
| 3792 | + foreach ($msg_ids as $msg_id) { |
|
| 3793 | + if (EEM_Message::instance()->delete_by_ID($msg_id)) { |
|
| 3794 | + $deleted_count++; |
|
| 3795 | + } |
|
| 3796 | + } |
|
| 3797 | + if ($deleted_count) { |
|
| 3798 | + $this->_redirect_after_action( |
|
| 3799 | + true, |
|
| 3800 | + _n('message', 'messages', $deleted_count, 'event_espresso'), |
|
| 3801 | + __('deleted', 'event_espresso') |
|
| 3802 | + ); |
|
| 3803 | + } else { |
|
| 3804 | + EE_Error::add_error( |
|
| 3805 | + _n('The message was not deleted.', 'The messages were not deleted', count($msg_ids), 'event_espresso'), |
|
| 3806 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 3807 | + ); |
|
| 3808 | + $this->_redirect_after_action(false, '', '', array(), true); |
|
| 3809 | + } |
|
| 3810 | + } |
|
| 3811 | + |
|
| 3812 | + |
|
| 3813 | + /** |
|
| 3814 | + * This looks for 'MSG_ID' key in the request and returns an array of MSG_ID's if present. |
|
| 3815 | + * @since 4.9.0 |
|
| 3816 | + * @return array |
|
| 3817 | + */ |
|
| 3818 | + protected function _get_msg_ids_from_request() |
|
| 3819 | + { |
|
| 3820 | + if ( ! isset($this->_req_data['MSG_ID'])) { |
|
| 3821 | + return array(); |
|
| 3822 | + } |
|
| 3823 | + |
|
| 3824 | + return is_array($this->_req_data['MSG_ID']) ? array_keys($this->_req_data['MSG_ID']) : array($this->_req_data['MSG_ID']); |
|
| 3825 | + } |
|
| 3826 | 3826 | |
| 3827 | 3827 | |
| 3828 | 3828 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | use EventEspresso\core\services\loaders\LoaderFactory; |
| 5 | 5 | |
| 6 | 6 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 7 | - exit('No direct script access allowed'); |
|
| 7 | + exit('No direct script access allowed'); |
|
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | |
@@ -24,208 +24,208 @@ discard block |
||
| 24 | 24 | class EE_Load_Espresso_Core implements EEI_Request_Decorator, EEI_Request_Stack_Core_App |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var EE_Request $request |
|
| 29 | - */ |
|
| 30 | - protected $request; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var EE_Response $response |
|
| 34 | - */ |
|
| 35 | - protected $response; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var EE_Dependency_Map $dependency_map |
|
| 39 | - */ |
|
| 40 | - protected $dependency_map; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var EE_Registry $registry |
|
| 44 | - */ |
|
| 45 | - protected $registry; |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * EE_Load_Espresso_Core constructor |
|
| 51 | - */ |
|
| 52 | - public function __construct() |
|
| 53 | - { |
|
| 54 | - espresso_load_required('EventEspresso\core\Factory', EE_CORE . 'Factory.php'); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * handle |
|
| 61 | - * sets hooks for running rest of system |
|
| 62 | - * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
|
| 63 | - * starting EE Addons from any other point may lead to problems |
|
| 64 | - * |
|
| 65 | - * @param EE_Request $request |
|
| 66 | - * @param EE_Response $response |
|
| 67 | - * @return EE_Response |
|
| 68 | - * @throws EE_Error |
|
| 69 | - * @throws InvalidDataTypeException |
|
| 70 | - * @throws InvalidInterfaceException |
|
| 71 | - * @throws InvalidArgumentException |
|
| 72 | - */ |
|
| 73 | - public function handle_request(EE_Request $request, EE_Response $response) |
|
| 74 | - { |
|
| 75 | - $this->request = $request; |
|
| 76 | - $this->response = $response; |
|
| 77 | - // info about how to load classes required by other classes |
|
| 78 | - $this->dependency_map = $this->_load_dependency_map(); |
|
| 79 | - // central repository for classes |
|
| 80 | - $this->registry = $this->_load_registry(); |
|
| 81 | - // PSR4 Autoloaders |
|
| 82 | - $this->registry->load_core('EE_Psr4AutoloaderInit'); |
|
| 83 | - do_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading'); |
|
| 84 | - $loader = LoaderFactory::getLoader($this->registry); |
|
| 85 | - $this->dependency_map->setLoader($loader); |
|
| 86 | - // build DI container |
|
| 87 | - // $OpenCoffeeShop = new EventEspresso\core\services\container\OpenCoffeeShop(); |
|
| 88 | - // $OpenCoffeeShop->addRecipes(); |
|
| 89 | - // $CoffeeShop = $OpenCoffeeShop->CoffeeShop(); |
|
| 90 | - // workarounds for PHP < 5.3 |
|
| 91 | - $this->_load_class_tools(); |
|
| 92 | - // load interfaces |
|
| 93 | - espresso_load_required('EEI_Payment_Method_Interfaces', |
|
| 94 | - EE_LIBRARIES . 'payment_methods' . DS . 'EEI_Payment_Method_Interfaces.php'); |
|
| 95 | - // deprecated functions |
|
| 96 | - espresso_load_required('EE_Deprecated', EE_CORE . 'EE_Deprecated.core.php'); |
|
| 97 | - // WP cron jobs |
|
| 98 | - $loader->getShared('EE_Cron_Tasks'); |
|
| 99 | - $loader->getShared('EE_Request_Handler'); |
|
| 100 | - $loader->getShared('EE_System'); |
|
| 101 | - return $this->response; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return EE_Request |
|
| 108 | - */ |
|
| 109 | - public function request() |
|
| 110 | - { |
|
| 111 | - return $this->request; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @return EE_Response |
|
| 118 | - */ |
|
| 119 | - public function response() |
|
| 120 | - { |
|
| 121 | - return $this->response; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return EE_Dependency_Map |
|
| 128 | - * @throws EE_Error |
|
| 129 | - */ |
|
| 130 | - public function dependency_map() |
|
| 131 | - { |
|
| 132 | - if (! $this->dependency_map instanceof EE_Dependency_Map) { |
|
| 133 | - throw new EE_Error( |
|
| 134 | - sprintf( |
|
| 135 | - __('Invalid EE_Dependency_Map: "%1$s"', 'event_espresso'), |
|
| 136 | - print_r($this->dependency_map, true) |
|
| 137 | - ) |
|
| 138 | - ); |
|
| 139 | - } |
|
| 140 | - return $this->dependency_map; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return EE_Registry |
|
| 147 | - * @throws EE_Error |
|
| 148 | - */ |
|
| 149 | - public function registry() |
|
| 150 | - { |
|
| 151 | - if (! $this->registry instanceof EE_Registry) { |
|
| 152 | - throw new EE_Error( |
|
| 153 | - sprintf( |
|
| 154 | - __('Invalid EE_Registry: "%1$s"', 'event_espresso'), |
|
| 155 | - print_r($this->registry, true) |
|
| 156 | - ) |
|
| 157 | - ); |
|
| 158 | - } |
|
| 159 | - return $this->registry; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * @return EE_Dependency_Map |
|
| 166 | - */ |
|
| 167 | - private function _load_dependency_map() |
|
| 168 | - { |
|
| 169 | - if (! is_readable(EE_CORE . 'EE_Dependency_Map.core.php')) { |
|
| 170 | - EE_Error::add_error( |
|
| 171 | - __('The EE_Dependency_Map core class could not be loaded.', 'event_espresso'), |
|
| 172 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 173 | - ); |
|
| 174 | - wp_die(EE_Error::get_notices()); |
|
| 175 | - } |
|
| 176 | - require_once(EE_CORE . 'EE_Dependency_Map.core.php'); |
|
| 177 | - return EE_Dependency_Map::instance($this->request, $this->response); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @return EE_Registry |
|
| 184 | - */ |
|
| 185 | - private function _load_registry() |
|
| 186 | - { |
|
| 187 | - if (! is_readable(EE_CORE . 'EE_Registry.core.php')) { |
|
| 188 | - EE_Error::add_error( |
|
| 189 | - __('The EE_Registry core class could not be loaded.', 'event_espresso'), |
|
| 190 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 191 | - ); |
|
| 192 | - wp_die(EE_Error::get_notices()); |
|
| 193 | - } |
|
| 194 | - require_once(EE_CORE . 'EE_Registry.core.php'); |
|
| 195 | - return EE_Registry::instance($this->dependency_map); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @return void |
|
| 202 | - */ |
|
| 203 | - private function _load_class_tools() |
|
| 204 | - { |
|
| 205 | - if (! is_readable(EE_HELPERS . 'EEH_Class_Tools.helper.php')) { |
|
| 206 | - EE_Error::add_error( |
|
| 207 | - __('The EEH_Class_Tools helper could not be loaded.', 'event_espresso'), |
|
| 208 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 209 | - ); |
|
| 210 | - } |
|
| 211 | - require_once(EE_HELPERS . 'EEH_Class_Tools.helper.php'); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * called after the request stack has been fully processed |
|
| 218 | - * if any of the middleware apps has requested the plugin be deactivated, then we do that now |
|
| 219 | - * |
|
| 220 | - * @param EE_Request $request |
|
| 221 | - * @param EE_Response $response |
|
| 222 | - */ |
|
| 223 | - public function handle_response(EE_Request $request, EE_Response $response) |
|
| 224 | - { |
|
| 225 | - if ($response->plugin_deactivated()) { |
|
| 226 | - espresso_deactivate_plugin(EE_PLUGIN_BASENAME); |
|
| 227 | - } |
|
| 228 | - } |
|
| 27 | + /** |
|
| 28 | + * @var EE_Request $request |
|
| 29 | + */ |
|
| 30 | + protected $request; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var EE_Response $response |
|
| 34 | + */ |
|
| 35 | + protected $response; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var EE_Dependency_Map $dependency_map |
|
| 39 | + */ |
|
| 40 | + protected $dependency_map; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var EE_Registry $registry |
|
| 44 | + */ |
|
| 45 | + protected $registry; |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * EE_Load_Espresso_Core constructor |
|
| 51 | + */ |
|
| 52 | + public function __construct() |
|
| 53 | + { |
|
| 54 | + espresso_load_required('EventEspresso\core\Factory', EE_CORE . 'Factory.php'); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * handle |
|
| 61 | + * sets hooks for running rest of system |
|
| 62 | + * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
|
| 63 | + * starting EE Addons from any other point may lead to problems |
|
| 64 | + * |
|
| 65 | + * @param EE_Request $request |
|
| 66 | + * @param EE_Response $response |
|
| 67 | + * @return EE_Response |
|
| 68 | + * @throws EE_Error |
|
| 69 | + * @throws InvalidDataTypeException |
|
| 70 | + * @throws InvalidInterfaceException |
|
| 71 | + * @throws InvalidArgumentException |
|
| 72 | + */ |
|
| 73 | + public function handle_request(EE_Request $request, EE_Response $response) |
|
| 74 | + { |
|
| 75 | + $this->request = $request; |
|
| 76 | + $this->response = $response; |
|
| 77 | + // info about how to load classes required by other classes |
|
| 78 | + $this->dependency_map = $this->_load_dependency_map(); |
|
| 79 | + // central repository for classes |
|
| 80 | + $this->registry = $this->_load_registry(); |
|
| 81 | + // PSR4 Autoloaders |
|
| 82 | + $this->registry->load_core('EE_Psr4AutoloaderInit'); |
|
| 83 | + do_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading'); |
|
| 84 | + $loader = LoaderFactory::getLoader($this->registry); |
|
| 85 | + $this->dependency_map->setLoader($loader); |
|
| 86 | + // build DI container |
|
| 87 | + // $OpenCoffeeShop = new EventEspresso\core\services\container\OpenCoffeeShop(); |
|
| 88 | + // $OpenCoffeeShop->addRecipes(); |
|
| 89 | + // $CoffeeShop = $OpenCoffeeShop->CoffeeShop(); |
|
| 90 | + // workarounds for PHP < 5.3 |
|
| 91 | + $this->_load_class_tools(); |
|
| 92 | + // load interfaces |
|
| 93 | + espresso_load_required('EEI_Payment_Method_Interfaces', |
|
| 94 | + EE_LIBRARIES . 'payment_methods' . DS . 'EEI_Payment_Method_Interfaces.php'); |
|
| 95 | + // deprecated functions |
|
| 96 | + espresso_load_required('EE_Deprecated', EE_CORE . 'EE_Deprecated.core.php'); |
|
| 97 | + // WP cron jobs |
|
| 98 | + $loader->getShared('EE_Cron_Tasks'); |
|
| 99 | + $loader->getShared('EE_Request_Handler'); |
|
| 100 | + $loader->getShared('EE_System'); |
|
| 101 | + return $this->response; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return EE_Request |
|
| 108 | + */ |
|
| 109 | + public function request() |
|
| 110 | + { |
|
| 111 | + return $this->request; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @return EE_Response |
|
| 118 | + */ |
|
| 119 | + public function response() |
|
| 120 | + { |
|
| 121 | + return $this->response; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return EE_Dependency_Map |
|
| 128 | + * @throws EE_Error |
|
| 129 | + */ |
|
| 130 | + public function dependency_map() |
|
| 131 | + { |
|
| 132 | + if (! $this->dependency_map instanceof EE_Dependency_Map) { |
|
| 133 | + throw new EE_Error( |
|
| 134 | + sprintf( |
|
| 135 | + __('Invalid EE_Dependency_Map: "%1$s"', 'event_espresso'), |
|
| 136 | + print_r($this->dependency_map, true) |
|
| 137 | + ) |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | + return $this->dependency_map; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return EE_Registry |
|
| 147 | + * @throws EE_Error |
|
| 148 | + */ |
|
| 149 | + public function registry() |
|
| 150 | + { |
|
| 151 | + if (! $this->registry instanceof EE_Registry) { |
|
| 152 | + throw new EE_Error( |
|
| 153 | + sprintf( |
|
| 154 | + __('Invalid EE_Registry: "%1$s"', 'event_espresso'), |
|
| 155 | + print_r($this->registry, true) |
|
| 156 | + ) |
|
| 157 | + ); |
|
| 158 | + } |
|
| 159 | + return $this->registry; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * @return EE_Dependency_Map |
|
| 166 | + */ |
|
| 167 | + private function _load_dependency_map() |
|
| 168 | + { |
|
| 169 | + if (! is_readable(EE_CORE . 'EE_Dependency_Map.core.php')) { |
|
| 170 | + EE_Error::add_error( |
|
| 171 | + __('The EE_Dependency_Map core class could not be loaded.', 'event_espresso'), |
|
| 172 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 173 | + ); |
|
| 174 | + wp_die(EE_Error::get_notices()); |
|
| 175 | + } |
|
| 176 | + require_once(EE_CORE . 'EE_Dependency_Map.core.php'); |
|
| 177 | + return EE_Dependency_Map::instance($this->request, $this->response); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @return EE_Registry |
|
| 184 | + */ |
|
| 185 | + private function _load_registry() |
|
| 186 | + { |
|
| 187 | + if (! is_readable(EE_CORE . 'EE_Registry.core.php')) { |
|
| 188 | + EE_Error::add_error( |
|
| 189 | + __('The EE_Registry core class could not be loaded.', 'event_espresso'), |
|
| 190 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 191 | + ); |
|
| 192 | + wp_die(EE_Error::get_notices()); |
|
| 193 | + } |
|
| 194 | + require_once(EE_CORE . 'EE_Registry.core.php'); |
|
| 195 | + return EE_Registry::instance($this->dependency_map); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @return void |
|
| 202 | + */ |
|
| 203 | + private function _load_class_tools() |
|
| 204 | + { |
|
| 205 | + if (! is_readable(EE_HELPERS . 'EEH_Class_Tools.helper.php')) { |
|
| 206 | + EE_Error::add_error( |
|
| 207 | + __('The EEH_Class_Tools helper could not be loaded.', 'event_espresso'), |
|
| 208 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 209 | + ); |
|
| 210 | + } |
|
| 211 | + require_once(EE_HELPERS . 'EEH_Class_Tools.helper.php'); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * called after the request stack has been fully processed |
|
| 218 | + * if any of the middleware apps has requested the plugin be deactivated, then we do that now |
|
| 219 | + * |
|
| 220 | + * @param EE_Request $request |
|
| 221 | + * @param EE_Response $response |
|
| 222 | + */ |
|
| 223 | + public function handle_response(EE_Request $request, EE_Response $response) |
|
| 224 | + { |
|
| 225 | + if ($response->plugin_deactivated()) { |
|
| 226 | + espresso_deactivate_plugin(EE_PLUGIN_BASENAME); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | 230 | |
| 231 | 231 | |
@@ -275,7 +275,7 @@ discard block |
||
| 275 | 275 | |
| 276 | 276 | |
| 277 | 277 | /** |
| 278 | - * @param $string |
|
| 278 | + * @param string $string |
|
| 279 | 279 | * @return void |
| 280 | 280 | */ |
| 281 | 281 | public function add_output($string) |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | |
| 328 | 328 | |
| 329 | 329 | /** |
| 330 | - * @return mixed |
|
| 330 | + * @return boolean |
|
| 331 | 331 | */ |
| 332 | 332 | public function is_espresso_page() |
| 333 | 333 | { |
@@ -389,7 +389,7 @@ discard block |
||
| 389 | 389 | /** |
| 390 | 390 | * remove param |
| 391 | 391 | * |
| 392 | - * @param $key |
|
| 392 | + * @param string $key |
|
| 393 | 393 | * @return void |
| 394 | 394 | */ |
| 395 | 395 | public function un_set($key) |
@@ -13,389 +13,389 @@ |
||
| 13 | 13 | final class EE_Request_Handler implements InterminableInterface |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var EE_Request $request |
|
| 18 | - */ |
|
| 19 | - private $request; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var array $_notice |
|
| 23 | - */ |
|
| 24 | - private $_notice = array(); |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * rendered output to be returned to WP |
|
| 28 | - * |
|
| 29 | - * @var string $_output |
|
| 30 | - */ |
|
| 31 | - private $_output = ''; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * whether current request is via AJAX |
|
| 35 | - * |
|
| 36 | - * @var boolean $ajax |
|
| 37 | - */ |
|
| 38 | - public $ajax = false; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * whether current request is via AJAX from the frontend of the site |
|
| 42 | - * |
|
| 43 | - * @var boolean $front_ajax |
|
| 44 | - */ |
|
| 45 | - public $front_ajax = false; |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param EE_Request $request |
|
| 51 | - */ |
|
| 52 | - public function __construct(EE_Request $request) |
|
| 53 | - { |
|
| 54 | - $this->request = $request; |
|
| 55 | - $this->ajax = $this->request->ajax; |
|
| 56 | - $this->front_ajax = $this->request->front_ajax; |
|
| 57 | - do_action('AHEE__EE_Request_Handler__construct__complete'); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param WP $wp |
|
| 64 | - * @return void |
|
| 65 | - * @throws EE_Error |
|
| 66 | - * @throws ReflectionException |
|
| 67 | - */ |
|
| 68 | - public function parse_request($wp = null) |
|
| 69 | - { |
|
| 70 | - //if somebody forgot to provide us with WP, that's ok because its global |
|
| 71 | - if (! $wp instanceof WP) { |
|
| 72 | - global $wp; |
|
| 73 | - } |
|
| 74 | - $this->set_request_vars($wp); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param WP $wp |
|
| 81 | - * @return void |
|
| 82 | - * @throws EE_Error |
|
| 83 | - * @throws ReflectionException |
|
| 84 | - */ |
|
| 85 | - public function set_request_vars($wp = null) |
|
| 86 | - { |
|
| 87 | - if (! is_admin()) { |
|
| 88 | - // set request post_id |
|
| 89 | - $this->request->set('post_id', $this->get_post_id_from_request($wp)); |
|
| 90 | - // set request post name |
|
| 91 | - $this->request->set('post_name', $this->get_post_name_from_request($wp)); |
|
| 92 | - // set request post_type |
|
| 93 | - $this->request->set('post_type', $this->get_post_type_from_request($wp)); |
|
| 94 | - // true or false ? is this page being used by EE ? |
|
| 95 | - $this->set_espresso_page(); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param WP $wp |
|
| 103 | - * @return int |
|
| 104 | - */ |
|
| 105 | - public function get_post_id_from_request($wp = null) |
|
| 106 | - { |
|
| 107 | - if (! $wp instanceof WP) { |
|
| 108 | - global $wp; |
|
| 109 | - } |
|
| 110 | - $post_id = null; |
|
| 111 | - if (isset($wp->query_vars['p'])) { |
|
| 112 | - $post_id = $wp->query_vars['p']; |
|
| 113 | - } |
|
| 114 | - if (! $post_id && isset($wp->query_vars['page_id'])) { |
|
| 115 | - $post_id = $wp->query_vars['page_id']; |
|
| 116 | - } |
|
| 117 | - if (! $post_id && $wp->request !== null && is_numeric(basename($wp->request))) { |
|
| 118 | - $post_id = basename($wp->request); |
|
| 119 | - } |
|
| 120 | - return $post_id; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param WP $wp |
|
| 127 | - * @return string |
|
| 128 | - */ |
|
| 129 | - public function get_post_name_from_request($wp = null) |
|
| 130 | - { |
|
| 131 | - if (! $wp instanceof WP) { |
|
| 132 | - global $wp; |
|
| 133 | - } |
|
| 134 | - $post_name = null; |
|
| 135 | - if (isset($wp->query_vars['name']) && ! empty($wp->query_vars['name'])) { |
|
| 136 | - $post_name = $wp->query_vars['name']; |
|
| 137 | - } |
|
| 138 | - if (! $post_name && isset($wp->query_vars['pagename']) && ! empty($wp->query_vars['pagename'])) { |
|
| 139 | - $post_name = $wp->query_vars['pagename']; |
|
| 140 | - } |
|
| 141 | - if (! $post_name && $wp->request !== null && ! empty($wp->request)) { |
|
| 142 | - $possible_post_name = basename($wp->request); |
|
| 143 | - if (! is_numeric($possible_post_name)) { |
|
| 144 | - /** @type WPDB $wpdb */ |
|
| 145 | - global $wpdb; |
|
| 146 | - $SQL = |
|
| 147 | - "SELECT ID from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND post_name=%s"; |
|
| 148 | - $possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $possible_post_name)); |
|
| 149 | - if ($possible_post_name) { |
|
| 150 | - $post_name = $possible_post_name; |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - if (! $post_name && $this->get('post_id')) { |
|
| 155 | - /** @type WPDB $wpdb */ |
|
| 156 | - global $wpdb; |
|
| 157 | - $SQL = |
|
| 158 | - "SELECT post_name from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND ID=%d"; |
|
| 159 | - $possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $this->get('post_id'))); |
|
| 160 | - if ($possible_post_name) { |
|
| 161 | - $post_name = $possible_post_name; |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - return $post_name; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @param WP $wp |
|
| 171 | - * @return mixed |
|
| 172 | - */ |
|
| 173 | - public function get_post_type_from_request($wp = null) |
|
| 174 | - { |
|
| 175 | - if (! $wp instanceof WP) { |
|
| 176 | - global $wp; |
|
| 177 | - } |
|
| 178 | - return isset($wp->query_vars['post_type']) |
|
| 179 | - ? $wp->query_vars['post_type'] |
|
| 180 | - : null; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Just a helper method for getting the url for the displayed page. |
|
| 187 | - * |
|
| 188 | - * @param WP $wp |
|
| 189 | - * @return string |
|
| 190 | - */ |
|
| 191 | - public function get_current_page_permalink($wp = null) |
|
| 192 | - { |
|
| 193 | - $post_id = $this->get_post_id_from_request($wp); |
|
| 194 | - if ($post_id) { |
|
| 195 | - $current_page_permalink = get_permalink($post_id); |
|
| 196 | - } else { |
|
| 197 | - if (! $wp instanceof WP) { |
|
| 198 | - global $wp; |
|
| 199 | - } |
|
| 200 | - if ($wp->request) { |
|
| 201 | - $current_page_permalink = site_url($wp->request); |
|
| 202 | - } else { |
|
| 203 | - $current_page_permalink = esc_url(site_url($_SERVER['REQUEST_URI'])); |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - return $current_page_permalink; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @return bool |
|
| 213 | - * @throws EE_Error |
|
| 214 | - * @throws ReflectionException |
|
| 215 | - */ |
|
| 216 | - public function test_for_espresso_page() |
|
| 217 | - { |
|
| 218 | - global $wp; |
|
| 219 | - /** @type EE_CPT_Strategy $EE_CPT_Strategy */ |
|
| 220 | - $EE_CPT_Strategy = EE_Registry::instance()->load_core('CPT_Strategy'); |
|
| 221 | - $espresso_CPT_taxonomies = $EE_CPT_Strategy->get_CPT_taxonomies(); |
|
| 222 | - if (is_array($espresso_CPT_taxonomies)) { |
|
| 223 | - foreach ($espresso_CPT_taxonomies as $espresso_CPT_taxonomy => $details) { |
|
| 224 | - if (isset($wp->query_vars, $wp->query_vars[$espresso_CPT_taxonomy])) { |
|
| 225 | - return true; |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - // load espresso CPT endpoints |
|
| 230 | - $espresso_CPT_endpoints = $EE_CPT_Strategy->get_CPT_endpoints(); |
|
| 231 | - $post_type_CPT_endpoints = array_flip($espresso_CPT_endpoints); |
|
| 232 | - $post_types = (array)$this->get('post_type'); |
|
| 233 | - foreach ($post_types as $post_type) { |
|
| 234 | - // was a post name passed ? |
|
| 235 | - if (isset($post_type_CPT_endpoints[$post_type])) { |
|
| 236 | - // kk we know this is an espresso page, but is it a specific post ? |
|
| 237 | - if (! $this->get('post_name')) { |
|
| 238 | - // there's no specific post name set, so maybe it's one of our endpoints like www.domain.com/events |
|
| 239 | - $post_name = isset($post_type_CPT_endpoints[$this->get('post_type')]) |
|
| 240 | - ? $post_type_CPT_endpoints[$this->get('post_type')] |
|
| 241 | - : ''; |
|
| 242 | - // if the post type matches on of our then set the endpoint |
|
| 243 | - if ($post_name) { |
|
| 244 | - $this->set('post_name', $post_name); |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - return true; |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - return false; |
|
| 251 | - } |
|
| 252 | - /** |
|
| 253 | - * @param $key |
|
| 254 | - * @param $value |
|
| 255 | - * @return void |
|
| 256 | - */ |
|
| 257 | - public function set_notice($key, $value) |
|
| 258 | - { |
|
| 259 | - $this->_notice[$key] = $value; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @param $key |
|
| 266 | - * @return mixed |
|
| 267 | - */ |
|
| 268 | - public function get_notice($key) |
|
| 269 | - { |
|
| 270 | - return isset($this->_notice[$key]) |
|
| 271 | - ? $this->_notice[$key] |
|
| 272 | - : null; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * @param $string |
|
| 279 | - * @return void |
|
| 280 | - */ |
|
| 281 | - public function add_output($string) |
|
| 282 | - { |
|
| 283 | - $this->_output .= $string; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * @return string |
|
| 290 | - */ |
|
| 291 | - public function get_output() |
|
| 292 | - { |
|
| 293 | - return $this->_output; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * @param $item |
|
| 300 | - * @param $key |
|
| 301 | - */ |
|
| 302 | - public function sanitize_text_field_for_array_walk(&$item, &$key) |
|
| 303 | - { |
|
| 304 | - $item = strpos($item, 'email') !== false |
|
| 305 | - ? sanitize_email($item) |
|
| 306 | - : sanitize_text_field($item); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * @param null|bool $value |
|
| 313 | - * @return void |
|
| 314 | - * @throws EE_Error |
|
| 315 | - * @throws ReflectionException |
|
| 316 | - */ |
|
| 317 | - public function set_espresso_page($value = null) |
|
| 318 | - { |
|
| 319 | - $this->request->set( |
|
| 320 | - 'is_espresso_page', |
|
| 321 | - ! empty($value) |
|
| 322 | - ? $value |
|
| 323 | - : $this->test_for_espresso_page() |
|
| 324 | - ); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * @return mixed |
|
| 331 | - */ |
|
| 332 | - public function is_espresso_page() |
|
| 333 | - { |
|
| 334 | - return $this->request->is_set('is_espresso_page'); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * returns contents of $_REQUEST |
|
| 341 | - * |
|
| 342 | - * @return array |
|
| 343 | - */ |
|
| 344 | - public function params() |
|
| 345 | - { |
|
| 346 | - return $this->request->params(); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * @param $key |
|
| 353 | - * @param $value |
|
| 354 | - * @param bool $override_ee |
|
| 355 | - * @return void |
|
| 356 | - */ |
|
| 357 | - public function set($key, $value, $override_ee = false) |
|
| 358 | - { |
|
| 359 | - $this->request->set($key, $value, $override_ee); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * @param $key |
|
| 366 | - * @param null $default |
|
| 367 | - * @return mixed |
|
| 368 | - */ |
|
| 369 | - public function get($key, $default = null) |
|
| 370 | - { |
|
| 371 | - return $this->request->get($key, $default); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * check if param exists |
|
| 378 | - * |
|
| 379 | - * @param $key |
|
| 380 | - * @return boolean |
|
| 381 | - */ |
|
| 382 | - public function is_set($key) |
|
| 383 | - { |
|
| 384 | - return $this->request->is_set($key); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * remove param |
|
| 391 | - * |
|
| 392 | - * @param $key |
|
| 393 | - * @return void |
|
| 394 | - */ |
|
| 395 | - public function un_set($key) |
|
| 396 | - { |
|
| 397 | - $this->request->un_set($key); |
|
| 398 | - } |
|
| 16 | + /** |
|
| 17 | + * @var EE_Request $request |
|
| 18 | + */ |
|
| 19 | + private $request; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var array $_notice |
|
| 23 | + */ |
|
| 24 | + private $_notice = array(); |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * rendered output to be returned to WP |
|
| 28 | + * |
|
| 29 | + * @var string $_output |
|
| 30 | + */ |
|
| 31 | + private $_output = ''; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * whether current request is via AJAX |
|
| 35 | + * |
|
| 36 | + * @var boolean $ajax |
|
| 37 | + */ |
|
| 38 | + public $ajax = false; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * whether current request is via AJAX from the frontend of the site |
|
| 42 | + * |
|
| 43 | + * @var boolean $front_ajax |
|
| 44 | + */ |
|
| 45 | + public $front_ajax = false; |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param EE_Request $request |
|
| 51 | + */ |
|
| 52 | + public function __construct(EE_Request $request) |
|
| 53 | + { |
|
| 54 | + $this->request = $request; |
|
| 55 | + $this->ajax = $this->request->ajax; |
|
| 56 | + $this->front_ajax = $this->request->front_ajax; |
|
| 57 | + do_action('AHEE__EE_Request_Handler__construct__complete'); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param WP $wp |
|
| 64 | + * @return void |
|
| 65 | + * @throws EE_Error |
|
| 66 | + * @throws ReflectionException |
|
| 67 | + */ |
|
| 68 | + public function parse_request($wp = null) |
|
| 69 | + { |
|
| 70 | + //if somebody forgot to provide us with WP, that's ok because its global |
|
| 71 | + if (! $wp instanceof WP) { |
|
| 72 | + global $wp; |
|
| 73 | + } |
|
| 74 | + $this->set_request_vars($wp); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param WP $wp |
|
| 81 | + * @return void |
|
| 82 | + * @throws EE_Error |
|
| 83 | + * @throws ReflectionException |
|
| 84 | + */ |
|
| 85 | + public function set_request_vars($wp = null) |
|
| 86 | + { |
|
| 87 | + if (! is_admin()) { |
|
| 88 | + // set request post_id |
|
| 89 | + $this->request->set('post_id', $this->get_post_id_from_request($wp)); |
|
| 90 | + // set request post name |
|
| 91 | + $this->request->set('post_name', $this->get_post_name_from_request($wp)); |
|
| 92 | + // set request post_type |
|
| 93 | + $this->request->set('post_type', $this->get_post_type_from_request($wp)); |
|
| 94 | + // true or false ? is this page being used by EE ? |
|
| 95 | + $this->set_espresso_page(); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param WP $wp |
|
| 103 | + * @return int |
|
| 104 | + */ |
|
| 105 | + public function get_post_id_from_request($wp = null) |
|
| 106 | + { |
|
| 107 | + if (! $wp instanceof WP) { |
|
| 108 | + global $wp; |
|
| 109 | + } |
|
| 110 | + $post_id = null; |
|
| 111 | + if (isset($wp->query_vars['p'])) { |
|
| 112 | + $post_id = $wp->query_vars['p']; |
|
| 113 | + } |
|
| 114 | + if (! $post_id && isset($wp->query_vars['page_id'])) { |
|
| 115 | + $post_id = $wp->query_vars['page_id']; |
|
| 116 | + } |
|
| 117 | + if (! $post_id && $wp->request !== null && is_numeric(basename($wp->request))) { |
|
| 118 | + $post_id = basename($wp->request); |
|
| 119 | + } |
|
| 120 | + return $post_id; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param WP $wp |
|
| 127 | + * @return string |
|
| 128 | + */ |
|
| 129 | + public function get_post_name_from_request($wp = null) |
|
| 130 | + { |
|
| 131 | + if (! $wp instanceof WP) { |
|
| 132 | + global $wp; |
|
| 133 | + } |
|
| 134 | + $post_name = null; |
|
| 135 | + if (isset($wp->query_vars['name']) && ! empty($wp->query_vars['name'])) { |
|
| 136 | + $post_name = $wp->query_vars['name']; |
|
| 137 | + } |
|
| 138 | + if (! $post_name && isset($wp->query_vars['pagename']) && ! empty($wp->query_vars['pagename'])) { |
|
| 139 | + $post_name = $wp->query_vars['pagename']; |
|
| 140 | + } |
|
| 141 | + if (! $post_name && $wp->request !== null && ! empty($wp->request)) { |
|
| 142 | + $possible_post_name = basename($wp->request); |
|
| 143 | + if (! is_numeric($possible_post_name)) { |
|
| 144 | + /** @type WPDB $wpdb */ |
|
| 145 | + global $wpdb; |
|
| 146 | + $SQL = |
|
| 147 | + "SELECT ID from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND post_name=%s"; |
|
| 148 | + $possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $possible_post_name)); |
|
| 149 | + if ($possible_post_name) { |
|
| 150 | + $post_name = $possible_post_name; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + if (! $post_name && $this->get('post_id')) { |
|
| 155 | + /** @type WPDB $wpdb */ |
|
| 156 | + global $wpdb; |
|
| 157 | + $SQL = |
|
| 158 | + "SELECT post_name from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND ID=%d"; |
|
| 159 | + $possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $this->get('post_id'))); |
|
| 160 | + if ($possible_post_name) { |
|
| 161 | + $post_name = $possible_post_name; |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + return $post_name; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @param WP $wp |
|
| 171 | + * @return mixed |
|
| 172 | + */ |
|
| 173 | + public function get_post_type_from_request($wp = null) |
|
| 174 | + { |
|
| 175 | + if (! $wp instanceof WP) { |
|
| 176 | + global $wp; |
|
| 177 | + } |
|
| 178 | + return isset($wp->query_vars['post_type']) |
|
| 179 | + ? $wp->query_vars['post_type'] |
|
| 180 | + : null; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Just a helper method for getting the url for the displayed page. |
|
| 187 | + * |
|
| 188 | + * @param WP $wp |
|
| 189 | + * @return string |
|
| 190 | + */ |
|
| 191 | + public function get_current_page_permalink($wp = null) |
|
| 192 | + { |
|
| 193 | + $post_id = $this->get_post_id_from_request($wp); |
|
| 194 | + if ($post_id) { |
|
| 195 | + $current_page_permalink = get_permalink($post_id); |
|
| 196 | + } else { |
|
| 197 | + if (! $wp instanceof WP) { |
|
| 198 | + global $wp; |
|
| 199 | + } |
|
| 200 | + if ($wp->request) { |
|
| 201 | + $current_page_permalink = site_url($wp->request); |
|
| 202 | + } else { |
|
| 203 | + $current_page_permalink = esc_url(site_url($_SERVER['REQUEST_URI'])); |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + return $current_page_permalink; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @return bool |
|
| 213 | + * @throws EE_Error |
|
| 214 | + * @throws ReflectionException |
|
| 215 | + */ |
|
| 216 | + public function test_for_espresso_page() |
|
| 217 | + { |
|
| 218 | + global $wp; |
|
| 219 | + /** @type EE_CPT_Strategy $EE_CPT_Strategy */ |
|
| 220 | + $EE_CPT_Strategy = EE_Registry::instance()->load_core('CPT_Strategy'); |
|
| 221 | + $espresso_CPT_taxonomies = $EE_CPT_Strategy->get_CPT_taxonomies(); |
|
| 222 | + if (is_array($espresso_CPT_taxonomies)) { |
|
| 223 | + foreach ($espresso_CPT_taxonomies as $espresso_CPT_taxonomy => $details) { |
|
| 224 | + if (isset($wp->query_vars, $wp->query_vars[$espresso_CPT_taxonomy])) { |
|
| 225 | + return true; |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + // load espresso CPT endpoints |
|
| 230 | + $espresso_CPT_endpoints = $EE_CPT_Strategy->get_CPT_endpoints(); |
|
| 231 | + $post_type_CPT_endpoints = array_flip($espresso_CPT_endpoints); |
|
| 232 | + $post_types = (array)$this->get('post_type'); |
|
| 233 | + foreach ($post_types as $post_type) { |
|
| 234 | + // was a post name passed ? |
|
| 235 | + if (isset($post_type_CPT_endpoints[$post_type])) { |
|
| 236 | + // kk we know this is an espresso page, but is it a specific post ? |
|
| 237 | + if (! $this->get('post_name')) { |
|
| 238 | + // there's no specific post name set, so maybe it's one of our endpoints like www.domain.com/events |
|
| 239 | + $post_name = isset($post_type_CPT_endpoints[$this->get('post_type')]) |
|
| 240 | + ? $post_type_CPT_endpoints[$this->get('post_type')] |
|
| 241 | + : ''; |
|
| 242 | + // if the post type matches on of our then set the endpoint |
|
| 243 | + if ($post_name) { |
|
| 244 | + $this->set('post_name', $post_name); |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + return true; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + return false; |
|
| 251 | + } |
|
| 252 | + /** |
|
| 253 | + * @param $key |
|
| 254 | + * @param $value |
|
| 255 | + * @return void |
|
| 256 | + */ |
|
| 257 | + public function set_notice($key, $value) |
|
| 258 | + { |
|
| 259 | + $this->_notice[$key] = $value; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @param $key |
|
| 266 | + * @return mixed |
|
| 267 | + */ |
|
| 268 | + public function get_notice($key) |
|
| 269 | + { |
|
| 270 | + return isset($this->_notice[$key]) |
|
| 271 | + ? $this->_notice[$key] |
|
| 272 | + : null; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * @param $string |
|
| 279 | + * @return void |
|
| 280 | + */ |
|
| 281 | + public function add_output($string) |
|
| 282 | + { |
|
| 283 | + $this->_output .= $string; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * @return string |
|
| 290 | + */ |
|
| 291 | + public function get_output() |
|
| 292 | + { |
|
| 293 | + return $this->_output; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * @param $item |
|
| 300 | + * @param $key |
|
| 301 | + */ |
|
| 302 | + public function sanitize_text_field_for_array_walk(&$item, &$key) |
|
| 303 | + { |
|
| 304 | + $item = strpos($item, 'email') !== false |
|
| 305 | + ? sanitize_email($item) |
|
| 306 | + : sanitize_text_field($item); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * @param null|bool $value |
|
| 313 | + * @return void |
|
| 314 | + * @throws EE_Error |
|
| 315 | + * @throws ReflectionException |
|
| 316 | + */ |
|
| 317 | + public function set_espresso_page($value = null) |
|
| 318 | + { |
|
| 319 | + $this->request->set( |
|
| 320 | + 'is_espresso_page', |
|
| 321 | + ! empty($value) |
|
| 322 | + ? $value |
|
| 323 | + : $this->test_for_espresso_page() |
|
| 324 | + ); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * @return mixed |
|
| 331 | + */ |
|
| 332 | + public function is_espresso_page() |
|
| 333 | + { |
|
| 334 | + return $this->request->is_set('is_espresso_page'); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * returns contents of $_REQUEST |
|
| 341 | + * |
|
| 342 | + * @return array |
|
| 343 | + */ |
|
| 344 | + public function params() |
|
| 345 | + { |
|
| 346 | + return $this->request->params(); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * @param $key |
|
| 353 | + * @param $value |
|
| 354 | + * @param bool $override_ee |
|
| 355 | + * @return void |
|
| 356 | + */ |
|
| 357 | + public function set($key, $value, $override_ee = false) |
|
| 358 | + { |
|
| 359 | + $this->request->set($key, $value, $override_ee); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * @param $key |
|
| 366 | + * @param null $default |
|
| 367 | + * @return mixed |
|
| 368 | + */ |
|
| 369 | + public function get($key, $default = null) |
|
| 370 | + { |
|
| 371 | + return $this->request->get($key, $default); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * check if param exists |
|
| 378 | + * |
|
| 379 | + * @param $key |
|
| 380 | + * @return boolean |
|
| 381 | + */ |
|
| 382 | + public function is_set($key) |
|
| 383 | + { |
|
| 384 | + return $this->request->is_set($key); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * remove param |
|
| 391 | + * |
|
| 392 | + * @param $key |
|
| 393 | + * @return void |
|
| 394 | + */ |
|
| 395 | + public function un_set($key) |
|
| 396 | + { |
|
| 397 | + $this->request->un_set($key); |
|
| 398 | + } |
|
| 399 | 399 | |
| 400 | 400 | |
| 401 | 401 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | public function parse_request($wp = null) |
| 69 | 69 | { |
| 70 | 70 | //if somebody forgot to provide us with WP, that's ok because its global |
| 71 | - if (! $wp instanceof WP) { |
|
| 71 | + if ( ! $wp instanceof WP) { |
|
| 72 | 72 | global $wp; |
| 73 | 73 | } |
| 74 | 74 | $this->set_request_vars($wp); |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | public function set_request_vars($wp = null) |
| 86 | 86 | { |
| 87 | - if (! is_admin()) { |
|
| 87 | + if ( ! is_admin()) { |
|
| 88 | 88 | // set request post_id |
| 89 | 89 | $this->request->set('post_id', $this->get_post_id_from_request($wp)); |
| 90 | 90 | // set request post name |
@@ -104,17 +104,17 @@ discard block |
||
| 104 | 104 | */ |
| 105 | 105 | public function get_post_id_from_request($wp = null) |
| 106 | 106 | { |
| 107 | - if (! $wp instanceof WP) { |
|
| 107 | + if ( ! $wp instanceof WP) { |
|
| 108 | 108 | global $wp; |
| 109 | 109 | } |
| 110 | 110 | $post_id = null; |
| 111 | 111 | if (isset($wp->query_vars['p'])) { |
| 112 | 112 | $post_id = $wp->query_vars['p']; |
| 113 | 113 | } |
| 114 | - if (! $post_id && isset($wp->query_vars['page_id'])) { |
|
| 114 | + if ( ! $post_id && isset($wp->query_vars['page_id'])) { |
|
| 115 | 115 | $post_id = $wp->query_vars['page_id']; |
| 116 | 116 | } |
| 117 | - if (! $post_id && $wp->request !== null && is_numeric(basename($wp->request))) { |
|
| 117 | + if ( ! $post_id && $wp->request !== null && is_numeric(basename($wp->request))) { |
|
| 118 | 118 | $post_id = basename($wp->request); |
| 119 | 119 | } |
| 120 | 120 | return $post_id; |
@@ -128,19 +128,19 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | public function get_post_name_from_request($wp = null) |
| 130 | 130 | { |
| 131 | - if (! $wp instanceof WP) { |
|
| 131 | + if ( ! $wp instanceof WP) { |
|
| 132 | 132 | global $wp; |
| 133 | 133 | } |
| 134 | 134 | $post_name = null; |
| 135 | 135 | if (isset($wp->query_vars['name']) && ! empty($wp->query_vars['name'])) { |
| 136 | 136 | $post_name = $wp->query_vars['name']; |
| 137 | 137 | } |
| 138 | - if (! $post_name && isset($wp->query_vars['pagename']) && ! empty($wp->query_vars['pagename'])) { |
|
| 138 | + if ( ! $post_name && isset($wp->query_vars['pagename']) && ! empty($wp->query_vars['pagename'])) { |
|
| 139 | 139 | $post_name = $wp->query_vars['pagename']; |
| 140 | 140 | } |
| 141 | - if (! $post_name && $wp->request !== null && ! empty($wp->request)) { |
|
| 141 | + if ( ! $post_name && $wp->request !== null && ! empty($wp->request)) { |
|
| 142 | 142 | $possible_post_name = basename($wp->request); |
| 143 | - if (! is_numeric($possible_post_name)) { |
|
| 143 | + if ( ! is_numeric($possible_post_name)) { |
|
| 144 | 144 | /** @type WPDB $wpdb */ |
| 145 | 145 | global $wpdb; |
| 146 | 146 | $SQL = |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | } |
| 152 | 152 | } |
| 153 | 153 | } |
| 154 | - if (! $post_name && $this->get('post_id')) { |
|
| 154 | + if ( ! $post_name && $this->get('post_id')) { |
|
| 155 | 155 | /** @type WPDB $wpdb */ |
| 156 | 156 | global $wpdb; |
| 157 | 157 | $SQL = |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | */ |
| 173 | 173 | public function get_post_type_from_request($wp = null) |
| 174 | 174 | { |
| 175 | - if (! $wp instanceof WP) { |
|
| 175 | + if ( ! $wp instanceof WP) { |
|
| 176 | 176 | global $wp; |
| 177 | 177 | } |
| 178 | 178 | return isset($wp->query_vars['post_type']) |
@@ -194,7 +194,7 @@ discard block |
||
| 194 | 194 | if ($post_id) { |
| 195 | 195 | $current_page_permalink = get_permalink($post_id); |
| 196 | 196 | } else { |
| 197 | - if (! $wp instanceof WP) { |
|
| 197 | + if ( ! $wp instanceof WP) { |
|
| 198 | 198 | global $wp; |
| 199 | 199 | } |
| 200 | 200 | if ($wp->request) { |
@@ -229,12 +229,12 @@ discard block |
||
| 229 | 229 | // load espresso CPT endpoints |
| 230 | 230 | $espresso_CPT_endpoints = $EE_CPT_Strategy->get_CPT_endpoints(); |
| 231 | 231 | $post_type_CPT_endpoints = array_flip($espresso_CPT_endpoints); |
| 232 | - $post_types = (array)$this->get('post_type'); |
|
| 232 | + $post_types = (array) $this->get('post_type'); |
|
| 233 | 233 | foreach ($post_types as $post_type) { |
| 234 | 234 | // was a post name passed ? |
| 235 | 235 | if (isset($post_type_CPT_endpoints[$post_type])) { |
| 236 | 236 | // kk we know this is an espresso page, but is it a specific post ? |
| 237 | - if (! $this->get('post_name')) { |
|
| 237 | + if ( ! $this->get('post_name')) { |
|
| 238 | 238 | // there's no specific post name set, so maybe it's one of our endpoints like www.domain.com/events |
| 239 | 239 | $post_name = isset($post_type_CPT_endpoints[$this->get('post_type')]) |
| 240 | 240 | ? $post_type_CPT_endpoints[$this->get('post_type')] |
@@ -17,248 +17,248 @@ |
||
| 17 | 17 | final class EE_Module_Request_Router implements InterminableInterface |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var EE_Request $request |
|
| 22 | - */ |
|
| 23 | - private $request; |
|
| 20 | + /** |
|
| 21 | + * @var EE_Request $request |
|
| 22 | + */ |
|
| 23 | + private $request; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var array $_previous_routes |
|
| 27 | - */ |
|
| 28 | - private static $_previous_routes = array(); |
|
| 25 | + /** |
|
| 26 | + * @var array $_previous_routes |
|
| 27 | + */ |
|
| 28 | + private static $_previous_routes = array(); |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var WP_Query $WP_Query |
|
| 32 | - */ |
|
| 33 | - public $WP_Query; |
|
| 30 | + /** |
|
| 31 | + * @var WP_Query $WP_Query |
|
| 32 | + */ |
|
| 33 | + public $WP_Query; |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * EE_Module_Request_Router constructor. |
|
| 39 | - * |
|
| 40 | - * @param EE_Request $request |
|
| 41 | - */ |
|
| 42 | - public function __construct(EE_Request $request) |
|
| 43 | - { |
|
| 44 | - $this->request = $request; |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * EE_Module_Request_Router constructor. |
|
| 39 | + * |
|
| 40 | + * @param EE_Request $request |
|
| 41 | + */ |
|
| 42 | + public function __construct(EE_Request $request) |
|
| 43 | + { |
|
| 44 | + $this->request = $request; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * on the first call to this method, it checks the EE_Request_Handler for a "route" |
|
| 51 | - * on subsequent calls to this method, |
|
| 52 | - * instead of checking the EE_Request_Handler for a route, it checks the previous routes array, |
|
| 53 | - * and checks if the last called route has any forwarding routes registered for it |
|
| 54 | - * |
|
| 55 | - * @param WP_Query $WP_Query |
|
| 56 | - * @return NULL|string |
|
| 57 | - * @throws EE_Error |
|
| 58 | - * @throws ReflectionException |
|
| 59 | - */ |
|
| 60 | - public function get_route(WP_Query $WP_Query) |
|
| 61 | - { |
|
| 62 | - $this->WP_Query = $WP_Query; |
|
| 63 | - // assume this if first route being called |
|
| 64 | - $previous_route = false; |
|
| 65 | - // but is it really ??? |
|
| 66 | - if (! empty(self::$_previous_routes)) { |
|
| 67 | - // get last run route |
|
| 68 | - $previous_routes = array_values(self::$_previous_routes); |
|
| 69 | - $previous_route = array_pop($previous_routes); |
|
| 70 | - } |
|
| 71 | - // has another route already been run ? |
|
| 72 | - if ($previous_route) { |
|
| 73 | - // check if forwarding has been set |
|
| 74 | - $current_route = $this->get_forward($previous_route); |
|
| 75 | - try { |
|
| 76 | - //check for recursive forwarding |
|
| 77 | - if (isset(self::$_previous_routes[$current_route])) { |
|
| 78 | - throw new EE_Error( |
|
| 79 | - sprintf( |
|
| 80 | - __( |
|
| 81 | - 'An error occurred. The %s route has already been called, and therefore can not be forwarded to, because an infinite loop would be created and break the interweb.', |
|
| 82 | - 'event_espresso' |
|
| 83 | - ), |
|
| 84 | - $current_route |
|
| 85 | - ) |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - } catch (EE_Error $e) { |
|
| 89 | - $e->get_error(); |
|
| 90 | - return null; |
|
| 91 | - } |
|
| 92 | - } else { |
|
| 93 | - // first route called |
|
| 94 | - $current_route = null; |
|
| 95 | - // grab all routes |
|
| 96 | - $routes = EE_Config::get_routes(); |
|
| 97 | - //d( $routes ); |
|
| 98 | - foreach ($routes as $key => $route) { |
|
| 99 | - // check request for module route |
|
| 100 | - if ($this->request->is_set($key)) { |
|
| 101 | - $current_route = sanitize_text_field($this->request->get($key)); |
|
| 102 | - if ($current_route) { |
|
| 103 | - $current_route = array($key, $current_route); |
|
| 104 | - break; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - // sorry, but I can't read what you route ! |
|
| 110 | - if (empty($current_route)) { |
|
| 111 | - return null; |
|
| 112 | - } |
|
| 113 | - //add route to previous routes array |
|
| 114 | - self::$_previous_routes[] = $current_route; |
|
| 115 | - return $current_route; |
|
| 116 | - } |
|
| 49 | + /** |
|
| 50 | + * on the first call to this method, it checks the EE_Request_Handler for a "route" |
|
| 51 | + * on subsequent calls to this method, |
|
| 52 | + * instead of checking the EE_Request_Handler for a route, it checks the previous routes array, |
|
| 53 | + * and checks if the last called route has any forwarding routes registered for it |
|
| 54 | + * |
|
| 55 | + * @param WP_Query $WP_Query |
|
| 56 | + * @return NULL|string |
|
| 57 | + * @throws EE_Error |
|
| 58 | + * @throws ReflectionException |
|
| 59 | + */ |
|
| 60 | + public function get_route(WP_Query $WP_Query) |
|
| 61 | + { |
|
| 62 | + $this->WP_Query = $WP_Query; |
|
| 63 | + // assume this if first route being called |
|
| 64 | + $previous_route = false; |
|
| 65 | + // but is it really ??? |
|
| 66 | + if (! empty(self::$_previous_routes)) { |
|
| 67 | + // get last run route |
|
| 68 | + $previous_routes = array_values(self::$_previous_routes); |
|
| 69 | + $previous_route = array_pop($previous_routes); |
|
| 70 | + } |
|
| 71 | + // has another route already been run ? |
|
| 72 | + if ($previous_route) { |
|
| 73 | + // check if forwarding has been set |
|
| 74 | + $current_route = $this->get_forward($previous_route); |
|
| 75 | + try { |
|
| 76 | + //check for recursive forwarding |
|
| 77 | + if (isset(self::$_previous_routes[$current_route])) { |
|
| 78 | + throw new EE_Error( |
|
| 79 | + sprintf( |
|
| 80 | + __( |
|
| 81 | + 'An error occurred. The %s route has already been called, and therefore can not be forwarded to, because an infinite loop would be created and break the interweb.', |
|
| 82 | + 'event_espresso' |
|
| 83 | + ), |
|
| 84 | + $current_route |
|
| 85 | + ) |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + } catch (EE_Error $e) { |
|
| 89 | + $e->get_error(); |
|
| 90 | + return null; |
|
| 91 | + } |
|
| 92 | + } else { |
|
| 93 | + // first route called |
|
| 94 | + $current_route = null; |
|
| 95 | + // grab all routes |
|
| 96 | + $routes = EE_Config::get_routes(); |
|
| 97 | + //d( $routes ); |
|
| 98 | + foreach ($routes as $key => $route) { |
|
| 99 | + // check request for module route |
|
| 100 | + if ($this->request->is_set($key)) { |
|
| 101 | + $current_route = sanitize_text_field($this->request->get($key)); |
|
| 102 | + if ($current_route) { |
|
| 103 | + $current_route = array($key, $current_route); |
|
| 104 | + break; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + // sorry, but I can't read what you route ! |
|
| 110 | + if (empty($current_route)) { |
|
| 111 | + return null; |
|
| 112 | + } |
|
| 113 | + //add route to previous routes array |
|
| 114 | + self::$_previous_routes[] = $current_route; |
|
| 115 | + return $current_route; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | 118 | |
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * this method simply takes a valid route, and resolves what module class method the route points to |
|
| 122 | - * |
|
| 123 | - * @param string $key |
|
| 124 | - * @param string $current_route |
|
| 125 | - * @return mixed EED_Module | boolean |
|
| 126 | - * @throws EE_Error |
|
| 127 | - * @throws ReflectionException |
|
| 128 | - */ |
|
| 129 | - public function resolve_route($key, $current_route) |
|
| 130 | - { |
|
| 131 | - // get module method that route has been mapped to |
|
| 132 | - $module_method = EE_Config::get_route($current_route, $key); |
|
| 133 | - // verify result was returned |
|
| 134 | - if (empty($module_method)) { |
|
| 135 | - $msg = sprintf( |
|
| 136 | - __('The requested route %s could not be mapped to any registered modules.', 'event_espresso'), |
|
| 137 | - $current_route |
|
| 138 | - ); |
|
| 139 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - // verify that result is an array |
|
| 143 | - if (! is_array($module_method)) { |
|
| 144 | - $msg = sprintf(__('The %s route has not been properly registered.', 'event_espresso'), $current_route); |
|
| 145 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - // grab module name |
|
| 149 | - $module_name = $module_method[0]; |
|
| 150 | - // verify that a class method was registered properly |
|
| 151 | - if (! isset($module_method[1])) { |
|
| 152 | - $msg = sprintf( |
|
| 153 | - __('A class method for the %s route has not been properly registered.', 'event_espresso'), |
|
| 154 | - $current_route |
|
| 155 | - ); |
|
| 156 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 157 | - return false; |
|
| 158 | - } |
|
| 159 | - // grab method |
|
| 160 | - $method = $module_method[1]; |
|
| 161 | - // verify that class exists |
|
| 162 | - if (! class_exists($module_name)) { |
|
| 163 | - $msg = sprintf(__('The requested %s class could not be found.', 'event_espresso'), $module_name); |
|
| 164 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 165 | - return false; |
|
| 166 | - } |
|
| 167 | - // verify that method exists |
|
| 168 | - if (! method_exists($module_name, $method)) { |
|
| 169 | - $msg = sprintf( |
|
| 170 | - __('The class method %s for the %s route is in invalid.', 'event_espresso'), $method, $current_route |
|
| 171 | - ); |
|
| 172 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 173 | - return false; |
|
| 174 | - } |
|
| 175 | - // instantiate module and call route method |
|
| 176 | - return $this->_module_router($module_name, $method); |
|
| 177 | - } |
|
| 120 | + /** |
|
| 121 | + * this method simply takes a valid route, and resolves what module class method the route points to |
|
| 122 | + * |
|
| 123 | + * @param string $key |
|
| 124 | + * @param string $current_route |
|
| 125 | + * @return mixed EED_Module | boolean |
|
| 126 | + * @throws EE_Error |
|
| 127 | + * @throws ReflectionException |
|
| 128 | + */ |
|
| 129 | + public function resolve_route($key, $current_route) |
|
| 130 | + { |
|
| 131 | + // get module method that route has been mapped to |
|
| 132 | + $module_method = EE_Config::get_route($current_route, $key); |
|
| 133 | + // verify result was returned |
|
| 134 | + if (empty($module_method)) { |
|
| 135 | + $msg = sprintf( |
|
| 136 | + __('The requested route %s could not be mapped to any registered modules.', 'event_espresso'), |
|
| 137 | + $current_route |
|
| 138 | + ); |
|
| 139 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + // verify that result is an array |
|
| 143 | + if (! is_array($module_method)) { |
|
| 144 | + $msg = sprintf(__('The %s route has not been properly registered.', 'event_espresso'), $current_route); |
|
| 145 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + // grab module name |
|
| 149 | + $module_name = $module_method[0]; |
|
| 150 | + // verify that a class method was registered properly |
|
| 151 | + if (! isset($module_method[1])) { |
|
| 152 | + $msg = sprintf( |
|
| 153 | + __('A class method for the %s route has not been properly registered.', 'event_espresso'), |
|
| 154 | + $current_route |
|
| 155 | + ); |
|
| 156 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 157 | + return false; |
|
| 158 | + } |
|
| 159 | + // grab method |
|
| 160 | + $method = $module_method[1]; |
|
| 161 | + // verify that class exists |
|
| 162 | + if (! class_exists($module_name)) { |
|
| 163 | + $msg = sprintf(__('The requested %s class could not be found.', 'event_espresso'), $module_name); |
|
| 164 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 165 | + return false; |
|
| 166 | + } |
|
| 167 | + // verify that method exists |
|
| 168 | + if (! method_exists($module_name, $method)) { |
|
| 169 | + $msg = sprintf( |
|
| 170 | + __('The class method %s for the %s route is in invalid.', 'event_espresso'), $method, $current_route |
|
| 171 | + ); |
|
| 172 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 173 | + return false; |
|
| 174 | + } |
|
| 175 | + // instantiate module and call route method |
|
| 176 | + return $this->_module_router($module_name, $method); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | 179 | |
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * this method instantiates modules and calls the method that was defined when the route was registered |
|
| 183 | - * |
|
| 184 | - * @param string $module_name |
|
| 185 | - * @return EED_Module|object|null |
|
| 186 | - * @throws ReflectionException |
|
| 187 | - */ |
|
| 188 | - public static function module_factory($module_name) |
|
| 189 | - { |
|
| 190 | - if ($module_name === 'EED_Module') { |
|
| 191 | - EE_Error::add_error( |
|
| 192 | - sprintf( |
|
| 193 | - __( |
|
| 194 | - 'EED_Module is an abstract parent class an can not be instantiated. Please provide a proper module name.', |
|
| 195 | - 'event_espresso' |
|
| 196 | - ), $module_name |
|
| 197 | - ), __FILE__, __FUNCTION__, __LINE__ |
|
| 198 | - ); |
|
| 199 | - return null; |
|
| 200 | - } |
|
| 201 | - // instantiate module class |
|
| 202 | - $module = new $module_name(); |
|
| 203 | - // ensure that class is actually a module |
|
| 204 | - if (! $module instanceof EED_Module) { |
|
| 205 | - EE_Error::add_error( |
|
| 206 | - sprintf(__('The requested %s module is not of the class EED_Module.', 'event_espresso'), $module_name), |
|
| 207 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 208 | - ); |
|
| 209 | - return null; |
|
| 210 | - } |
|
| 211 | - return $module; |
|
| 212 | - } |
|
| 181 | + /** |
|
| 182 | + * this method instantiates modules and calls the method that was defined when the route was registered |
|
| 183 | + * |
|
| 184 | + * @param string $module_name |
|
| 185 | + * @return EED_Module|object|null |
|
| 186 | + * @throws ReflectionException |
|
| 187 | + */ |
|
| 188 | + public static function module_factory($module_name) |
|
| 189 | + { |
|
| 190 | + if ($module_name === 'EED_Module') { |
|
| 191 | + EE_Error::add_error( |
|
| 192 | + sprintf( |
|
| 193 | + __( |
|
| 194 | + 'EED_Module is an abstract parent class an can not be instantiated. Please provide a proper module name.', |
|
| 195 | + 'event_espresso' |
|
| 196 | + ), $module_name |
|
| 197 | + ), __FILE__, __FUNCTION__, __LINE__ |
|
| 198 | + ); |
|
| 199 | + return null; |
|
| 200 | + } |
|
| 201 | + // instantiate module class |
|
| 202 | + $module = new $module_name(); |
|
| 203 | + // ensure that class is actually a module |
|
| 204 | + if (! $module instanceof EED_Module) { |
|
| 205 | + EE_Error::add_error( |
|
| 206 | + sprintf(__('The requested %s module is not of the class EED_Module.', 'event_espresso'), $module_name), |
|
| 207 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 208 | + ); |
|
| 209 | + return null; |
|
| 210 | + } |
|
| 211 | + return $module; |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | 214 | |
| 215 | 215 | |
| 216 | - /** |
|
| 217 | - * this method instantiates modules and calls the method that was defined when the route was registered |
|
| 218 | - * |
|
| 219 | - * @param string $module_name |
|
| 220 | - * @param string $method |
|
| 221 | - * @return EED_Module|null |
|
| 222 | - * @throws EE_Error |
|
| 223 | - * @throws ReflectionException |
|
| 224 | - */ |
|
| 225 | - private function _module_router($module_name, $method) |
|
| 226 | - { |
|
| 227 | - // instantiate module class |
|
| 228 | - $module = EE_Module_Request_Router::module_factory($module_name); |
|
| 229 | - if ($module instanceof EED_Module) { |
|
| 230 | - // and call whatever action the route was for |
|
| 231 | - try { |
|
| 232 | - call_user_func(array($module, $method), $this->WP_Query); |
|
| 233 | - } catch (EE_Error $e) { |
|
| 234 | - $e->get_error(); |
|
| 235 | - return null; |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - return $module; |
|
| 239 | - } |
|
| 216 | + /** |
|
| 217 | + * this method instantiates modules and calls the method that was defined when the route was registered |
|
| 218 | + * |
|
| 219 | + * @param string $module_name |
|
| 220 | + * @param string $method |
|
| 221 | + * @return EED_Module|null |
|
| 222 | + * @throws EE_Error |
|
| 223 | + * @throws ReflectionException |
|
| 224 | + */ |
|
| 225 | + private function _module_router($module_name, $method) |
|
| 226 | + { |
|
| 227 | + // instantiate module class |
|
| 228 | + $module = EE_Module_Request_Router::module_factory($module_name); |
|
| 229 | + if ($module instanceof EED_Module) { |
|
| 230 | + // and call whatever action the route was for |
|
| 231 | + try { |
|
| 232 | + call_user_func(array($module, $method), $this->WP_Query); |
|
| 233 | + } catch (EE_Error $e) { |
|
| 234 | + $e->get_error(); |
|
| 235 | + return null; |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + return $module; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | 241 | |
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * @param $current_route |
|
| 245 | - * @return string |
|
| 246 | - */ |
|
| 247 | - public function get_forward($current_route) |
|
| 248 | - { |
|
| 249 | - return EE_Config::get_forward($current_route); |
|
| 250 | - } |
|
| 243 | + /** |
|
| 244 | + * @param $current_route |
|
| 245 | + * @return string |
|
| 246 | + */ |
|
| 247 | + public function get_forward($current_route) |
|
| 248 | + { |
|
| 249 | + return EE_Config::get_forward($current_route); |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | 252 | |
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * @param $current_route |
|
| 256 | - * @return string |
|
| 257 | - */ |
|
| 258 | - public function get_view($current_route) |
|
| 259 | - { |
|
| 260 | - return EE_Config::get_view($current_route); |
|
| 261 | - } |
|
| 254 | + /** |
|
| 255 | + * @param $current_route |
|
| 256 | + * @return string |
|
| 257 | + */ |
|
| 258 | + public function get_view($current_route) |
|
| 259 | + { |
|
| 260 | + return EE_Config::get_view($current_route); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | 263 | |
| 264 | 264 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 2 | - exit('No direct script access allowed'); |
|
| 2 | + exit('No direct script access allowed'); |
|
| 3 | 3 | } |
| 4 | 4 | do_action('AHEE_log', __FILE__, ' FILE LOADED', ''); |
| 5 | 5 | |
@@ -25,2714 +25,2714 @@ discard block |
||
| 25 | 25 | abstract class EE_Base_Class |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * This is an array of the original properties and values provided during construction |
|
| 30 | - * of this model object. (keys are model field names, values are their values). |
|
| 31 | - * This list is important to remember so that when we are merging data from the db, we know |
|
| 32 | - * which values to override and which to not override. |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - protected $_props_n_values_provided_in_constructor; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Timezone |
|
| 40 | - * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 41 | - * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 42 | - * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 43 | - * access to it. |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - protected $_timezone; |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * date format |
|
| 53 | - * pattern or format for displaying dates |
|
| 54 | - * |
|
| 55 | - * @var string $_dt_frmt |
|
| 56 | - */ |
|
| 57 | - protected $_dt_frmt; |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * time format |
|
| 63 | - * pattern or format for displaying time |
|
| 64 | - * |
|
| 65 | - * @var string $_tm_frmt |
|
| 66 | - */ |
|
| 67 | - protected $_tm_frmt; |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 73 | - * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 74 | - * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 75 | - * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 76 | - * |
|
| 77 | - * @var array |
|
| 78 | - */ |
|
| 79 | - protected $_cached_properties = array(); |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 83 | - * single |
|
| 84 | - * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 85 | - * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 86 | - * all others have an array) |
|
| 87 | - * |
|
| 88 | - * @var array |
|
| 89 | - */ |
|
| 90 | - protected $_model_relations = array(); |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 94 | - * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 95 | - * |
|
| 96 | - * @var array |
|
| 97 | - */ |
|
| 98 | - protected $_fields = array(); |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 102 | - * For example, we might create model objects intended to only be used for the duration |
|
| 103 | - * of this request and to be thrown away, and if they were accidentally saved |
|
| 104 | - * it would be a bug. |
|
| 105 | - */ |
|
| 106 | - protected $_allow_persist = true; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 110 | - */ |
|
| 111 | - protected $_has_changes = false; |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @var EEM_Base |
|
| 115 | - */ |
|
| 116 | - protected $_model; |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 122 | - * play nice |
|
| 123 | - * |
|
| 124 | - * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 125 | - * layer of the model's _fields array, (eg, EVT_ID, |
|
| 126 | - * TXN_amount, QST_name, etc) and values are their values |
|
| 127 | - * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 128 | - * corresponding db model or not. |
|
| 129 | - * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 130 | - * be in when instantiating a EE_Base_Class object. |
|
| 131 | - * @param array $date_formats An array of date formats to set on construct where first |
|
| 132 | - * value is the date_format and second value is the time |
|
| 133 | - * format. |
|
| 134 | - * @throws EE_Error |
|
| 135 | - */ |
|
| 136 | - protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 137 | - { |
|
| 138 | - $className = get_class($this); |
|
| 139 | - do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 140 | - $model = $this->get_model(); |
|
| 141 | - $model_fields = $model->field_settings(false); |
|
| 142 | - // ensure $fieldValues is an array |
|
| 143 | - $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 144 | - // EEH_Debug_Tools::printr( $fieldValues, '$fieldValues <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 145 | - // verify client code has not passed any invalid field names |
|
| 146 | - foreach ($fieldValues as $field_name => $field_value) { |
|
| 147 | - if ( ! isset($model_fields[$field_name])) { |
|
| 148 | - throw new EE_Error(sprintf(__("Invalid field (%s) passed to constructor of %s. Allowed fields are :%s", |
|
| 149 | - "event_espresso"), $field_name, get_class($this), implode(", ", array_keys($model_fields)))); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - // EEH_Debug_Tools::printr( $model_fields, '$model_fields <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 153 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 154 | - if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 155 | - list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 156 | - } else { |
|
| 157 | - //set default formats for date and time |
|
| 158 | - $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d'); |
|
| 159 | - $this->_tm_frmt = (string)get_option('time_format', 'g:i a'); |
|
| 160 | - } |
|
| 161 | - //if db model is instantiating |
|
| 162 | - if ($bydb) { |
|
| 163 | - //client code has indicated these field values are from the database |
|
| 164 | - foreach ($model_fields as $fieldName => $field) { |
|
| 165 | - $this->set_from_db($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null); |
|
| 166 | - } |
|
| 167 | - } else { |
|
| 168 | - //we're constructing a brand |
|
| 169 | - //new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 170 | - foreach ($model_fields as $fieldName => $field) { |
|
| 171 | - $this->set($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, true); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - //remember what values were passed to this constructor |
|
| 175 | - $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 176 | - //remember in entity mapper |
|
| 177 | - if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 178 | - $model->add_to_entity_map($this); |
|
| 179 | - } |
|
| 180 | - //setup all the relations |
|
| 181 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 182 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 183 | - $this->_model_relations[$relation_name] = null; |
|
| 184 | - } else { |
|
| 185 | - $this->_model_relations[$relation_name] = array(); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - /** |
|
| 189 | - * Action done at the end of each model object construction |
|
| 190 | - * |
|
| 191 | - * @param EE_Base_Class $this the model object just created |
|
| 192 | - */ |
|
| 193 | - do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 200 | - * |
|
| 201 | - * @return boolean |
|
| 202 | - */ |
|
| 203 | - public function allow_persist() |
|
| 204 | - { |
|
| 205 | - return $this->_allow_persist; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 212 | - * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 213 | - * you got new information that somehow made you change your mind. |
|
| 214 | - * |
|
| 215 | - * @param boolean $allow_persist |
|
| 216 | - * @return boolean |
|
| 217 | - */ |
|
| 218 | - public function set_allow_persist($allow_persist) |
|
| 219 | - { |
|
| 220 | - return $this->_allow_persist = $allow_persist; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Gets the field's original value when this object was constructed during this request. |
|
| 227 | - * This can be helpful when determining if a model object has changed or not |
|
| 228 | - * |
|
| 229 | - * @param string $field_name |
|
| 230 | - * @return mixed|null |
|
| 231 | - * @throws \EE_Error |
|
| 232 | - */ |
|
| 233 | - public function get_original($field_name) |
|
| 234 | - { |
|
| 235 | - if (isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
| 236 | - && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 237 | - ) { |
|
| 238 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
| 239 | - } else { |
|
| 240 | - return null; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param EE_Base_Class $obj |
|
| 248 | - * @return string |
|
| 249 | - */ |
|
| 250 | - public function get_class($obj) |
|
| 251 | - { |
|
| 252 | - return get_class($obj); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Overrides parent because parent expects old models. |
|
| 259 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 260 | - * |
|
| 261 | - * @param string $field_name |
|
| 262 | - * @param mixed $field_value |
|
| 263 | - * @param bool $use_default |
|
| 264 | - * @throws \EE_Error |
|
| 265 | - */ |
|
| 266 | - public function set($field_name, $field_value, $use_default = false) |
|
| 267 | - { |
|
| 268 | - // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 269 | - // then don't do anything |
|
| 270 | - if ( |
|
| 271 | - ! $use_default |
|
| 272 | - && $this->_fields[$field_name] === $field_value |
|
| 273 | - && $this->ID() |
|
| 274 | - ) { |
|
| 275 | - return; |
|
| 276 | - } |
|
| 277 | - $model = $this->get_model(); |
|
| 278 | - $this->_has_changes = true; |
|
| 279 | - $field_obj = $model->field_settings_for($field_name); |
|
| 280 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 281 | - // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 282 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 283 | - $field_obj->set_timezone($this->_timezone); |
|
| 284 | - $field_obj->set_date_format($this->_dt_frmt); |
|
| 285 | - $field_obj->set_time_format($this->_tm_frmt); |
|
| 286 | - } |
|
| 287 | - $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 288 | - //should the value be null? |
|
| 289 | - if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 290 | - $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
| 291 | - /** |
|
| 292 | - * To save having to refactor all the models, if a default value is used for a |
|
| 293 | - * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 294 | - * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 295 | - * object. |
|
| 296 | - * |
|
| 297 | - * @since 4.6.10+ |
|
| 298 | - */ |
|
| 299 | - if ( |
|
| 300 | - $field_obj instanceof EE_Datetime_Field |
|
| 301 | - && $this->_fields[$field_name] !== null |
|
| 302 | - && ! $this->_fields[$field_name] instanceof DateTime |
|
| 303 | - ) { |
|
| 304 | - empty($this->_fields[$field_name]) |
|
| 305 | - ? $this->set($field_name, time()) |
|
| 306 | - : $this->set($field_name, $this->_fields[$field_name]); |
|
| 307 | - } |
|
| 308 | - } else { |
|
| 309 | - $this->_fields[$field_name] = $holder_of_value; |
|
| 310 | - } |
|
| 311 | - //if we're not in the constructor... |
|
| 312 | - //now check if what we set was a primary key |
|
| 313 | - if ( |
|
| 314 | - //note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 315 | - $this->_props_n_values_provided_in_constructor |
|
| 316 | - && $field_value |
|
| 317 | - && $field_name === $model->primary_key_name() |
|
| 318 | - ) { |
|
| 319 | - //if so, we want all this object's fields to be filled either with |
|
| 320 | - //what we've explicitly set on this model |
|
| 321 | - //or what we have in the db |
|
| 322 | - // echo "setting primary key!"; |
|
| 323 | - $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 324 | - $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 325 | - foreach ($fields_on_model as $field_obj) { |
|
| 326 | - if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 327 | - && $field_obj->get_name() !== $field_name |
|
| 328 | - ) { |
|
| 329 | - $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - //oh this model object has an ID? well make sure its in the entity mapper |
|
| 333 | - $model->add_to_entity_map($this); |
|
| 334 | - } |
|
| 335 | - //let's unset any cache for this field_name from the $_cached_properties property. |
|
| 336 | - $this->_clear_cached_property($field_name); |
|
| 337 | - } else { |
|
| 338 | - throw new EE_Error(sprintf(__("A valid EE_Model_Field_Base could not be found for the given field name: %s", |
|
| 339 | - "event_espresso"), $field_name)); |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * This sets the field value on the db column if it exists for the given $column_name or |
|
| 347 | - * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 348 | - * |
|
| 349 | - * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 350 | - * @param string $field_name Must be the exact column name. |
|
| 351 | - * @param mixed $field_value The value to set. |
|
| 352 | - * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 353 | - * @throws \EE_Error |
|
| 354 | - */ |
|
| 355 | - public function set_field_or_extra_meta($field_name, $field_value) |
|
| 356 | - { |
|
| 357 | - if ($this->get_model()->has_field($field_name)) { |
|
| 358 | - $this->set($field_name, $field_value); |
|
| 359 | - return true; |
|
| 360 | - } else { |
|
| 361 | - //ensure this object is saved first so that extra meta can be properly related. |
|
| 362 | - $this->save(); |
|
| 363 | - return $this->update_extra_meta($field_name, $field_value); |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * This retrieves the value of the db column set on this class or if that's not present |
|
| 371 | - * it will attempt to retrieve from extra_meta if found. |
|
| 372 | - * Example Usage: |
|
| 373 | - * Via EE_Message child class: |
|
| 374 | - * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 375 | - * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 376 | - * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 377 | - * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 378 | - * value for those extra fields dynamically via the EE_message object. |
|
| 379 | - * |
|
| 380 | - * @param string $field_name expecting the fully qualified field name. |
|
| 381 | - * @return mixed|null value for the field if found. null if not found. |
|
| 382 | - * @throws \EE_Error |
|
| 383 | - */ |
|
| 384 | - public function get_field_or_extra_meta($field_name) |
|
| 385 | - { |
|
| 386 | - if ($this->get_model()->has_field($field_name)) { |
|
| 387 | - $column_value = $this->get($field_name); |
|
| 388 | - } else { |
|
| 389 | - //This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 390 | - $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 391 | - } |
|
| 392 | - return $column_value; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 399 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 400 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 401 | - * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 402 | - * |
|
| 403 | - * @access public |
|
| 404 | - * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 405 | - * @return void |
|
| 406 | - * @throws \EE_Error |
|
| 407 | - */ |
|
| 408 | - public function set_timezone($timezone = '') |
|
| 409 | - { |
|
| 410 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 411 | - //make sure we clear all cached properties because they won't be relevant now |
|
| 412 | - $this->_clear_cached_properties(); |
|
| 413 | - //make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 414 | - $model_fields = $this->get_model()->field_settings(false); |
|
| 415 | - foreach ($model_fields as $field_name => $field_obj) { |
|
| 416 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 417 | - $field_obj->set_timezone($this->_timezone); |
|
| 418 | - if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
| 419 | - $this->_fields[$field_name]->setTimezone(new DateTimeZone($this->_timezone)); |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * This just returns whatever is set for the current timezone. |
|
| 429 | - * |
|
| 430 | - * @access public |
|
| 431 | - * @return string timezone string |
|
| 432 | - */ |
|
| 433 | - public function get_timezone() |
|
| 434 | - { |
|
| 435 | - return $this->_timezone; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 442 | - * internally instead of wp set date format options |
|
| 443 | - * |
|
| 444 | - * @since 4.6 |
|
| 445 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 446 | - */ |
|
| 447 | - public function set_date_format($format) |
|
| 448 | - { |
|
| 449 | - $this->_dt_frmt = $format; |
|
| 450 | - //clear cached_properties because they won't be relevant now. |
|
| 451 | - $this->_clear_cached_properties(); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 458 | - * class internally instead of wp set time format options. |
|
| 459 | - * |
|
| 460 | - * @since 4.6 |
|
| 461 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 462 | - */ |
|
| 463 | - public function set_time_format($format) |
|
| 464 | - { |
|
| 465 | - $this->_tm_frmt = $format; |
|
| 466 | - //clear cached_properties because they won't be relevant now. |
|
| 467 | - $this->_clear_cached_properties(); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * This returns the current internal set format for the date and time formats. |
|
| 474 | - * |
|
| 475 | - * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 476 | - * where the first value is the date format and the second value is the time format. |
|
| 477 | - * @return mixed string|array |
|
| 478 | - */ |
|
| 479 | - public function get_format($full = true) |
|
| 480 | - { |
|
| 481 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * cache |
|
| 488 | - * stores the passed model object on the current model object. |
|
| 489 | - * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 490 | - * |
|
| 491 | - * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 492 | - * 'Registration' associated with this model object |
|
| 493 | - * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 494 | - * that could be a payment or a registration) |
|
| 495 | - * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 496 | - * items which will be stored in an array on this object |
|
| 497 | - * @throws EE_Error |
|
| 498 | - * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 499 | - * related thing, no array) |
|
| 500 | - */ |
|
| 501 | - public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 502 | - { |
|
| 503 | - // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 504 | - if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
| 505 | - return false; |
|
| 506 | - } |
|
| 507 | - // also get "how" the object is related, or throw an error |
|
| 508 | - if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 509 | - throw new EE_Error(sprintf(__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 510 | - $relationName, get_class($this))); |
|
| 511 | - } |
|
| 512 | - // how many things are related ? |
|
| 513 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 514 | - // if it's a "belongs to" relationship, then there's only one related model object eg, if this is a registration, there's only 1 attendee for it |
|
| 515 | - // so for these model objects just set it to be cached |
|
| 516 | - $this->_model_relations[$relationName] = $object_to_cache; |
|
| 517 | - $return = true; |
|
| 518 | - } else { |
|
| 519 | - // otherwise, this is the "many" side of a one to many relationship, so we'll add the object to the array of related objects for that type. |
|
| 520 | - // eg: if this is an event, there are many registrations for that event, so we cache the registrations in an array |
|
| 521 | - if ( ! is_array($this->_model_relations[$relationName])) { |
|
| 522 | - // if for some reason, the cached item is a model object, then stick that in the array, otherwise start with an empty array |
|
| 523 | - $this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class |
|
| 524 | - ? array($this->_model_relations[$relationName]) : array(); |
|
| 525 | - } |
|
| 526 | - // first check for a cache_id which is normally empty |
|
| 527 | - if ( ! empty($cache_id)) { |
|
| 528 | - // if the cache_id exists, then it means we are purposely trying to cache this with a known key that can then be used to retrieve the object later on |
|
| 529 | - $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
| 530 | - $return = $cache_id; |
|
| 531 | - } elseif ($object_to_cache->ID()) { |
|
| 532 | - // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 533 | - $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
| 534 | - $return = $object_to_cache->ID(); |
|
| 535 | - } else { |
|
| 536 | - // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 537 | - $this->_model_relations[$relationName][] = $object_to_cache; |
|
| 538 | - // move the internal pointer to the end of the array |
|
| 539 | - end($this->_model_relations[$relationName]); |
|
| 540 | - // and grab the key so that we can return it |
|
| 541 | - $return = key($this->_model_relations[$relationName]); |
|
| 542 | - } |
|
| 543 | - } |
|
| 544 | - return $return; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * For adding an item to the cached_properties property. |
|
| 551 | - * |
|
| 552 | - * @access protected |
|
| 553 | - * @param string $fieldname the property item the corresponding value is for. |
|
| 554 | - * @param mixed $value The value we are caching. |
|
| 555 | - * @param string|null $cache_type |
|
| 556 | - * @return void |
|
| 557 | - * @throws \EE_Error |
|
| 558 | - */ |
|
| 559 | - protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 560 | - { |
|
| 561 | - //first make sure this property exists |
|
| 562 | - $this->get_model()->field_settings_for($fieldname); |
|
| 563 | - $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 564 | - $this->_cached_properties[$fieldname][$cache_type] = $value; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 571 | - * This also SETS the cache if we return the actual property! |
|
| 572 | - * |
|
| 573 | - * @param string $fieldname the name of the property we're trying to retrieve |
|
| 574 | - * @param bool $pretty |
|
| 575 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 576 | - * (in cases where the same property may be used for different outputs |
|
| 577 | - * - i.e. datetime, money etc.) |
|
| 578 | - * It can also accept certain pre-defined "schema" strings |
|
| 579 | - * to define how to output the property. |
|
| 580 | - * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 581 | - * @return mixed whatever the value for the property is we're retrieving |
|
| 582 | - * @throws \EE_Error |
|
| 583 | - */ |
|
| 584 | - protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 585 | - { |
|
| 586 | - //verify the field exists |
|
| 587 | - $model = $this->get_model(); |
|
| 588 | - $model->field_settings_for($fieldname); |
|
| 589 | - $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 590 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 591 | - if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
|
| 592 | - return $this->_cached_properties[$fieldname][$cache_type]; |
|
| 593 | - } |
|
| 594 | - $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 595 | - $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 596 | - return $value; |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * If the cache didn't fetch the needed item, this fetches it. |
|
| 603 | - * @param string $fieldname |
|
| 604 | - * @param bool $pretty |
|
| 605 | - * @param string $extra_cache_ref |
|
| 606 | - * @return mixed |
|
| 607 | - */ |
|
| 608 | - protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 609 | - { |
|
| 610 | - $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 611 | - // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 612 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 613 | - $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 614 | - } |
|
| 615 | - if ( ! isset($this->_fields[$fieldname])) { |
|
| 616 | - $this->_fields[$fieldname] = null; |
|
| 617 | - } |
|
| 618 | - $value = $pretty |
|
| 619 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref) |
|
| 620 | - : $field_obj->prepare_for_get($this->_fields[$fieldname]); |
|
| 621 | - return $value; |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 628 | - * |
|
| 629 | - * @param \EE_Datetime_Field $datetime_field |
|
| 630 | - * @param bool $pretty |
|
| 631 | - * @param null $date_or_time |
|
| 632 | - * @return void |
|
| 633 | - * @throws \EE_Error |
|
| 634 | - */ |
|
| 635 | - protected function _prepare_datetime_field( |
|
| 636 | - EE_Datetime_Field $datetime_field, |
|
| 637 | - $pretty = false, |
|
| 638 | - $date_or_time = null |
|
| 639 | - ) { |
|
| 640 | - $datetime_field->set_timezone($this->_timezone); |
|
| 641 | - $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 642 | - $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 643 | - //set the output returned |
|
| 644 | - switch ($date_or_time) { |
|
| 645 | - case 'D' : |
|
| 646 | - $datetime_field->set_date_time_output('date'); |
|
| 647 | - break; |
|
| 648 | - case 'T' : |
|
| 649 | - $datetime_field->set_date_time_output('time'); |
|
| 650 | - break; |
|
| 651 | - default : |
|
| 652 | - $datetime_field->set_date_time_output(); |
|
| 653 | - } |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * This just takes care of clearing out the cached_properties |
|
| 660 | - * |
|
| 661 | - * @return void |
|
| 662 | - */ |
|
| 663 | - protected function _clear_cached_properties() |
|
| 664 | - { |
|
| 665 | - $this->_cached_properties = array(); |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * This just clears out ONE property if it exists in the cache |
|
| 672 | - * |
|
| 673 | - * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 674 | - * @return void |
|
| 675 | - */ |
|
| 676 | - protected function _clear_cached_property($property_name) |
|
| 677 | - { |
|
| 678 | - if (isset($this->_cached_properties[$property_name])) { |
|
| 679 | - unset($this->_cached_properties[$property_name]); |
|
| 680 | - } |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - |
|
| 684 | - |
|
| 685 | - /** |
|
| 686 | - * Ensures that this related thing is a model object. |
|
| 687 | - * |
|
| 688 | - * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 689 | - * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 690 | - * @return EE_Base_Class |
|
| 691 | - * @throws \EE_Error |
|
| 692 | - */ |
|
| 693 | - protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 694 | - { |
|
| 695 | - $other_model_instance = self::_get_model_instance_with_name( |
|
| 696 | - self::_get_model_classname($model_name), |
|
| 697 | - $this->_timezone |
|
| 698 | - ); |
|
| 699 | - return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - |
|
| 703 | - |
|
| 704 | - /** |
|
| 705 | - * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 706 | - * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 707 | - * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 708 | - * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 709 | - * |
|
| 710 | - * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 711 | - * Eg 'Registration' |
|
| 712 | - * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 713 | - * if you intend to use $clear_all = TRUE, or the relation only |
|
| 714 | - * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 715 | - * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 716 | - * this is HasMany or HABTM. |
|
| 717 | - * @throws EE_Error |
|
| 718 | - * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 719 | - * relation from all |
|
| 720 | - */ |
|
| 721 | - public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 722 | - { |
|
| 723 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 724 | - $index_in_cache = ''; |
|
| 725 | - if ( ! $relationship_to_model) { |
|
| 726 | - throw new EE_Error( |
|
| 727 | - sprintf( |
|
| 728 | - __("There is no relationship to %s on a %s. Cannot clear that cache", 'event_espresso'), |
|
| 729 | - $relationName, |
|
| 730 | - get_class($this) |
|
| 731 | - ) |
|
| 732 | - ); |
|
| 733 | - } |
|
| 734 | - if ($clear_all) { |
|
| 735 | - $obj_removed = true; |
|
| 736 | - $this->_model_relations[$relationName] = null; |
|
| 737 | - } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 738 | - $obj_removed = $this->_model_relations[$relationName]; |
|
| 739 | - $this->_model_relations[$relationName] = null; |
|
| 740 | - } else { |
|
| 741 | - if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 742 | - && $object_to_remove_or_index_into_array->ID() |
|
| 743 | - ) { |
|
| 744 | - $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 745 | - if (is_array($this->_model_relations[$relationName]) |
|
| 746 | - && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
| 747 | - ) { |
|
| 748 | - $index_found_at = null; |
|
| 749 | - //find this object in the array even though it has a different key |
|
| 750 | - foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
| 751 | - if ( |
|
| 752 | - $obj instanceof EE_Base_Class |
|
| 753 | - && ( |
|
| 754 | - $obj == $object_to_remove_or_index_into_array |
|
| 755 | - || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 756 | - ) |
|
| 757 | - ) { |
|
| 758 | - $index_found_at = $index; |
|
| 759 | - break; |
|
| 760 | - } |
|
| 761 | - } |
|
| 762 | - if ($index_found_at) { |
|
| 763 | - $index_in_cache = $index_found_at; |
|
| 764 | - } else { |
|
| 765 | - //it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 766 | - //if it wasn't in it to begin with. So we're done |
|
| 767 | - return $object_to_remove_or_index_into_array; |
|
| 768 | - } |
|
| 769 | - } |
|
| 770 | - } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 771 | - //so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 772 | - foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 773 | - if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 774 | - $index_in_cache = $index; |
|
| 775 | - } |
|
| 776 | - } |
|
| 777 | - } else { |
|
| 778 | - $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 779 | - } |
|
| 780 | - //supposedly we've found it. But it could just be that the client code |
|
| 781 | - //provided a bad index/object |
|
| 782 | - if ( |
|
| 783 | - isset( |
|
| 784 | - $this->_model_relations[$relationName], |
|
| 785 | - $this->_model_relations[$relationName][$index_in_cache] |
|
| 786 | - ) |
|
| 787 | - ) { |
|
| 788 | - $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
| 789 | - unset($this->_model_relations[$relationName][$index_in_cache]); |
|
| 790 | - } else { |
|
| 791 | - //that thing was never cached anyways. |
|
| 792 | - $obj_removed = null; |
|
| 793 | - } |
|
| 794 | - } |
|
| 795 | - return $obj_removed; |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - |
|
| 799 | - |
|
| 800 | - /** |
|
| 801 | - * update_cache_after_object_save |
|
| 802 | - * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 803 | - * obtained after being saved to the db |
|
| 804 | - * |
|
| 805 | - * @param string $relationName - the type of object that is cached |
|
| 806 | - * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 807 | - * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 808 | - * @return boolean TRUE on success, FALSE on fail |
|
| 809 | - * @throws \EE_Error |
|
| 810 | - */ |
|
| 811 | - public function update_cache_after_object_save( |
|
| 812 | - $relationName, |
|
| 813 | - EE_Base_Class $newly_saved_object, |
|
| 814 | - $current_cache_id = '' |
|
| 815 | - ) { |
|
| 816 | - // verify that incoming object is of the correct type |
|
| 817 | - $obj_class = 'EE_' . $relationName; |
|
| 818 | - if ($newly_saved_object instanceof $obj_class) { |
|
| 819 | - /* @type EE_Base_Class $newly_saved_object */ |
|
| 820 | - // now get the type of relation |
|
| 821 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 822 | - // if this is a 1:1 relationship |
|
| 823 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 824 | - // then just replace the cached object with the newly saved object |
|
| 825 | - $this->_model_relations[$relationName] = $newly_saved_object; |
|
| 826 | - return true; |
|
| 827 | - // or if it's some kind of sordid feral polyamorous relationship... |
|
| 828 | - } elseif (is_array($this->_model_relations[$relationName]) |
|
| 829 | - && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
| 830 | - ) { |
|
| 831 | - // then remove the current cached item |
|
| 832 | - unset($this->_model_relations[$relationName][$current_cache_id]); |
|
| 833 | - // and cache the newly saved object using it's new ID |
|
| 834 | - $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
| 835 | - return true; |
|
| 836 | - } |
|
| 837 | - } |
|
| 838 | - return false; |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - |
|
| 842 | - |
|
| 843 | - /** |
|
| 844 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 845 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 846 | - * |
|
| 847 | - * @param string $relationName |
|
| 848 | - * @return EE_Base_Class |
|
| 849 | - */ |
|
| 850 | - public function get_one_from_cache($relationName) |
|
| 851 | - { |
|
| 852 | - $cached_array_or_object = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] |
|
| 853 | - : null; |
|
| 854 | - if (is_array($cached_array_or_object)) { |
|
| 855 | - return array_shift($cached_array_or_object); |
|
| 856 | - } else { |
|
| 857 | - return $cached_array_or_object; |
|
| 858 | - } |
|
| 859 | - } |
|
| 860 | - |
|
| 861 | - |
|
| 862 | - |
|
| 863 | - /** |
|
| 864 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 865 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 866 | - * |
|
| 867 | - * @param string $relationName |
|
| 868 | - * @throws \EE_Error |
|
| 869 | - * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 870 | - */ |
|
| 871 | - public function get_all_from_cache($relationName) |
|
| 872 | - { |
|
| 873 | - $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array(); |
|
| 874 | - // if the result is not an array, but exists, make it an array |
|
| 875 | - $objects = is_array($objects) ? $objects : array($objects); |
|
| 876 | - //bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 877 | - //basically, if this model object was stored in the session, and these cached model objects |
|
| 878 | - //already have IDs, let's make sure they're in their model's entity mapper |
|
| 879 | - //otherwise we will have duplicates next time we call |
|
| 880 | - // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 881 | - $model = EE_Registry::instance()->load_model($relationName); |
|
| 882 | - foreach ($objects as $model_object) { |
|
| 883 | - if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 884 | - //ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 885 | - if ($model_object->ID()) { |
|
| 886 | - $model->add_to_entity_map($model_object); |
|
| 887 | - } |
|
| 888 | - } else { |
|
| 889 | - throw new EE_Error( |
|
| 890 | - sprintf( |
|
| 891 | - __( |
|
| 892 | - 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 893 | - 'event_espresso' |
|
| 894 | - ), |
|
| 895 | - $relationName, |
|
| 896 | - gettype($model_object) |
|
| 897 | - ) |
|
| 898 | - ); |
|
| 899 | - } |
|
| 900 | - } |
|
| 901 | - return $objects; |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - |
|
| 905 | - |
|
| 906 | - /** |
|
| 907 | - * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 908 | - * matching the given query conditions. |
|
| 909 | - * |
|
| 910 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 911 | - * @param int $limit How many objects to return. |
|
| 912 | - * @param array $query_params Any additional conditions on the query. |
|
| 913 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 914 | - * you can indicate just the columns you want returned |
|
| 915 | - * @return array|EE_Base_Class[] |
|
| 916 | - * @throws \EE_Error |
|
| 917 | - */ |
|
| 918 | - public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 919 | - { |
|
| 920 | - $model = $this->get_model(); |
|
| 921 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 922 | - ? $model->get_primary_key_field()->get_name() |
|
| 923 | - : $field_to_order_by; |
|
| 924 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 925 | - if (empty($field) || empty($current_value)) { |
|
| 926 | - return array(); |
|
| 927 | - } |
|
| 928 | - return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 929 | - } |
|
| 930 | - |
|
| 931 | - |
|
| 932 | - |
|
| 933 | - /** |
|
| 934 | - * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 935 | - * matching the given query conditions. |
|
| 936 | - * |
|
| 937 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 938 | - * @param int $limit How many objects to return. |
|
| 939 | - * @param array $query_params Any additional conditions on the query. |
|
| 940 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 941 | - * you can indicate just the columns you want returned |
|
| 942 | - * @return array|EE_Base_Class[] |
|
| 943 | - * @throws \EE_Error |
|
| 944 | - */ |
|
| 945 | - public function previous_x( |
|
| 946 | - $field_to_order_by = null, |
|
| 947 | - $limit = 1, |
|
| 948 | - $query_params = array(), |
|
| 949 | - $columns_to_select = null |
|
| 950 | - ) { |
|
| 951 | - $model = $this->get_model(); |
|
| 952 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 953 | - ? $model->get_primary_key_field()->get_name() |
|
| 954 | - : $field_to_order_by; |
|
| 955 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 956 | - if (empty($field) || empty($current_value)) { |
|
| 957 | - return array(); |
|
| 958 | - } |
|
| 959 | - return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 960 | - } |
|
| 961 | - |
|
| 962 | - |
|
| 963 | - |
|
| 964 | - /** |
|
| 965 | - * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 966 | - * matching the given query conditions. |
|
| 967 | - * |
|
| 968 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 969 | - * @param array $query_params Any additional conditions on the query. |
|
| 970 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 971 | - * you can indicate just the columns you want returned |
|
| 972 | - * @return array|EE_Base_Class |
|
| 973 | - * @throws \EE_Error |
|
| 974 | - */ |
|
| 975 | - public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 976 | - { |
|
| 977 | - $model = $this->get_model(); |
|
| 978 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 979 | - ? $model->get_primary_key_field()->get_name() |
|
| 980 | - : $field_to_order_by; |
|
| 981 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 982 | - if (empty($field) || empty($current_value)) { |
|
| 983 | - return array(); |
|
| 984 | - } |
|
| 985 | - return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 992 | - * matching the given query conditions. |
|
| 993 | - * |
|
| 994 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 995 | - * @param array $query_params Any additional conditions on the query. |
|
| 996 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 997 | - * you can indicate just the column you want returned |
|
| 998 | - * @return array|EE_Base_Class |
|
| 999 | - * @throws \EE_Error |
|
| 1000 | - */ |
|
| 1001 | - public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1002 | - { |
|
| 1003 | - $model = $this->get_model(); |
|
| 1004 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1005 | - ? $model->get_primary_key_field()->get_name() |
|
| 1006 | - : $field_to_order_by; |
|
| 1007 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1008 | - if (empty($field) || empty($current_value)) { |
|
| 1009 | - return array(); |
|
| 1010 | - } |
|
| 1011 | - return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1012 | - } |
|
| 1013 | - |
|
| 1014 | - |
|
| 1015 | - |
|
| 1016 | - /** |
|
| 1017 | - * Overrides parent because parent expects old models. |
|
| 1018 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1019 | - * |
|
| 1020 | - * @param string $field_name |
|
| 1021 | - * @param mixed $field_value_from_db |
|
| 1022 | - * @throws \EE_Error |
|
| 1023 | - */ |
|
| 1024 | - public function set_from_db($field_name, $field_value_from_db) |
|
| 1025 | - { |
|
| 1026 | - $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1027 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1028 | - //you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1029 | - //eg, a CPT model object could have an entry in the posts table, but no |
|
| 1030 | - //entry in the meta table. Meaning that all its columns in the meta table |
|
| 1031 | - //are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1032 | - if ($field_value_from_db === null) { |
|
| 1033 | - if ($field_obj->is_nullable()) { |
|
| 1034 | - //if the field allows nulls, then let it be null |
|
| 1035 | - $field_value = null; |
|
| 1036 | - } else { |
|
| 1037 | - $field_value = $field_obj->get_default_value(); |
|
| 1038 | - } |
|
| 1039 | - } else { |
|
| 1040 | - $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1041 | - } |
|
| 1042 | - $this->_fields[$field_name] = $field_value; |
|
| 1043 | - $this->_clear_cached_property($field_name); |
|
| 1044 | - } |
|
| 1045 | - } |
|
| 1046 | - |
|
| 1047 | - |
|
| 1048 | - |
|
| 1049 | - /** |
|
| 1050 | - * verifies that the specified field is of the correct type |
|
| 1051 | - * |
|
| 1052 | - * @param string $field_name |
|
| 1053 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1054 | - * (in cases where the same property may be used for different outputs |
|
| 1055 | - * - i.e. datetime, money etc.) |
|
| 1056 | - * @return mixed |
|
| 1057 | - * @throws \EE_Error |
|
| 1058 | - */ |
|
| 1059 | - public function get($field_name, $extra_cache_ref = null) |
|
| 1060 | - { |
|
| 1061 | - return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1062 | - } |
|
| 1063 | - |
|
| 1064 | - |
|
| 1065 | - |
|
| 1066 | - /** |
|
| 1067 | - * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1068 | - * |
|
| 1069 | - * @param string $field_name A valid fieldname |
|
| 1070 | - * @return mixed Whatever the raw value stored on the property is. |
|
| 1071 | - * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1072 | - */ |
|
| 1073 | - public function get_raw($field_name) |
|
| 1074 | - { |
|
| 1075 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1076 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
| 1077 | - ? $this->_fields[$field_name]->format('U') |
|
| 1078 | - : $this->_fields[$field_name]; |
|
| 1079 | - } |
|
| 1080 | - |
|
| 1081 | - |
|
| 1082 | - |
|
| 1083 | - /** |
|
| 1084 | - * This is used to return the internal DateTime object used for a field that is a |
|
| 1085 | - * EE_Datetime_Field. |
|
| 1086 | - * |
|
| 1087 | - * @param string $field_name The field name retrieving the DateTime object. |
|
| 1088 | - * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1089 | - * @throws \EE_Error |
|
| 1090 | - * an error is set and false returned. If the field IS an |
|
| 1091 | - * EE_Datetime_Field and but the field value is null, then |
|
| 1092 | - * just null is returned (because that indicates that likely |
|
| 1093 | - * this field is nullable). |
|
| 1094 | - */ |
|
| 1095 | - public function get_DateTime_object($field_name) |
|
| 1096 | - { |
|
| 1097 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1098 | - if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1099 | - EE_Error::add_error( |
|
| 1100 | - sprintf( |
|
| 1101 | - __( |
|
| 1102 | - 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1103 | - 'event_espresso' |
|
| 1104 | - ), |
|
| 1105 | - $field_name |
|
| 1106 | - ), |
|
| 1107 | - __FILE__, |
|
| 1108 | - __FUNCTION__, |
|
| 1109 | - __LINE__ |
|
| 1110 | - ); |
|
| 1111 | - return false; |
|
| 1112 | - } |
|
| 1113 | - return $this->_fields[$field_name]; |
|
| 1114 | - } |
|
| 1115 | - |
|
| 1116 | - |
|
| 1117 | - |
|
| 1118 | - /** |
|
| 1119 | - * To be used in template to immediately echo out the value, and format it for output. |
|
| 1120 | - * Eg, should call stripslashes and whatnot before echoing |
|
| 1121 | - * |
|
| 1122 | - * @param string $field_name the name of the field as it appears in the DB |
|
| 1123 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1124 | - * (in cases where the same property may be used for different outputs |
|
| 1125 | - * - i.e. datetime, money etc.) |
|
| 1126 | - * @return void |
|
| 1127 | - * @throws \EE_Error |
|
| 1128 | - */ |
|
| 1129 | - public function e($field_name, $extra_cache_ref = null) |
|
| 1130 | - { |
|
| 1131 | - echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - |
|
| 1135 | - |
|
| 1136 | - /** |
|
| 1137 | - * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1138 | - * can be easily used as the value of form input. |
|
| 1139 | - * |
|
| 1140 | - * @param string $field_name |
|
| 1141 | - * @return void |
|
| 1142 | - * @throws \EE_Error |
|
| 1143 | - */ |
|
| 1144 | - public function f($field_name) |
|
| 1145 | - { |
|
| 1146 | - $this->e($field_name, 'form_input'); |
|
| 1147 | - } |
|
| 1148 | - |
|
| 1149 | - |
|
| 1150 | - |
|
| 1151 | - /** |
|
| 1152 | - * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1153 | - * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1154 | - * to see what options are available. |
|
| 1155 | - * @param string $field_name |
|
| 1156 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1157 | - * (in cases where the same property may be used for different outputs |
|
| 1158 | - * - i.e. datetime, money etc.) |
|
| 1159 | - * @return mixed |
|
| 1160 | - * @throws \EE_Error |
|
| 1161 | - */ |
|
| 1162 | - public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1163 | - { |
|
| 1164 | - return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1165 | - } |
|
| 1166 | - |
|
| 1167 | - |
|
| 1168 | - |
|
| 1169 | - /** |
|
| 1170 | - * This simply returns the datetime for the given field name |
|
| 1171 | - * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1172 | - * (and the equivalent e_date, e_time, e_datetime). |
|
| 1173 | - * |
|
| 1174 | - * @access protected |
|
| 1175 | - * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1176 | - * @param string $dt_frmt valid datetime format used for date |
|
| 1177 | - * (if '' then we just use the default on the field, |
|
| 1178 | - * if NULL we use the last-used format) |
|
| 1179 | - * @param string $tm_frmt Same as above except this is for time format |
|
| 1180 | - * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1181 | - * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1182 | - * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1183 | - * if field is not a valid dtt field, or void if echoing |
|
| 1184 | - * @throws \EE_Error |
|
| 1185 | - */ |
|
| 1186 | - protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1187 | - { |
|
| 1188 | - // clear cached property |
|
| 1189 | - $this->_clear_cached_property($field_name); |
|
| 1190 | - //reset format properties because they are used in get() |
|
| 1191 | - $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1192 | - $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1193 | - if ($echo) { |
|
| 1194 | - $this->e($field_name, $date_or_time); |
|
| 1195 | - return ''; |
|
| 1196 | - } |
|
| 1197 | - return $this->get($field_name, $date_or_time); |
|
| 1198 | - } |
|
| 1199 | - |
|
| 1200 | - |
|
| 1201 | - |
|
| 1202 | - /** |
|
| 1203 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1204 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1205 | - * other echoes the pretty value for dtt) |
|
| 1206 | - * |
|
| 1207 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1208 | - * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1209 | - * @return string datetime value formatted |
|
| 1210 | - * @throws \EE_Error |
|
| 1211 | - */ |
|
| 1212 | - public function get_date($field_name, $format = '') |
|
| 1213 | - { |
|
| 1214 | - return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - |
|
| 1218 | - |
|
| 1219 | - /** |
|
| 1220 | - * @param $field_name |
|
| 1221 | - * @param string $format |
|
| 1222 | - * @throws \EE_Error |
|
| 1223 | - */ |
|
| 1224 | - public function e_date($field_name, $format = '') |
|
| 1225 | - { |
|
| 1226 | - $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - |
|
| 1230 | - |
|
| 1231 | - /** |
|
| 1232 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1233 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1234 | - * other echoes the pretty value for dtt) |
|
| 1235 | - * |
|
| 1236 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1237 | - * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1238 | - * @return string datetime value formatted |
|
| 1239 | - * @throws \EE_Error |
|
| 1240 | - */ |
|
| 1241 | - public function get_time($field_name, $format = '') |
|
| 1242 | - { |
|
| 1243 | - return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1244 | - } |
|
| 1245 | - |
|
| 1246 | - |
|
| 1247 | - |
|
| 1248 | - /** |
|
| 1249 | - * @param $field_name |
|
| 1250 | - * @param string $format |
|
| 1251 | - * @throws \EE_Error |
|
| 1252 | - */ |
|
| 1253 | - public function e_time($field_name, $format = '') |
|
| 1254 | - { |
|
| 1255 | - $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1256 | - } |
|
| 1257 | - |
|
| 1258 | - |
|
| 1259 | - |
|
| 1260 | - /** |
|
| 1261 | - * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1262 | - * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1263 | - * other echoes the pretty value for dtt) |
|
| 1264 | - * |
|
| 1265 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1266 | - * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1267 | - * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1268 | - * @return string datetime value formatted |
|
| 1269 | - * @throws \EE_Error |
|
| 1270 | - */ |
|
| 1271 | - public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1272 | - { |
|
| 1273 | - return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1274 | - } |
|
| 1275 | - |
|
| 1276 | - |
|
| 1277 | - |
|
| 1278 | - /** |
|
| 1279 | - * @param string $field_name |
|
| 1280 | - * @param string $dt_frmt |
|
| 1281 | - * @param string $tm_frmt |
|
| 1282 | - * @throws \EE_Error |
|
| 1283 | - */ |
|
| 1284 | - public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1285 | - { |
|
| 1286 | - $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - |
|
| 1290 | - |
|
| 1291 | - /** |
|
| 1292 | - * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1293 | - * |
|
| 1294 | - * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1295 | - * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1296 | - * on the object will be used. |
|
| 1297 | - * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1298 | - * @throws \EE_Error |
|
| 1299 | - * field name. |
|
| 1300 | - */ |
|
| 1301 | - public function get_i18n_datetime($field_name, $format = '') |
|
| 1302 | - { |
|
| 1303 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1304 | - return date_i18n( |
|
| 1305 | - $format, |
|
| 1306 | - EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone) |
|
| 1307 | - ); |
|
| 1308 | - } |
|
| 1309 | - |
|
| 1310 | - |
|
| 1311 | - |
|
| 1312 | - /** |
|
| 1313 | - * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1314 | - * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1315 | - * thrown. |
|
| 1316 | - * |
|
| 1317 | - * @param string $field_name The field name being checked |
|
| 1318 | - * @throws EE_Error |
|
| 1319 | - * @return EE_Datetime_Field |
|
| 1320 | - */ |
|
| 1321 | - protected function _get_dtt_field_settings($field_name) |
|
| 1322 | - { |
|
| 1323 | - $field = $this->get_model()->field_settings_for($field_name); |
|
| 1324 | - //check if field is dtt |
|
| 1325 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1326 | - return $field; |
|
| 1327 | - } else { |
|
| 1328 | - throw new EE_Error(sprintf(__('The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1329 | - 'event_espresso'), $field_name, self::_get_model_classname(get_class($this)))); |
|
| 1330 | - } |
|
| 1331 | - } |
|
| 1332 | - |
|
| 1333 | - |
|
| 1334 | - |
|
| 1335 | - |
|
| 1336 | - /** |
|
| 1337 | - * NOTE ABOUT BELOW: |
|
| 1338 | - * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1339 | - * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1340 | - * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1341 | - * method and make sure you send the entire datetime value for setting. |
|
| 1342 | - */ |
|
| 1343 | - /** |
|
| 1344 | - * sets the time on a datetime property |
|
| 1345 | - * |
|
| 1346 | - * @access protected |
|
| 1347 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1348 | - * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1349 | - * @throws \EE_Error |
|
| 1350 | - */ |
|
| 1351 | - protected function _set_time_for($time, $fieldname) |
|
| 1352 | - { |
|
| 1353 | - $this->_set_date_time('T', $time, $fieldname); |
|
| 1354 | - } |
|
| 1355 | - |
|
| 1356 | - |
|
| 1357 | - |
|
| 1358 | - /** |
|
| 1359 | - * sets the date on a datetime property |
|
| 1360 | - * |
|
| 1361 | - * @access protected |
|
| 1362 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1363 | - * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1364 | - * @throws \EE_Error |
|
| 1365 | - */ |
|
| 1366 | - protected function _set_date_for($date, $fieldname) |
|
| 1367 | - { |
|
| 1368 | - $this->_set_date_time('D', $date, $fieldname); |
|
| 1369 | - } |
|
| 1370 | - |
|
| 1371 | - |
|
| 1372 | - |
|
| 1373 | - /** |
|
| 1374 | - * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1375 | - * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1376 | - * |
|
| 1377 | - * @access protected |
|
| 1378 | - * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1379 | - * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1380 | - * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1381 | - * EE_Datetime_Field property) |
|
| 1382 | - * @throws \EE_Error |
|
| 1383 | - */ |
|
| 1384 | - protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1385 | - { |
|
| 1386 | - $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1387 | - $field->set_timezone($this->_timezone); |
|
| 1388 | - $field->set_date_format($this->_dt_frmt); |
|
| 1389 | - $field->set_time_format($this->_tm_frmt); |
|
| 1390 | - switch ($what) { |
|
| 1391 | - case 'T' : |
|
| 1392 | - $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time( |
|
| 1393 | - $datetime_value, |
|
| 1394 | - $this->_fields[$fieldname] |
|
| 1395 | - ); |
|
| 1396 | - break; |
|
| 1397 | - case 'D' : |
|
| 1398 | - $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date( |
|
| 1399 | - $datetime_value, |
|
| 1400 | - $this->_fields[$fieldname] |
|
| 1401 | - ); |
|
| 1402 | - break; |
|
| 1403 | - case 'B' : |
|
| 1404 | - $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value); |
|
| 1405 | - break; |
|
| 1406 | - } |
|
| 1407 | - $this->_clear_cached_property($fieldname); |
|
| 1408 | - } |
|
| 1409 | - |
|
| 1410 | - |
|
| 1411 | - |
|
| 1412 | - /** |
|
| 1413 | - * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1414 | - * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1415 | - * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1416 | - * that could lead to some unexpected results! |
|
| 1417 | - * |
|
| 1418 | - * @access public |
|
| 1419 | - * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1420 | - * value being returned. |
|
| 1421 | - * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1422 | - * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1423 | - * @param string $prepend You can include something to prepend on the timestamp |
|
| 1424 | - * @param string $append You can include something to append on the timestamp |
|
| 1425 | - * @throws EE_Error |
|
| 1426 | - * @return string timestamp |
|
| 1427 | - */ |
|
| 1428 | - public function display_in_my_timezone( |
|
| 1429 | - $field_name, |
|
| 1430 | - $callback = 'get_datetime', |
|
| 1431 | - $args = null, |
|
| 1432 | - $prepend = '', |
|
| 1433 | - $append = '' |
|
| 1434 | - ) { |
|
| 1435 | - $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1436 | - if ($timezone === $this->_timezone) { |
|
| 1437 | - return ''; |
|
| 1438 | - } |
|
| 1439 | - $original_timezone = $this->_timezone; |
|
| 1440 | - $this->set_timezone($timezone); |
|
| 1441 | - $fn = (array)$field_name; |
|
| 1442 | - $args = array_merge($fn, (array)$args); |
|
| 1443 | - if ( ! method_exists($this, $callback)) { |
|
| 1444 | - throw new EE_Error( |
|
| 1445 | - sprintf( |
|
| 1446 | - __( |
|
| 1447 | - 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1448 | - 'event_espresso' |
|
| 1449 | - ), |
|
| 1450 | - $callback |
|
| 1451 | - ) |
|
| 1452 | - ); |
|
| 1453 | - } |
|
| 1454 | - $args = (array)$args; |
|
| 1455 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1456 | - $this->set_timezone($original_timezone); |
|
| 1457 | - return $return; |
|
| 1458 | - } |
|
| 1459 | - |
|
| 1460 | - |
|
| 1461 | - |
|
| 1462 | - /** |
|
| 1463 | - * Deletes this model object. |
|
| 1464 | - * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1465 | - * override |
|
| 1466 | - * `EE_Base_Class::_delete` NOT this class. |
|
| 1467 | - * |
|
| 1468 | - * @return boolean | int |
|
| 1469 | - * @throws \EE_Error |
|
| 1470 | - */ |
|
| 1471 | - public function delete() |
|
| 1472 | - { |
|
| 1473 | - /** |
|
| 1474 | - * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1475 | - * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1476 | - * should be aware that `_delete` may not always result in a permanent delete. For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1477 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1478 | - * |
|
| 1479 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1480 | - */ |
|
| 1481 | - do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1482 | - $result = $this->_delete(); |
|
| 1483 | - /** |
|
| 1484 | - * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1485 | - * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1486 | - * should be aware that `_delete` may not always result in a permanent delete. For example `EE_Soft_Base_Class::_delete` |
|
| 1487 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1488 | - * |
|
| 1489 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1490 | - * @param boolean $result |
|
| 1491 | - */ |
|
| 1492 | - do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1493 | - return $result; |
|
| 1494 | - } |
|
| 1495 | - |
|
| 1496 | - |
|
| 1497 | - |
|
| 1498 | - /** |
|
| 1499 | - * Calls the specific delete method for the instantiated class. |
|
| 1500 | - * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1501 | - * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1502 | - * `EE_Base_Class::delete` |
|
| 1503 | - * |
|
| 1504 | - * @return bool|int |
|
| 1505 | - * @throws \EE_Error |
|
| 1506 | - */ |
|
| 1507 | - protected function _delete() |
|
| 1508 | - { |
|
| 1509 | - return $this->delete_permanently(); |
|
| 1510 | - } |
|
| 1511 | - |
|
| 1512 | - |
|
| 1513 | - |
|
| 1514 | - /** |
|
| 1515 | - * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an |
|
| 1516 | - * error) |
|
| 1517 | - * |
|
| 1518 | - * @return bool | int |
|
| 1519 | - * @throws \EE_Error |
|
| 1520 | - */ |
|
| 1521 | - public function delete_permanently() |
|
| 1522 | - { |
|
| 1523 | - /** |
|
| 1524 | - * Called just before HARD deleting a model object |
|
| 1525 | - * |
|
| 1526 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1527 | - */ |
|
| 1528 | - do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1529 | - $model = $this->get_model(); |
|
| 1530 | - $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1531 | - $this->refresh_cache_of_related_objects(); |
|
| 1532 | - /** |
|
| 1533 | - * Called just after HARD deleting a model object |
|
| 1534 | - * |
|
| 1535 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1536 | - * @param boolean $result |
|
| 1537 | - */ |
|
| 1538 | - do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1539 | - return $result; |
|
| 1540 | - } |
|
| 1541 | - |
|
| 1542 | - |
|
| 1543 | - |
|
| 1544 | - /** |
|
| 1545 | - * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1546 | - * related model objects |
|
| 1547 | - * |
|
| 1548 | - * @throws \EE_Error |
|
| 1549 | - */ |
|
| 1550 | - public function refresh_cache_of_related_objects() |
|
| 1551 | - { |
|
| 1552 | - $model = $this->get_model(); |
|
| 1553 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1554 | - if ( ! empty($this->_model_relations[$relation_name])) { |
|
| 1555 | - $related_objects = $this->_model_relations[$relation_name]; |
|
| 1556 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1557 | - //this relation only stores a single model object, not an array |
|
| 1558 | - //but let's make it consistent |
|
| 1559 | - $related_objects = array($related_objects); |
|
| 1560 | - } |
|
| 1561 | - foreach ($related_objects as $related_object) { |
|
| 1562 | - //only refresh their cache if they're in memory |
|
| 1563 | - if ($related_object instanceof EE_Base_Class) { |
|
| 1564 | - $related_object->clear_cache($model->get_this_model_name(), $this); |
|
| 1565 | - } |
|
| 1566 | - } |
|
| 1567 | - } |
|
| 1568 | - } |
|
| 1569 | - } |
|
| 1570 | - |
|
| 1571 | - |
|
| 1572 | - |
|
| 1573 | - /** |
|
| 1574 | - * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1575 | - * object just before saving. |
|
| 1576 | - * |
|
| 1577 | - * @access public |
|
| 1578 | - * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1579 | - * if provided during the save() method (often client code will change the fields' |
|
| 1580 | - * values before calling save) |
|
| 1581 | - * @throws \EE_Error |
|
| 1582 | - * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1583 | - * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1584 | - */ |
|
| 1585 | - public function save($set_cols_n_values = array()) |
|
| 1586 | - { |
|
| 1587 | - $model = $this->get_model(); |
|
| 1588 | - /** |
|
| 1589 | - * Filters the fields we're about to save on the model object |
|
| 1590 | - * |
|
| 1591 | - * @param array $set_cols_n_values |
|
| 1592 | - * @param EE_Base_Class $model_object |
|
| 1593 | - */ |
|
| 1594 | - $set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values, |
|
| 1595 | - $this); |
|
| 1596 | - //set attributes as provided in $set_cols_n_values |
|
| 1597 | - foreach ($set_cols_n_values as $column => $value) { |
|
| 1598 | - $this->set($column, $value); |
|
| 1599 | - } |
|
| 1600 | - // no changes ? then don't do anything |
|
| 1601 | - if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1602 | - return 0; |
|
| 1603 | - } |
|
| 1604 | - /** |
|
| 1605 | - * Saving a model object. |
|
| 1606 | - * Before we perform a save, this action is fired. |
|
| 1607 | - * |
|
| 1608 | - * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1609 | - */ |
|
| 1610 | - do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1611 | - if ( ! $this->allow_persist()) { |
|
| 1612 | - return 0; |
|
| 1613 | - } |
|
| 1614 | - //now get current attribute values |
|
| 1615 | - $save_cols_n_values = $this->_fields; |
|
| 1616 | - //if the object already has an ID, update it. Otherwise, insert it |
|
| 1617 | - //also: change the assumption about values passed to the model NOT being prepare dby the model object. They have been |
|
| 1618 | - $old_assumption_concerning_value_preparation = $model |
|
| 1619 | - ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1620 | - $model->assume_values_already_prepared_by_model_object(true); |
|
| 1621 | - //does this model have an autoincrement PK? |
|
| 1622 | - if ($model->has_primary_key_field()) { |
|
| 1623 | - if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1624 | - //ok check if it's set, if so: update; if not, insert |
|
| 1625 | - if ( ! empty($save_cols_n_values[$model->primary_key_name()])) { |
|
| 1626 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1627 | - } else { |
|
| 1628 | - unset($save_cols_n_values[$model->primary_key_name()]); |
|
| 1629 | - $results = $model->insert($save_cols_n_values); |
|
| 1630 | - if ($results) { |
|
| 1631 | - //if successful, set the primary key |
|
| 1632 | - //but don't use the normal SET method, because it will check if |
|
| 1633 | - //an item with the same ID exists in the mapper & db, then |
|
| 1634 | - //will find it in the db (because we just added it) and THAT object |
|
| 1635 | - //will get added to the mapper before we can add this one! |
|
| 1636 | - //but if we just avoid using the SET method, all that headache can be avoided |
|
| 1637 | - $pk_field_name = $model->primary_key_name(); |
|
| 1638 | - $this->_fields[$pk_field_name] = $results; |
|
| 1639 | - $this->_clear_cached_property($pk_field_name); |
|
| 1640 | - $model->add_to_entity_map($this); |
|
| 1641 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1642 | - } |
|
| 1643 | - } |
|
| 1644 | - } else {//PK is NOT auto-increment |
|
| 1645 | - //so check if one like it already exists in the db |
|
| 1646 | - if ($model->exists_by_ID($this->ID())) { |
|
| 1647 | - if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1648 | - throw new EE_Error( |
|
| 1649 | - sprintf( |
|
| 1650 | - __('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1651 | - 'event_espresso'), |
|
| 1652 | - get_class($this), |
|
| 1653 | - get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1654 | - get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1655 | - '<br />' |
|
| 1656 | - ) |
|
| 1657 | - ); |
|
| 1658 | - } |
|
| 1659 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1660 | - } else { |
|
| 1661 | - $results = $model->insert($save_cols_n_values); |
|
| 1662 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1663 | - } |
|
| 1664 | - } |
|
| 1665 | - } else {//there is NO primary key |
|
| 1666 | - $already_in_db = false; |
|
| 1667 | - foreach ($model->unique_indexes() as $index) { |
|
| 1668 | - $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1669 | - if ($model->exists(array($uniqueness_where_params))) { |
|
| 1670 | - $already_in_db = true; |
|
| 1671 | - } |
|
| 1672 | - } |
|
| 1673 | - if ($already_in_db) { |
|
| 1674 | - $combined_pk_fields_n_values = array_intersect_key($save_cols_n_values, |
|
| 1675 | - $model->get_combined_primary_key_fields()); |
|
| 1676 | - $results = $model->update($save_cols_n_values, $combined_pk_fields_n_values); |
|
| 1677 | - } else { |
|
| 1678 | - $results = $model->insert($save_cols_n_values); |
|
| 1679 | - } |
|
| 1680 | - } |
|
| 1681 | - //restore the old assumption about values being prepared by the model object |
|
| 1682 | - $model |
|
| 1683 | - ->assume_values_already_prepared_by_model_object($old_assumption_concerning_value_preparation); |
|
| 1684 | - /** |
|
| 1685 | - * After saving the model object this action is called |
|
| 1686 | - * |
|
| 1687 | - * @param EE_Base_Class $model_object which was just saved |
|
| 1688 | - * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1689 | - * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1690 | - */ |
|
| 1691 | - do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1692 | - $this->_has_changes = false; |
|
| 1693 | - return $results; |
|
| 1694 | - } |
|
| 1695 | - |
|
| 1696 | - |
|
| 1697 | - |
|
| 1698 | - /** |
|
| 1699 | - * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1700 | - * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1701 | - * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1702 | - * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1703 | - * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1704 | - * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1705 | - * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1706 | - * |
|
| 1707 | - * @return void |
|
| 1708 | - * @throws \EE_Error |
|
| 1709 | - */ |
|
| 1710 | - protected function _update_cached_related_model_objs_fks() |
|
| 1711 | - { |
|
| 1712 | - $model = $this->get_model(); |
|
| 1713 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1714 | - if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1715 | - foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1716 | - $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1717 | - $model->get_this_model_name() |
|
| 1718 | - ); |
|
| 1719 | - $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1720 | - if ($related_model_obj_in_cache->ID()) { |
|
| 1721 | - $related_model_obj_in_cache->save(); |
|
| 1722 | - } |
|
| 1723 | - } |
|
| 1724 | - } |
|
| 1725 | - } |
|
| 1726 | - } |
|
| 1727 | - |
|
| 1728 | - |
|
| 1729 | - |
|
| 1730 | - /** |
|
| 1731 | - * Saves this model object and its NEW cached relations to the database. |
|
| 1732 | - * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1733 | - * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1734 | - * because otherwise, there's a potential for infinite looping of saving |
|
| 1735 | - * Saves the cached related model objects, and ensures the relation between them |
|
| 1736 | - * and this object and properly setup |
|
| 1737 | - * |
|
| 1738 | - * @return int ID of new model object on save; 0 on failure+ |
|
| 1739 | - * @throws \EE_Error |
|
| 1740 | - */ |
|
| 1741 | - public function save_new_cached_related_model_objs() |
|
| 1742 | - { |
|
| 1743 | - //make sure this has been saved |
|
| 1744 | - if ( ! $this->ID()) { |
|
| 1745 | - $id = $this->save(); |
|
| 1746 | - } else { |
|
| 1747 | - $id = $this->ID(); |
|
| 1748 | - } |
|
| 1749 | - //now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1750 | - foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1751 | - if ($this->_model_relations[$relationName]) { |
|
| 1752 | - //is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1753 | - //or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1754 | - if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1755 | - //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1756 | - //but ONLY if it DOES NOT exist in the DB |
|
| 1757 | - /* @var $related_model_obj EE_Base_Class */ |
|
| 1758 | - $related_model_obj = $this->_model_relations[$relationName]; |
|
| 1759 | - // if( ! $related_model_obj->ID()){ |
|
| 1760 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1761 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1762 | - // } |
|
| 1763 | - } else { |
|
| 1764 | - foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
| 1765 | - //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1766 | - //but ONLY if it DOES NOT exist in the DB |
|
| 1767 | - // if( ! $related_model_obj->ID()){ |
|
| 1768 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1769 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1770 | - // } |
|
| 1771 | - } |
|
| 1772 | - } |
|
| 1773 | - } |
|
| 1774 | - } |
|
| 1775 | - return $id; |
|
| 1776 | - } |
|
| 1777 | - |
|
| 1778 | - |
|
| 1779 | - |
|
| 1780 | - /** |
|
| 1781 | - * for getting a model while instantiated. |
|
| 1782 | - * |
|
| 1783 | - * @return \EEM_Base | \EEM_CPT_Base |
|
| 1784 | - */ |
|
| 1785 | - public function get_model() |
|
| 1786 | - { |
|
| 1787 | - if( ! $this->_model){ |
|
| 1788 | - $modelName = self::_get_model_classname(get_class($this)); |
|
| 1789 | - $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 1790 | - } else { |
|
| 1791 | - $this->_model->set_timezone($this->_timezone); |
|
| 1792 | - } |
|
| 1793 | - |
|
| 1794 | - return $this->_model; |
|
| 1795 | - } |
|
| 1796 | - |
|
| 1797 | - |
|
| 1798 | - |
|
| 1799 | - /** |
|
| 1800 | - * @param $props_n_values |
|
| 1801 | - * @param $classname |
|
| 1802 | - * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 1803 | - * @throws \EE_Error |
|
| 1804 | - */ |
|
| 1805 | - protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 1806 | - { |
|
| 1807 | - //TODO: will not work for Term_Relationships because they have no PK! |
|
| 1808 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1809 | - if (array_key_exists($primary_id_ref, $props_n_values) && ! empty($props_n_values[$primary_id_ref])) { |
|
| 1810 | - $id = $props_n_values[$primary_id_ref]; |
|
| 1811 | - return self::_get_model($classname)->get_from_entity_map($id); |
|
| 1812 | - } |
|
| 1813 | - return false; |
|
| 1814 | - } |
|
| 1815 | - |
|
| 1816 | - |
|
| 1817 | - |
|
| 1818 | - /** |
|
| 1819 | - * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 1820 | - * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 1821 | - * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 1822 | - * we return false. |
|
| 1823 | - * |
|
| 1824 | - * @param array $props_n_values incoming array of properties and their values |
|
| 1825 | - * @param string $classname the classname of the child class |
|
| 1826 | - * @param null $timezone |
|
| 1827 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 1828 | - * date_format and the second value is the time format |
|
| 1829 | - * @return mixed (EE_Base_Class|bool) |
|
| 1830 | - * @throws \EE_Error |
|
| 1831 | - */ |
|
| 1832 | - protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 1833 | - { |
|
| 1834 | - $existing = null; |
|
| 1835 | - $model = self::_get_model($classname, $timezone); |
|
| 1836 | - if ($model->has_primary_key_field()) { |
|
| 1837 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1838 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 1839 | - && ! empty($props_n_values[$primary_id_ref]) |
|
| 1840 | - ) { |
|
| 1841 | - $existing = $model->get_one_by_ID( |
|
| 1842 | - $props_n_values[$primary_id_ref] |
|
| 1843 | - ); |
|
| 1844 | - } |
|
| 1845 | - } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 1846 | - //no primary key on this model, but there's still a matching item in the DB |
|
| 1847 | - $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 1848 | - self::_get_model($classname, $timezone)->get_index_primary_key_string($props_n_values) |
|
| 1849 | - ); |
|
| 1850 | - } |
|
| 1851 | - if ($existing) { |
|
| 1852 | - //set date formats if present before setting values |
|
| 1853 | - if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 1854 | - $existing->set_date_format($date_formats[0]); |
|
| 1855 | - $existing->set_time_format($date_formats[1]); |
|
| 1856 | - } else { |
|
| 1857 | - //set default formats for date and time |
|
| 1858 | - $existing->set_date_format(get_option('date_format')); |
|
| 1859 | - $existing->set_time_format(get_option('time_format')); |
|
| 1860 | - } |
|
| 1861 | - foreach ($props_n_values as $property => $field_value) { |
|
| 1862 | - $existing->set($property, $field_value); |
|
| 1863 | - } |
|
| 1864 | - return $existing; |
|
| 1865 | - } else { |
|
| 1866 | - return false; |
|
| 1867 | - } |
|
| 1868 | - } |
|
| 1869 | - |
|
| 1870 | - |
|
| 1871 | - |
|
| 1872 | - /** |
|
| 1873 | - * Gets the EEM_*_Model for this class |
|
| 1874 | - * |
|
| 1875 | - * @access public now, as this is more convenient |
|
| 1876 | - * @param $classname |
|
| 1877 | - * @param null $timezone |
|
| 1878 | - * @throws EE_Error |
|
| 1879 | - * @return EEM_Base |
|
| 1880 | - */ |
|
| 1881 | - protected static function _get_model($classname, $timezone = null) |
|
| 1882 | - { |
|
| 1883 | - //find model for this class |
|
| 1884 | - if ( ! $classname) { |
|
| 1885 | - throw new EE_Error( |
|
| 1886 | - sprintf( |
|
| 1887 | - __( |
|
| 1888 | - "What were you thinking calling _get_model(%s)?? You need to specify the class name", |
|
| 1889 | - "event_espresso" |
|
| 1890 | - ), |
|
| 1891 | - $classname |
|
| 1892 | - ) |
|
| 1893 | - ); |
|
| 1894 | - } |
|
| 1895 | - $modelName = self::_get_model_classname($classname); |
|
| 1896 | - return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 1897 | - } |
|
| 1898 | - |
|
| 1899 | - |
|
| 1900 | - |
|
| 1901 | - /** |
|
| 1902 | - * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 1903 | - * |
|
| 1904 | - * @param string $model_classname |
|
| 1905 | - * @param null $timezone |
|
| 1906 | - * @return EEM_Base |
|
| 1907 | - */ |
|
| 1908 | - protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 1909 | - { |
|
| 1910 | - $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 1911 | - $model = EE_Registry::instance()->load_model($model_classname); |
|
| 1912 | - $model->set_timezone($timezone); |
|
| 1913 | - return $model; |
|
| 1914 | - } |
|
| 1915 | - |
|
| 1916 | - |
|
| 1917 | - |
|
| 1918 | - /** |
|
| 1919 | - * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 1920 | - * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 1921 | - * |
|
| 1922 | - * @param null $model_name |
|
| 1923 | - * @return string like EEM_Attendee |
|
| 1924 | - */ |
|
| 1925 | - private static function _get_model_classname($model_name = null) |
|
| 1926 | - { |
|
| 1927 | - if (strpos($model_name, "EE_") === 0) { |
|
| 1928 | - $model_classname = str_replace("EE_", "EEM_", $model_name); |
|
| 1929 | - } else { |
|
| 1930 | - $model_classname = "EEM_" . $model_name; |
|
| 1931 | - } |
|
| 1932 | - return $model_classname; |
|
| 1933 | - } |
|
| 1934 | - |
|
| 1935 | - |
|
| 1936 | - |
|
| 1937 | - /** |
|
| 1938 | - * returns the name of the primary key attribute |
|
| 1939 | - * |
|
| 1940 | - * @param null $classname |
|
| 1941 | - * @throws EE_Error |
|
| 1942 | - * @return string |
|
| 1943 | - */ |
|
| 1944 | - protected static function _get_primary_key_name($classname = null) |
|
| 1945 | - { |
|
| 1946 | - if ( ! $classname) { |
|
| 1947 | - throw new EE_Error( |
|
| 1948 | - sprintf( |
|
| 1949 | - __("What were you thinking calling _get_primary_key_name(%s)", "event_espresso"), |
|
| 1950 | - $classname |
|
| 1951 | - ) |
|
| 1952 | - ); |
|
| 1953 | - } |
|
| 1954 | - return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 1955 | - } |
|
| 1956 | - |
|
| 1957 | - |
|
| 1958 | - |
|
| 1959 | - /** |
|
| 1960 | - * Gets the value of the primary key. |
|
| 1961 | - * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 1962 | - * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 1963 | - * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 1964 | - * |
|
| 1965 | - * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 1966 | - * @throws \EE_Error |
|
| 1967 | - */ |
|
| 1968 | - public function ID() |
|
| 1969 | - { |
|
| 1970 | - $model = $this->get_model(); |
|
| 1971 | - //now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 1972 | - if ($model->has_primary_key_field()) { |
|
| 1973 | - return $this->_fields[$model->primary_key_name()]; |
|
| 1974 | - } else { |
|
| 1975 | - return $model->get_index_primary_key_string($this->_fields); |
|
| 1976 | - } |
|
| 1977 | - } |
|
| 1978 | - |
|
| 1979 | - |
|
| 1980 | - |
|
| 1981 | - /** |
|
| 1982 | - * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 1983 | - * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 1984 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 1985 | - * |
|
| 1986 | - * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 1987 | - * @param string $relationName eg 'Events','Question',etc. |
|
| 1988 | - * an attendee to a group, you also want to specify which role they |
|
| 1989 | - * will have in that group. So you would use this parameter to |
|
| 1990 | - * specify array('role-column-name'=>'role-id') |
|
| 1991 | - * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 1992 | - * allow you to further constrict the relation to being added. |
|
| 1993 | - * However, keep in mind that the columns (keys) given must match a |
|
| 1994 | - * column on the JOIN table and currently only the HABTM models |
|
| 1995 | - * accept these additional conditions. Also remember that if an |
|
| 1996 | - * exact match isn't found for these extra cols/val pairs, then a |
|
| 1997 | - * NEW row is created in the join table. |
|
| 1998 | - * @param null $cache_id |
|
| 1999 | - * @throws EE_Error |
|
| 2000 | - * @return EE_Base_Class the object the relation was added to |
|
| 2001 | - */ |
|
| 2002 | - public function _add_relation_to( |
|
| 2003 | - $otherObjectModelObjectOrID, |
|
| 2004 | - $relationName, |
|
| 2005 | - $extra_join_model_fields_n_values = array(), |
|
| 2006 | - $cache_id = null |
|
| 2007 | - ) { |
|
| 2008 | - $model = $this->get_model(); |
|
| 2009 | - //if this thing exists in the DB, save the relation to the DB |
|
| 2010 | - if ($this->ID()) { |
|
| 2011 | - $otherObject = $model |
|
| 2012 | - ->add_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2013 | - $extra_join_model_fields_n_values); |
|
| 2014 | - //clear cache so future get_many_related and get_first_related() return new results. |
|
| 2015 | - $this->clear_cache($relationName, $otherObject, true); |
|
| 2016 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2017 | - $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2018 | - } |
|
| 2019 | - } else { |
|
| 2020 | - //this thing doesn't exist in the DB, so just cache it |
|
| 2021 | - if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2022 | - throw new EE_Error(sprintf( |
|
| 2023 | - __('Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 2024 | - 'event_espresso'), |
|
| 2025 | - $otherObjectModelObjectOrID, |
|
| 2026 | - get_class($this) |
|
| 2027 | - )); |
|
| 2028 | - } else { |
|
| 2029 | - $otherObject = $otherObjectModelObjectOrID; |
|
| 2030 | - } |
|
| 2031 | - $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2032 | - } |
|
| 2033 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2034 | - //fix the reciprocal relation too |
|
| 2035 | - if ($otherObject->ID()) { |
|
| 2036 | - //its saved so assumed relations exist in the DB, so we can just |
|
| 2037 | - //clear the cache so future queries use the updated info in the DB |
|
| 2038 | - $otherObject->clear_cache($model->get_this_model_name(), null, true); |
|
| 2039 | - } else { |
|
| 2040 | - //it's not saved, so it caches relations like this |
|
| 2041 | - $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2042 | - } |
|
| 2043 | - } |
|
| 2044 | - return $otherObject; |
|
| 2045 | - } |
|
| 2046 | - |
|
| 2047 | - |
|
| 2048 | - |
|
| 2049 | - /** |
|
| 2050 | - * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2051 | - * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2052 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2053 | - * from the cache |
|
| 2054 | - * |
|
| 2055 | - * @param mixed $otherObjectModelObjectOrID |
|
| 2056 | - * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2057 | - * to the DB yet |
|
| 2058 | - * @param string $relationName |
|
| 2059 | - * @param array $where_query |
|
| 2060 | - * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2061 | - * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2062 | - * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2063 | - * remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is |
|
| 2064 | - * created in the join table. |
|
| 2065 | - * @return EE_Base_Class the relation was removed from |
|
| 2066 | - * @throws \EE_Error |
|
| 2067 | - */ |
|
| 2068 | - public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2069 | - { |
|
| 2070 | - if ($this->ID()) { |
|
| 2071 | - //if this exists in the DB, save the relation change to the DB too |
|
| 2072 | - $otherObject = $this->get_model() |
|
| 2073 | - ->remove_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2074 | - $where_query); |
|
| 2075 | - $this->clear_cache($relationName, $otherObject); |
|
| 2076 | - } else { |
|
| 2077 | - //this doesn't exist in the DB, just remove it from the cache |
|
| 2078 | - $otherObject = $this->clear_cache($relationName, $otherObjectModelObjectOrID); |
|
| 2079 | - } |
|
| 2080 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2081 | - $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2082 | - } |
|
| 2083 | - return $otherObject; |
|
| 2084 | - } |
|
| 2085 | - |
|
| 2086 | - |
|
| 2087 | - |
|
| 2088 | - /** |
|
| 2089 | - * Removes ALL the related things for the $relationName. |
|
| 2090 | - * |
|
| 2091 | - * @param string $relationName |
|
| 2092 | - * @param array $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 2093 | - * @return EE_Base_Class |
|
| 2094 | - * @throws \EE_Error |
|
| 2095 | - */ |
|
| 2096 | - public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2097 | - { |
|
| 2098 | - if ($this->ID()) { |
|
| 2099 | - //if this exists in the DB, save the relation change to the DB too |
|
| 2100 | - $otherObjects = $this->get_model()->remove_relations($this, $relationName, $where_query_params); |
|
| 2101 | - $this->clear_cache($relationName, null, true); |
|
| 2102 | - } else { |
|
| 2103 | - //this doesn't exist in the DB, just remove it from the cache |
|
| 2104 | - $otherObjects = $this->clear_cache($relationName, null, true); |
|
| 2105 | - } |
|
| 2106 | - if (is_array($otherObjects)) { |
|
| 2107 | - foreach ($otherObjects as $otherObject) { |
|
| 2108 | - $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2109 | - } |
|
| 2110 | - } |
|
| 2111 | - return $otherObjects; |
|
| 2112 | - } |
|
| 2113 | - |
|
| 2114 | - |
|
| 2115 | - |
|
| 2116 | - /** |
|
| 2117 | - * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2118 | - * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2119 | - * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2120 | - * because we want to get even deleted items etc. |
|
| 2121 | - * |
|
| 2122 | - * @param string $relationName key in the model's _model_relations array |
|
| 2123 | - * @param array $query_params like EEM_Base::get_all |
|
| 2124 | - * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys |
|
| 2125 | - * @throws \EE_Error |
|
| 2126 | - * or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if |
|
| 2127 | - * you want IDs |
|
| 2128 | - */ |
|
| 2129 | - public function get_many_related($relationName, $query_params = array()) |
|
| 2130 | - { |
|
| 2131 | - if ($this->ID()) { |
|
| 2132 | - //this exists in the DB, so get the related things from either the cache or the DB |
|
| 2133 | - //if there are query parameters, forget about caching the related model objects. |
|
| 2134 | - if ($query_params) { |
|
| 2135 | - $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2136 | - } else { |
|
| 2137 | - //did we already cache the result of this query? |
|
| 2138 | - $cached_results = $this->get_all_from_cache($relationName); |
|
| 2139 | - if ( ! $cached_results) { |
|
| 2140 | - $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2141 | - //if no query parameters were passed, then we got all the related model objects |
|
| 2142 | - //for that relation. We can cache them then. |
|
| 2143 | - foreach ($related_model_objects as $related_model_object) { |
|
| 2144 | - $this->cache($relationName, $related_model_object); |
|
| 2145 | - } |
|
| 2146 | - } else { |
|
| 2147 | - $related_model_objects = $cached_results; |
|
| 2148 | - } |
|
| 2149 | - } |
|
| 2150 | - } else { |
|
| 2151 | - //this doesn't exist in the DB, so just get the related things from the cache |
|
| 2152 | - $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2153 | - } |
|
| 2154 | - return $related_model_objects; |
|
| 2155 | - } |
|
| 2156 | - |
|
| 2157 | - |
|
| 2158 | - |
|
| 2159 | - /** |
|
| 2160 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2161 | - * unless otherwise specified in the $query_params |
|
| 2162 | - * |
|
| 2163 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2164 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2165 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2166 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2167 | - * that by the setting $distinct to TRUE; |
|
| 2168 | - * @return int |
|
| 2169 | - */ |
|
| 2170 | - public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2171 | - { |
|
| 2172 | - return $this->get_model()->count_related($this, $relation_name, $query_params, $field_to_count, $distinct); |
|
| 2173 | - } |
|
| 2174 | - |
|
| 2175 | - |
|
| 2176 | - |
|
| 2177 | - /** |
|
| 2178 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2179 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2180 | - * |
|
| 2181 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2182 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2183 | - * @param string $field_to_sum name of field to count by. |
|
| 2184 | - * By default, uses primary key (which doesn't make much sense, so you should probably |
|
| 2185 | - * change it) |
|
| 2186 | - * @return int |
|
| 2187 | - */ |
|
| 2188 | - public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2189 | - { |
|
| 2190 | - return $this->get_model()->sum_related($this, $relation_name, $query_params, $field_to_sum); |
|
| 2191 | - } |
|
| 2192 | - |
|
| 2193 | - |
|
| 2194 | - |
|
| 2195 | - /** |
|
| 2196 | - * Gets the first (ie, one) related model object of the specified type. |
|
| 2197 | - * |
|
| 2198 | - * @param string $relationName key in the model's _model_relations array |
|
| 2199 | - * @param array $query_params like EEM_Base::get_all |
|
| 2200 | - * @return EE_Base_Class (not an array, a single object) |
|
| 2201 | - * @throws \EE_Error |
|
| 2202 | - */ |
|
| 2203 | - public function get_first_related($relationName, $query_params = array()) |
|
| 2204 | - { |
|
| 2205 | - $model = $this->get_model(); |
|
| 2206 | - if ($this->ID()) {//this exists in the DB, get from the cache OR the DB |
|
| 2207 | - //if they've provided some query parameters, don't bother trying to cache the result |
|
| 2208 | - //also make sure we're not caching the result of get_first_related |
|
| 2209 | - //on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2210 | - if ($query_params |
|
| 2211 | - || ! $model->related_settings_for($relationName) |
|
| 2212 | - instanceof |
|
| 2213 | - EE_Belongs_To_Relation |
|
| 2214 | - ) { |
|
| 2215 | - $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2216 | - } else { |
|
| 2217 | - //first, check if we've already cached the result of this query |
|
| 2218 | - $cached_result = $this->get_one_from_cache($relationName); |
|
| 2219 | - if ( ! $cached_result) { |
|
| 2220 | - $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2221 | - $this->cache($relationName, $related_model_object); |
|
| 2222 | - } else { |
|
| 2223 | - $related_model_object = $cached_result; |
|
| 2224 | - } |
|
| 2225 | - } |
|
| 2226 | - } else { |
|
| 2227 | - $related_model_object = null; |
|
| 2228 | - //this doesn't exist in the Db, but maybe the relation is of type belongs to, and so the related thing might |
|
| 2229 | - if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2230 | - $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2231 | - } |
|
| 2232 | - //this doesn't exist in the DB and apparently the thing it belongs to doesn't either, just get what's cached on this object |
|
| 2233 | - if ( ! $related_model_object) { |
|
| 2234 | - $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2235 | - } |
|
| 2236 | - } |
|
| 2237 | - return $related_model_object; |
|
| 2238 | - } |
|
| 2239 | - |
|
| 2240 | - |
|
| 2241 | - |
|
| 2242 | - /** |
|
| 2243 | - * Does a delete on all related objects of type $relationName and removes |
|
| 2244 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2245 | - * of blocking related model objects) does nothing. If the related model objects are |
|
| 2246 | - * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2247 | - * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2248 | - * |
|
| 2249 | - * @param string $relationName |
|
| 2250 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2251 | - * @return int how many deleted |
|
| 2252 | - * @throws \EE_Error |
|
| 2253 | - */ |
|
| 2254 | - public function delete_related($relationName, $query_params = array()) |
|
| 2255 | - { |
|
| 2256 | - if ($this->ID()) { |
|
| 2257 | - $count = $this->get_model()->delete_related($this, $relationName, $query_params); |
|
| 2258 | - } else { |
|
| 2259 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2260 | - $this->clear_cache($relationName, null, true); |
|
| 2261 | - } |
|
| 2262 | - return $count; |
|
| 2263 | - } |
|
| 2264 | - |
|
| 2265 | - |
|
| 2266 | - |
|
| 2267 | - /** |
|
| 2268 | - * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2269 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2270 | - * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2271 | - * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2272 | - * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2273 | - * |
|
| 2274 | - * @param string $relationName |
|
| 2275 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2276 | - * @return int how many deleted (including those soft deleted) |
|
| 2277 | - * @throws \EE_Error |
|
| 2278 | - */ |
|
| 2279 | - public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2280 | - { |
|
| 2281 | - if ($this->ID()) { |
|
| 2282 | - $count = $this->get_model()->delete_related_permanently($this, $relationName, $query_params); |
|
| 2283 | - } else { |
|
| 2284 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2285 | - } |
|
| 2286 | - $this->clear_cache($relationName, null, true); |
|
| 2287 | - return $count; |
|
| 2288 | - } |
|
| 2289 | - |
|
| 2290 | - |
|
| 2291 | - |
|
| 2292 | - /** |
|
| 2293 | - * is_set |
|
| 2294 | - * Just a simple utility function children can use for checking if property exists |
|
| 2295 | - * |
|
| 2296 | - * @access public |
|
| 2297 | - * @param string $field_name property to check |
|
| 2298 | - * @return bool TRUE if existing,FALSE if not. |
|
| 2299 | - */ |
|
| 2300 | - public function is_set($field_name) |
|
| 2301 | - { |
|
| 2302 | - return isset($this->_fields[$field_name]); |
|
| 2303 | - } |
|
| 2304 | - |
|
| 2305 | - |
|
| 2306 | - |
|
| 2307 | - /** |
|
| 2308 | - * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2309 | - * EE_Error exception if they don't |
|
| 2310 | - * |
|
| 2311 | - * @param mixed (string|array) $properties properties to check |
|
| 2312 | - * @throws EE_Error |
|
| 2313 | - * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2314 | - */ |
|
| 2315 | - protected function _property_exists($properties) |
|
| 2316 | - { |
|
| 2317 | - foreach ((array)$properties as $property_name) { |
|
| 2318 | - //first make sure this property exists |
|
| 2319 | - if ( ! $this->_fields[$property_name]) { |
|
| 2320 | - throw new EE_Error( |
|
| 2321 | - sprintf( |
|
| 2322 | - __( |
|
| 2323 | - 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2324 | - 'event_espresso' |
|
| 2325 | - ), |
|
| 2326 | - $property_name |
|
| 2327 | - ) |
|
| 2328 | - ); |
|
| 2329 | - } |
|
| 2330 | - } |
|
| 2331 | - return true; |
|
| 2332 | - } |
|
| 2333 | - |
|
| 2334 | - |
|
| 2335 | - |
|
| 2336 | - /** |
|
| 2337 | - * This simply returns an array of model fields for this object |
|
| 2338 | - * |
|
| 2339 | - * @return array |
|
| 2340 | - * @throws \EE_Error |
|
| 2341 | - */ |
|
| 2342 | - public function model_field_array() |
|
| 2343 | - { |
|
| 2344 | - $fields = $this->get_model()->field_settings(false); |
|
| 2345 | - $properties = array(); |
|
| 2346 | - //remove prepended underscore |
|
| 2347 | - foreach ($fields as $field_name => $settings) { |
|
| 2348 | - $properties[$field_name] = $this->get($field_name); |
|
| 2349 | - } |
|
| 2350 | - return $properties; |
|
| 2351 | - } |
|
| 2352 | - |
|
| 2353 | - |
|
| 2354 | - |
|
| 2355 | - /** |
|
| 2356 | - * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2357 | - * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2358 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 2359 | - * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that |
|
| 2360 | - * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 2361 | - * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 2362 | - * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 2363 | - * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 2364 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 2365 | - * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2366 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2367 | - * return $previousReturnValue.$returnString; |
|
| 2368 | - * } |
|
| 2369 | - * require('EE_Answer.class.php'); |
|
| 2370 | - * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2371 | - * echo $answer->my_callback('monkeys',100); |
|
| 2372 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2373 | - * |
|
| 2374 | - * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2375 | - * @param array $args array of original arguments passed to the function |
|
| 2376 | - * @throws EE_Error |
|
| 2377 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 2378 | - */ |
|
| 2379 | - public function __call($methodName, $args) |
|
| 2380 | - { |
|
| 2381 | - $className = get_class($this); |
|
| 2382 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2383 | - if ( ! has_filter($tagName)) { |
|
| 2384 | - throw new EE_Error( |
|
| 2385 | - sprintf( |
|
| 2386 | - __( |
|
| 2387 | - "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2388 | - "event_espresso" |
|
| 2389 | - ), |
|
| 2390 | - $methodName, |
|
| 2391 | - $className, |
|
| 2392 | - $tagName |
|
| 2393 | - ) |
|
| 2394 | - ); |
|
| 2395 | - } |
|
| 2396 | - return apply_filters($tagName, null, $this, $args); |
|
| 2397 | - } |
|
| 2398 | - |
|
| 2399 | - |
|
| 2400 | - |
|
| 2401 | - /** |
|
| 2402 | - * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2403 | - * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2404 | - * |
|
| 2405 | - * @param string $meta_key |
|
| 2406 | - * @param mixed $meta_value |
|
| 2407 | - * @param mixed $previous_value |
|
| 2408 | - * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2409 | - * @throws \EE_Error |
|
| 2410 | - * NOTE: if the values haven't changed, returns 0 |
|
| 2411 | - */ |
|
| 2412 | - public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2413 | - { |
|
| 2414 | - $query_params = array( |
|
| 2415 | - array( |
|
| 2416 | - 'EXM_key' => $meta_key, |
|
| 2417 | - 'OBJ_ID' => $this->ID(), |
|
| 2418 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2419 | - ), |
|
| 2420 | - ); |
|
| 2421 | - if ($previous_value !== null) { |
|
| 2422 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2423 | - } |
|
| 2424 | - $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2425 | - if ( ! $existing_rows_like_that) { |
|
| 2426 | - return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2427 | - } |
|
| 2428 | - foreach ($existing_rows_like_that as $existing_row) { |
|
| 2429 | - $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2430 | - } |
|
| 2431 | - return count($existing_rows_like_that); |
|
| 2432 | - } |
|
| 2433 | - |
|
| 2434 | - |
|
| 2435 | - |
|
| 2436 | - /** |
|
| 2437 | - * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2438 | - * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2439 | - * extra meta row was entered, false if not |
|
| 2440 | - * |
|
| 2441 | - * @param string $meta_key |
|
| 2442 | - * @param mixed $meta_value |
|
| 2443 | - * @param boolean $unique |
|
| 2444 | - * @return boolean |
|
| 2445 | - * @throws \EE_Error |
|
| 2446 | - */ |
|
| 2447 | - public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2448 | - { |
|
| 2449 | - if ($unique) { |
|
| 2450 | - $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2451 | - array( |
|
| 2452 | - array( |
|
| 2453 | - 'EXM_key' => $meta_key, |
|
| 2454 | - 'OBJ_ID' => $this->ID(), |
|
| 2455 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2456 | - ), |
|
| 2457 | - ) |
|
| 2458 | - ); |
|
| 2459 | - if ($existing_extra_meta) { |
|
| 2460 | - return false; |
|
| 2461 | - } |
|
| 2462 | - } |
|
| 2463 | - $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2464 | - array( |
|
| 2465 | - 'EXM_key' => $meta_key, |
|
| 2466 | - 'EXM_value' => $meta_value, |
|
| 2467 | - 'OBJ_ID' => $this->ID(), |
|
| 2468 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2469 | - ) |
|
| 2470 | - ); |
|
| 2471 | - $new_extra_meta->save(); |
|
| 2472 | - return true; |
|
| 2473 | - } |
|
| 2474 | - |
|
| 2475 | - |
|
| 2476 | - |
|
| 2477 | - /** |
|
| 2478 | - * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2479 | - * is specified, only deletes extra meta records with that value. |
|
| 2480 | - * |
|
| 2481 | - * @param string $meta_key |
|
| 2482 | - * @param mixed $meta_value |
|
| 2483 | - * @return int number of extra meta rows deleted |
|
| 2484 | - * @throws \EE_Error |
|
| 2485 | - */ |
|
| 2486 | - public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2487 | - { |
|
| 2488 | - $query_params = array( |
|
| 2489 | - array( |
|
| 2490 | - 'EXM_key' => $meta_key, |
|
| 2491 | - 'OBJ_ID' => $this->ID(), |
|
| 2492 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2493 | - ), |
|
| 2494 | - ); |
|
| 2495 | - if ($meta_value !== null) { |
|
| 2496 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2497 | - } |
|
| 2498 | - return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2499 | - } |
|
| 2500 | - |
|
| 2501 | - |
|
| 2502 | - |
|
| 2503 | - /** |
|
| 2504 | - * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2505 | - * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2506 | - * You can specify $default is case you haven't found the extra meta |
|
| 2507 | - * |
|
| 2508 | - * @param string $meta_key |
|
| 2509 | - * @param boolean $single |
|
| 2510 | - * @param mixed $default if we don't find anything, what should we return? |
|
| 2511 | - * @return mixed single value if $single; array if ! $single |
|
| 2512 | - * @throws \EE_Error |
|
| 2513 | - */ |
|
| 2514 | - public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2515 | - { |
|
| 2516 | - if ($single) { |
|
| 2517 | - $result = $this->get_first_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2518 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2519 | - return $result->value(); |
|
| 2520 | - } else { |
|
| 2521 | - return $default; |
|
| 2522 | - } |
|
| 2523 | - } else { |
|
| 2524 | - $results = $this->get_many_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2525 | - if ($results) { |
|
| 2526 | - $values = array(); |
|
| 2527 | - foreach ($results as $result) { |
|
| 2528 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2529 | - $values[$result->ID()] = $result->value(); |
|
| 2530 | - } |
|
| 2531 | - } |
|
| 2532 | - return $values; |
|
| 2533 | - } else { |
|
| 2534 | - return $default; |
|
| 2535 | - } |
|
| 2536 | - } |
|
| 2537 | - } |
|
| 2538 | - |
|
| 2539 | - |
|
| 2540 | - |
|
| 2541 | - /** |
|
| 2542 | - * Returns a simple array of all the extra meta associated with this model object. |
|
| 2543 | - * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2544 | - * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2545 | - * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2546 | - * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2547 | - * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2548 | - * finally the extra meta's value as each sub-value. (eg |
|
| 2549 | - * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2550 | - * |
|
| 2551 | - * @param boolean $one_of_each_key |
|
| 2552 | - * @return array |
|
| 2553 | - * @throws \EE_Error |
|
| 2554 | - */ |
|
| 2555 | - public function all_extra_meta_array($one_of_each_key = true) |
|
| 2556 | - { |
|
| 2557 | - $return_array = array(); |
|
| 2558 | - if ($one_of_each_key) { |
|
| 2559 | - $extra_meta_objs = $this->get_many_related('Extra_Meta', array('group_by' => 'EXM_key')); |
|
| 2560 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2561 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2562 | - $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
| 2563 | - } |
|
| 2564 | - } |
|
| 2565 | - } else { |
|
| 2566 | - $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2567 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2568 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2569 | - if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
| 2570 | - $return_array[$extra_meta_obj->key()] = array(); |
|
| 2571 | - } |
|
| 2572 | - $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
| 2573 | - } |
|
| 2574 | - } |
|
| 2575 | - } |
|
| 2576 | - return $return_array; |
|
| 2577 | - } |
|
| 2578 | - |
|
| 2579 | - |
|
| 2580 | - |
|
| 2581 | - /** |
|
| 2582 | - * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2583 | - * |
|
| 2584 | - * @return string |
|
| 2585 | - * @throws \EE_Error |
|
| 2586 | - */ |
|
| 2587 | - public function name() |
|
| 2588 | - { |
|
| 2589 | - //find a field that's not a text field |
|
| 2590 | - $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2591 | - if ($field_we_can_use) { |
|
| 2592 | - return $this->get($field_we_can_use->get_name()); |
|
| 2593 | - } else { |
|
| 2594 | - $first_few_properties = $this->model_field_array(); |
|
| 2595 | - $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2596 | - $name_parts = array(); |
|
| 2597 | - foreach ($first_few_properties as $name => $value) { |
|
| 2598 | - $name_parts[] = "$name:$value"; |
|
| 2599 | - } |
|
| 2600 | - return implode(",", $name_parts); |
|
| 2601 | - } |
|
| 2602 | - } |
|
| 2603 | - |
|
| 2604 | - |
|
| 2605 | - |
|
| 2606 | - /** |
|
| 2607 | - * in_entity_map |
|
| 2608 | - * Checks if this model object has been proven to already be in the entity map |
|
| 2609 | - * |
|
| 2610 | - * @return boolean |
|
| 2611 | - * @throws \EE_Error |
|
| 2612 | - */ |
|
| 2613 | - public function in_entity_map() |
|
| 2614 | - { |
|
| 2615 | - if ($this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this) { |
|
| 2616 | - //well, if we looked, did we find it in the entity map? |
|
| 2617 | - return true; |
|
| 2618 | - } else { |
|
| 2619 | - return false; |
|
| 2620 | - } |
|
| 2621 | - } |
|
| 2622 | - |
|
| 2623 | - |
|
| 2624 | - |
|
| 2625 | - /** |
|
| 2626 | - * refresh_from_db |
|
| 2627 | - * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 2628 | - * |
|
| 2629 | - * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 2630 | - * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 2631 | - */ |
|
| 2632 | - public function refresh_from_db() |
|
| 2633 | - { |
|
| 2634 | - if ($this->ID() && $this->in_entity_map()) { |
|
| 2635 | - $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 2636 | - } else { |
|
| 2637 | - //if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 2638 | - //if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 2639 | - //that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 2640 | - //absolutely nothing in it for this ID |
|
| 2641 | - if (WP_DEBUG) { |
|
| 2642 | - throw new EE_Error( |
|
| 2643 | - sprintf( |
|
| 2644 | - __('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 2645 | - 'event_espresso'), |
|
| 2646 | - $this->ID(), |
|
| 2647 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 2648 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 2649 | - ) |
|
| 2650 | - ); |
|
| 2651 | - } |
|
| 2652 | - } |
|
| 2653 | - } |
|
| 2654 | - |
|
| 2655 | - |
|
| 2656 | - |
|
| 2657 | - /** |
|
| 2658 | - * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 2659 | - * (probably a bad assumption they have made, oh well) |
|
| 2660 | - * |
|
| 2661 | - * @return string |
|
| 2662 | - */ |
|
| 2663 | - public function __toString() |
|
| 2664 | - { |
|
| 2665 | - try { |
|
| 2666 | - return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 2667 | - } catch (Exception $e) { |
|
| 2668 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2669 | - return ''; |
|
| 2670 | - } |
|
| 2671 | - } |
|
| 2672 | - |
|
| 2673 | - |
|
| 2674 | - |
|
| 2675 | - /** |
|
| 2676 | - * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 2677 | - * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 2678 | - * This means if we have made changes to those related model objects, and want to unserialize |
|
| 2679 | - * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 2680 | - * Instead, those related model objects should be directly serialized and stored. |
|
| 2681 | - * Eg, the following won't work: |
|
| 2682 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2683 | - * $att = $reg->attendee(); |
|
| 2684 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2685 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 2686 | - * //END REQUEST |
|
| 2687 | - * //START NEXT REQUEST |
|
| 2688 | - * $reg = get_option( 'my_option' ); |
|
| 2689 | - * $reg->attendee()->save(); |
|
| 2690 | - * And would need to be replace with: |
|
| 2691 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2692 | - * $att = $reg->attendee(); |
|
| 2693 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2694 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 2695 | - * //END REQUEST |
|
| 2696 | - * //START NEXT REQUEST |
|
| 2697 | - * $att = get_option( 'my_option' ); |
|
| 2698 | - * $att->save(); |
|
| 2699 | - * |
|
| 2700 | - * @return array |
|
| 2701 | - * @throws \EE_Error |
|
| 2702 | - */ |
|
| 2703 | - public function __sleep() |
|
| 2704 | - { |
|
| 2705 | - $model = $this->get_model(); |
|
| 2706 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 2707 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 2708 | - $classname = 'EE_' . $model->get_this_model_name(); |
|
| 2709 | - if ( |
|
| 2710 | - $this->get_one_from_cache($relation_name) instanceof $classname |
|
| 2711 | - && $this->get_one_from_cache($relation_name)->ID() |
|
| 2712 | - ) { |
|
| 2713 | - $this->clear_cache($relation_name, $this->get_one_from_cache($relation_name)->ID()); |
|
| 2714 | - } |
|
| 2715 | - } |
|
| 2716 | - } |
|
| 2717 | - $this->_props_n_values_provided_in_constructor = array(); |
|
| 2718 | - $properties_to_serialize = get_object_vars($this); |
|
| 2719 | - //don't serialize the model. It's big and that risks recursion |
|
| 2720 | - unset($properties_to_serialize['_model']); |
|
| 2721 | - return array_keys($properties_to_serialize); |
|
| 2722 | - } |
|
| 2723 | - |
|
| 2724 | - |
|
| 2725 | - |
|
| 2726 | - /** |
|
| 2727 | - * restore _props_n_values_provided_in_constructor |
|
| 2728 | - * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 2729 | - * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 2730 | - * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 2731 | - */ |
|
| 2732 | - public function __wakeup() |
|
| 2733 | - { |
|
| 2734 | - $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 2735 | - } |
|
| 28 | + /** |
|
| 29 | + * This is an array of the original properties and values provided during construction |
|
| 30 | + * of this model object. (keys are model field names, values are their values). |
|
| 31 | + * This list is important to remember so that when we are merging data from the db, we know |
|
| 32 | + * which values to override and which to not override. |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + protected $_props_n_values_provided_in_constructor; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Timezone |
|
| 40 | + * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 41 | + * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 42 | + * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 43 | + * access to it. |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + protected $_timezone; |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * date format |
|
| 53 | + * pattern or format for displaying dates |
|
| 54 | + * |
|
| 55 | + * @var string $_dt_frmt |
|
| 56 | + */ |
|
| 57 | + protected $_dt_frmt; |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * time format |
|
| 63 | + * pattern or format for displaying time |
|
| 64 | + * |
|
| 65 | + * @var string $_tm_frmt |
|
| 66 | + */ |
|
| 67 | + protected $_tm_frmt; |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 73 | + * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 74 | + * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 75 | + * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 76 | + * |
|
| 77 | + * @var array |
|
| 78 | + */ |
|
| 79 | + protected $_cached_properties = array(); |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 83 | + * single |
|
| 84 | + * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 85 | + * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 86 | + * all others have an array) |
|
| 87 | + * |
|
| 88 | + * @var array |
|
| 89 | + */ |
|
| 90 | + protected $_model_relations = array(); |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 94 | + * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 95 | + * |
|
| 96 | + * @var array |
|
| 97 | + */ |
|
| 98 | + protected $_fields = array(); |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 102 | + * For example, we might create model objects intended to only be used for the duration |
|
| 103 | + * of this request and to be thrown away, and if they were accidentally saved |
|
| 104 | + * it would be a bug. |
|
| 105 | + */ |
|
| 106 | + protected $_allow_persist = true; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 110 | + */ |
|
| 111 | + protected $_has_changes = false; |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @var EEM_Base |
|
| 115 | + */ |
|
| 116 | + protected $_model; |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 122 | + * play nice |
|
| 123 | + * |
|
| 124 | + * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 125 | + * layer of the model's _fields array, (eg, EVT_ID, |
|
| 126 | + * TXN_amount, QST_name, etc) and values are their values |
|
| 127 | + * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 128 | + * corresponding db model or not. |
|
| 129 | + * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 130 | + * be in when instantiating a EE_Base_Class object. |
|
| 131 | + * @param array $date_formats An array of date formats to set on construct where first |
|
| 132 | + * value is the date_format and second value is the time |
|
| 133 | + * format. |
|
| 134 | + * @throws EE_Error |
|
| 135 | + */ |
|
| 136 | + protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 137 | + { |
|
| 138 | + $className = get_class($this); |
|
| 139 | + do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 140 | + $model = $this->get_model(); |
|
| 141 | + $model_fields = $model->field_settings(false); |
|
| 142 | + // ensure $fieldValues is an array |
|
| 143 | + $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 144 | + // EEH_Debug_Tools::printr( $fieldValues, '$fieldValues <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 145 | + // verify client code has not passed any invalid field names |
|
| 146 | + foreach ($fieldValues as $field_name => $field_value) { |
|
| 147 | + if ( ! isset($model_fields[$field_name])) { |
|
| 148 | + throw new EE_Error(sprintf(__("Invalid field (%s) passed to constructor of %s. Allowed fields are :%s", |
|
| 149 | + "event_espresso"), $field_name, get_class($this), implode(", ", array_keys($model_fields)))); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + // EEH_Debug_Tools::printr( $model_fields, '$model_fields <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 153 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 154 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 155 | + list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 156 | + } else { |
|
| 157 | + //set default formats for date and time |
|
| 158 | + $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d'); |
|
| 159 | + $this->_tm_frmt = (string)get_option('time_format', 'g:i a'); |
|
| 160 | + } |
|
| 161 | + //if db model is instantiating |
|
| 162 | + if ($bydb) { |
|
| 163 | + //client code has indicated these field values are from the database |
|
| 164 | + foreach ($model_fields as $fieldName => $field) { |
|
| 165 | + $this->set_from_db($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null); |
|
| 166 | + } |
|
| 167 | + } else { |
|
| 168 | + //we're constructing a brand |
|
| 169 | + //new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 170 | + foreach ($model_fields as $fieldName => $field) { |
|
| 171 | + $this->set($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, true); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + //remember what values were passed to this constructor |
|
| 175 | + $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 176 | + //remember in entity mapper |
|
| 177 | + if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 178 | + $model->add_to_entity_map($this); |
|
| 179 | + } |
|
| 180 | + //setup all the relations |
|
| 181 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 182 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 183 | + $this->_model_relations[$relation_name] = null; |
|
| 184 | + } else { |
|
| 185 | + $this->_model_relations[$relation_name] = array(); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + /** |
|
| 189 | + * Action done at the end of each model object construction |
|
| 190 | + * |
|
| 191 | + * @param EE_Base_Class $this the model object just created |
|
| 192 | + */ |
|
| 193 | + do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 200 | + * |
|
| 201 | + * @return boolean |
|
| 202 | + */ |
|
| 203 | + public function allow_persist() |
|
| 204 | + { |
|
| 205 | + return $this->_allow_persist; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 212 | + * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 213 | + * you got new information that somehow made you change your mind. |
|
| 214 | + * |
|
| 215 | + * @param boolean $allow_persist |
|
| 216 | + * @return boolean |
|
| 217 | + */ |
|
| 218 | + public function set_allow_persist($allow_persist) |
|
| 219 | + { |
|
| 220 | + return $this->_allow_persist = $allow_persist; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Gets the field's original value when this object was constructed during this request. |
|
| 227 | + * This can be helpful when determining if a model object has changed or not |
|
| 228 | + * |
|
| 229 | + * @param string $field_name |
|
| 230 | + * @return mixed|null |
|
| 231 | + * @throws \EE_Error |
|
| 232 | + */ |
|
| 233 | + public function get_original($field_name) |
|
| 234 | + { |
|
| 235 | + if (isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
| 236 | + && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 237 | + ) { |
|
| 238 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
| 239 | + } else { |
|
| 240 | + return null; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param EE_Base_Class $obj |
|
| 248 | + * @return string |
|
| 249 | + */ |
|
| 250 | + public function get_class($obj) |
|
| 251 | + { |
|
| 252 | + return get_class($obj); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Overrides parent because parent expects old models. |
|
| 259 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 260 | + * |
|
| 261 | + * @param string $field_name |
|
| 262 | + * @param mixed $field_value |
|
| 263 | + * @param bool $use_default |
|
| 264 | + * @throws \EE_Error |
|
| 265 | + */ |
|
| 266 | + public function set($field_name, $field_value, $use_default = false) |
|
| 267 | + { |
|
| 268 | + // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 269 | + // then don't do anything |
|
| 270 | + if ( |
|
| 271 | + ! $use_default |
|
| 272 | + && $this->_fields[$field_name] === $field_value |
|
| 273 | + && $this->ID() |
|
| 274 | + ) { |
|
| 275 | + return; |
|
| 276 | + } |
|
| 277 | + $model = $this->get_model(); |
|
| 278 | + $this->_has_changes = true; |
|
| 279 | + $field_obj = $model->field_settings_for($field_name); |
|
| 280 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 281 | + // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 282 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 283 | + $field_obj->set_timezone($this->_timezone); |
|
| 284 | + $field_obj->set_date_format($this->_dt_frmt); |
|
| 285 | + $field_obj->set_time_format($this->_tm_frmt); |
|
| 286 | + } |
|
| 287 | + $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 288 | + //should the value be null? |
|
| 289 | + if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 290 | + $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
| 291 | + /** |
|
| 292 | + * To save having to refactor all the models, if a default value is used for a |
|
| 293 | + * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 294 | + * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 295 | + * object. |
|
| 296 | + * |
|
| 297 | + * @since 4.6.10+ |
|
| 298 | + */ |
|
| 299 | + if ( |
|
| 300 | + $field_obj instanceof EE_Datetime_Field |
|
| 301 | + && $this->_fields[$field_name] !== null |
|
| 302 | + && ! $this->_fields[$field_name] instanceof DateTime |
|
| 303 | + ) { |
|
| 304 | + empty($this->_fields[$field_name]) |
|
| 305 | + ? $this->set($field_name, time()) |
|
| 306 | + : $this->set($field_name, $this->_fields[$field_name]); |
|
| 307 | + } |
|
| 308 | + } else { |
|
| 309 | + $this->_fields[$field_name] = $holder_of_value; |
|
| 310 | + } |
|
| 311 | + //if we're not in the constructor... |
|
| 312 | + //now check if what we set was a primary key |
|
| 313 | + if ( |
|
| 314 | + //note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 315 | + $this->_props_n_values_provided_in_constructor |
|
| 316 | + && $field_value |
|
| 317 | + && $field_name === $model->primary_key_name() |
|
| 318 | + ) { |
|
| 319 | + //if so, we want all this object's fields to be filled either with |
|
| 320 | + //what we've explicitly set on this model |
|
| 321 | + //or what we have in the db |
|
| 322 | + // echo "setting primary key!"; |
|
| 323 | + $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 324 | + $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 325 | + foreach ($fields_on_model as $field_obj) { |
|
| 326 | + if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 327 | + && $field_obj->get_name() !== $field_name |
|
| 328 | + ) { |
|
| 329 | + $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + //oh this model object has an ID? well make sure its in the entity mapper |
|
| 333 | + $model->add_to_entity_map($this); |
|
| 334 | + } |
|
| 335 | + //let's unset any cache for this field_name from the $_cached_properties property. |
|
| 336 | + $this->_clear_cached_property($field_name); |
|
| 337 | + } else { |
|
| 338 | + throw new EE_Error(sprintf(__("A valid EE_Model_Field_Base could not be found for the given field name: %s", |
|
| 339 | + "event_espresso"), $field_name)); |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * This sets the field value on the db column if it exists for the given $column_name or |
|
| 347 | + * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 348 | + * |
|
| 349 | + * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 350 | + * @param string $field_name Must be the exact column name. |
|
| 351 | + * @param mixed $field_value The value to set. |
|
| 352 | + * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 353 | + * @throws \EE_Error |
|
| 354 | + */ |
|
| 355 | + public function set_field_or_extra_meta($field_name, $field_value) |
|
| 356 | + { |
|
| 357 | + if ($this->get_model()->has_field($field_name)) { |
|
| 358 | + $this->set($field_name, $field_value); |
|
| 359 | + return true; |
|
| 360 | + } else { |
|
| 361 | + //ensure this object is saved first so that extra meta can be properly related. |
|
| 362 | + $this->save(); |
|
| 363 | + return $this->update_extra_meta($field_name, $field_value); |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * This retrieves the value of the db column set on this class or if that's not present |
|
| 371 | + * it will attempt to retrieve from extra_meta if found. |
|
| 372 | + * Example Usage: |
|
| 373 | + * Via EE_Message child class: |
|
| 374 | + * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 375 | + * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 376 | + * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 377 | + * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 378 | + * value for those extra fields dynamically via the EE_message object. |
|
| 379 | + * |
|
| 380 | + * @param string $field_name expecting the fully qualified field name. |
|
| 381 | + * @return mixed|null value for the field if found. null if not found. |
|
| 382 | + * @throws \EE_Error |
|
| 383 | + */ |
|
| 384 | + public function get_field_or_extra_meta($field_name) |
|
| 385 | + { |
|
| 386 | + if ($this->get_model()->has_field($field_name)) { |
|
| 387 | + $column_value = $this->get($field_name); |
|
| 388 | + } else { |
|
| 389 | + //This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 390 | + $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 391 | + } |
|
| 392 | + return $column_value; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 399 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 400 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 401 | + * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 402 | + * |
|
| 403 | + * @access public |
|
| 404 | + * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 405 | + * @return void |
|
| 406 | + * @throws \EE_Error |
|
| 407 | + */ |
|
| 408 | + public function set_timezone($timezone = '') |
|
| 409 | + { |
|
| 410 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 411 | + //make sure we clear all cached properties because they won't be relevant now |
|
| 412 | + $this->_clear_cached_properties(); |
|
| 413 | + //make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 414 | + $model_fields = $this->get_model()->field_settings(false); |
|
| 415 | + foreach ($model_fields as $field_name => $field_obj) { |
|
| 416 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 417 | + $field_obj->set_timezone($this->_timezone); |
|
| 418 | + if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
| 419 | + $this->_fields[$field_name]->setTimezone(new DateTimeZone($this->_timezone)); |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * This just returns whatever is set for the current timezone. |
|
| 429 | + * |
|
| 430 | + * @access public |
|
| 431 | + * @return string timezone string |
|
| 432 | + */ |
|
| 433 | + public function get_timezone() |
|
| 434 | + { |
|
| 435 | + return $this->_timezone; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 442 | + * internally instead of wp set date format options |
|
| 443 | + * |
|
| 444 | + * @since 4.6 |
|
| 445 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 446 | + */ |
|
| 447 | + public function set_date_format($format) |
|
| 448 | + { |
|
| 449 | + $this->_dt_frmt = $format; |
|
| 450 | + //clear cached_properties because they won't be relevant now. |
|
| 451 | + $this->_clear_cached_properties(); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 458 | + * class internally instead of wp set time format options. |
|
| 459 | + * |
|
| 460 | + * @since 4.6 |
|
| 461 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 462 | + */ |
|
| 463 | + public function set_time_format($format) |
|
| 464 | + { |
|
| 465 | + $this->_tm_frmt = $format; |
|
| 466 | + //clear cached_properties because they won't be relevant now. |
|
| 467 | + $this->_clear_cached_properties(); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * This returns the current internal set format for the date and time formats. |
|
| 474 | + * |
|
| 475 | + * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 476 | + * where the first value is the date format and the second value is the time format. |
|
| 477 | + * @return mixed string|array |
|
| 478 | + */ |
|
| 479 | + public function get_format($full = true) |
|
| 480 | + { |
|
| 481 | + return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * cache |
|
| 488 | + * stores the passed model object on the current model object. |
|
| 489 | + * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 490 | + * |
|
| 491 | + * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 492 | + * 'Registration' associated with this model object |
|
| 493 | + * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 494 | + * that could be a payment or a registration) |
|
| 495 | + * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 496 | + * items which will be stored in an array on this object |
|
| 497 | + * @throws EE_Error |
|
| 498 | + * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 499 | + * related thing, no array) |
|
| 500 | + */ |
|
| 501 | + public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 502 | + { |
|
| 503 | + // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 504 | + if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
| 505 | + return false; |
|
| 506 | + } |
|
| 507 | + // also get "how" the object is related, or throw an error |
|
| 508 | + if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 509 | + throw new EE_Error(sprintf(__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 510 | + $relationName, get_class($this))); |
|
| 511 | + } |
|
| 512 | + // how many things are related ? |
|
| 513 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 514 | + // if it's a "belongs to" relationship, then there's only one related model object eg, if this is a registration, there's only 1 attendee for it |
|
| 515 | + // so for these model objects just set it to be cached |
|
| 516 | + $this->_model_relations[$relationName] = $object_to_cache; |
|
| 517 | + $return = true; |
|
| 518 | + } else { |
|
| 519 | + // otherwise, this is the "many" side of a one to many relationship, so we'll add the object to the array of related objects for that type. |
|
| 520 | + // eg: if this is an event, there are many registrations for that event, so we cache the registrations in an array |
|
| 521 | + if ( ! is_array($this->_model_relations[$relationName])) { |
|
| 522 | + // if for some reason, the cached item is a model object, then stick that in the array, otherwise start with an empty array |
|
| 523 | + $this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class |
|
| 524 | + ? array($this->_model_relations[$relationName]) : array(); |
|
| 525 | + } |
|
| 526 | + // first check for a cache_id which is normally empty |
|
| 527 | + if ( ! empty($cache_id)) { |
|
| 528 | + // if the cache_id exists, then it means we are purposely trying to cache this with a known key that can then be used to retrieve the object later on |
|
| 529 | + $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
| 530 | + $return = $cache_id; |
|
| 531 | + } elseif ($object_to_cache->ID()) { |
|
| 532 | + // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 533 | + $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
| 534 | + $return = $object_to_cache->ID(); |
|
| 535 | + } else { |
|
| 536 | + // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 537 | + $this->_model_relations[$relationName][] = $object_to_cache; |
|
| 538 | + // move the internal pointer to the end of the array |
|
| 539 | + end($this->_model_relations[$relationName]); |
|
| 540 | + // and grab the key so that we can return it |
|
| 541 | + $return = key($this->_model_relations[$relationName]); |
|
| 542 | + } |
|
| 543 | + } |
|
| 544 | + return $return; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * For adding an item to the cached_properties property. |
|
| 551 | + * |
|
| 552 | + * @access protected |
|
| 553 | + * @param string $fieldname the property item the corresponding value is for. |
|
| 554 | + * @param mixed $value The value we are caching. |
|
| 555 | + * @param string|null $cache_type |
|
| 556 | + * @return void |
|
| 557 | + * @throws \EE_Error |
|
| 558 | + */ |
|
| 559 | + protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 560 | + { |
|
| 561 | + //first make sure this property exists |
|
| 562 | + $this->get_model()->field_settings_for($fieldname); |
|
| 563 | + $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 564 | + $this->_cached_properties[$fieldname][$cache_type] = $value; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 571 | + * This also SETS the cache if we return the actual property! |
|
| 572 | + * |
|
| 573 | + * @param string $fieldname the name of the property we're trying to retrieve |
|
| 574 | + * @param bool $pretty |
|
| 575 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 576 | + * (in cases where the same property may be used for different outputs |
|
| 577 | + * - i.e. datetime, money etc.) |
|
| 578 | + * It can also accept certain pre-defined "schema" strings |
|
| 579 | + * to define how to output the property. |
|
| 580 | + * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 581 | + * @return mixed whatever the value for the property is we're retrieving |
|
| 582 | + * @throws \EE_Error |
|
| 583 | + */ |
|
| 584 | + protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 585 | + { |
|
| 586 | + //verify the field exists |
|
| 587 | + $model = $this->get_model(); |
|
| 588 | + $model->field_settings_for($fieldname); |
|
| 589 | + $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 590 | + $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 591 | + if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
|
| 592 | + return $this->_cached_properties[$fieldname][$cache_type]; |
|
| 593 | + } |
|
| 594 | + $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 595 | + $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 596 | + return $value; |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * If the cache didn't fetch the needed item, this fetches it. |
|
| 603 | + * @param string $fieldname |
|
| 604 | + * @param bool $pretty |
|
| 605 | + * @param string $extra_cache_ref |
|
| 606 | + * @return mixed |
|
| 607 | + */ |
|
| 608 | + protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 609 | + { |
|
| 610 | + $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 611 | + // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 612 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 613 | + $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 614 | + } |
|
| 615 | + if ( ! isset($this->_fields[$fieldname])) { |
|
| 616 | + $this->_fields[$fieldname] = null; |
|
| 617 | + } |
|
| 618 | + $value = $pretty |
|
| 619 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref) |
|
| 620 | + : $field_obj->prepare_for_get($this->_fields[$fieldname]); |
|
| 621 | + return $value; |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 628 | + * |
|
| 629 | + * @param \EE_Datetime_Field $datetime_field |
|
| 630 | + * @param bool $pretty |
|
| 631 | + * @param null $date_or_time |
|
| 632 | + * @return void |
|
| 633 | + * @throws \EE_Error |
|
| 634 | + */ |
|
| 635 | + protected function _prepare_datetime_field( |
|
| 636 | + EE_Datetime_Field $datetime_field, |
|
| 637 | + $pretty = false, |
|
| 638 | + $date_or_time = null |
|
| 639 | + ) { |
|
| 640 | + $datetime_field->set_timezone($this->_timezone); |
|
| 641 | + $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 642 | + $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 643 | + //set the output returned |
|
| 644 | + switch ($date_or_time) { |
|
| 645 | + case 'D' : |
|
| 646 | + $datetime_field->set_date_time_output('date'); |
|
| 647 | + break; |
|
| 648 | + case 'T' : |
|
| 649 | + $datetime_field->set_date_time_output('time'); |
|
| 650 | + break; |
|
| 651 | + default : |
|
| 652 | + $datetime_field->set_date_time_output(); |
|
| 653 | + } |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * This just takes care of clearing out the cached_properties |
|
| 660 | + * |
|
| 661 | + * @return void |
|
| 662 | + */ |
|
| 663 | + protected function _clear_cached_properties() |
|
| 664 | + { |
|
| 665 | + $this->_cached_properties = array(); |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * This just clears out ONE property if it exists in the cache |
|
| 672 | + * |
|
| 673 | + * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 674 | + * @return void |
|
| 675 | + */ |
|
| 676 | + protected function _clear_cached_property($property_name) |
|
| 677 | + { |
|
| 678 | + if (isset($this->_cached_properties[$property_name])) { |
|
| 679 | + unset($this->_cached_properties[$property_name]); |
|
| 680 | + } |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + |
|
| 684 | + |
|
| 685 | + /** |
|
| 686 | + * Ensures that this related thing is a model object. |
|
| 687 | + * |
|
| 688 | + * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 689 | + * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 690 | + * @return EE_Base_Class |
|
| 691 | + * @throws \EE_Error |
|
| 692 | + */ |
|
| 693 | + protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 694 | + { |
|
| 695 | + $other_model_instance = self::_get_model_instance_with_name( |
|
| 696 | + self::_get_model_classname($model_name), |
|
| 697 | + $this->_timezone |
|
| 698 | + ); |
|
| 699 | + return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + |
|
| 703 | + |
|
| 704 | + /** |
|
| 705 | + * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 706 | + * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 707 | + * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 708 | + * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 709 | + * |
|
| 710 | + * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 711 | + * Eg 'Registration' |
|
| 712 | + * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 713 | + * if you intend to use $clear_all = TRUE, or the relation only |
|
| 714 | + * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 715 | + * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 716 | + * this is HasMany or HABTM. |
|
| 717 | + * @throws EE_Error |
|
| 718 | + * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 719 | + * relation from all |
|
| 720 | + */ |
|
| 721 | + public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 722 | + { |
|
| 723 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 724 | + $index_in_cache = ''; |
|
| 725 | + if ( ! $relationship_to_model) { |
|
| 726 | + throw new EE_Error( |
|
| 727 | + sprintf( |
|
| 728 | + __("There is no relationship to %s on a %s. Cannot clear that cache", 'event_espresso'), |
|
| 729 | + $relationName, |
|
| 730 | + get_class($this) |
|
| 731 | + ) |
|
| 732 | + ); |
|
| 733 | + } |
|
| 734 | + if ($clear_all) { |
|
| 735 | + $obj_removed = true; |
|
| 736 | + $this->_model_relations[$relationName] = null; |
|
| 737 | + } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 738 | + $obj_removed = $this->_model_relations[$relationName]; |
|
| 739 | + $this->_model_relations[$relationName] = null; |
|
| 740 | + } else { |
|
| 741 | + if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 742 | + && $object_to_remove_or_index_into_array->ID() |
|
| 743 | + ) { |
|
| 744 | + $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 745 | + if (is_array($this->_model_relations[$relationName]) |
|
| 746 | + && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
| 747 | + ) { |
|
| 748 | + $index_found_at = null; |
|
| 749 | + //find this object in the array even though it has a different key |
|
| 750 | + foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
| 751 | + if ( |
|
| 752 | + $obj instanceof EE_Base_Class |
|
| 753 | + && ( |
|
| 754 | + $obj == $object_to_remove_or_index_into_array |
|
| 755 | + || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 756 | + ) |
|
| 757 | + ) { |
|
| 758 | + $index_found_at = $index; |
|
| 759 | + break; |
|
| 760 | + } |
|
| 761 | + } |
|
| 762 | + if ($index_found_at) { |
|
| 763 | + $index_in_cache = $index_found_at; |
|
| 764 | + } else { |
|
| 765 | + //it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 766 | + //if it wasn't in it to begin with. So we're done |
|
| 767 | + return $object_to_remove_or_index_into_array; |
|
| 768 | + } |
|
| 769 | + } |
|
| 770 | + } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 771 | + //so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 772 | + foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 773 | + if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 774 | + $index_in_cache = $index; |
|
| 775 | + } |
|
| 776 | + } |
|
| 777 | + } else { |
|
| 778 | + $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 779 | + } |
|
| 780 | + //supposedly we've found it. But it could just be that the client code |
|
| 781 | + //provided a bad index/object |
|
| 782 | + if ( |
|
| 783 | + isset( |
|
| 784 | + $this->_model_relations[$relationName], |
|
| 785 | + $this->_model_relations[$relationName][$index_in_cache] |
|
| 786 | + ) |
|
| 787 | + ) { |
|
| 788 | + $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
| 789 | + unset($this->_model_relations[$relationName][$index_in_cache]); |
|
| 790 | + } else { |
|
| 791 | + //that thing was never cached anyways. |
|
| 792 | + $obj_removed = null; |
|
| 793 | + } |
|
| 794 | + } |
|
| 795 | + return $obj_removed; |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + |
|
| 799 | + |
|
| 800 | + /** |
|
| 801 | + * update_cache_after_object_save |
|
| 802 | + * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 803 | + * obtained after being saved to the db |
|
| 804 | + * |
|
| 805 | + * @param string $relationName - the type of object that is cached |
|
| 806 | + * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 807 | + * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 808 | + * @return boolean TRUE on success, FALSE on fail |
|
| 809 | + * @throws \EE_Error |
|
| 810 | + */ |
|
| 811 | + public function update_cache_after_object_save( |
|
| 812 | + $relationName, |
|
| 813 | + EE_Base_Class $newly_saved_object, |
|
| 814 | + $current_cache_id = '' |
|
| 815 | + ) { |
|
| 816 | + // verify that incoming object is of the correct type |
|
| 817 | + $obj_class = 'EE_' . $relationName; |
|
| 818 | + if ($newly_saved_object instanceof $obj_class) { |
|
| 819 | + /* @type EE_Base_Class $newly_saved_object */ |
|
| 820 | + // now get the type of relation |
|
| 821 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 822 | + // if this is a 1:1 relationship |
|
| 823 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 824 | + // then just replace the cached object with the newly saved object |
|
| 825 | + $this->_model_relations[$relationName] = $newly_saved_object; |
|
| 826 | + return true; |
|
| 827 | + // or if it's some kind of sordid feral polyamorous relationship... |
|
| 828 | + } elseif (is_array($this->_model_relations[$relationName]) |
|
| 829 | + && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
| 830 | + ) { |
|
| 831 | + // then remove the current cached item |
|
| 832 | + unset($this->_model_relations[$relationName][$current_cache_id]); |
|
| 833 | + // and cache the newly saved object using it's new ID |
|
| 834 | + $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
| 835 | + return true; |
|
| 836 | + } |
|
| 837 | + } |
|
| 838 | + return false; |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + |
|
| 842 | + |
|
| 843 | + /** |
|
| 844 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 845 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 846 | + * |
|
| 847 | + * @param string $relationName |
|
| 848 | + * @return EE_Base_Class |
|
| 849 | + */ |
|
| 850 | + public function get_one_from_cache($relationName) |
|
| 851 | + { |
|
| 852 | + $cached_array_or_object = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] |
|
| 853 | + : null; |
|
| 854 | + if (is_array($cached_array_or_object)) { |
|
| 855 | + return array_shift($cached_array_or_object); |
|
| 856 | + } else { |
|
| 857 | + return $cached_array_or_object; |
|
| 858 | + } |
|
| 859 | + } |
|
| 860 | + |
|
| 861 | + |
|
| 862 | + |
|
| 863 | + /** |
|
| 864 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 865 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 866 | + * |
|
| 867 | + * @param string $relationName |
|
| 868 | + * @throws \EE_Error |
|
| 869 | + * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 870 | + */ |
|
| 871 | + public function get_all_from_cache($relationName) |
|
| 872 | + { |
|
| 873 | + $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array(); |
|
| 874 | + // if the result is not an array, but exists, make it an array |
|
| 875 | + $objects = is_array($objects) ? $objects : array($objects); |
|
| 876 | + //bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 877 | + //basically, if this model object was stored in the session, and these cached model objects |
|
| 878 | + //already have IDs, let's make sure they're in their model's entity mapper |
|
| 879 | + //otherwise we will have duplicates next time we call |
|
| 880 | + // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 881 | + $model = EE_Registry::instance()->load_model($relationName); |
|
| 882 | + foreach ($objects as $model_object) { |
|
| 883 | + if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 884 | + //ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 885 | + if ($model_object->ID()) { |
|
| 886 | + $model->add_to_entity_map($model_object); |
|
| 887 | + } |
|
| 888 | + } else { |
|
| 889 | + throw new EE_Error( |
|
| 890 | + sprintf( |
|
| 891 | + __( |
|
| 892 | + 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 893 | + 'event_espresso' |
|
| 894 | + ), |
|
| 895 | + $relationName, |
|
| 896 | + gettype($model_object) |
|
| 897 | + ) |
|
| 898 | + ); |
|
| 899 | + } |
|
| 900 | + } |
|
| 901 | + return $objects; |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + |
|
| 905 | + |
|
| 906 | + /** |
|
| 907 | + * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 908 | + * matching the given query conditions. |
|
| 909 | + * |
|
| 910 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 911 | + * @param int $limit How many objects to return. |
|
| 912 | + * @param array $query_params Any additional conditions on the query. |
|
| 913 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 914 | + * you can indicate just the columns you want returned |
|
| 915 | + * @return array|EE_Base_Class[] |
|
| 916 | + * @throws \EE_Error |
|
| 917 | + */ |
|
| 918 | + public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 919 | + { |
|
| 920 | + $model = $this->get_model(); |
|
| 921 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 922 | + ? $model->get_primary_key_field()->get_name() |
|
| 923 | + : $field_to_order_by; |
|
| 924 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 925 | + if (empty($field) || empty($current_value)) { |
|
| 926 | + return array(); |
|
| 927 | + } |
|
| 928 | + return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 929 | + } |
|
| 930 | + |
|
| 931 | + |
|
| 932 | + |
|
| 933 | + /** |
|
| 934 | + * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 935 | + * matching the given query conditions. |
|
| 936 | + * |
|
| 937 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 938 | + * @param int $limit How many objects to return. |
|
| 939 | + * @param array $query_params Any additional conditions on the query. |
|
| 940 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 941 | + * you can indicate just the columns you want returned |
|
| 942 | + * @return array|EE_Base_Class[] |
|
| 943 | + * @throws \EE_Error |
|
| 944 | + */ |
|
| 945 | + public function previous_x( |
|
| 946 | + $field_to_order_by = null, |
|
| 947 | + $limit = 1, |
|
| 948 | + $query_params = array(), |
|
| 949 | + $columns_to_select = null |
|
| 950 | + ) { |
|
| 951 | + $model = $this->get_model(); |
|
| 952 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 953 | + ? $model->get_primary_key_field()->get_name() |
|
| 954 | + : $field_to_order_by; |
|
| 955 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 956 | + if (empty($field) || empty($current_value)) { |
|
| 957 | + return array(); |
|
| 958 | + } |
|
| 959 | + return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + |
|
| 963 | + |
|
| 964 | + /** |
|
| 965 | + * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 966 | + * matching the given query conditions. |
|
| 967 | + * |
|
| 968 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 969 | + * @param array $query_params Any additional conditions on the query. |
|
| 970 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 971 | + * you can indicate just the columns you want returned |
|
| 972 | + * @return array|EE_Base_Class |
|
| 973 | + * @throws \EE_Error |
|
| 974 | + */ |
|
| 975 | + public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 976 | + { |
|
| 977 | + $model = $this->get_model(); |
|
| 978 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 979 | + ? $model->get_primary_key_field()->get_name() |
|
| 980 | + : $field_to_order_by; |
|
| 981 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 982 | + if (empty($field) || empty($current_value)) { |
|
| 983 | + return array(); |
|
| 984 | + } |
|
| 985 | + return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 992 | + * matching the given query conditions. |
|
| 993 | + * |
|
| 994 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 995 | + * @param array $query_params Any additional conditions on the query. |
|
| 996 | + * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 997 | + * you can indicate just the column you want returned |
|
| 998 | + * @return array|EE_Base_Class |
|
| 999 | + * @throws \EE_Error |
|
| 1000 | + */ |
|
| 1001 | + public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1002 | + { |
|
| 1003 | + $model = $this->get_model(); |
|
| 1004 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1005 | + ? $model->get_primary_key_field()->get_name() |
|
| 1006 | + : $field_to_order_by; |
|
| 1007 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1008 | + if (empty($field) || empty($current_value)) { |
|
| 1009 | + return array(); |
|
| 1010 | + } |
|
| 1011 | + return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1012 | + } |
|
| 1013 | + |
|
| 1014 | + |
|
| 1015 | + |
|
| 1016 | + /** |
|
| 1017 | + * Overrides parent because parent expects old models. |
|
| 1018 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1019 | + * |
|
| 1020 | + * @param string $field_name |
|
| 1021 | + * @param mixed $field_value_from_db |
|
| 1022 | + * @throws \EE_Error |
|
| 1023 | + */ |
|
| 1024 | + public function set_from_db($field_name, $field_value_from_db) |
|
| 1025 | + { |
|
| 1026 | + $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1027 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1028 | + //you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1029 | + //eg, a CPT model object could have an entry in the posts table, but no |
|
| 1030 | + //entry in the meta table. Meaning that all its columns in the meta table |
|
| 1031 | + //are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1032 | + if ($field_value_from_db === null) { |
|
| 1033 | + if ($field_obj->is_nullable()) { |
|
| 1034 | + //if the field allows nulls, then let it be null |
|
| 1035 | + $field_value = null; |
|
| 1036 | + } else { |
|
| 1037 | + $field_value = $field_obj->get_default_value(); |
|
| 1038 | + } |
|
| 1039 | + } else { |
|
| 1040 | + $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1041 | + } |
|
| 1042 | + $this->_fields[$field_name] = $field_value; |
|
| 1043 | + $this->_clear_cached_property($field_name); |
|
| 1044 | + } |
|
| 1045 | + } |
|
| 1046 | + |
|
| 1047 | + |
|
| 1048 | + |
|
| 1049 | + /** |
|
| 1050 | + * verifies that the specified field is of the correct type |
|
| 1051 | + * |
|
| 1052 | + * @param string $field_name |
|
| 1053 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1054 | + * (in cases where the same property may be used for different outputs |
|
| 1055 | + * - i.e. datetime, money etc.) |
|
| 1056 | + * @return mixed |
|
| 1057 | + * @throws \EE_Error |
|
| 1058 | + */ |
|
| 1059 | + public function get($field_name, $extra_cache_ref = null) |
|
| 1060 | + { |
|
| 1061 | + return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1062 | + } |
|
| 1063 | + |
|
| 1064 | + |
|
| 1065 | + |
|
| 1066 | + /** |
|
| 1067 | + * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1068 | + * |
|
| 1069 | + * @param string $field_name A valid fieldname |
|
| 1070 | + * @return mixed Whatever the raw value stored on the property is. |
|
| 1071 | + * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1072 | + */ |
|
| 1073 | + public function get_raw($field_name) |
|
| 1074 | + { |
|
| 1075 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1076 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
| 1077 | + ? $this->_fields[$field_name]->format('U') |
|
| 1078 | + : $this->_fields[$field_name]; |
|
| 1079 | + } |
|
| 1080 | + |
|
| 1081 | + |
|
| 1082 | + |
|
| 1083 | + /** |
|
| 1084 | + * This is used to return the internal DateTime object used for a field that is a |
|
| 1085 | + * EE_Datetime_Field. |
|
| 1086 | + * |
|
| 1087 | + * @param string $field_name The field name retrieving the DateTime object. |
|
| 1088 | + * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1089 | + * @throws \EE_Error |
|
| 1090 | + * an error is set and false returned. If the field IS an |
|
| 1091 | + * EE_Datetime_Field and but the field value is null, then |
|
| 1092 | + * just null is returned (because that indicates that likely |
|
| 1093 | + * this field is nullable). |
|
| 1094 | + */ |
|
| 1095 | + public function get_DateTime_object($field_name) |
|
| 1096 | + { |
|
| 1097 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1098 | + if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1099 | + EE_Error::add_error( |
|
| 1100 | + sprintf( |
|
| 1101 | + __( |
|
| 1102 | + 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1103 | + 'event_espresso' |
|
| 1104 | + ), |
|
| 1105 | + $field_name |
|
| 1106 | + ), |
|
| 1107 | + __FILE__, |
|
| 1108 | + __FUNCTION__, |
|
| 1109 | + __LINE__ |
|
| 1110 | + ); |
|
| 1111 | + return false; |
|
| 1112 | + } |
|
| 1113 | + return $this->_fields[$field_name]; |
|
| 1114 | + } |
|
| 1115 | + |
|
| 1116 | + |
|
| 1117 | + |
|
| 1118 | + /** |
|
| 1119 | + * To be used in template to immediately echo out the value, and format it for output. |
|
| 1120 | + * Eg, should call stripslashes and whatnot before echoing |
|
| 1121 | + * |
|
| 1122 | + * @param string $field_name the name of the field as it appears in the DB |
|
| 1123 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1124 | + * (in cases where the same property may be used for different outputs |
|
| 1125 | + * - i.e. datetime, money etc.) |
|
| 1126 | + * @return void |
|
| 1127 | + * @throws \EE_Error |
|
| 1128 | + */ |
|
| 1129 | + public function e($field_name, $extra_cache_ref = null) |
|
| 1130 | + { |
|
| 1131 | + echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + |
|
| 1135 | + |
|
| 1136 | + /** |
|
| 1137 | + * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1138 | + * can be easily used as the value of form input. |
|
| 1139 | + * |
|
| 1140 | + * @param string $field_name |
|
| 1141 | + * @return void |
|
| 1142 | + * @throws \EE_Error |
|
| 1143 | + */ |
|
| 1144 | + public function f($field_name) |
|
| 1145 | + { |
|
| 1146 | + $this->e($field_name, 'form_input'); |
|
| 1147 | + } |
|
| 1148 | + |
|
| 1149 | + |
|
| 1150 | + |
|
| 1151 | + /** |
|
| 1152 | + * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1153 | + * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1154 | + * to see what options are available. |
|
| 1155 | + * @param string $field_name |
|
| 1156 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1157 | + * (in cases where the same property may be used for different outputs |
|
| 1158 | + * - i.e. datetime, money etc.) |
|
| 1159 | + * @return mixed |
|
| 1160 | + * @throws \EE_Error |
|
| 1161 | + */ |
|
| 1162 | + public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1163 | + { |
|
| 1164 | + return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1165 | + } |
|
| 1166 | + |
|
| 1167 | + |
|
| 1168 | + |
|
| 1169 | + /** |
|
| 1170 | + * This simply returns the datetime for the given field name |
|
| 1171 | + * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1172 | + * (and the equivalent e_date, e_time, e_datetime). |
|
| 1173 | + * |
|
| 1174 | + * @access protected |
|
| 1175 | + * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1176 | + * @param string $dt_frmt valid datetime format used for date |
|
| 1177 | + * (if '' then we just use the default on the field, |
|
| 1178 | + * if NULL we use the last-used format) |
|
| 1179 | + * @param string $tm_frmt Same as above except this is for time format |
|
| 1180 | + * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1181 | + * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1182 | + * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1183 | + * if field is not a valid dtt field, or void if echoing |
|
| 1184 | + * @throws \EE_Error |
|
| 1185 | + */ |
|
| 1186 | + protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1187 | + { |
|
| 1188 | + // clear cached property |
|
| 1189 | + $this->_clear_cached_property($field_name); |
|
| 1190 | + //reset format properties because they are used in get() |
|
| 1191 | + $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1192 | + $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1193 | + if ($echo) { |
|
| 1194 | + $this->e($field_name, $date_or_time); |
|
| 1195 | + return ''; |
|
| 1196 | + } |
|
| 1197 | + return $this->get($field_name, $date_or_time); |
|
| 1198 | + } |
|
| 1199 | + |
|
| 1200 | + |
|
| 1201 | + |
|
| 1202 | + /** |
|
| 1203 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1204 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1205 | + * other echoes the pretty value for dtt) |
|
| 1206 | + * |
|
| 1207 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1208 | + * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1209 | + * @return string datetime value formatted |
|
| 1210 | + * @throws \EE_Error |
|
| 1211 | + */ |
|
| 1212 | + public function get_date($field_name, $format = '') |
|
| 1213 | + { |
|
| 1214 | + return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + |
|
| 1218 | + |
|
| 1219 | + /** |
|
| 1220 | + * @param $field_name |
|
| 1221 | + * @param string $format |
|
| 1222 | + * @throws \EE_Error |
|
| 1223 | + */ |
|
| 1224 | + public function e_date($field_name, $format = '') |
|
| 1225 | + { |
|
| 1226 | + $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + |
|
| 1230 | + |
|
| 1231 | + /** |
|
| 1232 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1233 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1234 | + * other echoes the pretty value for dtt) |
|
| 1235 | + * |
|
| 1236 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1237 | + * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1238 | + * @return string datetime value formatted |
|
| 1239 | + * @throws \EE_Error |
|
| 1240 | + */ |
|
| 1241 | + public function get_time($field_name, $format = '') |
|
| 1242 | + { |
|
| 1243 | + return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1244 | + } |
|
| 1245 | + |
|
| 1246 | + |
|
| 1247 | + |
|
| 1248 | + /** |
|
| 1249 | + * @param $field_name |
|
| 1250 | + * @param string $format |
|
| 1251 | + * @throws \EE_Error |
|
| 1252 | + */ |
|
| 1253 | + public function e_time($field_name, $format = '') |
|
| 1254 | + { |
|
| 1255 | + $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1256 | + } |
|
| 1257 | + |
|
| 1258 | + |
|
| 1259 | + |
|
| 1260 | + /** |
|
| 1261 | + * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1262 | + * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1263 | + * other echoes the pretty value for dtt) |
|
| 1264 | + * |
|
| 1265 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1266 | + * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1267 | + * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1268 | + * @return string datetime value formatted |
|
| 1269 | + * @throws \EE_Error |
|
| 1270 | + */ |
|
| 1271 | + public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1272 | + { |
|
| 1273 | + return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1274 | + } |
|
| 1275 | + |
|
| 1276 | + |
|
| 1277 | + |
|
| 1278 | + /** |
|
| 1279 | + * @param string $field_name |
|
| 1280 | + * @param string $dt_frmt |
|
| 1281 | + * @param string $tm_frmt |
|
| 1282 | + * @throws \EE_Error |
|
| 1283 | + */ |
|
| 1284 | + public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1285 | + { |
|
| 1286 | + $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + |
|
| 1290 | + |
|
| 1291 | + /** |
|
| 1292 | + * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1293 | + * |
|
| 1294 | + * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1295 | + * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1296 | + * on the object will be used. |
|
| 1297 | + * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1298 | + * @throws \EE_Error |
|
| 1299 | + * field name. |
|
| 1300 | + */ |
|
| 1301 | + public function get_i18n_datetime($field_name, $format = '') |
|
| 1302 | + { |
|
| 1303 | + $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1304 | + return date_i18n( |
|
| 1305 | + $format, |
|
| 1306 | + EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone) |
|
| 1307 | + ); |
|
| 1308 | + } |
|
| 1309 | + |
|
| 1310 | + |
|
| 1311 | + |
|
| 1312 | + /** |
|
| 1313 | + * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1314 | + * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1315 | + * thrown. |
|
| 1316 | + * |
|
| 1317 | + * @param string $field_name The field name being checked |
|
| 1318 | + * @throws EE_Error |
|
| 1319 | + * @return EE_Datetime_Field |
|
| 1320 | + */ |
|
| 1321 | + protected function _get_dtt_field_settings($field_name) |
|
| 1322 | + { |
|
| 1323 | + $field = $this->get_model()->field_settings_for($field_name); |
|
| 1324 | + //check if field is dtt |
|
| 1325 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1326 | + return $field; |
|
| 1327 | + } else { |
|
| 1328 | + throw new EE_Error(sprintf(__('The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1329 | + 'event_espresso'), $field_name, self::_get_model_classname(get_class($this)))); |
|
| 1330 | + } |
|
| 1331 | + } |
|
| 1332 | + |
|
| 1333 | + |
|
| 1334 | + |
|
| 1335 | + |
|
| 1336 | + /** |
|
| 1337 | + * NOTE ABOUT BELOW: |
|
| 1338 | + * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1339 | + * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1340 | + * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1341 | + * method and make sure you send the entire datetime value for setting. |
|
| 1342 | + */ |
|
| 1343 | + /** |
|
| 1344 | + * sets the time on a datetime property |
|
| 1345 | + * |
|
| 1346 | + * @access protected |
|
| 1347 | + * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1348 | + * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1349 | + * @throws \EE_Error |
|
| 1350 | + */ |
|
| 1351 | + protected function _set_time_for($time, $fieldname) |
|
| 1352 | + { |
|
| 1353 | + $this->_set_date_time('T', $time, $fieldname); |
|
| 1354 | + } |
|
| 1355 | + |
|
| 1356 | + |
|
| 1357 | + |
|
| 1358 | + /** |
|
| 1359 | + * sets the date on a datetime property |
|
| 1360 | + * |
|
| 1361 | + * @access protected |
|
| 1362 | + * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1363 | + * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1364 | + * @throws \EE_Error |
|
| 1365 | + */ |
|
| 1366 | + protected function _set_date_for($date, $fieldname) |
|
| 1367 | + { |
|
| 1368 | + $this->_set_date_time('D', $date, $fieldname); |
|
| 1369 | + } |
|
| 1370 | + |
|
| 1371 | + |
|
| 1372 | + |
|
| 1373 | + /** |
|
| 1374 | + * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1375 | + * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1376 | + * |
|
| 1377 | + * @access protected |
|
| 1378 | + * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1379 | + * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1380 | + * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1381 | + * EE_Datetime_Field property) |
|
| 1382 | + * @throws \EE_Error |
|
| 1383 | + */ |
|
| 1384 | + protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1385 | + { |
|
| 1386 | + $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1387 | + $field->set_timezone($this->_timezone); |
|
| 1388 | + $field->set_date_format($this->_dt_frmt); |
|
| 1389 | + $field->set_time_format($this->_tm_frmt); |
|
| 1390 | + switch ($what) { |
|
| 1391 | + case 'T' : |
|
| 1392 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time( |
|
| 1393 | + $datetime_value, |
|
| 1394 | + $this->_fields[$fieldname] |
|
| 1395 | + ); |
|
| 1396 | + break; |
|
| 1397 | + case 'D' : |
|
| 1398 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date( |
|
| 1399 | + $datetime_value, |
|
| 1400 | + $this->_fields[$fieldname] |
|
| 1401 | + ); |
|
| 1402 | + break; |
|
| 1403 | + case 'B' : |
|
| 1404 | + $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value); |
|
| 1405 | + break; |
|
| 1406 | + } |
|
| 1407 | + $this->_clear_cached_property($fieldname); |
|
| 1408 | + } |
|
| 1409 | + |
|
| 1410 | + |
|
| 1411 | + |
|
| 1412 | + /** |
|
| 1413 | + * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1414 | + * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1415 | + * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1416 | + * that could lead to some unexpected results! |
|
| 1417 | + * |
|
| 1418 | + * @access public |
|
| 1419 | + * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1420 | + * value being returned. |
|
| 1421 | + * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1422 | + * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1423 | + * @param string $prepend You can include something to prepend on the timestamp |
|
| 1424 | + * @param string $append You can include something to append on the timestamp |
|
| 1425 | + * @throws EE_Error |
|
| 1426 | + * @return string timestamp |
|
| 1427 | + */ |
|
| 1428 | + public function display_in_my_timezone( |
|
| 1429 | + $field_name, |
|
| 1430 | + $callback = 'get_datetime', |
|
| 1431 | + $args = null, |
|
| 1432 | + $prepend = '', |
|
| 1433 | + $append = '' |
|
| 1434 | + ) { |
|
| 1435 | + $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1436 | + if ($timezone === $this->_timezone) { |
|
| 1437 | + return ''; |
|
| 1438 | + } |
|
| 1439 | + $original_timezone = $this->_timezone; |
|
| 1440 | + $this->set_timezone($timezone); |
|
| 1441 | + $fn = (array)$field_name; |
|
| 1442 | + $args = array_merge($fn, (array)$args); |
|
| 1443 | + if ( ! method_exists($this, $callback)) { |
|
| 1444 | + throw new EE_Error( |
|
| 1445 | + sprintf( |
|
| 1446 | + __( |
|
| 1447 | + 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1448 | + 'event_espresso' |
|
| 1449 | + ), |
|
| 1450 | + $callback |
|
| 1451 | + ) |
|
| 1452 | + ); |
|
| 1453 | + } |
|
| 1454 | + $args = (array)$args; |
|
| 1455 | + $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1456 | + $this->set_timezone($original_timezone); |
|
| 1457 | + return $return; |
|
| 1458 | + } |
|
| 1459 | + |
|
| 1460 | + |
|
| 1461 | + |
|
| 1462 | + /** |
|
| 1463 | + * Deletes this model object. |
|
| 1464 | + * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1465 | + * override |
|
| 1466 | + * `EE_Base_Class::_delete` NOT this class. |
|
| 1467 | + * |
|
| 1468 | + * @return boolean | int |
|
| 1469 | + * @throws \EE_Error |
|
| 1470 | + */ |
|
| 1471 | + public function delete() |
|
| 1472 | + { |
|
| 1473 | + /** |
|
| 1474 | + * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1475 | + * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1476 | + * should be aware that `_delete` may not always result in a permanent delete. For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1477 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1478 | + * |
|
| 1479 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1480 | + */ |
|
| 1481 | + do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1482 | + $result = $this->_delete(); |
|
| 1483 | + /** |
|
| 1484 | + * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1485 | + * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1486 | + * should be aware that `_delete` may not always result in a permanent delete. For example `EE_Soft_Base_Class::_delete` |
|
| 1487 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1488 | + * |
|
| 1489 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1490 | + * @param boolean $result |
|
| 1491 | + */ |
|
| 1492 | + do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1493 | + return $result; |
|
| 1494 | + } |
|
| 1495 | + |
|
| 1496 | + |
|
| 1497 | + |
|
| 1498 | + /** |
|
| 1499 | + * Calls the specific delete method for the instantiated class. |
|
| 1500 | + * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1501 | + * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1502 | + * `EE_Base_Class::delete` |
|
| 1503 | + * |
|
| 1504 | + * @return bool|int |
|
| 1505 | + * @throws \EE_Error |
|
| 1506 | + */ |
|
| 1507 | + protected function _delete() |
|
| 1508 | + { |
|
| 1509 | + return $this->delete_permanently(); |
|
| 1510 | + } |
|
| 1511 | + |
|
| 1512 | + |
|
| 1513 | + |
|
| 1514 | + /** |
|
| 1515 | + * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an |
|
| 1516 | + * error) |
|
| 1517 | + * |
|
| 1518 | + * @return bool | int |
|
| 1519 | + * @throws \EE_Error |
|
| 1520 | + */ |
|
| 1521 | + public function delete_permanently() |
|
| 1522 | + { |
|
| 1523 | + /** |
|
| 1524 | + * Called just before HARD deleting a model object |
|
| 1525 | + * |
|
| 1526 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1527 | + */ |
|
| 1528 | + do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1529 | + $model = $this->get_model(); |
|
| 1530 | + $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1531 | + $this->refresh_cache_of_related_objects(); |
|
| 1532 | + /** |
|
| 1533 | + * Called just after HARD deleting a model object |
|
| 1534 | + * |
|
| 1535 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1536 | + * @param boolean $result |
|
| 1537 | + */ |
|
| 1538 | + do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1539 | + return $result; |
|
| 1540 | + } |
|
| 1541 | + |
|
| 1542 | + |
|
| 1543 | + |
|
| 1544 | + /** |
|
| 1545 | + * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1546 | + * related model objects |
|
| 1547 | + * |
|
| 1548 | + * @throws \EE_Error |
|
| 1549 | + */ |
|
| 1550 | + public function refresh_cache_of_related_objects() |
|
| 1551 | + { |
|
| 1552 | + $model = $this->get_model(); |
|
| 1553 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1554 | + if ( ! empty($this->_model_relations[$relation_name])) { |
|
| 1555 | + $related_objects = $this->_model_relations[$relation_name]; |
|
| 1556 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1557 | + //this relation only stores a single model object, not an array |
|
| 1558 | + //but let's make it consistent |
|
| 1559 | + $related_objects = array($related_objects); |
|
| 1560 | + } |
|
| 1561 | + foreach ($related_objects as $related_object) { |
|
| 1562 | + //only refresh their cache if they're in memory |
|
| 1563 | + if ($related_object instanceof EE_Base_Class) { |
|
| 1564 | + $related_object->clear_cache($model->get_this_model_name(), $this); |
|
| 1565 | + } |
|
| 1566 | + } |
|
| 1567 | + } |
|
| 1568 | + } |
|
| 1569 | + } |
|
| 1570 | + |
|
| 1571 | + |
|
| 1572 | + |
|
| 1573 | + /** |
|
| 1574 | + * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1575 | + * object just before saving. |
|
| 1576 | + * |
|
| 1577 | + * @access public |
|
| 1578 | + * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1579 | + * if provided during the save() method (often client code will change the fields' |
|
| 1580 | + * values before calling save) |
|
| 1581 | + * @throws \EE_Error |
|
| 1582 | + * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1583 | + * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1584 | + */ |
|
| 1585 | + public function save($set_cols_n_values = array()) |
|
| 1586 | + { |
|
| 1587 | + $model = $this->get_model(); |
|
| 1588 | + /** |
|
| 1589 | + * Filters the fields we're about to save on the model object |
|
| 1590 | + * |
|
| 1591 | + * @param array $set_cols_n_values |
|
| 1592 | + * @param EE_Base_Class $model_object |
|
| 1593 | + */ |
|
| 1594 | + $set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values, |
|
| 1595 | + $this); |
|
| 1596 | + //set attributes as provided in $set_cols_n_values |
|
| 1597 | + foreach ($set_cols_n_values as $column => $value) { |
|
| 1598 | + $this->set($column, $value); |
|
| 1599 | + } |
|
| 1600 | + // no changes ? then don't do anything |
|
| 1601 | + if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1602 | + return 0; |
|
| 1603 | + } |
|
| 1604 | + /** |
|
| 1605 | + * Saving a model object. |
|
| 1606 | + * Before we perform a save, this action is fired. |
|
| 1607 | + * |
|
| 1608 | + * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1609 | + */ |
|
| 1610 | + do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1611 | + if ( ! $this->allow_persist()) { |
|
| 1612 | + return 0; |
|
| 1613 | + } |
|
| 1614 | + //now get current attribute values |
|
| 1615 | + $save_cols_n_values = $this->_fields; |
|
| 1616 | + //if the object already has an ID, update it. Otherwise, insert it |
|
| 1617 | + //also: change the assumption about values passed to the model NOT being prepare dby the model object. They have been |
|
| 1618 | + $old_assumption_concerning_value_preparation = $model |
|
| 1619 | + ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1620 | + $model->assume_values_already_prepared_by_model_object(true); |
|
| 1621 | + //does this model have an autoincrement PK? |
|
| 1622 | + if ($model->has_primary_key_field()) { |
|
| 1623 | + if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1624 | + //ok check if it's set, if so: update; if not, insert |
|
| 1625 | + if ( ! empty($save_cols_n_values[$model->primary_key_name()])) { |
|
| 1626 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1627 | + } else { |
|
| 1628 | + unset($save_cols_n_values[$model->primary_key_name()]); |
|
| 1629 | + $results = $model->insert($save_cols_n_values); |
|
| 1630 | + if ($results) { |
|
| 1631 | + //if successful, set the primary key |
|
| 1632 | + //but don't use the normal SET method, because it will check if |
|
| 1633 | + //an item with the same ID exists in the mapper & db, then |
|
| 1634 | + //will find it in the db (because we just added it) and THAT object |
|
| 1635 | + //will get added to the mapper before we can add this one! |
|
| 1636 | + //but if we just avoid using the SET method, all that headache can be avoided |
|
| 1637 | + $pk_field_name = $model->primary_key_name(); |
|
| 1638 | + $this->_fields[$pk_field_name] = $results; |
|
| 1639 | + $this->_clear_cached_property($pk_field_name); |
|
| 1640 | + $model->add_to_entity_map($this); |
|
| 1641 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1642 | + } |
|
| 1643 | + } |
|
| 1644 | + } else {//PK is NOT auto-increment |
|
| 1645 | + //so check if one like it already exists in the db |
|
| 1646 | + if ($model->exists_by_ID($this->ID())) { |
|
| 1647 | + if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1648 | + throw new EE_Error( |
|
| 1649 | + sprintf( |
|
| 1650 | + __('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1651 | + 'event_espresso'), |
|
| 1652 | + get_class($this), |
|
| 1653 | + get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1654 | + get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1655 | + '<br />' |
|
| 1656 | + ) |
|
| 1657 | + ); |
|
| 1658 | + } |
|
| 1659 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1660 | + } else { |
|
| 1661 | + $results = $model->insert($save_cols_n_values); |
|
| 1662 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1663 | + } |
|
| 1664 | + } |
|
| 1665 | + } else {//there is NO primary key |
|
| 1666 | + $already_in_db = false; |
|
| 1667 | + foreach ($model->unique_indexes() as $index) { |
|
| 1668 | + $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1669 | + if ($model->exists(array($uniqueness_where_params))) { |
|
| 1670 | + $already_in_db = true; |
|
| 1671 | + } |
|
| 1672 | + } |
|
| 1673 | + if ($already_in_db) { |
|
| 1674 | + $combined_pk_fields_n_values = array_intersect_key($save_cols_n_values, |
|
| 1675 | + $model->get_combined_primary_key_fields()); |
|
| 1676 | + $results = $model->update($save_cols_n_values, $combined_pk_fields_n_values); |
|
| 1677 | + } else { |
|
| 1678 | + $results = $model->insert($save_cols_n_values); |
|
| 1679 | + } |
|
| 1680 | + } |
|
| 1681 | + //restore the old assumption about values being prepared by the model object |
|
| 1682 | + $model |
|
| 1683 | + ->assume_values_already_prepared_by_model_object($old_assumption_concerning_value_preparation); |
|
| 1684 | + /** |
|
| 1685 | + * After saving the model object this action is called |
|
| 1686 | + * |
|
| 1687 | + * @param EE_Base_Class $model_object which was just saved |
|
| 1688 | + * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1689 | + * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1690 | + */ |
|
| 1691 | + do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1692 | + $this->_has_changes = false; |
|
| 1693 | + return $results; |
|
| 1694 | + } |
|
| 1695 | + |
|
| 1696 | + |
|
| 1697 | + |
|
| 1698 | + /** |
|
| 1699 | + * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1700 | + * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1701 | + * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1702 | + * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1703 | + * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1704 | + * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1705 | + * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1706 | + * |
|
| 1707 | + * @return void |
|
| 1708 | + * @throws \EE_Error |
|
| 1709 | + */ |
|
| 1710 | + protected function _update_cached_related_model_objs_fks() |
|
| 1711 | + { |
|
| 1712 | + $model = $this->get_model(); |
|
| 1713 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1714 | + if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1715 | + foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1716 | + $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1717 | + $model->get_this_model_name() |
|
| 1718 | + ); |
|
| 1719 | + $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1720 | + if ($related_model_obj_in_cache->ID()) { |
|
| 1721 | + $related_model_obj_in_cache->save(); |
|
| 1722 | + } |
|
| 1723 | + } |
|
| 1724 | + } |
|
| 1725 | + } |
|
| 1726 | + } |
|
| 1727 | + |
|
| 1728 | + |
|
| 1729 | + |
|
| 1730 | + /** |
|
| 1731 | + * Saves this model object and its NEW cached relations to the database. |
|
| 1732 | + * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1733 | + * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1734 | + * because otherwise, there's a potential for infinite looping of saving |
|
| 1735 | + * Saves the cached related model objects, and ensures the relation between them |
|
| 1736 | + * and this object and properly setup |
|
| 1737 | + * |
|
| 1738 | + * @return int ID of new model object on save; 0 on failure+ |
|
| 1739 | + * @throws \EE_Error |
|
| 1740 | + */ |
|
| 1741 | + public function save_new_cached_related_model_objs() |
|
| 1742 | + { |
|
| 1743 | + //make sure this has been saved |
|
| 1744 | + if ( ! $this->ID()) { |
|
| 1745 | + $id = $this->save(); |
|
| 1746 | + } else { |
|
| 1747 | + $id = $this->ID(); |
|
| 1748 | + } |
|
| 1749 | + //now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1750 | + foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1751 | + if ($this->_model_relations[$relationName]) { |
|
| 1752 | + //is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1753 | + //or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1754 | + if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1755 | + //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1756 | + //but ONLY if it DOES NOT exist in the DB |
|
| 1757 | + /* @var $related_model_obj EE_Base_Class */ |
|
| 1758 | + $related_model_obj = $this->_model_relations[$relationName]; |
|
| 1759 | + // if( ! $related_model_obj->ID()){ |
|
| 1760 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1761 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1762 | + // } |
|
| 1763 | + } else { |
|
| 1764 | + foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
| 1765 | + //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1766 | + //but ONLY if it DOES NOT exist in the DB |
|
| 1767 | + // if( ! $related_model_obj->ID()){ |
|
| 1768 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1769 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1770 | + // } |
|
| 1771 | + } |
|
| 1772 | + } |
|
| 1773 | + } |
|
| 1774 | + } |
|
| 1775 | + return $id; |
|
| 1776 | + } |
|
| 1777 | + |
|
| 1778 | + |
|
| 1779 | + |
|
| 1780 | + /** |
|
| 1781 | + * for getting a model while instantiated. |
|
| 1782 | + * |
|
| 1783 | + * @return \EEM_Base | \EEM_CPT_Base |
|
| 1784 | + */ |
|
| 1785 | + public function get_model() |
|
| 1786 | + { |
|
| 1787 | + if( ! $this->_model){ |
|
| 1788 | + $modelName = self::_get_model_classname(get_class($this)); |
|
| 1789 | + $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 1790 | + } else { |
|
| 1791 | + $this->_model->set_timezone($this->_timezone); |
|
| 1792 | + } |
|
| 1793 | + |
|
| 1794 | + return $this->_model; |
|
| 1795 | + } |
|
| 1796 | + |
|
| 1797 | + |
|
| 1798 | + |
|
| 1799 | + /** |
|
| 1800 | + * @param $props_n_values |
|
| 1801 | + * @param $classname |
|
| 1802 | + * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 1803 | + * @throws \EE_Error |
|
| 1804 | + */ |
|
| 1805 | + protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 1806 | + { |
|
| 1807 | + //TODO: will not work for Term_Relationships because they have no PK! |
|
| 1808 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1809 | + if (array_key_exists($primary_id_ref, $props_n_values) && ! empty($props_n_values[$primary_id_ref])) { |
|
| 1810 | + $id = $props_n_values[$primary_id_ref]; |
|
| 1811 | + return self::_get_model($classname)->get_from_entity_map($id); |
|
| 1812 | + } |
|
| 1813 | + return false; |
|
| 1814 | + } |
|
| 1815 | + |
|
| 1816 | + |
|
| 1817 | + |
|
| 1818 | + /** |
|
| 1819 | + * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 1820 | + * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 1821 | + * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 1822 | + * we return false. |
|
| 1823 | + * |
|
| 1824 | + * @param array $props_n_values incoming array of properties and their values |
|
| 1825 | + * @param string $classname the classname of the child class |
|
| 1826 | + * @param null $timezone |
|
| 1827 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 1828 | + * date_format and the second value is the time format |
|
| 1829 | + * @return mixed (EE_Base_Class|bool) |
|
| 1830 | + * @throws \EE_Error |
|
| 1831 | + */ |
|
| 1832 | + protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 1833 | + { |
|
| 1834 | + $existing = null; |
|
| 1835 | + $model = self::_get_model($classname, $timezone); |
|
| 1836 | + if ($model->has_primary_key_field()) { |
|
| 1837 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1838 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 1839 | + && ! empty($props_n_values[$primary_id_ref]) |
|
| 1840 | + ) { |
|
| 1841 | + $existing = $model->get_one_by_ID( |
|
| 1842 | + $props_n_values[$primary_id_ref] |
|
| 1843 | + ); |
|
| 1844 | + } |
|
| 1845 | + } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 1846 | + //no primary key on this model, but there's still a matching item in the DB |
|
| 1847 | + $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 1848 | + self::_get_model($classname, $timezone)->get_index_primary_key_string($props_n_values) |
|
| 1849 | + ); |
|
| 1850 | + } |
|
| 1851 | + if ($existing) { |
|
| 1852 | + //set date formats if present before setting values |
|
| 1853 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 1854 | + $existing->set_date_format($date_formats[0]); |
|
| 1855 | + $existing->set_time_format($date_formats[1]); |
|
| 1856 | + } else { |
|
| 1857 | + //set default formats for date and time |
|
| 1858 | + $existing->set_date_format(get_option('date_format')); |
|
| 1859 | + $existing->set_time_format(get_option('time_format')); |
|
| 1860 | + } |
|
| 1861 | + foreach ($props_n_values as $property => $field_value) { |
|
| 1862 | + $existing->set($property, $field_value); |
|
| 1863 | + } |
|
| 1864 | + return $existing; |
|
| 1865 | + } else { |
|
| 1866 | + return false; |
|
| 1867 | + } |
|
| 1868 | + } |
|
| 1869 | + |
|
| 1870 | + |
|
| 1871 | + |
|
| 1872 | + /** |
|
| 1873 | + * Gets the EEM_*_Model for this class |
|
| 1874 | + * |
|
| 1875 | + * @access public now, as this is more convenient |
|
| 1876 | + * @param $classname |
|
| 1877 | + * @param null $timezone |
|
| 1878 | + * @throws EE_Error |
|
| 1879 | + * @return EEM_Base |
|
| 1880 | + */ |
|
| 1881 | + protected static function _get_model($classname, $timezone = null) |
|
| 1882 | + { |
|
| 1883 | + //find model for this class |
|
| 1884 | + if ( ! $classname) { |
|
| 1885 | + throw new EE_Error( |
|
| 1886 | + sprintf( |
|
| 1887 | + __( |
|
| 1888 | + "What were you thinking calling _get_model(%s)?? You need to specify the class name", |
|
| 1889 | + "event_espresso" |
|
| 1890 | + ), |
|
| 1891 | + $classname |
|
| 1892 | + ) |
|
| 1893 | + ); |
|
| 1894 | + } |
|
| 1895 | + $modelName = self::_get_model_classname($classname); |
|
| 1896 | + return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 1897 | + } |
|
| 1898 | + |
|
| 1899 | + |
|
| 1900 | + |
|
| 1901 | + /** |
|
| 1902 | + * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 1903 | + * |
|
| 1904 | + * @param string $model_classname |
|
| 1905 | + * @param null $timezone |
|
| 1906 | + * @return EEM_Base |
|
| 1907 | + */ |
|
| 1908 | + protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 1909 | + { |
|
| 1910 | + $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 1911 | + $model = EE_Registry::instance()->load_model($model_classname); |
|
| 1912 | + $model->set_timezone($timezone); |
|
| 1913 | + return $model; |
|
| 1914 | + } |
|
| 1915 | + |
|
| 1916 | + |
|
| 1917 | + |
|
| 1918 | + /** |
|
| 1919 | + * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 1920 | + * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 1921 | + * |
|
| 1922 | + * @param null $model_name |
|
| 1923 | + * @return string like EEM_Attendee |
|
| 1924 | + */ |
|
| 1925 | + private static function _get_model_classname($model_name = null) |
|
| 1926 | + { |
|
| 1927 | + if (strpos($model_name, "EE_") === 0) { |
|
| 1928 | + $model_classname = str_replace("EE_", "EEM_", $model_name); |
|
| 1929 | + } else { |
|
| 1930 | + $model_classname = "EEM_" . $model_name; |
|
| 1931 | + } |
|
| 1932 | + return $model_classname; |
|
| 1933 | + } |
|
| 1934 | + |
|
| 1935 | + |
|
| 1936 | + |
|
| 1937 | + /** |
|
| 1938 | + * returns the name of the primary key attribute |
|
| 1939 | + * |
|
| 1940 | + * @param null $classname |
|
| 1941 | + * @throws EE_Error |
|
| 1942 | + * @return string |
|
| 1943 | + */ |
|
| 1944 | + protected static function _get_primary_key_name($classname = null) |
|
| 1945 | + { |
|
| 1946 | + if ( ! $classname) { |
|
| 1947 | + throw new EE_Error( |
|
| 1948 | + sprintf( |
|
| 1949 | + __("What were you thinking calling _get_primary_key_name(%s)", "event_espresso"), |
|
| 1950 | + $classname |
|
| 1951 | + ) |
|
| 1952 | + ); |
|
| 1953 | + } |
|
| 1954 | + return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 1955 | + } |
|
| 1956 | + |
|
| 1957 | + |
|
| 1958 | + |
|
| 1959 | + /** |
|
| 1960 | + * Gets the value of the primary key. |
|
| 1961 | + * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 1962 | + * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 1963 | + * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 1964 | + * |
|
| 1965 | + * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 1966 | + * @throws \EE_Error |
|
| 1967 | + */ |
|
| 1968 | + public function ID() |
|
| 1969 | + { |
|
| 1970 | + $model = $this->get_model(); |
|
| 1971 | + //now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 1972 | + if ($model->has_primary_key_field()) { |
|
| 1973 | + return $this->_fields[$model->primary_key_name()]; |
|
| 1974 | + } else { |
|
| 1975 | + return $model->get_index_primary_key_string($this->_fields); |
|
| 1976 | + } |
|
| 1977 | + } |
|
| 1978 | + |
|
| 1979 | + |
|
| 1980 | + |
|
| 1981 | + /** |
|
| 1982 | + * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 1983 | + * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 1984 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 1985 | + * |
|
| 1986 | + * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 1987 | + * @param string $relationName eg 'Events','Question',etc. |
|
| 1988 | + * an attendee to a group, you also want to specify which role they |
|
| 1989 | + * will have in that group. So you would use this parameter to |
|
| 1990 | + * specify array('role-column-name'=>'role-id') |
|
| 1991 | + * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 1992 | + * allow you to further constrict the relation to being added. |
|
| 1993 | + * However, keep in mind that the columns (keys) given must match a |
|
| 1994 | + * column on the JOIN table and currently only the HABTM models |
|
| 1995 | + * accept these additional conditions. Also remember that if an |
|
| 1996 | + * exact match isn't found for these extra cols/val pairs, then a |
|
| 1997 | + * NEW row is created in the join table. |
|
| 1998 | + * @param null $cache_id |
|
| 1999 | + * @throws EE_Error |
|
| 2000 | + * @return EE_Base_Class the object the relation was added to |
|
| 2001 | + */ |
|
| 2002 | + public function _add_relation_to( |
|
| 2003 | + $otherObjectModelObjectOrID, |
|
| 2004 | + $relationName, |
|
| 2005 | + $extra_join_model_fields_n_values = array(), |
|
| 2006 | + $cache_id = null |
|
| 2007 | + ) { |
|
| 2008 | + $model = $this->get_model(); |
|
| 2009 | + //if this thing exists in the DB, save the relation to the DB |
|
| 2010 | + if ($this->ID()) { |
|
| 2011 | + $otherObject = $model |
|
| 2012 | + ->add_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2013 | + $extra_join_model_fields_n_values); |
|
| 2014 | + //clear cache so future get_many_related and get_first_related() return new results. |
|
| 2015 | + $this->clear_cache($relationName, $otherObject, true); |
|
| 2016 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2017 | + $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2018 | + } |
|
| 2019 | + } else { |
|
| 2020 | + //this thing doesn't exist in the DB, so just cache it |
|
| 2021 | + if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2022 | + throw new EE_Error(sprintf( |
|
| 2023 | + __('Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 2024 | + 'event_espresso'), |
|
| 2025 | + $otherObjectModelObjectOrID, |
|
| 2026 | + get_class($this) |
|
| 2027 | + )); |
|
| 2028 | + } else { |
|
| 2029 | + $otherObject = $otherObjectModelObjectOrID; |
|
| 2030 | + } |
|
| 2031 | + $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2032 | + } |
|
| 2033 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2034 | + //fix the reciprocal relation too |
|
| 2035 | + if ($otherObject->ID()) { |
|
| 2036 | + //its saved so assumed relations exist in the DB, so we can just |
|
| 2037 | + //clear the cache so future queries use the updated info in the DB |
|
| 2038 | + $otherObject->clear_cache($model->get_this_model_name(), null, true); |
|
| 2039 | + } else { |
|
| 2040 | + //it's not saved, so it caches relations like this |
|
| 2041 | + $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2042 | + } |
|
| 2043 | + } |
|
| 2044 | + return $otherObject; |
|
| 2045 | + } |
|
| 2046 | + |
|
| 2047 | + |
|
| 2048 | + |
|
| 2049 | + /** |
|
| 2050 | + * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2051 | + * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2052 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2053 | + * from the cache |
|
| 2054 | + * |
|
| 2055 | + * @param mixed $otherObjectModelObjectOrID |
|
| 2056 | + * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2057 | + * to the DB yet |
|
| 2058 | + * @param string $relationName |
|
| 2059 | + * @param array $where_query |
|
| 2060 | + * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2061 | + * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2062 | + * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2063 | + * remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is |
|
| 2064 | + * created in the join table. |
|
| 2065 | + * @return EE_Base_Class the relation was removed from |
|
| 2066 | + * @throws \EE_Error |
|
| 2067 | + */ |
|
| 2068 | + public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2069 | + { |
|
| 2070 | + if ($this->ID()) { |
|
| 2071 | + //if this exists in the DB, save the relation change to the DB too |
|
| 2072 | + $otherObject = $this->get_model() |
|
| 2073 | + ->remove_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2074 | + $where_query); |
|
| 2075 | + $this->clear_cache($relationName, $otherObject); |
|
| 2076 | + } else { |
|
| 2077 | + //this doesn't exist in the DB, just remove it from the cache |
|
| 2078 | + $otherObject = $this->clear_cache($relationName, $otherObjectModelObjectOrID); |
|
| 2079 | + } |
|
| 2080 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2081 | + $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2082 | + } |
|
| 2083 | + return $otherObject; |
|
| 2084 | + } |
|
| 2085 | + |
|
| 2086 | + |
|
| 2087 | + |
|
| 2088 | + /** |
|
| 2089 | + * Removes ALL the related things for the $relationName. |
|
| 2090 | + * |
|
| 2091 | + * @param string $relationName |
|
| 2092 | + * @param array $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 2093 | + * @return EE_Base_Class |
|
| 2094 | + * @throws \EE_Error |
|
| 2095 | + */ |
|
| 2096 | + public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2097 | + { |
|
| 2098 | + if ($this->ID()) { |
|
| 2099 | + //if this exists in the DB, save the relation change to the DB too |
|
| 2100 | + $otherObjects = $this->get_model()->remove_relations($this, $relationName, $where_query_params); |
|
| 2101 | + $this->clear_cache($relationName, null, true); |
|
| 2102 | + } else { |
|
| 2103 | + //this doesn't exist in the DB, just remove it from the cache |
|
| 2104 | + $otherObjects = $this->clear_cache($relationName, null, true); |
|
| 2105 | + } |
|
| 2106 | + if (is_array($otherObjects)) { |
|
| 2107 | + foreach ($otherObjects as $otherObject) { |
|
| 2108 | + $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2109 | + } |
|
| 2110 | + } |
|
| 2111 | + return $otherObjects; |
|
| 2112 | + } |
|
| 2113 | + |
|
| 2114 | + |
|
| 2115 | + |
|
| 2116 | + /** |
|
| 2117 | + * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2118 | + * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2119 | + * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2120 | + * because we want to get even deleted items etc. |
|
| 2121 | + * |
|
| 2122 | + * @param string $relationName key in the model's _model_relations array |
|
| 2123 | + * @param array $query_params like EEM_Base::get_all |
|
| 2124 | + * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys |
|
| 2125 | + * @throws \EE_Error |
|
| 2126 | + * or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if |
|
| 2127 | + * you want IDs |
|
| 2128 | + */ |
|
| 2129 | + public function get_many_related($relationName, $query_params = array()) |
|
| 2130 | + { |
|
| 2131 | + if ($this->ID()) { |
|
| 2132 | + //this exists in the DB, so get the related things from either the cache or the DB |
|
| 2133 | + //if there are query parameters, forget about caching the related model objects. |
|
| 2134 | + if ($query_params) { |
|
| 2135 | + $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2136 | + } else { |
|
| 2137 | + //did we already cache the result of this query? |
|
| 2138 | + $cached_results = $this->get_all_from_cache($relationName); |
|
| 2139 | + if ( ! $cached_results) { |
|
| 2140 | + $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2141 | + //if no query parameters were passed, then we got all the related model objects |
|
| 2142 | + //for that relation. We can cache them then. |
|
| 2143 | + foreach ($related_model_objects as $related_model_object) { |
|
| 2144 | + $this->cache($relationName, $related_model_object); |
|
| 2145 | + } |
|
| 2146 | + } else { |
|
| 2147 | + $related_model_objects = $cached_results; |
|
| 2148 | + } |
|
| 2149 | + } |
|
| 2150 | + } else { |
|
| 2151 | + //this doesn't exist in the DB, so just get the related things from the cache |
|
| 2152 | + $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2153 | + } |
|
| 2154 | + return $related_model_objects; |
|
| 2155 | + } |
|
| 2156 | + |
|
| 2157 | + |
|
| 2158 | + |
|
| 2159 | + /** |
|
| 2160 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2161 | + * unless otherwise specified in the $query_params |
|
| 2162 | + * |
|
| 2163 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2164 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2165 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2166 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2167 | + * that by the setting $distinct to TRUE; |
|
| 2168 | + * @return int |
|
| 2169 | + */ |
|
| 2170 | + public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2171 | + { |
|
| 2172 | + return $this->get_model()->count_related($this, $relation_name, $query_params, $field_to_count, $distinct); |
|
| 2173 | + } |
|
| 2174 | + |
|
| 2175 | + |
|
| 2176 | + |
|
| 2177 | + /** |
|
| 2178 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2179 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2180 | + * |
|
| 2181 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2182 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2183 | + * @param string $field_to_sum name of field to count by. |
|
| 2184 | + * By default, uses primary key (which doesn't make much sense, so you should probably |
|
| 2185 | + * change it) |
|
| 2186 | + * @return int |
|
| 2187 | + */ |
|
| 2188 | + public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2189 | + { |
|
| 2190 | + return $this->get_model()->sum_related($this, $relation_name, $query_params, $field_to_sum); |
|
| 2191 | + } |
|
| 2192 | + |
|
| 2193 | + |
|
| 2194 | + |
|
| 2195 | + /** |
|
| 2196 | + * Gets the first (ie, one) related model object of the specified type. |
|
| 2197 | + * |
|
| 2198 | + * @param string $relationName key in the model's _model_relations array |
|
| 2199 | + * @param array $query_params like EEM_Base::get_all |
|
| 2200 | + * @return EE_Base_Class (not an array, a single object) |
|
| 2201 | + * @throws \EE_Error |
|
| 2202 | + */ |
|
| 2203 | + public function get_first_related($relationName, $query_params = array()) |
|
| 2204 | + { |
|
| 2205 | + $model = $this->get_model(); |
|
| 2206 | + if ($this->ID()) {//this exists in the DB, get from the cache OR the DB |
|
| 2207 | + //if they've provided some query parameters, don't bother trying to cache the result |
|
| 2208 | + //also make sure we're not caching the result of get_first_related |
|
| 2209 | + //on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2210 | + if ($query_params |
|
| 2211 | + || ! $model->related_settings_for($relationName) |
|
| 2212 | + instanceof |
|
| 2213 | + EE_Belongs_To_Relation |
|
| 2214 | + ) { |
|
| 2215 | + $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2216 | + } else { |
|
| 2217 | + //first, check if we've already cached the result of this query |
|
| 2218 | + $cached_result = $this->get_one_from_cache($relationName); |
|
| 2219 | + if ( ! $cached_result) { |
|
| 2220 | + $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2221 | + $this->cache($relationName, $related_model_object); |
|
| 2222 | + } else { |
|
| 2223 | + $related_model_object = $cached_result; |
|
| 2224 | + } |
|
| 2225 | + } |
|
| 2226 | + } else { |
|
| 2227 | + $related_model_object = null; |
|
| 2228 | + //this doesn't exist in the Db, but maybe the relation is of type belongs to, and so the related thing might |
|
| 2229 | + if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2230 | + $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2231 | + } |
|
| 2232 | + //this doesn't exist in the DB and apparently the thing it belongs to doesn't either, just get what's cached on this object |
|
| 2233 | + if ( ! $related_model_object) { |
|
| 2234 | + $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2235 | + } |
|
| 2236 | + } |
|
| 2237 | + return $related_model_object; |
|
| 2238 | + } |
|
| 2239 | + |
|
| 2240 | + |
|
| 2241 | + |
|
| 2242 | + /** |
|
| 2243 | + * Does a delete on all related objects of type $relationName and removes |
|
| 2244 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2245 | + * of blocking related model objects) does nothing. If the related model objects are |
|
| 2246 | + * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2247 | + * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2248 | + * |
|
| 2249 | + * @param string $relationName |
|
| 2250 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2251 | + * @return int how many deleted |
|
| 2252 | + * @throws \EE_Error |
|
| 2253 | + */ |
|
| 2254 | + public function delete_related($relationName, $query_params = array()) |
|
| 2255 | + { |
|
| 2256 | + if ($this->ID()) { |
|
| 2257 | + $count = $this->get_model()->delete_related($this, $relationName, $query_params); |
|
| 2258 | + } else { |
|
| 2259 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2260 | + $this->clear_cache($relationName, null, true); |
|
| 2261 | + } |
|
| 2262 | + return $count; |
|
| 2263 | + } |
|
| 2264 | + |
|
| 2265 | + |
|
| 2266 | + |
|
| 2267 | + /** |
|
| 2268 | + * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2269 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2270 | + * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2271 | + * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2272 | + * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2273 | + * |
|
| 2274 | + * @param string $relationName |
|
| 2275 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2276 | + * @return int how many deleted (including those soft deleted) |
|
| 2277 | + * @throws \EE_Error |
|
| 2278 | + */ |
|
| 2279 | + public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2280 | + { |
|
| 2281 | + if ($this->ID()) { |
|
| 2282 | + $count = $this->get_model()->delete_related_permanently($this, $relationName, $query_params); |
|
| 2283 | + } else { |
|
| 2284 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2285 | + } |
|
| 2286 | + $this->clear_cache($relationName, null, true); |
|
| 2287 | + return $count; |
|
| 2288 | + } |
|
| 2289 | + |
|
| 2290 | + |
|
| 2291 | + |
|
| 2292 | + /** |
|
| 2293 | + * is_set |
|
| 2294 | + * Just a simple utility function children can use for checking if property exists |
|
| 2295 | + * |
|
| 2296 | + * @access public |
|
| 2297 | + * @param string $field_name property to check |
|
| 2298 | + * @return bool TRUE if existing,FALSE if not. |
|
| 2299 | + */ |
|
| 2300 | + public function is_set($field_name) |
|
| 2301 | + { |
|
| 2302 | + return isset($this->_fields[$field_name]); |
|
| 2303 | + } |
|
| 2304 | + |
|
| 2305 | + |
|
| 2306 | + |
|
| 2307 | + /** |
|
| 2308 | + * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2309 | + * EE_Error exception if they don't |
|
| 2310 | + * |
|
| 2311 | + * @param mixed (string|array) $properties properties to check |
|
| 2312 | + * @throws EE_Error |
|
| 2313 | + * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2314 | + */ |
|
| 2315 | + protected function _property_exists($properties) |
|
| 2316 | + { |
|
| 2317 | + foreach ((array)$properties as $property_name) { |
|
| 2318 | + //first make sure this property exists |
|
| 2319 | + if ( ! $this->_fields[$property_name]) { |
|
| 2320 | + throw new EE_Error( |
|
| 2321 | + sprintf( |
|
| 2322 | + __( |
|
| 2323 | + 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2324 | + 'event_espresso' |
|
| 2325 | + ), |
|
| 2326 | + $property_name |
|
| 2327 | + ) |
|
| 2328 | + ); |
|
| 2329 | + } |
|
| 2330 | + } |
|
| 2331 | + return true; |
|
| 2332 | + } |
|
| 2333 | + |
|
| 2334 | + |
|
| 2335 | + |
|
| 2336 | + /** |
|
| 2337 | + * This simply returns an array of model fields for this object |
|
| 2338 | + * |
|
| 2339 | + * @return array |
|
| 2340 | + * @throws \EE_Error |
|
| 2341 | + */ |
|
| 2342 | + public function model_field_array() |
|
| 2343 | + { |
|
| 2344 | + $fields = $this->get_model()->field_settings(false); |
|
| 2345 | + $properties = array(); |
|
| 2346 | + //remove prepended underscore |
|
| 2347 | + foreach ($fields as $field_name => $settings) { |
|
| 2348 | + $properties[$field_name] = $this->get($field_name); |
|
| 2349 | + } |
|
| 2350 | + return $properties; |
|
| 2351 | + } |
|
| 2352 | + |
|
| 2353 | + |
|
| 2354 | + |
|
| 2355 | + /** |
|
| 2356 | + * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2357 | + * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2358 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 2359 | + * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that |
|
| 2360 | + * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 2361 | + * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 2362 | + * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 2363 | + * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 2364 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 2365 | + * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2366 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2367 | + * return $previousReturnValue.$returnString; |
|
| 2368 | + * } |
|
| 2369 | + * require('EE_Answer.class.php'); |
|
| 2370 | + * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2371 | + * echo $answer->my_callback('monkeys',100); |
|
| 2372 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2373 | + * |
|
| 2374 | + * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2375 | + * @param array $args array of original arguments passed to the function |
|
| 2376 | + * @throws EE_Error |
|
| 2377 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 2378 | + */ |
|
| 2379 | + public function __call($methodName, $args) |
|
| 2380 | + { |
|
| 2381 | + $className = get_class($this); |
|
| 2382 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2383 | + if ( ! has_filter($tagName)) { |
|
| 2384 | + throw new EE_Error( |
|
| 2385 | + sprintf( |
|
| 2386 | + __( |
|
| 2387 | + "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2388 | + "event_espresso" |
|
| 2389 | + ), |
|
| 2390 | + $methodName, |
|
| 2391 | + $className, |
|
| 2392 | + $tagName |
|
| 2393 | + ) |
|
| 2394 | + ); |
|
| 2395 | + } |
|
| 2396 | + return apply_filters($tagName, null, $this, $args); |
|
| 2397 | + } |
|
| 2398 | + |
|
| 2399 | + |
|
| 2400 | + |
|
| 2401 | + /** |
|
| 2402 | + * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2403 | + * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2404 | + * |
|
| 2405 | + * @param string $meta_key |
|
| 2406 | + * @param mixed $meta_value |
|
| 2407 | + * @param mixed $previous_value |
|
| 2408 | + * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2409 | + * @throws \EE_Error |
|
| 2410 | + * NOTE: if the values haven't changed, returns 0 |
|
| 2411 | + */ |
|
| 2412 | + public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2413 | + { |
|
| 2414 | + $query_params = array( |
|
| 2415 | + array( |
|
| 2416 | + 'EXM_key' => $meta_key, |
|
| 2417 | + 'OBJ_ID' => $this->ID(), |
|
| 2418 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2419 | + ), |
|
| 2420 | + ); |
|
| 2421 | + if ($previous_value !== null) { |
|
| 2422 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2423 | + } |
|
| 2424 | + $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2425 | + if ( ! $existing_rows_like_that) { |
|
| 2426 | + return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2427 | + } |
|
| 2428 | + foreach ($existing_rows_like_that as $existing_row) { |
|
| 2429 | + $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2430 | + } |
|
| 2431 | + return count($existing_rows_like_that); |
|
| 2432 | + } |
|
| 2433 | + |
|
| 2434 | + |
|
| 2435 | + |
|
| 2436 | + /** |
|
| 2437 | + * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2438 | + * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2439 | + * extra meta row was entered, false if not |
|
| 2440 | + * |
|
| 2441 | + * @param string $meta_key |
|
| 2442 | + * @param mixed $meta_value |
|
| 2443 | + * @param boolean $unique |
|
| 2444 | + * @return boolean |
|
| 2445 | + * @throws \EE_Error |
|
| 2446 | + */ |
|
| 2447 | + public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2448 | + { |
|
| 2449 | + if ($unique) { |
|
| 2450 | + $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2451 | + array( |
|
| 2452 | + array( |
|
| 2453 | + 'EXM_key' => $meta_key, |
|
| 2454 | + 'OBJ_ID' => $this->ID(), |
|
| 2455 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2456 | + ), |
|
| 2457 | + ) |
|
| 2458 | + ); |
|
| 2459 | + if ($existing_extra_meta) { |
|
| 2460 | + return false; |
|
| 2461 | + } |
|
| 2462 | + } |
|
| 2463 | + $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2464 | + array( |
|
| 2465 | + 'EXM_key' => $meta_key, |
|
| 2466 | + 'EXM_value' => $meta_value, |
|
| 2467 | + 'OBJ_ID' => $this->ID(), |
|
| 2468 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2469 | + ) |
|
| 2470 | + ); |
|
| 2471 | + $new_extra_meta->save(); |
|
| 2472 | + return true; |
|
| 2473 | + } |
|
| 2474 | + |
|
| 2475 | + |
|
| 2476 | + |
|
| 2477 | + /** |
|
| 2478 | + * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2479 | + * is specified, only deletes extra meta records with that value. |
|
| 2480 | + * |
|
| 2481 | + * @param string $meta_key |
|
| 2482 | + * @param mixed $meta_value |
|
| 2483 | + * @return int number of extra meta rows deleted |
|
| 2484 | + * @throws \EE_Error |
|
| 2485 | + */ |
|
| 2486 | + public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2487 | + { |
|
| 2488 | + $query_params = array( |
|
| 2489 | + array( |
|
| 2490 | + 'EXM_key' => $meta_key, |
|
| 2491 | + 'OBJ_ID' => $this->ID(), |
|
| 2492 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2493 | + ), |
|
| 2494 | + ); |
|
| 2495 | + if ($meta_value !== null) { |
|
| 2496 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2497 | + } |
|
| 2498 | + return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2499 | + } |
|
| 2500 | + |
|
| 2501 | + |
|
| 2502 | + |
|
| 2503 | + /** |
|
| 2504 | + * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2505 | + * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2506 | + * You can specify $default is case you haven't found the extra meta |
|
| 2507 | + * |
|
| 2508 | + * @param string $meta_key |
|
| 2509 | + * @param boolean $single |
|
| 2510 | + * @param mixed $default if we don't find anything, what should we return? |
|
| 2511 | + * @return mixed single value if $single; array if ! $single |
|
| 2512 | + * @throws \EE_Error |
|
| 2513 | + */ |
|
| 2514 | + public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2515 | + { |
|
| 2516 | + if ($single) { |
|
| 2517 | + $result = $this->get_first_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2518 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2519 | + return $result->value(); |
|
| 2520 | + } else { |
|
| 2521 | + return $default; |
|
| 2522 | + } |
|
| 2523 | + } else { |
|
| 2524 | + $results = $this->get_many_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2525 | + if ($results) { |
|
| 2526 | + $values = array(); |
|
| 2527 | + foreach ($results as $result) { |
|
| 2528 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2529 | + $values[$result->ID()] = $result->value(); |
|
| 2530 | + } |
|
| 2531 | + } |
|
| 2532 | + return $values; |
|
| 2533 | + } else { |
|
| 2534 | + return $default; |
|
| 2535 | + } |
|
| 2536 | + } |
|
| 2537 | + } |
|
| 2538 | + |
|
| 2539 | + |
|
| 2540 | + |
|
| 2541 | + /** |
|
| 2542 | + * Returns a simple array of all the extra meta associated with this model object. |
|
| 2543 | + * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2544 | + * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2545 | + * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2546 | + * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2547 | + * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2548 | + * finally the extra meta's value as each sub-value. (eg |
|
| 2549 | + * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2550 | + * |
|
| 2551 | + * @param boolean $one_of_each_key |
|
| 2552 | + * @return array |
|
| 2553 | + * @throws \EE_Error |
|
| 2554 | + */ |
|
| 2555 | + public function all_extra_meta_array($one_of_each_key = true) |
|
| 2556 | + { |
|
| 2557 | + $return_array = array(); |
|
| 2558 | + if ($one_of_each_key) { |
|
| 2559 | + $extra_meta_objs = $this->get_many_related('Extra_Meta', array('group_by' => 'EXM_key')); |
|
| 2560 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2561 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2562 | + $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
| 2563 | + } |
|
| 2564 | + } |
|
| 2565 | + } else { |
|
| 2566 | + $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2567 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2568 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2569 | + if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
| 2570 | + $return_array[$extra_meta_obj->key()] = array(); |
|
| 2571 | + } |
|
| 2572 | + $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
| 2573 | + } |
|
| 2574 | + } |
|
| 2575 | + } |
|
| 2576 | + return $return_array; |
|
| 2577 | + } |
|
| 2578 | + |
|
| 2579 | + |
|
| 2580 | + |
|
| 2581 | + /** |
|
| 2582 | + * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2583 | + * |
|
| 2584 | + * @return string |
|
| 2585 | + * @throws \EE_Error |
|
| 2586 | + */ |
|
| 2587 | + public function name() |
|
| 2588 | + { |
|
| 2589 | + //find a field that's not a text field |
|
| 2590 | + $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2591 | + if ($field_we_can_use) { |
|
| 2592 | + return $this->get($field_we_can_use->get_name()); |
|
| 2593 | + } else { |
|
| 2594 | + $first_few_properties = $this->model_field_array(); |
|
| 2595 | + $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2596 | + $name_parts = array(); |
|
| 2597 | + foreach ($first_few_properties as $name => $value) { |
|
| 2598 | + $name_parts[] = "$name:$value"; |
|
| 2599 | + } |
|
| 2600 | + return implode(",", $name_parts); |
|
| 2601 | + } |
|
| 2602 | + } |
|
| 2603 | + |
|
| 2604 | + |
|
| 2605 | + |
|
| 2606 | + /** |
|
| 2607 | + * in_entity_map |
|
| 2608 | + * Checks if this model object has been proven to already be in the entity map |
|
| 2609 | + * |
|
| 2610 | + * @return boolean |
|
| 2611 | + * @throws \EE_Error |
|
| 2612 | + */ |
|
| 2613 | + public function in_entity_map() |
|
| 2614 | + { |
|
| 2615 | + if ($this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this) { |
|
| 2616 | + //well, if we looked, did we find it in the entity map? |
|
| 2617 | + return true; |
|
| 2618 | + } else { |
|
| 2619 | + return false; |
|
| 2620 | + } |
|
| 2621 | + } |
|
| 2622 | + |
|
| 2623 | + |
|
| 2624 | + |
|
| 2625 | + /** |
|
| 2626 | + * refresh_from_db |
|
| 2627 | + * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 2628 | + * |
|
| 2629 | + * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 2630 | + * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 2631 | + */ |
|
| 2632 | + public function refresh_from_db() |
|
| 2633 | + { |
|
| 2634 | + if ($this->ID() && $this->in_entity_map()) { |
|
| 2635 | + $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 2636 | + } else { |
|
| 2637 | + //if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 2638 | + //if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 2639 | + //that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 2640 | + //absolutely nothing in it for this ID |
|
| 2641 | + if (WP_DEBUG) { |
|
| 2642 | + throw new EE_Error( |
|
| 2643 | + sprintf( |
|
| 2644 | + __('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 2645 | + 'event_espresso'), |
|
| 2646 | + $this->ID(), |
|
| 2647 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 2648 | + get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 2649 | + ) |
|
| 2650 | + ); |
|
| 2651 | + } |
|
| 2652 | + } |
|
| 2653 | + } |
|
| 2654 | + |
|
| 2655 | + |
|
| 2656 | + |
|
| 2657 | + /** |
|
| 2658 | + * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 2659 | + * (probably a bad assumption they have made, oh well) |
|
| 2660 | + * |
|
| 2661 | + * @return string |
|
| 2662 | + */ |
|
| 2663 | + public function __toString() |
|
| 2664 | + { |
|
| 2665 | + try { |
|
| 2666 | + return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 2667 | + } catch (Exception $e) { |
|
| 2668 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2669 | + return ''; |
|
| 2670 | + } |
|
| 2671 | + } |
|
| 2672 | + |
|
| 2673 | + |
|
| 2674 | + |
|
| 2675 | + /** |
|
| 2676 | + * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 2677 | + * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 2678 | + * This means if we have made changes to those related model objects, and want to unserialize |
|
| 2679 | + * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 2680 | + * Instead, those related model objects should be directly serialized and stored. |
|
| 2681 | + * Eg, the following won't work: |
|
| 2682 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2683 | + * $att = $reg->attendee(); |
|
| 2684 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2685 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 2686 | + * //END REQUEST |
|
| 2687 | + * //START NEXT REQUEST |
|
| 2688 | + * $reg = get_option( 'my_option' ); |
|
| 2689 | + * $reg->attendee()->save(); |
|
| 2690 | + * And would need to be replace with: |
|
| 2691 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2692 | + * $att = $reg->attendee(); |
|
| 2693 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2694 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 2695 | + * //END REQUEST |
|
| 2696 | + * //START NEXT REQUEST |
|
| 2697 | + * $att = get_option( 'my_option' ); |
|
| 2698 | + * $att->save(); |
|
| 2699 | + * |
|
| 2700 | + * @return array |
|
| 2701 | + * @throws \EE_Error |
|
| 2702 | + */ |
|
| 2703 | + public function __sleep() |
|
| 2704 | + { |
|
| 2705 | + $model = $this->get_model(); |
|
| 2706 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 2707 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 2708 | + $classname = 'EE_' . $model->get_this_model_name(); |
|
| 2709 | + if ( |
|
| 2710 | + $this->get_one_from_cache($relation_name) instanceof $classname |
|
| 2711 | + && $this->get_one_from_cache($relation_name)->ID() |
|
| 2712 | + ) { |
|
| 2713 | + $this->clear_cache($relation_name, $this->get_one_from_cache($relation_name)->ID()); |
|
| 2714 | + } |
|
| 2715 | + } |
|
| 2716 | + } |
|
| 2717 | + $this->_props_n_values_provided_in_constructor = array(); |
|
| 2718 | + $properties_to_serialize = get_object_vars($this); |
|
| 2719 | + //don't serialize the model. It's big and that risks recursion |
|
| 2720 | + unset($properties_to_serialize['_model']); |
|
| 2721 | + return array_keys($properties_to_serialize); |
|
| 2722 | + } |
|
| 2723 | + |
|
| 2724 | + |
|
| 2725 | + |
|
| 2726 | + /** |
|
| 2727 | + * restore _props_n_values_provided_in_constructor |
|
| 2728 | + * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 2729 | + * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 2730 | + * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 2731 | + */ |
|
| 2732 | + public function __wakeup() |
|
| 2733 | + { |
|
| 2734 | + $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 2735 | + } |
|
| 2736 | 2736 | |
| 2737 | 2737 | |
| 2738 | 2738 | |
@@ -15,289 +15,289 @@ |
||
| 15 | 15 | class EE_Request implements InterminableInterface |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * $_GET parameters |
|
| 20 | - * |
|
| 21 | - * @var array $_get |
|
| 22 | - */ |
|
| 23 | - private $_get; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * $_POST parameters |
|
| 27 | - * |
|
| 28 | - * @var array $_post |
|
| 29 | - */ |
|
| 30 | - private $_post; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * $_COOKIE parameters |
|
| 34 | - * |
|
| 35 | - * @var array $_cookie |
|
| 36 | - */ |
|
| 37 | - private $_cookie; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * $_REQUEST parameters |
|
| 41 | - * |
|
| 42 | - * @var array $_params |
|
| 43 | - */ |
|
| 44 | - private $_params; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * whether current request is via AJAX |
|
| 48 | - * |
|
| 49 | - * @access public |
|
| 50 | - */ |
|
| 51 | - public $ajax = false; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * whether current request is via AJAX from the frontend of the site |
|
| 55 | - * |
|
| 56 | - * @access public |
|
| 57 | - */ |
|
| 58 | - public $front_ajax = false; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * IP address for request |
|
| 62 | - * |
|
| 63 | - * @var string $_ip_address |
|
| 64 | - */ |
|
| 65 | - private $_ip_address; |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * class constructor |
|
| 71 | - * |
|
| 72 | - * @access public |
|
| 73 | - * @param array $get |
|
| 74 | - * @param array $post |
|
| 75 | - * @param array $cookie |
|
| 76 | - */ |
|
| 77 | - public function __construct(array $get, array $post, array $cookie) |
|
| 78 | - { |
|
| 79 | - // grab request vars |
|
| 80 | - $this->_get = (array)$get; |
|
| 81 | - $this->_post = (array)$post; |
|
| 82 | - $this->_cookie = (array)$cookie; |
|
| 83 | - $this->_params = array_merge($this->_get, $this->_post); |
|
| 84 | - // AJAX ??? |
|
| 85 | - $this->ajax = defined('DOING_AJAX') ? true : false; |
|
| 86 | - $this->front_ajax = $this->is_set('ee_front_ajax') && (int)$this->get('ee_front_ajax') === 1; |
|
| 87 | - // grab user IP |
|
| 88 | - $this->_ip_address = $this->_visitor_ip(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @return array |
|
| 95 | - */ |
|
| 96 | - public function get_params() |
|
| 97 | - { |
|
| 98 | - return $this->_get; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @return array |
|
| 105 | - */ |
|
| 106 | - public function post_params() |
|
| 107 | - { |
|
| 108 | - return $this->_post; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @return array |
|
| 115 | - */ |
|
| 116 | - public function cookie_params() |
|
| 117 | - { |
|
| 118 | - return $this->_cookie; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * returns contents of $_REQUEST |
|
| 125 | - * |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - public function params() |
|
| 129 | - { |
|
| 130 | - return $this->_params; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @param $key |
|
| 137 | - * @param $value |
|
| 138 | - * @param bool $override_ee |
|
| 139 | - * @return void |
|
| 140 | - */ |
|
| 141 | - public function set($key, $value, $override_ee = false) |
|
| 142 | - { |
|
| 143 | - // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 144 | - if ( |
|
| 145 | - $key !== 'ee' |
|
| 146 | - || ($key === 'ee' && empty($this->_params['ee'])) |
|
| 147 | - || ($key === 'ee' && ! empty($this->_params['ee']) && $override_ee) |
|
| 148 | - ) { |
|
| 149 | - $this->_params[$key] = $value; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * returns the value for a request param if the given key exists |
|
| 157 | - * |
|
| 158 | - * @param $key |
|
| 159 | - * @param null $default |
|
| 160 | - * @return mixed |
|
| 161 | - */ |
|
| 162 | - public function get($key, $default = null) |
|
| 163 | - { |
|
| 164 | - return $this->request_parameter_drill_down($key, $default, 'get'); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * check if param exists |
|
| 171 | - * @param $key |
|
| 172 | - * @return bool |
|
| 173 | - */ |
|
| 174 | - public function is_set($key) |
|
| 175 | - { |
|
| 176 | - return $this->request_parameter_drill_down($key); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 183 | - * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 184 | - * by using square brackets to surround keys for deeper array elements. |
|
| 185 | - * For example : |
|
| 186 | - * if the supplied $key was: "first[second][third]" |
|
| 187 | - * then this will attempt to drill down into the request parameter array to find a value. |
|
| 188 | - * Given the following request parameters: |
|
| 189 | - * array( |
|
| 190 | - * 'first' => array( |
|
| 191 | - * 'second' => array( |
|
| 192 | - * 'third' => 'has a value' |
|
| 193 | - * ) |
|
| 194 | - * ) |
|
| 195 | - * ) |
|
| 196 | - * would return true |
|
| 197 | - * |
|
| 198 | - * @param string $is_set_or_get |
|
| 199 | - * @param $key |
|
| 200 | - * @param null $default |
|
| 201 | - * @param array $request_params |
|
| 202 | - * @return bool|mixed|null |
|
| 203 | - */ |
|
| 204 | - private function request_parameter_drill_down( |
|
| 205 | - $key, |
|
| 206 | - $default = null, |
|
| 207 | - $is_set_or_get = 'is_set', |
|
| 208 | - array $request_params = array() |
|
| 209 | - ) { |
|
| 210 | - $request_params = ! empty($request_params) |
|
| 211 | - ? $request_params |
|
| 212 | - : $this->_params; |
|
| 213 | - // does incoming key represent an array like 'first[second][third]' ? |
|
| 214 | - if (strpos($key, '[') !== false) { |
|
| 215 | - // turn it into an actual array |
|
| 216 | - $key = str_replace(']', '', $key); |
|
| 217 | - $keys = explode('[', $key); |
|
| 218 | - $key = array_shift($keys); |
|
| 219 | - // check if top level key exists |
|
| 220 | - if (isset($request_params[$key])) { |
|
| 221 | - // build a new key to pass along like: 'second[third]' |
|
| 222 | - // or just 'second' depending on depth of keys |
|
| 223 | - $key_string = array_shift($keys); |
|
| 224 | - if (! empty($keys)) { |
|
| 225 | - $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 226 | - } |
|
| 227 | - return $this->request_parameter_drill_down( |
|
| 228 | - $key_string, |
|
| 229 | - $default, |
|
| 230 | - $is_set_or_get, |
|
| 231 | - $request_params[$key] |
|
| 232 | - ); |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - if ($is_set_or_get === 'is_set') { |
|
| 236 | - return isset($request_params[$key]); |
|
| 237 | - } |
|
| 238 | - return isset($request_params[$key]) |
|
| 239 | - ? $request_params[$key] |
|
| 240 | - : $default; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * remove param |
|
| 247 | - * @param $key |
|
| 248 | - * @param bool $unset_from_global_too |
|
| 249 | - */ |
|
| 250 | - public function un_set($key, $unset_from_global_too = false) |
|
| 251 | - { |
|
| 252 | - unset($this->_params[$key]); |
|
| 253 | - if ($unset_from_global_too) { |
|
| 254 | - unset($_REQUEST[$key]); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @return string |
|
| 262 | - */ |
|
| 263 | - public function ip_address() |
|
| 264 | - { |
|
| 265 | - return $this->_ip_address; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * _visitor_ip |
|
| 272 | - * attempt to get IP address of current visitor from server |
|
| 273 | - * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 274 | - * |
|
| 275 | - * @access public |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 278 | - private function _visitor_ip() |
|
| 279 | - { |
|
| 280 | - $visitor_ip = '0.0.0.0'; |
|
| 281 | - $server_keys = array( |
|
| 282 | - 'HTTP_CLIENT_IP', |
|
| 283 | - 'HTTP_X_FORWARDED_FOR', |
|
| 284 | - 'HTTP_X_FORWARDED', |
|
| 285 | - 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 286 | - 'HTTP_FORWARDED_FOR', |
|
| 287 | - 'HTTP_FORWARDED', |
|
| 288 | - 'REMOTE_ADDR', |
|
| 289 | - ); |
|
| 290 | - foreach ($server_keys as $key) { |
|
| 291 | - if (isset($_SERVER[$key])) { |
|
| 292 | - foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) { |
|
| 293 | - if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 294 | - $visitor_ip = $ip; |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - return $visitor_ip; |
|
| 300 | - } |
|
| 18 | + /** |
|
| 19 | + * $_GET parameters |
|
| 20 | + * |
|
| 21 | + * @var array $_get |
|
| 22 | + */ |
|
| 23 | + private $_get; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * $_POST parameters |
|
| 27 | + * |
|
| 28 | + * @var array $_post |
|
| 29 | + */ |
|
| 30 | + private $_post; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * $_COOKIE parameters |
|
| 34 | + * |
|
| 35 | + * @var array $_cookie |
|
| 36 | + */ |
|
| 37 | + private $_cookie; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * $_REQUEST parameters |
|
| 41 | + * |
|
| 42 | + * @var array $_params |
|
| 43 | + */ |
|
| 44 | + private $_params; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * whether current request is via AJAX |
|
| 48 | + * |
|
| 49 | + * @access public |
|
| 50 | + */ |
|
| 51 | + public $ajax = false; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * whether current request is via AJAX from the frontend of the site |
|
| 55 | + * |
|
| 56 | + * @access public |
|
| 57 | + */ |
|
| 58 | + public $front_ajax = false; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * IP address for request |
|
| 62 | + * |
|
| 63 | + * @var string $_ip_address |
|
| 64 | + */ |
|
| 65 | + private $_ip_address; |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * class constructor |
|
| 71 | + * |
|
| 72 | + * @access public |
|
| 73 | + * @param array $get |
|
| 74 | + * @param array $post |
|
| 75 | + * @param array $cookie |
|
| 76 | + */ |
|
| 77 | + public function __construct(array $get, array $post, array $cookie) |
|
| 78 | + { |
|
| 79 | + // grab request vars |
|
| 80 | + $this->_get = (array)$get; |
|
| 81 | + $this->_post = (array)$post; |
|
| 82 | + $this->_cookie = (array)$cookie; |
|
| 83 | + $this->_params = array_merge($this->_get, $this->_post); |
|
| 84 | + // AJAX ??? |
|
| 85 | + $this->ajax = defined('DOING_AJAX') ? true : false; |
|
| 86 | + $this->front_ajax = $this->is_set('ee_front_ajax') && (int)$this->get('ee_front_ajax') === 1; |
|
| 87 | + // grab user IP |
|
| 88 | + $this->_ip_address = $this->_visitor_ip(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @return array |
|
| 95 | + */ |
|
| 96 | + public function get_params() |
|
| 97 | + { |
|
| 98 | + return $this->_get; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @return array |
|
| 105 | + */ |
|
| 106 | + public function post_params() |
|
| 107 | + { |
|
| 108 | + return $this->_post; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @return array |
|
| 115 | + */ |
|
| 116 | + public function cookie_params() |
|
| 117 | + { |
|
| 118 | + return $this->_cookie; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * returns contents of $_REQUEST |
|
| 125 | + * |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + public function params() |
|
| 129 | + { |
|
| 130 | + return $this->_params; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @param $key |
|
| 137 | + * @param $value |
|
| 138 | + * @param bool $override_ee |
|
| 139 | + * @return void |
|
| 140 | + */ |
|
| 141 | + public function set($key, $value, $override_ee = false) |
|
| 142 | + { |
|
| 143 | + // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 144 | + if ( |
|
| 145 | + $key !== 'ee' |
|
| 146 | + || ($key === 'ee' && empty($this->_params['ee'])) |
|
| 147 | + || ($key === 'ee' && ! empty($this->_params['ee']) && $override_ee) |
|
| 148 | + ) { |
|
| 149 | + $this->_params[$key] = $value; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * returns the value for a request param if the given key exists |
|
| 157 | + * |
|
| 158 | + * @param $key |
|
| 159 | + * @param null $default |
|
| 160 | + * @return mixed |
|
| 161 | + */ |
|
| 162 | + public function get($key, $default = null) |
|
| 163 | + { |
|
| 164 | + return $this->request_parameter_drill_down($key, $default, 'get'); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * check if param exists |
|
| 171 | + * @param $key |
|
| 172 | + * @return bool |
|
| 173 | + */ |
|
| 174 | + public function is_set($key) |
|
| 175 | + { |
|
| 176 | + return $this->request_parameter_drill_down($key); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 183 | + * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 184 | + * by using square brackets to surround keys for deeper array elements. |
|
| 185 | + * For example : |
|
| 186 | + * if the supplied $key was: "first[second][third]" |
|
| 187 | + * then this will attempt to drill down into the request parameter array to find a value. |
|
| 188 | + * Given the following request parameters: |
|
| 189 | + * array( |
|
| 190 | + * 'first' => array( |
|
| 191 | + * 'second' => array( |
|
| 192 | + * 'third' => 'has a value' |
|
| 193 | + * ) |
|
| 194 | + * ) |
|
| 195 | + * ) |
|
| 196 | + * would return true |
|
| 197 | + * |
|
| 198 | + * @param string $is_set_or_get |
|
| 199 | + * @param $key |
|
| 200 | + * @param null $default |
|
| 201 | + * @param array $request_params |
|
| 202 | + * @return bool|mixed|null |
|
| 203 | + */ |
|
| 204 | + private function request_parameter_drill_down( |
|
| 205 | + $key, |
|
| 206 | + $default = null, |
|
| 207 | + $is_set_or_get = 'is_set', |
|
| 208 | + array $request_params = array() |
|
| 209 | + ) { |
|
| 210 | + $request_params = ! empty($request_params) |
|
| 211 | + ? $request_params |
|
| 212 | + : $this->_params; |
|
| 213 | + // does incoming key represent an array like 'first[second][third]' ? |
|
| 214 | + if (strpos($key, '[') !== false) { |
|
| 215 | + // turn it into an actual array |
|
| 216 | + $key = str_replace(']', '', $key); |
|
| 217 | + $keys = explode('[', $key); |
|
| 218 | + $key = array_shift($keys); |
|
| 219 | + // check if top level key exists |
|
| 220 | + if (isset($request_params[$key])) { |
|
| 221 | + // build a new key to pass along like: 'second[third]' |
|
| 222 | + // or just 'second' depending on depth of keys |
|
| 223 | + $key_string = array_shift($keys); |
|
| 224 | + if (! empty($keys)) { |
|
| 225 | + $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 226 | + } |
|
| 227 | + return $this->request_parameter_drill_down( |
|
| 228 | + $key_string, |
|
| 229 | + $default, |
|
| 230 | + $is_set_or_get, |
|
| 231 | + $request_params[$key] |
|
| 232 | + ); |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + if ($is_set_or_get === 'is_set') { |
|
| 236 | + return isset($request_params[$key]); |
|
| 237 | + } |
|
| 238 | + return isset($request_params[$key]) |
|
| 239 | + ? $request_params[$key] |
|
| 240 | + : $default; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * remove param |
|
| 247 | + * @param $key |
|
| 248 | + * @param bool $unset_from_global_too |
|
| 249 | + */ |
|
| 250 | + public function un_set($key, $unset_from_global_too = false) |
|
| 251 | + { |
|
| 252 | + unset($this->_params[$key]); |
|
| 253 | + if ($unset_from_global_too) { |
|
| 254 | + unset($_REQUEST[$key]); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @return string |
|
| 262 | + */ |
|
| 263 | + public function ip_address() |
|
| 264 | + { |
|
| 265 | + return $this->_ip_address; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * _visitor_ip |
|
| 272 | + * attempt to get IP address of current visitor from server |
|
| 273 | + * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 274 | + * |
|
| 275 | + * @access public |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | + private function _visitor_ip() |
|
| 279 | + { |
|
| 280 | + $visitor_ip = '0.0.0.0'; |
|
| 281 | + $server_keys = array( |
|
| 282 | + 'HTTP_CLIENT_IP', |
|
| 283 | + 'HTTP_X_FORWARDED_FOR', |
|
| 284 | + 'HTTP_X_FORWARDED', |
|
| 285 | + 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 286 | + 'HTTP_FORWARDED_FOR', |
|
| 287 | + 'HTTP_FORWARDED', |
|
| 288 | + 'REMOTE_ADDR', |
|
| 289 | + ); |
|
| 290 | + foreach ($server_keys as $key) { |
|
| 291 | + if (isset($_SERVER[$key])) { |
|
| 292 | + foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) { |
|
| 293 | + if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 294 | + $visitor_ip = $ip; |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + return $visitor_ip; |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | 302 | |
| 303 | 303 | |
@@ -77,13 +77,13 @@ discard block |
||
| 77 | 77 | public function __construct(array $get, array $post, array $cookie) |
| 78 | 78 | { |
| 79 | 79 | // grab request vars |
| 80 | - $this->_get = (array)$get; |
|
| 81 | - $this->_post = (array)$post; |
|
| 82 | - $this->_cookie = (array)$cookie; |
|
| 80 | + $this->_get = (array) $get; |
|
| 81 | + $this->_post = (array) $post; |
|
| 82 | + $this->_cookie = (array) $cookie; |
|
| 83 | 83 | $this->_params = array_merge($this->_get, $this->_post); |
| 84 | 84 | // AJAX ??? |
| 85 | 85 | $this->ajax = defined('DOING_AJAX') ? true : false; |
| 86 | - $this->front_ajax = $this->is_set('ee_front_ajax') && (int)$this->get('ee_front_ajax') === 1; |
|
| 86 | + $this->front_ajax = $this->is_set('ee_front_ajax') && (int) $this->get('ee_front_ajax') === 1; |
|
| 87 | 87 | // grab user IP |
| 88 | 88 | $this->_ip_address = $this->_visitor_ip(); |
| 89 | 89 | } |
@@ -221,8 +221,8 @@ discard block |
||
| 221 | 221 | // build a new key to pass along like: 'second[third]' |
| 222 | 222 | // or just 'second' depending on depth of keys |
| 223 | 223 | $key_string = array_shift($keys); |
| 224 | - if (! empty($keys)) { |
|
| 225 | - $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 224 | + if ( ! empty($keys)) { |
|
| 225 | + $key_string .= '['.implode('][', $keys).']'; |
|
| 226 | 226 | } |
| 227 | 227 | return $this->request_parameter_drill_down( |
| 228 | 228 | $key_string, |
@@ -22,103 +22,103 @@ |
||
| 22 | 22 | { |
| 23 | 23 | |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var LoaderDecoratorInterface $new_loader |
|
| 27 | - */ |
|
| 28 | - private $new_loader; |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var LoaderDecoratorInterface $shared_loader |
|
| 33 | - */ |
|
| 34 | - private $shared_loader; |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Loader constructor. |
|
| 40 | - * |
|
| 41 | - * @param LoaderDecoratorInterface|null $new_loader |
|
| 42 | - * @param LoaderDecoratorInterface|null $shared_loader |
|
| 43 | - * @throws InvalidInterfaceException |
|
| 44 | - * @throws InvalidArgumentException |
|
| 45 | - * @throws InvalidDataTypeException |
|
| 46 | - */ |
|
| 47 | - public function __construct(LoaderDecoratorInterface $new_loader, LoaderDecoratorInterface $shared_loader) |
|
| 48 | - { |
|
| 49 | - $this->new_loader = $new_loader; |
|
| 50 | - $this->shared_loader = $shared_loader; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @return LoaderDecoratorInterface |
|
| 57 | - */ |
|
| 58 | - public function getNewLoader() |
|
| 59 | - { |
|
| 60 | - return $this->new_loader; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @return LoaderDecoratorInterface |
|
| 67 | - */ |
|
| 68 | - public function getSharedLoader() |
|
| 69 | - { |
|
| 70 | - return $this->shared_loader; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param string $fqcn |
|
| 77 | - * @param array $arguments |
|
| 78 | - * @param bool $shared |
|
| 79 | - * @return mixed |
|
| 80 | - */ |
|
| 81 | - public function load($fqcn, $arguments = array(), $shared = true) |
|
| 82 | - { |
|
| 83 | - return $shared |
|
| 84 | - ? $this->getSharedLoader()->load($fqcn, $arguments, $shared) |
|
| 85 | - : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $fqcn |
|
| 92 | - * @param array $arguments |
|
| 93 | - * @return mixed |
|
| 94 | - */ |
|
| 95 | - public function getNew($fqcn, $arguments = array()) |
|
| 96 | - { |
|
| 97 | - return $this->getNewLoader()->load($fqcn, $arguments, false); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param string $fqcn |
|
| 104 | - * @param array $arguments |
|
| 105 | - * @return mixed |
|
| 106 | - */ |
|
| 107 | - public function getShared($fqcn, $arguments = array()) |
|
| 108 | - { |
|
| 109 | - return $this->getSharedLoader()->load($fqcn, $arguments, true); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * calls reset() on loaders if that method exists |
|
| 116 | - */ |
|
| 117 | - public function reset() |
|
| 118 | - { |
|
| 119 | - $this->new_loader->reset(); |
|
| 120 | - $this->shared_loader->reset(); |
|
| 121 | - } |
|
| 25 | + /** |
|
| 26 | + * @var LoaderDecoratorInterface $new_loader |
|
| 27 | + */ |
|
| 28 | + private $new_loader; |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var LoaderDecoratorInterface $shared_loader |
|
| 33 | + */ |
|
| 34 | + private $shared_loader; |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Loader constructor. |
|
| 40 | + * |
|
| 41 | + * @param LoaderDecoratorInterface|null $new_loader |
|
| 42 | + * @param LoaderDecoratorInterface|null $shared_loader |
|
| 43 | + * @throws InvalidInterfaceException |
|
| 44 | + * @throws InvalidArgumentException |
|
| 45 | + * @throws InvalidDataTypeException |
|
| 46 | + */ |
|
| 47 | + public function __construct(LoaderDecoratorInterface $new_loader, LoaderDecoratorInterface $shared_loader) |
|
| 48 | + { |
|
| 49 | + $this->new_loader = $new_loader; |
|
| 50 | + $this->shared_loader = $shared_loader; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @return LoaderDecoratorInterface |
|
| 57 | + */ |
|
| 58 | + public function getNewLoader() |
|
| 59 | + { |
|
| 60 | + return $this->new_loader; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @return LoaderDecoratorInterface |
|
| 67 | + */ |
|
| 68 | + public function getSharedLoader() |
|
| 69 | + { |
|
| 70 | + return $this->shared_loader; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param string $fqcn |
|
| 77 | + * @param array $arguments |
|
| 78 | + * @param bool $shared |
|
| 79 | + * @return mixed |
|
| 80 | + */ |
|
| 81 | + public function load($fqcn, $arguments = array(), $shared = true) |
|
| 82 | + { |
|
| 83 | + return $shared |
|
| 84 | + ? $this->getSharedLoader()->load($fqcn, $arguments, $shared) |
|
| 85 | + : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $fqcn |
|
| 92 | + * @param array $arguments |
|
| 93 | + * @return mixed |
|
| 94 | + */ |
|
| 95 | + public function getNew($fqcn, $arguments = array()) |
|
| 96 | + { |
|
| 97 | + return $this->getNewLoader()->load($fqcn, $arguments, false); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param string $fqcn |
|
| 104 | + * @param array $arguments |
|
| 105 | + * @return mixed |
|
| 106 | + */ |
|
| 107 | + public function getShared($fqcn, $arguments = array()) |
|
| 108 | + { |
|
| 109 | + return $this->getSharedLoader()->load($fqcn, $arguments, true); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * calls reset() on loaders if that method exists |
|
| 116 | + */ |
|
| 117 | + public function reset() |
|
| 118 | + { |
|
| 119 | + $this->new_loader->reset(); |
|
| 120 | + $this->shared_loader->reset(); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | 123 | } |
| 124 | 124 | // End of file Loader.php |
@@ -18,305 +18,305 @@ |
||
| 18 | 18 | class DatetimeOffsetFix extends JobHandler |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Key for the option used to track which models have been processed when doing the batches. |
|
| 23 | - */ |
|
| 24 | - const MODELS_TO_PROCESS_OPTION_KEY = 'ee_models_processed_for_datetime_offset_fix'; |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - const COUNT_OF_MODELS_PROCESSED = 'ee_count_of_ee_models_processed_for_datetime_offset_fixed'; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Key for the option used to track what the current offset is that will be applied when this tool is executed. |
|
| 31 | - */ |
|
| 32 | - const OFFSET_TO_APPLY_OPTION_KEY = 'ee_datetime_offset_fix_offset_to_apply'; |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * String labelling the datetime offset fix type for change-log entries. |
|
| 37 | - */ |
|
| 38 | - const DATETIME_OFFSET_FIX_CHANGELOG_TYPE = 'datetime_offset_fix'; |
|
| 39 | - |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * String labelling a datetime offset fix error for change-log entries. |
|
| 43 | - */ |
|
| 44 | - const DATETIME_OFFSET_FIX_CHANGELOG_ERROR_TYPE = 'datetime_offset_fix_error'; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var EEM_Base[] |
|
| 48 | - */ |
|
| 49 | - protected $models_with_datetime_fields = array(); |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Performs any necessary setup for starting the job. This is also a good |
|
| 54 | - * place to setup the $job_arguments which will be used for subsequent HTTP requests |
|
| 55 | - * when continue_job will be called |
|
| 56 | - * |
|
| 57 | - * @param JobParameters $job_parameters |
|
| 58 | - * @throws BatchRequestException |
|
| 59 | - * @return JobStepResponse |
|
| 60 | - */ |
|
| 61 | - public function create_job(JobParameters $job_parameters) |
|
| 62 | - { |
|
| 63 | - $models_with_datetime_fields = $this->getModelsWithDatetimeFields(); |
|
| 64 | - //we'll be doing each model as a batch. |
|
| 65 | - $job_parameters->set_job_size(count($models_with_datetime_fields)); |
|
| 66 | - return new JobStepResponse( |
|
| 67 | - $job_parameters, |
|
| 68 | - esc_html__('Starting Datetime Offset Fix', 'event_espresso') |
|
| 69 | - ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Performs another step of the job |
|
| 74 | - * |
|
| 75 | - * @param JobParameters $job_parameters |
|
| 76 | - * @param int $batch_size |
|
| 77 | - * @return JobStepResponse |
|
| 78 | - * @throws \EE_Error |
|
| 79 | - */ |
|
| 80 | - public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
|
| 81 | - { |
|
| 82 | - $models_to_process = $this->getModelsWithDatetimeFields(); |
|
| 83 | - //let's pop off the a model and do the query to apply the offset. |
|
| 84 | - $model_to_process = array_pop($models_to_process); |
|
| 85 | - //update our record |
|
| 86 | - $this->setModelsToProcess($models_to_process); |
|
| 87 | - $this->processModel($model_to_process); |
|
| 88 | - $this->updateCountOfModelsProcessed(); |
|
| 89 | - $job_parameters->set_units_processed($this->getCountOfModelsProcessed()); |
|
| 90 | - if (count($models_to_process) > 0) { |
|
| 91 | - $job_parameters->set_status(JobParameters::status_continue); |
|
| 92 | - } else { |
|
| 93 | - $job_parameters->set_status(JobParameters::status_complete); |
|
| 94 | - } |
|
| 95 | - return new JobStepResponse( |
|
| 96 | - $job_parameters, |
|
| 97 | - sprintf( |
|
| 98 | - esc_html__('Updated the offset for all datetime fields on the %s model.', 'event_espresso'), |
|
| 99 | - $model_to_process |
|
| 100 | - ) |
|
| 101 | - ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Performs any clean-up logic when we know the job is completed |
|
| 106 | - * |
|
| 107 | - * @param JobParameters $job_parameters |
|
| 108 | - * @return JobStepResponse |
|
| 109 | - * @throws BatchRequestException |
|
| 110 | - */ |
|
| 111 | - public function cleanup_job(JobParameters $job_parameters) |
|
| 112 | - { |
|
| 113 | - //delete important saved options. |
|
| 114 | - delete_option(self::MODELS_TO_PROCESS_OPTION_KEY); |
|
| 115 | - delete_option(self::COUNT_OF_MODELS_PROCESSED); |
|
| 116 | - return new JobStepResponse($job_parameters, esc_html__( |
|
| 117 | - 'Offset has been applied to all affected fields.', |
|
| 118 | - 'event_espresso' |
|
| 119 | - )); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Contains the logic for processing a model and applying the datetime offset to affected fields on that model. |
|
| 125 | - * @param string $model_class_name |
|
| 126 | - * @throws \EE_Error |
|
| 127 | - */ |
|
| 128 | - protected function processModel($model_class_name) |
|
| 129 | - { |
|
| 130 | - global $wpdb; |
|
| 131 | - /** @var EEM_Base $model */ |
|
| 132 | - $model = $model_class_name::instance(); |
|
| 133 | - $original_offset = self::getOffset(); |
|
| 134 | - $sql_date_function = $original_offset > 0 ? 'DATE_ADD' : 'DATE_SUB'; |
|
| 135 | - $offset = abs($original_offset) * 60; |
|
| 136 | - //since some affected models might have two tables, we have to get our tables and set up a query for each table. |
|
| 137 | - foreach ($model->get_tables() as $table) { |
|
| 138 | - $query = 'UPDATE ' . $table->get_table_name(); |
|
| 139 | - $fields_affected = array(); |
|
| 140 | - $inner_query = array(); |
|
| 141 | - foreach ($model->_get_fields_for_table($table->get_table_alias()) as $model_field) { |
|
| 142 | - if ($model_field instanceof EE_Datetime_Field) { |
|
| 143 | - $inner_query[] = $model_field->get_table_column() . ' = ' |
|
| 144 | - . $sql_date_function . '(' |
|
| 145 | - . $model_field->get_table_column() |
|
| 146 | - . ", INTERVAL $offset MINUTE)"; |
|
| 147 | - $fields_affected[] = $model_field; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - if (! $fields_affected) { |
|
| 151 | - continue; |
|
| 152 | - } |
|
| 153 | - //k convert innerquery to string |
|
| 154 | - $query .= ' SET ' . implode(',', $inner_query); |
|
| 155 | - //execute query |
|
| 156 | - $result = $wpdb->query($query); |
|
| 157 | - //record log |
|
| 158 | - if ($result !== false) { |
|
| 159 | - $this->recordChangeLog($model, $original_offset, $table, $fields_affected); |
|
| 160 | - } else { |
|
| 161 | - //record error. |
|
| 162 | - $error_message = $wpdb->last_error; |
|
| 163 | - //handle the edgecases where last_error might be empty. |
|
| 164 | - if (! $error_message) { |
|
| 165 | - $error_message = esc_html__('Unknown mysql error occured.', 'event_espresso'); |
|
| 166 | - } |
|
| 167 | - $this->recordChangeLog($model, $original_offset, $table, $fields_affected, $error_message); |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Records a changelog entry using the given information. |
|
| 175 | - * |
|
| 176 | - * @param EEM_Base $model |
|
| 177 | - * @param float $offset |
|
| 178 | - * @param EE_Table_Base $table |
|
| 179 | - * @param EE_Model_Field_Base[] $model_fields_affected |
|
| 180 | - * @param string $error_message If present then there was an error so let's record that instead. |
|
| 181 | - * @throws \EE_Error |
|
| 182 | - */ |
|
| 183 | - private function recordChangeLog( |
|
| 184 | - EEM_Base $model, |
|
| 185 | - $offset, |
|
| 186 | - EE_Table_Base $table, |
|
| 187 | - $model_fields_affected, |
|
| 188 | - $error_message = '' |
|
| 189 | - ) { |
|
| 190 | - //setup $fields list. |
|
| 191 | - $fields = array(); |
|
| 192 | - /** @var EE_Datetime_Field $model_field */ |
|
| 193 | - foreach ($model_fields_affected as $model_field) { |
|
| 194 | - if (! $model_field instanceof EE_Datetime_Field) { |
|
| 195 | - continue; |
|
| 196 | - } |
|
| 197 | - $fields[] = $model_field->get_name(); |
|
| 198 | - } |
|
| 199 | - //setup the message for the changelog entry. |
|
| 200 | - $message = $error_message |
|
| 201 | - ? sprintf( |
|
| 202 | - esc_html__( |
|
| 203 | - 'The %1$s table for the %2$s model did not have the offset of %3$f applied to its fields (%4$s), because of the following error:%5$s', |
|
| 204 | - 'event_espresso' |
|
| 205 | - ), |
|
| 206 | - $table->get_table_name(), |
|
| 207 | - $model->get_this_model_name(), |
|
| 208 | - $offset, |
|
| 209 | - implode(',', $fields), |
|
| 210 | - $error_message |
|
| 211 | - ) |
|
| 212 | - : sprintf( |
|
| 213 | - esc_html__( |
|
| 214 | - 'The %1$s table for the %2$s model has had the offset of %3$f applied to its following fields: %4$s', |
|
| 215 | - 'event_espresso' |
|
| 216 | - ), |
|
| 217 | - $table->get_table_name(), |
|
| 218 | - $model->get_this_model_name(), |
|
| 219 | - $offset, |
|
| 220 | - implode(',', $fields) |
|
| 221 | - ); |
|
| 222 | - //write to the log |
|
| 223 | - $changelog = EE_Change_Log::new_instance(array( |
|
| 224 | - 'LOG_type' => $error_message |
|
| 225 | - ? self::DATETIME_OFFSET_FIX_CHANGELOG_ERROR_TYPE |
|
| 226 | - : self::DATETIME_OFFSET_FIX_CHANGELOG_TYPE, |
|
| 227 | - 'LOG_message' => $message |
|
| 228 | - )); |
|
| 229 | - $changelog->save(); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Returns an array of models that have datetime fields. |
|
| 235 | - * This array is added to a short lived transient cache to keep having to build this list to a minimum. |
|
| 236 | - * @return array an array of model class names. |
|
| 237 | - */ |
|
| 238 | - private function getModelsWithDatetimeFields() |
|
| 239 | - { |
|
| 240 | - $this->getModelsToProcess(); |
|
| 241 | - if (! empty($this->models_with_datetime_fields)) { |
|
| 242 | - return $this->models_with_datetime_fields; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - $all_non_abstract_models = EE_Registry::instance()->non_abstract_db_models; |
|
| 246 | - foreach ($all_non_abstract_models as $non_abstract_model) { |
|
| 247 | - //get model instance |
|
| 248 | - /** @var EEM_Base $non_abstract_model */ |
|
| 249 | - $non_abstract_model = $non_abstract_model::instance(); |
|
| 250 | - if ($non_abstract_model->get_a_field_of_type('EE_Datetime_Field') instanceof EE_Datetime_Field) { |
|
| 251 | - $this->models_with_datetime_fields[] = get_class($non_abstract_model); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - $this->setModelsToProcess($this->models_with_datetime_fields); |
|
| 255 | - return $this->models_with_datetime_fields; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * This simply records the models that have been processed with our tracking option. |
|
| 261 | - * @param array $models_to_set array of model class names. |
|
| 262 | - */ |
|
| 263 | - private function setModelsToProcess($models_to_set) |
|
| 264 | - { |
|
| 265 | - update_option(self::MODELS_TO_PROCESS_OPTION_KEY, $models_to_set); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Used to keep track of how many models have been processed for the batch |
|
| 271 | - * @param $count |
|
| 272 | - */ |
|
| 273 | - private function updateCountOfModelsProcessed($count = 1) |
|
| 274 | - { |
|
| 275 | - $count = $this->getCountOfModelsProcessed() + (int) $count; |
|
| 276 | - update_option(self::COUNT_OF_MODELS_PROCESSED, $count); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Retrieve the tracked number of models processed between requests. |
|
| 282 | - * @return int |
|
| 283 | - */ |
|
| 284 | - private function getCountOfModelsProcessed() |
|
| 285 | - { |
|
| 286 | - return (int) get_option(self::COUNT_OF_MODELS_PROCESSED, 0); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Returns the models that are left to process. |
|
| 292 | - * @return array an array of model class names. |
|
| 293 | - */ |
|
| 294 | - private function getModelsToProcess() |
|
| 295 | - { |
|
| 296 | - if (empty($this->models_with_datetime_fields)) { |
|
| 297 | - $this->models_with_datetime_fields = get_option(self::MODELS_TO_PROCESS_OPTION_KEY, array()); |
|
| 298 | - } |
|
| 299 | - return $this->models_with_datetime_fields; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Used to record the offset that will be applied to dates and times for EE_Datetime_Field columns. |
|
| 305 | - * @param float $offset |
|
| 306 | - */ |
|
| 307 | - public static function updateOffset($offset) |
|
| 308 | - { |
|
| 309 | - update_option(self::OFFSET_TO_APPLY_OPTION_KEY, $offset); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Used to retrieve the saved offset that will be applied to dates and times for EE_Datetime_Field columns. |
|
| 315 | - * |
|
| 316 | - * @return float |
|
| 317 | - */ |
|
| 318 | - public static function getOffset() |
|
| 319 | - { |
|
| 320 | - return (float) get_option(self::OFFSET_TO_APPLY_OPTION_KEY, 0); |
|
| 321 | - } |
|
| 21 | + /** |
|
| 22 | + * Key for the option used to track which models have been processed when doing the batches. |
|
| 23 | + */ |
|
| 24 | + const MODELS_TO_PROCESS_OPTION_KEY = 'ee_models_processed_for_datetime_offset_fix'; |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + const COUNT_OF_MODELS_PROCESSED = 'ee_count_of_ee_models_processed_for_datetime_offset_fixed'; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Key for the option used to track what the current offset is that will be applied when this tool is executed. |
|
| 31 | + */ |
|
| 32 | + const OFFSET_TO_APPLY_OPTION_KEY = 'ee_datetime_offset_fix_offset_to_apply'; |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * String labelling the datetime offset fix type for change-log entries. |
|
| 37 | + */ |
|
| 38 | + const DATETIME_OFFSET_FIX_CHANGELOG_TYPE = 'datetime_offset_fix'; |
|
| 39 | + |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * String labelling a datetime offset fix error for change-log entries. |
|
| 43 | + */ |
|
| 44 | + const DATETIME_OFFSET_FIX_CHANGELOG_ERROR_TYPE = 'datetime_offset_fix_error'; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var EEM_Base[] |
|
| 48 | + */ |
|
| 49 | + protected $models_with_datetime_fields = array(); |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Performs any necessary setup for starting the job. This is also a good |
|
| 54 | + * place to setup the $job_arguments which will be used for subsequent HTTP requests |
|
| 55 | + * when continue_job will be called |
|
| 56 | + * |
|
| 57 | + * @param JobParameters $job_parameters |
|
| 58 | + * @throws BatchRequestException |
|
| 59 | + * @return JobStepResponse |
|
| 60 | + */ |
|
| 61 | + public function create_job(JobParameters $job_parameters) |
|
| 62 | + { |
|
| 63 | + $models_with_datetime_fields = $this->getModelsWithDatetimeFields(); |
|
| 64 | + //we'll be doing each model as a batch. |
|
| 65 | + $job_parameters->set_job_size(count($models_with_datetime_fields)); |
|
| 66 | + return new JobStepResponse( |
|
| 67 | + $job_parameters, |
|
| 68 | + esc_html__('Starting Datetime Offset Fix', 'event_espresso') |
|
| 69 | + ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Performs another step of the job |
|
| 74 | + * |
|
| 75 | + * @param JobParameters $job_parameters |
|
| 76 | + * @param int $batch_size |
|
| 77 | + * @return JobStepResponse |
|
| 78 | + * @throws \EE_Error |
|
| 79 | + */ |
|
| 80 | + public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
|
| 81 | + { |
|
| 82 | + $models_to_process = $this->getModelsWithDatetimeFields(); |
|
| 83 | + //let's pop off the a model and do the query to apply the offset. |
|
| 84 | + $model_to_process = array_pop($models_to_process); |
|
| 85 | + //update our record |
|
| 86 | + $this->setModelsToProcess($models_to_process); |
|
| 87 | + $this->processModel($model_to_process); |
|
| 88 | + $this->updateCountOfModelsProcessed(); |
|
| 89 | + $job_parameters->set_units_processed($this->getCountOfModelsProcessed()); |
|
| 90 | + if (count($models_to_process) > 0) { |
|
| 91 | + $job_parameters->set_status(JobParameters::status_continue); |
|
| 92 | + } else { |
|
| 93 | + $job_parameters->set_status(JobParameters::status_complete); |
|
| 94 | + } |
|
| 95 | + return new JobStepResponse( |
|
| 96 | + $job_parameters, |
|
| 97 | + sprintf( |
|
| 98 | + esc_html__('Updated the offset for all datetime fields on the %s model.', 'event_espresso'), |
|
| 99 | + $model_to_process |
|
| 100 | + ) |
|
| 101 | + ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Performs any clean-up logic when we know the job is completed |
|
| 106 | + * |
|
| 107 | + * @param JobParameters $job_parameters |
|
| 108 | + * @return JobStepResponse |
|
| 109 | + * @throws BatchRequestException |
|
| 110 | + */ |
|
| 111 | + public function cleanup_job(JobParameters $job_parameters) |
|
| 112 | + { |
|
| 113 | + //delete important saved options. |
|
| 114 | + delete_option(self::MODELS_TO_PROCESS_OPTION_KEY); |
|
| 115 | + delete_option(self::COUNT_OF_MODELS_PROCESSED); |
|
| 116 | + return new JobStepResponse($job_parameters, esc_html__( |
|
| 117 | + 'Offset has been applied to all affected fields.', |
|
| 118 | + 'event_espresso' |
|
| 119 | + )); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Contains the logic for processing a model and applying the datetime offset to affected fields on that model. |
|
| 125 | + * @param string $model_class_name |
|
| 126 | + * @throws \EE_Error |
|
| 127 | + */ |
|
| 128 | + protected function processModel($model_class_name) |
|
| 129 | + { |
|
| 130 | + global $wpdb; |
|
| 131 | + /** @var EEM_Base $model */ |
|
| 132 | + $model = $model_class_name::instance(); |
|
| 133 | + $original_offset = self::getOffset(); |
|
| 134 | + $sql_date_function = $original_offset > 0 ? 'DATE_ADD' : 'DATE_SUB'; |
|
| 135 | + $offset = abs($original_offset) * 60; |
|
| 136 | + //since some affected models might have two tables, we have to get our tables and set up a query for each table. |
|
| 137 | + foreach ($model->get_tables() as $table) { |
|
| 138 | + $query = 'UPDATE ' . $table->get_table_name(); |
|
| 139 | + $fields_affected = array(); |
|
| 140 | + $inner_query = array(); |
|
| 141 | + foreach ($model->_get_fields_for_table($table->get_table_alias()) as $model_field) { |
|
| 142 | + if ($model_field instanceof EE_Datetime_Field) { |
|
| 143 | + $inner_query[] = $model_field->get_table_column() . ' = ' |
|
| 144 | + . $sql_date_function . '(' |
|
| 145 | + . $model_field->get_table_column() |
|
| 146 | + . ", INTERVAL $offset MINUTE)"; |
|
| 147 | + $fields_affected[] = $model_field; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + if (! $fields_affected) { |
|
| 151 | + continue; |
|
| 152 | + } |
|
| 153 | + //k convert innerquery to string |
|
| 154 | + $query .= ' SET ' . implode(',', $inner_query); |
|
| 155 | + //execute query |
|
| 156 | + $result = $wpdb->query($query); |
|
| 157 | + //record log |
|
| 158 | + if ($result !== false) { |
|
| 159 | + $this->recordChangeLog($model, $original_offset, $table, $fields_affected); |
|
| 160 | + } else { |
|
| 161 | + //record error. |
|
| 162 | + $error_message = $wpdb->last_error; |
|
| 163 | + //handle the edgecases where last_error might be empty. |
|
| 164 | + if (! $error_message) { |
|
| 165 | + $error_message = esc_html__('Unknown mysql error occured.', 'event_espresso'); |
|
| 166 | + } |
|
| 167 | + $this->recordChangeLog($model, $original_offset, $table, $fields_affected, $error_message); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Records a changelog entry using the given information. |
|
| 175 | + * |
|
| 176 | + * @param EEM_Base $model |
|
| 177 | + * @param float $offset |
|
| 178 | + * @param EE_Table_Base $table |
|
| 179 | + * @param EE_Model_Field_Base[] $model_fields_affected |
|
| 180 | + * @param string $error_message If present then there was an error so let's record that instead. |
|
| 181 | + * @throws \EE_Error |
|
| 182 | + */ |
|
| 183 | + private function recordChangeLog( |
|
| 184 | + EEM_Base $model, |
|
| 185 | + $offset, |
|
| 186 | + EE_Table_Base $table, |
|
| 187 | + $model_fields_affected, |
|
| 188 | + $error_message = '' |
|
| 189 | + ) { |
|
| 190 | + //setup $fields list. |
|
| 191 | + $fields = array(); |
|
| 192 | + /** @var EE_Datetime_Field $model_field */ |
|
| 193 | + foreach ($model_fields_affected as $model_field) { |
|
| 194 | + if (! $model_field instanceof EE_Datetime_Field) { |
|
| 195 | + continue; |
|
| 196 | + } |
|
| 197 | + $fields[] = $model_field->get_name(); |
|
| 198 | + } |
|
| 199 | + //setup the message for the changelog entry. |
|
| 200 | + $message = $error_message |
|
| 201 | + ? sprintf( |
|
| 202 | + esc_html__( |
|
| 203 | + 'The %1$s table for the %2$s model did not have the offset of %3$f applied to its fields (%4$s), because of the following error:%5$s', |
|
| 204 | + 'event_espresso' |
|
| 205 | + ), |
|
| 206 | + $table->get_table_name(), |
|
| 207 | + $model->get_this_model_name(), |
|
| 208 | + $offset, |
|
| 209 | + implode(',', $fields), |
|
| 210 | + $error_message |
|
| 211 | + ) |
|
| 212 | + : sprintf( |
|
| 213 | + esc_html__( |
|
| 214 | + 'The %1$s table for the %2$s model has had the offset of %3$f applied to its following fields: %4$s', |
|
| 215 | + 'event_espresso' |
|
| 216 | + ), |
|
| 217 | + $table->get_table_name(), |
|
| 218 | + $model->get_this_model_name(), |
|
| 219 | + $offset, |
|
| 220 | + implode(',', $fields) |
|
| 221 | + ); |
|
| 222 | + //write to the log |
|
| 223 | + $changelog = EE_Change_Log::new_instance(array( |
|
| 224 | + 'LOG_type' => $error_message |
|
| 225 | + ? self::DATETIME_OFFSET_FIX_CHANGELOG_ERROR_TYPE |
|
| 226 | + : self::DATETIME_OFFSET_FIX_CHANGELOG_TYPE, |
|
| 227 | + 'LOG_message' => $message |
|
| 228 | + )); |
|
| 229 | + $changelog->save(); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Returns an array of models that have datetime fields. |
|
| 235 | + * This array is added to a short lived transient cache to keep having to build this list to a minimum. |
|
| 236 | + * @return array an array of model class names. |
|
| 237 | + */ |
|
| 238 | + private function getModelsWithDatetimeFields() |
|
| 239 | + { |
|
| 240 | + $this->getModelsToProcess(); |
|
| 241 | + if (! empty($this->models_with_datetime_fields)) { |
|
| 242 | + return $this->models_with_datetime_fields; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + $all_non_abstract_models = EE_Registry::instance()->non_abstract_db_models; |
|
| 246 | + foreach ($all_non_abstract_models as $non_abstract_model) { |
|
| 247 | + //get model instance |
|
| 248 | + /** @var EEM_Base $non_abstract_model */ |
|
| 249 | + $non_abstract_model = $non_abstract_model::instance(); |
|
| 250 | + if ($non_abstract_model->get_a_field_of_type('EE_Datetime_Field') instanceof EE_Datetime_Field) { |
|
| 251 | + $this->models_with_datetime_fields[] = get_class($non_abstract_model); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + $this->setModelsToProcess($this->models_with_datetime_fields); |
|
| 255 | + return $this->models_with_datetime_fields; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * This simply records the models that have been processed with our tracking option. |
|
| 261 | + * @param array $models_to_set array of model class names. |
|
| 262 | + */ |
|
| 263 | + private function setModelsToProcess($models_to_set) |
|
| 264 | + { |
|
| 265 | + update_option(self::MODELS_TO_PROCESS_OPTION_KEY, $models_to_set); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Used to keep track of how many models have been processed for the batch |
|
| 271 | + * @param $count |
|
| 272 | + */ |
|
| 273 | + private function updateCountOfModelsProcessed($count = 1) |
|
| 274 | + { |
|
| 275 | + $count = $this->getCountOfModelsProcessed() + (int) $count; |
|
| 276 | + update_option(self::COUNT_OF_MODELS_PROCESSED, $count); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Retrieve the tracked number of models processed between requests. |
|
| 282 | + * @return int |
|
| 283 | + */ |
|
| 284 | + private function getCountOfModelsProcessed() |
|
| 285 | + { |
|
| 286 | + return (int) get_option(self::COUNT_OF_MODELS_PROCESSED, 0); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Returns the models that are left to process. |
|
| 292 | + * @return array an array of model class names. |
|
| 293 | + */ |
|
| 294 | + private function getModelsToProcess() |
|
| 295 | + { |
|
| 296 | + if (empty($this->models_with_datetime_fields)) { |
|
| 297 | + $this->models_with_datetime_fields = get_option(self::MODELS_TO_PROCESS_OPTION_KEY, array()); |
|
| 298 | + } |
|
| 299 | + return $this->models_with_datetime_fields; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Used to record the offset that will be applied to dates and times for EE_Datetime_Field columns. |
|
| 305 | + * @param float $offset |
|
| 306 | + */ |
|
| 307 | + public static function updateOffset($offset) |
|
| 308 | + { |
|
| 309 | + update_option(self::OFFSET_TO_APPLY_OPTION_KEY, $offset); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Used to retrieve the saved offset that will be applied to dates and times for EE_Datetime_Field columns. |
|
| 315 | + * |
|
| 316 | + * @return float |
|
| 317 | + */ |
|
| 318 | + public static function getOffset() |
|
| 319 | + { |
|
| 320 | + return (float) get_option(self::OFFSET_TO_APPLY_OPTION_KEY, 0); |
|
| 321 | + } |
|
| 322 | 322 | } |
@@ -135,23 +135,23 @@ discard block |
||
| 135 | 135 | $offset = abs($original_offset) * 60; |
| 136 | 136 | //since some affected models might have two tables, we have to get our tables and set up a query for each table. |
| 137 | 137 | foreach ($model->get_tables() as $table) { |
| 138 | - $query = 'UPDATE ' . $table->get_table_name(); |
|
| 138 | + $query = 'UPDATE '.$table->get_table_name(); |
|
| 139 | 139 | $fields_affected = array(); |
| 140 | 140 | $inner_query = array(); |
| 141 | 141 | foreach ($model->_get_fields_for_table($table->get_table_alias()) as $model_field) { |
| 142 | 142 | if ($model_field instanceof EE_Datetime_Field) { |
| 143 | - $inner_query[] = $model_field->get_table_column() . ' = ' |
|
| 144 | - . $sql_date_function . '(' |
|
| 143 | + $inner_query[] = $model_field->get_table_column().' = ' |
|
| 144 | + . $sql_date_function.'(' |
|
| 145 | 145 | . $model_field->get_table_column() |
| 146 | 146 | . ", INTERVAL $offset MINUTE)"; |
| 147 | 147 | $fields_affected[] = $model_field; |
| 148 | 148 | } |
| 149 | 149 | } |
| 150 | - if (! $fields_affected) { |
|
| 150 | + if ( ! $fields_affected) { |
|
| 151 | 151 | continue; |
| 152 | 152 | } |
| 153 | 153 | //k convert innerquery to string |
| 154 | - $query .= ' SET ' . implode(',', $inner_query); |
|
| 154 | + $query .= ' SET '.implode(',', $inner_query); |
|
| 155 | 155 | //execute query |
| 156 | 156 | $result = $wpdb->query($query); |
| 157 | 157 | //record log |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | //record error. |
| 162 | 162 | $error_message = $wpdb->last_error; |
| 163 | 163 | //handle the edgecases where last_error might be empty. |
| 164 | - if (! $error_message) { |
|
| 164 | + if ( ! $error_message) { |
|
| 165 | 165 | $error_message = esc_html__('Unknown mysql error occured.', 'event_espresso'); |
| 166 | 166 | } |
| 167 | 167 | $this->recordChangeLog($model, $original_offset, $table, $fields_affected, $error_message); |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | $fields = array(); |
| 192 | 192 | /** @var EE_Datetime_Field $model_field */ |
| 193 | 193 | foreach ($model_fields_affected as $model_field) { |
| 194 | - if (! $model_field instanceof EE_Datetime_Field) { |
|
| 194 | + if ( ! $model_field instanceof EE_Datetime_Field) { |
|
| 195 | 195 | continue; |
| 196 | 196 | } |
| 197 | 197 | $fields[] = $model_field->get_name(); |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | private function getModelsWithDatetimeFields() |
| 239 | 239 | { |
| 240 | 240 | $this->getModelsToProcess(); |
| 241 | - if (! empty($this->models_with_datetime_fields)) { |
|
| 241 | + if ( ! empty($this->models_with_datetime_fields)) { |
|
| 242 | 242 | return $this->models_with_datetime_fields; |
| 243 | 243 | } |
| 244 | 244 | |
@@ -20,159 +20,159 @@ |
||
| 20 | 20 | class EE_Register_Shortcode implements EEI_Plugin_API |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Holds values for registered shortcodes |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - protected static $_settings = array(); |
|
| 23 | + /** |
|
| 24 | + * Holds values for registered shortcodes |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + protected static $_settings = array(); |
|
| 29 | 29 | |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Method for registering new EE_Shortcodes |
|
| 33 | - * |
|
| 34 | - * @since 4.3.0 |
|
| 35 | - * @since 4.9.46.rc.025 for the new `shortcode_fqcns` array argument. |
|
| 36 | - * @param string $shortcode_id a unique identifier for this set of modules Required. |
|
| 37 | - * @param array $setup_args an array of arguments provided for registering shortcodes Required. |
|
| 38 | - * @type array shortcode_paths an array of full server paths to folders containing any |
|
| 39 | - * EES_Shortcodes |
|
| 40 | - * @type array shortcode_fqcns an array of fully qualified class names for any new shortcode |
|
| 41 | - * classes to register. Shortcode classes should extend |
|
| 42 | - * EspressoShortcode and be properly namespaced so they are |
|
| 43 | - * autoloaded. |
|
| 44 | - * @throws EE_Error |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public static function register($shortcode_id = null, $setup_args = array()) |
|
| 48 | - { |
|
| 49 | - //required fields MUST be present, so let's make sure they are. |
|
| 50 | - if (empty($shortcode_id) |
|
| 51 | - || ! is_array($setup_args) |
|
| 52 | - || ( |
|
| 53 | - empty($setup_args['shortcode_paths'])) |
|
| 54 | - && empty($setup_args['shortcode_fqcns']) |
|
| 55 | - ) { |
|
| 56 | - throw new EE_Error( |
|
| 57 | - esc_html__( |
|
| 58 | - 'In order to register Modules with EE_Register_Shortcode::register(), you must include a "shortcode_id" (a unique identifier for this set of shortcodes), and an array containing the following keys: "shortcode_paths" (an array of full server paths to folders that contain shortcodes, or to the shortcode files themselves)', |
|
| 59 | - 'event_espresso' |
|
| 60 | - ) |
|
| 61 | - ); |
|
| 62 | - } |
|
| 31 | + /** |
|
| 32 | + * Method for registering new EE_Shortcodes |
|
| 33 | + * |
|
| 34 | + * @since 4.3.0 |
|
| 35 | + * @since 4.9.46.rc.025 for the new `shortcode_fqcns` array argument. |
|
| 36 | + * @param string $shortcode_id a unique identifier for this set of modules Required. |
|
| 37 | + * @param array $setup_args an array of arguments provided for registering shortcodes Required. |
|
| 38 | + * @type array shortcode_paths an array of full server paths to folders containing any |
|
| 39 | + * EES_Shortcodes |
|
| 40 | + * @type array shortcode_fqcns an array of fully qualified class names for any new shortcode |
|
| 41 | + * classes to register. Shortcode classes should extend |
|
| 42 | + * EspressoShortcode and be properly namespaced so they are |
|
| 43 | + * autoloaded. |
|
| 44 | + * @throws EE_Error |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public static function register($shortcode_id = null, $setup_args = array()) |
|
| 48 | + { |
|
| 49 | + //required fields MUST be present, so let's make sure they are. |
|
| 50 | + if (empty($shortcode_id) |
|
| 51 | + || ! is_array($setup_args) |
|
| 52 | + || ( |
|
| 53 | + empty($setup_args['shortcode_paths'])) |
|
| 54 | + && empty($setup_args['shortcode_fqcns']) |
|
| 55 | + ) { |
|
| 56 | + throw new EE_Error( |
|
| 57 | + esc_html__( |
|
| 58 | + 'In order to register Modules with EE_Register_Shortcode::register(), you must include a "shortcode_id" (a unique identifier for this set of shortcodes), and an array containing the following keys: "shortcode_paths" (an array of full server paths to folders that contain shortcodes, or to the shortcode files themselves)', |
|
| 59 | + 'event_espresso' |
|
| 60 | + ) |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - //make sure we don't register twice |
|
| 65 | - if (isset(self::$_settings[$shortcode_id])) { |
|
| 66 | - return; |
|
| 67 | - } |
|
| 64 | + //make sure we don't register twice |
|
| 65 | + if (isset(self::$_settings[$shortcode_id])) { |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - //make sure this was called in the right place! |
|
| 70 | - if (! did_action('AHEE__EE_System__load_espresso_addons') |
|
| 71 | - || did_action('AHEE__EE_System__register_shortcodes_modules_and_widgets') |
|
| 72 | - ) { |
|
| 73 | - EE_Error::doing_it_wrong( |
|
| 74 | - __METHOD__, |
|
| 75 | - esc_html__( |
|
| 76 | - 'An attempt to register shortcodes has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__register_shortcodes_modules_and_widgets" hook to register shortcodes.', |
|
| 77 | - 'event_espresso' |
|
| 78 | - ), |
|
| 79 | - '4.3.0' |
|
| 80 | - ); |
|
| 81 | - } |
|
| 82 | - //setup $_settings array from incoming values. |
|
| 83 | - self::$_settings[$shortcode_id] = array( |
|
| 84 | - // array of full server paths to any EES_Shortcodes used by the shortcode |
|
| 85 | - 'shortcode_paths' => isset($setup_args['shortcode_paths']) |
|
| 86 | - ? (array) $setup_args['shortcode_paths'] |
|
| 87 | - : array(), |
|
| 88 | - 'shortcode_fqcns' => isset($setup_args['shortcode_fqcns']) |
|
| 89 | - ? (array) $setup_args['shortcode_fqcns'] |
|
| 90 | - : array() |
|
| 91 | - ); |
|
| 92 | - // add to list of shortcodes to be registered |
|
| 93 | - add_filter( |
|
| 94 | - 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', |
|
| 95 | - array('EE_Register_Shortcode', 'add_shortcodes') |
|
| 96 | - ); |
|
| 69 | + //make sure this was called in the right place! |
|
| 70 | + if (! did_action('AHEE__EE_System__load_espresso_addons') |
|
| 71 | + || did_action('AHEE__EE_System__register_shortcodes_modules_and_widgets') |
|
| 72 | + ) { |
|
| 73 | + EE_Error::doing_it_wrong( |
|
| 74 | + __METHOD__, |
|
| 75 | + esc_html__( |
|
| 76 | + 'An attempt to register shortcodes has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__register_shortcodes_modules_and_widgets" hook to register shortcodes.', |
|
| 77 | + 'event_espresso' |
|
| 78 | + ), |
|
| 79 | + '4.3.0' |
|
| 80 | + ); |
|
| 81 | + } |
|
| 82 | + //setup $_settings array from incoming values. |
|
| 83 | + self::$_settings[$shortcode_id] = array( |
|
| 84 | + // array of full server paths to any EES_Shortcodes used by the shortcode |
|
| 85 | + 'shortcode_paths' => isset($setup_args['shortcode_paths']) |
|
| 86 | + ? (array) $setup_args['shortcode_paths'] |
|
| 87 | + : array(), |
|
| 88 | + 'shortcode_fqcns' => isset($setup_args['shortcode_fqcns']) |
|
| 89 | + ? (array) $setup_args['shortcode_fqcns'] |
|
| 90 | + : array() |
|
| 91 | + ); |
|
| 92 | + // add to list of shortcodes to be registered |
|
| 93 | + add_filter( |
|
| 94 | + 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', |
|
| 95 | + array('EE_Register_Shortcode', 'add_shortcodes') |
|
| 96 | + ); |
|
| 97 | 97 | |
| 98 | - add_filter( |
|
| 99 | - 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
|
| 100 | - array('EE_Register_Shortcode', 'instantiateAndAddToShortcodeCollection') |
|
| 101 | - ); |
|
| 102 | - } |
|
| 98 | + add_filter( |
|
| 99 | + 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
|
| 100 | + array('EE_Register_Shortcode', 'instantiateAndAddToShortcodeCollection') |
|
| 101 | + ); |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Filters the list of shortcodes to add ours. |
|
| 107 | - * and they're just full filepaths to FOLDERS containing a shortcode class file. Eg. |
|
| 108 | - * array('espresso_monkey'=>'/public_html/wonder-site/wp-content/plugins/ee4/shortcodes/espresso_monkey',...) |
|
| 109 | - * |
|
| 110 | - * @param array $shortcodes_to_register array of paths to all shortcodes that require registering |
|
| 111 | - * @return array |
|
| 112 | - */ |
|
| 113 | - public static function add_shortcodes($shortcodes_to_register) |
|
| 114 | - { |
|
| 115 | - foreach (self::$_settings as $settings) { |
|
| 116 | - $shortcodes_to_register = array_merge($shortcodes_to_register, $settings['shortcode_paths']); |
|
| 117 | - } |
|
| 118 | - return $shortcodes_to_register; |
|
| 119 | - } |
|
| 105 | + /** |
|
| 106 | + * Filters the list of shortcodes to add ours. |
|
| 107 | + * and they're just full filepaths to FOLDERS containing a shortcode class file. Eg. |
|
| 108 | + * array('espresso_monkey'=>'/public_html/wonder-site/wp-content/plugins/ee4/shortcodes/espresso_monkey',...) |
|
| 109 | + * |
|
| 110 | + * @param array $shortcodes_to_register array of paths to all shortcodes that require registering |
|
| 111 | + * @return array |
|
| 112 | + */ |
|
| 113 | + public static function add_shortcodes($shortcodes_to_register) |
|
| 114 | + { |
|
| 115 | + foreach (self::$_settings as $settings) { |
|
| 116 | + $shortcodes_to_register = array_merge($shortcodes_to_register, $settings['shortcode_paths']); |
|
| 117 | + } |
|
| 118 | + return $shortcodes_to_register; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Hooks into FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection |
|
| 124 | - * and registers any provided shortcode fully qualified class names. |
|
| 125 | - * @param CollectionInterface $shortcodes_collection |
|
| 126 | - * @return CollectionInterface |
|
| 127 | - * @throws InvalidArgumentException |
|
| 128 | - * @throws InvalidClassException |
|
| 129 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 130 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 131 | - */ |
|
| 132 | - public static function instantiateAndAddToShortcodeCollection(CollectionInterface $shortcodes_collection) |
|
| 133 | - { |
|
| 134 | - foreach (self::$_settings as $settings) { |
|
| 135 | - if (! empty($settings['shortcode_fqcns'])) { |
|
| 136 | - foreach ($settings['shortcode_fqcns'] as $shortcode_fqcn) { |
|
| 137 | - if (! class_exists($shortcode_fqcn)) { |
|
| 138 | - throw new InvalidClassException( |
|
| 139 | - sprintf( |
|
| 140 | - esc_html__( |
|
| 141 | - 'Are you sure %s is the right fully qualified class name for the shortcode class?', |
|
| 142 | - 'event_espresso' |
|
| 143 | - ), |
|
| 144 | - $shortcode_fqcn |
|
| 145 | - ) |
|
| 146 | - ); |
|
| 147 | - } |
|
| 148 | - if (! EE_Dependency_Map::instance()->has_dependency_for_class($shortcode_fqcn)) { |
|
| 149 | - //register dependencies |
|
| 150 | - EE_Dependency_Map::register_dependencies( |
|
| 151 | - $shortcode_fqcn, |
|
| 152 | - array( |
|
| 153 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 154 | - ) |
|
| 155 | - ); |
|
| 156 | - } |
|
| 157 | - $shortcodes_collection->add(LoaderFactory::getLoader()->getShared($shortcode_fqcn)); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - return $shortcodes_collection; |
|
| 162 | - } |
|
| 122 | + /** |
|
| 123 | + * Hooks into FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection |
|
| 124 | + * and registers any provided shortcode fully qualified class names. |
|
| 125 | + * @param CollectionInterface $shortcodes_collection |
|
| 126 | + * @return CollectionInterface |
|
| 127 | + * @throws InvalidArgumentException |
|
| 128 | + * @throws InvalidClassException |
|
| 129 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 130 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 131 | + */ |
|
| 132 | + public static function instantiateAndAddToShortcodeCollection(CollectionInterface $shortcodes_collection) |
|
| 133 | + { |
|
| 134 | + foreach (self::$_settings as $settings) { |
|
| 135 | + if (! empty($settings['shortcode_fqcns'])) { |
|
| 136 | + foreach ($settings['shortcode_fqcns'] as $shortcode_fqcn) { |
|
| 137 | + if (! class_exists($shortcode_fqcn)) { |
|
| 138 | + throw new InvalidClassException( |
|
| 139 | + sprintf( |
|
| 140 | + esc_html__( |
|
| 141 | + 'Are you sure %s is the right fully qualified class name for the shortcode class?', |
|
| 142 | + 'event_espresso' |
|
| 143 | + ), |
|
| 144 | + $shortcode_fqcn |
|
| 145 | + ) |
|
| 146 | + ); |
|
| 147 | + } |
|
| 148 | + if (! EE_Dependency_Map::instance()->has_dependency_for_class($shortcode_fqcn)) { |
|
| 149 | + //register dependencies |
|
| 150 | + EE_Dependency_Map::register_dependencies( |
|
| 151 | + $shortcode_fqcn, |
|
| 152 | + array( |
|
| 153 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 154 | + ) |
|
| 155 | + ); |
|
| 156 | + } |
|
| 157 | + $shortcodes_collection->add(LoaderFactory::getLoader()->getShared($shortcode_fqcn)); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + return $shortcodes_collection; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * This deregisters a shortcode that was previously registered with a specific $shortcode_id. |
|
| 167 | - * |
|
| 168 | - * @since 4.3.0 |
|
| 169 | - * @param string $shortcode_id the name for the shortcode that was previously registered |
|
| 170 | - * @return void |
|
| 171 | - */ |
|
| 172 | - public static function deregister($shortcode_id = null) |
|
| 173 | - { |
|
| 174 | - if (isset(self::$_settings[$shortcode_id])) { |
|
| 175 | - unset(self::$_settings[$shortcode_id]); |
|
| 176 | - } |
|
| 177 | - } |
|
| 165 | + /** |
|
| 166 | + * This deregisters a shortcode that was previously registered with a specific $shortcode_id. |
|
| 167 | + * |
|
| 168 | + * @since 4.3.0 |
|
| 169 | + * @param string $shortcode_id the name for the shortcode that was previously registered |
|
| 170 | + * @return void |
|
| 171 | + */ |
|
| 172 | + public static function deregister($shortcode_id = null) |
|
| 173 | + { |
|
| 174 | + if (isset(self::$_settings[$shortcode_id])) { |
|
| 175 | + unset(self::$_settings[$shortcode_id]); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | 178 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | //make sure this was called in the right place! |
| 70 | - if (! did_action('AHEE__EE_System__load_espresso_addons') |
|
| 70 | + if ( ! did_action('AHEE__EE_System__load_espresso_addons') |
|
| 71 | 71 | || did_action('AHEE__EE_System__register_shortcodes_modules_and_widgets') |
| 72 | 72 | ) { |
| 73 | 73 | EE_Error::doing_it_wrong( |
@@ -132,9 +132,9 @@ discard block |
||
| 132 | 132 | public static function instantiateAndAddToShortcodeCollection(CollectionInterface $shortcodes_collection) |
| 133 | 133 | { |
| 134 | 134 | foreach (self::$_settings as $settings) { |
| 135 | - if (! empty($settings['shortcode_fqcns'])) { |
|
| 135 | + if ( ! empty($settings['shortcode_fqcns'])) { |
|
| 136 | 136 | foreach ($settings['shortcode_fqcns'] as $shortcode_fqcn) { |
| 137 | - if (! class_exists($shortcode_fqcn)) { |
|
| 137 | + if ( ! class_exists($shortcode_fqcn)) { |
|
| 138 | 138 | throw new InvalidClassException( |
| 139 | 139 | sprintf( |
| 140 | 140 | esc_html__( |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | ) |
| 146 | 146 | ); |
| 147 | 147 | } |
| 148 | - if (! EE_Dependency_Map::instance()->has_dependency_for_class($shortcode_fqcn)) { |
|
| 148 | + if ( ! EE_Dependency_Map::instance()->has_dependency_for_class($shortcode_fqcn)) { |
|
| 149 | 149 | //register dependencies |
| 150 | 150 | EE_Dependency_Map::register_dependencies( |
| 151 | 151 | $shortcode_fqcn, |