Completed
Branch TASK-9118-extensions-page (b89c2b)
by
unknown
506:56 queued 485:08
created

EEM_CPT_Base   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 452
Duplicated Lines 9.07 %

Coupling/Cohesion

Components 3
Dependencies 17
Metric Value
wmc 60
lcom 3
cbo 17
dl 41
loc 452
rs 3.5484

18 Methods

Rating   Name   Duplication   Size   Complexity  
F __construct() 15 53 12
A public_event_stati() 0 4 1
A deleted_field_name() 0 3 1
A post_status_field_name() 8 8 2
A _alter_query_params_so_only_trashed_items_included() 0 5 1
A _alter_query_params_so_deleted_and_undeleted_items_included() 0 4 1
A delete_or_restore() 0 10 3
A meta_table() 0 5 2
B get_meta_table_fields() 0 19 6
B add_event_category() 0 32 3
A remove_event_category() 0 10 2
A get_feature_image() 0 3 1
A get_post_statuses() 0 8 2
A get_status_array() 0 8 1
A get_custom_post_statuses() 0 7 2
B instantiate_class_from_post_object_orig() 9 21 6
D instantiate_class_from_post_object() 9 32 10
A post_type() 0 12 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like EEM_CPT_Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EEM_CPT_Base, and based on these observations, apply Extract Interface, too.

1
<?php
2
define('EE_Event_Category_Taxonomy','espresso_event_category');
3
/**
4
 *
5
 * EEM_CPT_Base
6
 *
7
 * For shared functionality between models internally implemented
8
 * as Custom Post Types. Subclass of EEM_Soft_Delete_Base, meaning that when you 'delete' one of these model objects
9
 * we actually default ot just trashing it. (It works differently than EEM_Soft_Delete under the hood,because there's a post status field
10
 * instead of a soft-delete flag, but the functionality is the same)
11
 * Note: if you add a new subclass of EEM_CPT_Base, you should add it as a relation
12
 * on EEM_Term_Taxonomy and EEM_Term_Relationship
13
 *
14
 * @package 			Event Espresso
15
 * @subpackage 	core
16
 * @author 				Mike Nelson
17
 *
18
 */
