0

I’m looking for a way to put pages on archived statut after expiration of the publication date.

First, I add archived statut to the platform (code below). This statut appear if the page is published. If it is on draft or pending review, the statut not appear. Other element, if we put a published page on archived statut. We are redirected to listing page and we can access to archived page only with quick edit for changing his statut (we can not access the page directly).

Add Archived page statut code

<?php
define( 'ARCHIVED_POST_STATUS_VERSION', '0.3.7' );
define( 'ARCHIVED_POST_STATUS_PLUGIN', plugin_basename( __FILE__ ) );
define( 'ARCHIVED_POST_STATUS_DIR', plugin_dir_path( __FILE__ ) );
define( 'ARCHIVED_POST_STATUS_URL', plugin_dir_url( __FILE__ ) );
define( 'ARCHIVED_POST_STATUS_LANG_PATH', dirname( ARCHIVED_POST_STATUS_PLUGIN ) . '/languages' );

/**
 * Load languages.
 *
 * @action plugins_loaded
 */
function aps_i18n() {

    load_plugin_textdomain( 'archived-post-status', false, ARCHIVED_POST_STATUS_LANG_PATH );

}
add_action( 'plugins_loaded', 'aps_i18n' );

/**
 * Translations strings placeholder function.
 *
 * Translation strings that are not used elsewhere but
 * Plugin Title and Description are helt here to be
 * picked up by Poedit. Keep these in sync with the
 * actual plugin's title and description.
 */
function aps_i18n_strings() {

    __( 'Archived Post Status', 'archived-post-status' );
    __( 'Allows posts and pages to be archived so you can unpublish content without having to trash it.', 'archived-post-status' );

}

/**
 * Register a custom post status for Archived.
 *
 * @action init
 */
function aps_register_archive_post_status() {

    $args = array(
        'label'                     => __( 'Archived', 'archived-post-status' ),
        'public'                    => (bool) apply_filters( 'aps_status_arg_public', aps_current_user_can_view() ),
        'private'                   => (bool) apply_filters( 'aps_status_arg_private', true ),
        'exclude_from_search'       => (bool) apply_filters( 'aps_status_arg_exclude_from_search', ! aps_current_user_can_view() ),
        'show_in_admin_all_list'    => (bool) apply_filters( 'aps_status_arg_show_in_admin_all_list', aps_current_user_can_view() ),
        'show_in_admin_status_list' => (bool) apply_filters( 'aps_status_arg_show_in_admin_status_list', aps_current_user_can_view() ),
        'label_count'               => _n_noop( 'Archived <span class="count">(%s)</span>', 'Archived <span class="count">(%s)</span>', 'archived-post-status' ),
    );

    register_post_status( 'archive', $args );

}
add_action( 'init', 'aps_register_archive_post_status' );

/**
 * Check if we are on the frontend.
 *
 * @filter aps_status_arg_exclude_from_search
 *
 * @return bool
 */
function aps_is_frontend() {

    return ! is_admin();

}
add_filter( 'aps_status_arg_exclude_from_search', 'aps_is_frontend' );

/**
 * Check if the current user can view Archived content.
 *
 * @return bool
 */
function aps_current_user_can_view() {

    /**
     * Default capability to grant ability to view Archived content.
     *
     * @since 0.3.0
     *
     * @return string
     */
    $capability = (string) apply_filters( 'aps_default_read_capability', 'read_private_posts' );

    return current_user_can( $capability );

}

/**
 * Check if Archived content is read-only.
 *
 * @return bool
 */
function aps_is_read_only() {

    /**
     * Archived content is read-only by default.
     *
     * @since 0.3.5
     *
     * @return bool
     */
    return (bool) apply_filters( 'aps_is_read_only', true );

}

/**
 * Filter Archived post titles on the frontend.
 *
 * @param  string $title
 * @param  int    $post_id (optional)
 *
 * @return string
 */
