@@ -20,78 +20,78 @@ |
||
20 | 20 | { |
21 | 21 | |
22 | 22 | |
23 | - /** |
|
24 | - * the actual shortcode tag that gets registered with WordPress |
|
25 | - * |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - public function getTag() |
|
29 | - { |
|
30 | - return 'ESPRESSO_CANCELLED'; |
|
31 | - } |
|
23 | + /** |
|
24 | + * the actual shortcode tag that gets registered with WordPress |
|
25 | + * |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + public function getTag() |
|
29 | + { |
|
30 | + return 'ESPRESSO_CANCELLED'; |
|
31 | + } |
|
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * the time in seconds to cache the results of the processShortcode() method |
|
36 | - * 0 means the processShortcode() results will NOT be cached at all |
|
37 | - * |
|
38 | - * @return int |
|
39 | - */ |
|
40 | - public function cacheExpiration() |
|
41 | - { |
|
42 | - return 0; |
|
43 | - } |
|
34 | + /** |
|
35 | + * the time in seconds to cache the results of the processShortcode() method |
|
36 | + * 0 means the processShortcode() results will NOT be cached at all |
|
37 | + * |
|
38 | + * @return int |
|
39 | + */ |
|
40 | + public function cacheExpiration() |
|
41 | + { |
|
42 | + return 0; |
|
43 | + } |
|
44 | 44 | |
45 | 45 | |
46 | - /** |
|
47 | - * a place for adding any initialization code that needs to run prior to wp_header(). |
|
48 | - * this may be required for shortcodes that utilize a corresponding module, |
|
49 | - * and need to enqueue assets for that module |
|
50 | - * |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function initializeShortcode() |
|
54 | - { |
|
55 | - $this->shortcodeHasBeenInitialized(); |
|
56 | - } |
|
46 | + /** |
|
47 | + * a place for adding any initialization code that needs to run prior to wp_header(). |
|
48 | + * this may be required for shortcodes that utilize a corresponding module, |
|
49 | + * and need to enqueue assets for that module |
|
50 | + * |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function initializeShortcode() |
|
54 | + { |
|
55 | + $this->shortcodeHasBeenInitialized(); |
|
56 | + } |
|
57 | 57 | |
58 | 58 | |
59 | - /** |
|
60 | - * callback that runs when the shortcode is encountered in post content. |
|
61 | - * IMPORTANT !!! |
|
62 | - * remember that shortcode content should be RETURNED and NOT echoed out |
|
63 | - * |
|
64 | - * @param array $attributes |
|
65 | - * @return string |
|
66 | - * @throws \EE_Error |
|
67 | - */ |
|
68 | - public function processShortcode($attributes = array()) |
|
69 | - { |
|
70 | - $transaction = EE_Registry::instance()->SSN->get_session_data('transaction'); |
|
71 | - if ($transaction instanceof EE_Transaction) { |
|
72 | - do_action('AHEE__EES_Espresso_Cancelled__process_shortcode__transaction', $transaction); |
|
73 | - $registrations = $transaction->registrations(); |
|
74 | - foreach ($registrations as $registration) { |
|
75 | - if ($registration instanceof EE_Registration) { |
|
76 | - do_action('AHEE__EES_Espresso_Cancelled__process_shortcode__registration', $registration); |
|
77 | - } |
|
78 | - } |
|
79 | - } |
|
80 | - do_action('AHEE__EES_Espresso_Cancelled__process_shortcode__clear_session'); |
|
81 | - // remove all unwanted records from the db |
|
82 | - if (EE_Registry::instance()->CART instanceof EE_Cart) { |
|
83 | - EE_Registry::instance()->CART->delete_cart(); |
|
84 | - } |
|
85 | - // phpcs:disable WordPress.WP.I18n.UnorderedPlaceholdersText |
|
86 | - EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
87 | - return sprintf( |
|
88 | - __( |
|
89 | - '%sAll unsaved registration information entered during this session has been deleted.%s', |
|
90 | - 'event_espresso' |
|
91 | - ), |
|
92 | - '<p class="ee-registrations-cancelled-pg ee-attention">', |
|
93 | - '</p>' |
|
94 | - ); |
|
95 | - // phpcs:enable |
|
96 | - } |
|
59 | + /** |
|
60 | + * callback that runs when the shortcode is encountered in post content. |
|
61 | + * IMPORTANT !!! |
|
62 | + * remember that shortcode content should be RETURNED and NOT echoed out |
|
63 | + * |
|
64 | + * @param array $attributes |
|
65 | + * @return string |
|
66 | + * @throws \EE_Error |
|
67 | + */ |
|
68 | + public function processShortcode($attributes = array()) |
|
69 | + { |
|
70 | + $transaction = EE_Registry::instance()->SSN->get_session_data('transaction'); |
|
71 | + if ($transaction instanceof EE_Transaction) { |
|
72 | + do_action('AHEE__EES_Espresso_Cancelled__process_shortcode__transaction', $transaction); |
|
73 | + $registrations = $transaction->registrations(); |
|
74 | + foreach ($registrations as $registration) { |
|
75 | + if ($registration instanceof EE_Registration) { |
|
76 | + do_action('AHEE__EES_Espresso_Cancelled__process_shortcode__registration', $registration); |
|
77 | + } |
|
78 | + } |
|
79 | + } |
|
80 | + do_action('AHEE__EES_Espresso_Cancelled__process_shortcode__clear_session'); |
|
81 | + // remove all unwanted records from the db |
|
82 | + if (EE_Registry::instance()->CART instanceof EE_Cart) { |
|
83 | + EE_Registry::instance()->CART->delete_cart(); |
|
84 | + } |
|
85 | + // phpcs:disable WordPress.WP.I18n.UnorderedPlaceholdersText |
|
86 | + EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
87 | + return sprintf( |
|
88 | + __( |
|
89 | + '%sAll unsaved registration information entered during this session has been deleted.%s', |
|
90 | + 'event_espresso' |
|
91 | + ), |
|
92 | + '<p class="ee-registrations-cancelled-pg ee-attention">', |
|
93 | + '</p>' |
|
94 | + ); |
|
95 | + // phpcs:enable |
|
96 | + } |
|
97 | 97 | } |
@@ -16,198 +16,198 @@ |
||
16 | 16 | class EventListQuery extends WP_Query |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @var string $title |
|
21 | - */ |
|
22 | - private $title; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var integer $limit |
|
26 | - */ |
|
27 | - private $limit = 10; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var string $css_class |
|
31 | - */ |
|
32 | - private $css_class; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var boolean $show_expired |
|
36 | - */ |
|
37 | - private $show_expired = false; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var string $month |
|
41 | - */ |
|
42 | - private $month; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var string $category_slug |
|
46 | - */ |
|
47 | - private $category_slug; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var string $order_by |
|
51 | - */ |
|
52 | - private $order_by; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var string $sort |
|
56 | - */ |
|
57 | - private $sort; |
|
58 | - |
|
59 | - /** |
|
60 | - * @var boolean $show_title |
|
61 | - */ |
|
62 | - private $show_title = true; |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * EE_Event_List_Query Constructor * |
|
67 | - * |
|
68 | - * @param array $args |
|
69 | - */ |
|
70 | - public function __construct($args = array()) |
|
71 | - { |
|
72 | - $args = $this->parseArgs((array) $args); |
|
73 | - $this->setupEventQueryHelper(); |
|
74 | - $this->setupFilters(); |
|
75 | - $args = $this->getQueryArgs($args); |
|
76 | - // run the query |
|
77 | - parent::__construct($args); |
|
78 | - } |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * @param array $args |
|
83 | - * @return array |
|
84 | - */ |
|
85 | - private function parseArgs(array $args) |
|
86 | - { |
|
87 | - // incoming args could be a mix of WP query args + EE shortcode args |
|
88 | - foreach ($args as $property => $value) { |
|
89 | - // if the arg is a property of this class, then it's an EE shortcode arg |
|
90 | - if (property_exists($this, $property) && ! property_exists('WP_Query', $property)) { |
|
91 | - // set the property value |
|
92 | - $this->{$property} = $value; |
|
93 | - // then remove it from the array of args that will later be passed to WP_Query() |
|
94 | - unset($args[ $property ]); |
|
95 | - } |
|
96 | - } |
|
97 | - return $args; |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - private function setupEventQueryHelper() |
|
102 | - { |
|
103 | - // add query filters |
|
104 | - EEH_Event_Query::add_query_filters(); |
|
105 | - // set params that will get used by the filters |
|
106 | - EEH_Event_Query::set_query_params( |
|
107 | - $this->month, |
|
108 | - $this->category_slug, |
|
109 | - $this->show_expired, |
|
110 | - $this->order_by, |
|
111 | - $this->sort |
|
112 | - ); |
|
113 | - } |
|
114 | - |
|
115 | - |
|
116 | - private function setupFilters() |
|
117 | - { |
|
118 | - // first off, let's remove any filters from previous queries |
|
119 | - remove_filter( |
|
120 | - 'FHEE__archive_espresso_events_template__show_header', |
|
121 | - array($this, 'show_event_list_title') |
|
122 | - ); |
|
123 | - remove_filter( |
|
124 | - 'FHEE__archive_espresso_events_template__upcoming_events_h1', |
|
125 | - array($this, 'event_list_title') |
|
126 | - ); |
|
127 | - remove_all_filters('FHEE__content_espresso_events__event_class'); |
|
128 | - // Event List Title ? |
|
129 | - add_filter( |
|
130 | - 'FHEE__archive_espresso_events_template__show_header', |
|
131 | - array($this, 'show_event_list_title') |
|
132 | - ); |
|
133 | - add_filter( |
|
134 | - 'FHEE__archive_espresso_events_template__upcoming_events_h1', |
|
135 | - array($this, 'event_list_title'), |
|
136 | - 10, |
|
137 | - 1 |
|
138 | - ); |
|
139 | - // add the css class |
|
140 | - add_filter( |
|
141 | - 'FHEE__content_espresso_events__event_class', |
|
142 | - array($this, 'event_list_css'), |
|
143 | - 10, |
|
144 | - 1 |
|
145 | - ); |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - private function getQueryArgs(array $args) |
|
150 | - { |
|
151 | - // the current "page" we are viewing |
|
152 | - $paged = max(1, get_query_var('paged')); |
|
153 | - // Force these args |
|
154 | - return array_merge( |
|
155 | - $args, |
|
156 | - array( |
|
157 | - 'post_type' => 'espresso_events', |
|
158 | - 'posts_per_page' => $this->limit, |
|
159 | - 'update_post_term_cache' => false, |
|
160 | - 'update_post_meta_cache' => false, |
|
161 | - 'paged' => $paged, |
|
162 | - 'offset' => ($paged - 1) * $this->limit, |
|
163 | - ) |
|
164 | - ); |
|
165 | - } |
|
166 | - |
|
167 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
168 | - |
|
169 | - /** |
|
170 | - * show_event_list_title |
|
171 | - * |
|
172 | - * @return boolean |
|
173 | - */ |
|
174 | - public function show_event_list_title() |
|
175 | - { |
|
176 | - return filter_var( |
|
177 | - $this->show_title, |
|
178 | - FILTER_VALIDATE_BOOLEAN |
|
179 | - ); |
|
180 | - } |
|
181 | - |
|
182 | - |
|
183 | - /** |
|
184 | - * callback for FHEE__archive_espresso_events_template__upcoming_events_h1 filter |
|
185 | - * |
|
186 | - * @param string $event_list_title |
|
187 | - * @return string |
|
188 | - */ |
|
189 | - public function event_list_title($event_list_title = '') |
|
190 | - { |
|
191 | - if (! empty($this->title)) { |
|
192 | - return $this->title; |
|
193 | - } |
|
194 | - return $event_list_title; |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * callback for FHEE__content_espresso_events__event_class filter |
|
200 | - * |
|
201 | - * @param string $event_list_css |
|
202 | - * @return string |
|
203 | - */ |
|
204 | - public function event_list_css($event_list_css = '') |
|
205 | - { |
|
206 | - $event_list_css .= ! empty($event_list_css) ? ' ' : ''; |
|
207 | - $event_list_css .= ! empty($this->css_class) ? $this->css_class : ''; |
|
208 | - $event_list_css .= ! empty($event_list_css) ? ' ' : ''; |
|
209 | - $event_list_css .= ! empty($this->category_slug) ? $this->category_slug : ''; |
|
210 | - return $event_list_css; |
|
211 | - } |
|
212 | - // phpcs:enable |
|
19 | + /** |
|
20 | + * @var string $title |
|
21 | + */ |
|
22 | + private $title; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var integer $limit |
|
26 | + */ |
|
27 | + private $limit = 10; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var string $css_class |
|
31 | + */ |
|
32 | + private $css_class; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var boolean $show_expired |
|
36 | + */ |
|
37 | + private $show_expired = false; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var string $month |
|
41 | + */ |
|
42 | + private $month; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var string $category_slug |
|
46 | + */ |
|
47 | + private $category_slug; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var string $order_by |
|
51 | + */ |
|
52 | + private $order_by; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var string $sort |
|
56 | + */ |
|
57 | + private $sort; |
|
58 | + |
|
59 | + /** |
|
60 | + * @var boolean $show_title |
|
61 | + */ |
|
62 | + private $show_title = true; |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * EE_Event_List_Query Constructor * |
|
67 | + * |
|
68 | + * @param array $args |
|
69 | + */ |
|
70 | + public function __construct($args = array()) |
|
71 | + { |
|
72 | + $args = $this->parseArgs((array) $args); |
|
73 | + $this->setupEventQueryHelper(); |
|
74 | + $this->setupFilters(); |
|
75 | + $args = $this->getQueryArgs($args); |
|
76 | + // run the query |
|
77 | + parent::__construct($args); |
|
78 | + } |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * @param array $args |
|
83 | + * @return array |
|
84 | + */ |
|
85 | + private function parseArgs(array $args) |
|
86 | + { |
|
87 | + // incoming args could be a mix of WP query args + EE shortcode args |
|
88 | + foreach ($args as $property => $value) { |
|
89 | + // if the arg is a property of this class, then it's an EE shortcode arg |
|
90 | + if (property_exists($this, $property) && ! property_exists('WP_Query', $property)) { |
|
91 | + // set the property value |
|
92 | + $this->{$property} = $value; |
|
93 | + // then remove it from the array of args that will later be passed to WP_Query() |
|
94 | + unset($args[ $property ]); |
|
95 | + } |
|
96 | + } |
|
97 | + return $args; |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + private function setupEventQueryHelper() |
|
102 | + { |
|
103 | + // add query filters |
|
104 | + EEH_Event_Query::add_query_filters(); |
|
105 | + // set params that will get used by the filters |
|
106 | + EEH_Event_Query::set_query_params( |
|
107 | + $this->month, |
|
108 | + $this->category_slug, |
|
109 | + $this->show_expired, |
|
110 | + $this->order_by, |
|
111 | + $this->sort |
|
112 | + ); |
|
113 | + } |
|
114 | + |
|
115 | + |
|
116 | + private function setupFilters() |
|
117 | + { |
|
118 | + // first off, let's remove any filters from previous queries |
|
119 | + remove_filter( |
|
120 | + 'FHEE__archive_espresso_events_template__show_header', |
|
121 | + array($this, 'show_event_list_title') |
|
122 | + ); |
|
123 | + remove_filter( |
|
124 | + 'FHEE__archive_espresso_events_template__upcoming_events_h1', |
|
125 | + array($this, 'event_list_title') |
|
126 | + ); |
|
127 | + remove_all_filters('FHEE__content_espresso_events__event_class'); |
|
128 | + // Event List Title ? |
|
129 | + add_filter( |
|
130 | + 'FHEE__archive_espresso_events_template__show_header', |
|
131 | + array($this, 'show_event_list_title') |
|
132 | + ); |
|
133 | + add_filter( |
|
134 | + 'FHEE__archive_espresso_events_template__upcoming_events_h1', |
|
135 | + array($this, 'event_list_title'), |
|
136 | + 10, |
|
137 | + 1 |
|
138 | + ); |
|
139 | + // add the css class |
|
140 | + add_filter( |
|
141 | + 'FHEE__content_espresso_events__event_class', |
|
142 | + array($this, 'event_list_css'), |
|
143 | + 10, |
|
144 | + 1 |
|
145 | + ); |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + private function getQueryArgs(array $args) |
|
150 | + { |
|
151 | + // the current "page" we are viewing |
|
152 | + $paged = max(1, get_query_var('paged')); |
|
153 | + // Force these args |
|
154 | + return array_merge( |
|
155 | + $args, |
|
156 | + array( |
|
157 | + 'post_type' => 'espresso_events', |
|
158 | + 'posts_per_page' => $this->limit, |
|
159 | + 'update_post_term_cache' => false, |
|
160 | + 'update_post_meta_cache' => false, |
|
161 | + 'paged' => $paged, |
|
162 | + 'offset' => ($paged - 1) * $this->limit, |
|
163 | + ) |
|
164 | + ); |
|
165 | + } |
|
166 | + |
|
167 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
168 | + |
|
169 | + /** |
|
170 | + * show_event_list_title |
|
171 | + * |
|
172 | + * @return boolean |
|
173 | + */ |
|
174 | + public function show_event_list_title() |
|
175 | + { |
|
176 | + return filter_var( |
|
177 | + $this->show_title, |
|
178 | + FILTER_VALIDATE_BOOLEAN |
|
179 | + ); |
|
180 | + } |
|
181 | + |
|
182 | + |
|
183 | + /** |
|
184 | + * callback for FHEE__archive_espresso_events_template__upcoming_events_h1 filter |
|
185 | + * |
|
186 | + * @param string $event_list_title |
|
187 | + * @return string |
|
188 | + */ |
|
189 | + public function event_list_title($event_list_title = '') |
|
190 | + { |
|
191 | + if (! empty($this->title)) { |
|
192 | + return $this->title; |
|
193 | + } |
|
194 | + return $event_list_title; |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * callback for FHEE__content_espresso_events__event_class filter |
|
200 | + * |
|
201 | + * @param string $event_list_css |
|
202 | + * @return string |
|
203 | + */ |
|
204 | + public function event_list_css($event_list_css = '') |
|
205 | + { |
|
206 | + $event_list_css .= ! empty($event_list_css) ? ' ' : ''; |
|
207 | + $event_list_css .= ! empty($this->css_class) ? $this->css_class : ''; |
|
208 | + $event_list_css .= ! empty($event_list_css) ? ' ' : ''; |
|
209 | + $event_list_css .= ! empty($this->category_slug) ? $this->category_slug : ''; |
|
210 | + return $event_list_css; |
|
211 | + } |
|
212 | + // phpcs:enable |
|
213 | 213 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | // set the property value |
92 | 92 | $this->{$property} = $value; |
93 | 93 | // then remove it from the array of args that will later be passed to WP_Query() |
94 | - unset($args[ $property ]); |
|
94 | + unset($args[$property]); |
|
95 | 95 | } |
96 | 96 | } |
97 | 97 | return $args; |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | */ |
189 | 189 | public function event_list_title($event_list_title = '') |
190 | 190 | { |
191 | - if (! empty($this->title)) { |
|
191 | + if ( ! empty($this->title)) { |
|
192 | 192 | return $this->title; |
193 | 193 | } |
194 | 194 | return $event_list_title; |
@@ -16,180 +16,180 @@ |
||
16 | 16 | class RegisterCustomTaxonomyTerms |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @var array[] $custom_taxonomy_terms |
|
21 | - */ |
|
22 | - public $custom_taxonomy_terms = array(); |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * RegisterCustomTaxonomyTerms constructor. |
|
27 | - */ |
|
28 | - public function __construct() |
|
29 | - { |
|
30 | - // hook into save_post so that we can make sure that the default terms get saved on publish of registered cpts |
|
31 | - // IF they don't have a term for that taxonomy set. |
|
32 | - add_action('save_post', array($this, 'saveDefaultTerm'), 100, 2); |
|
33 | - do_action( |
|
34 | - 'AHEE__EventEspresso_core_domain_services_custom_post_types_RegisterCustomTaxonomyTerms__construct_end', |
|
35 | - $this |
|
36 | - ); |
|
37 | - } |
|
38 | - |
|
39 | - |
|
40 | - public function registerCustomTaxonomyTerms() |
|
41 | - { |
|
42 | - // setup default terms in any of our taxonomies (but only if we're in admin). |
|
43 | - // Why not added via register_activation_hook? |
|
44 | - // Because it's possible that in future iterations of EE we may add new defaults for specialized taxonomies |
|
45 | - // (think event_types) and register_activation_hook only reliably runs when a user manually activates the plugin. |
|
46 | - // Keep in mind that this will READ these terms if they are deleted by the user. Hence MUST use terms. |
|
47 | - // if ( is_admin() ) { |
|
48 | - // $this->set_must_use_event_types(); |
|
49 | - // } |
|
50 | - // set default terms |
|
51 | - $this->registerCustomTaxonomyTerm( |
|
52 | - 'espresso_event_type', |
|
53 | - 'single-event', |
|
54 | - array('espresso_events') |
|
55 | - ); |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * Allows us to set what the default will be for terms when a cpt is PUBLISHED. |
|
61 | - * |
|
62 | - * @param string $taxonomy The taxonomy we're using for the default term |
|
63 | - * @param string $term_slug The slug of the term that will be the default. |
|
64 | - * @param array $cpt_slugs An array of custom post types we want the default assigned to |
|
65 | - */ |
|
66 | - public function registerCustomTaxonomyTerm($taxonomy, $term_slug, array $cpt_slugs = array()) |
|
67 | - { |
|
68 | - $this->custom_taxonomy_terms[][ $term_slug ] = new CustomTaxonomyTerm( |
|
69 | - $taxonomy, |
|
70 | - $term_slug, |
|
71 | - $cpt_slugs |
|
72 | - ); |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * hooked into the wp 'save_post' action hook for setting our default terms found in the $_default_terms property |
|
78 | - * |
|
79 | - * @param int $post_id ID of CPT being saved |
|
80 | - * @param WP_Post $post Post object |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function saveDefaultTerm($post_id, WP_Post $post) |
|
84 | - { |
|
85 | - if (empty($this->custom_taxonomy_terms)) { |
|
86 | - return; |
|
87 | - } |
|
88 | - // no default terms set so lets just exit. |
|
89 | - foreach ($this->custom_taxonomy_terms as $custom_taxonomy_terms) { |
|
90 | - foreach ($custom_taxonomy_terms as $custom_taxonomy_term) { |
|
91 | - if ($post->post_status === 'publish' |
|
92 | - && $custom_taxonomy_term instanceof CustomTaxonomyTerm |
|
93 | - && in_array($post->post_type, $custom_taxonomy_term->customPostTypeSlugs(), true) |
|
94 | - ) { |
|
95 | - // note some error proofing going on here to save unnecessary db queries |
|
96 | - $taxonomies = get_object_taxonomies($post->post_type); |
|
97 | - foreach ($taxonomies as $taxonomy) { |
|
98 | - $terms = wp_get_post_terms($post_id, $taxonomy); |
|
99 | - if (empty($terms) && $taxonomy === $custom_taxonomy_term->taxonomySlug()) { |
|
100 | - wp_set_object_terms( |
|
101 | - $post_id, |
|
102 | - array($custom_taxonomy_term->termSlug()), |
|
103 | - $taxonomy |
|
104 | - ); |
|
105 | - } |
|
106 | - } |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - public function setMustUseEventTypes() |
|
117 | - { |
|
118 | - $term_details = array( |
|
119 | - // Attendee's register for the first date-time only |
|
120 | - 'single-event' => array( |
|
121 | - 'term' => esc_html__('Single Event', 'event_espresso'), |
|
122 | - 'desc' => esc_html__( |
|
123 | - 'A single event that spans one or more consecutive days.', |
|
124 | - 'event_espresso' |
|
125 | - ), |
|
126 | - ), |
|
127 | - // example: a party or two-day long workshop |
|
128 | - // Attendee's can register for any of the date-times |
|
129 | - 'multi-event' => array( |
|
130 | - 'term' => esc_html__('Multi Event', 'event_espresso'), |
|
131 | - 'desc' => esc_html__( |
|
132 | - 'Multiple, separate, but related events that occur on consecutive days.', |
|
133 | - 'event_espresso' |
|
134 | - ), |
|
135 | - ), |
|
136 | - // example: a three day music festival or week long conference |
|
137 | - // Attendee's register for the first date-time only |
|
138 | - 'event-series' => array( |
|
139 | - 'term' => esc_html__('Event Series', 'event_espresso'), |
|
140 | - 'desc' => esc_html__( |
|
141 | - ' Multiple events that occur over multiple non-consecutive days.', |
|
142 | - 'event_espresso' |
|
143 | - ), |
|
144 | - ), |
|
145 | - // example: an 8 week introduction to basket weaving course |
|
146 | - // Attendee's can register for any of the date-times. |
|
147 | - 'recurring-event' => array( |
|
148 | - 'term' => esc_html__('Recurring Event', 'event_espresso'), |
|
149 | - 'desc' => esc_html__( |
|
150 | - 'Multiple events that occur over multiple non-consecutive days.', |
|
151 | - 'event_espresso' |
|
152 | - ), |
|
153 | - ), |
|
154 | - // example: a yoga class |
|
155 | - 'ongoing' => array( |
|
156 | - 'term' => esc_html__('Ongoing Event', 'event_espresso'), |
|
157 | - 'desc' => esc_html__( |
|
158 | - 'An "event" that people can purchase tickets to gain access for anytime for this event regardless of date times on the event', |
|
159 | - 'event_espresso' |
|
160 | - ), |
|
161 | - ) |
|
162 | - // example: access to a museum |
|
163 | - // 'walk-in' => array( esc_html__('Walk In', 'event_espresso'), esc_html__('Single datetime and single entry recurring events. Attendees register for one or multiple datetimes individually.', 'event_espresso') ), |
|
164 | - // 'reservation' => array( esc_html__('Reservation', 'event_espresso'), esc_html__('Reservations are created by specifying available datetimes and quantities. Attendees choose from the available datetimes and specify the quantity available (if the maximum is greater than 1)') ), //@TODO to avoid confusion we'll implement this in a later iteration > EE4.1 |
|
165 | - // 'multiple-session' => array( esc_html__('Multiple Session', 'event_espresso'), esc_html__('Multiple event, multiple datetime, hierarchically organized, custom entry events. Attendees may be required to register for a parent event before being allowed to register for child events. Attendees can register for any combination of child events as long as the datetimes do not conflict. Parent and child events may have additional fees or registration questions.') ), //@TODO to avoid confusion we'll implement this in a later iteration > EE4.1 |
|
166 | - // 'appointment' => array( esc_html__('Appointments', 'event_espresso'), esc_html__('Time slotted events where datetimes are generally in hours or minutes. For example, attendees can register for a single 15 minute or 1 hour time slot and this type of availability frequently reoccurs.', 'event_espresso') ) |
|
167 | - ); |
|
168 | - $this->setMustUseTerms('espresso_event_type', $term_details); |
|
169 | - } |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * wrapper method for handling the setting up of initial terms in the db (if they don't already exist). |
|
174 | - * Note this should ONLY be used for terms that always must be present. Be aware that if an initial term is |
|
175 | - * deleted then it WILL be recreated. |
|
176 | - * |
|
177 | - * @param string $taxonomy The name of the taxonomy |
|
178 | - * @param array $term_details An array of term details indexed by slug and containing Name of term, and |
|
179 | - * description as the elements in the array |
|
180 | - * @return void |
|
181 | - */ |
|
182 | - public function setMustUseTerms($taxonomy, $term_details) |
|
183 | - { |
|
184 | - $term_details = (array) $term_details; |
|
185 | - foreach ($term_details as $slug => $details) { |
|
186 | - if (isset($details['term'], $details['desc']) && ! term_exists($slug, $taxonomy)) { |
|
187 | - $insert_arr = array( |
|
188 | - 'slug' => $slug, |
|
189 | - 'description' => $details['desc'], |
|
190 | - ); |
|
191 | - wp_insert_term($details['term'], $taxonomy, $insert_arr); |
|
192 | - } |
|
193 | - } |
|
194 | - } |
|
19 | + /** |
|
20 | + * @var array[] $custom_taxonomy_terms |
|
21 | + */ |
|
22 | + public $custom_taxonomy_terms = array(); |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * RegisterCustomTaxonomyTerms constructor. |
|
27 | + */ |
|
28 | + public function __construct() |
|
29 | + { |
|
30 | + // hook into save_post so that we can make sure that the default terms get saved on publish of registered cpts |
|
31 | + // IF they don't have a term for that taxonomy set. |
|
32 | + add_action('save_post', array($this, 'saveDefaultTerm'), 100, 2); |
|
33 | + do_action( |
|
34 | + 'AHEE__EventEspresso_core_domain_services_custom_post_types_RegisterCustomTaxonomyTerms__construct_end', |
|
35 | + $this |
|
36 | + ); |
|
37 | + } |
|
38 | + |
|
39 | + |
|
40 | + public function registerCustomTaxonomyTerms() |
|
41 | + { |
|
42 | + // setup default terms in any of our taxonomies (but only if we're in admin). |
|
43 | + // Why not added via register_activation_hook? |
|
44 | + // Because it's possible that in future iterations of EE we may add new defaults for specialized taxonomies |
|
45 | + // (think event_types) and register_activation_hook only reliably runs when a user manually activates the plugin. |
|
46 | + // Keep in mind that this will READ these terms if they are deleted by the user. Hence MUST use terms. |
|
47 | + // if ( is_admin() ) { |
|
48 | + // $this->set_must_use_event_types(); |
|
49 | + // } |
|
50 | + // set default terms |
|
51 | + $this->registerCustomTaxonomyTerm( |
|
52 | + 'espresso_event_type', |
|
53 | + 'single-event', |
|
54 | + array('espresso_events') |
|
55 | + ); |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * Allows us to set what the default will be for terms when a cpt is PUBLISHED. |
|
61 | + * |
|
62 | + * @param string $taxonomy The taxonomy we're using for the default term |
|
63 | + * @param string $term_slug The slug of the term that will be the default. |
|
64 | + * @param array $cpt_slugs An array of custom post types we want the default assigned to |
|
65 | + */ |
|
66 | + public function registerCustomTaxonomyTerm($taxonomy, $term_slug, array $cpt_slugs = array()) |
|
67 | + { |
|
68 | + $this->custom_taxonomy_terms[][ $term_slug ] = new CustomTaxonomyTerm( |
|
69 | + $taxonomy, |
|
70 | + $term_slug, |
|
71 | + $cpt_slugs |
|
72 | + ); |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * hooked into the wp 'save_post' action hook for setting our default terms found in the $_default_terms property |
|
78 | + * |
|
79 | + * @param int $post_id ID of CPT being saved |
|
80 | + * @param WP_Post $post Post object |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function saveDefaultTerm($post_id, WP_Post $post) |
|
84 | + { |
|
85 | + if (empty($this->custom_taxonomy_terms)) { |
|
86 | + return; |
|
87 | + } |
|
88 | + // no default terms set so lets just exit. |
|
89 | + foreach ($this->custom_taxonomy_terms as $custom_taxonomy_terms) { |
|
90 | + foreach ($custom_taxonomy_terms as $custom_taxonomy_term) { |
|
91 | + if ($post->post_status === 'publish' |
|
92 | + && $custom_taxonomy_term instanceof CustomTaxonomyTerm |
|
93 | + && in_array($post->post_type, $custom_taxonomy_term->customPostTypeSlugs(), true) |
|
94 | + ) { |
|
95 | + // note some error proofing going on here to save unnecessary db queries |
|
96 | + $taxonomies = get_object_taxonomies($post->post_type); |
|
97 | + foreach ($taxonomies as $taxonomy) { |
|
98 | + $terms = wp_get_post_terms($post_id, $taxonomy); |
|
99 | + if (empty($terms) && $taxonomy === $custom_taxonomy_term->taxonomySlug()) { |
|
100 | + wp_set_object_terms( |
|
101 | + $post_id, |
|
102 | + array($custom_taxonomy_term->termSlug()), |
|
103 | + $taxonomy |
|
104 | + ); |
|
105 | + } |
|
106 | + } |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + public function setMustUseEventTypes() |
|
117 | + { |
|
118 | + $term_details = array( |
|
119 | + // Attendee's register for the first date-time only |
|
120 | + 'single-event' => array( |
|
121 | + 'term' => esc_html__('Single Event', 'event_espresso'), |
|
122 | + 'desc' => esc_html__( |
|
123 | + 'A single event that spans one or more consecutive days.', |
|
124 | + 'event_espresso' |
|
125 | + ), |
|
126 | + ), |
|
127 | + // example: a party or two-day long workshop |
|
128 | + // Attendee's can register for any of the date-times |
|
129 | + 'multi-event' => array( |
|
130 | + 'term' => esc_html__('Multi Event', 'event_espresso'), |
|
131 | + 'desc' => esc_html__( |
|
132 | + 'Multiple, separate, but related events that occur on consecutive days.', |
|
133 | + 'event_espresso' |
|
134 | + ), |
|
135 | + ), |
|
136 | + // example: a three day music festival or week long conference |
|
137 | + // Attendee's register for the first date-time only |
|
138 | + 'event-series' => array( |
|
139 | + 'term' => esc_html__('Event Series', 'event_espresso'), |
|
140 | + 'desc' => esc_html__( |
|
141 | + ' Multiple events that occur over multiple non-consecutive days.', |
|
142 | + 'event_espresso' |
|
143 | + ), |
|
144 | + ), |
|
145 | + // example: an 8 week introduction to basket weaving course |
|
146 | + // Attendee's can register for any of the date-times. |
|
147 | + 'recurring-event' => array( |
|
148 | + 'term' => esc_html__('Recurring Event', 'event_espresso'), |
|
149 | + 'desc' => esc_html__( |
|
150 | + 'Multiple events that occur over multiple non-consecutive days.', |
|
151 | + 'event_espresso' |
|
152 | + ), |
|
153 | + ), |
|
154 | + // example: a yoga class |
|
155 | + 'ongoing' => array( |
|
156 | + 'term' => esc_html__('Ongoing Event', 'event_espresso'), |
|
157 | + 'desc' => esc_html__( |
|
158 | + 'An "event" that people can purchase tickets to gain access for anytime for this event regardless of date times on the event', |
|
159 | + 'event_espresso' |
|
160 | + ), |
|
161 | + ) |
|
162 | + // example: access to a museum |
|
163 | + // 'walk-in' => array( esc_html__('Walk In', 'event_espresso'), esc_html__('Single datetime and single entry recurring events. Attendees register for one or multiple datetimes individually.', 'event_espresso') ), |
|
164 | + // 'reservation' => array( esc_html__('Reservation', 'event_espresso'), esc_html__('Reservations are created by specifying available datetimes and quantities. Attendees choose from the available datetimes and specify the quantity available (if the maximum is greater than 1)') ), //@TODO to avoid confusion we'll implement this in a later iteration > EE4.1 |
|
165 | + // 'multiple-session' => array( esc_html__('Multiple Session', 'event_espresso'), esc_html__('Multiple event, multiple datetime, hierarchically organized, custom entry events. Attendees may be required to register for a parent event before being allowed to register for child events. Attendees can register for any combination of child events as long as the datetimes do not conflict. Parent and child events may have additional fees or registration questions.') ), //@TODO to avoid confusion we'll implement this in a later iteration > EE4.1 |
|
166 | + // 'appointment' => array( esc_html__('Appointments', 'event_espresso'), esc_html__('Time slotted events where datetimes are generally in hours or minutes. For example, attendees can register for a single 15 minute or 1 hour time slot and this type of availability frequently reoccurs.', 'event_espresso') ) |
|
167 | + ); |
|
168 | + $this->setMustUseTerms('espresso_event_type', $term_details); |
|
169 | + } |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * wrapper method for handling the setting up of initial terms in the db (if they don't already exist). |
|
174 | + * Note this should ONLY be used for terms that always must be present. Be aware that if an initial term is |
|
175 | + * deleted then it WILL be recreated. |
|
176 | + * |
|
177 | + * @param string $taxonomy The name of the taxonomy |
|
178 | + * @param array $term_details An array of term details indexed by slug and containing Name of term, and |
|
179 | + * description as the elements in the array |
|
180 | + * @return void |
|
181 | + */ |
|
182 | + public function setMustUseTerms($taxonomy, $term_details) |
|
183 | + { |
|
184 | + $term_details = (array) $term_details; |
|
185 | + foreach ($term_details as $slug => $details) { |
|
186 | + if (isset($details['term'], $details['desc']) && ! term_exists($slug, $taxonomy)) { |
|
187 | + $insert_arr = array( |
|
188 | + 'slug' => $slug, |
|
189 | + 'description' => $details['desc'], |
|
190 | + ); |
|
191 | + wp_insert_term($details['term'], $taxonomy, $insert_arr); |
|
192 | + } |
|
193 | + } |
|
194 | + } |
|
195 | 195 | } |
@@ -18,240 +18,240 @@ |
||
18 | 18 | class RegisterCustomPostTypes |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @var CustomPostTypeDefinitions $custom_post_types |
|
23 | - */ |
|
24 | - public $custom_post_types; |
|
21 | + /** |
|
22 | + * @var CustomPostTypeDefinitions $custom_post_types |
|
23 | + */ |
|
24 | + public $custom_post_types; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var WP_Post_Type[] $wp_post_types |
|
28 | - */ |
|
29 | - public $wp_post_types = array(); |
|
26 | + /** |
|
27 | + * @var WP_Post_Type[] $wp_post_types |
|
28 | + */ |
|
29 | + public $wp_post_types = array(); |
|
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * RegisterCustomPostTypes constructor. |
|
34 | - * |
|
35 | - * @param CustomPostTypeDefinitions $custom_post_types |
|
36 | - */ |
|
37 | - public function __construct(CustomPostTypeDefinitions $custom_post_types) |
|
38 | - { |
|
39 | - $this->custom_post_types = $custom_post_types; |
|
40 | - } |
|
32 | + /** |
|
33 | + * RegisterCustomPostTypes constructor. |
|
34 | + * |
|
35 | + * @param CustomPostTypeDefinitions $custom_post_types |
|
36 | + */ |
|
37 | + public function __construct(CustomPostTypeDefinitions $custom_post_types) |
|
38 | + { |
|
39 | + $this->custom_post_types = $custom_post_types; |
|
40 | + } |
|
41 | 41 | |
42 | 42 | |
43 | - /** |
|
44 | - * @return WP_Post_Type[] |
|
45 | - */ |
|
46 | - public function getRegisteredCustomPostTypes() |
|
47 | - { |
|
48 | - return $this->wp_post_types; |
|
49 | - } |
|
43 | + /** |
|
44 | + * @return WP_Post_Type[] |
|
45 | + */ |
|
46 | + public function getRegisteredCustomPostTypes() |
|
47 | + { |
|
48 | + return $this->wp_post_types; |
|
49 | + } |
|
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * @return void |
|
54 | - * @throws DomainException |
|
55 | - */ |
|
56 | - public function registerCustomPostTypes() |
|
57 | - { |
|
58 | - $custom_post_types = $this->custom_post_types->getDefinitions(); |
|
59 | - foreach ($custom_post_types as $custom_post_type => $CPT) { |
|
60 | - $this->wp_post_types[ $custom_post_type ] = $this->registerCustomPostType( |
|
61 | - $custom_post_type, |
|
62 | - $CPT['singular_name'], |
|
63 | - $CPT['plural_name'], |
|
64 | - $CPT['singular_slug'], |
|
65 | - $CPT['plural_slug'], |
|
66 | - $CPT['args'] |
|
67 | - ); |
|
68 | - } |
|
69 | - } |
|
52 | + /** |
|
53 | + * @return void |
|
54 | + * @throws DomainException |
|
55 | + */ |
|
56 | + public function registerCustomPostTypes() |
|
57 | + { |
|
58 | + $custom_post_types = $this->custom_post_types->getDefinitions(); |
|
59 | + foreach ($custom_post_types as $custom_post_type => $CPT) { |
|
60 | + $this->wp_post_types[ $custom_post_type ] = $this->registerCustomPostType( |
|
61 | + $custom_post_type, |
|
62 | + $CPT['singular_name'], |
|
63 | + $CPT['plural_name'], |
|
64 | + $CPT['singular_slug'], |
|
65 | + $CPT['plural_slug'], |
|
66 | + $CPT['args'] |
|
67 | + ); |
|
68 | + } |
|
69 | + } |
|
70 | 70 | |
71 | 71 | |
72 | - /** |
|
73 | - * Registers a new custom post type. Sets default settings given only the following params. |
|
74 | - * Returns the registered post type object, or an error object. |
|
75 | - * |
|
76 | - * @param string $post_type the actual post type name |
|
77 | - * IMPORTANT: |
|
78 | - * this must match what the slug is for admin pages related to this CPT |
|
79 | - * Also any models must use this slug as well |
|
80 | - * @param string $singular_name a pre-internationalized string for the singular name of the objects |
|
81 | - * @param string $plural_name a pre-internationalized string for the plural name of the objects |
|
82 | - * @param string $singular_slug |
|
83 | - * @param string $plural_slug |
|
84 | - * @param array $override_arguments exactly like $args as described in |
|
85 | - * http://codex.wordpress.org/Function_Reference/register_post_type |
|
86 | - * @return WP_Post_Type|WP_Error |
|
87 | - * @throws DomainException |
|
88 | - */ |
|
89 | - public function registerCustomPostType( |
|
90 | - $post_type, |
|
91 | - $singular_name, |
|
92 | - $plural_name, |
|
93 | - $singular_slug = '', |
|
94 | - $plural_slug = '', |
|
95 | - array $override_arguments = array() |
|
96 | - ) { |
|
97 | - $wp_post_type = register_post_type( |
|
98 | - $post_type, |
|
99 | - $this->prepareArguments( |
|
100 | - $post_type, |
|
101 | - $singular_name, |
|
102 | - $plural_name, |
|
103 | - $singular_slug, |
|
104 | - $plural_slug, |
|
105 | - $override_arguments |
|
106 | - ) |
|
107 | - ); |
|
108 | - if ($wp_post_type instanceof WP_Error) { |
|
109 | - throw new DomainException($wp_post_type->get_error_message()); |
|
110 | - } |
|
111 | - return $wp_post_type; |
|
112 | - } |
|
72 | + /** |
|
73 | + * Registers a new custom post type. Sets default settings given only the following params. |
|
74 | + * Returns the registered post type object, or an error object. |
|
75 | + * |
|
76 | + * @param string $post_type the actual post type name |
|
77 | + * IMPORTANT: |
|
78 | + * this must match what the slug is for admin pages related to this CPT |
|
79 | + * Also any models must use this slug as well |
|
80 | + * @param string $singular_name a pre-internationalized string for the singular name of the objects |
|
81 | + * @param string $plural_name a pre-internationalized string for the plural name of the objects |
|
82 | + * @param string $singular_slug |
|
83 | + * @param string $plural_slug |
|
84 | + * @param array $override_arguments exactly like $args as described in |
|
85 | + * http://codex.wordpress.org/Function_Reference/register_post_type |
|
86 | + * @return WP_Post_Type|WP_Error |
|
87 | + * @throws DomainException |
|
88 | + */ |
|
89 | + public function registerCustomPostType( |
|
90 | + $post_type, |
|
91 | + $singular_name, |
|
92 | + $plural_name, |
|
93 | + $singular_slug = '', |
|
94 | + $plural_slug = '', |
|
95 | + array $override_arguments = array() |
|
96 | + ) { |
|
97 | + $wp_post_type = register_post_type( |
|
98 | + $post_type, |
|
99 | + $this->prepareArguments( |
|
100 | + $post_type, |
|
101 | + $singular_name, |
|
102 | + $plural_name, |
|
103 | + $singular_slug, |
|
104 | + $plural_slug, |
|
105 | + $override_arguments |
|
106 | + ) |
|
107 | + ); |
|
108 | + if ($wp_post_type instanceof WP_Error) { |
|
109 | + throw new DomainException($wp_post_type->get_error_message()); |
|
110 | + } |
|
111 | + return $wp_post_type; |
|
112 | + } |
|
113 | 113 | |
114 | 114 | |
115 | - /** |
|
116 | - * @param string $post_type the actual post type name |
|
117 | - * @param string $singular_name a pre-internationalized string for the singular name of the objects |
|
118 | - * @param string $plural_name a pre-internationalized string for the plural name of the objects |
|
119 | - * @param string $singular_slug |
|
120 | - * @param string $plural_slug |
|
121 | - * @param array $override_arguments The default values set in this function will be overridden |
|
122 | - * by whatever you set in $override_arguments |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - protected function prepareArguments( |
|
126 | - $post_type, |
|
127 | - $singular_name, |
|
128 | - $plural_name, |
|
129 | - $singular_slug, |
|
130 | - $plural_slug, |
|
131 | - array $override_arguments = array() |
|
132 | - ) { |
|
133 | - // verify plural slug and singular slug, if they aren't we'll use $singular_name and $plural_name |
|
134 | - $singular_slug = ! empty($singular_slug) ? $singular_slug : $singular_name; |
|
135 | - $plural_slug = ! empty($plural_slug) ? $plural_slug : $plural_name; |
|
136 | - $labels = $this->getLabels( |
|
137 | - $singular_name, |
|
138 | - $plural_name, |
|
139 | - $singular_slug, |
|
140 | - $plural_slug |
|
141 | - ); |
|
142 | - // note the page_templates arg in the supports index is something specific to EE. |
|
143 | - // WordPress doesn't actually have that in their register_post_type api. |
|
144 | - $arguments = $this->getDefaultArguments($labels, $post_type, $plural_slug); |
|
145 | - if ($override_arguments) { |
|
146 | - if (isset($override_arguments['labels'])) { |
|
147 | - $labels = array_merge($arguments['labels'], $override_arguments['labels']); |
|
148 | - } |
|
149 | - $arguments = array_merge($arguments, $override_arguments); |
|
150 | - $arguments['labels'] = $labels; |
|
151 | - } |
|
152 | - return $arguments; |
|
153 | - } |
|
115 | + /** |
|
116 | + * @param string $post_type the actual post type name |
|
117 | + * @param string $singular_name a pre-internationalized string for the singular name of the objects |
|
118 | + * @param string $plural_name a pre-internationalized string for the plural name of the objects |
|
119 | + * @param string $singular_slug |
|
120 | + * @param string $plural_slug |
|
121 | + * @param array $override_arguments The default values set in this function will be overridden |
|
122 | + * by whatever you set in $override_arguments |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + protected function prepareArguments( |
|
126 | + $post_type, |
|
127 | + $singular_name, |
|
128 | + $plural_name, |
|
129 | + $singular_slug, |
|
130 | + $plural_slug, |
|
131 | + array $override_arguments = array() |
|
132 | + ) { |
|
133 | + // verify plural slug and singular slug, if they aren't we'll use $singular_name and $plural_name |
|
134 | + $singular_slug = ! empty($singular_slug) ? $singular_slug : $singular_name; |
|
135 | + $plural_slug = ! empty($plural_slug) ? $plural_slug : $plural_name; |
|
136 | + $labels = $this->getLabels( |
|
137 | + $singular_name, |
|
138 | + $plural_name, |
|
139 | + $singular_slug, |
|
140 | + $plural_slug |
|
141 | + ); |
|
142 | + // note the page_templates arg in the supports index is something specific to EE. |
|
143 | + // WordPress doesn't actually have that in their register_post_type api. |
|
144 | + $arguments = $this->getDefaultArguments($labels, $post_type, $plural_slug); |
|
145 | + if ($override_arguments) { |
|
146 | + if (isset($override_arguments['labels'])) { |
|
147 | + $labels = array_merge($arguments['labels'], $override_arguments['labels']); |
|
148 | + } |
|
149 | + $arguments = array_merge($arguments, $override_arguments); |
|
150 | + $arguments['labels'] = $labels; |
|
151 | + } |
|
152 | + return $arguments; |
|
153 | + } |
|
154 | 154 | |
155 | 155 | |
156 | - /** |
|
157 | - * @param string $singular_name |
|
158 | - * @param string $plural_name |
|
159 | - * @param string $singular_slug |
|
160 | - * @param string $plural_slug |
|
161 | - * @return array |
|
162 | - */ |
|
163 | - private function getLabels($singular_name, $plural_name, $singular_slug, $plural_slug) |
|
164 | - { |
|
165 | - return array( |
|
166 | - 'name' => $plural_name, |
|
167 | - 'singular_name' => $singular_name, |
|
168 | - 'singular_slug' => $singular_slug, |
|
169 | - 'plural_slug' => $plural_slug, |
|
170 | - 'add_new' => sprintf( |
|
171 | - esc_html_x('Add %s', 'Add Event', 'event_espresso'), |
|
172 | - $singular_name |
|
173 | - ), |
|
174 | - 'add_new_item' => sprintf( |
|
175 | - esc_html_x('Add New %s', 'Add New Event', 'event_espresso'), |
|
176 | - $singular_name |
|
177 | - ), |
|
178 | - 'edit_item' => sprintf( |
|
179 | - esc_html_x('Edit %s', 'Edit Event', 'event_espresso'), |
|
180 | - $singular_name |
|
181 | - ), |
|
182 | - 'new_item' => sprintf( |
|
183 | - esc_html_x('New %s', 'New Event', 'event_espresso'), |
|
184 | - $singular_name |
|
185 | - ), |
|
186 | - 'all_items' => sprintf( |
|
187 | - esc_html_x('All %s', 'All Events', 'event_espresso'), |
|
188 | - $plural_name |
|
189 | - ), |
|
190 | - 'view_item' => sprintf( |
|
191 | - esc_html_x('View %s', 'View Event', 'event_espresso'), |
|
192 | - $singular_name |
|
193 | - ), |
|
194 | - 'search_items' => sprintf( |
|
195 | - esc_html_x('Search %s', 'Search Events', 'event_espresso'), |
|
196 | - $plural_name |
|
197 | - ), |
|
198 | - 'not_found' => sprintf( |
|
199 | - esc_html_x('No %s found', 'No Events found', 'event_espresso'), |
|
200 | - $plural_name |
|
201 | - ), |
|
202 | - 'not_found_in_trash' => sprintf( |
|
203 | - esc_html_x('No %s found in Trash', 'No Events found in Trash', 'event_espresso'), |
|
204 | - $plural_name |
|
205 | - ), |
|
206 | - 'parent_item_colon' => '', |
|
207 | - 'menu_name' => $plural_name, |
|
208 | - ); |
|
209 | - } |
|
156 | + /** |
|
157 | + * @param string $singular_name |
|
158 | + * @param string $plural_name |
|
159 | + * @param string $singular_slug |
|
160 | + * @param string $plural_slug |
|
161 | + * @return array |
|
162 | + */ |
|
163 | + private function getLabels($singular_name, $plural_name, $singular_slug, $plural_slug) |
|
164 | + { |
|
165 | + return array( |
|
166 | + 'name' => $plural_name, |
|
167 | + 'singular_name' => $singular_name, |
|
168 | + 'singular_slug' => $singular_slug, |
|
169 | + 'plural_slug' => $plural_slug, |
|
170 | + 'add_new' => sprintf( |
|
171 | + esc_html_x('Add %s', 'Add Event', 'event_espresso'), |
|
172 | + $singular_name |
|
173 | + ), |
|
174 | + 'add_new_item' => sprintf( |
|
175 | + esc_html_x('Add New %s', 'Add New Event', 'event_espresso'), |
|
176 | + $singular_name |
|
177 | + ), |
|
178 | + 'edit_item' => sprintf( |
|
179 | + esc_html_x('Edit %s', 'Edit Event', 'event_espresso'), |
|
180 | + $singular_name |
|
181 | + ), |
|
182 | + 'new_item' => sprintf( |
|
183 | + esc_html_x('New %s', 'New Event', 'event_espresso'), |
|
184 | + $singular_name |
|
185 | + ), |
|
186 | + 'all_items' => sprintf( |
|
187 | + esc_html_x('All %s', 'All Events', 'event_espresso'), |
|
188 | + $plural_name |
|
189 | + ), |
|
190 | + 'view_item' => sprintf( |
|
191 | + esc_html_x('View %s', 'View Event', 'event_espresso'), |
|
192 | + $singular_name |
|
193 | + ), |
|
194 | + 'search_items' => sprintf( |
|
195 | + esc_html_x('Search %s', 'Search Events', 'event_espresso'), |
|
196 | + $plural_name |
|
197 | + ), |
|
198 | + 'not_found' => sprintf( |
|
199 | + esc_html_x('No %s found', 'No Events found', 'event_espresso'), |
|
200 | + $plural_name |
|
201 | + ), |
|
202 | + 'not_found_in_trash' => sprintf( |
|
203 | + esc_html_x('No %s found in Trash', 'No Events found in Trash', 'event_espresso'), |
|
204 | + $plural_name |
|
205 | + ), |
|
206 | + 'parent_item_colon' => '', |
|
207 | + 'menu_name' => $plural_name, |
|
208 | + ); |
|
209 | + } |
|
210 | 210 | |
211 | 211 | |
212 | - /** |
|
213 | - * @param array $labels |
|
214 | - * @param string $post_type |
|
215 | - * @param string $plural_slug |
|
216 | - * @return array |
|
217 | - */ |
|
218 | - private function getDefaultArguments(array $labels, $post_type, $plural_slug) |
|
219 | - { |
|
220 | - return array( |
|
221 | - 'labels' => $labels, |
|
222 | - 'public' => true, |
|
223 | - 'publicly_queryable' => true, |
|
224 | - 'show_ui' => false, |
|
225 | - 'show_ee_ui' => true, |
|
226 | - 'show_in_menu' => false, |
|
227 | - 'show_in_nav_menus' => false, |
|
228 | - 'query_var' => true, |
|
229 | - 'rewrite' => apply_filters( |
|
230 | - 'FHEE__EventEspresso_core_domain_entities_custom_post_types_RegisterCustomPostTypes__getDefaultArguments__rewrite', |
|
231 | - // legacy filter applied for now, |
|
232 | - // later on we'll run a has_filter($tag) check and throw a doing_it_wrong() notice |
|
233 | - apply_filters( |
|
234 | - 'FHEE__EE_Register_CPTs__register_CPT__rewrite', |
|
235 | - array('slug' => $plural_slug), |
|
236 | - $post_type |
|
237 | - ), |
|
238 | - $post_type, |
|
239 | - $plural_slug |
|
240 | - ), |
|
241 | - 'capability_type' => 'post', |
|
242 | - 'map_meta_cap' => true, |
|
243 | - 'has_archive' => true, |
|
244 | - 'hierarchical' => false, |
|
245 | - 'menu_position' => null, |
|
246 | - 'supports' => array( |
|
247 | - 'title', |
|
248 | - 'editor', |
|
249 | - 'author', |
|
250 | - 'thumbnail', |
|
251 | - 'excerpt', |
|
252 | - 'custom-fields', |
|
253 | - 'comments', |
|
254 | - ), |
|
255 | - ); |
|
256 | - } |
|
212 | + /** |
|
213 | + * @param array $labels |
|
214 | + * @param string $post_type |
|
215 | + * @param string $plural_slug |
|
216 | + * @return array |
|
217 | + */ |
|
218 | + private function getDefaultArguments(array $labels, $post_type, $plural_slug) |
|
219 | + { |
|
220 | + return array( |
|
221 | + 'labels' => $labels, |
|
222 | + 'public' => true, |
|
223 | + 'publicly_queryable' => true, |
|
224 | + 'show_ui' => false, |
|
225 | + 'show_ee_ui' => true, |
|
226 | + 'show_in_menu' => false, |
|
227 | + 'show_in_nav_menus' => false, |
|
228 | + 'query_var' => true, |
|
229 | + 'rewrite' => apply_filters( |
|
230 | + 'FHEE__EventEspresso_core_domain_entities_custom_post_types_RegisterCustomPostTypes__getDefaultArguments__rewrite', |
|
231 | + // legacy filter applied for now, |
|
232 | + // later on we'll run a has_filter($tag) check and throw a doing_it_wrong() notice |
|
233 | + apply_filters( |
|
234 | + 'FHEE__EE_Register_CPTs__register_CPT__rewrite', |
|
235 | + array('slug' => $plural_slug), |
|
236 | + $post_type |
|
237 | + ), |
|
238 | + $post_type, |
|
239 | + $plural_slug |
|
240 | + ), |
|
241 | + 'capability_type' => 'post', |
|
242 | + 'map_meta_cap' => true, |
|
243 | + 'has_archive' => true, |
|
244 | + 'hierarchical' => false, |
|
245 | + 'menu_position' => null, |
|
246 | + 'supports' => array( |
|
247 | + 'title', |
|
248 | + 'editor', |
|
249 | + 'author', |
|
250 | + 'thumbnail', |
|
251 | + 'excerpt', |
|
252 | + 'custom-fields', |
|
253 | + 'comments', |
|
254 | + ), |
|
255 | + ); |
|
256 | + } |
|
257 | 257 | } |
@@ -15,112 +15,112 @@ |
||
15 | 15 | class Basic implements EmailValidatorInterface |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @param string $email_address |
|
20 | - * @return bool |
|
21 | - * @throws EmailValidationException |
|
22 | - */ |
|
23 | - public function validate($email_address) |
|
24 | - { |
|
25 | - if (! preg_match('/^.+\@\S+$/', $email_address)) { |
|
26 | - // email not in correct {string}@{string} format |
|
27 | - throw new EmailValidationException( |
|
28 | - esc_html__('Email does not have the required @ sign.', 'event_espresso') |
|
29 | - ); |
|
30 | - } |
|
31 | - $atIndex = $this->getAtIndex($email_address); |
|
32 | - $local = $this->getLocalPartOfEmail($email_address, $atIndex); |
|
33 | - $localLen = strlen($local); |
|
34 | - if ($localLen < 1) { |
|
35 | - // no local part |
|
36 | - throw new EmailValidationException( |
|
37 | - esc_html__('Email local-part (before the @) is required.', 'event_espresso') |
|
38 | - ); |
|
39 | - } |
|
40 | - if ($localLen > 64) { |
|
41 | - // local part length exceeded |
|
42 | - throw new EmailValidationException( |
|
43 | - esc_html__('Email local-part (before the @) is too long.', 'event_espresso') |
|
44 | - ); |
|
45 | - } |
|
46 | - if ($local[0] === '.') { |
|
47 | - // local part starts with '.' |
|
48 | - throw new EmailValidationException( |
|
49 | - esc_html__('Email local-part (before the @) must not begin with a period.', 'event_espresso') |
|
50 | - ); |
|
51 | - } |
|
52 | - if ($local[ $localLen - 1 ] === '.') { |
|
53 | - // local part starts or ends with '.' |
|
54 | - throw new EmailValidationException( |
|
55 | - esc_html__('Email local-part (before the @) must not end with a period.', 'event_espresso') |
|
56 | - ); |
|
57 | - } |
|
58 | - if (preg_match('/\\.\\./', $local)) { |
|
59 | - // local part has two consecutive dots |
|
60 | - throw new EmailValidationException( |
|
61 | - esc_html__( |
|
62 | - 'Email local-part (before the @) must not have two consecutive periods.', |
|
63 | - 'event_espresso' |
|
64 | - ) |
|
65 | - ); |
|
66 | - } |
|
67 | - $domain = $this->getDomainPartOfEmail($email_address, $atIndex); |
|
68 | - $domainLen = strlen($domain); |
|
69 | - if ($domainLen < 1) { |
|
70 | - throw new EmailValidationException( |
|
71 | - esc_html__('Email domain (after the @) is required.', 'event_espresso') |
|
72 | - ); |
|
73 | - } |
|
74 | - if ($domainLen > 255) { |
|
75 | - // domain part length exceeded |
|
76 | - throw new EmailValidationException( |
|
77 | - esc_html__('Email domain (after the @) is too long.', 'event_espresso') |
|
78 | - ); |
|
79 | - } |
|
80 | - if (preg_match('/\\.\\./', $domain)) { |
|
81 | - // domain part has two consecutive dots |
|
82 | - throw new EmailValidationException( |
|
83 | - esc_html__('Email domain (after the @) must not have two consecutive periods.', 'event_espresso') |
|
84 | - ); |
|
85 | - } |
|
86 | - return true; |
|
87 | - } |
|
18 | + /** |
|
19 | + * @param string $email_address |
|
20 | + * @return bool |
|
21 | + * @throws EmailValidationException |
|
22 | + */ |
|
23 | + public function validate($email_address) |
|
24 | + { |
|
25 | + if (! preg_match('/^.+\@\S+$/', $email_address)) { |
|
26 | + // email not in correct {string}@{string} format |
|
27 | + throw new EmailValidationException( |
|
28 | + esc_html__('Email does not have the required @ sign.', 'event_espresso') |
|
29 | + ); |
|
30 | + } |
|
31 | + $atIndex = $this->getAtIndex($email_address); |
|
32 | + $local = $this->getLocalPartOfEmail($email_address, $atIndex); |
|
33 | + $localLen = strlen($local); |
|
34 | + if ($localLen < 1) { |
|
35 | + // no local part |
|
36 | + throw new EmailValidationException( |
|
37 | + esc_html__('Email local-part (before the @) is required.', 'event_espresso') |
|
38 | + ); |
|
39 | + } |
|
40 | + if ($localLen > 64) { |
|
41 | + // local part length exceeded |
|
42 | + throw new EmailValidationException( |
|
43 | + esc_html__('Email local-part (before the @) is too long.', 'event_espresso') |
|
44 | + ); |
|
45 | + } |
|
46 | + if ($local[0] === '.') { |
|
47 | + // local part starts with '.' |
|
48 | + throw new EmailValidationException( |
|
49 | + esc_html__('Email local-part (before the @) must not begin with a period.', 'event_espresso') |
|
50 | + ); |
|
51 | + } |
|
52 | + if ($local[ $localLen - 1 ] === '.') { |
|
53 | + // local part starts or ends with '.' |
|
54 | + throw new EmailValidationException( |
|
55 | + esc_html__('Email local-part (before the @) must not end with a period.', 'event_espresso') |
|
56 | + ); |
|
57 | + } |
|
58 | + if (preg_match('/\\.\\./', $local)) { |
|
59 | + // local part has two consecutive dots |
|
60 | + throw new EmailValidationException( |
|
61 | + esc_html__( |
|
62 | + 'Email local-part (before the @) must not have two consecutive periods.', |
|
63 | + 'event_espresso' |
|
64 | + ) |
|
65 | + ); |
|
66 | + } |
|
67 | + $domain = $this->getDomainPartOfEmail($email_address, $atIndex); |
|
68 | + $domainLen = strlen($domain); |
|
69 | + if ($domainLen < 1) { |
|
70 | + throw new EmailValidationException( |
|
71 | + esc_html__('Email domain (after the @) is required.', 'event_espresso') |
|
72 | + ); |
|
73 | + } |
|
74 | + if ($domainLen > 255) { |
|
75 | + // domain part length exceeded |
|
76 | + throw new EmailValidationException( |
|
77 | + esc_html__('Email domain (after the @) is too long.', 'event_espresso') |
|
78 | + ); |
|
79 | + } |
|
80 | + if (preg_match('/\\.\\./', $domain)) { |
|
81 | + // domain part has two consecutive dots |
|
82 | + throw new EmailValidationException( |
|
83 | + esc_html__('Email domain (after the @) must not have two consecutive periods.', 'event_espresso') |
|
84 | + ); |
|
85 | + } |
|
86 | + return true; |
|
87 | + } |
|
88 | 88 | |
89 | 89 | |
90 | - /** |
|
91 | - * returns the location of the @ symbol |
|
92 | - * |
|
93 | - * @param string $email_address |
|
94 | - * @return bool|string |
|
95 | - */ |
|
96 | - protected function getAtIndex($email_address) |
|
97 | - { |
|
98 | - return strrpos($email_address, '@'); |
|
99 | - } |
|
90 | + /** |
|
91 | + * returns the location of the @ symbol |
|
92 | + * |
|
93 | + * @param string $email_address |
|
94 | + * @return bool|string |
|
95 | + */ |
|
96 | + protected function getAtIndex($email_address) |
|
97 | + { |
|
98 | + return strrpos($email_address, '@'); |
|
99 | + } |
|
100 | 100 | |
101 | 101 | |
102 | - /** |
|
103 | - * Gets the local part of the email |
|
104 | - * |
|
105 | - * @param string $email_address |
|
106 | - * @param bool|int $atIndex |
|
107 | - * @return bool|string |
|
108 | - */ |
|
109 | - protected function getLocalPartOfEmail($email_address, $atIndex) |
|
110 | - { |
|
111 | - return substr($email_address, 0, $atIndex); |
|
112 | - } |
|
102 | + /** |
|
103 | + * Gets the local part of the email |
|
104 | + * |
|
105 | + * @param string $email_address |
|
106 | + * @param bool|int $atIndex |
|
107 | + * @return bool|string |
|
108 | + */ |
|
109 | + protected function getLocalPartOfEmail($email_address, $atIndex) |
|
110 | + { |
|
111 | + return substr($email_address, 0, $atIndex); |
|
112 | + } |
|
113 | 113 | |
114 | 114 | |
115 | - /** |
|
116 | - * Gets the domain part of the email |
|
117 | - * |
|
118 | - * @param string $email_address |
|
119 | - * @param bool|int $atIndex |
|
120 | - * @return bool|string |
|
121 | - */ |
|
122 | - protected function getDomainPartOfEmail($email_address, $atIndex) |
|
123 | - { |
|
124 | - return substr($email_address, $atIndex + 1); |
|
125 | - } |
|
115 | + /** |
|
116 | + * Gets the domain part of the email |
|
117 | + * |
|
118 | + * @param string $email_address |
|
119 | + * @param bool|int $atIndex |
|
120 | + * @return bool|string |
|
121 | + */ |
|
122 | + protected function getDomainPartOfEmail($email_address, $atIndex) |
|
123 | + { |
|
124 | + return substr($email_address, $atIndex + 1); |
|
125 | + } |
|
126 | 126 | } |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | */ |
23 | 23 | public function validate($email_address) |
24 | 24 | { |
25 | - if (! preg_match('/^.+\@\S+$/', $email_address)) { |
|
25 | + if ( ! preg_match('/^.+\@\S+$/', $email_address)) { |
|
26 | 26 | // email not in correct {string}@{string} format |
27 | 27 | throw new EmailValidationException( |
28 | 28 | esc_html__('Email does not have the required @ sign.', 'event_espresso') |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | esc_html__('Email local-part (before the @) must not begin with a period.', 'event_espresso') |
50 | 50 | ); |
51 | 51 | } |
52 | - if ($local[ $localLen - 1 ] === '.') { |
|
52 | + if ($local[$localLen - 1] === '.') { |
|
53 | 53 | // local part starts or ends with '.' |
54 | 54 | throw new EmailValidationException( |
55 | 55 | esc_html__('Email local-part (before the @) must not end with a period.', 'event_espresso') |
@@ -26,149 +26,149 @@ |
||
26 | 26 | { |
27 | 27 | |
28 | 28 | |
29 | - /** |
|
30 | - * @param EE_Registration $target_registration |
|
31 | - * @param EE_Registration $registration_to_copy |
|
32 | - * @return bool |
|
33 | - * @throws UnexpectedEntityException |
|
34 | - * @throws EntityNotFoundException |
|
35 | - * @throws RuntimeException |
|
36 | - * @throws EE_Error |
|
37 | - */ |
|
38 | - public function copyRegistrationDetails( |
|
39 | - EE_Registration $target_registration, |
|
40 | - EE_Registration $registration_to_copy |
|
41 | - ) { |
|
42 | - // copy attendee |
|
43 | - $target_registration->set_attendee_id($registration_to_copy->attendee_ID()); |
|
44 | - $target_registration->updateStatusBasedOnTotalPaid(false); |
|
45 | - $target_registration->save(); |
|
46 | - // get answers to previous reg questions |
|
47 | - $answers = $this->reindexAnswersByQuestionId($registration_to_copy->answers()); |
|
48 | - // get questions to new event reg form |
|
49 | - $new_event = $target_registration->event(); |
|
50 | - $question_groups = $new_event->question_groups( |
|
51 | - array( |
|
52 | - array( |
|
53 | - 'Event.EVT_ID' => $new_event->ID(), |
|
54 | - 'Event_Question_Group.EQG_primary' => $registration_to_copy->is_primary_registrant(), |
|
55 | - ), |
|
56 | - 'order_by' => array('QSG_order' => 'ASC'), |
|
57 | - ) |
|
58 | - ); |
|
59 | - foreach ($question_groups as $question_group) { |
|
60 | - if ($question_group instanceof \EE_Question_Group) { |
|
61 | - foreach ($question_group->questions() as $question) { |
|
62 | - if ($question instanceof EE_Question) { |
|
63 | - $this->generateNewAnswer( |
|
64 | - $question, |
|
65 | - $target_registration, |
|
66 | - $answers |
|
67 | - ); |
|
68 | - } |
|
69 | - } |
|
70 | - } |
|
71 | - } |
|
72 | - return true; |
|
73 | - } |
|
29 | + /** |
|
30 | + * @param EE_Registration $target_registration |
|
31 | + * @param EE_Registration $registration_to_copy |
|
32 | + * @return bool |
|
33 | + * @throws UnexpectedEntityException |
|
34 | + * @throws EntityNotFoundException |
|
35 | + * @throws RuntimeException |
|
36 | + * @throws EE_Error |
|
37 | + */ |
|
38 | + public function copyRegistrationDetails( |
|
39 | + EE_Registration $target_registration, |
|
40 | + EE_Registration $registration_to_copy |
|
41 | + ) { |
|
42 | + // copy attendee |
|
43 | + $target_registration->set_attendee_id($registration_to_copy->attendee_ID()); |
|
44 | + $target_registration->updateStatusBasedOnTotalPaid(false); |
|
45 | + $target_registration->save(); |
|
46 | + // get answers to previous reg questions |
|
47 | + $answers = $this->reindexAnswersByQuestionId($registration_to_copy->answers()); |
|
48 | + // get questions to new event reg form |
|
49 | + $new_event = $target_registration->event(); |
|
50 | + $question_groups = $new_event->question_groups( |
|
51 | + array( |
|
52 | + array( |
|
53 | + 'Event.EVT_ID' => $new_event->ID(), |
|
54 | + 'Event_Question_Group.EQG_primary' => $registration_to_copy->is_primary_registrant(), |
|
55 | + ), |
|
56 | + 'order_by' => array('QSG_order' => 'ASC'), |
|
57 | + ) |
|
58 | + ); |
|
59 | + foreach ($question_groups as $question_group) { |
|
60 | + if ($question_group instanceof \EE_Question_Group) { |
|
61 | + foreach ($question_group->questions() as $question) { |
|
62 | + if ($question instanceof EE_Question) { |
|
63 | + $this->generateNewAnswer( |
|
64 | + $question, |
|
65 | + $target_registration, |
|
66 | + $answers |
|
67 | + ); |
|
68 | + } |
|
69 | + } |
|
70 | + } |
|
71 | + } |
|
72 | + return true; |
|
73 | + } |
|
74 | 74 | |
75 | 75 | |
76 | - /** |
|
77 | - * @param EE_Answer[] $answers |
|
78 | - * @return array |
|
79 | - * @throws EE_Error |
|
80 | - */ |
|
81 | - protected function reindexAnswersByQuestionId(array $answers) |
|
82 | - { |
|
83 | - $reindexed_answers = array(); |
|
84 | - foreach ($answers as $answer) { |
|
85 | - if ($answer instanceof EE_Answer) { |
|
86 | - $reindexed_answers[ $answer->question_ID() ] = $answer->value(); |
|
87 | - } |
|
88 | - } |
|
89 | - return $reindexed_answers; |
|
90 | - } |
|
76 | + /** |
|
77 | + * @param EE_Answer[] $answers |
|
78 | + * @return array |
|
79 | + * @throws EE_Error |
|
80 | + */ |
|
81 | + protected function reindexAnswersByQuestionId(array $answers) |
|
82 | + { |
|
83 | + $reindexed_answers = array(); |
|
84 | + foreach ($answers as $answer) { |
|
85 | + if ($answer instanceof EE_Answer) { |
|
86 | + $reindexed_answers[ $answer->question_ID() ] = $answer->value(); |
|
87 | + } |
|
88 | + } |
|
89 | + return $reindexed_answers; |
|
90 | + } |
|
91 | 91 | |
92 | 92 | |
93 | - /** |
|
94 | - * @param EE_Question $question |
|
95 | - * @param EE_Registration $registration |
|
96 | - * @param $previous_answers |
|
97 | - * @return EE_Answer |
|
98 | - * @throws UnexpectedEntityException |
|
99 | - * @throws EE_Error |
|
100 | - */ |
|
101 | - protected function generateNewAnswer( |
|
102 | - EE_Question $question, |
|
103 | - EE_Registration $registration, |
|
104 | - $previous_answers |
|
105 | - ) { |
|
106 | - $old_answer_value = isset($previous_answers[ $question->ID() ]) |
|
107 | - ? $previous_answers[ $question->ID() ] |
|
108 | - : ''; |
|
109 | - $new_answer = EE_Answer::new_instance( |
|
110 | - array( |
|
111 | - 'QST_ID' => $question->ID(), |
|
112 | - 'REG_ID' => $registration->ID(), |
|
113 | - 'ANS_value' => $old_answer_value, |
|
114 | - ) |
|
115 | - ); |
|
116 | - if (! $new_answer instanceof EE_Answer) { |
|
117 | - throw new UnexpectedEntityException($new_answer, 'EE_Answer'); |
|
118 | - } |
|
119 | - $new_answer->save(); |
|
120 | - return $new_answer; |
|
121 | - } |
|
93 | + /** |
|
94 | + * @param EE_Question $question |
|
95 | + * @param EE_Registration $registration |
|
96 | + * @param $previous_answers |
|
97 | + * @return EE_Answer |
|
98 | + * @throws UnexpectedEntityException |
|
99 | + * @throws EE_Error |
|
100 | + */ |
|
101 | + protected function generateNewAnswer( |
|
102 | + EE_Question $question, |
|
103 | + EE_Registration $registration, |
|
104 | + $previous_answers |
|
105 | + ) { |
|
106 | + $old_answer_value = isset($previous_answers[ $question->ID() ]) |
|
107 | + ? $previous_answers[ $question->ID() ] |
|
108 | + : ''; |
|
109 | + $new_answer = EE_Answer::new_instance( |
|
110 | + array( |
|
111 | + 'QST_ID' => $question->ID(), |
|
112 | + 'REG_ID' => $registration->ID(), |
|
113 | + 'ANS_value' => $old_answer_value, |
|
114 | + ) |
|
115 | + ); |
|
116 | + if (! $new_answer instanceof EE_Answer) { |
|
117 | + throw new UnexpectedEntityException($new_answer, 'EE_Answer'); |
|
118 | + } |
|
119 | + $new_answer->save(); |
|
120 | + return $new_answer; |
|
121 | + } |
|
122 | 122 | |
123 | 123 | |
124 | - /** |
|
125 | - * @param EE_Registration $target_registration |
|
126 | - * @param EE_Registration $registration_to_copy |
|
127 | - * @return bool |
|
128 | - * @throws RuntimeException |
|
129 | - * @throws UnexpectedEntityException |
|
130 | - * @throws EE_Error |
|
131 | - */ |
|
132 | - public function copyPaymentDetails( |
|
133 | - EE_Registration $target_registration, |
|
134 | - EE_Registration $registration_to_copy |
|
135 | - ) { |
|
136 | - $save = false; |
|
137 | - $previous_registration_payments = $registration_to_copy->registration_payments(); |
|
138 | - $new_registration_payment_total = 0; |
|
139 | - $registration_to_copy_total = $registration_to_copy->paid(); |
|
140 | - foreach ($previous_registration_payments as $previous_registration_payment) { |
|
141 | - if ($previous_registration_payment instanceof EE_Registration_Payment |
|
142 | - && $previous_registration_payment->payment() instanceof EE_Payment |
|
143 | - && $previous_registration_payment->payment()->is_approved() |
|
144 | - ) { |
|
145 | - $payment_amount = $previous_registration_payment->amount(); |
|
146 | - $new_registration_payment = EE_Registration_Payment::new_instance( |
|
147 | - array( |
|
148 | - 'REG_ID' => $target_registration->ID(), |
|
149 | - 'PAY_ID' => $previous_registration_payment->payment()->ID(), |
|
150 | - 'RPY_amount' => $payment_amount, |
|
151 | - ) |
|
152 | - ); |
|
153 | - if (! $new_registration_payment instanceof EE_Registration_Payment) { |
|
154 | - throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment'); |
|
155 | - } |
|
156 | - $new_registration_payment->save(); |
|
157 | - // if new reg payment is good, then set old reg payment amount to zero |
|
158 | - $previous_registration_payment->set_amount(0); |
|
159 | - $previous_registration_payment->save(); |
|
160 | - // now increment/decrement payment amounts |
|
161 | - $new_registration_payment_total += $payment_amount; |
|
162 | - $registration_to_copy_total -= $payment_amount; |
|
163 | - $save = true; |
|
164 | - } |
|
165 | - } |
|
166 | - if ($save) { |
|
167 | - $target_registration->set_paid($new_registration_payment_total); |
|
168 | - $target_registration->save(); |
|
169 | - $registration_to_copy->set_paid($registration_to_copy_total); |
|
170 | - $registration_to_copy->save(); |
|
171 | - } |
|
172 | - return true; |
|
173 | - } |
|
124 | + /** |
|
125 | + * @param EE_Registration $target_registration |
|
126 | + * @param EE_Registration $registration_to_copy |
|
127 | + * @return bool |
|
128 | + * @throws RuntimeException |
|
129 | + * @throws UnexpectedEntityException |
|
130 | + * @throws EE_Error |
|
131 | + */ |
|
132 | + public function copyPaymentDetails( |
|
133 | + EE_Registration $target_registration, |
|
134 | + EE_Registration $registration_to_copy |
|
135 | + ) { |
|
136 | + $save = false; |
|
137 | + $previous_registration_payments = $registration_to_copy->registration_payments(); |
|
138 | + $new_registration_payment_total = 0; |
|
139 | + $registration_to_copy_total = $registration_to_copy->paid(); |
|
140 | + foreach ($previous_registration_payments as $previous_registration_payment) { |
|
141 | + if ($previous_registration_payment instanceof EE_Registration_Payment |
|
142 | + && $previous_registration_payment->payment() instanceof EE_Payment |
|
143 | + && $previous_registration_payment->payment()->is_approved() |
|
144 | + ) { |
|
145 | + $payment_amount = $previous_registration_payment->amount(); |
|
146 | + $new_registration_payment = EE_Registration_Payment::new_instance( |
|
147 | + array( |
|
148 | + 'REG_ID' => $target_registration->ID(), |
|
149 | + 'PAY_ID' => $previous_registration_payment->payment()->ID(), |
|
150 | + 'RPY_amount' => $payment_amount, |
|
151 | + ) |
|
152 | + ); |
|
153 | + if (! $new_registration_payment instanceof EE_Registration_Payment) { |
|
154 | + throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment'); |
|
155 | + } |
|
156 | + $new_registration_payment->save(); |
|
157 | + // if new reg payment is good, then set old reg payment amount to zero |
|
158 | + $previous_registration_payment->set_amount(0); |
|
159 | + $previous_registration_payment->save(); |
|
160 | + // now increment/decrement payment amounts |
|
161 | + $new_registration_payment_total += $payment_amount; |
|
162 | + $registration_to_copy_total -= $payment_amount; |
|
163 | + $save = true; |
|
164 | + } |
|
165 | + } |
|
166 | + if ($save) { |
|
167 | + $target_registration->set_paid($new_registration_payment_total); |
|
168 | + $target_registration->save(); |
|
169 | + $registration_to_copy->set_paid($registration_to_copy_total); |
|
170 | + $registration_to_copy->save(); |
|
171 | + } |
|
172 | + return true; |
|
173 | + } |
|
174 | 174 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | $reindexed_answers = array(); |
84 | 84 | foreach ($answers as $answer) { |
85 | 85 | if ($answer instanceof EE_Answer) { |
86 | - $reindexed_answers[ $answer->question_ID() ] = $answer->value(); |
|
86 | + $reindexed_answers[$answer->question_ID()] = $answer->value(); |
|
87 | 87 | } |
88 | 88 | } |
89 | 89 | return $reindexed_answers; |
@@ -103,8 +103,8 @@ discard block |
||
103 | 103 | EE_Registration $registration, |
104 | 104 | $previous_answers |
105 | 105 | ) { |
106 | - $old_answer_value = isset($previous_answers[ $question->ID() ]) |
|
107 | - ? $previous_answers[ $question->ID() ] |
|
106 | + $old_answer_value = isset($previous_answers[$question->ID()]) |
|
107 | + ? $previous_answers[$question->ID()] |
|
108 | 108 | : ''; |
109 | 109 | $new_answer = EE_Answer::new_instance( |
110 | 110 | array( |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | 'ANS_value' => $old_answer_value, |
114 | 114 | ) |
115 | 115 | ); |
116 | - if (! $new_answer instanceof EE_Answer) { |
|
116 | + if ( ! $new_answer instanceof EE_Answer) { |
|
117 | 117 | throw new UnexpectedEntityException($new_answer, 'EE_Answer'); |
118 | 118 | } |
119 | 119 | $new_answer->save(); |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | 'RPY_amount' => $payment_amount, |
151 | 151 | ) |
152 | 152 | ); |
153 | - if (! $new_registration_payment instanceof EE_Registration_Payment) { |
|
153 | + if ( ! $new_registration_payment instanceof EE_Registration_Payment) { |
|
154 | 154 | throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment'); |
155 | 155 | } |
156 | 156 | $new_registration_payment->save(); |
@@ -26,702 +26,702 @@ |
||
26 | 26 | class EventSpacesCalculator |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * @var EE_Event $event |
|
31 | - */ |
|
32 | - private $event; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var array $datetime_query_params |
|
36 | - */ |
|
37 | - private $datetime_query_params; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var EE_Ticket[] $active_tickets |
|
41 | - */ |
|
42 | - private $active_tickets = array(); |
|
43 | - |
|
44 | - /** |
|
45 | - * @var EE_Datetime[] $datetimes |
|
46 | - */ |
|
47 | - private $datetimes = array(); |
|
48 | - |
|
49 | - /** |
|
50 | - * Array of Ticket IDs grouped by Datetime |
|
51 | - * |
|
52 | - * @var array $datetimes |
|
53 | - */ |
|
54 | - private $datetime_tickets = array(); |
|
55 | - |
|
56 | - /** |
|
57 | - * Max spaces for each Datetime (reg limit - previous sold) |
|
58 | - * |
|
59 | - * @var array $datetime_spaces |
|
60 | - */ |
|
61 | - private $datetime_spaces = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * Array of Datetime IDs grouped by Ticket |
|
65 | - * |
|
66 | - * @var array[] $ticket_datetimes |
|
67 | - */ |
|
68 | - private $ticket_datetimes = array(); |
|
69 | - |
|
70 | - /** |
|
71 | - * maximum ticket quantities for each ticket (adjusted for reg limit) |
|
72 | - * |
|
73 | - * @var array $ticket_quantities |
|
74 | - */ |
|
75 | - private $ticket_quantities = array(); |
|
76 | - |
|
77 | - /** |
|
78 | - * total quantity of sold and reserved for each ticket |
|
79 | - * |
|
80 | - * @var array $tickets_sold |
|
81 | - */ |
|
82 | - private $tickets_sold = array(); |
|
83 | - |
|
84 | - /** |
|
85 | - * total spaces available across all datetimes |
|
86 | - * |
|
87 | - * @var array $total_spaces |
|
88 | - */ |
|
89 | - private $total_spaces = array(); |
|
90 | - |
|
91 | - /** |
|
92 | - * @var boolean $debug |
|
93 | - */ |
|
94 | - private $debug = false; // true false |
|
95 | - |
|
96 | - /** |
|
97 | - * @var null|int $spaces_remaining |
|
98 | - */ |
|
99 | - private $spaces_remaining; |
|
100 | - |
|
101 | - /** |
|
102 | - * @var null|int $total_spaces_available |
|
103 | - */ |
|
104 | - private $total_spaces_available; |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * EventSpacesCalculator constructor. |
|
109 | - * |
|
110 | - * @param EE_Event $event |
|
111 | - * @param array $datetime_query_params |
|
112 | - * @throws EE_Error |
|
113 | - */ |
|
114 | - public function __construct(EE_Event $event, array $datetime_query_params = array()) |
|
115 | - { |
|
116 | - $this->event = $event; |
|
117 | - $this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC')); |
|
118 | - $this->setHooks(); |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - private function setHooks() |
|
126 | - { |
|
127 | - add_action('AHEE__EE_Ticket__increase_sold', array($this, 'clearResults')); |
|
128 | - add_action('AHEE__EE_Ticket__decrease_sold', array($this, 'clearResults')); |
|
129 | - add_action('AHEE__EE_Datetime__increase_sold', array($this, 'clearResults')); |
|
130 | - add_action('AHEE__EE_Datetime__decrease_sold', array($this, 'clearResults')); |
|
131 | - add_action('AHEE__EE_Ticket__increase_reserved', array($this, 'clearResults')); |
|
132 | - add_action('AHEE__EE_Ticket__decrease_reserved', array($this, 'clearResults')); |
|
133 | - add_action('AHEE__EE_Datetime__increase_reserved', array($this, 'clearResults')); |
|
134 | - add_action('AHEE__EE_Datetime__decrease_reserved', array($this, 'clearResults')); |
|
135 | - } |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * @return void |
|
140 | - */ |
|
141 | - public function clearResults() |
|
142 | - { |
|
143 | - $this->spaces_remaining = null; |
|
144 | - $this->total_spaces_available = null; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * @return EE_Ticket[] |
|
150 | - * @throws EE_Error |
|
151 | - * @throws InvalidDataTypeException |
|
152 | - * @throws InvalidInterfaceException |
|
153 | - * @throws InvalidArgumentException |
|
154 | - */ |
|
155 | - public function getActiveTickets() |
|
156 | - { |
|
157 | - if (empty($this->active_tickets)) { |
|
158 | - $this->active_tickets = $this->event->tickets( |
|
159 | - array( |
|
160 | - array('TKT_deleted' => false), |
|
161 | - 'order_by' => array('TKT_qty' => 'ASC'), |
|
162 | - ) |
|
163 | - ); |
|
164 | - } |
|
165 | - return $this->active_tickets; |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * @param EE_Ticket[] $active_tickets |
|
171 | - * @throws EE_Error |
|
172 | - * @throws DomainException |
|
173 | - * @throws UnexpectedEntityException |
|
174 | - */ |
|
175 | - public function setActiveTickets(array $active_tickets = array()) |
|
176 | - { |
|
177 | - if (! empty($active_tickets)) { |
|
178 | - foreach ($active_tickets as $active_ticket) { |
|
179 | - $this->validateTicket($active_ticket); |
|
180 | - } |
|
181 | - // sort incoming array by ticket quantity (asc) |
|
182 | - usort( |
|
183 | - $active_tickets, |
|
184 | - function (EE_Ticket $a, EE_Ticket $b) { |
|
185 | - if ($a->qty() === $b->qty()) { |
|
186 | - return 0; |
|
187 | - } |
|
188 | - return ($a->qty() < $b->qty()) |
|
189 | - ? -1 |
|
190 | - : 1; |
|
191 | - } |
|
192 | - ); |
|
193 | - } |
|
194 | - $this->active_tickets = $active_tickets; |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * @param $ticket |
|
200 | - * @throws DomainException |
|
201 | - * @throws EE_Error |
|
202 | - * @throws UnexpectedEntityException |
|
203 | - */ |
|
204 | - private function validateTicket($ticket) |
|
205 | - { |
|
206 | - if (! $ticket instanceof EE_Ticket) { |
|
207 | - throw new DomainException( |
|
208 | - esc_html__( |
|
209 | - 'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.', |
|
210 | - 'event_espresso' |
|
211 | - ) |
|
212 | - ); |
|
213 | - } |
|
214 | - if ($ticket->get_event_ID() !== $this->event->ID()) { |
|
215 | - throw new DomainException( |
|
216 | - sprintf( |
|
217 | - esc_html__( |
|
218 | - 'An EE_Ticket for Event %1$d was supplied while calculating event space availability for Event %2$d.', |
|
219 | - 'event_espresso' |
|
220 | - ), |
|
221 | - $ticket->get_event_ID(), |
|
222 | - $this->event->ID() |
|
223 | - ) |
|
224 | - ); |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - |
|
229 | - /** |
|
230 | - * @return EE_Datetime[] |
|
231 | - */ |
|
232 | - public function getDatetimes() |
|
233 | - { |
|
234 | - return $this->datetimes; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * @param EE_Datetime $datetime |
|
240 | - * @throws EE_Error |
|
241 | - * @throws DomainException |
|
242 | - */ |
|
243 | - public function setDatetime(EE_Datetime $datetime) |
|
244 | - { |
|
245 | - if ($datetime->event()->ID() !== $this->event->ID()) { |
|
246 | - throw new DomainException( |
|
247 | - sprintf( |
|
248 | - esc_html__( |
|
249 | - 'An EE_Datetime for Event %1$d was supplied while calculating event space availability for Event %2$d.', |
|
250 | - 'event_espresso' |
|
251 | - ), |
|
252 | - $datetime->event()->ID(), |
|
253 | - $this->event->ID() |
|
254 | - ) |
|
255 | - ); |
|
256 | - } |
|
257 | - $this->datetimes[ $datetime->ID() ] = $datetime; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * calculate spaces remaining based on "saleable" tickets |
|
263 | - * |
|
264 | - * @return float|int |
|
265 | - * @throws EE_Error |
|
266 | - * @throws DomainException |
|
267 | - * @throws UnexpectedEntityException |
|
268 | - * @throws InvalidDataTypeException |
|
269 | - * @throws InvalidInterfaceException |
|
270 | - * @throws InvalidArgumentException |
|
271 | - */ |
|
272 | - public function spacesRemaining() |
|
273 | - { |
|
274 | - if ($this->spaces_remaining === null) { |
|
275 | - $this->initialize(); |
|
276 | - $this->spaces_remaining = $this->calculate(); |
|
277 | - } |
|
278 | - return $this->spaces_remaining; |
|
279 | - } |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * calculates total available spaces for an event with no regard for sold tickets |
|
284 | - * |
|
285 | - * @return int|float |
|
286 | - * @throws EE_Error |
|
287 | - * @throws DomainException |
|
288 | - * @throws UnexpectedEntityException |
|
289 | - * @throws InvalidDataTypeException |
|
290 | - * @throws InvalidInterfaceException |
|
291 | - * @throws InvalidArgumentException |
|
292 | - */ |
|
293 | - public function totalSpacesAvailable() |
|
294 | - { |
|
295 | - if ($this->total_spaces_available === null) { |
|
296 | - $this->initialize(); |
|
297 | - $this->total_spaces_available = $this->calculate(false); |
|
298 | - } |
|
299 | - return $this->total_spaces_available; |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - /** |
|
304 | - * Loops through the active tickets for the event |
|
305 | - * and builds a series of data arrays that will be used for calculating |
|
306 | - * the total maximum available spaces, as well as the spaces remaining. |
|
307 | - * Because ticket quantities affect datetime spaces and vice versa, |
|
308 | - * we need to be constantly updating these data arrays as things change, |
|
309 | - * which is the entire reason for their existence. |
|
310 | - * |
|
311 | - * @throws EE_Error |
|
312 | - * @throws DomainException |
|
313 | - * @throws UnexpectedEntityException |
|
314 | - * @throws InvalidDataTypeException |
|
315 | - * @throws InvalidInterfaceException |
|
316 | - * @throws InvalidArgumentException |
|
317 | - */ |
|
318 | - private function initialize() |
|
319 | - { |
|
320 | - if ($this->debug) { |
|
321 | - \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
322 | - } |
|
323 | - $this->datetime_tickets = array(); |
|
324 | - $this->datetime_spaces = array(); |
|
325 | - $this->ticket_datetimes = array(); |
|
326 | - $this->ticket_quantities = array(); |
|
327 | - $this->tickets_sold = array(); |
|
328 | - $this->total_spaces = array(); |
|
329 | - $active_tickets = $this->getActiveTickets(); |
|
330 | - if (! empty($active_tickets)) { |
|
331 | - foreach ($active_tickets as $ticket) { |
|
332 | - $this->validateTicket($ticket); |
|
333 | - // we need to index our data arrays using strings for the purpose of sorting, |
|
334 | - // but we also need them to be unique, so we'll just prepend a letter T to the ID |
|
335 | - $ticket_identifier = "T{$ticket->ID()}"; |
|
336 | - // to start, we'll just consider the raw qty to be the maximum availability for this ticket, |
|
337 | - // unless the ticket is past its "sell until" date, in which case the qty will be 0 |
|
338 | - $max_tickets = $ticket->is_expired() ? 0 : $ticket->qty(); |
|
339 | - // but we'll adjust that after looping over each datetime for the ticket and checking reg limits |
|
340 | - $ticket_datetimes = $ticket->datetimes($this->datetime_query_params); |
|
341 | - foreach ($ticket_datetimes as $datetime) { |
|
342 | - // save all datetimes |
|
343 | - $this->setDatetime($datetime); |
|
344 | - $datetime_identifier = "D{$datetime->ID()}"; |
|
345 | - $reg_limit = $datetime->reg_limit(); |
|
346 | - // ticket quantity can not exceed datetime reg limit |
|
347 | - $max_tickets = min($max_tickets, $reg_limit); |
|
348 | - // as described earlier, because we need to be able to constantly adjust numbers for things, |
|
349 | - // we are going to move all of our data into the following arrays: |
|
350 | - // datetime spaces initially represents the reg limit for each datetime, |
|
351 | - // but this will get adjusted as tickets are accounted for |
|
352 | - $this->datetime_spaces[ $datetime_identifier ] = $reg_limit; |
|
353 | - // just an array of ticket IDs grouped by datetime |
|
354 | - $this->datetime_tickets[ $datetime_identifier ][] = $ticket_identifier; |
|
355 | - // and an array of datetime IDs grouped by ticket |
|
356 | - $this->ticket_datetimes[ $ticket_identifier ][] = $datetime_identifier; |
|
357 | - } |
|
358 | - // total quantity of sold and reserved for each ticket |
|
359 | - $this->tickets_sold[ $ticket_identifier ] = $ticket->sold() + $ticket->reserved(); |
|
360 | - // and the maximum ticket quantities for each ticket (adjusted for reg limit) |
|
361 | - $this->ticket_quantities[ $ticket_identifier ] = $max_tickets; |
|
362 | - } |
|
363 | - } |
|
364 | - // sort datetime spaces by reg limit, but maintain our string indexes |
|
365 | - asort($this->datetime_spaces, SORT_NUMERIC); |
|
366 | - // datetime tickets need to be sorted in the SAME order as the above array... |
|
367 | - // so we'll just use array_merge() to take the structure of datetime_spaces |
|
368 | - // but overwrite all of the data with that from datetime_tickets |
|
369 | - $this->datetime_tickets = array_merge( |
|
370 | - $this->datetime_spaces, |
|
371 | - $this->datetime_tickets |
|
372 | - ); |
|
373 | - if ($this->debug) { |
|
374 | - \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__); |
|
375 | - \EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__); |
|
376 | - \EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__); |
|
377 | - } |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - /** |
|
382 | - * performs calculations on initialized data |
|
383 | - * |
|
384 | - * @param bool $consider_sold |
|
385 | - * @return int|float |
|
386 | - */ |
|
387 | - private function calculate($consider_sold = true) |
|
388 | - { |
|
389 | - if ($this->debug) { |
|
390 | - \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
391 | - \EEH_Debug_Tools::printr($consider_sold, '$consider_sold', __FILE__, __LINE__); |
|
392 | - } |
|
393 | - if ($consider_sold) { |
|
394 | - // subtract amounts sold from all ticket quantities and datetime spaces |
|
395 | - $this->adjustTicketQuantitiesDueToSales(); |
|
396 | - } |
|
397 | - foreach ($this->datetime_tickets as $datetime_identifier => $tickets) { |
|
398 | - $this->trackAvailableSpacesForDatetimes($datetime_identifier, $tickets); |
|
399 | - } |
|
400 | - // total spaces available is just the sum of the spaces available for each datetime |
|
401 | - $spaces_remaining = array_sum($this->total_spaces); |
|
402 | - if ($this->debug) { |
|
403 | - \EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__); |
|
404 | - \EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__); |
|
405 | - \EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__); |
|
406 | - } |
|
407 | - return $spaces_remaining; |
|
408 | - } |
|
409 | - |
|
410 | - |
|
411 | - /** |
|
412 | - * subtracts amount of tickets sold from ticket quantities and datetime spaces |
|
413 | - */ |
|
414 | - private function adjustTicketQuantitiesDueToSales() |
|
415 | - { |
|
416 | - if ($this->debug) { |
|
417 | - \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
418 | - } |
|
419 | - foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) { |
|
420 | - if (isset($this->ticket_quantities[ $ticket_identifier ])) { |
|
421 | - $this->ticket_quantities[ $ticket_identifier ] -= $tickets_sold; |
|
422 | - // don't let values go below zero |
|
423 | - $this->ticket_quantities[ $ticket_identifier ] = max( |
|
424 | - $this->ticket_quantities[ $ticket_identifier ], |
|
425 | - 0 |
|
426 | - ); |
|
427 | - if ($this->debug) { |
|
428 | - \EEH_Debug_Tools::printr( |
|
429 | - "{$tickets_sold} sales for ticket {$ticket_identifier} ", |
|
430 | - 'subtracting', |
|
431 | - __FILE__, |
|
432 | - __LINE__ |
|
433 | - ); |
|
434 | - } |
|
435 | - } |
|
436 | - if (isset($this->ticket_datetimes[ $ticket_identifier ]) |
|
437 | - && is_array($this->ticket_datetimes[ $ticket_identifier ]) |
|
438 | - ) { |
|
439 | - foreach ($this->ticket_datetimes[ $ticket_identifier ] as $ticket_datetime) { |
|
440 | - if (isset($this->ticket_quantities[ $ticket_identifier ])) { |
|
441 | - $this->datetime_spaces[ $ticket_datetime ] -= $tickets_sold; |
|
442 | - // don't let values go below zero |
|
443 | - $this->datetime_spaces[ $ticket_datetime ] = max( |
|
444 | - $this->datetime_spaces[ $ticket_datetime ], |
|
445 | - 0 |
|
446 | - ); |
|
447 | - if ($this->debug) { |
|
448 | - \EEH_Debug_Tools::printr( |
|
449 | - "{$tickets_sold} sales for datetime {$ticket_datetime} ", |
|
450 | - 'subtracting', |
|
451 | - __FILE__, |
|
452 | - __LINE__ |
|
453 | - ); |
|
454 | - } |
|
455 | - } |
|
456 | - } |
|
457 | - } |
|
458 | - } |
|
459 | - } |
|
460 | - |
|
461 | - |
|
462 | - /** |
|
463 | - * @param string $datetime_identifier |
|
464 | - * @param array $tickets |
|
465 | - */ |
|
466 | - private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets) |
|
467 | - { |
|
468 | - // make sure a reg limit is set for the datetime |
|
469 | - $reg_limit = isset($this->datetime_spaces[ $datetime_identifier ]) |
|
470 | - ? $this->datetime_spaces[ $datetime_identifier ] |
|
471 | - : 0; |
|
472 | - // and bail if it is not |
|
473 | - if (! $reg_limit) { |
|
474 | - if ($this->debug) { |
|
475 | - \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__); |
|
476 | - } |
|
477 | - return; |
|
478 | - } |
|
479 | - if ($this->debug) { |
|
480 | - \EEH_Debug_Tools::printr($datetime_identifier, '* $datetime_identifier', __FILE__, __LINE__, 1); |
|
481 | - \EEH_Debug_Tools::printr( |
|
482 | - "{$reg_limit}", |
|
483 | - 'REG LIMIT', |
|
484 | - __FILE__, |
|
485 | - __LINE__ |
|
486 | - ); |
|
487 | - } |
|
488 | - // number of allocated spaces always starts at zero |
|
489 | - $spaces_allocated = 0; |
|
490 | - $this->total_spaces[ $datetime_identifier ] = 0; |
|
491 | - foreach ($tickets as $ticket_identifier) { |
|
492 | - $spaces_allocated = $this->calculateAvailableSpacesForTicket( |
|
493 | - $datetime_identifier, |
|
494 | - $reg_limit, |
|
495 | - $ticket_identifier, |
|
496 | - $spaces_allocated |
|
497 | - ); |
|
498 | - } |
|
499 | - // spaces can't be negative |
|
500 | - $spaces_allocated = max($spaces_allocated, 0); |
|
501 | - if ($spaces_allocated) { |
|
502 | - // track any non-zero values |
|
503 | - $this->total_spaces[ $datetime_identifier ] += $spaces_allocated; |
|
504 | - if ($this->debug) { |
|
505 | - \EEH_Debug_Tools::printr((string) $spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__); |
|
506 | - } |
|
507 | - } else { |
|
508 | - if ($this->debug) { |
|
509 | - \EEH_Debug_Tools::printr(' ', ' . NO TICKETS AVAILABLE FOR DATETIME', __FILE__, __LINE__); |
|
510 | - } |
|
511 | - } |
|
512 | - if ($this->debug) { |
|
513 | - \EEH_Debug_Tools::printr( |
|
514 | - $this->total_spaces[ $datetime_identifier ], |
|
515 | - '$total_spaces', |
|
516 | - __FILE__, |
|
517 | - __LINE__ |
|
518 | - ); |
|
519 | - \EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__); |
|
520 | - \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__); |
|
521 | - } |
|
522 | - } |
|
523 | - |
|
524 | - |
|
525 | - /** |
|
526 | - * @param string $datetime_identifier |
|
527 | - * @param int $reg_limit |
|
528 | - * @param string $ticket_identifier |
|
529 | - * @param int $spaces_allocated |
|
530 | - * @return int |
|
531 | - */ |
|
532 | - private function calculateAvailableSpacesForTicket( |
|
533 | - $datetime_identifier, |
|
534 | - $reg_limit, |
|
535 | - $ticket_identifier, |
|
536 | - $spaces_allocated |
|
537 | - ) { |
|
538 | - // make sure ticket quantity is set |
|
539 | - $ticket_quantity = isset($this->ticket_quantities[ $ticket_identifier ]) |
|
540 | - ? $this->ticket_quantities[ $ticket_identifier ] |
|
541 | - : 0; |
|
542 | - if ($this->debug) { |
|
543 | - \EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__); |
|
544 | - \EEH_Debug_Tools::printr( |
|
545 | - "{$ticket_quantity}", |
|
546 | - "ticket $ticket_identifier quantity: ", |
|
547 | - __FILE__, |
|
548 | - __LINE__, |
|
549 | - 2 |
|
550 | - ); |
|
551 | - } |
|
552 | - if ($ticket_quantity) { |
|
553 | - if ($this->debug) { |
|
554 | - \EEH_Debug_Tools::printr( |
|
555 | - ($spaces_allocated <= $reg_limit) |
|
556 | - ? 'true' |
|
557 | - : 'false', |
|
558 | - ' . spaces_allocated <= reg_limit = ', |
|
559 | - __FILE__, |
|
560 | - __LINE__ |
|
561 | - ); |
|
562 | - } |
|
563 | - // if the datetime is NOT at full capacity yet |
|
564 | - if ($spaces_allocated <= $reg_limit) { |
|
565 | - // then the maximum ticket quantity we can allocate is the lowest value of either: |
|
566 | - // the number of remaining spaces for the datetime, which is the limit - spaces already taken |
|
567 | - // or the maximum ticket quantity |
|
568 | - $ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity); |
|
569 | - // adjust the available quantity in our tracking array |
|
570 | - $this->ticket_quantities[ $ticket_identifier ] -= $ticket_quantity; |
|
571 | - // and increment spaces allocated for this datetime |
|
572 | - $spaces_allocated += $ticket_quantity; |
|
573 | - $at_capacity = $spaces_allocated >= $reg_limit; |
|
574 | - if ($this->debug) { |
|
575 | - \EEH_Debug_Tools::printr( |
|
576 | - "{$ticket_quantity} {$ticket_identifier} tickets", |
|
577 | - ' > > allocate ', |
|
578 | - __FILE__, |
|
579 | - __LINE__, |
|
580 | - 3 |
|
581 | - ); |
|
582 | - if ($at_capacity) { |
|
583 | - \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__, 3); |
|
584 | - } |
|
585 | - } |
|
586 | - // now adjust all other datetimes that allow access to this ticket |
|
587 | - $this->adjustDatetimes( |
|
588 | - $datetime_identifier, |
|
589 | - $ticket_identifier, |
|
590 | - $ticket_quantity, |
|
591 | - $at_capacity |
|
592 | - ); |
|
593 | - } |
|
594 | - } |
|
595 | - return $spaces_allocated; |
|
596 | - } |
|
597 | - |
|
598 | - |
|
599 | - /** |
|
600 | - * subtracts ticket amounts from all datetime reg limits |
|
601 | - * that allow access to the ticket specified, |
|
602 | - * because that ticket could be used |
|
603 | - * to attend any of the datetimes it has access to |
|
604 | - * |
|
605 | - * @param string $datetime_identifier |
|
606 | - * @param string $ticket_identifier |
|
607 | - * @param bool $at_capacity |
|
608 | - * @param int $ticket_quantity |
|
609 | - */ |
|
610 | - private function adjustDatetimes( |
|
611 | - $datetime_identifier, |
|
612 | - $ticket_identifier, |
|
613 | - $ticket_quantity, |
|
614 | - $at_capacity |
|
615 | - ) { |
|
616 | - /** @var array $datetime_tickets */ |
|
617 | - foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) { |
|
618 | - if ($datetime_ID !== $datetime_identifier || ! is_array($datetime_tickets)) { |
|
619 | - continue; |
|
620 | - } |
|
621 | - $adjusted = $this->adjustDatetimeSpaces( |
|
622 | - $datetime_ID, |
|
623 | - $ticket_identifier, |
|
624 | - $ticket_quantity |
|
625 | - ); |
|
626 | - // skip to next ticket if nothing changed |
|
627 | - if (! ($adjusted || $at_capacity)) { |
|
628 | - continue; |
|
629 | - } |
|
630 | - // then all of it's tickets are now unavailable |
|
631 | - foreach ($datetime_tickets as $datetime_ticket) { |
|
632 | - if (($ticket_identifier === $datetime_ticket || $at_capacity) |
|
633 | - && isset($this->ticket_quantities[ $datetime_ticket ]) |
|
634 | - && $this->ticket_quantities[ $datetime_ticket ] > 0 |
|
635 | - ) { |
|
636 | - if ($this->debug) { |
|
637 | - \EEH_Debug_Tools::printr( |
|
638 | - $datetime_ticket, |
|
639 | - ' . . . adjust ticket quantities for', |
|
640 | - __FILE__, |
|
641 | - __LINE__ |
|
642 | - ); |
|
643 | - } |
|
644 | - // if this datetime is at full capacity, set any tracked available quantities to zero |
|
645 | - // otherwise just subtract the ticket quantity |
|
646 | - $new_quantity = $at_capacity |
|
647 | - ? 0 |
|
648 | - : $this->ticket_quantities[ $datetime_ticket ] - $ticket_quantity; |
|
649 | - // don't let ticket quantity go below zero |
|
650 | - $this->ticket_quantities[ $datetime_ticket ] = max($new_quantity, 0); |
|
651 | - if ($this->debug) { |
|
652 | - \EEH_Debug_Tools::printr( |
|
653 | - $at_capacity |
|
654 | - ? "0 because Datetime {$datetime_identifier} is at capacity" |
|
655 | - : "{$this->ticket_quantities[ $datetime_ticket ]}", |
|
656 | - " . . . . {$datetime_ticket} quantity set to ", |
|
657 | - __FILE__, |
|
658 | - __LINE__ |
|
659 | - ); |
|
660 | - } |
|
661 | - } |
|
662 | - // but we also need to adjust spaces for any other datetimes this ticket has access to |
|
663 | - if ($datetime_ticket === $ticket_identifier) { |
|
664 | - if (isset($this->ticket_datetimes[ $datetime_ticket ]) |
|
665 | - && is_array($this->ticket_datetimes[ $datetime_ticket ]) |
|
666 | - ) { |
|
667 | - if ($this->debug) { |
|
668 | - \EEH_Debug_Tools::printr( |
|
669 | - $datetime_ticket, |
|
670 | - ' . . adjust other Datetimes for', |
|
671 | - __FILE__, |
|
672 | - __LINE__ |
|
673 | - ); |
|
674 | - } |
|
675 | - foreach ($this->ticket_datetimes[ $datetime_ticket ] as $datetime) { |
|
676 | - // don't adjust the current datetime twice |
|
677 | - if ($datetime !== $datetime_identifier) { |
|
678 | - $this->adjustDatetimeSpaces( |
|
679 | - $datetime, |
|
680 | - $datetime_ticket, |
|
681 | - $ticket_quantity |
|
682 | - ); |
|
683 | - } |
|
684 | - } |
|
685 | - } |
|
686 | - } |
|
687 | - } |
|
688 | - } |
|
689 | - } |
|
690 | - |
|
691 | - private function adjustDatetimeSpaces($datetime_identifier, $ticket_identifier, $ticket_quantity = 0) |
|
692 | - { |
|
693 | - // does datetime have spaces available? |
|
694 | - // and does the supplied ticket have access to this datetime ? |
|
695 | - if ($this->datetime_spaces[ $datetime_identifier ] > 0 |
|
696 | - && isset($this->datetime_spaces[ $datetime_identifier ], $this->datetime_tickets[ $datetime_identifier ]) |
|
697 | - && in_array($ticket_identifier, $this->datetime_tickets[ $datetime_identifier ], true) |
|
698 | - ) { |
|
699 | - if ($this->debug) { |
|
700 | - \EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__); |
|
701 | - \EEH_Debug_Tools::printr( |
|
702 | - "{$this->datetime_spaces[ $datetime_identifier ]}", |
|
703 | - " . . current {$datetime_identifier} spaces available", |
|
704 | - __FILE__, |
|
705 | - __LINE__ |
|
706 | - ); |
|
707 | - } |
|
708 | - // then decrement the available spaces for the datetime |
|
709 | - $this->datetime_spaces[ $datetime_identifier ] -= $ticket_quantity; |
|
710 | - // but don't let quantities go below zero |
|
711 | - $this->datetime_spaces[ $datetime_identifier ] = max( |
|
712 | - $this->datetime_spaces[ $datetime_identifier ], |
|
713 | - 0 |
|
714 | - ); |
|
715 | - if ($this->debug) { |
|
716 | - \EEH_Debug_Tools::printr( |
|
717 | - "{$ticket_quantity}", |
|
718 | - " . . . {$datetime_identifier} capacity reduced by", |
|
719 | - __FILE__, |
|
720 | - __LINE__ |
|
721 | - ); |
|
722 | - } |
|
723 | - return true; |
|
724 | - } |
|
725 | - return false; |
|
726 | - } |
|
29 | + /** |
|
30 | + * @var EE_Event $event |
|
31 | + */ |
|
32 | + private $event; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var array $datetime_query_params |
|
36 | + */ |
|
37 | + private $datetime_query_params; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var EE_Ticket[] $active_tickets |
|
41 | + */ |
|
42 | + private $active_tickets = array(); |
|
43 | + |
|
44 | + /** |
|
45 | + * @var EE_Datetime[] $datetimes |
|
46 | + */ |
|
47 | + private $datetimes = array(); |
|
48 | + |
|
49 | + /** |
|
50 | + * Array of Ticket IDs grouped by Datetime |
|
51 | + * |
|
52 | + * @var array $datetimes |
|
53 | + */ |
|
54 | + private $datetime_tickets = array(); |
|
55 | + |
|
56 | + /** |
|
57 | + * Max spaces for each Datetime (reg limit - previous sold) |
|
58 | + * |
|
59 | + * @var array $datetime_spaces |
|
60 | + */ |
|
61 | + private $datetime_spaces = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * Array of Datetime IDs grouped by Ticket |
|
65 | + * |
|
66 | + * @var array[] $ticket_datetimes |
|
67 | + */ |
|
68 | + private $ticket_datetimes = array(); |
|
69 | + |
|
70 | + /** |
|
71 | + * maximum ticket quantities for each ticket (adjusted for reg limit) |
|
72 | + * |
|
73 | + * @var array $ticket_quantities |
|
74 | + */ |
|
75 | + private $ticket_quantities = array(); |
|
76 | + |
|
77 | + /** |
|
78 | + * total quantity of sold and reserved for each ticket |
|
79 | + * |
|
80 | + * @var array $tickets_sold |
|
81 | + */ |
|
82 | + private $tickets_sold = array(); |
|
83 | + |
|
84 | + /** |
|
85 | + * total spaces available across all datetimes |
|
86 | + * |
|
87 | + * @var array $total_spaces |
|
88 | + */ |
|
89 | + private $total_spaces = array(); |
|
90 | + |
|
91 | + /** |
|
92 | + * @var boolean $debug |
|
93 | + */ |
|
94 | + private $debug = false; // true false |
|
95 | + |
|
96 | + /** |
|
97 | + * @var null|int $spaces_remaining |
|
98 | + */ |
|
99 | + private $spaces_remaining; |
|
100 | + |
|
101 | + /** |
|
102 | + * @var null|int $total_spaces_available |
|
103 | + */ |
|
104 | + private $total_spaces_available; |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * EventSpacesCalculator constructor. |
|
109 | + * |
|
110 | + * @param EE_Event $event |
|
111 | + * @param array $datetime_query_params |
|
112 | + * @throws EE_Error |
|
113 | + */ |
|
114 | + public function __construct(EE_Event $event, array $datetime_query_params = array()) |
|
115 | + { |
|
116 | + $this->event = $event; |
|
117 | + $this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC')); |
|
118 | + $this->setHooks(); |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + private function setHooks() |
|
126 | + { |
|
127 | + add_action('AHEE__EE_Ticket__increase_sold', array($this, 'clearResults')); |
|
128 | + add_action('AHEE__EE_Ticket__decrease_sold', array($this, 'clearResults')); |
|
129 | + add_action('AHEE__EE_Datetime__increase_sold', array($this, 'clearResults')); |
|
130 | + add_action('AHEE__EE_Datetime__decrease_sold', array($this, 'clearResults')); |
|
131 | + add_action('AHEE__EE_Ticket__increase_reserved', array($this, 'clearResults')); |
|
132 | + add_action('AHEE__EE_Ticket__decrease_reserved', array($this, 'clearResults')); |
|
133 | + add_action('AHEE__EE_Datetime__increase_reserved', array($this, 'clearResults')); |
|
134 | + add_action('AHEE__EE_Datetime__decrease_reserved', array($this, 'clearResults')); |
|
135 | + } |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * @return void |
|
140 | + */ |
|
141 | + public function clearResults() |
|
142 | + { |
|
143 | + $this->spaces_remaining = null; |
|
144 | + $this->total_spaces_available = null; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * @return EE_Ticket[] |
|
150 | + * @throws EE_Error |
|
151 | + * @throws InvalidDataTypeException |
|
152 | + * @throws InvalidInterfaceException |
|
153 | + * @throws InvalidArgumentException |
|
154 | + */ |
|
155 | + public function getActiveTickets() |
|
156 | + { |
|
157 | + if (empty($this->active_tickets)) { |
|
158 | + $this->active_tickets = $this->event->tickets( |
|
159 | + array( |
|
160 | + array('TKT_deleted' => false), |
|
161 | + 'order_by' => array('TKT_qty' => 'ASC'), |
|
162 | + ) |
|
163 | + ); |
|
164 | + } |
|
165 | + return $this->active_tickets; |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * @param EE_Ticket[] $active_tickets |
|
171 | + * @throws EE_Error |
|
172 | + * @throws DomainException |
|
173 | + * @throws UnexpectedEntityException |
|
174 | + */ |
|
175 | + public function setActiveTickets(array $active_tickets = array()) |
|
176 | + { |
|
177 | + if (! empty($active_tickets)) { |
|
178 | + foreach ($active_tickets as $active_ticket) { |
|
179 | + $this->validateTicket($active_ticket); |
|
180 | + } |
|
181 | + // sort incoming array by ticket quantity (asc) |
|
182 | + usort( |
|
183 | + $active_tickets, |
|
184 | + function (EE_Ticket $a, EE_Ticket $b) { |
|
185 | + if ($a->qty() === $b->qty()) { |
|
186 | + return 0; |
|
187 | + } |
|
188 | + return ($a->qty() < $b->qty()) |
|
189 | + ? -1 |
|
190 | + : 1; |
|
191 | + } |
|
192 | + ); |
|
193 | + } |
|
194 | + $this->active_tickets = $active_tickets; |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * @param $ticket |
|
200 | + * @throws DomainException |
|
201 | + * @throws EE_Error |
|
202 | + * @throws UnexpectedEntityException |
|
203 | + */ |
|
204 | + private function validateTicket($ticket) |
|
205 | + { |
|
206 | + if (! $ticket instanceof EE_Ticket) { |
|
207 | + throw new DomainException( |
|
208 | + esc_html__( |
|
209 | + 'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.', |
|
210 | + 'event_espresso' |
|
211 | + ) |
|
212 | + ); |
|
213 | + } |
|
214 | + if ($ticket->get_event_ID() !== $this->event->ID()) { |
|
215 | + throw new DomainException( |
|
216 | + sprintf( |
|
217 | + esc_html__( |
|
218 | + 'An EE_Ticket for Event %1$d was supplied while calculating event space availability for Event %2$d.', |
|
219 | + 'event_espresso' |
|
220 | + ), |
|
221 | + $ticket->get_event_ID(), |
|
222 | + $this->event->ID() |
|
223 | + ) |
|
224 | + ); |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + |
|
229 | + /** |
|
230 | + * @return EE_Datetime[] |
|
231 | + */ |
|
232 | + public function getDatetimes() |
|
233 | + { |
|
234 | + return $this->datetimes; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * @param EE_Datetime $datetime |
|
240 | + * @throws EE_Error |
|
241 | + * @throws DomainException |
|
242 | + */ |
|
243 | + public function setDatetime(EE_Datetime $datetime) |
|
244 | + { |
|
245 | + if ($datetime->event()->ID() !== $this->event->ID()) { |
|
246 | + throw new DomainException( |
|
247 | + sprintf( |
|
248 | + esc_html__( |
|
249 | + 'An EE_Datetime for Event %1$d was supplied while calculating event space availability for Event %2$d.', |
|
250 | + 'event_espresso' |
|
251 | + ), |
|
252 | + $datetime->event()->ID(), |
|
253 | + $this->event->ID() |
|
254 | + ) |
|
255 | + ); |
|
256 | + } |
|
257 | + $this->datetimes[ $datetime->ID() ] = $datetime; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * calculate spaces remaining based on "saleable" tickets |
|
263 | + * |
|
264 | + * @return float|int |
|
265 | + * @throws EE_Error |
|
266 | + * @throws DomainException |
|
267 | + * @throws UnexpectedEntityException |
|
268 | + * @throws InvalidDataTypeException |
|
269 | + * @throws InvalidInterfaceException |
|
270 | + * @throws InvalidArgumentException |
|
271 | + */ |
|
272 | + public function spacesRemaining() |
|
273 | + { |
|
274 | + if ($this->spaces_remaining === null) { |
|
275 | + $this->initialize(); |
|
276 | + $this->spaces_remaining = $this->calculate(); |
|
277 | + } |
|
278 | + return $this->spaces_remaining; |
|
279 | + } |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * calculates total available spaces for an event with no regard for sold tickets |
|
284 | + * |
|
285 | + * @return int|float |
|
286 | + * @throws EE_Error |
|
287 | + * @throws DomainException |
|
288 | + * @throws UnexpectedEntityException |
|
289 | + * @throws InvalidDataTypeException |
|
290 | + * @throws InvalidInterfaceException |
|
291 | + * @throws InvalidArgumentException |
|
292 | + */ |
|
293 | + public function totalSpacesAvailable() |
|
294 | + { |
|
295 | + if ($this->total_spaces_available === null) { |
|
296 | + $this->initialize(); |
|
297 | + $this->total_spaces_available = $this->calculate(false); |
|
298 | + } |
|
299 | + return $this->total_spaces_available; |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + /** |
|
304 | + * Loops through the active tickets for the event |
|
305 | + * and builds a series of data arrays that will be used for calculating |
|
306 | + * the total maximum available spaces, as well as the spaces remaining. |
|
307 | + * Because ticket quantities affect datetime spaces and vice versa, |
|
308 | + * we need to be constantly updating these data arrays as things change, |
|
309 | + * which is the entire reason for their existence. |
|
310 | + * |
|
311 | + * @throws EE_Error |
|
312 | + * @throws DomainException |
|
313 | + * @throws UnexpectedEntityException |
|
314 | + * @throws InvalidDataTypeException |
|
315 | + * @throws InvalidInterfaceException |
|
316 | + * @throws InvalidArgumentException |
|
317 | + */ |
|
318 | + private function initialize() |
|
319 | + { |
|
320 | + if ($this->debug) { |
|
321 | + \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
322 | + } |
|
323 | + $this->datetime_tickets = array(); |
|
324 | + $this->datetime_spaces = array(); |
|
325 | + $this->ticket_datetimes = array(); |
|
326 | + $this->ticket_quantities = array(); |
|
327 | + $this->tickets_sold = array(); |
|
328 | + $this->total_spaces = array(); |
|
329 | + $active_tickets = $this->getActiveTickets(); |
|
330 | + if (! empty($active_tickets)) { |
|
331 | + foreach ($active_tickets as $ticket) { |
|
332 | + $this->validateTicket($ticket); |
|
333 | + // we need to index our data arrays using strings for the purpose of sorting, |
|
334 | + // but we also need them to be unique, so we'll just prepend a letter T to the ID |
|
335 | + $ticket_identifier = "T{$ticket->ID()}"; |
|
336 | + // to start, we'll just consider the raw qty to be the maximum availability for this ticket, |
|
337 | + // unless the ticket is past its "sell until" date, in which case the qty will be 0 |
|
338 | + $max_tickets = $ticket->is_expired() ? 0 : $ticket->qty(); |
|
339 | + // but we'll adjust that after looping over each datetime for the ticket and checking reg limits |
|
340 | + $ticket_datetimes = $ticket->datetimes($this->datetime_query_params); |
|
341 | + foreach ($ticket_datetimes as $datetime) { |
|
342 | + // save all datetimes |
|
343 | + $this->setDatetime($datetime); |
|
344 | + $datetime_identifier = "D{$datetime->ID()}"; |
|
345 | + $reg_limit = $datetime->reg_limit(); |
|
346 | + // ticket quantity can not exceed datetime reg limit |
|
347 | + $max_tickets = min($max_tickets, $reg_limit); |
|
348 | + // as described earlier, because we need to be able to constantly adjust numbers for things, |
|
349 | + // we are going to move all of our data into the following arrays: |
|
350 | + // datetime spaces initially represents the reg limit for each datetime, |
|
351 | + // but this will get adjusted as tickets are accounted for |
|
352 | + $this->datetime_spaces[ $datetime_identifier ] = $reg_limit; |
|
353 | + // just an array of ticket IDs grouped by datetime |
|
354 | + $this->datetime_tickets[ $datetime_identifier ][] = $ticket_identifier; |
|
355 | + // and an array of datetime IDs grouped by ticket |
|
356 | + $this->ticket_datetimes[ $ticket_identifier ][] = $datetime_identifier; |
|
357 | + } |
|
358 | + // total quantity of sold and reserved for each ticket |
|
359 | + $this->tickets_sold[ $ticket_identifier ] = $ticket->sold() + $ticket->reserved(); |
|
360 | + // and the maximum ticket quantities for each ticket (adjusted for reg limit) |
|
361 | + $this->ticket_quantities[ $ticket_identifier ] = $max_tickets; |
|
362 | + } |
|
363 | + } |
|
364 | + // sort datetime spaces by reg limit, but maintain our string indexes |
|
365 | + asort($this->datetime_spaces, SORT_NUMERIC); |
|
366 | + // datetime tickets need to be sorted in the SAME order as the above array... |
|
367 | + // so we'll just use array_merge() to take the structure of datetime_spaces |
|
368 | + // but overwrite all of the data with that from datetime_tickets |
|
369 | + $this->datetime_tickets = array_merge( |
|
370 | + $this->datetime_spaces, |
|
371 | + $this->datetime_tickets |
|
372 | + ); |
|
373 | + if ($this->debug) { |
|
374 | + \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__); |
|
375 | + \EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__); |
|
376 | + \EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__); |
|
377 | + } |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + /** |
|
382 | + * performs calculations on initialized data |
|
383 | + * |
|
384 | + * @param bool $consider_sold |
|
385 | + * @return int|float |
|
386 | + */ |
|
387 | + private function calculate($consider_sold = true) |
|
388 | + { |
|
389 | + if ($this->debug) { |
|
390 | + \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
391 | + \EEH_Debug_Tools::printr($consider_sold, '$consider_sold', __FILE__, __LINE__); |
|
392 | + } |
|
393 | + if ($consider_sold) { |
|
394 | + // subtract amounts sold from all ticket quantities and datetime spaces |
|
395 | + $this->adjustTicketQuantitiesDueToSales(); |
|
396 | + } |
|
397 | + foreach ($this->datetime_tickets as $datetime_identifier => $tickets) { |
|
398 | + $this->trackAvailableSpacesForDatetimes($datetime_identifier, $tickets); |
|
399 | + } |
|
400 | + // total spaces available is just the sum of the spaces available for each datetime |
|
401 | + $spaces_remaining = array_sum($this->total_spaces); |
|
402 | + if ($this->debug) { |
|
403 | + \EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__); |
|
404 | + \EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__); |
|
405 | + \EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__); |
|
406 | + } |
|
407 | + return $spaces_remaining; |
|
408 | + } |
|
409 | + |
|
410 | + |
|
411 | + /** |
|
412 | + * subtracts amount of tickets sold from ticket quantities and datetime spaces |
|
413 | + */ |
|
414 | + private function adjustTicketQuantitiesDueToSales() |
|
415 | + { |
|
416 | + if ($this->debug) { |
|
417 | + \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
418 | + } |
|
419 | + foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) { |
|
420 | + if (isset($this->ticket_quantities[ $ticket_identifier ])) { |
|
421 | + $this->ticket_quantities[ $ticket_identifier ] -= $tickets_sold; |
|
422 | + // don't let values go below zero |
|
423 | + $this->ticket_quantities[ $ticket_identifier ] = max( |
|
424 | + $this->ticket_quantities[ $ticket_identifier ], |
|
425 | + 0 |
|
426 | + ); |
|
427 | + if ($this->debug) { |
|
428 | + \EEH_Debug_Tools::printr( |
|
429 | + "{$tickets_sold} sales for ticket {$ticket_identifier} ", |
|
430 | + 'subtracting', |
|
431 | + __FILE__, |
|
432 | + __LINE__ |
|
433 | + ); |
|
434 | + } |
|
435 | + } |
|
436 | + if (isset($this->ticket_datetimes[ $ticket_identifier ]) |
|
437 | + && is_array($this->ticket_datetimes[ $ticket_identifier ]) |
|
438 | + ) { |
|
439 | + foreach ($this->ticket_datetimes[ $ticket_identifier ] as $ticket_datetime) { |
|
440 | + if (isset($this->ticket_quantities[ $ticket_identifier ])) { |
|
441 | + $this->datetime_spaces[ $ticket_datetime ] -= $tickets_sold; |
|
442 | + // don't let values go below zero |
|
443 | + $this->datetime_spaces[ $ticket_datetime ] = max( |
|
444 | + $this->datetime_spaces[ $ticket_datetime ], |
|
445 | + 0 |
|
446 | + ); |
|
447 | + if ($this->debug) { |
|
448 | + \EEH_Debug_Tools::printr( |
|
449 | + "{$tickets_sold} sales for datetime {$ticket_datetime} ", |
|
450 | + 'subtracting', |
|
451 | + __FILE__, |
|
452 | + __LINE__ |
|
453 | + ); |
|
454 | + } |
|
455 | + } |
|
456 | + } |
|
457 | + } |
|
458 | + } |
|
459 | + } |
|
460 | + |
|
461 | + |
|
462 | + /** |
|
463 | + * @param string $datetime_identifier |
|
464 | + * @param array $tickets |
|
465 | + */ |
|
466 | + private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets) |
|
467 | + { |
|
468 | + // make sure a reg limit is set for the datetime |
|
469 | + $reg_limit = isset($this->datetime_spaces[ $datetime_identifier ]) |
|
470 | + ? $this->datetime_spaces[ $datetime_identifier ] |
|
471 | + : 0; |
|
472 | + // and bail if it is not |
|
473 | + if (! $reg_limit) { |
|
474 | + if ($this->debug) { |
|
475 | + \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__); |
|
476 | + } |
|
477 | + return; |
|
478 | + } |
|
479 | + if ($this->debug) { |
|
480 | + \EEH_Debug_Tools::printr($datetime_identifier, '* $datetime_identifier', __FILE__, __LINE__, 1); |
|
481 | + \EEH_Debug_Tools::printr( |
|
482 | + "{$reg_limit}", |
|
483 | + 'REG LIMIT', |
|
484 | + __FILE__, |
|
485 | + __LINE__ |
|
486 | + ); |
|
487 | + } |
|
488 | + // number of allocated spaces always starts at zero |
|
489 | + $spaces_allocated = 0; |
|
490 | + $this->total_spaces[ $datetime_identifier ] = 0; |
|
491 | + foreach ($tickets as $ticket_identifier) { |
|
492 | + $spaces_allocated = $this->calculateAvailableSpacesForTicket( |
|
493 | + $datetime_identifier, |
|
494 | + $reg_limit, |
|
495 | + $ticket_identifier, |
|
496 | + $spaces_allocated |
|
497 | + ); |
|
498 | + } |
|
499 | + // spaces can't be negative |
|
500 | + $spaces_allocated = max($spaces_allocated, 0); |
|
501 | + if ($spaces_allocated) { |
|
502 | + // track any non-zero values |
|
503 | + $this->total_spaces[ $datetime_identifier ] += $spaces_allocated; |
|
504 | + if ($this->debug) { |
|
505 | + \EEH_Debug_Tools::printr((string) $spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__); |
|
506 | + } |
|
507 | + } else { |
|
508 | + if ($this->debug) { |
|
509 | + \EEH_Debug_Tools::printr(' ', ' . NO TICKETS AVAILABLE FOR DATETIME', __FILE__, __LINE__); |
|
510 | + } |
|
511 | + } |
|
512 | + if ($this->debug) { |
|
513 | + \EEH_Debug_Tools::printr( |
|
514 | + $this->total_spaces[ $datetime_identifier ], |
|
515 | + '$total_spaces', |
|
516 | + __FILE__, |
|
517 | + __LINE__ |
|
518 | + ); |
|
519 | + \EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__); |
|
520 | + \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__); |
|
521 | + } |
|
522 | + } |
|
523 | + |
|
524 | + |
|
525 | + /** |
|
526 | + * @param string $datetime_identifier |
|
527 | + * @param int $reg_limit |
|
528 | + * @param string $ticket_identifier |
|
529 | + * @param int $spaces_allocated |
|
530 | + * @return int |
|
531 | + */ |
|
532 | + private function calculateAvailableSpacesForTicket( |
|
533 | + $datetime_identifier, |
|
534 | + $reg_limit, |
|
535 | + $ticket_identifier, |
|
536 | + $spaces_allocated |
|
537 | + ) { |
|
538 | + // make sure ticket quantity is set |
|
539 | + $ticket_quantity = isset($this->ticket_quantities[ $ticket_identifier ]) |
|
540 | + ? $this->ticket_quantities[ $ticket_identifier ] |
|
541 | + : 0; |
|
542 | + if ($this->debug) { |
|
543 | + \EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__); |
|
544 | + \EEH_Debug_Tools::printr( |
|
545 | + "{$ticket_quantity}", |
|
546 | + "ticket $ticket_identifier quantity: ", |
|
547 | + __FILE__, |
|
548 | + __LINE__, |
|
549 | + 2 |
|
550 | + ); |
|
551 | + } |
|
552 | + if ($ticket_quantity) { |
|
553 | + if ($this->debug) { |
|
554 | + \EEH_Debug_Tools::printr( |
|
555 | + ($spaces_allocated <= $reg_limit) |
|
556 | + ? 'true' |
|
557 | + : 'false', |
|
558 | + ' . spaces_allocated <= reg_limit = ', |
|
559 | + __FILE__, |
|
560 | + __LINE__ |
|
561 | + ); |
|
562 | + } |
|
563 | + // if the datetime is NOT at full capacity yet |
|
564 | + if ($spaces_allocated <= $reg_limit) { |
|
565 | + // then the maximum ticket quantity we can allocate is the lowest value of either: |
|
566 | + // the number of remaining spaces for the datetime, which is the limit - spaces already taken |
|
567 | + // or the maximum ticket quantity |
|
568 | + $ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity); |
|
569 | + // adjust the available quantity in our tracking array |
|
570 | + $this->ticket_quantities[ $ticket_identifier ] -= $ticket_quantity; |
|
571 | + // and increment spaces allocated for this datetime |
|
572 | + $spaces_allocated += $ticket_quantity; |
|
573 | + $at_capacity = $spaces_allocated >= $reg_limit; |
|
574 | + if ($this->debug) { |
|
575 | + \EEH_Debug_Tools::printr( |
|
576 | + "{$ticket_quantity} {$ticket_identifier} tickets", |
|
577 | + ' > > allocate ', |
|
578 | + __FILE__, |
|
579 | + __LINE__, |
|
580 | + 3 |
|
581 | + ); |
|
582 | + if ($at_capacity) { |
|
583 | + \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__, 3); |
|
584 | + } |
|
585 | + } |
|
586 | + // now adjust all other datetimes that allow access to this ticket |
|
587 | + $this->adjustDatetimes( |
|
588 | + $datetime_identifier, |
|
589 | + $ticket_identifier, |
|
590 | + $ticket_quantity, |
|
591 | + $at_capacity |
|
592 | + ); |
|
593 | + } |
|
594 | + } |
|
595 | + return $spaces_allocated; |
|
596 | + } |
|
597 | + |
|
598 | + |
|
599 | + /** |
|
600 | + * subtracts ticket amounts from all datetime reg limits |
|
601 | + * that allow access to the ticket specified, |
|
602 | + * because that ticket could be used |
|
603 | + * to attend any of the datetimes it has access to |
|
604 | + * |
|
605 | + * @param string $datetime_identifier |
|
606 | + * @param string $ticket_identifier |
|
607 | + * @param bool $at_capacity |
|
608 | + * @param int $ticket_quantity |
|
609 | + */ |
|
610 | + private function adjustDatetimes( |
|
611 | + $datetime_identifier, |
|
612 | + $ticket_identifier, |
|
613 | + $ticket_quantity, |
|
614 | + $at_capacity |
|
615 | + ) { |
|
616 | + /** @var array $datetime_tickets */ |
|
617 | + foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) { |
|
618 | + if ($datetime_ID !== $datetime_identifier || ! is_array($datetime_tickets)) { |
|
619 | + continue; |
|
620 | + } |
|
621 | + $adjusted = $this->adjustDatetimeSpaces( |
|
622 | + $datetime_ID, |
|
623 | + $ticket_identifier, |
|
624 | + $ticket_quantity |
|
625 | + ); |
|
626 | + // skip to next ticket if nothing changed |
|
627 | + if (! ($adjusted || $at_capacity)) { |
|
628 | + continue; |
|
629 | + } |
|
630 | + // then all of it's tickets are now unavailable |
|
631 | + foreach ($datetime_tickets as $datetime_ticket) { |
|
632 | + if (($ticket_identifier === $datetime_ticket || $at_capacity) |
|
633 | + && isset($this->ticket_quantities[ $datetime_ticket ]) |
|
634 | + && $this->ticket_quantities[ $datetime_ticket ] > 0 |
|
635 | + ) { |
|
636 | + if ($this->debug) { |
|
637 | + \EEH_Debug_Tools::printr( |
|
638 | + $datetime_ticket, |
|
639 | + ' . . . adjust ticket quantities for', |
|
640 | + __FILE__, |
|
641 | + __LINE__ |
|
642 | + ); |
|
643 | + } |
|
644 | + // if this datetime is at full capacity, set any tracked available quantities to zero |
|
645 | + // otherwise just subtract the ticket quantity |
|
646 | + $new_quantity = $at_capacity |
|
647 | + ? 0 |
|
648 | + : $this->ticket_quantities[ $datetime_ticket ] - $ticket_quantity; |
|
649 | + // don't let ticket quantity go below zero |
|
650 | + $this->ticket_quantities[ $datetime_ticket ] = max($new_quantity, 0); |
|
651 | + if ($this->debug) { |
|
652 | + \EEH_Debug_Tools::printr( |
|
653 | + $at_capacity |
|
654 | + ? "0 because Datetime {$datetime_identifier} is at capacity" |
|
655 | + : "{$this->ticket_quantities[ $datetime_ticket ]}", |
|
656 | + " . . . . {$datetime_ticket} quantity set to ", |
|
657 | + __FILE__, |
|
658 | + __LINE__ |
|
659 | + ); |
|
660 | + } |
|
661 | + } |
|
662 | + // but we also need to adjust spaces for any other datetimes this ticket has access to |
|
663 | + if ($datetime_ticket === $ticket_identifier) { |
|
664 | + if (isset($this->ticket_datetimes[ $datetime_ticket ]) |
|
665 | + && is_array($this->ticket_datetimes[ $datetime_ticket ]) |
|
666 | + ) { |
|
667 | + if ($this->debug) { |
|
668 | + \EEH_Debug_Tools::printr( |
|
669 | + $datetime_ticket, |
|
670 | + ' . . adjust other Datetimes for', |
|
671 | + __FILE__, |
|
672 | + __LINE__ |
|
673 | + ); |
|
674 | + } |
|
675 | + foreach ($this->ticket_datetimes[ $datetime_ticket ] as $datetime) { |
|
676 | + // don't adjust the current datetime twice |
|
677 | + if ($datetime !== $datetime_identifier) { |
|
678 | + $this->adjustDatetimeSpaces( |
|
679 | + $datetime, |
|
680 | + $datetime_ticket, |
|
681 | + $ticket_quantity |
|
682 | + ); |
|
683 | + } |
|
684 | + } |
|
685 | + } |
|
686 | + } |
|
687 | + } |
|
688 | + } |
|
689 | + } |
|
690 | + |
|
691 | + private function adjustDatetimeSpaces($datetime_identifier, $ticket_identifier, $ticket_quantity = 0) |
|
692 | + { |
|
693 | + // does datetime have spaces available? |
|
694 | + // and does the supplied ticket have access to this datetime ? |
|
695 | + if ($this->datetime_spaces[ $datetime_identifier ] > 0 |
|
696 | + && isset($this->datetime_spaces[ $datetime_identifier ], $this->datetime_tickets[ $datetime_identifier ]) |
|
697 | + && in_array($ticket_identifier, $this->datetime_tickets[ $datetime_identifier ], true) |
|
698 | + ) { |
|
699 | + if ($this->debug) { |
|
700 | + \EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__); |
|
701 | + \EEH_Debug_Tools::printr( |
|
702 | + "{$this->datetime_spaces[ $datetime_identifier ]}", |
|
703 | + " . . current {$datetime_identifier} spaces available", |
|
704 | + __FILE__, |
|
705 | + __LINE__ |
|
706 | + ); |
|
707 | + } |
|
708 | + // then decrement the available spaces for the datetime |
|
709 | + $this->datetime_spaces[ $datetime_identifier ] -= $ticket_quantity; |
|
710 | + // but don't let quantities go below zero |
|
711 | + $this->datetime_spaces[ $datetime_identifier ] = max( |
|
712 | + $this->datetime_spaces[ $datetime_identifier ], |
|
713 | + 0 |
|
714 | + ); |
|
715 | + if ($this->debug) { |
|
716 | + \EEH_Debug_Tools::printr( |
|
717 | + "{$ticket_quantity}", |
|
718 | + " . . . {$datetime_identifier} capacity reduced by", |
|
719 | + __FILE__, |
|
720 | + __LINE__ |
|
721 | + ); |
|
722 | + } |
|
723 | + return true; |
|
724 | + } |
|
725 | + return false; |
|
726 | + } |
|
727 | 727 | } |
@@ -174,14 +174,14 @@ discard block |
||
174 | 174 | */ |
175 | 175 | public function setActiveTickets(array $active_tickets = array()) |
176 | 176 | { |
177 | - if (! empty($active_tickets)) { |
|
177 | + if ( ! empty($active_tickets)) { |
|
178 | 178 | foreach ($active_tickets as $active_ticket) { |
179 | 179 | $this->validateTicket($active_ticket); |
180 | 180 | } |
181 | 181 | // sort incoming array by ticket quantity (asc) |
182 | 182 | usort( |
183 | 183 | $active_tickets, |
184 | - function (EE_Ticket $a, EE_Ticket $b) { |
|
184 | + function(EE_Ticket $a, EE_Ticket $b) { |
|
185 | 185 | if ($a->qty() === $b->qty()) { |
186 | 186 | return 0; |
187 | 187 | } |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | */ |
204 | 204 | private function validateTicket($ticket) |
205 | 205 | { |
206 | - if (! $ticket instanceof EE_Ticket) { |
|
206 | + if ( ! $ticket instanceof EE_Ticket) { |
|
207 | 207 | throw new DomainException( |
208 | 208 | esc_html__( |
209 | 209 | 'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.', |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | ) |
255 | 255 | ); |
256 | 256 | } |
257 | - $this->datetimes[ $datetime->ID() ] = $datetime; |
|
257 | + $this->datetimes[$datetime->ID()] = $datetime; |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | $this->tickets_sold = array(); |
328 | 328 | $this->total_spaces = array(); |
329 | 329 | $active_tickets = $this->getActiveTickets(); |
330 | - if (! empty($active_tickets)) { |
|
330 | + if ( ! empty($active_tickets)) { |
|
331 | 331 | foreach ($active_tickets as $ticket) { |
332 | 332 | $this->validateTicket($ticket); |
333 | 333 | // we need to index our data arrays using strings for the purpose of sorting, |
@@ -349,16 +349,16 @@ discard block |
||
349 | 349 | // we are going to move all of our data into the following arrays: |
350 | 350 | // datetime spaces initially represents the reg limit for each datetime, |
351 | 351 | // but this will get adjusted as tickets are accounted for |
352 | - $this->datetime_spaces[ $datetime_identifier ] = $reg_limit; |
|
352 | + $this->datetime_spaces[$datetime_identifier] = $reg_limit; |
|
353 | 353 | // just an array of ticket IDs grouped by datetime |
354 | - $this->datetime_tickets[ $datetime_identifier ][] = $ticket_identifier; |
|
354 | + $this->datetime_tickets[$datetime_identifier][] = $ticket_identifier; |
|
355 | 355 | // and an array of datetime IDs grouped by ticket |
356 | - $this->ticket_datetimes[ $ticket_identifier ][] = $datetime_identifier; |
|
356 | + $this->ticket_datetimes[$ticket_identifier][] = $datetime_identifier; |
|
357 | 357 | } |
358 | 358 | // total quantity of sold and reserved for each ticket |
359 | - $this->tickets_sold[ $ticket_identifier ] = $ticket->sold() + $ticket->reserved(); |
|
359 | + $this->tickets_sold[$ticket_identifier] = $ticket->sold() + $ticket->reserved(); |
|
360 | 360 | // and the maximum ticket quantities for each ticket (adjusted for reg limit) |
361 | - $this->ticket_quantities[ $ticket_identifier ] = $max_tickets; |
|
361 | + $this->ticket_quantities[$ticket_identifier] = $max_tickets; |
|
362 | 362 | } |
363 | 363 | } |
364 | 364 | // sort datetime spaces by reg limit, but maintain our string indexes |
@@ -417,11 +417,11 @@ discard block |
||
417 | 417 | \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
418 | 418 | } |
419 | 419 | foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) { |
420 | - if (isset($this->ticket_quantities[ $ticket_identifier ])) { |
|
421 | - $this->ticket_quantities[ $ticket_identifier ] -= $tickets_sold; |
|
420 | + if (isset($this->ticket_quantities[$ticket_identifier])) { |
|
421 | + $this->ticket_quantities[$ticket_identifier] -= $tickets_sold; |
|
422 | 422 | // don't let values go below zero |
423 | - $this->ticket_quantities[ $ticket_identifier ] = max( |
|
424 | - $this->ticket_quantities[ $ticket_identifier ], |
|
423 | + $this->ticket_quantities[$ticket_identifier] = max( |
|
424 | + $this->ticket_quantities[$ticket_identifier], |
|
425 | 425 | 0 |
426 | 426 | ); |
427 | 427 | if ($this->debug) { |
@@ -433,15 +433,15 @@ discard block |
||
433 | 433 | ); |
434 | 434 | } |
435 | 435 | } |
436 | - if (isset($this->ticket_datetimes[ $ticket_identifier ]) |
|
437 | - && is_array($this->ticket_datetimes[ $ticket_identifier ]) |
|
436 | + if (isset($this->ticket_datetimes[$ticket_identifier]) |
|
437 | + && is_array($this->ticket_datetimes[$ticket_identifier]) |
|
438 | 438 | ) { |
439 | - foreach ($this->ticket_datetimes[ $ticket_identifier ] as $ticket_datetime) { |
|
440 | - if (isset($this->ticket_quantities[ $ticket_identifier ])) { |
|
441 | - $this->datetime_spaces[ $ticket_datetime ] -= $tickets_sold; |
|
439 | + foreach ($this->ticket_datetimes[$ticket_identifier] as $ticket_datetime) { |
|
440 | + if (isset($this->ticket_quantities[$ticket_identifier])) { |
|
441 | + $this->datetime_spaces[$ticket_datetime] -= $tickets_sold; |
|
442 | 442 | // don't let values go below zero |
443 | - $this->datetime_spaces[ $ticket_datetime ] = max( |
|
444 | - $this->datetime_spaces[ $ticket_datetime ], |
|
443 | + $this->datetime_spaces[$ticket_datetime] = max( |
|
444 | + $this->datetime_spaces[$ticket_datetime], |
|
445 | 445 | 0 |
446 | 446 | ); |
447 | 447 | if ($this->debug) { |
@@ -466,11 +466,11 @@ discard block |
||
466 | 466 | private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets) |
467 | 467 | { |
468 | 468 | // make sure a reg limit is set for the datetime |
469 | - $reg_limit = isset($this->datetime_spaces[ $datetime_identifier ]) |
|
470 | - ? $this->datetime_spaces[ $datetime_identifier ] |
|
469 | + $reg_limit = isset($this->datetime_spaces[$datetime_identifier]) |
|
470 | + ? $this->datetime_spaces[$datetime_identifier] |
|
471 | 471 | : 0; |
472 | 472 | // and bail if it is not |
473 | - if (! $reg_limit) { |
|
473 | + if ( ! $reg_limit) { |
|
474 | 474 | if ($this->debug) { |
475 | 475 | \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__); |
476 | 476 | } |
@@ -487,7 +487,7 @@ discard block |
||
487 | 487 | } |
488 | 488 | // number of allocated spaces always starts at zero |
489 | 489 | $spaces_allocated = 0; |
490 | - $this->total_spaces[ $datetime_identifier ] = 0; |
|
490 | + $this->total_spaces[$datetime_identifier] = 0; |
|
491 | 491 | foreach ($tickets as $ticket_identifier) { |
492 | 492 | $spaces_allocated = $this->calculateAvailableSpacesForTicket( |
493 | 493 | $datetime_identifier, |
@@ -500,7 +500,7 @@ discard block |
||
500 | 500 | $spaces_allocated = max($spaces_allocated, 0); |
501 | 501 | if ($spaces_allocated) { |
502 | 502 | // track any non-zero values |
503 | - $this->total_spaces[ $datetime_identifier ] += $spaces_allocated; |
|
503 | + $this->total_spaces[$datetime_identifier] += $spaces_allocated; |
|
504 | 504 | if ($this->debug) { |
505 | 505 | \EEH_Debug_Tools::printr((string) $spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__); |
506 | 506 | } |
@@ -511,7 +511,7 @@ discard block |
||
511 | 511 | } |
512 | 512 | if ($this->debug) { |
513 | 513 | \EEH_Debug_Tools::printr( |
514 | - $this->total_spaces[ $datetime_identifier ], |
|
514 | + $this->total_spaces[$datetime_identifier], |
|
515 | 515 | '$total_spaces', |
516 | 516 | __FILE__, |
517 | 517 | __LINE__ |
@@ -536,8 +536,8 @@ discard block |
||
536 | 536 | $spaces_allocated |
537 | 537 | ) { |
538 | 538 | // make sure ticket quantity is set |
539 | - $ticket_quantity = isset($this->ticket_quantities[ $ticket_identifier ]) |
|
540 | - ? $this->ticket_quantities[ $ticket_identifier ] |
|
539 | + $ticket_quantity = isset($this->ticket_quantities[$ticket_identifier]) |
|
540 | + ? $this->ticket_quantities[$ticket_identifier] |
|
541 | 541 | : 0; |
542 | 542 | if ($this->debug) { |
543 | 543 | \EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__); |
@@ -567,7 +567,7 @@ discard block |
||
567 | 567 | // or the maximum ticket quantity |
568 | 568 | $ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity); |
569 | 569 | // adjust the available quantity in our tracking array |
570 | - $this->ticket_quantities[ $ticket_identifier ] -= $ticket_quantity; |
|
570 | + $this->ticket_quantities[$ticket_identifier] -= $ticket_quantity; |
|
571 | 571 | // and increment spaces allocated for this datetime |
572 | 572 | $spaces_allocated += $ticket_quantity; |
573 | 573 | $at_capacity = $spaces_allocated >= $reg_limit; |
@@ -624,14 +624,14 @@ discard block |
||
624 | 624 | $ticket_quantity |
625 | 625 | ); |
626 | 626 | // skip to next ticket if nothing changed |
627 | - if (! ($adjusted || $at_capacity)) { |
|
627 | + if ( ! ($adjusted || $at_capacity)) { |
|
628 | 628 | continue; |
629 | 629 | } |
630 | 630 | // then all of it's tickets are now unavailable |
631 | 631 | foreach ($datetime_tickets as $datetime_ticket) { |
632 | 632 | if (($ticket_identifier === $datetime_ticket || $at_capacity) |
633 | - && isset($this->ticket_quantities[ $datetime_ticket ]) |
|
634 | - && $this->ticket_quantities[ $datetime_ticket ] > 0 |
|
633 | + && isset($this->ticket_quantities[$datetime_ticket]) |
|
634 | + && $this->ticket_quantities[$datetime_ticket] > 0 |
|
635 | 635 | ) { |
636 | 636 | if ($this->debug) { |
637 | 637 | \EEH_Debug_Tools::printr( |
@@ -645,14 +645,14 @@ discard block |
||
645 | 645 | // otherwise just subtract the ticket quantity |
646 | 646 | $new_quantity = $at_capacity |
647 | 647 | ? 0 |
648 | - : $this->ticket_quantities[ $datetime_ticket ] - $ticket_quantity; |
|
648 | + : $this->ticket_quantities[$datetime_ticket] - $ticket_quantity; |
|
649 | 649 | // don't let ticket quantity go below zero |
650 | - $this->ticket_quantities[ $datetime_ticket ] = max($new_quantity, 0); |
|
650 | + $this->ticket_quantities[$datetime_ticket] = max($new_quantity, 0); |
|
651 | 651 | if ($this->debug) { |
652 | 652 | \EEH_Debug_Tools::printr( |
653 | 653 | $at_capacity |
654 | 654 | ? "0 because Datetime {$datetime_identifier} is at capacity" |
655 | - : "{$this->ticket_quantities[ $datetime_ticket ]}", |
|
655 | + : "{$this->ticket_quantities[$datetime_ticket]}", |
|
656 | 656 | " . . . . {$datetime_ticket} quantity set to ", |
657 | 657 | __FILE__, |
658 | 658 | __LINE__ |
@@ -661,8 +661,8 @@ discard block |
||
661 | 661 | } |
662 | 662 | // but we also need to adjust spaces for any other datetimes this ticket has access to |
663 | 663 | if ($datetime_ticket === $ticket_identifier) { |
664 | - if (isset($this->ticket_datetimes[ $datetime_ticket ]) |
|
665 | - && is_array($this->ticket_datetimes[ $datetime_ticket ]) |
|
664 | + if (isset($this->ticket_datetimes[$datetime_ticket]) |
|
665 | + && is_array($this->ticket_datetimes[$datetime_ticket]) |
|
666 | 666 | ) { |
667 | 667 | if ($this->debug) { |
668 | 668 | \EEH_Debug_Tools::printr( |
@@ -672,7 +672,7 @@ discard block |
||
672 | 672 | __LINE__ |
673 | 673 | ); |
674 | 674 | } |
675 | - foreach ($this->ticket_datetimes[ $datetime_ticket ] as $datetime) { |
|
675 | + foreach ($this->ticket_datetimes[$datetime_ticket] as $datetime) { |
|
676 | 676 | // don't adjust the current datetime twice |
677 | 677 | if ($datetime !== $datetime_identifier) { |
678 | 678 | $this->adjustDatetimeSpaces( |
@@ -692,24 +692,24 @@ discard block |
||
692 | 692 | { |
693 | 693 | // does datetime have spaces available? |
694 | 694 | // and does the supplied ticket have access to this datetime ? |
695 | - if ($this->datetime_spaces[ $datetime_identifier ] > 0 |
|
696 | - && isset($this->datetime_spaces[ $datetime_identifier ], $this->datetime_tickets[ $datetime_identifier ]) |
|
697 | - && in_array($ticket_identifier, $this->datetime_tickets[ $datetime_identifier ], true) |
|
695 | + if ($this->datetime_spaces[$datetime_identifier] > 0 |
|
696 | + && isset($this->datetime_spaces[$datetime_identifier], $this->datetime_tickets[$datetime_identifier]) |
|
697 | + && in_array($ticket_identifier, $this->datetime_tickets[$datetime_identifier], true) |
|
698 | 698 | ) { |
699 | 699 | if ($this->debug) { |
700 | 700 | \EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__); |
701 | 701 | \EEH_Debug_Tools::printr( |
702 | - "{$this->datetime_spaces[ $datetime_identifier ]}", |
|
702 | + "{$this->datetime_spaces[$datetime_identifier]}", |
|
703 | 703 | " . . current {$datetime_identifier} spaces available", |
704 | 704 | __FILE__, |
705 | 705 | __LINE__ |
706 | 706 | ); |
707 | 707 | } |
708 | 708 | // then decrement the available spaces for the datetime |
709 | - $this->datetime_spaces[ $datetime_identifier ] -= $ticket_quantity; |
|
709 | + $this->datetime_spaces[$datetime_identifier] -= $ticket_quantity; |
|
710 | 710 | // but don't let quantities go below zero |
711 | - $this->datetime_spaces[ $datetime_identifier ] = max( |
|
712 | - $this->datetime_spaces[ $datetime_identifier ], |
|
711 | + $this->datetime_spaces[$datetime_identifier] = max( |
|
712 | + $this->datetime_spaces[$datetime_identifier], |
|
713 | 713 | 0 |
714 | 714 | ); |
715 | 715 | if ($this->debug) { |
@@ -15,293 +15,293 @@ |
||
15 | 15 | class StatsGatherer |
16 | 16 | { |
17 | 17 | |
18 | - const COUNT_ALL_EVENTS = 'event'; |
|
19 | - const COUNT_ACTIVE_EVENTS = 'active_event'; |
|
20 | - const COUNT_DATETIMES = 'datetime'; |
|
21 | - const COUNT_TICKETS = 'ticket'; |
|
22 | - const COUNT_DATETIMES_SOLD = 'datetime_sold'; |
|
23 | - const COUNT_TICKETS_FREE = 'free_ticket'; |
|
24 | - const COUNT_TICKETS_PAID = 'paid_ticket'; |
|
25 | - const COUNT_TICKETS_SOLD = 'ticket_sold'; |
|
26 | - const COUNT_REGISTRATIONS_APPROVED = 'registrations_approved'; |
|
27 | - const COUNT_REGISTRATIONS_NOT_APPROVED = 'registrations_not_approved'; |
|
28 | - const COUNT_REGISTRATIONS_PENDING = 'registrations_pending'; |
|
29 | - const COUNT_REGISTRATIONS_INCOMPLETE = 'registrations_incomplete'; |
|
30 | - const COUNT_REGISTRATIONS_ALL = 'registrations_all'; |
|
31 | - const COUNT_REGISTRATIONS_CANCELLED = 'registrations_cancelled'; |
|
32 | - const COUNT_REGISTRATIONS_DECLINED = 'registrations_declined'; |
|
33 | - const SUM_TRANSACTIONS_COMPLETE_TOTAL = 'transactions_complete_total_sum'; |
|
34 | - const SUM_TRANSACTIONS_ALL_PAID = 'transactions_all_paid'; |
|
35 | - const INFO_SITE_CURRENCY = 'site_currency'; |
|
18 | + const COUNT_ALL_EVENTS = 'event'; |
|
19 | + const COUNT_ACTIVE_EVENTS = 'active_event'; |
|
20 | + const COUNT_DATETIMES = 'datetime'; |
|
21 | + const COUNT_TICKETS = 'ticket'; |
|
22 | + const COUNT_DATETIMES_SOLD = 'datetime_sold'; |
|
23 | + const COUNT_TICKETS_FREE = 'free_ticket'; |
|
24 | + const COUNT_TICKETS_PAID = 'paid_ticket'; |
|
25 | + const COUNT_TICKETS_SOLD = 'ticket_sold'; |
|
26 | + const COUNT_REGISTRATIONS_APPROVED = 'registrations_approved'; |
|
27 | + const COUNT_REGISTRATIONS_NOT_APPROVED = 'registrations_not_approved'; |
|
28 | + const COUNT_REGISTRATIONS_PENDING = 'registrations_pending'; |
|
29 | + const COUNT_REGISTRATIONS_INCOMPLETE = 'registrations_incomplete'; |
|
30 | + const COUNT_REGISTRATIONS_ALL = 'registrations_all'; |
|
31 | + const COUNT_REGISTRATIONS_CANCELLED = 'registrations_cancelled'; |
|
32 | + const COUNT_REGISTRATIONS_DECLINED = 'registrations_declined'; |
|
33 | + const SUM_TRANSACTIONS_COMPLETE_TOTAL = 'transactions_complete_total_sum'; |
|
34 | + const SUM_TRANSACTIONS_ALL_PAID = 'transactions_all_paid'; |
|
35 | + const INFO_SITE_CURRENCY = 'site_currency'; |
|
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * @var EEM_Payment_Method |
|
40 | - */ |
|
41 | - private $payment_method_model; |
|
38 | + /** |
|
39 | + * @var EEM_Payment_Method |
|
40 | + */ |
|
41 | + private $payment_method_model; |
|
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * @var EEM_Event |
|
46 | - */ |
|
47 | - private $event_model; |
|
44 | + /** |
|
45 | + * @var EEM_Event |
|
46 | + */ |
|
47 | + private $event_model; |
|
48 | 48 | |
49 | - /** |
|
50 | - * @var EEM_Datetime |
|
51 | - */ |
|
52 | - private $datetime_model; |
|
49 | + /** |
|
50 | + * @var EEM_Datetime |
|
51 | + */ |
|
52 | + private $datetime_model; |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * @var EEM_Ticket |
|
57 | - */ |
|
58 | - private $ticket_model; |
|
55 | + /** |
|
56 | + * @var EEM_Ticket |
|
57 | + */ |
|
58 | + private $ticket_model; |
|
59 | 59 | |
60 | 60 | |
61 | - /** |
|
62 | - * @var EEM_Registration |
|
63 | - */ |
|
64 | - private $registration_model; |
|
61 | + /** |
|
62 | + * @var EEM_Registration |
|
63 | + */ |
|
64 | + private $registration_model; |
|
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * @var EEM_Transaction |
|
69 | - */ |
|
70 | - private $transaction_model; |
|
67 | + /** |
|
68 | + * @var EEM_Transaction |
|
69 | + */ |
|
70 | + private $transaction_model; |
|
71 | 71 | |
72 | 72 | |
73 | - /** |
|
74 | - * @var EE_Config |
|
75 | - */ |
|
76 | - private $config; |
|
73 | + /** |
|
74 | + * @var EE_Config |
|
75 | + */ |
|
76 | + private $config; |
|
77 | 77 | |
78 | 78 | |
79 | - /** |
|
80 | - * StatsGatherer constructor. |
|
81 | - * |
|
82 | - * @param EEM_Payment_Method $payment_method_model |
|
83 | - * @param EEM_Event $event_model |
|
84 | - * @param EEM_Datetime $datetime_model |
|
85 | - * @param EEM_Ticket $ticket_model |
|
86 | - * @param EEM_Registration $registration_model |
|
87 | - * @param EEM_Transaction $transaction_model |
|
88 | - * @param EE_Config $config |
|
89 | - */ |
|
90 | - public function __construct( |
|
91 | - EEM_Payment_Method $payment_method_model, |
|
92 | - EEM_Event $event_model, |
|
93 | - EEM_Datetime $datetime_model, |
|
94 | - EEM_Ticket $ticket_model, |
|
95 | - EEM_Registration $registration_model, |
|
96 | - EEM_Transaction $transaction_model, |
|
97 | - EE_Config $config |
|
98 | - ) { |
|
99 | - $this->payment_method_model = $payment_method_model; |
|
100 | - $this->event_model = $event_model; |
|
101 | - $this->datetime_model = $datetime_model; |
|
102 | - $this->ticket_model = $ticket_model; |
|
103 | - $this->registration_model = $registration_model; |
|
104 | - $this->transaction_model = $transaction_model; |
|
105 | - $this->config = $config; |
|
106 | - } |
|
79 | + /** |
|
80 | + * StatsGatherer constructor. |
|
81 | + * |
|
82 | + * @param EEM_Payment_Method $payment_method_model |
|
83 | + * @param EEM_Event $event_model |
|
84 | + * @param EEM_Datetime $datetime_model |
|
85 | + * @param EEM_Ticket $ticket_model |
|
86 | + * @param EEM_Registration $registration_model |
|
87 | + * @param EEM_Transaction $transaction_model |
|
88 | + * @param EE_Config $config |
|
89 | + */ |
|
90 | + public function __construct( |
|
91 | + EEM_Payment_Method $payment_method_model, |
|
92 | + EEM_Event $event_model, |
|
93 | + EEM_Datetime $datetime_model, |
|
94 | + EEM_Ticket $ticket_model, |
|
95 | + EEM_Registration $registration_model, |
|
96 | + EEM_Transaction $transaction_model, |
|
97 | + EE_Config $config |
|
98 | + ) { |
|
99 | + $this->payment_method_model = $payment_method_model; |
|
100 | + $this->event_model = $event_model; |
|
101 | + $this->datetime_model = $datetime_model; |
|
102 | + $this->ticket_model = $ticket_model; |
|
103 | + $this->registration_model = $registration_model; |
|
104 | + $this->transaction_model = $transaction_model; |
|
105 | + $this->config = $config; |
|
106 | + } |
|
107 | 107 | |
108 | 108 | |
109 | - /** |
|
110 | - * Return the stats array for PUE UXIP stats. |
|
111 | - * |
|
112 | - * @return array |
|
113 | - */ |
|
114 | - public function stats() |
|
115 | - { |
|
116 | - $stats = $this->paymentMethodStats(); |
|
117 | - // a-ok so let's setup our stats. |
|
118 | - $stats = array_merge($stats, array( |
|
119 | - 'is_multisite' => is_multisite() && is_main_site(), |
|
120 | - 'active_theme' => $this->getActiveThemeStat(), |
|
121 | - 'ee4_all_events_count' => $this->getCountFor(self::COUNT_ALL_EVENTS), |
|
122 | - 'ee4_active_events_count' => $this->getCountFor(self::COUNT_ACTIVE_EVENTS), |
|
123 | - 'all_dtts_count' => $this->getCountFor(self::COUNT_DATETIMES), |
|
124 | - 'dtt_sold' => $this->getCountFor(self::COUNT_DATETIMES_SOLD), |
|
125 | - 'all_tkt_count' => $this->getCountFor(self::COUNT_TICKETS), |
|
126 | - 'free_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_FREE), |
|
127 | - 'paid_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_PAID), |
|
128 | - 'tkt_sold' => $this->getCountFor(self::COUNT_TICKETS_SOLD), |
|
129 | - 'approve_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_APPROVED), |
|
130 | - 'pending_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_PENDING), |
|
131 | - 'not_approved_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_NOT_APPROVED), |
|
132 | - 'incomplete_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_INCOMPLETE), |
|
133 | - 'cancelled_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_CANCELLED), |
|
134 | - 'declined_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_DECLINED), |
|
135 | - 'all_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_ALL), |
|
136 | - 'completed_transaction_total_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_COMPLETE_TOTAL), |
|
137 | - 'all_transaction_paid_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_ALL_PAID), |
|
138 | - self::INFO_SITE_CURRENCY => $this->config->currency instanceof EE_Currency_Config |
|
139 | - ? $this->config->currency->code |
|
140 | - : 'unknown', |
|
141 | - 'phpversion' => implode( |
|
142 | - '.', |
|
143 | - array(PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION) |
|
144 | - ), |
|
145 | - )); |
|
146 | - // remove any values that equal null. This ensures any stats that weren't retrieved successfully are excluded. |
|
147 | - return array_filter($stats, function ($value) { |
|
148 | - return $value !== null; |
|
149 | - }); |
|
150 | - } |
|
109 | + /** |
|
110 | + * Return the stats array for PUE UXIP stats. |
|
111 | + * |
|
112 | + * @return array |
|
113 | + */ |
|
114 | + public function stats() |
|
115 | + { |
|
116 | + $stats = $this->paymentMethodStats(); |
|
117 | + // a-ok so let's setup our stats. |
|
118 | + $stats = array_merge($stats, array( |
|
119 | + 'is_multisite' => is_multisite() && is_main_site(), |
|
120 | + 'active_theme' => $this->getActiveThemeStat(), |
|
121 | + 'ee4_all_events_count' => $this->getCountFor(self::COUNT_ALL_EVENTS), |
|
122 | + 'ee4_active_events_count' => $this->getCountFor(self::COUNT_ACTIVE_EVENTS), |
|
123 | + 'all_dtts_count' => $this->getCountFor(self::COUNT_DATETIMES), |
|
124 | + 'dtt_sold' => $this->getCountFor(self::COUNT_DATETIMES_SOLD), |
|
125 | + 'all_tkt_count' => $this->getCountFor(self::COUNT_TICKETS), |
|
126 | + 'free_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_FREE), |
|
127 | + 'paid_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_PAID), |
|
128 | + 'tkt_sold' => $this->getCountFor(self::COUNT_TICKETS_SOLD), |
|
129 | + 'approve_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_APPROVED), |
|
130 | + 'pending_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_PENDING), |
|
131 | + 'not_approved_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_NOT_APPROVED), |
|
132 | + 'incomplete_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_INCOMPLETE), |
|
133 | + 'cancelled_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_CANCELLED), |
|
134 | + 'declined_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_DECLINED), |
|
135 | + 'all_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_ALL), |
|
136 | + 'completed_transaction_total_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_COMPLETE_TOTAL), |
|
137 | + 'all_transaction_paid_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_ALL_PAID), |
|
138 | + self::INFO_SITE_CURRENCY => $this->config->currency instanceof EE_Currency_Config |
|
139 | + ? $this->config->currency->code |
|
140 | + : 'unknown', |
|
141 | + 'phpversion' => implode( |
|
142 | + '.', |
|
143 | + array(PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION) |
|
144 | + ), |
|
145 | + )); |
|
146 | + // remove any values that equal null. This ensures any stats that weren't retrieved successfully are excluded. |
|
147 | + return array_filter($stats, function ($value) { |
|
148 | + return $value !== null; |
|
149 | + }); |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @param string $which enum (@see constants prefixed with COUNT) |
|
154 | - * @return int|null |
|
155 | - */ |
|
156 | - private function getCountFor($which) |
|
157 | - { |
|
158 | - try { |
|
159 | - switch ($which) { |
|
160 | - case self::COUNT_ALL_EVENTS: |
|
161 | - $count = $this->event_model->count(); |
|
162 | - break; |
|
163 | - case self::COUNT_TICKETS: |
|
164 | - $count = $this->ticket_model->count(); |
|
165 | - break; |
|
166 | - case self::COUNT_DATETIMES: |
|
167 | - $count = $this->datetime_model->count(); |
|
168 | - break; |
|
169 | - case self::COUNT_ACTIVE_EVENTS: |
|
170 | - $count = $this->event_model->get_active_events(array(), true); |
|
171 | - break; |
|
172 | - case self::COUNT_DATETIMES_SOLD: |
|
173 | - $count = $this->datetime_model->sum(array(), 'DTT_sold'); |
|
174 | - break; |
|
175 | - case self::COUNT_TICKETS_FREE: |
|
176 | - $count = $this->ticket_model->count(array( |
|
177 | - array( |
|
178 | - 'TKT_price' => 0, |
|
179 | - ), |
|
180 | - )); |
|
181 | - break; |
|
182 | - case self::COUNT_TICKETS_PAID: |
|
183 | - $count = $this->ticket_model->count(array( |
|
184 | - array( |
|
185 | - 'TKT_price' => array('>', 0), |
|
186 | - ), |
|
187 | - )); |
|
188 | - break; |
|
189 | - case self::COUNT_TICKETS_SOLD: |
|
190 | - $count = $this->ticket_model->sum(array(), 'TKT_sold'); |
|
191 | - break; |
|
192 | - case self::COUNT_REGISTRATIONS_ALL: |
|
193 | - $count = $this->registration_model->count(); |
|
194 | - break; |
|
195 | - case self::COUNT_REGISTRATIONS_CANCELLED: |
|
196 | - $count = $this->registration_model->count( |
|
197 | - array( |
|
198 | - array( |
|
199 | - 'STS_ID' => EEM_Registration::status_id_cancelled, |
|
200 | - ), |
|
201 | - ) |
|
202 | - ); |
|
203 | - break; |
|
204 | - case self::COUNT_REGISTRATIONS_INCOMPLETE: |
|
205 | - $count = $this->registration_model->count( |
|
206 | - array( |
|
207 | - array( |
|
208 | - 'STS_ID' => EEM_Registration::status_id_incomplete, |
|
209 | - ), |
|
210 | - ) |
|
211 | - ); |
|
212 | - break; |
|
213 | - case self::COUNT_REGISTRATIONS_NOT_APPROVED: |
|
214 | - $count = $this->registration_model->count( |
|
215 | - array( |
|
216 | - array( |
|
217 | - 'STS_ID' => EEM_Registration::status_id_not_approved, |
|
218 | - ), |
|
219 | - ) |
|
220 | - ); |
|
221 | - break; |
|
222 | - case self::COUNT_REGISTRATIONS_DECLINED: |
|
223 | - $count = $this->registration_model->count( |
|
224 | - array( |
|
225 | - array( |
|
226 | - 'STS_ID' => EEM_Registration::status_id_declined, |
|
227 | - ), |
|
228 | - ) |
|
229 | - ); |
|
230 | - break; |
|
231 | - case self::COUNT_REGISTRATIONS_PENDING: |
|
232 | - $count = $this->registration_model->count( |
|
233 | - array( |
|
234 | - array( |
|
235 | - 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
236 | - ), |
|
237 | - ) |
|
238 | - ); |
|
239 | - break; |
|
240 | - case self::COUNT_REGISTRATIONS_APPROVED: |
|
241 | - $count = $this->registration_model->count( |
|
242 | - array( |
|
243 | - array( |
|
244 | - 'STS_ID' => EEM_Registration::status_id_approved, |
|
245 | - ), |
|
246 | - ) |
|
247 | - ); |
|
248 | - break; |
|
249 | - case self::SUM_TRANSACTIONS_COMPLETE_TOTAL: |
|
250 | - $count = $this->transaction_model->sum( |
|
251 | - array( |
|
252 | - array( |
|
253 | - 'STS_ID' => EEM_Transaction::complete_status_code, |
|
254 | - ), |
|
255 | - ), |
|
256 | - 'TXN_total' |
|
257 | - ); |
|
258 | - break; |
|
259 | - case self::SUM_TRANSACTIONS_ALL_PAID: |
|
260 | - $count = $this->transaction_model->sum( |
|
261 | - array(), |
|
262 | - 'TXN_paid' |
|
263 | - ); |
|
264 | - break; |
|
265 | - default: |
|
266 | - $count = null; |
|
267 | - break; |
|
268 | - } |
|
269 | - } catch (Exception $e) { |
|
270 | - $count = null; |
|
271 | - } |
|
272 | - return $count; |
|
273 | - } |
|
152 | + /** |
|
153 | + * @param string $which enum (@see constants prefixed with COUNT) |
|
154 | + * @return int|null |
|
155 | + */ |
|
156 | + private function getCountFor($which) |
|
157 | + { |
|
158 | + try { |
|
159 | + switch ($which) { |
|
160 | + case self::COUNT_ALL_EVENTS: |
|
161 | + $count = $this->event_model->count(); |
|
162 | + break; |
|
163 | + case self::COUNT_TICKETS: |
|
164 | + $count = $this->ticket_model->count(); |
|
165 | + break; |
|
166 | + case self::COUNT_DATETIMES: |
|
167 | + $count = $this->datetime_model->count(); |
|
168 | + break; |
|
169 | + case self::COUNT_ACTIVE_EVENTS: |
|
170 | + $count = $this->event_model->get_active_events(array(), true); |
|
171 | + break; |
|
172 | + case self::COUNT_DATETIMES_SOLD: |
|
173 | + $count = $this->datetime_model->sum(array(), 'DTT_sold'); |
|
174 | + break; |
|
175 | + case self::COUNT_TICKETS_FREE: |
|
176 | + $count = $this->ticket_model->count(array( |
|
177 | + array( |
|
178 | + 'TKT_price' => 0, |
|
179 | + ), |
|
180 | + )); |
|
181 | + break; |
|
182 | + case self::COUNT_TICKETS_PAID: |
|
183 | + $count = $this->ticket_model->count(array( |
|
184 | + array( |
|
185 | + 'TKT_price' => array('>', 0), |
|
186 | + ), |
|
187 | + )); |
|
188 | + break; |
|
189 | + case self::COUNT_TICKETS_SOLD: |
|
190 | + $count = $this->ticket_model->sum(array(), 'TKT_sold'); |
|
191 | + break; |
|
192 | + case self::COUNT_REGISTRATIONS_ALL: |
|
193 | + $count = $this->registration_model->count(); |
|
194 | + break; |
|
195 | + case self::COUNT_REGISTRATIONS_CANCELLED: |
|
196 | + $count = $this->registration_model->count( |
|
197 | + array( |
|
198 | + array( |
|
199 | + 'STS_ID' => EEM_Registration::status_id_cancelled, |
|
200 | + ), |
|
201 | + ) |
|
202 | + ); |
|
203 | + break; |
|
204 | + case self::COUNT_REGISTRATIONS_INCOMPLETE: |
|
205 | + $count = $this->registration_model->count( |
|
206 | + array( |
|
207 | + array( |
|
208 | + 'STS_ID' => EEM_Registration::status_id_incomplete, |
|
209 | + ), |
|
210 | + ) |
|
211 | + ); |
|
212 | + break; |
|
213 | + case self::COUNT_REGISTRATIONS_NOT_APPROVED: |
|
214 | + $count = $this->registration_model->count( |
|
215 | + array( |
|
216 | + array( |
|
217 | + 'STS_ID' => EEM_Registration::status_id_not_approved, |
|
218 | + ), |
|
219 | + ) |
|
220 | + ); |
|
221 | + break; |
|
222 | + case self::COUNT_REGISTRATIONS_DECLINED: |
|
223 | + $count = $this->registration_model->count( |
|
224 | + array( |
|
225 | + array( |
|
226 | + 'STS_ID' => EEM_Registration::status_id_declined, |
|
227 | + ), |
|
228 | + ) |
|
229 | + ); |
|
230 | + break; |
|
231 | + case self::COUNT_REGISTRATIONS_PENDING: |
|
232 | + $count = $this->registration_model->count( |
|
233 | + array( |
|
234 | + array( |
|
235 | + 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
236 | + ), |
|
237 | + ) |
|
238 | + ); |
|
239 | + break; |
|
240 | + case self::COUNT_REGISTRATIONS_APPROVED: |
|
241 | + $count = $this->registration_model->count( |
|
242 | + array( |
|
243 | + array( |
|
244 | + 'STS_ID' => EEM_Registration::status_id_approved, |
|
245 | + ), |
|
246 | + ) |
|
247 | + ); |
|
248 | + break; |
|
249 | + case self::SUM_TRANSACTIONS_COMPLETE_TOTAL: |
|
250 | + $count = $this->transaction_model->sum( |
|
251 | + array( |
|
252 | + array( |
|
253 | + 'STS_ID' => EEM_Transaction::complete_status_code, |
|
254 | + ), |
|
255 | + ), |
|
256 | + 'TXN_total' |
|
257 | + ); |
|
258 | + break; |
|
259 | + case self::SUM_TRANSACTIONS_ALL_PAID: |
|
260 | + $count = $this->transaction_model->sum( |
|
261 | + array(), |
|
262 | + 'TXN_paid' |
|
263 | + ); |
|
264 | + break; |
|
265 | + default: |
|
266 | + $count = null; |
|
267 | + break; |
|
268 | + } |
|
269 | + } catch (Exception $e) { |
|
270 | + $count = null; |
|
271 | + } |
|
272 | + return $count; |
|
273 | + } |
|
274 | 274 | |
275 | - /** |
|
276 | - * Return the active theme. |
|
277 | - * |
|
278 | - * @return false|string |
|
279 | - */ |
|
280 | - private function getActiveThemeStat() |
|
281 | - { |
|
282 | - $theme = wp_get_theme(); |
|
283 | - return $theme->get('Name'); |
|
284 | - } |
|
275 | + /** |
|
276 | + * Return the active theme. |
|
277 | + * |
|
278 | + * @return false|string |
|
279 | + */ |
|
280 | + private function getActiveThemeStat() |
|
281 | + { |
|
282 | + $theme = wp_get_theme(); |
|
283 | + return $theme->get('Name'); |
|
284 | + } |
|
285 | 285 | |
286 | - /** |
|
287 | - * @return array |
|
288 | - */ |
|
289 | - private function paymentMethodStats() |
|
290 | - { |
|
291 | - $payment_method_stats = array(); |
|
292 | - try { |
|
293 | - $active_payment_methods = $this->payment_method_model->get_all_active( |
|
294 | - null, |
|
295 | - array('group_by' => 'PMD_type') |
|
296 | - ); |
|
297 | - if ($active_payment_methods) { |
|
298 | - foreach ($active_payment_methods as $payment_method) { |
|
299 | - $payment_method_stats[ $payment_method->name() . '_active_payment_method' ] = 1; |
|
300 | - } |
|
301 | - } |
|
302 | - } catch (Exception $e) { |
|
303 | - // do nothing just prevents fatals. |
|
304 | - } |
|
305 | - return $payment_method_stats; |
|
306 | - } |
|
286 | + /** |
|
287 | + * @return array |
|
288 | + */ |
|
289 | + private function paymentMethodStats() |
|
290 | + { |
|
291 | + $payment_method_stats = array(); |
|
292 | + try { |
|
293 | + $active_payment_methods = $this->payment_method_model->get_all_active( |
|
294 | + null, |
|
295 | + array('group_by' => 'PMD_type') |
|
296 | + ); |
|
297 | + if ($active_payment_methods) { |
|
298 | + foreach ($active_payment_methods as $payment_method) { |
|
299 | + $payment_method_stats[ $payment_method->name() . '_active_payment_method' ] = 1; |
|
300 | + } |
|
301 | + } |
|
302 | + } catch (Exception $e) { |
|
303 | + // do nothing just prevents fatals. |
|
304 | + } |
|
305 | + return $payment_method_stats; |
|
306 | + } |
|
307 | 307 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | ), |
145 | 145 | )); |
146 | 146 | // remove any values that equal null. This ensures any stats that weren't retrieved successfully are excluded. |
147 | - return array_filter($stats, function ($value) { |
|
147 | + return array_filter($stats, function($value) { |
|
148 | 148 | return $value !== null; |
149 | 149 | }); |
150 | 150 | } |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | ); |
297 | 297 | if ($active_payment_methods) { |
298 | 298 | foreach ($active_payment_methods as $payment_method) { |
299 | - $payment_method_stats[ $payment_method->name() . '_active_payment_method' ] = 1; |
|
299 | + $payment_method_stats[$payment_method->name().'_active_payment_method'] = 1; |
|
300 | 300 | } |
301 | 301 | } |
302 | 302 | } catch (Exception $e) { |
@@ -22,88 +22,88 @@ discard block |
||
22 | 22 | class Stats |
23 | 23 | { |
24 | 24 | |
25 | - const OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS = 'ee_uxip_stats_expiry'; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var Config |
|
29 | - */ |
|
30 | - private $config; |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * @var StatsGatherer |
|
35 | - */ |
|
36 | - private $stats_gatherer; |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * @var EE_Maintenance_Mode |
|
41 | - */ |
|
42 | - private $maintenance_mode; |
|
43 | - |
|
44 | - public function __construct( |
|
45 | - Config $config, |
|
46 | - EE_Maintenance_Mode $maintenance_mode, |
|
47 | - StatsGatherer $stats_gatherer |
|
48 | - ) { |
|
49 | - $this->config = $config; |
|
50 | - $this->maintenance_mode = $maintenance_mode; |
|
51 | - $this->stats_gatherer = $stats_gatherer; |
|
52 | - $this->setUxipNotices(); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * Displays uxip opt-in notice if necessary. |
|
58 | - */ |
|
59 | - private function setUxipNotices() |
|
60 | - { |
|
61 | - if ($this->canDisplayNotices()) { |
|
62 | - add_action('admin_notices', array($this, 'optinNotice')); |
|
63 | - add_action('admin_enqueue_scripts', array($this, 'enqueueScripts')); |
|
64 | - add_action('wp_ajax_espresso_data_optin', array($this, 'ajaxHandler')); |
|
65 | - // makes sure optin defaults to yes even if its currently empty. |
|
66 | - $this->config->setHasOptedInForUxip(); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * This returns the callback that PluginUpdateEngineChecker will use for getting any extra stats to send. |
|
73 | - * |
|
74 | - * @return Closure |
|
75 | - */ |
|
76 | - public function statsCallback() |
|
77 | - { |
|
78 | - // returns a callback that can is used to retrieve the stats to send along to the pue server. |
|
79 | - return function () { |
|
80 | - // we only send stats one a week, so let's see if our stat timestamp has expired. |
|
81 | - if (! $this->sendStats()) { |
|
82 | - return array(); |
|
83 | - } |
|
84 | - return $this->stats_gatherer->stats(); |
|
85 | - }; |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Return whether notices can be displayed or not |
|
91 | - * |
|
92 | - * @return bool |
|
93 | - */ |
|
94 | - private function canDisplayNotices() |
|
95 | - { |
|
96 | - return ! $this->config->hasNotifiedForUxip() |
|
97 | - && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance; |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Callback for the admin_notices hook that outputs the UXIP optin-in notice. |
|
103 | - */ |
|
104 | - public function optinNotice() |
|
105 | - { |
|
106 | - ?> |
|
25 | + const OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS = 'ee_uxip_stats_expiry'; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var Config |
|
29 | + */ |
|
30 | + private $config; |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * @var StatsGatherer |
|
35 | + */ |
|
36 | + private $stats_gatherer; |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * @var EE_Maintenance_Mode |
|
41 | + */ |
|
42 | + private $maintenance_mode; |
|
43 | + |
|
44 | + public function __construct( |
|
45 | + Config $config, |
|
46 | + EE_Maintenance_Mode $maintenance_mode, |
|
47 | + StatsGatherer $stats_gatherer |
|
48 | + ) { |
|
49 | + $this->config = $config; |
|
50 | + $this->maintenance_mode = $maintenance_mode; |
|
51 | + $this->stats_gatherer = $stats_gatherer; |
|
52 | + $this->setUxipNotices(); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * Displays uxip opt-in notice if necessary. |
|
58 | + */ |
|
59 | + private function setUxipNotices() |
|
60 | + { |
|
61 | + if ($this->canDisplayNotices()) { |
|
62 | + add_action('admin_notices', array($this, 'optinNotice')); |
|
63 | + add_action('admin_enqueue_scripts', array($this, 'enqueueScripts')); |
|
64 | + add_action('wp_ajax_espresso_data_optin', array($this, 'ajaxHandler')); |
|
65 | + // makes sure optin defaults to yes even if its currently empty. |
|
66 | + $this->config->setHasOptedInForUxip(); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * This returns the callback that PluginUpdateEngineChecker will use for getting any extra stats to send. |
|
73 | + * |
|
74 | + * @return Closure |
|
75 | + */ |
|
76 | + public function statsCallback() |
|
77 | + { |
|
78 | + // returns a callback that can is used to retrieve the stats to send along to the pue server. |
|
79 | + return function () { |
|
80 | + // we only send stats one a week, so let's see if our stat timestamp has expired. |
|
81 | + if (! $this->sendStats()) { |
|
82 | + return array(); |
|
83 | + } |
|
84 | + return $this->stats_gatherer->stats(); |
|
85 | + }; |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Return whether notices can be displayed or not |
|
91 | + * |
|
92 | + * @return bool |
|
93 | + */ |
|
94 | + private function canDisplayNotices() |
|
95 | + { |
|
96 | + return ! $this->config->hasNotifiedForUxip() |
|
97 | + && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance; |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Callback for the admin_notices hook that outputs the UXIP optin-in notice. |
|
103 | + */ |
|
104 | + public function optinNotice() |
|
105 | + { |
|
106 | + ?> |
|
107 | 107 | <div class="updated data-collect-optin" id="espresso-data-collect-optin-container"> |
108 | 108 | <div id="data-collect-optin-options-container"> |
109 | 109 | <span class="dashicons dashicons-admin-site"></span> |
@@ -116,125 +116,125 @@ discard block |
||
116 | 116 | </div> |
117 | 117 | </div> |
118 | 118 | <?php |
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * Retrieves the optin text (static so it can be used in multiple places as necessary). |
|
124 | - * |
|
125 | - * @param bool $extra |
|
126 | - */ |
|
127 | - public static function optinText($extra = true) |
|
128 | - { |
|
129 | - if (! $extra) { |
|
130 | - echo '<h2 class="ee-admin-settings-hdr" ' |
|
131 | - . (! $extra ? 'id="UXIP_settings"' : '') |
|
132 | - . '>' |
|
133 | - . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso') |
|
134 | - . EEH_Template::get_help_tab_link('organization_logo_info') |
|
135 | - . '</h2>'; |
|
136 | - printf( |
|
137 | - esc_html__( |
|
138 | - '%1$sPlease help us make Event Espresso better and vote for your favorite features.%2$s The %3$sUser eXperience Improvement Program (UXIP)%4$s, has been created so when you use Event Espresso you are voting for the features and settings that are important to you. The UXIP helps us understand how you use our products and services, track problems and in what context. If you opt-out of the UXIP you essentially elect for us to disregard how you use Event Espresso as we build new features and make changes. Participation in the program is completely voluntary but it is enabled by default. The end results of the UXIP are software improvements to better meet your needs. The data we collect will never be sold, traded, or misused in any way. %5$sPlease see our %6$sPrivacy Policy%7$s for more information.', |
|
139 | - 'event_espresso' |
|
140 | - ), |
|
141 | - '<p><em>', |
|
142 | - '</em></p>', |
|
143 | - '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
144 | - '</a>', |
|
145 | - '<br><br>', |
|
146 | - '<a href="https://eventespresso.com/about/privacy-policy/" target="_blank">', |
|
147 | - '</a>' |
|
148 | - ); |
|
149 | - } else { |
|
150 | - $settings_url = EE_Admin_Page::add_query_args_and_nonce( |
|
151 | - array('action' => 'default'), |
|
152 | - admin_url('admin.php?page=espresso_general_settings') |
|
153 | - ); |
|
154 | - $settings_url .= '#UXIP_settings'; |
|
155 | - printf( |
|
156 | - esc_html__( |
|
157 | - 'The Event Espresso UXIP feature is active on your site. For %1$smore info%2$s and to opt-out %3$sclick here%4$s.', |
|
158 | - 'event_espresso' |
|
159 | - ), |
|
160 | - '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
161 | - '</a>', |
|
162 | - '<a href="' . $settings_url . '" target="_blank">', |
|
163 | - '</a>' |
|
164 | - ); |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * Callback for admin_enqueue_scripts that sets up the scripts and styles for the uxip notice |
|
171 | - */ |
|
172 | - public function enqueueScripts() |
|
173 | - { |
|
174 | - wp_register_script( |
|
175 | - 'ee-data-optin-js', |
|
176 | - EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', |
|
177 | - array('jquery'), |
|
178 | - EVENT_ESPRESSO_VERSION, |
|
179 | - true |
|
180 | - ); |
|
181 | - wp_register_style( |
|
182 | - 'ee-data-optin-css', |
|
183 | - EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', |
|
184 | - array(), |
|
185 | - EVENT_ESPRESSO_VERSION |
|
186 | - ); |
|
187 | - |
|
188 | - wp_enqueue_script('ee-data-optin-js'); |
|
189 | - wp_enqueue_style('ee-data-optin-css'); |
|
190 | - } |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * Callback for wp_ajax_espresso_data_optin that handles the ajax request |
|
195 | - */ |
|
196 | - public function ajaxHandler() |
|
197 | - { |
|
198 | - // verify nonce |
|
199 | - if (isset($_POST['nonce']) && ! wp_verify_nonce($_POST['nonce'], 'ee-data-optin')) { |
|
200 | - exit(); |
|
201 | - } |
|
202 | - |
|
203 | - // update has notified option |
|
204 | - $this->config->setHasNotifiedAboutUxip(); |
|
205 | - exit(); |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - /** |
|
210 | - * Used to determine whether additional stats are sent. |
|
211 | - */ |
|
212 | - private function sendStats() |
|
213 | - { |
|
214 | - return $this->config->isOptedInForUxip() |
|
215 | - && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance |
|
216 | - && $this->statSendTimestampExpired(); |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * Returns true when the timestamp used to track whether stats get sent (currently a weekly interval) is expired. |
|
222 | - * Returns false otherwise. |
|
223 | - * |
|
224 | - * @return bool |
|
225 | - */ |
|
226 | - private function statSendTimestampExpired() |
|
227 | - { |
|
228 | - $current_expiry = get_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, null); |
|
229 | - if ($current_expiry === null) { |
|
230 | - add_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS, '', 'no'); |
|
231 | - return true; |
|
232 | - } |
|
233 | - |
|
234 | - if (time() > (int) $current_expiry) { |
|
235 | - update_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS); |
|
236 | - return true; |
|
237 | - } |
|
238 | - return false; |
|
239 | - } |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * Retrieves the optin text (static so it can be used in multiple places as necessary). |
|
124 | + * |
|
125 | + * @param bool $extra |
|
126 | + */ |
|
127 | + public static function optinText($extra = true) |
|
128 | + { |
|
129 | + if (! $extra) { |
|
130 | + echo '<h2 class="ee-admin-settings-hdr" ' |
|
131 | + . (! $extra ? 'id="UXIP_settings"' : '') |
|
132 | + . '>' |
|
133 | + . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso') |
|
134 | + . EEH_Template::get_help_tab_link('organization_logo_info') |
|
135 | + . '</h2>'; |
|
136 | + printf( |
|
137 | + esc_html__( |
|
138 | + '%1$sPlease help us make Event Espresso better and vote for your favorite features.%2$s The %3$sUser eXperience Improvement Program (UXIP)%4$s, has been created so when you use Event Espresso you are voting for the features and settings that are important to you. The UXIP helps us understand how you use our products and services, track problems and in what context. If you opt-out of the UXIP you essentially elect for us to disregard how you use Event Espresso as we build new features and make changes. Participation in the program is completely voluntary but it is enabled by default. The end results of the UXIP are software improvements to better meet your needs. The data we collect will never be sold, traded, or misused in any way. %5$sPlease see our %6$sPrivacy Policy%7$s for more information.', |
|
139 | + 'event_espresso' |
|
140 | + ), |
|
141 | + '<p><em>', |
|
142 | + '</em></p>', |
|
143 | + '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
144 | + '</a>', |
|
145 | + '<br><br>', |
|
146 | + '<a href="https://eventespresso.com/about/privacy-policy/" target="_blank">', |
|
147 | + '</a>' |
|
148 | + ); |
|
149 | + } else { |
|
150 | + $settings_url = EE_Admin_Page::add_query_args_and_nonce( |
|
151 | + array('action' => 'default'), |
|
152 | + admin_url('admin.php?page=espresso_general_settings') |
|
153 | + ); |
|
154 | + $settings_url .= '#UXIP_settings'; |
|
155 | + printf( |
|
156 | + esc_html__( |
|
157 | + 'The Event Espresso UXIP feature is active on your site. For %1$smore info%2$s and to opt-out %3$sclick here%4$s.', |
|
158 | + 'event_espresso' |
|
159 | + ), |
|
160 | + '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
161 | + '</a>', |
|
162 | + '<a href="' . $settings_url . '" target="_blank">', |
|
163 | + '</a>' |
|
164 | + ); |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * Callback for admin_enqueue_scripts that sets up the scripts and styles for the uxip notice |
|
171 | + */ |
|
172 | + public function enqueueScripts() |
|
173 | + { |
|
174 | + wp_register_script( |
|
175 | + 'ee-data-optin-js', |
|
176 | + EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', |
|
177 | + array('jquery'), |
|
178 | + EVENT_ESPRESSO_VERSION, |
|
179 | + true |
|
180 | + ); |
|
181 | + wp_register_style( |
|
182 | + 'ee-data-optin-css', |
|
183 | + EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', |
|
184 | + array(), |
|
185 | + EVENT_ESPRESSO_VERSION |
|
186 | + ); |
|
187 | + |
|
188 | + wp_enqueue_script('ee-data-optin-js'); |
|
189 | + wp_enqueue_style('ee-data-optin-css'); |
|
190 | + } |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * Callback for wp_ajax_espresso_data_optin that handles the ajax request |
|
195 | + */ |
|
196 | + public function ajaxHandler() |
|
197 | + { |
|
198 | + // verify nonce |
|
199 | + if (isset($_POST['nonce']) && ! wp_verify_nonce($_POST['nonce'], 'ee-data-optin')) { |
|
200 | + exit(); |
|
201 | + } |
|
202 | + |
|
203 | + // update has notified option |
|
204 | + $this->config->setHasNotifiedAboutUxip(); |
|
205 | + exit(); |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + /** |
|
210 | + * Used to determine whether additional stats are sent. |
|
211 | + */ |
|
212 | + private function sendStats() |
|
213 | + { |
|
214 | + return $this->config->isOptedInForUxip() |
|
215 | + && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance |
|
216 | + && $this->statSendTimestampExpired(); |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * Returns true when the timestamp used to track whether stats get sent (currently a weekly interval) is expired. |
|
222 | + * Returns false otherwise. |
|
223 | + * |
|
224 | + * @return bool |
|
225 | + */ |
|
226 | + private function statSendTimestampExpired() |
|
227 | + { |
|
228 | + $current_expiry = get_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, null); |
|
229 | + if ($current_expiry === null) { |
|
230 | + add_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS, '', 'no'); |
|
231 | + return true; |
|
232 | + } |
|
233 | + |
|
234 | + if (time() > (int) $current_expiry) { |
|
235 | + update_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS); |
|
236 | + return true; |
|
237 | + } |
|
238 | + return false; |
|
239 | + } |
|
240 | 240 | } |
@@ -76,9 +76,9 @@ discard block |
||
76 | 76 | public function statsCallback() |
77 | 77 | { |
78 | 78 | // returns a callback that can is used to retrieve the stats to send along to the pue server. |
79 | - return function () { |
|
79 | + return function() { |
|
80 | 80 | // we only send stats one a week, so let's see if our stat timestamp has expired. |
81 | - if (! $this->sendStats()) { |
|
81 | + if ( ! $this->sendStats()) { |
|
82 | 82 | return array(); |
83 | 83 | } |
84 | 84 | return $this->stats_gatherer->stats(); |
@@ -126,9 +126,9 @@ discard block |
||
126 | 126 | */ |
127 | 127 | public static function optinText($extra = true) |
128 | 128 | { |
129 | - if (! $extra) { |
|
129 | + if ( ! $extra) { |
|
130 | 130 | echo '<h2 class="ee-admin-settings-hdr" ' |
131 | - . (! $extra ? 'id="UXIP_settings"' : '') |
|
131 | + . ( ! $extra ? 'id="UXIP_settings"' : '') |
|
132 | 132 | . '>' |
133 | 133 | . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso') |
134 | 134 | . EEH_Template::get_help_tab_link('organization_logo_info') |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | ), |
160 | 160 | '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
161 | 161 | '</a>', |
162 | - '<a href="' . $settings_url . '" target="_blank">', |
|
162 | + '<a href="'.$settings_url.'" target="_blank">', |
|
163 | 163 | '</a>' |
164 | 164 | ); |
165 | 165 | } |
@@ -173,14 +173,14 @@ discard block |
||
173 | 173 | { |
174 | 174 | wp_register_script( |
175 | 175 | 'ee-data-optin-js', |
176 | - EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', |
|
176 | + EE_GLOBAL_ASSETS_URL.'scripts/ee-data-optin.js', |
|
177 | 177 | array('jquery'), |
178 | 178 | EVENT_ESPRESSO_VERSION, |
179 | 179 | true |
180 | 180 | ); |
181 | 181 | wp_register_style( |
182 | 182 | 'ee-data-optin-css', |
183 | - EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', |
|
183 | + EE_GLOBAL_ASSETS_URL.'css/ee-data-optin.css', |
|
184 | 184 | array(), |
185 | 185 | EVENT_ESPRESSO_VERSION |
186 | 186 | ); |