-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmntcs-private-site.php
217 lines (198 loc) · 6.06 KB
/
smntcs-private-site.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Plugin Name: SMNTCS Private Site
* Plugin URI: https://github.com/nielslange/smntcs-private-site
* Description: Allow only logged in users to access the site.
* Author: Niels Lange
* Author URI: https://nielslange.de
* Text Domain: smntcs-private-site
* Version: 1.8
* Requires at least: 3.4
* Requires PHP: 5.6
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package SMNTCS_Private_Site
*/
defined( 'ABSPATH' ) || exit;
/**
* Add settings link on plugin page
*
* @param array $links The original array with customizer links.
* @return array $links The updated array with customizer links.
*/
function smntcs_ps_plugin_settings_link( $links ) {
$admin_url = admin_url( 'customize.php?autofocus[control]=smntcs_ps_enable' );
$settings_url = sprintf( '<a href="%s">%s</a>', $admin_url, __( 'Settings', 'smntcs-private-site' ) );
array_unshift( $links, $settings_url );
return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'smntcs_ps_plugin_settings_link' );
/**
* Enhance customizer
*
* @param WP_Customize_Manager $wp_customize The instance of the WP_Customize_Manager class.
*/
function smntcs_ps_register_customize( $wp_customize ) {
$wp_customize->add_section(
'smntcs_ps_section',
array(
'priority' => 500,
'title' => __( 'Private Site', 'smntcs-private-site' ),
)
);
$wp_customize->add_setting(
'smntcs_ps_enable',
array(
'capability' => 'edit_theme_options',
'default' => false,
'type' => 'option',
)
);
$wp_customize->add_control(
'smntcs_ps_enable',
array(
'label' => __( 'Enable Private Site', 'smntcs-private-site' ),
'section' => 'smntcs_ps_section',
'type' => 'checkbox',
)
);
$wp_customize->add_setting(
'smntcs_ps_login_button',
array(
'capability' => 'edit_theme_options',
'default' => false,
'type' => 'option',
)
);
$wp_customize->add_control(
'smntcs_ps_login_button',
array(
'label' => __( 'Show login button', 'smntcs-private-site' ),
'section' => 'smntcs_ps_section',
'type' => 'checkbox',
)
);
$wp_customize->add_setting(
'smntcs_ps_message',
array(
'capability' => 'edit_theme_options',
'default' => __( 'This site is marked private by its owner.', 'smntcs-private-site' ),
'sanitize_callback' => 'sanitize_text_field',
'type' => 'option',
)
);
$wp_customize->add_control(
'smntcs_ps_message',
array(
'label' => __( 'Message', 'smntcs-private-site' ),
'section' => 'smntcs_ps_section',
'type' => 'textarea',
'input_attrs' => array(
'placeholder' => __( 'This site is marked private by its owner.', 'smntcs-private-site' ),
),
)
);
$wp_customize->add_setting(
'smntcs_ps_message_color',
array(
'default' => '#2e4453',
'type' => 'option',
)
);
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
'smntcs_ps_message_color',
array(
'label' => __( 'Message Color', 'smntcs-private-site' ),
'section' => 'smntcs_ps_section',
'settings' => 'smntcs_ps_message_color',
)
)
);
$wp_customize->add_setting(
'smntcs_ps_background',
array(
'capability' => 'edit_theme_options',
'default' => null,
'type' => 'option',
)
);
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
'smntcs_ps_background',
array(
'label' => __( 'Background Image', 'smntcs-private-site' ),
'section' => 'smntcs_ps_section',
'settings' => 'smntcs_ps_background',
)
)
);
}
add_action( 'customize_register', 'smntcs_ps_register_customize' );
/**
* Load custom template
*
* @param array $page_template The original string with page template.
* @return array $page_template The updated string with page template.
*/
function smntcs_ps_page_template( $page_template ) {
if ( esc_attr( get_option( 'smntcs_ps_enable' ) ) && ! is_user_logged_in() ) {
$page_template = dirname( __FILE__ ) . '/template/private-site.php';
}
return $page_template;
}
add_filter( '404_template', 'smntcs_ps_page_template' );
add_filter( 'archive_template', 'smntcs_ps_page_template' );
add_filter( 'attachment_template', 'smntcs_ps_page_template' );
add_filter( 'author_template', 'smntcs_ps_page_template' );
add_filter( 'category_template', 'smntcs_ps_page_template' );
add_filter( 'date_template', 'smntcs_ps_page_template' );
add_filter( 'frontpage_template', 'smntcs_ps_page_template' );
add_filter( 'embed_template', 'smntcs_ps_page_template' );
add_filter( 'frontpage_template', 'smntcs_ps_page_template' );
add_filter( 'home_template', 'smntcs_ps_page_template' );
add_filter( 'index_template', 'smntcs_ps_page_template' );
add_filter( 'page_template', 'smntcs_ps_page_template' );
add_filter( 'paged_template', 'smntcs_ps_page_template' );
add_filter( 'privacypolicy_template', 'smntcs_ps_page_template' );
add_filter( 'search_template', 'smntcs_ps_page_template' );
add_filter( 'single_template', 'smntcs_ps_page_template' );
add_filter( 'singular_template', 'smntcs_ps_page_template' );
add_filter( 'tag_template', 'smntcs_ps_page_template' );
add_filter( 'taxonomy_template', 'smntcs_ps_page_template' );
/**
* Load custom CSS.
*
* @return void
*/
function smntcs_ps_custom_css() {
if ( esc_attr( get_option( 'smntcs_ps_enable' ) ) && ! is_user_logged_in() ) {
?>
<style>
body {
background: url(<?php print( esc_html( get_option( 'smntcs_ps_background' ) ) ); ?>) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
color: <?php print( esc_html( get_option( 'smntcs_ps_message_color', '#2e4453' ) ) ); ?> !important;
display: flex;
align-items: center;
justify-content: center;
}
#message {
text-align: center;
width: 200px;
}
p {
font-weight: 600;
margin-bottom: 1em;
}
</style>
<?php
}
}
add_action( 'wp_head', 'smntcs_ps_custom_css' );