This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-bootstrap-recent.php
173 lines (139 loc) · 4.8 KB
/
wp-bootstrap-recent.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
<?php
/**
* The plugin file
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @link https://github.com/Narthur/wp-bootstrap-recent
* @since 1.0.0
* @package wp-bootstrap-recent
*
* @wordpress-plugin
* Plugin Name: Simple Recent Posts Slider
* Plugin URI: https://github.com/Narthur/wp-bootstrap-recent
* Description: Adds a light-weight and responsive recent posts slider shortcode.
* Version: 1.0.0
* Author: Nathan Arthur
* Author URI: http://NathanArthur.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: wp-bootstrap-recent
*/
// If this file is called directly, abort.
namespace {
if ( ! defined( 'WPINC' ) ) {
die;
}
}
namespace bootstrap_slider_space {
function activate_thumbnail_support() {
add_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'bootstrap_slider_space\\activate_thumbnail_support' );
function enqueue_assets() {
enqueue_bootstrap();
enqueue_unslider();
enqueue_bootstrap_slider();
}
function enqueue_bootstrap_slider() {
wp_enqueue_script(
'bootstrap-slider',
plugins_url( 'main.js', __FILE__ ),
array( 'jquery', 'unslider' )
);
wp_enqueue_style(
'bootstrap-slider',
plugins_url( 'style.css', __FILE__ )
);
}
function enqueue_unslider() {
wp_enqueue_style(
'unslider',
plugins_url( 'includes/unslider/dist/css/unslider.css', __FILE__ )
);
wp_enqueue_script(
'unslider',
plugins_url( 'includes/unslider/src/js/unslider.js', __FILE__ ),
array( 'jquery' )
);
}
function enqueue_bootstrap() {
wp_enqueue_style(
'bootstrap',
plugins_url( 'includes/bootstrap-3.3.6-dist/css/bootstrap.min.css', __FILE__ )
);
wp_enqueue_script(
'bootstrap',
plugins_url( 'includes/bootstrap-3.3.6-dist/js/bootstrap.min.js', __FILE__ ),
array( 'jquery' )
);
}
add_action( 'wp_enqueue_scripts', 'bootstrap_slider_space\\enqueue_assets' );
function slider( $atts ) {
$a = shortcode_atts( array(
'quantity' => 3,
'category' => null
), $atts );
$recent_posts = get_recent_posts( $a );
return make_slider( $recent_posts );
}
function make_slider( $recent_posts ) {
$slides = '';
foreach ( $recent_posts as $p ) {
$url = get_permalink( $p );
$thumbnailDiv = make_thumbnail_div( $p, $url );
$bodyDiv = make_body_div( $p, $url );
$slide = '<li><div class="media">' . $thumbnailDiv . $bodyDiv . '</div></li>';
$slides .= $slide;
}
$classes = ( count($recent_posts) > 1 ) ? 'bootstrap-slider' : 'bootstrap-slider single';
return "<div class='$classes'><ul>$slides</ul></div>";
}
function get_recent_posts( $a ) {
$categoryId = ( $a['category'] ) ? get_cat_ID($a['category']) : 0;
$args = array(
'numberposts' => $a['quantity'],
'offset' => 0,
'category' => $categoryId,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
return wp_get_recent_posts( $args, ARRAY_A );
}
function make_body_div( $p, $url ) {
$title = $p['post_title'];
$excerpt = get_excerpt_by_id( $p['ID'] );
$h4 = "<h4 class='media-heading'><a href='$url'>$title</a></h4>";
$text = "<p>$excerpt</p>";
$icon = '<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>';
$button = "<a href='$url' class='btn btn-default btn-sm'>Read More $icon</a>";
return "<div class='media-body'>{$h4}{$text}{$button}</div>";
}
function make_thumbnail_div( $p, $url ) {
$image = get_the_post_thumbnail( get_post( $p['ID'] ), 'thumbnail', array(
'class' => 'media-object'
) );
return ( $image ) ? "<div class='media-left'><a href='$url' class='thumbnail'>$image</a></div>" : '';
}
function get_excerpt_by_id( $post_id ) {
$the_post = get_post( $post_id ); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 35; //Sets excerpt length by word count
$the_excerpt = strip_tags( strip_shortcodes( $the_excerpt ) ); //Strips tags and images
$words = explode( ' ', $the_excerpt, $excerpt_length + 1 );
if ( count( $words ) > $excerpt_length ) :
array_pop( $words );
array_push( $words, '…' );
$the_excerpt = implode( ' ', $words );
endif;
$the_excerpt = '<p>' . $the_excerpt . '</p>';
return $the_excerpt;
}
add_shortcode( 'recent_slider_simple', 'bootstrap_slider_space\\slider' );
}