wp_cycle(); template tag, which will generate all the necessary HTML for outputting the rotating images.
Version: 0.1.12
Author: Nathan Rice
Author URI: http://www.nathanrice.net/
This plugin inherits the GPL license from it's parent system, WordPress.
*/
/*
///////////////////////////////////////////////
This section defines the variables that
will be used throughout the plugin
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
// define our defaults (filterable)
$wp_cycle_defaults = apply_filters('wp_cycle_defaults', array(
'rotate' => 1,
'effect' => 'fade',
'delay' => 3,
'duration' => 1,
'img_width' => 300,
'img_height' => 200,
'div' => 'rotator'
));
// pull the settings from the db
$wp_cycle_settings = get_option('wp_cycle_settings');
$wp_cycle_images = get_option('wp_cycle_images');
// fallback
$wp_cycle_settings = wp_parse_args($wp_cycle_settings, $wp_cycle_defaults);
/*
///////////////////////////////////////////////
This section hooks the proper functions
to the proper actions in WordPress
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
// this function registers our settings in the db
add_action('admin_init', 'wp_cycle_register_settings');
function wp_cycle_register_settings() {
register_setting('wp_cycle_images', 'wp_cycle_images', 'wp_cycle_images_validate');
register_setting('wp_cycle_settings', 'wp_cycle_settings', 'wp_cycle_settings_validate');
}
// this function adds the settings page to the Appearance tab
add_action('admin_menu', 'add_wp_cycle_menu');
function add_wp_cycle_menu() {
add_submenu_page('upload.php', 'WP-Cycle Settings', 'WP-Cycle', 'upload_files', 'wp-cycle', 'wp_cycle_admin_page');
}
// add "Settings" link to plugin page
add_filter('plugin_action_links_' . plugin_basename(__FILE__) , 'wp_cycle_plugin_action_links');
function wp_cycle_plugin_action_links($links) {
$wp_cycle_settings_link = sprintf( '%s', admin_url( 'upload.php?page=wp-cycle' ), __('Settings') );
array_unshift($links, $wp_cycle_settings_link);
return $links;
}
/*
///////////////////////////////////////////////
this function is the code that gets loaded when the
settings page gets loaded by the browser. It calls
functions that handle image uploads and image settings
changes, as well as producing the visible page output.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
function wp_cycle_admin_page() {
echo '
';
// handle image upload, if necessary
if($_REQUEST['action'] == 'wp_handle_upload')
wp_cycle_handle_upload();
// delete an image, if necessary
if(isset($_REQUEST['delete']))
wp_cycle_delete_upload($_REQUEST['delete']);
// the image management form
wp_cycle_images_admin();
// the settings management form
wp_cycle_settings_admin();
echo '
';
}
/*
///////////////////////////////////////////////
this section handles uploading images, adding
the image data to the database, deleting images,
and deleting image data from the database.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
// this function handles the file upload,
// resize/crop, and adds the image data to the db
function wp_cycle_handle_upload() {
global $wp_cycle_settings, $wp_cycle_images;
// upload the image
$upload = wp_handle_upload($_FILES['wp_cycle'], 0);
// extract the $upload array
extract($upload);
// the URL of the directory the file was loaded in
$upload_dir_url = str_replace(basename($file), '', $url);
// get the image dimensions
list($width, $height) = getimagesize($file);
// if the uploaded file is NOT an image
if(strpos($type, 'image') === FALSE) {
unlink($file); // delete the file
echo 'Sorry, but the file you uploaded does not seem to be a valid image. Please try again.
';
return;
}
// if the image doesn't meet the minimum width/height requirements ...
if($width < $wp_cycle_settings['img_width'] || $height < $wp_cycle_settings['img_height']) {
unlink($file); // delete the image
echo 'Sorry, but this image does not meet the minimum height/width requirements. Please upload another image
';
return;
}
// if the image is larger than the width/height requirements, then scale it down.
if($width > $wp_cycle_settings['img_width'] || $height > $wp_cycle_settings['img_height']) {
// resize the image
$resized = image_resize($file, $wp_cycle_settings['img_width'], $wp_cycle_settings['img_height'], true, 'resized');
$resized_url = $upload_dir_url . basename($resized);
// delete the original
unlink($file);
$file = $resized;
$url = $resized_url;
}
// make the thumbnail
$thumb_height = round((100 * $wp_cycle_settings['img_height']) / $wp_cycle_settings['img_width']);
if(isset($upload['file'])) {
$thumbnail = image_resize($file, 100, $thumb_height, true, 'thumb');
$thumbnail_url = $upload_dir_url . basename($thumbnail);
}
// use the timestamp as the array key and id
$time = date('YmdHis');
// add the image data to the array
$wp_cycle_images[$time] = array(
'id' => $time,
'file' => $file,
'file_url' => $url,
'thumbnail' => $thumbnail,
'thumbnail_url' => $thumbnail_url,
'image_links_to' => '',
'alt_text' => ''
);
// add the image information to the database
$wp_cycle_images['update'] = 'Added';
update_option('wp_cycle_images', $wp_cycle_images);
}
// this function deletes the image,
// and removes the image data from the db
function wp_cycle_delete_upload($id) {
global $wp_cycle_images;
// if the ID passed to this function is invalid,
// halt the process, and don't try to delete.
if(!isset($wp_cycle_images[$id])) return;
// delete the image and thumbnail
unlink($wp_cycle_images[$id]['file']);
unlink($wp_cycle_images[$id]['thumbnail']);
// indicate that the image was deleted
$wp_cycle_images['update'] = 'Deleted';
// remove the image data from the db
unset($wp_cycle_images[$id]);
update_option('wp_cycle_images', $wp_cycle_images);
}
/*
///////////////////////////////////////////////
these two functions check to see if an update
to the data just occurred. if it did, then they
will display a notice, and reset the update option.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
// this function checks to see if we just updated the settings
// if so, it displays the "updated" message.
function wp_cycle_settings_update_check() {
global $wp_cycle_settings;
if(isset($wp_cycle_settings['update'])) {
echo 'WP-Cycle Settings '.$wp_cycle_settings['update'].'
';
unset($wp_cycle_settings['update']);
update_option('wp_cycle_settings', $wp_cycle_settings);
}
}
// this function checks to see if we just added a new image
// if so, it displays the "updated" message.
function wp_cycle_images_update_check() {
global $wp_cycle_images;
if($wp_cycle_images['update'] == 'Added' || $wp_cycle_images['update'] == 'Deleted' || $wp_cycle_images['update'] == 'Updated') {
echo 'Image(s) '.$wp_cycle_images['update'].' Successfully
';
unset($wp_cycle_images['update']);
update_option('wp_cycle_images', $wp_cycle_images);
}
}
/*
///////////////////////////////////////////////
these two functions display the front-end code
on the admin page. it's mostly form markup.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
// display the images administration code
function wp_cycle_images_admin() { ?>
Image |
Image Links To |
Alt Text |
Actions |
Image |
Image Links To |
Alt Text |
Actions |
$value) {
if($key != 'update') {
$input[$key]['file_url'] = clean_url($value['file_url']);
$input[$key]['thumbnail_url'] = clean_url($value['thumbnail_url']);
if($value['image_links_to'])
$input[$key]['image_links_to'] = clean_url($value['image_links_to']);
$input[$key]['alt_text'] = esc_textarea($value['alt_text']);
}
}
return $input;
}
/*
///////////////////////////////////////////////
this final section generates all the code that
is displayed on the front-end of the WP Theme
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
function wp_cycle($args = array(), $content = null) {
global $wp_cycle_settings, $wp_cycle_images;
// possible future use
$args = wp_parse_args($args, $wp_cycle_settings);
$newline = "\n"; // line break
echo ''.$newline;
}
// create the shortcode [wp_cycle]
add_shortcode('wp_cycle', 'wp_cycle_shortcode');
function wp_cycle_shortcode($atts) {
// Temp solution, output buffer the echo function.
ob_start();
wp_cycle();
$output = ob_get_clean();
return $output;
}
add_action('wp_print_scripts', 'wp_cycle_scripts');
function wp_cycle_scripts() {
if(!is_admin())
wp_enqueue_script('cycle', WP_CONTENT_URL.'/plugins/wp-cycle/jquery.cycle.all.min.js', array('jquery'), '', true);
}
add_action('wp_footer', 'wp_cycle_args', 15);
function wp_cycle_args() {
global $wp_cycle_settings; ?>