| @@ -4,9 +4,26 @@ | ||
| 4 | 4 | |
| 5 | 5 | public $form_field = []; |
| 6 | 6 | |
| 7 | 7 | public function __construct() |
| 8 | - { | |
| 8 | + { | |
| 9 | + // Qui non si traduce nulla. Il plugin parte su plugins_loaded, che viene | |
| 10 | + // prima di init: da WordPress 6.7 chiamare __() a quel punto fa scattare | |
| 11 | + // la notice "Translation loading was triggered too early". L'elenco dei | |
| 12 | + // campi si costruisce quando serve davvero, cioe su admin_init. | |
| 13 | + } | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * Definizione dei campi del modulo impostazioni. | |
| 17 | + * | |
| 18 | + * @return array | |
| 19 | + */ | |
| 20 | + public function get_form_fields() | |
| 21 | + { | |
| 22 | + if ( ! empty( $this->form_field ) ) { | |
| 23 | + return $this->form_field; | |
| 24 | + } | |
| 25 | + | |
| 9 | 26 | $this->form_field = array( |
| 10 | 27 | '0' => array( |
| 11 | 28 | 'id' => 'basic-auth-plugin-admin-enable', |
| 12 | 29 | 'title' => __('Enable for wp-admin', 'easy-basic-authentication'), |
| @@ -52,8 +69,13 @@ | ||
| 52 | 69 | 'title' => __('Url White list', 'easy-basic-authentication'), |
| 53 | 70 | 'callback' => 'basic_auth_plugin_urlwhitelist_cb', |
| 54 | 71 | ), |
| 55 | 72 | '9' => array( |
| 73 | + 'id' => 'basic-auth-plugin-access-token', | |
| 74 | + 'title' => __('Access token for crawlers', 'easy-basic-authentication'), | |
| 75 | + 'callback' => 'basic_auth_plugin_access_token_cb', | |
| 76 | + ), | |
| 77 | + '10' => array( | |
| 56 | 78 | 'id' => 'basic-auth-plugin-remove-data-after-uninstall', |
| 57 | 79 | 'title' => __('Remove plugin data after uninstall', 'easy-basic-authentication'), |
| 58 | 80 | 'callback' => 'basic_auth_plugin_remove_data_after_uninstall_cb', |
| 59 | 81 | ), |
| @@ -58,10 +80,12 @@ | ||
| 58 | 80 | 'callback' => 'basic_auth_plugin_remove_data_after_uninstall_cb', |
| 59 | 81 | ), |
| 60 | 82 | |
| 61 | 83 | ); |
| 84 | + | |
| 85 | + return $this->form_field; | |
| 62 | 86 | } |
| 63 | - | |
| 87 | + | |
| 64 | 88 | public function basic_auth_plugin_settings_init() { |
| 65 | 89 | register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable', array( 'sanitize_callback' => 'absint' ) ); |
| 66 | 90 | register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable', array( 'sanitize_callback' => 'absint' ) ); |
| 67 | 91 | register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username', array( 'sanitize_callback' => 'sanitize_text_field' ) ); |
| @@ -73,9 +97,9 @@ | ||
| 73 | 97 | array($this,'basic_auth_plugin_section_cb'), |
| 74 | 98 | 'basic-auth-plugin-settings' |
| 75 | 99 | ); |
| 76 | 100 | |
| 77 | - foreach($this->form_field as $field) { | |
| 101 | + foreach($this->get_form_fields() as $field) { | |
| 78 | 102 | add_settings_field( |
| 79 | 103 | $field['id'], |
| 80 | 104 | esc_html($field['title']), |
| 81 | 105 | array($this,$field['callback']), |
| @@ -136,8 +160,33 @@ | ||
| 136 | 160 | $email_alert = get_option( 'basic_auth_plugin_alertemail' ); |
| 137 | 161 | $this->printInputText("text","basic_auth_plugin_alertemail",esc_attr( $email_alert ),__('Enter the email', 'easy-basic-authentication')); |
| 138 | 162 | } |
| 139 | 163 | |
| 164 | + public function basic_auth_plugin_access_token_cb() { | |
| 165 | + $token = (string) get_option( 'basic_auth_plugin_access_token', '' ); | |
| 166 | + ?> | |
| 167 | + <input type="text" name="basic_auth_plugin_access_token" value="<?php echo esc_attr( $token ); ?>" | |
| 168 | + class="regular-text code" readonly onclick="this.select();" autocomplete="off"> | |
| 169 | + <label style="display:block;margin-top:6px;"> | |
| 170 | + <input type="checkbox" name="basic_auth_plugin_generate_token" value="1"> | |
| 171 | + <?php echo '' === $token | |
| 172 | + ? esc_html__( 'Generate a token when saving', 'easy-basic-authentication' ) | |
| 173 | + : esc_html__( 'Replace this token with a new one when saving', 'easy-basic-authentication' ); ?> | |
| 174 | + </label> | |
| 175 | + <?php if ( '' !== $token ) : ?> | |
| 176 | + <label style="display:block;margin-top:4px;"> | |
| 177 | + <input type="checkbox" name="basic_auth_plugin_delete_token" value="1"> | |
| 178 | + <?php esc_html_e( 'Delete the token and close this door', 'easy-basic-authentication' ); ?> | |
| 179 | + </label> | |
| 180 | + <?php endif; ?> | |
| 181 | + <p class="description"> | |
| 182 | + <?php esc_html_e( 'Lets a crawler or an external service through without a username and password. Send it as an X-Basic-Auth-Token header, or as Authorization: Bearer.', 'easy-basic-authentication' ); ?> | |
| 183 | + <br> | |
| 184 | + <strong><?php esc_html_e( 'It opens the site only — the admin area and the login page stay protected. Anyone holding this token can read your whole site, so treat it as a password.', 'easy-basic-authentication' ); ?></strong> | |
| 185 | + </p> | |
| 186 | + <?php | |
| 187 | + } | |
| 188 | + | |
| 140 | 189 | public function basic_auth_plugin_whitelist_cb() { |
| 141 | 190 | $white_list = get_option( 'basic_auth_plugin_whitelist' ); |
| 142 | 191 | $this->printInputText("text","basic_auth_plugin_whitelist",esc_attr( $white_list ),__('White list, separated by comma. Ex. 127.0.0.1, 1.1.1.1/20, 1.1.0.0 - 1.1.0.255', 'easy-basic-authentication')); |
| 143 | 192 | } |
| @@ -208,8 +257,16 @@ | ||
| 208 | 257 | update_option( 'basic_auth_plugin_admin_log_enable', $log_enable ); |
| 209 | 258 | update_option( 'basic_auth_plugin_alert_enable', $alert_enable ); |
| 210 | 259 | update_option( 'basic_auth_plugin_remove_data_after_uninstall', $remove_data ); |
| 211 | 260 | update_option( 'basic_auth_plugin_urlwhitelist', $url_white_list ); |
| 261 | + | |
| 262 | + // Il token non si scrive a mano: o lo si genera o lo si cancella. Cosi | |
| 263 | + // non puo essere corto o indovinabile. | |
| 264 | + if ( ! empty( $param['basic_auth_plugin_delete_token'] ) ) { | |
| 265 | + delete_option( 'basic_auth_plugin_access_token' ); | |
| 266 | + } elseif ( ! empty( $param['basic_auth_plugin_generate_token'] ) ) { | |
| 267 | + update_option( 'basic_auth_plugin_access_token', wp_generate_password( 48, false, false ) ); | |
| 268 | + } | |
| 212 | 269 | |
| 213 | 270 | if( $this->validateIpList( $white_list ) || strlen($white_list) == 0 ) { |
| 214 | 271 | update_option( 'basic_auth_plugin_whitelist', $white_list ); |
| 215 | 272 | } else { |