PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-cycle/wp-cycle.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 470 lines | 289 code | 76 blank | 105 comment | 27 complexity | b3a95e666ed5866f05849234152e7345 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: WP-Cycle
  4. Plugin URI: http://www.nathanrice.net/plugins/wp-cycle/
  5. Description: This plugin creates an image slideshow from the images you upload using the jQuery Cycle plugin. You can upload/delete images via the administration panel, and display the images in your theme by using the <code>wp_cycle();</code> template tag, which will generate all the necessary HTML for outputting the rotating images.
  6. Version: 0.1.8
  7. Author: Nathan Rice
  8. Author URI: http://www.nathanrice.net/
  9. This plugin inherits the GPL license from it's parent system, WordPress.
  10. */
  11. /*
  12. ///////////////////////////////////////////////
  13. This section defines the variables that
  14. will be used throughout the plugin
  15. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  16. */
  17. // define our defaults (filterable)
  18. $wp_cycle_defaults = apply_filters('wp_cycle_defaults', array(
  19. 'rotate' => 1,
  20. 'effect' => 'fade',
  21. 'delay' => 3,
  22. 'duration' => 1,
  23. 'img_width' => 300,
  24. 'img_height' => 200,
  25. 'div' => 'rotator'
  26. ));
  27. // pull the settings from the db
  28. $wp_cycle_settings = get_option('wp_cycle_settings');
  29. $wp_cycle_images = get_option('wp_cycle_images');
  30. // fallback
  31. $wp_cycle_settings = wp_parse_args($wp_cycle_settings, $wp_cycle_defaults);
  32. /*
  33. ///////////////////////////////////////////////
  34. This section hooks the proper functions
  35. to the proper actions in WordPress
  36. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  37. */
  38. // this function registers our settings in the db
  39. add_action('admin_init', 'wp_cycle_register_settings');
  40. function wp_cycle_register_settings() {
  41. register_setting('wp_cycle_images', 'wp_cycle_images', 'wp_cycle_images_validate');
  42. register_setting('wp_cycle_settings', 'wp_cycle_settings', 'wp_cycle_settings_validate');
  43. }
  44. // this function adds the settings page to the Appearance tab
  45. add_action('admin_menu', 'add_wp_cycle_menu');
  46. function add_wp_cycle_menu() {
  47. add_submenu_page('plugins.php', 'WP-Cycle Settings', 'WP-Cycle', 8, 'wp-cycle', 'wp_cycle_admin_page');
  48. }
  49. /*
  50. ///////////////////////////////////////////////
  51. this function is the code that gets loaded when the
  52. settings page gets loaded by the browser. It calls
  53. functions that handle image uploads and image settings
  54. changes, as well as producing the visible page output.
  55. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  56. */
  57. function wp_cycle_admin_page() {
  58. echo '<div class="wrap">';
  59. // handle image upload, if necessary
  60. if($_REQUEST['action'] == 'wp_handle_upload')
  61. wp_cycle_handle_upload();
  62. // delete an image, if necessary
  63. if(isset($_REQUEST['delete']))
  64. wp_cycle_delete_upload($_REQUEST['delete']);
  65. // the image management form
  66. wp_cycle_images_admin();
  67. // the settings management form
  68. wp_cycle_settings_admin();
  69. echo '</div>';
  70. }
  71. /*
  72. ///////////////////////////////////////////////
  73. this section handles uploading images, adding
  74. the image data to the database, deleting images,
  75. and deleting image data from the database.
  76. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  77. */
  78. // this function handles the file upload,
  79. // resize/crop, and adds the image data to the db
  80. function wp_cycle_handle_upload() {
  81. global $wp_cycle_settings, $wp_cycle_images;
  82. // upload the image
  83. $upload = wp_handle_upload($_FILES['wp_cycle'], 0);
  84. // extract the $upload array
  85. extract($upload);
  86. // the URL of the directory the file was loaded in
  87. $upload_dir_url = str_replace(basename($file), '', $url);
  88. // get the image dimensions
  89. list($width, $height) = getimagesize($file);
  90. // if the uploaded file is NOT an image
  91. if(strpos($type, 'image') === FALSE) {
  92. unlink($file); // delete the file
  93. echo '<div class="error" id="message"><p>Sorry, but the file you uploaded does not seem to be a valid image. Please try again.</p></div>';
  94. return;
  95. }
  96. // if the image doesn't meet the minimum width/height requirements ...
  97. if($width < $wp_cycle_settings['img_width'] || $height < $wp_cycle_settings['img_height']) {
  98. unlink($file); // delete the image
  99. echo '<div class="error" id="message"><p>Sorry, but this image does not meet the minimum height/width requirements. Please upload another image</p></div>';
  100. return;
  101. }
  102. // if the image is larger than the width/height requirements, then scale it down.
  103. if($width > $wp_cycle_settings['img_width'] || $height > $wp_cycle_settings['img_height']) {
  104. // resize the image
  105. $resized = image_resize($file, $wp_cycle_settings['img_width'], $wp_cycle_settings['img_height'], true, 'resized');
  106. $resized_url = $upload_dir_url . basename($resized);
  107. // delete the original
  108. unlink($file);
  109. $file = $resized;
  110. $url = $resized_url;
  111. }
  112. // make the thumbnail
  113. $thumb_height = round((100 * $wp_cycle_settings['img_height']) / $wp_cycle_settings['img_width']);
  114. if(isset($upload['file'])) {
  115. $thumbnail = image_resize($file, 100, $thumb_height, true, 'thumb');
  116. $thumbnail_url = $upload_dir_url . basename($thumbnail);
  117. }
  118. // use the timestamp as the array key and id
  119. $time = date('YmdHis');
  120. // add the image data to the array
  121. $wp_cycle_images[$time] = array(
  122. 'id' => $time,
  123. 'file' => $file,
  124. 'file_url' => $url,
  125. 'thumbnail' => $thumbnail,
  126. 'thumbnail_url' => $thumbnail_url,
  127. 'image_links_to' => ''
  128. );
  129. // add the image information to the database
  130. $wp_cycle_images['update'] = 'Added';
  131. update_option('wp_cycle_images', $wp_cycle_images);
  132. }
  133. // this function deletes the image,
  134. // and removes the image data from the db
  135. function wp_cycle_delete_upload($id) {
  136. global $wp_cycle_images;
  137. // if the ID passed to this function is invalid,
  138. // halt the process, and don't try to delete.
  139. if(!isset($wp_cycle_images[$id])) return;
  140. // delete the image and thumbnail
  141. unlink($wp_cycle_images[$id]['file']);
  142. unlink($wp_cycle_images[$id]['thumbnail']);
  143. // indicate that the image was deleted
  144. $wp_cycle_images['update'] = 'Deleted';
  145. // remove the image data from the db
  146. unset($wp_cycle_images[$id]);
  147. update_option('wp_cycle_images', $wp_cycle_images);
  148. }
  149. /*
  150. ///////////////////////////////////////////////
  151. these two functions check to see if an update
  152. to the data just occurred. if it did, then they
  153. will display a notice, and reset the update option.
  154. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  155. */
  156. // this function checks to see if we just updated the settings
  157. // if so, it displays the "updated" message.
  158. function wp_cycle_settings_update_check() {
  159. global $wp_cycle_settings;
  160. if(isset($wp_cycle_settings['update'])) {
  161. echo '<div class="updated fade" id="message"><p>WP-Cycle Settings <strong>'.$wp_cycle_settings['update'].'</strong></p></div>';
  162. unset($wp_cycle_settings['update']);
  163. update_option('wp_cycle_settings', $wp_cycle_settings);
  164. }
  165. }
  166. // this function checks to see if we just added a new image
  167. // if so, it displays the "updated" message.
  168. function wp_cycle_images_update_check() {
  169. global $wp_cycle_images;
  170. if($wp_cycle_images['update'] == 'Added' || $wp_cycle_images['update'] == 'Deleted' || $wp_cycle_images['update'] == 'Updated') {
  171. echo '<div class="updated fade" id="message"><p>Image(s) '.$wp_cycle_images['update'].' Successfully</p></div>';
  172. unset($wp_cycle_images['update']);
  173. update_option('wp_cycle_images', $wp_cycle_images);
  174. }
  175. }
  176. /*
  177. ///////////////////////////////////////////////
  178. these two functions display the front-end code
  179. on the admin page. it's mostly form markup.
  180. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  181. */
  182. // display the images administration code
  183. function wp_cycle_images_admin() { ?>
  184. <?php global $wp_cycle_images; ?>
  185. <?php wp_cycle_images_update_check(); ?>
  186. <h2><?php _e('WP-Cycle Images', 'wp_cycle'); ?></h2>
  187. <table class="form-table">
  188. <tr valign="top"><th scope="row">Upload New Image</th>
  189. <td>
  190. <form enctype="multipart/form-data" method="post" action="?page=wp-cycle">
  191. <input type="hidden" name="post_id" id="post_id" value="0" />
  192. <input type="hidden" name="action" id="action" value="wp_handle_upload" />
  193. <label for="wp_cycle">Select a File: </label>
  194. <input type="file" name="wp_cycle" id="wp_cycle" />
  195. <input type="submit" class="button-primary" name="html-upload" value="Upload" />
  196. </form>
  197. </td>
  198. </tr>
  199. </table><br />
  200. <?php if(!empty($wp_cycle_images)) : ?>
  201. <table class="widefat fixed" cellspacing="0">
  202. <thead>
  203. <tr>
  204. <th scope="col" class="column-slug">Image</th>
  205. <th scope="col">Image Links To</th>
  206. <th scope="col" class="column-slug">Actions</th>
  207. </tr>
  208. </thead>
  209. <tfoot>
  210. <tr>
  211. <th scope="col" class="column-slug">Image</th>
  212. <th scope="col">Image Links To</th>
  213. <th scope="col" class="column-slug">Actions</th>
  214. </tr>
  215. </tfoot>
  216. <tbody>
  217. <form method="post" action="options.php">
  218. <?php settings_fields('wp_cycle_images'); ?>
  219. <?php foreach((array)$wp_cycle_images as $image => $data) : ?>
  220. <tr>
  221. <input type="hidden" name="wp_cycle_images[<?php echo $image; ?>][id]" value="<?php echo $data['id']; ?>" />
  222. <input type="hidden" name="wp_cycle_images[<?php echo $image; ?>][file]" value="<?php echo $data['file']; ?>" />
  223. <input type="hidden" name="wp_cycle_images[<?php echo $image; ?>][file_url]" value="<?php echo $data['file_url']; ?>" />
  224. <input type="hidden" name="wp_cycle_images[<?php echo $image; ?>][thumbnail]" value="<?php echo $data['thumbnail']; ?>" />
  225. <input type="hidden" name="wp_cycle_images[<?php echo $image; ?>][thumbnail_url]" value="<?php echo $data['thumbnail_url']; ?>" />
  226. <th scope="row" class="column-slug"><img src="<?php echo $data['thumbnail_url']; ?>" /></th>
  227. <td><input type="text" name="wp_cycle_images[<?php echo $image; ?>][image_links_to]" value="<?php echo $data['image_links_to']; ?>" size="35" /></td>
  228. <td class="column-slug"><input type="submit" class="button-primary" value="Update" /> <a href="?page=wp-cycle&amp;delete=<?php echo $image; ?>" class="button">Delete</a></td>
  229. </tr>
  230. <?php endforeach; ?>
  231. <input type="hidden" name="wp_cycle_images[update]" value="Updated" />
  232. </form>
  233. </tbody>
  234. </table>
  235. <?php endif; ?>
  236. <?php
  237. }
  238. // display the settings administration code
  239. function wp_cycle_settings_admin() { ?>
  240. <?php wp_cycle_settings_update_check(); ?>
  241. <h2><?php _e('WP-Cycle Settings', 'wp-cycle'); ?></h2>
  242. <form method="post" action="options.php">
  243. <?php settings_fields('wp_cycle_settings'); ?>
  244. <?php global $wp_cycle_settings; $options = $wp_cycle_settings; ?>
  245. <table class="form-table">
  246. <tr valign="top"><th scope="row">Transition Enabled</th>
  247. <td><input name="wp_cycle_settings[rotate]" type="checkbox" value="1" <?php checked('1', $options['rotate']); ?> /> <label for="wp_cycle_settings[rotate]">Check this box if you want to enable the transition effects</td>
  248. </tr>
  249. <tr><th scope="row">Transition Effect</th>
  250. <td>Please select the effect you would like to use when your images rotate (if applicable):<br />
  251. <select name="wp_cycle_settings[effect]">
  252. <option value="fade" <?php selected('fade', $options['effect']); ?>>fade</option>
  253. <option value="wipe" <?php selected('wipe', $options['effect']); ?>>wipe</option>
  254. <option value="scrollUp" <?php selected('scrollUp', $options['effect']); ?>>scrollUp</option>
  255. <option value="scrollDown" <?php selected('scrollDown', $options['effect']); ?>>scrollDown</option>
  256. <option value="scrollLeft" <?php selected('scrollLeft', $options['effect']); ?>>scrollLeft</option>
  257. <option value="scrollRight" <?php selected('scrollRight', $options['effect']); ?>>scrollRight</option>
  258. <option value="cover" <?php selected('cover', $options['effect']); ?>>cover</option>
  259. <option value="shuffle" <?php selected('shuffle', $options['effect']); ?>>shuffle</option>
  260. </select>
  261. </td></tr>
  262. <tr><th scope="row">Transition Delay</th>
  263. <td>Length of time (in seconds) you would like each image to be visible:<br />
  264. <input type="text" name="wp_cycle_settings[delay]" value="<?php echo $options['delay'] ?>" size="4" />
  265. <label for="wp_cycle_settings[delay]">second(s)</label>
  266. </td></tr>
  267. <tr><th scope="row">Transition Length</th>
  268. <td>Length of time (in seconds) you would like the transition length to be:<br />
  269. <input type="text" name="wp_cycle_settings[duration]" value="<?php echo $options['duration'] ?>" size="4" />
  270. <label for="wp_cycle_settings[duration]">second(s)</label>
  271. </td></tr>
  272. <tr><th scope="row">Image Dimensions</th>
  273. <td>Please input the width of the image rotator:<br />
  274. <input type="text" name="wp_cycle_settings[img_width]" value="<?php echo $options['img_width'] ?>" size="4" />
  275. <label for="wp_cycle_settings[img_width]">px</label>
  276. <br /><br />
  277. Please input the height of the image rotator:<br />
  278. <input type="text" name="wp_cycle_settings[img_height]" value="<?php echo $options['img_height'] ?>" size="4" />
  279. <label for="wp_cycle_settings[img_height]">px</label>
  280. </td></tr>
  281. <tr><th scope="row">Rotator DIV ID</th>
  282. <td>Please indicate what you would like the rotator DIV ID to be:<br />
  283. <input type="text" name="wp_cycle_settings[div]" value="<?php echo $options['div'] ?>" />
  284. </td></tr>
  285. <input type="hidden" name="wp_cycle_settings[update]" value="UPDATED" />
  286. </table>
  287. <p class="submit">
  288. <input type="submit" class="button-primary" value="<?php _e('Save Settings') ?>" />
  289. </form>
  290. <!-- The Reset Optiom -->
  291. <form method="post" action="options.php">
  292. <?php settings_fields('wp_cycle_settings'); ?>
  293. <?php global $wp_cycle_defaults; // use the defaults ?>
  294. <?php foreach((array)$wp_cycle_defaults as $key => $value) : ?>
  295. <input type="hidden" name="wp_cycle_settings[<?php echo $key; ?>]" value="<?php echo $value; ?>" />
  296. <?php endforeach; ?>
  297. <input type="hidden" name="wp_cycle_settings[update]" value="RESET" />
  298. <input type="submit" class="button" value="<?php _e('Reset Settings') ?>" />
  299. </form>
  300. <!-- End Reset Option -->
  301. </p>
  302. <?php
  303. }
  304. /*
  305. ///////////////////////////////////////////////
  306. these two functions sanitize the data before it
  307. gets stored in the database via options.php
  308. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  309. */
  310. // this function sanitizes our settings data for storage
  311. function wp_cycle_settings_validate($input) {
  312. $input['rotate'] = ($input['rotate'] == 1 ? 1 : 0);
  313. $input['effect'] = wp_filter_nohtml_kses($input['effect']);
  314. $input['img_width'] = intval($input['img_width']);
  315. $input['img_height'] = intval($input['img_height']);
  316. $input['div'] = wp_filter_nohtml_kses($input['div']);
  317. return $input;
  318. }
  319. // this function sanitizes our image data for storage
  320. function wp_cycle_images_validate($input) {
  321. foreach((array)$input as $key => $value) {
  322. if($key != 'update') {
  323. $input[$key]['file_url'] = clean_url($value['file_url']);
  324. $input[$key]['thumbnail_url'] = clean_url($value['thumbnail_url']);
  325. if($value['image_links_to'])
  326. $input[$key]['image_links_to'] = clean_url($value['image_links_to']);
  327. }
  328. }
  329. return $input;
  330. }
  331. /*
  332. ///////////////////////////////////////////////
  333. this final section generates all the code that
  334. is displayed on the front-end of the WP Theme
  335. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  336. */
  337. function wp_cycle($args = array(), $content = null) {
  338. global $wp_cycle_settings, $wp_cycle_images;
  339. // possible future use
  340. $args = wp_parse_args($args, $wp_cycle_settings);
  341. $newline = "\n"; // line break
  342. echo '<div id="'.$wp_cycle_settings['div'].'">'.$newline;
  343. foreach((array)$wp_cycle_images as $image => $data) {
  344. if($data['image_links_to'])
  345. echo '<a href="'.$data['image_links_to'].'">';
  346. echo '<img src="'.$data['file_url'].'" width="'.$wp_cycle_settings['img_width'].'" height="'.$wp_cycle_settings['img_height'].'" class="'.$data['id'].'" alt="" />';
  347. if($data['image_links_to'])
  348. echo '</a>';
  349. echo $newline;
  350. }
  351. echo '</div>'.$newline;
  352. }
  353. // create the shortcode [wp_cycle]
  354. add_shortcode('wp_cycle', 'wp_cycle_shortcode');
  355. function wp_cycle_shortcode($atts) {
  356. // Temp solution, output buffer the echo function.
  357. ob_start();
  358. wp_cycle();
  359. $output = ob_get_clean();
  360. return $output;
  361. }
  362. add_action('wp_print_scripts', 'wp_cycle_scripts');
  363. function wp_cycle_scripts() {
  364. if(!is_admin())
  365. wp_enqueue_script('cycle', $src = WP_CONTENT_URL.'/plugins/wp-cycle/jquery.cycle.all.min.js', $deps = array('jquery'));
  366. }
  367. add_action('wp_head', 'wp_cycle_head');
  368. function wp_cycle_head() {
  369. global $wp_cycle_settings; ?>
  370. <?php if($wp_cycle_settings['rotate']) : ?>
  371. <script type="text/javascript">
  372. jQuery(document).ready(function($) {
  373. $("#<?php echo $wp_cycle_settings['div']; ?>").cycle({
  374. fx: '<?php echo $wp_cycle_settings['effect']; ?>',
  375. timeout: <?php echo ($wp_cycle_settings['delay'] * 1000); ?>,
  376. speed: <?php echo ($wp_cycle_settings['duration'] * 1000); ?>,
  377. pause: 1,
  378. fit: 1
  379. });
  380. });
  381. </script>
  382. <?php endif; ?>
  383. <style type="text/css" media="screen">
  384. #<?php echo $wp_cycle_settings['div']; ?> {
  385. position: relative;
  386. width: <?php echo $wp_cycle_settings['img_width']; ?>px;
  387. height: <?php echo $wp_cycle_settings['img_height']?>px;
  388. margin: 0; padding: 0;
  389. overflow: hidden;
  390. }
  391. </style>
  392. <?php }
  393. ?>