getCurrentPostId(); return [ 'post_id' => $postId, 'categories' => $this->getPostCategories($postId), 'url' => $this->getCurrentUrl(), 'request_uri' => $this->requestProvider->getRequestUri(), 'post_type' => $this->getCurrentPostType($postId), ]; } private function getCurrentPostId(): int { if (is_singular()) { return get_the_ID() ?: 0; } return 0; } /** * @return array */ private function getPostCategories(int $postId): array { if ($postId === 0) { return []; } $categories = get_the_category($postId); if (empty($categories)) { return []; } return array_map(function (\WP_Term $term): array { return [ 'term_id' => $term->term_id, 'slug' => $term->slug, 'name' => $term->name, ]; }, $categories); } private function getCurrentUrl(): string { global $wp; if (isset($wp->request)) { return home_url($wp->request); } return home_url(add_query_arg([], false)); } private function getCurrentPostType(int $postId): string { if ($postId === 0) { return ''; } return get_post_type($postId) ?: ''; } }