PluginProbe
Translation with DeepL API / trunk
Translation with DeepL API vtrunk
trunk 0.1.20180323 1.0 1.2.5.1 1.5.2.5 1.7.5 2.4.3.12 2.4.3.9 2.4.5
wpdeepl / admin / deepl-admin-functions.php

deepl-admin-functions.php in Translation with DeepL API trunk, at admin/deepl-admin-functions.php

188 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3 function wpdeepl_test_admin() {
4 return;
5 }
6
7
8
9
10
11 function wpdeepl_language_selector(
12 $type = 'target',
13 $css_id = 'deepl_language_selector',
14 $selected = false,
15 $not_selected = false,
16 $forbid_auto = false
17 ) {
18 $languages = DeepLConfiguration::DefaultsAllLanguages();
19
20 $wp_locale = get_locale();
21
22 $default_target_language = DeepLConfiguration::getDefaultTargetLanguage();
23
24 if ( 'target' === $type && false === $selected ) {
25 $selected = $default_target_language;
26 }
27
28 $html = "";
29
30 // 1. Sécurisation des attributs ID et NAME
31 $html .= "\n" . '<select id="' . esc_attr( $css_id ) . '" name="' . esc_attr( $css_id ) . '" class="deepl_translate_form">';
32
33 if ( 'source' === $type ) {
34 if( ! $forbid_auto ) {
35 // 2. Sécurisation des textes traduits
36 if( ! defined('WPDEEPLPRO_NAME') || ! DeepLConfiguration::usingGlossaries() ) {
37 $html .= '
38 <option value="auto">' . esc_html__( 'Automatic', 'wpdeepl' ) . '</option>';
39 }
40 else {
41 $html .= '
42 <option value="auto">' . esc_html__( 'Automatic (no glossary)', 'wpdeepl' ) . '</option>';
43 }
44 }
45 }
46
47 $languages_to_display = DeepLConfiguration::getDisplayedLanguages();
48
49 foreach ( $languages as $ln_id => $language ) {
50
51 if ( $languages_to_display && ! in_array( $ln_id, $languages_to_display, true ) ) {
52 continue;
53 }
54
55 if (
56 $default_target_language
57 && $ln_id == $default_target_language
58 && 'source' === $type
59 ) {
60 //continue;
61 }
62
63 // 3. Sécurisation de la valeur de l'option
64 $html .= '
65 <option value="' . esc_attr( $ln_id ) .'"';
66
67 if ( $ln_id == $selected && $ln_id != $not_selected ) {
68 $html .= ' selected="selected"';
69 }
70
71 $label = ( $wp_locale && isset( $language['labels'][$wp_locale] )) ? $language['labels'][$wp_locale] : $language['labels']['fr_FR'];
72
73 // 4. Sécurisation impérative du label affiché (FAIL fréquent du PCP ici)
74 $html .= '>' . esc_html( $label ) . '</option>';
75 }
76
77 if ( 'target' === $type ) {
78 // Correction typo + escaping
79 $html .= '
80 <option value="notranslation">' . esc_html__( "Don't translate", 'wpdeepl' ) . '</option>';
81 }
82
83 $html .="\n</select>";
84
85 return $html;
86 }
87
88 function wpdeepl_show_clear_logs_button() {
89 ?>
90 <p class="submit">
91 <button name="clear_logs" class="button-primary" type="submit" value="clear_logs"><?php esc_html_e('Clear logs', 'wpdeepl'); ?></button>
92 </p>
93 <?php
94 }
95
96
97
98 function wpdeepl_clear_logs() {
99 $log_files = glob( trailingslashit( WPDEEPL_FILES ) .'*.log');
100 if ($log_files) foreach ( $log_files as $log_file) {
101 wp_delete_file($log_file);
102 }
103 ?>
104 <div class="notice notice-success"><p><?php esc_html_e('Log files deleted', 'wpdeepl'); ?></p></div>
105 <?php
106 }
107 function wpdeepl_log( $bits, $type ) {
108 $log_lines = array_merge(array('date' => gmdate('d/m/Y H:i:s')), $bits);
109 $log_line = serialize($log_lines) . "\n";
110 $type = html_entity_decode( $type );
111 $log_file = trailingslashit( WPDEEPL_FILES ) . gmdate( 'Y-m' ) . '-' . $type . '.log';
112 file_put_contents( $log_file, $log_line, FILE_APPEND );
113 }
114
115
116 function wpdeepl_prune_logs() {
117 if ( !current_user_can( 'manage_options' ) ) {
118 return false;
119 }
120
121 $nonce_value = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
122 if ( ! $nonce_value || ! wp_verify_nonce( $nonce_value, 'prune_logs' ) ) {
123 return false;
124 }
125
126 $logs = glob( trailingslashit( WPDEEPL_FILES ) . '*.log');
127 if ($logs) foreach ($logs as $log_file) {
128 $file_name = basename( $log_file );
129 if (preg_match('#(\d+)-(\d+)-(\w+)\.log#', $file_name, $match)) {
130 //$date = $match[2] . '/' . $match[1];
131
132 $log_time = mktime(0, 0, 0, $match[2], 1, $match[1] );
133
134 $first_day_of_the_month = new DateTime('first day of this month');
135 $first_day_of_the_month->modify('- 1 day');
136 $first_day_time = $first_day_of_the_month->getTimestamp();
137
138 if ( $log_time < $first_day_time ) {
139 //echo " <br />SUPPRESSION $log_file : " . date('Y-m-d H:i:s', $log_time) . " < " . date('Y-m-d H:i:s', $first_day_time );
140 wp_delete_file( $log_file );
141 }
142 }
143 }
144 }
145
146
147 function wpdeepl_display_logs() {
148
149
150 ?>
151 <h3 class="wc-settings-sub-title" id="logs"><?php esc_html_e('Logs','wpdeepl'); ?></h3>
152 <?php
153
154 $log_files = glob( trailingslashit( WPDEEPL_FILES ) .'*.log');
155 if ($log_files) {
156 foreach ($log_files as $log_file) {
157 $file_name = basename( $log_file );
158 $contents = file_get_contents( $log_file );
159 if (preg_match('#(\d+)-(\d+)-(\w+)\.log#', $file_name, $match)) {
160 $date = $match[2] . '/' . $match[1];
161 ?>
162 <h3><?php
163 echo esc_html(
164 sprintf(
165 /* translators: 1. file name 2. month */
166 __("File '%1\$s' for %2\$s", 'wpdeepl' ),
167 $match[3],
168 $date
169 )
170 );
171 ?>
172 </h3><?php
173
174 $lines = explode("\n", $contents);
175 foreach ($lines as $line) {
176 wpdeepl_debug_display(unserialize($line));
177 }
178
179 }
180
181 }
182 }
183 else {
184 esc_html_e( 'No log files', 'wpdeepl' );
185 }
186 }
187
188