| @@ -1,141 +1,143 @@ | ||
| 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 | -} | |
| 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 | +} | |