Post Dates.
*
* @package Bulk_Post_Date_Change
*/
defined( 'ABSPATH' ) || exit;
final class QBPD_Admin {
const SLUG = 'redate';
const SITE_URL = 'https://quintaphon.com/wordpress/redate/';
/** The mark: a calendar with an arrow moving forward. Brand colours #21B68E and #364058. */
const MARK = 'M10.6 6h2.4v2.6h6V6h2.4v2.6H25V25H7V8.6h3.6ZM9.4 13.4v9.2h13.2v-9.2ZM12 17.2h4.4v-2.4l4 3.6-4 3.6v-2.4H12Z';
private static $hook = '';
public static function init() {
add_action( 'admin_menu', array( __CLASS__, 'menu' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) );
add_filter( 'plugin_action_links_' . plugin_basename( QBPD_FILE ), array( __CLASS__, 'action_links' ) );
}
public static function menu() {
self::$hook = add_management_page( 'Redate', __( 'Post Dates', 'redate' ), QBPD_Job::CAP, self::SLUG, array( __CLASS__, 'page' ) );
}
public static function action_links( $links ) {
array_unshift( $links, '' . esc_html__( 'Open', 'redate' ) . ' ' );
return $links;
}
/**
* Link to a screen of the plugin.
*
* @param array $args Query arguments.
* @return string
*/
public static function url( array $args = array() ) {
return add_query_arg( array_map( 'rawurlencode', array_merge( array( 'page' => self::SLUG ), $args ) ), admin_url( 'tools.php' ) );
}
public static function assets( $hook ) {
if ( ! self::$hook || self::$hook !== $hook ) {
return;
}
wp_enqueue_style( 'qbpd-admin', QBPD_URL . 'assets/admin.css', array( 'dashicons' ), QBPD_VERSION . '.' . filemtime( QBPD_DIR . 'assets/admin.css' ) );
wp_enqueue_script( 'qbpd-admin', QBPD_URL . 'assets/admin.js', array( 'wp-i18n' ), QBPD_VERSION . '.' . filemtime( QBPD_DIR . 'assets/admin.js' ), true );
wp_set_script_translations( 'qbpd-admin', 'redate' );
wp_add_inline_script(
'qbpd-admin',
'window.QBPD = ' . wp_json_encode(
array(
'ajax' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'qbpd' ),
'history' => self::url( array( 'tab' => 'history' ) ),
)
) . ';',
'before'
);
}
/* ------------------------------------------------------------------
* Building blocks
* ------------------------------------------------------------------ */
private static function logo( $size = 34 ) {
return sprintf(
' ',
(int) $size,
self::MARK
);
}
private static function svg_html() {
return array(
'svg' => array(
'viewbox' => true,
'width' => true,
'height' => true,
'aria-hidden' => true,
),
'rect' => array(
'width' => true,
'height' => true,
'rx' => true,
'fill' => true,
),
'path' => array(
'd' => true,
'fill' => true,
'fill-rule' => true,
),
);
}
private static function the_icon( $name ) {
echo ' ';
}
private static function card_start( $title, $desc = '', $tools = '', $id = '' ) {
echo '' . esc_html( $title ) . ' ';
if ( $desc ) {
echo '
' . wp_kses( $desc, array( 'code' => array(), 'strong' => array() ) ) . '
';
}
echo '
';
if ( $tools ) {
echo '' . wp_kses( $tools, array( 'a' => array( 'class' => true, 'href' => true ) ) ) . '
';
}
echo ' ';
}
private static function stat( $label, $value, $sub = '', $tone = '' ) {
echo '' . esc_html( $label ) . ' ' . esc_html( $value ) . ' ';
if ( $sub ) {
echo '' . esc_html( $sub ) . ' ';
}
echo '
';
}
private static function toggle_row( $id, $title, $help, $on, $row_id = '' ) {
?>
$label ) {
echo '' . esc_html( $label ) . ' ';
}
}
/**
* Taxonomies of the post types the plugin works on.
*
* @return array name => "Label (Post type)"
*/
private static function taxonomies() {
$types = array_keys( QBPD_Job::post_types() );
$out = array();
foreach ( get_taxonomies( array( 'show_ui' => true ), 'objects' ) as $t ) {
$on = array_values( array_intersect( (array) $t->object_type, $types ) );
if ( ! $on || 'post_format' === $t->name ) {
continue;
}
$type = get_post_type_object( $on[0] );
if ( $type && 'post' !== $on[0] ) {
/* translators: 1: taxonomy name, 2: post type name. */
$out[ $t->name ] = sprintf( __( '%1$s (%2$s)', 'redate' ), $t->labels->name, $type->labels->name );
} else {
$out[ $t->name ] = $t->labels->name;
}
}
return $out;
}
/**
* Users who wrote posts, by name.
*
* @return array ID => display name
*/
private static function authors() {
$users = get_users(
array(
'has_published_posts' => array_keys( QBPD_Job::post_types() ),
'number' => 300,
'orderby' => 'display_name',
'fields' => array( 'ID', 'display_name' ),
)
);
$out = array();
foreach ( $users as $u ) {
$out[ (int) $u->ID ] = $u->display_name;
}
return $out;
}
/**
* Short description of a run's or schedule's filters.
*
* @param array $f Filters.
* @return string
*/
public static function describe_filters( array $f ) {
if ( 'selection' === $f['scope'] ) {
$n = count( $f['ids'] );
/* translators: %s: number of posts. */
return sprintf( _n( '%s selected post', '%s selected posts', $n, 'redate' ), number_format_i18n( $n ) );
}
$types = QBPD_Job::post_types();
$labels = QBPD_Job::status_labels();
$bits = array();
$bits[] = implode( ', ', array_map( function ( $t ) use ( $types ) {
return isset( $types[ $t ] ) ? $types[ $t ] : $t;
}, $f['types'] ) );
$bits[] = strtolower( implode( ', ', array_map( function ( $s ) use ( $labels ) {
return isset( $labels[ $s ] ) ? $labels[ $s ] : $s;
}, $f['statuses'] ) ) );
if ( $f['terms'] ) {
$names = array();
foreach ( array_slice( $f['terms'], 0, 3 ) as $tid ) {
$t = get_term( $tid, $f['tax'] );
if ( $t && ! is_wp_error( $t ) ) {
$names[] = $t->name;
}
}
$more = count( $f['terms'] ) - 3;
if ( $more > 0 ) {
/* translators: 1: term names, 2: number of further terms. */
$bits[] = sprintf( _n( 'in %1$s and %2$s more', 'in %1$s and %2$s more', $more, 'redate' ), implode( ', ', $names ), number_format_i18n( $more ) );
} else {
/* translators: %s: term names. */
$bits[] = sprintf( __( 'in %s', 'redate' ), implode( ', ', $names ) );
}
}
if ( $f['author'] ) {
$u = get_userdata( $f['author'] );
/* translators: %s: author name. */
$bits[] = sprintf( __( 'by %s', 'redate' ), $u ? $u->display_name : '#' . $f['author'] );
}
if ( $f['pub_from'] || $f['pub_to'] ) {
/* translators: 1: first date (or "start"), 2: last date (or "now"). */
$bits[] = sprintf( __( 'published %1$s to %2$s', 'redate' ), $f['pub_from'] ? $f['pub_from'] : __( 'start', 'redate' ), $f['pub_to'] ? $f['pub_to'] : __( 'now', 'redate' ) );
}
if ( $f['stale_days'] ) {
/* translators: %s: number of days. */
$bits[] = sprintf( _n( 'not modified for %s day', 'not modified for %s days', (int) $f['stale_days'], 'redate' ), number_format_i18n( (int) $f['stale_days'] ) );
}
if ( '' !== $f['search'] ) {
/* translators: %s: text the post titles contain. */
$bits[] = sprintf( __( '"%s"', 'redate' ), $f['search'] );
}
if ( $f['only_ids'] ) {
$n = count( $f['only_ids'] );
/* translators: %s: number of post IDs. */
$bits[] = sprintf( _n( '%s listed ID', '%s listed IDs', $n, 'redate' ), number_format_i18n( $n ) );
}
if ( $f['limit'] ) {
$orders = array(
'oldest' => __( 'oldest', 'redate' ),
'newest' => __( 'newest', 'redate' ),
'stale' => __( 'least recently modified', 'redate' ),
'random' => __( 'random', 'redate' ),
);
/* translators: 1: most posts, 2: which posts are taken first (oldest, newest, ...). */
$bits[] = sprintf( __( 'at most %1$s, %2$s first', 'redate' ), number_format_i18n( $f['limit'] ), $orders[ $f['order'] ] );
}
return implode( ' · ', $bits );
}
/* ------------------------------------------------------------------
* Page
* ------------------------------------------------------------------ */
private static function tabs() {
$tabs = array(
'overview' => array( __( 'Overview', 'redate' ), 'dashboard', __( 'Change the dates of many posts at once, safely.', 'redate' ) ),
'change' => array( __( 'Change dates', 'redate' ), 'calendar-alt', __( 'Pick the posts, choose the new dates, preview and apply.', 'redate' ) ),
'schedules' => array( __( 'Schedules', 'redate' ), 'clock', __( 'Changes that run by themselves every day, week or month.', 'redate' ) ),
'history' => array( __( 'History', 'redate' ), 'backup', __( 'Every change made, with an undo.', 'redate' ) ),
);
if ( current_user_can( 'manage_options' ) ) {
$tabs['settings'] = array( __( 'Settings', 'redate' ), 'admin-generic', __( 'History, batch size and other plugins.', 'redate' ) );
}
return $tabs;
}
public static function page() {
if ( ! current_user_can( QBPD_Job::CAP ) ) {
return;
}
QBPD_Store::cleanup();
$tabs = self::tabs();
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- navigation only.
$tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : '';
$sel = isset( $_GET['sel'] ) ? sanitize_key( wp_unslash( $_GET['sel'] ) ) : '';
// phpcs:enable
if ( '' === $tab ) {
$tab = $sel ? 'change' : 'overview';
}
$tab = isset( $tabs[ $tab ] ) ? $tab : 'overview';
$runs = QBPD_Store::runs();
$schedules = QBPD_Schedules::all();
$active = count(
array_filter(
$schedules,
function ( $r ) {
return $r['on'];
}
)
);
$applied = count(
array_filter(
$runs,
function ( $r ) {
return 'done' === $r['status'];
}
)
);
$ctx = array(
'runs' => $runs,
'schedules' => $schedules,
'active' => $active,
'applied' => $applied,
'sel' => $sel,
'ids' => QBPD_Job::selection( $sel ),
);
if ( $active ) {
/* translators: %s: number of active schedules. */
$pill_schedules = sprintf( _n( '%s schedule on', '%s schedules on', $active, 'redate' ), number_format_i18n( $active ) );
} else {
$pill_schedules = __( 'No schedules', 'redate' );
}
if ( $applied ) {
/* translators: %s: number of applied runs. */
$pill_history = sprintf( _n( 'History: %s run', 'History: %s runs', $applied, 'redate' ), number_format_i18n( $applied ) );
} else {
$pill_history = __( 'History: empty', 'redate' );
}
/* translators: %s: plugin version number. */
$version = sprintf( __( 'Version %s', 'redate' ), QBPD_VERSION );
?>
publish ),
/* translators: %s: number of scheduled posts. */
sprintf( _n( '%s scheduled post', '%s scheduled posts', (int) $posts->future, 'redate' ), number_format_i18n( (int) $posts->future ) )
);
self::stat( __( 'Published pages', 'redate' ), number_format_i18n( (int) $pages->publish ), __( 'and every other post type', 'redate' ) );
if ( $last ) {
/* translators: %s: date and time of the last change. */
$last_text = sprintf( __( 'Last: %s', 'redate' ), QBPD_Job::fmt( $last['finished'] ? $last['finished'] : $last['updated'] ) );
} else {
$last_text = __( 'each one can be undone', 'redate' );
}
self::stat( __( 'Changes in History', 'redate' ), number_format_i18n( $ctx['applied'] ), $last_text );
self::stat(
__( 'Schedules on', 'redate' ),
number_format_i18n( $ctx['active'] ),
/* translators: %s: number of schedules. */
sprintf( _n( '%s schedule in all', '%s schedules in all', $n_sched, 'redate' ), number_format_i18n( $n_sched ) )
);
?>
' . esc_html__( 'Open Posts', 'redate' ) . '' ); ?>
Posts, Pages , Products or any other list and tick the posts.', 'redate' ), array( 'strong' => array() ) ); ?>
Change dates under Bulk actions and click Apply.', 'redate' ), array( 'strong' => array() ) ); ?>
'change' ) ) ) . '">' . esc_html__( 'Change dates', 'redate' ) . '' ); ?>
__( 'Mon', 'redate' ),
2 => __( 'Tue', 'redate' ),
3 => __( 'Wed', 'redate' ),
4 => __( 'Thu', 'redate' ),
5 => __( 'Fri', 'redate' ),
6 => __( 'Sat', 'redate' ),
0 => __( 'Sun', 'redate' ),
);
if ( $ctx['sel'] && ! $n_sel ) {
echo '
';
self::the_icon( 'warning' );
echo '
' . esc_html__( 'The selection from the post list has expired (selections are kept for an hour). Select the posts again, or use the filters below.', 'redate' ) . '
';
}
/* translators: %s: number of posts. */
$sel_text = sprintf( _n( '%s post you ticked in a post list.', '%s posts you ticked in a post list.', $n_sel, 'redate' ), number_format_i18n( $n_sel ) );
self::card_start( __( 'Which posts', 'redate' ) );
?>
$c ) : ?>
>
__( 'Hours', 'redate' ), 'days' => __( 'Days', 'redate' ), 'weeks' => __( 'Weeks', 'redate' ), 'months' => __( 'Months', 'redate' ), 'years' => __( 'Years', 'redate' ) ), $o['rel_unit'] ); ?>
__( 'Forward: the oldest post gets the start, then later', 'redate' ), 'backward' => __( 'Backward: the newest post gets the start, then earlier', 'redate' ) ), $o['seq_dir'] ); ?>
__( 'Minutes', 'redate' ), 'hours' => __( 'Hours', 'redate' ), 'days' => __( 'Days', 'redate' ), 'weeks' => __( 'Weeks', 'redate' ) ), $o['seq_unit'] ); ?>
__( 'Earlier (back in time)', 'redate' ), 'later' => __( 'Later (forward in time)', 'redate' ) ), $o['shift_dir'] ); ?>
__( 'Minutes', 'redate' ), 'hours' => __( 'Hours', 'redate' ), 'days' => __( 'Days', 'redate' ), 'weeks' => __( 'Weeks', 'redate' ), 'months' => __( 'Months', 'redate' ), 'years' => __( 'Years', 'redate' ) ), $o['shift_unit'] ); ?>
__( 'Use now instead (they stay published)', 'redate' ), 'schedule' => __( 'Schedule them for that date', 'redate' ) ), $o['future'] ); ?>
'change' ) ) ) . '">' . esc_html__( 'New schedule', 'redate' ) . '' );
?>
display_name );
} else {
$by = __( 'By a removed user', 'redate' );
}
if ( $r['on'] ) {
/* translators: %s: date and time of the next run. */
$next = sprintf( __( 'Next: %s', 'redate' ), QBPD_Job::fmt( $r['next'] ) );
} else {
$next = __( 'Paused', 'redate' );
}
?>
'change' ) ) ) . '">' . esc_html__( 'New change', 'redate' ) . ''
);
?>
2 * MINUTE_IN_SECONDS;
if ( 'auto' === $run['kind'] ) {
/* translators: %s: schedule name. */
$who = sprintf( __( 'Schedule: %s', 'redate' ), isset( $names[ $run['rule'] ] ) ? $names[ $run['rule'] ] : __( 'deleted', 'redate' ) );
} else {
$who = $user ? $user->display_name : __( 'Removed user', 'redate' );
}
/* translators: 1: posts changed, 2: posts in the run. */
$of_text = sprintf( __( '%1$s of %2$s', 'redate' ), number_format_i18n( $run['applied'] ), number_format_i18n( $run['total'] ) );
/* translators: %s: number of posts skipped. */
$skip_text = sprintf( _n( '%s skipped (no permission or deleted)', '%s skipped (no permission or deleted)', (int) $run['skipped'], 'redate' ), number_format_i18n( $run['skipped'] ) );
/* translators: %s: number of comments. */
$com_text = sprintf( _n( '%s comment moved', '%s comments moved', (int) $run['comments'], 'redate' ), number_format_i18n( $run['comments'] ) );
?>
' . esc_html__( 'Applied', 'redate' ) . '';
} elseif ( 'undone' === $run['status'] ) {
echo '' . esc_html__( 'Undone', 'redate' ) . ' ' . esc_html( QBPD_Job::fmt( isset( $run['undone_time'] ) ? $run['undone_time'] : $run['updated'] ) ) . ' ';
} elseif ( 'error' === $run['status'] ) {
echo '' . esc_html__( 'Failed', 'redate' ) . ' ' . esc_html( $run['error'] ) . ' ';
} elseif ( $stale ) {
echo '' . esc_html__( 'Interrupted', 'redate' ) . ' ' . esc_html( 'apply' === $run['status'] ? __( 'Applying stopped part way', 'redate' ) : __( 'Undo stopped part way', 'redate' ) ) . ' ';
} else {
echo '' . esc_html__( 'Running', 'redate' ) . ' ';
}
?>
' . esc_html( 'wp-content/uploads/' . QBPD_Store::DIR . '/' ) . ''
),
array( 'code' => array() )
);
echo ' ';
esc_html_e( 'Undo only puts back what a run changed: if a post\'s status was changed by hand since, its status is left alone.', 'redate' );
?>