forked from wdgdc/Jarvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjarvis.php
executable file
·180 lines (152 loc) · 7.45 KB
/
jarvis.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
<?php
/*
Plugin Name: Jarvis
Plugin URI: http://www.wpjarvis.com
Description: Jarvis is your administration assistant, putting WordPress at your fingertips.
Version: 0.31
Author: wdgdc, David Everett, Joan Piedra, Kurtis Shaner
Author URI: http://www.webdevelopmentgroup.com
License: GPLv2 or later
Text Domain: jarvis
*/
class Jarvis {
private $options = array(
'hotkey' => 191,
'loadingimg' => 'img/wpspin.gif',
'dashicons' => false
);
private $intPageLen = 4;
public function __construct() {
global $wp_version;
$this->options['loadingimg'] = plugins_url($this->options['loadingimg'], __FILE__);
$this->options['dashicons'] = (version_compare($wp_version, '3.8', '>=')) ? true : false;
add_action('wp_ajax_jarvis-search', array($this, 'get_search_results'), 1);
add_action('admin_enqueue_scripts', array($this, 'enqueue'));
//add_action('admin_menu', array($this, 'admin_menu'));
add_action('admin_footer', array($this, 'init'));
add_action('admin_bar_menu', array($this, 'menubar_icon'), 100);
add_action('wp_ajax_jarvis_settings', array($this, 'wp_ajax_jarvis_settings'));
add_action('admin_init', array($this, 'resigter_jarvis_settings'));
}
public function resigter_jarvis_settings() {
register_setting('jarvis_settings', 'hotkey');
register_setting('jarvis_settings', 'results_limit');
}
public function enqueue() {
if (is_user_logged_in()) {
wp_enqueue_style('wp-jarvis', plugins_url('css/jarvis.css', __FILE__));
wp_enqueue_script('typeahead', plugins_url('js/typeahead.min.js', __FILE__), array('jquery'), '0.9.3');
wp_enqueue_script('hogan', plugins_url('js/hogan.min.js', __FILE__), null, '2.0.0');
wp_enqueue_script('wp-jarvis', plugins_url('js/jarvis.js', __FILE__), array('typeahead', 'hogan'), '.1');
}
}
public function init() {
global $wp_version;
if ( current_user_can('edit_posts') ) { ?>
<script>
var wp = wp || {};
wp.jarvis = new Jarvis(<?php echo json_encode($this->options); ?>);
jQuery("#wp-admin-bar-jarvis_menubar_icon a").on("click", function(e) {
wp.jarvis.open(e);
console.log('hello');
});
</script>
<?php
}
}
public function admin_menu() {
add_options_page('Jarvis Options', 'Jarvis', 'administrator', 'jarvis_settings', array($this, 'wp_ajax_jarvis_settings'));
}
public function wp_ajax_jarvis_settings() {
include_once('settings.php');
}
public function menubar_icon($admin_bar) {
if ( current_user_can('edit_posts') ) {
$className = ($this->options['dashicons'] === true) ? 'dashicon' : 'image';
$admin_bar->add_menu(array(
'id' => 'jarvis_menubar_icon',
'title' => '<span>Jarvis Search</span>',
'href' => '#jarvis',
'meta' => array(
'title' => 'Invoke Jarvis',
'class' => $className
),
'parent' => 'top-secondary'
));
}
}
public function get_search_results() {
global $wpdb;
$arrTypeEditPaths = array(
'_default_' => 'post.php?post=%s&action=edit',
'term' => 'edit-tags.php?action=edit&tag_ID=%s&taxonomy=%s',
'post' => 'post.php?post=%s&action=edit'
);
$_REQUEST['q'] = isset($_REQUEST['q']) ? $_REQUEST['q'] : '';
$srch_qry = like_escape($_REQUEST['q']);
$srch_escaped_spaces = str_replace(' ', '%', $srch_qry);
$strQry = " SELECT\n".
" ". $wpdb->prefix ."terms.term_id as 'id',\n".
" ". $wpdb->prefix ."terms.`name` as 'title',\n".
" ". $wpdb->prefix ."term_taxonomy.taxonomy as 'type',\n".
" 'term' as 'kind', ". $wpdb->prefix ."terms.slug as 'slug', \n".
" FLOOR( (LENGTH(". $wpdb->prefix ."terms.term_id) - LENGTH(REPLACE(LOWER(". $wpdb->prefix ."terms.term_id), LOWER('". $srch_qry ."'), '')) / LENGTH('". $srch_qry ."')) ) as 'relv_id', \n".
" FLOOR( (LENGTH(". $wpdb->prefix ."term_taxonomy.taxonomy) - LENGTH(REPLACE(LOWER(". $wpdb->prefix ."term_taxonomy.taxonomy), LOWER('". $srch_qry ."'), '')) / LENGTH('". $srch_qry ."')) ) as 'relv_title', \n".
" FLOOR( (LENGTH(". $wpdb->prefix ."terms.`name`) - LENGTH(REPLACE(LOWER(". $wpdb->prefix ."terms.`name`), LOWER('". $srch_qry ."'), '')) / LENGTH('". $srch_qry ."')) ) as 'relv_type', \n".
" FLOOR( LENGTH(". $wpdb->prefix ."terms.slug) / LENGTH(REPLACE(LOWER(". $wpdb->prefix ."terms.slug), LOWER('". $srch_qry ."'), '')) ) as 'relv_slug' \n".
" FROM\n".
" ". $wpdb->prefix ."terms\n".
" INNER JOIN ". $wpdb->prefix ."term_taxonomy ON ". $wpdb->prefix ."term_taxonomy.term_id = ". $wpdb->prefix ."terms.term_id\n".
" WHERE \n".
" " . $wpdb->prefix ."terms.`name` LIKE '%". $srch_escaped_spaces ."%' OR\n".
" " . $wpdb->prefix ."terms.slug LIKE '%". $srch_escaped_spaces ."%'\n".
" UNION\n".
" SELECT\n".
" ". $wpdb->prefix ."posts.ID as 'id',\n".
" ". $wpdb->prefix ."posts.post_title as 'title',\n".
" ". $wpdb->prefix ."posts.post_type as 'type',\n".
" 'post' as 'kind', ". $wpdb->prefix ."posts.post_name as 'slug', \n".
" FLOOR( (LENGTH(". $wpdb->prefix ."posts.ID) - LENGTH(REPLACE(LOWER(". $wpdb->prefix ."posts.ID), LOWER('". $srch_qry ."'), '')) / LENGTH('". $srch_qry ."')) ) as 'relv_id', \n".
" FLOOR( (LENGTH(". $wpdb->prefix ."posts.post_title) - LENGTH(REPLACE(LOWER(". $wpdb->prefix ."posts.post_title), LOWER('". $srch_qry ."'), '')) / LENGTH('". $srch_qry ."')) ) as 'relv_title', \n".
" FLOOR( (LENGTH(". $wpdb->prefix ."posts.post_type) - LENGTH(REPLACE(LOWER(". $wpdb->prefix ."posts.post_type), LOWER('". $srch_qry ."'), '')) / LENGTH('". $srch_qry ."')) ) as 'relv_type', \n".
" FLOOR( ( LENGTH(". $wpdb->prefix ."posts.post_name) / LENGTH(REPLACE(LOWER(". $wpdb->prefix ."posts.post_name), LOWER('". $srch_qry ."'), '')) ) ) as 'relv_slug' \n".
" FROM ". $wpdb->prefix ."posts\n".
" WHERE\n".
" ". $wpdb->prefix ."posts.post_status NOT IN ('revision', 'auto-draft') AND ". $wpdb->prefix ."posts.post_type <> 'revision' AND \n".
" (". $wpdb->prefix ."posts.post_title LIKE '%". $srch_escaped_spaces ."%' OR ". $wpdb->prefix ."posts.post_name LIKE '%". $srch_escaped_spaces ."%')\n".
" ORDER BY relv_id, relv_slug, relv_type, relv_title DESC\n";
" LIMIT 20";
if( !isset($_REQUEST['debug']) ) {
$intPageFrom = isset($_REQUEST['p']) ? (((int)$_REQUEST['p'] - 1) * $this->intPageLen) : 0 ;
$strQry .= " LIMIT ". $intPageFrom .", ". $this->intPageLen ."\n";
}
$results = $wpdb->get_results( $strQry, ARRAY_A);
$strSiteUrl = get_site_url();
foreach($results as $itmIdx => &$itmRrd) {
$tmpType = $itmRrd['type'];
$tmpKind = $itmRrd['kind'];
$tmpId = $itmRrd['id'];
if( !isset($arrRtrn['types'][ $tmpType ]) ){
$arrRtrn['types'][ $tmpType ] = array();
}
$arrRtrn['types'][ $tmpType ][ $tmpId ] = $itmIdx;
$itmRrd['href'] = $strSiteUrl . '/wp-admin/' . sprintf(( isset($arrTypeEditPaths[$tmpKind]) ? $arrTypeEditPaths[$tmpKind] : $arrTypeEditPaths['_default_'] ), $tmpId, $tmpType);
switch($itmRrd['type']) {
case 'attachment':
$itmRrd['att_src'] = wp_get_attachment_image_src($itmRrd['id'], array(28,28));
$itmRrd['att_src'] = $itmRrd['att_src'][0];
break;
case 'post':
$itmRrd['att_src'] = wp_get_attachment_image_src(get_post_thumbnail_id($itmRrd['id'], array(28,28)));
$itmRrd['att_src'] = $itmRrd['att_src'][0];
break;
}
}
header('Content-type: application/json');
echo json_encode($results);
// --- WhyTF would this be something that need to be done ?!? --- //
die(); // this is required to return a proper result
}
}
if (is_admin()) $wp_jarvis = new Jarvis();
?>