| @@ -12,29 +12,36 @@ | ||
| 12 | 12 | * @return void |
| 13 | 13 | */ |
| 14 | 14 | function wplng_register_assets() { |
| 15 | 15 | |
| 16 | - if ( is_admin() ) { | |
| 16 | + if ( is_admin() || ! wplng_url_is_translatable() ) { | |
| 17 | 17 | return; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | - * Enqueue jQuery | |
| 22 | - */ | |
| 23 | - | |
| 24 | - wp_enqueue_script( 'jquery' ); | |
| 25 | - | |
| 26 | - /** | |
| 27 | 21 | * Enqueue wpLingua JS script |
| 28 | 22 | */ |
| 29 | 23 | |
| 30 | 24 | wp_enqueue_script( |
| 31 | 25 | 'wplingua-script', |
| 32 | - plugins_url() . '/wplingua/assets/js/script.js', | |
| 33 | - array( 'jquery' ), | |
| 34 | - WPLNG_API_VERSION | |
| 26 | + plugins_url() . '/wplingua/assets/js/front.js', | |
| 27 | + array(), | |
| 28 | + WPLNG_PLUGIN_VERSION | |
| 35 | 29 | ); |
| 36 | 30 | |
| 31 | + wp_add_inline_script( | |
| 32 | + 'wplingua-script', | |
| 33 | + 'window.wplngCookiePath=' . wp_json_encode( COOKIEPATH ) . ';', | |
| 34 | + 'before' | |
| 35 | + ); | |
| 36 | + | |
| 37 | + // The load-in-progress script relies on jQuery and is injected in translated pages. | |
| 38 | + if ( current_user_can( 'edit_posts' ) | |
| 39 | + && get_option( 'wplng_load_in_progress', false ) | |
| 40 | + ) { | |
| 41 | + wp_enqueue_script( 'jquery' ); | |
| 42 | + } | |
| 43 | + | |
| 37 | 44 | /** |
| 38 | 45 | * Enqueue wpLingua CSS style |
| 39 | 46 | */ |
| 40 | 47 | |
| @@ -41,48 +48,106 @@ | ||
| 41 | 48 | wp_enqueue_style( |
| 42 | 49 | 'wplingua', |
| 43 | 50 | plugins_url() . '/wplingua/assets/css/front.css', |
| 44 | 51 | array(), |
| 45 | - WPLNG_API_VERSION | |
| 52 | + WPLNG_PLUGIN_VERSION | |
| 46 | 53 | ); |
| 47 | 54 | |
| 48 | 55 | /** |
| 49 | - * Load assets for visual editor | |
| 56 | + * Add scripts and CSS for translation editor | |
| 50 | 57 | */ |
| 51 | 58 | |
| 52 | - if ( isset( $_GET['wplingua-editor'] ) ) { | |
| 59 | + if ( ! empty( $_GET['wplng-mode'] ) | |
| 60 | + && ( | |
| 61 | + 'editor' === $_GET['wplng-mode'] | |
| 62 | + || 'list' === $_GET['wplng-mode'] | |
| 63 | + ) | |
| 64 | + && current_user_can( 'edit_posts' ) | |
| 65 | + ) { | |
| 53 | 66 | |
| 54 | - wp_enqueue_style( | |
| 55 | - 'wplingua-editor', | |
| 56 | - plugins_url() . '/wplingua/assets/css/editor.css', | |
| 57 | - array(), | |
| 58 | - WPLNG_API_VERSION | |
| 67 | + wp_enqueue_style( 'dashicons' ); | |
| 68 | + wp_enqueue_script( 'jquery' ); | |
| 69 | + | |
| 70 | + wp_enqueue_script( | |
| 71 | + 'wplingua-translation', | |
| 72 | + plugins_url() . '/wplingua/assets/js/admin/edit-translation.js', | |
| 73 | + array( 'jquery' ), | |
| 74 | + WPLNG_PLUGIN_VERSION | |
| 59 | 75 | ); |
| 60 | 76 | |
| 61 | - } elseif ( isset( $_GET['wplingua-list'] ) ) { | |
| 77 | + wp_localize_script( | |
| 78 | + 'wplingua-translation', | |
| 79 | + 'wplngI18nTranslation', | |
| 80 | + array( | |
| 81 | + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), | |
| 82 | + 'currentLanguage' => wplng_get_language_current_id(), | |
| 83 | + 'message' => array( | |
| 84 | + 'exitPage' => esc_html__( | |
| 85 | + 'You are about to leave the page without saving your changes. They will be lost if you continue. Would you like to leave the page anyway?', | |
| 86 | + 'wplingua' | |
| 87 | + ), | |
| 88 | + 'exitEditorModal' => esc_html__( | |
| 89 | + 'You are about to exit without saving your changes. They will be lost if you continue. Would you like to leave anyway?', | |
| 90 | + 'wplingua' | |
| 91 | + ), | |
| 92 | + 'buttonSave' => esc_html__( 'Save', 'wplingua' ), | |
| 93 | + 'buttonSaveInProgress' => esc_html__( 'Save in progress...', 'wplingua' ), | |
| 94 | + ), | |
| 95 | + ) | |
| 96 | + ); | |
| 62 | 97 | |
| 63 | - wp_enqueue_style( | |
| 64 | - 'wplingua-list', | |
| 65 | - plugins_url() . '/wplingua/assets/css/list.css', | |
| 66 | - array(), | |
| 67 | - WPLNG_API_VERSION | |
| 68 | - ); | |
| 98 | + } | |
| 99 | +} | |
| 69 | 100 | |
| 101 | + | |
| 102 | +/** | |
| 103 | + * Add custom inline styles for wpLingua. | |
| 104 | + * | |
| 105 | + * This function retrieves custom CSS from the database (stored in the 'wplng_custom_css' option), | |
| 106 | + * sanitizes it, and outputs it directly into the <head> section of the page. | |
| 107 | + * | |
| 108 | + * @return void | |
| 109 | + */ | |
| 110 | +function wplng_add_custom_inline_styles() { | |
| 111 | + | |
| 112 | + if ( ! empty( $_GET['wplng-mode'] ) | |
| 113 | + && 'list' === $_GET['wplng-mode'] | |
| 114 | + ) { | |
| 115 | + return; | |
| 70 | 116 | } |
| 71 | 117 | |
| 72 | - /** | |
| 73 | - * Add inline style for wpLingua custom CSS | |
| 74 | - */ | |
| 118 | + $custom_css = get_option( 'wplng_custom_css' ); | |
| 75 | 119 | |
| 76 | - if ( ! isset( $_GET['wplingua-list'] ) ) { | |
| 120 | + if ( ! empty( $custom_css ) && is_string( $custom_css ) ) { | |
| 121 | + $custom_css = wp_strip_all_tags( $custom_css ); | |
| 122 | + // Prevent breaking out of the <style> context | |
| 123 | + $custom_css = str_replace( '</style', '', $custom_css ); | |
| 124 | + echo '<style id="wplingua-inline-styles">' . $custom_css . '</style>'; | |
| 125 | + } | |
| 126 | +} | |
| 77 | 127 | |
| 78 | - $custom_css = get_option( 'wplng_custom_css' ); | |
| 79 | 128 | |
| 80 | - if ( ! empty( $custom_css ) | |
| 81 | - && is_string( $custom_css ) | |
| 82 | - ) { | |
| 83 | - $custom_css = wp_strip_all_tags( $custom_css ); | |
| 84 | - wp_add_inline_style( 'wplingua', $custom_css ); | |
| 85 | - } | |
| 129 | +/** | |
| 130 | + * Print the on page scrip | |
| 131 | + * | |
| 132 | + * @return void | |
| 133 | + */ | |
| 134 | +function wplng_on_page_script() { | |
| 135 | + | |
| 136 | + if ( ! empty( $_GET['wplng-load'] ) ) { | |
| 137 | + return; | |
| 86 | 138 | } |
| 87 | 139 | |
| 140 | + $script = file_get_contents( WPLNG_PLUGIN_DIR . '/assets/js/on-page.js' ); | |
| 141 | + | |
| 142 | + if ( empty( $script ) ) { | |
| 143 | + return; | |
| 144 | + } | |
| 145 | + | |
| 146 | + $script = str_replace( | |
| 147 | + array( '[admin-ajax-php]', '[wplng-cookie-path]', '//# sourceMappingURL=on-page.js.map' ), | |
| 148 | + array( admin_url( 'admin-ajax.php' ), COOKIEPATH, '' ), | |
| 149 | + $script | |
| 150 | + ); | |
| 151 | + | |
| 152 | + echo '<script id="wplingua-js-on-page">' . rtrim( $script ) . '</script>'; | |
| 88 | 153 | } |