function aps_the_title( $title, $post_id = null ) {

    $post = get_post( $post_id );

    if (
        ! is_admin()
        &&
        isset( $post->post_status )
        &&
        'archive' === $post->post_status
    ) {

        $title = sprintf( '%s: %s', __( 'Archived', 'archived-post-status' ), $title );

    }

    return $title;

}
add_filter( 'the_title', 'aps_the_title', 10, 2 );

/**
 * Check if a post type should NOT be using the Archived status.
 *
 * @param  string $post_type
 *
 * @return bool
 */
function aps_is_excluded_post_type( $post_type ) {

    /**
     * Prevent the Archived status from being used on these post types.
     *
     * @since 0.1.0
     *
     * @return array
     */
    $excluded = (array) apply_filters( 'aps_excluded_post_types', array( 'attachment' ) );

    return in_array( $post_type, $excluded );

}

/**
 * Modify the DOM on post screens.
 *
 * @action admin_footer-post.php
 */
function aps_post_screen_js() {

    global $post;

    if ( aps_is_excluded_post_type( $post->post_type ) ) {

        return;

    }

    if ( 'draft' !== $post->post_status && 'pending' !== $post->post_status ) {

        ?>
        <script>
        jQuery( document ).ready( function( $ ) {
            $( '#post_status' ).append( '<option value="archive"><?php esc_html_e( 'Archived', 'archived-post-status' ) ?></option>' );
        } );
        </script>
        <?php

    }

    if ( 'archive' === $post->post_status ) {

        ?>
        <script>
        jQuery( document ).ready( function( $ ) {
            $( '#post-status-display' ).text( '<?php esc_html_e( 'Archived', 'archived-post-status' ) ?>' );
        } );
        </script>
        <?php

    }

}
add_action( 'admin_footer-post.php', 'aps_post_screen_js' );

/**
 * Modify the DOM on edit screens.
 *
 * @action admin_footer-edit.php
 */
function aps_edit_screen_js() {

    global $typenow;

    if ( aps_is_excluded_post_type( $typenow ) ) {

        return;

    }

    ?>
    <script>
    jQuery( document ).ready( function( $ ) {
    <?php if ( aps_is_read_only() ) : ?>
        $rows = $( '#the-list tr.status-archive' );

        $.each( $rows, function() {
            disallowEditing( $( this ) );
        } );
    <?php endif; ?>

        $( 'select[name="_status"]' ).append( '<option value="archive"><?php esc_html_e( 'Archived', 'archived-post-status' ) ?></option>' );

        $( '.editinline' ).on( 'click', function() {
            var $row        = $( this ).closest( 'tr' ),
                $option     = $( '.inline-edit-row' ).find( 'select[name="_status"] option[value="archive"]' ),
                is_archived = $row.hasClass( 'status-archive' );

            $option.prop( 'selected', is_archived );
        } );

    <?php if ( aps_is_read_only() ) : ?>
        $( '.inline-edit-row' ).on( 'remove', function() {
            var id   = $( this ).prop( 'id' ).replace( 'edit-', '' ),
                $row = $( '#post-' + id );

            if ( $row.hasClass( 'status-archive' ) ) {
                disallowEditing( $row );
            }
        } );

        function disallowEditing( $row ) {
            var title = $row.find( '.column-title a.row-title' ).text();

            $row.find( '.column-title a.row-title' ).replaceWith( title );
            $row.find( '.row-actions .edit' ).remove();
        }
    <?php endif; ?>
    } );
    </script>
    <?php

}
add_action( 'admin_footer-edit.php', 'aps_edit_screen_js' );

/**
 * Prevent Archived content from being edited.
 *
 * @action load-post.php
 */
