' . esc_html__( 'Settings', 'planetfoss' ) . '' ); return $links; } ); add_action( 'admin_init', static function (): void { register_setting( 'planetfoss', 'planetfoss_settings', array( 'type' => 'array', 'sanitize_callback' => 'planetfoss_sanitize_settings', 'default' => array(), ) ); } ); /** * Cleans the form. An empty key field keeps the stored key (the field never shows it); the checkbox removes it. * * @param mixed $in Form values. * Turning the connection on fetches right away; turning it off stops the schedule and removes the stored data. * * @return array{connect: bool, key: string, lang: string, auto: bool, css: bool} */ function planetfoss_sanitize_settings( $in ): array { $in = (array) $in; $old = planetfoss_settings(); $key = trim( sanitize_text_field( (string) ( $in['key'] ?? '' ) ) ); if ( '' === $key ) { $key = ! empty( $in['key_remove'] ) ? '' : $old['key']; } $out = array( 'connect' => ! empty( $in['connect'] ), 'key' => preg_match( '/^[A-Za-z0-9_-]{20,200}$/', $key ) ? $key : ( '' === $key ? '' : $old['key'] ), 'lang' => in_array( $in['lang'] ?? '', array( 'de', 'en' ), true ) ? (string) $in['lang'] : 'auto', 'auto' => ! empty( $in['auto'] ), 'css' => ! empty( $in['css'] ), ); if ( ! $out['connect'] ) { wp_clear_scheduled_hook( 'planetfoss_fetch' ); if ( $old['connect'] ) { delete_option( 'planetfoss_data' ); delete_option( 'planetfoss_status' ); } } elseif ( ! $old['connect'] || $out['key'] !== $old['key'] || $out['lang'] !== $old['lang'] ) { // Connection turned on, new key or new language: fetch right after saving instead of waiting 30 minutes. wp_schedule_single_event( time(), 'planetfoss_fetch' ); } return $out; } /** "Fetch now" button. */ add_action( 'admin_post_planetfoss_fetch', static function (): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'planetfoss' ), 403 ); } check_admin_referer( 'planetfoss_fetch' ); if ( planetfoss_settings()['connect'] ) { planetfoss_fetch(); } wp_safe_redirect( admin_url( 'options-general.php?page=planetfoss&fetched=1' ) ); exit; } ); /** One notice after activation: where the settings are. */ add_action( 'admin_notices', static function (): void { if ( ! get_option( 'planetfoss_notice' ) || ! current_user_can( 'manage_options' ) ) { return; } delete_option( 'planetfoss_notice' ); printf( '

%s %s

', esc_html__( 'PlanetFOSS is installed. It does not contact planetfoss.net until you turn on the connection in the settings.', 'planetfoss' ), esc_url( admin_url( 'options-general.php?page=planetfoss' ) ), esc_html__( 'Settings', 'planetfoss' ) ); } ); function planetfoss_settings_page(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $s = planetfoss_settings(); $status = (array) get_option( 'planetfoss_status', array() ); $data = planetfoss_data(); $next = wp_next_scheduled( 'planetfoss_fetch' ); ?>