IvyCatTestimonialsWidget::form()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 120

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 120
rs 8
c 0
b 0
f 0
cc 3
nc 3
nop 1

How to fix   Long Method   

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
 * Class IvyCatTestimonialsWidget
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
class IvyCatTestimonialsWidget extends WP_Widget {
12
13
	public function __construct() {
14
		$widget_ops = array( 'description' => __( 'Displays testimonial custom post type content in a widget', 'ivycat-ajax-testimonials' ) );
15
		parent::__construct( 'IvyCatTestimonialsWidget', __( 'IvyCat Testimonial Widget', 'ivycat-ajax-testimonials' ), $widget_ops );
16
	}
17
18
	function form( $instance ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
		$instance = wp_parse_args( $instance, array(
20
			'title'                         => __( 'Testimonials', 'ivycat-ajax-testimonials' ),
21
			'testimonial_group'             => 0,
22
			'testimonial_quantity'          => 3,
23
			'testimonial_num_words'         => 0,
24
			'testimonial_read_more'         => 0,
25
			'testimonial_ajax_on'           => false,
26
			'testimonial_display'           => 'single',
27
			'testimonial_link_testimonials' => false,
28
			'testimonial_show_all_title'    => __( 'See All Testimonials', 'ivycat-ajax-testimonials' ),
29
			'testimonial_show_all'          => get_bloginfo( 'url' ),
30
			'template'                      => '',
31
			'testimonial_slide_speed'       => 8000,
32
			'testimonial_fadein'            => 1000,
33
			'testimonial_fadeout'           => 1000,
34
		) ); ?>
35
36
        <p>
37
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'ivycat-ajax-testimonials' ); ?></label>
38
            <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
39
                   id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
40
                   value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat">
41
        </p>
42
        <p>
43
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_group' ) ); ?>"><?php _e( 'Display Testimonial Group:', 'ivycat-ajax-testimonials' ); ?></label>
44
            <select name="<?php echo esc_attr( $this->get_field_name( 'testimonial_group' ) ); ?>"
45
                    id="<?php echo esc_attr( $this->get_field_id( 'testimonial_group' ) ); ?>" class="widefat">
46
                <option><?php _e( 'All Groups', 'ivycat-ajax-testimonials' ); ?></option><?php
47
				$cats = get_terms( 'testimonial-group', array( 'hide_empty' => 0 ) );
48
				foreach ( ( object ) $cats as $cat ) :
49
					if ( array_key_exists( 'testimonial_group', $instance ) ) {
50
						printf( '<option value="%s"%s">%s</option>',
51
							$cat->slug,
52
							selected( $instance['testimonial_group'], $cat->slug, false ),
53
							$cat->name
54
						);
55
					}
56
				endforeach; ?>
57
            </select>
58
        </p>
59
        <p>
60
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_quantity' ) ); ?>"><?php _e( 'How many testimonials in rotation?', 'ivycat-ajax-testimonials' ); ?></label>
61
            <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_quantity' ) ); ?>"
62
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_quantity' ) ); ?>" class="widefat"
63
                   value="<?php echo absint( $instance['testimonial_quantity'] ); ?>"/>
64
        </p>
65
        <p>
66
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_num_words' ) ); ?>"><?php _e( 'Number of Words (0 for all)', 'ivycat-ajax-testimonials' ); ?></label>
67
            <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_num_words' ) ); ?>"
68
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_num_words' ) ); ?>" class="widefat"
69
                   value="<?php echo absint( $instance['testimonial_num_words'] ); ?>"/>
70
        </p>
71
        <p>
72
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_read_more' ) ); ?>"><?php _e( 'Read More Text', 'ivycat-ajax-testimonials' ); ?></label>
73
            <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_read_more' ) ); ?>"
74
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_read_more' ) ); ?>" class="widefat"
75
                   value="<?php echo esc_attr( $instance['testimonial_read_more'] ); ?>"/>
76
        </p>
77
        <p>
78
            <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_ajax_on' ) ); ?>"
79
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_ajax_on' ) ); ?>" class="checkbox"
80
                   value="no"<?php checked( $instance['testimonial_ajax_on'] ); ?>/>
81
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_ajax_on' ) ); ?>"><?php _e( 'Disable AJAX', 'ivycat-ajax-testimonials' ); ?></label>
82
        </p>
83
        <p>
84
            <input type="checkbox"
85
                   name="<?php echo esc_attr( $this->get_field_name( 'testimonial_link_testimonials' ) ); ?>"
86
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_link_testimonials' ) ); ?>"
87
                   class="checkbox"
88
                   value="yes"<?php checked( $instance['testimonial_link_testimonials'] ); ?>/>
89
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_link_testimonials' ) ); ?>"><?php _e( 'Link Individual Testimonials', 'ivycat-ajax-testimonials' ); ?></label>
90
        </p>
91
        <p>
92
            <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_display' ) ); ?>"
93
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_display' ) ); ?>" class="checkbox"
94
                   value="single"<?php checked( $instance['testimonial_display'], 'list' ); ?>/>
95
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_display' ) ); ?>"><?php _e( 'List Mode', 'ivycat-ajax-testimonials' ); ?></label>
96
        </p>
97
        <p>
98
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_show_all_title' ) ); ?>"><?php _e( 'Title for Link to all Testimonials', 'ivycat-ajax-testimonials' ); ?></label>
99
            <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_show_all_title' ) ); ?>"
100
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_show_all_title' ) ); ?>" class="widefat"
101
                   value="<?php echo esc_attr( $instance['testimonial_show_all_title'] ); ?>"/>
102
        </p>
103
        <p>
104
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_show_all' ) ); ?>"><?php _e( 'Link to all Testimonials', 'ivycat-ajax-testimonials' ); ?></label>
105
            <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_show_all' ) ); ?>"
106
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_show_all' ) ); ?>" class="widefat"
107
                   value="<?php echo esc_url( $instance['testimonial_show_all'] ); ?>"/>
108
        </p>
109
        <p>
110
            <label for="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>"><?php _e( 'Custom Template (enable List Mode)', 'ivycat-ajax-testimonials' ); ?></label>
111
            <input type="text" placeholder="Place file in theme folder"
112
                   name="<?php echo esc_attr( $this->get_field_name( 'template' ) ); ?>"
113
                   id="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>" class="widefat"
114
                   value="<?php echo esc_attr( $instance['template'] ); ?>"/>
115
        </p>
116
        <h3>Testimonial Rotation Settings</h3>
117
        <p>
118
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_slide_speed' ) ); ?>"><?php _e( 'Testimonial Rotation (miliseconds)', 'ivycat-ajax-testimonials' ); ?></label>
119
            <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_slide_speed' ) ); ?>"
120
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_slide_speed' ) ); ?>" class="widefat"
121
                   value="<?php echo absint( $instance['testimonial_slide_speed'] ); ?>"/>
122
        </p>
123
        <p>
124
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_fadein' ) ); ?>"><?php _e( 'Testimonial Fade In (miliseconds)', 'ivycat-ajax-testimonials' ); ?></label>
125
            <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_fadein' ) ); ?>"
126
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_fadein' ) ); ?>" class="widefat"
127
                   value="<?php echo absint( $instance['testimonial_fadein'] ); ?>"/>
128
        </p>
129
        <p>
130
            <label for="<?php echo esc_attr( $this->get_field_id( 'testimonial_fadeout' ) ); ?>"><?php _e( 'Testimonial Fade Out (miliseconds)', 'ivycat-ajax-testimonials' ); ?></label>
131
            <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'testimonial_fadeout' ) ); ?>"
