settings = $settings; } public function register(): void { add_action( 'admin_init', array( $this, 'columns' ) ); add_action( 'restrict_manage_posts', array( $this, 'filter_dropdown' ), 5, 1 ); add_action( 'pre_get_posts', array( $this, 'filter_query' ) ); add_filter( 'posts_where', array( $this, 'filter_where' ), 10, 2 ); add_action( 'admin_post_translateplus_translate', array( $this, 'translate_now' ) ); add_action( 'admin_notices', array( $this, 'notices' ) ); } public function columns(): void { if ( ! Languages::targets() ) { return; } foreach ( PostTypes::enabled() as $type ) { add_filter( "manage_{$type}_posts_columns", array( $this, 'add_column' ) ); add_action( "manage_{$type}_posts_custom_column", array( $this, 'render_column' ), 10, 2 ); add_filter( "bulk_actions-edit-{$type}", array( $this, 'bulk_actions' ) ); add_filter( "handle_bulk_actions-edit-{$type}", array( $this, 'handle_bulk' ), 10, 3 ); } } public function add_column( array $columns ): array { $out = array(); foreach ( $columns as $key => $label ) { $out[ $key ] = $label; if ( 'title' === $key ) { $out['translateplus'] = ' ' . esc_html__( 'Languages', 'translateplus' ); } } return $out; } public function render_column( string $column, int $post_id ): void { if ( 'translateplus' !== $column ) { return; } if ( Groups::is_translation( 'post', $post_id ) ) { $source = Groups::source_id( 'post', $post_id ); $lang = Groups::lang_of( 'post', $post_id ); printf( '%s %s %s', esc_html( strtoupper( strtok( $lang, '-' ) ) ), esc_html__( 'translation of', 'translateplus' ), esc_url( (string) get_edit_post_link( $source ) ), esc_html( get_the_title( $source ) ) ); return; } $members = Groups::members( 'post', $post_id ); echo ''; foreach ( Languages::targets() as $lang ) { $row = $members[ $lang ] ?? null; $id = $row ? (int) $row['object_id'] : 0; $label = strtoupper( strtok( $lang, '-' ) ); $name = Languages::name( $lang ); if ( $id && get_post( $id ) ) { $status = $row['status']; $titles = array( Groups::STATUS_TRANSLATED => __( 'Translated', 'translateplus' ), Groups::STATUS_OUTDATED => __( 'Outdated – source changed', 'translateplus' ), Groups::STATUS_REVIEW => __( 'Needs review', 'translateplus' ), Groups::STATUS_MANUAL => __( 'Edited manually', 'translateplus' ), ); $url = admin_url( 'admin.php?page=translateplus-editor&post=' . $post_id . '&lang=' . rawurlencode( $lang ) ); printf( '%4$s', esc_attr( $status ), esc_url( $url ), esc_attr( $name . ': ' . ( $titles[ $status ] ?? $status ) ), esc_html( $label ) ); } elseif ( Roles::can_translate( $lang ) ) { $url = wp_nonce_url( admin_url( 'admin-post.php?action=translateplus_translate&post=' . $post_id . '&lang=' . rawurlencode( $lang ) ), 'translateplus_translate_' . $post_id ); printf( '%3$s', esc_url( $url ), /* translators: %s: language */ esc_attr( sprintf( __( 'Translate into %s now', 'translateplus' ), $name ) ), esc_html( $label ) ); } } echo ''; } public function filter_dropdown( $post_type ): void { if ( ! PostTypes::is_translatable( (string) $post_type ) || ! Languages::targets() ) { return; } $current = $this->current_filter(); echo ''; echo ''; } private function current_filter(): string { $v = isset( $_GET[ self::FILTER ] ) ? sanitize_text_field( wp_unslash( $_GET[ self::FILTER ] ) ) : Languages::source(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended return 'all' === $v || Languages::is_enabled( $v ) ? $v : Languages::source(); } public function filter_query( \WP_Query $q ): void { global $pagenow; if ( ! is_admin() || 'edit.php' !== $pagenow || ! $q->is_main_query() || ! Languages::targets() ) { return; } $type = (string) ( $q->get( 'post_type' ) ?: 'post' ); if ( ! PostTypes::is_translatable( $type ) ) { return; } $q->set( 'tp_admin_lang', $this->current_filter() ); } public function filter_where( string $where, \WP_Query $q ): string { $lang = (string) $q->get( 'tp_admin_lang' ); if ( '' === $lang || 'all' === $lang ) { return $where; } global $wpdb; return $where . Query::clause( $wpdb->posts, 'ID', 'post', $lang ); } public function bulk_actions( array $actions ): array { if ( current_user_can( 'manage_options' ) ) { $actions['translateplus_translate'] = __( 'Translate with TranslatePlus', 'translateplus' ); } return $actions; } public function handle_bulk( string $redirect, string $action, array $ids ): string { if ( 'translateplus_translate' !== $action || ! current_user_can( 'manage_options' ) ) { return $redirect; } $ids = array_map( static function ( $id ) { return Groups::source_id( 'post', (int) $id ); }, $ids ); $job = Plugin::instance()->jobs()->create( array( 'post_types' => PostTypes::enabled(), 'languages' => Languages::targets(), 'mode' => 'update', 'ids' => array_values( array_unique( $ids ) ), 'include_drafts' => true, ) ); return add_query_arg( 'tp_job', $job, $redirect ); } public function translate_now(): void { $post_id = isset( $_GET['post'] ) ? (int) $_GET['post'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $lang = isset( $_GET['lang'] ) ? sanitize_text_field( wp_unslash( $_GET['lang'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended check_admin_referer( 'translateplus_translate_' . $post_id ); if ( ! Roles::can_translate( $lang ) ) { wp_die( esc_html__( 'You are not allowed to translate into this language.', 'translateplus' ), 403 ); } $back = wp_get_referer() ?: admin_url( 'edit.php?post_type=' . get_post_type( $post_id ) ); try { Plugin::instance()->posts()->translate( $post_id, $lang ); $back = add_query_arg( array( 'tp_done' => rawurlencode( $lang ) ), $back ); } catch ( ApiException $e ) { $back = add_query_arg( array( 'tp_error' => rawurlencode( $e->getMessage() ) ), $back ); } catch ( \Throwable $e ) { $back = add_query_arg( array( 'tp_error' => rawurlencode( $e->getMessage() ) ), $back ); } wp_safe_redirect( $back ); exit; } public function notices(): void { // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['tp_done'] ) ) { $lang = sanitize_text_field( wp_unslash( $_GET['tp_done'] ) ); /* translators: %s: language */ echo '

' . esc_html( sprintf( __( 'Translated into %s.', 'translateplus' ), Languages::name( $lang ) ) ) . '

'; } if ( isset( $_GET['tp_error'] ) ) { echo '

TranslatePlus: ' . esc_html( sanitize_text_field( wp_unslash( $_GET['tp_error'] ) ) ) . '

'; } if ( isset( $_GET['tp_job'] ) ) { $url = admin_url( 'admin.php?page=translateplus-translate' ); /* translators: %s: URL */ echo '

' . wp_kses_post( sprintf( __( 'Translation started in the background. Track progress.', 'translateplus' ), esc_url( $url ) ) ) . '

'; } // phpcs:enable } }