19
abstract class EEM_CPT_Base extends EEM_Soft_Delete_Base{
20
21
	/**
22
	 * @var string post_status_publish - the wp post status for published cpts
23
	 */
24
	const post_status_publish = 'publish';
25
26
	/**
27
	 * @var string post_status_future - the wp post status for scheduled cpts
28
	 */
29
	const post_status_future = 'future';
30
31
	/**
32
	 * @var string post_status_draft - the wp post status for draft cpts
33
	 */
34
	const post_status_draft = 'draft';
35
36
	/**
37
	 * @var string post_status_pending - the wp post status for pending cpts
38
	 */
39
	const post_status_pending = 'pending';
40
41
	/**
42
	 * @var string post_status_private - the wp post status for private cpts
43
	 */
44
	const post_status_private = 'private';
45
46
	/**
47
	 * @var string post_status_trashed - the wp post status for trashed cpts
48
	 */
49
	const post_status_trashed = 'trash';
50
51
	/**
52
	 * This is an array of custom statuses for the given CPT model (modified by children)
53
	 * format:
54
	 * array(
55
	 * 		'status_name' => array(
56
	 * 			'label' => __('Status Name', 'event_espresso'),
57
	 * 			'public' => TRUE //whether a public status or not.
58
	 * 		)
59
	 * )
60
	 * @var array
61
	 */
62
	protected $_custom_stati = array();
63
64
65
66
	/**
67
	 * Adds a relationship to Term_Taxonomy for each CPT_Base
68
	 *
69
	 * @param string $timezone
70
	 * @throws \EE_Error
71
	 */
72
	protected function __construct( $timezone = NULL ){
73
74
		//adds a relationship to Term_Taxonomy for all these models. For this to work
75
		//Term_Relationship must have a relation to each model subclassing EE_CPT_Base explicitly
76
		//eg, in EEM_Term_Relationship, inside the _model_relations array, there must be an entry
77
		//with key equalling the subclassing model's model name (eg 'Event' or 'Venue'), and the value
78
		//must also be new EE_HABTM_Relation('Term_Relationship');
79
		$this->_model_relations['Term_Taxonomy'] =new EE_HABTM_Relation('Term_Relationship');
80
		$primary_table_name = NULL;
81
		//add  the common _status field to all CPT primary tables.
82
		foreach ( $this->_tables as $alias => $table_obj ) {
83
			if ( $table_obj instanceof EE_Primary_Table ) {
84
				$primary_table_name = $alias;
85
			}
86
		}
87
		//set default wp post statuses if child has not already set.
88
		if ( ! isset( $this->_fields[$primary_table_name]['status'] )) {
89
			$this->_fields[$primary_table_name]['status'] = new EE_WP_Post_Status_Field('post_status', __("Event Status", "event_espresso"), false, 'draft');
90
		}
91 View Code Duplication
		if( ! isset( $this->_fields[$primary_table_name]['to_ping'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92
			$this->_fields[$primary_table_name]['to_ping'] = new EE_DB_Only_Text_Field('to_ping', __( 'To Ping', 'event_espresso' ), FALSE, '');
1 ignored issue
show
Documentation introduced by
'' is of type string, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
		}
94 View Code Duplication
		if( ! isset( $this->_fields[$primary_table_name]['pinged'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
95
			$this->_fields[$primary_table_name]['pinged'] = new EE_DB_Only_Text_Field('pinged', __( 'Pinged', 'event_espresso' ), FALSE, '');
1 ignored issue
show
Documentation introduced by
'' is of type string, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
		}
97
98 View Code Duplication
		if( ! isset( $this->_fields[$primary_table_name]['comment_status'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
			$this->_fields[$primary_table_name]['comment_status'] = new EE_Plain_Text_Field('comment_status', __('Comment Status', 'event_espresso' ), FALSE, 'open');
1 ignored issue
show
Documentation introduced by
'open' is of type string, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
		}
101
102 View Code Duplication
		if( ! isset( $this->_fields[$primary_table_name]['ping_status'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
103
			$this->_fields[$primary_table_name]['ping_status'] = new EE_Plain_Text_Field('ping_status', __('Ping Status', 'event_espresso'), FALSE, 'open');
1 ignored issue
show
Documentation introduced by
'open' is of type string, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
		}
105
106 View Code Duplication
		if( ! isset( $this->_fields[$primary_table_name]['post_content_filtered'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
107
			$this->_fields[$primary_table_name]['post_content_filtered'] = new EE_DB_Only_Text_Field('post_content_filtered', __( 'Post Content Filtered', 'event_espresso' ), FALSE, '');
1 ignored issue
show
Documentation introduced by
'' is of type string, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
		}
109
		if( ! isset( $this->_model_relations[ 'Post_Meta' ] ) ) {
110
			//don't block deletes though because we want to maintain the current behaviour
111
			$this->_model_relations[ 'Post_Meta' ] = new EE_Has_Many_Relation( false );
112
		}
113
		if( ! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions ){
114
			//nothing was set during child constructor, so set default
115
			$this->_minimum_where_conditions_strategy = new EE_CPT_Minimum_Where_Conditions( $this->post_type() );
116
		}
117
		if( ! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions ) {
118
			//nothing was set during child constructor, so set default
119
			//it's ok for child classes to specify this, but generally this is more DRY
120
			$this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions( $this->post_type() );
121
		}
122
		parent::__construct($timezone);
0 ignored issues
show
Bug introduced by
It seems like $timezone defined by parameter $timezone on line 72 can also be of type string; however, EEM_Soft_Delete_Base::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
123
124
	}
125
126
127
128
	/**
129
	 * @return array
130
	 */
131
	public function public_event_stati() {
132
		// @see wp-includes/post.php
133
		return get_post_stati( array( 'public' => TRUE ));
134
	}
135
136
137
138
	/**
139
	 * Searches for field on this model of type 'deleted_flag'. if it is found,
140
	 * returns it's name. BUT That doesn't apply to CPTs. We should instead use post_status_field_name
141
	 * @return string
142
	 * @throws EE_Error
143
	 */
144
	public function deleted_field_name(){
145
		throw new EE_Error(sprintf(__("EEM_CPT_Base should nto call deleted_field_name! It should instead use post_status_field_name", "event_espresso")));
146
	}
147
148
149
150
	/**
151
	 * Gets the field's name that sets the post status
152
	 * @return string
153
	 * @throws EE_Error
154
	 */
155 View Code Duplication
	public function post_status_field_name(){
0 ignored issues
show
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...
156
		$field = $this->get_a_field_of_type('EE_WP_Post_Status_Field');
157
		if($field){
158
			return $field->get_name();
159
		}else{
160
			throw new EE_Error(sprintf(__('We are trying to find the post status flag field on %s, but none was found. Are you sure there is a field of type EE_Trashed_Flag_Field in %s constructor?','event_espresso'),get_class($this),get_class($this)));
161
		}
162
	}
163
164
165
166
	/**
167
	 * Alters the query params so that only trashed/soft-deleted items are considered
168
	 * @param array $query_params like EEM_Base::get_all's $query_params
169
	 * @return array like EEM_Base::get_all's $query_params
170
	 */
171
	protected function _alter_query_params_so_only_trashed_items_included($query_params){
172
		$post_status_field_name=$this->post_status_field_name();
173
		$query_params[0][$post_status_field_name]=self::post_status_trashed;
174
		return $query_params;
175
	}
176
177
178
179
	/**
180
	 * Alters the query params so each item's deleted status is ignored.
181
	 * @param array $query_params
182
	 * @return array
183
	 */
184
	protected function _alter_query_params_so_deleted_and_undeleted_items_included($query_params){
185
		$query_params[ 'default_where_conditions' ] = 'minimum';
186
		return $query_params;
187
	}
188
189
190
191
	/**
192
	 * Performs deletes or restores on items. Both soft-deleted and non-soft-deleted items considered.
193
	 * @param boolean $delete true to indicate deletion, false to indicate restoration
194
	 * @param array $query_params like EEM_Base::get_all
195
	 * @return boolean success
196
	 */
197
	function delete_or_restore($delete=true,$query_params = array()){
198
		$post_status_field_name=$this->post_status_field_name();
199
		$query_params = $this->_alter_query_params_so_deleted_and_undeleted_items_included($query_params);
200
		$new_status = $delete ? self::post_status_trashed : 'draft';
201
		if ( $this->update (array($post_status_field_name=>$new_status), $query_params )) {
202
			return TRUE;
203
		} else {
204
			return FALSE;
205
		}
206
	}
207
208
209
210
	/**
211
	 * meta_table
212
	 * returns first EE_Secondary_Table table name
213
	 * @access public
214
	 * @return string
215
	 */
216
	public function meta_table() {
217
		$meta_table = $this->_get_other_tables();
218
		$meta_table = reset( $meta_table );
219
		return $meta_table instanceof EE_Secondary_Table ? $meta_table->get_table_name() : NULL;
220
	}
221
222
223
224
225
	/**
226
	 * This simply returns an array of the meta table fields (useful for when we just need to update those fields)
227
	 * @param  bool $all triggers whether we include DB_Only fields or JUST non DB_Only fields.  Defaults to false (no db only fields)
228
	 * @return array
229
	 */
230
	public function get_meta_table_fields( $all = FALSE ) {
231
		$all_fields = $fields_to_return = array();
232
		foreach ( $this->_tables as $alias => $table_obj ) {
233
			if ( $table_obj instanceof EE_Secondary_Table )
234
				$all_fields = array_merge( $this->_get_fields_for_table($alias), $all_fields );
235
		}
236
237
		if ( !$all ) {
238
			foreach ( $all_fields as $name => $obj ) {
239
				if ( $obj instanceof EE_DB_Only_Field_Base )
240
					continue;
241
				$fields_to_return[] = $name;
242
			}
243
		} else {
244
			$fields_to_return = array_keys($all_fields);
245
		}
246
247
		return $fields_to_return;
248
	}
249
250
251
252
	/**
253
	 * Adds an event category with the specified name and description to the specified
254
	 * $cpt_model_object. Intelligently adds a term if necessary, and adds a term_taxonomy if necessary,
255
	 * and adds an entry in the term_relationship if necessary.
256
	 * @param EE_CPT_Base $cpt_model_object
257
	 * @param string $category_name (used to derive the term slug too)
258
	 * @param string $category_description
259
	 * @param int $parent_term_taxonomy_id
260
	 * @return EE_Term_Taxonomy
261
	 */
262
	function add_event_category(EE_CPT_Base $cpt_model_object, $category_name, $category_description ='',$parent_term_taxonomy_id = null){
263
		//create term
264
		require_once( EE_MODELS . 'EEM_Term.model.php');
265
		//first, check for a term by the same name or slug
266
		$category_slug = sanitize_title($category_name);
267
		$term = EEM_Term::instance()->get_one(array(array('OR'=>array('name'=>$category_name,'slug'=>$category_slug))));
268
		if( ! $term ){
269
			$term = EE_Term::new_instance(array(
270
				'name'=>$category_name,
271
				'slug'=>$category_slug
272
			));
273
			$term->save();
274
		}
275
		//make sure there's a term-taxonomy entry too
276
		require_once( EE_MODELS . 'EEM_Term_Taxonomy.model.php');
277
		$term_taxonomy = EEM_Term_Taxonomy::instance()->get_one(array(array('term_id'=>$term->ID(),'taxonomy'=>EE_Event_Category_Taxonomy)));
278
		/** @var $term_taxonomy EE_Term_Taxonomy */
279
		if( ! $term_taxonomy ){
280
			$term_taxonomy = EE_Term_Taxonomy::new_instance(array(
281
				'term_id'=>$term->ID(),
282
				'taxonomy'=>EE_Event_Category_Taxonomy,
283
				'description'=>$category_description,
284
				'count'=>1,
285
				'parent'=>$parent_term_taxonomy_id
286
			));
287
			$term_taxonomy->save();
288
		}else{
289
			$term_taxonomy->set_count($term_taxonomy->count() + 1);
290
			$term_taxonomy->save();
291
		}
292
		return $this->add_relationship_to($cpt_model_object, $term_taxonomy, 'Term_Taxonomy');
293
	}
294
295
296
	/**
297
	 * Removed the category specified by name as having a relation to this event.
298
	 * Does not remove the term or term_taxonomy.
299
	 * @param EE_CPT_Base $cpt_model_object_event
300
	 * @param string $category_name name of the event category (term)
301
	 * @return bool
302
	 */
303
	function remove_event_category(EE_CPT_Base $cpt_model_object_event, $category_name){
304
		//find the term_taxonomy by that name
305
		$term_taxonomy = $this->get_first_related($cpt_model_object_event, 'Term_Taxonomy', array(array('Term.name'=>$category_name,'taxonomy'=>EE_Event_Category_Taxonomy)));
306
		/** @var $term_taxonomy EE_Term_Taxonomy */
307
		if( $term_taxonomy ){
308
			$term_taxonomy->set_count($term_taxonomy->count() - 1);
309
			$term_taxonomy->save();
310
		}
311
		return $this->remove_relationship_to($cpt_model_object_event, $term_taxonomy, 'Term_Taxonomy');
312
	}
313
314
315
316
317
	/**
318
	 * This is a wrapper for the WordPress get_the_post_thumbnail() function that returns the feature image for the given CPT ID.  It accepts the same params as what get_the_post_thumbnail() accepts.
319
	 *
320
	 * @link http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
321
	 * @access public
322
	 * @param int          $id the ID for the cpt we want the feature image for
323
	 * @param string|array $size (optional) Image size. Defaults to 'post-thumbnail' but can also be a 2-item array representing width and height in pixels (i.e. array(32,32) ).
324
	 * @param string|array $attr Optional. Query string or array of attributes.
325
	 * @return string HTML image element
326
	 */
327
	public function get_feature_image( $id, $size = 'thumbnail', $attr = '' ) {
328
		return get_the_post_thumbnail( $id, $size, $attr );
329
	}
330
331
332
333
334
335
336
	/**
337
	 * Just a handy way to get the list of post statuses currently registered with WP.
338
	 * @global array $wp_post_statuses set in wp core for storing all the post stati
339
	 * @return array
340
	 */
341
	public function get_post_statuses(){
342
		global $wp_post_statuses;
343
		$statuses = array();
344
		foreach($wp_post_statuses as $post_status => $args_object){
345
			$statuses[$post_status] = $args_object->label;
346
		}
347
		return $statuses;
348
	}
349
350
351
352
	/**
353
	 * public method that can be used to retrieve the protected status array on the instantiated cpt model
354
	 * @return array array of statuses.
355
	 */
356
	public function get_status_array() {
357
		$statuses = $this->get_post_statuses();
358
		//first the global filter
359
		$statuses = apply_filters( 'FHEE_EEM_CPT_Base__get_status_array', $statuses );
360
		//now the class specific filter
361
		$statuses = apply_filters( 'FHEE_EEM_' . get_class($this) . '__get_status_array', $statuses );
362
		return $statuses;
363
	}
364
365
366
367
	/**
368
	 * this returns the post statuses that are NOT the default wordpress status
369
	 * @return array
370
	 */
371
	public function get_custom_post_statuses() {
372
		$new_stati = array();
373
		foreach ( $this->_custom_stati as $status => $props ) {
374
			$new_stati[$status] = $props['label'];
375
		}
376
		return $new_stati;
377
	}
378
379
380
381
	/**
382
	 * Creates a child of EE_CPT_Base given a WP_Post or array of wpdb results which
383
	 * are a row from the posts table. If we're missing any fields required for the model,
384
	 * we just fetch the entire entry from the DB (ie, if you want to use this to save DB queries,
385
	 * make sure you are attaching all the model's fields onto the post)
386
	 * @param WP_Post|array $post
387
	 * @return EE_CPT_Base
388
	 */
389
	public function instantiate_class_from_post_object_orig($post){
390
		$post = (array)$post;
391
		$has_all_necessary_fields_for_table = true;
392
		//check if the post has fields on the meta table already
393 View Code Duplication
		foreach($this->_get_other_tables() as $table_obj){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
394
			$fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias());
395
			foreach($fields_for_that_table as $field_obj){
0 ignored issues
show
Bug introduced by
The expression $fields_for_that_table of type object<EE_Model_Field_Base> is not traversable.
Loading history...
396
				if( ! isset($post[$field_obj->get_table_column()])
397
					&& ! isset($post[$field_obj->get_qualified_column()])){
398
					$has_all_necessary_fields_for_table = false;
399
				}
400
			}
401
		}
402
		//if we don't have all the fields we need, then just fetch the proper model from the DB
403
		if( ! $has_all_necessary_fields_for_table){
404
405
			return $this->get_one_by_ID($post['ID']);
406
		}else{
407
			return $this->instantiate_class_from_array_or_object($post);
408
		}
409
	}
410
411
412
413
	/**
414
	 * @param null $post
415
	 * @return EE_Base_Class|EE_Soft_Delete_Base_Class
416
	 */
417
	public function instantiate_class_from_post_object( $post = NULL ){
418
		if ( empty( $post )) {
419
			global $post;
420
		}
421
		$post = (array)$post;
422
		$tables_needing_to_be_queried = array();
423
		//check if the post has fields on the meta table already
424 View Code Duplication
		foreach($this->get_tables() as $table_obj){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
425
			$fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias());
426
			foreach($fields_for_that_table as $field_obj){
0 ignored issues
show
Bug introduced by
The expression $fields_for_that_table of type object<EE_Model_Field_Base> is not traversable.
Loading history...
427
				if( ! isset($post[$field_obj->get_table_column()])
428
					&& ! isset($post[$field_obj->get_qualified_column()])){
429
					$tables_needing_to_be_queried[$table_obj->get_table_alias()] = $table_obj;
430
				}
431
			}
432
		}
433
		//if we don't have all the fields we need, then just fetch the proper model from the DB
434
		if( $tables_needing_to_be_queried){
0 ignored issues
show
Bug Best Practice introduced by
The expression $tables_needing_to_be_queried of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
435
			if(count($tables_needing_to_be_queried) == 1 && reset($tables_needing_to_be_queried) instanceof EE_Secondary_Table){
436
				//so we're only missing data from a secondary table. Well that's not too hard to query for
437
				$table_to_query = reset($tables_needing_to_be_queried);
438
				$missing_data = $this->_do_wpdb_query( 'get_row', array( 'SELECT * FROM ' . $table_to_query->get_table_name() . ' WHERE ' . $table_to_query->get_fk_on_table() . ' = ' . $post['ID'], ARRAY_A ));
439
				if ( ! empty( $missing_data )) {
440
					$post = array_merge( $post, $missing_data );
441
				}
442
			} else {
443
				return $this->get_one_by_ID($post['ID']);
444
			}
445
		}
446
		return $this->instantiate_class_from_array_or_object($post);
447
448
	}
449
450
451
452
	/**
453
	 * Gets the post type associated with this
454
	 * @throws EE_Error
455
	 * @return string
456
	 */
457
	public function post_type(){
458
		$post_type_field = NULL;
459
		foreach($this->field_settings(true) as $field_obj){
460
			if($field_obj instanceof EE_WP_Post_Type_Field){
461
				$post_type_field = $field_obj;break;
462
			}
463
		}
464
		if($post_type_field == NULL){
465
			throw new EE_Error(sprintf(__("CPT Model %s should have a field of type EE_WP_Post_Type, but doesnt", "event_espresso"),get_class($this)));
466
		}
467
		return $post_type_field->get_default_value();
468
	}
469
470
}
471