Completed
Branch BUG-9464-payment-methods-myste... (4799f5)
by
unknown
328:53 queued 313:26
created

Event::image_medium()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
namespace EventEspresso\core\libraries\rest_api\calculations;
3
4
use EventEspresso\core\libraries\rest_api\calculations\Base as Calculations_Base;
5
use EventEspresso\core\libraries\rest_api\controllers\model\Base;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, EventEspresso\core\libra...t_api\calculations\Base.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
/**
7
 *
8
 * Class Event_Calculations
9
 *
10
 * Description here
11
 *
12
 * @package         Event Espresso
13
 * @subpackage
14
 * @author				Mike Nelson
15
 * @since		 	   $VID:$
16
 *
17
 */
18
if( !defined( 'EVENT_ESPRESSO_VERSION' ) ) {
19
	exit( 'No direct script access allowed' );
20
}
21
22
class Event extends Calculations_Base {
23
24
	/**
25
	 * Calculates the total spaces on the event (not subtracting sales, but taking
26
	 * sales into account; so this is the optimum sales that CAN still be achieved)
27
	 * See EE_Event::total_available_spaces( true );
28
	 *
29
	 * @param array            $wpdb_row
30
	 * @param \WP_REST_Request $request
31
	 * @param Base             $controller
32
	 * @return int
33
	 * @throws \EE_Error
34
	 */
35 View Code Duplication
	public static function optimum_sales_at_start( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
		if( is_array( $wpdb_row ) && isset( $wpdb_row[ 'Event_CPT.ID' ] ) ) {
37
			$event_obj = \EEM_Event::instance()->get_one_by_ID( $wpdb_row[ 'Event_CPT.ID' ] );
38
		} else {
39
			$event_obj = null;
40
		}
41
		if( $event_obj instanceof \EE_Event ) {
42
			return $event_obj->total_available_spaces( true );
43
		} else {
44
			throw new \EE_Error(
45
				sprintf(
46
					__( 'Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', 'event_espresso' ),
47
					$wpdb_row[ 'Event_CPT.ID' ],
48
					print_r( $wpdb_row, true )
49
				)
50
			);
51
		}
52
	}
53
54
55
56
	/**
57
	 * Calculates the total spaces on the event (ignoring all sales; so this is the optimum
58
	 * sales that COULD have been achieved)
59
	 * See EE_Event::total_available_spaces( true );
60
	 *
61
	 * @param array            $wpdb_row
62
	 * @param \WP_REST_Request $request
63
	 * @param Base             $controller
64
	 * @return int
65
	 * @throws \EE_Error
66
	 */
67 View Code Duplication
	public static function optimum_sales_now( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
		if( is_array( $wpdb_row ) && isset( $wpdb_row[ 'Event_CPT.ID' ] ) ) {
69
			$event_obj = \EEM_Event::instance()->get_one_by_ID( $wpdb_row[ 'Event_CPT.ID' ] );
70
		} else {
71
			$event_obj = null;
72
		}
73
		if( $event_obj instanceof \EE_Event ) {
74
			return $event_obj->total_available_spaces( false );
75
		} else {
76
			throw new \EE_Error(
77
				sprintf(
78
					__( 'Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', 'event_espresso' ),
79
					$wpdb_row[ 'Event_CPT.ID' ],
80
					print_r( $wpdb_row, true )
81
				)
82
			);
83
		}
84
	}
85
86
87
88
	/**
89
	 * Like optimum_sales_now, but minus total sales so far.
90
	 * See EE_Event::spaces_remaining_for_sale( true );
91
	 *
92
	 * @param array            $wpdb_row
93
	 * @param \WP_REST_Request $request
94
	 * @param Base             $controller
95
	 * @return int
96
	 * @throws \EE_Error
97
	 */
98 View Code Duplication
	public static function spaces_remaining( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
		if( is_array( $wpdb_row ) && isset( $wpdb_row[ 'Event_CPT.ID' ] ) ) {
100
			$event_obj = \EEM_Event::instance()->get_one_by_ID( $wpdb_row[ 'Event_CPT.ID' ] );
101
		} else {
102
			$event_obj = null;
103
		}
104
		if( $event_obj instanceof \EE_Event ) {
105
			return $event_obj->spaces_remaining_for_sale();
106
		} else {
107
			throw new \EE_Error(
108
				sprintf(
109
					__( 'Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', 'event_espresso' ),
110
					$wpdb_row[ 'Event_CPT.ID' ],
111
					print_r( $wpdb_row, true )
112
				)
113
			);
114
		}
115
	}
116
117
118
119
	/**
120
	 * Counts the number of approved registrations for this event (regardless
121
	 * of how many datetimes each registrations' ticket purchase is for)
122
	 *
123
	 * @param array            $wpdb_row
124
	 * @param \WP_REST_Request $request
125
	 * @param Base             $controller
126
	 * @return int
127
	 * @throws \EE_Error
128
	 */
129 View Code Duplication
	public static function spots_taken( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
		if( ! is_array( $wpdb_row ) || ! isset( $wpdb_row[ 'Event_CPT.ID' ] ) ) {
131
			throw new \EE_Error(
132
				sprintf(
133
					__( 'Cannot calculate spots_taken because the database row %1$s does not have an entry for "Event_CPT.ID"', 'event_espresso' ),
134
					print_r( $wpdb_row, true )
135
				)
136
			);
137
		}
138
		return \EEM_Registration::instance()->count(
139
			array(
140
				array(
141
					'EVT_ID' => $wpdb_row[ 'Event_CPT.ID' ],
142
					'STS_ID' => \EEM_Registration::status_id_approved
143
				)
144
			),
145
			'REG_ID',
146
			true
147
		);
148
	}
149
150
151
152
	/**
153
	 * Counts the number of pending-payment registrations for this event (regardless
154
	 * of how many datetimes each registrations' ticket purchase is for)
155
	 *
156
	 * @param array            $wpdb_row
157
	 * @param \WP_REST_Request $request
158
	 * @param Base             $controller
159
	 * @return int
160
	 * @throws \EE_Error
161
	 * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception
162
	 */
163 View Code Duplication
	public static function spots_taken_pending_payment( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
		if( ! is_array( $wpdb_row ) || ! isset( $wpdb_row[ 'Event_CPT.ID' ] ) ) {
165
			throw new \EE_Error(
166
				sprintf(
167
					__( 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', 'event_espresso' ),
168
					print_r( $wpdb_row, true )
169
				)
170
			);
171
		}
172
		self::_verify_current_user_can( 'ee_read_registrations', 'spots_taken_pending_payment' );
173
		return \EEM_Registration::instance()->count(
174
			array(
175
				array(
176
					'EVT_ID' => $wpdb_row[ 'Event_CPT.ID' ],
177
					'STS_ID' => \EEM_Registration::status_id_pending_payment
178
				)
179
			),
180
			'REG_ID',
181
			true
182
		);
183
	}
184
185
186
187
	/**
188
	 * Counts all the registrations who have checked into one of this events' datetimes
189
	 * See EE_Event::total_available_spaces( false );
190
	 *
191
	 * @param array            $wpdb_row
192
	 * @param \WP_REST_Request $request
193
	 * @param Base             $controller
194
	 * @return int|null if permission denied
195
	 * @throws \EE_Error
196
	 * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception
197
	 */
198 View Code Duplication
	public static function registrations_checked_in_count( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
		if( ! is_array( $wpdb_row ) || ! isset( $wpdb_row[ 'Event_CPT.ID' ] ) ) {
200
			throw new \EE_Error(
201
				sprintf(
202
					__( 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', 'event_espresso' ),
203
					print_r( $wpdb_row, true )
204
				)
205
			);
206
		}
207
		self::_verify_current_user_can( 'ee_read_checkins', 'registrations_checked_in_count' );
208
		return \EEM_Registration::instance()->count_registrations_checked_into_event( $wpdb_row[ 'Event_CPT.ID' ], true );
209
	}
210
211
212
213
	/**
214
	 * Counts all the registrations who have checked out of one of this events' datetimes
215
	 * See EE_Event::total_available_spaces( false );
216
	 *
217
	 * @param array            $wpdb_row
218
	 * @param \WP_REST_Request $request
219
	 * @param Base             $controller
220
	 * @return int
221
	 * @throws \EE_Error
222
	 * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception
223
	 */
224 View Code Duplication
	public static function registrations_checked_out_count( $wpdb_row, $request, $controller ){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
225
		if( ! is_array( $wpdb_row ) || ! isset( $wpdb_row[ 'Event_CPT.ID' ] ) ) {
226
			throw new \EE_Error(
227
				sprintf(
228
					__( 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', 'event_espresso' ),
229
					print_r( $wpdb_row, true )
230
				)
231
			);
232
		}
233
		self::_verify_current_user_can( 'ee_read_checkins', 'registrations_checked_out_count' );
234
		return \EEM_Registration::instance()->count_registrations_checked_into_event( $wpdb_row[ 'Event_CPT.ID' ], false );
235
	}
236
237
	/**
238
	 * Gets the thumbnail image
239
	 * @param array $wpdb_row
240
	 * @param \WP_REST_Request $request
241
	 * @param Base $controller
242
	 * @return array
243
	 */
244
	public static function image_thumbnail( $wpdb_row, $request, $controller ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
245
		return self::_calculate_image_data($wpdb_row[ 'Event_CPT.ID' ], 'thumbnail' );
246
	}
247
248
	/**
249
	 * Gets the medium image
250
	 * @param array $wpdb_row
251
	 * @param \WP_REST_Request $request
252
	 * @param Base $controller
253
	 * @return array
254
	 */
255
	public static function image_medium( $wpdb_row, $request, $controller ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
256
		return self::_calculate_image_data($wpdb_row[ 'Event_CPT.ID' ], 'medium' );
257
	}
258
259
	/**
260
	 * Gets the medium-large image
261
	 * @param array $wpdb_row
262
	 * @param \WP_REST_Request $request
263
	 * @param Base $controller
264
	 * @return array
265
	 */
266
	public static function image_medium_large( $wpdb_row, $request, $controller ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
267
		return self::_calculate_image_data($wpdb_row[ 'Event_CPT.ID' ], 'medium_large' );
268
	}
269
270
	/**
271
	 * Gets the large image
272
	 * @param array $wpdb_row
273
	 * @param \WP_REST_Request $request
274
	 * @param Base $controller
275
	 * @return array
276
	 */
277
	public static function image_large( $wpdb_row, $request, $controller ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
278
		return self::_calculate_image_data($wpdb_row[ 'Event_CPT.ID' ], 'large' );
279
	}
280
281
	/**
282
	 * Gets the post-thumbnail image
283
	 * @param array $wpdb_row
284
	 * @param \WP_REST_Request $request
285
	 * @param Base $controller
286
	 * @return array
287
	 */
288
	public static function image_post_thumbnail( $wpdb_row, $request, $controller ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
289
		return self::_calculate_image_data($wpdb_row[ 'Event_CPT.ID' ], 'post-thumbnail' );
290
	}
291
292
	/**
293
	 * Gets the full size image
294
	 * @param array $wpdb_row
295
	 * @param \WP_REST_Request $request
296
	 * @param Base $controller
297
	 * @return array
298
	 */
299
	public static function image_full( $wpdb_row, $request, $controller ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
300
		return self::_calculate_image_data($wpdb_row[ 'Event_CPT.ID' ], 'full' );
301
	}
302
303
	/**
304
	 * Gets image specs and formats them for the display in the API,
305
	 * according to the image size requested
306
	 * @param int $EVT_ID
307
	 * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full
308
	 * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original'
309
	 */
310
	protected static function _calculate_image_data( $EVT_ID, $image_size ) {
311
		$attachment_id = get_post_thumbnail_id( $EVT_ID );
312
		$data = wp_get_attachment_image_src( $attachment_id, $image_size );
313
		if( ! $data ) {
314
			return false;
315
		}
316
		if( isset( $data[ 3] ) ) {
317
			$generated = $data[ 3 ];
318
		} else {
319
			$generated = true;
320
		}
321
		return array(
322
			'url' => $data[ 0 ],
323
			'width' => $data[ 1 ],
324
			'height' => $data[ 2 ],
325
			'generated' => $generated
326
		);
327
	}
328
}
329