-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.wide-admin-menu-settings-page.php
136 lines (120 loc) · 3.85 KB
/
class.wide-admin-menu-settings-page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require_once 'constants.php';
if ( ! class_exists( 'Wide_Admin_Menu_Settings_Page' ) ) {
class Wide_Admin_Menu_Settings_Page {
private Wide_Admin_Menu_Renderer $renderer;
private int $width;
public function __construct( Wide_Admin_Menu_Renderer $renderer, int $width ) {
$this->renderer = $renderer;
$this->width = $width;
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
public function admin_menu(): void {
add_options_page(
esc_html__( 'Wide Admin Menu', 'wide-admin-menu' ),
esc_html__( 'Wide Admin Menu', 'wide-admin-menu' ),
'manage_options',
'wide-admin-menu',
array(
$this,
'admin_options_page'
),
99
);
}
public function admin_options_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( isset( $_GET['settings-updated'] ) ) {
add_settings_error(
'wide-admin-menu-options',
'wide-admin-menu',
esc_html__( 'Settings Saved', 'wide-admin-menu' ),
'success'
);
}
ob_start();
settings_fields( 'wide-admin-menu-option-group' );
$settings_fields = ob_get_clean();
ob_start();
do_settings_sections( 'wide-admin-menu-settings-page-general' );
$do_settings_sections = ob_get_clean();
ob_start();
submit_button( __( 'Save Settings', 'wide-admin-menu' ) );
$submit_button = ob_get_clean();
$this->renderer->render( 'wide-admin-menu-admin-options-page', array(
'pageTitle' => esc_html( get_admin_page_title() ),
'settings_fields' => $settings_fields,
'do_settings_sections' => $do_settings_sections,
'submit_button' => $submit_button
) );
}
public function register_settings(): void {
register_setting(
'wide-admin-menu-option-group',
'wide-admin-menu-options',
array(
'label' => esc_html__( 'Wide Admin Menu', 'wide-admin-menu' ),
'description' => esc_html__( 'Settings for the Wide Admin Menu plugin.', 'wide-admin-menu' ),
'sanitize_callback' => array( $this, 'sanitize_options' ),
'show_in_rest' => true,
'default' => array(
'wide_admin_menu_width' => $this->width
),
)
);
add_settings_section(
'wide-admin-menu-settings-section-general',
esc_html__( 'General', 'wide-admin-menu' ),
array( $this, 'section_general' ),
'wide-admin-menu-settings-page-general'
);
add_settings_field(
'wide-admin-menu-width',
esc_html__( 'Width', 'wide-admin-menu' ),
array( $this, 'menu_width_field' ),
'wide-admin-menu-settings-page-general',
'wide-admin-menu-settings-section-general',
array( 'label_for' => 'wide-admin-menu-width' )
);
}
public function sanitize_options( $options ): array {
$options['wide-admin-menu-width'] = sanitize_text_field( $options['wide-admin-menu-width'] );
$options['wide-admin-menu-width'] = (int) $options['wide-admin-menu-width'];
if ( $options['wide-admin-menu-width'] < 160 ) {
$options['wide-admin-menu-width'] = 160;
}
if ( $options['wide-admin-menu-width'] > 512 ) {
$options['wide-admin-menu-width'] = 512;
}
return $options;
}
public function section_general(): void {
$this->renderer->render( 'wide-admin-menu-settings-section-general-description', array(
'section_description' => esc_html__(
'General settings for the Wide Admin Menu plugin.',
'wide-admin-menu'
)
) );
}
public function menu_width_field(): void {
$field_value = esc_attr( $this->width );
$field_description = esc_html__(
'The width for the Admin menu.',
'wide-admin-menu'
);
$this->renderer->render(
'wide-admin-menu-width-input-field',
array(
'field_value' => $field_value,
'field_description' => $field_description
)
);
}
}
}