$actions * @param \WP_Post $post * @return array */ public function add_row_action( $actions, $post ) { if ( ! $post instanceof \WP_Post ) { return $actions; } if ( ! current_user_can( 'manage_options' ) ) { return $actions; } if ( ! Network_Settings::is_enabled( $post->post_type ) ) { return $actions; } // Single copies use the same chunked, timeout-safe runner as bulk. $url = add_query_arg( array( 'page' => Bulk_Copy::PAGE_SLUG, 'ids' => (int) $post->ID, '_mspc_nonce' => wp_create_nonce( Bulk_Copy::PAGE_NONCE ), ), admin_url( 'admin.php' ) ); $actions['mspc_copy'] = sprintf( '%s', esc_url( $url ), esc_html__( 'Copy to location…', '42works-multisite-cpt-migrator' ) ); return $actions; } public function register_hidden_page() { add_submenu_page( null, __( 'Copy post to location', '42works-multisite-cpt-migrator' ), __( 'Copy post', '42works-multisite-cpt-migrator' ), 'manage_options', self::PAGE_SLUG, array( $this, 'render_page' ) ); } public function render_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Insufficient permissions.', '42works-multisite-cpt-migrator' ) ); } $source_id = isset( $_GET['source'] ) ? (int) $_GET['source'] : 0; $nonce = isset( $_GET['_mspc_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_mspc_nonce'] ) ) : ''; if ( ! $source_id || ! wp_verify_nonce( $nonce, self::ROW_NONCE . $source_id ) ) { wp_die( esc_html__( 'Invalid or expired request. Re-open from the post list.', '42works-multisite-cpt-migrator' ) ); } $post = get_post( $source_id ); if ( ! $post ) { wp_die( esc_html__( 'Source post not found.', '42works-multisite-cpt-migrator' ) ); } if ( ! Network_Settings::is_enabled( $post->post_type ) ) { wp_die( esc_html__( 'This post type is not enabled for cloning. Enable it under Network Admin → Settings → MultiSite Post Cloner.', '42works-multisite-cpt-migrator' ) ); } $cpt_obj = get_post_type_object( $post->post_type ); $cpt_name = $cpt_obj ? $cpt_obj->labels->singular_name : $post->post_type; $sites = get_sites( array( 'number' => 0, 'site__not_in' => array( get_current_blog_id() ), ) ); ?>

' . esc_html( get_the_title( $source_id ) ) . '', '' . esc_html( $cpt_name ) . '' ); ?>

redirect_with_error( 'invalid_source' ); } if ( ! Network_Settings::is_enabled( $source_post->post_type ) ) { $this->redirect_with_error( 'cpt_disabled', $source_post->post_type ); } $result = ( new Post_Cloner() )->copy( $source_id, $target_id ); if ( is_wp_error( $result ) ) { $this->redirect_with_error( $result->get_error_code(), $source_post->post_type, $result->get_error_message() ); } // Success — land the admin on the target post's edit screen so they // immediately see the warning panel (if any). wp_safe_redirect( $result['edit_url'] ); exit; } /** * @param string $code * @param string $cpt * @param string $message */ private function redirect_with_error( $code, $cpt = '', $message = '' ) { $args = array( 'mspc_error' => $code ); if ( $message ) { $args['mspc_error_msg'] = rawurlencode( $message ); } $base = $cpt && post_type_exists( $cpt ) ? admin_url( 'edit.php?post_type=' . $cpt ) : admin_url( 'index.php' ); wp_safe_redirect( add_query_arg( $args, $base ) ); exit; } public function render_error_notice() { if ( empty( $_GET['mspc_error'] ) ) { return; } $code = sanitize_key( wp_unslash( $_GET['mspc_error'] ) ); if ( isset( $_GET['mspc_error_msg'] ) ) { $raw_msg = wp_unslash( $_GET['mspc_error_msg'] ); $msg = sanitize_text_field( rawurldecode( $raw_msg ) ); } else { $msg = ''; } $labels = array( 'invalid_source' => __( 'Could not copy: source post not found.', '42works-multisite-cpt-migrator' ), 'cpt_disabled' => __( 'Could not copy: this post type is not enabled for cloning. Enable it under Network Admin → Settings → MultiSite Post Cloner.', '42works-multisite-cpt-migrator' ), 'mspc_same_site' => __( 'Could not copy: target site is the same as the current site.', '42works-multisite-cpt-migrator' ), 'mspc_invalid_target' => __( 'Could not copy: target subsite does not exist.', '42works-multisite-cpt-migrator' ), 'mspc_no_cpt' => __( 'Could not copy: the target subsite does not have this post type registered.', '42works-multisite-cpt-migrator' ), 'mspc_insert_failed' => __( 'Could not copy: WordPress refused the new post.', '42works-multisite-cpt-migrator' ), ); $head = isset( $labels[ $code ] ) ? $labels[ $code ] : __( 'Could not copy this post.', '42works-multisite-cpt-migrator' ); printf( '

%s%s

', esc_html( $head ), $msg ? ' ' . wp_kses_post($msg) : '' ); } }