1 | <?php |
||
16 | class Admin { |
||
17 | |||
18 | /** |
||
19 | * Instantiates the admin panel object. |
||
20 | * |
||
21 | * @uses \add_action() |
||
22 | */ |
||
23 | public function __construct () { |
||
24 | \add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
25 | \add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Initializes the admin panel and registers settings fields. |
||
30 | * |
||
31 | * Triggered by the `admin_init` action. |
||
32 | * |
||
33 | * @uses \add_action() |
||
34 | */ |
||
35 | public function admin_init() { |
||
41 | |||
42 | /** |
||
43 | * Register the menu entry for the plugin's settings page. |
||
44 | * |
||
45 | * @since 1.1.0 |
||
46 | * |
||
47 | * @uses \__() |
||
48 | * @uses \add_options_page() |
||
49 | */ |
||
50 | public function admin_menu() { |
||
51 | \add_options_page( |
||
52 | __( 'Cassava CAS Server', 'wp-cas-server' ), |
||
53 | __( 'Cassava CAS Server', 'wp-cas-server' ), |
||
54 | 'manage_options', |
||
55 | Plugin::SLUG, |
||
56 | array( $this, 'pageSettings' ) |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Presents admin notices. |
||
62 | * |
||
63 | * Triggered by the `admin_notices` action. |
||
64 | * |
||
65 | * @uses \current_user_can() |
||
66 | * @uses \is_ssl() |
||
67 | * |
||
68 | * @uses ::adminNoticeNoSSL() |
||
69 | * |
||
70 | * @SuppressWarnings(CamelCaseMethodName) |
||
71 | */ |
||
72 | public function admin_notices() { |
||
73 | if ( ! \is_ssl() && \current_user_can( 'install_plugins' ) ) { |
||
74 | $this->adminNoticeNoSSL(); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Nags the user with an administration notice explaining that the plugin will only |
||
80 | * work if HTTP |
||
81 | */ |
||
82 | protected function adminNoticeNoSSL() { |
||
83 | ?> |
||
84 | <div class="update-nag"> |
||
85 | <?php _e( 'Cassava CAS Server requires that this site be configured for HTTPS. For more information, contact your system administrator or hosting provider.', 'wp-cas-server' ); ?> |
||
86 | </div> |
||
87 | <?php |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Updates the CAS server endpoint when saving permalinks. |
||
92 | * |
||
93 | * @uses \is_admin() |
||
94 | * @uses \sanitize_text_field() |
||
95 | */ |
||
96 | protected function savePermalinks() { |
||
|
|||
97 | if ( ! \is_admin() ) { |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | $option = Options::KEY . '_endpoint_slug'; |
||
102 | |||
103 | if ( false |
||
104 | || isset( $_POST['permalink_structure'] ) |
||
105 | || isset( $_POST['category_base'] ) |
||
106 | || isset( $_POST[ $option ] ) |
||
107 | ) { |
||
108 | Options::set( 'endpoint_slug', trim( \sanitize_text_field( $_POST[ $option ] ) ) ); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Register plugin settings. |
||
114 | * |
||
115 | * @uses \add_settings_field() |
||
116 | * @uses \add_settings_section() |
||
117 | * @uses \register_setting() |
||
118 | * |
||
119 | * @since 1.0.0 |
||
120 | */ |
||
121 | protected function addSettings() { |
||
122 | \register_setting( |
||
123 | Plugin::SLUG, |
||
124 | Options::KEY, |
||
125 | array( $this, 'validateSettings' ) |
||
126 | ); |
||
127 | |||
128 | // Default plugin settings: |
||
129 | |||
130 | \add_settings_section( 'default', '', false, Plugin::SLUG ); |
||
131 | |||
132 | \add_settings_field( |
||
133 | 'attributes', |
||
134 | __( 'User Attributes To Return', 'wp-cas-server' ), |
||
135 | array( $this, 'fieldUserAttributes' ), |
||
136 | Plugin::SLUG |
||
137 | ); |
||
138 | |||
139 | // Permalink settings: |
||
140 | |||
141 | \add_settings_field( |
||
142 | Options::KEY . '_endpoint_slug', |
||
143 | __( 'CAS server base', 'wp-cas-server' ), |
||
144 | array( $this, 'fieldPermalinksEndpointSlug' ), |
||
145 | 'permalink', |
||
146 | 'optional' |
||
147 | ); |
||
148 | |||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Validates and updates CAS server plugin settings. |
||
153 | * |
||
154 | * @param array $input Unvalidated input arguments when settings are updated. |
||
155 | * |
||
156 | * @return array Validated plugin settings to be saved in the database. |
||
157 | * |
||
158 | * @since 1.1.0 |
||
159 | */ |
||
160 | public function validateSettings( $input ) { |
||
167 | |||
168 | /** |
||
169 | * Display the configuration field for the CAS endpoint. |
||
170 | * |
||
171 | * @uses \esc_attr() |
||
172 | * |
||
173 | * @since 1.0.0 |
||
174 | */ |
||
175 | public function fieldPermalinksEndpointSlug() { |
||
185 | |||
186 | /** |
||
187 | * Displays the CAS server settings page in the dashboard. |
||
188 | * |
||
189 | * @uses \_e() |
||
190 | * @uses \do_settings_sections() |
||
191 | * @uses \settings_fields() |
||
192 | * @uses \submit_button() |
||
193 | * |
||
194 | * @since 1.1.0 |
||
195 | */ |
||
196 | public function pageSettings() { |
||
211 | |||
212 | /** |
||
213 | * Display the configuration fieldset for the user attributs to return on successful |
||
214 | * requests. |
||
215 | * |
||
216 | * Checked attributes for the authenticated user will be returned on successful |
||
217 | * `/validateService` request responses inside an optional `<cas:attributes></cas:attributes>` |
||
218 | * tag. |
||
219 | * |
||
220 | * @uses \_e() |
||
221 | * @uses \apply_filters() |
||
222 | * |
||
223 | * @since 1.1.0 |
||
224 | */ |
||
225 | public function fieldUserAttributes() { |
||
277 | } |
||
278 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: