PluginProbe
Translation with DeepL API / 2.4.5
Translation with DeepL API v2.4.5
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.5, at settings/wp-improved-settings.class.php

656 lines 17.5 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
135 /*
136 $this->WC_Improved_Settings_API = new WC_Improved_Settings_API( $this->getPluginID(), $this->getSettingsStructure() );
137
138
139 $key_name = $this->plugin_id . '_options_save';
140 if ( isset( $_REQUEST['save'] ) && isset( $_REQUEST[$key_name] ) && $_REQUEST[$key_name] ) {
141 $this->saveSettings();
142 // echo " saving";
143 if ( method_exists( $this, 'on_save' ) ) {
144 // echo "on save update";
145 $this->on_save();
146 }
147
148 add_action( 'admin_notices', array( $this, 'print_saved_notice' ) );
149 }
150
151 add_action( 'admin_notices', array( $this, 'maybe_print_notices' ) );
152 */
153 // Migration notices
154 //add_action( 'admin_notices', array( $this, 'maybe_display_migration_notice' ), 1 );
155
156 // Delete migration notice
157 //$this->hide_migration_notice();
158 }
159
160 function getPluginID() {
161 return $this->plugin_id;
162 }
163
164 function getOptionPage() {
165 return $this->option_page;
166 }
167
168 function getMinimumCapability() {
169 return $this->minimum_capability;
170 }
171
172 function saveSettings() {
173 $nonce = isset( $_REQUEST['_zenonce'] ) ? $_REQUEST['_zenonce'] : false;
174 if ( ! wp_verify_nonce( $nonce, 'zesave_settings' ) ) {
175 $error_msg = __( 'Unable to submit this form, please refresh and try again.' );
176 return;
177 }
178
179 $this->WC_Improved_Settings_API->process_admin_options();
180 //$this->process_admin_options();
181 }
182 /*
183 function me() {
184 $this->loadSettings();
185 $this->WC_Improved_Settings_API = WC_Improved_Settings_API( $this->getPluginID(), $this->getSettingsStructure() );
186 return $this->WC_Improved_Settings_API->process_admin_options();
187 }*/
188
189 function maybe_print_notices() {
190 }
191
192 function print_saved_notice() {
193 ?>
194 <div id="message" class="updated notice is-dismissible"><p><strong><?php _e( 'Settings saved.' ); ?></strong></p></div>
195 <?php
196 }
197
198 public function loadSettings() {
199 $this->settingsStructure = $this->getSettingsStructure();
200 $this->WC_Improved_Settings_API = new WC_Improved_Settings_API( $this->getPluginID(), $this->settingsStructure );
201 // load settings into $this sttings ?
202 //plouf( $this->settingsStructure ); die( 'oka6z4e4z64ze4' );
203 }
204
205 /**
206 * Add Settings link to menu
207 *
208 * @access public
209 * @return voidaddToMenu
210 */
211 public function addToMenu() {
212 //die( 'menu to '.$this->parent_menu . ' page title = ' . $this->getPageTitle() .' menu = ' . $this->getMenuTitle() . ' cap ' . 'manage_options' . ' option page = ' . $this->getOptionPage() );
213 add_submenu_page(
214 $this->parent_menu,
215 $this->getPageTitle(),
216 $this->getMenuTitle(),
217 $this->getMinimumCapability(),
218 $this->getOptionPage(),
219 array( $this, 'settingsPage' )
220 );
221 }
222
223 /**
224 * Print settings page
225 *
226 * @access public
227 * @return void
228 */
229 public function settingsPage() {
230
231
232 // Get current tab
233 $current_tab = ( isset( $_GET['tab'] ) ) ? htmlspecialchars( $_GET['tab'] ) : $this->defaultSettingsTab;
234
235 // plouf( $_POST );
236
237 // Print header
238 $this->printHeader();
239
240 $this->printFields();
241
242 if ( count( $this->extendedActions ) ) foreach ( $this->extendedActions as $action => $function ) {
243 if ( isset( $_REQUEST[$action] ) ) {
244 if ( function_exists( $function) ) {
245 $function();
246 }
247 else {
248 printf( __( 'Attention, fonction non définie %s' ), $function );
249 }
250 }
251 }
252 $this->printFooter();
253 }
254
255 public function registerSettings() {
256 // Check if current user can manage plugin settings
257 if ( !is_admin() ) {
258 return;
259 }
260
261 $key_name = $this->plugin_id . '_options_save';
262 if ( isset( $_REQUEST['save'] ) && isset( $_REQUEST[$key_name] ) && $_REQUEST[$key_name] ) {
263 $this->saveSettings();
264 // echo " saving";
265 if ( method_exists( $this, 'on_save' ) ) {
266 // echo "on save update";
267 $this->on_save();
268 }
269
270 add_action( 'admin_notices', array( $this, 'print_saved_notice' ) );
271 }
272 add_action( 'admin_notices', array( $this, 'maybe_print_notices' ) );
273
274 // Iterate over tabs
275 foreach ( $this->settingsStructure as $tab_key => $tab ) {
276 // Register tab
277 register_setting(
278 $this->option_page .'_group_' . $tab_key,
279 $this->option_page,
280 array( $this, 'validateSettings' )
281 );
282
283 // Iterate over sections
284 foreach ( $tab['sections'] as $section_key => $section ) {
285 $settings_page_id = $this->plugin_id . '-admin-' . str_replace( '_', '-', $tab_key );
286
287 // Register section
288 add_settings_section(
289 $section_key,
290 $section['title'],
291 array( $this, 'print_section_info' ),
292 $settings_page_id
293 );
294
295 // Iterate over fields
296 if( isset( $section['fields'] ) ) foreach ( $section['fields'] as $field_key => $field ) {
297 // Register field
298 add_settings_field(
299 $this->plugin_id . '_' . $field_key,
300 $field['title'],
301 array( $this, 'print_field_' . $field['type'] ),
302 $settings_page_id,
303 $section_key,
304 array(
305 'field_key' => $field_key,
306 'field' => $field,
307 'data-' . $this->plugin_id . '-setting-hint' => !empty( $field['hint'] ) ? $field['hint'] : null,
308 )
309 );
310 }
311 }
312 }
313 }
314
315 function validateSettings() {
316 return true;
317 }
318
319 function getActiveTab() {
320 if ( isset( $_GET[ 'tab' ] ) ) {
321 $active_tab = htmlspecialchars( $_GET[ 'tab' ] );
322 }
323 elseif ( $this->defaultSettingsTab != '' ) {
324 $active_tab = $this->defaultSettingsTab;
325 }
326
327 if ( !isset( $this->settingsStructure[$active_tab] ) ) {
328 return false;
329 }
330 return $active_tab;
331 }
332
333 function printHeader() {
334 $tabs = array();
335 foreach ( $this->settingsStructure as $setting_tab_slug => $setting_data ) {
336 $tabs[$setting_tab_slug] = $setting_data['title'];
337 }
338 //echo '<div class="wrap woocommerce"><form method="post" action="options.php" enctype="multipart/form-data">';
339 ?>
340
341 <div class="wrap">
342
343 <div id="icon-themes" class="icon32"></div>
344 <h2><?php echo esc_html( $this->getPageTitle() ); ?></h2>
345 <?php
346 settings_errors();
347
348 $active_tab = $this->getActiveTab();
349
350 $parent_menu = $this->parent_menu;
351 $parsed_url = parse_url( $parent_menu );
352 $extended_url = '';
353 if ( isset( $parsed_url['query'] ) && strlen( $parsed_url['query'] ) ) {
354 $extended_url = '&' . $parsed_url['query'];
355 }
356
357 if ( property_exists($this, 'post_type' ) && $this->post_type ) {
358 $action = 'edit.php';
359 }
360 else {
361 $action = 'admin.php';
362 }
363 $action .= '?';
364
365 if ( $this->post_type ) {
366 $action .= 'post_type=' . $this->post_type .'&';
367 }
368 $action .= 'page=' . $this->getOptionPage();
369 if ($active_tab) {
370 $action .= '&tab=' . $active_tab;
371 }
372
373 $page_link = '?';
374 if ( $this->post_type ) {
375 $page_link .= 'post_type=' . $this->post_type .'&';
376 }
377 $page_link .= 'page=' . $this->getOptionPage();
378
379
380
381 ?>
382 <form method="post" id="mainform" action="<?php echo esc_attr( $action ); ?>" enctype="multipart/form-data">
383 <input type="hidden" name="<?php echo esc_attr( $this->plugin_id ); ?>_options_save" value="1">
384 <input type="hidden" name="tab" value="<?php echo esc_attr( $active_tab ); ?>">
385
386 <nav class="nav-tab-wrapper woo-nav-tab-wrapper">
387 <?php foreach ( $tabs as $tab => $label ) :
388 $nav_tab_id = $this->getOptionPage() . '-' . $tab; ?>
389 <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>
390 <?php endforeach; ?>
391 </nav>
392
393 <?php
394 if ( !$active_tab = $this->getActiveTab() ) {
395 return false;
396 }
397
398 /*
399 $tab_data = $this->settingsStructure[$active_tab];
400 $defaults = array(
401 'title' => '',
402 'class' => '',
403 'description' => '',
404 'sections' => array(),
405 );
406 $tab_data = wp_parse_args( $tab_data, $defaults );
407
408 $this->displayTabTitle( $active_tab, $tab_data );
409 */
410 }
411
412 function printFields() {
413 if (!$this->getActiveTab()) {
414 return false;
415 }
416 $active_tab = $this->getActiveTab();
417 $tab_data = $this->settingsStructure[$active_tab];
418
419
420 //plouf($tab_data, " T AB DATA");
421
422
423 foreach ( $tab_data['sections'] as $section_id => $section ) {
424 $defaults = array(
425 'title' => '',
426 'class' => '',
427 'description' => '',
428 'fields' => array(),
429 'html' => false,
430 );
431 $section_data = wp_parse_args( $section, $defaults );
432
433 //plouf( $section );
434 ?>
435 <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>
436 <?php if ( ! empty( $section_data['description'] ) ) : ?>
437 <p><?php echo wp_kses_post( $section_data['description'] ); ?></p>
438 <?php endif; ?>
439
440 <table class="form-table">
441
442 <?php
443
444 $this->WC_Improved_Settings_API->id = $active_tab;
445
446 $section_fields = $section_data['fields'];
447
448 $fields = array();
449 foreach ( $section_fields as $field ) {
450 $fields[] = $field;
451 }
452
453 //plouf($fields, "on a fields");
454
455 $this->WC_Improved_Settings_API->generate_settings_html( $fields );
456 ?>
457 </table>
458 <?php
459 if ( isset( $section['html'] ) && $section['html'] ) {
460 // not escaped
461 echo ( $section['html'] );
462 }
463 ?>
464
465 <?php if ( isset( $section['actions'] ) && $section['actions'] ) foreach ( $section['actions'] as $action ) {
466 $param = false;
467 if ( is_array( $action ) ) {
468 list($action, $param) = $action;
469 if ( function_exists( $action ) ) {
470 $action( $param );
471 }
472 }
473 elseif ( function_exists( $action ) ) {
474 $action( $param );
475 }
476 else {
477 printf( __( 'Attention, fonction non définie %s' ), $action );
478 }
479 }
480
481 ?>
482
483
484 <?php if ( count( $fields ) ) : ?>
485
486 <p class="submit">
487 <?php wp_nonce_field('zesave_settings', '_zenonce' ); ?>
488 <?php if ( empty( $GLOBALS['hide_save_button'] ) ) : ?>
489 <button name="save" class="button-primary" type="submit" value="<?php esc_attr_e( 'Update' ); ?>"><?php _e( 'Update' ); ?></button>
490 <?php endif; ?>
491 <?php
492 // wp_nonce_field( 'woocommerce-settings' );
493 ?>
494 </p>
495 <?php endif; ?>
496
497 <?php
498 }
499 ?>
500
501 <?php
502 }
503
504 function displayTabTitle( $tab_id, $tab_data ) {
505 ?>
506 <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>
507 <?php if ( ! empty( $tab_data['description'] ) ) : ?>
508 <p><?php echo wp_kses_post( $tab_data['description'] ); ?></p>
509 <?php endif;
510 }
511
512 function tabFooter( $tab_id, $tab_data ) {
513 if ( isset( $tab_data['footer'] ) ) {
514 if ( isset( $tab_data['footer']['html'] ) ) foreach ( $tab_data['footer']['html'] as $raw_html ) {
515 // not escaped
516 echo ( $raw_html );
517 }
518 if ( isset( $tab_data['footer']['actions'] ) ) {
519 echo '<hr />';
520 foreach ( $tab_data['footer']['actions'] as $action ) {
521 // echo " ACTION = $action";
522 $param = false;
523
524 if ( is_array( $action ) ) {
525 list($object, $method) = $action;
526 if ( method_exists( $object, $method ) ) {
527 $object->$method();
528 }
529 }
530 elseif ( function_exists( $action ) ) {
531 $action( $param );
532 }
533 else {
534 printf( __( 'Attention, fonction non définie %s' ), $action );
535 }
536 //plouf($action, "action");
537 }
538 }
539 }
540 }
541
542 function printFooter() {
543 ?>
544 <?php
545 $active_tab = $this->getActiveTab();
546 if ( $active_tab ) {
547 $tab_data = $this->settingsStructure[$active_tab];
548 $this->tabFooter( $active_tab, $tab_data );
549 }
550 ?>
551
552 </form>
553
554 </div>
555 <?php
556 }
557
558
559 public function showServerInfo() {
560 echo '<h2>' . __('Server information', '' ) . '</h2>';
561
562 if( function_exists('ini_get_all' ) ) {
563
564 $ini_values = ini_get_all();
565 $timeout = $ini_values['max_execution_time']['local_value'];
566 }
567 else {
568 $timeout = '';
569 }
570
571 $bytes = memory_get_usage();
572 $s = array('o', 'Ko', 'Mo', 'Go', 'To', 'Po');
573 $e = floor(log($bytes)/log(1024));
574 $memory_usage = sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));
575
576
577
578 if ($timeout > 1000 ) {
579 $timeout = round($timeout /1000,1);
580 }
581
582 $informations = array(
583 'Server time' => date('d/m/Y H:i:s'),
584 'Real path' => get_home_path(),
585 'PHP version' => phpversion(),
586 'Timeout' => $timeout .' s',
587 'Memory usage' => $memory_usage,
588 );
589
590 if ( function_exists( 'sys_getloadavg') ) {
591 $sys_getloadavg = sys_getloadavg();
592 if( is_array( $sys_getloadavg ) ){
593 $informations['Load'] = implode(', ', $sys_getloadavg );
594 }
595 else {
596 $informations['Load'] = $sys_getloadavg;
597 }
598 }
599
600 foreach ($informations as $label => $value) {
601 printf( "<p><strong>%s</strong>&nbsp;%s</p>", $label, $value );
602 }
603
604
605 }
606
607
608
609 public function showLogs() {
610
611 if ( !is_admin() ) {
612 return false;
613 }
614 /*$current_plugin_page = $_REQUEST['page'];
615
616 plouf($this->plugin_paths);
617 if ( !isset( $this->plugin_paths[$current_plugin_page] ) ) {
618 echo "no path";
619 return false;
620 }
621 $path = $this->plugin_paths[$current_plugin_page];*/
622 $path = $this->log_folder;
623
624 $logs = glob( trailingslashit( $path ) . '*.log');
625 //plouf($logs, "LOGS");
626 if ($logs) foreach ($logs as $log_file) {
627 $file_name = basename( $log_file );
628 $contents = file_get_contents( $log_file );
629 if (preg_match('#(\d+)-(\d+)-(\w+)\.log#', $file_name, $match)) {
630 $date = $match[2] . '/' . $match[1];
631 echo '<h3>';
632 printf(
633 __("Fichier '%s' pour %s" ),
634 $match[3],
635 $date
636 );
637 echo '</h3>';
638 $lines = explode( "\n", $contents);
639 foreach ( $lines as $line ) {
640 //$line = preg_replace('#\{"body":".*?"},#ism', '-body-', $line);
641 //$line = preg_replace( '#"raw":".*?","headers#ism', 'headers', $line );
642 $line = preg_replace( '#"body":"<!DOCTYPE.*?","headers#ism', '"body":"PROBLEME COTE INSURED (disponible dans les logs complets)", "headers', $line);
643 if ( stripos( $line, '<!DOCTYPE html>' ) ) {
644 continue;
645 }
646 // not escaped
647 echo "<br /><br />" . ( $line ) . "\n";
648 }
649 //plouf($contents);
650
651 }
652 }
653
654 }
655 }
656 }