[ 'Content-Type' => 'application/json; charset=utf-8', self::TOKEN_HEADER => $token, ], // No site URL here, so "Anonymize" is not undone by the header. 'user-agent' => 'WordPress/' . get_bloginfo( 'version' ), 'body' => wp_json_encode( $payload ), ] ) ); if ( is_wp_error( $response ) ) { return; } $code = (int) wp_remote_retrieve_response_code( $response ); if ( $code < 200 || $code >= 300 ) { return; } } /** * Shop REST usage endpoint. * * @return string */ private static function endpoint(): string { return License_Shop_Client::shop_base_url() . '/wp-json/advanced-ads/v1/usage'; } /** * Build anonymous payload. * * @param array $prefs Saved preferences. * @return array */ private static function build_payload( array $prefs ): array { $anonymize = ! empty( $prefs['anonymize'] ); // Stable per site, but cannot be turned back into the domain. $site = $anonymize ? hash( 'sha256', home_url( '/' ) . wp_salt() ) : home_url( '/' ); $ads = wp_count_posts( Constants::POST_TYPE_AD ); $plc = wp_count_posts( Constants::POST_TYPE_PLACEMENT ); $payload = [ 'site' => $site, 'anonymize' => $anonymize, 'dataFreq' => ( $prefs['dataFreq'] ?? 'monthly' ) === 'weekly' ? 'weekly' : 'monthly', 'pluginVersion' => defined( 'ADVADS_VERSION' ) ? ADVADS_VERSION : '', 'counts' => [ 'ads' => isset( $ads->publish ) ? (int) $ads->publish : 0, 'placements' => isset( $plc->publish ) ? (int) $plc->publish : 0, 'groups' => (int) wp_count_terms( [ 'taxonomy' => Constants::TAXONOMY_GROUP, 'hide_empty' => false, ] ), ], ]; if ( ! empty( $prefs['inclEnv'] ) ) { $payload['env'] = [ 'wp' => get_bloginfo( 'version' ), 'php' => PHP_VERSION, ]; } if ( ! empty( $prefs['inclPlugins'] ) ) { $payload['plugins'] = array_values( (array) get_option( 'active_plugins', [] ) ); $payload['theme'] = get_stylesheet(); } /** * Filter usage-data payload before send. * * @param array $payload Payload. * @param array $prefs Preferences. */ return (array) apply_filters( 'advanced-ads-data-usage-payload', $payload, $prefs ); } }