$actions * @return array */ public function add_bulk_action( $actions ) { if ( current_user_can( 'manage_options' ) ) { $actions[ self::BULK_ACTION ] = __( 'Copy to location…', '42works-multisite-cpt-migrator' ); } return $actions; } /** * @param string $redirect * @param string $action * @param int[] $post_ids * @return string */ public function handle_bulk_action( $redirect, $action, $post_ids ) { if ( self::BULK_ACTION !== $action ) { return $redirect; } if ( ! current_user_can( 'manage_options' ) ) { return $redirect; } $ids = array_values( array_unique( array_filter( array_map( 'intval', (array) $post_ids ) ) ) ); if ( empty( $ids ) ) { return add_query_arg( 'mspc_bulk_notice', 'none_selected', $redirect ); } if ( count( $ids ) > self::MAX_POSTS ) { return add_query_arg( 'mspc_bulk_notice', 'too_many', $redirect ); } return add_query_arg( array( 'page' => self::PAGE_SLUG, 'ids' => implode( ',', $ids ), '_mspc_nonce' => wp_create_nonce( self::PAGE_NONCE ), ), admin_url( 'admin.php' ) ); } public function register_hidden_page() { add_submenu_page( null, __( 'Copy posts to location', '42works-multisite-cpt-migrator' ), __( 'Copy posts', '42works-multisite-cpt-migrator' ), 'manage_options', self::PAGE_SLUG, array( $this, 'render_page' ) ); } /** * @param string $hook */ public function enqueue_assets( $hook ) { if ( 'admin_page_' . self::PAGE_SLUG !== $hook ) { return; } wp_enqueue_script( 'mspc-bulk-copy', MSPC_URL . 'js/bulk-copy.js', array(), MSPC_VERSION, true ); wp_localize_script( 'mspc-bulk-copy', 'mspcBulkCopy', array( 'ajaxUrl' => admin_url( 'admin-post.php' ), 'action' => self::AJAX_ACTION, 'mediaAction' => self::MEDIA_ACTION, 'nonce' => wp_create_nonce( self::AJAX_NONCE ), 'i18n' => array( 'pickTarget' => __( 'Please select a target location.', '42works-multisite-cpt-migrator' ), 'confirm' => __( 'Copy %d post(s) as drafts to the selected location?', '42works-multisite-cpt-migrator' ), 'queued' => __( 'Queued', '42works-multisite-cpt-migrator' ), 'copying' => __( 'Copying…', '42works-multisite-cpt-migrator' ), 'media' => __( 'Media %1$d/%2$d', '42works-multisite-cpt-migrator' ), 'finishing' => __( 'Creating post…', '42works-multisite-cpt-migrator' ), 'retrying' => __( 'Retrying…', '42works-multisite-cpt-migrator' ), 'copied' => __( 'Copied', '42works-multisite-cpt-migrator' ), 'failed' => __( 'Failed', '42works-multisite-cpt-migrator' ), 'skipped' => __( 'Skipped', '42works-multisite-cpt-migrator' ), 'editDraft' => __( 'Edit draft', '42works-multisite-cpt-migrator' ), 'warnings' => __( '%d item(s) need review', '42works-multisite-cpt-migrator' ), 'network' => __( 'Request failed (network or server error).', '42works-multisite-cpt-migrator' ), 'server' => __( 'Server error (HTTP %d) — the copy may be incomplete. Check the draft, delete it, and copy this post again on its own.', '42works-multisite-cpt-migrator' ), 'details' => __( '%1$d/%2$d ACF fields, %3$d media uploaded, %4$d reused', '42works-multisite-cpt-migrator' ), 'done' => __( 'Finished: %1$d copied, %2$d failed.', '42works-multisite-cpt-migrator' ), 'leave' => __( 'Copies are still running. Leave this page?', '42works-multisite-cpt-migrator' ), ), ) ); } public function render_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Insufficient permissions.', '42works-multisite-cpt-migrator' ) ); } $nonce = isset( $_GET['_mspc_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_mspc_nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, self::PAGE_NONCE ) ) { wp_die( esc_html__( 'Invalid or expired request. Re-open from the post list.', '42works-multisite-cpt-migrator' ) ); } $raw_ids = isset( $_GET['ids'] ) ? sanitize_text_field( wp_unslash( $_GET['ids'] ) ) : ''; $ids = array_slice( array_values( array_unique( array_filter( array_map( 'intval', explode( ',', $raw_ids ) ) ) ) ), 0, self::MAX_POSTS ); $posts = array(); $post_type = ''; foreach ( $ids as $id ) { $p = get_post( $id ); if ( ! $p || ! Network_Settings::is_enabled( $p->post_type ) ) { continue; } $posts[] = $p; $post_type = $p->post_type; } if ( empty( $posts ) ) { wp_die( esc_html__( 'None of the selected posts can be copied. Check that their post type is enabled for cloning.', '42works-multisite-cpt-migrator' ) ); } $cpt_obj = get_post_type_object( $post_type ); $plural = $cpt_obj ? $cpt_obj->labels->name : $post_type; $back_url = admin_url( 'edit.php?post_type=' . $post_type ); $sites = get_sites( array( 'number' => 0, 'site__not_in' => array( get_current_blog_id() ), ) ); ?>