function aps_load_post_screen() {

    if ( ! aps_is_read_only() ) {

        return;

    }

    $post_id = (int) filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
    $post    = get_post( $post_id );

    if (
        is_null( $post )
        ||
        aps_is_excluded_post_type( $post->post_type )
        ||
        'archive' !== $post->post_status
    ) {

        return;

    }

    $action  = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_STRING );
    $message = (int) filter_input( INPUT_GET, 'message', FILTER_SANITIZE_NUMBER_INT );

    // Redirect to list table after saving as Archived
    if ( 'edit' === $action && 1 === $message ) {

        wp_safe_redirect(
            add_query_arg(
                'post_type',
                $post->post_type,
                self_admin_url( 'edit.php' )
            ),
            302
        );

        exit;

    }

    wp_die(
        __( "You can't edit this item because it has been Archived. Please change the post status and try again.", 'archived-post-status' ),
        translate( 'WordPress &rsaquo; Error' )
    );

}
add_action( 'load-post.php', 'aps_load_post_screen' );

/**
 * Display custom post state text next to post titles that are Archived.
 *
 * @filter display_post_states
 *
 * @param  array   $post_states
 * @param  WP_Post $post
 *
 * @return array
 */
function aps_display_post_states( $post_states, $post ) {

    if (
        aps_is_excluded_post_type( $post->post_type )
        ||
        'archive' !== $post->post_status
        ||
        'archive' === get_query_var( 'post_status' )
    ) {

        return $post_states;

    }

    return array_merge(
        $post_states,
        array(
            'archive' => __( 'Archived', 'archived-post-status' ),
        )
    );

}
add_filter( 'display_post_states', 'aps_display_post_states', 10, 2 );

/**
 * Close comments and pings when content is Archived.
 *
 * @action save_post
 *
 * @param int     $post_id
 * @param WP_Post $post
 * @param bool    $update
 */
function aps_save_post( $post_id, $post, $update ) {

    if (
        aps_is_excluded_post_type( $post->post_type )
        ||
        wp_is_post_revision( $post )
    ) {

        return;

    }

    if ( 'archive' === $post->post_status ) {

        // Unhook to prevent infinite loop
        remove_action( 'save_post', __FUNCTION__ );

        $args = array(
            'ID'             => $post->ID,
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
        );

        wp_update_post( $args );

        // Add hook back again
        add_action( 'save_post', __FUNCTION__, 10, 3 );

    }

}
add_action( 'save_post', 'aps_save_post', 10, 3 );

The second code below is about the expiration of pages. Right now after the expiration of page. it change to draft statut. my GOAL is to put it in archived statut if the expiration date arrived. and if a try to change change date on a published page with an old date. make the page as archived and redirected to listing pages. I try to change draft statut on the code but it doesn't change anything.

Code expiration post/page

<?php

// ADD OPTIONS PAGE PHP PAGE STYLE
class DropdownOptionSetting {
    private $hugu_ped_setting_options;

