Created
September 7, 2012 22:27
-
-
Save wpsmith/3670279 to your computer and use it in GitHub Desktop.
Soliloquy: Make Images Have Start and End Dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Soliloquy Start/Expiration Addon | |
| Plugin URI: http://wpsmith.net/ | |
| Description: Soliloquy is the best responsive jQuery slider plugin for WordPress. This makes it better by adding start/expiration dates. | |
| Author: Travis Smith | |
| Author URI: http://wpsmith.net/ | |
| Version: 1.0.0 | |
| License: GNU General Public License v2.0 or later | |
| License URI: http://www.opensource.org/licenses/gpl-license.php | |
| */ | |
| /** | |
| * Adds start/expiration to images/slides | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @package Soliloquy | |
| * @author Travis Smith | |
| */ | |
| // Fix Args | |
| add_filter( 'tgmsp_get_slider_images_args', 'wps_tgmsp_get_slider_images_args', 10, 2 ); | |
| function wps_tgmsp_get_slider_images_args( $args, $id ) { | |
| // Check ID | |
| if ( '13399' != $id ) return $args;; | |
| $args = array( | |
| 'orderby' => 'menu_order', | |
| 'order' => 'ASC', | |
| 'post_type' => 'attachment', | |
| 'post_parent' => $id, | |
| 'post_mime_type' => 'image', | |
| 'post_status' => null, | |
| 'posts_per_page' => -1, | |
| 'meta_query' => array( | |
| array( | |
| 'key' => '_soliloquy_image_expiration_date', | |
| 'value' => time(), | |
| 'compare' => '>', | |
| ), | |
| array( | |
| 'key' => '_soliloquy_image_start_date', | |
| 'value' => time(), | |
| 'compare' => '<=', | |
| ), | |
| ), | |
| ); | |
| return $args; | |
| } | |
| // Validate the time | |
| function wps_date_validator( $type, $param ) { | |
| switch( $type ) { | |
| case 'month': | |
| if ( 12 < $param ) | |
| $param = 12; | |
| elseif( 1 > $param ) | |
| $param = 1; | |
| break; | |
| case 'day': | |
| if ( 12 < $param ) | |
| $param = 12; | |
| elseif( 1 > $param ) | |
| $param = 1; | |
| break; | |
| case 'year': | |
| if( 2000 > $param ) | |
| $param = date( 'Y' ); | |
| break; | |
| case 'hour': | |
| if ( 23 < $param ) | |
| $param = 23; | |
| elseif( 0 > $param ) | |
| $param = 0; | |
| break; | |
| case 'minute': | |
| if ( 59 < $param ) | |
| $param = 59; | |
| elseif( 0 > $param ) | |
| $param = 0; | |
| break; | |
| default: | |
| break; | |
| } | |
| return $param; | |
| } | |
| // Return double digits for single digit numbers | |
| function wps_return_double_digits( $time ) { | |
| if ( '' == $time ) | |
| return '00'; | |
| if ( 10 > $time ) | |
| return '0' . $time; | |
| else | |
| return $time; | |
| } | |
| // Get the all time post meta | |
| function wps_get_time( $id ) { | |
| $time = array( | |
| 'expiration' => array( | |
| 'month' => get_post_meta( $id, '_soliloquy_image_expiration_month', true ), | |
| 'day' => get_post_meta( $id, '_soliloquy_image_expiration_day', true ), | |
| 'year' => get_post_meta( $id, '_soliloquy_image_expiration_year', true ), | |
| 'hour' => get_post_meta( $id, '_soliloquy_image_expiration_hour', true ), | |
| 'minute' => get_post_meta( $id, '_soliloquy_image_expiration_minute', true ), | |
| ), | |
| 'start' => array( | |
| 'month' => get_post_meta( $id, '_soliloquy_image_start_month', true ), | |
| 'day' => get_post_meta( $id, '_soliloquy_image_start_day', true ), | |
| 'year' => get_post_meta( $id, '_soliloquy_image_start_year', true ), | |
| 'hour' => get_post_meta( $id, '_soliloquy_image_start_hour', true ), | |
| 'minute' => get_post_meta( $id, '_soliloquy_image_start_minute', true ), | |
| ), | |
| ); | |
| return $time; | |
| } | |
| // Upload Image Fields | |
| add_filter( 'tgmsp_media_fields', 'wps_tgmsp_media_fields', 10, 2 ); | |
| function wps_tgmsp_media_fields( $fields, $attachment ) { | |
| $time = wps_get_time( $attachment->ID ); | |
| $fields['soliloquy_image_start_month'] = array( | |
| 'label' => 'Start Month (MM)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['start']['month'] ) | |
| ); | |
| $fields['soliloquy_image_start_day'] = array( | |
| 'label' => 'Start Day (DD)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['start']['day'] ) | |
| ); | |
| $fields['soliloquy_image_start_year'] = array( | |
| 'label' => 'Start Year (YYYY)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['start']['year'] ) | |
| ); | |
| $fields['soliloquy_image_start_hour'] = array( | |
| 'label' => 'Start Hour (HH, 0-24)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['start']['hour'] ) | |
| ); | |
| $fields['soliloquy_image_start_minute'] = array( | |
| 'label' => 'Start Minute (MM)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['start']['minute'] ) | |
| ); | |
| $fields['soliloquy_image_expiration_month'] = array( | |
| 'label' => 'Expiration Month (MM)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['expiration']['month'] ) | |
| ); | |
| $fields['soliloquy_image_expiration_day'] = array( | |
| 'label' => 'Expiration Day (DD)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['expiration']['day'] ) | |
| ); | |
| $fields['soliloquy_image_expiration_year'] = array( | |
| 'label' => 'Expiration Year (YYYY)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['expiration']['year'] ) | |
| ); | |
| $fields['soliloquy_image_expiration_hour'] = array( | |
| 'label' => 'Expiration Hour (HH, 0-24)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['expiration']['hour'] ) | |
| ); | |
| $fields['soliloquy_image_expiration_minute'] = array( | |
| 'label' => 'Expiration Minute (MM)', | |
| 'input' => 'text', | |
| 'value' => wps_return_double_digits( $time['expiration']['minute'] ) | |
| ); | |
| return $fields; | |
| } | |
| // Update Expiration from Upload Image Fields | |
| add_action( 'tgmsp_update_media_fields', 'wps_tgmsp_update_expiration_media_fields', 10, 2 ); | |
| function wps_tgmsp_update_expiration_media_fields( $attachment, $post_var ) { | |
| $month = isset( $post_var['soliloquy_image_expiration_month'] ) ? wps_date_validator( 'month', intval( $post_var['soliloquy_image_expiration_month'] ) ) : ''; | |
| $day = isset( $post_var['soliloquy_image_expiration_day'] ) ? wps_date_validator( 'day', intval( $post_var['soliloquy_image_expiration_day'] ) ) : ''; | |
| $year = isset( $post_var['soliloquy_image_expiration_year'] ) ? wps_date_validator( 'year', intval( $post_var['soliloquy_image_expiration_year'] ) ) : ''; | |
| $hour = isset( $post_var['soliloquy_image_expiration_hour'] ) ? wps_date_validator( 'hour', intval( $post_var['soliloquy_image_expiration_hour'] ) ) : ''; | |
| $minute = isset( $post_var['soliloquy_image_expiration_minute'] ) ? wps_date_validator( 'minute', intval( $post_var['soliloquy_image_expiration_minute'] ) ) : ''; | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_expiration_month', $month ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_expiration_day', $day ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_expiration_year', $year ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_expiration_hour', $hour ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_expiration_minute', $minute ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_expiration_date', mktime( $hour, $minute, 0, $month, $day, $year ) ); | |
| } | |
| // Update from Upload Image Fields | |
| add_action( 'tgmsp_update_media_fields', 'wps_tgmsp_update_start_media_fields', 10, 2 ); | |
| function wps_tgmsp_update_start_media_fields( $attachment, $post_var ) { | |
| $month = isset( $post_var['soliloquy_image_start_month'] ) ? wps_date_validator( 'month', intval( $post_var['soliloquy_image_start_month'] ) ) : ''; | |
| $day = isset( $post_var['soliloquy_image_start_day'] ) ? wps_date_validator( 'day', intval( $post_var['soliloquy_image_start_day'] ) ) : ''; | |
| $year = isset( $post_var['soliloquy_image_start_year'] ) ? wps_date_validator( 'year', intval( $post_var['soliloquy_image_start_year'] ) ) : ''; | |
| $hour = isset( $post_var['soliloquy_image_start_hour'] ) ? wps_date_validator( 'hour', intval( $post_var['soliloquy_image_start_hour'] ) ) : ''; | |
| $minute = isset( $post_var['soliloquy_image_start_minute'] ) ? wps_date_validator( 'minute', intval( $post_var['soliloquy_image_start_minute'] ) ) : ''; | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_start_month', $month ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_start_day', $day ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_start_year', $year ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_start_hour', $hour ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_start_minute', $minute ); | |
| update_post_meta( $attachment['ID'], '_soliloquy_image_start_date', mktime( $hour, $minute, 0, $month, $day, $year ) ); | |
| } | |
| // Thickbox Update Image Metadata | |
| add_action( 'tgmsp_after_meta_defaults', 'wps_tgmsp_after_meta_defaults' ); | |
| function wps_tgmsp_after_meta_defaults( $attachment ) { | |
| $time = wps_get_time( $attachment->ID ); | |
| echo '<tr id="soliloquy-image-start-box-' . $attachment->ID . '" valign="middle">'; | |
| echo '<th scope="row">Starts</th>'; | |
| echo '<td>'; | |
| echo '<input id="soliloquy-image-start-month-' . $attachment->ID . '" class="soliloquy-image-start-month" type="text" size="10" name="_soliloquy_uploads[post_start_month]" value="' . wps_return_double_digits( $time['start']['month'] ) . '" title="Month (MM)" alt="Month (MM)" />'; | |
| echo '<input id="soliloquy-image-start-day-' . $attachment->ID . '" class="soliloquy-image-start-day" type="text" size="10" name="_soliloquy_uploads[post_start_day]" value="' . wps_return_double_digits( $time['start']['day'] ) . '" title="Day (DD)" alt="Day (DD)" />'; | |
| echo '<input id="soliloquy-image-start-year-' . $attachment->ID . '" class="soliloquy-image-start-year" type="text" size="10" name="_soliloquy_uploads[post_start_year]" value="' . wps_return_double_digits( $time['start']['year'] ) . '" title="Year (YYYY)" alt="Year (YYYY)" />'; | |
| echo '<input id="soliloquy-image-start-hour-' . $attachment->ID . '" class="soliloquy-image-start-hour" type="text" size="10" name="_soliloquy_uploads[post_start_hour]" value="' . wps_return_double_digits( $time['start']['hour'] ) . '" title="Hour, Military Time (0-24)" alt="Hour, Military Time (0-24)" />'; | |
| echo '<input id="soliloquy-image-start-minute-' . $attachment->ID . '" class="soliloquy-image-start-minute" type="text" size="10" name="_soliloquy_uploads[post_start_minute]" value="' . wps_return_double_digits( $time['start']['minute'] ) . '" title="Minutes (0-60)" alt="Minutes (0-60)" />'; | |
| echo '<br />Day, Month, Year, Hour, Minutes (e.g., 02, 12, 2013, 23, 30)'; | |
| echo '</td>'; | |
| echo '</tr>'; | |
| echo '<tr id="soliloquy-image-expire-box-' . $attachment->ID . '" valign="middle">'; | |
| echo '<th scope="row">Expires</th>'; | |
| echo '<td>'; | |
| echo '<input id="soliloquy-image-expire-month-' . $attachment->ID . '" class="soliloquy-image-expire-month" type="text" size="10" name="_soliloquy_uploads[post_expire_month]" value="' . wps_return_double_digits( $time['expiration']['month'] ) . '" title="Month (MM)" alt="Month (MM)" />'; | |
| echo '<input id="soliloquy-image-expire-day-' . $attachment->ID . '" class="soliloquy-image-expire-day" type="text" size="10" name="_soliloquy_uploads[post_expire_day]" value="' . wps_return_double_digits( $time['expiration']['day'] ) . '" title="Day (DD)" alt="Day (DD)" />'; | |
| echo '<input id="soliloquy-image-expire-year-' . $attachment->ID . '" class="soliloquy-image-expire-year" type="text" size="10" name="_soliloquy_uploads[post_expire_year]" value="' . wps_return_double_digits( $time['expiration']['year'] ) . '" title="Year (YYYY)" alt="Year (YYYY)" />'; | |
| echo '<input id="soliloquy-image-expire-hour-' . $attachment->ID . '" class="soliloquy-image-expire-hour" type="text" size="10" name="_soliloquy_uploads[post_expire_hour]" value="' . wps_return_double_digits( $time['expiration']['hour'] ) . '" title="Hour, Military Time (0-24)" alt="Hour, Military Time (0-24)" />'; | |
| echo '<input id="soliloquy-image-expire-minute-' . $attachment->ID . '" class="soliloquy-image-expire-minute" type="text" size="10" name="_soliloquy_uploads[post_expire_minute]" value="' . wps_return_double_digits( $time['expiration']['minute'] ) . '" title="Minutes (0-60)" alt="Minutes (0-60)" />'; | |
| echo '<br />Day, Month, Year, Hour, Minutes (e.g., 02, 12, 2013, 23, 30)'; | |
| echo '</td>'; | |
| echo '</tr>'; | |
| } | |
| // Update from Thickbox Update Image Expiration Metadata | |
| add_action( 'tgmsp_ajax_update_meta', 'wps_tgmsp_ajax_update_expiration_meta' ); | |
| function wps_tgmsp_ajax_update_expiration_meta( $_p ) { | |
| $month = isset( $_p['soliloquy-image-expire-month'] ) ? wps_date_validator( 'month', intval( $_p['soliloquy-image-expire-month'] ) ) : ''; | |
| $day = isset( $_p['soliloquy-image-expire-day'] ) ? wps_date_validator( 'day', intval( $_p['soliloquy-image-expire-day'] ) ) : ''; | |
| $year = isset( $_p['soliloquy-image-expire-year'] ) ? wps_date_validator( 'year', intval( $_p['soliloquy-image-expire-year'] ) ) : ''; | |
| $hour = isset( $_p['soliloquy-image-expire-hour'] ) ? wps_date_validator( 'hour', intval( $_p['soliloquy-image-expire-hour'] ) ) : ''; | |
| $minute = isset( $_p['soliloquy-image-expire-minute'] ) ? wps_date_validator( 'minute', intval( $_p['soliloquy-image-expire-minute'] ) ) : ''; | |
| /** Make sure attachment ID is an integer */ | |
| $attachment_id = (int) $_p['attach']; | |
| update_post_meta( $attachment_id, '_soliloquy_image_expiration_month', $month ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_expiration_day', $day ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_expiration_year', $year ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_expiration_hour', $hour ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_expiration_minute', $minute ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_expiration_date', mktime( $hour, $minute, 0, $month, $day, $year ) ); | |
| } | |
| // Update from Thickbox Update Image Start Metadata | |
| add_action( 'tgmsp_ajax_update_meta', 'wps_tgmsp_ajax_update_start_meta' ); | |
| function wps_tgmsp_ajax_update_start_meta( $_p ) { | |
| $month = isset( $_p['soliloquy-image-start-month'] ) ? wps_date_validator( 'month', intval( $_p['soliloquy-image-start-month'] ) ) : ''; | |
| $day = isset( $_p['soliloquy-image-start-day'] ) ? wps_date_validator( 'day', intval( $_p['soliloquy-image-start-day'] ) ) : ''; | |
| $year = isset( $_p['soliloquy-image-start-year'] ) ? wps_date_validator( 'year', intval( $_p['soliloquy-image-start-year'] ) ) : ''; | |
| $hour = isset( $_p['soliloquy-image-start-hour'] ) ? wps_date_validator( 'hour', intval( $_p['soliloquy-image-start-hour'] ) ) : ''; | |
| $minute = isset( $_p['soliloquy-image-start-minute'] ) ? wps_date_validator( 'minute', intval( $_p['soliloquy-image-start-minute'] ) ) : ''; | |
| /** Make sure attachment ID is an integer */ | |
| $attachment_id = (int) $_p['attach']; | |
| update_post_meta( $attachment_id, '_soliloquy_image_start_month', $month ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_start_day', $day ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_start_year', $year ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_start_hour', $hour ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_start_minute', $minute ); | |
| update_post_meta( $attachment_id, '_soliloquy_image_start_date', mktime( $hour, $minute, 0, $month, $day, $year ) ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment