-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-disable-author-pages.php
210 lines (190 loc) · 8.33 KB
/
class-disable-author-pages.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
<?php
/*
Copyright 2014-2017 Frank Staude (email : frank@staude.net)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class disable_author_pages {
/**
* Constructor
*
* Register all actions and filters
*/
function __construct() {
add_action( 'template_redirect', array( 'disable_author_pages', 'disable_author_page' ) );
add_action( 'admin_init', array( 'disable_author_pages', 'register_settings' ) );
add_action( 'admin_menu', array( 'disable_author_pages', 'options_menu' ) );
add_action( 'plugins_loaded', array( 'disable_author_pages', 'load_translations' ) );
add_filter( 'author_link', array( 'disable_author_pages', 'disable_author_link') );
add_filter( 'plugin_action_links_disable-author-pages/disable-author-pages.php' , array( 'disable_author_pages', 'plugin_settings_link' ) );
}
/**
* Redirect the user
*
* This function is registerd to the template_redirect hook and checks
* to redirect the user to the selected page (or to the homepage)
*
*/
static public function disable_author_page() {
global $post;
$authorrequest = FALSE;
if ( is_404() && ( get_query_var( 'author' ) || get_query_var( 'author_name' ) ) ) {
if ( get_option( 'disable_author_pages_redirect_non_authors' ) == 1 ) {
$authorrequest = true;
}
}
if ( is_404() && ! ( get_query_var( 'author' ) || get_query_var( 'author_name' ) ) ) {
return;
}
if ( ( is_author() || $authorrequest ) && get_option( 'disable_author_pages_activate' ) == 1 ) {
$adminonly = get_option( 'disable_author_pages_adminonly', '0' );
$author_can = false;
if ( ! is_404() && $adminonly ) {
if( is_object( $post ) ) {
$author_can = author_can( get_the_ID(), 'administrator' );
}
}
if ( $adminonly && $author_can===true || !$adminonly && !is_404() || is_404() && ( get_option( 'disable_author_pages_redirect_non_authors' ) == 1 ) ) {
$status = get_option( 'disable_author_pages_status', '301' );
$url = get_option( 'disable_author_pages_destination', '' );
if ( $url == '' ) {
$url = home_url();
} else {
$url = get_permalink( $url );
}
wp_redirect( $url, $status );
exit;
}
}
}
/**
* Register all settings
*
* Register all the settings, the plugin uses.
*/
static public function register_settings() {
register_setting( 'disable_author_pages_settings', 'disable_author_pages_activate' );
register_setting( 'disable_author_pages_settings', 'disable_author_pages_destination' );
register_setting( 'disable_author_pages_settings', 'disable_author_pages_status' );
register_setting( 'disable_author_pages_settings', 'disable_author_pages_authorlink' );
register_setting( 'disable_author_pages_settings', 'disable_author_pages_adminonly' );
register_setting( 'disable_author_pages_settings', 'disable_author_pages_redirect_non_authors' );
}
/**
* Overwrite the author url with an empty string
*
* @param string $content url to author page
* @return string
*/
static public function disable_author_link( $content ) {
if ( get_option( 'disable_author_pages_authorlink', '0' ) == 1 ) {
return "";
} else {
return $content;
}
}
/**
* load the plugin textdomain
*
* load the plugin textdomain with translations for the backend settingspage
*/
static public function load_translations() {
load_plugin_textdomain( 'disable-author-pages', false, apply_filters ( 'disable_author_pages_translationpath', dirname( plugin_basename( __FILE__ )) . '/languages/' ) );
}
/**
* Generate the options menu page
*
* Generate the options page under the options menu
*/
static public function options_menu() {
add_options_page( 'Disable Author Pages', __('Author Pages','disable-author-pages', 'hinweis'), 'manage_options',
__FILE__, array( 'disable_author_pages', 'create_options_disable_author_menu' ) );
}
/**
* Generate the options page for the plugin
*
* @global type $settings
*/
static public function create_options_disable_author_menu() {
global $settings;
$selectedpage = get_option( 'disable_author_pages_destination' );
?>
<div class="wrap" id="disableauthorpages">
<h2><?php _e( 'Disable Author settings', 'disable-author-pages' ); ?></h2>
<p><?php _e( 'Settings to disable the author pages.', 'disable-author-pages' ); ?></p>
<form method="POST" action="options.php">
<?php
settings_fields( 'disable_author_pages_settings' );
echo '<table class="form-table">';
?>
<tr>
<td style="width: 13px;"><input type="checkbox" name="disable_author_pages_activate" value="1" <?php if ( get_option( 'disable_author_pages_activate' ) ) echo " checked "; ?> /></td>
<td><?php _e( 'Disable Author Pages', 'disable-author-pages' ); ?></td>
</tr>
<tr>
<td></td>
<td>
<select name="disable_author_pages_status">
<option value="301" <?php if ( get_option( 'disable_author_pages_status' ) == '301' ) { echo ' selected '; } ?> ><?php _e( '301 (Moved Permanently)', 'disable-author-pages' );?></option>
<option value="307" <?php if ( get_option( 'disable_author_pages_status' ) == '307' ) { echo ' selected '; } ?> ><?php _e( '307 (Temporary Redirect)', 'disable-author-pages' );?></option>
</select> <?php _e( 'HTTP Status', 'disable-author-pages' );?>
</td>
</tr>
<tr>
<td></td>
<td>
<?php
echo wp_dropdown_pages("name=disable_author_pages_destination&selected={$selectedpage}&echo=0");
_e( 'Destinationpage', 'disable-author-pages' ); ?>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="checkbox" name="disable_author_pages_authorlink" value="1" <?php if ( get_option( 'disable_author_pages_authorlink' ) ) echo " checked "; ?> />
<?php _e( 'Disable Authorlink', 'disable-author-pages' ); ?>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="checkbox" name="disable_author_pages_redirect_non_authors" value="1" <?php if ( get_option( 'disable_author_pages_redirect_non_authors' ) ) echo " checked "; ?> />
<?php _e( 'Redirect non exists author pages', 'disable-author-pages' ); ?>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="checkbox" name="disable_author_pages_adminonly" value="1" <?php if ( get_option( 'disable_author_pages_adminonly' ) ) echo " checked "; ?> />
<?php _e( 'Disable for admin author pages only', 'disable-author-pages' ); ?>
</td>
</tr>
</table>
<br/>
<input type="submit" class="button-primary" value="<?php _e('Save Changes', 'disable-author-pages' )?>" />
</form>
</div>
<?php
}
/**
* add link on plugin page
*
* @param $links
* @return mixed
* @since 0.10
*/
static public function plugin_settings_link( $links ) {
$settings_link = '<a href="options-general.php?page=disable-author-pages%2Fclass-disable-author-pages.php">' . __('Settings', 'disable-author-pages') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
}