repository->for_post( $post->ID, 'post' )[0] ?? null; $leads = array_values( array_filter( $this->repository->pointing_at( 'post', $post->ID ), static fn( Rule $r ): bool => 'post' !== $r->origin ) ); wp_nonce_field( self::NONCE, self::NONCE ); echo '
'; if ( 'publish' !== $post->post_status ) { printf( '

%s

', esc_html__( 'Once this is published you can send its visitors somewhere else from here.', 'codebold-redirects' ) ); } else { printf( '

', checked( null !== $own && $own->enabled, true, false ), esc_html__( 'Send visitors somewhere else', 'codebold-redirects' ) ); printf( '
', null !== $own && $own->enabled ? '' : ' hidden' ); // A plain text field, which postbox.js turns into the same page picker as the // Redirects screen. The hidden fields carry a picked page's id, so the redirect // follows that page if it is renamed — and without scripts it still works typed. printf( '

', esc_html__( 'Send them to', 'codebold-redirects' ), esc_attr( null !== $own ? self::display_target( $own ) : '' ), esc_attr__( 'Search pages or paste an address', 'codebold-redirects' ), esc_attr( null !== $own ? $own->target_type : 'url' ), esc_attr( null !== $own ? $own->target_label() : '' ), absint( null !== $own ? $own->target_id : 0 ) ); printf( '

', esc_html__( 'Type of redirect', 'codebold-redirects' ) ); echo '

'; printf( '

%s

', esc_html__( 'Visitors and search engines will go there instead of seeing this page. You can keep editing it in the meantime.', 'codebold-redirects' ) ); echo '
'; } // Redrawn by postbox.js after each save in the block editor, which saves without // reloading the page — renaming a page there adds an old address to this list. printf( '
', (int) $post->ID ); if ( [] !== $leads ) { printf( '

%s

'; if ( count( $leads ) > 8 ) { /* translators: %d: number of further redirects */ printf( '

%s

', esc_html( sprintf( __( 'and %d more', 'codebold-redirects' ), count( $leads ) - 8 ) ) ); } } echo '
'; printf( '

%s

', esc_url( AdminModule::url( [ 'add_to_post' => $post->ID ] ) ), esc_html__( 'Add an old address for this page', 'codebold-redirects' ) ); echo '
'; } /** * What the box's field shows for a saved destination: a linked page's address rather * than its id, because that is what the person typed. */ private static function display_target( Rule $rule ): string { if ( 'url' !== $rule->target_type && '' !== $rule->target ) { return Address::parse( $rule->target )->is_local() ? Address::parse( $rule->target )->relative() : $rule->target; } return $rule->target; } public function save( int $post_id, \WP_Post $post ): void { if ( ! isset( $_POST[ self::NONCE ] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST[ self::NONCE ] ) ), self::NONCE ) ) { return; } if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || ! current_user_can( 'edit_post', $post_id ) || ! Access::can_manage() ) { return; } if ( 'publish' !== $post->post_status ) { return; } $on = ! empty( $_POST['codebold_redirect_on'] ); $target = isset( $_POST['codebold_redirect_to'] ) ? sanitize_text_field( wp_unslash( $_POST['codebold_redirect_to'] ) ) : ''; $type = isset( $_POST['codebold_redirect_to_type'] ) ? sanitize_key( wp_unslash( $_POST['codebold_redirect_to_type'] ) ) : 'url'; $to_id = isset( $_POST['codebold_redirect_to_id'] ) ? absint( $_POST['codebold_redirect_to_id'] ) : 0; $code = isset( $_POST['codebold_redirect_code'] ) ? absint( $_POST['codebold_redirect_code'] ) : 301; if ( ! in_array( $type, [ 'post', 'term' ], true ) || $to_id <= 0 ) { $type = 'url'; $to_id = 0; } $own = $this->repository->for_post( $post_id, 'post' )[0] ?? null; if ( ! $on || '' === $target ) { if ( null !== $own ) { $this->manager->delete( [ $own->id ] ); } return; } $input = [ 'source' => Address::parse( (string) get_permalink( $post ) )->path, 'target' => $target, 'target_type' => $type, 'target_id' => $to_id, 'status_code' => $code, 'origin' => 'post', 'post_id' => $post_id, 'enabled' => true, ]; $result = null === $own ? $this->manager->create( $input ) : $this->manager->update( $own->id, $input ); // The editor gives no way to show an error from a meta box save, so it is kept for // the next screen to show. if ( is_wp_error( $result ) ) { set_transient( 'codebold_redirect_box_error_' . get_current_user_id(), $result->get_error_message(), MINUTE_IN_SECONDS ); } } }