@@ -10,136 +10,136 @@ |
||
10 | 10 | class EE_Country extends EE_Base_Class |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @param array $props_n_values |
|
15 | - * @return EE_Country|mixed |
|
16 | - * @throws EE_Error |
|
17 | - * @throws ReflectionException |
|
18 | - */ |
|
19 | - public static function new_instance($props_n_values = []) |
|
20 | - { |
|
21 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
22 | - return $has_object ? $has_object : new self($props_n_values); |
|
23 | - } |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * @param array $props_n_values |
|
28 | - * @return EE_Country |
|
29 | - * @throws EE_Error |
|
30 | - * @throws ReflectionException |
|
31 | - */ |
|
32 | - public static function new_instance_from_db($props_n_values = []) |
|
33 | - { |
|
34 | - return new self($props_n_values, true); |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * Gets the country name |
|
40 | - * |
|
41 | - * @return string |
|
42 | - * @throws EE_Error |
|
43 | - */ |
|
44 | - public function name() |
|
45 | - { |
|
46 | - return $this->get('CNT_name'); |
|
47 | - } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * gets the country's currency code |
|
52 | - * |
|
53 | - * @return string |
|
54 | - * @throws EE_Error |
|
55 | - */ |
|
56 | - public function currency_code() |
|
57 | - { |
|
58 | - return $this->get('CNT_cur_code'); |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * gets the country's currency sign/symbol |
|
64 | - * |
|
65 | - * @return string |
|
66 | - * @throws EE_Error |
|
67 | - */ |
|
68 | - public function currency_sign() |
|
69 | - { |
|
70 | - $CNT_cur_sign = $this->get('CNT_cur_sign'); |
|
71 | - return $CNT_cur_sign ? $CNT_cur_sign : ''; |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * Currency name singular |
|
77 | - * |
|
78 | - * @return string |
|
79 | - * @throws EE_Error |
|
80 | - */ |
|
81 | - public function currency_name_single() |
|
82 | - { |
|
83 | - return $this->get('CNT_cur_single'); |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * Currency name plural |
|
89 | - * |
|
90 | - * @return string |
|
91 | - * @throws EE_Error |
|
92 | - */ |
|
93 | - public function currency_name_plural() |
|
94 | - { |
|
95 | - return $this->get('CNT_cur_plural'); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * currency_sign_before - ie: $TRUE or FALSE$ |
|
101 | - * |
|
102 | - * @return boolean |
|
103 | - * @throws EE_Error |
|
104 | - */ |
|
105 | - public function currency_sign_before() |
|
106 | - { |
|
107 | - return $this->get('CNT_cur_sign_b4'); |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - /** |
|
112 | - * currency_decimal_places : 2 = 0.00 3 = 0.000 |
|
113 | - * |
|
114 | - * @return integer |
|
115 | - * @throws EE_Error |
|
116 | - */ |
|
117 | - public function currency_decimal_places() |
|
118 | - { |
|
119 | - return $this->get('CNT_cur_dec_plc'); |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * currency_decimal_mark : (comma) ',' = 0,01 or (decimal) '.' = 0.01 |
|
125 | - * |
|
126 | - * @return string |
|
127 | - * @throws EE_Error |
|
128 | - */ |
|
129 | - public function currency_decimal_mark() |
|
130 | - { |
|
131 | - return $this->get('CNT_cur_dec_mrk'); |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * currency thousands separator: (comma) ',' = 1,000 or (decimal) '.' = 1.000 |
|
137 | - * |
|
138 | - * @return string |
|
139 | - * @throws EE_Error |
|
140 | - */ |
|
141 | - public function currency_thousands_separator() |
|
142 | - { |
|
143 | - return $this->get('CNT_cur_thsnds'); |
|
144 | - } |
|
13 | + /** |
|
14 | + * @param array $props_n_values |
|
15 | + * @return EE_Country|mixed |
|
16 | + * @throws EE_Error |
|
17 | + * @throws ReflectionException |
|
18 | + */ |
|
19 | + public static function new_instance($props_n_values = []) |
|
20 | + { |
|
21 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
22 | + return $has_object ? $has_object : new self($props_n_values); |
|
23 | + } |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * @param array $props_n_values |
|
28 | + * @return EE_Country |
|
29 | + * @throws EE_Error |
|
30 | + * @throws ReflectionException |
|
31 | + */ |
|
32 | + public static function new_instance_from_db($props_n_values = []) |
|
33 | + { |
|
34 | + return new self($props_n_values, true); |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * Gets the country name |
|
40 | + * |
|
41 | + * @return string |
|
42 | + * @throws EE_Error |
|
43 | + */ |
|
44 | + public function name() |
|
45 | + { |
|
46 | + return $this->get('CNT_name'); |
|
47 | + } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * gets the country's currency code |
|
52 | + * |
|
53 | + * @return string |
|
54 | + * @throws EE_Error |
|
55 | + */ |
|
56 | + public function currency_code() |
|
57 | + { |
|
58 | + return $this->get('CNT_cur_code'); |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * gets the country's currency sign/symbol |
|
64 | + * |
|
65 | + * @return string |
|
66 | + * @throws EE_Error |
|
67 | + */ |
|
68 | + public function currency_sign() |
|
69 | + { |
|
70 | + $CNT_cur_sign = $this->get('CNT_cur_sign'); |
|
71 | + return $CNT_cur_sign ? $CNT_cur_sign : ''; |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * Currency name singular |
|
77 | + * |
|
78 | + * @return string |
|
79 | + * @throws EE_Error |
|
80 | + */ |
|
81 | + public function currency_name_single() |
|
82 | + { |
|
83 | + return $this->get('CNT_cur_single'); |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * Currency name plural |
|
89 | + * |
|
90 | + * @return string |
|
91 | + * @throws EE_Error |
|
92 | + */ |
|
93 | + public function currency_name_plural() |
|
94 | + { |
|
95 | + return $this->get('CNT_cur_plural'); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * currency_sign_before - ie: $TRUE or FALSE$ |
|
101 | + * |
|
102 | + * @return boolean |
|
103 | + * @throws EE_Error |
|
104 | + */ |
|
105 | + public function currency_sign_before() |
|
106 | + { |
|
107 | + return $this->get('CNT_cur_sign_b4'); |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + /** |
|
112 | + * currency_decimal_places : 2 = 0.00 3 = 0.000 |
|
113 | + * |
|
114 | + * @return integer |
|
115 | + * @throws EE_Error |
|
116 | + */ |
|
117 | + public function currency_decimal_places() |
|
118 | + { |
|
119 | + return $this->get('CNT_cur_dec_plc'); |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * currency_decimal_mark : (comma) ',' = 0,01 or (decimal) '.' = 0.01 |
|
125 | + * |
|
126 | + * @return string |
|
127 | + * @throws EE_Error |
|
128 | + */ |
|
129 | + public function currency_decimal_mark() |
|
130 | + { |
|
131 | + return $this->get('CNT_cur_dec_mrk'); |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * currency thousands separator: (comma) ',' = 1,000 or (decimal) '.' = 1.000 |
|
137 | + * |
|
138 | + * @return string |
|
139 | + * @throws EE_Error |
|
140 | + */ |
|
141 | + public function currency_thousands_separator() |
|
142 | + { |
|
143 | + return $this->get('CNT_cur_thsnds'); |
|
144 | + } |
|
145 | 145 | } |
@@ -10,153 +10,153 @@ |
||
10 | 10 | class EE_Answer extends EE_Base_Class |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @param array $props_n_values |
|
15 | - * @return EE_Answer |
|
16 | - * @throws EE_Error |
|
17 | - * @throws ReflectionException |
|
18 | - */ |
|
19 | - public static function new_instance($props_n_values = []) |
|
20 | - { |
|
21 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
22 | - return $has_object ? $has_object : new self($props_n_values); |
|
23 | - } |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * @param array $props_n_values |
|
28 | - * @return EE_Answer |
|
29 | - * @throws EE_Error |
|
30 | - * @throws ReflectionException |
|
31 | - */ |
|
32 | - public static function new_instance_from_db($props_n_values = []) |
|
33 | - { |
|
34 | - return new self($props_n_values, true); |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * Set Question ID |
|
40 | - * |
|
41 | - * @param int $QST_ID |
|
42 | - * @throws EE_Error |
|
43 | - * @throws ReflectionException |
|
44 | - */ |
|
45 | - public function set_question($QST_ID = 0) |
|
46 | - { |
|
47 | - $this->set('QST_ID', $QST_ID); |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * Set Registration ID |
|
53 | - * |
|
54 | - * @param int $REG_ID |
|
55 | - * @throws EE_Error |
|
56 | - * @throws ReflectionException |
|
57 | - */ |
|
58 | - public function set_registration($REG_ID = 0) |
|
59 | - { |
|
60 | - $this->set('REG_ID', $REG_ID); |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Set Answer value |
|
66 | - * |
|
67 | - * @param mixed $ANS_value |
|
68 | - * @throws EE_Error |
|
69 | - * @throws ReflectionException |
|
70 | - */ |
|
71 | - public function set_value($ANS_value = '') |
|
72 | - { |
|
73 | - $this->set('ANS_value', $ANS_value); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * get Attendee First Name |
|
79 | - * |
|
80 | - * @return int |
|
81 | - * @throws EE_Error |
|
82 | - */ |
|
83 | - public function registration_ID() |
|
84 | - { |
|
85 | - return $this->get('REG_ID'); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * get Attendee Last Name |
|
91 | - * |
|
92 | - * @return int |
|
93 | - * @throws EE_Error |
|
94 | - */ |
|
95 | - public function question_ID() |
|
96 | - { |
|
97 | - return $this->get('QST_ID'); |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * get Attendee Address |
|
103 | - * |
|
104 | - * @return string |
|
105 | - * @throws EE_Error |
|
106 | - */ |
|
107 | - public function value() |
|
108 | - { |
|
109 | - return $this->get('ANS_value'); |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * Gets a pretty form of the value (mostly applies to answers that have multiple answers) |
|
115 | - * |
|
116 | - * @param null $schema |
|
117 | - * @return string |
|
118 | - * @throws EE_Error |
|
119 | - */ |
|
120 | - public function pretty_value($schema = null) |
|
121 | - { |
|
122 | - return $this->get_pretty('ANS_value', $schema); |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * Echoes out a pretty value (even for multi-choice options) |
|
128 | - * |
|
129 | - * @param string $schema |
|
130 | - * @throws EE_Error |
|
131 | - */ |
|
132 | - public function e_value($schema = null) |
|
133 | - { |
|
134 | - $this->e('ANS_value', $schema); |
|
135 | - } |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * Gets the related EE_Question to this EE_Answer |
|
140 | - * |
|
141 | - * @return EE_Base_Class|EE_Question |
|
142 | - * @throws EE_Error |
|
143 | - * @throws ReflectionException |
|
144 | - */ |
|
145 | - public function question() |
|
146 | - { |
|
147 | - return $this->get_first_related('Question'); |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * Gets the related EE_Registration to this EE_Answer |
|
153 | - * |
|
154 | - * @return EE_Base_Class|EE_Registration |
|
155 | - * @throws EE_Error |
|
156 | - * @throws ReflectionException |
|
157 | - */ |
|
158 | - public function registration() |
|
159 | - { |
|
160 | - return $this->get_first_related('Registration'); |
|
161 | - } |
|
13 | + /** |
|
14 | + * @param array $props_n_values |
|
15 | + * @return EE_Answer |
|
16 | + * @throws EE_Error |
|
17 | + * @throws ReflectionException |
|
18 | + */ |
|
19 | + public static function new_instance($props_n_values = []) |
|
20 | + { |
|
21 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
22 | + return $has_object ? $has_object : new self($props_n_values); |
|
23 | + } |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * @param array $props_n_values |
|
28 | + * @return EE_Answer |
|
29 | + * @throws EE_Error |
|
30 | + * @throws ReflectionException |
|
31 | + */ |
|
32 | + public static function new_instance_from_db($props_n_values = []) |
|
33 | + { |
|
34 | + return new self($props_n_values, true); |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * Set Question ID |
|
40 | + * |
|
41 | + * @param int $QST_ID |
|
42 | + * @throws EE_Error |
|
43 | + * @throws ReflectionException |
|
44 | + */ |
|
45 | + public function set_question($QST_ID = 0) |
|
46 | + { |
|
47 | + $this->set('QST_ID', $QST_ID); |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * Set Registration ID |
|
53 | + * |
|
54 | + * @param int $REG_ID |
|
55 | + * @throws EE_Error |
|
56 | + * @throws ReflectionException |
|
57 | + */ |
|
58 | + public function set_registration($REG_ID = 0) |
|
59 | + { |
|
60 | + $this->set('REG_ID', $REG_ID); |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Set Answer value |
|
66 | + * |
|
67 | + * @param mixed $ANS_value |
|
68 | + * @throws EE_Error |
|
69 | + * @throws ReflectionException |
|
70 | + */ |
|
71 | + public function set_value($ANS_value = '') |
|
72 | + { |
|
73 | + $this->set('ANS_value', $ANS_value); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * get Attendee First Name |
|
79 | + * |
|
80 | + * @return int |
|
81 | + * @throws EE_Error |
|
82 | + */ |
|
83 | + public function registration_ID() |
|
84 | + { |
|
85 | + return $this->get('REG_ID'); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * get Attendee Last Name |
|
91 | + * |
|
92 | + * @return int |
|
93 | + * @throws EE_Error |
|
94 | + */ |
|
95 | + public function question_ID() |
|
96 | + { |
|
97 | + return $this->get('QST_ID'); |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * get Attendee Address |
|
103 | + * |
|
104 | + * @return string |
|
105 | + * @throws EE_Error |
|
106 | + */ |
|
107 | + public function value() |
|
108 | + { |
|
109 | + return $this->get('ANS_value'); |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * Gets a pretty form of the value (mostly applies to answers that have multiple answers) |
|
115 | + * |
|
116 | + * @param null $schema |
|
117 | + * @return string |
|
118 | + * @throws EE_Error |
|
119 | + */ |
|
120 | + public function pretty_value($schema = null) |
|
121 | + { |
|
122 | + return $this->get_pretty('ANS_value', $schema); |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * Echoes out a pretty value (even for multi-choice options) |
|
128 | + * |
|
129 | + * @param string $schema |
|
130 | + * @throws EE_Error |
|
131 | + */ |
|
132 | + public function e_value($schema = null) |
|
133 | + { |
|
134 | + $this->e('ANS_value', $schema); |
|
135 | + } |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * Gets the related EE_Question to this EE_Answer |
|
140 | + * |
|
141 | + * @return EE_Base_Class|EE_Question |
|
142 | + * @throws EE_Error |
|
143 | + * @throws ReflectionException |
|
144 | + */ |
|
145 | + public function question() |
|
146 | + { |
|
147 | + return $this->get_first_related('Question'); |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * Gets the related EE_Registration to this EE_Answer |
|
153 | + * |
|
154 | + * @return EE_Base_Class|EE_Registration |
|
155 | + * @throws EE_Error |
|
156 | + * @throws ReflectionException |
|
157 | + */ |
|
158 | + public function registration() |
|
159 | + { |
|
160 | + return $this->get_first_related('Registration'); |
|
161 | + } |
|
162 | 162 | } |
@@ -13,625 +13,625 @@ |
||
13 | 13 | class EE_CSV |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * string used for 1st cell in exports, |
|
18 | - * which indicates that the following 2 rows will be metadata keys and values |
|
19 | - */ |
|
20 | - const metadata_header = 'Event Espresso Export Meta Data'; |
|
21 | - |
|
22 | - |
|
23 | - /** |
|
24 | - * instance of the EE_CSV object |
|
25 | - * |
|
26 | - * @var EE_CSV |
|
27 | - */ |
|
28 | - private static $_instance; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - private $_primary_keys; |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * private constructor to prevent direct creation |
|
38 | - * |
|
39 | - * @return void |
|
40 | - */ |
|
41 | - private function __construct() |
|
42 | - { |
|
43 | - global $wpdb; |
|
44 | - |
|
45 | - $this->_primary_keys = [ |
|
46 | - $wpdb->prefix . 'esp_answer' => ['ANS_ID'], |
|
47 | - $wpdb->prefix . 'esp_attendee' => ['ATT_ID'], |
|
48 | - $wpdb->prefix . 'esp_datetime' => ['DTT_ID'], |
|
49 | - $wpdb->prefix . 'esp_event_question_group' => ['EQG_ID'], |
|
50 | - $wpdb->prefix . 'esp_message_template' => ['MTP_ID'], |
|
51 | - $wpdb->prefix . 'esp_payment' => ['PAY_ID'], |
|
52 | - $wpdb->prefix . 'esp_price' => ['PRC_ID'], |
|
53 | - $wpdb->prefix . 'esp_price_type' => ['PRT_ID'], |
|
54 | - $wpdb->prefix . 'esp_question' => ['QST_ID'], |
|
55 | - $wpdb->prefix . 'esp_question_group' => ['QSG_ID'], |
|
56 | - $wpdb->prefix . 'esp_question_group_question' => ['QGQ_ID'], |
|
57 | - $wpdb->prefix . 'esp_question_option' => ['QSO_ID'], |
|
58 | - $wpdb->prefix . 'esp_registration' => ['REG_ID'], |
|
59 | - $wpdb->prefix . 'esp_status' => ['STS_ID'], |
|
60 | - $wpdb->prefix . 'esp_transaction' => ['TXN_ID'], |
|
61 | - $wpdb->prefix . 'esp_transaction' => ['TXN_ID'], |
|
62 | - $wpdb->prefix . 'events_detail' => ['id'], |
|
63 | - $wpdb->prefix . 'events_category_detail' => ['id'], |
|
64 | - $wpdb->prefix . 'events_category_rel' => ['id'], |
|
65 | - $wpdb->prefix . 'events_venue' => ['id'], |
|
66 | - $wpdb->prefix . 'events_venue_rel' => ['emeta_id'], |
|
67 | - $wpdb->prefix . 'events_locale' => ['id'], |
|
68 | - $wpdb->prefix . 'events_locale_rel' => ['id'], |
|
69 | - $wpdb->prefix . 'events_personnel' => ['id'], |
|
70 | - $wpdb->prefix . 'events_personnel_rel' => ['id'], |
|
71 | - ]; |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * @ singleton method used to instantiate class object |
|
77 | - * @ access public |
|
78 | - * |
|
79 | - * @return EE_CSV |
|
80 | - */ |
|
81 | - public static function instance() |
|
82 | - { |
|
83 | - // check if class object is instantiated |
|
84 | - if (! self::$_instance instanceof EE_CSV) { |
|
85 | - self::$_instance = new self(); |
|
86 | - } |
|
87 | - return self::$_instance; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * @Import contents of csv file and store values in an array to be manipulated by other functions |
|
93 | - * @access public |
|
94 | - * @param string $path_to_file - the csv file to be imported including the path to it's location. |
|
95 | - * If $model_name is provided, assumes that each row in the CSV represents a |
|
96 | - * model object for that model If $model_name ISN'T provided, assumes that |
|
97 | - * before model object data, there is a row where the first entry is simply |
|
98 | - * 'MODEL', and next entry is the model's name, (untranslated) like Event, and |
|
99 | - * then maybe a row of headers, and then the model data. Eg. |
|
100 | - * '<br>MODEL,Event,<br>EVT_ID,EVT_name,...<br>1,Monkey |
|
101 | - * Party,...<br>2,Llamarama,...<br>MODEL,Venue,<br>VNU_ID,VNU_name<br>1,The |
|
102 | - * Forest |
|
103 | - * @param string $model_name model name if we know what model we're importing |
|
104 | - * @param boolean $first_row_is_headers - whether the first row of data is headers or not - TRUE = headers, FALSE = |
|
105 | - * data |
|
106 | - * @return mixed - array on success - multi dimensional with headers as keys (if headers exist) OR string on fail - |
|
107 | - * error message like the following array('Event'=>array( |
|
108 | - * array('EVT_ID'=>1,'EVT_name'=>'bob party',...), |
|
109 | - * array('EVT_ID'=>2,'EVT_name'=>'llamarama',...), |
|
110 | - * ... |
|
111 | - * ) |
|
112 | - * 'Venue'=>array( |
|
113 | - * array('VNU_ID'=>1,'VNU_name'=>'the shack',...), |
|
114 | - * array('VNU_ID'=>2,'VNU_name'=>'tree house',...), |
|
115 | - * ... |
|
116 | - * ) |
|
117 | - * ... |
|
118 | - * ) |
|
119 | - * @throws EE_Error |
|
120 | - */ |
|
121 | - public function import_csv_to_model_data_array($path_to_file, $model_name = false, $first_row_is_headers = true) |
|
122 | - { |
|
123 | - $multi_dimensional_array = $this->import_csv_to_multi_dimensional_array($path_to_file); |
|
124 | - if (empty($multi_dimensional_array)) { |
|
125 | - return false; |
|
126 | - } |
|
127 | - // gotta start somewhere |
|
128 | - $row = 1; |
|
129 | - // array to store csv data in |
|
130 | - $ee_formatted_data = []; |
|
131 | - // array to store headers (column names) |
|
132 | - $headers = []; |
|
133 | - foreach ($multi_dimensional_array as $data) { |
|
134 | - // if first cell is MODEL, then second cell is the MODEL name |
|
135 | - if ($data[0] == 'MODEL') { |
|
136 | - $model_name = $data[1]; |
|
137 | - // don't bother looking for model data in this row. |
|
138 | - // The rest of this row should be blank |
|
139 | - // AND pretend this is the first row again |
|
140 | - $row = 1; |
|
141 | - // reset headers |
|
142 | - $headers = []; |
|
143 | - continue; |
|
144 | - } |
|
145 | - if (strpos($data[0], EE_CSV::metadata_header) !== false) { |
|
146 | - $model_name = EE_CSV::metadata_header; |
|
147 | - // store like model data, we just won't try importing it etc. |
|
148 | - $row = 1; |
|
149 | - continue; |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - // how many columns are there? |
|
154 | - $columns = count($data); |
|
155 | - |
|
156 | - $model_entry = []; |
|
157 | - // loop through each column |
|
158 | - for ($i = 0; $i < $columns; $i++) { |
|
159 | - // replace csv_enclosures with backslashed quotes |
|
160 | - $data[ $i ] = str_replace('"""', '\\"', $data[ $i ]); |
|
161 | - // do we need to grab the column names? |
|
162 | - if ($row === 1) { |
|
163 | - if ($first_row_is_headers) { |
|
164 | - // store the column names to use for keys |
|
165 | - $column_name = $data[ $i ]; |
|
166 | - // check it's not blank... sometimes CSV editing programs |
|
167 | - // add a bunch of empty columns onto the end... |
|
168 | - if (! $column_name) { |
|
169 | - continue; |
|
170 | - } |
|
171 | - $matches = []; |
|
172 | - if ($model_name == EE_CSV::metadata_header) { |
|
173 | - $headers[ $i ] = $column_name; |
|
174 | - } else { |
|
175 | - // now get the db table name from it (the part between square brackets) |
|
176 | - $success = preg_match('~(.*)\[(.*)\]~', $column_name, $matches); |
|
177 | - if (! $success) { |
|
178 | - EE_Error::add_error( |
|
179 | - sprintf( |
|
180 | - esc_html__( |
|
181 | - "The column titled %s is invalid for importing. It must be be in the format of 'Nice Name[model_field_name]' in row %s", |
|
182 | - "event_espresso" |
|
183 | - ), |
|
184 | - $column_name, |
|
185 | - implode(",", $data) |
|
186 | - ), |
|
187 | - __FILE__, |
|
188 | - __FUNCTION__, |
|
189 | - __LINE__ |
|
190 | - ); |
|
191 | - return false; |
|
192 | - } |
|
193 | - $headers[ $i ] = $matches[2]; |
|
194 | - } |
|
195 | - } else { |
|
196 | - // no column names means our final array will just use counters for keys |
|
197 | - $model_entry[ $headers[ $i ] ] = $data[ $i ]; |
|
198 | - $headers[ $i ] = $i; |
|
199 | - } |
|
200 | - // and we need to store csv data |
|
201 | - } else { |
|
202 | - // this column isn' ta header, store it if there is a header for it |
|
203 | - if (isset($headers[ $i ])) { |
|
204 | - $model_entry[ $headers[ $i ] ] = $data[ $i ]; |
|
205 | - } |
|
206 | - } |
|
207 | - } |
|
208 | - // save the row's data IF it's a non-header-row |
|
209 | - if (! $first_row_is_headers || $row > 1) { |
|
210 | - $ee_formatted_data[ $model_name ][] = $model_entry; |
|
211 | - } |
|
212 | - // advance to next row |
|
213 | - $row++; |
|
214 | - } |
|
215 | - |
|
216 | - // delete the uploaded file |
|
217 | - unlink($path_to_file); |
|
218 | - |
|
219 | - // it's good to give back |
|
220 | - return $ee_formatted_data; |
|
221 | - } |
|
222 | - |
|
223 | - |
|
224 | - /** |
|
225 | - * Generic CSV-functionality to turn an entire CSV file into a single array that's |
|
226 | - * NOT in a specific format to EE. It's just a 2-level array, with top-level arrays |
|
227 | - * representing each row in the CSV file, and the second-level arrays being each column in that row |
|
228 | - * |
|
229 | - * @param string $path_to_file |
|
230 | - * @return array of arrays. Top-level array has rows, second-level array has each item |
|
231 | - * @throws EE_Error |
|
232 | - */ |
|
233 | - public function import_csv_to_multi_dimensional_array($path_to_file) |
|
234 | - { |
|
235 | - // needed to deal with Mac line endings |
|
236 | - ini_set('auto_detect_line_endings', true); |
|
237 | - // because fgetcsv does not correctly deal with backslashed quotes such as \" |
|
238 | - // we'll read the file into a string |
|
239 | - $file_contents = $this->read_unicode_file($path_to_file); |
|
240 | - // replace backslashed quotes with CSV enclosures |
|
241 | - $file_contents = str_replace('\\"', '"""', $file_contents); |
|
242 | - // HEY YOU! PUT THAT FILE BACK!!! |
|
243 | - file_put_contents($path_to_file, $file_contents); |
|
244 | - |
|
245 | - if (($file_handle = fopen($path_to_file, "r")) !== false) { |
|
246 | - $csv_array = []; |
|
247 | - // in PHP 5.3 fgetcsv accepts a 5th parameter, but the pre 5.3 versions |
|
248 | - // of fgetcsv choke if passed more than 4 - is that crazy or what? |
|
249 | - if (version_compare(PHP_VERSION, '5.3.0') < 0) { |
|
250 | - // PHP 5.2- version |
|
251 | - // loop through each row of the file |
|
252 | - while (($data = fgetcsv($file_handle)) !== false) { |
|
253 | - $csv_array[] = $data; |
|
254 | - } |
|
255 | - } else { |
|
256 | - // loop through each row of the file |
|
257 | - while (($data = fgetcsv($file_handle)) !== false) { |
|
258 | - $csv_array[] = $data; |
|
259 | - } |
|
260 | - } |
|
261 | - # Close the File. |
|
262 | - fclose($file_handle); |
|
263 | - return $csv_array; |
|
264 | - } |
|
265 | - EE_Error::add_error( |
|
266 | - sprintf( |
|
267 | - esc_html__( |
|
268 | - "An error occurred - the file: %s could not opened.", |
|
269 | - "event_espresso" |
|
270 | - ), |
|
271 | - $path_to_file |
|
272 | - ), |
|
273 | - __FILE__, |
|
274 | - __FUNCTION__, |
|
275 | - __LINE__ |
|
276 | - ); |
|
277 | - return []; |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * Opens a unicode or utf file |
|
283 | - * (normal file_get_contents has difficulty reading a unicode file) |
|
284 | - * |
|
285 | - * @param string $file_path |
|
286 | - * @return string |
|
287 | - * @throws EE_Error |
|
288 | - * @see http://stackoverflow.com/questions/15092764/how-to-read-unicode-text-file-in-php |
|
289 | - */ |
|
290 | - private function read_unicode_file($file_path) |
|
291 | - { |
|
292 | - $fc = ""; |
|
293 | - $fh = fopen($file_path, "rb"); |
|
294 | - if (! $fh) { |
|
295 | - throw new EE_Error( |
|
296 | - sprintf( |
|
297 | - esc_html__("Cannot open file for read: %s<br>\n", 'event_espresso'), |
|
298 | - $file_path |
|
299 | - ) |
|
300 | - ); |
|
301 | - } |
|
302 | - $file_length = filesize($file_path); |
|
303 | - $bc = fread($fh, $file_length); |
|
304 | - for ($i = 0; $i < $file_length; $i++) { |
|
305 | - $c = substr($bc, $i, 1); |
|
306 | - if ((ord($c) != 0) && (ord($c) != 13)) { |
|
307 | - $fc = $fc . $c; |
|
308 | - } |
|
309 | - } |
|
310 | - if ((ord(substr($fc, 0, 1)) == 255) && (ord(substr($fc, 1, 1)) == 254)) { |
|
311 | - $fc = substr($fc, 2); |
|
312 | - } |
|
313 | - return ($fc); |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * @param $csv_data_array |
|
319 | - * @param false $model_name |
|
320 | - * @return bool |
|
321 | - * @throws EE_Error |
|
322 | - */ |
|
323 | - public function save_csv_to_db($csv_data_array, $model_name = false) |
|
324 | - { |
|
325 | - EE_Error::doing_it_wrong( |
|
326 | - 'save_csv_to_db', |
|
327 | - esc_html__( |
|
328 | - 'Function moved to EE_Import and renamed to save_csv_data_array_to_db', |
|
329 | - 'event_espresso' |
|
330 | - ), |
|
331 | - '4.6.7' |
|
332 | - ); |
|
333 | - return EE_Import::instance()->save_csv_data_array_to_db($csv_data_array, $model_name); |
|
334 | - } |
|
335 | - |
|
336 | - |
|
337 | - /** |
|
338 | - * Writes the CSV file to the output buffer, with rows corresponding to $model_data_array, |
|
339 | - * and dies (in order to avoid other plugins from messing up the csv output) |
|
340 | - * |
|
341 | - * @param string $filename the filename you want to give the file |
|
342 | - * @param array $model_data_array 3d array, as described in EE_CSV::write_model_data_to_csv() |
|
343 | - * @return bool | void writes CSV file to output and dies |
|
344 | - * @throws EE_Error |
|
345 | - * @throws ReflectionException |
|
346 | - */ |
|
347 | - public function export_multiple_model_data_to_csv($filename, $model_data_array) |
|
348 | - { |
|
349 | - $file_handle = $this->begin_sending_csv($filename); |
|
350 | - $this->write_model_data_to_csv($file_handle, $model_data_array); |
|
351 | - $this->end_sending_csv($file_handle); |
|
352 | - } |
|
353 | - |
|
354 | - |
|
355 | - /** |
|
356 | - * Sends HTTP headers to indicate that the browser should download a file, |
|
357 | - * and starts writing the file to PHP's output. Returns the file handle so other functions can |
|
358 | - * also write to it |
|
359 | - * |
|
360 | - * @param string $filename the name of the file that the user will download |
|
361 | - * @return resource, like the results of fopen(), which can be used for fwrite, fputcsv2, etc. |
|
362 | - */ |
|
363 | - public function begin_sending_csv($filename) |
|
364 | - { |
|
365 | - // grab file extension |
|
366 | - $ext = substr(strrchr($filename, '.'), 1); |
|
367 | - if ($ext == '.csv' or $ext == '.xls') { |
|
368 | - str_replace($ext, '', $filename); |
|
369 | - } |
|
370 | - $filename .= '.csv'; |
|
371 | - |
|
372 | - // if somebody's been naughty and already started outputting stuff, trash it |
|
373 | - // and start writing our stuff. |
|
374 | - if (ob_get_length()) { |
|
375 | - @ob_flush(); |
|
376 | - @flush(); |
|
377 | - @ob_end_flush(); |
|
378 | - } |
|
379 | - @ob_start(); |
|
380 | - header("Pragma: public"); |
|
381 | - header("Expires: 0"); |
|
382 | - header("Pragma: no-cache"); |
|
383 | - header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); |
|
384 | - // header("Content-Type: application/force-download"); |
|
385 | - // header("Content-Type: application/octet-stream"); |
|
386 | - // header("Content-Type: application/download"); |
|
387 | - header('Content-disposition: attachment; filename=' . $filename); |
|
388 | - header("Content-Type: text/csv; charset=utf-8"); |
|
389 | - do_action('AHEE__EE_CSV__begin_sending_csv__headers'); |
|
390 | - echo apply_filters( |
|
391 | - 'FHEE__EE_CSV__begin_sending_csv__start_writing', |
|
392 | - "\xEF\xBB\xBF" |
|
393 | - ); // makes excel open it as UTF-8. UTF-8 BOM, see http://stackoverflow.com/a/4440143/2773835 |
|
394 | - return fopen('php://output', 'w'); |
|
395 | - } |
|
396 | - |
|
397 | - |
|
398 | - /** |
|
399 | - * Given an open file, writes all the model data to it in the format the importer expects. |
|
400 | - * Usually preceded by begin_sending_csv($filename), and followed by end_sending_csv($file_handle). |
|
401 | - * |
|
402 | - * @param resource $file_handle |
|
403 | - * @param array $model_data_array is assumed to be a 3d array: 1st layer has keys of model names (eg 'Event'), |
|
404 | - * next layer is numerically indexed to represent each model object (eg, each |
|
405 | - * individual event), and the last layer has all the attributes of that model |
|
406 | - * object (eg, the event's id, name, etc) |
|
407 | - * @return void |
|
408 | - * @throws EE_Error |
|
409 | - * @throws ReflectionException |
|
410 | - */ |
|
411 | - public function write_model_data_to_csv($file_handle, $model_data_array) |
|
412 | - { |
|
413 | - $this->write_metadata_to_csv($file_handle); |
|
414 | - foreach ($model_data_array as $model_name => $model_instance_arrays) { |
|
415 | - // first: output a special row stating the model |
|
416 | - $this->fputcsv2($file_handle, ['MODEL', $model_name]); |
|
417 | - // if we have items to put in the CSV, do it normally |
|
418 | - |
|
419 | - if (! empty($model_instance_arrays)) { |
|
420 | - $this->write_data_array_to_csv($file_handle, $model_instance_arrays); |
|
421 | - } else { |
|
422 | - // echo "no data to write... so just write the headers"; |
|
423 | - // so there's actually NO model objects for that model. |
|
424 | - // probably still want to show the columns |
|
425 | - $model = EE_Registry::instance()->load_model($model_name); |
|
426 | - $column_names = []; |
|
427 | - foreach ($model->field_settings() as $field) { |
|
428 | - $column_names[ $field->get_nicename() . "[" . $field->get_name() . "]" ] = null; |
|
429 | - } |
|
430 | - $this->write_data_array_to_csv($file_handle, [$column_names]); |
|
431 | - } |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * Writes some meta data to the CSV as a bunch of columns. Initially we're only |
|
438 | - * mentioning the version and timezone |
|
439 | - * |
|
440 | - * @param resource $file_handle |
|
441 | - * @throws EE_Error |
|
442 | - * @throws EE_Error |
|
443 | - */ |
|
444 | - public function write_metadata_to_csv($file_handle) |
|
445 | - { |
|
446 | - $data_row = [EE_CSV::metadata_header];// do NOT translate because this exact string is used when importing |
|
447 | - $this->fputcsv2($file_handle, $data_row); |
|
448 | - $meta_data = [ |
|
449 | - 0 => [ |
|
450 | - 'version' => espresso_version(), |
|
451 | - 'timezone' => EEH_DTT_Helper::get_timezone(), |
|
452 | - 'time_of_export' => current_time('mysql'), |
|
453 | - 'site_url' => site_url(), |
|
454 | - ], |
|
455 | - ]; |
|
456 | - $this->write_data_array_to_csv($file_handle, $meta_data); |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - /** |
|
461 | - * @Drop in replacement for PHP's fputcsv function - but this one works!!! |
|
462 | - * @access private |
|
463 | - * @param resource $fh - file handle - what we are writing to |
|
464 | - * @param array $row - individual row of csv data |
|
465 | - * @param string $delimiter - csv delimiter |
|
466 | - * @param string $enclosure - csv enclosure |
|
467 | - * @param string $mysql_null - allows php NULL to be overridden with MySQl's insertable NULL value |
|
468 | - * @return void |
|
469 | - */ |
|
470 | - private function fputcsv2($fh, array $row, $delimiter = ',', $enclosure = '"', $mysql_null = false) |
|
471 | - { |
|
472 | - // Allow user to filter the csv delimiter and enclosure for other countries csv standards |
|
473 | - $delimiter = apply_filters('FHEE__EE_CSV__fputcsv2__delimiter', $delimiter); |
|
474 | - $enclosure = apply_filters('FHEE__EE_CSV__fputcsv2__enclosure', $enclosure); |
|
475 | - |
|
476 | - $delimiter_esc = preg_quote($delimiter, '/'); |
|
477 | - $enclosure_esc = preg_quote($enclosure, '/'); |
|
478 | - |
|
479 | - $output = []; |
|
480 | - foreach ($row as $field_value) { |
|
481 | - if (is_object($field_value) || is_array($field_value)) { |
|
482 | - $field_value = serialize($field_value); |
|
483 | - } |
|
484 | - if ($field_value === null && $mysql_null) { |
|
485 | - $output[] = 'NULL'; |
|
486 | - continue; |
|
487 | - } |
|
488 | - |
|
489 | - $output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field_value) |
|
490 | - ? ($enclosure . str_replace($enclosure, $enclosure . $enclosure, $field_value) . $enclosure) |
|
491 | - : $field_value; |
|
492 | - } |
|
493 | - |
|
494 | - fwrite($fh, join($delimiter, $output) . PHP_EOL); |
|
495 | - } |
|
496 | - |
|
497 | - |
|
498 | - /** |
|
499 | - * Writes $data to the csv file open in $file_handle. uses the array indices of $data for column headers |
|
500 | - * |
|
501 | - * @param resource $file_handle |
|
502 | - * @param array $data 2D array, first numerically-indexed, |
|
503 | - * and next-level-down preferably indexed by string |
|
504 | - * @return boolean if we successfully wrote to the CSV or not. |
|
505 | - * If there's no $data, we consider that a success |
|
506 | - * (because we wrote everything there was...nothing) |
|
507 | - * @throws EE_Error |
|
508 | - * @throws EE_Error |
|
509 | - */ |
|
510 | - public function write_data_array_to_csv($file_handle, $data) |
|
511 | - { |
|
512 | - // determine if $data is actually a 2d array |
|
513 | - if ($data && is_array($data) && is_array(EEH_Array::get_one_item_from_array($data))) { |
|
514 | - // make sure top level is numerically indexed, |
|
515 | - |
|
516 | - if (EEH_Array::is_associative_array($data)) { |
|
517 | - throw new EE_Error( |
|
518 | - sprintf( |
|
519 | - esc_html__( |
|
520 | - "top-level array must be numerically indexed. Does these look like numbers to you? %s", |
|
521 | - "event_espresso" |
|
522 | - ), |
|
523 | - implode(",", array_keys($data)) |
|
524 | - ) |
|
525 | - ); |
|
526 | - } |
|
527 | - $item_in_top_level_array = EEH_Array::get_one_item_from_array($data); |
|
528 | - // now, is the last item in the top-level array of $data an associative or numeric array? |
|
529 | - if (EEH_Array::is_associative_array($item_in_top_level_array)) { |
|
530 | - // its associative, so we want to output its keys as column headers |
|
531 | - $keys = array_keys($item_in_top_level_array); |
|
532 | - $this->fputcsv2($file_handle, $keys); |
|
533 | - } |
|
534 | - // start writing data |
|
535 | - foreach ($data as $data_row) { |
|
536 | - $this->fputcsv2($file_handle, $data_row); |
|
537 | - } |
|
538 | - return true; |
|
539 | - } |
|
540 | - // no data TO write... so we can assume that's a success |
|
541 | - return true; |
|
542 | - } |
|
543 | - |
|
544 | - |
|
545 | - /** |
|
546 | - * Should be called after begin_sending_csv(), and one or more write_data_array_to_csv()s. |
|
547 | - * Calls exit to prevent polluting the CSV file with other junk |
|
548 | - * |
|
549 | - * @param resource $fh file_handle where we're writing the CSV to |
|
550 | - */ |
|
551 | - public function end_sending_csv($fh) |
|
552 | - { |
|
553 | - fclose($fh); |
|
554 | - exit(0); |
|
555 | - } |
|
556 | - |
|
557 | - |
|
558 | - /** |
|
559 | - * @Export contents of an array to csv file |
|
560 | - * @access public |
|
561 | - * @param array $data - the array of data to be converted to csv and exported |
|
562 | - * @param string $filename - name for newly created csv file |
|
563 | - * @return bool TRUE on success, FALSE on fail |
|
564 | - */ |
|
565 | - public function export_array_to_csv($data = false, $filename = false) |
|
566 | - { |
|
567 | - // no data file?? get outta here |
|
568 | - if (! $data or ! is_array($data) or empty($data)) { |
|
569 | - return false; |
|
570 | - } |
|
571 | - |
|
572 | - // no filename?? get outta here |
|
573 | - if (! $filename) { |
|
574 | - return false; |
|
575 | - } |
|
576 | - $fh = $this->begin_sending_csv($filename); |
|
577 | - $this->end_sending_csv($fh); |
|
578 | - return true; |
|
579 | - } |
|
580 | - |
|
581 | - |
|
582 | - /** |
|
583 | - * @Determine the maximum upload file size based on php.ini settings |
|
584 | - * @access public |
|
585 | - * @param int $percent_of_max - desired percentage of the max upload_mb |
|
586 | - * @return int KB |
|
587 | - */ |
|
588 | - public function get_max_upload_size($percent_of_max = false) |
|
589 | - { |
|
590 | - $max_upload = (int) (ini_get('upload_max_filesize')); |
|
591 | - $max_post = (int) (ini_get('post_max_size')); |
|
592 | - $memory_limit = (int) (ini_get('memory_limit')); |
|
593 | - |
|
594 | - // determine the smallest of the three values from above |
|
595 | - $upload_mb = min($max_upload, $max_post, $memory_limit); |
|
596 | - |
|
597 | - // convert MB to KB |
|
598 | - $upload_mb = $upload_mb * 1024; |
|
599 | - |
|
600 | - // don't want the full monty? then reduce the max upload size |
|
601 | - if ($percent_of_max) { |
|
602 | - // is percent_of_max like this -> 50 or like this -> 0.50 ? |
|
603 | - if ($percent_of_max > 1) { |
|
604 | - // changes 50 to 0.50 |
|
605 | - $percent_of_max = $percent_of_max / 100; |
|
606 | - } |
|
607 | - // make upload_mb a percentage of the max upload_mb |
|
608 | - $upload_mb = $upload_mb * $percent_of_max; |
|
609 | - } |
|
610 | - |
|
611 | - return $upload_mb; |
|
612 | - } |
|
613 | - |
|
614 | - |
|
615 | - /** |
|
616 | - * Gets the date format to use in teh csv. filterable |
|
617 | - * |
|
618 | - * @param string $current_format |
|
619 | - * @return string |
|
620 | - */ |
|
621 | - public function get_date_format_for_csv($current_format = null) |
|
622 | - { |
|
623 | - return apply_filters('FHEE__EE_CSV__get_date_format_for_csv__format', 'Y-m-d', $current_format); |
|
624 | - } |
|
625 | - |
|
626 | - |
|
627 | - /** |
|
628 | - * Gets the time format we want to use in CSV reports. Filterable |
|
629 | - * |
|
630 | - * @param string $current_format |
|
631 | - * @return string |
|
632 | - */ |
|
633 | - public function get_time_format_for_csv($current_format = null) |
|
634 | - { |
|
635 | - return apply_filters('FHEE__EE_CSV__get_time_format_for_csv__format', 'H:i:s', $current_format); |
|
636 | - } |
|
16 | + /** |
|
17 | + * string used for 1st cell in exports, |
|
18 | + * which indicates that the following 2 rows will be metadata keys and values |
|
19 | + */ |
|
20 | + const metadata_header = 'Event Espresso Export Meta Data'; |
|
21 | + |
|
22 | + |
|
23 | + /** |
|
24 | + * instance of the EE_CSV object |
|
25 | + * |
|
26 | + * @var EE_CSV |
|
27 | + */ |
|
28 | + private static $_instance; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + private $_primary_keys; |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * private constructor to prevent direct creation |
|
38 | + * |
|
39 | + * @return void |
|
40 | + */ |
|
41 | + private function __construct() |
|
42 | + { |
|
43 | + global $wpdb; |
|
44 | + |
|
45 | + $this->_primary_keys = [ |
|
46 | + $wpdb->prefix . 'esp_answer' => ['ANS_ID'], |
|
47 | + $wpdb->prefix . 'esp_attendee' => ['ATT_ID'], |
|
48 | + $wpdb->prefix . 'esp_datetime' => ['DTT_ID'], |
|
49 | + $wpdb->prefix . 'esp_event_question_group' => ['EQG_ID'], |
|
50 | + $wpdb->prefix . 'esp_message_template' => ['MTP_ID'], |
|
51 | + $wpdb->prefix . 'esp_payment' => ['PAY_ID'], |
|
52 | + $wpdb->prefix . 'esp_price' => ['PRC_ID'], |
|
53 | + $wpdb->prefix . 'esp_price_type' => ['PRT_ID'], |
|
54 | + $wpdb->prefix . 'esp_question' => ['QST_ID'], |
|
55 | + $wpdb->prefix . 'esp_question_group' => ['QSG_ID'], |
|
56 | + $wpdb->prefix . 'esp_question_group_question' => ['QGQ_ID'], |
|
57 | + $wpdb->prefix . 'esp_question_option' => ['QSO_ID'], |
|
58 | + $wpdb->prefix . 'esp_registration' => ['REG_ID'], |
|
59 | + $wpdb->prefix . 'esp_status' => ['STS_ID'], |
|
60 | + $wpdb->prefix . 'esp_transaction' => ['TXN_ID'], |
|
61 | + $wpdb->prefix . 'esp_transaction' => ['TXN_ID'], |
|
62 | + $wpdb->prefix . 'events_detail' => ['id'], |
|
63 | + $wpdb->prefix . 'events_category_detail' => ['id'], |
|
64 | + $wpdb->prefix . 'events_category_rel' => ['id'], |
|
65 | + $wpdb->prefix . 'events_venue' => ['id'], |
|
66 | + $wpdb->prefix . 'events_venue_rel' => ['emeta_id'], |
|
67 | + $wpdb->prefix . 'events_locale' => ['id'], |
|
68 | + $wpdb->prefix . 'events_locale_rel' => ['id'], |
|
69 | + $wpdb->prefix . 'events_personnel' => ['id'], |
|
70 | + $wpdb->prefix . 'events_personnel_rel' => ['id'], |
|
71 | + ]; |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * @ singleton method used to instantiate class object |
|
77 | + * @ access public |
|
78 | + * |
|
79 | + * @return EE_CSV |
|
80 | + */ |
|
81 | + public static function instance() |
|
82 | + { |
|
83 | + // check if class object is instantiated |
|
84 | + if (! self::$_instance instanceof EE_CSV) { |
|
85 | + self::$_instance = new self(); |
|
86 | + } |
|
87 | + return self::$_instance; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * @Import contents of csv file and store values in an array to be manipulated by other functions |
|
93 | + * @access public |
|
94 | + * @param string $path_to_file - the csv file to be imported including the path to it's location. |
|
95 | + * If $model_name is provided, assumes that each row in the CSV represents a |
|
96 | + * model object for that model If $model_name ISN'T provided, assumes that |
|
97 | + * before model object data, there is a row where the first entry is simply |
|
98 | + * 'MODEL', and next entry is the model's name, (untranslated) like Event, and |
|
99 | + * then maybe a row of headers, and then the model data. Eg. |
|
100 | + * '<br>MODEL,Event,<br>EVT_ID,EVT_name,...<br>1,Monkey |
|
101 | + * Party,...<br>2,Llamarama,...<br>MODEL,Venue,<br>VNU_ID,VNU_name<br>1,The |
|
102 | + * Forest |
|
103 | + * @param string $model_name model name if we know what model we're importing |
|
104 | + * @param boolean $first_row_is_headers - whether the first row of data is headers or not - TRUE = headers, FALSE = |
|
105 | + * data |
|
106 | + * @return mixed - array on success - multi dimensional with headers as keys (if headers exist) OR string on fail - |
|
107 | + * error message like the following array('Event'=>array( |
|
108 | + * array('EVT_ID'=>1,'EVT_name'=>'bob party',...), |
|
109 | + * array('EVT_ID'=>2,'EVT_name'=>'llamarama',...), |
|
110 | + * ... |
|
111 | + * ) |
|
112 | + * 'Venue'=>array( |
|
113 | + * array('VNU_ID'=>1,'VNU_name'=>'the shack',...), |
|
114 | + * array('VNU_ID'=>2,'VNU_name'=>'tree house',...), |
|
115 | + * ... |
|
116 | + * ) |
|
117 | + * ... |
|
118 | + * ) |
|
119 | + * @throws EE_Error |
|
120 | + */ |
|
121 | + public function import_csv_to_model_data_array($path_to_file, $model_name = false, $first_row_is_headers = true) |
|
122 | + { |
|
123 | + $multi_dimensional_array = $this->import_csv_to_multi_dimensional_array($path_to_file); |
|
124 | + if (empty($multi_dimensional_array)) { |
|
125 | + return false; |
|
126 | + } |
|
127 | + // gotta start somewhere |
|
128 | + $row = 1; |
|
129 | + // array to store csv data in |
|
130 | + $ee_formatted_data = []; |
|
131 | + // array to store headers (column names) |
|
132 | + $headers = []; |
|
133 | + foreach ($multi_dimensional_array as $data) { |
|
134 | + // if first cell is MODEL, then second cell is the MODEL name |
|
135 | + if ($data[0] == 'MODEL') { |
|
136 | + $model_name = $data[1]; |
|
137 | + // don't bother looking for model data in this row. |
|
138 | + // The rest of this row should be blank |
|
139 | + // AND pretend this is the first row again |
|
140 | + $row = 1; |
|
141 | + // reset headers |
|
142 | + $headers = []; |
|
143 | + continue; |
|
144 | + } |
|
145 | + if (strpos($data[0], EE_CSV::metadata_header) !== false) { |
|
146 | + $model_name = EE_CSV::metadata_header; |
|
147 | + // store like model data, we just won't try importing it etc. |
|
148 | + $row = 1; |
|
149 | + continue; |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + // how many columns are there? |
|
154 | + $columns = count($data); |
|
155 | + |
|
156 | + $model_entry = []; |
|
157 | + // loop through each column |
|
158 | + for ($i = 0; $i < $columns; $i++) { |
|
159 | + // replace csv_enclosures with backslashed quotes |
|
160 | + $data[ $i ] = str_replace('"""', '\\"', $data[ $i ]); |
|
161 | + // do we need to grab the column names? |
|
162 | + if ($row === 1) { |
|
163 | + if ($first_row_is_headers) { |
|
164 | + // store the column names to use for keys |
|
165 | + $column_name = $data[ $i ]; |
|
166 | + // check it's not blank... sometimes CSV editing programs |
|
167 | + // add a bunch of empty columns onto the end... |
|
168 | + if (! $column_name) { |
|
169 | + continue; |
|
170 | + } |
|
171 | + $matches = []; |
|
172 | + if ($model_name == EE_CSV::metadata_header) { |
|
173 | + $headers[ $i ] = $column_name; |
|
174 | + } else { |
|
175 | + // now get the db table name from it (the part between square brackets) |
|
176 | + $success = preg_match('~(.*)\[(.*)\]~', $column_name, $matches); |
|
177 | + if (! $success) { |
|
178 | + EE_Error::add_error( |
|
179 | + sprintf( |
|
180 | + esc_html__( |
|
181 | + "The column titled %s is invalid for importing. It must be be in the format of 'Nice Name[model_field_name]' in row %s", |
|
182 | + "event_espresso" |
|
183 | + ), |
|
184 | + $column_name, |
|
185 | + implode(",", $data) |
|
186 | + ), |
|
187 | + __FILE__, |
|
188 | + __FUNCTION__, |
|
189 | + __LINE__ |
|
190 | + ); |
|
191 | + return false; |
|
192 | + } |
|
193 | + $headers[ $i ] = $matches[2]; |
|
194 | + } |
|
195 | + } else { |
|
196 | + // no column names means our final array will just use counters for keys |
|
197 | + $model_entry[ $headers[ $i ] ] = $data[ $i ]; |
|
198 | + $headers[ $i ] = $i; |
|
199 | + } |
|
200 | + // and we need to store csv data |
|
201 | + } else { |
|
202 | + // this column isn' ta header, store it if there is a header for it |
|
203 | + if (isset($headers[ $i ])) { |
|
204 | + $model_entry[ $headers[ $i ] ] = $data[ $i ]; |
|
205 | + } |
|
206 | + } |
|
207 | + } |
|
208 | + // save the row's data IF it's a non-header-row |
|
209 | + if (! $first_row_is_headers || $row > 1) { |
|
210 | + $ee_formatted_data[ $model_name ][] = $model_entry; |
|
211 | + } |
|
212 | + // advance to next row |
|
213 | + $row++; |
|
214 | + } |
|
215 | + |
|
216 | + // delete the uploaded file |
|
217 | + unlink($path_to_file); |
|
218 | + |
|
219 | + // it's good to give back |
|
220 | + return $ee_formatted_data; |
|
221 | + } |
|
222 | + |
|
223 | + |
|
224 | + /** |
|
225 | + * Generic CSV-functionality to turn an entire CSV file into a single array that's |
|
226 | + * NOT in a specific format to EE. It's just a 2-level array, with top-level arrays |
|
227 | + * representing each row in the CSV file, and the second-level arrays being each column in that row |
|
228 | + * |
|
229 | + * @param string $path_to_file |
|
230 | + * @return array of arrays. Top-level array has rows, second-level array has each item |
|
231 | + * @throws EE_Error |
|
232 | + */ |
|
233 | + public function import_csv_to_multi_dimensional_array($path_to_file) |
|
234 | + { |
|
235 | + // needed to deal with Mac line endings |
|
236 | + ini_set('auto_detect_line_endings', true); |
|
237 | + // because fgetcsv does not correctly deal with backslashed quotes such as \" |
|
238 | + // we'll read the file into a string |
|
239 | + $file_contents = $this->read_unicode_file($path_to_file); |
|
240 | + // replace backslashed quotes with CSV enclosures |
|
241 | + $file_contents = str_replace('\\"', '"""', $file_contents); |
|
242 | + // HEY YOU! PUT THAT FILE BACK!!! |
|
243 | + file_put_contents($path_to_file, $file_contents); |
|
244 | + |
|
245 | + if (($file_handle = fopen($path_to_file, "r")) !== false) { |
|
246 | + $csv_array = []; |
|
247 | + // in PHP 5.3 fgetcsv accepts a 5th parameter, but the pre 5.3 versions |
|
248 | + // of fgetcsv choke if passed more than 4 - is that crazy or what? |
|
249 | + if (version_compare(PHP_VERSION, '5.3.0') < 0) { |
|
250 | + // PHP 5.2- version |
|
251 | + // loop through each row of the file |
|
252 | + while (($data = fgetcsv($file_handle)) !== false) { |
|
253 | + $csv_array[] = $data; |
|
254 | + } |
|
255 | + } else { |
|
256 | + // loop through each row of the file |
|
257 | + while (($data = fgetcsv($file_handle)) !== false) { |
|
258 | + $csv_array[] = $data; |
|
259 | + } |
|
260 | + } |
|
261 | + # Close the File. |
|
262 | + fclose($file_handle); |
|
263 | + return $csv_array; |
|
264 | + } |
|
265 | + EE_Error::add_error( |
|
266 | + sprintf( |
|
267 | + esc_html__( |
|
268 | + "An error occurred - the file: %s could not opened.", |
|
269 | + "event_espresso" |
|
270 | + ), |
|
271 | + $path_to_file |
|
272 | + ), |
|
273 | + __FILE__, |
|
274 | + __FUNCTION__, |
|
275 | + __LINE__ |
|
276 | + ); |
|
277 | + return []; |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * Opens a unicode or utf file |
|
283 | + * (normal file_get_contents has difficulty reading a unicode file) |
|
284 | + * |
|
285 | + * @param string $file_path |
|
286 | + * @return string |
|
287 | + * @throws EE_Error |
|
288 | + * @see http://stackoverflow.com/questions/15092764/how-to-read-unicode-text-file-in-php |
|
289 | + */ |
|
290 | + private function read_unicode_file($file_path) |
|
291 | + { |
|
292 | + $fc = ""; |
|
293 | + $fh = fopen($file_path, "rb"); |
|
294 | + if (! $fh) { |
|
295 | + throw new EE_Error( |
|
296 | + sprintf( |
|
297 | + esc_html__("Cannot open file for read: %s<br>\n", 'event_espresso'), |
|
298 | + $file_path |
|
299 | + ) |
|
300 | + ); |
|
301 | + } |
|
302 | + $file_length = filesize($file_path); |
|
303 | + $bc = fread($fh, $file_length); |
|
304 | + for ($i = 0; $i < $file_length; $i++) { |
|
305 | + $c = substr($bc, $i, 1); |
|
306 | + if ((ord($c) != 0) && (ord($c) != 13)) { |
|
307 | + $fc = $fc . $c; |
|
308 | + } |
|
309 | + } |
|
310 | + if ((ord(substr($fc, 0, 1)) == 255) && (ord(substr($fc, 1, 1)) == 254)) { |
|
311 | + $fc = substr($fc, 2); |
|
312 | + } |
|
313 | + return ($fc); |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * @param $csv_data_array |
|
319 | + * @param false $model_name |
|
320 | + * @return bool |
|
321 | + * @throws EE_Error |
|
322 | + */ |
|
323 | + public function save_csv_to_db($csv_data_array, $model_name = false) |
|
324 | + { |
|
325 | + EE_Error::doing_it_wrong( |
|
326 | + 'save_csv_to_db', |
|
327 | + esc_html__( |
|
328 | + 'Function moved to EE_Import and renamed to save_csv_data_array_to_db', |
|
329 | + 'event_espresso' |
|
330 | + ), |
|
331 | + '4.6.7' |
|
332 | + ); |
|
333 | + return EE_Import::instance()->save_csv_data_array_to_db($csv_data_array, $model_name); |
|
334 | + } |
|
335 | + |
|
336 | + |
|
337 | + /** |
|
338 | + * Writes the CSV file to the output buffer, with rows corresponding to $model_data_array, |
|
339 | + * and dies (in order to avoid other plugins from messing up the csv output) |
|
340 | + * |
|
341 | + * @param string $filename the filename you want to give the file |
|
342 | + * @param array $model_data_array 3d array, as described in EE_CSV::write_model_data_to_csv() |
|
343 | + * @return bool | void writes CSV file to output and dies |
|
344 | + * @throws EE_Error |
|
345 | + * @throws ReflectionException |
|
346 | + */ |
|
347 | + public function export_multiple_model_data_to_csv($filename, $model_data_array) |
|
348 | + { |
|
349 | + $file_handle = $this->begin_sending_csv($filename); |
|
350 | + $this->write_model_data_to_csv($file_handle, $model_data_array); |
|
351 | + $this->end_sending_csv($file_handle); |
|
352 | + } |
|
353 | + |
|
354 | + |
|
355 | + /** |
|
356 | + * Sends HTTP headers to indicate that the browser should download a file, |
|
357 | + * and starts writing the file to PHP's output. Returns the file handle so other functions can |
|
358 | + * also write to it |
|
359 | + * |
|
360 | + * @param string $filename the name of the file that the user will download |
|
361 | + * @return resource, like the results of fopen(), which can be used for fwrite, fputcsv2, etc. |
|
362 | + */ |
|
363 | + public function begin_sending_csv($filename) |
|
364 | + { |
|
365 | + // grab file extension |
|
366 | + $ext = substr(strrchr($filename, '.'), 1); |
|
367 | + if ($ext == '.csv' or $ext == '.xls') { |
|
368 | + str_replace($ext, '', $filename); |
|
369 | + } |
|
370 | + $filename .= '.csv'; |
|
371 | + |
|
372 | + // if somebody's been naughty and already started outputting stuff, trash it |
|
373 | + // and start writing our stuff. |
|
374 | + if (ob_get_length()) { |
|
375 | + @ob_flush(); |
|
376 | + @flush(); |
|
377 | + @ob_end_flush(); |
|
378 | + } |
|
379 | + @ob_start(); |
|
380 | + header("Pragma: public"); |
|
381 | + header("Expires: 0"); |
|
382 | + header("Pragma: no-cache"); |
|
383 | + header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); |
|
384 | + // header("Content-Type: application/force-download"); |
|
385 | + // header("Content-Type: application/octet-stream"); |
|
386 | + // header("Content-Type: application/download"); |
|
387 | + header('Content-disposition: attachment; filename=' . $filename); |
|
388 | + header("Content-Type: text/csv; charset=utf-8"); |
|
389 | + do_action('AHEE__EE_CSV__begin_sending_csv__headers'); |
|
390 | + echo apply_filters( |
|
391 | + 'FHEE__EE_CSV__begin_sending_csv__start_writing', |
|
392 | + "\xEF\xBB\xBF" |
|
393 | + ); // makes excel open it as UTF-8. UTF-8 BOM, see http://stackoverflow.com/a/4440143/2773835 |
|
394 | + return fopen('php://output', 'w'); |
|
395 | + } |
|
396 | + |
|
397 | + |
|
398 | + /** |
|
399 | + * Given an open file, writes all the model data to it in the format the importer expects. |
|
400 | + * Usually preceded by begin_sending_csv($filename), and followed by end_sending_csv($file_handle). |
|
401 | + * |
|
402 | + * @param resource $file_handle |
|
403 | + * @param array $model_data_array is assumed to be a 3d array: 1st layer has keys of model names (eg 'Event'), |
|
404 | + * next layer is numerically indexed to represent each model object (eg, each |
|
405 | + * individual event), and the last layer has all the attributes of that model |
|
406 | + * object (eg, the event's id, name, etc) |
|
407 | + * @return void |
|
408 | + * @throws EE_Error |
|
409 | + * @throws ReflectionException |
|
410 | + */ |
|
411 | + public function write_model_data_to_csv($file_handle, $model_data_array) |
|
412 | + { |
|
413 | + $this->write_metadata_to_csv($file_handle); |
|
414 | + foreach ($model_data_array as $model_name => $model_instance_arrays) { |
|
415 | + // first: output a special row stating the model |
|
416 | + $this->fputcsv2($file_handle, ['MODEL', $model_name]); |
|
417 | + // if we have items to put in the CSV, do it normally |
|
418 | + |
|
419 | + if (! empty($model_instance_arrays)) { |
|
420 | + $this->write_data_array_to_csv($file_handle, $model_instance_arrays); |
|
421 | + } else { |
|
422 | + // echo "no data to write... so just write the headers"; |
|
423 | + // so there's actually NO model objects for that model. |
|
424 | + // probably still want to show the columns |
|
425 | + $model = EE_Registry::instance()->load_model($model_name); |
|
426 | + $column_names = []; |
|
427 | + foreach ($model->field_settings() as $field) { |
|
428 | + $column_names[ $field->get_nicename() . "[" . $field->get_name() . "]" ] = null; |
|
429 | + } |
|
430 | + $this->write_data_array_to_csv($file_handle, [$column_names]); |
|
431 | + } |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * Writes some meta data to the CSV as a bunch of columns. Initially we're only |
|
438 | + * mentioning the version and timezone |
|
439 | + * |
|
440 | + * @param resource $file_handle |
|
441 | + * @throws EE_Error |
|
442 | + * @throws EE_Error |
|
443 | + */ |
|
444 | + public function write_metadata_to_csv($file_handle) |
|
445 | + { |
|
446 | + $data_row = [EE_CSV::metadata_header];// do NOT translate because this exact string is used when importing |
|
447 | + $this->fputcsv2($file_handle, $data_row); |
|
448 | + $meta_data = [ |
|
449 | + 0 => [ |
|
450 | + 'version' => espresso_version(), |
|
451 | + 'timezone' => EEH_DTT_Helper::get_timezone(), |
|
452 | + 'time_of_export' => current_time('mysql'), |
|
453 | + 'site_url' => site_url(), |
|
454 | + ], |
|
455 | + ]; |
|
456 | + $this->write_data_array_to_csv($file_handle, $meta_data); |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + /** |
|
461 | + * @Drop in replacement for PHP's fputcsv function - but this one works!!! |
|
462 | + * @access private |
|
463 | + * @param resource $fh - file handle - what we are writing to |
|
464 | + * @param array $row - individual row of csv data |
|
465 | + * @param string $delimiter - csv delimiter |
|
466 | + * @param string $enclosure - csv enclosure |
|
467 | + * @param string $mysql_null - allows php NULL to be overridden with MySQl's insertable NULL value |
|
468 | + * @return void |
|
469 | + */ |
|
470 | + private function fputcsv2($fh, array $row, $delimiter = ',', $enclosure = '"', $mysql_null = false) |
|
471 | + { |
|
472 | + // Allow user to filter the csv delimiter and enclosure for other countries csv standards |
|
473 | + $delimiter = apply_filters('FHEE__EE_CSV__fputcsv2__delimiter', $delimiter); |
|
474 | + $enclosure = apply_filters('FHEE__EE_CSV__fputcsv2__enclosure', $enclosure); |
|
475 | + |
|
476 | + $delimiter_esc = preg_quote($delimiter, '/'); |
|
477 | + $enclosure_esc = preg_quote($enclosure, '/'); |
|
478 | + |
|
479 | + $output = []; |
|
480 | + foreach ($row as $field_value) { |
|
481 | + if (is_object($field_value) || is_array($field_value)) { |
|
482 | + $field_value = serialize($field_value); |
|
483 | + } |
|
484 | + if ($field_value === null && $mysql_null) { |
|
485 | + $output[] = 'NULL'; |
|
486 | + continue; |
|
487 | + } |
|
488 | + |
|
489 | + $output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field_value) |
|
490 | + ? ($enclosure . str_replace($enclosure, $enclosure . $enclosure, $field_value) . $enclosure) |
|
491 | + : $field_value; |
|
492 | + } |
|
493 | + |
|
494 | + fwrite($fh, join($delimiter, $output) . PHP_EOL); |
|
495 | + } |
|
496 | + |
|
497 | + |
|
498 | + /** |
|
499 | + * Writes $data to the csv file open in $file_handle. uses the array indices of $data for column headers |
|
500 | + * |
|
501 | + * @param resource $file_handle |
|
502 | + * @param array $data 2D array, first numerically-indexed, |
|
503 | + * and next-level-down preferably indexed by string |
|
504 | + * @return boolean if we successfully wrote to the CSV or not. |
|
505 | + * If there's no $data, we consider that a success |
|
506 | + * (because we wrote everything there was...nothing) |
|
507 | + * @throws EE_Error |
|
508 | + * @throws EE_Error |
|
509 | + */ |
|
510 | + public function write_data_array_to_csv($file_handle, $data) |
|
511 | + { |
|
512 | + // determine if $data is actually a 2d array |
|
513 | + if ($data && is_array($data) && is_array(EEH_Array::get_one_item_from_array($data))) { |
|
514 | + // make sure top level is numerically indexed, |
|
515 | + |
|
516 | + if (EEH_Array::is_associative_array($data)) { |
|
517 | + throw new EE_Error( |
|
518 | + sprintf( |
|
519 | + esc_html__( |
|
520 | + "top-level array must be numerically indexed. Does these look like numbers to you? %s", |
|
521 | + "event_espresso" |
|
522 | + ), |
|
523 | + implode(",", array_keys($data)) |
|
524 | + ) |
|
525 | + ); |
|
526 | + } |
|
527 | + $item_in_top_level_array = EEH_Array::get_one_item_from_array($data); |
|
528 | + // now, is the last item in the top-level array of $data an associative or numeric array? |
|
529 | + if (EEH_Array::is_associative_array($item_in_top_level_array)) { |
|
530 | + // its associative, so we want to output its keys as column headers |
|
531 | + $keys = array_keys($item_in_top_level_array); |
|
532 | + $this->fputcsv2($file_handle, $keys); |
|
533 | + } |
|
534 | + // start writing data |
|
535 | + foreach ($data as $data_row) { |
|
536 | + $this->fputcsv2($file_handle, $data_row); |
|
537 | + } |
|
538 | + return true; |
|
539 | + } |
|
540 | + // no data TO write... so we can assume that's a success |
|
541 | + return true; |
|
542 | + } |
|
543 | + |
|
544 | + |
|
545 | + /** |
|
546 | + * Should be called after begin_sending_csv(), and one or more write_data_array_to_csv()s. |
|
547 | + * Calls exit to prevent polluting the CSV file with other junk |
|
548 | + * |
|
549 | + * @param resource $fh file_handle where we're writing the CSV to |
|
550 | + */ |
|
551 | + public function end_sending_csv($fh) |
|
552 | + { |
|
553 | + fclose($fh); |
|
554 | + exit(0); |
|
555 | + } |
|
556 | + |
|
557 | + |
|
558 | + /** |
|
559 | + * @Export contents of an array to csv file |
|
560 | + * @access public |
|
561 | + * @param array $data - the array of data to be converted to csv and exported |
|
562 | + * @param string $filename - name for newly created csv file |
|
563 | + * @return bool TRUE on success, FALSE on fail |
|
564 | + */ |
|
565 | + public function export_array_to_csv($data = false, $filename = false) |
|
566 | + { |
|
567 | + // no data file?? get outta here |
|
568 | + if (! $data or ! is_array($data) or empty($data)) { |
|
569 | + return false; |
|
570 | + } |
|
571 | + |
|
572 | + // no filename?? get outta here |
|
573 | + if (! $filename) { |
|
574 | + return false; |
|
575 | + } |
|
576 | + $fh = $this->begin_sending_csv($filename); |
|
577 | + $this->end_sending_csv($fh); |
|
578 | + return true; |
|
579 | + } |
|
580 | + |
|
581 | + |
|
582 | + /** |
|
583 | + * @Determine the maximum upload file size based on php.ini settings |
|
584 | + * @access public |
|
585 | + * @param int $percent_of_max - desired percentage of the max upload_mb |
|
586 | + * @return int KB |
|
587 | + */ |
|
588 | + public function get_max_upload_size($percent_of_max = false) |
|
589 | + { |
|
590 | + $max_upload = (int) (ini_get('upload_max_filesize')); |
|
591 | + $max_post = (int) (ini_get('post_max_size')); |
|
592 | + $memory_limit = (int) (ini_get('memory_limit')); |
|
593 | + |
|
594 | + // determine the smallest of the three values from above |
|
595 | + $upload_mb = min($max_upload, $max_post, $memory_limit); |
|
596 | + |
|
597 | + // convert MB to KB |
|
598 | + $upload_mb = $upload_mb * 1024; |
|
599 | + |
|
600 | + // don't want the full monty? then reduce the max upload size |
|
601 | + if ($percent_of_max) { |
|
602 | + // is percent_of_max like this -> 50 or like this -> 0.50 ? |
|
603 | + if ($percent_of_max > 1) { |
|
604 | + // changes 50 to 0.50 |
|
605 | + $percent_of_max = $percent_of_max / 100; |
|
606 | + } |
|
607 | + // make upload_mb a percentage of the max upload_mb |
|
608 | + $upload_mb = $upload_mb * $percent_of_max; |
|
609 | + } |
|
610 | + |
|
611 | + return $upload_mb; |
|
612 | + } |
|
613 | + |
|
614 | + |
|
615 | + /** |
|
616 | + * Gets the date format to use in teh csv. filterable |
|
617 | + * |
|
618 | + * @param string $current_format |
|
619 | + * @return string |
|
620 | + */ |
|
621 | + public function get_date_format_for_csv($current_format = null) |
|
622 | + { |
|
623 | + return apply_filters('FHEE__EE_CSV__get_date_format_for_csv__format', 'Y-m-d', $current_format); |
|
624 | + } |
|
625 | + |
|
626 | + |
|
627 | + /** |
|
628 | + * Gets the time format we want to use in CSV reports. Filterable |
|
629 | + * |
|
630 | + * @param string $current_format |
|
631 | + * @return string |
|
632 | + */ |
|
633 | + public function get_time_format_for_csv($current_format = null) |
|
634 | + { |
|
635 | + return apply_filters('FHEE__EE_CSV__get_time_format_for_csv__format', 'H:i:s', $current_format); |
|
636 | + } |
|
637 | 637 | } |
@@ -43,31 +43,31 @@ discard block |
||
43 | 43 | global $wpdb; |
44 | 44 | |
45 | 45 | $this->_primary_keys = [ |
46 | - $wpdb->prefix . 'esp_answer' => ['ANS_ID'], |
|
47 | - $wpdb->prefix . 'esp_attendee' => ['ATT_ID'], |
|
48 | - $wpdb->prefix . 'esp_datetime' => ['DTT_ID'], |
|
49 | - $wpdb->prefix . 'esp_event_question_group' => ['EQG_ID'], |
|
50 | - $wpdb->prefix . 'esp_message_template' => ['MTP_ID'], |
|
51 | - $wpdb->prefix . 'esp_payment' => ['PAY_ID'], |
|
52 | - $wpdb->prefix . 'esp_price' => ['PRC_ID'], |
|
53 | - $wpdb->prefix . 'esp_price_type' => ['PRT_ID'], |
|
54 | - $wpdb->prefix . 'esp_question' => ['QST_ID'], |
|
55 | - $wpdb->prefix . 'esp_question_group' => ['QSG_ID'], |
|
56 | - $wpdb->prefix . 'esp_question_group_question' => ['QGQ_ID'], |
|
57 | - $wpdb->prefix . 'esp_question_option' => ['QSO_ID'], |
|
58 | - $wpdb->prefix . 'esp_registration' => ['REG_ID'], |
|
59 | - $wpdb->prefix . 'esp_status' => ['STS_ID'], |
|
60 | - $wpdb->prefix . 'esp_transaction' => ['TXN_ID'], |
|
61 | - $wpdb->prefix . 'esp_transaction' => ['TXN_ID'], |
|
62 | - $wpdb->prefix . 'events_detail' => ['id'], |
|
63 | - $wpdb->prefix . 'events_category_detail' => ['id'], |
|
64 | - $wpdb->prefix . 'events_category_rel' => ['id'], |
|
65 | - $wpdb->prefix . 'events_venue' => ['id'], |
|
66 | - $wpdb->prefix . 'events_venue_rel' => ['emeta_id'], |
|
67 | - $wpdb->prefix . 'events_locale' => ['id'], |
|
68 | - $wpdb->prefix . 'events_locale_rel' => ['id'], |
|
69 | - $wpdb->prefix . 'events_personnel' => ['id'], |
|
70 | - $wpdb->prefix . 'events_personnel_rel' => ['id'], |
|
46 | + $wpdb->prefix.'esp_answer' => ['ANS_ID'], |
|
47 | + $wpdb->prefix.'esp_attendee' => ['ATT_ID'], |
|
48 | + $wpdb->prefix.'esp_datetime' => ['DTT_ID'], |
|
49 | + $wpdb->prefix.'esp_event_question_group' => ['EQG_ID'], |
|
50 | + $wpdb->prefix.'esp_message_template' => ['MTP_ID'], |
|
51 | + $wpdb->prefix.'esp_payment' => ['PAY_ID'], |
|
52 | + $wpdb->prefix.'esp_price' => ['PRC_ID'], |
|
53 | + $wpdb->prefix.'esp_price_type' => ['PRT_ID'], |
|
54 | + $wpdb->prefix.'esp_question' => ['QST_ID'], |
|
55 | + $wpdb->prefix.'esp_question_group' => ['QSG_ID'], |
|
56 | + $wpdb->prefix.'esp_question_group_question' => ['QGQ_ID'], |
|
57 | + $wpdb->prefix.'esp_question_option' => ['QSO_ID'], |
|
58 | + $wpdb->prefix.'esp_registration' => ['REG_ID'], |
|
59 | + $wpdb->prefix.'esp_status' => ['STS_ID'], |
|
60 | + $wpdb->prefix.'esp_transaction' => ['TXN_ID'], |
|
61 | + $wpdb->prefix.'esp_transaction' => ['TXN_ID'], |
|
62 | + $wpdb->prefix.'events_detail' => ['id'], |
|
63 | + $wpdb->prefix.'events_category_detail' => ['id'], |
|
64 | + $wpdb->prefix.'events_category_rel' => ['id'], |
|
65 | + $wpdb->prefix.'events_venue' => ['id'], |
|
66 | + $wpdb->prefix.'events_venue_rel' => ['emeta_id'], |
|
67 | + $wpdb->prefix.'events_locale' => ['id'], |
|
68 | + $wpdb->prefix.'events_locale_rel' => ['id'], |
|
69 | + $wpdb->prefix.'events_personnel' => ['id'], |
|
70 | + $wpdb->prefix.'events_personnel_rel' => ['id'], |
|
71 | 71 | ]; |
72 | 72 | } |
73 | 73 | |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | public static function instance() |
82 | 82 | { |
83 | 83 | // check if class object is instantiated |
84 | - if (! self::$_instance instanceof EE_CSV) { |
|
84 | + if ( ! self::$_instance instanceof EE_CSV) { |
|
85 | 85 | self::$_instance = new self(); |
86 | 86 | } |
87 | 87 | return self::$_instance; |
@@ -157,24 +157,24 @@ discard block |
||
157 | 157 | // loop through each column |
158 | 158 | for ($i = 0; $i < $columns; $i++) { |
159 | 159 | // replace csv_enclosures with backslashed quotes |
160 | - $data[ $i ] = str_replace('"""', '\\"', $data[ $i ]); |
|
160 | + $data[$i] = str_replace('"""', '\\"', $data[$i]); |
|
161 | 161 | // do we need to grab the column names? |
162 | 162 | if ($row === 1) { |
163 | 163 | if ($first_row_is_headers) { |
164 | 164 | // store the column names to use for keys |
165 | - $column_name = $data[ $i ]; |
|
165 | + $column_name = $data[$i]; |
|
166 | 166 | // check it's not blank... sometimes CSV editing programs |
167 | 167 | // add a bunch of empty columns onto the end... |
168 | - if (! $column_name) { |
|
168 | + if ( ! $column_name) { |
|
169 | 169 | continue; |
170 | 170 | } |
171 | 171 | $matches = []; |
172 | 172 | if ($model_name == EE_CSV::metadata_header) { |
173 | - $headers[ $i ] = $column_name; |
|
173 | + $headers[$i] = $column_name; |
|
174 | 174 | } else { |
175 | 175 | // now get the db table name from it (the part between square brackets) |
176 | 176 | $success = preg_match('~(.*)\[(.*)\]~', $column_name, $matches); |
177 | - if (! $success) { |
|
177 | + if ( ! $success) { |
|
178 | 178 | EE_Error::add_error( |
179 | 179 | sprintf( |
180 | 180 | esc_html__( |
@@ -190,24 +190,24 @@ discard block |
||
190 | 190 | ); |
191 | 191 | return false; |
192 | 192 | } |
193 | - $headers[ $i ] = $matches[2]; |
|
193 | + $headers[$i] = $matches[2]; |
|
194 | 194 | } |
195 | 195 | } else { |
196 | 196 | // no column names means our final array will just use counters for keys |
197 | - $model_entry[ $headers[ $i ] ] = $data[ $i ]; |
|
198 | - $headers[ $i ] = $i; |
|
197 | + $model_entry[$headers[$i]] = $data[$i]; |
|
198 | + $headers[$i] = $i; |
|
199 | 199 | } |
200 | 200 | // and we need to store csv data |
201 | 201 | } else { |
202 | 202 | // this column isn' ta header, store it if there is a header for it |
203 | - if (isset($headers[ $i ])) { |
|
204 | - $model_entry[ $headers[ $i ] ] = $data[ $i ]; |
|
203 | + if (isset($headers[$i])) { |
|
204 | + $model_entry[$headers[$i]] = $data[$i]; |
|
205 | 205 | } |
206 | 206 | } |
207 | 207 | } |
208 | 208 | // save the row's data IF it's a non-header-row |
209 | - if (! $first_row_is_headers || $row > 1) { |
|
210 | - $ee_formatted_data[ $model_name ][] = $model_entry; |
|
209 | + if ( ! $first_row_is_headers || $row > 1) { |
|
210 | + $ee_formatted_data[$model_name][] = $model_entry; |
|
211 | 211 | } |
212 | 212 | // advance to next row |
213 | 213 | $row++; |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | { |
292 | 292 | $fc = ""; |
293 | 293 | $fh = fopen($file_path, "rb"); |
294 | - if (! $fh) { |
|
294 | + if ( ! $fh) { |
|
295 | 295 | throw new EE_Error( |
296 | 296 | sprintf( |
297 | 297 | esc_html__("Cannot open file for read: %s<br>\n", 'event_espresso'), |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | for ($i = 0; $i < $file_length; $i++) { |
305 | 305 | $c = substr($bc, $i, 1); |
306 | 306 | if ((ord($c) != 0) && (ord($c) != 13)) { |
307 | - $fc = $fc . $c; |
|
307 | + $fc = $fc.$c; |
|
308 | 308 | } |
309 | 309 | } |
310 | 310 | if ((ord(substr($fc, 0, 1)) == 255) && (ord(substr($fc, 1, 1)) == 254)) { |
@@ -384,7 +384,7 @@ discard block |
||
384 | 384 | // header("Content-Type: application/force-download"); |
385 | 385 | // header("Content-Type: application/octet-stream"); |
386 | 386 | // header("Content-Type: application/download"); |
387 | - header('Content-disposition: attachment; filename=' . $filename); |
|
387 | + header('Content-disposition: attachment; filename='.$filename); |
|
388 | 388 | header("Content-Type: text/csv; charset=utf-8"); |
389 | 389 | do_action('AHEE__EE_CSV__begin_sending_csv__headers'); |
390 | 390 | echo apply_filters( |
@@ -416,7 +416,7 @@ discard block |
||
416 | 416 | $this->fputcsv2($file_handle, ['MODEL', $model_name]); |
417 | 417 | // if we have items to put in the CSV, do it normally |
418 | 418 | |
419 | - if (! empty($model_instance_arrays)) { |
|
419 | + if ( ! empty($model_instance_arrays)) { |
|
420 | 420 | $this->write_data_array_to_csv($file_handle, $model_instance_arrays); |
421 | 421 | } else { |
422 | 422 | // echo "no data to write... so just write the headers"; |
@@ -425,7 +425,7 @@ discard block |
||
425 | 425 | $model = EE_Registry::instance()->load_model($model_name); |
426 | 426 | $column_names = []; |
427 | 427 | foreach ($model->field_settings() as $field) { |
428 | - $column_names[ $field->get_nicename() . "[" . $field->get_name() . "]" ] = null; |
|
428 | + $column_names[$field->get_nicename()."[".$field->get_name()."]"] = null; |
|
429 | 429 | } |
430 | 430 | $this->write_data_array_to_csv($file_handle, [$column_names]); |
431 | 431 | } |
@@ -443,7 +443,7 @@ discard block |
||
443 | 443 | */ |
444 | 444 | public function write_metadata_to_csv($file_handle) |
445 | 445 | { |
446 | - $data_row = [EE_CSV::metadata_header];// do NOT translate because this exact string is used when importing |
|
446 | + $data_row = [EE_CSV::metadata_header]; // do NOT translate because this exact string is used when importing |
|
447 | 447 | $this->fputcsv2($file_handle, $data_row); |
448 | 448 | $meta_data = [ |
449 | 449 | 0 => [ |
@@ -487,11 +487,11 @@ discard block |
||
487 | 487 | } |
488 | 488 | |
489 | 489 | $output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field_value) |
490 | - ? ($enclosure . str_replace($enclosure, $enclosure . $enclosure, $field_value) . $enclosure) |
|
490 | + ? ($enclosure.str_replace($enclosure, $enclosure.$enclosure, $field_value).$enclosure) |
|
491 | 491 | : $field_value; |
492 | 492 | } |
493 | 493 | |
494 | - fwrite($fh, join($delimiter, $output) . PHP_EOL); |
|
494 | + fwrite($fh, join($delimiter, $output).PHP_EOL); |
|
495 | 495 | } |
496 | 496 | |
497 | 497 | |
@@ -565,12 +565,12 @@ discard block |
||
565 | 565 | public function export_array_to_csv($data = false, $filename = false) |
566 | 566 | { |
567 | 567 | // no data file?? get outta here |
568 | - if (! $data or ! is_array($data) or empty($data)) { |
|
568 | + if ( ! $data or ! is_array($data) or empty($data)) { |
|
569 | 569 | return false; |
570 | 570 | } |
571 | 571 | |
572 | 572 | // no filename?? get outta here |
573 | - if (! $filename) { |
|
573 | + if ( ! $filename) { |
|
574 | 574 | return false; |
575 | 575 | } |
576 | 576 | $fh = $this->begin_sending_csv($filename); |
@@ -176,7 +176,7 @@ |
||
176 | 176 | { |
177 | 177 | $model_name_of_related_obj = $this->OBJ_type(); |
178 | 178 | $is_model_name = EE_Registry::instance()->is_model_name($model_name_of_related_obj); |
179 | - if (! $is_model_name) { |
|
179 | + if ( ! $is_model_name) { |
|
180 | 180 | return null; |
181 | 181 | } |
182 | 182 | return $this->get_first_related($model_name_of_related_obj); |
@@ -10,240 +10,240 @@ |
||
10 | 10 | class EE_Change_Log extends EE_Base_Class |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @param array $props_n_values incoming values |
|
15 | - * @param string $timezone incoming timezone |
|
16 | - * If not set the timezone for the website will be used. |
|
17 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
18 | - * date_format and the second value is the time format |
|
19 | - * @return EE_Change_Log |
|
20 | - * @throws EE_Error |
|
21 | - * @throws ReflectionException |
|
22 | - */ |
|
23 | - public static function new_instance($props_n_values = [], $timezone = '', $date_formats = []) |
|
24 | - { |
|
25 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats); |
|
26 | - return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
|
27 | - } |
|
28 | - |
|
29 | - |
|
30 | - /** |
|
31 | - * @param array $props_n_values incoming values from the database |
|
32 | - * @param string $timezone incoming timezone as set by the model. |
|
33 | - * If not set the timezone for the website will be used. |
|
34 | - * @return EE_Change_Log |
|
35 | - * @throws EE_Error |
|
36 | - * @throws ReflectionException |
|
37 | - */ |
|
38 | - public static function new_instance_from_db($props_n_values = [], $timezone = '') |
|
39 | - { |
|
40 | - return new self($props_n_values, true, $timezone); |
|
41 | - } |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * Gets message |
|
46 | - * |
|
47 | - * @return mixed |
|
48 | - * @throws EE_Error |
|
49 | - */ |
|
50 | - public function message() |
|
51 | - { |
|
52 | - return $this->get('LOG_message'); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * Sets message |
|
58 | - * |
|
59 | - * @param mixed $message |
|
60 | - * @throws EE_Error |
|
61 | - * @throws ReflectionException |
|
62 | - */ |
|
63 | - public function set_message($message) |
|
64 | - { |
|
65 | - $this->set('LOG_message', $message); |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * Gets time |
|
71 | - * |
|
72 | - * @return string |
|
73 | - * @throws EE_Error |
|
74 | - */ |
|
75 | - public function time() |
|
76 | - { |
|
77 | - return $this->get('LOG_time'); |
|
78 | - } |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * Sets time |
|
83 | - * |
|
84 | - * @param string $time |
|
85 | - * @throws EE_Error |
|
86 | - * @throws ReflectionException |
|
87 | - */ |
|
88 | - public function set_time($time) |
|
89 | - { |
|
90 | - $this->set('LOG_time', $time); |
|
91 | - } |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * Return the localized log type label. |
|
96 | - * |
|
97 | - * @return string |
|
98 | - * @throws EE_Error |
|
99 | - */ |
|
100 | - public function log_type_label() |
|
101 | - { |
|
102 | - return EEM_Change_Log::get_pretty_label_for_type($this->log_type()); |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * Gets log_type |
|
108 | - * |
|
109 | - * @return string |
|
110 | - * @throws EE_Error |
|
111 | - */ |
|
112 | - public function log_type() |
|
113 | - { |
|
114 | - return $this->get('LOG_type'); |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * Sets log_type |
|
120 | - * |
|
121 | - * @param string $log_type |
|
122 | - * @throws EE_Error |
|
123 | - * @throws ReflectionException |
|
124 | - */ |
|
125 | - public function set_log_type($log_type) |
|
126 | - { |
|
127 | - $this->set('LOG_type', $log_type); |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * Gets OBJ_ID (the ID of the item related to this log) |
|
133 | - * |
|
134 | - * @return mixed |
|
135 | - * @throws EE_Error |
|
136 | - */ |
|
137 | - public function OBJ_ID() |
|
138 | - { |
|
139 | - return $this->get('OBJ_ID'); |
|
140 | - } |
|
141 | - |
|
142 | - |
|
143 | - /** |
|
144 | - * Gets wp_user |
|
145 | - * |
|
146 | - * @return int |
|
147 | - * @throws EE_Error |
|
148 | - */ |
|
149 | - public function wp_user() |
|
150 | - { |
|
151 | - return $this->get('LOG_wp_user'); |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * Sets wp_user |
|
157 | - * |
|
158 | - * @param int $wp_user_id |
|
159 | - * @throws EE_Error |
|
160 | - * @throws ReflectionException |
|
161 | - */ |
|
162 | - public function set_wp_user($wp_user_id) |
|
163 | - { |
|
164 | - $this->set('LOG_wp_user', $wp_user_id); |
|
165 | - } |
|
166 | - |
|
167 | - |
|
168 | - /** |
|
169 | - * Gets the model object attached to this log |
|
170 | - * |
|
171 | - * @return EE_Base_Class |
|
172 | - * @throws EE_Error |
|
173 | - * @throws ReflectionException |
|
174 | - */ |
|
175 | - public function object() |
|
176 | - { |
|
177 | - $model_name_of_related_obj = $this->OBJ_type(); |
|
178 | - $is_model_name = EE_Registry::instance()->is_model_name($model_name_of_related_obj); |
|
179 | - if (! $is_model_name) { |
|
180 | - return null; |
|
181 | - } |
|
182 | - return $this->get_first_related($model_name_of_related_obj); |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * Gets type of the model object related to this log |
|
188 | - * |
|
189 | - * @return string |
|
190 | - * @throws EE_Error |
|
191 | - */ |
|
192 | - public function OBJ_type() |
|
193 | - { |
|
194 | - return $this->get('OBJ_type'); |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * Shorthand for setting the OBJ_ID and OBJ_type. Slightly handier than using |
|
200 | - * _add_relation_to because you don't have to specify what type of model you're |
|
201 | - * associating it with |
|
202 | - * |
|
203 | - * @param EE_Base_Class $object |
|
204 | - * @param boolean $save |
|
205 | - * @return bool if $save=true, NULL is $save=false |
|
206 | - * @throws EE_Error |
|
207 | - * @throws ReflectionException |
|
208 | - */ |
|
209 | - public function set_object($object, $save = true) |
|
210 | - { |
|
211 | - if ($object instanceof EE_Base_Class) { |
|
212 | - $this->set_OBJ_type($object->get_model()->get_this_model_name()); |
|
213 | - $this->set_OBJ_ID($object->ID()); |
|
214 | - } else { |
|
215 | - $this->set_OBJ_type(null); |
|
216 | - $this->set_OBJ_ID(null); |
|
217 | - } |
|
218 | - if ($save) { |
|
219 | - return $this->save(); |
|
220 | - } |
|
221 | - return null; |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - /** |
|
226 | - * Sets type |
|
227 | - * |
|
228 | - * @param string $type |
|
229 | - * @throws EE_Error |
|
230 | - * @throws ReflectionException |
|
231 | - */ |
|
232 | - public function set_OBJ_type($type) |
|
233 | - { |
|
234 | - $this->set('OBJ_type', $type); |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * Sets OBJ_ID |
|
240 | - * |
|
241 | - * @param mixed $OBJ_ID |
|
242 | - * @throws EE_Error |
|
243 | - * @throws ReflectionException |
|
244 | - */ |
|
245 | - public function set_OBJ_ID($OBJ_ID) |
|
246 | - { |
|
247 | - $this->set('OBJ_ID', $OBJ_ID); |
|
248 | - } |
|
13 | + /** |
|
14 | + * @param array $props_n_values incoming values |
|
15 | + * @param string $timezone incoming timezone |
|
16 | + * If not set the timezone for the website will be used. |
|
17 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
18 | + * date_format and the second value is the time format |
|
19 | + * @return EE_Change_Log |
|
20 | + * @throws EE_Error |
|
21 | + * @throws ReflectionException |
|
22 | + */ |
|
23 | + public static function new_instance($props_n_values = [], $timezone = '', $date_formats = []) |
|
24 | + { |
|
25 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats); |
|
26 | + return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
|
27 | + } |
|
28 | + |
|
29 | + |
|
30 | + /** |
|
31 | + * @param array $props_n_values incoming values from the database |
|
32 | + * @param string $timezone incoming timezone as set by the model. |
|
33 | + * If not set the timezone for the website will be used. |
|
34 | + * @return EE_Change_Log |
|
35 | + * @throws EE_Error |
|
36 | + * @throws ReflectionException |
|
37 | + */ |
|
38 | + public static function new_instance_from_db($props_n_values = [], $timezone = '') |
|
39 | + { |
|
40 | + return new self($props_n_values, true, $timezone); |
|
41 | + } |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * Gets message |
|
46 | + * |
|
47 | + * @return mixed |
|
48 | + * @throws EE_Error |
|
49 | + */ |
|
50 | + public function message() |
|
51 | + { |
|
52 | + return $this->get('LOG_message'); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * Sets message |
|
58 | + * |
|
59 | + * @param mixed $message |
|
60 | + * @throws EE_Error |
|
61 | + * @throws ReflectionException |
|
62 | + */ |
|
63 | + public function set_message($message) |
|
64 | + { |
|
65 | + $this->set('LOG_message', $message); |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * Gets time |
|
71 | + * |
|
72 | + * @return string |
|
73 | + * @throws EE_Error |
|
74 | + */ |
|
75 | + public function time() |
|
76 | + { |
|
77 | + return $this->get('LOG_time'); |
|
78 | + } |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * Sets time |
|
83 | + * |
|
84 | + * @param string $time |
|
85 | + * @throws EE_Error |
|
86 | + * @throws ReflectionException |
|
87 | + */ |
|
88 | + public function set_time($time) |
|
89 | + { |
|
90 | + $this->set('LOG_time', $time); |
|
91 | + } |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * Return the localized log type label. |
|
96 | + * |
|
97 | + * @return string |
|
98 | + * @throws EE_Error |
|
99 | + */ |
|
100 | + public function log_type_label() |
|
101 | + { |
|
102 | + return EEM_Change_Log::get_pretty_label_for_type($this->log_type()); |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * Gets log_type |
|
108 | + * |
|
109 | + * @return string |
|
110 | + * @throws EE_Error |
|
111 | + */ |
|
112 | + public function log_type() |
|
113 | + { |
|
114 | + return $this->get('LOG_type'); |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * Sets log_type |
|
120 | + * |
|
121 | + * @param string $log_type |
|
122 | + * @throws EE_Error |
|
123 | + * @throws ReflectionException |
|
124 | + */ |
|
125 | + public function set_log_type($log_type) |
|
126 | + { |
|
127 | + $this->set('LOG_type', $log_type); |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * Gets OBJ_ID (the ID of the item related to this log) |
|
133 | + * |
|
134 | + * @return mixed |
|
135 | + * @throws EE_Error |
|
136 | + */ |
|
137 | + public function OBJ_ID() |
|
138 | + { |
|
139 | + return $this->get('OBJ_ID'); |
|
140 | + } |
|
141 | + |
|
142 | + |
|
143 | + /** |
|
144 | + * Gets wp_user |
|
145 | + * |
|
146 | + * @return int |
|
147 | + * @throws EE_Error |
|
148 | + */ |
|
149 | + public function wp_user() |
|
150 | + { |
|
151 | + return $this->get('LOG_wp_user'); |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * Sets wp_user |
|
157 | + * |
|
158 | + * @param int $wp_user_id |
|
159 | + * @throws EE_Error |
|
160 | + * @throws ReflectionException |
|
161 | + */ |
|
162 | + public function set_wp_user($wp_user_id) |
|
163 | + { |
|
164 | + $this->set('LOG_wp_user', $wp_user_id); |
|
165 | + } |
|
166 | + |
|
167 | + |
|
168 | + /** |
|
169 | + * Gets the model object attached to this log |
|
170 | + * |
|
171 | + * @return EE_Base_Class |
|
172 | + * @throws EE_Error |
|
173 | + * @throws ReflectionException |
|
174 | + */ |
|
175 | + public function object() |
|
176 | + { |
|
177 | + $model_name_of_related_obj = $this->OBJ_type(); |
|
178 | + $is_model_name = EE_Registry::instance()->is_model_name($model_name_of_related_obj); |
|
179 | + if (! $is_model_name) { |
|
180 | + return null; |
|
181 | + } |
|
182 | + return $this->get_first_related($model_name_of_related_obj); |
|
183 | + } |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * Gets type of the model object related to this log |
|
188 | + * |
|
189 | + * @return string |
|
190 | + * @throws EE_Error |
|
191 | + */ |
|
192 | + public function OBJ_type() |
|
193 | + { |
|
194 | + return $this->get('OBJ_type'); |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * Shorthand for setting the OBJ_ID and OBJ_type. Slightly handier than using |
|
200 | + * _add_relation_to because you don't have to specify what type of model you're |
|
201 | + * associating it with |
|
202 | + * |
|
203 | + * @param EE_Base_Class $object |
|
204 | + * @param boolean $save |
|
205 | + * @return bool if $save=true, NULL is $save=false |
|
206 | + * @throws EE_Error |
|
207 | + * @throws ReflectionException |
|
208 | + */ |
|
209 | + public function set_object($object, $save = true) |
|
210 | + { |
|
211 | + if ($object instanceof EE_Base_Class) { |
|
212 | + $this->set_OBJ_type($object->get_model()->get_this_model_name()); |
|
213 | + $this->set_OBJ_ID($object->ID()); |
|
214 | + } else { |
|
215 | + $this->set_OBJ_type(null); |
|
216 | + $this->set_OBJ_ID(null); |
|
217 | + } |
|
218 | + if ($save) { |
|
219 | + return $this->save(); |
|
220 | + } |
|
221 | + return null; |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + /** |
|
226 | + * Sets type |
|
227 | + * |
|
228 | + * @param string $type |
|
229 | + * @throws EE_Error |
|
230 | + * @throws ReflectionException |
|
231 | + */ |
|
232 | + public function set_OBJ_type($type) |
|
233 | + { |
|
234 | + $this->set('OBJ_type', $type); |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * Sets OBJ_ID |
|
240 | + * |
|
241 | + * @param mixed $OBJ_ID |
|
242 | + * @throws EE_Error |
|
243 | + * @throws ReflectionException |
|
244 | + */ |
|
245 | + public function set_OBJ_ID($OBJ_ID) |
|
246 | + { |
|
247 | + $this->set('OBJ_ID', $OBJ_ID); |
|
248 | + } |
|
249 | 249 | } |
@@ -19,222 +19,222 @@ |
||
19 | 19 | class PhpCompatLessFiveSixHelper extends AbstractHelper |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * PhpCompatLessFiveSixHelper constructor. |
|
24 | - * |
|
25 | - * @throws DomainException |
|
26 | - */ |
|
27 | - public function __construct() |
|
28 | - { |
|
29 | - if (PHP_VERSION_ID >= 50600) { |
|
30 | - throw new DomainException( |
|
31 | - sprintf( |
|
32 | - esc_html__( |
|
33 | - 'The %1$s is only usable on php versions less than 5.6. You\'ll want to use %2$s instead.', |
|
34 | - 'event_espresso' |
|
35 | - ), |
|
36 | - __CLASS__, |
|
37 | - 'EventEspresso\core\services\helpers\datetime\PhpCompatGreaterFiveSixHelper' |
|
38 | - ) |
|
39 | - ); |
|
40 | - } |
|
41 | - parent::__construct(); |
|
42 | - } |
|
22 | + /** |
|
23 | + * PhpCompatLessFiveSixHelper constructor. |
|
24 | + * |
|
25 | + * @throws DomainException |
|
26 | + */ |
|
27 | + public function __construct() |
|
28 | + { |
|
29 | + if (PHP_VERSION_ID >= 50600) { |
|
30 | + throw new DomainException( |
|
31 | + sprintf( |
|
32 | + esc_html__( |
|
33 | + 'The %1$s is only usable on php versions less than 5.6. You\'ll want to use %2$s instead.', |
|
34 | + 'event_espresso' |
|
35 | + ), |
|
36 | + __CLASS__, |
|
37 | + 'EventEspresso\core\services\helpers\datetime\PhpCompatGreaterFiveSixHelper' |
|
38 | + ) |
|
39 | + ); |
|
40 | + } |
|
41 | + parent::__construct(); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Returns a timezone string for the provided gmt_offset. |
|
46 | - * |
|
47 | - * @param float|string $gmt_offset |
|
48 | - * @return string |
|
49 | - * @throws EE_Error |
|
50 | - */ |
|
51 | - public function getTimezoneStringFromGmtOffset($gmt_offset = '') |
|
52 | - { |
|
53 | - $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset); |
|
54 | - if (is_string($gmt_offset_or_timezone_string)) { |
|
55 | - return $gmt_offset_or_timezone_string; |
|
56 | - } |
|
57 | - // well we know its a float, so let's roll with it. |
|
58 | - $gmt_offset = $gmt_offset_or_timezone_string; |
|
59 | - // convert GMT offset to seconds |
|
60 | - $gmt_offset *= HOUR_IN_SECONDS; |
|
61 | - // although we don't know the TZ abbreviation, we know the UTC offset |
|
62 | - $timezone_string = timezone_name_from_abbr(null, $gmt_offset); |
|
63 | - // only use this timezone_string IF it's current offset matches the given offset |
|
64 | - if (! empty($timezone_string)) { |
|
65 | - $offset = null; |
|
66 | - try { |
|
67 | - $offset = $this->getTimezoneOffset(new DateTimeZone($timezone_string)); |
|
68 | - if ($offset !== $gmt_offset) { |
|
69 | - $timezone_string = false; |
|
70 | - } |
|
71 | - } catch (Exception $e) { |
|
72 | - $timezone_string = false; |
|
73 | - } |
|
74 | - } |
|
75 | - // better have a valid timezone string by now, but if not, sigh... loop thru the timezone_abbreviations_list() |
|
76 | - // ... |
|
77 | - $timezone_string = $timezone_string !== false |
|
78 | - ? $timezone_string |
|
79 | - : $this->getTimezoneStringFromAbbreviationsList($gmt_offset); |
|
80 | - return $timezone_string; |
|
81 | - } |
|
44 | + /** |
|
45 | + * Returns a timezone string for the provided gmt_offset. |
|
46 | + * |
|
47 | + * @param float|string $gmt_offset |
|
48 | + * @return string |
|
49 | + * @throws EE_Error |
|
50 | + */ |
|
51 | + public function getTimezoneStringFromGmtOffset($gmt_offset = '') |
|
52 | + { |
|
53 | + $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset); |
|
54 | + if (is_string($gmt_offset_or_timezone_string)) { |
|
55 | + return $gmt_offset_or_timezone_string; |
|
56 | + } |
|
57 | + // well we know its a float, so let's roll with it. |
|
58 | + $gmt_offset = $gmt_offset_or_timezone_string; |
|
59 | + // convert GMT offset to seconds |
|
60 | + $gmt_offset *= HOUR_IN_SECONDS; |
|
61 | + // although we don't know the TZ abbreviation, we know the UTC offset |
|
62 | + $timezone_string = timezone_name_from_abbr(null, $gmt_offset); |
|
63 | + // only use this timezone_string IF it's current offset matches the given offset |
|
64 | + if (! empty($timezone_string)) { |
|
65 | + $offset = null; |
|
66 | + try { |
|
67 | + $offset = $this->getTimezoneOffset(new DateTimeZone($timezone_string)); |
|
68 | + if ($offset !== $gmt_offset) { |
|
69 | + $timezone_string = false; |
|
70 | + } |
|
71 | + } catch (Exception $e) { |
|
72 | + $timezone_string = false; |
|
73 | + } |
|
74 | + } |
|
75 | + // better have a valid timezone string by now, but if not, sigh... loop thru the timezone_abbreviations_list() |
|
76 | + // ... |
|
77 | + $timezone_string = $timezone_string !== false |
|
78 | + ? $timezone_string |
|
79 | + : $this->getTimezoneStringFromAbbreviationsList($gmt_offset); |
|
80 | + return $timezone_string; |
|
81 | + } |
|
82 | 82 | |
83 | 83 | |
84 | - /** |
|
85 | - * @param int $gmt_offset |
|
86 | - * @param bool $coerce If true, we attempt to coerce with our adjustment table |
|
87 | - * @see self::adjustInvalidGmtOffset |
|
88 | - * @return string |
|
89 | - * @throws EE_Error |
|
90 | - */ |
|
91 | - protected function getTimezoneStringFromAbbreviationsList($gmt_offset = 0, $coerce = true) |
|
92 | - { |
|
93 | - $gmt_offset = (int) $gmt_offset; |
|
94 | - /** @var array[] $abbreviations */ |
|
95 | - $abbreviations = DateTimeZone::listAbbreviations(); |
|
96 | - foreach ($abbreviations as $abbreviation) { |
|
97 | - foreach ($abbreviation as $timezone) { |
|
98 | - if ((int) $timezone['offset'] === $gmt_offset && (bool) $timezone['dst'] === false) { |
|
99 | - try { |
|
100 | - $offset = $this->getTimezoneOffset(new DateTimeZone($timezone['timezone_id'])); |
|
101 | - if ($offset !== $gmt_offset) { |
|
102 | - continue; |
|
103 | - } |
|
104 | - return $timezone['timezone_id']; |
|
105 | - } catch (Exception $e) { |
|
106 | - continue; |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - } |
|
111 | - // if $coerce is true, let's see if we can get a timezone string after the offset is adjusted |
|
112 | - if ($coerce === true) { |
|
113 | - $timezone_string = $this->getTimezoneStringFromAbbreviationsList( |
|
114 | - $this->adjustInvalidGmtOffsets($gmt_offset), |
|
115 | - false |
|
116 | - ); |
|
117 | - if ($timezone_string) { |
|
118 | - return $timezone_string; |
|
119 | - } |
|
120 | - } |
|
121 | - throw new EE_Error( |
|
122 | - sprintf( |
|
123 | - esc_html__( |
|
124 | - 'The provided GMT offset (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
125 | - 'event_espresso' |
|
126 | - ), |
|
127 | - $gmt_offset / HOUR_IN_SECONDS, |
|
128 | - '<a href="http://www.php.net/manual/en/timezones.php">', |
|
129 | - '</a>' |
|
130 | - ) |
|
131 | - ); |
|
132 | - } |
|
84 | + /** |
|
85 | + * @param int $gmt_offset |
|
86 | + * @param bool $coerce If true, we attempt to coerce with our adjustment table |
|
87 | + * @see self::adjustInvalidGmtOffset |
|
88 | + * @return string |
|
89 | + * @throws EE_Error |
|
90 | + */ |
|
91 | + protected function getTimezoneStringFromAbbreviationsList($gmt_offset = 0, $coerce = true) |
|
92 | + { |
|
93 | + $gmt_offset = (int) $gmt_offset; |
|
94 | + /** @var array[] $abbreviations */ |
|
95 | + $abbreviations = DateTimeZone::listAbbreviations(); |
|
96 | + foreach ($abbreviations as $abbreviation) { |
|
97 | + foreach ($abbreviation as $timezone) { |
|
98 | + if ((int) $timezone['offset'] === $gmt_offset && (bool) $timezone['dst'] === false) { |
|
99 | + try { |
|
100 | + $offset = $this->getTimezoneOffset(new DateTimeZone($timezone['timezone_id'])); |
|
101 | + if ($offset !== $gmt_offset) { |
|
102 | + continue; |
|
103 | + } |
|
104 | + return $timezone['timezone_id']; |
|
105 | + } catch (Exception $e) { |
|
106 | + continue; |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + } |
|
111 | + // if $coerce is true, let's see if we can get a timezone string after the offset is adjusted |
|
112 | + if ($coerce === true) { |
|
113 | + $timezone_string = $this->getTimezoneStringFromAbbreviationsList( |
|
114 | + $this->adjustInvalidGmtOffsets($gmt_offset), |
|
115 | + false |
|
116 | + ); |
|
117 | + if ($timezone_string) { |
|
118 | + return $timezone_string; |
|
119 | + } |
|
120 | + } |
|
121 | + throw new EE_Error( |
|
122 | + sprintf( |
|
123 | + esc_html__( |
|
124 | + 'The provided GMT offset (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
125 | + 'event_espresso' |
|
126 | + ), |
|
127 | + $gmt_offset / HOUR_IN_SECONDS, |
|
128 | + '<a href="http://www.php.net/manual/en/timezones.php">', |
|
129 | + '</a>' |
|
130 | + ) |
|
131 | + ); |
|
132 | + } |
|
133 | 133 | |
134 | 134 | |
135 | - /** |
|
136 | - * Depending on PHP version, |
|
137 | - * there might not be valid current timezone strings to match these gmt_offsets in its timezone tables. |
|
138 | - * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
139 | - * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
140 | - * |
|
141 | - * @param int $gmt_offset |
|
142 | - * @return int |
|
143 | - */ |
|
144 | - public function adjustInvalidGmtOffsets($gmt_offset = 0) |
|
145 | - { |
|
146 | - // make sure $gmt_offset is int |
|
147 | - $gmt_offset = (int) $gmt_offset; |
|
148 | - switch ($gmt_offset) { |
|
149 | - // -12 |
|
150 | - case -43200: |
|
151 | - $gmt_offset = -39600; |
|
152 | - break; |
|
153 | - // -11.5 |
|
154 | - case -41400: |
|
155 | - $gmt_offset = -39600; |
|
156 | - break; |
|
157 | - // -10.5 |
|
158 | - case -37800: |
|
159 | - $gmt_offset = -39600; |
|
160 | - break; |
|
161 | - // -8.5 |
|
162 | - case -30600: |
|
163 | - $gmt_offset = -28800; |
|
164 | - break; |
|
165 | - // -7.5 |
|
166 | - case -27000: |
|
167 | - $gmt_offset = -25200; |
|
168 | - break; |
|
169 | - // -6.5 |
|
170 | - case -23400: |
|
171 | - $gmt_offset = -21600; |
|
172 | - break; |
|
173 | - // -5.5 |
|
174 | - case -19800: |
|
175 | - $gmt_offset = -18000; |
|
176 | - break; |
|
177 | - // -4.5 |
|
178 | - case -16200: |
|
179 | - $gmt_offset = -14400; |
|
180 | - break; |
|
181 | - // -3.5 |
|
182 | - case -12600: |
|
183 | - $gmt_offset = -10800; |
|
184 | - break; |
|
185 | - // -2.5 |
|
186 | - case -9000: |
|
187 | - $gmt_offset = -7200; |
|
188 | - break; |
|
189 | - // -1.5 |
|
190 | - case -5400: |
|
191 | - $gmt_offset = -3600; |
|
192 | - break; |
|
193 | - // -0.5 |
|
194 | - case -1800: |
|
195 | - $gmt_offset = 0; |
|
196 | - break; |
|
197 | - // .5 |
|
198 | - case 1800: |
|
199 | - $gmt_offset = 3600; |
|
200 | - break; |
|
201 | - // 1.5 |
|
202 | - case 5400: |
|
203 | - $gmt_offset = 7200; |
|
204 | - break; |
|
205 | - // 2.5 |
|
206 | - case 9000: |
|
207 | - $gmt_offset = 10800; |
|
208 | - break; |
|
209 | - // 3.5 |
|
210 | - case 12600: |
|
211 | - $gmt_offset = 14400; |
|
212 | - break; |
|
213 | - // 7.5 |
|
214 | - case 27000: |
|
215 | - $gmt_offset = 28800; |
|
216 | - break; |
|
217 | - // 8.5 |
|
218 | - case 30600: |
|
219 | - $gmt_offset = 31500; |
|
220 | - break; |
|
221 | - // 10.5 |
|
222 | - case 37800: |
|
223 | - $gmt_offset = 39600; |
|
224 | - break; |
|
225 | - // 11.5 |
|
226 | - case 41400: |
|
227 | - $gmt_offset = 43200; |
|
228 | - break; |
|
229 | - // 12.75 |
|
230 | - case 45900: |
|
231 | - $gmt_offset = 46800; |
|
232 | - break; |
|
233 | - // 13.75 |
|
234 | - case 49500: |
|
235 | - $gmt_offset = 50400; |
|
236 | - break; |
|
237 | - } |
|
238 | - return $gmt_offset; |
|
239 | - } |
|
135 | + /** |
|
136 | + * Depending on PHP version, |
|
137 | + * there might not be valid current timezone strings to match these gmt_offsets in its timezone tables. |
|
138 | + * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
139 | + * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
140 | + * |
|
141 | + * @param int $gmt_offset |
|
142 | + * @return int |
|
143 | + */ |
|
144 | + public function adjustInvalidGmtOffsets($gmt_offset = 0) |
|
145 | + { |
|
146 | + // make sure $gmt_offset is int |
|
147 | + $gmt_offset = (int) $gmt_offset; |
|
148 | + switch ($gmt_offset) { |
|
149 | + // -12 |
|
150 | + case -43200: |
|
151 | + $gmt_offset = -39600; |
|
152 | + break; |
|
153 | + // -11.5 |
|
154 | + case -41400: |
|
155 | + $gmt_offset = -39600; |
|
156 | + break; |
|
157 | + // -10.5 |
|
158 | + case -37800: |
|
159 | + $gmt_offset = -39600; |
|
160 | + break; |
|
161 | + // -8.5 |
|
162 | + case -30600: |
|
163 | + $gmt_offset = -28800; |
|
164 | + break; |
|
165 | + // -7.5 |
|
166 | + case -27000: |
|
167 | + $gmt_offset = -25200; |
|
168 | + break; |
|
169 | + // -6.5 |
|
170 | + case -23400: |
|
171 | + $gmt_offset = -21600; |
|
172 | + break; |
|
173 | + // -5.5 |
|
174 | + case -19800: |
|
175 | + $gmt_offset = -18000; |
|
176 | + break; |
|
177 | + // -4.5 |
|
178 | + case -16200: |
|
179 | + $gmt_offset = -14400; |
|
180 | + break; |
|
181 | + // -3.5 |
|
182 | + case -12600: |
|
183 | + $gmt_offset = -10800; |
|
184 | + break; |
|
185 | + // -2.5 |
|
186 | + case -9000: |
|
187 | + $gmt_offset = -7200; |
|
188 | + break; |
|
189 | + // -1.5 |
|
190 | + case -5400: |
|
191 | + $gmt_offset = -3600; |
|
192 | + break; |
|
193 | + // -0.5 |
|
194 | + case -1800: |
|
195 | + $gmt_offset = 0; |
|
196 | + break; |
|
197 | + // .5 |
|
198 | + case 1800: |
|
199 | + $gmt_offset = 3600; |
|
200 | + break; |
|
201 | + // 1.5 |
|
202 | + case 5400: |
|
203 | + $gmt_offset = 7200; |
|
204 | + break; |
|
205 | + // 2.5 |
|
206 | + case 9000: |
|
207 | + $gmt_offset = 10800; |
|
208 | + break; |
|
209 | + // 3.5 |
|
210 | + case 12600: |
|
211 | + $gmt_offset = 14400; |
|
212 | + break; |
|
213 | + // 7.5 |
|
214 | + case 27000: |
|
215 | + $gmt_offset = 28800; |
|
216 | + break; |
|
217 | + // 8.5 |
|
218 | + case 30600: |
|
219 | + $gmt_offset = 31500; |
|
220 | + break; |
|
221 | + // 10.5 |
|
222 | + case 37800: |
|
223 | + $gmt_offset = 39600; |
|
224 | + break; |
|
225 | + // 11.5 |
|
226 | + case 41400: |
|
227 | + $gmt_offset = 43200; |
|
228 | + break; |
|
229 | + // 12.75 |
|
230 | + case 45900: |
|
231 | + $gmt_offset = 46800; |
|
232 | + break; |
|
233 | + // 13.75 |
|
234 | + case 49500: |
|
235 | + $gmt_offset = 50400; |
|
236 | + break; |
|
237 | + } |
|
238 | + return $gmt_offset; |
|
239 | + } |
|
240 | 240 | } |
@@ -16,95 +16,95 @@ |
||
16 | 16 | interface HelperInterface |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * Ensures that a valid timezone string is returned. |
|
21 | - * |
|
22 | - * @param string $timezone_string When not provided then attempt to use the timezone_string set in the WP Time |
|
23 | - * settings (or derive from set UTC offset). |
|
24 | - * @param bool $throw_error |
|
25 | - * @return string |
|
26 | - */ |
|
27 | - public function getValidTimezoneString($timezone_string = '', $throw_error = false); |
|
28 | - |
|
29 | - |
|
30 | - /** |
|
31 | - * The only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
32 | - * |
|
33 | - * @param string $timezone_string |
|
34 | - * @param bool $throw_error |
|
35 | - * @return bool |
|
36 | - */ |
|
37 | - public function validateTimezone($timezone_string, $throw_error = true); |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * Returns a timezone string for the provided gmt_offset. |
|
42 | - * @param float|string $gmt_offset |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public function getTimezoneStringFromGmtOffset($gmt_offset = ''); |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * Gets the site's GMT offset based on either the timezone string |
|
50 | - * (in which case the gmt offset will vary depending on the location's |
|
51 | - * observance of daylight savings time) or the gmt_offset wp option |
|
52 | - * |
|
53 | - * @return int seconds offset |
|
54 | - */ |
|
55 | - public function getSiteTimezoneGmtOffset(); |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * Get timezone transitions |
|
60 | - * @param DateTimeZone $date_time_zone |
|
61 | - * @param int|null $time |
|
62 | - * @param bool $first_only |
|
63 | - * @return array |
|
64 | - */ |
|
65 | - public function getTimezoneTransitions(DateTimeZone $date_time_zone, $time = null, $first_only = true); |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * Get Timezone offset for given timezone object |
|
70 | - * @param DateTimeZone $date_time_zone |
|
71 | - * @param null|int $time |
|
72 | - * @return int |
|
73 | - */ |
|
74 | - public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null); |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Provide a timezone select input |
|
79 | - * @param string $timezone_string |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function timezoneSelectInput($timezone_string = ''); |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
87 | - * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
88 | - * the site is used. |
|
89 | - * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
90 | - * computed timestamp (i.e. date_i18n() ) |
|
91 | - * |
|
92 | - * @param int $unix_timestamp if 0, then time() will be used. |
|
93 | - * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
94 | - * site will be used. |
|
95 | - * @return int unix_timestamp value with the offset applied for the given timezone. |
|
96 | - */ |
|
97 | - public function getTimestampWithOffset($unix_timestamp = 0, $timezone_string = ''); |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * Depending on PHP version, |
|
102 | - * there might not be valid current timezone strings to match these gmt_offsets in its timezone tables. |
|
103 | - * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
104 | - * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
105 | - * |
|
106 | - * @param int $gmt_offset |
|
107 | - * @return int |
|
108 | - */ |
|
109 | - public function adjustInvalidGmtOffsets($gmt_offset); |
|
19 | + /** |
|
20 | + * Ensures that a valid timezone string is returned. |
|
21 | + * |
|
22 | + * @param string $timezone_string When not provided then attempt to use the timezone_string set in the WP Time |
|
23 | + * settings (or derive from set UTC offset). |
|
24 | + * @param bool $throw_error |
|
25 | + * @return string |
|
26 | + */ |
|
27 | + public function getValidTimezoneString($timezone_string = '', $throw_error = false); |
|
28 | + |
|
29 | + |
|
30 | + /** |
|
31 | + * The only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
32 | + * |
|
33 | + * @param string $timezone_string |
|
34 | + * @param bool $throw_error |
|
35 | + * @return bool |
|
36 | + */ |
|
37 | + public function validateTimezone($timezone_string, $throw_error = true); |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * Returns a timezone string for the provided gmt_offset. |
|
42 | + * @param float|string $gmt_offset |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public function getTimezoneStringFromGmtOffset($gmt_offset = ''); |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * Gets the site's GMT offset based on either the timezone string |
|
50 | + * (in which case the gmt offset will vary depending on the location's |
|
51 | + * observance of daylight savings time) or the gmt_offset wp option |
|
52 | + * |
|
53 | + * @return int seconds offset |
|
54 | + */ |
|
55 | + public function getSiteTimezoneGmtOffset(); |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * Get timezone transitions |
|
60 | + * @param DateTimeZone $date_time_zone |
|
61 | + * @param int|null $time |
|
62 | + * @param bool $first_only |
|
63 | + * @return array |
|
64 | + */ |
|
65 | + public function getTimezoneTransitions(DateTimeZone $date_time_zone, $time = null, $first_only = true); |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * Get Timezone offset for given timezone object |
|
70 | + * @param DateTimeZone $date_time_zone |
|
71 | + * @param null|int $time |
|
72 | + * @return int |
|
73 | + */ |
|
74 | + public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null); |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Provide a timezone select input |
|
79 | + * @param string $timezone_string |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function timezoneSelectInput($timezone_string = ''); |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
87 | + * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
88 | + * the site is used. |
|
89 | + * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
90 | + * computed timestamp (i.e. date_i18n() ) |
|
91 | + * |
|
92 | + * @param int $unix_timestamp if 0, then time() will be used. |
|
93 | + * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
94 | + * site will be used. |
|
95 | + * @return int unix_timestamp value with the offset applied for the given timezone. |
|
96 | + */ |
|
97 | + public function getTimestampWithOffset($unix_timestamp = 0, $timezone_string = ''); |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Depending on PHP version, |
|
102 | + * there might not be valid current timezone strings to match these gmt_offsets in its timezone tables. |
|
103 | + * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
104 | + * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
105 | + * |
|
106 | + * @param int $gmt_offset |
|
107 | + * @return int |
|
108 | + */ |
|
109 | + public function adjustInvalidGmtOffsets($gmt_offset); |
|
110 | 110 | } |
@@ -9,92 +9,92 @@ |
||
9 | 9 | |
10 | 10 | class PhpCompatGreaterFiveSixHelper extends AbstractHelper |
11 | 11 | { |
12 | - /** |
|
13 | - * PhpCompatLessFiveSixHelper constructor. |
|
14 | - * |
|
15 | - * @throws DomainException |
|
16 | - */ |
|
17 | - public function __construct() |
|
18 | - { |
|
19 | - if (PHP_VERSION_ID < 50600) { |
|
20 | - throw new DomainException( |
|
21 | - sprintf( |
|
22 | - esc_html__( |
|
23 | - 'The %1$s is only usable on php versions greater than 5.6. You\'ll want to use %2$s instead.', |
|
24 | - 'event_espresso' |
|
25 | - ), |
|
26 | - __CLASS__, |
|
27 | - 'EventEspresso\core\services\helpers\datetime\PhpCompatLessFiveSixHelper' |
|
28 | - ) |
|
29 | - ); |
|
30 | - } |
|
31 | - parent::__construct(); |
|
32 | - } |
|
12 | + /** |
|
13 | + * PhpCompatLessFiveSixHelper constructor. |
|
14 | + * |
|
15 | + * @throws DomainException |
|
16 | + */ |
|
17 | + public function __construct() |
|
18 | + { |
|
19 | + if (PHP_VERSION_ID < 50600) { |
|
20 | + throw new DomainException( |
|
21 | + sprintf( |
|
22 | + esc_html__( |
|
23 | + 'The %1$s is only usable on php versions greater than 5.6. You\'ll want to use %2$s instead.', |
|
24 | + 'event_espresso' |
|
25 | + ), |
|
26 | + __CLASS__, |
|
27 | + 'EventEspresso\core\services\helpers\datetime\PhpCompatLessFiveSixHelper' |
|
28 | + ) |
|
29 | + ); |
|
30 | + } |
|
31 | + parent::__construct(); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Returns a timezone string for the provided gmt_offset. |
|
36 | - * This is a valid timezone string that can be sent into DateTimeZone |
|
37 | - * |
|
38 | - * @param float|string $gmt_offset |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function getTimezoneStringFromGmtOffset($gmt_offset = '') |
|
42 | - { |
|
43 | - $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset); |
|
44 | - return is_float($gmt_offset_or_timezone_string) |
|
45 | - ? $this->convertWpGmtOffsetForDateTimeZone($gmt_offset_or_timezone_string) |
|
46 | - : $gmt_offset_or_timezone_string; |
|
47 | - } |
|
34 | + /** |
|
35 | + * Returns a timezone string for the provided gmt_offset. |
|
36 | + * This is a valid timezone string that can be sent into DateTimeZone |
|
37 | + * |
|
38 | + * @param float|string $gmt_offset |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function getTimezoneStringFromGmtOffset($gmt_offset = '') |
|
42 | + { |
|
43 | + $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset); |
|
44 | + return is_float($gmt_offset_or_timezone_string) |
|
45 | + ? $this->convertWpGmtOffsetForDateTimeZone($gmt_offset_or_timezone_string) |
|
46 | + : $gmt_offset_or_timezone_string; |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * Returns a formatted offset for use as an argument for constructing DateTimeZone |
|
53 | - * @param float $gmt_offset This should be a float representing the gmt_offset. |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - protected function convertWpGmtOffsetForDateTimeZone($gmt_offset) |
|
57 | - { |
|
58 | - $gmt_offset = (float) $gmt_offset; |
|
59 | - $is_negative = $gmt_offset < 0; |
|
60 | - $gmt_offset *= 100; |
|
61 | - $gmt_offset = absint($gmt_offset); |
|
62 | - // negative and need zero padding? |
|
63 | - if (strlen($gmt_offset) < 4) { |
|
64 | - $gmt_offset = str_pad($gmt_offset, 4, '0', STR_PAD_LEFT); |
|
65 | - } |
|
66 | - $gmt_offset = $this->convertToTimeFraction($gmt_offset); |
|
67 | - // return something like -1300, -0200 or +1300, +0200 |
|
68 | - return $is_negative ? '-' . $gmt_offset : '+' . $gmt_offset; |
|
69 | - } |
|
51 | + /** |
|
52 | + * Returns a formatted offset for use as an argument for constructing DateTimeZone |
|
53 | + * @param float $gmt_offset This should be a float representing the gmt_offset. |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + protected function convertWpGmtOffsetForDateTimeZone($gmt_offset) |
|
57 | + { |
|
58 | + $gmt_offset = (float) $gmt_offset; |
|
59 | + $is_negative = $gmt_offset < 0; |
|
60 | + $gmt_offset *= 100; |
|
61 | + $gmt_offset = absint($gmt_offset); |
|
62 | + // negative and need zero padding? |
|
63 | + if (strlen($gmt_offset) < 4) { |
|
64 | + $gmt_offset = str_pad($gmt_offset, 4, '0', STR_PAD_LEFT); |
|
65 | + } |
|
66 | + $gmt_offset = $this->convertToTimeFraction($gmt_offset); |
|
67 | + // return something like -1300, -0200 or +1300, +0200 |
|
68 | + return $is_negative ? '-' . $gmt_offset : '+' . $gmt_offset; |
|
69 | + } |
|
70 | 70 | |
71 | 71 | |
72 | - /** |
|
73 | - * Converts something like `1550` to `1530` or `0275` to `0245` |
|
74 | - * Incoming offset should be a positive value, this will mutate negative values. Be aware! |
|
75 | - * @param int $offset |
|
76 | - * @return mixed |
|
77 | - */ |
|
78 | - protected function convertToTimeFraction($offset) |
|
79 | - { |
|
80 | - $first_part = substr($offset, 0, 2); |
|
81 | - $second_part = substr($offset, 2, 2); |
|
82 | - $second_part = str_replace(array('25', '50', '75'), array('15', '30', '45'), $second_part); |
|
83 | - return $first_part . $second_part; |
|
84 | - } |
|
72 | + /** |
|
73 | + * Converts something like `1550` to `1530` or `0275` to `0245` |
|
74 | + * Incoming offset should be a positive value, this will mutate negative values. Be aware! |
|
75 | + * @param int $offset |
|
76 | + * @return mixed |
|
77 | + */ |
|
78 | + protected function convertToTimeFraction($offset) |
|
79 | + { |
|
80 | + $first_part = substr($offset, 0, 2); |
|
81 | + $second_part = substr($offset, 2, 2); |
|
82 | + $second_part = str_replace(array('25', '50', '75'), array('15', '30', '45'), $second_part); |
|
83 | + return $first_part . $second_part; |
|
84 | + } |
|
85 | 85 | |
86 | 86 | |
87 | - /** |
|
88 | - * Get Timezone offset for given timezone object |
|
89 | - * |
|
90 | - * @param DateTimeZone $date_time_zone |
|
91 | - * @param null|int $time |
|
92 | - * @return int |
|
93 | - */ |
|
94 | - public function getTimezoneOffset(DateTimezone $date_time_zone, $time = null) |
|
95 | - { |
|
96 | - $time = is_int($time) || $time === null ? $time : (int) strtotime($time); |
|
97 | - $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
98 | - return $date_time_zone->getOffset(new DateTime('@' . $time)); |
|
99 | - } |
|
87 | + /** |
|
88 | + * Get Timezone offset for given timezone object |
|
89 | + * |
|
90 | + * @param DateTimeZone $date_time_zone |
|
91 | + * @param null|int $time |
|
92 | + * @return int |
|
93 | + */ |
|
94 | + public function getTimezoneOffset(DateTimezone $date_time_zone, $time = null) |
|
95 | + { |
|
96 | + $time = is_int($time) || $time === null ? $time : (int) strtotime($time); |
|
97 | + $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
98 | + return $date_time_zone->getOffset(new DateTime('@' . $time)); |
|
99 | + } |
|
100 | 100 | } |
@@ -11,195 +11,195 @@ discard block |
||
11 | 11 | |
12 | 12 | abstract class AbstractHelper implements HelperInterface |
13 | 13 | { |
14 | - /** |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - private $default_timezone_string = ''; |
|
18 | - |
|
19 | - |
|
20 | - /** |
|
21 | - * AbstractHelper constructor. |
|
22 | - */ |
|
23 | - public function __construct() |
|
24 | - { |
|
25 | - add_action('update_option_timezone_string', [$this, 'updateDefaultTimezoneString'], 10, 2); |
|
26 | - } |
|
27 | - /** |
|
28 | - * AbstractHelper destructor. |
|
29 | - */ |
|
30 | - public function __destruct() |
|
31 | - { |
|
32 | - remove_action('update_option_timezone_string', [$this, 'updateDefaultTimezoneString']); |
|
33 | - } |
|
34 | - |
|
35 | - |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * Ensures that a valid timezone string is returned. |
|
40 | - * |
|
41 | - * @param string $timezone_string When not provided then attempt to use the timezone_string set in the WP Time |
|
42 | - * settings (or derive from set UTC offset). |
|
43 | - * @param bool $throw_error |
|
44 | - * @return string |
|
45 | - * @throws EE_Error |
|
46 | - */ |
|
47 | - public function getValidTimezoneString($timezone_string = '', $throw_error = false) |
|
48 | - { |
|
49 | - // if a valid TZ is supplied, then just use that, but don't throw errors if it's invalid |
|
50 | - if (! empty($timezone_string) && $this->validateTimezone($timezone_string, $throw_error)) { |
|
51 | - return $timezone_string; |
|
52 | - } |
|
53 | - // cache whatever gets set as the site default |
|
54 | - // no default set yet? then let's get to it! |
|
55 | - if ($this->default_timezone_string === '') { |
|
56 | - // ignore the incoming TZ string because we already know it's invalid |
|
57 | - // and just pull the default set for the site |
|
58 | - $timezone_string = (string) get_option('timezone_string'); |
|
59 | - // if the site admin has not set a valid default, |
|
60 | - // then derive the TZ from the UTC offset (and hope that that is set) |
|
61 | - $timezone_string = ! empty($timezone_string) |
|
62 | - ? $timezone_string |
|
63 | - : $this->getTimezoneStringFromGmtOffset(); |
|
64 | - // make sure it's all good (and this time blow things up if no TZ can be determined) |
|
65 | - $this->validateTimezone($timezone_string); |
|
66 | - // cache the default so that we don't do this a Brazilian times |
|
67 | - $this->default_timezone_string = $timezone_string; |
|
68 | - } |
|
69 | - return $this->default_timezone_string; |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - public function resetDefaultTimezoneString() |
|
74 | - { |
|
75 | - $this->default_timezone_string = ''; |
|
76 | - } |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * @param $old_value |
|
81 | - * @param $value |
|
82 | - * @throws EE_Error |
|
83 | - * @since $VID:$ |
|
84 | - */ |
|
85 | - public function updateDefaultTimezoneString($old_value, $value) |
|
86 | - { |
|
87 | - if ($value && $value !== $old_value && $value !== $this->default_timezone_string) { |
|
88 | - $this->resetDefaultTimezoneString(); |
|
89 | - $this->getValidTimezoneString($value); |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * The only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
95 | - * |
|
96 | - * @param string $timezone_string |
|
97 | - * @param bool $throw_error |
|
98 | - * @return bool |
|
99 | - * @throws EE_Error |
|
100 | - */ |
|
101 | - public function validateTimezone($timezone_string, $throw_error = true) |
|
102 | - { |
|
103 | - // easiest way to test a timezone string is just see if it throws an error when you try to create a |
|
104 | - // DateTimeZone object with it |
|
105 | - try { |
|
106 | - new DateTimeZone($timezone_string); |
|
107 | - } catch (Exception $e) { |
|
108 | - // sometimes we take exception to exceptions |
|
109 | - if (! $throw_error) { |
|
110 | - return false; |
|
111 | - } |
|
112 | - throw new EE_Error( |
|
113 | - sprintf( |
|
114 | - esc_html__( |
|
115 | - 'The timezone given (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
116 | - 'event_espresso' |
|
117 | - ), |
|
118 | - $timezone_string, |
|
119 | - '<a href="http://www.php.net/manual/en/timezones.php">', |
|
120 | - '</a>' |
|
121 | - ) |
|
122 | - ); |
|
123 | - } |
|
124 | - return true; |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Gets the site's GMT offset based on either the timezone string |
|
130 | - * (in which case the gmt offset will vary depending on the location's |
|
131 | - * observance of daylight savings time) or the gmt_offset wp option |
|
132 | - * |
|
133 | - * @return int seconds offset |
|
134 | - */ |
|
135 | - public function getSiteTimezoneGmtOffset() |
|
136 | - { |
|
137 | - $timezone_string = (string) get_option('timezone_string'); |
|
138 | - if ($timezone_string) { |
|
139 | - try { |
|
140 | - $timezone = new DateTimeZone($timezone_string); |
|
141 | - return $timezone->getOffset(new DateTime()); // in WordPress DateTime defaults to UTC |
|
142 | - } catch (Exception $e) { |
|
143 | - } |
|
144 | - } |
|
145 | - $offset = get_option('gmt_offset'); |
|
146 | - return (int) ($offset * HOUR_IN_SECONDS); |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * Get Timezone offset for given timezone object |
|
152 | - * |
|
153 | - * @param DateTimeZone $date_time_zone |
|
154 | - * @param null|int $time |
|
155 | - * @return int |
|
156 | - * @throws DomainException |
|
157 | - */ |
|
158 | - public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null) |
|
159 | - { |
|
160 | - $transition = $this->getTimezoneTransitions($date_time_zone, $time); |
|
161 | - if (! isset($transition['offset'])) { |
|
162 | - throw new DomainException( |
|
163 | - sprintf( |
|
164 | - esc_html__('An invalid timezone transition was received %1$s', 'event_espresso'), |
|
165 | - print_r($transition, true) |
|
166 | - ) |
|
167 | - ); |
|
168 | - } |
|
169 | - return $transition['offset']; |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * Provide a timezone select input |
|
175 | - * |
|
176 | - * @param string $timezone_string |
|
177 | - * @return string |
|
178 | - * @throws EE_Error |
|
179 | - */ |
|
180 | - public function timezoneSelectInput($timezone_string = '') |
|
181 | - { |
|
182 | - // get WP date time format |
|
183 | - $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
|
184 | - // if passed a value, then use that, else get WP option |
|
185 | - $timezone_string = ! empty($timezone_string) ? $timezone_string : (string) get_option('timezone_string'); |
|
186 | - // check if the timezone is valid but don't throw any errors if it isn't |
|
187 | - $timezone_string = $this->validateTimezone($timezone_string, false) |
|
188 | - ? $timezone_string |
|
189 | - : ''; |
|
190 | - $gmt_offset = get_option('gmt_offset'); |
|
191 | - $check_zone_info = true; |
|
192 | - if (empty($timezone_string)) { |
|
193 | - // Create a UTC+- zone if no timezone string exists |
|
194 | - $timezone_string = 'UTC'; |
|
195 | - $check_zone_info = false; |
|
196 | - if ($gmt_offset > 0) { |
|
197 | - $timezone_string = 'UTC+' . $gmt_offset; |
|
198 | - } elseif ($gmt_offset < 0) { |
|
199 | - $timezone_string = 'UTC' . $gmt_offset; |
|
200 | - } |
|
201 | - } |
|
202 | - ?> |
|
14 | + /** |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + private $default_timezone_string = ''; |
|
18 | + |
|
19 | + |
|
20 | + /** |
|
21 | + * AbstractHelper constructor. |
|
22 | + */ |
|
23 | + public function __construct() |
|
24 | + { |
|
25 | + add_action('update_option_timezone_string', [$this, 'updateDefaultTimezoneString'], 10, 2); |
|
26 | + } |
|
27 | + /** |
|
28 | + * AbstractHelper destructor. |
|
29 | + */ |
|
30 | + public function __destruct() |
|
31 | + { |
|
32 | + remove_action('update_option_timezone_string', [$this, 'updateDefaultTimezoneString']); |
|
33 | + } |
|
34 | + |
|
35 | + |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * Ensures that a valid timezone string is returned. |
|
40 | + * |
|
41 | + * @param string $timezone_string When not provided then attempt to use the timezone_string set in the WP Time |
|
42 | + * settings (or derive from set UTC offset). |
|
43 | + * @param bool $throw_error |
|
44 | + * @return string |
|
45 | + * @throws EE_Error |
|
46 | + */ |
|
47 | + public function getValidTimezoneString($timezone_string = '', $throw_error = false) |
|
48 | + { |
|
49 | + // if a valid TZ is supplied, then just use that, but don't throw errors if it's invalid |
|
50 | + if (! empty($timezone_string) && $this->validateTimezone($timezone_string, $throw_error)) { |
|
51 | + return $timezone_string; |
|
52 | + } |
|
53 | + // cache whatever gets set as the site default |
|
54 | + // no default set yet? then let's get to it! |
|
55 | + if ($this->default_timezone_string === '') { |
|
56 | + // ignore the incoming TZ string because we already know it's invalid |
|
57 | + // and just pull the default set for the site |
|
58 | + $timezone_string = (string) get_option('timezone_string'); |
|
59 | + // if the site admin has not set a valid default, |
|
60 | + // then derive the TZ from the UTC offset (and hope that that is set) |
|
61 | + $timezone_string = ! empty($timezone_string) |
|
62 | + ? $timezone_string |
|
63 | + : $this->getTimezoneStringFromGmtOffset(); |
|
64 | + // make sure it's all good (and this time blow things up if no TZ can be determined) |
|
65 | + $this->validateTimezone($timezone_string); |
|
66 | + // cache the default so that we don't do this a Brazilian times |
|
67 | + $this->default_timezone_string = $timezone_string; |
|
68 | + } |
|
69 | + return $this->default_timezone_string; |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + public function resetDefaultTimezoneString() |
|
74 | + { |
|
75 | + $this->default_timezone_string = ''; |
|
76 | + } |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * @param $old_value |
|
81 | + * @param $value |
|
82 | + * @throws EE_Error |
|
83 | + * @since $VID:$ |
|
84 | + */ |
|
85 | + public function updateDefaultTimezoneString($old_value, $value) |
|
86 | + { |
|
87 | + if ($value && $value !== $old_value && $value !== $this->default_timezone_string) { |
|
88 | + $this->resetDefaultTimezoneString(); |
|
89 | + $this->getValidTimezoneString($value); |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * The only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
95 | + * |
|
96 | + * @param string $timezone_string |
|
97 | + * @param bool $throw_error |
|
98 | + * @return bool |
|
99 | + * @throws EE_Error |
|
100 | + */ |
|
101 | + public function validateTimezone($timezone_string, $throw_error = true) |
|
102 | + { |
|
103 | + // easiest way to test a timezone string is just see if it throws an error when you try to create a |
|
104 | + // DateTimeZone object with it |
|
105 | + try { |
|
106 | + new DateTimeZone($timezone_string); |
|
107 | + } catch (Exception $e) { |
|
108 | + // sometimes we take exception to exceptions |
|
109 | + if (! $throw_error) { |
|
110 | + return false; |
|
111 | + } |
|
112 | + throw new EE_Error( |
|
113 | + sprintf( |
|
114 | + esc_html__( |
|
115 | + 'The timezone given (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
116 | + 'event_espresso' |
|
117 | + ), |
|
118 | + $timezone_string, |
|
119 | + '<a href="http://www.php.net/manual/en/timezones.php">', |
|
120 | + '</a>' |
|
121 | + ) |
|
122 | + ); |
|
123 | + } |
|
124 | + return true; |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Gets the site's GMT offset based on either the timezone string |
|
130 | + * (in which case the gmt offset will vary depending on the location's |
|
131 | + * observance of daylight savings time) or the gmt_offset wp option |
|
132 | + * |
|
133 | + * @return int seconds offset |
|
134 | + */ |
|
135 | + public function getSiteTimezoneGmtOffset() |
|
136 | + { |
|
137 | + $timezone_string = (string) get_option('timezone_string'); |
|
138 | + if ($timezone_string) { |
|
139 | + try { |
|
140 | + $timezone = new DateTimeZone($timezone_string); |
|
141 | + return $timezone->getOffset(new DateTime()); // in WordPress DateTime defaults to UTC |
|
142 | + } catch (Exception $e) { |
|
143 | + } |
|
144 | + } |
|
145 | + $offset = get_option('gmt_offset'); |
|
146 | + return (int) ($offset * HOUR_IN_SECONDS); |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * Get Timezone offset for given timezone object |
|
152 | + * |
|
153 | + * @param DateTimeZone $date_time_zone |
|
154 | + * @param null|int $time |
|
155 | + * @return int |
|
156 | + * @throws DomainException |
|
157 | + */ |
|
158 | + public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null) |
|
159 | + { |
|
160 | + $transition = $this->getTimezoneTransitions($date_time_zone, $time); |
|
161 | + if (! isset($transition['offset'])) { |
|
162 | + throw new DomainException( |
|
163 | + sprintf( |
|
164 | + esc_html__('An invalid timezone transition was received %1$s', 'event_espresso'), |
|
165 | + print_r($transition, true) |
|
166 | + ) |
|
167 | + ); |
|
168 | + } |
|
169 | + return $transition['offset']; |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * Provide a timezone select input |
|
175 | + * |
|
176 | + * @param string $timezone_string |
|
177 | + * @return string |
|
178 | + * @throws EE_Error |
|
179 | + */ |
|
180 | + public function timezoneSelectInput($timezone_string = '') |
|
181 | + { |
|
182 | + // get WP date time format |
|
183 | + $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
|
184 | + // if passed a value, then use that, else get WP option |
|
185 | + $timezone_string = ! empty($timezone_string) ? $timezone_string : (string) get_option('timezone_string'); |
|
186 | + // check if the timezone is valid but don't throw any errors if it isn't |
|
187 | + $timezone_string = $this->validateTimezone($timezone_string, false) |
|
188 | + ? $timezone_string |
|
189 | + : ''; |
|
190 | + $gmt_offset = get_option('gmt_offset'); |
|
191 | + $check_zone_info = true; |
|
192 | + if (empty($timezone_string)) { |
|
193 | + // Create a UTC+- zone if no timezone string exists |
|
194 | + $timezone_string = 'UTC'; |
|
195 | + $check_zone_info = false; |
|
196 | + if ($gmt_offset > 0) { |
|
197 | + $timezone_string = 'UTC+' . $gmt_offset; |
|
198 | + } elseif ($gmt_offset < 0) { |
|
199 | + $timezone_string = 'UTC' . $gmt_offset; |
|
200 | + } |
|
201 | + } |
|
202 | + ?> |
|
203 | 203 | <p> |
204 | 204 | <label for="timezone_string"><?php _e('timezone', 'event_espresso'); ?></label> |
205 | 205 | <select id="timezone_string" name="timezone_string"> |
@@ -212,150 +212,150 @@ discard block |
||
212 | 212 | <p> |
213 | 213 | <span> |
214 | 214 | <?php |
215 | - printf( |
|
216 | - __('%1$sUTC%2$s time is %3$s', 'event_espresso'), |
|
217 | - '<abbr title="Coordinated Universal Time">', |
|
218 | - '</abbr>', |
|
219 | - '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
|
220 | - ); |
|
221 | - ?></span> |
|
215 | + printf( |
|
216 | + __('%1$sUTC%2$s time is %3$s', 'event_espresso'), |
|
217 | + '<abbr title="Coordinated Universal Time">', |
|
218 | + '</abbr>', |
|
219 | + '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
|
220 | + ); |
|
221 | + ?></span> |
|
222 | 222 | <?php |
223 | - if (! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
|
223 | + if (! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
|
224 | 224 | <br/><span><?php printf(__('Local time is %1$s', 'event_espresso'), '<code>' . date_i18n($datetime_format) . '</code>'); ?></span> |
225 | 225 | <?php |
226 | - endif; ?> |
|
226 | + endif; ?> |
|
227 | 227 | |
228 | 228 | <?php |
229 | - if ($check_zone_info && $timezone_string) : ?> |
|
229 | + if ($check_zone_info && $timezone_string) : ?> |
|
230 | 230 | <br/> |
231 | 231 | <span> |
232 | 232 | <?php |
233 | - // Set TZ so localtime works. |
|
234 | - date_default_timezone_set($timezone_string); |
|
235 | - $now = localtime(time(), true); |
|
236 | - if ($now['tm_isdst']) { |
|
237 | - _e('This timezone is currently in daylight saving time.', 'event_espresso'); |
|
238 | - } else { |
|
239 | - _e('This timezone is currently in standard time.', 'event_espresso'); |
|
240 | - } |
|
241 | - ?> |
|
233 | + // Set TZ so localtime works. |
|
234 | + date_default_timezone_set($timezone_string); |
|
235 | + $now = localtime(time(), true); |
|
236 | + if ($now['tm_isdst']) { |
|
237 | + _e('This timezone is currently in daylight saving time.', 'event_espresso'); |
|
238 | + } else { |
|
239 | + _e('This timezone is currently in standard time.', 'event_espresso'); |
|
240 | + } |
|
241 | + ?> |
|
242 | 242 | <br/> |
243 | 243 | <?php |
244 | - if (function_exists('timezone_transitions_get')) { |
|
245 | - $found = false; |
|
246 | - $date_time_zone_selected = new DateTimeZone($timezone_string); |
|
247 | - $tz_offset = timezone_offset_get($date_time_zone_selected, date_create()); |
|
248 | - $right_now = time(); |
|
249 | - $tr['isdst'] = false; |
|
250 | - foreach (timezone_transitions_get($date_time_zone_selected) as $tr) { |
|
251 | - if ($tr['ts'] > $right_now) { |
|
252 | - $found = true; |
|
253 | - break; |
|
254 | - } |
|
255 | - } |
|
256 | - if ($found) { |
|
257 | - $message = $tr['isdst'] |
|
258 | - ? __(' Daylight saving time begins on: %s.', 'event_espresso') |
|
259 | - : __(' Standard time begins on: %s.', 'event_espresso'); |
|
260 | - // Add the difference between the current offset and the new offset to ts to get the correct |
|
261 | - // transition time from date_i18n(). |
|
262 | - printf( |
|
263 | - $message, |
|
264 | - '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >' |
|
265 | - ); |
|
266 | - } else { |
|
267 | - _e('This timezone does not observe daylight saving time.', 'event_espresso'); |
|
268 | - } |
|
269 | - } |
|
270 | - // Set back to UTC. |
|
271 | - date_default_timezone_set('UTC'); |
|
272 | - ?> |
|
244 | + if (function_exists('timezone_transitions_get')) { |
|
245 | + $found = false; |
|
246 | + $date_time_zone_selected = new DateTimeZone($timezone_string); |
|
247 | + $tz_offset = timezone_offset_get($date_time_zone_selected, date_create()); |
|
248 | + $right_now = time(); |
|
249 | + $tr['isdst'] = false; |
|
250 | + foreach (timezone_transitions_get($date_time_zone_selected) as $tr) { |
|
251 | + if ($tr['ts'] > $right_now) { |
|
252 | + $found = true; |
|
253 | + break; |
|
254 | + } |
|
255 | + } |
|
256 | + if ($found) { |
|
257 | + $message = $tr['isdst'] |
|
258 | + ? __(' Daylight saving time begins on: %s.', 'event_espresso') |
|
259 | + : __(' Standard time begins on: %s.', 'event_espresso'); |
|
260 | + // Add the difference between the current offset and the new offset to ts to get the correct |
|
261 | + // transition time from date_i18n(). |
|
262 | + printf( |
|
263 | + $message, |
|
264 | + '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >' |
|
265 | + ); |
|
266 | + } else { |
|
267 | + _e('This timezone does not observe daylight saving time.', 'event_espresso'); |
|
268 | + } |
|
269 | + } |
|
270 | + // Set back to UTC. |
|
271 | + date_default_timezone_set('UTC'); |
|
272 | + ?> |
|
273 | 273 | </span></p> |
274 | 274 | <?php |
275 | - endif; |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - /** |
|
280 | - * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
281 | - * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
282 | - * the site is used. |
|
283 | - * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
284 | - * computed timestamp (i.e. date_i18n() ) |
|
285 | - * |
|
286 | - * @param int $unix_timestamp if 0, then time() will be used. |
|
287 | - * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
288 | - * site will be used. |
|
289 | - * @return int unix_timestamp value with the offset applied for the given timezone. |
|
290 | - * @throws EE_Error |
|
291 | - */ |
|
292 | - public function getTimestampWithOffset($unix_timestamp = 0, $timezone_string = '') |
|
293 | - { |
|
294 | - $unix_timestamp = $unix_timestamp === 0 ? time() : (int) $unix_timestamp; |
|
295 | - $timezone_string = $this->getValidTimezoneString($timezone_string); |
|
296 | - $TimeZone = new DateTimeZone($timezone_string); |
|
297 | - $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
|
298 | - $offset = timezone_offset_get($TimeZone, $DateTime); |
|
299 | - return (int) $DateTime->format('U') + (int) $offset; |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - /** |
|
304 | - * Get Timezone Transitions |
|
305 | - * |
|
306 | - * @param DateTimeZone $date_time_zone |
|
307 | - * @param int|null $time |
|
308 | - * @param bool $first_only |
|
309 | - * @return array|mixed |
|
310 | - */ |
|
311 | - public function getTimezoneTransitions(DateTimeZone $date_time_zone, $time = null, $first_only = true) |
|
312 | - { |
|
313 | - $time = is_int($time) || $time === null ? $time : (int) strtotime($time); |
|
314 | - $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
315 | - $transitions = $date_time_zone->getTransitions($time); |
|
316 | - return $first_only && ! isset($transitions['ts']) ? reset($transitions) : $transitions; |
|
317 | - } |
|
318 | - |
|
319 | - |
|
320 | - |
|
321 | - /** |
|
322 | - * Default to just returning the provided $gmt_offset. Children can override if adjustment needed. |
|
323 | - * |
|
324 | - * @param int $gmt_offset |
|
325 | - * @return int |
|
326 | - */ |
|
327 | - public function adjustInvalidGmtOffsets($gmt_offset = 0) |
|
328 | - { |
|
329 | - return $gmt_offset; |
|
330 | - } |
|
331 | - |
|
332 | - |
|
333 | - |
|
334 | - /** |
|
335 | - * This receives an incoming gmt_offset and santizes it. If the provide value is an empty string, then this will |
|
336 | - * attempt to get the offset from the timezone string. If this returns a string, then a timezone string was |
|
337 | - * successfully derived from existing timezone_string in the db. If not, then a float is returned for the provided |
|
338 | - * offset. |
|
339 | - * @param float|string $gmt_offset |
|
340 | - * @return float|string |
|
341 | - */ |
|
342 | - protected function sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset) |
|
343 | - { |
|
344 | - // if there is no incoming gmt_offset, then because WP hooks in on timezone_string, we need to see if that is |
|
345 | - // set because it will override `gmt_offset` via `pre_get_option` filter. If that's set, then let's just use |
|
346 | - // that! Otherwise we'll leave timezone_string at the default of 'UTC' before doing other logic. |
|
347 | - if ($gmt_offset === '') { |
|
348 | - // autoloaded so no need to set to a variable. There will not be multiple hits to the db. |
|
349 | - if (get_option('timezone_string')) { |
|
350 | - return (string) get_option('timezone_string'); |
|
351 | - } |
|
352 | - } |
|
353 | - $gmt_offset = $gmt_offset !== '' ? $gmt_offset : (string) get_option('gmt_offset'); |
|
354 | - $gmt_offset = (float) $gmt_offset; |
|
355 | - // if $gmt_offset is 0 or is still an empty string, then just return UTC |
|
356 | - if ($gmt_offset === (float) 0) { |
|
357 | - return 'UTC'; |
|
358 | - } |
|
359 | - return $gmt_offset; |
|
360 | - } |
|
275 | + endif; |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + /** |
|
280 | + * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
281 | + * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
282 | + * the site is used. |
|
283 | + * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
284 | + * computed timestamp (i.e. date_i18n() ) |
|
285 | + * |
|
286 | + * @param int $unix_timestamp if 0, then time() will be used. |
|
287 | + * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
288 | + * site will be used. |
|
289 | + * @return int unix_timestamp value with the offset applied for the given timezone. |
|
290 | + * @throws EE_Error |
|
291 | + */ |
|
292 | + public function getTimestampWithOffset($unix_timestamp = 0, $timezone_string = '') |
|
293 | + { |
|
294 | + $unix_timestamp = $unix_timestamp === 0 ? time() : (int) $unix_timestamp; |
|
295 | + $timezone_string = $this->getValidTimezoneString($timezone_string); |
|
296 | + $TimeZone = new DateTimeZone($timezone_string); |
|
297 | + $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
|
298 | + $offset = timezone_offset_get($TimeZone, $DateTime); |
|
299 | + return (int) $DateTime->format('U') + (int) $offset; |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + /** |
|
304 | + * Get Timezone Transitions |
|
305 | + * |
|
306 | + * @param DateTimeZone $date_time_zone |
|
307 | + * @param int|null $time |
|
308 | + * @param bool $first_only |
|
309 | + * @return array|mixed |
|
310 | + */ |
|
311 | + public function getTimezoneTransitions(DateTimeZone $date_time_zone, $time = null, $first_only = true) |
|
312 | + { |
|
313 | + $time = is_int($time) || $time === null ? $time : (int) strtotime($time); |
|
314 | + $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
315 | + $transitions = $date_time_zone->getTransitions($time); |
|
316 | + return $first_only && ! isset($transitions['ts']) ? reset($transitions) : $transitions; |
|
317 | + } |
|
318 | + |
|
319 | + |
|
320 | + |
|
321 | + /** |
|
322 | + * Default to just returning the provided $gmt_offset. Children can override if adjustment needed. |
|
323 | + * |
|
324 | + * @param int $gmt_offset |
|
325 | + * @return int |
|
326 | + */ |
|
327 | + public function adjustInvalidGmtOffsets($gmt_offset = 0) |
|
328 | + { |
|
329 | + return $gmt_offset; |
|
330 | + } |
|
331 | + |
|
332 | + |
|
333 | + |
|
334 | + /** |
|
335 | + * This receives an incoming gmt_offset and santizes it. If the provide value is an empty string, then this will |
|
336 | + * attempt to get the offset from the timezone string. If this returns a string, then a timezone string was |
|
337 | + * successfully derived from existing timezone_string in the db. If not, then a float is returned for the provided |
|
338 | + * offset. |
|
339 | + * @param float|string $gmt_offset |
|
340 | + * @return float|string |
|
341 | + */ |
|
342 | + protected function sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset) |
|
343 | + { |
|
344 | + // if there is no incoming gmt_offset, then because WP hooks in on timezone_string, we need to see if that is |
|
345 | + // set because it will override `gmt_offset` via `pre_get_option` filter. If that's set, then let's just use |
|
346 | + // that! Otherwise we'll leave timezone_string at the default of 'UTC' before doing other logic. |
|
347 | + if ($gmt_offset === '') { |
|
348 | + // autoloaded so no need to set to a variable. There will not be multiple hits to the db. |
|
349 | + if (get_option('timezone_string')) { |
|
350 | + return (string) get_option('timezone_string'); |
|
351 | + } |
|
352 | + } |
|
353 | + $gmt_offset = $gmt_offset !== '' ? $gmt_offset : (string) get_option('gmt_offset'); |
|
354 | + $gmt_offset = (float) $gmt_offset; |
|
355 | + // if $gmt_offset is 0 or is still an empty string, then just return UTC |
|
356 | + if ($gmt_offset === (float) 0) { |
|
357 | + return 'UTC'; |
|
358 | + } |
|
359 | + return $gmt_offset; |
|
360 | + } |
|
361 | 361 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | public function getValidTimezoneString($timezone_string = '', $throw_error = false) |
48 | 48 | { |
49 | 49 | // if a valid TZ is supplied, then just use that, but don't throw errors if it's invalid |
50 | - if (! empty($timezone_string) && $this->validateTimezone($timezone_string, $throw_error)) { |
|
50 | + if ( ! empty($timezone_string) && $this->validateTimezone($timezone_string, $throw_error)) { |
|
51 | 51 | return $timezone_string; |
52 | 52 | } |
53 | 53 | // cache whatever gets set as the site default |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | new DateTimeZone($timezone_string); |
107 | 107 | } catch (Exception $e) { |
108 | 108 | // sometimes we take exception to exceptions |
109 | - if (! $throw_error) { |
|
109 | + if ( ! $throw_error) { |
|
110 | 110 | return false; |
111 | 111 | } |
112 | 112 | throw new EE_Error( |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null) |
159 | 159 | { |
160 | 160 | $transition = $this->getTimezoneTransitions($date_time_zone, $time); |
161 | - if (! isset($transition['offset'])) { |
|
161 | + if ( ! isset($transition['offset'])) { |
|
162 | 162 | throw new DomainException( |
163 | 163 | sprintf( |
164 | 164 | esc_html__('An invalid timezone transition was received %1$s', 'event_espresso'), |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | public function timezoneSelectInput($timezone_string = '') |
181 | 181 | { |
182 | 182 | // get WP date time format |
183 | - $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
|
183 | + $datetime_format = get_option('date_format').' '.get_option('time_format'); |
|
184 | 184 | // if passed a value, then use that, else get WP option |
185 | 185 | $timezone_string = ! empty($timezone_string) ? $timezone_string : (string) get_option('timezone_string'); |
186 | 186 | // check if the timezone is valid but don't throw any errors if it isn't |
@@ -194,9 +194,9 @@ discard block |
||
194 | 194 | $timezone_string = 'UTC'; |
195 | 195 | $check_zone_info = false; |
196 | 196 | if ($gmt_offset > 0) { |
197 | - $timezone_string = 'UTC+' . $gmt_offset; |
|
197 | + $timezone_string = 'UTC+'.$gmt_offset; |
|
198 | 198 | } elseif ($gmt_offset < 0) { |
199 | - $timezone_string = 'UTC' . $gmt_offset; |
|
199 | + $timezone_string = 'UTC'.$gmt_offset; |
|
200 | 200 | } |
201 | 201 | } |
202 | 202 | ?> |
@@ -216,12 +216,12 @@ discard block |
||
216 | 216 | __('%1$sUTC%2$s time is %3$s', 'event_espresso'), |
217 | 217 | '<abbr title="Coordinated Universal Time">', |
218 | 218 | '</abbr>', |
219 | - '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
|
219 | + '<code>'.date_i18n($datetime_format, false, true).'</code>' |
|
220 | 220 | ); |
221 | 221 | ?></span> |
222 | 222 | <?php |
223 | - if (! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
|
224 | - <br/><span><?php printf(__('Local time is %1$s', 'event_espresso'), '<code>' . date_i18n($datetime_format) . '</code>'); ?></span> |
|
223 | + if ( ! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
|
224 | + <br/><span><?php printf(__('Local time is %1$s', 'event_espresso'), '<code>'.date_i18n($datetime_format).'</code>'); ?></span> |
|
225 | 225 | <?php |
226 | 226 | endif; ?> |
227 | 227 | |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | // transition time from date_i18n(). |
262 | 262 | printf( |
263 | 263 | $message, |
264 | - '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >' |
|
264 | + '<code >'.date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])).'</code >' |
|
265 | 265 | ); |
266 | 266 | } else { |
267 | 267 | _e('This timezone does not observe daylight saving time.', 'event_espresso'); |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | $unix_timestamp = $unix_timestamp === 0 ? time() : (int) $unix_timestamp; |
295 | 295 | $timezone_string = $this->getValidTimezoneString($timezone_string); |
296 | 296 | $TimeZone = new DateTimeZone($timezone_string); |
297 | - $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
|
297 | + $DateTime = new DateTime('@'.$unix_timestamp, $TimeZone); |
|
298 | 298 | $offset = timezone_offset_get($TimeZone, $DateTime); |
299 | 299 | return (int) $DateTime->format('U') + (int) $offset; |
300 | 300 | } |
@@ -23,1704 +23,1704 @@ |
||
23 | 23 | class EE_Registry implements ResettableInterface |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @var EE_Registry $_instance |
|
28 | - */ |
|
29 | - private static $_instance; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var EE_Dependency_Map $_dependency_map |
|
33 | - */ |
|
34 | - protected $_dependency_map; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var Mirror |
|
38 | - */ |
|
39 | - private $mirror; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var ClassInterfaceCache $class_cache |
|
43 | - */ |
|
44 | - private $class_cache; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var array $_class_abbreviations |
|
48 | - */ |
|
49 | - protected $_class_abbreviations = array(); |
|
50 | - |
|
51 | - /** |
|
52 | - * @var CommandBusInterface $BUS |
|
53 | - */ |
|
54 | - public $BUS; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var EE_Cart $CART |
|
58 | - */ |
|
59 | - public $CART; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var EE_Config $CFG |
|
63 | - */ |
|
64 | - public $CFG; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var EE_Network_Config $NET_CFG |
|
68 | - */ |
|
69 | - public $NET_CFG; |
|
70 | - |
|
71 | - /** |
|
72 | - * StdClass object for storing library classes in |
|
73 | - * |
|
74 | - * @var RegistryContainer $LIB |
|
75 | - */ |
|
76 | - public $LIB; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var EE_Request_Handler $REQ |
|
80 | - */ |
|
81 | - public $REQ; |
|
82 | - |
|
83 | - /** |
|
84 | - * @var EE_Session $SSN |
|
85 | - */ |
|
86 | - public $SSN; |
|
87 | - |
|
88 | - /** |
|
89 | - * @since 4.5.0 |
|
90 | - * @var EE_Capabilities $CAP |
|
91 | - */ |
|
92 | - public $CAP; |
|
93 | - |
|
94 | - /** |
|
95 | - * @since 4.9.0 |
|
96 | - * @var EE_Message_Resource_Manager $MRM |
|
97 | - */ |
|
98 | - public $MRM; |
|
99 | - |
|
100 | - /** |
|
101 | - * @var Registry $AssetsRegistry |
|
102 | - */ |
|
103 | - public $AssetsRegistry; |
|
104 | - |
|
105 | - /** |
|
106 | - * StdClass object for holding addons which have registered themselves to work with EE core |
|
107 | - * |
|
108 | - * @var EE_Addon[] $addons |
|
109 | - */ |
|
110 | - public $addons; |
|
111 | - |
|
112 | - /** |
|
113 | - * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
114 | - * |
|
115 | - * @var EEM_Base[] $models |
|
116 | - */ |
|
117 | - public $models = array(); |
|
118 | - |
|
119 | - /** |
|
120 | - * @var EED_Module[] $modules |
|
121 | - */ |
|
122 | - public $modules; |
|
123 | - |
|
124 | - /** |
|
125 | - * @var EES_Shortcode[] $shortcodes |
|
126 | - */ |
|
127 | - public $shortcodes; |
|
128 | - |
|
129 | - /** |
|
130 | - * @var WP_Widget[] $widgets |
|
131 | - */ |
|
132 | - public $widgets; |
|
133 | - |
|
134 | - /** |
|
135 | - * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
136 | - * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
137 | - * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
138 | - * classnames (eg "EEM_Event") |
|
139 | - * |
|
140 | - * @var array $non_abstract_db_models |
|
141 | - */ |
|
142 | - public $non_abstract_db_models = array(); |
|
143 | - |
|
144 | - /** |
|
145 | - * internationalization for JS strings |
|
146 | - * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
147 | - * in js file: var translatedString = eei18n.string_key; |
|
148 | - * |
|
149 | - * @var array $i18n_js_strings |
|
150 | - */ |
|
151 | - public static $i18n_js_strings = array(); |
|
152 | - |
|
153 | - /** |
|
154 | - * $main_file - path to espresso.php |
|
155 | - * |
|
156 | - * @var array $main_file |
|
157 | - */ |
|
158 | - public $main_file; |
|
159 | - |
|
160 | - /** |
|
161 | - * array of ReflectionClass objects where the key is the class name |
|
162 | - * |
|
163 | - * @deprecated 4.9.62.p |
|
164 | - * @var ReflectionClass[] $_reflectors |
|
165 | - */ |
|
166 | - public $_reflectors; |
|
167 | - |
|
168 | - /** |
|
169 | - * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
170 | - * |
|
171 | - * @var boolean $_cache_on |
|
172 | - */ |
|
173 | - protected $_cache_on = true; |
|
174 | - |
|
175 | - /** |
|
176 | - * @var ObjectIdentifier |
|
177 | - */ |
|
178 | - private $object_identifier; |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * @singleton method used to instantiate class object |
|
183 | - * @param EE_Dependency_Map|null $dependency_map |
|
184 | - * @param Mirror|null $mirror |
|
185 | - * @param ClassInterfaceCache|null $class_cache |
|
186 | - * @param ObjectIdentifier|null $object_identifier |
|
187 | - * @return EE_Registry instance |
|
188 | - */ |
|
189 | - public static function instance( |
|
190 | - EE_Dependency_Map $dependency_map = null, |
|
191 | - Mirror $mirror = null, |
|
192 | - ClassInterfaceCache $class_cache = null, |
|
193 | - ObjectIdentifier $object_identifier = null |
|
194 | - ) { |
|
195 | - // check if class object is instantiated |
|
196 | - if ( |
|
197 | - ! self::$_instance instanceof EE_Registry |
|
198 | - && $dependency_map instanceof EE_Dependency_Map |
|
199 | - && $mirror instanceof Mirror |
|
200 | - && $class_cache instanceof ClassInterfaceCache |
|
201 | - && $object_identifier instanceof ObjectIdentifier |
|
202 | - ) { |
|
203 | - self::$_instance = new self( |
|
204 | - $dependency_map, |
|
205 | - $mirror, |
|
206 | - $class_cache, |
|
207 | - $object_identifier |
|
208 | - ); |
|
209 | - } |
|
210 | - return self::$_instance; |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * protected constructor to prevent direct creation |
|
216 | - * |
|
217 | - * @Constructor |
|
218 | - * @param EE_Dependency_Map $dependency_map |
|
219 | - * @param Mirror $mirror |
|
220 | - * @param ClassInterfaceCache $class_cache |
|
221 | - * @param ObjectIdentifier $object_identifier |
|
222 | - */ |
|
223 | - protected function __construct( |
|
224 | - EE_Dependency_Map $dependency_map, |
|
225 | - Mirror $mirror, |
|
226 | - ClassInterfaceCache $class_cache, |
|
227 | - ObjectIdentifier $object_identifier |
|
228 | - ) { |
|
229 | - $this->_dependency_map = $dependency_map; |
|
230 | - $this->mirror = $mirror; |
|
231 | - $this->class_cache = $class_cache; |
|
232 | - $this->object_identifier = $object_identifier; |
|
233 | - // $registry_container = new RegistryContainer(); |
|
234 | - $this->LIB = new RegistryContainer(); |
|
235 | - $this->addons = new RegistryContainer(); |
|
236 | - $this->modules = new RegistryContainer(); |
|
237 | - $this->shortcodes = new RegistryContainer(); |
|
238 | - $this->widgets = new RegistryContainer(); |
|
239 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
240 | - } |
|
241 | - |
|
242 | - |
|
243 | - /** |
|
244 | - * initialize |
|
245 | - * |
|
246 | - * @throws OutOfBoundsException |
|
247 | - * @throws InvalidArgumentException |
|
248 | - * @throws InvalidInterfaceException |
|
249 | - * @throws InvalidDataTypeException |
|
250 | - * @throws EE_Error |
|
251 | - * @throws ReflectionException |
|
252 | - */ |
|
253 | - public function initialize() |
|
254 | - { |
|
255 | - $this->_class_abbreviations = apply_filters( |
|
256 | - 'FHEE__EE_Registry____construct___class_abbreviations', |
|
257 | - array( |
|
258 | - 'EE_Config' => 'CFG', |
|
259 | - 'EE_Session' => 'SSN', |
|
260 | - 'EE_Capabilities' => 'CAP', |
|
261 | - 'EE_Cart' => 'CART', |
|
262 | - 'EE_Network_Config' => 'NET_CFG', |
|
263 | - 'EE_Request_Handler' => 'REQ', |
|
264 | - 'EE_Message_Resource_Manager' => 'MRM', |
|
265 | - 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
266 | - 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
267 | - ) |
|
268 | - ); |
|
269 | - $this->load_core('Base', array(), true); |
|
270 | - // add our request and response objects to the cache |
|
271 | - $request_loader = $this->_dependency_map->class_loader( |
|
272 | - 'EventEspresso\core\services\request\Request' |
|
273 | - ); |
|
274 | - $this->_set_cached_class( |
|
275 | - $request_loader(), |
|
276 | - 'EventEspresso\core\services\request\Request' |
|
277 | - ); |
|
278 | - $response_loader = $this->_dependency_map->class_loader( |
|
279 | - 'EventEspresso\core\services\request\Response' |
|
280 | - ); |
|
281 | - $this->_set_cached_class( |
|
282 | - $response_loader(), |
|
283 | - 'EventEspresso\core\services\request\Response' |
|
284 | - ); |
|
285 | - add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
286 | - } |
|
287 | - |
|
288 | - |
|
289 | - /** |
|
290 | - * @return void |
|
291 | - */ |
|
292 | - public function init() |
|
293 | - { |
|
294 | - // Get current page protocol |
|
295 | - $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
296 | - // Output admin-ajax.php URL with same protocol as current page |
|
297 | - self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
298 | - self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
299 | - } |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * localize_i18n_js_strings |
|
304 | - * |
|
305 | - * @return string |
|
306 | - */ |
|
307 | - public static function localize_i18n_js_strings() |
|
308 | - { |
|
309 | - $i18n_js_strings = (array) self::$i18n_js_strings; |
|
310 | - foreach ($i18n_js_strings as $key => $value) { |
|
311 | - if (is_scalar($value)) { |
|
312 | - $i18n_js_strings[ $key ] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
313 | - } |
|
314 | - } |
|
315 | - return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * @param mixed string | EED_Module $module |
|
321 | - * @throws OutOfBoundsException |
|
322 | - * @throws InvalidArgumentException |
|
323 | - * @throws InvalidInterfaceException |
|
324 | - * @throws InvalidDataTypeException |
|
325 | - * @throws EE_Error |
|
326 | - * @throws ReflectionException |
|
327 | - */ |
|
328 | - public function add_module($module) |
|
329 | - { |
|
330 | - if ($module instanceof EED_Module) { |
|
331 | - $module_class = get_class($module); |
|
332 | - $this->modules->{$module_class} = $module; |
|
333 | - } else { |
|
334 | - if (! class_exists('EE_Module_Request_Router', false)) { |
|
335 | - $this->load_core('Module_Request_Router'); |
|
336 | - } |
|
337 | - EE_Module_Request_Router::module_factory($module); |
|
338 | - } |
|
339 | - } |
|
340 | - |
|
341 | - |
|
342 | - /** |
|
343 | - * @param string $module_name |
|
344 | - * @return mixed EED_Module | NULL |
|
345 | - */ |
|
346 | - public function get_module($module_name = '') |
|
347 | - { |
|
348 | - return isset($this->modules->{$module_name}) |
|
349 | - ? $this->modules->{$module_name} |
|
350 | - : null; |
|
351 | - } |
|
352 | - |
|
353 | - |
|
354 | - /** |
|
355 | - * loads core classes - must be singletons |
|
356 | - * |
|
357 | - * @param string $class_name - simple class name ie: session |
|
358 | - * @param mixed $arguments |
|
359 | - * @param bool $load_only |
|
360 | - * @return mixed |
|
361 | - * @throws InvalidInterfaceException |
|
362 | - * @throws InvalidDataTypeException |
|
363 | - * @throws EE_Error |
|
364 | - * @throws ReflectionException |
|
365 | - * @throws InvalidArgumentException |
|
366 | - */ |
|
367 | - public function load_core($class_name, $arguments = array(), $load_only = false) |
|
368 | - { |
|
369 | - $core_paths = apply_filters( |
|
370 | - 'FHEE__EE_Registry__load_core__core_paths', |
|
371 | - array( |
|
372 | - EE_CORE, |
|
373 | - EE_ADMIN, |
|
374 | - EE_CPTS, |
|
375 | - EE_CORE . 'data_migration_scripts/', |
|
376 | - EE_CORE . 'capabilities/', |
|
377 | - EE_CORE . 'request_stack/', |
|
378 | - EE_CORE . 'middleware/', |
|
379 | - ) |
|
380 | - ); |
|
381 | - // retrieve instantiated class |
|
382 | - return $this->_load( |
|
383 | - $core_paths, |
|
384 | - 'EE_', |
|
385 | - $class_name, |
|
386 | - 'core', |
|
387 | - $arguments, |
|
388 | - false, |
|
389 | - true, |
|
390 | - $load_only |
|
391 | - ); |
|
392 | - } |
|
393 | - |
|
394 | - |
|
395 | - /** |
|
396 | - * loads service classes |
|
397 | - * |
|
398 | - * @param string $class_name - simple class name ie: session |
|
399 | - * @param mixed $arguments |
|
400 | - * @param bool $load_only |
|
401 | - * @return mixed |
|
402 | - * @throws InvalidInterfaceException |
|
403 | - * @throws InvalidDataTypeException |
|
404 | - * @throws EE_Error |
|
405 | - * @throws ReflectionException |
|
406 | - * @throws InvalidArgumentException |
|
407 | - */ |
|
408 | - public function load_service($class_name, $arguments = array(), $load_only = false) |
|
409 | - { |
|
410 | - $service_paths = apply_filters( |
|
411 | - 'FHEE__EE_Registry__load_service__service_paths', |
|
412 | - array( |
|
413 | - EE_CORE . 'services/', |
|
414 | - ) |
|
415 | - ); |
|
416 | - // retrieve instantiated class |
|
417 | - return $this->_load( |
|
418 | - $service_paths, |
|
419 | - 'EE_', |
|
420 | - $class_name, |
|
421 | - 'class', |
|
422 | - $arguments, |
|
423 | - false, |
|
424 | - true, |
|
425 | - $load_only |
|
426 | - ); |
|
427 | - } |
|
428 | - |
|
429 | - |
|
430 | - /** |
|
431 | - * loads data_migration_scripts |
|
432 | - * |
|
433 | - * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
434 | - * @param mixed $arguments |
|
435 | - * @return EE_Data_Migration_Script_Base|mixed |
|
436 | - * @throws InvalidInterfaceException |
|
437 | - * @throws InvalidDataTypeException |
|
438 | - * @throws EE_Error |
|
439 | - * @throws ReflectionException |
|
440 | - * @throws InvalidArgumentException |
|
441 | - */ |
|
442 | - public function load_dms($class_name, $arguments = array()) |
|
443 | - { |
|
444 | - // retrieve instantiated class |
|
445 | - return $this->_load( |
|
446 | - EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
447 | - 'EE_DMS_', |
|
448 | - $class_name, |
|
449 | - 'dms', |
|
450 | - $arguments, |
|
451 | - false, |
|
452 | - false |
|
453 | - ); |
|
454 | - } |
|
455 | - |
|
456 | - |
|
457 | - /** |
|
458 | - * loads object creating classes - must be singletons |
|
459 | - * |
|
460 | - * @param string $class_name - simple class name ie: attendee |
|
461 | - * @param mixed $arguments - an array of arguments to pass to the class |
|
462 | - * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
463 | - * instantiate |
|
464 | - * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
465 | - * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
466 | - * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
467 | - * (default) |
|
468 | - * @return EE_Base_Class | bool |
|
469 | - * @throws InvalidInterfaceException |
|
470 | - * @throws InvalidDataTypeException |
|
471 | - * @throws EE_Error |
|
472 | - * @throws ReflectionException |
|
473 | - * @throws InvalidArgumentException |
|
474 | - */ |
|
475 | - public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
476 | - { |
|
477 | - $paths = apply_filters( |
|
478 | - 'FHEE__EE_Registry__load_class__paths', |
|
479 | - array( |
|
480 | - EE_CORE, |
|
481 | - EE_CLASSES, |
|
482 | - EE_BUSINESS, |
|
483 | - ) |
|
484 | - ); |
|
485 | - // retrieve instantiated class |
|
486 | - return $this->_load( |
|
487 | - $paths, |
|
488 | - 'EE_', |
|
489 | - $class_name, |
|
490 | - 'class', |
|
491 | - $arguments, |
|
492 | - $from_db, |
|
493 | - $cache, |
|
494 | - $load_only |
|
495 | - ); |
|
496 | - } |
|
497 | - |
|
498 | - |
|
499 | - /** |
|
500 | - * loads helper classes - must be singletons |
|
501 | - * |
|
502 | - * @param string $class_name - simple class name ie: price |
|
503 | - * @param mixed $arguments |
|
504 | - * @param bool $load_only |
|
505 | - * @return EEH_Base | bool |
|
506 | - * @throws InvalidInterfaceException |
|
507 | - * @throws InvalidDataTypeException |
|
508 | - * @throws EE_Error |
|
509 | - * @throws ReflectionException |
|
510 | - * @throws InvalidArgumentException |
|
511 | - */ |
|
512 | - public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
513 | - { |
|
514 | - // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
515 | - $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
516 | - // retrieve instantiated class |
|
517 | - return $this->_load( |
|
518 | - $helper_paths, |
|
519 | - 'EEH_', |
|
520 | - $class_name, |
|
521 | - 'helper', |
|
522 | - $arguments, |
|
523 | - false, |
|
524 | - true, |
|
525 | - $load_only |
|
526 | - ); |
|
527 | - } |
|
528 | - |
|
529 | - |
|
530 | - /** |
|
531 | - * loads core classes - must be singletons |
|
532 | - * |
|
533 | - * @param string $class_name - simple class name ie: session |
|
534 | - * @param mixed $arguments |
|
535 | - * @param bool $load_only |
|
536 | - * @param bool $cache whether to cache the object or not. |
|
537 | - * @return mixed |
|
538 | - * @throws InvalidInterfaceException |
|
539 | - * @throws InvalidDataTypeException |
|
540 | - * @throws EE_Error |
|
541 | - * @throws ReflectionException |
|
542 | - * @throws InvalidArgumentException |
|
543 | - */ |
|
544 | - public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
545 | - { |
|
546 | - $paths = array( |
|
547 | - EE_LIBRARIES, |
|
548 | - EE_LIBRARIES . 'messages/', |
|
549 | - EE_LIBRARIES . 'shortcodes/', |
|
550 | - EE_LIBRARIES . 'qtips/', |
|
551 | - EE_LIBRARIES . 'payment_methods/', |
|
552 | - ); |
|
553 | - // retrieve instantiated class |
|
554 | - return $this->_load( |
|
555 | - $paths, |
|
556 | - 'EE_', |
|
557 | - $class_name, |
|
558 | - 'lib', |
|
559 | - $arguments, |
|
560 | - false, |
|
561 | - $cache, |
|
562 | - $load_only |
|
563 | - ); |
|
564 | - } |
|
565 | - |
|
566 | - |
|
567 | - /** |
|
568 | - * loads model classes - must be singletons |
|
569 | - * |
|
570 | - * @param string $class_name - simple class name ie: price |
|
571 | - * @param mixed $arguments |
|
572 | - * @param bool $load_only |
|
573 | - * @return EEM_Base | bool |
|
574 | - * @throws InvalidInterfaceException |
|
575 | - * @throws InvalidDataTypeException |
|
576 | - * @throws EE_Error |
|
577 | - * @throws ReflectionException |
|
578 | - * @throws InvalidArgumentException |
|
579 | - */ |
|
580 | - public function load_model($class_name, $arguments = array(), $load_only = false) |
|
581 | - { |
|
582 | - $paths = apply_filters( |
|
583 | - 'FHEE__EE_Registry__load_model__paths', |
|
584 | - array( |
|
585 | - EE_MODELS, |
|
586 | - EE_CORE, |
|
587 | - ) |
|
588 | - ); |
|
589 | - // retrieve instantiated class |
|
590 | - return $this->_load( |
|
591 | - $paths, |
|
592 | - 'EEM_', |
|
593 | - $class_name, |
|
594 | - 'model', |
|
595 | - $arguments, |
|
596 | - false, |
|
597 | - true, |
|
598 | - $load_only |
|
599 | - ); |
|
600 | - } |
|
601 | - |
|
602 | - |
|
603 | - /** |
|
604 | - * loads model classes - must be singletons |
|
605 | - * |
|
606 | - * @param string $class_name - simple class name ie: price |
|
607 | - * @param mixed $arguments |
|
608 | - * @param bool $load_only |
|
609 | - * @return mixed | bool |
|
610 | - * @throws InvalidInterfaceException |
|
611 | - * @throws InvalidDataTypeException |
|
612 | - * @throws EE_Error |
|
613 | - * @throws ReflectionException |
|
614 | - * @throws InvalidArgumentException |
|
615 | - */ |
|
616 | - public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
617 | - { |
|
618 | - $paths = array( |
|
619 | - EE_MODELS . 'fields/', |
|
620 | - EE_MODELS . 'helpers/', |
|
621 | - EE_MODELS . 'relations/', |
|
622 | - EE_MODELS . 'strategies/', |
|
623 | - ); |
|
624 | - // retrieve instantiated class |
|
625 | - return $this->_load( |
|
626 | - $paths, |
|
627 | - 'EE_', |
|
628 | - $class_name, |
|
629 | - '', |
|
630 | - $arguments, |
|
631 | - false, |
|
632 | - true, |
|
633 | - $load_only |
|
634 | - ); |
|
635 | - } |
|
636 | - |
|
637 | - |
|
638 | - /** |
|
639 | - * Determines if $model_name is the name of an actual EE model. |
|
640 | - * |
|
641 | - * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
642 | - * @return boolean |
|
643 | - */ |
|
644 | - public function is_model_name($model_name) |
|
645 | - { |
|
646 | - return isset($this->models[ $model_name ]); |
|
647 | - } |
|
648 | - |
|
649 | - |
|
650 | - /** |
|
651 | - * generic class loader |
|
652 | - * |
|
653 | - * @param string $path_to_file - directory path to file location, not including filename |
|
654 | - * @param string $file_name - file name ie: my_file.php, including extension |
|
655 | - * @param string $type - file type - core? class? helper? model? |
|
656 | - * @param mixed $arguments |
|
657 | - * @param bool $load_only |
|
658 | - * @return mixed |
|
659 | - * @throws InvalidInterfaceException |
|
660 | - * @throws InvalidDataTypeException |
|
661 | - * @throws EE_Error |
|
662 | - * @throws ReflectionException |
|
663 | - * @throws InvalidArgumentException |
|
664 | - */ |
|
665 | - public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
666 | - { |
|
667 | - // retrieve instantiated class |
|
668 | - return $this->_load( |
|
669 | - $path_to_file, |
|
670 | - '', |
|
671 | - $file_name, |
|
672 | - $type, |
|
673 | - $arguments, |
|
674 | - false, |
|
675 | - true, |
|
676 | - $load_only |
|
677 | - ); |
|
678 | - } |
|
679 | - |
|
680 | - |
|
681 | - /** |
|
682 | - * @param string $path_to_file - directory path to file location, not including filename |
|
683 | - * @param string $class_name - full class name ie: My_Class |
|
684 | - * @param string $type - file type - core? class? helper? model? |
|
685 | - * @param mixed $arguments |
|
686 | - * @param bool $load_only |
|
687 | - * @return bool|EE_Addon|object |
|
688 | - * @throws InvalidInterfaceException |
|
689 | - * @throws InvalidDataTypeException |
|
690 | - * @throws EE_Error |
|
691 | - * @throws ReflectionException |
|
692 | - * @throws InvalidArgumentException |
|
693 | - */ |
|
694 | - public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
695 | - { |
|
696 | - // retrieve instantiated class |
|
697 | - return $this->_load( |
|
698 | - $path_to_file, |
|
699 | - 'addon', |
|
700 | - $class_name, |
|
701 | - $type, |
|
702 | - $arguments, |
|
703 | - false, |
|
704 | - true, |
|
705 | - $load_only |
|
706 | - ); |
|
707 | - } |
|
708 | - |
|
709 | - |
|
710 | - /** |
|
711 | - * instantiates, caches, and automatically resolves dependencies |
|
712 | - * for classes that use a Fully Qualified Class Name. |
|
713 | - * if the class is not capable of being loaded using PSR-4 autoloading, |
|
714 | - * then you need to use one of the existing load_*() methods |
|
715 | - * which can resolve the classname and filepath from the passed arguments |
|
716 | - * |
|
717 | - * @param bool|string $class_name Fully Qualified Class Name |
|
718 | - * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
719 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
720 | - * @param bool $from_db some classes are instantiated from the db |
|
721 | - * and thus call a different method to instantiate |
|
722 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
723 | - * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
724 | - * @return bool|null|mixed null = failure to load or instantiate class object. |
|
725 | - * object = class loaded and instantiated successfully. |
|
726 | - * bool = fail or success when $load_only is true |
|
727 | - * @throws InvalidInterfaceException |
|
728 | - * @throws InvalidDataTypeException |
|
729 | - * @throws EE_Error |
|
730 | - * @throws ReflectionException |
|
731 | - * @throws InvalidArgumentException |
|
732 | - */ |
|
733 | - public function create( |
|
734 | - $class_name = false, |
|
735 | - $arguments = array(), |
|
736 | - $cache = false, |
|
737 | - $from_db = false, |
|
738 | - $load_only = false, |
|
739 | - $addon = false |
|
740 | - ) { |
|
741 | - $class_name = ltrim($class_name, '\\'); |
|
742 | - $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
743 | - $class_exists = $this->loadOrVerifyClassExists($class_name, $arguments); |
|
744 | - // if a non-FQCN was passed, then |
|
745 | - // verifyClassExists() might return an object |
|
746 | - // or it could return null if the class just could not be found anywhere |
|
747 | - if ($class_exists instanceof $class_name || $class_exists === null) { |
|
748 | - // either way, return the results |
|
749 | - return $class_exists; |
|
750 | - } |
|
751 | - $class_name = $class_exists; |
|
752 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
753 | - if ($load_only) { |
|
754 | - return true; |
|
755 | - } |
|
756 | - $addon = $addon ? 'addon' : ''; |
|
757 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
758 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
759 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
760 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
761 | - // return object if it's already cached |
|
762 | - $cached_class = $this->_get_cached_class($class_name, $addon, $arguments); |
|
763 | - if ($cached_class !== null) { |
|
764 | - return $cached_class; |
|
765 | - } |
|
766 | - }// obtain the loader method from the dependency map |
|
767 | - $loader = $this->_dependency_map->class_loader($class_name);// instantiate the requested object |
|
768 | - if ($loader instanceof Closure) { |
|
769 | - $class_obj = $loader($arguments); |
|
770 | - } else { |
|
771 | - if ($loader && method_exists($this, $loader)) { |
|
772 | - $class_obj = $this->{$loader}($class_name, $arguments); |
|
773 | - } else { |
|
774 | - $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
775 | - } |
|
776 | - } |
|
777 | - if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
778 | - // save it for later... kinda like gum { : $ |
|
779 | - $this->_set_cached_class( |
|
780 | - $class_obj, |
|
781 | - $class_name, |
|
782 | - $addon, |
|
783 | - $from_db, |
|
784 | - $arguments |
|
785 | - ); |
|
786 | - } |
|
787 | - $this->_cache_on = true; |
|
788 | - return $class_obj; |
|
789 | - } |
|
790 | - |
|
791 | - |
|
792 | - /** |
|
793 | - * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
|
794 | - * |
|
795 | - * @param string|object $class_name |
|
796 | - * @param array $arguments |
|
797 | - * @param int $attempt |
|
798 | - * @return mixed |
|
799 | - */ |
|
800 | - private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) |
|
801 | - { |
|
802 | - if (is_object($class_name) || class_exists($class_name)) { |
|
803 | - return $class_name; |
|
804 | - } |
|
805 | - switch ($attempt) { |
|
806 | - case 1: |
|
807 | - // if it's a FQCN then maybe the class is registered with a preceding \ |
|
808 | - $class_name = strpos($class_name, '\\') !== false |
|
809 | - ? '\\' . ltrim($class_name, '\\') |
|
810 | - : $class_name; |
|
811 | - break; |
|
812 | - case 2: |
|
813 | - // |
|
814 | - $loader = $this->_dependency_map->class_loader($class_name); |
|
815 | - if ($loader && method_exists($this, $loader)) { |
|
816 | - return $this->{$loader}($class_name, $arguments); |
|
817 | - } |
|
818 | - break; |
|
819 | - case 3: |
|
820 | - default: |
|
821 | - return null; |
|
822 | - } |
|
823 | - $attempt++; |
|
824 | - return $this->loadOrVerifyClassExists($class_name, $arguments, $attempt); |
|
825 | - } |
|
826 | - |
|
827 | - |
|
828 | - /** |
|
829 | - * instantiates, caches, and injects dependencies for classes |
|
830 | - * |
|
831 | - * @param array $file_paths an array of paths to folders to look in |
|
832 | - * @param string $class_prefix EE or EEM or... ??? |
|
833 | - * @param bool|string $class_name $class name |
|
834 | - * @param string $type file type - core? class? helper? model? |
|
835 | - * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
836 | - * @param bool $from_db some classes are instantiated from the db |
|
837 | - * and thus call a different method to instantiate |
|
838 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
839 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
840 | - * @return bool|null|object null = failure to load or instantiate class object. |
|
841 | - * object = class loaded and instantiated successfully. |
|
842 | - * bool = fail or success when $load_only is true |
|
843 | - * @throws EE_Error |
|
844 | - * @throws ReflectionException |
|
845 | - * @throws InvalidInterfaceException |
|
846 | - * @throws InvalidDataTypeException |
|
847 | - * @throws InvalidArgumentException |
|
848 | - */ |
|
849 | - protected function _load( |
|
850 | - $file_paths = array(), |
|
851 | - $class_prefix = 'EE_', |
|
852 | - $class_name = false, |
|
853 | - $type = 'class', |
|
854 | - $arguments = array(), |
|
855 | - $from_db = false, |
|
856 | - $cache = true, |
|
857 | - $load_only = false |
|
858 | - ) { |
|
859 | - $class_name = ltrim($class_name, '\\'); |
|
860 | - // strip php file extension |
|
861 | - $class_name = str_replace('.php', '', trim($class_name)); |
|
862 | - // does the class have a prefix ? |
|
863 | - if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
864 | - // make sure $class_prefix is uppercase |
|
865 | - $class_prefix = strtoupper(trim($class_prefix)); |
|
866 | - // add class prefix ONCE!!! |
|
867 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
868 | - } |
|
869 | - $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
870 | - $class_exists = class_exists($class_name, false); |
|
871 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
872 | - if ($load_only && $class_exists) { |
|
873 | - return true; |
|
874 | - } |
|
875 | - $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
876 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
877 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
878 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
879 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
880 | - // return object if it's already cached |
|
881 | - $cached_class = $this->_get_cached_class($class_name, $class_prefix, $arguments); |
|
882 | - if ($cached_class !== null) { |
|
883 | - return $cached_class; |
|
884 | - } |
|
885 | - } |
|
886 | - // if the class doesn't already exist.. then we need to try and find the file and load it |
|
887 | - if (! $class_exists) { |
|
888 | - // get full path to file |
|
889 | - $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
890 | - // load the file |
|
891 | - $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
892 | - // if we are only loading a file but NOT instantiating an object |
|
893 | - // then return boolean for whether class was loaded or not |
|
894 | - if ($load_only) { |
|
895 | - return $loaded; |
|
896 | - } |
|
897 | - // if an object was expected but loading failed, then return nothing |
|
898 | - if (! $loaded) { |
|
899 | - return null; |
|
900 | - } |
|
901 | - } |
|
902 | - // instantiate the requested object |
|
903 | - $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
904 | - if ($this->_cache_on && $cache) { |
|
905 | - // save it for later... kinda like gum { : $ |
|
906 | - $this->_set_cached_class( |
|
907 | - $class_obj, |
|
908 | - $class_name, |
|
909 | - $class_prefix, |
|
910 | - $from_db, |
|
911 | - $arguments |
|
912 | - ); |
|
913 | - } |
|
914 | - $this->_cache_on = true; |
|
915 | - return $class_obj; |
|
916 | - } |
|
917 | - |
|
918 | - |
|
919 | - /** |
|
920 | - * @param string $class_name |
|
921 | - * @param string $default have to specify something, but not anything that will conflict |
|
922 | - * @return mixed|string |
|
923 | - */ |
|
924 | - protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
925 | - { |
|
926 | - return isset($this->_class_abbreviations[ $class_name ]) |
|
927 | - ? $this->_class_abbreviations[ $class_name ] |
|
928 | - : $default; |
|
929 | - } |
|
930 | - |
|
931 | - |
|
932 | - /** |
|
933 | - * attempts to find a cached version of the requested class |
|
934 | - * by looking in the following places: |
|
935 | - * $this->{$class_abbreviation} ie: $this->CART |
|
936 | - * $this->{$class_name} ie: $this->Some_Class |
|
937 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
938 | - * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
939 | - * |
|
940 | - * @param string $class_name |
|
941 | - * @param string $class_prefix |
|
942 | - * @param array $arguments |
|
943 | - * @return mixed |
|
944 | - */ |
|
945 | - protected function _get_cached_class( |
|
946 | - $class_name, |
|
947 | - $class_prefix = '', |
|
948 | - $arguments = array() |
|
949 | - ) { |
|
950 | - if ($class_name === 'EE_Registry') { |
|
951 | - return $this; |
|
952 | - } |
|
953 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
954 | - // check if class has already been loaded, and return it if it has been |
|
955 | - if (isset($this->{$class_abbreviation})) { |
|
956 | - return $this->{$class_abbreviation}; |
|
957 | - } |
|
958 | - $class_name = str_replace('\\', '_', $class_name); |
|
959 | - if (isset($this->{$class_name})) { |
|
960 | - return $this->{$class_name}; |
|
961 | - } |
|
962 | - if ($class_prefix === 'addon' && isset($this->addons->{$class_name})) { |
|
963 | - return $this->addons->{$class_name}; |
|
964 | - } |
|
965 | - $object_identifier = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
966 | - if (isset($this->LIB->{$object_identifier})) { |
|
967 | - return $this->LIB->{$object_identifier}; |
|
968 | - } |
|
969 | - foreach ($this->LIB as $key => $object) { |
|
970 | - if ( |
|
26 | + /** |
|
27 | + * @var EE_Registry $_instance |
|
28 | + */ |
|
29 | + private static $_instance; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var EE_Dependency_Map $_dependency_map |
|
33 | + */ |
|
34 | + protected $_dependency_map; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var Mirror |
|
38 | + */ |
|
39 | + private $mirror; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var ClassInterfaceCache $class_cache |
|
43 | + */ |
|
44 | + private $class_cache; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var array $_class_abbreviations |
|
48 | + */ |
|
49 | + protected $_class_abbreviations = array(); |
|
50 | + |
|
51 | + /** |
|
52 | + * @var CommandBusInterface $BUS |
|
53 | + */ |
|
54 | + public $BUS; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var EE_Cart $CART |
|
58 | + */ |
|
59 | + public $CART; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var EE_Config $CFG |
|
63 | + */ |
|
64 | + public $CFG; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var EE_Network_Config $NET_CFG |
|
68 | + */ |
|
69 | + public $NET_CFG; |
|
70 | + |
|
71 | + /** |
|
72 | + * StdClass object for storing library classes in |
|
73 | + * |
|
74 | + * @var RegistryContainer $LIB |
|
75 | + */ |
|
76 | + public $LIB; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var EE_Request_Handler $REQ |
|
80 | + */ |
|
81 | + public $REQ; |
|
82 | + |
|
83 | + /** |
|
84 | + * @var EE_Session $SSN |
|
85 | + */ |
|
86 | + public $SSN; |
|
87 | + |
|
88 | + /** |
|
89 | + * @since 4.5.0 |
|
90 | + * @var EE_Capabilities $CAP |
|
91 | + */ |
|
92 | + public $CAP; |
|
93 | + |
|
94 | + /** |
|
95 | + * @since 4.9.0 |
|
96 | + * @var EE_Message_Resource_Manager $MRM |
|
97 | + */ |
|
98 | + public $MRM; |
|
99 | + |
|
100 | + /** |
|
101 | + * @var Registry $AssetsRegistry |
|
102 | + */ |
|
103 | + public $AssetsRegistry; |
|
104 | + |
|
105 | + /** |
|
106 | + * StdClass object for holding addons which have registered themselves to work with EE core |
|
107 | + * |
|
108 | + * @var EE_Addon[] $addons |
|
109 | + */ |
|
110 | + public $addons; |
|
111 | + |
|
112 | + /** |
|
113 | + * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
114 | + * |
|
115 | + * @var EEM_Base[] $models |
|
116 | + */ |
|
117 | + public $models = array(); |
|
118 | + |
|
119 | + /** |
|
120 | + * @var EED_Module[] $modules |
|
121 | + */ |
|
122 | + public $modules; |
|
123 | + |
|
124 | + /** |
|
125 | + * @var EES_Shortcode[] $shortcodes |
|
126 | + */ |
|
127 | + public $shortcodes; |
|
128 | + |
|
129 | + /** |
|
130 | + * @var WP_Widget[] $widgets |
|
131 | + */ |
|
132 | + public $widgets; |
|
133 | + |
|
134 | + /** |
|
135 | + * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
136 | + * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
137 | + * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
138 | + * classnames (eg "EEM_Event") |
|
139 | + * |
|
140 | + * @var array $non_abstract_db_models |
|
141 | + */ |
|
142 | + public $non_abstract_db_models = array(); |
|
143 | + |
|
144 | + /** |
|
145 | + * internationalization for JS strings |
|
146 | + * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
147 | + * in js file: var translatedString = eei18n.string_key; |
|
148 | + * |
|
149 | + * @var array $i18n_js_strings |
|
150 | + */ |
|
151 | + public static $i18n_js_strings = array(); |
|
152 | + |
|
153 | + /** |
|
154 | + * $main_file - path to espresso.php |
|
155 | + * |
|
156 | + * @var array $main_file |
|
157 | + */ |
|
158 | + public $main_file; |
|
159 | + |
|
160 | + /** |
|
161 | + * array of ReflectionClass objects where the key is the class name |
|
162 | + * |
|
163 | + * @deprecated 4.9.62.p |
|
164 | + * @var ReflectionClass[] $_reflectors |
|
165 | + */ |
|
166 | + public $_reflectors; |
|
167 | + |
|
168 | + /** |
|
169 | + * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
170 | + * |
|
171 | + * @var boolean $_cache_on |
|
172 | + */ |
|
173 | + protected $_cache_on = true; |
|
174 | + |
|
175 | + /** |
|
176 | + * @var ObjectIdentifier |
|
177 | + */ |
|
178 | + private $object_identifier; |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * @singleton method used to instantiate class object |
|
183 | + * @param EE_Dependency_Map|null $dependency_map |
|
184 | + * @param Mirror|null $mirror |
|
185 | + * @param ClassInterfaceCache|null $class_cache |
|
186 | + * @param ObjectIdentifier|null $object_identifier |
|
187 | + * @return EE_Registry instance |
|
188 | + */ |
|
189 | + public static function instance( |
|
190 | + EE_Dependency_Map $dependency_map = null, |
|
191 | + Mirror $mirror = null, |
|
192 | + ClassInterfaceCache $class_cache = null, |
|
193 | + ObjectIdentifier $object_identifier = null |
|
194 | + ) { |
|
195 | + // check if class object is instantiated |
|
196 | + if ( |
|
197 | + ! self::$_instance instanceof EE_Registry |
|
198 | + && $dependency_map instanceof EE_Dependency_Map |
|
199 | + && $mirror instanceof Mirror |
|
200 | + && $class_cache instanceof ClassInterfaceCache |
|
201 | + && $object_identifier instanceof ObjectIdentifier |
|
202 | + ) { |
|
203 | + self::$_instance = new self( |
|
204 | + $dependency_map, |
|
205 | + $mirror, |
|
206 | + $class_cache, |
|
207 | + $object_identifier |
|
208 | + ); |
|
209 | + } |
|
210 | + return self::$_instance; |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * protected constructor to prevent direct creation |
|
216 | + * |
|
217 | + * @Constructor |
|
218 | + * @param EE_Dependency_Map $dependency_map |
|
219 | + * @param Mirror $mirror |
|
220 | + * @param ClassInterfaceCache $class_cache |
|
221 | + * @param ObjectIdentifier $object_identifier |
|
222 | + */ |
|
223 | + protected function __construct( |
|
224 | + EE_Dependency_Map $dependency_map, |
|
225 | + Mirror $mirror, |
|
226 | + ClassInterfaceCache $class_cache, |
|
227 | + ObjectIdentifier $object_identifier |
|
228 | + ) { |
|
229 | + $this->_dependency_map = $dependency_map; |
|
230 | + $this->mirror = $mirror; |
|
231 | + $this->class_cache = $class_cache; |
|
232 | + $this->object_identifier = $object_identifier; |
|
233 | + // $registry_container = new RegistryContainer(); |
|
234 | + $this->LIB = new RegistryContainer(); |
|
235 | + $this->addons = new RegistryContainer(); |
|
236 | + $this->modules = new RegistryContainer(); |
|
237 | + $this->shortcodes = new RegistryContainer(); |
|
238 | + $this->widgets = new RegistryContainer(); |
|
239 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
240 | + } |
|
241 | + |
|
242 | + |
|
243 | + /** |
|
244 | + * initialize |
|
245 | + * |
|
246 | + * @throws OutOfBoundsException |
|
247 | + * @throws InvalidArgumentException |
|
248 | + * @throws InvalidInterfaceException |
|
249 | + * @throws InvalidDataTypeException |
|
250 | + * @throws EE_Error |
|
251 | + * @throws ReflectionException |
|
252 | + */ |
|
253 | + public function initialize() |
|
254 | + { |
|
255 | + $this->_class_abbreviations = apply_filters( |
|
256 | + 'FHEE__EE_Registry____construct___class_abbreviations', |
|
257 | + array( |
|
258 | + 'EE_Config' => 'CFG', |
|
259 | + 'EE_Session' => 'SSN', |
|
260 | + 'EE_Capabilities' => 'CAP', |
|
261 | + 'EE_Cart' => 'CART', |
|
262 | + 'EE_Network_Config' => 'NET_CFG', |
|
263 | + 'EE_Request_Handler' => 'REQ', |
|
264 | + 'EE_Message_Resource_Manager' => 'MRM', |
|
265 | + 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
266 | + 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
267 | + ) |
|
268 | + ); |
|
269 | + $this->load_core('Base', array(), true); |
|
270 | + // add our request and response objects to the cache |
|
271 | + $request_loader = $this->_dependency_map->class_loader( |
|
272 | + 'EventEspresso\core\services\request\Request' |
|
273 | + ); |
|
274 | + $this->_set_cached_class( |
|
275 | + $request_loader(), |
|
276 | + 'EventEspresso\core\services\request\Request' |
|
277 | + ); |
|
278 | + $response_loader = $this->_dependency_map->class_loader( |
|
279 | + 'EventEspresso\core\services\request\Response' |
|
280 | + ); |
|
281 | + $this->_set_cached_class( |
|
282 | + $response_loader(), |
|
283 | + 'EventEspresso\core\services\request\Response' |
|
284 | + ); |
|
285 | + add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
286 | + } |
|
287 | + |
|
288 | + |
|
289 | + /** |
|
290 | + * @return void |
|
291 | + */ |
|
292 | + public function init() |
|
293 | + { |
|
294 | + // Get current page protocol |
|
295 | + $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
296 | + // Output admin-ajax.php URL with same protocol as current page |
|
297 | + self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
298 | + self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
299 | + } |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * localize_i18n_js_strings |
|
304 | + * |
|
305 | + * @return string |
|
306 | + */ |
|
307 | + public static function localize_i18n_js_strings() |
|
308 | + { |
|
309 | + $i18n_js_strings = (array) self::$i18n_js_strings; |
|
310 | + foreach ($i18n_js_strings as $key => $value) { |
|
311 | + if (is_scalar($value)) { |
|
312 | + $i18n_js_strings[ $key ] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
313 | + } |
|
314 | + } |
|
315 | + return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * @param mixed string | EED_Module $module |
|
321 | + * @throws OutOfBoundsException |
|
322 | + * @throws InvalidArgumentException |
|
323 | + * @throws InvalidInterfaceException |
|
324 | + * @throws InvalidDataTypeException |
|
325 | + * @throws EE_Error |
|
326 | + * @throws ReflectionException |
|
327 | + */ |
|
328 | + public function add_module($module) |
|
329 | + { |
|
330 | + if ($module instanceof EED_Module) { |
|
331 | + $module_class = get_class($module); |
|
332 | + $this->modules->{$module_class} = $module; |
|
333 | + } else { |
|
334 | + if (! class_exists('EE_Module_Request_Router', false)) { |
|
335 | + $this->load_core('Module_Request_Router'); |
|
336 | + } |
|
337 | + EE_Module_Request_Router::module_factory($module); |
|
338 | + } |
|
339 | + } |
|
340 | + |
|
341 | + |
|
342 | + /** |
|
343 | + * @param string $module_name |
|
344 | + * @return mixed EED_Module | NULL |
|
345 | + */ |
|
346 | + public function get_module($module_name = '') |
|
347 | + { |
|
348 | + return isset($this->modules->{$module_name}) |
|
349 | + ? $this->modules->{$module_name} |
|
350 | + : null; |
|
351 | + } |
|
352 | + |
|
353 | + |
|
354 | + /** |
|
355 | + * loads core classes - must be singletons |
|
356 | + * |
|
357 | + * @param string $class_name - simple class name ie: session |
|
358 | + * @param mixed $arguments |
|
359 | + * @param bool $load_only |
|
360 | + * @return mixed |
|
361 | + * @throws InvalidInterfaceException |
|
362 | + * @throws InvalidDataTypeException |
|
363 | + * @throws EE_Error |
|
364 | + * @throws ReflectionException |
|
365 | + * @throws InvalidArgumentException |
|
366 | + */ |
|
367 | + public function load_core($class_name, $arguments = array(), $load_only = false) |
|
368 | + { |
|
369 | + $core_paths = apply_filters( |
|
370 | + 'FHEE__EE_Registry__load_core__core_paths', |
|
371 | + array( |
|
372 | + EE_CORE, |
|
373 | + EE_ADMIN, |
|
374 | + EE_CPTS, |
|
375 | + EE_CORE . 'data_migration_scripts/', |
|
376 | + EE_CORE . 'capabilities/', |
|
377 | + EE_CORE . 'request_stack/', |
|
378 | + EE_CORE . 'middleware/', |
|
379 | + ) |
|
380 | + ); |
|
381 | + // retrieve instantiated class |
|
382 | + return $this->_load( |
|
383 | + $core_paths, |
|
384 | + 'EE_', |
|
385 | + $class_name, |
|
386 | + 'core', |
|
387 | + $arguments, |
|
388 | + false, |
|
389 | + true, |
|
390 | + $load_only |
|
391 | + ); |
|
392 | + } |
|
393 | + |
|
394 | + |
|
395 | + /** |
|
396 | + * loads service classes |
|
397 | + * |
|
398 | + * @param string $class_name - simple class name ie: session |
|
399 | + * @param mixed $arguments |
|
400 | + * @param bool $load_only |
|
401 | + * @return mixed |
|
402 | + * @throws InvalidInterfaceException |
|
403 | + * @throws InvalidDataTypeException |
|
404 | + * @throws EE_Error |
|
405 | + * @throws ReflectionException |
|
406 | + * @throws InvalidArgumentException |
|
407 | + */ |
|
408 | + public function load_service($class_name, $arguments = array(), $load_only = false) |
|
409 | + { |
|
410 | + $service_paths = apply_filters( |
|
411 | + 'FHEE__EE_Registry__load_service__service_paths', |
|
412 | + array( |
|
413 | + EE_CORE . 'services/', |
|
414 | + ) |
|
415 | + ); |
|
416 | + // retrieve instantiated class |
|
417 | + return $this->_load( |
|
418 | + $service_paths, |
|
419 | + 'EE_', |
|
420 | + $class_name, |
|
421 | + 'class', |
|
422 | + $arguments, |
|
423 | + false, |
|
424 | + true, |
|
425 | + $load_only |
|
426 | + ); |
|
427 | + } |
|
428 | + |
|
429 | + |
|
430 | + /** |
|
431 | + * loads data_migration_scripts |
|
432 | + * |
|
433 | + * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
434 | + * @param mixed $arguments |
|
435 | + * @return EE_Data_Migration_Script_Base|mixed |
|
436 | + * @throws InvalidInterfaceException |
|
437 | + * @throws InvalidDataTypeException |
|
438 | + * @throws EE_Error |
|
439 | + * @throws ReflectionException |
|
440 | + * @throws InvalidArgumentException |
|
441 | + */ |
|
442 | + public function load_dms($class_name, $arguments = array()) |
|
443 | + { |
|
444 | + // retrieve instantiated class |
|
445 | + return $this->_load( |
|
446 | + EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
447 | + 'EE_DMS_', |
|
448 | + $class_name, |
|
449 | + 'dms', |
|
450 | + $arguments, |
|
451 | + false, |
|
452 | + false |
|
453 | + ); |
|
454 | + } |
|
455 | + |
|
456 | + |
|
457 | + /** |
|
458 | + * loads object creating classes - must be singletons |
|
459 | + * |
|
460 | + * @param string $class_name - simple class name ie: attendee |
|
461 | + * @param mixed $arguments - an array of arguments to pass to the class |
|
462 | + * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
463 | + * instantiate |
|
464 | + * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
465 | + * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
466 | + * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
467 | + * (default) |
|
468 | + * @return EE_Base_Class | bool |
|
469 | + * @throws InvalidInterfaceException |
|
470 | + * @throws InvalidDataTypeException |
|
471 | + * @throws EE_Error |
|
472 | + * @throws ReflectionException |
|
473 | + * @throws InvalidArgumentException |
|
474 | + */ |
|
475 | + public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
476 | + { |
|
477 | + $paths = apply_filters( |
|
478 | + 'FHEE__EE_Registry__load_class__paths', |
|
479 | + array( |
|
480 | + EE_CORE, |
|
481 | + EE_CLASSES, |
|
482 | + EE_BUSINESS, |
|
483 | + ) |
|
484 | + ); |
|
485 | + // retrieve instantiated class |
|
486 | + return $this->_load( |
|
487 | + $paths, |
|
488 | + 'EE_', |
|
489 | + $class_name, |
|
490 | + 'class', |
|
491 | + $arguments, |
|
492 | + $from_db, |
|
493 | + $cache, |
|
494 | + $load_only |
|
495 | + ); |
|
496 | + } |
|
497 | + |
|
498 | + |
|
499 | + /** |
|
500 | + * loads helper classes - must be singletons |
|
501 | + * |
|
502 | + * @param string $class_name - simple class name ie: price |
|
503 | + * @param mixed $arguments |
|
504 | + * @param bool $load_only |
|
505 | + * @return EEH_Base | bool |
|
506 | + * @throws InvalidInterfaceException |
|
507 | + * @throws InvalidDataTypeException |
|
508 | + * @throws EE_Error |
|
509 | + * @throws ReflectionException |
|
510 | + * @throws InvalidArgumentException |
|
511 | + */ |
|
512 | + public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
513 | + { |
|
514 | + // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
515 | + $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
516 | + // retrieve instantiated class |
|
517 | + return $this->_load( |
|
518 | + $helper_paths, |
|
519 | + 'EEH_', |
|
520 | + $class_name, |
|
521 | + 'helper', |
|
522 | + $arguments, |
|
523 | + false, |
|
524 | + true, |
|
525 | + $load_only |
|
526 | + ); |
|
527 | + } |
|
528 | + |
|
529 | + |
|
530 | + /** |
|
531 | + * loads core classes - must be singletons |
|
532 | + * |
|
533 | + * @param string $class_name - simple class name ie: session |
|
534 | + * @param mixed $arguments |
|
535 | + * @param bool $load_only |
|
536 | + * @param bool $cache whether to cache the object or not. |
|
537 | + * @return mixed |
|
538 | + * @throws InvalidInterfaceException |
|
539 | + * @throws InvalidDataTypeException |
|
540 | + * @throws EE_Error |
|
541 | + * @throws ReflectionException |
|
542 | + * @throws InvalidArgumentException |
|
543 | + */ |
|
544 | + public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
545 | + { |
|
546 | + $paths = array( |
|
547 | + EE_LIBRARIES, |
|
548 | + EE_LIBRARIES . 'messages/', |
|
549 | + EE_LIBRARIES . 'shortcodes/', |
|
550 | + EE_LIBRARIES . 'qtips/', |
|
551 | + EE_LIBRARIES . 'payment_methods/', |
|
552 | + ); |
|
553 | + // retrieve instantiated class |
|
554 | + return $this->_load( |
|
555 | + $paths, |
|
556 | + 'EE_', |
|
557 | + $class_name, |
|
558 | + 'lib', |
|
559 | + $arguments, |
|
560 | + false, |
|
561 | + $cache, |
|
562 | + $load_only |
|
563 | + ); |
|
564 | + } |
|
565 | + |
|
566 | + |
|
567 | + /** |
|
568 | + * loads model classes - must be singletons |
|
569 | + * |
|
570 | + * @param string $class_name - simple class name ie: price |
|
571 | + * @param mixed $arguments |
|
572 | + * @param bool $load_only |
|
573 | + * @return EEM_Base | bool |
|
574 | + * @throws InvalidInterfaceException |
|
575 | + * @throws InvalidDataTypeException |
|
576 | + * @throws EE_Error |
|
577 | + * @throws ReflectionException |
|
578 | + * @throws InvalidArgumentException |
|
579 | + */ |
|
580 | + public function load_model($class_name, $arguments = array(), $load_only = false) |
|
581 | + { |
|
582 | + $paths = apply_filters( |
|
583 | + 'FHEE__EE_Registry__load_model__paths', |
|
584 | + array( |
|
585 | + EE_MODELS, |
|
586 | + EE_CORE, |
|
587 | + ) |
|
588 | + ); |
|
589 | + // retrieve instantiated class |
|
590 | + return $this->_load( |
|
591 | + $paths, |
|
592 | + 'EEM_', |
|
593 | + $class_name, |
|
594 | + 'model', |
|
595 | + $arguments, |
|
596 | + false, |
|
597 | + true, |
|
598 | + $load_only |
|
599 | + ); |
|
600 | + } |
|
601 | + |
|
602 | + |
|
603 | + /** |
|
604 | + * loads model classes - must be singletons |
|
605 | + * |
|
606 | + * @param string $class_name - simple class name ie: price |
|
607 | + * @param mixed $arguments |
|
608 | + * @param bool $load_only |
|
609 | + * @return mixed | bool |
|
610 | + * @throws InvalidInterfaceException |
|
611 | + * @throws InvalidDataTypeException |
|
612 | + * @throws EE_Error |
|
613 | + * @throws ReflectionException |
|
614 | + * @throws InvalidArgumentException |
|
615 | + */ |
|
616 | + public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
617 | + { |
|
618 | + $paths = array( |
|
619 | + EE_MODELS . 'fields/', |
|
620 | + EE_MODELS . 'helpers/', |
|
621 | + EE_MODELS . 'relations/', |
|
622 | + EE_MODELS . 'strategies/', |
|
623 | + ); |
|
624 | + // retrieve instantiated class |
|
625 | + return $this->_load( |
|
626 | + $paths, |
|
627 | + 'EE_', |
|
628 | + $class_name, |
|
629 | + '', |
|
630 | + $arguments, |
|
631 | + false, |
|
632 | + true, |
|
633 | + $load_only |
|
634 | + ); |
|
635 | + } |
|
636 | + |
|
637 | + |
|
638 | + /** |
|
639 | + * Determines if $model_name is the name of an actual EE model. |
|
640 | + * |
|
641 | + * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
642 | + * @return boolean |
|
643 | + */ |
|
644 | + public function is_model_name($model_name) |
|
645 | + { |
|
646 | + return isset($this->models[ $model_name ]); |
|
647 | + } |
|
648 | + |
|
649 | + |
|
650 | + /** |
|
651 | + * generic class loader |
|
652 | + * |
|
653 | + * @param string $path_to_file - directory path to file location, not including filename |
|
654 | + * @param string $file_name - file name ie: my_file.php, including extension |
|
655 | + * @param string $type - file type - core? class? helper? model? |
|
656 | + * @param mixed $arguments |
|
657 | + * @param bool $load_only |
|
658 | + * @return mixed |
|
659 | + * @throws InvalidInterfaceException |
|
660 | + * @throws InvalidDataTypeException |
|
661 | + * @throws EE_Error |
|
662 | + * @throws ReflectionException |
|
663 | + * @throws InvalidArgumentException |
|
664 | + */ |
|
665 | + public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
666 | + { |
|
667 | + // retrieve instantiated class |
|
668 | + return $this->_load( |
|
669 | + $path_to_file, |
|
670 | + '', |
|
671 | + $file_name, |
|
672 | + $type, |
|
673 | + $arguments, |
|
674 | + false, |
|
675 | + true, |
|
676 | + $load_only |
|
677 | + ); |
|
678 | + } |
|
679 | + |
|
680 | + |
|
681 | + /** |
|
682 | + * @param string $path_to_file - directory path to file location, not including filename |
|
683 | + * @param string $class_name - full class name ie: My_Class |
|
684 | + * @param string $type - file type - core? class? helper? model? |
|
685 | + * @param mixed $arguments |
|
686 | + * @param bool $load_only |
|
687 | + * @return bool|EE_Addon|object |
|
688 | + * @throws InvalidInterfaceException |
|
689 | + * @throws InvalidDataTypeException |
|
690 | + * @throws EE_Error |
|
691 | + * @throws ReflectionException |
|
692 | + * @throws InvalidArgumentException |
|
693 | + */ |
|
694 | + public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
695 | + { |
|
696 | + // retrieve instantiated class |
|
697 | + return $this->_load( |
|
698 | + $path_to_file, |
|
699 | + 'addon', |
|
700 | + $class_name, |
|
701 | + $type, |
|
702 | + $arguments, |
|
703 | + false, |
|
704 | + true, |
|
705 | + $load_only |
|
706 | + ); |
|
707 | + } |
|
708 | + |
|
709 | + |
|
710 | + /** |
|
711 | + * instantiates, caches, and automatically resolves dependencies |
|
712 | + * for classes that use a Fully Qualified Class Name. |
|
713 | + * if the class is not capable of being loaded using PSR-4 autoloading, |
|
714 | + * then you need to use one of the existing load_*() methods |
|
715 | + * which can resolve the classname and filepath from the passed arguments |
|
716 | + * |
|
717 | + * @param bool|string $class_name Fully Qualified Class Name |
|
718 | + * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
719 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
720 | + * @param bool $from_db some classes are instantiated from the db |
|
721 | + * and thus call a different method to instantiate |
|
722 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
723 | + * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
724 | + * @return bool|null|mixed null = failure to load or instantiate class object. |
|
725 | + * object = class loaded and instantiated successfully. |
|
726 | + * bool = fail or success when $load_only is true |
|
727 | + * @throws InvalidInterfaceException |
|
728 | + * @throws InvalidDataTypeException |
|
729 | + * @throws EE_Error |
|
730 | + * @throws ReflectionException |
|
731 | + * @throws InvalidArgumentException |
|
732 | + */ |
|
733 | + public function create( |
|
734 | + $class_name = false, |
|
735 | + $arguments = array(), |
|
736 | + $cache = false, |
|
737 | + $from_db = false, |
|
738 | + $load_only = false, |
|
739 | + $addon = false |
|
740 | + ) { |
|
741 | + $class_name = ltrim($class_name, '\\'); |
|
742 | + $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
743 | + $class_exists = $this->loadOrVerifyClassExists($class_name, $arguments); |
|
744 | + // if a non-FQCN was passed, then |
|
745 | + // verifyClassExists() might return an object |
|
746 | + // or it could return null if the class just could not be found anywhere |
|
747 | + if ($class_exists instanceof $class_name || $class_exists === null) { |
|
748 | + // either way, return the results |
|
749 | + return $class_exists; |
|
750 | + } |
|
751 | + $class_name = $class_exists; |
|
752 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
753 | + if ($load_only) { |
|
754 | + return true; |
|
755 | + } |
|
756 | + $addon = $addon ? 'addon' : ''; |
|
757 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
758 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
759 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
760 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
761 | + // return object if it's already cached |
|
762 | + $cached_class = $this->_get_cached_class($class_name, $addon, $arguments); |
|
763 | + if ($cached_class !== null) { |
|
764 | + return $cached_class; |
|
765 | + } |
|
766 | + }// obtain the loader method from the dependency map |
|
767 | + $loader = $this->_dependency_map->class_loader($class_name);// instantiate the requested object |
|
768 | + if ($loader instanceof Closure) { |
|
769 | + $class_obj = $loader($arguments); |
|
770 | + } else { |
|
771 | + if ($loader && method_exists($this, $loader)) { |
|
772 | + $class_obj = $this->{$loader}($class_name, $arguments); |
|
773 | + } else { |
|
774 | + $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
775 | + } |
|
776 | + } |
|
777 | + if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
778 | + // save it for later... kinda like gum { : $ |
|
779 | + $this->_set_cached_class( |
|
780 | + $class_obj, |
|
781 | + $class_name, |
|
782 | + $addon, |
|
783 | + $from_db, |
|
784 | + $arguments |
|
785 | + ); |
|
786 | + } |
|
787 | + $this->_cache_on = true; |
|
788 | + return $class_obj; |
|
789 | + } |
|
790 | + |
|
791 | + |
|
792 | + /** |
|
793 | + * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
|
794 | + * |
|
795 | + * @param string|object $class_name |
|
796 | + * @param array $arguments |
|
797 | + * @param int $attempt |
|
798 | + * @return mixed |
|
799 | + */ |
|
800 | + private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) |
|
801 | + { |
|
802 | + if (is_object($class_name) || class_exists($class_name)) { |
|
803 | + return $class_name; |
|
804 | + } |
|
805 | + switch ($attempt) { |
|
806 | + case 1: |
|
807 | + // if it's a FQCN then maybe the class is registered with a preceding \ |
|
808 | + $class_name = strpos($class_name, '\\') !== false |
|
809 | + ? '\\' . ltrim($class_name, '\\') |
|
810 | + : $class_name; |
|
811 | + break; |
|
812 | + case 2: |
|
813 | + // |
|
814 | + $loader = $this->_dependency_map->class_loader($class_name); |
|
815 | + if ($loader && method_exists($this, $loader)) { |
|
816 | + return $this->{$loader}($class_name, $arguments); |
|
817 | + } |
|
818 | + break; |
|
819 | + case 3: |
|
820 | + default: |
|
821 | + return null; |
|
822 | + } |
|
823 | + $attempt++; |
|
824 | + return $this->loadOrVerifyClassExists($class_name, $arguments, $attempt); |
|
825 | + } |
|
826 | + |
|
827 | + |
|
828 | + /** |
|
829 | + * instantiates, caches, and injects dependencies for classes |
|
830 | + * |
|
831 | + * @param array $file_paths an array of paths to folders to look in |
|
832 | + * @param string $class_prefix EE or EEM or... ??? |
|
833 | + * @param bool|string $class_name $class name |
|
834 | + * @param string $type file type - core? class? helper? model? |
|
835 | + * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
836 | + * @param bool $from_db some classes are instantiated from the db |
|
837 | + * and thus call a different method to instantiate |
|
838 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
839 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
840 | + * @return bool|null|object null = failure to load or instantiate class object. |
|
841 | + * object = class loaded and instantiated successfully. |
|
842 | + * bool = fail or success when $load_only is true |
|
843 | + * @throws EE_Error |
|
844 | + * @throws ReflectionException |
|
845 | + * @throws InvalidInterfaceException |
|
846 | + * @throws InvalidDataTypeException |
|
847 | + * @throws InvalidArgumentException |
|
848 | + */ |
|
849 | + protected function _load( |
|
850 | + $file_paths = array(), |
|
851 | + $class_prefix = 'EE_', |
|
852 | + $class_name = false, |
|
853 | + $type = 'class', |
|
854 | + $arguments = array(), |
|
855 | + $from_db = false, |
|
856 | + $cache = true, |
|
857 | + $load_only = false |
|
858 | + ) { |
|
859 | + $class_name = ltrim($class_name, '\\'); |
|
860 | + // strip php file extension |
|
861 | + $class_name = str_replace('.php', '', trim($class_name)); |
|
862 | + // does the class have a prefix ? |
|
863 | + if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
864 | + // make sure $class_prefix is uppercase |
|
865 | + $class_prefix = strtoupper(trim($class_prefix)); |
|
866 | + // add class prefix ONCE!!! |
|
867 | + $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
868 | + } |
|
869 | + $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
870 | + $class_exists = class_exists($class_name, false); |
|
871 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
872 | + if ($load_only && $class_exists) { |
|
873 | + return true; |
|
874 | + } |
|
875 | + $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
876 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
877 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
878 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
879 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
880 | + // return object if it's already cached |
|
881 | + $cached_class = $this->_get_cached_class($class_name, $class_prefix, $arguments); |
|
882 | + if ($cached_class !== null) { |
|
883 | + return $cached_class; |
|
884 | + } |
|
885 | + } |
|
886 | + // if the class doesn't already exist.. then we need to try and find the file and load it |
|
887 | + if (! $class_exists) { |
|
888 | + // get full path to file |
|
889 | + $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
890 | + // load the file |
|
891 | + $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
892 | + // if we are only loading a file but NOT instantiating an object |
|
893 | + // then return boolean for whether class was loaded or not |
|
894 | + if ($load_only) { |
|
895 | + return $loaded; |
|
896 | + } |
|
897 | + // if an object was expected but loading failed, then return nothing |
|
898 | + if (! $loaded) { |
|
899 | + return null; |
|
900 | + } |
|
901 | + } |
|
902 | + // instantiate the requested object |
|
903 | + $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
904 | + if ($this->_cache_on && $cache) { |
|
905 | + // save it for later... kinda like gum { : $ |
|
906 | + $this->_set_cached_class( |
|
907 | + $class_obj, |
|
908 | + $class_name, |
|
909 | + $class_prefix, |
|
910 | + $from_db, |
|
911 | + $arguments |
|
912 | + ); |
|
913 | + } |
|
914 | + $this->_cache_on = true; |
|
915 | + return $class_obj; |
|
916 | + } |
|
917 | + |
|
918 | + |
|
919 | + /** |
|
920 | + * @param string $class_name |
|
921 | + * @param string $default have to specify something, but not anything that will conflict |
|
922 | + * @return mixed|string |
|
923 | + */ |
|
924 | + protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
925 | + { |
|
926 | + return isset($this->_class_abbreviations[ $class_name ]) |
|
927 | + ? $this->_class_abbreviations[ $class_name ] |
|
928 | + : $default; |
|
929 | + } |
|
930 | + |
|
931 | + |
|
932 | + /** |
|
933 | + * attempts to find a cached version of the requested class |
|
934 | + * by looking in the following places: |
|
935 | + * $this->{$class_abbreviation} ie: $this->CART |
|
936 | + * $this->{$class_name} ie: $this->Some_Class |
|
937 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
938 | + * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
939 | + * |
|
940 | + * @param string $class_name |
|
941 | + * @param string $class_prefix |
|
942 | + * @param array $arguments |
|
943 | + * @return mixed |
|
944 | + */ |
|
945 | + protected function _get_cached_class( |
|
946 | + $class_name, |
|
947 | + $class_prefix = '', |
|
948 | + $arguments = array() |
|
949 | + ) { |
|
950 | + if ($class_name === 'EE_Registry') { |
|
951 | + return $this; |
|
952 | + } |
|
953 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
954 | + // check if class has already been loaded, and return it if it has been |
|
955 | + if (isset($this->{$class_abbreviation})) { |
|
956 | + return $this->{$class_abbreviation}; |
|
957 | + } |
|
958 | + $class_name = str_replace('\\', '_', $class_name); |
|
959 | + if (isset($this->{$class_name})) { |
|
960 | + return $this->{$class_name}; |
|
961 | + } |
|
962 | + if ($class_prefix === 'addon' && isset($this->addons->{$class_name})) { |
|
963 | + return $this->addons->{$class_name}; |
|
964 | + } |
|
965 | + $object_identifier = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
966 | + if (isset($this->LIB->{$object_identifier})) { |
|
967 | + return $this->LIB->{$object_identifier}; |
|
968 | + } |
|
969 | + foreach ($this->LIB as $key => $object) { |
|
970 | + if ( |
|
971 | 971 | // request does not contain new arguments and therefore no args identifier |
972 | - ! $this->object_identifier->hasArguments($object_identifier) |
|
973 | - // but previously cached class with args was found |
|
974 | - && $this->object_identifier->fqcnMatchesObjectIdentifier($class_name, $key) |
|
975 | - ) { |
|
976 | - return $object; |
|
977 | - } |
|
978 | - } |
|
979 | - return null; |
|
980 | - } |
|
981 | - |
|
982 | - |
|
983 | - /** |
|
984 | - * removes a cached version of the requested class |
|
985 | - * |
|
986 | - * @param string $class_name |
|
987 | - * @param boolean $addon |
|
988 | - * @param array $arguments |
|
989 | - * @return boolean |
|
990 | - */ |
|
991 | - public function clear_cached_class( |
|
992 | - $class_name, |
|
993 | - $addon = false, |
|
994 | - $arguments = array() |
|
995 | - ) { |
|
996 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
997 | - // check if class has already been loaded, and return it if it has been |
|
998 | - if (isset($this->{$class_abbreviation})) { |
|
999 | - $this->{$class_abbreviation} = null; |
|
1000 | - return true; |
|
1001 | - } |
|
1002 | - $class_name = str_replace('\\', '_', $class_name); |
|
1003 | - if (isset($this->{$class_name})) { |
|
1004 | - $this->{$class_name} = null; |
|
1005 | - return true; |
|
1006 | - } |
|
1007 | - if ($addon && isset($this->addons->{$class_name})) { |
|
1008 | - unset($this->addons->{$class_name}); |
|
1009 | - return true; |
|
1010 | - } |
|
1011 | - $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
1012 | - if (isset($this->LIB->{$class_name})) { |
|
1013 | - unset($this->LIB->{$class_name}); |
|
1014 | - return true; |
|
1015 | - } |
|
1016 | - return false; |
|
1017 | - } |
|
1018 | - |
|
1019 | - |
|
1020 | - /** |
|
1021 | - * _set_cached_class |
|
1022 | - * attempts to cache the instantiated class locally |
|
1023 | - * in one of the following places, in the following order: |
|
1024 | - * $this->{class_abbreviation} ie: $this->CART |
|
1025 | - * $this->{$class_name} ie: $this->Some_Class |
|
1026 | - * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
1027 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
1028 | - * |
|
1029 | - * @param object $class_obj |
|
1030 | - * @param string $class_name |
|
1031 | - * @param string $class_prefix |
|
1032 | - * @param bool $from_db |
|
1033 | - * @param array $arguments |
|
1034 | - * @return void |
|
1035 | - */ |
|
1036 | - protected function _set_cached_class( |
|
1037 | - $class_obj, |
|
1038 | - $class_name, |
|
1039 | - $class_prefix = '', |
|
1040 | - $from_db = false, |
|
1041 | - $arguments = array() |
|
1042 | - ) { |
|
1043 | - if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
1044 | - return; |
|
1045 | - } |
|
1046 | - // return newly instantiated class |
|
1047 | - $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
1048 | - if ($class_abbreviation) { |
|
1049 | - $this->{$class_abbreviation} = $class_obj; |
|
1050 | - return; |
|
1051 | - } |
|
1052 | - $class_name = str_replace('\\', '_', $class_name); |
|
1053 | - if (property_exists($this, $class_name)) { |
|
1054 | - $this->{$class_name} = $class_obj; |
|
1055 | - return; |
|
1056 | - } |
|
1057 | - if ($class_prefix === 'addon') { |
|
1058 | - $this->addons->{$class_name} = $class_obj; |
|
1059 | - return; |
|
1060 | - } |
|
1061 | - if (! $from_db) { |
|
1062 | - $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
1063 | - $this->LIB->{$class_name} = $class_obj; |
|
1064 | - } |
|
1065 | - } |
|
1066 | - |
|
1067 | - |
|
1068 | - /** |
|
1069 | - * attempts to find a full valid filepath for the requested class. |
|
1070 | - * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
1071 | - * then returns that path if the target file has been found and is readable |
|
1072 | - * |
|
1073 | - * @param string $class_name |
|
1074 | - * @param string $type |
|
1075 | - * @param array $file_paths |
|
1076 | - * @return string | bool |
|
1077 | - */ |
|
1078 | - protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
1079 | - { |
|
1080 | - // make sure $file_paths is an array |
|
1081 | - $file_paths = is_array($file_paths) |
|
1082 | - ? $file_paths |
|
1083 | - : array($file_paths); |
|
1084 | - // cycle thru paths |
|
1085 | - foreach ($file_paths as $key => $file_path) { |
|
1086 | - // convert all separators to proper /, if no filepath, then use EE_CLASSES |
|
1087 | - $file_path = $file_path |
|
1088 | - ? str_replace(array('/', '\\'), '/', $file_path) |
|
1089 | - : EE_CLASSES; |
|
1090 | - // prep file type |
|
1091 | - $type = ! empty($type) |
|
1092 | - ? trim($type, '.') . '.' |
|
1093 | - : ''; |
|
1094 | - // build full file path |
|
1095 | - $file_paths[ $key ] = rtrim($file_path, '/') . '/' . $class_name . '.' . $type . 'php'; |
|
1096 | - // does the file exist and can be read ? |
|
1097 | - if (is_readable($file_paths[ $key ])) { |
|
1098 | - return $file_paths[ $key ]; |
|
1099 | - } |
|
1100 | - } |
|
1101 | - return false; |
|
1102 | - } |
|
1103 | - |
|
1104 | - |
|
1105 | - /** |
|
1106 | - * basically just performs a require_once() |
|
1107 | - * but with some error handling |
|
1108 | - * |
|
1109 | - * @param string $path |
|
1110 | - * @param string $class_name |
|
1111 | - * @param string $type |
|
1112 | - * @param array $file_paths |
|
1113 | - * @return bool |
|
1114 | - * @throws EE_Error |
|
1115 | - * @throws ReflectionException |
|
1116 | - */ |
|
1117 | - protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
1118 | - { |
|
1119 | - $this->resolve_legacy_class_parent($class_name); |
|
1120 | - // don't give up! you gotta... |
|
1121 | - try { |
|
1122 | - // does the file exist and can it be read ? |
|
1123 | - if (! $path) { |
|
1124 | - // just in case the file has already been autoloaded, |
|
1125 | - // but discrepancies in the naming schema are preventing it from |
|
1126 | - // being loaded via one of the EE_Registry::load_*() methods, |
|
1127 | - // then let's try one last hail mary before throwing an exception |
|
1128 | - // and call class_exists() again, but with autoloading turned ON |
|
1129 | - if (class_exists($class_name)) { |
|
1130 | - return true; |
|
1131 | - } |
|
1132 | - // so sorry, can't find the file |
|
1133 | - throw new EE_Error( |
|
1134 | - sprintf( |
|
1135 | - esc_html__( |
|
1136 | - 'The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', |
|
1137 | - 'event_espresso' |
|
1138 | - ), |
|
1139 | - trim($type, '.'), |
|
1140 | - $class_name, |
|
1141 | - '<br />' . implode(',<br />', $file_paths) |
|
1142 | - ) |
|
1143 | - ); |
|
1144 | - } |
|
1145 | - // get the file |
|
1146 | - require_once($path); |
|
1147 | - // if the class isn't already declared somewhere |
|
1148 | - if (class_exists($class_name, false) === false) { |
|
1149 | - // so sorry, not a class |
|
1150 | - throw new EE_Error( |
|
1151 | - sprintf( |
|
1152 | - esc_html__( |
|
1153 | - 'The %s file %s does not appear to contain the %s Class.', |
|
1154 | - 'event_espresso' |
|
1155 | - ), |
|
1156 | - $type, |
|
1157 | - $path, |
|
1158 | - $class_name |
|
1159 | - ) |
|
1160 | - ); |
|
1161 | - } |
|
1162 | - } catch (EE_Error $e) { |
|
1163 | - $e->get_error(); |
|
1164 | - return false; |
|
1165 | - } |
|
1166 | - return true; |
|
1167 | - } |
|
1168 | - |
|
1169 | - |
|
1170 | - /** |
|
1171 | - * Some of our legacy classes that extended a parent class would simply use a require() statement |
|
1172 | - * before their class declaration in order to ensure that the parent class was loaded. |
|
1173 | - * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
|
1174 | - * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
|
1175 | - * |
|
1176 | - * @param string $class_name |
|
1177 | - */ |
|
1178 | - protected function resolve_legacy_class_parent($class_name = '') |
|
1179 | - { |
|
1180 | - try { |
|
1181 | - $legacy_parent_class_map = array( |
|
1182 | - 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php', |
|
1183 | - ); |
|
1184 | - if (isset($legacy_parent_class_map[ $class_name ])) { |
|
1185 | - require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[ $class_name ]; |
|
1186 | - } |
|
1187 | - } catch (Exception $exception) { |
|
1188 | - } |
|
1189 | - } |
|
1190 | - |
|
1191 | - |
|
1192 | - /** |
|
1193 | - * _create_object |
|
1194 | - * Attempts to instantiate the requested class via any of the |
|
1195 | - * commonly used instantiation methods employed throughout EE. |
|
1196 | - * The priority for instantiation is as follows: |
|
1197 | - * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
1198 | - * - model objects via their 'new_instance_from_db' method |
|
1199 | - * - model objects via their 'new_instance' method |
|
1200 | - * - "singleton" classes" via their 'instance' method |
|
1201 | - * - standard instantiable classes via their __constructor |
|
1202 | - * Prior to instantiation, if the classname exists in the dependency_map, |
|
1203 | - * then the constructor for the requested class will be examined to determine |
|
1204 | - * if any dependencies exist, and if they can be injected. |
|
1205 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1206 | - * |
|
1207 | - * @param string $class_name |
|
1208 | - * @param array $arguments |
|
1209 | - * @param string $type |
|
1210 | - * @param bool $from_db |
|
1211 | - * @return null|object|bool |
|
1212 | - * @throws InvalidArgumentException |
|
1213 | - * @throws InvalidInterfaceException |
|
1214 | - * @throws EE_Error |
|
1215 | - * @throws ReflectionException |
|
1216 | - * @throws InvalidDataTypeException |
|
1217 | - */ |
|
1218 | - protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
1219 | - { |
|
1220 | - // create reflection |
|
1221 | - $reflector = $this->mirror->getReflectionClass($class_name); |
|
1222 | - // make sure arguments are an array |
|
1223 | - $arguments = is_array($arguments) |
|
1224 | - ? $arguments |
|
1225 | - : array($arguments); |
|
1226 | - // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
1227 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
1228 | - $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
1229 | - ? $arguments |
|
1230 | - : array($arguments); |
|
1231 | - // attempt to inject dependencies ? |
|
1232 | - if ($this->_dependency_map->has($class_name)) { |
|
1233 | - $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
1234 | - } |
|
1235 | - // instantiate the class if possible |
|
1236 | - if ($reflector->isAbstract()) { |
|
1237 | - // nothing to instantiate, loading file was enough |
|
1238 | - // does not throw an exception so $instantiation_mode is unused |
|
1239 | - // $instantiation_mode = "1) no constructor abstract class"; |
|
1240 | - return true; |
|
1241 | - } |
|
1242 | - if ( |
|
1243 | - empty($arguments) |
|
1244 | - && $this->mirror->getConstructorFromReflection($reflector) === null |
|
1245 | - && $reflector->isInstantiable() |
|
1246 | - ) { |
|
1247 | - // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
1248 | - // $instantiation_mode = "2) no constructor but instantiable"; |
|
1249 | - return $reflector->newInstance(); |
|
1250 | - } |
|
1251 | - if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
1252 | - // $instantiation_mode = "3) new_instance_from_db()"; |
|
1253 | - return call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
1254 | - } |
|
1255 | - if (method_exists($class_name, 'new_instance')) { |
|
1256 | - // $instantiation_mode = "4) new_instance()"; |
|
1257 | - return call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
1258 | - } |
|
1259 | - if (method_exists($class_name, 'instance')) { |
|
1260 | - // $instantiation_mode = "5) instance()"; |
|
1261 | - return call_user_func_array(array($class_name, 'instance'), $arguments); |
|
1262 | - } |
|
1263 | - if ($reflector->isInstantiable()) { |
|
1264 | - $args_passed_count = count($arguments); |
|
1265 | - $args_required_count = count($this->mirror->getRequiredParameters($class_name)); |
|
1266 | - if ($args_passed_count < $args_required_count) { |
|
1267 | - throw new RuntimeException( |
|
1268 | - sprintf( |
|
1269 | - __( |
|
1270 | - 'Invalid arguments supplied for the %1$s class, %2$s were required but %3$s were passed.', |
|
1271 | - 'event_espresso' |
|
1272 | - ), |
|
1273 | - $class_name, |
|
1274 | - $args_required_count, |
|
1275 | - $args_passed_count |
|
1276 | - ) |
|
1277 | - ); |
|
1278 | - } |
|
1279 | - // $instantiation_mode = "6) constructor"; |
|
1280 | - return $reflector->newInstanceArgs($arguments); |
|
1281 | - } |
|
1282 | - // heh ? something's not right ! |
|
1283 | - throw new EE_Error( |
|
1284 | - sprintf( |
|
1285 | - __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
1286 | - $type, |
|
1287 | - $class_name |
|
1288 | - ) |
|
1289 | - ); |
|
1290 | - } |
|
1291 | - |
|
1292 | - |
|
1293 | - /** |
|
1294 | - * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
1295 | - * @param array $array |
|
1296 | - * @return bool |
|
1297 | - */ |
|
1298 | - protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
1299 | - { |
|
1300 | - return ! empty($array) |
|
1301 | - ? array_keys($array) === range(0, count($array) - 1) |
|
1302 | - : true; |
|
1303 | - } |
|
1304 | - |
|
1305 | - |
|
1306 | - /** |
|
1307 | - * _resolve_dependencies |
|
1308 | - * examines the constructor for the requested class to determine |
|
1309 | - * if any dependencies exist, and if they can be injected. |
|
1310 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1311 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
1312 | - * For example: |
|
1313 | - * if attempting to load a class "Foo" with the following constructor: |
|
1314 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
1315 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
1316 | - * but only IF they are NOT already present in the incoming arguments array, |
|
1317 | - * and the correct classes can be loaded |
|
1318 | - * |
|
1319 | - * @param ReflectionClass $reflector |
|
1320 | - * @param string $class_name |
|
1321 | - * @param array $arguments |
|
1322 | - * @return array |
|
1323 | - * @throws InvalidArgumentException |
|
1324 | - * @throws InvalidDataTypeException |
|
1325 | - * @throws InvalidInterfaceException |
|
1326 | - * @throws ReflectionException |
|
1327 | - */ |
|
1328 | - protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, array $arguments = array()) |
|
1329 | - { |
|
1330 | - // let's examine the constructor |
|
1331 | - $constructor = $this->mirror->getConstructorFromReflection($reflector); |
|
1332 | - // whu? huh? nothing? |
|
1333 | - if (! $constructor) { |
|
1334 | - return $arguments; |
|
1335 | - } |
|
1336 | - // get constructor parameters |
|
1337 | - $params = $this->mirror->getParametersFromReflection($reflector); |
|
1338 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
1339 | - $argument_keys = array_keys($arguments); |
|
1340 | - // now loop thru all of the constructors expected parameters |
|
1341 | - foreach ($params as $index => $param) { |
|
1342 | - try { |
|
1343 | - // is this a dependency for a specific class ? |
|
1344 | - $param_class = $this->mirror->getParameterClassName($param, $class_name, $index); |
|
1345 | - } catch (ReflectionException $exception) { |
|
1346 | - // uh-oh... most likely a legacy class that has not been autoloaded |
|
1347 | - // let's try to derive the classname from what we have now |
|
1348 | - // and hope that the property var name is close to the class name |
|
1349 | - $param_class = $param->getName(); |
|
1350 | - $param_class = str_replace('_', ' ', $param_class); |
|
1351 | - $param_class = ucwords($param_class); |
|
1352 | - $param_class = str_replace(' ', '_', $param_class); |
|
1353 | - } |
|
1354 | - // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
1355 | - $param_class = $this->class_cache->isAlias($param_class, $class_name) |
|
1356 | - ? $this->class_cache->getFqnForAlias($param_class, $class_name) |
|
1357 | - : $param_class; |
|
1358 | - if ( |
|
972 | + ! $this->object_identifier->hasArguments($object_identifier) |
|
973 | + // but previously cached class with args was found |
|
974 | + && $this->object_identifier->fqcnMatchesObjectIdentifier($class_name, $key) |
|
975 | + ) { |
|
976 | + return $object; |
|
977 | + } |
|
978 | + } |
|
979 | + return null; |
|
980 | + } |
|
981 | + |
|
982 | + |
|
983 | + /** |
|
984 | + * removes a cached version of the requested class |
|
985 | + * |
|
986 | + * @param string $class_name |
|
987 | + * @param boolean $addon |
|
988 | + * @param array $arguments |
|
989 | + * @return boolean |
|
990 | + */ |
|
991 | + public function clear_cached_class( |
|
992 | + $class_name, |
|
993 | + $addon = false, |
|
994 | + $arguments = array() |
|
995 | + ) { |
|
996 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
997 | + // check if class has already been loaded, and return it if it has been |
|
998 | + if (isset($this->{$class_abbreviation})) { |
|
999 | + $this->{$class_abbreviation} = null; |
|
1000 | + return true; |
|
1001 | + } |
|
1002 | + $class_name = str_replace('\\', '_', $class_name); |
|
1003 | + if (isset($this->{$class_name})) { |
|
1004 | + $this->{$class_name} = null; |
|
1005 | + return true; |
|
1006 | + } |
|
1007 | + if ($addon && isset($this->addons->{$class_name})) { |
|
1008 | + unset($this->addons->{$class_name}); |
|
1009 | + return true; |
|
1010 | + } |
|
1011 | + $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
1012 | + if (isset($this->LIB->{$class_name})) { |
|
1013 | + unset($this->LIB->{$class_name}); |
|
1014 | + return true; |
|
1015 | + } |
|
1016 | + return false; |
|
1017 | + } |
|
1018 | + |
|
1019 | + |
|
1020 | + /** |
|
1021 | + * _set_cached_class |
|
1022 | + * attempts to cache the instantiated class locally |
|
1023 | + * in one of the following places, in the following order: |
|
1024 | + * $this->{class_abbreviation} ie: $this->CART |
|
1025 | + * $this->{$class_name} ie: $this->Some_Class |
|
1026 | + * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
1027 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
1028 | + * |
|
1029 | + * @param object $class_obj |
|
1030 | + * @param string $class_name |
|
1031 | + * @param string $class_prefix |
|
1032 | + * @param bool $from_db |
|
1033 | + * @param array $arguments |
|
1034 | + * @return void |
|
1035 | + */ |
|
1036 | + protected function _set_cached_class( |
|
1037 | + $class_obj, |
|
1038 | + $class_name, |
|
1039 | + $class_prefix = '', |
|
1040 | + $from_db = false, |
|
1041 | + $arguments = array() |
|
1042 | + ) { |
|
1043 | + if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
1044 | + return; |
|
1045 | + } |
|
1046 | + // return newly instantiated class |
|
1047 | + $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
1048 | + if ($class_abbreviation) { |
|
1049 | + $this->{$class_abbreviation} = $class_obj; |
|
1050 | + return; |
|
1051 | + } |
|
1052 | + $class_name = str_replace('\\', '_', $class_name); |
|
1053 | + if (property_exists($this, $class_name)) { |
|
1054 | + $this->{$class_name} = $class_obj; |
|
1055 | + return; |
|
1056 | + } |
|
1057 | + if ($class_prefix === 'addon') { |
|
1058 | + $this->addons->{$class_name} = $class_obj; |
|
1059 | + return; |
|
1060 | + } |
|
1061 | + if (! $from_db) { |
|
1062 | + $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
1063 | + $this->LIB->{$class_name} = $class_obj; |
|
1064 | + } |
|
1065 | + } |
|
1066 | + |
|
1067 | + |
|
1068 | + /** |
|
1069 | + * attempts to find a full valid filepath for the requested class. |
|
1070 | + * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
1071 | + * then returns that path if the target file has been found and is readable |
|
1072 | + * |
|
1073 | + * @param string $class_name |
|
1074 | + * @param string $type |
|
1075 | + * @param array $file_paths |
|
1076 | + * @return string | bool |
|
1077 | + */ |
|
1078 | + protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
1079 | + { |
|
1080 | + // make sure $file_paths is an array |
|
1081 | + $file_paths = is_array($file_paths) |
|
1082 | + ? $file_paths |
|
1083 | + : array($file_paths); |
|
1084 | + // cycle thru paths |
|
1085 | + foreach ($file_paths as $key => $file_path) { |
|
1086 | + // convert all separators to proper /, if no filepath, then use EE_CLASSES |
|
1087 | + $file_path = $file_path |
|
1088 | + ? str_replace(array('/', '\\'), '/', $file_path) |
|
1089 | + : EE_CLASSES; |
|
1090 | + // prep file type |
|
1091 | + $type = ! empty($type) |
|
1092 | + ? trim($type, '.') . '.' |
|
1093 | + : ''; |
|
1094 | + // build full file path |
|
1095 | + $file_paths[ $key ] = rtrim($file_path, '/') . '/' . $class_name . '.' . $type . 'php'; |
|
1096 | + // does the file exist and can be read ? |
|
1097 | + if (is_readable($file_paths[ $key ])) { |
|
1098 | + return $file_paths[ $key ]; |
|
1099 | + } |
|
1100 | + } |
|
1101 | + return false; |
|
1102 | + } |
|
1103 | + |
|
1104 | + |
|
1105 | + /** |
|
1106 | + * basically just performs a require_once() |
|
1107 | + * but with some error handling |
|
1108 | + * |
|
1109 | + * @param string $path |
|
1110 | + * @param string $class_name |
|
1111 | + * @param string $type |
|
1112 | + * @param array $file_paths |
|
1113 | + * @return bool |
|
1114 | + * @throws EE_Error |
|
1115 | + * @throws ReflectionException |
|
1116 | + */ |
|
1117 | + protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
1118 | + { |
|
1119 | + $this->resolve_legacy_class_parent($class_name); |
|
1120 | + // don't give up! you gotta... |
|
1121 | + try { |
|
1122 | + // does the file exist and can it be read ? |
|
1123 | + if (! $path) { |
|
1124 | + // just in case the file has already been autoloaded, |
|
1125 | + // but discrepancies in the naming schema are preventing it from |
|
1126 | + // being loaded via one of the EE_Registry::load_*() methods, |
|
1127 | + // then let's try one last hail mary before throwing an exception |
|
1128 | + // and call class_exists() again, but with autoloading turned ON |
|
1129 | + if (class_exists($class_name)) { |
|
1130 | + return true; |
|
1131 | + } |
|
1132 | + // so sorry, can't find the file |
|
1133 | + throw new EE_Error( |
|
1134 | + sprintf( |
|
1135 | + esc_html__( |
|
1136 | + 'The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', |
|
1137 | + 'event_espresso' |
|
1138 | + ), |
|
1139 | + trim($type, '.'), |
|
1140 | + $class_name, |
|
1141 | + '<br />' . implode(',<br />', $file_paths) |
|
1142 | + ) |
|
1143 | + ); |
|
1144 | + } |
|
1145 | + // get the file |
|
1146 | + require_once($path); |
|
1147 | + // if the class isn't already declared somewhere |
|
1148 | + if (class_exists($class_name, false) === false) { |
|
1149 | + // so sorry, not a class |
|
1150 | + throw new EE_Error( |
|
1151 | + sprintf( |
|
1152 | + esc_html__( |
|
1153 | + 'The %s file %s does not appear to contain the %s Class.', |
|
1154 | + 'event_espresso' |
|
1155 | + ), |
|
1156 | + $type, |
|
1157 | + $path, |
|
1158 | + $class_name |
|
1159 | + ) |
|
1160 | + ); |
|
1161 | + } |
|
1162 | + } catch (EE_Error $e) { |
|
1163 | + $e->get_error(); |
|
1164 | + return false; |
|
1165 | + } |
|
1166 | + return true; |
|
1167 | + } |
|
1168 | + |
|
1169 | + |
|
1170 | + /** |
|
1171 | + * Some of our legacy classes that extended a parent class would simply use a require() statement |
|
1172 | + * before their class declaration in order to ensure that the parent class was loaded. |
|
1173 | + * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
|
1174 | + * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
|
1175 | + * |
|
1176 | + * @param string $class_name |
|
1177 | + */ |
|
1178 | + protected function resolve_legacy_class_parent($class_name = '') |
|
1179 | + { |
|
1180 | + try { |
|
1181 | + $legacy_parent_class_map = array( |
|
1182 | + 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php', |
|
1183 | + ); |
|
1184 | + if (isset($legacy_parent_class_map[ $class_name ])) { |
|
1185 | + require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[ $class_name ]; |
|
1186 | + } |
|
1187 | + } catch (Exception $exception) { |
|
1188 | + } |
|
1189 | + } |
|
1190 | + |
|
1191 | + |
|
1192 | + /** |
|
1193 | + * _create_object |
|
1194 | + * Attempts to instantiate the requested class via any of the |
|
1195 | + * commonly used instantiation methods employed throughout EE. |
|
1196 | + * The priority for instantiation is as follows: |
|
1197 | + * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
1198 | + * - model objects via their 'new_instance_from_db' method |
|
1199 | + * - model objects via their 'new_instance' method |
|
1200 | + * - "singleton" classes" via their 'instance' method |
|
1201 | + * - standard instantiable classes via their __constructor |
|
1202 | + * Prior to instantiation, if the classname exists in the dependency_map, |
|
1203 | + * then the constructor for the requested class will be examined to determine |
|
1204 | + * if any dependencies exist, and if they can be injected. |
|
1205 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1206 | + * |
|
1207 | + * @param string $class_name |
|
1208 | + * @param array $arguments |
|
1209 | + * @param string $type |
|
1210 | + * @param bool $from_db |
|
1211 | + * @return null|object|bool |
|
1212 | + * @throws InvalidArgumentException |
|
1213 | + * @throws InvalidInterfaceException |
|
1214 | + * @throws EE_Error |
|
1215 | + * @throws ReflectionException |
|
1216 | + * @throws InvalidDataTypeException |
|
1217 | + */ |
|
1218 | + protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
1219 | + { |
|
1220 | + // create reflection |
|
1221 | + $reflector = $this->mirror->getReflectionClass($class_name); |
|
1222 | + // make sure arguments are an array |
|
1223 | + $arguments = is_array($arguments) |
|
1224 | + ? $arguments |
|
1225 | + : array($arguments); |
|
1226 | + // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
1227 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
1228 | + $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
1229 | + ? $arguments |
|
1230 | + : array($arguments); |
|
1231 | + // attempt to inject dependencies ? |
|
1232 | + if ($this->_dependency_map->has($class_name)) { |
|
1233 | + $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
1234 | + } |
|
1235 | + // instantiate the class if possible |
|
1236 | + if ($reflector->isAbstract()) { |
|
1237 | + // nothing to instantiate, loading file was enough |
|
1238 | + // does not throw an exception so $instantiation_mode is unused |
|
1239 | + // $instantiation_mode = "1) no constructor abstract class"; |
|
1240 | + return true; |
|
1241 | + } |
|
1242 | + if ( |
|
1243 | + empty($arguments) |
|
1244 | + && $this->mirror->getConstructorFromReflection($reflector) === null |
|
1245 | + && $reflector->isInstantiable() |
|
1246 | + ) { |
|
1247 | + // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
1248 | + // $instantiation_mode = "2) no constructor but instantiable"; |
|
1249 | + return $reflector->newInstance(); |
|
1250 | + } |
|
1251 | + if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
1252 | + // $instantiation_mode = "3) new_instance_from_db()"; |
|
1253 | + return call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
1254 | + } |
|
1255 | + if (method_exists($class_name, 'new_instance')) { |
|
1256 | + // $instantiation_mode = "4) new_instance()"; |
|
1257 | + return call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
1258 | + } |
|
1259 | + if (method_exists($class_name, 'instance')) { |
|
1260 | + // $instantiation_mode = "5) instance()"; |
|
1261 | + return call_user_func_array(array($class_name, 'instance'), $arguments); |
|
1262 | + } |
|
1263 | + if ($reflector->isInstantiable()) { |
|
1264 | + $args_passed_count = count($arguments); |
|
1265 | + $args_required_count = count($this->mirror->getRequiredParameters($class_name)); |
|
1266 | + if ($args_passed_count < $args_required_count) { |
|
1267 | + throw new RuntimeException( |
|
1268 | + sprintf( |
|
1269 | + __( |
|
1270 | + 'Invalid arguments supplied for the %1$s class, %2$s were required but %3$s were passed.', |
|
1271 | + 'event_espresso' |
|
1272 | + ), |
|
1273 | + $class_name, |
|
1274 | + $args_required_count, |
|
1275 | + $args_passed_count |
|
1276 | + ) |
|
1277 | + ); |
|
1278 | + } |
|
1279 | + // $instantiation_mode = "6) constructor"; |
|
1280 | + return $reflector->newInstanceArgs($arguments); |
|
1281 | + } |
|
1282 | + // heh ? something's not right ! |
|
1283 | + throw new EE_Error( |
|
1284 | + sprintf( |
|
1285 | + __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
1286 | + $type, |
|
1287 | + $class_name |
|
1288 | + ) |
|
1289 | + ); |
|
1290 | + } |
|
1291 | + |
|
1292 | + |
|
1293 | + /** |
|
1294 | + * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
1295 | + * @param array $array |
|
1296 | + * @return bool |
|
1297 | + */ |
|
1298 | + protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
1299 | + { |
|
1300 | + return ! empty($array) |
|
1301 | + ? array_keys($array) === range(0, count($array) - 1) |
|
1302 | + : true; |
|
1303 | + } |
|
1304 | + |
|
1305 | + |
|
1306 | + /** |
|
1307 | + * _resolve_dependencies |
|
1308 | + * examines the constructor for the requested class to determine |
|
1309 | + * if any dependencies exist, and if they can be injected. |
|
1310 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1311 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
1312 | + * For example: |
|
1313 | + * if attempting to load a class "Foo" with the following constructor: |
|
1314 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
1315 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
1316 | + * but only IF they are NOT already present in the incoming arguments array, |
|
1317 | + * and the correct classes can be loaded |
|
1318 | + * |
|
1319 | + * @param ReflectionClass $reflector |
|
1320 | + * @param string $class_name |
|
1321 | + * @param array $arguments |
|
1322 | + * @return array |
|
1323 | + * @throws InvalidArgumentException |
|
1324 | + * @throws InvalidDataTypeException |
|
1325 | + * @throws InvalidInterfaceException |
|
1326 | + * @throws ReflectionException |
|
1327 | + */ |
|
1328 | + protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, array $arguments = array()) |
|
1329 | + { |
|
1330 | + // let's examine the constructor |
|
1331 | + $constructor = $this->mirror->getConstructorFromReflection($reflector); |
|
1332 | + // whu? huh? nothing? |
|
1333 | + if (! $constructor) { |
|
1334 | + return $arguments; |
|
1335 | + } |
|
1336 | + // get constructor parameters |
|
1337 | + $params = $this->mirror->getParametersFromReflection($reflector); |
|
1338 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
1339 | + $argument_keys = array_keys($arguments); |
|
1340 | + // now loop thru all of the constructors expected parameters |
|
1341 | + foreach ($params as $index => $param) { |
|
1342 | + try { |
|
1343 | + // is this a dependency for a specific class ? |
|
1344 | + $param_class = $this->mirror->getParameterClassName($param, $class_name, $index); |
|
1345 | + } catch (ReflectionException $exception) { |
|
1346 | + // uh-oh... most likely a legacy class that has not been autoloaded |
|
1347 | + // let's try to derive the classname from what we have now |
|
1348 | + // and hope that the property var name is close to the class name |
|
1349 | + $param_class = $param->getName(); |
|
1350 | + $param_class = str_replace('_', ' ', $param_class); |
|
1351 | + $param_class = ucwords($param_class); |
|
1352 | + $param_class = str_replace(' ', '_', $param_class); |
|
1353 | + } |
|
1354 | + // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
1355 | + $param_class = $this->class_cache->isAlias($param_class, $class_name) |
|
1356 | + ? $this->class_cache->getFqnForAlias($param_class, $class_name) |
|
1357 | + : $param_class; |
|
1358 | + if ( |
|
1359 | 1359 | // param is not even a class |
1360 | - $param_class === null |
|
1361 | - // and something already exists in the incoming arguments for this param |
|
1362 | - && array_key_exists($index, $argument_keys) |
|
1363 | - && array_key_exists($argument_keys[ $index ], $arguments) |
|
1364 | - ) { |
|
1365 | - // so let's skip this argument and move on to the next |
|
1366 | - continue; |
|
1367 | - } |
|
1368 | - if ( |
|
1360 | + $param_class === null |
|
1361 | + // and something already exists in the incoming arguments for this param |
|
1362 | + && array_key_exists($index, $argument_keys) |
|
1363 | + && array_key_exists($argument_keys[ $index ], $arguments) |
|
1364 | + ) { |
|
1365 | + // so let's skip this argument and move on to the next |
|
1366 | + continue; |
|
1367 | + } |
|
1368 | + if ( |
|
1369 | 1369 | // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
1370 | - $param_class !== null |
|
1371 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
1372 | - && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
1373 | - ) { |
|
1374 | - // skip this argument and move on to the next |
|
1375 | - continue; |
|
1376 | - } |
|
1377 | - if ( |
|
1370 | + $param_class !== null |
|
1371 | + && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
1372 | + && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
1373 | + ) { |
|
1374 | + // skip this argument and move on to the next |
|
1375 | + continue; |
|
1376 | + } |
|
1377 | + if ( |
|
1378 | 1378 | // parameter is type hinted as a class, and should be injected |
1379 | - $param_class !== null |
|
1380 | - && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
1381 | - ) { |
|
1382 | - $arguments = $this->_resolve_dependency( |
|
1383 | - $class_name, |
|
1384 | - $param_class, |
|
1385 | - $arguments, |
|
1386 | - $index |
|
1387 | - ); |
|
1388 | - } |
|
1389 | - if (empty($arguments[ $index ])) { |
|
1390 | - $arguments[ $index ] = $this->mirror->getParameterDefaultValue( |
|
1391 | - $param, |
|
1392 | - $class_name, |
|
1393 | - $index |
|
1394 | - ); |
|
1395 | - } |
|
1396 | - } |
|
1397 | - return $arguments; |
|
1398 | - } |
|
1399 | - |
|
1400 | - |
|
1401 | - /** |
|
1402 | - * @param string $class_name |
|
1403 | - * @param string $param_class |
|
1404 | - * @param array $arguments |
|
1405 | - * @param mixed $index |
|
1406 | - * @return array |
|
1407 | - * @throws InvalidArgumentException |
|
1408 | - * @throws InvalidInterfaceException |
|
1409 | - * @throws InvalidDataTypeException |
|
1410 | - */ |
|
1411 | - protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
1412 | - { |
|
1413 | - $dependency = null; |
|
1414 | - // should dependency be loaded from cache ? |
|
1415 | - $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
1416 | - $class_name, |
|
1417 | - $param_class |
|
1418 | - ); |
|
1419 | - $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
1420 | - // we might have a dependency... |
|
1421 | - // let's MAYBE try and find it in our cache if that's what's been requested |
|
1422 | - $cached_class = $cache_on |
|
1423 | - ? $this->_get_cached_class($param_class) |
|
1424 | - : null; |
|
1425 | - // and grab it if it exists |
|
1426 | - if ($cached_class instanceof $param_class) { |
|
1427 | - $dependency = $cached_class; |
|
1428 | - } elseif ($param_class !== $class_name) { |
|
1429 | - // obtain the loader method from the dependency map |
|
1430 | - $loader = $this->_dependency_map->class_loader($param_class); |
|
1431 | - // is loader a custom closure ? |
|
1432 | - if ($loader instanceof Closure) { |
|
1433 | - $dependency = $loader($arguments); |
|
1434 | - } else { |
|
1435 | - // set the cache on property for the recursive loading call |
|
1436 | - $this->_cache_on = $cache_on; |
|
1437 | - // if not, then let's try and load it via the registry |
|
1438 | - if ($loader && method_exists($this, $loader)) { |
|
1439 | - $dependency = $this->{$loader}($param_class); |
|
1440 | - } else { |
|
1441 | - $dependency = LoaderFactory::getLoader()->load( |
|
1442 | - $param_class, |
|
1443 | - array(), |
|
1444 | - $cache_on |
|
1445 | - ); |
|
1446 | - } |
|
1447 | - } |
|
1448 | - } |
|
1449 | - // did we successfully find the correct dependency ? |
|
1450 | - if ($dependency instanceof $param_class) { |
|
1451 | - // then let's inject it into the incoming array of arguments at the correct location |
|
1452 | - $arguments[ $index ] = $dependency; |
|
1453 | - } |
|
1454 | - return $arguments; |
|
1455 | - } |
|
1456 | - |
|
1457 | - |
|
1458 | - /** |
|
1459 | - * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
1460 | - * |
|
1461 | - * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
1462 | - * in the EE_Dependency_Map::$_class_loaders array, |
|
1463 | - * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
1464 | - * @param array $arguments |
|
1465 | - * @return object |
|
1466 | - */ |
|
1467 | - public static function factory($classname, $arguments = array()) |
|
1468 | - { |
|
1469 | - $loader = self::instance()->_dependency_map->class_loader($classname); |
|
1470 | - if ($loader instanceof Closure) { |
|
1471 | - return $loader($arguments); |
|
1472 | - } |
|
1473 | - if (method_exists(self::instance(), $loader)) { |
|
1474 | - return self::instance()->{$loader}($classname, $arguments); |
|
1475 | - } |
|
1476 | - return null; |
|
1477 | - } |
|
1478 | - |
|
1479 | - |
|
1480 | - /** |
|
1481 | - * Gets the addon by its class name |
|
1482 | - * |
|
1483 | - * @param string $class_name |
|
1484 | - * @return EE_Addon |
|
1485 | - */ |
|
1486 | - public function getAddon($class_name) |
|
1487 | - { |
|
1488 | - $class_name = str_replace('\\', '_', $class_name); |
|
1489 | - if (isset($this->addons->{$class_name})) { |
|
1490 | - return $this->addons->{$class_name}; |
|
1491 | - } else { |
|
1492 | - return null; |
|
1493 | - } |
|
1494 | - } |
|
1495 | - |
|
1496 | - |
|
1497 | - /** |
|
1498 | - * removes the addon from the internal cache |
|
1499 | - * |
|
1500 | - * @param string $class_name |
|
1501 | - * @return void |
|
1502 | - */ |
|
1503 | - public function removeAddon($class_name) |
|
1504 | - { |
|
1505 | - $class_name = str_replace('\\', '_', $class_name); |
|
1506 | - unset($this->addons->{$class_name}); |
|
1507 | - } |
|
1508 | - |
|
1509 | - |
|
1510 | - /** |
|
1511 | - * Gets the addon by its name/slug (not classname. For that, just |
|
1512 | - * use the get_addon() method above |
|
1513 | - * |
|
1514 | - * @param string $name |
|
1515 | - * @return EE_Addon |
|
1516 | - */ |
|
1517 | - public function get_addon_by_name($name) |
|
1518 | - { |
|
1519 | - foreach ($this->addons as $addon) { |
|
1520 | - if ($addon->name() === $name) { |
|
1521 | - return $addon; |
|
1522 | - } |
|
1523 | - } |
|
1524 | - return null; |
|
1525 | - } |
|
1526 | - |
|
1527 | - |
|
1528 | - /** |
|
1529 | - * Gets an array of all the registered addons, where the keys are their names. |
|
1530 | - * (ie, what each returns for their name() function) |
|
1531 | - * They're already available on EE_Registry::instance()->addons as properties, |
|
1532 | - * where each property's name is the addon's classname, |
|
1533 | - * So if you just want to get the addon by classname, |
|
1534 | - * OR use the get_addon() method above. |
|
1535 | - * PLEASE NOTE: |
|
1536 | - * addons with Fully Qualified Class Names |
|
1537 | - * have had the namespace separators converted to underscores, |
|
1538 | - * so a classname like Fully\Qualified\ClassName |
|
1539 | - * would have been converted to Fully_Qualified_ClassName |
|
1540 | - * |
|
1541 | - * @return EE_Addon[] where the KEYS are the addon's name() |
|
1542 | - */ |
|
1543 | - public function get_addons_by_name() |
|
1544 | - { |
|
1545 | - $addons = array(); |
|
1546 | - foreach ($this->addons as $addon) { |
|
1547 | - $addons[ $addon->name() ] = $addon; |
|
1548 | - } |
|
1549 | - return $addons; |
|
1550 | - } |
|
1551 | - |
|
1552 | - |
|
1553 | - /** |
|
1554 | - * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
1555 | - * a stale copy of it around |
|
1556 | - * |
|
1557 | - * @param string $model_name |
|
1558 | - * @return \EEM_Base |
|
1559 | - * @throws \EE_Error |
|
1560 | - */ |
|
1561 | - public function reset_model($model_name) |
|
1562 | - { |
|
1563 | - $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
1564 | - ? "EEM_{$model_name}" |
|
1565 | - : $model_name; |
|
1566 | - if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1567 | - return null; |
|
1568 | - } |
|
1569 | - // get that model reset it and make sure we nuke the old reference to it |
|
1570 | - if ( |
|
1571 | - $this->LIB->{$model_class_name} instanceof $model_class_name |
|
1572 | - && is_callable( |
|
1573 | - array($model_class_name, 'reset') |
|
1574 | - ) |
|
1575 | - ) { |
|
1576 | - $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
1577 | - } else { |
|
1578 | - throw new EE_Error( |
|
1579 | - sprintf( |
|
1580 | - esc_html__('Model %s does not have a method "reset"', 'event_espresso'), |
|
1581 | - $model_name |
|
1582 | - ) |
|
1583 | - ); |
|
1584 | - } |
|
1585 | - return $this->LIB->{$model_class_name}; |
|
1586 | - } |
|
1587 | - |
|
1588 | - |
|
1589 | - /** |
|
1590 | - * Resets the registry. |
|
1591 | - * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
1592 | - * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
1593 | - * - $_dependency_map |
|
1594 | - * - $_class_abbreviations |
|
1595 | - * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
1596 | - * - $REQ: Still on the same request so no need to change. |
|
1597 | - * - $CAP: There is no site specific state in the EE_Capability class. |
|
1598 | - * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
1599 | - * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
1600 | - * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
1601 | - * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
1602 | - * switch or on the restore. |
|
1603 | - * - $modules |
|
1604 | - * - $shortcodes |
|
1605 | - * - $widgets |
|
1606 | - * |
|
1607 | - * @param boolean $hard [deprecated] |
|
1608 | - * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
1609 | - * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
1610 | - * sure if you CAN currently reinstantiate the singletons at the moment) |
|
1611 | - * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
1612 | - * client |
|
1613 | - * code instead can just change the model context to a different blog id if |
|
1614 | - * necessary |
|
1615 | - * @return EE_Registry |
|
1616 | - * @throws InvalidInterfaceException |
|
1617 | - * @throws InvalidDataTypeException |
|
1618 | - * @throws EE_Error |
|
1619 | - * @throws ReflectionException |
|
1620 | - * @throws InvalidArgumentException |
|
1621 | - */ |
|
1622 | - public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
1623 | - { |
|
1624 | - $instance = self::instance(); |
|
1625 | - $instance->_cache_on = true; |
|
1626 | - // reset some "special" classes |
|
1627 | - EEH_Activation::reset(); |
|
1628 | - EEH_DTT_Helper::resetDefaultTimezoneString(); |
|
1629 | - |
|
1630 | - $hard = apply_filters('FHEE__EE_Registry__reset__hard', $hard); |
|
1631 | - $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
1632 | - $instance->CART = null; |
|
1633 | - $instance->MRM = null; |
|
1634 | - $instance->AssetsRegistry = LoaderFactory::getLoader()->getShared( |
|
1635 | - 'EventEspresso\core\services\assets\Registry' |
|
1636 | - ); |
|
1637 | - // messages reset |
|
1638 | - EED_Messages::reset(); |
|
1639 | - // handle of objects cached on LIB |
|
1640 | - foreach (array('LIB', 'modules') as $cache) { |
|
1641 | - foreach ($instance->{$cache} as $class_name => $class) { |
|
1642 | - if (self::_reset_and_unset_object($class, $reset_models)) { |
|
1643 | - unset($instance->{$cache}->{$class_name}); |
|
1644 | - } |
|
1645 | - } |
|
1646 | - } |
|
1647 | - return $instance; |
|
1648 | - } |
|
1649 | - |
|
1650 | - |
|
1651 | - /** |
|
1652 | - * if passed object implements ResettableInterface, then call it's reset() method |
|
1653 | - * if passed object implements InterminableInterface, then return false, |
|
1654 | - * to indicate that it should NOT be cleared from the Registry cache |
|
1655 | - * |
|
1656 | - * @param $object |
|
1657 | - * @param bool $reset_models |
|
1658 | - * @return bool returns true if cached object should be unset |
|
1659 | - */ |
|
1660 | - private static function _reset_and_unset_object($object, $reset_models) |
|
1661 | - { |
|
1662 | - if (! is_object($object)) { |
|
1663 | - // don't unset anything that's not an object |
|
1664 | - return false; |
|
1665 | - } |
|
1666 | - if ($object instanceof EED_Module) { |
|
1667 | - $object::reset(); |
|
1668 | - // don't unset modules |
|
1669 | - return false; |
|
1670 | - } |
|
1671 | - if ($object instanceof ResettableInterface) { |
|
1672 | - if ($object instanceof EEM_Base) { |
|
1673 | - if ($reset_models) { |
|
1674 | - $object->reset(); |
|
1675 | - return true; |
|
1676 | - } |
|
1677 | - return false; |
|
1678 | - } |
|
1679 | - $object->reset(); |
|
1680 | - return true; |
|
1681 | - } |
|
1682 | - if (! $object instanceof InterminableInterface) { |
|
1683 | - return true; |
|
1684 | - } |
|
1685 | - return false; |
|
1686 | - } |
|
1687 | - |
|
1688 | - |
|
1689 | - /** |
|
1690 | - * Gets all the custom post type models defined |
|
1691 | - * |
|
1692 | - * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
1693 | - */ |
|
1694 | - public function cpt_models() |
|
1695 | - { |
|
1696 | - $cpt_models = array(); |
|
1697 | - foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
1698 | - if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
1699 | - $cpt_models[ $short_name ] = $classname; |
|
1700 | - } |
|
1701 | - } |
|
1702 | - return $cpt_models; |
|
1703 | - } |
|
1704 | - |
|
1705 | - |
|
1706 | - /** |
|
1707 | - * @return \EE_Config |
|
1708 | - */ |
|
1709 | - public static function CFG() |
|
1710 | - { |
|
1711 | - return self::instance()->CFG; |
|
1712 | - } |
|
1713 | - |
|
1714 | - |
|
1715 | - /** |
|
1716 | - * @deprecated 4.9.62.p |
|
1717 | - * @param string $class_name |
|
1718 | - * @return ReflectionClass |
|
1719 | - * @throws ReflectionException |
|
1720 | - * @throws InvalidDataTypeException |
|
1721 | - */ |
|
1722 | - public function get_ReflectionClass($class_name) |
|
1723 | - { |
|
1724 | - return $this->mirror->getReflectionClass($class_name); |
|
1725 | - } |
|
1379 | + $param_class !== null |
|
1380 | + && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
1381 | + ) { |
|
1382 | + $arguments = $this->_resolve_dependency( |
|
1383 | + $class_name, |
|
1384 | + $param_class, |
|
1385 | + $arguments, |
|
1386 | + $index |
|
1387 | + ); |
|
1388 | + } |
|
1389 | + if (empty($arguments[ $index ])) { |
|
1390 | + $arguments[ $index ] = $this->mirror->getParameterDefaultValue( |
|
1391 | + $param, |
|
1392 | + $class_name, |
|
1393 | + $index |
|
1394 | + ); |
|
1395 | + } |
|
1396 | + } |
|
1397 | + return $arguments; |
|
1398 | + } |
|
1399 | + |
|
1400 | + |
|
1401 | + /** |
|
1402 | + * @param string $class_name |
|
1403 | + * @param string $param_class |
|
1404 | + * @param array $arguments |
|
1405 | + * @param mixed $index |
|
1406 | + * @return array |
|
1407 | + * @throws InvalidArgumentException |
|
1408 | + * @throws InvalidInterfaceException |
|
1409 | + * @throws InvalidDataTypeException |
|
1410 | + */ |
|
1411 | + protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
1412 | + { |
|
1413 | + $dependency = null; |
|
1414 | + // should dependency be loaded from cache ? |
|
1415 | + $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
1416 | + $class_name, |
|
1417 | + $param_class |
|
1418 | + ); |
|
1419 | + $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
1420 | + // we might have a dependency... |
|
1421 | + // let's MAYBE try and find it in our cache if that's what's been requested |
|
1422 | + $cached_class = $cache_on |
|
1423 | + ? $this->_get_cached_class($param_class) |
|
1424 | + : null; |
|
1425 | + // and grab it if it exists |
|
1426 | + if ($cached_class instanceof $param_class) { |
|
1427 | + $dependency = $cached_class; |
|
1428 | + } elseif ($param_class !== $class_name) { |
|
1429 | + // obtain the loader method from the dependency map |
|
1430 | + $loader = $this->_dependency_map->class_loader($param_class); |
|
1431 | + // is loader a custom closure ? |
|
1432 | + if ($loader instanceof Closure) { |
|
1433 | + $dependency = $loader($arguments); |
|
1434 | + } else { |
|
1435 | + // set the cache on property for the recursive loading call |
|
1436 | + $this->_cache_on = $cache_on; |
|
1437 | + // if not, then let's try and load it via the registry |
|
1438 | + if ($loader && method_exists($this, $loader)) { |
|
1439 | + $dependency = $this->{$loader}($param_class); |
|
1440 | + } else { |
|
1441 | + $dependency = LoaderFactory::getLoader()->load( |
|
1442 | + $param_class, |
|
1443 | + array(), |
|
1444 | + $cache_on |
|
1445 | + ); |
|
1446 | + } |
|
1447 | + } |
|
1448 | + } |
|
1449 | + // did we successfully find the correct dependency ? |
|
1450 | + if ($dependency instanceof $param_class) { |
|
1451 | + // then let's inject it into the incoming array of arguments at the correct location |
|
1452 | + $arguments[ $index ] = $dependency; |
|
1453 | + } |
|
1454 | + return $arguments; |
|
1455 | + } |
|
1456 | + |
|
1457 | + |
|
1458 | + /** |
|
1459 | + * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
1460 | + * |
|
1461 | + * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
1462 | + * in the EE_Dependency_Map::$_class_loaders array, |
|
1463 | + * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
1464 | + * @param array $arguments |
|
1465 | + * @return object |
|
1466 | + */ |
|
1467 | + public static function factory($classname, $arguments = array()) |
|
1468 | + { |
|
1469 | + $loader = self::instance()->_dependency_map->class_loader($classname); |
|
1470 | + if ($loader instanceof Closure) { |
|
1471 | + return $loader($arguments); |
|
1472 | + } |
|
1473 | + if (method_exists(self::instance(), $loader)) { |
|
1474 | + return self::instance()->{$loader}($classname, $arguments); |
|
1475 | + } |
|
1476 | + return null; |
|
1477 | + } |
|
1478 | + |
|
1479 | + |
|
1480 | + /** |
|
1481 | + * Gets the addon by its class name |
|
1482 | + * |
|
1483 | + * @param string $class_name |
|
1484 | + * @return EE_Addon |
|
1485 | + */ |
|
1486 | + public function getAddon($class_name) |
|
1487 | + { |
|
1488 | + $class_name = str_replace('\\', '_', $class_name); |
|
1489 | + if (isset($this->addons->{$class_name})) { |
|
1490 | + return $this->addons->{$class_name}; |
|
1491 | + } else { |
|
1492 | + return null; |
|
1493 | + } |
|
1494 | + } |
|
1495 | + |
|
1496 | + |
|
1497 | + /** |
|
1498 | + * removes the addon from the internal cache |
|
1499 | + * |
|
1500 | + * @param string $class_name |
|
1501 | + * @return void |
|
1502 | + */ |
|
1503 | + public function removeAddon($class_name) |
|
1504 | + { |
|
1505 | + $class_name = str_replace('\\', '_', $class_name); |
|
1506 | + unset($this->addons->{$class_name}); |
|
1507 | + } |
|
1508 | + |
|
1509 | + |
|
1510 | + /** |
|
1511 | + * Gets the addon by its name/slug (not classname. For that, just |
|
1512 | + * use the get_addon() method above |
|
1513 | + * |
|
1514 | + * @param string $name |
|
1515 | + * @return EE_Addon |
|
1516 | + */ |
|
1517 | + public function get_addon_by_name($name) |
|
1518 | + { |
|
1519 | + foreach ($this->addons as $addon) { |
|
1520 | + if ($addon->name() === $name) { |
|
1521 | + return $addon; |
|
1522 | + } |
|
1523 | + } |
|
1524 | + return null; |
|
1525 | + } |
|
1526 | + |
|
1527 | + |
|
1528 | + /** |
|
1529 | + * Gets an array of all the registered addons, where the keys are their names. |
|
1530 | + * (ie, what each returns for their name() function) |
|
1531 | + * They're already available on EE_Registry::instance()->addons as properties, |
|
1532 | + * where each property's name is the addon's classname, |
|
1533 | + * So if you just want to get the addon by classname, |
|
1534 | + * OR use the get_addon() method above. |
|
1535 | + * PLEASE NOTE: |
|
1536 | + * addons with Fully Qualified Class Names |
|
1537 | + * have had the namespace separators converted to underscores, |
|
1538 | + * so a classname like Fully\Qualified\ClassName |
|
1539 | + * would have been converted to Fully_Qualified_ClassName |
|
1540 | + * |
|
1541 | + * @return EE_Addon[] where the KEYS are the addon's name() |
|
1542 | + */ |
|
1543 | + public function get_addons_by_name() |
|
1544 | + { |
|
1545 | + $addons = array(); |
|
1546 | + foreach ($this->addons as $addon) { |
|
1547 | + $addons[ $addon->name() ] = $addon; |
|
1548 | + } |
|
1549 | + return $addons; |
|
1550 | + } |
|
1551 | + |
|
1552 | + |
|
1553 | + /** |
|
1554 | + * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
1555 | + * a stale copy of it around |
|
1556 | + * |
|
1557 | + * @param string $model_name |
|
1558 | + * @return \EEM_Base |
|
1559 | + * @throws \EE_Error |
|
1560 | + */ |
|
1561 | + public function reset_model($model_name) |
|
1562 | + { |
|
1563 | + $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
1564 | + ? "EEM_{$model_name}" |
|
1565 | + : $model_name; |
|
1566 | + if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1567 | + return null; |
|
1568 | + } |
|
1569 | + // get that model reset it and make sure we nuke the old reference to it |
|
1570 | + if ( |
|
1571 | + $this->LIB->{$model_class_name} instanceof $model_class_name |
|
1572 | + && is_callable( |
|
1573 | + array($model_class_name, 'reset') |
|
1574 | + ) |
|
1575 | + ) { |
|
1576 | + $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
1577 | + } else { |
|
1578 | + throw new EE_Error( |
|
1579 | + sprintf( |
|
1580 | + esc_html__('Model %s does not have a method "reset"', 'event_espresso'), |
|
1581 | + $model_name |
|
1582 | + ) |
|
1583 | + ); |
|
1584 | + } |
|
1585 | + return $this->LIB->{$model_class_name}; |
|
1586 | + } |
|
1587 | + |
|
1588 | + |
|
1589 | + /** |
|
1590 | + * Resets the registry. |
|
1591 | + * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
1592 | + * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
1593 | + * - $_dependency_map |
|
1594 | + * - $_class_abbreviations |
|
1595 | + * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
1596 | + * - $REQ: Still on the same request so no need to change. |
|
1597 | + * - $CAP: There is no site specific state in the EE_Capability class. |
|
1598 | + * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
1599 | + * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
1600 | + * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
1601 | + * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
1602 | + * switch or on the restore. |
|
1603 | + * - $modules |
|
1604 | + * - $shortcodes |
|
1605 | + * - $widgets |
|
1606 | + * |
|
1607 | + * @param boolean $hard [deprecated] |
|
1608 | + * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
1609 | + * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
1610 | + * sure if you CAN currently reinstantiate the singletons at the moment) |
|
1611 | + * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
1612 | + * client |
|
1613 | + * code instead can just change the model context to a different blog id if |
|
1614 | + * necessary |
|
1615 | + * @return EE_Registry |
|
1616 | + * @throws InvalidInterfaceException |
|
1617 | + * @throws InvalidDataTypeException |
|
1618 | + * @throws EE_Error |
|
1619 | + * @throws ReflectionException |
|
1620 | + * @throws InvalidArgumentException |
|
1621 | + */ |
|
1622 | + public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
1623 | + { |
|
1624 | + $instance = self::instance(); |
|
1625 | + $instance->_cache_on = true; |
|
1626 | + // reset some "special" classes |
|
1627 | + EEH_Activation::reset(); |
|
1628 | + EEH_DTT_Helper::resetDefaultTimezoneString(); |
|
1629 | + |
|
1630 | + $hard = apply_filters('FHEE__EE_Registry__reset__hard', $hard); |
|
1631 | + $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
1632 | + $instance->CART = null; |
|
1633 | + $instance->MRM = null; |
|
1634 | + $instance->AssetsRegistry = LoaderFactory::getLoader()->getShared( |
|
1635 | + 'EventEspresso\core\services\assets\Registry' |
|
1636 | + ); |
|
1637 | + // messages reset |
|
1638 | + EED_Messages::reset(); |
|
1639 | + // handle of objects cached on LIB |
|
1640 | + foreach (array('LIB', 'modules') as $cache) { |
|
1641 | + foreach ($instance->{$cache} as $class_name => $class) { |
|
1642 | + if (self::_reset_and_unset_object($class, $reset_models)) { |
|
1643 | + unset($instance->{$cache}->{$class_name}); |
|
1644 | + } |
|
1645 | + } |
|
1646 | + } |
|
1647 | + return $instance; |
|
1648 | + } |
|
1649 | + |
|
1650 | + |
|
1651 | + /** |
|
1652 | + * if passed object implements ResettableInterface, then call it's reset() method |
|
1653 | + * if passed object implements InterminableInterface, then return false, |
|
1654 | + * to indicate that it should NOT be cleared from the Registry cache |
|
1655 | + * |
|
1656 | + * @param $object |
|
1657 | + * @param bool $reset_models |
|
1658 | + * @return bool returns true if cached object should be unset |
|
1659 | + */ |
|
1660 | + private static function _reset_and_unset_object($object, $reset_models) |
|
1661 | + { |
|
1662 | + if (! is_object($object)) { |
|
1663 | + // don't unset anything that's not an object |
|
1664 | + return false; |
|
1665 | + } |
|
1666 | + if ($object instanceof EED_Module) { |
|
1667 | + $object::reset(); |
|
1668 | + // don't unset modules |
|
1669 | + return false; |
|
1670 | + } |
|
1671 | + if ($object instanceof ResettableInterface) { |
|
1672 | + if ($object instanceof EEM_Base) { |
|
1673 | + if ($reset_models) { |
|
1674 | + $object->reset(); |
|
1675 | + return true; |
|
1676 | + } |
|
1677 | + return false; |
|
1678 | + } |
|
1679 | + $object->reset(); |
|
1680 | + return true; |
|
1681 | + } |
|
1682 | + if (! $object instanceof InterminableInterface) { |
|
1683 | + return true; |
|
1684 | + } |
|
1685 | + return false; |
|
1686 | + } |
|
1687 | + |
|
1688 | + |
|
1689 | + /** |
|
1690 | + * Gets all the custom post type models defined |
|
1691 | + * |
|
1692 | + * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
1693 | + */ |
|
1694 | + public function cpt_models() |
|
1695 | + { |
|
1696 | + $cpt_models = array(); |
|
1697 | + foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
1698 | + if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
1699 | + $cpt_models[ $short_name ] = $classname; |
|
1700 | + } |
|
1701 | + } |
|
1702 | + return $cpt_models; |
|
1703 | + } |
|
1704 | + |
|
1705 | + |
|
1706 | + /** |
|
1707 | + * @return \EE_Config |
|
1708 | + */ |
|
1709 | + public static function CFG() |
|
1710 | + { |
|
1711 | + return self::instance()->CFG; |
|
1712 | + } |
|
1713 | + |
|
1714 | + |
|
1715 | + /** |
|
1716 | + * @deprecated 4.9.62.p |
|
1717 | + * @param string $class_name |
|
1718 | + * @return ReflectionClass |
|
1719 | + * @throws ReflectionException |
|
1720 | + * @throws InvalidDataTypeException |
|
1721 | + */ |
|
1722 | + public function get_ReflectionClass($class_name) |
|
1723 | + { |
|
1724 | + return $this->mirror->getReflectionClass($class_name); |
|
1725 | + } |
|
1726 | 1726 | } |