settings = $settings; $this->posts = $posts; } public function register(): void { add_action( 'init', array( $this, 'maybe_start' ), 5 ); add_action( 'admin_bar_menu', array( $this, 'admin_bar' ), 90 ); } private function requested(): bool { return isset( $_GET[ self::PARAM ] ) && ! is_admin(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } public function maybe_start(): void { if ( ! $this->requested() || ! is_user_logged_in() || Languages::is_source_current() || ! Roles::can_translate( Languages::current() ) ) { return; } Plugin::instance()->strings()->track_seen( true ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) ); add_action( 'wp_footer', array( $this, 'data' ), 9999 ); add_filter( 'show_admin_bar', '__return_true' ); nocache_headers(); } public function enqueue(): void { wp_enqueue_style( 'translateplus-visual', TRANSLATEPLUS_URL . 'assets/css/visual-editor.css', array(), TRANSLATEPLUS_VERSION ); wp_enqueue_script( 'translateplus-visual', TRANSLATEPLUS_URL . 'assets/js/visual-editor.js', array( 'wp-api-fetch' ), TRANSLATEPLUS_VERSION, true ); } public function data(): void { $lang = Languages::current(); $segments = array(); $source = 0; if ( is_singular() ) { $id = (int) get_queried_object_id(); // Segment text only for posts the user may access (never password-protected content they cannot edit). if ( Groups::lang_of( 'post', $id ) === $lang && Roles::can_access_post( $id ) && ! post_password_required( $id ) ) { $source = Groups::source_id( 'post', $id ); foreach ( Segments::get( 'post', $source, $lang ) as $key => $row ) { $segments[] = array( 'type' => 'segment', 'key' => $key, 'source' => $row['source'], 'translation' => $row['translation'], 'status' => $row['status'], ); } } } $strings = array(); foreach ( Plugin::instance()->strings()->seen() as $s ) { $strings[] = array( 'type' => 'string', 'hash' => $s['hash'], 'domain' => $s['domain'], 'source' => $s['source'], 'translation' => $s['translation'], ); } $exit = Router::instance()->current_url_in( $lang ); $data = array( 'lang' => $lang, 'logo' => TRANSLATEPLUS_URL . 'assets/images/logo.png', 'langName' => Languages::name( $lang ), 'rtl' => Languages::is_rtl( $lang ), 'sourceId' => $source, 'items' => array_merge( $segments, $strings ), 'exitUrl' => remove_query_arg( self::PARAM, $exit ), 'editorUrl' => $source ? admin_url( 'admin.php?page=translateplus-editor&post=' . $source . '&lang=' . rawurlencode( $lang ) ) : '', 'i18n' => array( 'title' => __( 'Visual translation editor', 'translateplus' ), 'original' => __( 'Original', 'translateplus' ), 'translation'=> __( 'Translation', 'translateplus' ), 'save' => __( 'Save', 'translateplus' ), 'saving' => __( 'Saving…', 'translateplus' ), 'saved' => __( 'Saved', 'translateplus' ), 'cancel' => __( 'Cancel', 'translateplus' ), 'exit' => __( 'Exit editor', 'translateplus' ), 'hint' => __( 'Click any highlighted text to edit its translation.', 'translateplus' ), 'found' => __( 'editable items on this page', 'translateplus' ), 'fullEditor' => __( 'Open side-by-side editor', 'translateplus' ), 'error' => __( 'Could not save. Please try again.', 'translateplus' ), 'pending' => __( 'Saved. Waiting for a reviewer before it appears on the site.', 'translateplus' ), ), ); // Printed last so it includes every string rendered on the page; the editor script waits for DOMContentLoaded. wp_print_inline_script_tag( 'window.translateplusVisual = ' . wp_json_encode( $data ) . ';', array( 'id' => 'translateplus-visual-data' ) ); } /** * "Translate page" entry in the admin bar. * * @param \WP_Admin_Bar $bar Admin bar. */ public function admin_bar( $bar ): void { if ( is_admin() || ! Languages::targets() || ! Roles::can_translate() ) { return; } $router = Router::instance(); $bar->add_node( array( 'id' => 'translateplus', 'title' => '' . esc_html__( 'Translate page', 'translateplus' ), 'href' => Languages::is_source_current() ? false : add_query_arg( self::PARAM, 1, $router->current_url_in( Languages::current() ) ), ) ); foreach ( Roles::user_languages() as $lang ) { $bar->add_node( array( 'parent' => 'translateplus', 'id' => 'translateplus-' . $lang, /* translators: %s: language name */ 'title' => esc_html( sprintf( __( 'Edit %s on this page', 'translateplus' ), Languages::name( $lang ) ) ), 'href' => add_query_arg( self::PARAM, 1, $router->language_url( $lang ) ), ) ); } $bar->add_node( array( 'parent' => 'translateplus', 'id' => 'translateplus-dashboard', 'title' => esc_html__( 'TranslatePlus dashboard', 'translateplus' ), 'href' => admin_url( 'admin.php?page=translateplus' ), ) ); } }