get_settings(); // Config options add_action('admin_menu', array(&$this, 'wpadp_menu')); // Post-level settings if (PER_POST == $this->settings['PerPostOrSite']) { if ( version_compare($wp_version, '2.5.0', '>=')) { add_action("add_meta_boxes", array(&$this, "add_custom_meta_box")); } else { add_action('dbx_post_sidebar', array(&$this, 'wpadp_post_options')); } } add_action('save_post', array(&$this, 'save_post')); if (DELETE_POSTS == $this->settings['PostAction']) { // Delete hooks add_action('edit_post', array(&$this, 'wpadp_check_and_delete_posts')); add_action('publish_post', array(&$this, 'wpadp_check_and_delete_posts')); } else if (MOVE_POSTS == $this->settings['PostAction']) { // Move hooks add_action('edit_post', array(&$this, 'wpadp_check_and_move_posts')); add_action('publish_post', array(&$this, 'wpadp_check_and_move_posts')); } else if (ADD_CAT == $this->settings['PostAction']) { // Add Category hooks add_action('edit_post', array(&$this, 'wpadp_check_and_add_cats')); add_action('publish_post', array(&$this, 'wpadp_check_and_add_cats')); } } function add_custom_meta_box() { add_meta_box('adp-post-age', 'Per post expiration', array(&$this, 'custom_meta_box_markup'), 'post', 'side', 'low'); } /* get_settings */ /** * * * @return unknown */ function get_settings() { $this->defaultSettings = array( // Number of days for post to expire 'PostAge' => 14, // Per post or Site Wide operation 'PerPostOrSite' => SITE_WIDE, // Delete drafts or published posts 'DelPostType' => DEL_PUB_POSTS, // Delete, Move or Preview 'PostAction' => PREVIEW, // Delete category 'DeleteCategory' => array(0 => 1), // Move category 'MoveCategory' => 1, // Add category 'AddCategory' => 1, // Delete attachments 'DeleteAttachments' => DEL_ATTACHMENTS_NO ); if (!isset($this->settings)) { $this->settings = get_option('wpadp_options'); if (FALSE == $this->settings) { $this->settings = $this->defaultSettings; } else { $this->settings = array_merge($this->defaultSettings, $this->settings); } $this->sanitize_settings(); } return $this->settings; } /* save_settings */ /** * */ function save_settings() { $this->get_settings(); foreach ($this->defaultSettings as $k=>$v) { if (isset($_POST[$k]) && $_POST[$k] != 0) { $this->settings[$k] = $_POST[$k]; } } $this->sanitize_settings(); update_option('wpadp_options', $this->settings); } /* sanitize_settings */ /** * */ function sanitize_settings() { foreach (array_keys($this->settings) as $k) { $v = $this->settings[$k]; switch ($k) { case 'PostAge': case 'PostAction': case 'PerPostOrSite': case 'MoveCategory': case 'AddCategory': case 'DelPostType': case 'DeleteAttachments': $this->settings[$k] = (int) $v; break; case 'DeleteCategory': // This is an array, break; default: unset($this->settings[$k]); } } } /* Add menu page */ /** * */ function wpadp_menu() { add_options_page('Auto Delete Posts Options', 'Auto Delete Posts', 'manage_options', basename(__FILE__), array(&$this, 'wpadp_manage')); } /* Manage options in admin menu */ /** * */ function wpadp_manage() { global $wpdb; if ('POST' == $_SERVER['REQUEST_METHOD']) { $this->save_settings(); } else { $this->get_settings(); } require_once dirname(__FILE__) . '/auto-delete-posts.config.php'; } /** * * * @param unknown $postID */ function save_post($postID) { $this->get_settings(); if (PER_POST == $this->settings['PerPostOrSite']) { $postAge = (int)$_POST['adpPostAge']; if ($postAge == 0) { $postAge = $this->settings['PostAge']; } if (!update_post_meta($postID, '_auto_delete_posts', $postAge)) { add_post_meta($postID, '_auto_delete_posts', $postAge); } } } /* Check posts that need to be deleted. */ /** * */ function wpadp_check_and_delete_posts() { global $wpdb; global $wp_version; $this->get_settings(); $ids = $wpdb->get_results($this->gen_query()); if ($ids) { foreach ($ids as $id) { if ('' == $id->ID) { continue; } // If attachments are to be delete, do so first if (DEL_ATTACHMENTS_YES == $this->settings['DeleteAttachments']) { $args = array ( 'posts_per_page' => -1, 'order' => 'ASC', 'post_parent' => $id->ID, 'post_type' => 'attachment', ); $attachments = get_children($args); foreach ($attachments as $attachment) { if (version_compare($wp_version, '2.0.0', '>=')) { // This API was added in WP 2.0, so only use it on those WP versions that support // it. Also bypass trash to avoid clutter wp_delete_attachment($attachment->ID, true); } } } if (version_compare($wp_version, '2.9', ">=")) { // Starting with WP 2.9.0, a force_delete option was added to wp_delete_post to allow // posts to be sent to the trash for recovery as opposed to deleted entirely. Since // we don't want to clutter up the database with posts that we are automatically // asked to delete, let's go ahead and bypass the trash. wp_delete_post($id->ID, true); } else { wp_delete_post($id->ID); } } } } /* Check posts that need to be moved. */ /** * */ function wpadp_check_and_move_posts() { global $wpdb; $this->get_settings(); $expiration = $this->settings['PostAge']; $category = array($this->settings['MoveCategory']); /* * Create a query that will give us all the post ID's that predate the * interval we are given, that is, NOW - expiration date. */ $date = date('Y-m-d'); $query = "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_date < '$date' - INTERVAL ". $expiration." DAY"; $ids = $wpdb->get_results($query); foreach ($ids as $id) { if ('' == $id->ID) { continue; } wp_set_post_cats('', $id->ID, $category); } } /** * */ function wpadp_check_and_add_cats() { global $wpdb; $this->get_settings(); $expiration = $this->settings['PostAge']; $newCat = array($this->settings['AddCategory']); /* * Create a query that will give us all the post ID's that predate the * interval we are given, that is, NOW - expiration date. */ $query = "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_date < '$date' - INTERVAL ". $expiration." DAY"; $ids = $wpdb->get_results($query); foreach ($ids as $id) { if ('' == $id->ID) { continue; } $curCat = wp_get_post_cats('', $id->ID); // Merge the contents of the newCat array with the curCat array, each of these can contain // multiple values $curCat = array_merge($curCat, $newCat); wp_set_post_cats('', $id->ID, $curCat); } } function custom_meta_box_markup() { wp_nonce_field(basename(__FILE__), "meta-box-nonce"); global $post_ID; $postAge = get_post_meta($post_ID, '_auto_delete_posts', true); ?>