-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
78 lines (66 loc) · 2.68 KB
/
functions.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
<?php
function filter_posts($posts) {
// Should contain all the posts fetched from the DB.
// Are we in admin area?
if (is_admin()) return $posts;
// If the user isn't logged in, then they will get the same slugs as registered users with no Auth0 permissions.
if (!is_user_logged_in()) {
$Auth0Data= "";
} else {
// Get the Auth0 Details.
$Auth0Data = json_decode(get_user_meta(get_current_user_id(), "wp_auth0_obj", true));
}
// What category slug names do we let any logged in user view?
$allowedCategories= array("free");
// Will contain the posts that we let WordPress then render/consume.
$allowedPosts= array();
// if they don't have Auth0 details, then no access?
if ($Auth0Data== "")
{
// Do we want to do anything here?
} else if (!isset($Auth0Data->blogCategories)) {
// The user has Auth0 data, but no Blog Categories defined... want to do anything?
} else {
// grab the WordPress category slugs defined in the Auth0 user and add them to the array.
$allowedCategories= array_merge($allowedCategories, $Auth0Data->blogCategories);
}
// Run through each post in the array and see if the post has any categories that are allowed against this user.
for ($loopPosts= 0; $loopPosts< count($posts); $loopPosts++) {
$postAllowed= false;
// We want to limit this to just post_type's of post
if ($posts[$loopPosts]->post_type== "post") {
$post_categories= wp_get_post_categories($posts[$loopPosts]->ID);
foreach ($post_categories as $post_category) {
$category= get_category($post_category);
// Compare the slug with the roles we get back from Auth0!!!!!!
if (in_array($category->slug, $allowedCategories))
{
$postAllowed= true;
}
}
} else {
// Post type isn't post, so allow...
$postAllowed= true;
}
if ($postAllowed) {
// Could consider just giving back the extracts when the post is denied?g
array_push($allowedPosts, $posts[$loopPosts]);
}
}
// Send back the list of posts...
return $allowedPosts;
} // End of filter_posts
// Add our function as a hook, every time WordPress queries a list of posts.
add_action('the_posts', 'filter_posts' );
// I created this work within a child theme's functions.php .
// The function below is to make sure that the child theme inherits properly...
function my_theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>