• I have made a new plugin that implements “expireable” posts and pages.

    Looking for feedback and beta testing.

    Hope that this plugin to contribute to some core changes in WordPress (previous_post_link and next_post_link should, in my opinion, apply the custom filters set to post fetching query).

    Regards,
    Adrian


    <?php
    /*
    Plugin Name: Rotea Expire Posts
    Plugin URI: http://ww.wp.xz.cn/#
    Description: This plugin filters the posts, hiding the expired ones from the site only and *NOT* from admin section too! The post expiration is triggered by the custom field <code>expiration</code> attached to the post. If <code>expiration</code> is *NOT* set then that post will never expire. Due to some not implemented behaviuors in WordPress you *MUST* use <code>rotea_previous_post_link</code> and <code>rotea_next_post_link</code> instead of <code>previous_post_link</code> and <code>next_post_link</code> in your templates.
    Author: Adrian Rotea <[email protected]>
    Version: 1.0
    Author URI: mailto:[email protected]
    */

    // some constants
    define('EXPIRATION_META', 'expiration'); // post meta key used in the wp database

    // expirtion form field
    function rotea_expire_posts_form_field() {
    global $month, $postdata, $content;

    $time_adj = time() + (get_settings('gmt_offset') * 3600);
    $post_date = get_post_meta($postdata->ID, EXPIRATION_META, true);

    if (!empty($post_date)) {
    $checked = ' checked="checked" ';
    $edit = true;
    } else {
    $checked = ' ';
    $edit = false;
    }

    echo "
    <script type=\"text/javascript\">
    function toggle_expiration_edit(form, enable)
    {
    if (enable) {
    form.elements['expiration[mm]'].disabled = false;
    form.elements['expiration[jj]'].disabled = false;
    form.elements['expiration[aa]'].disabled = false;
    form.elements['expiration[hh]'].disabled = false;
    form.elements['expiration[mn]'].disabled = false;
    form.elements['expiration[ss]'].disabled = false;
    } else {
    form.elements['expiration[mm]'].disabled = true;
    form.elements['expiration[jj]'].disabled = true;
    form.elements['expiration[aa]'].disabled = true;
    form.elements['expiration[hh]'].disabled = true;
    form.elements['expiration[mn]'].disabled = true;
    form.elements['expiration[ss]'].disabled = true;
    }
    }
    </script>
    ";
    echo '<table border="0" cellspacing="3" cellpadding="3" width="100%"><tr><th width="1%">Expiration:</th><td width="99%">';
    echo '<fieldset><legend><input type="checkbox" class="checkbox" name="expiration[edit_date]" value="1" id="expiration[timestamp]" ' . $checked . ' onClick="toggle_expiration_edit(this.form, this.checked);" /> <label for="expiration[timestamp]">' . __('Edit timestamp') . '</label></legend>';

    $jj = ($edit) ? mysql2date('d', $post_date) : gmdate('d', $time_adj);
    $mm = ($edit) ? mysql2date('m', $post_date) : gmdate('m', $time_adj);
    $aa = ($edit) ? mysql2date('Y', $post_date) : gmdate('Y', $time_adj);
    $hh = ($edit) ? mysql2date('H', $post_date) : gmdate('H', $time_adj);
    $mn = ($edit) ? mysql2date('i', $post_date) : gmdate('i', $time_adj);
    $ss = ($edit) ? mysql2date('s', $post_date) : gmdate('s', $time_adj);

    echo "<select name=\"expiration[mm]\" " . ($edit ? '' : 'disabled') . " >n";
    for ($i=1; $i < 13; $i=$i+1) {
    echo "ttt<option value=\"$i\"";
    if ($i == $mm)
    echo " selected='selected'";
    if ($i < 10) {
    $ii = "0".$i;
    } else {
    $ii = "$i";
    }
    echo ">".$month["$ii"]."</option>n";
    }

    ?>

    </select>
    <input type="text" name="expiration[jj]" value="<?php echo $jj; ?>" size="2" maxlength="2" <?php echo ($edit ? '' : 'disabled') ?> />
    <input type="text" name="expiration[aa]" value="<?php echo $aa ?>" size="4" maxlength="5" <?php echo ($edit ? '' : 'disabled') ?> /> @
    <input type="text" name="expiration[hh]" value="<?php echo $hh ?>" size="2" maxlength="2" <?php echo ($edit ? '' : 'disabled') ?> /> :
    <input type="text" name="expiration[mn]" value="<?php echo $mn ?>" size="2" maxlength="2" <?php echo ($edit ? '' : 'disabled') ?> />
    <input type="hidden" name="expiration[ss]" value="<?php echo 59/*$ss*/ ?>" size="2" maxlength="2" <?php echo ($edit ? '' : 'disabled') ?> />

    <?php
    // We might need to readjust to display proper existing timestamp
    if ( $for_post && ('draft' == $postdata->post_status) ) {
    $jj = mysql2date('d', $post_date);
    $mm = mysql2date('m', $post_date);
    $aa = mysql2date('Y', $post_date);
    $hh = mysql2date('H', $post_date);
    $mn = mysql2date('i', $post_date);
    $ss = mysql2date('s', $post_date);
    }

    echo ($edit ? __('Existing timestamp') . ": {$month[$mm]} $jj, $aa @ $hh:$mn" : '');
    ?>

    </fieldset>
    </td>
    </tr>
    </table>
    <?php
    }

    // form function
    function rotea_expire_posts_form()
    {
    rotea_expire_posts_form_field();
    }

    // save function
    function rotea_expire_posts_save($id)
    {
    // remove old value
    delete_post_meta($id, EXPIRATION_META);

    // clean up expiration date
    if (!empty($_POST['expiration']['edit_date'])) {
    $aa = $_POST['expiration']['aa'];
    $mm = $_POST['expiration']['mm'];
    $jj = $_POST['expiration']['jj'];
    $hh = $_POST['expiration']['hh'];
    $mn = $_POST['expiration']['mn'];
    $ss = $_POST['expiration']['ss'];

    $jj = ($jj > 31) ? 31 : $jj;
    $hh = ($hh > 23) ? $hh - 24 : $hh;
    $mn = ($mn > 59) ? $mn - 60 : $mn;
    $ss = ($ss > 59) ? $ss - 60 : $ss;

    $expiration = "$aa-$mm-$jj $hh:$mn:$ss"; // local
    //$expiration = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss"); //gmt
    } else {
    $expiration = '';
    }

    // save
    if (!empty($expiration)) {
    add_post_meta($id, EXPIRATION_META, $expiration);
    }
    }

    // where function
    function rotea_expire_posts_where($where)
    {
    if (!is_admin()) {
    global $wpdb;

    //$where .= " AND IF($wpdb->postmeta.meta_id IS NULL, 1, $wpdb->postmeta.meta_value >= '" . gmdate('Y-m-d H:i:59') . "') ";
    $where .= " AND IF($wpdb->postmeta.meta_id IS NULL, 1, $wpdb->postmeta.meta_value >= NOW()) ";
    }

    return $where;
    }

    // join function
    function rotea_expire_posts_join($join)
    {
    if (!is_admin()) {
    global $wpdb;

    $join .= " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'expiration') ";
    }

    return $join;
    }

    // Navigation links
    function rotea_get_previous_post($in_same_cat = false, $excluded_categories = '') {
    global $post, $wpdb;

    if(! is_single()) {
    return null;
    }

    $current_post_date = $post->post_date;

    $join = '';
    if ($in_same_cat) {
    $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
    $cat_array = get_the_category($post->ID);
    $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
    for ($i = 1; $i < (count($cat_array)); $i++) {
    $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
    }
    $join .= ')';
    }

    // Apply filters on join prior to paging so that any
    // manipulations to them are reflected in the paging by day queries.
    $join = apply_filters('posts_join', $join);

    $sql_exclude_cats = '';
    if (!empty($excluded_categories)) {
    $blah = explode('and', $excluded_categories);
    foreach($blah as $category) {
    $category = intval($category);
    $sql_exclude_cats .= " AND post_category != $category";
    }
    }

    // Apply filters on where prior to paging so that any
    // manipulations to them are reflected in the paging by day queries.
    $where = apply_filters('posts_where', $where);

    return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join WHERE post_date < '$current_post_date' AND post_status = 'publish' $where $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT 1");
    }

    function rotea_get_next_post($in_same_cat = false, $excluded_categories = '') {
    global $post, $wpdb;

    if(! is_single()) {
    return null;
    }

    $current_post_date = $post->post_date;

    $join = '';
    if ($in_same_cat) {
    $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
    $cat_array = get_the_category($post->ID);
    $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
    for ($i = 1; $i < (count($cat_array)); $i++) {
    $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
    }
    $join .= ')';
    }

    // Apply filters on join prior to paging so that any
    // manipulations to them are reflected in the paging by day queries.
    $join = apply_filters('posts_join', $join);

    $sql_exclude_cats = '';
    if (!empty($excluded_categories)) {
    $blah = explode('and', $excluded_categories);
    foreach($blah as $category) {
    $category = intval($category);
    $sql_exclude_cats .= " AND post_category != $category";
    }
    }

    // Apply filters on where prior to paging so that any
    // manipulations to them are reflected in the paging by day queries.
    $where = apply_filters('posts_where', $where);

    $now = current_time('mysql');

    return @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts $join WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $where $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT 1");
    }

    function rotea_previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    $post = rotea_get_previous_post($in_same_cat, $excluded_categories);

    if(! $post) {
    return;
    }

    $title = apply_filters('the_title', $post->post_title, $post);

    $string = '<a href="'.get_permalink($post->ID).'">';

    $link = str_replace('%title', $title, $link);

    $link = $string . $link . '</a>';

    $format = str_replace('%link', $link, $format);

    echo $format;
    }

    function rotea_next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    $post = rotea_get_next_post($in_same_cat, $excluded_categories);

    if(! $post) {
    return;
    }

    $title = apply_filters('the_title', $post->post_title, $post);

    $string = '<a href="'.get_permalink($post->ID).'">';

    $link = str_replace('%title', $title, $link);

    $link = $string . $link . '</a>';

    $format = str_replace('%link', $link, $format);

    echo $format;
    }

    // link function
    /*
    function rotea_expire_posts_link($title, $post)
    {
    $expiration = get_post_meta($post->ID, EXPIRATION_META, true);

    if (!empty($expiration) && intval($expiration = strtotime($expiration)) > 0 && current_time('timestamp') >= $expiration) {
    return NULL;
    }

    return $title;
    }

    // prev/next link
    //add_filter('post_link', 'rotea_expire_posts_link', 10, 2);
    //add_filter('the_title', 'rotea_expire_posts_link', 10, 2);
    */

    // form
    add_action('simple_edit_form', 'rotea_expire_posts_form');
    add_action('edit_form_advanced', 'rotea_expire_posts_form');
    add_filter('edit_page_form', 'rotea_expire_posts_form');

    // save
    add_action('edit_post', 'rotea_expire_posts_save');
    add_action('publish_post', 'rotea_expire_posts_save');
    add_action('save_post', 'rotea_expire_posts_save');

    // query
    add_filter('posts_where', 'rotea_expire_posts_where');
    add_filter('posts_join', 'rotea_expire_posts_join');
    //add_filter('posts_where_paged', 'rotea_expire_posts_where');
    //add_filter('posts_join_paged', 'rotea_expire_posts_join');

    ?>

Viewing 16 replies (of 16 total)
  • WPChina

    (@wordpresschina)

    I have been using “Posts Expire 1.02” from http://wunder-ful.com/wordpress-plugins for a few months.

    It seemed to work fine at first and then I stopped monitoring it. But now I see that many posts that should have expired and been removed from view a few weeks ago are still there… any tips on how to get them to automatically go away?

Viewing 16 replies (of 16 total)

The topic ‘Expire Posts’ is closed to new replies.