Completed
Push — development ( 4ee6a1...22da9f )
by Eric
01:27
created

IvyCatTestimonialsWidget::update()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 18
nc 8
nop 2
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for form.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

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 ) :
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(object)" but found "( object )"
Loading history...
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 sc_attr( $this->get_field_name( 'testimonial_read_more' ) ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sc_attr'
Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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'];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$args'
Loading history...
161
		echo ( empty( $title ) ) ? '' : $args['before_title'] . $title . $args['after_title'];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
introduced by
Expected next thing to be a escaping function, not '$title'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$args'
Loading history...
162
		echo $ivycat_testimonials->do_testimonials( $atts );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$ivycat_testimonials'
Loading history...
163
		echo $args['after_widget'];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$args'
Loading history...
164
	}
165
166
	public function update( $new_instance, $old_instance ) {
0 ignored issues
show
Coding Style introduced by
update uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
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