    public function __construct() {
        add_action( 'admin_menu', array( $this, 'hugu_ped_setting_add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'hugu_ped_setting_page_init' ) );
    }

    public function hugu_ped_setting_add_plugin_page() {
        add_options_page(
            'Post Expiration', // page_title
            'Post Expiration', // menu_title
            'manage_options', // capability
            'hugu_ped_option_setting', // menu_slug
            array( $this, 'hugu_ped_setting_create_admin_page' ) // function
        );
    }

//CONSTRUCT THE OPTION PAGE
    public function hugu_ped_setting_create_admin_page() {
        $this->hugu_ped_setting_options = get_option( 'hugu_ped_setting_option_name' ); ?>

        <div class="wrap">
            <h2>Post Expiration Date Settings</h2>
            <p></p>
            <!-- ?php settings_errors(); ? NOT NEEDED -->
            <form method="post" action="options.php">
                <?php
                    settings_fields( 'hugu_ped_setting_option_group' );
                    do_settings_sections( 'hugu_ped-setting-admin' );
                    submit_button();
                ?>
            </form>

            <hr>

<form action="https://huguetteinc.us19.list-manage.com/subscribe/post?u=5e4a771ae0566de48ad8da160&amp;id=901a41d61b" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>

<h2>Join our mailing list for news and updates</h2>

<div id="mc_embed_signup">
    <div id="mc_embed_signup_scroll">
<div class="mc-field-group">
    <label for="mce-EMAIL">Email Address: </label>
    <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>
    <div id="mce-responses" class="clear">
        <div class="response" id="mce-error-response" style="display:none"></div>
        <div class="response" id="mce-success-response" style="display:none"></div>
    </div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_5e4a771ae0566de48ad8da160_901a41d61b" tabindex="-1" value=""></div>
    <div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
    </div>


    <p><i>Sign up and you can receive periodic updates about this plugin. We'll never spam you, but we will give you a chance to provide feedback that will play a part in the future of this plugin.</i></p>

<p><i><a href="https://huguetteinc.com">Huguette Inc.</a></i></p>

</form>
            </div>
        </div>
    <?php }

    public function hugu_ped_setting_page_init() {
        register_setting(
            'hugu_ped_setting_option_group', // option_group
            'hugu_ped_setting_option_name', // option_name
            array( $this, 'hugu_ped_setting_sanitize' ) // sanitize callback
        );

        add_settings_section(
            'hugu_ped_setting_setting_section', // id
            'Settings', // title
            array( $this, 'hugu_ped_setting_section_info' ), // callback
            'hugu_ped-setting-admin' // page
        );

        add_settings_field(
            'hugu_ped_0', // id
            'DateTime Selector:', // title
            array( $this, 'hugu_ped_0_callback' ), // callback
            'hugu_ped-setting-admin', // page
            'hugu_ped_setting_setting_section' // section
        );
    }

    public function hugu_ped_setting_sanitize($input) {
        $sanitary_values = array();
        if ( isset( $input['hugu_ped_0'] ) ) {
            $sanitary_values['hugu_ped_0'] = $input['hugu_ped_0'];
        }

        return $sanitary_values;
    }

    public function hugu_ped_setting_section_info() {

    }
//BUILD THE DROPDOWN
//OPTION 3 HIDDEN FOR NOW!
    public function hugu_ped_0_callback() {
        ?> <select name="hugu_ped_setting_option_name[hugu_ped_0]" id="hugu_ped_0">
            <?php $selected = (isset( $this->hugu_ped_setting_options['hugu_ped_0'] ) && $this->hugu_ped_setting_options['hugu_ped_0'] === 'option-one') ? 'selected' : '' ; ?>
            <option value="option-one" <?php echo $selected; ?>>Universal</option>
            <?php $selected = (isset( $this->hugu_ped_setting_options['hugu_ped_0'] ) && $this->hugu_ped_setting_options['hugu_ped_0'] === 'option-two') ? 'selected' : '' ; ?>
            <option value="option-two" <?php echo $selected; ?>>Native</option>
            <?php $selected = (isset( $this->hugu_ped_setting_options['hugu_ped_0'] ) && $this->hugu_ped_setting_options['hugu_ped_0'] === 'option-three') ? 'selected' : '' ; ?>
            <option value="option-three" style="display: none;"<?php echo $selected; ?>>Option Three</option>
        </select> <?php
    }

}
if ( is_admin() )
    $hugu_ped_setting = new DropdownOptionSetting();

//
//BACK TO VERIONS <=1.1 STUFF
// INCLUDE TIMEDATE PICKER JQUERY IN ADMIN AREA
function HUGU_include_jquery() {

    wp_register_script('flatpickr', 'https://cdn.jsdelivr.net/npm/flatpickr');
    wp_enqueue_script( 'flatpickr' );

    wp_register_style('flatpickrStyle', 'https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css', 'all');
    wp_enqueue_style('flatpickrStyle');
}
add_action('admin_head', 'HUGU_include_jquery');

// ADD LINKS ON THE PLUGIN PAGE
function hugu_ped_plugin_action_links($links, $file) {
    $this_plugin = basename(plugin_dir_url(__FILE__)) . '/post-expiration-date.php';
    if($file == $this_plugin) {
        $links[] = '<a href="options-general.php?page=hugu_ped_option_setting">' . __('Settings', 'post-expiration-date') . '</a>';
    }
    return $links;
}
add_filter('plugin_action_links', 'hugu_ped_plugin_action_links', 10, 2);

// ADD THE METABOX TO THE EDIT POSTS PAGE
function hugu_ped_add_expire_date_metabox() {
    //ADD CUSTOM POST TYPES HERE
    $screens = array( 'post', 'page');

    foreach ( $screens as $screen ) {
    add_meta_box( 'hugu_ped_expire_date_metabox', __( 'Expiration Date', 'hugu'), 'hugu_ped_expire_date_metabox_callback', $screen, 'side', 'high' );
}
}
add_action( 'add_meta_boxes', 'hugu_ped_add_expire_date_metabox' );

// THIS IS THE CALLBACK FUNCTION FOR THE METABOX
function hugu_ped_expire_date_metabox_callback( $post ) { ?>

    <form action="" method="post">

    <?php       
        // THE NONCE
        wp_nonce_field( 'hugu_ped_expire_date_metabox_nonce', 'hugu_nonce' );

        //CHECK FOR METADATA
        $hugu_expire_date = get_post_meta( $post->ID, 'expires', true );
        ?>

        <label for "hugu_expire_date"><?php __('expire Date', 'hugu' ); ?></label>

        <script type="text/javascript">
    jQuery(document).ready(function($){
        $('#HUGUdatetime').flatpickr({
            altInput: true,
            altFormat: "M j, Y @ H:i",
            enableTime: true,
            dateFormat: 'Y-m-d\\TH:i'
        });
        $(".clear_button").click(function() {
            $('#HUGUdatetime').flatpickr().clear();
            $('#HUGUdatetime').attr('value', '');
        });

    });
</script>

<?php
//function dateTimePost(){      
    $dropdown_option = get_option( 'hugu_ped_setting_option_name' ); // Array
    $dropdown_value =  $dropdown_option['hugu_ped_0']; // Option value
            if ($dropdown_value === 'option-two') {
                echo'<input id="date" type="datetime-local" class="MyDate" name="hugu_expire_date" value="' . $hugu_expire_date . '"/>';
            } else {
                echo '<input type="text" id="HUGUdatetime" class="MyDate" name="hugu_expire_date" value="' . $hugu_expire_date . '"/><a class="clear_button hide-if-no-js button-cancel" title="clear" data-clear style="text-decoration:underline;">Clear</a>';
            }   
    ?>
    </form>

<?php }

// SAVE THE DATETIME FROM THE METABOX
function hugu_ped_save_expire_date_meta( $post_id ) {

// NONCE EXISTS?
    if( !isset( $_POST['hugu_nonce'] ) ||
        !wp_verify_nonce( $_POST['hugu_nonce'],
        'hugu_ped_expire_date_metabox_nonce'
        ) ) {
        return;
    }

    // CHECK FOR USER PERMISSION
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;

    if ( !empty( $_POST['hugu_expire_date'] ) ) {
        //CHECK THE FORMAT BEFORE SUBMIT
        if(hugu_ped_validateDate($_POST['hugu_expire_date'])){
            // if expires time is in the past, set post to draft 
            // and don't save the date

            $expires_epoch = strtotime($_POST['hugu_expire_date']);
            if($expires_epoch < time()){
                remove_action( 'save_post', 'hugu_ped_save_expire_date_meta' );
                wp_update_post(array(
                    'ID' => $post_id,
                    'post_status' => 'draft',
                ));
                delete_post_meta( $post_id, 'expires' );
                add_action( 'save_post', 'hugu_ped_save_expire_date_meta' );
            }
            else {
                // FORMAT CORRECT? UPDATE. ELSE CLEAR META DATA
                update_post_meta( $post_id, 'expires', $_POST['hugu_expire_date']);                 
            }
        } else {
            delete_post_meta( $post_id, 'expires' );  //If you remove the expiration date in the form, it will remove also from the meta
        }
        } else{
            delete_post_meta($post_id, 'expires');
    }


}
add_action( 'save_post', 'hugu_ped_save_expire_date_meta' );

function hugu_ped_validateDate($date)
{
    try {
        $dateTime = new DateTime($date);
    }
    catch(Exception $e){
        return false;
    }
    return $dateTime !== false;
}

function hugu_ped_wpdb_query(){
    // CODE COMPETITION METHOD
    global $wpdb;

    // check if there are expirable posts
    $expirable_posts = $wpdb->get_results("
        SELECT * from {$wpdb->prefix}postmeta
        WHERE {$wpdb->prefix}postmeta.meta_key='expires' AND {$wpdb->prefix}postmeta.meta_value <= NOW()
        ");

    if(!empty($expirable_posts)){

        $wpdb->query("
            UPDATE {$wpdb->prefix}posts 
            JOIN {$wpdb->prefix}postmeta
            ON {$wpdb->prefix}postmeta.post_id={$wpdb->prefix}posts.ID 
            SET {$wpdb->prefix}posts.post_status= 'draft' 
            WHERE {$wpdb->prefix}postmeta.meta_key='expires' AND {$wpdb->prefix}postmeta.meta_value <= NOW()
            ");

        $wpdb->query("
            DELETE FROM {$wpdb->prefix}postmeta 
            WHERE meta_key='expires' AND meta_value < NOW()
        "); 
    }

}
add_action( 'wp_loaded', 'hugu_ped_wpdb_query' ); 

// ADD COLUMN TO EDIT.PHP //////////
// CREATE THE COLUMNS
add_filter('manage_posts_columns', 'hugu_ped_my_columns');
function hugu_ped_my_columns($columns) {
    $columns['expires'] = 'Exp. Date';
    return $columns;
}
add_filter('manage_pages_columns', 'hugu_ped_my_pages_columns');
function hugu_ped_my_pages_columns($columnsPage) {
    $columnsPage['expires'] = 'Exp. Date';
    return $columnsPage;
}
//POPULATE THE COLUMNS
add_action('manage_posts_custom_column',  'hugu_ped_my_show_columns');
function hugu_ped_my_show_columns($name) {
    global $post;
    switch ($name) {
        case 'expires':
            $eventDate = get_post_meta( $post->ID, 'expires', true );
            if(!empty($eventDate)){
                echo date('Y/m/d H:i', strtotime($eventDate));
            }
            else {
                echo '&mdash;';
            }
    }
}
add_action('manage_pages_custom_column',  'hugu_ped_my_show_pages_columns');
function hugu_ped_my_show_pages_columns($name) {
    global $post;
    switch ($name) {
        case 'expires':
            $eventDateP = get_post_meta( $post->ID, 'expires', true );
            if(!empty($eventDateP)){
                echo date('Y/m/d H:i', strtotime($eventDateP));
            }
            else {
                echo '&mdash;';
            }
    }
}
/////////COLUMN CREATION FINISHED///////

if (is_admin()) {
    //register_activation_hook(__FILE__, 'activate_HUGU_easyCDS');
    //register_deactivation_hook(__FILE__, 'deactive_HUGU_easyCDS');
    //add_action('admin_init', 'admin_init_HUGU_easyCDS');
    //add_action('admin_menu', 'admin_menu_hugu_ped');
    //add_action( 'admin_init', 'hugu_ped_register_settings' );
}

?>

Hope someone can HELP. Thanks.

0

You could create a function with a query that gets all posts older than a certain date, loop through the posts and set your archive variable. If you call that function with a custom action hook like this add_action( 'my_daily_archive_cronjob', 'archive_old_pages' ) you can trigger it daily with this plugin: WP Crontrol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.