PluginProbe
Translation with DeepL API / 1.0
Translation with DeepL API v1.0
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 1.0, at settings/wp-improved-settings.class.php

570 lines 17.1 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 *
6 * @package WP_Improved_Settings
7 * @version 20190115
8 */
9
10 namespace WP_Improved_Settings;
11
12 if( !class_exists( 'WP_Improved_Settings\WP_Improved_Settings' ) ) {
13 class WP_Improved_Settings {
14 // loosely based on wc-dynamic-pricing-and-discounts/classes/rp-wcdpd-settings.class.php
15 public $settingsStructure = array();
16 public $extendedActions = array();
17
18 public $plugin_id;
19 public $menu_order = 20;
20 public $minimum_capability = 'manage_options';
21 public $option_page = '';
22 public $defaultSettingsTab = '';
23 public $parent_menu = '';
24 /*Dashboard: 'index.php'
25 Posts: 'edit.php'
26 Media: 'upload.php'
27 Pages: 'edit.php?post_type=page'
28 Comments: 'edit-comments.php'
29 Custom Post Types: 'edit.php?post_type=your_post_type'
30 Appearance: 'themes.php'
31 Plugins: 'plugins.php'
32 Users: 'users.php'
33 Tools: 'tools.php'
34 Settings: 'options-general.php'
35 Network Settings: 'settings.php'
36 WooCommerce : 'woocommerce'
37 */
38
39 public function __construct() {
40 // Register settings
41
42 add_action( 'admin_init', array( $this, 'loadSettings' ) );
43 add_action( 'admin_init', array( $this, 'registerSettings' ) );
44
45 // Add link to menu
46
47 global $wp_filter;
48 $real_order = $this->menu_order;
49 while( isset( $wp_filter['admin_menu']->callbacks[$real_order] ) ) {
50 $real_order++;
51 }
52 add_action( 'admin_menu', array( $this, 'addToMenu' ), $real_order );
53
54 // Pass configuration to Javascript
55 //add_action( 'admin_enqueue_scripts', array( $this, 'configuration_to_javascript' ), 999 );
56
57 // Enqueue templates to be rendered in footer
58 //add_action( 'admin_footer', array( $this, 'render_templates_in_footer' ) );
59
60 // Settings export call
61 if ( !empty( $_REQUEST['export_settings'] ) ) {
62 add_action( 'wp_loaded', array( $this, 'export' ) );
63 }
64
65 // Settings import call
66 if ( !empty( $_FILES[$this->option_page]['name']['import'] ) ) {
67 add_action( 'wp_loaded', array( $this, 'import' ) );
68 }
69
70 // Print settings import notice
71 if ( isset( $_REQUEST[$this->option_page .'_imported'] ) ) {
72 add_action( 'admin_notices', array( $this, 'print_import_notice' ) );
73 }
74
75 $this->FromWC_Settings_API = WC_Improved_Settings_API( $this->getPluginID(), $this->getSettingsStructure() );
76
77 $key_name = $this->plugin_id . '_options_save';
78 if( isset( $_REQUEST['save'] ) && isset( $_REQUEST[$key_name] ) && $_REQUEST[$key_name] ) {
79 $this->saveSettings();
80 // echo " saving";
81 if( method_exists( $this, 'on_save' ) ) {
82 // echo "on save update";
83 $this->on_save();
84 }
85
86 add_action( 'admin_notices', array( $this, 'print_saved_notice' ) );
87 }
88
89 add_action( 'admin_notices', array( $this, 'maybe_print_notices' ) );
90
91 // Migration notices
92 //add_action( 'admin_notices', array( $this, 'maybe_display_migration_notice' ), 1 );
93
94 // Delete migration notice
95 //$this->hide_migration_notice();
96 }
97
98 function getPluginID() {
99 return $this->plugin_id;
100 }
101
102 function getOptionPage() {
103 return $this->option_page;
104 }
105
106 function getMinimumCapability() {
107 return $this->minimum_capability;
108 }
109
110 function saveSettings() {
111 $this->FromWC_Settings_API->process_admin_options();
112 }
113
114 function me() {
115 $this->loadSettings();
116 $this->FromWC_Settings_API = WC_Improved_Settings_API( $this->getPluginID(), $this->getSettingsStructure() );
117 return $this->FromWC_Settings_API->process_admin_options();
118 }
119
120 function maybe_print_notices() {
121 }
122
123 function print_saved_notice() {
124 ?>
125 <div id="message" class="updated notice is-dismissible"><p><strong><?php _e( 'Settings saved.' ); ?></strong></p></div>
126 <?php
127 }
128
129 public function loadSettings() {
130 // load settings into $this sttings ?
131 $this->settingsStructure = $this->getSettingsStructure();
132 //plouf( $this->settingsStructure ); die( 'ok' );
133 }
134
135 /**
136 * Add Settings link to menu
137 *
138 * @access public
139 * @return voidaddToMenu
140 */
141 public function addToMenu() {
142 //die( 'menu to '.$this->parent_menu . ' page title = ' . $this->getPageTitle() .' menu = ' . $this->getMenuTitle() . ' cap ' . 'manage_options' . ' option page = ' . $this->getOptionPage() );
143 add_submenu_page(
144 $this->parent_menu,
145 $this->getPageTitle(),
146 $this->getMenuTitle(),
147 $this->getMinimumCapability(),
148 $this->getOptionPage(),
149 array( $this, 'settingsPage' )
150 );
151 }
152
153 /**
154 * Print settings page
155 *
156 * @access public
157 * @return void
158 */
159 public function settingsPage() {
160 $this->FromWC_Settings_API = WC_Improved_Settings_API( $this->getPluginID(), $this->settingsStructure );
161
162 // Get current tab
163 $current_tab = ( isset( $_GET['tab'] ) ) ? $_GET['tab'] : $this->defaultSettingsTab;
164
165 // plouf( $_POST );
166
167 // Print header
168 $this->printHeader();
169
170 $this->printFields();
171
172 if( count( $this->extendedActions ) ) foreach( $this->extendedActions as $action => $function ) {
173 if( isset( $_REQUEST[$action] ) ) {
174 $this->$function();
175 }
176 }
177 $this->printFooter();
178 }
179
180 public function registerSettings() {
181 // Check if current user can manage plugin settings
182 if ( !is_admin() ) {
183 return;
184 }
185
186 // Iterate over tabs
187 foreach ( $this->settingsStructure as $tab_key => $tab ) {
188 // Register tab
189 register_setting(
190 $this->option_page .'_group_' . $tab_key,
191 $this->option_page,
192 array( $this, 'validateSettings' )
193 );
194
195 // Iterate over sections
196 foreach ( $tab['sections'] as $section_key => $section ) {
197 $settings_page_id = $this->plugin_id . '-admin-' . str_replace( '_', '-', $tab_key );
198
199 // Register section
200 add_settings_section(
201 $section_key,
202 $section['title'],
203 array( $this, 'print_section_info' ),
204 $settings_page_id
205 );
206
207 // Iterate over fields
208 foreach ( $section['fields'] as $field_key => $field ) {
209 // Register field
210 add_settings_field(
211 $this->plugin_id . '_' . $field_key,
212 $field['title'],
213 array( $this, 'print_field_' . $field['type'] ),
214 $settings_page_id,
215 $section_key,
216 array(
217 'field_key' => $field_key,
218 'field' => $field,
219 'data-' . $this->plugin_id . '-setting-hint' => !empty( $field['hint'] ) ? $field['hint'] : null,
220 )
221 );
222 }
223 }
224 }
225 }
226
227 function validateSettings() {
228 return true;
229 }
230
231 function getActiveTab() {
232 if( isset( $_GET[ 'tab' ] ) ) {
233 $active_tab = $_GET[ 'tab' ];
234 }
235 elseif( $this->defaultSettingsTab != '' ) {
236 $active_tab = $this->defaultSettingsTab;
237 }
238
239 if( !isset( $this->settingsStructure[$active_tab] ) ) {
240 return false;
241 }
242 return $active_tab;
243 }
244
245 function printHeader() {
246 $tabs = array();
247 foreach( $this->settingsStructure as $setting_tab_slug => $setting_data ) {
248 $tabs[$setting_tab_slug] = $setting_data['title'];
249 }
250 //echo '<div class="wrap woocommerce"><form method="post" action="options.php" enctype="multipart/form-data">';
251 ?>
252
253 <div class="wrap">
254
255 <div id="icon-themes" class="icon32"></div>
256 <h2><?php echo $this->getPageTitle(); ?></h2>
257 <?php
258 settings_errors();
259
260 $active_tab = $this->getActiveTab();
261
262 $parent_menu = $this->parent_menu;
263 $parsed_url = parse_url( $parent_menu );
264 if( isset( $parsed_url['query'] ) && strlen( $parsed_url['query'] ) ) {
265 $extended_url = '&' . $parsed_url['query'];
266 }
267 else {
268 $extended_url = '';
269 }
270
271 ?>
272 <form method="post" id="mainform" action="admin.php?page=<?php echo $this->getOptionPage(); ?><?php echo ( $active_tab ) ?'&tab=' . $active_tab : ''; ?>" enctype="multipart/form-data">
273 <input type="hidden" name="<?php echo $this->plugin_id; ?>_options_save" value="1">
274
275 <nav class="nav-tab-wrapper woo-nav-tab-wrapper">
276 <?php foreach( $tabs as $tab => $label ) : ?>
277 <a href="?page=<?php echo $this->getOptionPage(); ?>&tab=<?php echo $tab; ?><?php echo $extended_url; ?>" class="nav-tab <?php echo $active_tab == $tab ? 'nav-tab-active' : ''; ?>"><?php echo $label; ?></a>
278 <?php endforeach; ?>
279 </nav>
280
281 <?php
282 if( !$active_tab = $this->getActiveTab() ) {
283 return false;
284 }
285
286 /*
287 $tab_data = $this->settingsStructure[$active_tab];
288 $defaults = array(
289 'title' => '',
290 'class' => '',
291 'description' => '',
292 'sections' => array(),
293 );
294 $tab_data = wp_parse_args( $tab_data, $defaults );
295
296 $this->displayTabTitle( $active_tab, $tab_data );
297 */
298 }
299
300 function printFields() {
301 if( !$active_tab = $this->getActiveTab() ) {
302 return false;
303 }
304 $tab_data = $this->settingsStructure[$active_tab];
305
306 foreach( $tab_data['sections'] as $section_id => $section ) {
307 $defaults = array(
308 'title' => '',
309 'class' => '',
310 'description' => '',
311 'fields' => array(),
312 );
313 $section_data = wp_parse_args( $section, $defaults );
314
315 //plouf( $section );
316 ?>
317 <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>
318 <?php if ( ! empty( $section_data['description'] ) ) : ?>
319 <p><?php echo wp_kses_post( $section_data['description'] ); ?></p>
320 <?php endif; ?>
321
322 <table class="form-table">
323
324 <?php
325
326 $this->FromWC_Settings_API->id = $active_tab;
327
328 $section_fields = $section_data['fields'];
329
330 $fields = array();
331 foreach( $section_fields as $field ) {
332 $fields[$field['id']] = $field;
333 }
334
335 $this->FromWC_Settings_API->generate_settings_html( $fields );
336 ?>
337 </table>
338
339 <?php
340 }
341 ?>
342
343 <?php if( count( $fields ) ) : ?>
344
345 <p class="submit">
346 <?php if ( empty( $GLOBALS['hide_save_button'] ) ) : ?>
347 <button name="save" class="button-primary" type="submit" value="<?php esc_attr_e( 'Update' ); ?>"><?php _e( 'Update' ); ?></button>
348 <?php endif; ?>
349 <?php
350 // wp_nonce_field( 'woocommerce-settings' );
351 ?>
352 </p>
353 <?php endif; ?>
354
355 <?php
356 }
357
358 function displayTabTitle( $tab_id, $tab_data ) {
359 ?>
360 <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>
361 <?php if ( ! empty( $tab_data['description'] ) ) : ?>
362 <p><?php echo wp_kses_post( $tab_data['description'] ); ?></p>
363 <?php endif;
364 }
365
366 function tabFooter( $tab_id, $tab_data ) {
367 if( isset( $tab_data['footer'] ) ) {
368 if( isset( $tab_data['footer']['html'] ) ) foreach( $tab_data['footer']['html'] as $raw_html ) {
369 echo $raw_html;
370 }
371 if( isset( $tab_data['footer']['actions'] ) ) foreach( $tab_data['footer']['actions'] as $action ) {
372 if( function_exists( $action ) ) {
373 $action();
374 }
375 else {
376 printf( __( 'Attention, fonction non définie %s' ), $action );
377 }
378 }
379 }
380 }
381
382 function printFooter() {
383 ?>
384 <?php
385 $active_tab = $this->getActiveTab();
386 if( $active_tab ) {
387 $tab_data = $this->settingsStructure[$active_tab];
388 $this->tabFooter( $active_tab, $tab_data );
389 }
390 ?>
391
392 </form>
393
394 </div>
395 <?php
396 }
397 }
398 }
399
400 if( !function_exists( 'WP_Improved_Settings\WC_Improved_Settings_API' ) ) {
401 function WC_Improved_Settings_API( $plugin_id, $settingsStructure ) {
402 //echo "function WC_Improved_Settings_API"; echo " class exists ? " ; var_dump( class_exists( 'WP_Improved_Settings\WC_Improved_Settings_API' ) );
403
404 if( !class_exists( 'WP_Improved_Settings\WC_Improved_Settings_API' ) ) {
405 if( !class_exists( 'WP_Improved_Settings\FromWC_Settings_API' ) ) {
406 require_once( dirname( __FILE__ ) . '/abstract-wc-settings-api.php' );
407 }
408
409 class WC_Improved_Settings_API extends FromWC_Settings_API {
410 public function __construct( $plugin_id , $settingsStructure ) {
411 $this->plugin_id = $plugin_id .'_';
412 $this->settingsStructure = $settingsStructure;
413 $this->init_form_fields();
414 //add_action( 'woocommerce_update_options_' . $this->id, array( $this, 'process_admin_options' ) );
415 // add_action( 'woocommerce_update_options', array( $this, 'test_action' ) );
416 }
417
418 public function init_form_fields() {
419 foreach( $this->settingsStructure as $tab_id => $tab_data ) {
420 foreach( $tab_data['sections'] as $section_id => $section_data ) {
421 foreach( $section_data['fields'] as $field ) {
422 $this->id = $tab_id;
423 $field_key = $this->get_field_key( $field['id'] );
424 //plouf( $field, $field_key );
425
426 $this->form_fields[$field_key] = $field;
427 }
428 }
429 }
430 }
431
432 function process_admin_options() {
433 $post_data = $this->get_post_data();
434
435 //plouf( $post_data ); plouf( array_keys( $this->form_fields ) );
436 foreach( $this->form_fields as $field_key => $field ) {
437 if( isset( $post_data[$field_key] ) ) {
438 $field_value = $post_data[$field_key];
439
440 if( $field['type'] == 'checkbox' && $field_value == 1 ) {
441 $field_value = 'yes';
442 }
443 if( $field['type'] == 'textarea' ) {
444 $field_value = stripslashes( $field_value );
445 }
446 //echo "\n<br /> updating $field_key = " . $post_data[$field_key] ." > " . $field_value;
447 update_option( $field_key, $field_value );
448 }
449 }
450
451 if( method_exists( $this, 'on_save' ) ) {
452 $this->on_save();
453 }
454 //plouf( $post_data ); plouf( $this->form_fields ); die( 'okzeprjio' );
455 }
456
457 public function get_field_key( $key ) {
458 //echo "<br /> KEY de $key = " . $this->plugin_id . $this->id . '_' . $key;
459 //return $this->plugin_id . $this->id . '_' . $key;
460 return $this->plugin_id . $key;
461 }
462
463 public function update_option( $key, $value = '' ) {
464 //echo "\n UPDATING '$key', OPTION = '" . $this->get_field_key( $key ) . "' avec VALUE = '" . $value ."'"; die( 'ok' );
465 return update_option( $this->get_field_key( $key ), $value );
466 }
467
468 public function get_option( $key, $empty_value = null ) {
469 //echo "\n<br /> vALUE de '$key' ( option = '" . $this->get_field_key( $key ) . '" ) = "' . get_option( $this->get_field_key( $key ) ).'"';
470 return get_option( $this->get_field_key( $key ) );
471 }
472
473 public function generate_radio_html( $key, $data ) {
474 $field_key = $this->get_field_key( $key );
475 $defaults = array(
476 'title' => '',
477 'label' => '',
478 'disabled' => false,
479 'class' => '',
480 'css' => '',
481 'type' => 'text',
482 'desc_tip' => false,
483 'description' => '',
484 'custom_attributes' => array(),
485 );
486
487 $data = wp_parse_args( $data, $defaults );
488
489 if ( ! $data['label'] ) {
490 $data['label'] = $data['title'];
491 }
492
493 ob_start();
494 ?>
495 <tr valign="top">
496 <th scope="row" class="titledesc">
497 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
498 </th>
499 <td class="forminp">
500 <fieldset>
501 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
502 <?php
503 $stored_value = $this->get_option( $key );
504 if( !$stored_value && isset( $data['default'] ) ) {
505 $stored_value = $data['default'];
506 }
507
508 if( isset( $data['values'] ) ) foreach( $data['values'] as $value => $label ) :
509 //echo " V $value / S $stored_value";
510
511 ?>
512 <input <?php disabled( $data['disabled'], true ); ?> class="<?php echo esc_attr( $data['class'] ); ?>" type="radio" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ) . '_' . sanitize_title( $value );?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo $value ?>" <?php checked( $stored_value, $value ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> />
513 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $label ); ?></label>
514 <br />
515 <?php endforeach; ?>
516 <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
517 </fieldset>
518 </td>
519 </tr>
520 <?php
521
522 return ob_get_clean();
523 }
524
525 public function generate_raw_html( $key, $data ) {
526 $field_key = $this->get_field_key( $key );
527 $defaults = array(
528 'title' => '',
529 'label' => '',
530 'disabled' => false,
531 'class' => '',
532 'css' => '',
533 'type' => 'text',
534 'desc_tip' => false,
535 'description' => '',
536 'custom_attributes' => array(),
537 );
538
539 $data = wp_parse_args( $data, $defaults );
540
541 ob_start();
542 ?>
543 <tr valign="top">
544 <th scope="row" class="titledesc">
545 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>
546 </th>
547 <td class="forminp">
548 <fieldset>
549 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
550 <?php
551 $stored_value = $this->get_option( $key );
552 if( !$stored_value && isset( $data['default'] ) ) {
553 $stored_value = $data['default'];
554 }
555
556 echo $data['raw_html']; ?>
557 <br/>
558 <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
559 </fieldset>
560 </td>
561 </tr>
562 <?php
563
564 return ob_get_clean();
565 }
566 }
567 }
568 return new WC_Improved_Settings_API( $plugin_id, $settingsStructure );
569 }
570 }