-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-rcp.php
142 lines (127 loc) · 3.4 KB
/
class-rcp.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
use WPGraphQL_RCP_Types;
final class WPGraphQL_RCP
{
/**
* Stores the instance of the WPGraphQL_RCP class
*
* @var WPGraphQL_RCP The one true WPGraphQL_RCP
* @since 0.0.1
* @access private
*/
private static $instance;
/**
* The instance of the WPGraphQL_RCP object
*
* @return object|WPGraphQL_RCP - The one true WPGraphQL_RCP
* @since 0.0.1
* @access public
*/
public static function instance()
{
if (!isset(self::$instance) && !(self::$instance instanceof WPGraphQL_RCP)) {
self::$instance = new WPGraphQL_RCP();
self::$instance->init();
}
/**
* Return the WPGraphQL_RCP Instance
*/
return self::$instance;
}
/**
* Throw error on object clone.
* The whole idea of the singleton design pattern is that there is a single object
* therefore, we don't want the object to be cloned.
*
* @since 0.0.1
* @access public
* @return void
*/
public function __clone()
{
// Cloning instances of the class is forbidden.
_doing_it_wrong(__FUNCTION__, esc_html__('The WPGraphQL_RCP class should not be cloned.', 'wpgraphql-restrict-content-pro'), '0.0.1');
}
/**
* Disable unserializing of the class.
*
* @since 0.0.1
* @access protected
* @return void
*/
public function __wakeup()
{
// De-serializing instances of the class is forbidden.
_doing_it_wrong(__FUNCTION__, esc_html__('De-serializing instances of the WPGraphQL_RCP class is not allowed', 'wpgraphql-restrict-content-pro'), '0.0.1');
}
/**
* Initialise plugin.
*
* @access private
* @since 0.0.1
* @return void
*/
private function init()
{
WPGraphQL_RCP_Types::register_enum();
$this->register_user_fields();
}
/**
* Register a post type as restricted via RCP
*
* @param string $type_name The name of the WP object type to register
* @param bool $allow_inactive Whether to allow inactive users to see the post
*/
public static function register_access_check(string $post_type_name, bool $allow_inactive)
{
add_filter('graphql_data_is_private', function ($is_private, $model_name, $data) use ($post_type_name, $allow_inactive) {
if ($model_name === 'PostObject') {
if ($data->post_type === $post_type_name) {
$user_id = get_current_user_id();
if (!$allow_inactive && !rcp_user_has_active_membership($user_id)) return true;
if (!rcp_user_can_access($user_id, $data->ID)) return true;
}
}
return $is_private;
}, 10, 3);
}
/**
* Register RCP User fields.
*
* @access public
* @since 0.0.1
* @return void
*/
private function register_user_fields()
{
// register_graphql_fields(
// 'User',
// []
// );
}
}
add_action('init', 'WPGraphQL_RCP_init');
if (!function_exists('WPGraphQL_RCP_init')) {
/**
* Function that instantiates the plugins main class
*
* @since 0.0.1
*/
function WPGraphQL_RCP_init()
{
/**
* Return an instance of the action
*/
return \WPGraphQL_RCP::instance();
}
}
/**
* Register a post type as restricted via RCP
*
* @param string $type_name The name of the WP object type to register
* @param bool $allow_inactive Whether to allow inactive users to see the post
*/
function register_graphql_post_type_restriction($type_name, $allow_inactive)
{
\WPGraphQL_RCP::register_access_check($type_name, $allow_inactive);
}