-
Notifications
You must be signed in to change notification settings - Fork 0
/
members-only.php
189 lines (164 loc) · 7.12 KB
/
members-only.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/*
Plugin Name: Members Only Posts
Plugin URI: https://github.com/davidfcarr/members-only-posts
Description: Create posts that will only be displayed (and listed in search) for logged in members. Just add the special category Members Only to any post not to be shared publicly. Includes Members Only sidebar widget and Site News widget (all posts not in Members Only category)
Author: David F. Carr
Tags: Members Only, Private Posts
Author URI: http://www.carrcommunications.com
Text Domain: members-only
Version: 1.0
*/
include 'members-only-widgets.php';
add_filter('the_content','carrcomm_member_only_content');
add_filter('the_excerpt','carrcomm_member_only_excerpt');
add_filter('get_the_excerpt','carrcomm_member_only_excerpt');
add_filter('jetpack_seo_meta_tags', 'carrcomm_members_only_jetpack');
add_filter('bp_get_activity_content_body','carrcomm_members_only_bp');
add_shortcode('carrcomm_members_only','carrcomm_members_only');
add_shortcode('carrcomm_site_news','carrcomm_site_news');
add_action( 'pre_get_posts', 'carrcomm_members_only_modify_query' );
add_action('admin_init','carrcomm_members_only_init');
function carrcomm_members_only_init() {
$members_category = get_category_by_slug('members-only');
if(empty($members_category))
wp_create_category('Members Only');
}
function carrcomm_is_club_member() {
return apply_filters('carrcomm_is_club_member',is_user_member_of_blog());
}
function carrcomm_member_only_content($content) {
if( !in_category('members-only') && !has_term('members-only','rsvpmaker-type') )
return $content;
if(!carrcomm_is_club_member() )
return '<div style="width: 100%; background-color: #ddd;">'.__('You must be logged in and a member of this blog to view this content','members-only').'</div>'. sprintf('<div id="member_only_login"><a href="%s">'.__('Login to View','members-only').'</a></div>',site_url('/wp-login.php?redirect_to='.urlencode(get_permalink()) ) );
else
return $content.'<div style="width: 100%; background-color: #ddd;">'.__('Note: This is member-only content (login required)','members-only').'</div>';
}
function carrcomm_members_only_jetpack ($tag_array) {
if( !in_category('members-only') && !has_term('members-only','rsvpmaker-type') )
return $tag_array;
$tag_array['description'] = __('Members only content','members-only');
return $tag_array;
}
function carrcomm_member_only_excerpt($excerpt) {
if( !in_category('members-only') && !has_term('members-only','rsvpmaker-type') )
return $excerpt;
if(!carrcomm_is_club_member() )
return __('You must be logged in and a member of this blog to view this content','members-only');
else
return $excerpt;
}
function carrcomm_members_only_bp($args) {
global $activities_template;
if($activities_template->activity->secondary_item_id)
{
$cat = wp_get_post_categories($activities_template->activity->secondary_item_id);
foreach($cat as $cat_id)
{
$category = get_category($cat_id);
if($category->slug == 'members-only')
return 'Members-only content (login required)';
}
}
return $args;
}
add_action( 'pre_get_posts', 'carrcomm_members_only_modify_query' );
function carrcomm_members_only_modify_query( $query ) {
if((get_post_type() == 'rsvpmaker') && ! carrcomm_is_club_member() )
{
$query->set('tax_query',array(array('taxonomy' => 'rsvpmaker-type',
'field' => 'slug',
'terms' => 'members-only',
'operator' => 'NOT IN')));
}
elseif ( ! is_admin() && $query->is_main_query() && ! carrcomm_is_club_member() )
{
$category = get_category_by_slug('members-only');
if($category)
$query->set( 'cat', '-'.$category->term_id );
}
}
function carrcomm_members_only($args) {
ob_start();
$show_date = (!empty($args["show_date"])) ? 1 : 0;
$show_excerpt = (!empty($args["show_excerpt"])) ? 1 : 0;
$show_thumbnail = (!empty($args["show_thumbnail"])) ? 1 : 0;
$number = (!empty($args["posts_per_page"])) ? $args["posts_per_page"] : 10;
$title = (isset($args["title"]) ) ? $args["title"] : 'Members Only';
if(!empty($title))
echo '<h2 class="club_news">'.$title."</h2>\n";
$qargs = array(
'posts_per_page' => $number,
'category_name' => 'members-only',
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true);
$r = new WP_Query( apply_filters( 'widget_posts_args', $qargs ) );
if ($r->have_posts()) :
?>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<h3>
<a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</h3>
<?php
if ( $show_thumbnail && has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<?php endif;
if ( $show_excerpt ) : ?>
<div class="post-excerpt"><?php the_excerpt(); ?></div>
<?php endif; ?>
<?php endwhile; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
return ob_get_clean();
}
function carrcomm_site_news($args) {
ob_start();
$show_date = (!empty($args["show_date"])) ? 1 : 0;
$show_excerpt = (!empty($args["show_excerpt"])) ? 1 : 0;
$show_thumbnail = (!empty($args["show_thumbnail"])) ? 1 : 0;
$number = (!empty($args["posts_per_page"])) ? $args["posts_per_page"] : 10;
$title = (isset($args["title"]) ) ? $args["title"] : 'Members Only';
if(!empty($title))
echo '<h2 class="club_news">'.$title."</h2>\n";
$category = get_category_by_slug('members-only');
$qargs = array(
'posts_per_page' => $number,
'cat' => '-'.$category->term_id,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true);
$r = new WP_Query( apply_filters( 'widget_posts_args', $qargs ) );
if ($r->have_posts()) :
?>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<h3>
<a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</h3>
<?php
if ( $show_thumbnail && has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<?php endif;
if ( $show_excerpt ) : ?>
<div class="post-excerpt"><?php the_excerpt(); ?></div>
<?php endif; ?>
<?php endwhile; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
return ob_get_clean();
}