$settings Plugin settings. * @return array>|\WP_Error */ public static function fetch_properties( array $settings ) { if ( empty( $settings['connection_token'] ) ) { return new \WP_Error( 'aivisy_no_token', __( 'Not connected.', 'ai-view-sync' ) ); } $url = self::get_properties_api_url(); if ( '' === $url ) { return new \WP_Error( 'aivisy_no_url', __( 'AI View app URL is not configured.', 'ai-view-sync' ) ); } $endpoint = add_query_arg( array( 'token' => (string) $settings['connection_token'], 'site_url' => home_url( '/' ), '_' => (string) time(), ), $url ); $response = wp_remote_get( $endpoint, array( 'timeout' => 20, 'sslverify' => true, 'headers' => array( 'Accept' => 'application/json' ), ) ); if ( is_wp_error( $response ) ) { return $response; } $code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); if ( $code >= 400 || ! is_array( $data ) || empty( $data['success'] ) ) { $err = is_array( $data ) && isset( $data['error'] ) ? (string) $data['error'] : 'HTTP ' . (string) $code; return new \WP_Error( 'aivisy_properties', $err ); } if ( empty( $data['properties'] ) || ! is_array( $data['properties'] ) ) { return array(); } return $data['properties']; } /** * @param string $property_id GA4 property id. * @param array $settings Plugin settings. * @return true|\WP_Error */ public static function bind_property( $property_id, array $settings ) { $url = self::get_bind_api_url(); if ( '' === $url ) { return new \WP_Error( 'aivisy_no_bind_url', __( 'Bind API URL missing.', 'ai-view-sync' ) ); } $response = wp_remote_post( $url, array( 'timeout' => 20, 'sslverify' => true, 'body' => array( 'token' => (string) $settings['connection_token'], 'site_url' => home_url( '/' ), 'property_id' => (string) $property_id, ), ) ); if ( is_wp_error( $response ) ) { return $response; } $code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); if ( $code >= 400 || ! is_array( $data ) || empty( $data['success'] ) ) { $err = is_array( $data ) && isset( $data['error'] ) ? (string) $data['error'] : 'HTTP ' . (string) $code; return new \WP_Error( 'aivisy_bind', $err ); } return true; } }