Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Garrett committed Oct 11, 2013
0 parents commit 6adf047
Show file tree
Hide file tree
Showing 16 changed files with 2,292 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
22 changes: 22 additions & 0 deletions css/jarvis.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#jarvis-overlay {position:fixed;top:0;right:0;bottom:0;left:0;background:rgb(0,0,0);opacity:.8;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)";z-index:100;}
#jarvis-modal {background:rgb(255,255,255);border-radius:.5em;max-width:75%;min-width:50%;padding:1em;position:fixed;left:50%;top:32px;z-index:101;}
#jarvis-search,
#jarvis-modal .tt-hint {background:#ebebeb;border:none;font-size:2em;padding:.5em;width:100%;}
#jarvis-search.tt-query {background: none !important;}
#jarvis-modal .tt-hint {color:rgb(150,150,150);}
#jarvis-modal a {display:block;text-decoration: none;padding:.5em;border:1px solid rgb(255,255,255);}
.twitter-typeahead {width:100%;}
.ie8 .twitter-typeahead {background:rgb(235,235,235);}
.tt-dropdown-menu {background:rgb(255,255,255);border-radius:.5em;padding:1em;margin:0 -1em 0 -1em;width:100%;}
.tt-suggestion {overflow:hidden;}
.tt-suggestion p {margin:0;}
.tt-suggestion a:hover,
.tt-is-under-cursor a {border-radius:.5em;background:rgb(239, 248, 255);color:rgb(213,78,33);}
.tt-is-under-cursor a:hover {border-color:rgb(209,218,225);}
.jarvis-icon {background:url(../../../../wp-includes/images/admin-bar-sprite.png) 0 -100px no-repeat;line-height:28px;width:28px;height:28px;float:left;}
.jarvis-title {display:block;line-height:28px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}
.jarvis-thumbnail {float:right;height:28px;width:28px;}
.jarvis-thumbnail img {display:block;height:28px;width:28px;}
#wp-admin-bar-jarvis_menubar_icon a > img {height:20px;width:20px;margin-top:3px;}

#jarvis-loading {position:absolute;top:50%;margin-top:-8px;right:20px;}
Binary file added img/jarvis-icon-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/wpspin-2x.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/wpspin.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
163 changes: 163 additions & 0 deletions jarvis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php
/*
Plugin Name: WP Jarvis
Plugin URI: http://www.wpjarvis.com
Description: WP Jarvis. Wordpress at your fingertips
Version: 2.0
Author: David Everett, Joan Piedra, Kurtis Shaner
Author URI: http://www.webdevelopmentgroup.com
Text Domain: jarvis
*/

class Jarvis {

private $options = array(
'hotkey' => 191,
'loadingimg' => 'img/wpspin.gif'
);
private $intPageLen = 4;

public function __construct() {
$this->options['loadingimg'] = plugins_url($this->options['loadingimg'], __FILE__);
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() { ?>
<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);
});
</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) {
$admin_bar->add_menu(array(
'id' => 'jarvis_menubar_icon',
'title' => '<img src="'.plugins_url('img/jarvis-icon-white.png', __FILE__).'">',
'href' => '#jarvis',
'meta' => array(
'title' => 'Invoke Jarvis'
),
'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();
?>
Loading

0 comments on commit 6adf047

Please sign in to comment.