Fipeo: the key, what to show, the state of each piece in Fipeo and the shortcodes to copy. Only for administrators * (manage_options). The settings go through the Settings API (nonce and sanitising); «Refresh» is an admin-post action with its nonce. * * @package Fipeo */ if ( ! defined( 'ABSPATH' ) ) { exit; } add_action( 'admin_menu', 'fipeo_admin_menu' ); add_action( 'admin_init', 'fipeo_admin_init' ); add_action( 'admin_post_fipeo_refresh', 'fipeo_admin_refresh' ); add_filter( 'plugin_action_links_' . plugin_basename( FIPEO_DIR . 'fipeo.php' ), 'fipeo_action_links' ); function fipeo_admin_menu() { add_options_page( __( 'Fipeo', 'fipeo' ), __( 'Fipeo', 'fipeo' ), 'manage_options', 'fipeo', 'fipeo_admin_page' ); } function fipeo_action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'fipeo' ) . '' ); return $links; } function fipeo_admin_init() { register_setting( 'fipeo', 'fipeo_settings', array( 'type' => 'array', 'sanitize_callback' => 'fipeo_sanitize_settings', 'default' => array(), ) ); // a configuration older than 12 hours is read again when an administrator opens the admin area (WP-Cron may not run on quiet sites) $c = fipeo_config(); $s = fipeo_settings(); if ( current_user_can( 'manage_options' ) && fipeo_valid_key( $s['key'] ) && time() - (int) $c['fetched_at'] > 12 * HOUR_IN_SECONDS && ! get_transient( 'fipeo_refreshing' ) ) { set_transient( 'fipeo_refreshing', 1, 5 * MINUTE_IN_SECONDS ); fipeo_refresh(); } } /** * @param mixed $in what the form sent * @return array */ function fipeo_sanitize_settings( $in ) { $in = is_array( $in ) ? $in : array(); $key = isset( $in['key'] ) ? trim( sanitize_text_field( wp_unslash( (string) $in['key'] ) ) ) : ''; $lang = isset( $in['lang'] ) ? sanitize_key( (string) $in['lang'] ) : ''; $show = array(); foreach ( fipeo_pieces() as $p ) { $show[ $p ] = ! empty( $in['show'][ $p ] ); } if ( '' !== $key && ! fipeo_valid_key( $key ) ) { add_settings_error( 'fipeo_settings', 'fipeo_key', __( 'This is not a Fipeo key. Copy it again from your Fipeo panel: Integrations > WordPress. It starts with «f» and has 32 characters.', 'fipeo' ) ); $key = fipeo_settings()['key']; } $out = array( 'key' => $key, 'lang' => in_array( $lang, array( 'en', 'es', 'fr', 'it', 'de', 'pt' ), true ) ? $lang : '', 'show' => $show, 'assistant_everywhere' => ! empty( $in['assistant_everywhere'] ), 'agents_footer' => ! empty( $in['agents_footer'] ), ); // read the configuration with this key (WordPress may call this function twice on the first save: one read is enough) if ( ! did_action( 'fipeo_settings_read' ) ) { do_action( 'fipeo_settings_read' ); fipeo_refresh( $key ); } return $out; } function fipeo_admin_refresh() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'fipeo' ) ); } check_admin_referer( 'fipeo_refresh' ); $ok = fipeo_refresh(); wp_safe_redirect( add_query_arg( array( 'page' => 'fipeo', 'fipeo_refreshed' => $ok ? '1' : '0' ), admin_url( 'options-general.php' ) ) ); exit; } /** The state of one piece for the table: [ok (bool), text]. */ function fipeo_admin_state( $id, $piece, $home ) { if ( empty( $piece ) ) { return array( false, __( 'Not read from Fipeo yet.', 'fipeo' ) ); } if ( empty( $piece['available'] ) ) { $setup = array( 'booking' => __( 'Not switched on in Fipeo: create a price widget in your panel (Booking form > Price widget) and switch it on.', 'fipeo' ), 'assistant' => __( 'Not switched on in Fipeo: switch on your website assistant in your panel (Assistant).', 'fipeo' ), 'reviews' => __( 'Not switched on in Fipeo: create the reviews widget in your panel (Reviews > Widget).', 'fipeo' ), 'placement_test' => __( 'You have no active placement test in Fipeo (Academic > Placement tests).', 'fipeo' ), ); return array( false, isset( $setup[ $id ] ) ? $setup[ $id ] : __( 'Not available.', 'fipeo' ) ); } if ( array_key_exists( 'allowed_here', $piece ) && false === $piece['allowed_here'] ) { return array( false, sprintf( /* translators: %s: the address of this WordPress, for example https://www.myschool.com */ __( 'Switched on, but your website is not among its allowed websites in Fipeo: add %s to its list of websites in your panel, then press «Refresh».', 'fipeo' ), $home ), ); } $extra = ''; if ( 'booking' === $id ) { $extra = '' !== $piece['pay_online_url'] ? ' ' . __( 'With the «Book and pay online» button (your own Stripe or PayPal).', 'fipeo' ) : ' ' . __( 'Online payment with your own Stripe or PayPal: ask Fipeo to switch it on for your school.', 'fipeo' ); } if ( 'agents' === $id && empty( $piece['portal'] ) ) { $extra = ' ' . __( 'Your agents portal is not switched on: the button opens the free sign-up for agencies in Fipeo.', 'fipeo' ); } return array( true, __( 'Ready.', 'fipeo' ) . $extra ); } function fipeo_admin_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $s = fipeo_settings(); $c = fipeo_config(); $home = untrailingslashit( home_url() ); $names = array( 'booking' => array( __( 'Prices and online booking', 'fipeo' ), '[fipeo_booking]' ), 'assistant' => array( __( 'Assistant', 'fipeo' ), '[fipeo_assistant]' ), 'reviews' => array( __( 'Reviews', 'fipeo' ), '[fipeo_reviews]' ), 'placement_test' => array( __( 'Placement test', 'fipeo' ), '[fipeo_placement_test]' ), 'agents' => array( __( 'Agents portal button', 'fipeo' ), '[fipeo_agents_button]' ), 'student_app' => array( __( 'Student app', 'fipeo' ), '[fipeo_student_app]' ), ); $langs = array( '' => __( 'The language of each page', 'fipeo' ), 'en' => 'English', 'es' => 'Español', 'fr' => 'Français', 'it' => 'Italiano', 'de' => 'Deutsch', 'pt' => 'Português', ); ?>

' . esc_html( $ok ? __( 'Read from Fipeo.', 'fipeo' ) : __( 'Fipeo could not be read. Check the key and try again in a few minutes.', 'fipeo' ) ) . '

'; } if ( 'bad_key' === $c['error'] ) { echo '

' . esc_html__( 'Fipeo does not know this key any more (a new one was created, or it was removed). Copy the current key from your Fipeo panel: Integrations > WordPress.', 'fipeo' ) . '

'; } elseif ( 'unreachable' === $c['error'] ) { echo '

' . esc_html__( 'Fipeo could not be reached the last time: your pages keep showing what was read before.', 'fipeo' ) . '

'; } ?>

$n ) : $st = fipeo_admin_state( $id, isset( $c['pieces'][ $id ] ) ? $c['pieces'][ $id ] : array(), $home ); ?>