Reading). * * @package Tedrix */ defined( 'ABSPATH' ) || exit; /** * Tedrix in the admin's language: tedrix.se is the Swedish site, tedrix.io * the English one. Same service behind both. * * @return string Base URL without trailing slash. */ function tedrix_webb() { if ( defined( 'TEDRIX_WEBB' ) ) { return rtrim( (string) TEDRIX_WEBB, '/' ); // local development only } return 0 === strpos( get_user_locale(), 'sv' ) ? 'https://tedrix.se' : 'https://tedrix.io'; } /** * Cache key per admin language: the service writes its answer in the language * sent, so a cached answer is only shown to someone reading that language. * * @return string */ function tedrix_ai_nyckel() { return 'tedrix_ai_kontroll_' . substr( md5( get_user_locale() ), 0, 8 ); } /** * The findings Tedrix can actually fix on a WordPress site through this * plugin: title, description, structured data and text on the page. The rest * (robots.txt, registers, hosting) only get advice, so they get no button. * * @return string[] */ function tedrix_ai_fixbara() { return array( 'titel', 'beskrivning', 'schema', 'org', 'faq', 'svaret' ); } /** * Runs the check against the Tedrix service and caches the answer for a day. * * @return array{ok: bool, message?: string, antal?: array, punkter?: array, at?: int} */ function tedrix_ai_kontroll_kor() { /* A site on localhost or a private address cannot be fetched from the * internet. Say so here instead of sending a request that must fail. */ $vard = (string) wp_parse_url( home_url(), PHP_URL_HOST ); if ( 'localhost' === $vard || preg_match( '/\.(local|test|localhost)$/', $vard ) || filter_var( $vard, FILTER_VALIDATE_IP ) ) { return array( 'ok' => false, 'message' => __( 'This site runs on a local address, so it cannot be checked from the internet. The check works once the site is online.', 'tedrix' ), ); } $svar = wp_remote_post( tedrix_webb() . '/api/wp/kontroll', array( 'timeout' => 45, 'headers' => array( 'Content-Type' => 'application/json' ), 'body' => wp_json_encode( array( 'url' => home_url( '/' ), 'locale' => get_user_locale(), ) ), ) ); if ( is_wp_error( $svar ) ) { return array( 'ok' => false, 'message' => __( 'The check could not reach the Tedrix service. Try again in a moment.', 'tedrix' ), ); } $data = json_decode( (string) wp_remote_retrieve_body( $svar ), true ); if ( ! is_array( $data ) || empty( $data['ok'] ) ) { return array( 'ok' => false, 'message' => is_array( $data ) && ! empty( $data['message'] ) ? sanitize_text_field( $data['message'] ) : __( 'The check did not return a result. Try again in a moment.', 'tedrix' ), ); } $punkter = array(); foreach ( (array) ( $data['punkter'] ?? array() ) as $p ) { if ( ! is_array( $p ) ) { continue; } $niva = in_array( $p['niva'] ?? '', array( 'fel', 'varning', 'ok' ), true ) ? $p['niva'] : 'varning'; $punkter[] = array( 'id' => sanitize_key( (string) ( $p['id'] ?? '' ) ), 'niva' => $niva, 'rubrik' => sanitize_text_field( (string) ( $p['rubrik'] ?? '' ) ), 'text' => sanitize_text_field( (string) ( $p['text'] ?? '' ) ), ); } $ut = array( 'ok' => true, 'punkter' => $punkter, 'antal' => array( 'fel' => (int) ( $data['antal']['fel'] ?? 0 ), 'varning' => (int) ( $data['antal']['varning'] ?? 0 ), 'ok' => (int) ( $data['antal']['ok'] ?? 0 ), ), 'at' => time(), ); set_transient( tedrix_ai_nyckel(), $ut, DAY_IN_SECONDS ); return $ut; } /** * Renders the Google and AI section. The button posts back to the same page * with a nonce; the result is cached so reopening the page costs nothing. */ function tedrix_ai_kontroll_sektion() { $resultat = get_transient( tedrix_ai_nyckel() ); if ( isset( $_POST['tedrix_kontrollera'] ) && check_admin_referer( 'tedrix_kontrollera' ) ) { $resultat = tedrix_ai_kontroll_kor(); } ?>
' . esc_html( $resultat['message'] ?? '' ) . '
'; return; } $flaggade = array_filter( $resultat['punkter'], function ( $p ) { return 'ok' !== $p['niva']; } ); $ok = array_filter( $resultat['punkter'], function ( $p ) { return 'ok' === $p['niva']; } ); ?>