|
1
|
|
|
<?php |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
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.