Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Some fixes made for inclusion into the VIP Shared Plugins repo #4

Merged
merged 6 commits into from over 1 year ago

3 participants

Daniel Bachhuber Niall Kennedy Andrew Ryno
This page is out of date. Refresh to see the latest.
2  content.php
@@ -71,7 +71,7 @@ public function enqueue_styles() {
71 71 * @uses wp_enqueue_script()
72 72 */
73 73 public function enqueue_scripts() {
74   - wp_enqueue_script( 'jquery', is_ssl() ? 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' : 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', array(), null, true );
  74 + wp_enqueue_script( 'jquery' );
75 75 wp_enqueue_script( 'angellist', plugins_url( 'static/js/angellist' . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ? '.dev' : '' ) . '.js', __FILE__ ), array( 'jquery' ), '1.2', true );
76 76 }
77 77
57 edit.php
@@ -27,6 +27,8 @@ class AngelList_Post_Meta_Box {
27 27 * @since 1.0
28 28 */
29 29 public function __construct() {
  30 + add_action( 'wp_ajax_angellist-search', array( &$this, 'search' ) );
  31 +
30 32 foreach ( array( 'post.php', 'post-new.php' ) as $action ) {
31 33 add_action( 'load-' . $action, array( &$this, 'load' ) );
32 34 }
@@ -67,7 +69,7 @@ public function add_meta_boxes() {
67 69 */
68 70 public function enqueue_scripts() {
69 71 // if jQuery not present load from Google CDN
70   - wp_enqueue_script( 'jquery', is_ssl() ? 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' : 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', array(), null );
  72 + wp_enqueue_script( 'jquery' );
71 73
72 74 // vary minified or not minified based on SCRIPT_DEBUG
73 75 $js_filename = 'angellist-company-selector.js';
@@ -141,7 +143,7 @@ public function company_selector() {
141 143
142 144 echo '<script type="text/javascript">jQuery( "#' . AngelList_Post_Meta_Box::BASE_ID . '" ).one( "' . AngelList_Post_Meta_Box::BASE_ID . '-onload", function(){';
143 145 foreach ( array(
144   - 'search_url' => plugins_url( 'search/startups.php', __FILE__ ),
  146 + 'search_url' => admin_url( 'admin-ajax.php' ),
145 147 'company_ids' => $company_ids,
146 148 'labels' => array(
147 149 'remove' => __( 'Delete', 'angellist' ),
@@ -198,7 +200,7 @@ public function process_saved_data( $post_id ) {
198 200 $company_id = absint( $company['id'] );
199 201 if ( $company_id < 1 || in_array( $company_id, $processed_company_ids, true ) )
200 202 continue;
201   - $companies[] = array( 'id' => $company_id, 'name' => trim( $company['name'] ) );
  203 + $companies[] = array( 'id' => $company_id, 'name' => trim( sanitize_text_field( $company['name'] ) ) );
202 204 $processed_company_ids[] = $company_id;
203 205 unset( $company_id );
204 206 }
@@ -316,5 +318,52 @@ public function add_help_tab() {
316 318 'content' => '<p>' . esc_html( __( 'Search for a company by name.', 'angellist' ) ) . '</p><p>' . esc_html( __( 'Select a matching company from the result list.', 'angellist' ) ) . '</p><p>' . esc_html( __( 'Rearrange companies in the order you would like them to appear in your post by dragging a company name to its new position.', 'angellist' ) ) . '</p><p>' . esc_html( sprintf( __( 'Remove a company from the list by clicking the %s button.', 'angellist' ), 'X' ) ) . '</p>'
317 319 ) );
318 320 }
  321 +
  322 + /**
  323 + * Search AngelList companies by freeform text
  324 + *
  325 + * @since 1.0
  326 + */
  327 + public function search() {
  328 + // GET only
  329 + if ( array_key_exists( 'REQUEST_METHOD', $_SERVER ) && $_SERVER['REQUEST_METHOD'] !== 'GET' ) {
  330 + header( 'HTTP/1.1 405 Method Not Allowed', true, 405 );
  331 + header( 'Allow: GET', true );
  332 + exit();
  333 + }
  334 +
  335 + // allow only logged-on users with the capability to see an edit post screen to access our API proxy
  336 + if ( ! current_user_can( 'edit_posts' ) )
  337 + $this->reject_message( new WP_Error( 403, __( 'Cheatin\' uh?' ) ) );
  338 +
  339 + if ( ! array_key_exists( 'q', $_GET ) )
  340 + $this->reject_message( new WP_Error( 400, 'Search string needed. Use q query parameter.' ) );
  341 +
  342 + $__search_term = trim( sanitize_text_field( $_GET['q'] ) );
  343 + if ( empty( $__search_term ) )
  344 + $this->reject_message( new WP_Error( 400, 'No search string provided.' ) );
  345 +
  346 + if ( ! class_exists( 'AngelList_Search' ) )
  347 + require_once( dirname(__FILE__) . '/search/class-angellist-search.php' );
  348 +
  349 + $__companies = AngelList_Search::startups( $__search_term );
  350 + if ( is_wp_error( $__companies ) )
  351 + $this->reject_message( $__companies );
  352 + else
  353 + echo json_encode( $__companies );
  354 +
  355 + die();
  356 + }
  357 +
  358 + /**
  359 + * Echo a JSON error message, set a HTTP status, and exit
  360 + *
  361 + * @since 1.0
  362 + * @param WP_Error $error error code of HTTP status int. error message echoed in JSON
  363 + */
  364 + public function reject_message( WP_Error $error ) {
  365 + status_header( $error->get_error_code() );
  366 + echo json_encode( array( 'error' => $error->get_error_message() ) );
  367 + exit();
  368 + }
319 369 }
320   -?>
53 search/startups.php
... ... @@ -1,53 +0,0 @@
1   -<?php
2   -/**
3   - * Search AngelList companies by freeform text
4   - *
5   - * @since 1.0
6   - */
7   -
8   -// GET only
9   -if ( array_key_exists( 'REQUEST_METHOD', $_SERVER ) && $_SERVER['REQUEST_METHOD'] !== 'GET' ) {
10   - header( 'HTTP/1.1 405 Method Not Allowed', true, 405 );
11   - header( 'Allow: GET', true );
12   - exit();
13   -}
14   -
15   -// WordPress bootstrap. assume a wp-content/plugins/
16   -if ( ! function_exists( 'current_user_can' ) )
17   - require_once( dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) . '/wp-load.php' );
18   -
19   -// override HTML default Content-Type with JSON
20   -header( 'Content-Type: application/json; charset=utf-8', true );
21   -
22   -/**
23   - * Echo a JSON error message, set a HTTP status, and exit
24   - *
25   - * @since 1.0
26   - * @param WP_Error $error error code of HTTP status int. error message echoed in JSON
27   - */
28   -function angellist_reject_message( WP_Error $error ) {
29   - status_header( $error->get_error_code() );
30   - echo json_encode( array( 'error' => $error->get_error_message() ) );
31   - exit();
32   -}
33   -
34   -// allow only logged-on users with the capability to see an edit post screen to access our API proxy
35   -if ( ! current_user_can( 'edit_posts' ) )
36   - angellist_reject_message( new WP_Error( 403, __( 'Cheatin\' uh?' ) ) );
37   -
38   -if ( ! array_key_exists( 'q', $_GET ) )
39   - angellist_reject_message( new WP_Error( 400, 'Search string needed. Use q query parameter.' ) );
40   -
41   -$__search_term = trim( $_GET['q'] );
42   -if ( empty( $__search_term ) )
43   - angellist_reject_message( new WP_Error( 400, 'No search string provided.' ) );
44   -
45   -if ( ! class_exists( 'AngelList_Search' ) )
46   - require_once( dirname(__FILE__) . '/class-angellist-search.php' );
47   -
48   -$__companies = AngelList_Search::startups( $__search_term );
49   -if ( is_wp_error( $__companies ) )
50   - angellist_reject_message( $__companies );
51   -else
52   - echo json_encode( $__companies );
53   -?>
6 static/js/angellist-company-selector.dev.js
@@ -148,7 +148,11 @@ angellist.company_selector = {
148 148 return;
149 149 }
150 150 jQuery.ajax({
151   - url: angellist.company_selector.search_url + "?" + jQuery.param( {q:term} ),
  151 + url: angellist.company_selector.search_url,
  152 + data: {
  153 + action: 'angellist-search',
  154 + q: term
  155 + },
152 156 dataType: "json",
153 157 success: function( companies ) {
154 158 response( jQuery.map( companies, function( company ) {
2  static/js/angellist-company-selector.js
... ... @@ -1 +1 @@
1   -var angellist=angellist||{};angellist.company_selector={version:"1.2",labels:{remove:"Delete",no_results:"No results found.",search:"Add a company:",search_placeholder:"Start typing..."},company_ids:[],create_company_list:function(){var results_div=jQuery("#angellist-company-selector-results");if(results_div.length===0){return}results_div.html("");angellist.company_selector.company_list=jQuery("<ol />").attr("id","angellist-company-selector-companies");results_div.append(angellist.company_selector.company_list);angellist.company_selector.enable_editable_company_list()},add_company:function(company){if(!jQuery.isPlainObject(company)||company.value===undefined||company.value===""||company.label===undefined||jQuery.inArray(company.value,angellist.company_selector.company_ids)>-1){return}if(angellist.company_selector.company_list===undefined||angellist.company_selector.company_list.length===0){angellist.company_selector.create_company_list()}var current_position=angellist.company_selector.company_ids.length;var li=jQuery("<li />").text(company.label);li.append(jQuery("<input />").attr({type:"hidden",name:"angellist-company["+current_position+"][id]"}).addClass("angellist-company-id").val(company.value));li.append(jQuery("<input />").attr({type:"hidden",name:"angellist-company["+current_position+"][name]"}).val(company.label));li.append(angellist.company_selector.company_delete_button());li.mouseenter(angellist.company_selector.company_mouseenter).mouseleave(angellist.company_selector.company_mouseleave);angellist.company_selector.company_list.append(li);angellist.company_selector.company_ids.push(company.value);if(angellist.company_selector.company_ids.length==2){angellist.company_selector.enable_editable_company_list()}},company_delete_button:function(){return jQuery("<button />").attr({type:"button",title:angellist.company_selector.labels.remove}).addClass("angellist-delete-company").text("X").click(angellist.company_selector.delete_company_handler).hide()},delete_company_handler:function(){var company=jQuery(this).closest("li");if(company.length===0){return}var company_id=parseInt(company.find(".angellist-company-id").val(),10);if(typeof company_id!=="number"){return}var position=jQuery.inArray(company_id,angellist.company_selector.company_ids);if(position!==-1){angellist.company_selector.company_ids.splice(position,1)}if(angellist.company_selector.company_ids.length===0){angellist.company_selector.company_list.remove();delete angellist.company_selector.company_list}else{company.remove();angellist.company_selector.company_list_onchange()}},company_mouseenter:function(){jQuery(this).find("button").show()},company_mouseleave:function(){jQuery(this).find("button").hide()},company_list_onchange:function(){angellist.company_selector.company_list.find("li").each(function(index){jQuery(this).find("input").each(function(){var input_el=jQuery(this);var name=input_el.attr("name");input_el.attr("name",name.substring(0,18)+index+name.substring(name.indexOf("]",18)))})})},create_autocomplete_search:function(){var search_div=jQuery("<div />").attr("id","angellist-company-selector-search");var searchbox_id="angellist-company-selector-searchbox";if(typeof angellist.company_selector.labels.search==="string"){search_div.append(jQuery("<div />").append(jQuery("<label />").attr("for",searchbox_id).text(angellist.company_selector.labels.search)))}var searchbox=jQuery("<input />").attr({id:searchbox_id,type:"search",size:30,autocomplete:"on"});if(typeof angellist.company_selector.labels.search_placeholder==="string"){searchbox.attr("placeholder",angellist.company_selector.labels.search_placeholder)}search_div.append(searchbox);angellist.company_selector.post_box.find("div.inside").append(search_div)},enable_autocomplete_search:function(){var search_div=jQuery("#angellist-company-selector-search");if(search_div.length===0){angellist.company_selector.create_autocomplete_search();search_div=jQuery("#angellist-company-selector-search")}search_div.show();jQuery("#angellist-company-selector-searchbox").autocomplete({appendTo:search_div,disabled:false,focus:function(){return false},minLength:3,select:function(event,ui){jQuery("#angellist-company-selector-searchbox").val("");angellist.company_selector.add_company(ui.item);return false},source:function(request,response){var term=jQuery.trim(request.term);if(term===""){return}jQuery.ajax({url:angellist.company_selector.search_url+"?"+jQuery.param({q:term}),dataType:"json",success:function(companies){response(jQuery.map(companies,function(company){return{label:company.name,value:parseInt(company.id,10)}}))},statusCode:{404:function(){response([{label:angellist.company_selector.labels.no_results,value:""}])}}})}})},enable_company_delete:function(){jQuery("#angellist-company-selector-companies li").each(function(){var company=jQuery(this);company.append(angellist.company_selector.company_delete_button());company.mouseenter(angellist.company_selector.company_mouseenter).mouseleave(angellist.company_selector.company_mouseleave)})},enable_editable_company_list:function(){if(angellist.company_selector.company_list===undefined){angellist.company_selector.company_list=jQuery("#angellist-company-selector-companies")}if(angellist.company_selector.company_list.length===0){return}angellist.company_selector.company_list.sortable({axis:"y",containment:"parent",cursor:"move",dropOnEmpty:false,items:"li",update:angellist.company_selector.company_list_onchange})},enable:function(){if(angellist.company_selector.company_ids.length>0){angellist.company_selector.company_list=jQuery("#angellist-company-selector-companies");angellist.company_selector.enable_company_delete();if(angellist.company_selector.company_ids.length>1){angellist.company_selector.enable_editable_company_list()}}angellist.company_selector.enable_autocomplete_search()}};jQuery(function(){angellist.company_selector.post_box=jQuery("#angellist-company-selector");if(angellist.company_selector.post_box.length===0){return}else{angellist.company_selector.post_box.trigger("angellist-company-selector-onload")}angellist.company_selector.enable()});
  1 +var angellist=angellist||{};angellist.company_selector={version:"1.2",labels:{remove:"Delete",no_results:"No results found.",search:"Add a company:",search_placeholder:"Start typing..."},company_ids:[],create_company_list:function(){var a=jQuery("#angellist-company-selector-results");if(a.length===0){return}a.html("");angellist.company_selector.company_list=jQuery("<ol />").attr("id","angellist-company-selector-companies");a.append(angellist.company_selector.company_list);angellist.company_selector.enable_editable_company_list()},add_company:function(c){if(!jQuery.isPlainObject(c)||c.value===undefined||c.value===""||c.label===undefined||jQuery.inArray(c.value,angellist.company_selector.company_ids)>-1){return}if(angellist.company_selector.company_list===undefined||angellist.company_selector.company_list.length===0){angellist.company_selector.create_company_list()}var b=angellist.company_selector.company_ids.length;var a=jQuery("<li />").text(c.label);a.append(jQuery("<input />").attr({type:"hidden",name:"angellist-company["+b+"][id]"}).addClass("angellist-company-id").val(c.value));a.append(jQuery("<input />").attr({type:"hidden",name:"angellist-company["+b+"][name]"}).val(c.label));a.append(angellist.company_selector.company_delete_button());a.mouseenter(angellist.company_selector.company_mouseenter).mouseleave(angellist.company_selector.company_mouseleave);angellist.company_selector.company_list.append(a);angellist.company_selector.company_ids.push(c.value);if(angellist.company_selector.company_ids.length==2){angellist.company_selector.enable_editable_company_list()}},company_delete_button:function(){return jQuery("<button />").attr({type:"button",title:angellist.company_selector.labels.remove}).addClass("angellist-delete-company").text("X").click(angellist.company_selector.delete_company_handler).hide()},delete_company_handler:function(){var b=jQuery(this).closest("li");if(b.length===0){return}var c=parseInt(b.find(".angellist-company-id").val(),10);if(typeof c!=="number"){return}var a=jQuery.inArray(c,angellist.company_selector.company_ids);if(a!==-1){angellist.company_selector.company_ids.splice(a,1)}if(angellist.company_selector.company_ids.length===0){angellist.company_selector.company_list.remove();delete angellist.company_selector.company_list}else{b.remove();angellist.company_selector.company_list_onchange()}},company_mouseenter:function(){jQuery(this).find("button").show()},company_mouseleave:function(){jQuery(this).find("button").hide()},company_list_onchange:function(){angellist.company_selector.company_list.find("li").each(function(a){jQuery(this).find("input").each(function(){var c=jQuery(this);var b=c.attr("name");c.attr("name",b.substring(0,18)+a+b.substring(b.indexOf("]",18)))})})},create_autocomplete_search:function(){var a=jQuery("<div />").attr("id","angellist-company-selector-search");var c="angellist-company-selector-searchbox";if(typeof angellist.company_selector.labels.search==="string"){a.append(jQuery("<div />").append(jQuery("<label />").attr("for",c).text(angellist.company_selector.labels.search)))}var b=jQuery("<input />").attr({id:c,type:"search",size:30,autocomplete:"on"});if(typeof angellist.company_selector.labels.search_placeholder==="string"){b.attr("placeholder",angellist.company_selector.labels.search_placeholder)}a.append(b);angellist.company_selector.post_box.find("div.inside").append(a)},enable_autocomplete_search:function(){var a=jQuery("#angellist-company-selector-search");if(a.length===0){angellist.company_selector.create_autocomplete_search();a=jQuery("#angellist-company-selector-search")}a.show();jQuery("#angellist-company-selector-searchbox").autocomplete({appendTo:a,disabled:false,focus:function(){return false},minLength:3,select:function(b,c){jQuery("#angellist-company-selector-searchbox").val("");angellist.company_selector.add_company(c.item);return false},source:function(d,b){var c=jQuery.trim(d.term);if(c===""){return}jQuery.ajax({url:angellist.company_selector.search_url,data:{action:"angellist-search",q:c},dataType:"json",success:function(e){b(jQuery.map(e,function(f){return{label:f.name,value:parseInt(f.id,10)}}))},statusCode:{404:function(){b([{label:angellist.company_selector.labels.no_results,value:""}])}}})}})},enable_company_delete:function(){jQuery("#angellist-company-selector-companies li").each(function(){var a=jQuery(this);a.append(angellist.company_selector.company_delete_button());a.mouseenter(angellist.company_selector.company_mouseenter).mouseleave(angellist.company_selector.company_mouseleave)})},enable_editable_company_list:function(){if(angellist.company_selector.company_list===undefined){angellist.company_selector.company_list=jQuery("#angellist-company-selector-companies")}if(angellist.company_selector.company_list.length===0){return}angellist.company_selector.company_list.sortable({axis:"y",containment:"parent",cursor:"move",dropOnEmpty:false,items:"li",update:angellist.company_selector.company_list_onchange})},enable:function(){if(angellist.company_selector.company_ids.length>0){angellist.company_selector.company_list=jQuery("#angellist-company-selector-companies");angellist.company_selector.enable_company_delete();if(angellist.company_selector.company_ids.length>1){angellist.company_selector.enable_editable_company_list()}}angellist.company_selector.enable_autocomplete_search()}};jQuery(function(){angellist.company_selector.post_box=jQuery("#angellist-company-selector");if(angellist.company_selector.post_box.length===0){return}else{angellist.company_selector.post_box.trigger("angellist-company-selector-onload")}angellist.company_selector.enable()});

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.