Support » Plugin: Post Expirator » [Plugin: Post Expirator] Custom Post Type Support

  • curtismchale

    (@curtismchale)



    Would be wonder to add custom post type support to this. It should read the registered custom post types on the site and allow you to set which ones expirator works for.

Viewing 12 replies - 1 through 12 (of 12 total)
  • ditto.

    would be great to have this functionality out the box.

    Thank!

    FYI have managed to get this to work with custom post types.

    Add the following

    /**
     * Add's hooks to get the meta box added to custom page types
     */
    function expirationdate_meta_custom() {
     $custom_post_types = get_post_types();
     foreach ($custom_post_types as $t) {
      add_meta_box('expirationdatediv', __('Post Expirator'), 'expirationdate_meta_box', $t, 'advanced', 'high');
     }
    }

    At line 179

    This worked for me 🙂

    If the developer could add this to the next release, i would be happy 🙂

    Thanks,
    Matt

    Very nice…thanks!

    I don’t think the original developer is working on this anymore. Would be great to see someone new continue with the development.

    ‘IF’ I ever get time…

    I will look to make a branch of this plugin and add this new feature if the developer has truly given up 🙂

    …’IF’

    Best,
    Matt

    That would be great 🙂

    The solution above does not appear to work at all and I am getting a cannot redeclare function error

    Hi qwik3r,

    See below for my entire file. This worked for me.

    Best,
    Matt

    <?php
    /*
    Plugin Name: Post Expirator
    Plugin URI: http://wordpress.org/extend/plugins/post-expirator/
    Description: Allows you to add an expiration date to posts which you can configure to either delete the post or change it to a draft.
    Author: Aaron Axelsen
    Version: 1.4
    Author URI: http://www.frozenpc.net
    */
    
    // Default Values
    $expirationdateDefaultDateFormat = 'l F jS, Y';
    $expirationdateDefaultTimeFormat = 'g:ia';
    $expirationdateDefaultFooterContents = 'Post expires at EXPIRATIONTIME on EXPIRATIONDATE';
    $expirationdateDefaultFooterStyle = 'font-style: italic;';
    
    // Detect WPMU
    function postExpirator_is_wpmu() {
            return file_exists(ABSPATH."/wpmu-settings.php");
    }
    
    // Timezone Setup
    function postExpiratorTimezoneSetup() {
            if ( !$timezone_string = get_option( 'timezone_string' ) ) {
                    return false;
            }
    
        @date_default_timezone_set($timezone_string);
    }
    
    // Add cron interval of 60 seconds
    function postExpiratorAddCronMinutes($array) {
           $array['postexpiratorminute'] = array(
                   'interval' => 60,
                   'display' => __('Once a Minute')
           );
        return $array;
    }
    add_filter('cron_schedules','postExpiratorAddCronMinutes');
    
    /**
     * Add admin notice hook if cron schedule needs to be reset
     */
    function postExpirationAdminNotice() {
        if (postExpiratorCronStatus() === false) {
            echo '<div class="error fade" style="background-color:red;"><p><strong>Post Expirator cron schedules need to be reset.
            <a href="'.admin_url('options-general.php?page=post-expirator.php&tab=upgrade').'" style="color: blue;">Click here to reset</a></strong></p></div>';
        }
    }
    add_action('admin_notices','postExpirationAdminNotice');
    
    /**
     * Function that does the actualy deleting - called by wp_cron
     */
    function expirationdate_delete_expired_posts() {
        global $wpdb;
        postExpiratorTimezoneSetup();
        $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 <= "' . mktime() . '"');
          if (!empty($result)) foreach ($result as $a) {
            $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';
            }
    
            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);
            }
        }
    }
    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);
            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');
        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 <br/><span style="font-size: 0.8em; font-weight: normal;">(YYYY/MM/DD HH:MM)</span>';
          return $columns;
    }
    add_filter ('manage_posts_columns', 'expirationdate_add_column');
    add_filter ('manage_pages_columns', 'expirationdate_add_column');
    
    /* Adds a custom section to the "advanced" Post and Page edit screens */
    function myplugin_add_custom_box() {
      if( function_exists( 'add_meta_box' )) {
        add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ),
                    'myplugin_inner_custom_box', 'post', 'advanced' );
        add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ),
                    'myplugin_inner_custom_box', 'page', 'advanced' );
       } else {
        add_action('dbx_post_advanced', 'myplugin_old_custom_box' );
        add_action('dbx_page_advanced', 'myplugin_old_custom_box' );
      }
    }
    
    /**
     * 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");
          }
    }
    add_action ('manage_posts_custom_column', 'expirationdate_show_value');
    add_action ('manage_pages_custom_column', 'expirationdate_show_value');
    
    /**
     * Add's hooks to get the meta box added to post
     */
    function expirationdate_meta_post() {
        add_meta_box('expirationdatediv', __('Post Expirator'), 'expirationdate_meta_box', 'post', 'advanced', 'high');
    }
    add_action ('dbx_post_advanced','expirationdate_meta_post');
    
    /**
     * Add's hooks to get the meta box added to page
     */
    function expirationdate_meta_page() {
        add_meta_box('expirationdatediv', __('Post Expirator'), 'expirationdate_meta_box', 'page', 'advanced', 'high');
    }
    add_action ('edit_page_form','expirationdate_meta_page');
    
    /**
     * Add's hooks to get the meta box added to custom page types
     */
    function expirationdate_meta_custom() {
        $custom_post_types = get_post_types();
        foreach ($custom_post_types as $t) {
            add_meta_box('expirationdatediv', __('Post Expirator'), 'expirationdate_meta_box', $t, 'advanced', 'high');
        }
    }
    add_action ('edit_form_advanced','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"';
        } else {
            $defaultmonth = date('F',$expirationdatets);
            $defaultday = date('d',$expirationdatets);
            $defaultyear = date('Y',$expirationdatets);
            $defaulthour = date('H',$expirationdatets);
            $defaultminute = date('i',$expirationdatets);
    
            $enabled = ' checked="checked"';
            $disabled = '';
        }
    
        $rv = array();
        $rv[] = '<p><input type="checkbox" name="enable-expirationdate" id="enable-expirationdate" value="checked"'.$enabled.' onclick="expirationdate_ajax_add_meta(\'enable-expirationdate\')" />';
        $rv[] = '<label for="enable-expirationdate">Enable Post Expiration</label></p>';
        $rv[] = '<table><tr>';
           $rv[] = '<th style="text-align: left;">Month</th>';
           $rv[] = '<th style="text-align: left;">Day</th>';
           $rv[] = '<th style="text-align: left;">Year</th>';
           $rv[] = '<th style="text-align: left;"></th>';
           $rv[] = '<th style="text-align: left;">Hour (24 Hour Format)</th>';
           $rv[] = '<th style="text-align: left;">Minute</th>';
        $rv[] = '</tr><tr>';
        $rv[] = '<td>';
        $rv[] = '<select name="expirationdate_month" id="expirationdate_month"'.$disabled.'">';
        for($i = 1; $i <= 12; $i++) {
            if ($defaultmonth == date('F',mktime(0, 0, 0, $i, 1, date("Y"))))
                $selected = ' selected="selected"';
            else
                $selected = '';
            $rv[] = '<option value="'.date('m',mktime(0, 0, 0, $i, 1, date("Y"))).'"'.$selected.'>'.date('F',mktime(0, 0, 0, $i, 1, date("Y"))).'</option>';
        }
        $rv[] = '</select>';
        $rv[] = '</td><td>';
        $rv[] = '<input type="text" id="expirationdate_day" name="expirationdate_day" value="'.$defaultday.'" size="2"'.$disabled.'" />,';
        $rv[] = '</td><td>';
        $rv[] = '<select name="expirationdate_year" id="expirationdate_year"'.$disabled.'">';
        $currentyear = date('Y');
        if ($defaultyear < $currentyear)
            $currentyear = $defaultyear;
        for($i = $currentyear; $i < $currentyear + 8; $i++) {
            if ($i == $defaultyear)
                $selected = ' selected="selected"';
            else
                $selected = '';
            $rv[] = '<option'.$selected.'>'.($i).'</option>';
        }
        $rv[] = '</select>';
        $rv[] = '</td><td>@</td><td>';
        $rv[] = '<input type="text" id="expirationdate_hour" name="expirationdate_hour" value="'.$defaulthour.'" size="2"'.$disabled.'" />';
        $rv[] = '</td><td>';
        $rv[] = '<input type="text" id="expirationdate_minute" name="expirationdate_minute" value="'.$defaultminute.'" size="2"'.$disabled.'" />';
        $rv[] = '<input type="hidden" name="expirationdate_formcheck" value="true" />';
        $rv[] = '</td></tr></table>';
    
        $rv[] = '<div id="expirationdate_ajax_result"></div>';
    
        echo implode("\n",$rv);
    }
    
    /**
     * 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
        ?>
    <script type="text/javascript">
    //<![CDATA[
    function expirationdate_ajax_add_meta(expireenable) {
        var mysack = new sack("<?php expirationdate_get_blog_url(); ?>wp-admin/admin-ajax.php");
    
        var expire = document.getElementById(expireenable);
    
        if (expire.checked == true) {
            var enable = 'true';
            document.getElementById('expirationdate_month').disabled = false;
            document.getElementById('expirationdate_day').disabled = false;
            document.getElementById('expirationdate_year').disabled = false;
            document.getElementById('expirationdate_hour').disabled = false;
            document.getElementById('expirationdate_minute').disabled = false;
        } else {
            document.getElementById('expirationdate_month').disabled = true;
            document.getElementById('expirationdate_day').disabled = true;
            document.getElementById('expirationdate_year').disabled = true;
            document.getElementById('expirationdate_hour').disabled = true;
            document.getElementById('expirationdate_minute').disabled = true;
            var enable = 'false';
        }
    
        mysack.execute = 1;
        mysack.method = 'POST';
        mysack.setVar( "action", "expirationdate_ajax" );
        mysack.setVar( "enable", enable );
        mysack.encVar( "cookie", document.cookie, false );
        mysack.onError = function() { alert('Ajax error in looking up elevation' )};
        mysack.runAJAX();
    
        return true;
    }
    //]]>
    </script>
    <?php
    }
    add_action('admin_print_scripts', 'expirationdate_js_admin_header' );
    
    /**
     * Get correct URL (HTTP or HTTPS)
     */
    function expirationdate_get_blog_url() {
        global $current_blog;
        $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
    
        if (postExpirator_is_wpmu())
                echo $schema.$current_blog->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'];
    
        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');
                update_post_meta($id, 'expiration-date', $ts, true);
        } else {
            delete_post_meta($id, 'expiration-date');
        }
    }
    add_action('save_post','expirationdate_update_post_meta');
    
    /**
     * Build the menu for the options page
     */
    function postExpiratorMenuTabs($tab) {
            echo '<h2>'._('Post Expirator Options').'</h2>';
            echo '<p>';
            echo '<a href="'.admin_url('options-general.php?page=post-expirator.php&tab=general').'"'.(empty($tab) || $tab == 'general' ? ' style="font-weight: bold; text-decoration:none;"' : '').'>General Settings</a> | ';
            echo '<a href="'.admin_url('options-general.php?page=post-expirator.php&tab=upgrade').'"'.($tab == 'upgrade' ? ' style="font-weight: bold; text-decoration:none;"' : '').'>Upgrade</a>';
            echo '</p><hr/>';
    }
    
    /**
     *
     */
    function postExpiratorMenu() {
            $tab = $_GET['tab'];
    
        echo '<div class="wrap">';
        postExpiratorMenuTabs($tab);
        if (empty($tab) || $tab == 'general') {
            postExpiratorMenuGeneral();
        } elseif ($tab == 'upgrade') {
            postExpiratorMenuUpgrade();
        }
        echo '</div>';
    }
    
    /**
     * Hook's to add plugin page menu
     */
    function postExpiratorPluginMenu() {
        add_submenu_page('options-general.php','Post Expirator Options','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']);
                    echo "<div id='message' class='updated fade'><p>Saved Options!</p></div>";
        }
    
        // 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;
        }
    
        $expireddisplayfooter = get_option('expirationdateDisplayFooter');
        if (empty($expireddisplayfooter))
            $expireddisplayfooter = 0;
    
        $expireddisplayfooterenabled = '';
        $expireddisplayfooterdisabled = '';
        if ($expireddisplayfooter == 0)
            $expireddisplayfooterdisabled = 'checked="checked"';
        else if ($expireddisplayfooter == 1)
            $expireddisplayfooterenabled = 'checked="checked"';
    
        $expirationdateFooterContents = get_option('expirationdateFooterContents');
        if (empty($expirationdateFooterContents)) {
            global $expirationdateDefaultFooterContents;
            $expirationdateFooterContents = $expirationdateDefaultFooterContents;
        }
    
        $expirationdateFooterStyle = get_option('expirationdateFooterStyle');
        if (empty($expirationdateFooterStyle)) {
            global $expirationdateDefaultFooterStyle;
            $expirationdateFooterStyle = $expirationdateDefaultFooterStyle;
        }
    
        ?>
        <p>
        The post expirator plugin sets a custom meta value, and then optionally allows you to select if you want the post
        changed to a draft status or deleted when it expires.
        </p>
        <p>Valid [postexpiration] attributes:
        <ul>
            <li>type - defaults to full - valid options are full,date,time</li>
            <li>dateformat - format set here will override the value set on the settings page</li>
            <li>timeformat - format set here will override the value set on the settings page</li>
        </ul>
        </p>
        <form method="post" id="expirationdate_save_options">
            <h3>Defaults</h3>
            <table class="form-table">
                <tr valign-"top">
                    <th scope="row"><label for="expired-post-status">Set Post To:</label></th>
                    <td>
                        <select name="expired-post-status" id="expired-post-status">
                        <option<?php if ($expirationdateExpiredPostStatus == 'Draft'){ echo ' selected="selected"';}?>>Draft</option>
                        <option<?php if ($expirationdateExpiredPostStatus == 'Delete'){ echo ' selected="selected"';}?>>Delete</option>
                        </select>
                        <br/>
                        Select whether the post should be deleted or changed to a draft at expiration time.
                    </td>
                </tr>
                <tr valign-"top">
                    <th scope="row"><label for="expired-page-status">Set Page To:</label></th>
                    <td>
                        <select name="expired-page-status" id="expired-page-status">
                        <option<?php if ($expirationdateExpiredPageStatus == 'Draft'){ echo ' selected="selected"';}?>>Draft</option>
                        <option<?php if ($expirationdateExpiredPageStatus == 'Delete'){ echo ' selected="selected"';}?>>Delete</option>
                        </select>
                        <br/>
                        Select whether the page should be deleted or changed to a draft at expiration time.
                    </td>
                </tr>
                <tr valign-"top">
                    <th scope="row"><label for="expired-default-date-format">Date Format:</label></th>
                    <td>
                        <input type="text" name="expired-default-date-format" id="expired-default-date-format" value="<?php echo $expirationdateDefaultDateFormat ?>" size="25" /> (<?php echo date("$expirationdateDefaultDateFormat") ?>)
                        <br/>
                        The default format to use when displaying the expiration date within a post using the [postexpirator]
                        shortcode or within the footer.  For information on valid formatting options, see: <a href="http://us2.php.net/manual/en/function.date.php" target="_blank">PHP Date Function</a>.
                    </td>
                </tr>
                <tr valign-"top">
                    <th scope="row"><label for="expired-default-time-format">Time Format:</label></th>
                    <td>
                        <input type="text" name="expired-default-time-format" id="expired-default-time-format" value="<?php echo $expirationdateDefaultTimeFormat ?>" size="25" /> (<?php echo date("$expirationdateDefaultTimeFormat") ?>)
                        <br/>
                        The default format to use when displaying the expiration time within a post using the [postexpirator]
                        shortcode or within the footer.  For information on valid formatting options, see: <a href="http://us2.php.net/manual/en/function.date.php" target="_blank">PHP Date Function</a>.
                    </td>
                </tr>
            </table>
            <h3>Post Footer Display</h3>
            <p>Enabling this below will display the expiration date automatically at the end of any post which is set to expire.</p>
            <table class="form-table">
                <tr valign-"top">
                    <th scope="row">Show in post footer?</th>
                    <td>
                        <input type="radio" name="expired-display-footer" id="expired-display-footer-true" value="1" <?php echo $expireddisplayfooterenabled ?>/> <label for="expired-display-footer-true">Enabled</label>
                        <input type="radio" name="expired-display-footer" id="expired-display-footer-false" value="0" <?php echo $expireddisplayfooterdisabled ?>/> <label for="expired-display-footer-false">Disabled</label>
                        <br/>
                        This will enable or disable displaying the post expiration date in the post footer.
                    </td>
                </tr>
                <tr valign-"top">
                    <th scope="row"><label for="expired-footer-contents">Footer Contents:</label></th>
                    <td>
                        <textarea id="expired-footer-contents" name="expired-footer-contents" rows="3" cols="50"><?php echo $expirationdateFooterContents; ?></textarea>
                        <br/>
                        Enter the text you would like to appear at the bottom of every post that will expire.  The following placeholders will be replaced
                        with the post expiration date in the following format:
                        <ul>
                            <li>EXPIRATIONFULL -> <?php echo date("$expirationdateDefaultDateFormat $expirationdateDefaultTimeFormat") ?></li>
                            <li>EXPIRATIONDATE -> <?php echo date("$expirationdateDefaultDateFormat") ?></li>
                            <li>EXPIRATIONTIME -> <?php echo date("$expirationdateDefaultTimeFormat") ?></li>
                        </ul>
                    </td>
                </tr>
                <tr valign-"top">
                    <th scope="row"><label for="expired-footer-style">Footer Style:</label></th>
                    <td>
                        <input type="text" name="expired-footer-style" id="expired-footer-style" value="<?php echo $expirationdateFooterStyle ?>" size="25" />
                        (<span style="<?php echo $expirationdateFooterStyle ?>">This post will expire on <?php echo date("$expirationdateDefaultDateFormat $expirationdateDefaultTimeFormat"); ?></span>)
                        <br/>
                        The inline css which will be used to style the footer text.
                    </td>
                </tr>
            </table>
            <p class="submit">
                <input type="submit" name="expirationdateSave" value="Save" />
            </p>
        </form>
        <?php
    }
    
    function postExpiratorCronStatus() {
        $names = array('expirationdate_delete','expirationdate_delete_');
        // WPMU
        if (postExpirator_is_wpmu()) {
            global $current_blog;
            $names[] = 'expirationdate_delete_'.$current_blog->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 "<div id='message' class='updated fade'><p>Reset Cron Scheules!</p></div>";
        }
    
        $status = postExpiratorCronStatus();
        if ($status)
            $cronstatus = '<span style="color:green">OK</span>';
        else
            $cronstatus = '<span style="color:red">RESET NEEDED</span>';
    
        ?>
            <form method="post" id="postExpiratorMenuUpgrade">
                    <h3>Upgrade</h3>
                    <table class="form-table">
                            <tr valign-"top">
                                    <th scope="row"><label for="reset-cron-schedules">Reset Cron Schedules:</label></th>
                                    <td>
                        <input type="submit" name="reset-cron-schedules" id="reset-cron-schedules" value="Reset" />
                        Status: <?php echo $cronstatus; ?>
                                            <br/>
                        Resets the cron scheduler and removes any old or stray entries.
                                    </td>
                            </tr>
                    </table>
            </form>
        <?php
    }
    
    // [postexpirator format="l F jS, Y g:ia" tz="foo"]
    function postexpirator_shortcode($atts) {
        global $post;
    
            $expirationdatets = get_post_meta($post->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;
    
            $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 = '<p style="'.$expirationdateFooterStyle.'">'.str_replace($search,$replace,$expirationdateFooterContents).'</p>';
        return $text.$add_to_footer;
    }
    add_action('the_content','postexpirator_add_footer',0);

    Ahh, that works. Very nice. Now for extra credit….

    Any way to have it work on specific custom post types (instead of all of them)? I’m using a custom post type to manage events, which are basically future posts. The problem is that while my page template for events is coded to display future posts and ignore published posts (good), the events automatically change to ‘publish’ed on the day they occur, therefore disappearing from view (bad). I’d like them to disappear the day after the publish date.

    Extra-extra credit:
    Any way to have this happen automatically, instead of having to set an expiration date at the creation of each post? This probably negates the need for a plugin, I realize

    Hi oneprcnt,

    I am mental busy at present, this is not something I would be able to help with 🙁

    Sorry Bud,
    Best,
    Matt

    After a lot of trial and error, I was able to solve my problem. The solution actually eliminated the need for a plug-in altogether.

    My dashboard breaks when I load this in. Any idea why?

    To add it for a specific post type:

    /**
     * Add's hooks to get the meta box added to custom page types
     */
    function expirationdate_meta_custom() {
     $custom_post_types = get_post_types();
     foreach ($custom_post_types as $t) {
      if ($t == 'events') { // or whatever your post type is called
        add_meta_box('expirationdatediv', __('Post Expirator'), 'expirationdate_meta_box', $t, 'advanced', 'high');
       }
      }
    }
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘[Plugin: Post Expirator] Custom Post Type Support’ is closed to new replies.