| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Register asset for wpLingua in front (CSS, JS) |
| 11 |
* |
| 12 |
* @return void |
| 13 |
*/ |
| 14 |
function wplng_register_assets() { |
| 15 |
|
| 16 |
if ( is_admin() || ! wplng_url_is_translatable() ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Enqueue wpLingua JS script |
| 22 |
*/ |
| 23 |
|
| 24 |
wp_enqueue_script( |
| 25 |
'wplingua-script', |
| 26 |
plugins_url() . '/wplingua/assets/js/front.js', |
| 27 |
array(), |
| 28 |
WPLNG_PLUGIN_VERSION |
| 29 |
); |
| 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 |
|
| 44 |
/** |
| 45 |
* Enqueue wpLingua CSS style |
| 46 |
*/ |
| 47 |
|
| 48 |
wp_enqueue_style( |
| 49 |
'wplingua', |
| 50 |
plugins_url() . '/wplingua/assets/css/front.css', |
| 51 |
array(), |
| 52 |
WPLNG_PLUGIN_VERSION |
| 53 |
); |
| 54 |
|
| 55 |
/** |
| 56 |
* Add scripts and CSS for translation editor |
| 57 |
*/ |
| 58 |
|
| 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 |
) { |
| 66 |
|
| 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 |
| 75 |
); |
| 76 |
|
| 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 |
); |
| 97 |
|
| 98 |
} |
| 99 |
} |
| 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; |
| 116 |
} |
| 117 |
|
| 118 |
$custom_css = get_option( 'wplng_custom_css' ); |
| 119 |
|
| 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 |
} |
| 127 |
|
| 128 |
|
| 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; |
| 138 |
} |
| 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>'; |
| 153 |
} |
| 154 |
|