132
                   id="<?php echo esc_attr( $this->get_field_id( 'testimonial_fadeout' ) ); ?>" class="widefat"
133
                   value="<?php echo absint( $instance['testimonial_fadeout'] ); ?>"/>
134
        </p>
135
		<?php
136
		do_action( 'ic_testimonials_widget_form', $instance );
137
	}
138
139
	public function widget( $args, $instance ) {
140
		global $ivycat_testimonials;
141
142
		$title    = empty( $instance['title'] ) ? ' ' : apply_filters( 'widget_title', $instance['title'] );
143
		$quantity = ( $instance['testimonial_quantity'] ) ? absint( $instance['testimonial_quantity'] ) : 1;
144
		$group    = ( isset( $instance['testimonial_group'] ) && 'All Groups' !== $instance['testimonial_group'] ) ? $instance['testimonial_group'] : false;
145
		$atts     = array(
146
			'quantity'          => ( is_numeric( $quantity ) ) ? $quantity : 3,
147
			'group'             => $group,
148
			'link_testimonials' => $instance['testimonial_link_testimonials'],
149
			'num_words'         => ( is_numeric( $instance['testimonial_num_words'] ) ) ? $instance['testimonial_num_words'] : false,
150
			'more_tag'          => ( strlen( $instance['testimonial_read_more'] ) > 1 ) ? $instance['testimonial_read_more'] : 'Read More...',
151
			'ajax_on'           => ( 'no' == $instance['testimonial_ajax_on'] ) ? 'no' : 'yes',
152
			'display'           => ( 'single' == $instance['testimonial_display'] ) ? 'single' : 'list',
153
			'all_title'         => ( strlen( $instance['testimonial_show_all_title'] ) > 1 ) ? $instance['testimonial_show_all_title'] : false,
154
			'all_url'           => ( strlen( $instance['testimonial_show_all'] ) > 1 ) ? $instance['testimonial_show_all'] : false,
155
			'template'          => ( strlen( $instance['template'] ) ) ? $instance['template'] : false,
156
			'fade_in'           => $instance['testimonial_fadein'],
157
			'fade_out'          => $instance['testimonial_fadeout'],
158
			'speed'             => $instance['testimonial_slide_speed'],
159
		);
160
		echo $args['before_widget'];
161
		echo ( empty( $title ) ) ? '' : $args['before_title'] . $title . $args['after_title'];
162
		echo $ivycat_testimonials->do_testimonials( $atts );
163
		echo $args['after_widget'];
164
	}
