PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.16.6
Translate and Go multilingual – Automatic AI translation – wpLingua v2.16.6
2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 All 111 releases
wplingua / inc / dom / mode-editor.php

mode-editor.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.16.6, at inc/dom/mode-editor.php

144 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * Modify dom for the editor mode
11 *
12 * @param object $dom
13 * @param array $args
14 * @return object
15 */
16 function wplng_dom_mode_editor( $dom, $args ) {
17
18 wplng_args_setup( $args );
19
20 if ( $args['mode'] !== 'editor'
21 || $args['load'] === 'progress'
22 || empty( $args['translations'] )
23 || ! current_user_can( 'edit_posts' )
24 ) {
25 return $dom;
26 }
27
28 /**
29 * Add body class : wplingua-list
30 */
31
32 foreach ( $dom->find( 'body' ) as $element ) {
33 if ( ! empty( $element->class ) ) {
34 $element->class = $element->class . ' wplingua-editor';
35 } else {
36 $element->class = 'wplingua-editor';
37 }
38 }
39
40 /**
41 * Add editor assets
42 */
43
44 $asset_url = add_query_arg(
45 'ver',
46 WPLNG_PLUGIN_VERSION,
47 plugins_url() . '/wplingua/assets/css/editor.css'
48 );
49
50 $asset = '<link';
51 $asset .= ' rel="stylesheet"';
52 $asset .= ' id="wplingua-editor-css"';
53 $asset .= ' href="' . esc_url( $asset_url ) . '"';
54 $asset .= ' type="text/css"';
55 $asset .= '/>';
56
57 foreach ( $dom->find( 'head' ) as $element ) {
58 $element->innertext = $element->innertext . $asset;
59 }
60
61 /**
62 * Replace <a> tags by <span>
63 */
64
65 foreach ( $dom->find( 'a' ) as $element ) {
66
67 $element->setAttribute( 'onclick', 'event.preventDefault()' );
68 $class = 'wplingua-editor-link';
69
70 if ( ! empty( $element->class ) ) {
71 $element->class = $class . ' ' . $element->class;
72 } else {
73 $element->setAttribute( 'class', $class );
74 }
75 }
76
77 /**
78 * Add edit links on text
79 */
80
81 $edit_link_excluded = wplng_data_excluded_editor_link();
82 $node_text_excluded = wplng_data_excluded_node_text();
83
84 foreach ( $dom->find( 'body text' ) as $element ) {
85
86 if ( in_array( $element->parent->tag, $edit_link_excluded )
87 || in_array( $element->parent->tag, $node_text_excluded )
88 ) {
89 continue;
90 }
91
92 $text = wplng_text_esc( $element->innertext );
93
94 if ( ! wplng_text_is_translatable( $text ) ) {
95 continue;
96 }
97
98 foreach ( $args['translations'] as $translation ) {
99
100 if ( ! isset( $translation['post_id'] )
101 || ! isset( $translation['source'] )
102 || ! isset( $translation['translation'] )
103 ) {
104 continue;
105 }
106
107 $source = wplng_text_esc( $translation['source'] );
108
109 if ( $text !== $source ) {
110 continue;
111 }
112
113 $class = 'wplng-edit-link';
114
115 if ( ! empty( $translation['review'] ) ) {
116 $class .= ' wplng-is-review';
117 }
118
119 $innertext = '<span';
120 $innertext .= ' class="' . esc_attr( $class ) . '"';
121 $innertext .= ' data-wplng-post="' . esc_attr( $translation['post_id'] ) . '"';
122 $innertext .= ' title="' . esc_attr__( 'Edit this translation', 'wplingua' ) . '"';
123 $innertext .= '>';
124 $innertext .= esc_html( wplng_text_esc( $translation['translation'] ) );
125 $innertext .= '</span>';
126
127 $element->innertext = $innertext;
128
129 break;
130 }
131 }
132
133 /**
134 * Place the translation edit modale
135 */
136
137 foreach ( $dom->find( 'body' ) as $body ) {
138 $html = wplng_translation_edit_modal_get_html();
139 $body->innertext .= $html;
140 }
141
142 return $dom;
143 }
144