Passed
Push — master ( 064bb6...00ccb5 )
by Warwick
02:33 queued 13s
created

helpers.php ➔ lsx_wetu_get_post_count()   B

Complexity

Conditions 11
Paths 6

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 6
nop 2
dl 0
loc 34
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Helper functions
4
 *
5
 * @package   lsx_wetu_importer
6
 * @author    LightSpeed
7
 * @license   GPL-2.0+
8
 * @link
9
 * @copyright 2019 LightSpeed
10
 **/
11
12
/**
13
 * Gets the settings
14
 *
15
 * @return array
16
 */
17
function lsx_wetu_get_options() {
18
	$options = get_option( 'lsx_wetu_importer_settings', array() );
19
	if ( empty( $options ) ) {
20
		// Check for any previous options.
21
		$temp_options = get_option( '_lsx-to_settings', false );
22
		if ( false !== $temp_options && isset( $temp_options['lsx-wetu-importer'] ) && ! empty( $temp_options['lsx-wetu-importer'] ) ) {
23
			$options = $temp_options['lsx-wetu-importer'];
24
		}
25
		if ( false !== $temp_options && isset( $temp_options['api']['wetu_api_key'] ) && '' !== $temp_options['api']['wetu_api_key'] ) {
26
			$options['api_key'] = $temp_options['api']['wetu_api_key'];
27
		}
28
	}
29
	return $options;
30
}
31
32
/**
33
 * Get the post count.
34
 *
35
 * @param string $post_type
36
 * @param string $post_status
37
 * @return void
38
 */
39
function lsx_wetu_get_post_count( $post_type = '', $post_status = '' ) {
40
	global $wpdb;
41
	$count = '0';
42
	if ( '' !== $post_type && '' !== $post_status ) {
43
		$result = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(`ID`) FROM $wpdb->posts WHERE `post_status` = '%s' AND `post_type` = '%s'", array( trim( $post_status ), $post_type ) ) );
44
		if ( false !== $result && '' !== $result ) {
45
			if ( 'tour' === $post_type ) {
46
				$wetu_tours = get_transient( 'lsx_ti_tours' );
47
				if ( false !== $wetu_tours ) {
48
					$results = $wpdb->get_results( $wpdb->prepare( "SELECT `ID` FROM $wpdb->posts WHERE `post_status` = '%s' AND `post_type` = '%s'", array( trim( $post_status ), $post_type ) ) );
49
					$result_count = 0;
50
					$tour_wetu_ids = array();
51
					foreach ( $wetu_tours as $wetu_tour ) {
52
						$tour_wetu_ids[] = $wetu_tour['identifier'];
53
					}
54
55
					if ( ! empty( $results ) ) {
56
						foreach ( $results as $tour ) {
57
							$current_wetu_id = get_post_meta( $tour->ID, 'lsx_wetu_id', true );
58
							if ( in_array( $current_wetu_id, $tour_wetu_ids ) ) {
59
								$result_count++;
60
							}
61
						}
62
					}
63
					$result = $result_count;
64
				} else {
65
					$result = 0;
66
				}
67
			}
68
			$count = $result;
69
		}
70
	}
71
	return $count;
72
}
73
74
/**
75
 * Returns the wetu queue count.
76
 *
77
 * @param string $post_type
78
 * @return void
79
 */
80
function lsx_wetu_get_queue_count( $post_type = '' ) {
81
	$count = '0';
82
	$queued_imports = get_option( 'lsx_wetu_importer_que', array() );
83
	if ( isset( $queued_imports[ $post_type ] ) ) {
84
		$count = count( $queued_imports[ $post_type ] );
85
	}
86
	return $count;
87
}
88
89
/**
90
 * Returns the wetu tour count.
91
 *
92
 * @param string $post_type
93
 * @return void
94
 */
95
function lsx_wetu_get_tour_count( $post_type = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_type 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...
96
	$count = '0';
97
	$wetu_tours = get_transient( 'lsx_ti_tours', array() );
98
	if ( ! empty( $wetu_tours ) ) {
99
		$count = count( $wetu_tours );
100
	}
101
	return $count;
102
}
103