Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (102 sloc) 3.764 kb
72ae897 @z720 Initial Code for testing
authored
1 <?php
2 /*
3 Plugin Name: Author Default Category
4 Plugin URI: http://github.com/z720/wp-adc
5 Description: This plugins allow any author to select its own default category.
6 Author: Sebastien Erard
005ea3d @z720 Update version to beta
authored
7 Version: 1.0-beta
72ae897 @z720 Initial Code for testing
authored
8 Author URI: http://z720.net
9 */
10
11 class AuthorDefaultCategory {
12
13 private $opt_name = 'default-category';
14
15 private function isUserContext() {
16 global $current_screen;
17 //die(print_r((WP_ADMIN && !($current_screen->id == 'options-writing')) == false, true));
18 // User logged in and can edit posts (Minimum)
19 $ret = (get_current_user_id() != 0 && user_can( get_current_user_id(), 'edit_posts'))
20 // Override in admin except options-writing to allow global default category to be edited
21 && ( (WP_ADMIN && !($current_screen->id == 'options-writing') )
22 // Override in XmlRPC request
23 || defined(XMLRPC_REQUEST) );
24 return $ret;
25 }
26
27 private function isGlobalContext() {
28 return $this->isUserContext();
29 }
30
31 /**
32 * get Option default category override
33 */
34 public function user_default_category($original_value){
35 // Override in the context of a user action
36 if($this->isUserContext()) {
37 $user_default_category = get_user_option($this->opt_name,get_current_user_id());
38 if($user_default_category) {
39 return $user_default_category;
40 }
41 }
42 return $original_value;
43 }
44
45 /**
46 * Constructor : init WordPress hooks
47 * @since 1.0
48 */
49 public function __construct() {
50 add_action('personal_options', array(&$this, 'show_personal_options'));
51 // profile save own or for admin
52 add_action( 'personal_options_update', array( &$this, 'save_personnal_options') );
53 add_action( 'edit_user_profile_update', array( &$this, 'save_personnal_options') );
54
55 // option override fro xmlrpc and post insert
56 add_filter('option_default_category', array(&$this, 'user_default_category'));
57 }
58
59 /**
60 * Save the option
61 */
62 public function save_personnal_options($user_id) {
63 // Save only if current user can and edited user can have a default category
64 if ( !current_user_can( 'edit_user', $user_id ) || !user_can( $user_id, 'edit_posts') ) return false;
65 // Save the option (blog dependent)
66 $cat = get_category($_POST[$this->opt_name]);
67 //die(print_r($cat,true));
68 if( !is_wp_error( $cat ) ) {
69 update_user_option( $user_id, $this->opt_name, $cat->term_id, false );
70 }
71 }
72
73 /**
74 * Show the default category field in the user profile
75 */
76 public function show_personal_options($user) {
77 global $current_screen;
78 if ( !user_can( $user->ID, 'edit_posts') ) return false;
79 // Args for standard wp category dropdown categories select
80 $args = array(
81 'show_option_all' => '',
82 'show_option_none' => '',
83 'orderby' => 'ID',
84 'order' => 'ASC',
85 'show_last_update' => 0,
86 'show_count' => 0,
87 'hide_empty' => 0,
88 'child_of' => 0,
89 'exclude' => '',
90 'echo' => 1,
91 'selected' => get_user_option($this->opt_name,$user->ID),
92 'hierarchical' => 0,
93 'name' => $this->opt_name,
94 'id' => $this->opt_name,
95 'class' => 'postform',
96 'depth' => 0,
97 'tab_index' => 0,
98 'taxonomy' => 'category',
99 'hide_if_empty' => false );
100 // Template for a single field ?>
101 <tr class="default-category">
102 <th scope="row"><?php _e('Default Category')?></th>
103 <td><fieldset><legend class="screen-reader-text"><span><?php _e('Default Category') ?></span></legend>
104 <label for="default-category">
105 <?php wp_dropdown_categories($args); /* translators: Show admin bar when viewing site / _e( 'when viewing site' );*/ ?></label></fieldset>
106 </td>
107 </tr><?php
108 }
109
110 }
111
112 // anonymous instance
113 new AuthorDefaultCategory();
Something went wrong with that request. Please try again.