| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Print HTML Option page : wpLingua Switcher |
| 11 |
* |
| 12 |
* @return void |
| 13 |
*/ |
| 14 |
function wplng_option_page_switcher() { |
| 15 |
|
| 16 |
$insert = wplng_get_switcher_insert(); |
| 17 |
$theme = wplng_get_switcher_theme(); |
| 18 |
$style = wplng_get_switcher_style(); |
| 19 |
$name_format = wplng_get_switcher_name_format(); |
| 20 |
$flags_style = wplng_get_switcher_flags_style(); |
| 21 |
$custom_css = get_option( 'wplng_custom_css' ); |
| 22 |
|
| 23 |
if ( empty( $custom_css ) || ! is_string( $custom_css ) ) { |
| 24 |
$custom_css = ''; |
| 25 |
} else { |
| 26 |
$custom_css = wp_strip_all_tags( $custom_css ); |
| 27 |
} |
| 28 |
|
| 29 |
?> |
| 30 |
|
| 31 |
<h1 class="wplin-option-page-title"><span class="dashicons dashicons-translation"></span> <?php esc_html_e( 'wpLingua - Switcher settings', 'wplingua' ); ?></h1> |
| 32 |
|
| 33 |
<div class="wrap"> |
| 34 |
<hr class="wp-header-end"> |
| 35 |
<form method="post" action="options.php"> |
| 36 |
<?php |
| 37 |
settings_fields( 'wplng_switcher' ); |
| 38 |
do_settings_sections( 'wplng_switcher' ); |
| 39 |
?> |
| 40 |
<table class="form-table wplng-form-table"> |
| 41 |
|
| 42 |
<tr> |
| 43 |
<th scope="row"><span class="dashicons dashicons-visibility"></span> <?php esc_html_e( 'Switcher preview', 'wplingua' ); ?></th> |
| 44 |
<td id="wplng-switcher-preview-container"> |
| 45 |
<div class="wplng-switcher-preview"> |
| 46 |
<?php |
| 47 |
echo wplng_get_switcher_html( |
| 48 |
array( 'class' => 'switcher-preview' ) |
| 49 |
); |
| 50 |
?> |
| 51 |
</div> |
| 52 |
</td> |
| 53 |
</tr> |
| 54 |
|
| 55 |
<tr> |
| 56 |
<th scope="row"><span class="dashicons dashicons-admin-appearance"></span> <?php esc_html_e( 'Switcher design', 'wplingua' ); ?></th> |
| 57 |
<td> |
| 58 |
<fieldset> |
| 59 |
|
| 60 |
<label for="wplng_style" class="wplng-fe-50"> |
| 61 |
<strong><?php esc_html_e( 'Switcher style: ', 'wplingua' ); ?></strong> |
| 62 |
</label> |
| 63 |
|
| 64 |
<select id="wplng_style" name="wplng_style" class="wplng-fe-50"> |
| 65 |
<?php |
| 66 |
|
| 67 |
$style_options = wplng_data_switcher_valid_style(); |
| 68 |
|
| 69 |
foreach ( $style_options as $option_value => $option_name ) { |
| 70 |
if ( $style === $option_value ) { |
| 71 |
echo '<option value="' . esc_attr( $option_value ) . '" selected>'; |
| 72 |
} else { |
| 73 |
echo '<option value="' . esc_attr( $option_value ) . '">'; |
| 74 |
} |
| 75 |
echo esc_html( $option_name ); |
| 76 |
echo '</option>'; |
| 77 |
} |
| 78 |
|
| 79 |
?> |
| 80 |
</select> |
| 81 |
|
| 82 |
</fieldset> |
| 83 |
|
| 84 |
<br> |
| 85 |
|
| 86 |
<fieldset> |
| 87 |
<label for="wplng_name_format" class="wplng-fe-50"> |
| 88 |
<strong><?php esc_html_e( 'Displayed name: ', 'wplingua' ); ?></strong> |
| 89 |
</label> |
| 90 |
<select id="wplng_name_format" name="wplng_name_format" class="wplng-fe-50"> |
| 91 |
<?php |
| 92 |
|
| 93 |
$name_format_options = wplng_data_switcher_valid_name_format(); |
| 94 |
|
| 95 |
foreach ( $name_format_options as $option_value => $option_name ) { |
| 96 |
if ( $name_format === $option_value ) { |
| 97 |
echo '<option value="' . esc_attr( $option_value ) . '" selected>'; |
| 98 |
} else { |
| 99 |
echo '<option value="' . esc_attr( $option_value ) . '">'; |
| 100 |
} |
| 101 |
echo esc_html( $option_name ); |
| 102 |
echo '</option>'; |
| 103 |
} |
| 104 |
|
| 105 |
?> |
| 106 |
</select> |
| 107 |
</fieldset> |
| 108 |
|
| 109 |
<br> |
| 110 |
|
| 111 |
<fieldset> |
| 112 |
<label for="wplng_flags_style" class="wplng-fe-50"> |
| 113 |
<strong><?php esc_html_e( 'Flag style: ', 'wplingua' ); ?></strong> |
| 114 |
</label> |
| 115 |
<select id="wplng_flags_style" name="wplng_flags_style" class="wplng-fe-50"> |
| 116 |
<?php |
| 117 |
|
| 118 |
$flags_style_options = wplng_data_switcher_valid_flags_style(); |
| 119 |
|
| 120 |
foreach ( $flags_style_options as $option_value => $option_name ) { |
| 121 |
if ( $flags_style === $option_value ) { |
| 122 |
echo '<option value="' . esc_attr( $option_value ) . '" selected>'; |
| 123 |
} else { |
| 124 |
echo '<option value="' . esc_attr( $option_value ) . '">'; |
| 125 |
} |
| 126 |
echo esc_html( $option_name ); |
| 127 |
echo '</option>'; |
| 128 |
} |
| 129 |
|
| 130 |
?> |
| 131 |
</select> |
| 132 |
</fieldset> |
| 133 |
|
| 134 |
<br> |
| 135 |
|
| 136 |
<fieldset> |
| 137 |
<label for="wplng_theme" class="wplng-fe-50"> |
| 138 |
<strong><?php esc_html_e( 'Switcher theme: ', 'wplingua' ); ?></strong> |
| 139 |
</label> |
| 140 |
|
| 141 |
<select id="wplng_theme" name="wplng_theme" class="wplng-fe-50"> |
| 142 |
<?php |
| 143 |
|
| 144 |
$theme_options = wplng_data_switcher_valid_theme(); |
| 145 |
|
| 146 |
foreach ( $theme_options as $option_value => $option_name ) { |
| 147 |
if ( $theme === $option_value ) { |
| 148 |
echo '<option value="' . esc_attr( $option_value ) . '" selected>'; |
| 149 |
} else { |
| 150 |
echo '<option value="' . esc_attr( $option_value ) . '">'; |
| 151 |
} |
| 152 |
echo esc_html( $option_name ); |
| 153 |
echo '</option>'; |
| 154 |
} |
| 155 |
|
| 156 |
?> |
| 157 |
</select> |
| 158 |
</fieldset> |
| 159 |
</td> |
| 160 |
</tr> |
| 161 |
|
| 162 |
<tr> |
| 163 |
<th scope="row"><span class="dashicons dashicons-editor-code"></span> <?php esc_html_e( 'Custom CSS', 'wplingua' ); ?></th> |
| 164 |
<td> |
| 165 |
<fieldset> |
| 166 |
<label for="wplng_custom_css"> |
| 167 |
<strong><?php esc_html_e( 'Set custom CSS:', 'wplingua' ); ?></strong> |
| 168 |
</label> |
| 169 |
<textarea name="wplng_custom_css" id="wplng_custom_css"><?php echo esc_textarea( $custom_css ); ?></textarea> |
| 170 |
</fieldset> |
| 171 |
</td> |
| 172 |
</tr> |
| 173 |
|
| 174 |
<tr> |
| 175 |
<th scope="row"><span class="dashicons dashicons-admin-post"></span> <?php esc_html_e( 'Switcher insertion', 'wplingua' ); ?></th> |
| 176 |
<td> |
| 177 |
<fieldset> |
| 178 |
|
| 179 |
<label for="wplng_insert" class="wplng-fe-50"> |
| 180 |
<strong><?php _e( 'Automatic insert: ', 'wplingua' ); ?></strong> |
| 181 |
</label> |
| 182 |
|
| 183 |
<select id="wplng_insert" name="wplng_insert" class="wplng-fe-50"> |
| 184 |
<?php |
| 185 |
|
| 186 |
$insert_options = wplng_data_switcher_valid_insert(); |
| 187 |
|
| 188 |
foreach ( $insert_options as $option_value => $option_name ) { |
| 189 |
if ( $insert === $option_value ) { |
| 190 |
echo '<option value="' . esc_attr( $option_value ) . '" selected>'; |
| 191 |
} else { |
| 192 |
echo '<option value="' . esc_attr( $option_value ) . '">'; |
| 193 |
} |
| 194 |
echo esc_html( $option_name ); |
| 195 |
echo '</option>'; |
| 196 |
} |
| 197 |
|
| 198 |
?> |
| 199 |
</select> |
| 200 |
|
| 201 |
</fieldset> |
| 202 |
<br> |
| 203 |
<p> |
| 204 |
<strong class="wplng-fe-50"><?php esc_html_e( 'Shortcode switcher: ', 'wplingua' ); ?></strong> |
| 205 |
<code class="wplng-fe-50">[wplng_switcher]</code> |
| 206 |
</p> |
| 207 |
|
| 208 |
</td> |
| 209 |
</tr> |
| 210 |
|
| 211 |
</table> |
| 212 |
<?php submit_button(); ?> |
| 213 |
</form> |
| 214 |
</div> |
| 215 |
<?php |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* Print HTML subsection of Option page : wpLingua Switcher - Flags style |
| 221 |
* |
| 222 |
* @param string $old_flags_style Flag URL |
| 223 |
* @param string $new_flags_style Flag URL |
| 224 |
* @return void |
| 225 |
*/ |
| 226 |
function wplng_options_switcher_update_flags_style( $old_flags_style, $new_flags_style ) { |
| 227 |
|
| 228 |
if ( $old_flags_style === $new_flags_style ) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
if ( 'none' === $new_flags_style ) { |
| 233 |
$new_flags_style = 'rectangular'; |
| 234 |
} |
| 235 |
|
| 236 |
if ( 'none' === $old_flags_style ) { |
| 237 |
$old_flags_style = 'rectangular'; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Replace flag URL for website language |
| 242 |
*/ |
| 243 |
|
| 244 |
$website_flag = wplng_get_language_website_flag(); |
| 245 |
|
| 246 |
$website_flag = str_replace( |
| 247 |
'/wplingua/assets/images/' . $old_flags_style . '/', |
| 248 |
'/wplingua/assets/images/' . $new_flags_style . '/', |
| 249 |
$website_flag |
| 250 |
); |
| 251 |
|
| 252 |
$website_flag = sanitize_url( $website_flag ); |
| 253 |
|
| 254 |
update_option( 'wplng_website_flag', $website_flag ); |
| 255 |
|
| 256 |
/** |
| 257 |
* Replace flag URL for target languages |
| 258 |
*/ |
| 259 |
|
| 260 |
$target_languages_json = get_option( 'wplng_target_languages' ); |
| 261 |
|
| 262 |
if ( empty( $target_languages_json ) ) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
$target_languages = json_decode( |
| 267 |
$target_languages_json, |
| 268 |
true |
| 269 |
); |
| 270 |
|
| 271 |
if ( empty( $target_languages ) || ! is_array( $target_languages ) ) { |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
foreach ( $target_languages as $key => $target_language ) { |
| 276 |
if ( ! empty( $target_language['flag'] ) |
| 277 |
&& is_string( $target_language['flag'] ) |
| 278 |
) { |
| 279 |
|
| 280 |
$old_src = sanitize_url( $target_language['flag'] ); |
| 281 |
|
| 282 |
$new_src = str_replace( |
| 283 |
'/' . $old_flags_style . '/', |
| 284 |
'/' . $new_flags_style . '/', |
| 285 |
$old_src |
| 286 |
); |
| 287 |
|
| 288 |
$new_src = sanitize_url( $new_src ); |
| 289 |
|
| 290 |
$target_languages[ $key ]['flag'] = $new_src; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
$target_languages_json = wp_json_encode( |
| 295 |
$target_languages, |
| 296 |
JSON_UNESCAPED_SLASHES |
| 297 |
); |
| 298 |
|
| 299 |
update_option( 'wplng_target_languages', $target_languages_json ); |
| 300 |
} |
| 301 |
|