PluginProbe
Translation with DeepL API / 2.4.3.12
Translation with DeepL API v2.4.3.12
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 / settings / wp-improved-settings.class.php

wp-improved-settings.class.php in Translation with DeepL API 2.4.3.12, at settings/wp-improved-settings.class.php

639 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Improved Settings
4 *
5 * @package WP_Improved_Settings
6 * @version 20221115
7 *
8 * 20221115 esc all
9 * 20210111 ajout wpimpsettings_find_option_like
10 * 20200320 ajout des setting en tableau
11 * 20190705 footer actions prise en compte des méthodes
12 * 201991125 plugin_text_domain supprimé
13 * 20191201 plugin paths
14 */
15
16 namespace WP_Improved_Settings;
17
18 if ( !function_exists( 'WP_Improved_Settings\zebench_get_plugin_paths' ) ) {
19 function zebench_get_plugin_paths() {
20 $array = apply_filters( 'zebench_get_plugin_paths', array() );
21 return $array;
22 }
23 }
24
25 if( !function_exists('WP_Improved_Settings\wpimpsettings_find_all_options_like') ){
26 function wpimpsettings_find_all_options_like( $string ) {
27 // ugly hack to fetch plugin_name_index options
28 global $wpdb;
29 $search = $wpdb->_real_escape( $string );
30 $sql = "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '%$search%'";
31
32 $results = $wpdb->get_results( $sql, ARRAY_A );
33 return $results;
34 }
35 }
36
37 if( !function_exists('WP_Improved_Settings\wpimpsettings_find_option_like') ){
38 function wpimpsettings_find_option_like( $string ) {
39 // ugly hack to fetch plugin_name_index options
40 global $wpdb;
41 $search = $wpdb->_real_escape( $string );
42 $sql = "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '$search%'";
43
44 $results = $wpdb->get_results( $sql, ARRAY_A );
45 $return = array();
46 if( $results ) foreach ( $results as $result ) {
47 $name = str_replace($string .'_', '', $result['option_name'] );
48 $explode = explode('_', $name );
49
50 $value = $result['option_value'];
51 $return[$explode[0]][$explode[1]] = $value;
52 }
53 return $return;
54 }
55 }
56
57 if ( !class_exists( 'WP_Improved_Settings\WP_Improved_Settings' ) ) {
58 class WP_Improved_Settings {
59 // loosely based on wc-dynamic-pricing-and-discounts/classes/rp-wcdpd-settings.class.php
60 public $settingsStructure = array();
61 public $extendedActions = array();
62
63
64 public $plugins_paths = array();
65
66 public $WC_Improved_Settings_API;
67
68 public $isMainMenu = false;
69 public $plugin_id;
70 public $menu_order = 20;
71 public $minimum_capability = 'manage_options';
72 public $option_page = '';
73 public $defaultSettingsTab = '';
74 public $parent_menu = '';
75 public $post_type = false;
76
77
78 /*Dashboard: 'index.php'
79 Posts: 'edit.php'
80 Media: 'upload.php'
81 Pages: 'edit.php?post_type=page'
82 Comments: 'edit-comments.php'
83 Custom Post Types: 'edit.php?post_type=your_post_type'
84 Appearance: 'themes.php'
85 Plugins: 'plugins.php'
86 Users: 'users.php'
87 Tools: 'tools.php'
88 Settings: 'options-general.php'
89 Network Settings: 'settings.php'
90 WooCommerce : 'woocommerce'
91 */
92
93 public function __construct() {
94 // Register settings
95
96 add_action( 'admin_init', array( $this, 'loadSettings' ) );
97 add_action( 'admin_init', array( $this, 'registerSettings' ) );
98
99 $this->plugins_paths = apply_filters('zebench_plugins_paths', array() );
100
101 // Add link to menu
102
103 global $wp_filter;
104 $real_order = $this->menu_order;
105 while( isset( $wp_filter['admin_menu']->callbacks[$real_order] ) ) {
106 $real_order++;
107 }
108 add_action( 'admin_menu', array( $this, 'addToMenu' ), $real_order );
109
110 // Pass configuration to Javascript
111 //add_action( 'admin_enqueue_scripts', array( $this, 'configuration_to_javascript' ), 999 );
112
113 // Enqueue templates to be rendered in footer
114 //add_action( 'admin_footer', array( $this, 'render_templates_in_footer' ) );
115
116 // Settings export call
117 if ( !empty( $_REQUEST['export_settings'] ) ) {
118 add_action( 'wp_loaded', array( $this, 'export' ) );
119 }
120
121 // Settings import call
122 if ( !empty( $_FILES[$this->option_page]['name']['import'] ) ) {
123 add_action( 'wp_loaded', array( $this, 'import' ) );
124 }
125
126 // Print settings import notice
127 if ( isset( $_REQUEST[$this->option_page .'_imported'] ) ) {
128 add_action( 'admin_notices', array( $this, 'print_import_notice' ) );
129 }
130
131 if ( !class_exists( 'WP_Improved_Settings\WC_Improved_Settings_API' )) {
132 require_once( dirname( __FILE__ ) . '/wp-improved-settings-api.class.php' );
133 }
134 $this->WC_Improved_Settings_API = new WC_Improved_Settings_API( $this->getPluginID(), $this->getSettingsStructure() );
135
136
137 $key_name = $this->plugin_id . '_options_save';
138 if ( isset( $_REQUEST['save'] ) && isset( $_REQUEST[$key_name] ) && $_REQUEST[$key_name] ) {
139 $this->saveSettings();
140 // echo " saving";
141 if ( method_exists( $this, 'on_save' ) ) {
142 // echo "on save update";
143 $this->on_save();
144 }
145
146 add_action( 'admin_notices', array( $this, 'print_saved_notice' ) );
147 }
148
149 add_action( 'admin_notices', array( $this, 'maybe_print_notices' ) );
150
151 // Migration notices
152 //add_action( 'admin_notices', array( $this, 'maybe_display_migration_notice' ), 1 );
153
154 // Delete migration notice
155 //$this->hide_migration_notice();
156 }
157
158 function getPluginID() {
159 return $this->plugin_id;
160 }
161
162 function getOptionPage() {
163 return $this->option_page;
164 }
165
166 function getMinimumCapability() {
167 return $this->minimum_capability;
168 }
169
170 function saveSettings() {
171 $nonce = isset( $_REQUEST['_zenonce'] ) ? $_REQUEST['_zenonce'] : false;
172 if ( ! wp_verify_nonce( $nonce, 'zesave_settings' ) ) {
173 $error_msg = __( 'Unable to submit this form, please refresh and try again.' );
174 return;
175 }
176
177 $this->WC_Improved_Settings_API->process_admin_options();
178 //$this->process_admin_options();
179 }
180 /*
181 function me() {
182 $this->loadSettings();
183 $this->WC_Improved_Settings_API = WC_Improved_Settings_API( $this->getPluginID(), $this->getSettingsStructure() );
184 return $this->WC_Improved_Settings_API->process_admin_options();
185 }*/
186
187 function maybe_print_notices() {
188 }
189
190 function print_saved_notice() {
191 ?>
192 <div id="message" class="updated notice is-dismissible"><p><strong><?php _e( 'Settings saved.' ); ?></strong></p></div>
193 <?php
194 }
195
196 public function loadSettings() {
197 // load settings into $this sttings ?
198 $this->settingsStructure = $this->getSettingsStructure();
199 //plouf( $this->settingsStructure ); die( 'oka6z4e4z64ze4' );
200 }
201
202 /**
203 * Add Settings link to menu
204 *
205 * @access public
206 * @return voidaddToMenu
207 */
208 public function addToMenu() {
209 //die( 'menu to '.$this->parent_menu . ' page title = ' . $this->getPageTitle() .' menu = ' . $this->getMenuTitle() . ' cap ' . 'manage_options' . ' option page = ' . $this->getOptionPage() );
210 add_submenu_page(
211 $this->parent_menu,
212 $this->getPageTitle(),
213 $this->getMenuTitle(),
214 $this->getMinimumCapability(),
215 $this->getOptionPage(),
216 array( $this, 'settingsPage' )
217 );
218 }
219
220 /**
221 * Print settings page
222 *
223 * @access public
224 * @return void
225 */
226 public function settingsPage() {
227
228
229 // Get current tab
230 $current_tab = ( isset( $_GET['tab'] ) ) ? htmlspecialchars( $_GET['tab'] ) : $this->defaultSettingsTab;
231
232 // plouf( $_POST );
233
234 // Print header
235 $this->printHeader();
236
237 $this->printFields();
238
239 if ( count( $this->extendedActions ) ) foreach ( $this->extendedActions as $action => $function ) {
240 if ( isset( $_REQUEST[$action] ) ) {
241 if ( function_exists( $function) ) {
242 $function();
243 }
244 else {
245 printf( __( 'Attention, fonction non définie %s' ), $function );
246 }
247 }
248 }
249 $this->printFooter();
250 }
251
252 public function registerSettings() {
253 // Check if current user can manage plugin settings
254 if ( !is_admin() ) {
255 return;
256 }
257
258 // Iterate over tabs
259 foreach ( $this->settingsStructure as $tab_key => $tab ) {
260 // Register tab
261 register_setting(
262 $this->option_page .'_group_' . $tab_key,
263 $this->option_page,
264 array( $this, 'validateSettings' )
265 );
266
267 // Iterate over sections
268 foreach ( $tab['sections'] as $section_key => $section ) {
269 $settings_page_id = $this->plugin_id . '-admin-' . str_replace( '_', '-', $tab_key );
270
271 // Register section
272 add_settings_section(
273 $section_key,
274 $section['title'],
275 array( $this, 'print_section_info' ),
276 $settings_page_id
277 );
278
279 // Iterate over fields
280 if( isset( $section['fields'] ) ) foreach ( $section['fields'] as $field_key => $field ) {
281 // Register field
282 add_settings_field(
283 $this->plugin_id . '_' . $field_key,
284 $field['title'],
285 array( $this, 'print_field_' . $field['type'] ),
286 $settings_page_id,
287 $section_key,
288 array(
289 'field_key' => $field_key,
290 'field' => $field,
291 'data-' . $this->plugin_id . '-setting-hint' => !empty( $field['hint'] ) ? $field['hint'] : null,
292 )
293 );
294 }
295 }
296 }
297 }
298
299 function validateSettings() {
300 return true;
301 }
302
303 function getActiveTab() {
304 if ( isset( $_GET[ 'tab' ] ) ) {
305 $active_tab = htmlspecialchars( $_GET[ 'tab' ] );
306 }
307 elseif ( $this->defaultSettingsTab != '' ) {
308 $active_tab = $this->defaultSettingsTab;
309 }
310
311 if ( !isset( $this->settingsStructure[$active_tab] ) ) {
312 return false;
313 }
314 return $active_tab;
315 }
316
317 function printHeader() {
318 $tabs = array();
319 foreach ( $this->settingsStructure as $setting_tab_slug => $setting_data ) {
320 $tabs[$setting_tab_slug] = $setting_data['title'];
321 }
322 //echo '<div class="wrap woocommerce"><form method="post" action="options.php" enctype="multipart/form-data">';
323 ?>
324
325 <div class="wrap">
326
327 <div id="icon-themes" class="icon32"></div>
328 <h2><?php echo esc_html( $this->getPageTitle() ); ?></h2>
329 <?php
330 settings_errors();
331
332 $active_tab = $this->getActiveTab();
333
334 $parent_menu = $this->parent_menu;
335 $parsed_url = parse_url( $parent_menu );
336 $extended_url = '';
337 if ( isset( $parsed_url['query'] ) && strlen( $parsed_url['query'] ) ) {
338 $extended_url = '&' . $parsed_url['query'];
339 }
340
341 if ( property_exists($this, 'post_type' ) && $this->post_type ) {
342 $action = 'edit.php';
343 }
344 else {
345 $action = 'admin.php';
346 }
347 $action .= '?';
348
349 if ( $this->post_type ) {
350 $action .= 'post_type=' . $this->post_type .'&';
351 }
352 $action .= 'page=' . $this->getOptionPage();
353 if ($active_tab) {
354 $action .= '&tab=' . $active_tab;
355 }
356
357 $page_link = '?';
358 if ( $this->post_type ) {
359 $page_link .= 'post_type=' . $this->post_type .'&';
360 }
361 $page_link .= 'page=' . $this->getOptionPage();
362
363
364
365 ?>
366 <form method="post" id="mainform" action="<?php echo esc_attr( $action ); ?>" enctype="multipart/form-data">
367 <input type="hidden" name="<?php echo esc_attr( $this->plugin_id ); ?>_options_save" value="1">
368 <input type="hidden" name="tab" value="<?php echo esc_attr( $active_tab ); ?>">
369
370 <nav class="nav-tab-wrapper woo-nav-tab-wrapper">
371 <?php foreach ( $tabs as $tab => $label ) :
372 $nav_tab_id = $this->getOptionPage() . '-' . $tab; ?>
373 <a id="<?php echo esc_attr( $nav_tab_id ); ?>" href="<?php echo esc_url( $page_link ) . '&tab=' . esc_attr( $tab . $extended_url ); ?>" class="nav-tab <?php echo $active_tab == $tab ? 'nav-tab-active' : ''; ?>"><?php echo esc_html( $label ); ?></a>
374 <?php endforeach; ?>
375 </nav>
376
377 <?php
378 if ( !$active_tab = $this->getActiveTab() ) {
379 return false;
380 }
381
382 /*
383 $tab_data = $this->settingsStructure[$active_tab];
384 $defaults = array(
385 'title' => '',
386 'class' => '',
387 'description' => '',
388 'sections' => array(),
389 );
390 $tab_data = wp_parse_args( $tab_data, $defaults );
391
392 $this->displayTabTitle( $active_tab, $tab_data );
393 */
394 }
395
396 function printFields() {
397 if (!$this->getActiveTab()) {
398 return false;
399 }
400 $active_tab = $this->getActiveTab();
401 $tab_data = $this->settingsStructure[$active_tab];
402
403 //plouf($tab_data, " T AB DATA");
404
405
406 foreach ( $tab_data['sections'] as $section_id => $section ) {
407 $defaults = array(
408 'title' => '',
409 'class' => '',
410 'description' => '',
411 'fields' => array(),
412 'html' => false,
413 );
414 $section_data = wp_parse_args( $section, $defaults );
415
416 //plouf( $section );
417 ?>
418 <h3 class="wc-settings-sub-title <?php echo esc_attr( $section_data['class'] ); ?>" id="<?php echo esc_attr( $section_id ); ?>"><?php echo wp_kses_post( $section_data['title'] ); ?></h3>
419 <?php if ( ! empty( $section_data['description'] ) ) : ?>
420 <p><?php echo wp_kses_post( $section_data['description'] ); ?></p>
421 <?php endif; ?>
422
423 <table class="form-table">
424
425 <?php
426
427 $this->WC_Improved_Settings_API->id = $active_tab;
428
429 $section_fields = $section_data['fields'];
430
431 $fields = array();
432 foreach ( $section_fields as $field ) {
433 $fields[] = $field;
434 }
435
436 //plouf($fields, "on a fields");
437
438 $this->WC_Improved_Settings_API->generate_settings_html( $fields );
439 ?>
440 </table>
441 <?php
442 if ( isset( $section['html'] ) && $section['html'] ) {
443 // not escaped
444 echo ( $section['html'] );
445 }
446 ?>
447
448 <?php if ( isset( $section['actions'] ) && $section['actions'] ) foreach ( $section['actions'] as $action ) {
449 $param = false;
450 if ( is_array( $action ) ) {
451 list($action, $param) = $action;
452 if ( function_exists( $action ) ) {
453 $action( $param );
454 }
455 }
456 elseif ( function_exists( $action ) ) {
457 $action( $param );
458 }
459 else {
460 printf( __( 'Attention, fonction non définie %s' ), $action );
461 }
462 }
463
464 ?>
465
466
467 <?php if ( count( $fields ) ) : ?>
468
469 <p class="submit">
470 <?php wp_nonce_field('zesave_settings', '_zenonce' ); ?>
471 <?php if ( empty( $GLOBALS['hide_save_button'] ) ) : ?>
472 <button name="save" class="button-primary" type="submit" value="<?php esc_attr_e( 'Update' ); ?>"><?php _e( 'Update' ); ?></button>
473 <?php endif; ?>
474 <?php
475 // wp_nonce_field( 'woocommerce-settings' );
476 ?>
477 </p>
478 <?php endif; ?>
479
480 <?php
481 }
482 ?>
483
484 <?php
485 }
486
487 function displayTabTitle( $tab_id, $tab_data ) {
488 ?>
489 <h2 class="wc-settings-sub-title <?php echo esc_attr( $tab_data['class'] ); ?>" id="<?php echo esc_attr( $tab_id ); ?>"><?php echo wp_kses_post( $tab_data['title'] ); ?></h2>
490 <?php if ( ! empty( $tab_data['description'] ) ) : ?>
491 <p><?php echo wp_kses_post( $tab_data['description'] ); ?></p>
492 <?php endif;
493 }
494
495 function tabFooter( $tab_id, $tab_data ) {
496 if ( isset( $tab_data['footer'] ) ) {
497 if ( isset( $tab_data['footer']['html'] ) ) foreach ( $tab_data['footer']['html'] as $raw_html ) {
498 // not escaped
499 echo ( $raw_html );
500 }
501 if ( isset( $tab_data['footer']['actions'] ) ) {
502 echo '<hr />';
503 foreach ( $tab_data['footer']['actions'] as $action ) {
504 // echo " ACTION = $action";
505 $param = false;
506
507 if ( is_array( $action ) ) {
508 list($object, $method) = $action;
509 if ( method_exists( $object, $method ) ) {
510 $object->$method();
511 }
512 }
513 elseif ( function_exists( $action ) ) {
514 $action( $param );
515 }
516 else {
517 printf( __( 'Attention, fonction non définie %s' ), $action );
518 }
519 //plouf($action, "action");
520 }
521 }
522 }
523 }
524
525 function printFooter() {
526 ?>
527 <?php
528 $active_tab = $this->getActiveTab();
529 if ( $active_tab ) {
530 $tab_data = $this->settingsStructure[$active_tab];
531 $this->tabFooter( $active_tab, $tab_data );
532 }
533 ?>
534
535 </form>
536
537 </div>
538 <?php
539 }
540
541
542 public function showServerInfo() {
543 echo '<h2>' . __('Server information', '' ) . '</h2>';
544
545 if( function_exists('ini_get_all' ) ) {
546
547 $ini_values = ini_get_all();
548 $timeout = $ini_values['max_execution_time']['local_value'];
549 }
550 else {
551 $timeout = '';
552 }
553
554 $bytes = memory_get_usage();
555 $s = array('o', 'Ko', 'Mo', 'Go', 'To', 'Po');
556 $e = floor(log($bytes)/log(1024));
557 $memory_usage = sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));
558
559
560
561 if ($timeout > 1000 ) {
562 $timeout = round($timeout /1000,1);
563 }
564
565 $informations = array(
566 'Server time' => date('d/m/Y H:i:s'),
567 'Real path' => get_home_path(),
568 'PHP version' => phpversion(),
569 'Timeout' => $timeout .' s',
570 'Memory usage' => $memory_usage,
571 );
572
573 if ( function_exists( 'sys_getloadavg') ) {
574 $sys_getloadavg = sys_getloadavg();
575 if( is_array( $sys_getloadavg ) ){
576 $informations['Load'] = implode(', ', $sys_getloadavg );
577 }
578 else {
579 $informations['Load'] = $sys_getloadavg;
580 }
581 }
582
583 foreach ($informations as $label => $value) {
584 printf( "<p><strong>%s</strong>&nbsp;%s</p>", $label, $value );
585 }
586
587
588 }
589
590
591
592 public function showLogs() {
593
594 if ( !is_admin() ) {
595 return false;
596 }
597 /*$current_plugin_page = $_REQUEST['page'];
598
599 plouf($this->plugin_paths);
600 if ( !isset( $this->plugin_paths[$current_plugin_page] ) ) {
601 echo "no path";
602 return false;
603 }
604 $path = $this->plugin_paths[$current_plugin_page];*/
605 $path = $this->log_folder;
606
607 $logs = glob( trailingslashit( $path ) . '*.log');
608 //plouf($logs, "LOGS");
609 if ($logs) foreach ($logs as $log_file) {
610 $file_name = basename( $log_file );
611 $contents = file_get_contents( $log_file );
612 if (preg_match('#(\d+)-(\d+)-(\w+)\.log#', $file_name, $match)) {
613 $date = $match[2] . '/' . $match[1];
614 echo '<h3>';
615 printf(
616 __("Fichier '%s' pour %s" ),
617 $match[3],
618 $date
619 );
620 echo '</h3>';
621 $lines = explode( "\n", $contents);
622 foreach ( $lines as $line ) {
623 //$line = preg_replace('#\{"body":".*?"},#ism', '-body-', $line);
624 //$line = preg_replace( '#"raw":".*?","headers#ism', 'headers', $line );
625 $line = preg_replace( '#"body":"<!DOCTYPE.*?","headers#ism', '"body":"PROBLEME COTE INSURED (disponible dans les logs complets)", "headers', $line);
626 if ( stripos( $line, '<!DOCTYPE html>' ) ) {
627 continue;
628 }
629 // not escaped
630 echo "<br /><br />" . ( $line ) . "\n";
631 }
632 //plouf($contents);
633
634 }
635 }
636
637 }
638 }
639 }