-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-slider-2.php
235 lines (206 loc) · 7.75 KB
/
simple-slider-2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/*
Plugin Name: Simple Slider 2
Plugin URI: https://github.com/diliaur/simple-slider-2
Description: A post slider based upon categories and featured image. Lets you choose time frame for posts to display and max number of post slots.
Version: 2.0
Author: Diliaur Tellei
Author URI: http://diliaur.github.io
License: GPL2
*/
class simple_slider_2_widget extends WP_Widget {
/**
*
* Constructor. Sets up the widget.
*
*/
public function __construct() {
$widget_options = array(
'classname' => 'simple_slider_2_widget',
'description' => 'A featured images-, category-based post slider.'
);
parent::__construct('simple_slider_2_widget','Simple Slider 2.0 Widget', $widget_options);
}
/**
*
* Outputs the admin-facing part of the widget. Provides widget options via a form.
* @param $instance The widget data & config options pulled from the database.
*/
public function form( $instance ) {
$title = !empty($instance['title']) ? $instance['title'] : "New title";
$max_num_posts = !empty($instance['max_num_posts']) ? $instance['max_num_posts'] : 5; // 5 is default
$time_frame = !empty($instance['time_frame']) ? $instance['time_frame'] : "-1 month"; // default is 1 month in the past
$categories = !empty($instance['categories']) ? $instance['categories'] : "";
?>
<!-- TITLE -->
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_attr_e( 'Title:', 'simple-slider-2' ); ?></label>
<input class="widefat"
type="text"
id="<?php echo $this->get_field_id('title'); ?>"
name="<?php echo esc_attr($this->get_field_name('title')); ?>"
value="<?php echo $title; ?>">
</p>
<!-- MAX NUMBER OF POSTS -->
<p>
<label><strong><?php esc_attr_e('Maximum number of posts to display:', 'simple-slider-2') ?></strong></label>
<br/><em>Recommended absolute max is 7.</em>
<input class="widefat"
type="text"
id="<?php echo esc_attr($this->get_field_id('max_num_posts')); ?>"
name="<?php echo esc_attr($this->get_field_name('max_num_posts')); ?>"
value="<?php echo $max_num_posts; ?>">
</p>
<!-- TIME FRAME OF POSTS -->
<p><strong>Only show posts within the timeframe:</strong><br/>
<input type="radio"
value="-1 week"
name="<?php echo esc_attr_e($this->get_field_name('time_frame')); ?>"
<?php checked( $time_frame, "-1 week" ); ?>>
<label>Past week</label><br/>
<input type="radio"
value="-1 month"
name="<?php echo esc_attr_e($this->get_field_name('time_frame')); ?>"
<?php checked( $time_frame, "-1 month" ); ?>>
<label>Past month</label><br/>
<input type="radio"
value="-3 months"
name="<?php echo esc_attr_e($this->get_field_name('time_frame')); ?>"
<?php checked( $time_frame, "-3 months" ); ?>>
<label>Past 3 months</label><br/>
<input type="radio"
value="-6 months"
name="<?php echo esc_attr_e($this->get_field_name('time_frame')); ?>"
<?php checked( $time_frame, "-6 months" ); ?>>
<label>Past 6 months</label><br/>
<input type="radio"
value="-1 year"
name="<?php echo esc_attr_e($this->get_field_name('time_frame')); ?>"
<?php checked( $time_frame, "-1 year" ); ?>>
<label>Past year</label><br/>
<input type="radio"
value="-3 years"
name="<?php echo esc_attr_e($this->get_field_name('time_frame')); ?>"
<?php checked( $time_frame, "-3 years" ); ?>>
<label>Past 3 years</label>
</p>
<!-- FEATURED CATEGORIES - which category/ies to pull for posts -->
<p>
<label><strong>Featured categories (comma-separated):</strong></label>
<br/><em>These must be categories which already exist on your site.</em>
<input class="widefat"
type="text"
id="<?php echo esc_attr_e($this->get_field_id('categories')); ?>"
name="<?php echo esc_attr($this->get_field_name('categories')); ?>"
value="<?php echo esc_attr($categories); ?>">
</p>
<?php
}
/**
*
* Outputs the visitor-facing part of the widget. Uses The Loop to show posts.
* @param $args Widget title, description, etc.
* @param $instance The widget data & config options pulled from the database
*/
public function widget( $args, $instance ) {
// vars
$categories = $instance['categories'];
$max_posts = $instance['max_num_posts'];
$time_frame = $instance['time_frame'];
// set up the args for the query
$args = array(
'category_name' => $categories,
'posts_per_page' => $max_posts
//'meta_key' => '_thumnail_id'
);
$my_query = new WP_Query( $args );
//test line
//echo "<p>showing posts from following categories: " . $categories . " from within " . $time_frame . "</p>";
//////////////
?>
<div class="ss2-container-slider">
<div class="ss2-container-slides">
<ul class="ss2-slide-list">
<?php
$args = array(
'category_name' => $categories,
'posts_per_page' => $max_posts,
'meta_key' => '_thumbnail_id'
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
//$do_not_duplicate = $post->ID; idk if this is necessary?
if ( has_post_thumbnail() && (strtotime(get_the_date()) > strtotime($time_frame)) ) { // if in this order, # slides is impacted.
?>
<li>
<div class="ss2-title-and-date">
<span class="ss2-title"><a href=<?php the_permalink(); ?>><?php the_title(); ?></a></span>
<span class="ss2-date"><?php echo get_the_date(); ?></span>
</div>
<div class="ss2-categories"><?php the_category(); ?></div>
<a href=<?php the_permalink(); ?>>
<?php the_post_thumbnail(null, array( 'class' => 'current-slide-img' )); ?>
</a>
<div class="ss2-excerpt"><?php the_excerpt(); ?></div>
</li>
<?php
} else {
//echo "sorry, this post is too old to show<br/>";
}
endwhile;
wp_reset_postdata(); // since used the_post()
?>
</ul>
<div class="ss2-slide-nav">
<div class="ss2-arrow-left"><i class="fa fa-chevron-left fa-2x"></i></div>
<div class="ss2-nav-dots"></div>
<div class="ss2-arrow-right"><i class="fa fa-chevron-right fa-2x"></i></div>
<div class="ss2-clearfix"></div>
</div>
</div>
<div class="ss2-container-titles"></div>
<div class="ss2-clearfix"></div>
</div><!--end slider container-->
<?php
//////////////
}
/**
*
* Saves the new admin configurations to the database.
* @param $new_instance The new options
* @param $old_instance The link to the current instance in the db, to be updated
*
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['max_num_posts'] = $new_instance['max_num_posts'];
$instance['time_frame'] = $new_instance['time_frame'];
// need to do some parsing for categories. first remove whitespace
$categories = str_replace(" ", "", $new_instance['categories']);
// then escape
$instance['categories'] = esc_attr($categories);
return $instance;
}
}
/**
*
* Register the widget
*
*/
function register_sslider2_widget() {
register_widget( 'simple_slider_2_widget' );
}
add_action('widgets_init', 'register_sslider2_widget');
/**
*
* Enqueue scripts and styles
*
*/
function simple_slider_2_enqueue_scripts_styles() {
wp_enqueue_script( 'simple-slider-2-js', plugins_url( 'slider.js', __FILE__ ), array( 'jquery' ), date("h:i:s") ); // slider.js
wp_enqueue_style( 'simple-slider-2-css', plugins_url( 'slider.css', __FILE__ ) );
wp_enqueue_style( 'ss2-font-awesome', plugins_url('lib/font-awesome-4.7.0/css/font-awesome.min.css',__FILE__) ); // font awesome icons
}
add_action( 'wp_enqueue_scripts', 'simple_slider_2_enqueue_scripts_styles' );
?>