-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautobookmarks.php
89 lines (68 loc) · 2.84 KB
/
autobookmarks.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
<?php
/*
* Plugin Name: WordPress Dashboard Autobookmarks
* Plugin URI: https://github.com/soulseekah/autobookmarks
* Description: This plugin automatically manages a list of most accessed dashboard pages for quick jumping.
* Author: Gennady Kovshenin
* Version: 0.1-alpha
* Author URI: https://codeseekah.com
*/
if ( !defined( 'ABSPATH' ) ) exit; /** Y U NO LOAD CORE? */
/** Start recording */
add_action( 'init', function() {
if ( !is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
return;
define( 'DOING_AUTOBOOKMARKS_BUFFERING', true );
/** Let's try to catch the title of the page */
ob_start();
} );
/** End recording */
add_action( 'admin_head', function() {
if ( !defined( 'DOING_AUTOBOOKMARKS_BUFFERING' ) )
return;
$header = ob_get_clean();
echo $header; // Show it now
$user_id = wp_get_current_user()->ID;
$blacklist = array( '_wpnonce', 'wp_http_referer', 'paged', 's' );
$screen = remove_query_arg( $blacklist );
/** Regular expressions for HTML parsing? Don't kill me :) */
preg_match( '#<title>(.*)</title>#', $header, $matches );
$title = count( $matches ) > 1 ? $matches[1] : '';
/** Cleanup automated title */
$title = trim( str_replace( array( get_bloginfo( 'name' ), ' — ' . __( 'WordPress' ) ), '', $title ) );
/** Tack on edited custom post types, users */
if ( get_current_screen()->base == 'post' && isset( $_GET['post'] ) ) {
$title = $title . ' ' . get_post( $_GET['post'] )->post_title;
} else if ( get_current_screen()->base == 'user-edit' && isset( $_GET['user_id'] ) ) {
$title = $title . ' ' . get_user_by( 'id', $_GET['user_id'] )->display_name;
}
/** Clean any stray arrows */
$title = preg_replace( '# ‹$#', '', $title );
$title = preg_replace( '#^‹ #', '', $title );
$autobookmarks = get_user_meta( $user_id, 'autobookmarks', true ) ? : array();
$autobookmarks[$screen] = empty( $autobookmarks[$screen] ) ? array( 'title' => $title, 'count' => 0 ) : $autobookmarks[$screen];
$autobookmarks[$screen]['title'] = $title;
$autobookmarks[$screen]['count']++;
update_user_meta( $user_id, 'autobookmarks', $autobookmarks );
} );
/** Replay */
add_action( 'admin_bar_menu', function( $admin_bar ) {
$admin_bar->add_node( array(
'id' => 'autobookmarks',
'title' => sprintf( '<span class="ab-icon dashicons dashicons-book"></span><span class="ab-label">%s</span>', 'Autobookmarks' ),
'parent' => false,
) );
$user_id = wp_get_current_user()->ID;
$autobookmarks = get_user_meta( $user_id, 'autobookmarks', true ) ? : array();
uasort( $autobookmarks, function( $a, $b ) {
return $b['count'] - $a['count'];
} );
foreach ( array_slice( $autobookmarks, 0, 10 ) as $href => $bookmark ) {
$admin_bar->add_node( array(
'id' => $href,
'href' => $href,
'title' => $bookmark['title'] . ' (' . $bookmark['count'] . ')',
'parent' => 'autobookmarks',
) );
}
}, 100 );