#ID ); ?> · —

ob_level = ob_get_level(); ob_start(); if ( ! current_user_can( 'manage_options' ) ) { $this->send( false, array( 'message' => __( 'Insufficient permissions.', '42works-multisite-cpt-migrator' ) ), 403 ); } $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, self::AJAX_NONCE ) ) { $this->send( false, array( 'message' => __( 'Session expired. Reload the page and try again.', '42works-multisite-cpt-migrator' ) ), 403 ); } $source_id = isset( $_POST['source'] ) ? (int) $_POST['source'] : 0; $target_id = isset( $_POST['target'] ) ? (int) $_POST['target'] : 0; $source_post = $source_id ? get_post( $source_id ) : null; if ( ! $source_post ) { $this->send( false, array( 'message' => __( 'Source post not found.', '42works-multisite-cpt-migrator' ) ) ); } if ( ! Network_Settings::is_enabled( $source_post->post_type ) ) { $this->send( false, array( 'message' => __( 'This post type is not enabled for cloning.', '42works-multisite-cpt-migrator' ) ) ); } if ( ! $target_id ) { $this->send( false, array( 'message' => __( 'No target location selected.', '42works-multisite-cpt-migrator' ) ) ); } // Identical call to the single-post handler (runtime limits are set // inside Post_Cloner::copy()). $result = ( new Post_Cloner() )->copy( $source_id, $target_id ); if ( is_wp_error( $result ) ) { $this->send( false, array( 'code' => $result->get_error_code(), 'message' => $result->get_error_message(), ) ); } $warning_total = 0; if ( ! empty( $result['report']['counts'] ) && is_array( $result['report']['counts'] ) ) { $warning_total = (int) array_sum( $result['report']['counts'] ); } $stats = isset( $result['stats'] ) ? (array) $result['stats'] : array(); $this->send( true, array( 'new_id' => (int) $result['new_id'], 'edit_url' => esc_url_raw( $result['edit_url'] ), 'warnings' => $warning_total, 'acf_written' => (int) ( $result['acf_written'] ?? 0 ), 'acf_total' => (int) ( $result['acf_total'] ?? 0 ), 'uploaded' => (int) ( $stats['successes'] ?? 0 ), 'reused' => (int) ( $stats['existing'] ?? 0 ) + (int) ( $stats['reused'] ?? 0 ), 'failed' => (int) ( $stats['failures'] ?? 0 ), ) ); } /** * One time-boxed media batch for one post. Called repeatedly by * bulk-copy.js (offset = previous "next") until done, before the final * ajax_copy_one() call. */ public function ajax_media_step() { $this->ob_level = ob_get_level(); ob_start(); if ( ! current_user_can( 'manage_options' ) ) { $this->send( false, array( 'message' => __( 'Insufficient permissions.', '42works-multisite-cpt-migrator' ) ), 403 ); } $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, self::AJAX_NONCE ) ) { $this->send( false, array( 'message' => __( 'Session expired. Reload the page and try again.', '42works-multisite-cpt-migrator' ) ), 403 ); } $source_id = isset( $_POST['source'] ) ? (int) $_POST['source'] : 0; $target_id = isset( $_POST['target'] ) ? (int) $_POST['target'] : 0; $offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0; $source_post = $source_id ? get_post( $source_id ) : null; if ( ! $source_post || ! Network_Settings::is_enabled( $source_post->post_type ) ) { $this->send( false, array( 'message' => __( 'Source post not found or its post type is not enabled.', '42works-multisite-cpt-migrator' ) ) ); } $result = ( new Post_Cloner() )->prepare_media( $source_id, $target_id, $offset ); if ( is_wp_error( $result ) ) { $this->send( false, array( 'message' => $result->get_error_message() ) ); } $this->send( true, $result ); } /** * Discard stray output, then send JSON and exit. * * @param bool $ok * @param array $data * @param int $status */ private function send( $ok, array $data, $status = 200 ) { // Only drop the buffers we opened, never the host's/WordPress's own. while ( ob_get_level() > $this->ob_level ) { ob_end_clean(); } if ( $ok ) { wp_send_json_success( $data, $status ); } wp_send_json_error( $data, $status ); } /** * Notices for bulk-action edge cases, shown back on the list table. */ public function render_notice() { if ( empty( $_GET['mspc_bulk_notice'] ) ) { return; } $code = sanitize_key( wp_unslash( $_GET['mspc_bulk_notice'] ) ); $map = array( 'none_selected' => __( 'No posts were selected to copy.', '42works-multisite-cpt-migrator' ), /* translators: %d: maximum posts per bulk copy */ 'too_many' => sprintf( __( 'Too many posts selected. Copy at most %d at a time.', '42works-multisite-cpt-migrator' ), self::MAX_POSTS ), ); if ( ! isset( $map[ $code ] ) ) { return; } printf( '

%s

', esc_html( $map[ $code ] ) ); } }