60,
'display' => __('Once a Minute','post-expirator')
);
return $array;
}
add_filter('cron_schedules','postExpiratorAddCronMinutes');
/**
* Add admin notice hook if cron schedule needs to be reset
*/
function postExpirationAdminNotice() {
if (postExpiratorCronStatus() === false) {
echo '
');
}
}
add_action('admin_notices','postExpirationAdminNotice');
/**
* Function that does the actual deleting - called by wp_cron
*/
function expirationdate_delete_expired_posts() {
global $wpdb;
postExpiratorTimezoneSetup();
$time_delete = time();
$result = $wpdb->get_results('select post_id, meta_value from ' . $wpdb->postmeta . ' as postmeta, '.$wpdb->posts.' as posts where postmeta.post_id = posts.ID AND posts.post_status = "publish" AND postmeta.meta_key = "expiration-date" AND postmeta.meta_value <= "' . $time_delete . '"');
if (!empty($result)) foreach ($result as $a) {
// Check to see if already proccessed
$processed = $wpdb->get_var('select meta_value from ' . $wpdb->postmeta . ' as postmeta, '.$wpdb->posts.' as posts where postmeta.post_id = posts.ID AND posts.ID = '.$a->post_id.' postmeta.meta_key = "_expiration-date-processed"');
if (!empty($processed) && $processed == 1) continue;
$post_result = $wpdb->get_var('select post_type from ' . $wpdb->posts .' where ID = '. $a->post_id);
if ($post_result == 'post') {
$expiredStatus = strtolower(get_option('expirationdateExpiredPostStatus'));
} else if ($post_result == 'page') {
$expiredStatus = strtolower(get_option('expirationdateExpiredPageStatus'));
} else {
$expiredStatus = 'draft';
}
// Check to see if expiration category is enabled and set - otherwise, proceed as normal
$catEnabled = get_option('expirationdateCategory');
$cat = get_post_meta($a->post_id,'_expiration-date-category');
if (($catEnabled === false || $catEnabled == 1) && (isset($cat) && !empty($cat[0]))) {
wp_update_post(array('ID' => $a->post_id, 'post_category' => $cat[0]));
} else {
if ($expiredStatus == 'delete')
wp_delete_post($a->post_id);
else {
wp_update_post(array('ID' => $a->post_id, 'post_status' => 'draft'));
delete_post_meta($a->post_id, 'expiration-date');
update_post_meta($a->post_id, 'expiration-date', $a->meta_value, true);
}
}
// Mark as Processed
update_post_meta($a->post_id, '_expiration-date-processed', 1);
}
}
if (postExpirator_is_wpmu())
add_action ('expirationdate_delete_'.$current_blog->blog_id, 'expirationdate_delete_expired_posts');
else
add_action ('expirationdate_delete', 'expirationdate_delete_expired_posts');
/**
* Called at plugin activation
*/
function expirationdate_activate () {
global $current_blog,$expirationdateDefaultDateFormat,$expirationdateDefaultTimeFormat,$expirationdateDefaultFooterContents,$expirationdateDefaultFooterStyle;
update_option('expirationdateExpiredPostStatus','Draft');
update_option('expirationdateExpiredPageStatus','Draft');
update_option('expirationdateDefaultDateFormat',$expirationdateDefaultDateFormat);
update_option('expirationdateDefaultTimeFormat',$expirationdateDefaultTimeFormat);
update_option('expirationdateFooterContents',$expirationdateDefaultFooterContents);
update_option('expirationdateFooterStyle',$expirationdateDefaultFooterStyle);
update_option('expirationdateDisplayFooter',0);
update_option('expirationdateCategory',1);
postExpiratorTimezoneSetup();
if (postExpirator_is_wpmu())
wp_schedule_event(mktime(date('H'),0,0,date('m'),date('d'),date('Y')), 'postexpiratorminute', 'expirationdate_delete_'.$current_blog->blog_id);
else
wp_schedule_event(mktime(date('H'),0,0,date('m'),date('d'),date('Y')), 'postexpiratorminute', 'expirationdate_delete');
}
register_activation_hook (__FILE__, 'expirationdate_activate');
/**
* Called at plugin deactivation
*/
function expirationdate_deactivate () {
global $current_blog;
delete_option('expirationdateExpiredPostStatus');
delete_option('expirationdateExpiredPageStatus');
delete_option('expirationdateDefaultDateFormat');
delete_option('expirationdateDefaultTimeFormat');
delete_option('expirationdateDisplayFooter');
delete_option('expirationdateFooterContents');
delete_option('expirationdateFooterStyle');
delete_option('expirationdateCategory');
if (postExpirator_is_wpmu())
wp_clear_scheduled_hook('expirationdate_delete_'.$current_blog->blog_id);
else
wp_clear_scheduled_hook('expirationdate_delete');
}
register_deactivation_hook (__FILE__, 'expirationdate_deactivate');
/**
* adds an 'Expires' column to the post display table.
*/
function expirationdate_add_column ($columns) {
$columns['expirationdate'] = __('Expires','post-expirator').'
(YYYY/MM/DD HH:MM)';
return $columns;
}
add_filter ('manage_posts_columns', 'expirationdate_add_column');
add_filter ('manage_pages_columns', 'expirationdate_add_column');
/**
* fills the 'Expires' column of the post display table.
*/
function expirationdate_show_value ($column_name) {
global $wpdb, $post;
$id = $post->ID;
if ($column_name === 'expirationdate') {
postExpiratorTimezoneSetup();
$query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = \"expiration-date\" AND post_id=$id";
$ed = $wpdb->get_var($query);
echo ($ed ? date('Y/m/d H:i',$ed) : __("Never",'post-expirator'));
}
}
add_action ('manage_posts_custom_column', 'expirationdate_show_value');
add_action ('manage_pages_custom_column', 'expirationdate_show_value');
/**
* Adds hooks to get the meta box added to pages and custom post types
*/
function expirationdate_meta_custom() {
$custom_post_types = get_post_types();
foreach ($custom_post_types as $t) {
add_meta_box('expirationdatediv', __('Post Expirator','post-expirator'), 'expirationdate_meta_box', 'post', 'side', 'core');
}
add_meta_box('expirationdatediv', __('Post Expirator','post-expirator'), 'expirationdate_meta_box', 'page', 'side', 'core');
}
add_action ('add_meta_boxes','expirationdate_meta_custom');
/**
* Actually adds the meta box
*/
function expirationdate_meta_box($post) {
// Get default month
postExpiratorTimezoneSetup();
$expirationdatets = get_post_meta($post->ID,'expiration-date',true);
if (empty($expirationdatets)) {
$defaultmonth = date('F');
$defaultday = date('d');
$defaulthour = date('H');
$defaultyear = date('Y');
$defaultminute = date('i');
$disabled = 'disabled="disabled"';
$categories = get_option('expirationdateCategoryDefaults');
} else {
$defaultmonth = date('F',$expirationdatets);
$defaultday = date('d',$expirationdatets);
$defaultyear = date('Y',$expirationdatets);
$defaulthour = date('H',$expirationdatets);
$defaultminute = date('i',$expirationdatets);
$categories = get_post_meta($post->ID,'_expiration-date-category',true);
$enabled = ' checked="checked"';
$disabled = '';
}
$rv = array();
$rv[] = '';
$rv[] = '
';
$rv[] = '';
$rv[] = '| '.__('Year','post-expirator').' | ';
$rv[] = ''.__('Month','post-expirator').' | ';
$rv[] = ''.__('Day','post-expirator').' | ';
$rv[] = '
';
$rv[] = '| ';
$rv[] = '';
$rv[] = ' | ';
$rv[] = '';
$rv[] = ' | ';
$rv[] = ',';
$rv[] = ' |
';
$rv[] = ' | ';
$rv[] = ''.__('Hour','post-expirator').'('.date('T',mktime(0, 0, 0, $i, 1, date("Y"))).') | ';
$rv[] = ''.__('Minute','post-expirator').' | ';
$rv[] = '
';
$rv[] = '| @ | ';
/* $rv[] = ''; */
$rv[] = ' | ';
$rv[] = '';
$rv[] = '';
$rv[] = ' |
';
echo implode("\n",$rv);
$catEnabled = get_option('expirationdateCategory');
if (($post->post_type != 'page') && ($catEnabled === false || $catEnabled == 1)) {
$args = array('hide_empty' => 0,'orderby' => 'name','hierarchical' => 0);
echo '
'.__('Expiration Categories','post-expirator').':
';
echo '';
echo ''.__('Setting an expiration category will change the category at expiration time, and override the default post action','post-expirator').'.
';
}
echo '';
}
/**
* PHP Code to be executed by ajax function call - currently nothing happens
*/
function expirationdate_ajax_process() {
// Gather Values
$enable = $_POST['enable'];
die(0);
}
add_action ('wp_ajax_expirationdate_ajax','expirationdate_ajax_process');
/**
* Add's ajax javascript
*/
function expirationdate_js_admin_header() {
// use JavaScript SACK library for Ajax
wp_print_scripts( array( 'sack' ));
// Define custom JavaScript function
?>
domain.$current_blog->path;
else
echo get_bloginfo('siteurl').'/';
}
/**
* Called when post is saved - stores expiration-date meta value
*/
function expirationdate_update_post_meta($id) {
if (!isset($_POST['expirationdate_formcheck']))
return false;
$month = $_POST['expirationdate_month'];
$day = $_POST['expirationdate_day'];
$year = $_POST['expirationdate_year'];
$hour = $_POST['expirationdate_hour'];
$minute = $_POST['expirationdate_minute'];
$category = $_POST['expirationdate_category'];
if (isset($_POST['enable-expirationdate'])) {
postExpiratorTimezoneSetup();
// Format Date
$ts = mktime($hour,$minute,0,$month,$day,$year);
// Update Post Meta
delete_post_meta($id, '_expiration-date-category');
delete_post_meta($id, 'expiration-date');
update_post_meta($id, 'expiration-date', $ts, true);
$catEnabled = get_option('expirationdateCategory');
if ((isset($category) && !empty($category)) && ($catEnabled === false || $catEnabled == 1)) {
if (!empty($category)) update_post_meta($id, '_expiration-date-category', $category);
}
update_post_meta($id, '_expiration-date-processed', 0, true);
} else {
delete_post_meta($id, 'expiration-date');
delete_post_meta($id, '_expiration-date-category');
delete_post_meta($id, '_expiration-date-processed');
}
}
add_action('save_post','expirationdate_update_post_meta');
/**
* Build the menu for the options page
*/
function postExpiratorMenuTabs($tab) {
echo ''.__('Post Expirator Options','post-expirator').'
';
echo '';
if (empty($tab)) $tab = 'general';
echo ''.__('General Settings','post-expirator').' | ';
echo ''.__('Upgrade','post-expirator').'';
echo '
';
}
/**
*
*/
function postExpiratorMenu() {
$tab = $_GET['tab'];
echo '';
postExpiratorMenuTabs($tab);
if (empty($tab) || $tab == 'general') {
postExpiratorMenuGeneral();
} elseif ($tab == 'upgrade') {
postExpiratorMenuUpgrade();
}
echo '
';
}
/**
* Hook's to add plugin page menu
*/
function postExpiratorPluginMenu() {
add_submenu_page('options-general.php',__('Post Expirator Options','post-expirator'),__('Post Expirator','post-expirator'),9,basename(__FILE__),'postExpiratorMenu');
}
add_action('admin_menu', 'postExpiratorPluginMenu');
/**
* Show the Expiration Date options page
*/
function postExpiratorMenuGeneral() {
if ($_POST['expirationdateSave']) {
update_option('expirationdateExpiredPostStatus',$_POST['expired-post-status']);
update_option('expirationdateExpiredPageStatus',$_POST['expired-page-status']);
update_option('expirationdateDefaultDateFormat',$_POST['expired-default-date-format']);
update_option('expirationdateDefaultTimeFormat',$_POST['expired-default-time-format']);
update_option('expirationdateDisplayFooter',$_POST['expired-display-footer']);
update_option('expirationdateFooterContents',$_POST['expired-footer-contents']);
update_option('expirationdateFooterStyle',$_POST['expired-footer-style']);
update_option('expirationdateCategory',$_POST['expired-category']);
update_option('expirationdateCategoryDefaults',$_POST['expirationdate_category']);
echo "";
_e('Saved Options!','post-expirator');
echo "
";
}
postExpiratorTimezoneSetup();
// Get Option
$expirationdateExpiredPostStatus = get_option('expirationdateExpiredPostStatus');
if (empty($expirationdateExpiredPostStatus))
$expirationdateExpiredPostStatus = 'Draft';
$expirationdateExpiredPageStatus = get_option('expirationdateExpiredPageStatus');
if (empty($expirationdateExpiredPageStatus))
$expirationdateExpiredPageStatus = 'Draft';
$expirationdateDefaultDateFormat = get_option('expirationdateDefaultDateFormat');
if (empty($expirationdateDefaultDateFormat)) {
global $expirationdateDefaultDateFormat;
$expirationdateDefaultDateFormat = $expirationdateDefaultDateFormat;
}
$expirationdateDefaultTimeFormat = get_option('expirationdateDefaultTimeFormat');
if (empty($expirationdateDefaultTimeFormat)) {
global $expirationdateDefaultTimeFormat;
$expirationdateDefaultTimeFormat = $expirationdateDefaultTimeFormat;
}
$categories = get_option('expirationdateCategoryDefaults');
$expiredcategory = get_option('expirationdateCategory');
if (!isset($expiredcategory))
$expiredcategory = 1;
$expireddisplayfooter = get_option('expirationdateDisplayFooter');
if (empty($expireddisplayfooter))
$expireddisplayfooter = 0;
$expireddisplayfooterenabled = '';
$expireddisplayfooterdisabled = '';
if ($expireddisplayfooter == 0)
$expireddisplayfooterdisabled = 'checked="checked"';
else if ($expireddisplayfooter == 1)
$expireddisplayfooterenabled = 'checked="checked"';
if ($expiredcategory == 0)
$expiredcategorydisabled = 'checked="checked"';
else if ($expiredcategory == 1)
$expiredcategoryenabled = 'checked="checked"';
$expirationdateFooterContents = get_option('expirationdateFooterContents');
if (empty($expirationdateFooterContents)) {
global $expirationdateDefaultFooterContents;
$expirationdateFooterContents = $expirationdateDefaultFooterContents;
}
$expirationdateFooterStyle = get_option('expirationdateFooterStyle');
if (empty($expirationdateFooterStyle)) {
global $expirationdateDefaultFooterStyle;
$expirationdateFooterStyle = $expirationdateDefaultFooterStyle;
}
?>
blog_id;
}
$results = array();
foreach ( $names as $name ) {
array_push($results,wp_get_schedule($name));
}
foreach ( $results as $result ) {
if ($result == 'hourly') return false;
}
return true;
}
/**
* Reset all cron schedules for Post Expirator Plugin
*/
function postExpiratorResetCron() {
postExpiratorTimezoneSetup();
if (postExpirator_is_wpmu()) {
global $current_blog;
wp_clear_scheduled_hook('expirationdate_delete_'.$current_blog->blog_id);
wp_schedule_event(mktime(date('H'),0,0,date('m'),date('d'),date('Y')), 'postexpiratorminute', 'expirationdate_delete_'.$current_blog->blog_id);
} else {
wp_clear_scheduled_hook('expirationdate_delete');
wp_clear_scheduled_hook('expirationdate_delete_');
wp_schedule_event(mktime(date('H'),0,0,date('m'),date('d'),date('Y')), 'postexpiratorminute', 'expirationdate_delete');
}
}
function postExpiratorMenuUpgrade() {
if (isset($_POST['reset-cron-schedules'])) {
postExpiratorResetCron();
echo ""; _e('Cron Schedules Reset!','post-expirator'); echo "
";
}
$status = postExpiratorCronStatus();
if ($status)
$cronstatus = ''.__('OK','post-expirator').'';
else
$cronstatus = ''.__('RESET NEEDED','post-expirator').'';
?>
ID,'expiration-date',true);
if (empty($expirationdatets))
return false;
extract(shortcode_atts(array(
'dateformat' => get_option('expirationdateDefaultDateFormat'),
'timeformat' => get_option('expirationdateDefaultTimeFormat'),
'type' => full,
'tz' => date('T')
), $atts));
if (empty($dateformat)) {
global $expirationdateDefaultDateFormat;
$dateformat = $expirationdateDefaultDateFormat;
}
if (empty($timeformat)) {
global $expirationdateDefaultTimeFormat;
$timeformat = $expirationdateDefaultTimeFormat;
}
if ($type == 'full')
$format = $dateformat.' '.$timeformat;
else if ($type == 'date')
$format = $dateformat;
else if ($type == 'time')
$format = $timeformat;
return date("$format",$expirationdatets);
}
add_shortcode('postexpirator', 'postexpirator_shortcode');
function postexpirator_add_footer($text) {
global $post;
// Check to see if its enabled
$displayFooter = get_option('expirationdateDisplayFooter');
if ($displayFooter === false || $displayFooter == 0)
return $text;
postExpiratorTimezoneSetup();
$expirationdatets = get_post_meta($post->ID,'expiration-date',true);
if (!is_numeric($expirationdatets))
return $text;
$dateformat = get_option('expirationdateDefaultDateFormat');
if (empty($dateformat)) {
global $expirationdateDefaultDateFormat;
$dateformat = $expirationdateDefaultDateFormat;
}
$timeformat = get_option('expirationdateDefaultTimeFormat');
if (empty($timeformat)) {
global $expirationdateDefaultTimeFormat;
$timeformat = $expirationdateDefaultTimeFormat;
}
$expirationdateFooterContents = get_option('expirationdateFooterContents');
if (empty($expirationdateFooterContents)) {
global $expirationdateDefaultFooterContents;
$expirationdateFooterContents = $expirationdateDefaultFooterContents;
}
$expirationdateFooterStyle = get_option('expirationdateFooterStyle');
if (empty($expirationdateFooterStyle)) {
global $expirationdateDefaultFooterStyle;
$expirationdateFooterStyle = $expirationdateDefaultFooterStyle;
}
$search = array(
'EXPIRATIONFULL',
'EXPIRATIONDATE',
'EXPIRATIONTIME'
);
$replace = array(
date("$dateformat $timeformat",$expirationdatets),
date("$dateformat",$expirationdatets),
date("$timeformat",$expirationdatets)
);
$add_to_footer = ''.str_replace($search,$replace,$expirationdateFooterContents).'
';
return $text.$add_to_footer;
}
add_action('the_content','postexpirator_add_footer',0);
/**
* Add Stylesheet
*/
function postexpirator_css() {
$myStyleUrl = plugins_url('style.css', __FILE__); // Respects SSL, Style.css is relative to the current file
$myStyleFile = WP_PLUGIN_DIR . '/post-expirator/style.css';
if ( file_exists($myStyleFile) ) {
wp_register_style('postexpirator-css', $myStyleUrl);
wp_enqueue_style('postexpirator-css');
}
}
add_action('admin_init','postexpirator_css');
class Walker_PostExpirator_Category_Checklist extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
var $disabled = '';
function setDisabled() {
$this->disabled = 'disabled="disabled"';
}
function start_lvl(&$output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "$indent\n";
}
function end_lvl(&$output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "$indent
\n";
}
function start_el(&$output, $category, $depth, $args) {
extract($args);
if ( empty($taxonomy) )
$taxonomy = 'category';
$name = 'expirationdate_category';
$class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
$output .= "\n" . '';
}
function end_el(&$output, $category, $depth, $args) {
$output .= "\n";
}
}