-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.smartkeys.php
112 lines (88 loc) · 2.57 KB
/
class.smartkeys.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
<?php
add_action( 'init', array( 'Smartkeys', 'init' ) );
class Smartkeys {
public static function init() {
static $instance = NULL;
if ( !$instance ) {
$instance = new Smartkeys;
}
return $instance;
}
function __construct() {
// Register scripts
add_action( 'init', array( $this, 'smartkeys_register_assets' ) );
if ( is_user_logged_in() ) {
add_action( 'admin_enqueue_scripts', array( $this, 'smartkeys_enqueue_admin_keys' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'smartkeys_enqueue_admin_keys' ) );
} else {
add_action( 'wp_enqueue_scripts', array( $this, 'smartkeys_enqueue_visitor_keys' ) );
}
}
function smartkeys_register_assets() {
wp_register_style( 'smartkeys-css', plugins_url( 'css/style.css' , __FILE__ ) );
}
/*
* Convert menu_slug to menu title
*
* @param string $path
*
* @return string - the Title of the page. Or WP_Error if something went wrong.
*/
function smartkeys_convert_to_title( $path = '' ) {
global $menu;
if ( ! is_string( $path ) ) {
return new WP_Error( 'not-a-string', 'gotta use a string' );
}
foreach ( $menu as $page => $values ) {
if ( $path == $values[2] ) {
// Some menu items have span tags in them, let's remove the number
if ( preg_match( '/(>\d{1,3}<)/', $values[0], $match ) ) {
$values[0] = wp_strip_all_tags( str_replace( $match, '', $values[0] ) );
}
return $values[0];
}
}
return 'Other';
}
/*
* All of the admin pages in organized arrays
*
* @return array
*/
function smartkeys_organized_pages() {
global $menu, $submenu;
$all_pages = array();
foreach ( $menu as $menu_item ) {
if ( $menu_item[0] ) {
$all_pages[ $menu_item[0] ] = $menu_item;
}
}
foreach ( $submenu as $index => $pages ) {
$title = $this->smartkeys_convert_to_title( $index );
$group = array();
foreach ( $pages as $page ) {
// $page[0] is the title of the page
$group[ $page[0] ] = $page;
$all_pages[ $title ] = array(
'sub_pages' => $group,
);
}
}
return $all_pages;
}
function smartkeys_enqueue_admin_keys() {
if ( ! is_admin() ) {
return;
}
include_once 'larry-bird.php';
wp_enqueue_script( 'smartkeys-master', plugin_dir_url( __FILE__ ) . 'js/smartkeys-master.js', array( 'jquery', 'underscore' ), false );
wp_localize_script( 'smartkeys-master', 'smartkeysMasterVars',
array(
'adminUrl' => get_admin_url( get_current_blog_id() ),
'optionKeys' => get_option( 'keys_to_save' ),
'adminPages' => $this->smartkeys_organized_pages(),
'currentCombo' => '',
)
);
}
}