admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'calcopolis_admin' ), 'connected' => Calcopolis_Connection::is_connected(), 'appBase' => Calcopolis_Hosts::app_base(), 'cdnBase' => Calcopolis_Hosts::cdn_base(), 'i18n' => array( 'reconnect' => __( 'Reconnect Calcopolis', 'calcopolis' ), 'copyShortcode' => __( 'Copy shortcode', 'calcopolis' ), 'copied' => __( 'Copied!', 'calcopolis' ), 'publishedList' => __( 'List of published forms', 'calcopolis' ), ), ) ); } public static function handle_actions() { if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) { return; } // OAuth callback if ( isset( $_GET['page'], $_GET['calcopolis_oauth'] ) && 'calcopolis' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! empty( $_GET['error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended self::add_notice( 'oauth_denied', __( 'Calcopolis connection was denied.', 'calcopolis' ), 'error' ); } elseif ( ! empty( $_GET['code'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $result = Calcopolis_OAuth_Client::exchange_code( sanitize_text_field( wp_unslash( $_GET['code'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( $result['ok'] ) { self::add_notice( 'oauth_ok', __( 'Connected to Calcopolis.', 'calcopolis' ), 'updated' ); } else { self::add_notice( 'oauth_fail', isset( $result['error'] ) ? $result['error'] : __( 'Connection failed.', 'calcopolis' ), 'error' ); } } self::redirect_with_notices(); } if ( empty( $_POST['calcopolis_action'] ) || empty( $_POST['_wpnonce'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'calcopolis_admin' ) ) { return; } $action = sanitize_key( wp_unslash( $_POST['calcopolis_action'] ) ); if ( 'connect' === $action ) { $result = Calcopolis_OAuth_Client::start_connect(); if ( ! empty( $result['ok'] ) && ! empty( $result['authorize_url'] ) && Calcopolis_Hosts::is_app_url( $result['authorize_url'] ) ) { self::redirect_to_authorize( $result['authorize_url'] ); } self::add_notice( 'connect_fail', ! empty( $result['error'] ) ? $result['error'] : __( 'Could not start Calcopolis connection.', 'calcopolis' ), 'error' ); } if ( 'disconnect' === $action ) { Calcopolis_OAuth_Client::disconnect(); self::add_notice( 'disconnected', __( 'Disconnected from Calcopolis.', 'calcopolis' ), 'updated' ); } if ( 'refresh' === $action ) { Calcopolis_Forms_Cache::get_forms( true ); self::add_notice( 'refreshed', __( 'Forms list refreshed.', 'calcopolis' ), 'updated' ); } } /** * Admin notice. settings_errors() prints the message without escaping. * * @param string $code Notice code. * @param string $message Plain-text message. * @param string $type error|updated|warning|info. */ private static function add_notice( $code, $message, $type ) { add_settings_error( 'calcopolis', $code, esc_html( $message ), $type ); } /** * Persist notices across the OAuth redirect, then return to this screen. */ private static function redirect_with_notices() { $errors = get_settings_errors( 'calcopolis' ); $url = admin_url( 'admin.php?page=calcopolis' ); if ( $errors ) { set_transient( 'settings_errors', $errors, 30 ); $url = add_query_arg( 'settings-updated', 'true', $url ); } wp_safe_redirect( $url ); exit; } /** * Send the administrator to the Calcopolis authorization page. * * @param string $url Authorization URL on the configured Calcopolis host. */ private static function redirect_to_authorize( $url ) { $host = wp_parse_url( $url, PHP_URL_HOST ); add_filter( 'allowed_redirect_hosts', static function ( $hosts ) use ( $host ) { $hosts[] = $host; return $hosts; } ); wp_safe_redirect( $url ); exit; } public static function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } settings_errors( 'calcopolis' ); $connected = Calcopolis_Connection::is_connected(); if ( $connected ) { Calcopolis_Connection::ensure_profile(); } ?>
%1$s). Connecting sends this site\'s address, name, and OAuth callback URL to that service and stores connection tokens in WordPress. Embedded calculators load their script from %2$s when a visitor opens a page that contains one. See the Terms of Service and Privacy Policy.', 'calcopolis' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array(), ), ) ), esc_url( $app_base ), esc_url( $cdn_base ), esc_url( $app_base . '/terms/' ), esc_url( $app_base . '/privacy/' ) ); ?>
' . esc_html__( 'Reconnect Calcopolis', 'calcopolis' ) . ''; } else { echo '' . esc_html__( 'Could not load forms.', 'calcopolis' ) . '
'; } return; } $forms = $result['forms']; if ( ! empty( $result['truncated'] ) ) { echo '' . esc_html__( 'Showing the first 100 forms. Open the Calcopolis app for the full list.', 'calcopolis' ) . '
'; } ?>| | | |||||||