= 3.0 and tested with PHP Interpreter >= 5.3.1
*/
if ( ! class_exists( 'FB_Archive' ) ) {
class FB_Archive {
static private $classobj;
/*
* Key for textdomain
*
* @var string
*/
public $textdomain = 'archive';
/*
* Key for custom post type
*
* @var string
*/
public $post_type_1 = 'archiv';
/*
* Key for custom taxonomy
*
* @var string
*/
public $taxonomy_type_1 = 'archive_structure';
// set capabilities on roles
public $todo_roles = array(
'administrator'
, 'editor'
);
public $read_roles = array(
'author'
, 'contributor'
, 'subscriber'
);
/**
* Key for save the original post type
*
* @var string
*/
public $post_meta_key = '_archived_post_type'; // use underline for dont see in custom fields
/**
* Keys for view archive-link on defined screens
* Add Screen Id or not an array for view link on all screens
* @see http://codex.wordpress.org/Plugin_API/Admin_Screen_Reference
*/
public $def_archive_screens = ''; //array( 'edit-post', 'edit-page' );
/**
* Keys for view undo-archive-link on defined screens
*/
public $def_unset_screens = array( 'edit-archiv' );
/**
* Key for active Schedluling posts
*
* @var boolean
*/
public $scheduled_archiving = FALSE; // true or false
/**
* Key for days, there we archived posts
*
* @var integer
*/
public $scheduled_archiving_days = 21; // in days
/**
* Keys for scheduled posts types
*
* @var array
*/
public $scheduled_archiving_post_type = array( 'post' ); // example: 'post', 'page'
/**
* construct
*
* @uses add_filter, add_action, localize_plugin, register_activation_hook, register_uninstall_hook
* @access public
* @since 0.0.1
* @return void
*/
public function __construct () {
// include settings on profile
//require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'inc/class.settings.php';
//$fb_archive_settings = FB_Archive_Settings :: get_object();
// for WP 3.1 and higher
//add_filter( 'site_transient_update_plugins', array( &$this, 'remove_update_nag' ) );
// load language file
$this->localize_plugin();
// on activation of the plugin add cap to roles
register_activation_hook( __FILE__, array( &$this, 'on_activate' ) );
// on uninstall remove capability from roles
register_uninstall_hook( __FILE__, array('FB_Archive', 'on_deactivate' ) );
// add post type
add_action( 'init', array( &$this, 'build_post_type' ) );
// add scheduled archive
add_action( 'init', array( $this, 'schedule_archived_check' ) );
if ( (bool) $this->scheduled_archiving )
add_action( 'scheduled_archiving', array( $this, 'scheduled_archiving' ) );
add_action( 'admin_init', array( $this, 'add_settings_error' ) );
// admin interface init
add_action( 'admin_init', array( &$this, 'on_admin_init' ) );
add_action( 'admin_menu', array( $this, 'remove_menu_entry' ) );
// include js
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_script' ), 10, 1 );
// help on snippet-pages in next version
add_action( 'contextual_help', array( &$this, 'add_help_text' ), 10, 3 );
}
/**
* Return Textdomain string
*
* @access public
* @since 0.0.2
* @return string
*/
public function get_textdomain() {
return $this -> textdomain;
}
/**
* points the class
*
* @access public
* @since 0.0.1
* @return object
*/
public function get_object () {
if ( FALSE === self :: $classobj )
self :: $classobj = new self;
return self :: $classobj;
}
/**
* localize_plugin function.
*
* @uses load_plugin_textdomain, plugin_basename
* @access public
* @since 0.0.1
* @return void
*/
public function localize_plugin () {
load_plugin_textdomain( $this->textdomain, FALSE, dirname( plugin_basename(__FILE__) ) . '/languages' );
}
/**
* return plugin comment data
*
* @uses get_plugin_data
* @access public
* @since 0.0.1
* @param $value string, default = 'Version'
* Name, PluginURI, Version, Description, Author, AuthorURI, TextDomain, DomainPath, Network, Title
* @return string
*/
public function get_plugin_data( $value = 'Version' ) {
$plugin_data = get_plugin_data( __FILE__ );
$plugin_value = $plugin_data[$value];
return $plugin_value;
}
/**
* On activate plugin
*
* @uses deactivate_plugins, get_plugin_data, wp_sprintf, flush_rules
* @access public
* @since 0.0.1
* @return void
*/
public function on_activate () {
global $wp_roles, $wp_version;
// check wp version
if ( ! version_compare( $wp_version, '3.0', '>=' ) ) {
deactivate_plugins(__FILE__);
die(
wp_sprintf(
'%s: ' .
__( 'Sorry, This plugin requires WordPress 3.0+', $this->textdomain )
, self::get_plugin_data('Name' )
)
);
}
// check php version
if ( version_compare( PHP_VERSION, '5.2.0', '<' ) ) {
deactivate_plugins( __FILE__ ); // Deactivate ourself
die(
wp_sprintf(
'%1s: ' .
__( 'Sorry, This plugin has taken a bold step in requiring PHP 5.0+, Your server is currently running PHP %2s, Please bug your host to upgrade to a recent version of PHP which is less bug-prone. At last count, over 80%% of WordPress installs are using PHP 5.2+.', $this->textdomain )
, self::get_plugin_data('Name' ), PHP_VERSION
)
);
}
foreach ( $this->todo_roles as $role ) {
$wp_roles->add_cap( $role, 'edit_' . $this->post_type_1 );
$wp_roles->add_cap( $role, 'edit_' . $this->post_type_1 . 's' );
$wp_roles->add_cap( $role, 'edit_others_' . $this->post_type_1 . 's' );
$wp_roles->add_cap( $role, 'publish_' . $this->post_type_1 . 's' );
$wp_roles->add_cap( $role, 'read_' . $this->post_type_1 );
$wp_roles->add_cap( $role, 'read_private_' . $this->post_type_1 . 's' );
$wp_roles->add_cap( $role, 'delete_' . $this->post_type_1 );
$wp_roles->add_cap( $role, 'manage_' . $this->taxonomy_type_1 );
}
foreach ( $this->read_roles as $role ) {
$wp_roles->add_cap( $role, 'read_' . $this->post_type_1 );
$wp_roles->add_cap( $role, 'read_' . $this->post_type_1 );
$wp_roles->add_cap( $role, 'read_' . $this->post_type_1 );
}
flush_rewrite_rules();
}
/**
* On deactivate plugin remove capabilities
*
* @uses get_object, remove_cap, read_roles, flush_rules
* @access public
* @since 0.0.1
* @return void
*/
static function on_deactivate () {
$obj = FB_Archive::get_object();
global $wp_roles;
foreach ( $obj->todo_roles as $role ) {
$wp_roles->remove_cap( $role, 'edit_' . $obj->post_type_1 );
$wp_roles->remove_cap( $role, 'edit_' . $obj->post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'edit_others_' . $obj->post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'publish_' . $obj->post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'read_' . $obj->post_type_1 );
$wp_roles->remove_cap( $role, 'read_private_' . $obj->post_type_1 . 's' );
$wp_roles->remove_cap( $role, 'delete_' . $obj->post_type_1 );
$wp_roles->remove_cap( $role, 'manage_' . $obj->taxonomy_type_1 );
}
foreach ( $obj->read_roles as $role ) {
$wp_roles->remove_cap( $role, 'read_' . $obj->post_type );
$wp_roles->remove_cap( $role, 'read_' . $obj->post_type );
$wp_roles->remove_cap( $role, 'read_' . $obj->post_type );
}
flush_rewrite_rules();
}
/**
* Disable plugin update notifications
*
* @param unknown_type $value
* @since 0.0.1
* @link http://dd32.id.au/2011/03/01/disable-plugin-update-notification-for-a-specific-plugin-in-wordpress-3-1/
* @param array string $value
* @return array string $value
*/
public function remove_update_nag( $value) {
if ( isset( $value) && is_object( $value) )
unset( $value->response[ plugin_basename(__FILE__) ] );
return $value;
}
/**
* Return post type
*
* @uses get_post_type_object, get_post
* @access public
* @since 0.0.1
* @return string $post_type
*/
private function get_post_type () {
if ( !function_exists('get_post_type_object' ) )
return NULL;
if ( isset( $_GET['post']) )
$post_id = (int) $_GET['post'];
elseif ( isset( $_POST['post_ID']) )
$post_id = (int) $_POST['post_ID'];
else
$post_id = 0;
$post = NULL;
$post_type_object = NULL;
$post_type = NULL;
if ( $post_id ) {
$post = get_post( $post_id);
if ( $post ) {
$post_type_object = get_post_type_object( $post->post_type );
if ( $post_type_object ) {
$post_type = $post->post_type;
$current_screen->post_type = $post->post_type;
$current_screen->id = $current_screen->post_type;
}
}
} elseif ( isset( $_POST['post_type']) ) {
$post_type_object = get_post_type_object( $_POST['post_type'] );
if ( $post_type_object ) {
$post_type = $post_type_object->name;
$current_screen->post_type = $post_type;
$current_screen->id = $current_screen->post_type;
}
} elseif ( isset( $_SERVER['QUERY_STRING'] ) ) {
$post_type = esc_attr( $_SERVER['QUERY_STRING'] );
$post_type = str_replace( 'post_type=', '', $post_type );
}
return $post_type;
}
/**
* On admin init
*
* @uses wp_register_style, wp_enqueue_style, add_action, add_filter, add_meta_box
* @access public
* @since 0.0.1
* @return void
*/
public function on_admin_init () {
$post_type = $this->get_post_type();
wp_register_style( 'archive-page', plugins_url( 'css/settings.css', __FILE__ ) );
wp_register_style( 'archive-structure-page', plugins_url( 'css/structures.css', __FILE__ ) );
wp_register_style( 'archive-menu', plugins_url( 'css/menu.css', __FILE__ ) );
wp_enqueue_style( 'archive-menu' );
add_filter( 'post_row_actions', array( $this, 'add_archive_link' ), 10, 2 );
add_filter( 'page_row_actions', array( $this, 'add_archive_link' ), 10, 2 );
add_action( 'admin_action_archive', array( $this, 'archive_post_type' ) );
add_action( 'admin_notices', array( $this, 'get_admin_notices' ) );
// modify bulk actions - current not possible in WP
//add_filter( 'bulk_actions-edit-' . $this->post_type_1, array( $this, 'filter_bulk_actions' ) );
add_filter( 'post_row_actions', array( $this, 'add_unset_archive_link' ), 10, 2 );
add_action( 'admin_action_unset_archive', array( $this, 'unset_archive_post_type' ) );
$this->add_value_to_row();
if ( $this->post_type_1 == $post_type ) {
// add meta box with ID
add_meta_box( 'id',
__( 'Archive Info', $this->textdomain ),
array( &$this, 'additional_meta_box' ),
$this->post_type_1, 'side', 'high'
);
}
$defined_pages = array(
'archiv&page=archive_settings_group&settings-updated=true',
'archiv&page=archive_settings_group',
$this -> post_type_1
);
if ( in_array( $post_type, $defined_pages ) )
wp_enqueue_style( 'archive-page' );
elseif ( 'taxonomy=' . $this->taxonomy_type_1 . '&' . $this->post_type_1 == $post_type )
wp_enqueue_style( 'archive-structure-page' );
}
/**
* Enqueue scripts in WP
*
* @uses wp_enqueue_script
* @access public
* @since 0.0.1
* @param unknown_type $pagehook
* @return void
*/
public function enqueue_script( $pagehook ) {
if ( defined('WP_DEBUG') && WP_DEBUG && isset( $_GET['debug']) && $_GET['debug'] === 'true' )
echo '
Pagehook: ' . $pagehook .$post_type . '';
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : '';
$archive_pages = array( 'edit.php' );
$archive_post_type = array( $this->post_type_1, $this->post_type_1 . '&debug =true' );
if ( in_array( $pagehook, $archive_pages ) && in_array( $this->get_post_type(), $archive_post_type ) ) {
wp_enqueue_script(
'jquery-archive-script',
WP_PLUGIN_URL . '/' . dirname( plugin_basename(__FILE__) ) . '/js/script' . $suffix. '.js',
array( 'jquery' )
);
}
}
/**
* Schedule check
*
* @uses wp_schedule_event, wp_next_scheduled
* @access public
* @since 0.0.1
* @return void
*/
public function schedule_archived_check () {
if ( ! wp_next_scheduled('scheduled_archiving') && (bool) $this->scheduled_archiving ) {
wp_schedule_event( time(), 'twicedaily', 'scheduled_archiving' ); // hourly, daily and twicedaily
} elseif ( ! (bool) $this->scheduled_archiving && wp_next_scheduled('scheduled_archiving') ) {
wp_clear_scheduled_hook( 'scheduled_archiving' );
}
}
/**
* Add link on archive
*
* @uses get_post_type_object, get_archive_post_link, current_user_can, esc_attr
* @access public
* @since 0.0.1
* @param array string $actions
* @param integer $id
* @return array $actions
*/
public function add_archive_link( $actions, $id ) {
global $post, $current_screen, $mode;
$post_type_object = get_post_type_object( $post->post_type );
//var_dump( $current_screen);
if ( is_array( $this->def_archive_screens ) && ! in_array( $current_screen->id, $this->def_archive_screens ) )
return $actions;
if ( ! current_user_can( $post_type_object->cap->delete_post, $post->ID ) )
return $actions;
$actions['archive'] = '' . __( 'Archive', $this->textdomain ) . '';
return $actions;
}
/**
* Add undo-link on archive
*
* @uses get_post_type_object, current_user_can, get_post_meta, esc_attr
* @access public
* @since 0.0.1
* @param array string $actions
* @param integer $id
* @return array $actions
*/
public function add_unset_archive_link( $actions, $id ) {
global $post, $current_screen, $mode;
$post_type_object = get_post_type_object( $post->post_type );
if ( in_array( $current_screen->id, $this->def_unset_screens )
&& current_user_can( $post_type_object->cap->delete_post, $post->ID )
) {
$archived_post_type = get_post_meta( $id->ID, $this->post_meta_key, TRUE );
$actions['archive'] = '' . __( 'Restore to', $this->textdomain ) . ' ' . $archived_post_type . '';
}
return $actions;
}
/**
* Return link for archive post type
* Use filter get_archive_post_link for custom link
*
* @uses get_post, get_post_type_object, current_user_can, apply_filters, admin_url, wp_nonce_url
* @access public
* @since 0.0.1
* @param integer $id, Default is 0
* @return string
*/
public function get_archive_post_link( $id = 0 ) {
if ( ! $post = &get_post( $id ) )
return;
$post_type_object = get_post_type_object( $post->post_type );
if ( ! $post_type_object )
return;
if ( ! current_user_can( $post_type_object->cap->delete_post, $post->ID ) )
return;
$action = NULL;
$archive_link = admin_url( 'admin.php?post=' . $post->ID . '&action=archive' );
return apply_filters(
'get_archive_post_link',
wp_nonce_url( $archive_link, "$action-{$post->post_type}_{$post->ID}" ),
$post->ID
);
}
/**
* Return link for undo-archive post type
* Use filter get_unset_archive_post_link for custom link
*
* @uses get_post, get_post_type_object, current_user_can, apply_filters, admin_url, wp_nonce_url
* @access public
* @since 0.0.1
* @param integer $id, Default is 0
* @return string
*/
public function get_unset_archive_post_link( $id = 0 ) {
if ( !$post = &get_post( $id ) )
return;
$post_type_object = get_post_type_object( $post->post_type );
if ( !$post_type_object )
return;
if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) )
return;
$action = NULL;
$archive_link = admin_url( 'admin.php?post=' . $post->ID . '&action=unset_archive' );
$archived_post_type = get_post_meta( $id, $this->post_meta_key, TRUE );
return apply_filters(
'get_unset_archive_post_link',
wp_nonce_url( $archive_link, "$action-{$archived_post_type}_{$post->ID}" ),
$post->ID
);
}
public function filter_bulk_actions( $actions ) {
$actions['restore_archive'] = __( 'Restore to Post Type', $this->textdomain );
return $actions;
}
/**
* Archive post type
*
* @uses wp_die, set_post_type, add_post_meta, wp_redirect, admin_url
* @access public
* @since 0.0.1
* @return void
*/
public function archive_post_type () {
if ( ! (
isset( $_GET['post']) ||
( isset( $_REQUEST['action']) && 'archive' == $_REQUEST['action'] )
) ) {
wp_die( __( 'No post to archive has been supplied!', $this->textdomain ) );
}
$id = (int) ( isset( $_GET['post']) ? $_GET['post'] : $_REQUEST['post']);
if ( $id ) {
$redirect_post_type = '';
$archived_post_type = get_post_type( $id );
if ( ! empty( $archived_post_type ) )
$redirect_post_type = 'post_type=' . $archived_post_type . '&';
// change post type
set_post_type( $id, $this->post_type_1 );
// add old post_type to post meta
add_post_meta( $id, $this->post_meta_key, $archived_post_type, TRUE );
wp_redirect( admin_url( 'edit.php?' . $redirect_post_type . 'archived=1&ids=' . $id ) );
exit;
} else {
wp_die( __( 'Sorry, i cant find the post-id', $this->textdomain ) );
}
}
/**
* Undo archive post type
*
* @uses wp_die, set_post_type, delete_post_meta, wp_redirect, admin_url
* @access public
* @since 0.0.1
* @return void
*/
public function unset_archive_post_type () {
if ( ! (
isset( $_GET['post']) ||
( isset( $_REQUEST['action']) && 'unset_archive' == $_REQUEST['action'] )
) ) {
wp_die( __('No item to undo archive has been supplied!', $this->textdomain ) );
}
$id = (int) ( isset( $_GET['post']) ? $_GET['post'] : $_REQUEST['post']);
if ( $id ) {
$redirect_post_type = '';
// get archived post type
$archived_post_type = get_post_meta( $id, $this->post_meta_key, TRUE );
if ( ! empty( $archived_post_type ) )
$redirect_post_type = 'post_type=' . $archived_post_type . '&';
// change post type to archived post type
set_post_type( $id, $archived_post_type );
// remove archived post type on post meta
delete_post_meta( $id, $this->post_meta_key );
// redirect to edit-page od post type
wp_redirect( admin_url( 'edit.php?' . $redirect_post_type . 'unset_archived=1&ids=' . $id ) );
exit;
} else {
wp_die( __( 'Sorry, i cant find the post-id', $this->textdomain ) );
}
}
/**
* For the scheduled archiving
*
* @uses wp_die, set_post_type, delete_post_meta, wp_redirect, admin_url
* @access public
* @since 0.0.1
* @return void
*/
public function scheduled_archiving () {
global $wpdb;
$current = get_site_transient( 'archivise_posts' );
if ( ! is_object( $current) ) {
$current = new stdClass;
}
// Update last_checked for current to prevent multiple blocking requests if request hangs
$current->last_checked = time();
set_site_transient( 'archivise_posts', $current );
// convert post type array to string
$scheduled_archiving_post_types = NULL;
foreach( $this->scheduled_archiving_post_type as $item ){
$scheduled_archiving_post_types .= "'" . $item . "'" . ', ';
}
$scheduled_archiving_post_types = substr( $scheduled_archiving_post_types, 0, -strlen(', ') );
$archived = $wpdb->get_results(
"SELECT ID
FROM $wpdb->posts
WHERE post_type IN ( $scheduled_archiving_post_types)
AND post_date < '" . date( 'Y-m-d', strtotime('-' . (int) $this->scheduled_archiving_days .' days') ) . "'",
ARRAY_A
);
//var_dump( $archived);exit;
if ( is_wp_error( $archived ) )
return FALSE;
if ( ! $archived )
return FALSE;
foreach ( $archived as $value ) {
if ( $value['ID'] ) {
$archived_post_type = get_post_type( $value['ID'] );
// change post type
set_post_type( $value['ID'], $this->post_type_1 );
// add old post_type to post meta
add_post_meta( $value['ID'], $this->post_meta_key, $archived_post_type, TRUE );
}
}
$updates = new stdClass();
$updates->last_checked = time();
set_site_transient( 'archivise_posts', $updates );
}
/**
* Add Messages for admin notice
*
* @uses number_format_i18n, get_post_type, add_settings_error
* @access public
* @since 0.0.2
* @return void
*/
public function add_settings_error () {
$message_archived = NULL;
$message_unset_archived = NULL;
if ( isset( $_REQUEST['archived'] ) ) {
$message_archived = sprintf(
_n( 'Item moved to the Archive.',
'%s items moved to the Archive.',
$_REQUEST['archived'],
'', $this->textdomain ),
number_format_i18n( $_REQUEST['archived'] )
);
$ids = isset( $_REQUEST['ids']) ? $_REQUEST['ids'] : 0;
$message_archived .= ' ' . __( 'Undo' ) . '';
}
if ( isset( $_REQUEST['unset_archived'] ) ) {
$message_unset_archived = sprintf(
_n( 'Item moved to the Post Type: %2$s.',
'%1$s items moved to the Post Types: %2$s.',
$_REQUEST['unset_archived'],
'', $this->textdomain ),
number_format_i18n( $_REQUEST['unset_archived'] ),
'' . get_post_type( $_REQUEST['ids'] ) . ''
);
}
if ( isset( $_REQUEST['archived'] ) && (int) $_REQUEST['archived'] ) {
add_settings_error(
'archived_message',
'archived',
$message_archived,
'updated'
);
}
if ( isset( $_REQUEST['unset_archived'] ) && (int) $_REQUEST['unset_archived'] ) {
add_settings_error(
'unset_archived_message',
'unset_archived',
$message_unset_archived,
'updated'
);
}
}
/**
* Return Admin Notice for inform about actions
*
* @uses settings_errors
* @access public
* @since 0.0.1
* @return string
*/
public function get_admin_notices () {
settings_errors( 'archived_message' );
settings_errors( 'unset_archived_message' );
}
/**
* Admin post meta contents
*
* @uses get_post_meta
* @ccess public
* @since 0.0.1
* @param array $data
* @return string markup with post-id
*/
public function additional_meta_box( $data ) {
if ( $data->ID ) {
echo '
' . __( 'ID of this archived item:', $this->textdomain )
. ' ' . $data->ID . '
' . __( 'Archived Post Type:', $this->textdomain )
. ' ' . get_post_meta( $data->ID, $this->post_meta_key, TRUE ) . '
' . __( 'Archive - maybe later an help for this plugin', $this->textdomain ) . '
' . "\n"; return $contextual_help; } } // end class /** * init class with plugin */ $fb_archive = new FB_Archive(); } // end if class ?>