initial commit
[danixland-user-panel.git] / danixland-user-panel.php
1 <?php
2 /*
3 Plugin Name: danixland User Panel
4 Plugin URI: http://danixland.net/blog/2010/603-danixland-user-panel-plugin/
5 Description: A modified version of the meta widget which gives more info about the user.
6 Version: 1.4.2
7 Author: Danilo 'danix' Macri
8 Author URI: http://danixland.net
9 Text Domain: dnxup
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 */
15
16 /**************************************************************************************
17 Since Version 1.1.1 this plugin uses some code and inspiration from the sidebar-login
18 plugin by Mike Jolley < http://blue-anvil.com > < http://mikejolley.com >
19 big kudos to him!!!
20 **************************************************************************************/
21
22 /**
23 * Add plugin i18n domain: dnxup
24 * @since 1.1.1
25 */
26 load_plugin_textdomain('dnxup', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
27
28 /**
29 * Include settings options for our plugin
30 * @since 1.1.1
31 */
32 function dnxup_settings() {
33 add_options_page('danixland User Panel Settings', __('User Panel Options', 'dnxup'), 'manage_options', 'dnxup_options', 'dnxup_settings_display');
34 }
35 add_action('admin_menu', 'dnxup_settings' );
36
37 /**
38 * The function that outputs our admin page
39 * @since 1.1.1
40 */
41 function dnxup_settings_display() {
42 ?>
43 <div class="wrap">
44 <h2><?php _e('danixland User Panel Settings', 'dnxup') ?></h2>
45 <form method="post" action="options.php">
46 <?php
47 settings_fields('dnxup_options');
48 do_settings_sections('dnxup_settings_sections');
49 ?>
50 <p class="submit">
51 <input name="dnxup_options[submit]" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes', 'dnxup') ?>" />
52 <input name="dnxup_options[reset]" type="submit" class="button-secondary" value="<?php esc_attr_e('Reset Defaults', 'dnxup'); ?>" />
53 </p>
54 </form>
55 </div>
56 <?php
57 }
58
59 /**
60 * Settings API options initilization and validation
61 * @since 1.1.1
62 */
63 function dnxup_register_options() {
64 require( dirname( __FILE__ ) . '/include/options-register.php' );
65 }
66 add_action('admin_init', 'dnxup_register_options');
67
68 /**
69 * DataBase initilization and update procedure
70 * @since 1.1.2
71 */
72
73 // helper function that returns DB defaults
74 function dnxup_db_defaults() {
75 $defaults = array(
76 'show_gravatar' => true,
77 'logged_out_heading' => 'Howdy Guest',
78 'logged_in_heading' => 'Howdy %username%',
79 'logged_in_links' => "<a href=\"" . admin_url() . "\">" . __('Dashboard','dnxup') . "</a>\n<a href=\"" . admin_url('profile.php') . "\">" . __('Profile','dnxup') . "</a>",
80 // database version
81 'dnxup_DB_VERSION' => '1'
82 );
83 return $defaults;
84 }
85
86 // helper function that starts up the DB
87 function dnxup_db_init() {
88 global $dnxup_options;
89 $dnxup_options = get_option('dnxup_options');
90 if( false === $dnxup_options ) {
91 $dnxup_options = dnxup_db_defaults();
92 }
93 update_option('dnxup_options', $dnxup_options);
94 }
95
96 // helper function that performs a DB version update when needed
97 function dnxup_db_update($db_version) {
98 global $dnxup_options;
99 $db_defaults = dnxup_db_defaults();
100 $merge = wp_parse_args( $dnxup_options, $db_defaults );
101 // update DB version
102 $merge['dnxup_DB_VERSION'] = $db_version;
103 update_option('dnxup_options', $merge);
104 }
105
106 // helper function that performs a DB check and then an init/update action
107 function dnxup_db_check() {
108 global $dnxup_options;
109 if(false === $dnxup_options) {
110 dnxup_db_init();
111 }
112 $old_db_version = $dnxup_options['dnxup_DB_VERSION'];
113 $new_db_version = DNXUP_CURRENT_DB_VERSION;
114 if(empty($old_db_version)) {
115 dnxup_db_init();
116 }
117 if( intval($old_db_version) < intval($new_db_version) ) {
118 dnxup_db_update( $new_db_version );
119 }
120 }
121 function dnxup_set_db_version() {
122 // Define plugin database version. This should only change when new settings are added.
123 if ( ! defined( 'DNXUP_CURRENT_DB_VERSION' ) ) {
124 define( 'DNXUP_CURRENT_DB_VERSION', 2 );
125 }
126 }
127 /**
128 * Add function on plugin activation that'll set our DB version and perform a check for update
129 * @since 1.1.3
130 */
131 register_activation_hook( __FILE__, 'dnxup_set_db_version' );
132 register_activation_hook( __FILE__, 'dnxup_db_check' );
133
134
135 /**
136 * function that returns the actual url for redirection after logout
137 * as seen on sidebar-login by Mike Jolley with small modifications
138 * @since 1.1.3
139 */
140 function dnxup_current_url($url = '') {
141
142 global $wpdb, $post, $cat, $tag, $author, $year, $monthnum, $day, $wp_query;
143 $pageURL = "";
144
145 if ( is_home() && $wp_query->is_posts_page==1)
146 {
147 $pageURL = get_permalink(get_option('page_for_posts'));
148 }
149 elseif (is_home() || is_front_page())
150 {
151 $pageURL = get_bloginfo('url');
152 }
153 elseif (is_post_type_archive())
154 {
155 $pageURL = get_post_type_archive_link( get_query_var('post_type') );
156 }
157 elseif (is_single() || is_page())
158 {
159 $pageURL = get_permalink($wp_query->post->ID);
160 }
161 elseif (is_category())
162 {
163 $pageURL = get_category_link($cat);
164 }
165 elseif (is_tag())
166 {
167 $tag_id = $wpdb->get_var("SELECT ".$wpdb->terms.".term_id FROM $wpdb->term_taxonomy
168 LEFT JOIN $wpdb->terms
169 ON (".$wpdb->term_taxonomy.".term_id = ".$wpdb->terms.".term_id)
170 WHERE ".$wpdb->terms.".slug = '$tag'
171 AND ".$wpdb->term_taxonomy.".taxonomy = 'post_tag' LIMIT 1");
172 $pageURL = get_tag_link($tag_id);
173 }
174 elseif (is_author())
175 {
176 $pageURL = get_author_posts_url($author);
177 }
178 elseif (is_date())
179 {
180
181 if ($day)
182 {
183 $pageURL = get_day_link( $year, $monthnum, $day);
184 }
185 elseif ($monthnum)
186 {
187 $pageURL = get_month_link( $year, $monthnum, $day);
188 }
189 elseif ($year)
190 {
191 $pageURL = get_year_link( $year, $monthnum, $day);
192 }
193
194 }
195 elseif (is_search())
196 {
197 $pageURL = get_bloginfo('wpurl');
198 if ("/" != substr($pageURL, -1)) $pageURL = $pageURL . "/";
199 $pageURL .= '?s='.stripslashes(strip_tags($_REQUEST['s'])).'';
200 }
201
202 if (!$pageURL || $pageURL=="" || !is_string($pageURL)) {
203 $pageURL = "";
204 $pageURL = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
205
206 if ($_SERVER["SERVER_PORT"] != "80") {
207 $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
208 } else {
209 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
210 }
211
212 //————–added by mick
213 if (!strstr(get_bloginfo('url'),'www.')) $pageURL = str_replace('www.','', $pageURL );
214 //——————–
215 }
216 if ($pageURL && !is_search()) if ("/" != substr($pageURL, -1)) $pageURL = $pageURL . "/";
217
218
219 return $pageURL;
220 }
221
222 /**
223 * Add function to widgets_init that'll load our widget.
224 * @since 0.1
225 */
226 add_action( 'widgets_init', 'danixland_user_panel' );
227
228 /**
229 * Register our widget.
230 * 'User_Panel' is the widget class used below.
231 *
232 * @since 0.1
233 */
234 function danixland_user_panel() {
235 register_widget( 'User_Panel' );
236 }
237
238 /**
239 * User_Panel class.
240 * This class handles everything that needs to be handled with the widget:
241 * the settings, form, display, and update. Nice!
242 *
243 * @since 0.1
244 */
245 class User_Panel extends WP_Widget {
246
247 /**
248 * Widget setup.
249 */
250 function __construct() {
251 parent::__construct(
252 'dnx-user-panel', // id_base
253 __('danixland User Panel', 'dnxup' ), // Name
254 array( 'description' => __('An improved version of the meta widget', 'dnxup') )
255 );
256 }
257
258 /**
259 * How to display the widget on the public side of the site.
260 */
261 public function widget( $args, $instance ) {
262 extract( $args );
263 global $user_ID, $current_user;
264 $dnxup_options = get_option('dnxup_options');
265 extract( $dnxup_options );
266
267 // Let's use our title
268 if($user_ID != '') { // if user is logged in let's greet him good
269 $current_user = wp_get_current_user();
270 if( '' != $logged_in_heading ) {
271 $title = apply_filters('widget_title', str_replace('%username%',ucwords($current_user->display_name),$logged_in_heading));
272 } else {
273 $title = false;
274 }
275 } else { // user is not logged in, let's treat him as a guest
276 if( '' != $logged_out_heading ) {
277 $title = apply_filters('widget_title', str_replace('%username%',ucwords($current_user->display_name),$logged_out_heading));
278 } else {
279 $title = false;
280 }
281 }
282
283
284 /* Before widget (defined by themes). */
285 echo $before_widget;
286
287 /* Display the widget title if one was input (before and after defined by themes). */
288 if ( $title )
289 echo $before_title . $title . $after_title;
290
291 /**
292 * if user is logged in to your site
293 * @since 0.8
294 */
295 if( $user_ID != '' ) {
296 /* If show_gravatar was selected, show the User's Gravatar. */
297 if ( $show_gravatar ) {
298 echo get_avatar($user_ID, 75);
299 }
300 echo '<ul id="dnxup_login_links" class="user_logged_in">';
301 echo '<li id="dashboard"><a href="' . get_bloginfo('url') . '/wp-admin/">' . __('Dashboard', 'dnxup') . '</a></li>';
302 echo '<li id="profile"><a href="' . get_bloginfo('url') . '/wp-admin/profile.php">' . __('Profile and personal options', 'dnxup') . '</a></li>';
303
304 /**
305 * custom links functionality
306 * @since 1.1.4
307 */
308 $links = do_shortcode(trim($logged_in_links));
309 $links = explode("\n", $links);
310 if (sizeof($links)>0) {
311 foreach ($links as $l) {
312 $l = trim($l);
313 if (!empty($l)) {
314 $link = explode('|',$l);
315 if (isset($link[1])) {
316 $cap = strtolower(trim($link[1]));
317 if ($cap=='true') {
318 if (!current_user_can( 'manage_options' )) continue;
319 } else {
320 if (!current_user_can( $cap )) continue;
321 }
322 }
323 // Parse %USERNAME%
324 $link[0] = str_replace('%USERNAME%',$current_user->user_login,$link[0]);
325 $link[0] = str_replace('%username%',$current_user->user_login,$link[0]);
326 // Parse %USERID%
327 $link[0] = str_replace('%USERID%',$current_user->ID,$link[0]);
328 $link[0] = str_replace('%userid%',$current_user->ID,$link[0]);
329 echo '<li class="dnxup_custom_link">'.$link[0].'</li>';
330 }
331 }
332 }
333
334 echo('<li id="logout"><a href="' . wp_logout_url( dnxup_current_url() ) . '">' . __('Exit', 'dnxup') . '</a></li>');
335 echo '</ul>';
336 /**
337 * else if user is logged out and registration is disabled
338 * @since 0.8
339 */
340 } else {
341 if( ! get_option('users_can_register') ) {
342 echo '<ul id="dnxup_login_links" class="logged_out_noreg">';
343 echo '<li id="login"><a href="' . wp_login_url( dnxup_current_url() ) . '">' . __('Login', 'dnxup') . '</a></li>';
344 echo '<li id="lostpassword"><a href="' . wp_lostpassword_url( dnxup_current_url() ) . '">' . __('Forgot your password?', 'dnxup') . '</a></li>';
345 echo '</ul>';
346 /**
347 * or if user is still logged out but user registration is enabled
348 * @since 0.9
349 */
350 } else {
351 echo '<ul id="dnxup_login_links" class="logged_out_yesreg">';
352 echo '<li id="register"><a href="' . get_option('siteurl') . '/wp-register.php">' . __('Register', 'dnxup') . '</a></li>';
353 echo '<li id="login"><a href="' . wp_login_url( dnxup_current_url() ) . '">' . __('Login', 'dnxup') . '</a></li>';
354 echo '<li id="lostpassword"><a href="' . wp_lostpassword_url( dnxup_current_url() ) . '">' . __('Forgot your password?', 'dnxup') . '</a></li>';
355 echo '</ul>';
356 }
357 }
358 /* After widget (defined by themes). */
359 echo $after_widget;
360 }
361
362 /**
363 * Displays just a quick notice with a link to the Settings page
364 */
365 public function form( $instance ) {
366 ?>
367
368 <!-- No settings here, -->
369 <p>
370 <?php echo sprintf( __('Nothing to set here, already had a look at the <a href="%1$s">Settings Page</a>?', 'dnxup'),
371 admin_url('options-general.php?page=dnxup_options')
372 ); ?>
373 </p>
374 <?php
375 }
376 }
377
378 ?>