165
166
	public function update( $new_instance, $old_instance ) {
167
		$instance                                  = $old_instance;
168
		$widget_id                                 = 'widget-' . $_POST['id_base'] . '-' . $_POST['widget_number'] . '-';
0 ignored issues
show
Unused Code introduced by
$widget_id 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...
169
		$instance['testimonial_group']             = wp_strip_all_tags( $new_instance['testimonial_group'] );
170
		$instance['testimonial_quantity']          = absint( $new_instance['testimonial_quantity'] );
171
		$instance['testimonial_num_words']         = absint( $new_instance['testimonial_num_words'] );
172
		$instance['testimonial_read_more']         = wp_strip_all_tags( $new_instance['testimonial_read_more'] );
173
		$instance['testimonial_ajax_on']           = ( isset( $new_instance['testimonial_ajax_on'] ) ? true : false );
174
		$instance['testimonial_display']           = ( isset( $new_instance['testimonial_display'] ) ? 'list' : 'single' );
175
		$instance['testimonial_show_all_title']    = sanitize_text_field( $new_instance['testimonial_show_all_title'] );
176
		$instance['testimonial_show_all']          = esc_url_raw( $new_instance['testimonial_show_all'] );
177
		$instance['title']                         = wp_strip_all_tags( $new_instance['title'] );
178
		$instance['template']                      = wp_strip_all_tags( $new_instance['template'] );
179
		$instance['testimonial_slide_speed']       = absint( $new_instance['testimonial_slide_speed'] );
180
		$instance['testimonial_fadein']            = absint( $new_instance['testimonial_fadein'] );
181
		$instance['testimonial_fadeout']           = absint( $new_instance['testimonial_fadeout'] );
182
		$instance['testimonial_link_testimonials'] = ( isset( $new_instance['testimonial_link_testimonials'] ) ? true : false );
183
184
		return apply_filters( 'ic_testimonials_widget_save', $instance, $new_instance );
185
	}
186
}
187