1 <?php |
|
2 /** |
|
3 * Class SlideslowPlugin is called whenever a slideshow do_action tag is come across. |
|
4 * Responsible for outputting the slideshow's HTML, CSS and Javascript. |
|
5 * |
|
6 * TODO Create a variable in which all slideshow html can be stored <- Rethink this, slideshow containers have random ids. |
|
7 * @author: Stefan Boonstra |
|
8 * @version: 22-09-12 |
|
9 */ |
|
10 class SlideshowPlugin { |
|
11 |
|
12 /** |
|
13 * Function deploy prints out the prepared html |
|
14 * |
|
15 * @param int $postId |
|
16 */ |
|
17 static function deploy($postId = null){ |
|
18 echo self::prepare($postId); |
|
19 } |
|
20 |
|
21 /** |
|
22 * Function prepare returns the required html and enqueues |
|
23 * the scripts and stylesheets necessary for displaying the slideshow |
|
24 * |
|
25 * Passing this function no parameter or passing it a negative one will |
|
26 * result in a random pick of slideshow |
|
27 * |
|
28 * @param int $postId |
|
29 * @return String $output |
|
30 */ |
|
31 static function prepare($postId = null){ |
|
32 // Get post by its ID, or by its slug |
|
33 if(is_numeric($postId)) |
|
34 $post = wp_get_single_post($postId); |
|
35 if(is_string($postId)){ |
|
36 $query = new WP_Query(array( |
|
37 'post_type' => SlideshowPluginPostType::$postType, |
|
38 'name' => $postId, |
|
39 'orderby' => 'post_date', |
|
40 'order' => 'DESC' |
|
41 )); |
|
42 |
|
43 if($query->have_posts()) |
|
44 $post = $query->next_post(); |
|
45 } |
|
46 |
|
47 // When no slideshow is found, get one at random |
|
48 if(empty($post)){ |
|
49 $post = get_posts(array( |
|
50 'numberposts' => 1, |
|
51 'orderby' => 'rand', |
|
52 'post_type' => SlideshowPluginPostType::$postType |
|
53 )); |
|
54 |
|
55 if(is_array($post)) |
|
56 $post = $post[0]; |
|
57 } |
|
58 |
|
59 // Exit on error |
|
60 if(empty($post)){echo 'dayum'; |
|
61 return '<!-- Wordpress Slideshow - No slideshows available -->'; |
|
62 } |
|
63 |
|
64 // Get settings |
|
65 $allSettings = SlideshowPluginPostType::getSimpleSettings($post->ID, null, false); |
|
66 |
|
67 // Get stored slide settings and convert them to array([slide-key] => array([setting-name] => [value])); |
|
68 $slidesPreOrder = array(); |
|
69 $slideSettings = SlideshowPluginPostType::getSettings($post->ID, SlideshowPluginPostType::$prefixes['slide-list'], false); |
|
70 if(is_array($slideSettings) && count($slideSettings) > 0) |
|
71 foreach($slideSettings as $key => $value){ |
|
72 $key = explode('_', $key); |
|
73 if(is_numeric($key[1])) |
|
74 $slidesPreOrder[$key[1]][$key[2]] = $value; |
|
75 } |
|
76 |
|
77 // Create array ordered by the 'order' key of the slides array: array([order-key] => [slide-key]); |
|
78 $slidesOrder = array(); |
|
79 if(count($slidesPreOrder) > 0){ |
|
80 foreach($slidesPreOrder as $key => $value) |
|
81 if(isset($value['order']) && is_numeric($value['order']) && $value['order'] > 0) |
|
82 $slidesOrder[$value['order']][] = $key; |
|
83 } |
|
84 ksort($slidesOrder); |
|
85 |
|
86 // Order slides by the order key. |
|
87 $slides = array(); |
|
88 if(count($slidesOrder) > 0) |
|
89 foreach($slidesOrder as $value) |
|
90 if(is_array($value)) |
|
91 foreach($value as $slideId){ |
|
92 $slides[] = $slidesPreOrder[$slideId]; |
|
93 unset($slidesPreOrder[$slideId]); |
|
94 } |
|
95 |
|
96 // Add remaining (unordered) slides to the end of the array. |
|
97 $slides = array_merge($slides, $slidesPreOrder); |
|
98 |
|
99 // Randomize if setting is true. |
|
100 if(isset($allSettings['setting_random']) && $allSettings['setting_random'] == 'true') |
|
101 shuffle($slides); |
|
102 |
|
103 // Enqueue functional sheet |
|
104 wp_enqueue_style( |
|
105 'slideshow_functional_style', |
|
106 SlideshowPluginMain::getPluginUrl() . '/style/' . __CLASS__ . '/functional.css' |
|
107 ); |
|
108 |
|
109 // Create a microtime timestamp to host multiple slideshows with different styles and settings on the same page |
|
110 $randomId = rand(); |
|
111 |
|
112 // Get stylesheet for printing |
|
113 $style = ''; |
|
114 if($allSettings['style_style'] == 'custom' && isset($allSettings['style_custom']) && !empty($allSettings['style_custom'])){ // Custom style |
|
115 $style = str_replace('%plugin-url%', SlideshowPluginMain::getPluginUrl(), $allSettings['style_custom']); |
|
116 }else{ // Set style |
|
117 $filePath = SlideshowPluginMain::getPluginPath() . '/style/' . __CLASS__ . '/style-' . $allSettings['style_style'] . '.css'; |
|
118 if(file_exists(SlideshowPluginMain::getPluginPath() . '/style/' . __CLASS__ . '/style-' . $allSettings['style_style'] . '.css')){ |
|
119 ob_start(); |
|
120 include($filePath); |
|
121 $style = str_replace('%plugin-url%', SlideshowPluginMain::getPluginUrl(), ob_get_clean()); |
|
122 } |
|
123 } |
|
124 |
|
125 // Append the random ID to the slideshow container in the stylesheet, to identify multiple slideshows |
|
126 if(!empty($style)) |
|
127 $style = str_replace('.slideshow_container', '.slideshow_container_' . $randomId, $style); |
|
128 |
|
129 // Filter settings to only contain settings, then remove prefix |
|
130 $settings = array(); |
|
131 if(is_array($allSettings) && count($allSettings) > 0) |
|
132 foreach($allSettings as $key => $value) |
|
133 if(SlideshowPluginPostType::$prefixes['settings'] == substr($key, 0, strlen(SlideshowPluginPostType::$prefixes['settings']))) |
|
134 $settings[substr($key, strlen(SlideshowPluginPostType::$prefixes['settings']))] = $value; |
|
135 |
|
136 // Include output file that stores output in $output. |
|
137 $output = ''; |
|
138 ob_start(); |
|
139 include(SlideshowPluginMain::getPluginPath() . '/views/' . __CLASS__ . '/slideshow.php'); |
|
140 $output .= ob_get_clean(); |
|
141 |
|
142 // Enqueue flash object creation script |
|
143 wp_enqueue_script( |
|
144 'swfobject', |
|
145 SlideshowPluginMain::getPluginUrl() . '/js/' . __CLASS__ . 'swfobject.js' |
|
146 ); |
|
147 |
|
148 // Enqueue slideshow script |
|
149 wp_enqueue_script( |
|
150 'slideshow_script', |
|
151 SlideshowPluginMain::getPluginUrl() . '/js/' . __CLASS__ . '/slideshow.js', |
|
152 array('jquery') |
|
153 ); |
|
154 |
|
155 // Return output |
|
156 return $output; |
|
157 } |
|
158 } |
|