-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathowlcarousel.php
executable file
·459 lines (385 loc) · 16 KB
/
owlcarousel.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
/*
Plugin Name: Owl Carousel
Description: A simple plugin to include an Owl Carousel in any post
Author: Pierre JEHAN
Version: 0.5.3
Author URI: http://www.pierre-jehan.com
Licence: GPL2
*/
add_theme_support('post-thumbnails');
add_action('init', 'owlcarousel_init');
add_action('wp_print_scripts', 'owl_register_scripts');
add_action('wp_print_styles', 'owl_register_styles');
add_action('widgets_init', 'owl_widgets_init');
add_action('manage_edit-owl-carousel_columns', 'owl_columnfilter');
add_action('manage_posts_custom_column', 'owl_column');
add_action('admin_menu', 'owl_carousel_menu');
add_action('admin_enqueue_scripts', 'owl_carousel_admin_register_scripts');
if (filter_var(get_option('owl_carousel_wordpress_gallery', false), FILTER_VALIDATE_BOOLEAN)) {
add_filter('post_gallery', 'owl_carousel_post_gallery', 10, 2);
}
// Add functions to create a new attachments fields
add_filter("attachment_fields_to_edit", "owl_carousel_attachment_fields_to_edit", null, 2);
add_filter("attachment_fields_to_save", "owl_carousel_attachment_fields_to_save", null, 2);
/**
* Initilize the plugin
*/
function owlcarousel_init() {
$labels = array(
'name' => __('Owl Carousel', 'owl-carousel-domain'),
'singular_name' => __('Carousel Slide', 'owl-carousel-domain'),
'add_new' => __('Add New Slide', 'owl-carousel-domain'),
'add_new_item' => __('Add New Carousel Slide', 'owl-carousel-domain'),
'edit_item' => __('Edit Carousel Slide', 'owl-carousel-domain'),
'new_item' => __('Add New Carousel Slide', 'owl-carousel-domain'),
'view_item' => __('View Slide', 'owl-carousel-domain'),
'search_items' => __('Search Carousel', 'owl-carousel-domain'),
'not_found' => __('No carousel slides found', 'owl-carousel-domain'),
'not_found_in_trash' => __('No carousel slides found in trash', 'owl-carousel-domain'),
);
register_post_type('owl-carousel', array(
'public' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'label' => 'Owl Carousel',
'menu_icon' => plugins_url('/owl-carousel/images/owl-logo-16.png'),
'labels' => $labels,
'capability_type' => 'post',
'supports' => array(
'title',
'editor',
'thumbnail'
)
));
$taxonomy_labels = array(
'name' => __('Carousels', 'owl-carousel-domain'),
'singular_name' => __('Carousel', 'owl-carousel-domain'),
'search_items' => __('Search Carousels', 'owl-carousel-domain'),
'popular_items' => __('Popular Carousels', 'owl-carousel-domain'),
'all_items' => __('All Carousels', 'owl-carousel-domain'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Edit Carousel', 'owl-carousel-domain'),
'update_item' => __('Update Carousel', 'owl-carousel-domain'),
'add_new_item' => __('Add New Carousel', 'owl-carousel-domain'),
'new_item_name' => __('New Carousel Name', 'owl-carousel-domain'),
'separate_items_with_commas' => __('Separate carousels with commas', 'owl-carousel-domain'),
'add_or_remove_items' => __('Add or remove carousels', 'owl-carousel-domain'),
'choose_from_most_used' => __('Choose from the most used carousels', 'owl-carousel-domain'),
'not_found' => __('No carousels found.', 'owl-carousel-domain'),
'menu_name' => __('Carousels', 'owl-carousel-domain'),
);
register_taxonomy('Carousel', 'owl-carousel', array(
'labels' => $taxonomy_labels,
'rewrite' => array('slug' => 'carousel'),
'hierarchical' => true,
'show_admin_column' => true,
));
add_image_size('owl_widget', 180, 100, true);
add_image_size('owl_function', 600, 280, true);
add_shortcode('owl-carousel', 'owl_function');
add_filter("mce_external_plugins", "owl_register_tinymce_plugin");
add_filter('mce_buttons', 'owl_add_tinymce_button');
// Add Wordpress Gallery option
add_option('owl_carousel_wordpress_gallery', 'off');
add_option('owl_carousel_orderby', 'post_date');
}
function owl_carousel_menu() {
add_submenu_page('edit.php?post_type=owl-carousel', __('Parameters', 'owl-carousel-domain'), __('Parameters', 'owl-carousel-domain'), 'manage_options', 'owl-carousel-parameters', 'submenu_parameters');
}
function submenu_parameters() {
$isWordpressGallery = (filter_var(get_option('owl_carousel_wordpress_gallery', false), FILTER_VALIDATE_BOOLEAN)) ? 'checked' : '';
$orderBy = get_option('owl_carousel_orderby', 'post_date');
$orderByOptions = array('post_date', 'title');
echo '<div class="wrap owl_carousel_page">';
echo '<?php update_option("owl_carousel_wordpress_gallery", $_POST["wordpress_gallery"]); ?>';
echo '<h2>' . __('Owl Carousel parameters', 'owl-carousel-domain') . '</h2>';
echo '<form action="' . plugin_dir_url(__FILE__) . 'save_parameter.php" method="POST" id="owlcarouselparameterform">';
echo '<h3>' . __('Wordpress Gallery', 'owl-carousel-domain') . '</h3>';
echo '<input type="checkbox" name="wordpress_gallery" ' . $isWordpressGallery . ' />';
echo '<label>' . __('Use Owl Carousel with Wordpress Gallery', 'owl-carousel-domain') . '</label>';
echo '<br />';
echo '<label>' . __('Order Owl Carousel elements by ', 'owl-carousel-domain') . '</label>';
echo '<select name="orderby" />';
foreach ($orderByOptions as $option) {
echo '<option value="' . $option . '" ' . (($option == $orderBy) ? 'selected="selected"' : '') . '>' . $option . '</option>';
}
echo '</select>';
echo '<br />';
echo '<br />';
echo '<input type="submit" class="button-primary owl-carousel-save-parameter-btn" value="' . __('Save changes', 'owl-carousel-domain') . '" />';
echo '<span class="spinner"></span>';
echo '</form>';
echo '</div>';
}
/**
* List of JavaScript / CSS files for admin
*/
function owl_carousel_admin_register_scripts() {
wp_register_style('owl.carousel.admin.styles', plugin_dir_url(__FILE__) . 'css/admin_styles.css');
wp_enqueue_style('owl.carousel.admin.styles');
wp_register_script('owl.carousel.admin.script', plugin_dir_url(__FILE__) . 'js/admin_script.js');
wp_enqueue_script('owl.carousel.admin.script');
}
/**
* List of JavaScript files
*/
function owl_register_scripts() {
wp_register_script('js.owl.carousel', plugins_url('/owl-carousel/js/owl.carousel.js'));
wp_register_script('js.owl.carousel.script', plugins_url('/owl-carousel/js/script.js'));
wp_enqueue_script('jquery');
wp_enqueue_script('js.owl.carousel');
wp_enqueue_script('js.owl.carousel.script');
}
/**
* List of CSS files
*/
function owl_register_styles() {
wp_register_style('style.owl.carousel', plugins_url('/owl-carousel/css/owl.carousel.css'));
wp_register_style('style.owl.carousel.theme', plugins_url('/owl-carousel/css/owl.theme.css'));
wp_register_style('style.owl.carousel.transitions', plugins_url('/owl-carousel/css/owl.transitions.css'));
wp_register_style('style.owl.carousel.styles', plugins_url('/owl-carousel/css/styles.css'));
wp_enqueue_style('style.owl.carousel');
wp_enqueue_style('style.owl.carousel.theme');
wp_enqueue_style('style.owl.carousel.transitions');
wp_enqueue_style('style.owl.carousel.styles');
}
function owl_register_tinymce_plugin($plugin_array) {
$plugin_array['owl_button'] = plugins_url('/owl-carousel/js/owl-tinymce-plugin.js');
return $plugin_array;
}
function owl_add_tinymce_button($buttons) {
$buttons[] = "owl_button";
return $buttons;
}
/*
* Initialize Owl Widget
*/
function owl_widgets_init() {
register_widget("owl_Widget");
}
class owl_Widget extends WP_Widget {
public function __construct() {
parent::__construct('owl_Widget', 'Owl Carousel', array('description' => __('A Owl Carousel Widget', 'text_domain')));
}
public function form($instance) {
if (isset($instance['title'])) {
$title = $instance['title'];
} else {
$title = __('Widget Carousel', 'text_domain');
}
if (isset($instance['category'])) {
$carousel = $instance['category'];
} else {
$carousel = 'Uncategorized';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<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); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Carousel:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" type="text" value="<?php echo esc_attr($carousel); ?>" />
</p>
<?php
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = strip_tags($new_instance['title']);
$instance['category'] = strip_tags($new_instance['category']);
return $instance;
}
public function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if (!empty($title))
echo $before_title . $title . $after_title;
echo owl_function(array(category => $instance['category'], singleItem => "true", autoPlay => "true", pagination => "false"));
echo $after_widget;
}
}
/**
* Add custom column filters in administration
* @param array $columns
*/
function owl_columnfilter($columns) {
$thumb = array('thumbnail' => 'Image');
$columns = array_slice($columns, 0, 2) + $thumb + array_slice($columns, 2, null);
return $columns;
}
/**
* Add custom column contents in administration
* @param string $columnName
*/
function owl_column($columnName) {
global $post;
if ($columnName == 'thumbnail') {
echo edit_post_link(get_the_post_thumbnail($post->ID, 'thumbnail'), null, null, $post->ID);
}
}
/**
* Adding our images custom fields to the $form_fields array
* @param array $form_fields
* @param object $post
* @return array
*/
function owl_carousel_attachment_fields_to_edit($form_fields, $post) {
// add our custom field to the $form_fields array
// input type="text" name/id="attachments[$attachment->ID][custom1]"
$form_fields["owlurl"] = array(
"label" => __("Owl Carousel URL"),
"input" => "text",
"value" => get_post_meta($post->ID, "_owlurl", true)
);
return $form_fields;
}
/**
* Save images custom fields
* @param array $post
* @param array $attachment
* @return array
*/
function owl_carousel_attachment_fields_to_save($post, $attachment) {
if (isset($attachment['owlurl'])) {
update_post_meta($post['ID'], '_owlurl', $attachment['owlurl']);
}
return $post;
}
/**
* Plugin main function
* @param type $atts Owl parameters
* @param type $content
* @return string Owl HTML code
*/
function owl_function($atts, $content = null) {
extract(shortcode_atts(array(
'category' => 'Uncategoryzed'
), $atts));
$data_attr = "";
foreach ($atts as $key => $value) {
if ($key != "category") {
$data_attr .= ' data-' . $key . '="' . $value . '" ';
}
}
$lazyLoad = array_key_exists("lazyload", $atts) && $atts["lazyload"] == true;
$args = array(
'post_type' => 'owl-carousel',
'orderby' => get_option('owl_carousel_orderby', 'post_date'),
'order' => 'asc',
'tax_query' => array(
array(
'taxonomy' => 'Carousel',
'field' => 'slug',
'terms' => $atts['category']
)
),
'nopaging' => true
);
$result = '<div id="owl-carousel-' . rand() . '" class="owl-carousel owl-carousel-' . sanitize_title($atts['category']) . '" ' . $data_attr . '>';
$loop = new WP_Query($args);
while ($loop->have_posts()) {
$loop->the_post();
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), get_post_type());
$meta_link = get_post_meta(get_post_thumbnail_id(get_the_ID()), '_owlurl', true);
$result .= '<div class="item">';
if ($img_src[0]) {
$result .= '<div>';
if (!empty($meta_link)) {
$result .= '<a href="' . $meta_link . '">';
}
if ($lazyLoad) {
$result .= '<img class="lazyOwl" title="' . get_the_title() . '" data-src="' . $img_src[0] . '" alt="' . get_the_title() . '"/>';
} else {
$result .= '<img title="' . get_the_title() . '" src="' . $img_src[0] . '" alt="' . get_the_title() . '"/>';
}
if (!empty($meta_link)) {
$result .= '</a>';
}
// Add image overlay with hook
$slide_title = get_the_title();
$slide_content = get_the_content();
$img_overlay = '<div class="owl-carousel-item-imgoverlay">';
$img_overlay .= '<div class="owl-carousel-item-imgtitle">' . $slide_title . '</div>';
$img_overlay .= '<div class="owl-carousel-item-imgcontent">' . wpautop($slide_content) . '</div>';
$img_overlay .= '</div>';
$result .= apply_filters('owlcarousel_img_overlay', $img_overlay, $slide_title, $slide_content, $meta_link);
$result .= '</div>';
} else {
$result .= '<div class="owl-carousel-item-text">' . get_the_content() . '</div>';
}
$result .= '</div>';
}
$result .= '</div>';
/* Restore original Post Data */
wp_reset_postdata();
return $result;
}
/**
* Owl Carousel for Wordpress image gallery
* @param string $output Gallery output
* @param array $attr Parameters
* @return string Owl HTML code
*/
function owl_carousel_post_gallery($output, $attr) {
global $post;
if (isset($attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
if (!$attr['orderby'])
unset($attr['orderby']);
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ('RAND' == $order)
$orderby = 'none';
if (!empty($include)) {
$include = preg_replace('/[^0-9,]+/', '', $include);
$_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
$attachments = array();
foreach ($_attachments as $key => $val) {
$attachments[$val->ID] = $_attachments[$key];
}
}
if (empty($attachments))
return '';
// Add item number if not defined
if (!isset($attr['items'])) {
$attr['items'] = '1';
}
$data_attr = "";
foreach ($attr as $key => $value) {
if ($key != "category") {
$data_attr .= ' data-' . $key . '="' . $value . '" ';
}
}
$output .= '<div id="owl-carousel-' . rand() . '" class="owl-carousel" ' . $data_attr . '>';
foreach ($attachments as $id => $attachment) {
$img = wp_get_attachment_image_src($id, 'full');
$meta_link = get_post_meta($id, '_owlurl', true);
$title = $attachment->post_title;
$output .= "<div class=\"item\">";
if (!empty($meta_link)) {
$output .= "<a href=\"" . $meta_link . "\">";
}
$output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"$title\" />\n";
if (!empty($meta_link)) {
$output .= "</a>";
}
$output .= "</div>";
}
$output .= "</div>";
return $output;
}