the settings, with defaults */ function fipeo_settings() { $s = get_option( 'fipeo_settings', array() ); $s = is_array( $s ) ? $s : array(); $d = array( 'key' => '', 'lang' => '', 'show' => array_fill_keys( fipeo_pieces(), true ), 'assistant_everywhere' => false, 'agents_footer' => false, ); $s = array_merge( $d, $s ); $s['show'] = array_merge( array_fill_keys( fipeo_pieces(), false ), is_array( $s['show'] ) ? $s['show'] : array() ); return $s; } function fipeo_valid_key( $key ) { return is_string( $key ) && 1 === preg_match( '/^f[A-Za-z0-9]{31}$/', $key ); } /** @return array {pieces, school, fetched_at, error} (pieces empty when nothing was read yet) */ function fipeo_config() { $c = get_option( 'fipeo_config', array() ); $c = is_array( $c ) ? $c : array(); return array_merge( array( 'pieces' => array(), 'school' => '', 'site' => '', 'fetched_at' => 0, 'error' => '', ), $c ); } /** * The language of the pieces: the setting, or the language of the page being shown (get_locale follows WPML and Polylang), if Fipeo * speaks it; otherwise English. */ function fipeo_lang() { $s = fipeo_settings(); $lang = '' !== $s['lang'] ? $s['lang'] : strtolower( substr( (string) get_locale(), 0, 2 ) ); return in_array( $lang, array( 'en', 'es', 'fr', 'it', 'de', 'pt' ), true ) ? $lang : 'en'; } /** * Read the configuration of the school from Fipeo and keep it. Called when the settings are saved, from the «Refresh» button and twice a * day by WP-Cron; never while a visitor loads a page. If Fipeo cannot be reached the previous configuration stays (with the error). * * @param string|null $key the key to use (null: the saved one) * @return bool true when a configuration was read */ function fipeo_refresh( $key = null ) { $s = fipeo_settings(); $old = fipeo_config(); if ( is_string( $key ) ) { $s['key'] = $key; } if ( ! fipeo_valid_key( $s['key'] ) ) { update_option( 'fipeo_config', array( 'pieces' => array(), 'school' => '', 'site' => '', 'fetched_at' => 0, 'error' => '' ) ); return false; } $url = fipeo_origin() . '/api/v1/public/wordpress/' . rawurlencode( $s['key'] ) . '/config?site=' . rawurlencode( home_url( '/' ) ); $r = wp_remote_get( $url, array( 'timeout' => 10, 'headers' => array( 'Accept' => 'application/json' ), 'user-agent' => 'Fipeo-WordPress/' . FIPEO_VERSION . '; ' . home_url( '/' ), ) ); if ( is_wp_error( $r ) ) { $old['error'] = 'unreachable'; update_option( 'fipeo_config', $old ); return false; } $code = (int) wp_remote_retrieve_response_code( $r ); $body = json_decode( (string) wp_remote_retrieve_body( $r ), true ); if ( 404 === $code ) { // the key was replaced or removed in Fipeo: show nothing any more update_option( 'fipeo_config', array( 'pieces' => array(), 'school' => '', 'site' => '', 'fetched_at' => time(), 'error' => 'bad_key' ) ); return false; } if ( 200 !== $code || ! is_array( $body ) || ! isset( $body['pieces'] ) || ! is_array( $body['pieces'] ) ) { $old['error'] = 'unreachable'; update_option( 'fipeo_config', $old ); return false; } update_option( 'fipeo_config', array( 'pieces' => fipeo_clean_pieces( $body['pieces'] ), 'school' => isset( $body['school']['name'] ) ? sanitize_text_field( (string) $body['school']['name'] ) : '', 'site' => isset( $body['site'] ) ? esc_url_raw( (string) $body['site'] ) : '', 'fetched_at' => time(), 'error' => '', ) ); return true; } /** Only https addresses (they come from Fipeo, but they go into the school's pages). */ function fipeo_clean_url( $v ) { $v = is_string( $v ) ? esc_url_raw( $v, array( 'https' ) ) : ''; return 0 === strpos( $v, 'https://' ) ? $v : ''; } /** * Keep only what the plugin uses, each value checked (keys by their format, addresses https only). * * @param array $p the pieces of Fipeo's answer * @return array> */ function fipeo_clean_pieces( $p ) { $g = function ( $k ) use ( $p ) { return isset( $p[ $k ] ) && is_array( $p[ $k ] ) ? $p[ $k ] : array(); }; $ok = function ( $a ) { return ! empty( $a['available'] ); }; $tri = function ( $a ) { return array_key_exists( 'allowed_here', $a ) && null !== $a['allowed_here'] ? (bool) $a['allowed_here'] : null; }; $b = $g( 'booking' ); $a = $g( 'assistant' ); $r = $g( 'reviews' ); $t = $g( 'placement_test' ); $ag = $g( 'agents' ); $st = $g( 'student_app' ); $wk = isset( $b['widget_key'] ) && is_string( $b['widget_key'] ) && preg_match( '/^w[A-Za-z0-9]{31}$/', $b['widget_key'] ) ? $b['widget_key'] : ''; $ak = isset( $a['key'] ) && is_string( $a['key'] ) && preg_match( '/^a[A-Za-z0-9]{31}$/', $a['key'] ) ? $a['key'] : ''; return array( 'booking' => array( 'available' => $ok( $b ) && '' !== $wk, 'widget_key' => $wk, 'allowed_here' => $tri( $b ), 'pay_online_url' => fipeo_clean_url( isset( $b['pay_online_url'] ) ? $b['pay_online_url'] : '' ), ), 'assistant' => array( 'available' => $ok( $a ) && '' !== $ak, 'key' => $ak, 'allowed_here' => $tri( $a ), ), 'reviews' => array( 'available' => $ok( $r ) && '' !== fipeo_clean_url( isset( $r['url'] ) ? $r['url'] : '' ), 'url' => fipeo_clean_url( isset( $r['url'] ) ? $r['url'] : '' ), ), 'placement_test' => array( 'available' => $ok( $t ) && '' !== fipeo_clean_url( isset( $t['url'] ) ? $t['url'] : '' ), 'url' => fipeo_clean_url( isset( $t['url'] ) ? $t['url'] : '' ), ), 'agents' => array( 'available' => '' !== fipeo_clean_url( isset( $ag['url'] ) ? $ag['url'] : '' ), 'portal' => ! empty( $ag['portal'] ), 'url' => fipeo_clean_url( isset( $ag['url'] ) ? $ag['url'] : '' ), ), 'student_app' => array( 'available' => '' !== fipeo_clean_url( isset( $st['url'] ) ? $st['url'] : '' ), 'url' => fipeo_clean_url( isset( $st['url'] ) ? $st['url'] : '' ), ), ); } /** * A piece the school chose to show and that Fipeo has switched on for it, or null. * * @return array|null */ function fipeo_piece( $id ) { $s = fipeo_settings(); $c = fipeo_config(); if ( empty( $s['show'][ $id ] ) || empty( $c['pieces'][ $id ]['available'] ) ) { return null; } return $c['pieces'][ $id ]; } /** * The words on the buttons, in the language of the pieces. They can be changed with the «label» attribute of each shortcode or block, * or with the filter «fipeo_label» ($text, $id, $lang). */ function fipeo_label( $id, $lang ) { $t = array( 'en' => array( 'pay' => 'Book and pay online', 'test' => 'Take your placement test', 'agents' => 'For agents', 'agents_join' => 'Agents: work with us', 'app' => 'Student app', 'test_note' => 'Already booked? Sign in to the student app to take your placement test.' ), 'es' => array( 'pay' => 'Reserva y paga online', 'test' => 'Haz tu prueba de nivel', 'agents' => 'Para agentes', 'agents_join' => 'Agentes: trabaja con nosotros', 'app' => 'App del alumno', 'test_note' => '¿Ya has reservado? Entra en la app del alumno para hacer tu prueba de nivel.' ), 'fr' => array( 'pay' => 'Réserver et payer en ligne', 'test' => 'Passez votre test de niveau', 'agents' => 'Pour les agents', 'agents_join' => 'Agents : travaillez avec nous', 'app' => 'Appli étudiant', 'test_note' => 'Déjà réservé ? Connectez-vous à l’appli étudiant pour passer votre test de niveau.' ), 'it' => array( 'pay' => 'Prenota e paga online', 'test' => 'Fai il tuo test di livello', 'agents' => 'Per gli agenti', 'agents_join' => 'Agenti: lavora con noi', 'app' => 'App dello studente', 'test_note' => 'Hai già prenotato? Entra nell’app dello studente per fare il test di livello.' ), 'de' => array( 'pay' => 'Online buchen und bezahlen', 'test' => 'Zum Einstufungstest', 'agents' => 'Für Agenturen', 'agents_join' => 'Agenturen: arbeiten Sie mit uns', 'app' => 'Studenten-App', 'test_note' => 'Schon gebucht? Melden Sie sich in der Studenten-App an, um Ihren Einstufungstest zu machen.' ), 'pt' => array( 'pay' => 'Reserve e pague online', 'test' => 'Faça o seu teste de nível', 'agents' => 'Para agentes', 'agents_join' => 'Agentes: trabalhe connosco', 'app' => 'App do aluno', 'test_note' => 'Já reservou? Entre na app do aluno para fazer o seu teste de nível.' ), ); $l = isset( $t[ $lang ] ) ? $t[ $lang ] : $t['en']; return (string) apply_filters( 'fipeo_label', isset( $l[ $id ] ) ? $l[ $id ] : '', $id, $lang ); }