settings->get()->languages() ) < 2 ) { return; } foreach ( $this->terms->taxonomies() as $tax ) { Hooks::filter( "manage_edit-{$tax}_columns", array( $this, 'add_column' ) ); Hooks::filter( "manage_{$tax}_custom_column", array( $this, 'render_column' ), 10, 3 ); } } /** * @param mixed $columns * @return array */ public function add_column( mixed $columns ): array { $columns = (array) $columns; $out = array(); foreach ( $columns as $key => $label ) { $out[ (string) $key ] = (string) $label; if ( 'name' === $key ) { $out[ self::COLUMN ] = __( 'Language', 'blocklingo' ); } } $out[ self::COLUMN ] ??= __( 'Language', 'blocklingo' ); return $out; } public function render_column( mixed $content, mixed $column, mixed $term_id ): mixed { if ( self::COLUMN !== $column ) { return $content; } $term_id = (int) $term_id; $settings = $this->settings->get(); $own = $this->terms->language_of( $term_id ); $group = $this->terms->group( $term_id ); $machine = $settings->machine_ready() && current_user_can( 'publish_posts' ); echo ''; foreach ( $settings->languages() as $code => $lang ) { $mark = '' !== $lang->flag ? $lang->flag : strtoupper( $code ); if ( $code === $own ) { printf( '%s', esc_attr( $lang->name ), esc_html( $mark ) ); } elseif ( isset( $group[ $code ] ) ) { printf( '%s', esc_url( (string) get_edit_term_link( $group[ $code ] ) ), /* translators: %s: language name. */ esc_attr( sprintf( __( 'Edit %s translation', 'blocklingo' ), $lang->name ) ), esc_html( $mark ) ); } else { printf( '+%s', esc_url( self::new_translation_url( $term_id, $code ) ), esc_attr( $machine /* translators: %s: language name. */ ? sprintf( __( 'Add %s translation (machine-translated)', 'blocklingo' ), $lang->name ) /* translators: %s: language name. */ : sprintf( __( 'Add %s translation (copy)', 'blocklingo' ), $lang->name ) ), esc_html( strtoupper( $code ) ) ); } } echo ''; return $content; } public static function new_translation_url( int $term_id, string $lang ): string { return wp_nonce_url( admin_url( 'admin-post.php?action=blocklingo_new_term_translation&term=' . $term_id . '&lang=' . rawurlencode( $lang ) ), 'blocklingo_new_term_translation_' . $term_id ); } /** Handler of the "+UK" links: create the term's translation and open it. */ public function new_translation(): void { $term_id = isset( $_GET['term'] ) ? (int) $_GET['term'] : 0; $lang = isset( $_GET['lang'] ) ? sanitize_key( wp_unslash( $_GET['lang'] ) ) : ''; check_admin_referer( 'blocklingo_new_term_translation_' . $term_id ); if ( ! current_user_can( 'edit_term', $term_id ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'blocklingo' ), 403 ); } $settings = $this->settings->get(); $machine = $settings->machine_ready() && current_user_can( 'publish_posts' ); $mode = $machine ? 'machine' : 'copy'; $new_id = $this->terms->create_translation( $term_id, $lang, $mode ); if ( $new_id instanceof WP_Error ) { wp_die( esc_html( $new_id->get_error_message() ) ); } wp_safe_redirect( (string) get_edit_term_link( $new_id ) ); exit; } }