@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | */ |
18 | 18 | public function __construct($validation_error_message = null) |
19 | 19 | { |
20 | - if (! $validation_error_message) { |
|
20 | + if ( ! $validation_error_message) { |
|
21 | 21 | $validation_error_message = sprintf(esc_html__("Only numeric characters, commas, periods, and spaces, please!", "event_espresso")); |
22 | 22 | } |
23 | 23 | parent::__construct($validation_error_message); |
@@ -42,6 +42,6 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public function get_jquery_validation_rule_array() |
44 | 44 | { |
45 | - return array('number' => true, 'messages' => array( 'number' => $this->get_validation_error_message() ) ); |
|
45 | + return array('number' => true, 'messages' => array('number' => $this->get_validation_error_message())); |
|
46 | 46 | } |
47 | 47 | } |
@@ -11,36 +11,36 @@ |
||
11 | 11 | */ |
12 | 12 | class EE_Float_Validation_Strategy extends EE_Validation_Strategy_Base |
13 | 13 | { |
14 | - /** |
|
15 | - * @param null $validation_error_message |
|
16 | - */ |
|
17 | - public function __construct($validation_error_message = null) |
|
18 | - { |
|
19 | - if (! $validation_error_message) { |
|
20 | - $validation_error_message = sprintf(esc_html__("Only numeric characters, commas, periods, and spaces, please!", "event_espresso")); |
|
21 | - } |
|
22 | - parent::__construct($validation_error_message); |
|
23 | - } |
|
14 | + /** |
|
15 | + * @param null $validation_error_message |
|
16 | + */ |
|
17 | + public function __construct($validation_error_message = null) |
|
18 | + { |
|
19 | + if (! $validation_error_message) { |
|
20 | + $validation_error_message = sprintf(esc_html__("Only numeric characters, commas, periods, and spaces, please!", "event_espresso")); |
|
21 | + } |
|
22 | + parent::__construct($validation_error_message); |
|
23 | + } |
|
24 | 24 | |
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * @param $normalized_value |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function validate($normalized_value) |
|
33 | - { |
|
34 | - // errors should have been detected by the normalization strategy |
|
35 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * @param $normalized_value |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function validate($normalized_value) |
|
33 | + { |
|
34 | + // errors should have been detected by the normalization strategy |
|
35 | + } |
|
36 | 36 | |
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * @return array |
|
41 | - */ |
|
42 | - public function get_jquery_validation_rule_array() |
|
43 | - { |
|
44 | - return array('number' => true, 'messages' => array( 'number' => $this->get_validation_error_message() ) ); |
|
45 | - } |
|
39 | + /** |
|
40 | + * @return array |
|
41 | + */ |
|
42 | + public function get_jquery_validation_rule_array() |
|
43 | + { |
|
44 | + return array('number' => true, 'messages' => array( 'number' => $this->get_validation_error_message() ) ); |
|
45 | + } |
|
46 | 46 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | if (is_float($value_to_normalize) || is_int($value_to_normalize)) { |
34 | 34 | return (float) $value_to_normalize; |
35 | 35 | } |
36 | - if (! is_string($value_to_normalize)) { |
|
36 | + if ( ! is_string($value_to_normalize)) { |
|
37 | 37 | throw new EE_Validation_Error( |
38 | 38 | sprintf( |
39 | 39 | esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | } |
70 | 70 | // this really shouldn't ever happen because fields with a float normalization strategy |
71 | 71 | // should also have a float validation strategy, but in case it doesn't use the default |
72 | - if (! $validation_error_message) { |
|
72 | + if ( ! $validation_error_message) { |
|
73 | 73 | $default_validation_strategy = new EE_Float_Validation_Strategy(); |
74 | 74 | $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
75 | 75 | } |
@@ -10,84 +10,84 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Float_Normalization extends EE_Normalization_Strategy_Base |
12 | 12 | { |
13 | - /* |
|
13 | + /* |
|
14 | 14 | * regex pattern that matches for the following: |
15 | 15 | * * optional negative sign |
16 | 16 | * * one or more digits or decimals |
17 | 17 | */ |
18 | - const REGEX = '/^(-?)([\d.]+)$/'; |
|
18 | + const REGEX = '/^(-?)([\d.]+)$/'; |
|
19 | 19 | |
20 | 20 | |
21 | 21 | |
22 | - /** |
|
23 | - * @param string $value_to_normalize |
|
24 | - * @return float |
|
25 | - * @throws \EE_Validation_Error |
|
26 | - */ |
|
27 | - public function normalize($value_to_normalize) |
|
28 | - { |
|
29 | - if ($value_to_normalize === null) { |
|
30 | - return null; |
|
31 | - } |
|
32 | - if (is_float($value_to_normalize) || is_int($value_to_normalize)) { |
|
33 | - return (float) $value_to_normalize; |
|
34 | - } |
|
35 | - if (! is_string($value_to_normalize)) { |
|
36 | - throw new EE_Validation_Error( |
|
37 | - sprintf( |
|
38 | - esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
|
39 | - print_r($value_to_normalize, true), |
|
40 | - gettype($value_to_normalize) |
|
41 | - ) |
|
42 | - ); |
|
43 | - } |
|
44 | - $normalized_value = filter_var( |
|
45 | - $value_to_normalize, |
|
46 | - FILTER_SANITIZE_NUMBER_FLOAT, |
|
47 | - FILTER_FLAG_ALLOW_FRACTION |
|
48 | - ); |
|
49 | - if ($normalized_value === '') { |
|
50 | - return null; |
|
51 | - } |
|
52 | - if (preg_match(EE_Float_Normalization::REGEX, $normalized_value, $matches)) { |
|
53 | - if (count($matches) === 3) { |
|
54 | - // if first match is the negative sign, |
|
55 | - // then the number needs to be multiplied by -1 to remain negative |
|
56 | - return $matches[1] === '-' |
|
57 | - ? (float) $matches[2] * -1 |
|
58 | - : (float) $matches[2]; |
|
59 | - } |
|
60 | - } |
|
61 | - // find if this input has a float validation strategy |
|
62 | - // in which case, use its message |
|
63 | - $validation_error_message = null; |
|
64 | - foreach ($this->_input->get_validation_strategies() as $validation_strategy) { |
|
65 | - if ($validation_strategy instanceof EE_Float_Validation_Strategy) { |
|
66 | - $validation_error_message = $validation_strategy->get_validation_error_message(); |
|
67 | - } |
|
68 | - } |
|
69 | - // this really shouldn't ever happen because fields with a float normalization strategy |
|
70 | - // should also have a float validation strategy, but in case it doesn't use the default |
|
71 | - if (! $validation_error_message) { |
|
72 | - $default_validation_strategy = new EE_Float_Validation_Strategy(); |
|
73 | - $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
|
74 | - } |
|
75 | - throw new EE_Validation_Error($validation_error_message, 'float_only'); |
|
76 | - } |
|
22 | + /** |
|
23 | + * @param string $value_to_normalize |
|
24 | + * @return float |
|
25 | + * @throws \EE_Validation_Error |
|
26 | + */ |
|
27 | + public function normalize($value_to_normalize) |
|
28 | + { |
|
29 | + if ($value_to_normalize === null) { |
|
30 | + return null; |
|
31 | + } |
|
32 | + if (is_float($value_to_normalize) || is_int($value_to_normalize)) { |
|
33 | + return (float) $value_to_normalize; |
|
34 | + } |
|
35 | + if (! is_string($value_to_normalize)) { |
|
36 | + throw new EE_Validation_Error( |
|
37 | + sprintf( |
|
38 | + esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
|
39 | + print_r($value_to_normalize, true), |
|
40 | + gettype($value_to_normalize) |
|
41 | + ) |
|
42 | + ); |
|
43 | + } |
|
44 | + $normalized_value = filter_var( |
|
45 | + $value_to_normalize, |
|
46 | + FILTER_SANITIZE_NUMBER_FLOAT, |
|
47 | + FILTER_FLAG_ALLOW_FRACTION |
|
48 | + ); |
|
49 | + if ($normalized_value === '') { |
|
50 | + return null; |
|
51 | + } |
|
52 | + if (preg_match(EE_Float_Normalization::REGEX, $normalized_value, $matches)) { |
|
53 | + if (count($matches) === 3) { |
|
54 | + // if first match is the negative sign, |
|
55 | + // then the number needs to be multiplied by -1 to remain negative |
|
56 | + return $matches[1] === '-' |
|
57 | + ? (float) $matches[2] * -1 |
|
58 | + : (float) $matches[2]; |
|
59 | + } |
|
60 | + } |
|
61 | + // find if this input has a float validation strategy |
|
62 | + // in which case, use its message |
|
63 | + $validation_error_message = null; |
|
64 | + foreach ($this->_input->get_validation_strategies() as $validation_strategy) { |
|
65 | + if ($validation_strategy instanceof EE_Float_Validation_Strategy) { |
|
66 | + $validation_error_message = $validation_strategy->get_validation_error_message(); |
|
67 | + } |
|
68 | + } |
|
69 | + // this really shouldn't ever happen because fields with a float normalization strategy |
|
70 | + // should also have a float validation strategy, but in case it doesn't use the default |
|
71 | + if (! $validation_error_message) { |
|
72 | + $default_validation_strategy = new EE_Float_Validation_Strategy(); |
|
73 | + $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
|
74 | + } |
|
75 | + throw new EE_Validation_Error($validation_error_message, 'float_only'); |
|
76 | + } |
|
77 | 77 | |
78 | 78 | |
79 | 79 | |
80 | - /** |
|
81 | - * Converts a float into a string |
|
82 | - * |
|
83 | - * @param float $normalized_value |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public function unnormalize($normalized_value) |
|
87 | - { |
|
88 | - if (empty($normalized_value)) { |
|
89 | - return '0.00'; |
|
90 | - } |
|
91 | - return "{$normalized_value}"; |
|
92 | - } |
|
80 | + /** |
|
81 | + * Converts a float into a string |
|
82 | + * |
|
83 | + * @param float $normalized_value |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public function unnormalize($normalized_value) |
|
87 | + { |
|
88 | + if (empty($normalized_value)) { |
|
89 | + return '0.00'; |
|
90 | + } |
|
91 | + return "{$normalized_value}"; |
|
92 | + } |
|
93 | 93 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | if (is_int($value_to_normalize) || is_float($value_to_normalize)) { |
34 | 34 | return (int) $value_to_normalize; |
35 | 35 | } |
36 | - if (! is_string($value_to_normalize)) { |
|
36 | + if ( ! is_string($value_to_normalize)) { |
|
37 | 37 | throw new EE_Validation_Error( |
38 | 38 | sprintf( |
39 | 39 | esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | } |
71 | 71 | // this really shouldn't ever happen because fields with a int normalization strategy |
72 | 72 | // should also have a int validation strategy, but in case it doesn't use the default |
73 | - if (! $validation_error_message) { |
|
73 | + if ( ! $validation_error_message) { |
|
74 | 74 | $default_validation_strategy = new EE_Int_Validation_Strategy(); |
75 | 75 | $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
76 | 76 | } |
@@ -10,86 +10,86 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Int_Normalization extends EE_Normalization_Strategy_Base |
12 | 12 | { |
13 | - /* |
|
13 | + /* |
|
14 | 14 | * regex pattern that matches for the following: |
15 | 15 | * * optional negative sign |
16 | 16 | * * one or more digits |
17 | 17 | */ |
18 | - const REGEX = '/^(-?)(\d+)(?:\.0+)?$/'; |
|
18 | + const REGEX = '/^(-?)(\d+)(?:\.0+)?$/'; |
|
19 | 19 | |
20 | 20 | |
21 | 21 | |
22 | - /** |
|
23 | - * @param string $value_to_normalize |
|
24 | - * @return int|null |
|
25 | - * @throws EE_Validation_Error |
|
26 | - */ |
|
27 | - public function normalize($value_to_normalize) |
|
28 | - { |
|
29 | - if ($value_to_normalize === null) { |
|
30 | - return null; |
|
31 | - } |
|
32 | - if (is_int($value_to_normalize) || is_float($value_to_normalize)) { |
|
33 | - return (int) $value_to_normalize; |
|
34 | - } |
|
35 | - if (! is_string($value_to_normalize)) { |
|
36 | - throw new EE_Validation_Error( |
|
37 | - sprintf( |
|
38 | - esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
|
39 | - print_r($value_to_normalize, true), |
|
40 | - gettype($value_to_normalize) |
|
41 | - ) |
|
42 | - ); |
|
43 | - } |
|
44 | - $value_to_normalize = filter_var( |
|
45 | - $value_to_normalize, |
|
46 | - FILTER_SANITIZE_NUMBER_FLOAT, |
|
47 | - FILTER_FLAG_ALLOW_FRACTION |
|
48 | - ); |
|
49 | - if ($value_to_normalize === '') { |
|
50 | - return null; |
|
51 | - } |
|
52 | - $matches = array(); |
|
53 | - if (preg_match(EE_Int_Normalization::REGEX, $value_to_normalize, $matches)) { |
|
54 | - if (count($matches) === 3) { |
|
55 | - // if first match is the negative sign, |
|
56 | - // then the number needs to be multiplied by -1 to remain negative |
|
57 | - return $matches[1] === '-' ? (int) $matches[2] * -1 : (int) $matches[2]; |
|
58 | - } |
|
59 | - } |
|
60 | - // find if this input has a int validation strategy |
|
61 | - // in which case, use its message |
|
62 | - $validation_error_message = null; |
|
63 | - foreach ($this->_input->get_validation_strategies() as $validation_strategy) { |
|
64 | - if ($validation_strategy instanceof EE_Int_Validation_Strategy) { |
|
65 | - $validation_error_message = $validation_strategy->get_validation_error_message(); |
|
66 | - } |
|
67 | - } |
|
68 | - // this really shouldn't ever happen because fields with a int normalization strategy |
|
69 | - // should also have a int validation strategy, but in case it doesn't use the default |
|
70 | - if (! $validation_error_message) { |
|
71 | - $default_validation_strategy = new EE_Int_Validation_Strategy(); |
|
72 | - $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
|
73 | - } |
|
74 | - throw new EE_Validation_Error($validation_error_message, 'numeric_only'); |
|
75 | - } |
|
22 | + /** |
|
23 | + * @param string $value_to_normalize |
|
24 | + * @return int|null |
|
25 | + * @throws EE_Validation_Error |
|
26 | + */ |
|
27 | + public function normalize($value_to_normalize) |
|
28 | + { |
|
29 | + if ($value_to_normalize === null) { |
|
30 | + return null; |
|
31 | + } |
|
32 | + if (is_int($value_to_normalize) || is_float($value_to_normalize)) { |
|
33 | + return (int) $value_to_normalize; |
|
34 | + } |
|
35 | + if (! is_string($value_to_normalize)) { |
|
36 | + throw new EE_Validation_Error( |
|
37 | + sprintf( |
|
38 | + esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
|
39 | + print_r($value_to_normalize, true), |
|
40 | + gettype($value_to_normalize) |
|
41 | + ) |
|
42 | + ); |
|
43 | + } |
|
44 | + $value_to_normalize = filter_var( |
|
45 | + $value_to_normalize, |
|
46 | + FILTER_SANITIZE_NUMBER_FLOAT, |
|
47 | + FILTER_FLAG_ALLOW_FRACTION |
|
48 | + ); |
|
49 | + if ($value_to_normalize === '') { |
|
50 | + return null; |
|
51 | + } |
|
52 | + $matches = array(); |
|
53 | + if (preg_match(EE_Int_Normalization::REGEX, $value_to_normalize, $matches)) { |
|
54 | + if (count($matches) === 3) { |
|
55 | + // if first match is the negative sign, |
|
56 | + // then the number needs to be multiplied by -1 to remain negative |
|
57 | + return $matches[1] === '-' ? (int) $matches[2] * -1 : (int) $matches[2]; |
|
58 | + } |
|
59 | + } |
|
60 | + // find if this input has a int validation strategy |
|
61 | + // in which case, use its message |
|
62 | + $validation_error_message = null; |
|
63 | + foreach ($this->_input->get_validation_strategies() as $validation_strategy) { |
|
64 | + if ($validation_strategy instanceof EE_Int_Validation_Strategy) { |
|
65 | + $validation_error_message = $validation_strategy->get_validation_error_message(); |
|
66 | + } |
|
67 | + } |
|
68 | + // this really shouldn't ever happen because fields with a int normalization strategy |
|
69 | + // should also have a int validation strategy, but in case it doesn't use the default |
|
70 | + if (! $validation_error_message) { |
|
71 | + $default_validation_strategy = new EE_Int_Validation_Strategy(); |
|
72 | + $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
|
73 | + } |
|
74 | + throw new EE_Validation_Error($validation_error_message, 'numeric_only'); |
|
75 | + } |
|
76 | 76 | |
77 | 77 | |
78 | 78 | |
79 | - /** |
|
80 | - * Converts the int into a string for use in teh html form |
|
81 | - * |
|
82 | - * @param int $normalized_value |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function unnormalize($normalized_value) |
|
86 | - { |
|
87 | - if ($normalized_value === null || $normalized_value === '') { |
|
88 | - return ''; |
|
89 | - } |
|
90 | - if (empty($normalized_value)) { |
|
91 | - return '0'; |
|
92 | - } |
|
93 | - return "$normalized_value"; |
|
94 | - } |
|
79 | + /** |
|
80 | + * Converts the int into a string for use in teh html form |
|
81 | + * |
|
82 | + * @param int $normalized_value |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function unnormalize($normalized_value) |
|
86 | + { |
|
87 | + if ($normalized_value === null || $normalized_value === '') { |
|
88 | + return ''; |
|
89 | + } |
|
90 | + if (empty($normalized_value)) { |
|
91 | + return '0'; |
|
92 | + } |
|
93 | + return "$normalized_value"; |
|
94 | + } |
|
95 | 95 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | ); |
75 | 75 | // make sure limit and caps are always set |
76 | 76 | $query_params = array_merge( |
77 | - array( 'limit' => 10, 'caps' => EEM_Base::caps_read_admin ), |
|
77 | + array('limit' => 10, 'caps' => EEM_Base::caps_read_admin), |
|
78 | 78 | $query_params |
79 | 79 | ); |
80 | 80 | $this->_value_field_name = EEH_Array::is_set( |
@@ -155,12 +155,12 @@ discard block |
||
155 | 155 | $values_for_options = (array) $value; |
156 | 156 | $value_field = $this->_get_model()->field_settings_for($this->_value_field_name); |
157 | 157 | $display_field = $this->_get_model()->field_settings_for($this->_display_field_name); |
158 | - $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name; |
|
159 | - $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name; |
|
158 | + $this->_extra_select_columns[] = $value_field->get_qualified_column().' AS '.$this->_value_field_name; |
|
159 | + $this->_extra_select_columns[] = $display_field->get_qualified_column().' AS '.$this->_display_field_name; |
|
160 | 160 | $display_values = $this->_get_model()->get_all_wpdb_results( |
161 | 161 | array( |
162 | 162 | array( |
163 | - $this->_value_field_name => array( 'IN', $values_for_options ) |
|
163 | + $this->_value_field_name => array('IN', $values_for_options) |
|
164 | 164 | ) |
165 | 165 | ), |
166 | 166 | ARRAY_A, |
@@ -170,9 +170,9 @@ discard block |
||
170 | 170 | if (is_array($select_options)) { |
171 | 171 | foreach ($display_values as $db_rows) { |
172 | 172 | $db_rows = (array) $db_rows; |
173 | - $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters( |
|
173 | + $select_options[$db_rows[$this->_value_field_name]] = apply_filters( |
|
174 | 174 | 'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value', |
175 | - $db_rows[ $this->_display_field_name ], |
|
175 | + $db_rows[$this->_display_field_name], |
|
176 | 176 | $db_rows |
177 | 177 | ); |
178 | 178 | } |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | */ |
189 | 189 | protected function _get_model() |
190 | 190 | { |
191 | - if (! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
191 | + if ( ! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
192 | 192 | throw new EE_Error( |
193 | 193 | sprintf( |
194 | 194 | esc_html__( |
@@ -16,189 +16,189 @@ |
||
16 | 16 | */ |
17 | 17 | class EE_Select_Ajax_Model_Rest_Input extends EE_Form_Input_With_Options_Base |
18 | 18 | { |
19 | - /** |
|
20 | - * @var string $_model_name |
|
21 | - */ |
|
22 | - protected $_model_name; |
|
19 | + /** |
|
20 | + * @var string $_model_name |
|
21 | + */ |
|
22 | + protected $_model_name; |
|
23 | 23 | |
24 | - /** |
|
25 | - * @var string $_display_field_name |
|
26 | - */ |
|
27 | - protected $_display_field_name; |
|
24 | + /** |
|
25 | + * @var string $_display_field_name |
|
26 | + */ |
|
27 | + protected $_display_field_name; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var string $_value_field_name |
|
31 | - */ |
|
32 | - protected $_value_field_name; |
|
29 | + /** |
|
30 | + * @var string $_value_field_name |
|
31 | + */ |
|
32 | + protected $_value_field_name; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var array $_extra_select_columns |
|
36 | - */ |
|
37 | - protected $_extra_select_columns = array(); |
|
34 | + /** |
|
35 | + * @var array $_extra_select_columns |
|
36 | + */ |
|
37 | + protected $_extra_select_columns = array(); |
|
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * @param array $input_settings { |
|
42 | - * @type string $model_name the name of model to be used for searching, both via the REST API and server-side model queries |
|
43 | - * @type array $query_params default query parameters which will apply to both REST API queries and server-side queries. This should be |
|
44 | - * in the exact format that will be used for server-side model usage (eg use index 0 for where conditions, not |
|
45 | - * the string "where") |
|
46 | - * @type string $value_field_name the name of the model field on this model to |
|
47 | - * be used for the HTML select's option's values |
|
48 | - * @type string $display_field_name the name of the model field on this model |
|
49 | - * to be used for the HTML select's option's display text |
|
50 | - * @type array $select2_args arguments to be passed directly into the select2's JS constructor |
|
51 | - * } |
|
52 | - * And the arguments accepted by EE_Form_Input_With_Options_Base |
|
53 | - * } |
|
54 | - * @throws EE_Error |
|
55 | - * @throws InvalidArgumentException |
|
56 | - * @throws InvalidDataTypeException |
|
57 | - * @throws InvalidInterfaceException |
|
58 | - */ |
|
59 | - public function __construct($input_settings = array()) |
|
60 | - { |
|
61 | - // needed input settings: |
|
62 | - // select2_args |
|
63 | - $this->_model_name = EEH_Array::is_set( |
|
64 | - $input_settings, |
|
65 | - 'model_name', |
|
66 | - null |
|
67 | - ); |
|
68 | - $model = $this->_get_model(); |
|
69 | - $query_params = EEH_Array::is_set( |
|
70 | - $input_settings, |
|
71 | - 'query_params', |
|
72 | - array() |
|
73 | - ); |
|
74 | - // make sure limit and caps are always set |
|
75 | - $query_params = array_merge( |
|
76 | - array( 'limit' => 10, 'caps' => EEM_Base::caps_read_admin ), |
|
77 | - $query_params |
|
78 | - ); |
|
79 | - $this->_value_field_name = EEH_Array::is_set( |
|
80 | - $input_settings, |
|
81 | - 'value_field_name', |
|
82 | - $model->primary_key_name() |
|
83 | - ); |
|
84 | - $this->_display_field_name = EEH_Array::is_set( |
|
85 | - $input_settings, |
|
86 | - 'display_field_name', |
|
87 | - $model->get_a_field_of_type('EE_Text_Field_Base')->get_name() |
|
88 | - ); |
|
89 | - $this->_extra_select_columns = EEH_Array::is_set( |
|
90 | - $input_settings, |
|
91 | - 'extra_select_columns', |
|
92 | - array() |
|
93 | - ); |
|
94 | - $this->_add_validation_strategy( |
|
95 | - new EE_Model_Matching_Query_Validation_Strategy( |
|
96 | - '', |
|
97 | - $this->_model_name, |
|
98 | - $query_params, |
|
99 | - $this->_value_field_name |
|
100 | - ) |
|
101 | - ); |
|
102 | - // get resource endpoint |
|
103 | - $rest_controller = LoaderFactory::getLoader()->getNew( |
|
104 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read' |
|
105 | - ); |
|
106 | - $rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version()); |
|
107 | - $default_select2_args = array( |
|
108 | - 'ajax' => array( |
|
109 | - 'url' => $rest_controller->getVersionedLinkTo( |
|
110 | - EEH_Inflector::pluralize_and_lower($this->_model_name) |
|
111 | - ), |
|
112 | - 'dataType' => 'json', |
|
113 | - 'delay' => '250', |
|
114 | - 'data_interface' => 'EE_Select2_REST_API_Interface', |
|
115 | - 'data_interface_args' => array( |
|
116 | - 'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi( |
|
117 | - $query_params, |
|
118 | - $model |
|
119 | - ), |
|
120 | - 'display_field' => $this->_display_field_name, |
|
121 | - 'value_field' => $this->_value_field_name, |
|
122 | - 'nonce' => wp_create_nonce('wp_rest'), |
|
123 | - 'locale' => str_replace('_', '-', strtolower(get_locale())) |
|
124 | - ), |
|
125 | - ), |
|
126 | - 'cache' => true, |
|
127 | - 'width' => 'resolve' |
|
128 | - ); |
|
129 | - $select2_args = array_replace_recursive( |
|
130 | - $default_select2_args, |
|
131 | - (array) EEH_Array::is_set($input_settings, 'select2_args', array()) |
|
132 | - ); |
|
133 | - $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args)); |
|
134 | - parent::__construct(array(), $input_settings); |
|
135 | - } |
|
40 | + /** |
|
41 | + * @param array $input_settings { |
|
42 | + * @type string $model_name the name of model to be used for searching, both via the REST API and server-side model queries |
|
43 | + * @type array $query_params default query parameters which will apply to both REST API queries and server-side queries. This should be |
|
44 | + * in the exact format that will be used for server-side model usage (eg use index 0 for where conditions, not |
|
45 | + * the string "where") |
|
46 | + * @type string $value_field_name the name of the model field on this model to |
|
47 | + * be used for the HTML select's option's values |
|
48 | + * @type string $display_field_name the name of the model field on this model |
|
49 | + * to be used for the HTML select's option's display text |
|
50 | + * @type array $select2_args arguments to be passed directly into the select2's JS constructor |
|
51 | + * } |
|
52 | + * And the arguments accepted by EE_Form_Input_With_Options_Base |
|
53 | + * } |
|
54 | + * @throws EE_Error |
|
55 | + * @throws InvalidArgumentException |
|
56 | + * @throws InvalidDataTypeException |
|
57 | + * @throws InvalidInterfaceException |
|
58 | + */ |
|
59 | + public function __construct($input_settings = array()) |
|
60 | + { |
|
61 | + // needed input settings: |
|
62 | + // select2_args |
|
63 | + $this->_model_name = EEH_Array::is_set( |
|
64 | + $input_settings, |
|
65 | + 'model_name', |
|
66 | + null |
|
67 | + ); |
|
68 | + $model = $this->_get_model(); |
|
69 | + $query_params = EEH_Array::is_set( |
|
70 | + $input_settings, |
|
71 | + 'query_params', |
|
72 | + array() |
|
73 | + ); |
|
74 | + // make sure limit and caps are always set |
|
75 | + $query_params = array_merge( |
|
76 | + array( 'limit' => 10, 'caps' => EEM_Base::caps_read_admin ), |
|
77 | + $query_params |
|
78 | + ); |
|
79 | + $this->_value_field_name = EEH_Array::is_set( |
|
80 | + $input_settings, |
|
81 | + 'value_field_name', |
|
82 | + $model->primary_key_name() |
|
83 | + ); |
|
84 | + $this->_display_field_name = EEH_Array::is_set( |
|
85 | + $input_settings, |
|
86 | + 'display_field_name', |
|
87 | + $model->get_a_field_of_type('EE_Text_Field_Base')->get_name() |
|
88 | + ); |
|
89 | + $this->_extra_select_columns = EEH_Array::is_set( |
|
90 | + $input_settings, |
|
91 | + 'extra_select_columns', |
|
92 | + array() |
|
93 | + ); |
|
94 | + $this->_add_validation_strategy( |
|
95 | + new EE_Model_Matching_Query_Validation_Strategy( |
|
96 | + '', |
|
97 | + $this->_model_name, |
|
98 | + $query_params, |
|
99 | + $this->_value_field_name |
|
100 | + ) |
|
101 | + ); |
|
102 | + // get resource endpoint |
|
103 | + $rest_controller = LoaderFactory::getLoader()->getNew( |
|
104 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read' |
|
105 | + ); |
|
106 | + $rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version()); |
|
107 | + $default_select2_args = array( |
|
108 | + 'ajax' => array( |
|
109 | + 'url' => $rest_controller->getVersionedLinkTo( |
|
110 | + EEH_Inflector::pluralize_and_lower($this->_model_name) |
|
111 | + ), |
|
112 | + 'dataType' => 'json', |
|
113 | + 'delay' => '250', |
|
114 | + 'data_interface' => 'EE_Select2_REST_API_Interface', |
|
115 | + 'data_interface_args' => array( |
|
116 | + 'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi( |
|
117 | + $query_params, |
|
118 | + $model |
|
119 | + ), |
|
120 | + 'display_field' => $this->_display_field_name, |
|
121 | + 'value_field' => $this->_value_field_name, |
|
122 | + 'nonce' => wp_create_nonce('wp_rest'), |
|
123 | + 'locale' => str_replace('_', '-', strtolower(get_locale())) |
|
124 | + ), |
|
125 | + ), |
|
126 | + 'cache' => true, |
|
127 | + 'width' => 'resolve' |
|
128 | + ); |
|
129 | + $select2_args = array_replace_recursive( |
|
130 | + $default_select2_args, |
|
131 | + (array) EEH_Array::is_set($input_settings, 'select2_args', array()) |
|
132 | + ); |
|
133 | + $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args)); |
|
134 | + parent::__construct(array(), $input_settings); |
|
135 | + } |
|
136 | 136 | |
137 | 137 | |
138 | 138 | |
139 | - /** |
|
140 | - * Before setting the raw value (usually because we're setting the default, |
|
141 | - * or we've received a form submission and this might be re-displayed to the user), |
|
142 | - * sets the options so that the current selections appear on initial display. |
|
143 | - * |
|
144 | - * Note: because this input uses EE_Model_Matching_Query_Validation_Strategy |
|
145 | - * for validation, this input's options only affect DISPLAY and NOT validation, |
|
146 | - * which is why its ok to just assume the provided $value to be in the list of acceptable values |
|
147 | - * |
|
148 | - * @param mixed $value |
|
149 | - * @return void |
|
150 | - * @throws \EE_Error |
|
151 | - */ |
|
152 | - public function _set_raw_value($value) |
|
153 | - { |
|
154 | - $values_for_options = (array) $value; |
|
155 | - $value_field = $this->_get_model()->field_settings_for($this->_value_field_name); |
|
156 | - $display_field = $this->_get_model()->field_settings_for($this->_display_field_name); |
|
157 | - $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name; |
|
158 | - $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name; |
|
159 | - $display_values = $this->_get_model()->get_all_wpdb_results( |
|
160 | - array( |
|
161 | - array( |
|
162 | - $this->_value_field_name => array( 'IN', $values_for_options ) |
|
163 | - ) |
|
164 | - ), |
|
165 | - ARRAY_A, |
|
166 | - implode(',', $this->_extra_select_columns) |
|
167 | - ); |
|
168 | - $select_options = array(); |
|
169 | - if (is_array($select_options)) { |
|
170 | - foreach ($display_values as $db_rows) { |
|
171 | - $db_rows = (array) $db_rows; |
|
172 | - $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters( |
|
173 | - 'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value', |
|
174 | - $db_rows[ $this->_display_field_name ], |
|
175 | - $db_rows |
|
176 | - ); |
|
177 | - } |
|
178 | - } |
|
179 | - $this->set_select_options($select_options); |
|
180 | - parent::_set_raw_value($value); |
|
181 | - } |
|
139 | + /** |
|
140 | + * Before setting the raw value (usually because we're setting the default, |
|
141 | + * or we've received a form submission and this might be re-displayed to the user), |
|
142 | + * sets the options so that the current selections appear on initial display. |
|
143 | + * |
|
144 | + * Note: because this input uses EE_Model_Matching_Query_Validation_Strategy |
|
145 | + * for validation, this input's options only affect DISPLAY and NOT validation, |
|
146 | + * which is why its ok to just assume the provided $value to be in the list of acceptable values |
|
147 | + * |
|
148 | + * @param mixed $value |
|
149 | + * @return void |
|
150 | + * @throws \EE_Error |
|
151 | + */ |
|
152 | + public function _set_raw_value($value) |
|
153 | + { |
|
154 | + $values_for_options = (array) $value; |
|
155 | + $value_field = $this->_get_model()->field_settings_for($this->_value_field_name); |
|
156 | + $display_field = $this->_get_model()->field_settings_for($this->_display_field_name); |
|
157 | + $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name; |
|
158 | + $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name; |
|
159 | + $display_values = $this->_get_model()->get_all_wpdb_results( |
|
160 | + array( |
|
161 | + array( |
|
162 | + $this->_value_field_name => array( 'IN', $values_for_options ) |
|
163 | + ) |
|
164 | + ), |
|
165 | + ARRAY_A, |
|
166 | + implode(',', $this->_extra_select_columns) |
|
167 | + ); |
|
168 | + $select_options = array(); |
|
169 | + if (is_array($select_options)) { |
|
170 | + foreach ($display_values as $db_rows) { |
|
171 | + $db_rows = (array) $db_rows; |
|
172 | + $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters( |
|
173 | + 'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value', |
|
174 | + $db_rows[ $this->_display_field_name ], |
|
175 | + $db_rows |
|
176 | + ); |
|
177 | + } |
|
178 | + } |
|
179 | + $this->set_select_options($select_options); |
|
180 | + parent::_set_raw_value($value); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Returns the model, or throws an exception if the model name provided in constructor doesn't exist |
|
185 | - * @return EEM_Base |
|
186 | - * @throws EE_Error |
|
187 | - */ |
|
188 | - protected function _get_model() |
|
189 | - { |
|
190 | - if (! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
191 | - throw new EE_Error( |
|
192 | - sprintf( |
|
193 | - esc_html__( |
|
194 | - '%1$s is not a proper model name. Please provide a model name in the "model_name" form input argument', |
|
195 | - 'event_espresso' |
|
196 | - ), |
|
197 | - $this->_model_name |
|
198 | - ) |
|
199 | - ); |
|
200 | - } else { |
|
201 | - return EE_Registry::instance()->load_model($this->_model_name); |
|
202 | - } |
|
203 | - } |
|
183 | + /** |
|
184 | + * Returns the model, or throws an exception if the model name provided in constructor doesn't exist |
|
185 | + * @return EEM_Base |
|
186 | + * @throws EE_Error |
|
187 | + */ |
|
188 | + protected function _get_model() |
|
189 | + { |
|
190 | + if (! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
191 | + throw new EE_Error( |
|
192 | + sprintf( |
|
193 | + esc_html__( |
|
194 | + '%1$s is not a proper model name. Please provide a model name in the "model_name" form input argument', |
|
195 | + 'event_espresso' |
|
196 | + ), |
|
197 | + $this->_model_name |
|
198 | + ) |
|
199 | + ); |
|
200 | + } else { |
|
201 | + return EE_Registry::instance()->load_model($this->_model_name); |
|
202 | + } |
|
203 | + } |
|
204 | 204 | } |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | ), |
144 | 144 | ], |
145 | 145 | ]; |
146 | - $this->_model_relations = [ |
|
146 | + $this->_model_relations = [ |
|
147 | 147 | 'Registration' => new EE_Has_Many_Relation(), |
148 | 148 | 'Payment' => new EE_Has_Many_Relation(), |
149 | 149 | 'Status' => new EE_Belongs_To_Relation(), |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | ], |
212 | 212 | OBJECT, |
213 | 213 | [ |
214 | - 'txnDate' => ['DATE(' . $query_interval . ')', '%s'], |
|
214 | + 'txnDate' => ['DATE('.$query_interval.')', '%s'], |
|
215 | 215 | 'revenue' => ['SUM(TransactionTable.TXN_paid)', '%d'], |
216 | 216 | ] |
217 | 217 | ); |
@@ -230,17 +230,17 @@ discard block |
||
230 | 230 | public function get_revenue_per_event_report($period = '-1 month') |
231 | 231 | { |
232 | 232 | global $wpdb; |
233 | - $transaction_table = $wpdb->prefix . 'esp_transaction'; |
|
234 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
235 | - $registration_payment_table = $wpdb->prefix . 'esp_registration_payment'; |
|
233 | + $transaction_table = $wpdb->prefix.'esp_transaction'; |
|
234 | + $registration_table = $wpdb->prefix.'esp_registration'; |
|
235 | + $registration_payment_table = $wpdb->prefix.'esp_registration_payment'; |
|
236 | 236 | $event_table = $wpdb->posts; |
237 | - $payment_table = $wpdb->prefix . 'esp_payment'; |
|
237 | + $payment_table = $wpdb->prefix.'esp_payment'; |
|
238 | 238 | $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
239 | 239 | $approved_payment_status = EEM_Payment::status_id_approved; |
240 | 240 | $extra_event_on_join = ''; |
241 | 241 | // exclude events not authored by user if permissions in effect |
242 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
243 | - $extra_event_on_join = ' AND Event.post_author = ' . get_current_user_id(); |
|
242 | + if ( ! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
243 | + $extra_event_on_join = ' AND Event.post_author = '.get_current_user_id(); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | return $wpdb->get_results( |
@@ -318,7 +318,7 @@ discard block |
||
318 | 318 | public function update_based_on_payments($transaction_obj_or_id, $save_txn = true) |
319 | 319 | { |
320 | 320 | EE_Error::doing_it_wrong( |
321 | - __CLASS__ . '::' . __FUNCTION__, |
|
321 | + __CLASS__.'::'.__FUNCTION__, |
|
322 | 322 | sprintf( |
323 | 323 | esc_html__('This method is deprecated. Please use "%s" instead', 'event_espresso'), |
324 | 324 | 'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()' |
@@ -390,7 +390,7 @@ discard block |
||
390 | 390 | $time_to_leave_alone |
391 | 391 | ); |
392 | 392 | // now that we have the ids to delete |
393 | - if (! empty($txn_ids) && is_array($txn_ids)) { |
|
393 | + if ( ! empty($txn_ids) && is_array($txn_ids)) { |
|
394 | 394 | // first, make sure these TXN's are removed the "ee_locked_transactions" array |
395 | 395 | EEM_Transaction::unset_locked_transactions($txn_ids); |
396 | 396 | |
@@ -403,7 +403,7 @@ discard block |
||
403 | 403 | // let's get deleting... |
404 | 404 | // We got the ids from the original query to get them FROM |
405 | 405 | // the db (which is sanitized) so no need to prepare them again. |
406 | - $query = $wpdb->prepare("DELETE FROM " . $this->table() . " WHERE TXN_ID IN ( $format )", $txn_ids); |
|
406 | + $query = $wpdb->prepare("DELETE FROM ".$this->table()." WHERE TXN_ID IN ( $format )", $txn_ids); |
|
407 | 407 | $deleted = $wpdb->query($query); |
408 | 408 | } |
409 | 409 | if ($deleted) { |
@@ -427,8 +427,8 @@ discard block |
||
427 | 427 | $locked_transactions = get_option('ee_locked_transactions', []); |
428 | 428 | $update = false; |
429 | 429 | foreach ($transaction_IDs as $TXN_ID) { |
430 | - if (isset($locked_transactions[ $TXN_ID ])) { |
|
431 | - unset($locked_transactions[ $TXN_ID ]); |
|
430 | + if (isset($locked_transactions[$TXN_ID])) { |
|
431 | + unset($locked_transactions[$TXN_ID]); |
|
432 | 432 | $update = true; |
433 | 433 | } |
434 | 434 | } |
@@ -18,226 +18,226 @@ discard block |
||
18 | 18 | */ |
19 | 19 | class EEM_Transaction extends EEM_Base |
20 | 20 | { |
21 | - protected static ?EEM_Transaction $_instance = null; |
|
22 | - |
|
23 | - /** |
|
24 | - * Status ID(STS_ID on esp_status table) to indicate the transaction is complete, |
|
25 | - * but payment is pending. This is the state for transactions where payment is promised |
|
26 | - * from an offline gateway. |
|
27 | - */ |
|
28 | - // const open_status_code = 'TPN'; |
|
29 | - |
|
30 | - /** |
|
31 | - * Status ID(STS_ID on esp_status table) to indicate the transaction failed, |
|
32 | - * either due to a technical reason (server or computer crash during registration), |
|
33 | - * or some other reason that prevent the collection of any useful contact information from any of the registrants |
|
34 | - */ |
|
35 | - const failed_status_code = 'TFL'; |
|
36 | - |
|
37 | - /** |
|
38 | - * Status ID(STS_ID on esp_status table) to indicate the transaction was abandoned, |
|
39 | - * either due to a technical reason (server or computer crash during registration), |
|
40 | - * or due to an abandoned cart after registrant chose not to complete the registration process |
|
41 | - * HOWEVER... |
|
42 | - * an abandoned TXN differs from a failed TXN in that it was able to capture contact information for at least one |
|
43 | - * registrant |
|
44 | - */ |
|
45 | - const abandoned_status_code = 'TAB'; |
|
46 | - |
|
47 | - /** |
|
48 | - * Status ID(STS_ID on esp_status table) to indicate an incomplete transaction, |
|
49 | - * meaning that monies are still owing: TXN_paid < TXN_total |
|
50 | - */ |
|
51 | - const incomplete_status_code = 'TIN'; |
|
52 | - |
|
53 | - /** |
|
54 | - * Status ID (STS_ID on esp_status table) to indicate a complete transaction. |
|
55 | - * meaning that NO monies are owing: TXN_paid == TXN_total |
|
56 | - */ |
|
57 | - const complete_status_code = 'TCM'; |
|
58 | - |
|
59 | - /** |
|
60 | - * Status ID(STS_ID on esp_status table) to indicate the transaction is overpaid. |
|
61 | - * This is the same as complete, but site admins actually owe clients the moneys! TXN_paid > TXN_total |
|
62 | - */ |
|
63 | - const overpaid_status_code = 'TOP'; |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * private constructor to prevent direct creation |
|
68 | - * |
|
69 | - * @param string|null $timezone string representing the timezone we want to set for returned Date Time Strings (and |
|
70 | - * any incoming timezone data that gets saved). Note this just sends the timezone info |
|
71 | - * to the date time model field objects. Default is NULL (and will be assumed using |
|
72 | - * the set timezone in the 'timezone_string' wp option) |
|
73 | - * @throws EE_Error |
|
74 | - * @throws ReflectionException |
|
75 | - */ |
|
76 | - protected function __construct(?string $timezone = '') |
|
77 | - { |
|
78 | - $this->singular_item = esc_html__('Transaction', 'event_espresso'); |
|
79 | - $this->plural_item = esc_html__('Transactions', 'event_espresso'); |
|
80 | - |
|
81 | - $this->_tables = [ |
|
82 | - 'TransactionTable' => new EE_Primary_Table('esp_transaction', 'TXN_ID'), |
|
83 | - ]; |
|
84 | - $this->_fields = [ |
|
85 | - 'TransactionTable' => [ |
|
86 | - 'TXN_ID' => new EE_Primary_Key_Int_Field( |
|
87 | - 'TXN_ID', |
|
88 | - esc_html__('Transaction ID', 'event_espresso') |
|
89 | - ), |
|
90 | - 'TXN_timestamp' => new EE_Datetime_Field( |
|
91 | - 'TXN_timestamp', |
|
92 | - esc_html__('date when transaction was created', 'event_espresso'), |
|
93 | - false, |
|
94 | - EE_Datetime_Field::now, |
|
95 | - $timezone |
|
96 | - ), |
|
97 | - 'TXN_total' => new EE_Money_Field( |
|
98 | - 'TXN_total', |
|
99 | - esc_html__('Total value of Transaction', 'event_espresso'), |
|
100 | - false, |
|
101 | - 0 |
|
102 | - ), |
|
103 | - 'TXN_paid' => new EE_Money_Field( |
|
104 | - 'TXN_paid', |
|
105 | - esc_html__('Amount paid towards transaction to date', 'event_espresso'), |
|
106 | - false, |
|
107 | - 0 |
|
108 | - ), |
|
109 | - 'STS_ID' => new EE_Foreign_Key_String_Field( |
|
110 | - 'STS_ID', |
|
111 | - esc_html__('Status ID', 'event_espresso'), |
|
112 | - false, |
|
113 | - EEM_Transaction::failed_status_code, |
|
114 | - 'Status' |
|
115 | - ), |
|
116 | - 'TXN_session_data' => new EE_Serialized_Text_Field( |
|
117 | - 'TXN_session_data', |
|
118 | - esc_html__('Serialized session data', 'event_espresso'), |
|
119 | - true, |
|
120 | - '' |
|
121 | - ), |
|
122 | - 'TXN_hash_salt' => new EE_Plain_Text_Field( |
|
123 | - 'TXN_hash_salt', |
|
124 | - esc_html__('Transaction Hash Salt', 'event_espresso'), |
|
125 | - true, |
|
126 | - '' |
|
127 | - ), |
|
128 | - 'PMD_ID' => new EE_Foreign_Key_Int_Field( |
|
129 | - 'PMD_ID', |
|
130 | - esc_html__("Last Used Payment Method", 'event_espresso'), |
|
131 | - true, |
|
132 | - null, |
|
133 | - 'Payment_Method' |
|
134 | - ), |
|
135 | - 'TXN_reg_steps' => new EE_Serialized_Text_Field( |
|
136 | - 'TXN_reg_steps', |
|
137 | - esc_html__('Registration Steps', 'event_espresso'), |
|
138 | - false, |
|
139 | - [] |
|
140 | - ), |
|
141 | - ], |
|
142 | - ]; |
|
143 | - $this->_model_relations = [ |
|
144 | - 'Registration' => new EE_Has_Many_Relation(), |
|
145 | - 'Payment' => new EE_Has_Many_Relation(), |
|
146 | - 'Status' => new EE_Belongs_To_Relation(), |
|
147 | - 'Line_Item' => new EE_Has_Many_Relation(false), |
|
148 | - // you can delete a transaction without needing to delete its line items |
|
149 | - 'Payment_Method' => new EE_Belongs_To_Relation(), |
|
150 | - 'Message' => new EE_Has_Many_Relation(), |
|
151 | - ]; |
|
152 | - $this->_model_chain_to_wp_user = 'Registration.Event'; |
|
153 | - parent::__construct($timezone); |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * txn_status_array |
|
159 | - * get list of transaction statuses |
|
160 | - * |
|
161 | - * @access public |
|
162 | - * @return array |
|
163 | - */ |
|
164 | - public static function txn_status_array() |
|
165 | - { |
|
166 | - return apply_filters( |
|
167 | - 'FHEE__EEM_Transaction__txn_status_array', |
|
168 | - [ |
|
169 | - EEM_Transaction::overpaid_status_code, |
|
170 | - EEM_Transaction::complete_status_code, |
|
171 | - EEM_Transaction::incomplete_status_code, |
|
172 | - EEM_Transaction::abandoned_status_code, |
|
173 | - EEM_Transaction::failed_status_code, |
|
174 | - ] |
|
175 | - ); |
|
176 | - } |
|
177 | - |
|
178 | - |
|
179 | - /** |
|
180 | - * get the revenue per day for the Transaction Admin page Reports Tab |
|
181 | - * |
|
182 | - * @access public |
|
183 | - * @param string $period |
|
184 | - * @return stdClass[] |
|
185 | - * @throws EE_Error |
|
186 | - * @throws EE_Error |
|
187 | - */ |
|
188 | - public function get_revenue_per_day_report($period = '-1 month') |
|
189 | - { |
|
190 | - $sql_date = $this->convert_datetime_for_query( |
|
191 | - 'TXN_timestamp', |
|
192 | - date('Y-m-d H:i:s', strtotime($period)), |
|
193 | - 'Y-m-d H:i:s', |
|
194 | - 'UTC' |
|
195 | - ); |
|
196 | - |
|
197 | - $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'TXN_timestamp'); |
|
198 | - |
|
199 | - return $this->_get_all_wpdb_results( |
|
200 | - [ |
|
201 | - [ |
|
202 | - 'TXN_timestamp' => ['>=', $sql_date], |
|
203 | - ], |
|
204 | - 'group_by' => 'txnDate', |
|
205 | - 'order_by' => ['TXN_timestamp' => 'ASC'], |
|
206 | - ], |
|
207 | - OBJECT, |
|
208 | - [ |
|
209 | - 'txnDate' => ['DATE(' . $query_interval . ')', '%s'], |
|
210 | - 'revenue' => ['SUM(TransactionTable.TXN_paid)', '%d'], |
|
211 | - ] |
|
212 | - ); |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * get the revenue per event for the Transaction Admin page Reports Tab |
|
218 | - * |
|
219 | - * @access public |
|
220 | - * @param string $period |
|
221 | - * @return EE_Transaction[] |
|
222 | - */ |
|
223 | - public function get_revenue_per_event_report($period = '-1 month') |
|
224 | - { |
|
225 | - global $wpdb; |
|
226 | - $transaction_table = $wpdb->prefix . 'esp_transaction'; |
|
227 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
228 | - $registration_payment_table = $wpdb->prefix . 'esp_registration_payment'; |
|
229 | - $event_table = $wpdb->posts; |
|
230 | - $payment_table = $wpdb->prefix . 'esp_payment'; |
|
231 | - $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
|
232 | - $approved_payment_status = EEM_Payment::status_id_approved; |
|
233 | - $extra_event_on_join = ''; |
|
234 | - // exclude events not authored by user if permissions in effect |
|
235 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
236 | - $extra_event_on_join = ' AND Event.post_author = ' . get_current_user_id(); |
|
237 | - } |
|
238 | - |
|
239 | - return $wpdb->get_results( |
|
240 | - "SELECT |
|
21 | + protected static ?EEM_Transaction $_instance = null; |
|
22 | + |
|
23 | + /** |
|
24 | + * Status ID(STS_ID on esp_status table) to indicate the transaction is complete, |
|
25 | + * but payment is pending. This is the state for transactions where payment is promised |
|
26 | + * from an offline gateway. |
|
27 | + */ |
|
28 | + // const open_status_code = 'TPN'; |
|
29 | + |
|
30 | + /** |
|
31 | + * Status ID(STS_ID on esp_status table) to indicate the transaction failed, |
|
32 | + * either due to a technical reason (server or computer crash during registration), |
|
33 | + * or some other reason that prevent the collection of any useful contact information from any of the registrants |
|
34 | + */ |
|
35 | + const failed_status_code = 'TFL'; |
|
36 | + |
|
37 | + /** |
|
38 | + * Status ID(STS_ID on esp_status table) to indicate the transaction was abandoned, |
|
39 | + * either due to a technical reason (server or computer crash during registration), |
|
40 | + * or due to an abandoned cart after registrant chose not to complete the registration process |
|
41 | + * HOWEVER... |
|
42 | + * an abandoned TXN differs from a failed TXN in that it was able to capture contact information for at least one |
|
43 | + * registrant |
|
44 | + */ |
|
45 | + const abandoned_status_code = 'TAB'; |
|
46 | + |
|
47 | + /** |
|
48 | + * Status ID(STS_ID on esp_status table) to indicate an incomplete transaction, |
|
49 | + * meaning that monies are still owing: TXN_paid < TXN_total |
|
50 | + */ |
|
51 | + const incomplete_status_code = 'TIN'; |
|
52 | + |
|
53 | + /** |
|
54 | + * Status ID (STS_ID on esp_status table) to indicate a complete transaction. |
|
55 | + * meaning that NO monies are owing: TXN_paid == TXN_total |
|
56 | + */ |
|
57 | + const complete_status_code = 'TCM'; |
|
58 | + |
|
59 | + /** |
|
60 | + * Status ID(STS_ID on esp_status table) to indicate the transaction is overpaid. |
|
61 | + * This is the same as complete, but site admins actually owe clients the moneys! TXN_paid > TXN_total |
|
62 | + */ |
|
63 | + const overpaid_status_code = 'TOP'; |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * private constructor to prevent direct creation |
|
68 | + * |
|
69 | + * @param string|null $timezone string representing the timezone we want to set for returned Date Time Strings (and |
|
70 | + * any incoming timezone data that gets saved). Note this just sends the timezone info |
|
71 | + * to the date time model field objects. Default is NULL (and will be assumed using |
|
72 | + * the set timezone in the 'timezone_string' wp option) |
|
73 | + * @throws EE_Error |
|
74 | + * @throws ReflectionException |
|
75 | + */ |
|
76 | + protected function __construct(?string $timezone = '') |
|
77 | + { |
|
78 | + $this->singular_item = esc_html__('Transaction', 'event_espresso'); |
|
79 | + $this->plural_item = esc_html__('Transactions', 'event_espresso'); |
|
80 | + |
|
81 | + $this->_tables = [ |
|
82 | + 'TransactionTable' => new EE_Primary_Table('esp_transaction', 'TXN_ID'), |
|
83 | + ]; |
|
84 | + $this->_fields = [ |
|
85 | + 'TransactionTable' => [ |
|
86 | + 'TXN_ID' => new EE_Primary_Key_Int_Field( |
|
87 | + 'TXN_ID', |
|
88 | + esc_html__('Transaction ID', 'event_espresso') |
|
89 | + ), |
|
90 | + 'TXN_timestamp' => new EE_Datetime_Field( |
|
91 | + 'TXN_timestamp', |
|
92 | + esc_html__('date when transaction was created', 'event_espresso'), |
|
93 | + false, |
|
94 | + EE_Datetime_Field::now, |
|
95 | + $timezone |
|
96 | + ), |
|
97 | + 'TXN_total' => new EE_Money_Field( |
|
98 | + 'TXN_total', |
|
99 | + esc_html__('Total value of Transaction', 'event_espresso'), |
|
100 | + false, |
|
101 | + 0 |
|
102 | + ), |
|
103 | + 'TXN_paid' => new EE_Money_Field( |
|
104 | + 'TXN_paid', |
|
105 | + esc_html__('Amount paid towards transaction to date', 'event_espresso'), |
|
106 | + false, |
|
107 | + 0 |
|
108 | + ), |
|
109 | + 'STS_ID' => new EE_Foreign_Key_String_Field( |
|
110 | + 'STS_ID', |
|
111 | + esc_html__('Status ID', 'event_espresso'), |
|
112 | + false, |
|
113 | + EEM_Transaction::failed_status_code, |
|
114 | + 'Status' |
|
115 | + ), |
|
116 | + 'TXN_session_data' => new EE_Serialized_Text_Field( |
|
117 | + 'TXN_session_data', |
|
118 | + esc_html__('Serialized session data', 'event_espresso'), |
|
119 | + true, |
|
120 | + '' |
|
121 | + ), |
|
122 | + 'TXN_hash_salt' => new EE_Plain_Text_Field( |
|
123 | + 'TXN_hash_salt', |
|
124 | + esc_html__('Transaction Hash Salt', 'event_espresso'), |
|
125 | + true, |
|
126 | + '' |
|
127 | + ), |
|
128 | + 'PMD_ID' => new EE_Foreign_Key_Int_Field( |
|
129 | + 'PMD_ID', |
|
130 | + esc_html__("Last Used Payment Method", 'event_espresso'), |
|
131 | + true, |
|
132 | + null, |
|
133 | + 'Payment_Method' |
|
134 | + ), |
|
135 | + 'TXN_reg_steps' => new EE_Serialized_Text_Field( |
|
136 | + 'TXN_reg_steps', |
|
137 | + esc_html__('Registration Steps', 'event_espresso'), |
|
138 | + false, |
|
139 | + [] |
|
140 | + ), |
|
141 | + ], |
|
142 | + ]; |
|
143 | + $this->_model_relations = [ |
|
144 | + 'Registration' => new EE_Has_Many_Relation(), |
|
145 | + 'Payment' => new EE_Has_Many_Relation(), |
|
146 | + 'Status' => new EE_Belongs_To_Relation(), |
|
147 | + 'Line_Item' => new EE_Has_Many_Relation(false), |
|
148 | + // you can delete a transaction without needing to delete its line items |
|
149 | + 'Payment_Method' => new EE_Belongs_To_Relation(), |
|
150 | + 'Message' => new EE_Has_Many_Relation(), |
|
151 | + ]; |
|
152 | + $this->_model_chain_to_wp_user = 'Registration.Event'; |
|
153 | + parent::__construct($timezone); |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * txn_status_array |
|
159 | + * get list of transaction statuses |
|
160 | + * |
|
161 | + * @access public |
|
162 | + * @return array |
|
163 | + */ |
|
164 | + public static function txn_status_array() |
|
165 | + { |
|
166 | + return apply_filters( |
|
167 | + 'FHEE__EEM_Transaction__txn_status_array', |
|
168 | + [ |
|
169 | + EEM_Transaction::overpaid_status_code, |
|
170 | + EEM_Transaction::complete_status_code, |
|
171 | + EEM_Transaction::incomplete_status_code, |
|
172 | + EEM_Transaction::abandoned_status_code, |
|
173 | + EEM_Transaction::failed_status_code, |
|
174 | + ] |
|
175 | + ); |
|
176 | + } |
|
177 | + |
|
178 | + |
|
179 | + /** |
|
180 | + * get the revenue per day for the Transaction Admin page Reports Tab |
|
181 | + * |
|
182 | + * @access public |
|
183 | + * @param string $period |
|
184 | + * @return stdClass[] |
|
185 | + * @throws EE_Error |
|
186 | + * @throws EE_Error |
|
187 | + */ |
|
188 | + public function get_revenue_per_day_report($period = '-1 month') |
|
189 | + { |
|
190 | + $sql_date = $this->convert_datetime_for_query( |
|
191 | + 'TXN_timestamp', |
|
192 | + date('Y-m-d H:i:s', strtotime($period)), |
|
193 | + 'Y-m-d H:i:s', |
|
194 | + 'UTC' |
|
195 | + ); |
|
196 | + |
|
197 | + $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'TXN_timestamp'); |
|
198 | + |
|
199 | + return $this->_get_all_wpdb_results( |
|
200 | + [ |
|
201 | + [ |
|
202 | + 'TXN_timestamp' => ['>=', $sql_date], |
|
203 | + ], |
|
204 | + 'group_by' => 'txnDate', |
|
205 | + 'order_by' => ['TXN_timestamp' => 'ASC'], |
|
206 | + ], |
|
207 | + OBJECT, |
|
208 | + [ |
|
209 | + 'txnDate' => ['DATE(' . $query_interval . ')', '%s'], |
|
210 | + 'revenue' => ['SUM(TransactionTable.TXN_paid)', '%d'], |
|
211 | + ] |
|
212 | + ); |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * get the revenue per event for the Transaction Admin page Reports Tab |
|
218 | + * |
|
219 | + * @access public |
|
220 | + * @param string $period |
|
221 | + * @return EE_Transaction[] |
|
222 | + */ |
|
223 | + public function get_revenue_per_event_report($period = '-1 month') |
|
224 | + { |
|
225 | + global $wpdb; |
|
226 | + $transaction_table = $wpdb->prefix . 'esp_transaction'; |
|
227 | + $registration_table = $wpdb->prefix . 'esp_registration'; |
|
228 | + $registration_payment_table = $wpdb->prefix . 'esp_registration_payment'; |
|
229 | + $event_table = $wpdb->posts; |
|
230 | + $payment_table = $wpdb->prefix . 'esp_payment'; |
|
231 | + $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
|
232 | + $approved_payment_status = EEM_Payment::status_id_approved; |
|
233 | + $extra_event_on_join = ''; |
|
234 | + // exclude events not authored by user if permissions in effect |
|
235 | + if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
236 | + $extra_event_on_join = ' AND Event.post_author = ' . get_current_user_id(); |
|
237 | + } |
|
238 | + |
|
239 | + return $wpdb->get_results( |
|
240 | + "SELECT |
|
241 | 241 | Transaction_Event.event_name AS event_name, |
242 | 242 | SUM(Transaction_Event.paid) AS revenue |
243 | 243 | FROM |
@@ -265,232 +265,232 @@ discard block |
||
265 | 265 | $extra_event_on_join |
266 | 266 | ) AS Transaction_Event |
267 | 267 | GROUP BY event_name" |
268 | - ); |
|
269 | - } |
|
270 | - |
|
271 | - |
|
272 | - /** |
|
273 | - * Gets the current transaction given the reg_url_link, or assumes the reg_url_link is in the |
|
274 | - * request global variable. Either way, tries to find the current transaction (through |
|
275 | - * the registration pointed to by reg_url_link), if not returns null |
|
276 | - * |
|
277 | - * @param string $reg_url_link |
|
278 | - * @return EE_Transaction |
|
279 | - * @throws EE_Error |
|
280 | - */ |
|
281 | - public function get_transaction_from_reg_url_link($reg_url_link = '') |
|
282 | - { |
|
283 | - if (empty($reg_url_link)) { |
|
284 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
285 | - $reg_url_link = $request->getRequestParam('e_reg_url_link'); |
|
286 | - } |
|
287 | - return $this->get_one( |
|
288 | - [ |
|
289 | - [ |
|
290 | - 'Registration.REG_url_link' => $reg_url_link, |
|
291 | - ], |
|
292 | - ] |
|
293 | - ); |
|
294 | - } |
|
295 | - |
|
296 | - |
|
297 | - /** |
|
298 | - * Updates the provided EE_Transaction with all the applicable payments |
|
299 | - * (or fetch the EE_Transaction from its ID) |
|
300 | - * |
|
301 | - * @param EE_Transaction|int $transaction_obj_or_id |
|
302 | - * @param boolean $save_txn whether or not to save the transaction during this function call |
|
303 | - * @return array |
|
304 | - * @throws EE_Error |
|
305 | - * @throws ReflectionException |
|
306 | - * @deprecated |
|
307 | - */ |
|
308 | - public function update_based_on_payments($transaction_obj_or_id, $save_txn = true) |
|
309 | - { |
|
310 | - EE_Error::doing_it_wrong( |
|
311 | - __CLASS__ . '::' . __FUNCTION__, |
|
312 | - sprintf( |
|
313 | - esc_html__('This method is deprecated. Please use "%s" instead', 'event_espresso'), |
|
314 | - 'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()' |
|
315 | - ), |
|
316 | - '4.6.0' |
|
317 | - ); |
|
318 | - /** @type EE_Transaction_Processor $transaction_processor */ |
|
319 | - $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
|
320 | - |
|
321 | - return $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment( |
|
322 | - $this->ensure_is_obj($transaction_obj_or_id) |
|
323 | - ); |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * Deletes "junk" transactions that were probably added by bots. There might be TONS |
|
329 | - * of these, so we are very careful to NOT select (which the models do even when deleting), |
|
330 | - * and so we only use wpdb directly and only do minimal joins. |
|
331 | - * Transactions are considered "junk" if they're failed for longer than a week. |
|
332 | - * Also, there is an extra check for payments related to the transaction, because if a transaction has a payment on |
|
333 | - * it, it's probably not junk (regardless of what status it has). |
|
334 | - * The downside to this approach is that is addons are listening for object deletions |
|
335 | - * on EEM_Base::delete() they won't be notified of this. However, there is an action that plugins can hook into |
|
336 | - * to catch these types of deletions. |
|
337 | - * |
|
338 | - * @return int |
|
339 | - * @throws EE_Error |
|
340 | - * @throws EE_Error |
|
341 | - * @global WPDB $wpdb |
|
342 | - */ |
|
343 | - public function delete_junk_transactions() |
|
344 | - { |
|
345 | - global $wpdb; |
|
346 | - $deleted = false; |
|
347 | - $time_to_leave_alone = (int) apply_filters( |
|
348 | - 'FHEE__EEM_Transaction__delete_junk_transactions__time_to_leave_alone', |
|
349 | - WEEK_IN_SECONDS |
|
350 | - ); |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * This allows code to filter the query arguments used for retrieving the transaction IDs to delete. |
|
355 | - * Useful for plugins that want to exclude transactions matching certain query parameters. |
|
356 | - * The query parameters should be in the format accepted by the EEM_Base model queries. |
|
357 | - */ |
|
358 | - $ids_query = apply_filters( |
|
359 | - 'FHEE__EEM_Transaction__delete_junk_transactions__initial_query_args', |
|
360 | - [ |
|
361 | - 0 => [ |
|
362 | - 'STS_ID' => EEM_Transaction::failed_status_code, |
|
363 | - 'Payment.PAY_ID' => ['IS NULL'], |
|
364 | - 'TXN_timestamp' => ['<', time() - $time_to_leave_alone], |
|
365 | - ], |
|
366 | - 'order_by' => ['TXN_timestamp' => 'ASC'], |
|
367 | - 'limit' => 1000, |
|
368 | - ], |
|
369 | - $time_to_leave_alone |
|
370 | - ); |
|
371 | - |
|
372 | - |
|
373 | - /** |
|
374 | - * This filter is for when code needs to filter the list of transaction ids that represent transactions |
|
375 | - * about to be deleted based on some other criteria that isn't easily done via the query args filter. |
|
376 | - */ |
|
377 | - $txn_ids = apply_filters( |
|
378 | - 'FHEE__EEM_Transaction__delete_junk_transactions__transaction_ids_to_delete', |
|
379 | - EEM_Transaction::instance()->get_col($ids_query, 'TXN_ID'), |
|
380 | - $time_to_leave_alone |
|
381 | - ); |
|
382 | - // now that we have the ids to delete |
|
383 | - if (! empty($txn_ids) && is_array($txn_ids)) { |
|
384 | - // first, make sure these TXN's are removed the "ee_locked_transactions" array |
|
385 | - EEM_Transaction::unset_locked_transactions($txn_ids); |
|
386 | - |
|
387 | - // Create IDs placeholder. |
|
388 | - $placeholders = array_fill(0, count($txn_ids), '%d'); |
|
389 | - |
|
390 | - // Glue it together to use inside $wpdb->prepare. |
|
391 | - $format = implode(', ', $placeholders); |
|
392 | - |
|
393 | - // let's get deleting... |
|
394 | - // We got the ids from the original query to get them FROM |
|
395 | - // the db (which is sanitized) so no need to prepare them again. |
|
396 | - $query = $wpdb->prepare("DELETE FROM " . $this->table() . " WHERE TXN_ID IN ( $format )", $txn_ids); |
|
397 | - $deleted = $wpdb->query($query); |
|
398 | - } |
|
399 | - if ($deleted) { |
|
400 | - /** |
|
401 | - * Allows code to do something after the transactions have been deleted. |
|
402 | - */ |
|
403 | - do_action('AHEE__EEM_Transaction__delete_junk_transactions__successful_deletion', $txn_ids); |
|
404 | - } |
|
405 | - |
|
406 | - return $deleted; |
|
407 | - } |
|
408 | - |
|
409 | - |
|
410 | - /** |
|
411 | - * @param array $transaction_IDs |
|
412 | - * @return bool |
|
413 | - */ |
|
414 | - public static function unset_locked_transactions(array $transaction_IDs) |
|
415 | - { |
|
416 | - $locked_transactions = get_option('ee_locked_transactions', []); |
|
417 | - $update = false; |
|
418 | - foreach ($transaction_IDs as $TXN_ID) { |
|
419 | - if (isset($locked_transactions[ $TXN_ID ])) { |
|
420 | - unset($locked_transactions[ $TXN_ID ]); |
|
421 | - $update = true; |
|
422 | - } |
|
423 | - } |
|
424 | - if ($update) { |
|
425 | - update_option('ee_locked_transactions', $locked_transactions); |
|
426 | - } |
|
427 | - |
|
428 | - return $update; |
|
429 | - } |
|
430 | - |
|
431 | - |
|
432 | - /** |
|
433 | - * returns an array of EE_Transaction objects whose timestamp is greater than |
|
434 | - * the current time minus the session lifespan, which defaults to 60 minutes |
|
435 | - * |
|
436 | - * @return EE_Base_Class[]|EE_Transaction[] |
|
437 | - * @throws EE_Error |
|
438 | - * @throws InvalidArgumentException |
|
439 | - * @throws InvalidDataTypeException |
|
440 | - * @throws InvalidInterfaceException |
|
441 | - */ |
|
442 | - public function get_transactions_in_progress() |
|
443 | - { |
|
444 | - return $this->_get_transactions_in_progress(); |
|
445 | - } |
|
446 | - |
|
447 | - |
|
448 | - /** |
|
449 | - * returns an array of EE_Transaction objects whose timestamp is less than |
|
450 | - * the current time minus the session lifespan, which defaults to 60 minutes |
|
451 | - * |
|
452 | - * @return EE_Base_Class[]|EE_Transaction[] |
|
453 | - * @throws EE_Error |
|
454 | - * @throws InvalidArgumentException |
|
455 | - * @throws InvalidDataTypeException |
|
456 | - * @throws InvalidInterfaceException |
|
457 | - */ |
|
458 | - public function get_transactions_not_in_progress() |
|
459 | - { |
|
460 | - return $this->_get_transactions_in_progress('<='); |
|
461 | - } |
|
462 | - |
|
463 | - |
|
464 | - /** |
|
465 | - * @param string $comparison |
|
466 | - * @return EE_Transaction[] |
|
467 | - * @throws EE_Error |
|
468 | - * @throws InvalidArgumentException |
|
469 | - * @throws InvalidDataTypeException |
|
470 | - * @throws InvalidInterfaceException |
|
471 | - */ |
|
472 | - private function _get_transactions_in_progress($comparison = '>=') |
|
473 | - { |
|
474 | - $comparison = $comparison === '>=' || $comparison === '<=' |
|
475 | - ? $comparison |
|
476 | - : '>='; |
|
477 | - /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */ |
|
478 | - $session_lifespan = LoaderFactory::getLoader()->getShared( |
|
479 | - 'EventEspresso\core\domain\values\session\SessionLifespan' |
|
480 | - ); |
|
481 | - return $this->get_all( |
|
482 | - [ |
|
483 | - [ |
|
484 | - 'TXN_timestamp' => [ |
|
485 | - $comparison, |
|
486 | - $session_lifespan->expiration(), |
|
487 | - ], |
|
488 | - 'STS_ID' => [ |
|
489 | - '!=', |
|
490 | - EEM_Transaction::complete_status_code, |
|
491 | - ], |
|
492 | - ], |
|
493 | - ] |
|
494 | - ); |
|
495 | - } |
|
268 | + ); |
|
269 | + } |
|
270 | + |
|
271 | + |
|
272 | + /** |
|
273 | + * Gets the current transaction given the reg_url_link, or assumes the reg_url_link is in the |
|
274 | + * request global variable. Either way, tries to find the current transaction (through |
|
275 | + * the registration pointed to by reg_url_link), if not returns null |
|
276 | + * |
|
277 | + * @param string $reg_url_link |
|
278 | + * @return EE_Transaction |
|
279 | + * @throws EE_Error |
|
280 | + */ |
|
281 | + public function get_transaction_from_reg_url_link($reg_url_link = '') |
|
282 | + { |
|
283 | + if (empty($reg_url_link)) { |
|
284 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
285 | + $reg_url_link = $request->getRequestParam('e_reg_url_link'); |
|
286 | + } |
|
287 | + return $this->get_one( |
|
288 | + [ |
|
289 | + [ |
|
290 | + 'Registration.REG_url_link' => $reg_url_link, |
|
291 | + ], |
|
292 | + ] |
|
293 | + ); |
|
294 | + } |
|
295 | + |
|
296 | + |
|
297 | + /** |
|
298 | + * Updates the provided EE_Transaction with all the applicable payments |
|
299 | + * (or fetch the EE_Transaction from its ID) |
|
300 | + * |
|
301 | + * @param EE_Transaction|int $transaction_obj_or_id |
|
302 | + * @param boolean $save_txn whether or not to save the transaction during this function call |
|
303 | + * @return array |
|
304 | + * @throws EE_Error |
|
305 | + * @throws ReflectionException |
|
306 | + * @deprecated |
|
307 | + */ |
|
308 | + public function update_based_on_payments($transaction_obj_or_id, $save_txn = true) |
|
309 | + { |
|
310 | + EE_Error::doing_it_wrong( |
|
311 | + __CLASS__ . '::' . __FUNCTION__, |
|
312 | + sprintf( |
|
313 | + esc_html__('This method is deprecated. Please use "%s" instead', 'event_espresso'), |
|
314 | + 'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()' |
|
315 | + ), |
|
316 | + '4.6.0' |
|
317 | + ); |
|
318 | + /** @type EE_Transaction_Processor $transaction_processor */ |
|
319 | + $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
|
320 | + |
|
321 | + return $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment( |
|
322 | + $this->ensure_is_obj($transaction_obj_or_id) |
|
323 | + ); |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * Deletes "junk" transactions that were probably added by bots. There might be TONS |
|
329 | + * of these, so we are very careful to NOT select (which the models do even when deleting), |
|
330 | + * and so we only use wpdb directly and only do minimal joins. |
|
331 | + * Transactions are considered "junk" if they're failed for longer than a week. |
|
332 | + * Also, there is an extra check for payments related to the transaction, because if a transaction has a payment on |
|
333 | + * it, it's probably not junk (regardless of what status it has). |
|
334 | + * The downside to this approach is that is addons are listening for object deletions |
|
335 | + * on EEM_Base::delete() they won't be notified of this. However, there is an action that plugins can hook into |
|
336 | + * to catch these types of deletions. |
|
337 | + * |
|
338 | + * @return int |
|
339 | + * @throws EE_Error |
|
340 | + * @throws EE_Error |
|
341 | + * @global WPDB $wpdb |
|
342 | + */ |
|
343 | + public function delete_junk_transactions() |
|
344 | + { |
|
345 | + global $wpdb; |
|
346 | + $deleted = false; |
|
347 | + $time_to_leave_alone = (int) apply_filters( |
|
348 | + 'FHEE__EEM_Transaction__delete_junk_transactions__time_to_leave_alone', |
|
349 | + WEEK_IN_SECONDS |
|
350 | + ); |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * This allows code to filter the query arguments used for retrieving the transaction IDs to delete. |
|
355 | + * Useful for plugins that want to exclude transactions matching certain query parameters. |
|
356 | + * The query parameters should be in the format accepted by the EEM_Base model queries. |
|
357 | + */ |
|
358 | + $ids_query = apply_filters( |
|
359 | + 'FHEE__EEM_Transaction__delete_junk_transactions__initial_query_args', |
|
360 | + [ |
|
361 | + 0 => [ |
|
362 | + 'STS_ID' => EEM_Transaction::failed_status_code, |
|
363 | + 'Payment.PAY_ID' => ['IS NULL'], |
|
364 | + 'TXN_timestamp' => ['<', time() - $time_to_leave_alone], |
|
365 | + ], |
|
366 | + 'order_by' => ['TXN_timestamp' => 'ASC'], |
|
367 | + 'limit' => 1000, |
|
368 | + ], |
|
369 | + $time_to_leave_alone |
|
370 | + ); |
|
371 | + |
|
372 | + |
|
373 | + /** |
|
374 | + * This filter is for when code needs to filter the list of transaction ids that represent transactions |
|
375 | + * about to be deleted based on some other criteria that isn't easily done via the query args filter. |
|
376 | + */ |
|
377 | + $txn_ids = apply_filters( |
|
378 | + 'FHEE__EEM_Transaction__delete_junk_transactions__transaction_ids_to_delete', |
|
379 | + EEM_Transaction::instance()->get_col($ids_query, 'TXN_ID'), |
|
380 | + $time_to_leave_alone |
|
381 | + ); |
|
382 | + // now that we have the ids to delete |
|
383 | + if (! empty($txn_ids) && is_array($txn_ids)) { |
|
384 | + // first, make sure these TXN's are removed the "ee_locked_transactions" array |
|
385 | + EEM_Transaction::unset_locked_transactions($txn_ids); |
|
386 | + |
|
387 | + // Create IDs placeholder. |
|
388 | + $placeholders = array_fill(0, count($txn_ids), '%d'); |
|
389 | + |
|
390 | + // Glue it together to use inside $wpdb->prepare. |
|
391 | + $format = implode(', ', $placeholders); |
|
392 | + |
|
393 | + // let's get deleting... |
|
394 | + // We got the ids from the original query to get them FROM |
|
395 | + // the db (which is sanitized) so no need to prepare them again. |
|
396 | + $query = $wpdb->prepare("DELETE FROM " . $this->table() . " WHERE TXN_ID IN ( $format )", $txn_ids); |
|
397 | + $deleted = $wpdb->query($query); |
|
398 | + } |
|
399 | + if ($deleted) { |
|
400 | + /** |
|
401 | + * Allows code to do something after the transactions have been deleted. |
|
402 | + */ |
|
403 | + do_action('AHEE__EEM_Transaction__delete_junk_transactions__successful_deletion', $txn_ids); |
|
404 | + } |
|
405 | + |
|
406 | + return $deleted; |
|
407 | + } |
|
408 | + |
|
409 | + |
|
410 | + /** |
|
411 | + * @param array $transaction_IDs |
|
412 | + * @return bool |
|
413 | + */ |
|
414 | + public static function unset_locked_transactions(array $transaction_IDs) |
|
415 | + { |
|
416 | + $locked_transactions = get_option('ee_locked_transactions', []); |
|
417 | + $update = false; |
|
418 | + foreach ($transaction_IDs as $TXN_ID) { |
|
419 | + if (isset($locked_transactions[ $TXN_ID ])) { |
|
420 | + unset($locked_transactions[ $TXN_ID ]); |
|
421 | + $update = true; |
|
422 | + } |
|
423 | + } |
|
424 | + if ($update) { |
|
425 | + update_option('ee_locked_transactions', $locked_transactions); |
|
426 | + } |
|
427 | + |
|
428 | + return $update; |
|
429 | + } |
|
430 | + |
|
431 | + |
|
432 | + /** |
|
433 | + * returns an array of EE_Transaction objects whose timestamp is greater than |
|
434 | + * the current time minus the session lifespan, which defaults to 60 minutes |
|
435 | + * |
|
436 | + * @return EE_Base_Class[]|EE_Transaction[] |
|
437 | + * @throws EE_Error |
|
438 | + * @throws InvalidArgumentException |
|
439 | + * @throws InvalidDataTypeException |
|
440 | + * @throws InvalidInterfaceException |
|
441 | + */ |
|
442 | + public function get_transactions_in_progress() |
|
443 | + { |
|
444 | + return $this->_get_transactions_in_progress(); |
|
445 | + } |
|
446 | + |
|
447 | + |
|
448 | + /** |
|
449 | + * returns an array of EE_Transaction objects whose timestamp is less than |
|
450 | + * the current time minus the session lifespan, which defaults to 60 minutes |
|
451 | + * |
|
452 | + * @return EE_Base_Class[]|EE_Transaction[] |
|
453 | + * @throws EE_Error |
|
454 | + * @throws InvalidArgumentException |
|
455 | + * @throws InvalidDataTypeException |
|
456 | + * @throws InvalidInterfaceException |
|
457 | + */ |
|
458 | + public function get_transactions_not_in_progress() |
|
459 | + { |
|
460 | + return $this->_get_transactions_in_progress('<='); |
|
461 | + } |
|
462 | + |
|
463 | + |
|
464 | + /** |
|
465 | + * @param string $comparison |
|
466 | + * @return EE_Transaction[] |
|
467 | + * @throws EE_Error |
|
468 | + * @throws InvalidArgumentException |
|
469 | + * @throws InvalidDataTypeException |
|
470 | + * @throws InvalidInterfaceException |
|
471 | + */ |
|
472 | + private function _get_transactions_in_progress($comparison = '>=') |
|
473 | + { |
|
474 | + $comparison = $comparison === '>=' || $comparison === '<=' |
|
475 | + ? $comparison |
|
476 | + : '>='; |
|
477 | + /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */ |
|
478 | + $session_lifespan = LoaderFactory::getLoader()->getShared( |
|
479 | + 'EventEspresso\core\domain\values\session\SessionLifespan' |
|
480 | + ); |
|
481 | + return $this->get_all( |
|
482 | + [ |
|
483 | + [ |
|
484 | + 'TXN_timestamp' => [ |
|
485 | + $comparison, |
|
486 | + $session_lifespan->expiration(), |
|
487 | + ], |
|
488 | + 'STS_ID' => [ |
|
489 | + '!=', |
|
490 | + EEM_Transaction::complete_status_code, |
|
491 | + ], |
|
492 | + ], |
|
493 | + ] |
|
494 | + ); |
|
495 | + } |
|
496 | 496 | } |
@@ -67,12 +67,12 @@ discard block |
||
67 | 67 | public function heartbeatResponse($response = array(), $data = array()) |
68 | 68 | { |
69 | 69 | // does this heartbeat contain our data ? |
70 | - if (! isset($data[ $this->heartbeat ])) { |
|
70 | + if ( ! isset($data[$this->heartbeat])) { |
|
71 | 71 | return $response; |
72 | 72 | } |
73 | 73 | // check for reg_url_link in the incoming heartbeat data |
74 | - if (! isset($data[ $this->heartbeat ]['e_reg_url_link'])) { |
|
75 | - $response[ $this->heartbeat ] = array( |
|
74 | + if ( ! isset($data[$this->heartbeat]['e_reg_url_link'])) { |
|
75 | + $response[$this->heartbeat] = array( |
|
76 | 76 | 'errors' => ! empty($notices['errors']) |
77 | 77 | ? $notices['errors'] |
78 | 78 | : esc_html__( |
@@ -85,22 +85,22 @@ discard block |
||
85 | 85 | // kk heartbeat has our data |
86 | 86 | $response = $this->initializeThankYouPageAndTransaction($response, $data); |
87 | 87 | // if something went wrong... |
88 | - if (isset($response[ $this->heartbeat ]['errors'])) { |
|
88 | + if (isset($response[$this->heartbeat]['errors'])) { |
|
89 | 89 | return $response; |
90 | 90 | } |
91 | 91 | // grab transient of Transaction's status |
92 | - $txn_status = isset($data[ $this->heartbeat ]['txn_status']) |
|
93 | - ? $data[ $this->heartbeat ]['txn_status'] |
|
92 | + $txn_status = isset($data[$this->heartbeat]['txn_status']) |
|
93 | + ? $data[$this->heartbeat]['txn_status'] |
|
94 | 94 | : null; |
95 | 95 | $response = $this->getTransactionDetails($txn_status, $response, $data); |
96 | 96 | // no payment data yet? |
97 | - if (isset($response[ $this->heartbeat ]['still_waiting'])) { |
|
97 | + if (isset($response[$this->heartbeat]['still_waiting'])) { |
|
98 | 98 | return $response; |
99 | 99 | } |
100 | 100 | // TXN is happening so let's get the payments now |
101 | 101 | // if we've already gotten payments then the heartbeat data will contain the timestamp of the last time we checked |
102 | - $since = isset($data[ $this->heartbeat ]['get_payments_since']) |
|
103 | - ? $data[ $this->heartbeat ]['get_payments_since'] |
|
102 | + $since = isset($data[$this->heartbeat]['get_payments_since']) |
|
103 | + ? $data[$this->heartbeat]['get_payments_since'] |
|
104 | 104 | : 0; |
105 | 105 | return $this->paymentDetails($response, $since); |
106 | 106 | } |
@@ -117,18 +117,18 @@ discard block |
||
117 | 117 | */ |
118 | 118 | private function initializeThankYouPageAndTransaction($response, $data) |
119 | 119 | { |
120 | - require_once EE_MODULES . 'thank_you_page/EED_Thank_You_Page.module.php'; |
|
120 | + require_once EE_MODULES.'thank_you_page/EED_Thank_You_Page.module.php'; |
|
121 | 121 | // set_definitions, instantiate the thank you page class, and get the ball rolling |
122 | 122 | EED_Thank_You_Page::set_definitions(); |
123 | 123 | $this->thank_you_page = EED_Thank_You_Page::instance(); |
124 | - $this->thank_you_page->set_reg_url_link($data[ $this->heartbeat ]['e_reg_url_link']); |
|
124 | + $this->thank_you_page->set_reg_url_link($data[$this->heartbeat]['e_reg_url_link']); |
|
125 | 125 | $this->thank_you_page->init(); |
126 | 126 | // get TXN |
127 | 127 | $transaction = $this->thank_you_page->get_txn(); |
128 | 128 | // no TXN? then get out |
129 | - if (! $transaction instanceof EE_Transaction) { |
|
129 | + if ( ! $transaction instanceof EE_Transaction) { |
|
130 | 130 | $notices = EE_Error::get_notices(); |
131 | - $response[ $this->heartbeat ] = array( |
|
131 | + $response[$this->heartbeat] = array( |
|
132 | 132 | 'errors' => ! empty($notices['errors']) |
133 | 133 | ? $notices['errors'] |
134 | 134 | : sprintf( |
@@ -173,12 +173,12 @@ discard block |
||
173 | 173 | case EEM_Transaction::failed_status_code: |
174 | 174 | default: |
175 | 175 | // keep on waiting... |
176 | - return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
176 | + return $this->updateServerWaitTime($data[$this->heartbeat]); |
|
177 | 177 | } |
178 | 178 | // or is the TXN still failed (never been updated) ??? |
179 | 179 | } elseif ($this->transaction->failed()) { |
180 | 180 | // keep on waiting... |
181 | - return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
181 | + return $this->updateServerWaitTime($data[$this->heartbeat]); |
|
182 | 182 | } |
183 | 183 | return $response; |
184 | 184 | } |
@@ -196,11 +196,11 @@ discard block |
||
196 | 196 | */ |
197 | 197 | private function setTransactionDetails($response, $status_only = false) |
198 | 198 | { |
199 | - if (! $status_only && ! isset($response[ $this->heartbeat ]['transaction_details'])) { |
|
200 | - $response[ $this->heartbeat ]['transaction_details'] = $this->thank_you_page->get_transaction_details(); |
|
199 | + if ( ! $status_only && ! isset($response[$this->heartbeat]['transaction_details'])) { |
|
200 | + $response[$this->heartbeat]['transaction_details'] = $this->thank_you_page->get_transaction_details(); |
|
201 | 201 | } |
202 | - if (! isset($response[ $this->heartbeat ]['txn_status'])) { |
|
203 | - $response[ $this->heartbeat ]['txn_status'] = $this->transaction->status_ID(); |
|
202 | + if ( ! isset($response[$this->heartbeat]['txn_status'])) { |
|
203 | + $response[$this->heartbeat]['txn_status'] = $this->transaction->status_ID(); |
|
204 | 204 | } |
205 | 205 | return $response; |
206 | 206 | } |
@@ -221,19 +221,19 @@ discard block |
||
221 | 221 | // then check for payments |
222 | 222 | $payments = $this->thank_you_page->get_txn_payments($since); |
223 | 223 | // has a payment been processed ? |
224 | - if (! empty($payments) || $this->thank_you_page->isOfflinePaymentMethod()) { |
|
224 | + if ( ! empty($payments) || $this->thank_you_page->isOfflinePaymentMethod()) { |
|
225 | 225 | if ($since) { |
226 | - $response[ $this->heartbeat ]['new_payments'] = $this->thank_you_page->get_new_payments($payments); |
|
226 | + $response[$this->heartbeat]['new_payments'] = $this->thank_you_page->get_new_payments($payments); |
|
227 | 227 | $response = $this->setTransactionDetails($response); |
228 | 228 | } else { |
229 | - $response[ $this->heartbeat ]['payment_details'] = $this->thank_you_page->get_payment_details( |
|
229 | + $response[$this->heartbeat]['payment_details'] = $this->thank_you_page->get_payment_details( |
|
230 | 230 | $payments |
231 | 231 | ); |
232 | 232 | } |
233 | 233 | // reset time to check for payments |
234 | - $response[ $this->heartbeat ]['get_payments_since'] = time(); |
|
234 | + $response[$this->heartbeat]['get_payments_since'] = time(); |
|
235 | 235 | } else { |
236 | - $response[ $this->heartbeat ]['get_payments_since'] = $since; |
|
236 | + $response[$this->heartbeat]['get_payments_since'] = $since; |
|
237 | 237 | } |
238 | 238 | return $response; |
239 | 239 | } |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | */ |
252 | 252 | private function updateServerWaitTime($thank_you_page_data) |
253 | 253 | { |
254 | - $response[ $this->heartbeat ]['still_waiting'] = isset($thank_you_page_data['initial_access']) |
|
254 | + $response[$this->heartbeat]['still_waiting'] = isset($thank_you_page_data['initial_access']) |
|
255 | 255 | ? time() - $thank_you_page_data['initial_access'] |
256 | 256 | : 0; |
257 | 257 | $response = $this->setTransactionDetails($response, true); |
@@ -21,239 +21,239 @@ |
||
21 | 21 | */ |
22 | 22 | class ThankYouPageIpnMonitor |
23 | 23 | { |
24 | - /** |
|
25 | - * @var string $heartbeat |
|
26 | - */ |
|
27 | - private $heartbeat; |
|
24 | + /** |
|
25 | + * @var string $heartbeat |
|
26 | + */ |
|
27 | + private $heartbeat; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var EED_Thank_You_Page $thank_you_page |
|
31 | - */ |
|
32 | - private $thank_you_page; |
|
29 | + /** |
|
30 | + * @var EED_Thank_You_Page $thank_you_page |
|
31 | + */ |
|
32 | + private $thank_you_page; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var EE_Transaction $transaction |
|
36 | - */ |
|
37 | - private $transaction; |
|
34 | + /** |
|
35 | + * @var EE_Transaction $transaction |
|
36 | + */ |
|
37 | + private $transaction; |
|
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * EventEditorHeartbeat constructor. |
|
42 | - */ |
|
43 | - public function __construct() |
|
44 | - { |
|
45 | - $this->heartbeat = WordpressHeartbeat::RESPONSE_KEY_THANK_YOU_PAGE; |
|
46 | - add_filter('heartbeat_received', array($this, 'heartbeatResponse'), 10, 3); |
|
47 | - add_filter('heartbeat_nopriv_received', array($this, 'heartbeatResponse'), 10, 3); |
|
48 | - } |
|
40 | + /** |
|
41 | + * EventEditorHeartbeat constructor. |
|
42 | + */ |
|
43 | + public function __construct() |
|
44 | + { |
|
45 | + $this->heartbeat = WordpressHeartbeat::RESPONSE_KEY_THANK_YOU_PAGE; |
|
46 | + add_filter('heartbeat_received', array($this, 'heartbeatResponse'), 10, 3); |
|
47 | + add_filter('heartbeat_nopriv_received', array($this, 'heartbeatResponse'), 10, 3); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * thank_you_page_IPN_monitor |
|
53 | - * this basically just pulls the TXN based on the reg_url_link sent from the server, |
|
54 | - * then checks that the TXN status is not failed, and that no other errors have been generated. |
|
55 | - * it also calculates the IPN wait time since the Thank You page was first loaded |
|
56 | - * |
|
57 | - * @param array $response |
|
58 | - * @param array $data |
|
59 | - * @return array |
|
60 | - * @throws EE_Error |
|
61 | - * @throws InvalidArgumentException |
|
62 | - * @throws InvalidDataTypeException |
|
63 | - * @throws InvalidInterfaceException |
|
64 | - * @throws ReflectionException |
|
65 | - */ |
|
66 | - public function heartbeatResponse($response = array(), $data = array()) |
|
67 | - { |
|
68 | - // does this heartbeat contain our data ? |
|
69 | - if (! isset($data[ $this->heartbeat ])) { |
|
70 | - return $response; |
|
71 | - } |
|
72 | - // check for reg_url_link in the incoming heartbeat data |
|
73 | - if (! isset($data[ $this->heartbeat ]['e_reg_url_link'])) { |
|
74 | - $response[ $this->heartbeat ] = array( |
|
75 | - 'errors' => ! empty($notices['errors']) |
|
76 | - ? $notices['errors'] |
|
77 | - : esc_html__( |
|
78 | - 'No transaction information could be retrieved because the registration URL link is missing or invalid.', |
|
79 | - 'event_espresso' |
|
80 | - ), |
|
81 | - ); |
|
82 | - return $response; |
|
83 | - } |
|
84 | - // kk heartbeat has our data |
|
85 | - $response = $this->initializeThankYouPageAndTransaction($response, $data); |
|
86 | - // if something went wrong... |
|
87 | - if (isset($response[ $this->heartbeat ]['errors'])) { |
|
88 | - return $response; |
|
89 | - } |
|
90 | - // grab transient of Transaction's status |
|
91 | - $txn_status = isset($data[ $this->heartbeat ]['txn_status']) |
|
92 | - ? $data[ $this->heartbeat ]['txn_status'] |
|
93 | - : null; |
|
94 | - $response = $this->getTransactionDetails($txn_status, $response, $data); |
|
95 | - // no payment data yet? |
|
96 | - if (isset($response[ $this->heartbeat ]['still_waiting'])) { |
|
97 | - return $response; |
|
98 | - } |
|
99 | - // TXN is happening so let's get the payments now |
|
100 | - // if we've already gotten payments then the heartbeat data will contain the timestamp of the last time we checked |
|
101 | - $since = isset($data[ $this->heartbeat ]['get_payments_since']) |
|
102 | - ? $data[ $this->heartbeat ]['get_payments_since'] |
|
103 | - : 0; |
|
104 | - return $this->paymentDetails($response, $since); |
|
105 | - } |
|
51 | + /** |
|
52 | + * thank_you_page_IPN_monitor |
|
53 | + * this basically just pulls the TXN based on the reg_url_link sent from the server, |
|
54 | + * then checks that the TXN status is not failed, and that no other errors have been generated. |
|
55 | + * it also calculates the IPN wait time since the Thank You page was first loaded |
|
56 | + * |
|
57 | + * @param array $response |
|
58 | + * @param array $data |
|
59 | + * @return array |
|
60 | + * @throws EE_Error |
|
61 | + * @throws InvalidArgumentException |
|
62 | + * @throws InvalidDataTypeException |
|
63 | + * @throws InvalidInterfaceException |
|
64 | + * @throws ReflectionException |
|
65 | + */ |
|
66 | + public function heartbeatResponse($response = array(), $data = array()) |
|
67 | + { |
|
68 | + // does this heartbeat contain our data ? |
|
69 | + if (! isset($data[ $this->heartbeat ])) { |
|
70 | + return $response; |
|
71 | + } |
|
72 | + // check for reg_url_link in the incoming heartbeat data |
|
73 | + if (! isset($data[ $this->heartbeat ]['e_reg_url_link'])) { |
|
74 | + $response[ $this->heartbeat ] = array( |
|
75 | + 'errors' => ! empty($notices['errors']) |
|
76 | + ? $notices['errors'] |
|
77 | + : esc_html__( |
|
78 | + 'No transaction information could be retrieved because the registration URL link is missing or invalid.', |
|
79 | + 'event_espresso' |
|
80 | + ), |
|
81 | + ); |
|
82 | + return $response; |
|
83 | + } |
|
84 | + // kk heartbeat has our data |
|
85 | + $response = $this->initializeThankYouPageAndTransaction($response, $data); |
|
86 | + // if something went wrong... |
|
87 | + if (isset($response[ $this->heartbeat ]['errors'])) { |
|
88 | + return $response; |
|
89 | + } |
|
90 | + // grab transient of Transaction's status |
|
91 | + $txn_status = isset($data[ $this->heartbeat ]['txn_status']) |
|
92 | + ? $data[ $this->heartbeat ]['txn_status'] |
|
93 | + : null; |
|
94 | + $response = $this->getTransactionDetails($txn_status, $response, $data); |
|
95 | + // no payment data yet? |
|
96 | + if (isset($response[ $this->heartbeat ]['still_waiting'])) { |
|
97 | + return $response; |
|
98 | + } |
|
99 | + // TXN is happening so let's get the payments now |
|
100 | + // if we've already gotten payments then the heartbeat data will contain the timestamp of the last time we checked |
|
101 | + $since = isset($data[ $this->heartbeat ]['get_payments_since']) |
|
102 | + ? $data[ $this->heartbeat ]['get_payments_since'] |
|
103 | + : 0; |
|
104 | + return $this->paymentDetails($response, $since); |
|
105 | + } |
|
106 | 106 | |
107 | 107 | |
108 | - /** |
|
109 | - * @param array $response |
|
110 | - * @param array $data |
|
111 | - * @return array |
|
112 | - * @throws EE_Error |
|
113 | - * @throws InvalidArgumentException |
|
114 | - * @throws InvalidDataTypeException |
|
115 | - * @throws InvalidInterfaceException |
|
116 | - */ |
|
117 | - private function initializeThankYouPageAndTransaction($response, $data) |
|
118 | - { |
|
119 | - require_once EE_MODULES . 'thank_you_page/EED_Thank_You_Page.module.php'; |
|
120 | - // set_definitions, instantiate the thank you page class, and get the ball rolling |
|
121 | - EED_Thank_You_Page::set_definitions(); |
|
122 | - $this->thank_you_page = EED_Thank_You_Page::instance(); |
|
123 | - $this->thank_you_page->set_reg_url_link($data[ $this->heartbeat ]['e_reg_url_link']); |
|
124 | - $this->thank_you_page->init(); |
|
125 | - // get TXN |
|
126 | - $transaction = $this->thank_you_page->get_txn(); |
|
127 | - // no TXN? then get out |
|
128 | - if (! $transaction instanceof EE_Transaction) { |
|
129 | - $notices = EE_Error::get_notices(); |
|
130 | - $response[ $this->heartbeat ] = array( |
|
131 | - 'errors' => ! empty($notices['errors']) |
|
132 | - ? $notices['errors'] |
|
133 | - : sprintf( |
|
134 | - esc_html__( |
|
135 | - 'The information for your transaction could not be retrieved from the server or the transaction data received was invalid because of a technical reason. (%s)', |
|
136 | - 'event_espresso' |
|
137 | - ), |
|
138 | - __LINE__ |
|
139 | - ), |
|
140 | - ); |
|
141 | - return $response; |
|
142 | - } |
|
143 | - $this->transaction = $transaction; |
|
144 | - return $response; |
|
145 | - } |
|
108 | + /** |
|
109 | + * @param array $response |
|
110 | + * @param array $data |
|
111 | + * @return array |
|
112 | + * @throws EE_Error |
|
113 | + * @throws InvalidArgumentException |
|
114 | + * @throws InvalidDataTypeException |
|
115 | + * @throws InvalidInterfaceException |
|
116 | + */ |
|
117 | + private function initializeThankYouPageAndTransaction($response, $data) |
|
118 | + { |
|
119 | + require_once EE_MODULES . 'thank_you_page/EED_Thank_You_Page.module.php'; |
|
120 | + // set_definitions, instantiate the thank you page class, and get the ball rolling |
|
121 | + EED_Thank_You_Page::set_definitions(); |
|
122 | + $this->thank_you_page = EED_Thank_You_Page::instance(); |
|
123 | + $this->thank_you_page->set_reg_url_link($data[ $this->heartbeat ]['e_reg_url_link']); |
|
124 | + $this->thank_you_page->init(); |
|
125 | + // get TXN |
|
126 | + $transaction = $this->thank_you_page->get_txn(); |
|
127 | + // no TXN? then get out |
|
128 | + if (! $transaction instanceof EE_Transaction) { |
|
129 | + $notices = EE_Error::get_notices(); |
|
130 | + $response[ $this->heartbeat ] = array( |
|
131 | + 'errors' => ! empty($notices['errors']) |
|
132 | + ? $notices['errors'] |
|
133 | + : sprintf( |
|
134 | + esc_html__( |
|
135 | + 'The information for your transaction could not be retrieved from the server or the transaction data received was invalid because of a technical reason. (%s)', |
|
136 | + 'event_espresso' |
|
137 | + ), |
|
138 | + __LINE__ |
|
139 | + ), |
|
140 | + ); |
|
141 | + return $response; |
|
142 | + } |
|
143 | + $this->transaction = $transaction; |
|
144 | + return $response; |
|
145 | + } |
|
146 | 146 | |
147 | 147 | |
148 | - /** |
|
149 | - * @param string $txn_status |
|
150 | - * @param array $response |
|
151 | - * @param array $data |
|
152 | - * @return array |
|
153 | - * @throws EE_Error |
|
154 | - * @throws InvalidArgumentException |
|
155 | - * @throws InvalidDataTypeException |
|
156 | - * @throws InvalidInterfaceException |
|
157 | - * @throws ReflectionException |
|
158 | - */ |
|
159 | - private function getTransactionDetails($txn_status, $response, $data) |
|
160 | - { |
|
161 | - // has the TXN status changed since we last checked (or empty because this is the first time running through this code)? |
|
162 | - if ($txn_status !== $this->transaction->status_ID()) { |
|
163 | - // switch between two possible basic outcomes |
|
164 | - switch ($this->transaction->status_ID()) { |
|
165 | - // TXN has been updated in some way |
|
166 | - case EEM_Transaction::overpaid_status_code: |
|
167 | - case EEM_Transaction::complete_status_code: |
|
168 | - case EEM_Transaction::incomplete_status_code: |
|
169 | - // send updated TXN results back to client, |
|
170 | - return $this->setTransactionDetails($response); |
|
171 | - // or we have a bad TXN, or really slow IPN, so calculate the wait time and send that back... |
|
172 | - case EEM_Transaction::failed_status_code: |
|
173 | - default: |
|
174 | - // keep on waiting... |
|
175 | - return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
176 | - } |
|
177 | - // or is the TXN still failed (never been updated) ??? |
|
178 | - } elseif ($this->transaction->failed()) { |
|
179 | - // keep on waiting... |
|
180 | - return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
181 | - } |
|
182 | - return $response; |
|
183 | - } |
|
148 | + /** |
|
149 | + * @param string $txn_status |
|
150 | + * @param array $response |
|
151 | + * @param array $data |
|
152 | + * @return array |
|
153 | + * @throws EE_Error |
|
154 | + * @throws InvalidArgumentException |
|
155 | + * @throws InvalidDataTypeException |
|
156 | + * @throws InvalidInterfaceException |
|
157 | + * @throws ReflectionException |
|
158 | + */ |
|
159 | + private function getTransactionDetails($txn_status, $response, $data) |
|
160 | + { |
|
161 | + // has the TXN status changed since we last checked (or empty because this is the first time running through this code)? |
|
162 | + if ($txn_status !== $this->transaction->status_ID()) { |
|
163 | + // switch between two possible basic outcomes |
|
164 | + switch ($this->transaction->status_ID()) { |
|
165 | + // TXN has been updated in some way |
|
166 | + case EEM_Transaction::overpaid_status_code: |
|
167 | + case EEM_Transaction::complete_status_code: |
|
168 | + case EEM_Transaction::incomplete_status_code: |
|
169 | + // send updated TXN results back to client, |
|
170 | + return $this->setTransactionDetails($response); |
|
171 | + // or we have a bad TXN, or really slow IPN, so calculate the wait time and send that back... |
|
172 | + case EEM_Transaction::failed_status_code: |
|
173 | + default: |
|
174 | + // keep on waiting... |
|
175 | + return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
176 | + } |
|
177 | + // or is the TXN still failed (never been updated) ??? |
|
178 | + } elseif ($this->transaction->failed()) { |
|
179 | + // keep on waiting... |
|
180 | + return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
181 | + } |
|
182 | + return $response; |
|
183 | + } |
|
184 | 184 | |
185 | 185 | |
186 | - /** |
|
187 | - * @param array $response |
|
188 | - * @param boolean $status_only |
|
189 | - * @return array |
|
190 | - * @throws EE_Error |
|
191 | - * @throws InvalidArgumentException |
|
192 | - * @throws InvalidDataTypeException |
|
193 | - * @throws InvalidInterfaceException |
|
194 | - * @throws ReflectionException |
|
195 | - */ |
|
196 | - private function setTransactionDetails($response, $status_only = false) |
|
197 | - { |
|
198 | - if (! $status_only && ! isset($response[ $this->heartbeat ]['transaction_details'])) { |
|
199 | - $response[ $this->heartbeat ]['transaction_details'] = $this->thank_you_page->get_transaction_details(); |
|
200 | - } |
|
201 | - if (! isset($response[ $this->heartbeat ]['txn_status'])) { |
|
202 | - $response[ $this->heartbeat ]['txn_status'] = $this->transaction->status_ID(); |
|
203 | - } |
|
204 | - return $response; |
|
205 | - } |
|
186 | + /** |
|
187 | + * @param array $response |
|
188 | + * @param boolean $status_only |
|
189 | + * @return array |
|
190 | + * @throws EE_Error |
|
191 | + * @throws InvalidArgumentException |
|
192 | + * @throws InvalidDataTypeException |
|
193 | + * @throws InvalidInterfaceException |
|
194 | + * @throws ReflectionException |
|
195 | + */ |
|
196 | + private function setTransactionDetails($response, $status_only = false) |
|
197 | + { |
|
198 | + if (! $status_only && ! isset($response[ $this->heartbeat ]['transaction_details'])) { |
|
199 | + $response[ $this->heartbeat ]['transaction_details'] = $this->thank_you_page->get_transaction_details(); |
|
200 | + } |
|
201 | + if (! isset($response[ $this->heartbeat ]['txn_status'])) { |
|
202 | + $response[ $this->heartbeat ]['txn_status'] = $this->transaction->status_ID(); |
|
203 | + } |
|
204 | + return $response; |
|
205 | + } |
|
206 | 206 | |
207 | 207 | |
208 | - /** |
|
209 | - * @param array $response |
|
210 | - * @param int $since |
|
211 | - * @return array |
|
212 | - * @throws EE_Error |
|
213 | - * @throws InvalidArgumentException |
|
214 | - * @throws InvalidDataTypeException |
|
215 | - * @throws InvalidInterfaceException |
|
216 | - * @throws ReflectionException |
|
217 | - */ |
|
218 | - private function paymentDetails($response, $since) |
|
219 | - { |
|
220 | - // then check for payments |
|
221 | - $payments = $this->thank_you_page->get_txn_payments($since); |
|
222 | - // has a payment been processed ? |
|
223 | - if (! empty($payments) || $this->thank_you_page->isOfflinePaymentMethod()) { |
|
224 | - if ($since) { |
|
225 | - $response[ $this->heartbeat ]['new_payments'] = $this->thank_you_page->get_new_payments($payments); |
|
226 | - $response = $this->setTransactionDetails($response); |
|
227 | - } else { |
|
228 | - $response[ $this->heartbeat ]['payment_details'] = $this->thank_you_page->get_payment_details( |
|
229 | - $payments |
|
230 | - ); |
|
231 | - } |
|
232 | - // reset time to check for payments |
|
233 | - $response[ $this->heartbeat ]['get_payments_since'] = time(); |
|
234 | - } else { |
|
235 | - $response[ $this->heartbeat ]['get_payments_since'] = $since; |
|
236 | - } |
|
237 | - return $response; |
|
238 | - } |
|
208 | + /** |
|
209 | + * @param array $response |
|
210 | + * @param int $since |
|
211 | + * @return array |
|
212 | + * @throws EE_Error |
|
213 | + * @throws InvalidArgumentException |
|
214 | + * @throws InvalidDataTypeException |
|
215 | + * @throws InvalidInterfaceException |
|
216 | + * @throws ReflectionException |
|
217 | + */ |
|
218 | + private function paymentDetails($response, $since) |
|
219 | + { |
|
220 | + // then check for payments |
|
221 | + $payments = $this->thank_you_page->get_txn_payments($since); |
|
222 | + // has a payment been processed ? |
|
223 | + if (! empty($payments) || $this->thank_you_page->isOfflinePaymentMethod()) { |
|
224 | + if ($since) { |
|
225 | + $response[ $this->heartbeat ]['new_payments'] = $this->thank_you_page->get_new_payments($payments); |
|
226 | + $response = $this->setTransactionDetails($response); |
|
227 | + } else { |
|
228 | + $response[ $this->heartbeat ]['payment_details'] = $this->thank_you_page->get_payment_details( |
|
229 | + $payments |
|
230 | + ); |
|
231 | + } |
|
232 | + // reset time to check for payments |
|
233 | + $response[ $this->heartbeat ]['get_payments_since'] = time(); |
|
234 | + } else { |
|
235 | + $response[ $this->heartbeat ]['get_payments_since'] = $since; |
|
236 | + } |
|
237 | + return $response; |
|
238 | + } |
|
239 | 239 | |
240 | 240 | |
241 | - /** |
|
242 | - * @param array $thank_you_page_data thank you page portion of the incoming JSON array |
|
243 | - * from the WP heartbeat data |
|
244 | - * @return array |
|
245 | - * @throws EE_Error |
|
246 | - * @throws InvalidArgumentException |
|
247 | - * @throws InvalidDataTypeException |
|
248 | - * @throws InvalidInterfaceException |
|
249 | - * @throws ReflectionException |
|
250 | - */ |
|
251 | - private function updateServerWaitTime($thank_you_page_data) |
|
252 | - { |
|
253 | - $response[ $this->heartbeat ]['still_waiting'] = isset($thank_you_page_data['initial_access']) |
|
254 | - ? time() - $thank_you_page_data['initial_access'] |
|
255 | - : 0; |
|
256 | - $response = $this->setTransactionDetails($response, true); |
|
257 | - return $response; |
|
258 | - } |
|
241 | + /** |
|
242 | + * @param array $thank_you_page_data thank you page portion of the incoming JSON array |
|
243 | + * from the WP heartbeat data |
|
244 | + * @return array |
|
245 | + * @throws EE_Error |
|
246 | + * @throws InvalidArgumentException |
|
247 | + * @throws InvalidDataTypeException |
|
248 | + * @throws InvalidInterfaceException |
|
249 | + * @throws ReflectionException |
|
250 | + */ |
|
251 | + private function updateServerWaitTime($thank_you_page_data) |
|
252 | + { |
|
253 | + $response[ $this->heartbeat ]['still_waiting'] = isset($thank_you_page_data['initial_access']) |
|
254 | + ? time() - $thank_you_page_data['initial_access'] |
|
255 | + : 0; |
|
256 | + $response = $this->setTransactionDetails($response, true); |
|
257 | + return $response; |
|
258 | + } |
|
259 | 259 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | ); |
89 | 89 | $success = \EEH_Line_Item::add_item($transaction->total_line_item(), $cancelled_line_item); |
90 | 90 | } |
91 | - if (! $success) { |
|
91 | + if ( ! $success) { |
|
92 | 92 | throw new \RuntimeException( |
93 | 93 | sprintf( |
94 | 94 | esc_html__('An error occurred while attempting to cancel ticket line item %1$s', 'event_espresso'), |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | break; |
126 | 126 | } |
127 | 127 | } |
128 | - if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) { |
|
128 | + if ( ! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) { |
|
129 | 129 | throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID()); |
130 | 130 | } |
131 | 131 | return $line_item; |
@@ -18,114 +18,114 @@ |
||
18 | 18 | */ |
19 | 19 | class CancelTicketLineItemService extends DomainService |
20 | 20 | { |
21 | - /** |
|
22 | - * @param \EE_Registration $registration |
|
23 | - * @param int $quantity |
|
24 | - * @return bool|int |
|
25 | - */ |
|
26 | - public function forRegistration(\EE_Registration $registration, $quantity = 1) |
|
27 | - { |
|
28 | - return $this->cancel( |
|
29 | - $registration->transaction(), |
|
30 | - $registration->ticket(), |
|
31 | - $quantity, |
|
32 | - $registration->ticket_line_item() |
|
33 | - ); |
|
34 | - } |
|
21 | + /** |
|
22 | + * @param \EE_Registration $registration |
|
23 | + * @param int $quantity |
|
24 | + * @return bool|int |
|
25 | + */ |
|
26 | + public function forRegistration(\EE_Registration $registration, $quantity = 1) |
|
27 | + { |
|
28 | + return $this->cancel( |
|
29 | + $registration->transaction(), |
|
30 | + $registration->ticket(), |
|
31 | + $quantity, |
|
32 | + $registration->ticket_line_item() |
|
33 | + ); |
|
34 | + } |
|
35 | 35 | |
36 | 36 | |
37 | - /** |
|
38 | - * @param \EE_Transaction $transaction |
|
39 | - * @param \EE_Ticket $ticket |
|
40 | - * @param int $quantity |
|
41 | - * @param \EE_Line_Item $ticket_line_item |
|
42 | - * @return bool|int |
|
43 | - */ |
|
44 | - public function cancel( |
|
45 | - \EE_Transaction $transaction, |
|
46 | - \EE_Ticket $ticket, |
|
47 | - $quantity = 1, |
|
48 | - \EE_Line_Item $ticket_line_item = null |
|
49 | - ) { |
|
50 | - $ticket_line_item = $ticket_line_item instanceof \EE_Line_Item |
|
51 | - ? $ticket_line_item |
|
52 | - : $this->getTicketLineItem($transaction, $ticket); |
|
53 | - // first we need to decrement the ticket quantity |
|
54 | - \EEH_Line_Item::decrement_quantity($ticket_line_item, $quantity); |
|
55 | - // no tickets left for this line item ? |
|
56 | - if ((int) $ticket_line_item->quantity() === 0) { |
|
57 | - // then just set this line item as cancelled, save, and get out |
|
58 | - $ticket_line_item->set_type(\EEM_Line_Item::type_cancellation); |
|
59 | - $success = $ticket_line_item->save(); |
|
60 | - } else { |
|
61 | - // otherwise create a new cancelled line item, so that we have a record of the cancellation |
|
62 | - $items_subtotal = \EEH_Line_Item::get_pre_tax_subtotal( |
|
63 | - \EEH_Line_Item::get_event_line_item_for_ticket( |
|
64 | - $transaction->total_line_item(), |
|
65 | - $ticket |
|
66 | - ) |
|
67 | - ); |
|
68 | - $cancelled_line_item = \EE_Line_Item::new_instance( |
|
69 | - array( |
|
70 | - 'LIN_name' => $ticket_line_item->name(), |
|
71 | - 'LIN_desc' => sprintf( |
|
72 | - esc_html__('%1$s Cancelled: %2$s', 'event_espresso'), |
|
73 | - $ticket_line_item->desc(), |
|
74 | - date('Y-m-d h:i a') |
|
75 | - ), |
|
76 | - 'LIN_unit_price' => (float) $ticket_line_item->unit_price(), |
|
77 | - 'LIN_quantity' => $quantity, |
|
78 | - 'LIN_percent' => null, |
|
79 | - 'LIN_is_taxable' => false, |
|
80 | - 'LIN_order' => $items_subtotal instanceof \EE_Line_Item |
|
81 | - ? count($items_subtotal->children()) |
|
82 | - : 0, |
|
83 | - 'LIN_total' => (float) $ticket_line_item->unit_price(), |
|
84 | - 'LIN_type' => \EEM_Line_Item::type_cancellation, |
|
85 | - ) |
|
86 | - ); |
|
87 | - $success = \EEH_Line_Item::add_item($transaction->total_line_item(), $cancelled_line_item); |
|
88 | - } |
|
89 | - if (! $success) { |
|
90 | - throw new \RuntimeException( |
|
91 | - sprintf( |
|
92 | - esc_html__('An error occurred while attempting to cancel ticket line item %1$s', 'event_espresso'), |
|
93 | - $ticket_line_item->ID() |
|
94 | - ) |
|
95 | - ); |
|
96 | - } |
|
97 | - return $success; |
|
98 | - } |
|
37 | + /** |
|
38 | + * @param \EE_Transaction $transaction |
|
39 | + * @param \EE_Ticket $ticket |
|
40 | + * @param int $quantity |
|
41 | + * @param \EE_Line_Item $ticket_line_item |
|
42 | + * @return bool|int |
|
43 | + */ |
|
44 | + public function cancel( |
|
45 | + \EE_Transaction $transaction, |
|
46 | + \EE_Ticket $ticket, |
|
47 | + $quantity = 1, |
|
48 | + \EE_Line_Item $ticket_line_item = null |
|
49 | + ) { |
|
50 | + $ticket_line_item = $ticket_line_item instanceof \EE_Line_Item |
|
51 | + ? $ticket_line_item |
|
52 | + : $this->getTicketLineItem($transaction, $ticket); |
|
53 | + // first we need to decrement the ticket quantity |
|
54 | + \EEH_Line_Item::decrement_quantity($ticket_line_item, $quantity); |
|
55 | + // no tickets left for this line item ? |
|
56 | + if ((int) $ticket_line_item->quantity() === 0) { |
|
57 | + // then just set this line item as cancelled, save, and get out |
|
58 | + $ticket_line_item->set_type(\EEM_Line_Item::type_cancellation); |
|
59 | + $success = $ticket_line_item->save(); |
|
60 | + } else { |
|
61 | + // otherwise create a new cancelled line item, so that we have a record of the cancellation |
|
62 | + $items_subtotal = \EEH_Line_Item::get_pre_tax_subtotal( |
|
63 | + \EEH_Line_Item::get_event_line_item_for_ticket( |
|
64 | + $transaction->total_line_item(), |
|
65 | + $ticket |
|
66 | + ) |
|
67 | + ); |
|
68 | + $cancelled_line_item = \EE_Line_Item::new_instance( |
|
69 | + array( |
|
70 | + 'LIN_name' => $ticket_line_item->name(), |
|
71 | + 'LIN_desc' => sprintf( |
|
72 | + esc_html__('%1$s Cancelled: %2$s', 'event_espresso'), |
|
73 | + $ticket_line_item->desc(), |
|
74 | + date('Y-m-d h:i a') |
|
75 | + ), |
|
76 | + 'LIN_unit_price' => (float) $ticket_line_item->unit_price(), |
|
77 | + 'LIN_quantity' => $quantity, |
|
78 | + 'LIN_percent' => null, |
|
79 | + 'LIN_is_taxable' => false, |
|
80 | + 'LIN_order' => $items_subtotal instanceof \EE_Line_Item |
|
81 | + ? count($items_subtotal->children()) |
|
82 | + : 0, |
|
83 | + 'LIN_total' => (float) $ticket_line_item->unit_price(), |
|
84 | + 'LIN_type' => \EEM_Line_Item::type_cancellation, |
|
85 | + ) |
|
86 | + ); |
|
87 | + $success = \EEH_Line_Item::add_item($transaction->total_line_item(), $cancelled_line_item); |
|
88 | + } |
|
89 | + if (! $success) { |
|
90 | + throw new \RuntimeException( |
|
91 | + sprintf( |
|
92 | + esc_html__('An error occurred while attempting to cancel ticket line item %1$s', 'event_espresso'), |
|
93 | + $ticket_line_item->ID() |
|
94 | + ) |
|
95 | + ); |
|
96 | + } |
|
97 | + return $success; |
|
98 | + } |
|
99 | 99 | |
100 | 100 | |
101 | - /** |
|
102 | - * @param \EE_Transaction $transaction |
|
103 | - * @param \EE_Ticket $ticket |
|
104 | - * @return \EE_Line_Item |
|
105 | - * @throws EntityNotFoundException |
|
106 | - * @throws \EE_Error |
|
107 | - */ |
|
108 | - protected static function getTicketLineItem(\EE_Transaction $transaction, \EE_Ticket $ticket) |
|
109 | - { |
|
110 | - $line_item = null; |
|
111 | - $ticket_line_items = \EEH_Line_Item::get_line_items_by_object_type_and_IDs( |
|
112 | - $transaction->total_line_item(), |
|
113 | - 'Ticket', |
|
114 | - array($ticket->ID()) |
|
115 | - ); |
|
116 | - foreach ($ticket_line_items as $ticket_line_item) { |
|
117 | - if ( |
|
118 | - $ticket_line_item instanceof \EE_Line_Item |
|
119 | - && $ticket_line_item->OBJ_type() === 'Ticket' |
|
120 | - && $ticket_line_item->OBJ_ID() === $ticket->ID() |
|
121 | - ) { |
|
122 | - $line_item = $ticket_line_item; |
|
123 | - break; |
|
124 | - } |
|
125 | - } |
|
126 | - if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) { |
|
127 | - throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID()); |
|
128 | - } |
|
129 | - return $line_item; |
|
130 | - } |
|
101 | + /** |
|
102 | + * @param \EE_Transaction $transaction |
|
103 | + * @param \EE_Ticket $ticket |
|
104 | + * @return \EE_Line_Item |
|
105 | + * @throws EntityNotFoundException |
|
106 | + * @throws \EE_Error |
|
107 | + */ |
|
108 | + protected static function getTicketLineItem(\EE_Transaction $transaction, \EE_Ticket $ticket) |
|
109 | + { |
|
110 | + $line_item = null; |
|
111 | + $ticket_line_items = \EEH_Line_Item::get_line_items_by_object_type_and_IDs( |
|
112 | + $transaction->total_line_item(), |
|
113 | + 'Ticket', |
|
114 | + array($ticket->ID()) |
|
115 | + ); |
|
116 | + foreach ($ticket_line_items as $ticket_line_item) { |
|
117 | + if ( |
|
118 | + $ticket_line_item instanceof \EE_Line_Item |
|
119 | + && $ticket_line_item->OBJ_type() === 'Ticket' |
|
120 | + && $ticket_line_item->OBJ_ID() === $ticket->ID() |
|
121 | + ) { |
|
122 | + $line_item = $ticket_line_item; |
|
123 | + break; |
|
124 | + } |
|
125 | + } |
|
126 | + if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) { |
|
127 | + throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID()); |
|
128 | + } |
|
129 | + return $line_item; |
|
130 | + } |
|
131 | 131 | } |
@@ -42,7 +42,7 @@ |
||
42 | 42 | */ |
43 | 43 | public function set_group_template_id($GRP_ID = false) |
44 | 44 | { |
45 | - if (! $GRP_ID) { |
|
45 | + if ( ! $GRP_ID) { |
|
46 | 46 | throw new EE_Error(esc_html__('Missing required value for the message template group id', 'event_espresso')); |
47 | 47 | } |
48 | 48 | $this->set('GRP_ID', $GRP_ID); |
@@ -12,173 +12,173 @@ |
||
12 | 12 | */ |
13 | 13 | class EE_Message_Template extends EE_Base_Class |
14 | 14 | { |
15 | - /** |
|
16 | - * @param array $props_n_values |
|
17 | - * @param string $timezone |
|
18 | - * @return EE_Message_Template|mixed |
|
19 | - */ |
|
20 | - public static function new_instance($props_n_values = array(), $timezone = '') |
|
21 | - { |
|
22 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone); |
|
23 | - return $has_object ? $has_object : new self($props_n_values, false, $timezone); |
|
24 | - } |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * @param array $props_n_values |
|
29 | - * @param string $timezone |
|
30 | - * @return EE_Message_Template |
|
31 | - */ |
|
32 | - public static function new_instance_from_db($props_n_values = array(), $timezone = '') |
|
33 | - { |
|
34 | - return new self($props_n_values, true, $timezone); |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * @param bool $GRP_ID |
|
40 | - * @throws EE_Error |
|
41 | - */ |
|
42 | - public function set_group_template_id($GRP_ID = false) |
|
43 | - { |
|
44 | - if (! $GRP_ID) { |
|
45 | - throw new EE_Error(esc_html__('Missing required value for the message template group id', 'event_espresso')); |
|
46 | - } |
|
47 | - $this->set('GRP_ID', $GRP_ID); |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * get Group ID |
|
53 | - * |
|
54 | - * @access public |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - public function GRP_ID() |
|
58 | - { |
|
59 | - return $this->get('GRP_ID'); |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * get User ID |
|
65 | - * |
|
66 | - * @access public |
|
67 | - * @return int |
|
68 | - */ |
|
69 | - public function user() |
|
70 | - { |
|
71 | - return $this->get_first_related('Message_Template_Group')->get('MTP_user_id'); |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * get Message Messenger |
|
77 | - * |
|
78 | - * @access public |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function messenger() |
|
82 | - { |
|
83 | - return $this->get_first_related('Message_Template_Group')->messenger(); |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * get Message Messenger OBJECT |
|
89 | - * |
|
90 | - * @access public |
|
91 | - * @return object Messenger Object for the given messenger |
|
92 | - */ |
|
93 | - public function messenger_obj() |
|
94 | - { |
|
95 | - return $this->get_first_related('Message_Template_Group')->messenger_obj(); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * get Message Type |
|
101 | - * |
|
102 | - * @access public |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - public function message_type() |
|
106 | - { |
|
107 | - return $this->get_first_related('Message_Template_Group')->message_type(); |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - /** |
|
112 | - * get Message type OBJECT |
|
113 | - * |
|
114 | - * @access public |
|
115 | - * @return object Message Type object for the given message type |
|
116 | - */ |
|
117 | - public function message_type_obj() |
|
118 | - { |
|
119 | - return $this->get_first_related('Message_Template_Group')->message_type_obj(); |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * This returns the set context array configured in the message type object |
|
125 | - * |
|
126 | - * @access public |
|
127 | - * @return array array of contexts and their configuration. |
|
128 | - */ |
|
129 | - public function contexts_config() |
|
130 | - { |
|
131 | - return $this->get_first_related('Message_Template_Group')->contexts_config(); |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * This returns the context_label for contexts as set in the message type object |
|
137 | - * |
|
138 | - * @access public |
|
139 | - * @return string label for "context" |
|
140 | - */ |
|
141 | - public function context_label() |
|
142 | - { |
|
143 | - return $this->get_first_related('Message_Template_Group')->context_label(); |
|
144 | - } |
|
145 | - |
|
146 | - |
|
147 | - /** |
|
148 | - * this returns if the template group this template belongs to is global |
|
149 | - * |
|
150 | - * @return boolean true if it is, false if it isn't |
|
151 | - */ |
|
152 | - public function is_global() |
|
153 | - { |
|
154 | - return $this->get_first_related('Message_Template_Group')->is_global(); |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - /** |
|
159 | - * this returns if the template group this template belongs to is active (i.e. turned "on" or not) |
|
160 | - * |
|
161 | - * @return boolean true if it is, false if it isn't |
|
162 | - */ |
|
163 | - public function is_active() |
|
164 | - { |
|
165 | - return $this->get_first_related('Message_Template_Group')->is_active(); |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * This will return an array of shortcodes => labels from the messenger and message_type objects associated with |
|
171 | - * this template. |
|
172 | - * |
|
173 | - * @access public |
|
174 | - * @param string $context what context we're going to return shortcodes for |
|
175 | - * @param array $fields what fields we're returning valid shortcodes for. If empty then we assume all fields are |
|
176 | - * to be merged and returned. |
|
177 | - * @return mixed (array|bool) an array of shortcodes in the format array( '[shortcode] => 'label') OR FALSE if no |
|
178 | - * shortcodes found. |
|
179 | - */ |
|
180 | - public function get_shortcodes($context, $fields = array()) |
|
181 | - { |
|
182 | - return $this->get_first_related('Message_Template_Group')->get_shortcodes($context, $fields); |
|
183 | - } |
|
15 | + /** |
|
16 | + * @param array $props_n_values |
|
17 | + * @param string $timezone |
|
18 | + * @return EE_Message_Template|mixed |
|
19 | + */ |
|
20 | + public static function new_instance($props_n_values = array(), $timezone = '') |
|
21 | + { |
|
22 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone); |
|
23 | + return $has_object ? $has_object : new self($props_n_values, false, $timezone); |
|
24 | + } |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * @param array $props_n_values |
|
29 | + * @param string $timezone |
|
30 | + * @return EE_Message_Template |
|
31 | + */ |
|
32 | + public static function new_instance_from_db($props_n_values = array(), $timezone = '') |
|
33 | + { |
|
34 | + return new self($props_n_values, true, $timezone); |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * @param bool $GRP_ID |
|
40 | + * @throws EE_Error |
|
41 | + */ |
|
42 | + public function set_group_template_id($GRP_ID = false) |
|
43 | + { |
|
44 | + if (! $GRP_ID) { |
|
45 | + throw new EE_Error(esc_html__('Missing required value for the message template group id', 'event_espresso')); |
|
46 | + } |
|
47 | + $this->set('GRP_ID', $GRP_ID); |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * get Group ID |
|
53 | + * |
|
54 | + * @access public |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + public function GRP_ID() |
|
58 | + { |
|
59 | + return $this->get('GRP_ID'); |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * get User ID |
|
65 | + * |
|
66 | + * @access public |
|
67 | + * @return int |
|
68 | + */ |
|
69 | + public function user() |
|
70 | + { |
|
71 | + return $this->get_first_related('Message_Template_Group')->get('MTP_user_id'); |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * get Message Messenger |
|
77 | + * |
|
78 | + * @access public |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function messenger() |
|
82 | + { |
|
83 | + return $this->get_first_related('Message_Template_Group')->messenger(); |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * get Message Messenger OBJECT |
|
89 | + * |
|
90 | + * @access public |
|
91 | + * @return object Messenger Object for the given messenger |
|
92 | + */ |
|
93 | + public function messenger_obj() |
|
94 | + { |
|
95 | + return $this->get_first_related('Message_Template_Group')->messenger_obj(); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * get Message Type |
|
101 | + * |
|
102 | + * @access public |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + public function message_type() |
|
106 | + { |
|
107 | + return $this->get_first_related('Message_Template_Group')->message_type(); |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + /** |
|
112 | + * get Message type OBJECT |
|
113 | + * |
|
114 | + * @access public |
|
115 | + * @return object Message Type object for the given message type |
|
116 | + */ |
|
117 | + public function message_type_obj() |
|
118 | + { |
|
119 | + return $this->get_first_related('Message_Template_Group')->message_type_obj(); |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * This returns the set context array configured in the message type object |
|
125 | + * |
|
126 | + * @access public |
|
127 | + * @return array array of contexts and their configuration. |
|
128 | + */ |
|
129 | + public function contexts_config() |
|
130 | + { |
|
131 | + return $this->get_first_related('Message_Template_Group')->contexts_config(); |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * This returns the context_label for contexts as set in the message type object |
|
137 | + * |
|
138 | + * @access public |
|
139 | + * @return string label for "context" |
|
140 | + */ |
|
141 | + public function context_label() |
|
142 | + { |
|
143 | + return $this->get_first_related('Message_Template_Group')->context_label(); |
|
144 | + } |
|
145 | + |
|
146 | + |
|
147 | + /** |
|
148 | + * this returns if the template group this template belongs to is global |
|
149 | + * |
|
150 | + * @return boolean true if it is, false if it isn't |
|
151 | + */ |
|
152 | + public function is_global() |
|
153 | + { |
|
154 | + return $this->get_first_related('Message_Template_Group')->is_global(); |
|
155 | + } |
|
156 | + |
|
157 | + |
|
158 | + /** |
|
159 | + * this returns if the template group this template belongs to is active (i.e. turned "on" or not) |
|
160 | + * |
|
161 | + * @return boolean true if it is, false if it isn't |
|
162 | + */ |
|
163 | + public function is_active() |
|
164 | + { |
|
165 | + return $this->get_first_related('Message_Template_Group')->is_active(); |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * This will return an array of shortcodes => labels from the messenger and message_type objects associated with |
|
171 | + * this template. |
|
172 | + * |
|
173 | + * @access public |
|
174 | + * @param string $context what context we're going to return shortcodes for |
|
175 | + * @param array $fields what fields we're returning valid shortcodes for. If empty then we assume all fields are |
|
176 | + * to be merged and returned. |
|
177 | + * @return mixed (array|bool) an array of shortcodes in the format array( '[shortcode] => 'label') OR FALSE if no |
|
178 | + * shortcodes found. |
|
179 | + */ |
|
180 | + public function get_shortcodes($context, $fields = array()) |
|
181 | + { |
|
182 | + return $this->get_first_related('Message_Template_Group')->get_shortcodes($context, $fields); |
|
183 | + } |
|
184 | 184 | } |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | public static function validateType($type) |
69 | 69 | { |
70 | 70 | $types = CoffeeMaker::getTypes(); |
71 | - if (! in_array($type, $types, true)) { |
|
71 | + if ( ! in_array($type, $types, true)) { |
|
72 | 72 | throw new InvalidIdentifierException( |
73 | 73 | is_object($type) ? get_class($type) : gettype($type), |
74 | 74 | esc_html__( |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | protected function resolveClassAndFilepath(RecipeInterface $recipe) |
152 | 152 | { |
153 | 153 | $paths = $recipe->paths(); |
154 | - if (! empty($paths)) { |
|
154 | + if ( ! empty($paths)) { |
|
155 | 155 | foreach ($paths as $path) { |
156 | 156 | if (strpos($path, '*') === false && is_readable($path)) { |
157 | 157 | require_once($path); |
@@ -17,156 +17,156 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class CoffeeMaker implements CoffeeMakerInterface |
19 | 19 | { |
20 | - /** |
|
21 | - * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given) |
|
22 | - */ |
|
23 | - const BREW_NEW = 'new'; |
|
24 | - |
|
25 | - /** |
|
26 | - * Indicates that CoffeeMaker should always return a SHARED instance |
|
27 | - */ |
|
28 | - const BREW_SHARED = 'shared'; |
|
29 | - |
|
30 | - /** |
|
31 | - * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate |
|
32 | - */ |
|
33 | - const BREW_LOAD_ONLY = 'load_only'; |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * @var CoffeePotInterface $coffee_pot |
|
38 | - */ |
|
39 | - private $coffee_pot; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var DependencyInjector $injector |
|
43 | - */ |
|
44 | - private $injector; |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * @return array |
|
49 | - */ |
|
50 | - public static function getTypes() |
|
51 | - { |
|
52 | - return (array) apply_filters( |
|
53 | - 'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes', |
|
54 | - array( |
|
55 | - CoffeeMaker::BREW_NEW, |
|
56 | - CoffeeMaker::BREW_SHARED, |
|
57 | - CoffeeMaker::BREW_LOAD_ONLY, |
|
58 | - ) |
|
59 | - ); |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * @param $type |
|
65 | - * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
66 | - */ |
|
67 | - public static function validateType($type) |
|
68 | - { |
|
69 | - $types = CoffeeMaker::getTypes(); |
|
70 | - if (! in_array($type, $types, true)) { |
|
71 | - throw new InvalidIdentifierException( |
|
72 | - is_object($type) ? get_class($type) : gettype($type), |
|
73 | - esc_html__( |
|
74 | - 'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)', |
|
75 | - 'event_espresso' |
|
76 | - ) |
|
77 | - ); |
|
78 | - } |
|
79 | - return $type; |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * CoffeeMaker constructor. |
|
85 | - * |
|
86 | - * @param CoffeePotInterface $coffee_pot |
|
87 | - * @param InjectorInterface $injector |
|
88 | - */ |
|
89 | - public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector) |
|
90 | - { |
|
91 | - $this->coffee_pot = $coffee_pot; |
|
92 | - $this->injector = $injector; |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * @return \EventEspresso\core\services\container\CoffeePotInterface |
|
98 | - */ |
|
99 | - protected function coffeePot() |
|
100 | - { |
|
101 | - return $this->coffee_pot; |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * @return \EventEspresso\core\services\container\DependencyInjector |
|
107 | - */ |
|
108 | - protected function injector() |
|
109 | - { |
|
110 | - return $this->injector; |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * Examines the constructor to determine which method should be used for instantiation |
|
116 | - * |
|
117 | - * @param \ReflectionClass $reflector |
|
118 | - * @return mixed |
|
119 | - * @throws InstantiationException |
|
120 | - */ |
|
121 | - protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
|
122 | - { |
|
123 | - if ($reflector->getConstructor() === null) { |
|
124 | - return 'NewInstance'; |
|
125 | - } |
|
126 | - if ($reflector->isInstantiable()) { |
|
127 | - return 'NewInstanceArgs'; |
|
128 | - } |
|
129 | - if (method_exists($reflector->getName(), 'instance')) { |
|
130 | - return 'instance'; |
|
131 | - } |
|
132 | - if (method_exists($reflector->getName(), 'new_instance')) { |
|
133 | - return 'new_instance'; |
|
134 | - } |
|
135 | - if (method_exists($reflector->getName(), 'new_instance_from_db')) { |
|
136 | - return 'new_instance_from_db'; |
|
137 | - } |
|
138 | - throw new InstantiationException($reflector->getName()); |
|
139 | - } |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * Ensures files for classes that are not PSR-4 compatible are loaded |
|
144 | - * and then verifies that classes exist where applicable |
|
145 | - * |
|
146 | - * @param RecipeInterface $recipe |
|
147 | - * @return bool |
|
148 | - * @throws InvalidClassException |
|
149 | - */ |
|
150 | - protected function resolveClassAndFilepath(RecipeInterface $recipe) |
|
151 | - { |
|
152 | - $paths = $recipe->paths(); |
|
153 | - if (! empty($paths)) { |
|
154 | - foreach ($paths as $path) { |
|
155 | - if (strpos($path, '*') === false && is_readable($path)) { |
|
156 | - require_once($path); |
|
157 | - } |
|
158 | - } |
|
159 | - } |
|
160 | - // re: using "false" for class_exists() second param: |
|
161 | - // if a class name is not already known to PHP, then class_exists() will run through |
|
162 | - // all of the registered spl_autoload functions until it either finds the class, |
|
163 | - // or gets to the end of the registered spl_autoload functions. |
|
164 | - // When the second parameter is true, it will also attempt to load the class file, |
|
165 | - // but it will also trigger an error if the class can not be loaded. |
|
166 | - // We don't want that extra error in the mix, so we have set the second param to "false" |
|
167 | - if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) { |
|
168 | - throw new InvalidClassException($recipe->identifier()); |
|
169 | - } |
|
170 | - return true; |
|
171 | - } |
|
20 | + /** |
|
21 | + * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given) |
|
22 | + */ |
|
23 | + const BREW_NEW = 'new'; |
|
24 | + |
|
25 | + /** |
|
26 | + * Indicates that CoffeeMaker should always return a SHARED instance |
|
27 | + */ |
|
28 | + const BREW_SHARED = 'shared'; |
|
29 | + |
|
30 | + /** |
|
31 | + * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate |
|
32 | + */ |
|
33 | + const BREW_LOAD_ONLY = 'load_only'; |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * @var CoffeePotInterface $coffee_pot |
|
38 | + */ |
|
39 | + private $coffee_pot; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var DependencyInjector $injector |
|
43 | + */ |
|
44 | + private $injector; |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * @return array |
|
49 | + */ |
|
50 | + public static function getTypes() |
|
51 | + { |
|
52 | + return (array) apply_filters( |
|
53 | + 'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes', |
|
54 | + array( |
|
55 | + CoffeeMaker::BREW_NEW, |
|
56 | + CoffeeMaker::BREW_SHARED, |
|
57 | + CoffeeMaker::BREW_LOAD_ONLY, |
|
58 | + ) |
|
59 | + ); |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * @param $type |
|
65 | + * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
66 | + */ |
|
67 | + public static function validateType($type) |
|
68 | + { |
|
69 | + $types = CoffeeMaker::getTypes(); |
|
70 | + if (! in_array($type, $types, true)) { |
|
71 | + throw new InvalidIdentifierException( |
|
72 | + is_object($type) ? get_class($type) : gettype($type), |
|
73 | + esc_html__( |
|
74 | + 'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)', |
|
75 | + 'event_espresso' |
|
76 | + ) |
|
77 | + ); |
|
78 | + } |
|
79 | + return $type; |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * CoffeeMaker constructor. |
|
85 | + * |
|
86 | + * @param CoffeePotInterface $coffee_pot |
|
87 | + * @param InjectorInterface $injector |
|
88 | + */ |
|
89 | + public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector) |
|
90 | + { |
|
91 | + $this->coffee_pot = $coffee_pot; |
|
92 | + $this->injector = $injector; |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * @return \EventEspresso\core\services\container\CoffeePotInterface |
|
98 | + */ |
|
99 | + protected function coffeePot() |
|
100 | + { |
|
101 | + return $this->coffee_pot; |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * @return \EventEspresso\core\services\container\DependencyInjector |
|
107 | + */ |
|
108 | + protected function injector() |
|
109 | + { |
|
110 | + return $this->injector; |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * Examines the constructor to determine which method should be used for instantiation |
|
116 | + * |
|
117 | + * @param \ReflectionClass $reflector |
|
118 | + * @return mixed |
|
119 | + * @throws InstantiationException |
|
120 | + */ |
|
121 | + protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
|
122 | + { |
|
123 | + if ($reflector->getConstructor() === null) { |
|
124 | + return 'NewInstance'; |
|
125 | + } |
|
126 | + if ($reflector->isInstantiable()) { |
|
127 | + return 'NewInstanceArgs'; |
|
128 | + } |
|
129 | + if (method_exists($reflector->getName(), 'instance')) { |
|
130 | + return 'instance'; |
|
131 | + } |
|
132 | + if (method_exists($reflector->getName(), 'new_instance')) { |
|
133 | + return 'new_instance'; |
|
134 | + } |
|
135 | + if (method_exists($reflector->getName(), 'new_instance_from_db')) { |
|
136 | + return 'new_instance_from_db'; |
|
137 | + } |
|
138 | + throw new InstantiationException($reflector->getName()); |
|
139 | + } |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * Ensures files for classes that are not PSR-4 compatible are loaded |
|
144 | + * and then verifies that classes exist where applicable |
|
145 | + * |
|
146 | + * @param RecipeInterface $recipe |
|
147 | + * @return bool |
|
148 | + * @throws InvalidClassException |
|
149 | + */ |
|
150 | + protected function resolveClassAndFilepath(RecipeInterface $recipe) |
|
151 | + { |
|
152 | + $paths = $recipe->paths(); |
|
153 | + if (! empty($paths)) { |
|
154 | + foreach ($paths as $path) { |
|
155 | + if (strpos($path, '*') === false && is_readable($path)) { |
|
156 | + require_once($path); |
|
157 | + } |
|
158 | + } |
|
159 | + } |
|
160 | + // re: using "false" for class_exists() second param: |
|
161 | + // if a class name is not already known to PHP, then class_exists() will run through |
|
162 | + // all of the registered spl_autoload functions until it either finds the class, |
|
163 | + // or gets to the end of the registered spl_autoload functions. |
|
164 | + // When the second parameter is true, it will also attempt to load the class file, |
|
165 | + // but it will also trigger an error if the class can not be loaded. |
|
166 | + // We don't want that extra error in the mix, so we have set the second param to "false" |
|
167 | + if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) { |
|
168 | + throw new InvalidClassException($recipe->identifier()); |
|
169 | + } |
|
170 | + return true; |
|
171 | + } |
|
172 | 172 | } |