options = get_option( self::OPTION_NAME, [] );
$this->init();
}
/**
* Start the engine running
*
* @return void
*/
private function init() {
$this->settings = new Content_Refresh_Settings( $this->options );
add_action( 'adminoptimizer_add_submenu_page', [ $this, 'add_submenu_page' ] );
add_action( 'init', [ $this, 'add_row_actions' ] );
add_action( 'admin_action_adminoptim-clone-republish', [ $this, 'create_clone_republish_post' ] );
add_filter( 'display_post_states', [ $this, 'show_clone_post_states' ], 10, 2 );
add_action( 'admin_notices', [ $this, 'post_admin_notice' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'gutenberg_ui_mod' ] );
add_action( 'transition_post_status', [ $this, 'schedule_or_republish' ], 8, 3 );
add_action( 'adminoptim_republish_post', [ $this, 'do_republish' ] );
add_action( 'admin_action_adminoptim-republisher-delete-post', [ $this, 'do_delete_republish' ] );
add_action( 'adminoptim_deactivate_plugin', [ $this, 'remove_schedule_on_deactivation' ] );
new CR_PostStatus();
new CR_Cleaner( $this->options );
new CR_MetaBoxes( $this->options );
}
/**
* Add submenu to WP Menu page
*
* @return void
*/
public function add_submenu_page() {
add_submenu_page(
ADMINOPTIMIZER_MODULES_MENU_SLUG,
__( 'Content Refresh', 'admin-optimizer' ),
__( 'Content Refresh', 'admin-optimizer' ),
'manage_options',
self::MENU_SLUG,
[ $this->settings, 'render_settings_page' ]
);
}
/**
* Hooks to the row_actions filters
*
* @return void
*/
public function add_row_actions() {
if ( is_admin() ) {
add_filter( 'post_row_actions', [ $this, 'row_actions' ], 10, 2 );
add_filter( 'page_row_actions', [ $this, 'row_actions' ], 10, 2 );
}
}
/**
* Row actions in actions
*
* @param array $actions Array of actions.
* @param \WP_Post $post Post object.
*
* @return mixed
*/
public function row_actions( $actions, $post ) {
if ( $this->is_republish( $post->ID ) ) {
return $actions;
}
$has_user_permission = $this->check_user_republish_permission( wp_get_current_user() );
if ( $has_user_permission ) {
if ( ! empty( $this->options['post_types'] ) && in_array( $post->post_type, $this->options['post_types'] ) ) {
$actions['clone_republish'] = '' . __( 'Create Republish Clone', 'admin-optimizer' ) . '';
}
}
return $actions;
}
/**
* Function to handle the cloning link in row action
*
* @return void
*/
public function create_clone_republish_post() {
$clone_parent_id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : null;
$has_permission = $this->check_user_republish_permission( wp_get_current_user() );
if ( check_admin_referer( 'adminoptim-clone-' . $clone_parent_id ) && $has_permission ) {
if ( $clone_parent_id ) {
$clone_id = $this->clone_republish_post( $clone_parent_id );
if ( 0 < $clone_id && ! is_wp_error( $clone_id ) ) {
wp_safe_redirect( get_edit_post_link( $clone_id, '&' ) );
exit;
}
} else {
wp_die( esc_html__( 'Post cloning failed', 'admin-optimizer' ) );
}
}
// if we didn't redirect out, then we fail.
wp_die( esc_html__( 'Invalid Post ID', 'admin-optimizer' ) );
}
/**
* Function to show the cloned post states in Posts screen.
*
* @param array $post_states Array of post states.
* @param \WP_Post $post Post object.
*
* @return array|mixed
*/
public function show_clone_post_states( $post_states, $post ) {
if ( ! $post instanceof \WP_Post || ! is_array( $post_states ) ) {
return $post_states;
}
$is_republish = $this->is_republish( $post->ID );
if ( $is_republish ) {
$post_states['adminoptim_republish_item'] = sprintf(
/* translators: %s is replaced by the post title. */
esc_html__( 'Republish clone of %s', 'admin-optimizer' ),
$this->get_parent_title( $post )
);
return $post_states;
}
return $post_states;
}
/**
* Add an admin notice at the top of republish posts.
*
* @return void
*/
public function post_admin_notice(): void {
global $post;
$screen = get_current_screen();
if ( $post instanceof \WP_Post && 'post' === $screen->base ) {
if ( $this->is_republish( $post->ID ) ) {
$parent_id = $this->get_republish_parent( $post->ID );
$parent_link = get_permalink( $parent_id );
$edit_post_link = ! is_null( get_edit_post_link( $parent_id, '&' ) ) ? get_edit_post_link( $parent_id, '&' ) : '';
?>
ID, '_republish_last_updated', true );
if ( ! empty( $last_update ) ) :
?>
ID ) && is_numeric( $post->ID ) ) {
$is_republish_post = $this->is_republish( $post->ID );
if ( 'post' === $screen->base && $screen->is_block_editor() ) {
$last_update = get_post_meta( $post->ID, '_republish_last_updated', true );
$republisher_block = [
'post_id' => $post->ID,
'is_republish' => $is_republish_post,
'last_update' => $last_update ?? '',
];
wp_enqueue_script( 'adminoptim-republisher-block-editor', self::MODULE_URL . 'assets/js/block-editor.min.js', [ 'jquery' ], ADMINOPTIMIZER_VERSION, true );
$republisher_block_i18n = [];
if ( $is_republish_post ) {
$parent_id = $this->get_republish_parent( $post->ID );
$parent_link = get_permalink( $parent_id );
$edit_post_link = ! is_null( get_edit_post_link( $parent_id, '&' ) ) ? get_edit_post_link( $parent_id, '&' ) : '';
$redirect_url = add_query_arg(
[
'action' => 'adminoptim-republisher-delete-post',
'republish_id' => $post->ID,
'parent_id' => $parent_id,
'republish_nonce' => wp_create_nonce( 'adminoptim-republisher-delete-post' ),
],
admin_url( 'admin.php' )
);
$republisher_block['parent_url'] = esc_url( $parent_link, null, '&' );
$republisher_block['parent_edit_link'] = esc_url( $edit_post_link, null, '&' );
$republisher_block['redirect_url'] = esc_url( $redirect_url, null, '&' );
$republisher_block_i18n = [
'message' => sprintf(
/* translators: %s is replaced by the post title. */
esc_html__( 'This is a republish copy of %s. Publishing this post will overwrite the original post with this content.', 'admin-optimizer' ),
'"' . esc_html( html_entity_decode( get_the_title( $parent_id ) ) ) . '"'
),
];
} elseif ( ! empty( $last_update ) ) {
$republisher_block_i18n = [
'message' => sprintf(
/* translators: %s is replaced by the last update date. */
esc_html__( 'This post was last updated on %s', 'admin-optimizer' ),
esc_html( $last_update )
),
];
}
wp_localize_script(
'adminoptim-republisher-block-editor',
'RepublisherBlockUI',
$republisher_block
);
wp_localize_script(
'adminoptim-republisher-block-editor',
'RepublisherBlocki18n',
$republisher_block_i18n
);
}
}
}
/**
* Schedule a republish post to go live in the future
*
* @param string $new_status Post status.
* @param string $old_status Post status.
* @param \WP_Post $post Post object.
*
* @return void
*/
public function schedule_or_republish( $new_status, $old_status, $post ) {
if ( 'republish' === $new_status && 'republish' !== $old_status ) {
$parent_id = $this->get_republish_parent( $post->ID );
$this->republish( $post );
if ( ! $this->is_rest_request() && ! wp_doing_ajax() ) {
wp_safe_redirect(
add_query_arg(
[
'action' => 'adminoptim-republisher-delete-post',
'republish_id' => $post->ID,
'parent_id' => $parent_id,
'republish_nonce' => wp_create_nonce( 'adminoptim-republisher-delete-post' ),
],
admin_url( 'admin.php' )
)
);
exit;
}
} elseif ( 'future-republish' === $new_status && $post instanceof \WP_Post ) {
$post_timestamp = get_post_timestamp( $post->ID );
if ( ! as_has_scheduled_action( 'adminoptim_republish_post', [ $post->ID ], 'admin-optimizer' ) ) {
as_schedule_single_action( $post_timestamp, 'adminoptim_republish_post', [ $post->ID ], 'admin-optimizer' );
} else {
// reschedule action if the post timestamp is not the same.
$scheduled_timestamp = as_next_scheduled_action( 'adminoptim_republish_post', [ $post->ID ], 'admin-optimizer' );
if ( $scheduled_timestamp !== $post_timestamp ) {
as_unschedule_action( 'adminoptim_republish_post', [ $post->ID ], 'admin-optimizer' );
as_schedule_single_action( $post_timestamp, 'adminoptim_republish_post', [ $post->ID ], 'admin-optimizer' );
}
}
} elseif ( 'future-republish' === $old_status && 'future-republish' !== $new_status ) {
// need to unschedule the scheduling.
if ( as_has_scheduled_action( 'adminoptim_republish_post', [ $post->ID ], 'admin-optimizer' ) ) {
as_unschedule_action( 'adminoptim_republish_post', [ $post->ID ], 'admin-optimizer' );
}
}
}
/**
* Run the Republish action.
*
* @param int $republish_id Post id.
*
* @return void
*/
public function do_republish( $republish_id ) {
$this->republish( $republish_id );
$this->delete_republish( $republish_id );
}
/**
* Check if the republish post need to be deleted.
*
* @return void
*/
public function do_delete_republish() {
$redirect_url = admin_url( 'edit.php' );
if ( check_admin_referer( 'adminoptim-republisher-delete-post', 'republish_nonce' ) ) {
if ( isset( $_GET['republish_id'] ) && is_numeric( $_GET['republish_id'] ) ) {
$republish_id = absint( $_GET['republish_id'] );
if ( 'pending-delete' === get_post_meta( $republish_id, '_republish_status', true ) ) {
$this->delete_republish( $republish_id );
}
}
if ( isset( $_GET['parent_id'] ) ) {
$parent_id = absint( $_GET['parent_id'] );
$redirect_url = admin_url( 'post.php?action=edit&post=' . $parent_id );
}
}
wp_safe_redirect( $redirect_url );
exit;
}
/**
* Deactivate schedule
*
* @return void
*/
public function remove_schedule_on_deactivation(): void {
if ( as_has_scheduled_action( 'adminoptim_republish_cleanup' ) ) {
as_unschedule_all_actions( 'adminoptim_republish_cleanup' );
}
if ( as_has_scheduled_action( 'adminoptim_republish_post' ) ) {
as_unschedule_all_actions( 'adminoptim_republish_post' );
}
}
}