ICTestimonialPosts   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 0
dl 0
loc 130
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A output_testimonials() 0 22 5
B paginate_links() 0 12 8
B set_args() 0 31 7
A has_theme_template() 0 7 3
A add_template_part() 0 17 3
1
<?php
2
/**
3
 * Page Posts Class, main workhorse for the ic_add_testimonials shortcode.
4
 *
5
 * @package     IvyCat AJAX Testimonials
6
 * @author      Eric Amundson <[email protected]>
7
 * @copyright   2017 IvyCat, Inc.
8
 * @license     GPL-2.0+
9
 */
10
11
if ( ! function_exists( 'add_action' ) ) {
12
	wp_die( __( 'You are trying to access this file in a manner not allowed.', 'ivycat-ajax-testimonials' ), __( 'Direct Access Forbidden', 'ivycat-ajax-testimonials' ), array( 'response' => '403' ) );
13
}
14
15
class ICTestimonialPosts {
16
17
	protected $args = array(
18
		'post_type'   => 'testimonials',
19
		'post_status' => 'publish',
20
		'orderby'     => 'date',
21
		'order'       => 'DESC',
22
		'paginate'    => false,
23
		'template'    => false,
24
	); // set defaults for wp_parse_args
25
26
	public function __construct( $atts ) {
27
		self::set_args( $atts );
28
	}
29
30
	/**
31
	 *    Output's the testimonials
32
	 *
33
	 * @return string output of template file
34
	 */
35
	public function output_testimonials() {
36
		if ( ! $this->args ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->args 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...
37
			return '';
38
		}
39
		$page_testimonials = apply_filters( 'testimonials_in_page_results', new WP_Query( $this->args ) ); // New WP_Query object
40
		$output            = '';
41
		if ( $page_testimonials->have_posts() ):
42
			while ( $page_testimonials->have_posts() ):
43
				$output .= self::add_template_part( $page_testimonials );
44
			endwhile;
45
			$output .= ( $this->args['paginate'] ) ? '<div class="pip-nav">' . apply_filters( 'testimonials_in_page_paginate',
46
				$this->paginate_links( $page_testimonials )
47
			) . '</div>' : '';
48
		endif;
49
		wp_reset_postdata();
50
51
		// remove our filters for excerpt more and length
52
		remove_filter( 'excerpt_more', array( 'IvyCatTestimonials', 'ivycat_custom_excerpt_more' ) );
53
		remove_filter( 'excerpt_length', array( 'IvyCatTestimonials', 'ivycat_custom_excerpt_length' ) );
54
55
		return $output;
56
	}
57
58
	protected function paginate_links( $posts ) {
59
		global $wp_query;
60
		$page_url    = home_url( '/' . $wp_query->post->post_name . '/' );
61
		$page        = isset( $_GET['page'] ) ? $_GET['page'] : 1;
0 ignored issues
show
Unused Code introduced by
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
		$total_pages = $posts->max_num_pages;
63
		$per_page    = $posts->query_vars['posts_per_page'];
0 ignored issues
show
Unused Code introduced by
$per_page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
		$curr_page   = ( isset( $posts->query_vars['paged'] ) && $posts->query_vars['paged'] > 0 ) ? $posts->query_vars['paged'] : 1;
65
		$prev        = ( $curr_page && $curr_page > 1 ) ? '<li><a href="' . $page_url . '?page=' . ( $curr_page - 1 ) . '">Previous</a></li>' : '';
66
		$next        = ( $curr_page && $curr_page < $total_pages ) ? '<li><a href="' . $page_url . '?page=' . ( $curr_page + 1 ) . '">Next</a></li>' : '';
67
68
		return '<ul>' . $prev . $next . '</ul>';
69
	}
70
71
	/**
72
	 *    Build additional Arguments for the WP_Query object
73
	 *
74
	 * @param array $atts Attritubes for building the $args array.
75
	 */
76
	protected function set_args( $atts ) {
77
		global $wp_query;
78
		$this->args['posts_per_page'] = get_option( 'posts_per_page' );
79
		// parse the arguments using the defaults
80
		$this->args = wp_parse_args( $atts, $this->args );
81
82
		// Use a specified template
83
		if ( isset( $atts['template'] ) ) {
84
			$this->args['template'] = $atts['template'];
85
		}
86
87
		// show number of posts (default is 10, showposts or posts_per_page are both valid, only one is needed)
88
		if ( isset( $atts['showposts'] ) ) {
89
			$this->args['posts_per_page'] = $atts['showposts'];
90
		}
91
92
		// handle pagination (for code, template pagination is in the template)
93
		if ( isset( $wp_query->query_vars['page'] ) && $wp_query->query_vars['page'] > 1 ) {
94
			$this->args['paged'] = $wp_query->query_vars['page'];
95
		}
96
		if ( false !== $atts['group'] ) {
97
			$this->args['tax_query'] = array(
98
				array(
99
					'taxonomy' => 'testimonial-group',
100
					'field'    => is_numeric( $atts['group'] ) ? 'id' : 'slug',
101
					'terms'    => $atts['group'],
102
				)
103
			);
104
		}
105
		$this->args = apply_filters( 'testimonials_in_page_args', $this->args );
106
	}
107
108
	/**
109
	 *    Tests if a theme has a theme template file that exists
110
	 *
111
	 * @return string|false if template exists, false otherwise.
112
	 */
113
	protected function has_theme_template() {
114
		$template_file = ( $this->args['template'] )
115
			? get_stylesheet_directory() . '/' . $this->args['template'] // use specified template file
116
			: get_stylesheet_directory() . '/testimonials-loop-template.php'; // use default template file
117
118
		return ( file_exists( $template_file ) ) ? $template_file : false;
119
	}
120
121
	/**
122
	 *    Retrieves the post loop template and returns the output
123
	 *
124
	 * @return string results of the output
125
	 */
126
	protected function add_template_part( $ic_testimonials, $singles = false ) {
127
		if ( $singles ) {
128
			setup_postdata( $ic_testimonials );
129
		} else {
130
			$ic_testimonials->the_post();
131
		}
132
		$output = '';
133
		ob_start();
134
		$output .= apply_filters( 'testimonials_in_page_pre_loop', '' );
135
		require( $file_path = self::has_theme_template() )
136
			? $file_path // use template file in theme
137
			: ICTESTI_DIR . '/testimonials-loop-template.php'; // use default plugin template file
138
		$output .= ob_get_contents();
139
		$output .= apply_filters( 'testimonials_in_page_post_loop', '' );
140
141
		return ob_get_clean();
142
	}
143
144
}
145