This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
235 lines (178 loc) · 5.7 KB
/
index.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/*
Plugin Name: WP Default Author
Plugin URI: http://wordpress.org/extend/plugins/wp-default-author/
Description: Default author settings (defined both globally and per user)
Version: 1.0.6
Author: birgire
Author URI: http://profiles.wordpress.org/birgire
License: GPLv2
*/
/**
* No direct access
*/
defined( 'ABSPATH' ) or die( 'Nothing here!' );
/**
* Init the Default_Author class
*/
if( ! class_exists( 'WP_Default_Author' ) ):
/**
* Call the class
*/
function call_WP_Default_Author() {
// only activate if the current user is an Editor
if( current_user_can( 'edit_others_posts' ) )
return new WP_Default_Author();
}
// we only need it for the backend
if ( is_admin() )
add_action( 'init', 'call_WP_Default_Author' );
/**
* Class Default_Author
*/
class WP_Default_Author{
protected $plugin_domain = 'wpdeau';
protected $default_author_option_name = 'wpdeau_default_author';
protected $default_global_author_option_name = 'wpdeau_global_default_author';
/**
* Class init
*/
public function __construct(){
add_action( 'admin_init', array( &$this, 'init' ) );
add_action( 'show_user_profile', array( &$this, 'show_user_settings' ) );
add_action( 'edit_user_profile', array( &$this, 'show_user_settings' ) );
add_action( 'personal_options_update', array( &$this, 'save_user_settings' ) );
add_action( 'edit_user_profile_update', array( &$this, 'save_user_settings' ) );
add_filter( 'wp_insert_post_data', array( &$this, 'wp_insert_post_data_callback' ), '99', 2 );
}
/**
* Plugin init
* @since 1.0.3
* @return void
*/
public function init(){
$plugin_dir = basename( dirname( __FILE__ ) );
load_plugin_textdomain( $this->plugin_domain, FALSE, $plugin_dir . '/languages/' );
register_setting(
'writing',
$this->default_global_author_option_name,
array( $this, 'validate_global_settings' )
);
add_settings_field(
$this->default_global_author_option_name,
sprintf( '<label for="%s">%s</label>' , $this->default_global_author_option_name, __( 'Global default author' , $this->plugin_domain ) ),
array( &$this, 'show_global_settings_field' ),
'writing'
);
}
/**
* Modify the post author, just before the insert new post
* @since 1.0.1
* @return array $data Post data to save
*/
public function wp_insert_post_data_callback( $data , $pa ) {
$current_user = wp_get_current_user();
if( 'auto-draft' !== $data['post_status'] ):
return $data;
endif;
if( !post_type_supports($data['post_type'], 'author') ):
return $data;
endif;
if( '0000-00-00 00:00:00' !== $data['post_date_gmt'] ):
return $data;
endif;
// Check first if global author is set,
// if not then check if the current user has one
// else use the current author as the post author
$default_global_author = get_option( $this->default_global_author_option_name , TRUE );
$default_author = get_the_author_meta( $this->default_author_option_name , $current_user->ID );
if( $default_author ){
$data['post_author'] = $default_author;
}elseif( $default_global_author ){
$data['post_author'] = $default_global_author;
}
return $data;
}
/**
* Validate global settings field from the Options/Writing Settings page
* @since 1.0.3
* @param string $input
* @return string $output Sanitized input
*/
public function validate_global_settings( $input ) {
$output = esc_attr( $input );
return $output;
}
/**
* Show the global default author settings on the Options/Writing Settings page
* @since 1.0.3
* @return void
*/
public function show_global_settings_field() {
$selected_global_author = get_option( $this->default_global_author_option_name, TRUE );
$args = array(
'name' => $this->default_global_author_option_name,
'selected' => $selected_global_author,
'show_option_all' => ' ',
);
wp_dropdown_users( $args );
?>
<span class="description">
<?php _e( 'This will leave the default user defined per user intact', $this->plugin_domain ); ?>
</span>
<?php
}
/**
* Save default author settings on the Profile Settings page
* @since 1.0.1
* @param integer $user_id
* @return void
*/
public function save_user_settings( $user_id ) {
// only allow editors to save
if ( !current_user_can( 'edit_others_posts' ) )
return FALSE;
// check if the input is set
$value = 0;
if( isset( $_POST[$this->default_author_option_name] ) )
$value = $_POST[$this->default_author_option_name];
// update user meta
if( $value > 0 )
update_usermeta( $user_id, $this->default_author_option_name, $value );
}
/**
* Show the default author settings on the Profile Settings page
* @since 1.0.1
* @param object $user WP_User Object
* @return void
*/
public function show_user_settings( $user ) {
?>
<h3>
<?php _e( 'Default Author Settings', $this->plugin_domain ); ?>
</h3>
<table class="form-table">
<tr>
<th>
<label for="<?php echo $this->default_author_option_name; ?>"><?php _e( 'Default author', $this->plugin_domain ); ?></label>
</th>
<td>
<?php
$selected_author = get_the_author_meta( $this->default_author_option_name, $user->ID );
$args = array(
'name' => $this->default_author_option_name,
'selected' => $selected_author,
'show_option_all' => ' ',
);
wp_dropdown_users( $args );
?>
<span class="description">
<?php _e( 'Select the default author, this will overwrite the global settings.', $this->plugin_domain ); ?>
</span>
</td>
</tr>
</table>
<?php
}
} // end class
endif; //end if class_exists