slug = $slug; $this->postType = jvbCheckBase($slug); $this->registrar = $registrar; if ($this->registrar->hasFeature('hide_single')) { add_filter('is_post_type_viewable', [$this, 'hideFromPublic']); if ($this->registrar->hasFeature('redirect_to_author')) { add_filter('post_type_link', [$this, 'redirectSingleToAuthor'], 15, 2); add_action('template_redirect', [$this, 'actuallyRedirectToAuthor']); } else { add_filter('post_type_link', [$this, 'redirectSingleToArchive'], 15, 2); add_action('template_redirect', [$this, 'actuallyRedirectToArchive']); } } } /** * Use $this->hide_single * @param bool $is_viewable * @return bool */ public function hideFromPublic(bool $is_viewable):bool { if (!is_admin() && is_singular($this->postType)) { return false; } return $is_viewable; } /** * Use $this->redirect_to_author for this method * @param string $url * @param \WP_Post $post * @return string */ public function redirectSingleToAuthor(string $url, \WP_Post $post): string { if ($post->post_type !== $this->postType) { return $url; } // Redirect to author page or archive $user_link = jvbUserProfileLink($post->post_author); if ($user_link) { $query_var = str_replace(BASE, '', $post->post_type); return add_query_arg($query_var, $post->ID, get_permalink($user_link)); } return get_post_type_archive_link($post->post_type); } public function actuallyRedirectToAuthor():void { if (is_singular($this->postType)) { global $post; $url = $this->redirectSingleToAuthor('', $post); $url = add_query_arg($this->slug, $post->ID, $url); wp_redirect($url, 301); exit; } } /** * Use $this->hide_single * @param string $url * @param \WP_Post $post * @return string */ public function redirectSingleToArchive(string $url, \WP_Post $post): string { if ($post->post_type !== $this->postType) { return $url; } return get_post_type_archive_link($post->post_type).'#'.$post->post_name; } public function actuallyRedirectToArchive():void { if (is_singular($this->postType)) { global $post; $url = get_post_type_archive_link($this->postType).'#'.$post->post_name; wp_redirect($url, 301); exit; } } }