render_notice(); echo '
'; echo '

' . esc_html__( 'AdPriva Settings', 'adpriva' ) . '

'; settings_errors( Adpriva_Config::OPTION ); echo '

' . esc_html__( 'Connection', 'adpriva' ) . '

'; if ( Adpriva_Tokens::is_connected() ) { $this->render_connected(); } else { $this->render_disconnected(); } echo '
'; echo '
'; settings_fields( Adpriva_Config::OPTION_GROUP ); do_settings_sections( Adpriva_Config::PAGE ); submit_button(); echo '
'; echo '
'; } /** * Start the OAuth flow by redirecting to the AdPriva authorize endpoint. * * @return void */ public function handle_connect() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to connect AdPriva.', 'adpriva' ) ); } check_admin_referer( 'adpriva_connect' ); // External authorize URL — wp_safe_redirect would reject the off-site host. wp_redirect( Adpriva_OAuth::connect_url() ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Intentional redirect to the external AdPriva authorize endpoint. exit; } /** * Revoke and clear the connection. * * @return void */ public function handle_disconnect() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to disconnect AdPriva.', 'adpriva' ) ); } check_admin_referer( 'adpriva_disconnect' ); Adpriva_OAuth::disconnect(); $this->redirect_with_notice( 'disconnected' ); } /** * Detect the OAuth redirect back to the settings page and exchange the code. * * @return void */ public function maybe_handle_callback() { if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) { return; } // The OAuth 'state' parameter (validated in handle_callback) is the CSRF // protection for this redirect-back; there is no WP nonce to verify. // phpcs:disable WordPress.Security.NonceVerification.Recommended $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; if ( 'adpriva' !== $page || ! isset( $_GET['code'], $_GET['state'] ) ) { return; } $code = sanitize_text_field( wp_unslash( $_GET['code'] ) ); $state = sanitize_text_field( wp_unslash( $_GET['state'] ) ); // phpcs:enable WordPress.Security.NonceVerification.Recommended $result = Adpriva_OAuth::handle_callback( $code, $state ); $this->redirect_with_notice( is_wp_error( $result ) ? 'error' : 'connected' ); } /** * Redirect back to a clean settings URL carrying a notice key. * * @param string $notice Notice key. * @return void */ private function redirect_with_notice( $notice ) { wp_safe_redirect( add_query_arg( array( 'page' => 'adpriva', 'adpriva_notice' => $notice, ), admin_url( 'admin.php' ) ) ); exit; } /** * Render the settings page. * * @return void */ public function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $this->render_notice(); // The top-level page is the results dashboard (it shows its own Connect // call-to-action when the site is not yet connected). Connection management // and configuration live on the Settings submenu. Adpriva_Dashboard::render(); } /** * Output an admin notice based on the adpriva_notice query arg. * * @return void */ private function render_notice() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only display of a status flag from our own redirect. $notice = isset( $_GET['adpriva_notice'] ) ? sanitize_text_field( wp_unslash( $_GET['adpriva_notice'] ) ) : ''; if ( '' === $notice ) { return; } $messages = array( 'connected' => array( 'success', __( 'Your site is now connected to AdPriva.', 'adpriva' ) ), 'disconnected' => array( 'success', __( 'Your site has been disconnected from AdPriva.', 'adpriva' ) ), 'error' => array( 'error', __( 'The connection could not be completed. Please try again.', 'adpriva' ) ), ); if ( ! isset( $messages[ $notice ] ) ) { return; } printf( '

%2$s

', esc_attr( $messages[ $notice ][0] ), esc_html( $messages[ $notice ][1] ) ); } /** * Render the "connected" state. * * @return void */ private function render_connected() { $domain = Adpriva_Tokens::domain(); echo '

' . sprintf( /* translators: %s: connected site domain. */ esc_html__( 'Connected as %s. The AdPriva verification tag is loading on your site.', 'adpriva' ), '' . esc_html( $domain ) . '' ) . '

'; echo '

' . esc_html__( 'View full traffic in AdPriva', 'adpriva' ) . '

'; echo '
'; echo ''; wp_nonce_field( 'adpriva_disconnect' ); echo '

'; echo '
'; } /** * Render the "not connected" state with the Connect button. * * @return void */ private function render_disconnected() { echo '

' . esc_html__( 'Connect this site to AdPriva to verify your ad traffic and see which traffic the automation checks flagged in your dashboard.', 'adpriva' ) . '

'; echo '
'; echo ''; wp_nonce_field( 'adpriva_connect' ); echo '

'; echo '
'; } }