Jake Vanderwerf
10 days ago 97e7c319d656a5f05489ca996e249e7359303d4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace JVBase\inc\registrar\helpers;
 
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
use WP_Post;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class HideSingle {
    protected string $slug;
    protected string $postType;
    protected Registrar $registrar;
    public function __construct(string $slug, Registrar $registrar) {
        $this->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;
        }
    }
}