PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.12.1
Translate and Go multilingual – Automatic AI translation – wpLingua v2.12.1
2.16.7 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 All 112 releases
wplingua / inc / dom / mode-editor.php

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

142 lines 2.9 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[class]' ) as $element ) {
33 $element->class = $element->class . ' wplingua-editor';
34 }
35
36 /**
37 * Add editor assets
38 */
39
40 $asset_url = add_query_arg(
41 'ver',
42 WPLNG_PLUGIN_VERSION,
43 plugins_url() . '/wplingua/assets/css/editor.css'
44 );
45
46 $asset = '<link';
47 $asset .= ' rel="stylesheet"';
48 $asset .= ' id="wplingua-editor-css"';
49 $asset .= ' href="' . esc_url( $asset_url ) . '"';
50 $asset .= ' type="text/css"';
51 $asset .= '/>';
52
53 foreach ( $dom->find( 'head' ) as $element ) {
54 $element->innertext = $element->innertext . $asset;
55 }
56
57 /**
58 * Replace <a> tags by <span>
59 */
60
61 foreach ( $dom->find( 'a' ) as $element ) {
62
63 $element->setAttribute( 'onclick', 'event.preventDefault()' );
64 $class = 'wplingua-editor-link';
65
66 if ( ! empty( $element->class ) ) {
67 $class = esc_attr( $class . ' ' . $element->class );
68
69 $element->class = $class;
70 } else {
71 $element->setAttribute( 'class', $class );
72 }
73 }
74
75 /**
76 * Add edit links on text
77 */
78
79 $edit_link_excluded = wplng_data_excluded_editor_link();
80 $node_text_excluded = wplng_data_excluded_node_text();
81
82 foreach ( $dom->find( 'body text' ) as $element ) {
83
84 if ( in_array( $element->parent->tag, $edit_link_excluded )
85 || in_array( $element->parent->tag, $node_text_excluded )
86 ) {
87 continue;
88 }
89
90 $text = wplng_text_esc( $element->innertext );
91
92 if ( ! wplng_text_is_translatable( $text ) ) {
93 continue;
94 }
95
96 foreach ( $args['translations'] as $translation ) {
97
98 if ( ! isset( $translation['post_id'] )
99 || ! isset( $translation['source'] )
100 || ! isset( $translation['translation'] )
101 ) {
102 continue;
103 }
104
105 $source = wplng_text_esc( $translation['source'] );
106
107 if ( $text !== $source ) {
108 continue;
109 }
110
111 $class = 'wplng-edit-link';
112
113 if ( ! empty( $translation['review'] ) ) {
114 $class .= ' wplng-is-review';
115 }
116
117 $innertext = '<span';
118 $innertext .= ' class="' . esc_attr( $class ) . '"';
119 $innertext .= ' data-wplng-post="' . esc_attr( $translation['post_id'] ) . '"';
120 $innertext .= ' title="' . esc_attr__( 'Edit this translation', 'wplingua' ) . '"';
121 $innertext .= '>';
122 $innertext .= esc_html( wplng_text_esc( $translation['translation'] ) );
123 $innertext .= '</span>';
124
125 $element->innertext = $innertext;
126
127 break;
128 }
129 }
130
131 /**
132 * Place the translation edit modale
133 */
134
135 foreach ( $dom->find( 'body' ) as $body ) {
136 $html = wplng_translation_edit_modal_get_html();
137 $body->innertext .= $html;
138 }
139
140 return $dom;
141 }
142