-
Notifications
You must be signed in to change notification settings - Fork 4
/
class.uw-slideshow.php
203 lines (152 loc) · 5.73 KB
/
class.uw-slideshow.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
<?php
/**!
* Plugin Name: UW Slideshow
* Plugin URI: http://uw.edu/brand/web/#slideshow
* Description: Put a slideshow on your pages and posts.
* Version: 1.0
* Author: UW Web Team
*/
class UW_Slideshow
{
const POST_TYPE = 'slideshow';
const POST_TYPE_NAME = 'Slideshow';
const POST_TYPE_PLURAL = 'Slideshows';
const META_BOX_TITLE = 'Slides';
function __construct()
{
add_action( 'init', array( $this, 'register_slideshow_post_type' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_slideshow_assets' ) );
add_action( 'wp_ajax_get_current_uw_slideshow', array( $this, 'get_current_uw_slideshow') );
add_action( 'save_post_' . self::POST_TYPE , array( $this, 'save_slideshow') );
add_filter( 'manage_'. self::POST_TYPE .'_posts_columns', array( $this, 'add_shortcode_column' ) );
add_action( 'manage_posts_custom_column' , array( $this, 'add_shortcode_column_content' ) , 10, 2 );
add_shortcode( 'slideshow', array( $this, 'shortcode') );
}
function register_slideshow_post_type()
{
$labels = array(
'name' => self::POST_TYPE_PLURAL,
'singular_name' => self::POST_TYPE_NAME,
'add_new_item' => 'Add New '. self::POST_TYPE_NAME
);
register_post_type( self::POST_TYPE,
array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'has_archive' => false,
'menu_position' => 5,
'show_in_nav_menus' => true,
'register_meta_box_cb' => array( $this, 'add_slideshow_meta_box' ),
'supports' => array( 'title' ),
)
);
}
function add_slideshow_meta_box()
{
add_meta_box(
self::POST_TYPE,
self::META_BOX_TITLE,
array( $this, 'add_slideshow_meta_box_html'),
self::POST_TYPE
);
}
function add_slideshow_meta_box_html()
{
wp_nonce_field( self::POST_TYPE . '_meta_box', self::POST_TYPE . '_meta_box_nonce' );
?>
<ul>
</ul>
<p>
<a id="add-new-slide" class="button-primary" href="#">Add a New Slide</a>
</p>
<?php
}
function save_slideshow( $post_id ) {
if ( ! empty( $_POST ) && ! check_admin_referer( self::POST_TYPE . '_meta_box', self::POST_TYPE . '_meta_box_nonce' ) ) {
return $post_id;
}
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
if ( ! empty( $_POST ) && check_admin_referer( self::POST_TYPE . '_meta_box', self::POST_TYPE . '_meta_box_nonce' ) && self::POST_TYPE == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
if ( isset( $_POST['slides'] ) )
update_post_meta( $post_id, 'slides', $_POST['slides'] );
}
function register_slideshow_assets()
{
wp_enqueue_script( self::POST_TYPE, plugins_url( 'js/admin.uw-slideshow.dev.js', __FILE__ ), array('backbone', 'jquery-ui-sortable') );
wp_enqueue_style( self::POST_TYPE, plugins_url( 'css/admin.uw-slideshow.css', __FILE__ ) );
wp_enqueue_media();
}
function add_shortcode_column( $columns )
{
return array_merge( array_slice( $columns, 0, 2 ), array('shortcode'=>'Shortcode'), array_slice( $columns, 2, null ));
}
function add_shortcode_column_content( $column, $post_id )
{
if ( $column == 'shortcode' ) echo '[slideshow id='. $post_id .']';
}
function shortcode( $atts )
{
$atts = (object) shortcode_atts( array(
'id' => null,
'simple' => false,
'fullwidth' => false,
'tallerimage' => false,
), $atts);
if ( ! $atts->id ) return;
$slides = (object) get_post_meta( $atts->id, 'slides', true );
// 137-141 Creates for a simple slideshow
$class = ( $atts->simple === "true" ? ' photo-slider' : null);
$fullwidth = ( 'true' === $atts->fullwidth ? ' fullwidth' : null );
$tallerimage = ( 'true' === $atts->tallerimage ? ' tallerimage' : null );
$slidereturn = '<div tabIndex="0" class="uw-slideshow' . $fullwidth . ' ' . $tallerimage . ' ' . $class . ' ' . 'slideshow-' . $atts->id . '">';
foreach ($slides as $slide )
{
$slide = (object) $slide;
$slide->title = empty($slide->title) ? '' : $slide->title;
$slide->esctitle = empty( $slide->title ) ? '' : esc_attr( $slide->title );
$slide->link = empty( $slide->link ) ? '' : $slide->link;
$slide->text = empty( $slide->text ) ? '' : $slide->text;
$slide->image = empty( $slide->image ) ? '' : $slide->image;
$slidereturn .= "<div class='slide " . ($slide->text || $slide->title ? 'has-text' : 'no-text') . "'>" .
"<a tabIndex='-1' href='{$slide->link}' title='{$slide->esctitle}'><img src='{$slide->image}' title='{$slide->esctitle}' /></a>" .
"<div>" .
"<h3 tabIndex='-1'><a tabIndex='-1' href='{$slide->link}' title='{$slide->esctitle}'>{$slide->title}</a>".
"</h3>".
"<p>{$slide->text}</p>" .
"</div>" .
"</div>";
}
return $slidereturn . '</div>';
}
function get_current_uw_slideshow()
{
$slideshow = array();
$slides = get_post_meta( $_GET['id'], 'slides', true );
$slides = $slides ? $slides : array();
foreach ($slides as $slide )
{
$slideshow[] = $slide;
}
wp_die( json_encode( $slideshow ) );
}
// Helper functions
static public function get_latest_slideshow()
{
$posts = get_posts( array(
'post_type' => self::POST_TYPE,
'numberposts' => 1
) );
$slideshow = array_shift( $posts );
$slides = get_post_meta( $slideshow->ID, 'slides', true );
return $slides ? json_decode( json_encode( array_reverse( $slides ) ) ) : array();
}
}
new UW_Slideshow;