tester = $tester; $this->queue = $queue; $this->log = $log; $this->pages = $pages; } /** * Register hooks. * * Only the logged-in variant is registered. There is no `nopriv` counterpart on purpose: none of * these actions has any meaning for a visitor, and registering one would publish a way to make the * site spend an outbound request per hit. * * @return void */ public function boot(): void { add_action( 'wp_ajax_adpix_analytics_test', array( $this, 'test' ) ); add_action( 'wp_ajax_adpix_analytics_purge', array( $this, 'purge' ) ); add_action( 'wp_ajax_adpix_analytics_clear_log', array( $this, 'clear_log' ) ); add_action( 'wp_ajax_adpix_analytics_find_pages', array( $this, 'find_pages' ) ); } /** * Search content by title, for the excluded-pages picker. * * @return void */ public function find_pages(): void { $this->authorize(); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Checked in authorize(). $search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : ''; wp_send_json_success( array( 'pages' => $this->pages->search( $search ) ) ); } /** * Verify the stored credentials against the collector. * * @return void */ public function test(): void { $this->authorize(); $result = $this->tester->test(); $payload = array( 'ok' => (bool) $result['ok'], 'message' => (string) $result['message'], ); // The property name is the useful confirmation: it tells the site owner they connected the // property they meant to, which a bare "connected" never can. The key itself is not echoed // back — it went out with the request, it does not need to come home. if ( ! empty( $result['property']['name'] ) ) { $payload['property'] = (string) $result['property']['name']; } if ( ! empty( $result['hmac_required'] ) ) { $payload['notice'] = __( 'This property requires request signing. Enable "Sign requests" below.', 'adpix-analytics' ); } if ( $payload['ok'] ) { wp_send_json_success( $payload ); } wp_send_json_error( $payload ); } /** * Discard everything waiting to be delivered. * * @return void */ public function purge(): void { $this->authorize(); $removed = $this->queue->purge(); wp_send_json_success( array( 'removed' => $removed, 'message' => sprintf( /* translators: %s: number of discarded events. */ _n( '%s pending event was discarded.', '%s pending events were discarded.', $removed, 'adpix-analytics' ), number_format_i18n( $removed ) ), ) ); } /** * Empty the recent-failures list. * * @return void */ public function clear_log(): void { $this->authorize(); $this->log->clear(); wp_send_json_success( array( 'message' => __( 'The log was cleared.', 'adpix-analytics' ) ) ); } /** * Refuse the request unless it came from our form and from someone entitled to make it. * * @return void */ private function authorize(): void { check_ajax_referer( self::NONCE, 'nonce' ); if ( ! current_user_can( SettingsController::CAPABILITY ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to do that.', 'adpix-analytics' ) ), 403 ); } } }