post_types() as $type ) { add_meta_box( 'vabelon-translations', 'Vabelon', array( $this, 'render' ), $type, 'side', 'default' ); } } /** * Print the box. * * @param WP_Post $post Post being edited. * @return void */ public function render( WP_Post $post ): void { wp_nonce_field( 'vabelon_translations_' . $post->ID, 'vabelon_translations_nonce' ); $current = Plugin::translations()->effective_language( $post->ID ); $linked = Plugin::translations()->translations_of( $post->ID ); echo '


'; foreach ( Plugin::languages()->all() as $language ) { $slug = $language->slug; if ( $slug === $current ) { continue; } echo '

' . esc_html( $language->native_name ) . '
'; if ( isset( $linked[ $slug ] ) ) { echo '' . esc_html( get_the_title( $linked[ $slug ] ) ) . '
'; echo ''; } else { echo ''; } echo '

'; } } /** * Save the box. * * @param int $post_id Post ID. * @param WP_Post $post Post being saved. * @return void */ public function save( int $post_id, WP_Post $post ): void { if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) || ! Plugin::translations()->supports( $post_id ) ) { return; } $nonce = isset( $_POST['vabelon_translations_nonce'] ) && is_string( $_POST['vabelon_translations_nonce'] ) ? sanitize_key( wp_unslash( $_POST['vabelon_translations_nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'vabelon_translations_' . $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { return; } $language = isset( $_POST['vabelon_language'] ) && is_string( $_POST['vabelon_language'] ) ? sanitize_key( wp_unslash( $_POST['vabelon_language'] ) ) : ''; $unlink = isset( $_POST['vabelon_unlink'] ) && is_array( $_POST['vabelon_unlink'] ) ? array_map( 'sanitize_key', array_keys( wp_unslash( $_POST['vabelon_unlink'] ) ) ) : array(); $posted = isset( $_POST['vabelon_link'] ) && is_array( $_POST['vabelon_link'] ) ? map_deep( wp_unslash( $_POST['vabelon_link'] ), 'sanitize_text_field' ) : array(); $link = array(); foreach ( is_array( $posted ) ? $posted : array() as $slug => $target ) { $link[ sanitize_key( (string) $slug ) ] = is_numeric( $target ) ? absint( $target ) : 0; } $error = $this->apply( $post_id, $language, $unlink, $link ); if ( '' !== $error ) { set_transient( 'vabelon_box_error_' . get_current_user_id(), $error, 60 ); } } /** * Apply the box's choices to a post. * * @param int $post_id Post ID. * @param string $language Chosen language. * @param array $unlink Languages whose translation is unlinked. * @param array $link Language => post ID to link as its translation. * @return string An error message, or ''. */ public function apply( int $post_id, string $language, array $unlink, array $link ): string { $linked = Plugin::translations()->translations_of( $post_id ); $taken = isset( $linked[ $language ] ) && $linked[ $language ] !== $post_id; if ( null !== Plugin::languages()->get( $language ) && ! $taken ) { Plugin::translations()->set_language( $post_id, $language ); } foreach ( $unlink as $slug ) { if ( isset( $linked[ $slug ] ) && $linked[ $slug ] !== $post_id ) { Plugin::translations()->unlink( $linked[ $slug ] ); } } foreach ( $link as $slug => $target ) { if ( 0 === $target ) { continue; } if ( ! current_user_can( 'edit_post', $target ) ) { return __( 'You cannot edit that post, so it was not linked.', 'vabelon' ); } $result = Plugin::translations()->link( $post_id, $target, $slug ); if ( is_wp_error( $result ) ) { return $result->get_error_message(); } } return ''; } /** * Show the last error once. * * @return void */ public function notice(): void { $key = 'vabelon_box_error_' . get_current_user_id(); $message = get_transient( $key ); if ( ! is_string( $message ) || '' === $message ) { return; } delete_transient( $key ); echo '

' . esc_html( $message ) . '

'; } }