Completed
Push — master ( 64c9c6...973681 )
by Michael
02:15
created

MetaSlider_Widget::form()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 53
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 41
nc 24
nop 1
dl 0
loc 53
rs 7.1199
c 0
b 0
f 0

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Adds Meta Slider widget.
4
 */
5
6
// disable direct access
7
if (!defined('ABSPATH')) {
8
    exit;
9
}
10
11
/**
12
 * Class MetaSlider_Widget
13
 */
14
class MetaSlider_Widget extends WP_Widget
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
{
16
    /**
17
     * Register widget with WordPress.
18
     */
19
    public function __construct()
20
    {
21
        parent::__construct('metaslider_widget', // Base ID
22
                            'Meta Slider', // Name
23
                            array('description' => __('Meta Slider', 'metaslider')) // Args
24
        );
25
    }
26
27
    /**
28
     * Front-end display of widget.
29
     *
30
     * @see WP_Widget::widget()
31
     *
32
     * @param array $args     Widget arguments.
33
     * @param array $instance Saved values from database.
34
     */
35
    public function widget($args, $instance)
36
    {
37
        extract($args);
38
39
        if (isset($instance['slider_id'])) {
40
            $slider_id = $instance['slider_id'];
41
42
            $title = apply_filters('widget_title', $instance['title']);
43
44
            echo $before_widget;
45
            if (!empty($title)) {
46
                echo $before_title . $title . $after_title;
47
            }
48
49
            echo do_shortcode("[metaslider id={$slider_id}]");
50
            echo $after_widget;
51
        }
52
    }
53
54
    /**
55
     * Sanitize widget form values as they are saved.
56
     *
57
     * @see WP_Widget::update()
58
     *
59
     * @param array $new_instance Values just sent to be saved.
60
     * @param array $old_instance Previously saved values from database.
61
     *
62
     * @return array Updated safe values to be saved.
63
     */
64
    public function update($new_instance, $old_instance)
65
    {
66
        $instance              = array();
67
        $instance['slider_id'] = strip_tags($new_instance['slider_id']);
68
        $instance['title']     = strip_tags($new_instance['title']);
69
70
        return $instance;
71
    }
72
73
    /**
74
     * Back-end widget form.
75
     *
76
     * @see WP_Widget::form()
77
     *
78
     * @param array $instance Previously saved values from database.
79
     */
80
    public function form($instance)
81
    {
82
        $selected_slider = 0;
83
        $title           = '';
84
        $sliders         = false;
85
86
        if (isset($instance['slider_id'])) {
87
            $selected_slider = $instance['slider_id'];
88
        }
89
90
        if (isset($instance['title'])) {
91
            $title = $instance['title'];
92
        }
93
94
        $posts = get_posts(array(
95
                               'post_type'      => 'ml-slider',
96
                               'post_status'    => 'publish',
97
                               'orderby'        => 'date',
98
                               'order'          => 'ASC',
99
                               'posts_per_page' => -1
100
                           ));
101
102
        foreach ($posts as $post) {
103
            $active = $selected_slider == $post->ID ? true : false;
104
105
            $sliders[] = array(
106
                'active' => $active,
107
                'title'  => $post->post_title,
108
                'id'     => $post->ID
109
            );
110
        } ?>
111
        <p>
112
        <?php if ($sliders) {
113
            ?>
114
        <p>
115
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
116
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>"/>
117
        </p>
118
        <label for="<?php echo $this->get_field_id('slider_id'); ?>"><?php _e('Select Slider:', 'metaslider'); ?></label>
119
        <select id="<?php echo $this->get_field_id('slider_id'); ?>" name="<?php echo $this->get_field_name('slider_id'); ?>">
120
            <?php
121
            foreach ($sliders as $slider) {
122
                $selected = $slider['active'] ? 'selected=selected' : '';
123
                echo "<option value='{$slider['id']}' {$selected}>{$slider['title']}</option>";
124
            } ?>
125
        </select>
126
        <?php
127
        } else {
128
            _e('No slideshows found', 'metaslider');
129
        } ?>
130
        </p>
131
        <?php
132
    }
133
}
134
135
add_action('widgets_init', 'register_metaslider_widget');
136
137
function register_metaslider_widget()
138
{
139
    register_widget('MetaSlider_Widget');
140
}
141
142
?>
143