| 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 |
|