| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class SlideshowSEPlugin is called whenever a slideshow do_action tag is come across. |
| 4 |
* Responsible for outputting the slideshow's HTML, CSS and Javascript. |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* @author: Stefan Boonstra |
| 8 |
*/ |
| 9 |
class SlideshowSEPlugin |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Function deploy prints out the prepared html |
| 13 |
* |
| 14 |
* @since 1.2.0 |
| 15 |
* @param int $postId |
| 16 |
*/ |
| 17 |
static function deploy($postId = null) |
| 18 |
{ |
| 19 |
echo self::prepare($postId); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Function prepare returns the required html and enqueues |
| 24 |
* the scripts and stylesheets necessary for displaying the slideshow |
| 25 |
* |
| 26 |
* Passing this function no parameter or passing it a negative one will |
| 27 |
* result in a random pick of slideshow |
| 28 |
* |
| 29 |
* @since 2.1.0 |
| 30 |
* @param int $postId |
| 31 |
* @return String $output |
| 32 |
*/ |
| 33 |
static function prepare($postId = null) |
| 34 |
{ |
| 35 |
$post = null; |
| 36 |
|
| 37 |
// Get post by its ID, if the ID is not a negative value |
| 38 |
if (is_numeric($postId) && |
| 39 |
$postId >= 0) |
| 40 |
{ |
| 41 |
$post = get_post($postId); |
| 42 |
} |
| 43 |
|
| 44 |
// Get slideshow by slug when it's a non-empty string |
| 45 |
if ($post === null && |
| 46 |
is_string($postId) && |
| 47 |
!is_numeric($postId) && |
| 48 |
!empty($postId)) |
| 49 |
{ |
| 50 |
$query = new WP_Query(array( |
| 51 |
'post_type' => SlideshowSEPluginPostType::$postType, |
| 52 |
'name' => $postId, |
| 53 |
'orderby' => 'post_date', |
| 54 |
'order' => 'DESC', |
| 55 |
'suppress_filters' => true |
| 56 |
)); |
| 57 |
|
| 58 |
if($query->have_posts()) |
| 59 |
{ |
| 60 |
$post = $query->next_post(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// When no slideshow is found, get one at random |
| 65 |
if ($post === null) |
| 66 |
{ |
| 67 |
$post = get_posts(array( |
| 68 |
'numberposts' => 1, |
| 69 |
'offset' => 0, |
| 70 |
'orderby' => 'rand', |
| 71 |
'post_type' => SlideshowSEPluginPostType::$postType, |
| 72 |
'suppress_filters' => true |
| 73 |
)); |
| 74 |
|
| 75 |
if(is_array($post)) |
| 76 |
{ |
| 77 |
$post = $post[0]; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// Exit on error |
| 82 |
if($post === null) |
| 83 |
{ |
| 84 |
return '<!-- WordPress Slideshow - No slideshows available -->'; |
| 85 |
} |
| 86 |
|
| 87 |
// Log slideshow's problems to be able to track them on the page. |
| 88 |
$log = array(); |
| 89 |
|
| 90 |
// Get slides |
| 91 |
$slides = SlideshowSEPluginSlideshowSettingsHandler::getSlides($post->ID); |
| 92 |
|
| 93 |
if (!is_array($slides) || |
| 94 |
count($slides) <= 0) |
| 95 |
{ |
| 96 |
$log[] = 'No slides were found'; |
| 97 |
} |
| 98 |
|
| 99 |
// Get settings |
| 100 |
$settings = SlideshowSEPluginSlideshowSettingsHandler::getSettings($post->ID); |
| 101 |
$styleSettings = SlideshowSEPluginSlideshowSettingsHandler::getStyleSettings($post->ID); |
| 102 |
|
| 103 |
// Only enqueue the functional stylesheet when the 'allStylesheetsRegistered' flag is false |
| 104 |
if (!SlideshowSEPluginSlideshowStylesheet::$allStylesheetsRegistered) |
| 105 |
{ |
| 106 |
wp_enqueue_style( |
| 107 |
'slideshow-jquery-image-gallery-stylesheet_functional', |
| 108 |
SlideshowSEPluginMain::getPluginUrl() . '/style/' . __CLASS__ . '/functional.css', |
| 109 |
array(), |
| 110 |
SlideshowSEPluginMain::$version |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
// Check if requested style is available. If not, use the default |
| 115 |
list($styleName, $styleVersion) = SlideshowSEPluginSlideshowStylesheet::enqueueStylesheet($styleSettings['style']); |
| 116 |
|
| 117 |
$data = new stdClass(); |
| 118 |
$data->log = $log; |
| 119 |
$data->post = $post; |
| 120 |
$data->slides = $slides; |
| 121 |
$data->settings = $settings; |
| 122 |
$data->styleName = $styleName; |
| 123 |
$data->styleVersion = $styleVersion; |
| 124 |
|
| 125 |
// Include output file to store output in $output. |
| 126 |
$output = SlideshowSEPluginMain::getView(__CLASS__ . DIRECTORY_SEPARATOR . 'slideshow.php', $data); |
| 127 |
|
| 128 |
// Enqueue slideshow script |
| 129 |
wp_enqueue_script( |
| 130 |
'slideshow-jquery-image-gallery-script', |
| 131 |
SlideshowSEPluginMain::getPluginUrl() . '/js/min/all.frontend.min.js', |
| 132 |
array('jquery'), |
| 133 |
SlideshowSEPluginMain::$version |
| 134 |
); |
| 135 |
|
| 136 |
// Set dimensionWidth and dimensionHeight if dimensions should be preserved |
| 137 |
if (isset($settings['preserveSlideshowDimensions']) && |
| 138 |
$settings['preserveSlideshowDimensions'] == 'true') |
| 139 |
{ |
| 140 |
$aspectRatio = explode(':', $settings['aspectRatio']); |
| 141 |
|
| 142 |
// Width |
| 143 |
if (isset($aspectRatio[0]) && |
| 144 |
is_numeric($aspectRatio[0])) |
| 145 |
{ |
| 146 |
$settings['dimensionWidth'] = $aspectRatio[0]; |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
$settings['dimensionWidth'] = 1; |
| 151 |
} |
| 152 |
|
| 153 |
// Height |
| 154 |
if (isset($aspectRatio[1]) && |
| 155 |
is_numeric($aspectRatio[1])) |
| 156 |
{ |
| 157 |
$settings['dimensionHeight'] = $aspectRatio[1]; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
$settings['dimensionHeight'] = 1; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (!SlideshowSEPluginGeneralSettings::getEnableLazyLoading()) |
| 166 |
{ |
| 167 |
// Include slideshow settings by localizing them |
| 168 |
wp_localize_script( |
| 169 |
'slideshow-jquery-image-gallery-script', |
| 170 |
'SlideshowSEPluginSettings_' . $post->ID, |
| 171 |
$settings |
| 172 |
); |
| 173 |
|
| 174 |
// Include the location of the admin-ajax.php file |
| 175 |
wp_localize_script( |
| 176 |
'slideshow-jquery-image-gallery-script', |
| 177 |
'slideshow_jquery_image_gallery_script_adminURL', |
| 178 |
admin_url() |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
// Return output |
| 183 |
return $output; |
| 184 |
} |
| 185 |
} |