-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadmin-last-pages.php
216 lines (161 loc) · 6.16 KB
/
admin-last-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
210
211
212
213
214
215
216
<?php
/**
* Plugin Name: Admin Last Pages
* Plugin URI: https://github.com/jonjennings/admin-last-pages
* Description: Add the last five pages visited in the admin to a drop downmenu in the admin bar.
* Version: 0.1
* Author: Jon Jennings, Flynn O'Connor
*/
/* Copyright 2013 Jon Jennings, Flynn O'Connor
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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 Admin_Last_Pages {
function __construct() {
// Add our menu to the admin bar
add_action( 'admin_bar_menu', array( $this, 'add_prev_pages_to_toolbar' ), 999 );
// Get the admin page title.
add_filter( 'admin_title', array( $this, 'get_admin_title' ), 10, 2);
// Queue scripts/styles
add_action( 'wp_enqueue_scripts', array( $this, 'queue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'queue_scripts' ) );
}
function queue_scripts() {
// Load the Font Awesome iconfont
wp_enqueue_style( 'font-awesome', '//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css' );
}
/**
* add_prev_pages_to_toolbar function.
*
* Loop through array stored in user meta and display previous admin pages visited.
*
* @access public
* @param mixed $wp_admin_bar
* @return void
*/
function add_prev_pages_to_toolbar( $wp_admin_bar ) {
// meta key is "_previous_pages"
$user_new = get_user_meta( get_current_user_id(), '_previous_pages', true );
// add a parent item
// TODO: excuse the inlined styles... when things get more complex move these out to a CSS file
$args = array(
'id' => 'previous_pages',
'title' => '<i title="' . __( 'Previous Pages', 'admin-last-pages' ) . '" style="font-family: FontAwesome; font-size: 21px;" class="icon-compass"></i>',
'parent' => 'top-secondary',
);
$wp_admin_bar->add_node( $args );
foreach( $user_new as $test_page ) {
// add a child item to our parent item
$args = array(
'id' => 'prev_page_' . $test_page['title'],
'title' => $test_page['title'],
'href' => $test_page['url'],
'parent' => 'previous_pages'
);
$wp_admin_bar->add_node( $args );
}
}
/**
* get_admin_title function.
*
* Get admin page title or if post get post title.
*
* @access public
* @param mixed $admin_title
* @param mixed $title
* @return void
*/
function get_admin_title( $admin_title, $title ) {
$limit = 5;
$user_id = get_current_user_id();
//current url
$the_url = $_SERVER['REQUEST_URI'];
//delete_user_meta( $user_id, '_previous_pages' );
$user_last = get_user_meta( $user_id, '_previous_pages', true );
//print_r($user_last);
// any update page or any other page that requires a nonce we don't want to save.
if( isset($_GET['_wpnonce']) )
return;
// Don't search page array if array doesn't exist (ie first time here)
if ( ! empty( $user_last ) ) {
// check if url and title are both already in array.
if ( self::in_array_r( $the_url, $user_last ) && self::in_array_r( $title, $user_last ) )
return;
// check if on post edit page.
if( isset( $_GET['post'] ) ) {
// loop through saved pages, if current post id is already in the array don't save it.
foreach($user_last as $page){
if( $page['type'] == 'post' && $page['id'] == $_GET['post'])
return;
}
}
//check if on taxonomy edit page
if( isset($_GET['taxonomy']) && isset($_GET['action']) ){
// loop through saved pages, if current taxonomy id is already in the array don't save it.
foreach($user_last as $page){
if($page['type'] == 'taxonomy' && $page['id'] == $_GET['tag_ID'])
return;
}
}
}
if( isset( $_GET['post'] ) ) {
// post hasn't been previously been saved, get the post title.
$current_page_title = get_the_title( $_GET['post'] );
$new_entry = array( 'title' => $current_page_title, 'url' => $the_url, 'id' => $_GET['post'], 'type' => 'post' );
}elseif( isset($_GET['taxonomy']) && isset($_GET['action']) ){
// if taxonomy term hasn't previously been saved, get the term name.
$term = get_term( $_GET['tag_ID'], $_GET['taxonomy'] );
$new_entry = array( 'title' => $title .' - '. $term->name, 'url' => $the_url, 'id' => $_GET['tag_ID'], 'type' => 'taxonomy' );
}else {
$new_entry = array( 'title' => $title, 'url' => $the_url );
}
// if the array is empty array_unshift won't work so we simply add a new array.
if ( ! empty( $user_last ) ) {
array_unshift( $user_last, $new_entry );
} else {
$user_last[] = $new_entry;
}
//limit the number of previous pages we list to 5. @todo: functionality to let user set limit.
$page_count = count( $user_last );
// any pages over the limit are unset.
if ( $page_count > $limit ) {
for ( $i = 5 ; $i <= $page_count; $i++ ) {
unset( $user_last[ $i ] );
}
}
update_user_meta( $user_id, '_previous_pages', $user_last );
return;
}
/**
* in_array_r function.
*
* look for a value in a multi-dimensional array.
* Found here: http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array
*
* @access private
* @static
* @param mixed $needle
* @param mixed $haystack
* @param bool $strict (default: false)
* @return void
*/
private static function in_array_r( $needle, $haystack, $strict = false) {
foreach ( $haystack as $item ) {
if ( ( $strict ? $item === $needle : $item == $needle ) || ( is_array( $item ) && self::in_array_r( $needle, $item, $strict ) ) ) {
return true;
}
}
return false;
}
}
// Instantiate ourselves
$admin_last_pages = new Admin_Last_Pages();
?>