Changeset 377014
- Timestamp:
- 04/25/2011 06:45:04 PM (15 years ago)
- Location:
- twitter-api-shortcodes/trunk
- Files:
-
- 6 added
- 2 deleted
- 5 edited
- 3 moved
-
TODO.txt (modified) (1 diff)
-
js/tas-search.js (added)
-
libs/json-api.php (deleted)
-
libs/tasforwp.class.inc.php (modified) (5 diffs)
-
libs/twitter.api.wp.class.inc.php (modified) (5 diffs)
-
libs/twitter.search.class.inc.php (modified) (6 diffs)
-
tests/ant-build.xml (added)
-
tests/bootstrap.php (added)
-
tests/phpunit-wp-integration.xml (added)
-
tests/phpunit.xml (moved) (moved from twitter-api-shortcodes/trunk/phpunit.xml) (1 diff)
-
tests/tasforwpOutputTest.php (deleted)
-
tests/unit (added)
-
tests/wp-integration (added)
-
tests/wp-integration/functionsTest.php (moved) (moved from twitter-api-shortcodes/trunk/tests/functionsTest.php) (2 diffs)
-
tests/wp-integration/tasforwpTest.php (moved) (moved from twitter-api-shortcodes/trunk/tests/tasforwpTest.php) (4 diffs)
-
twitter-api-shortcodes.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
twitter-api-shortcodes/trunk/TODO.txt
r322494 r377014 12 12 SEARCH 13 13 - Plan for (and eventually implement) grabbing searches from google's twitter search. Right now it isn't completely built out, but when it is we can use it, rather than caching searches. 14 - jQuery pagination of searches from the shortcode.15 14 16 15 MISC -
twitter-api-shortcodes/trunk/libs/tasforwp.class.inc.php
r373349 r377014 26 26 public static $admin_menu_hook = "TasForWp::tas_admin_menu"; 27 27 public static $wp_print_styles_hook = "TasForWp::tas_wp_print_styles"; 28 public static $wp_print_scripts_hook = "TasForWp::tas_wp_print_scripts"; 28 29 public static $admin_print_scripts_hook = "TasForWp::tas_admin_print_scripts"; 29 30 public static $admin_print_styles_hook = "TasForWp::tas_admin_print_styles"; 31 public static $search_ajax_hook = "TasForWp::tas_search_ajax"; 32 30 33 public static $status_by_id_shortcode = "TasForWp::twitter_status_by_id_func"; 31 34 public static $search_shortcode = "TasForWp::twitter_search_func"; … … 114 117 } 115 118 119 public static function tas_wp_print_scripts() 120 { 121 self::getInstance()->tas_wp_print_scripts_impl(); 122 } 123 124 private function tas_wp_print_scripts_impl() { 125 // TODO: We're dynamically loading the template, but not the CSS, need to look for an alternative css here as well, 126 // or simply ignore ours if a non standard template is used. 127 // Tweet Styles 128 $url = WP_PLUGIN_URL . '/twitter-api-shortcodes/js/tas-search.js'; 129 $file = WP_PLUGIN_DIR . '/twitter-api-shortcodes/js/tas-search.js'; 130 if(file_exists($file)) { 131 wp_register_script('tas_search_script', $url); 132 wp_enqueue_script('tas_search_script'); 133 134 wp_localize_script('tas_search_script', 'tas_search_script', array('ajaxurl' => admin_url('admin-ajax.php'))); 135 } 136 } 137 116 138 // TODO: Need to add a schedule to update author avatar URL's on cached statuses. Also need to add deactivation. 117 139 public static function tas_install() … … 202 224 203 225 update_option('tas_last_cron', time()); 226 } 227 228 public static function tas_search_ajax() 229 { 230 self::getInstance()->tas_search_ajax_impl(); 231 } 232 233 private function tas_search_ajax_impl() 234 { 235 // TODO: There's some work to be done here, mostly making the TwitterSearch class better. 236 $infoObj = (object)$_REQUEST['info_str']; 237 238 $search = new TwitterSearch($infoObj->id, $this->_wpdb, null); 239 $search->page = $infoObj->page == 'null' ? null : $infoObj->page; 240 $search->max_status_id = $infoObj->max_status_id == 'null' ? null : $infoObj->max_status_id; 241 $search->limit = $infoObj->limit; 242 $search->paging = $infoObj->paging; 243 244 $returnObject = new stdClass(); 245 $returnObject->statuses = ''; 246 247 $statusAry = $search->getStatuses(); 248 249 foreach($statusAry as $status) 250 { 251 $returnObject->statuses .= $this->formatStatus($status); 252 } 253 254 $infoObj->max_status_id = $statusAry[0]->id_str; 255 if(!isset($search->page)) { $infoObj->page = 1; } 256 257 if($search->older_statuses_available()) { 258 $returnObject->next_link = $this->searchNextButton(); 259 } 260 if(!$search->archive) { 261 $returnObject->refresh_link = $this->searchRefreshButton(); 262 } 263 $returnObject->info_obj = $infoObj; 264 265 print strval(jsonGenderBender($returnObject, 'string')); 266 exit; 204 267 } 205 268 … … 485 548 public function formatSearch(TwitterSearch $search) 486 549 { 487 $retVal = '<div class="tweet-list">'; 550 $thisDivGuid = sprintf("tas_search_%s", uniqid()); 551 $retVal = sprintf('<div class="tweet-list" id="%s">', $thisDivGuid); 488 552 $statuses = $search->getStatuses(); 553 $max_status_id = $statuses[0]->id_str; 489 554 if (is_array($statuses)) { 490 555 foreach ($statuses as $status) { … … 493 558 } 494 559 if($search->paging) { 495 $retVal .= "<a href='#'>Next Page</a>"; 560 $dataObj = new stdClass(); 561 $dataObj->id = $search->getId(); 562 $dataObj->max_status_id = $max_status_id; 563 $dataObj->limit = $search->limit; 564 $dataObj->div_guid = $thisDivGuid; 565 $dataObj->paging = $search->paging; 566 $dataJson = jsonGenderBender($dataObj, 'string'); 567 if($search->older_statuses_available()) 568 { 569 //$retVal .= "<a href='' class='tas_search_next' data='" . $dataJson ."'>Older Tweets</a>"; 570 $retVal .= $this->searchNextButton($dataJson); 571 } 572 if(!$search->archive) 573 { 574 $retVal .= $this->searchRefreshButton($dataJson); 575 } 496 576 } 497 577 $retVal .= '</div>'; 498 578 return $retVal; 499 579 } 580 581 private function searchNextButton($dataJson=null) 582 { 583 return sprintf("<input type='button' class='tas_search_next' data='%s' value='Older Tweets'/>", $dataJson); 584 } 585 586 private function searchRefreshButton($dataJson=null) 587 { 588 return sprintf("<input type='button' class='tas_search_refresh' data='%s' value='Refresh Search' />", $dataJson); 589 } 500 590 } TasForWp::getInstance(); -
twitter-api-shortcodes/trunk/libs/twitter.api.wp.class.inc.php
r372245 r377014 3 3 require_once(ABSPATH.'wp-content/plugins/twitter-api-shortcodes/libs/jmathai-twitter-async/EpiOAuth.php'); 4 4 require_once(ABSPATH.'wp-content/plugins/twitter-api-shortcodes/libs/jmathai-twitter-async/EpiTwitter.php'); 5 6 7 //require_once(ABSPATH.'wp-content/plugins/twitter-api-shortcodes/libs/twitteroauth/twitteroauth/twitteroauth.php'); 5 8 6 9 // Support for WP 3.x … … 35 38 ******************************************************************************************/ 36 39 40 /** 41 * http://apiwiki.twitter.com/Twitter-Search-API-Method:-search 42 * @param $paramAry 43 * @return bool 44 */ 37 45 public function search($paramAry) { 38 $twitterApi = new EpiTwitter(); 39 $response = $twitterApi->get_search($paramAry); 40 41 return $response; 46 // TODO: Maybe we ought to make this a public class var, so we can override it in tests. 47 $wp_http = new WP_Http(); 48 $request = 'http://search.twitter.com/search.json?'; 49 foreach($paramAry as $idx => $param) { 50 $request .= $idx.'='.urlencode($param).'&'; 51 } 52 $response = $wp_http->request($request); 53 return $this->_requestFailed($response) ? false : json_decode($response['body']); 42 54 } 43 55 … … 91 103 ); 92 104 } 105 106 /****************************************************************************************** 107 * Private helpers 108 ******************************************************************************************/ 109 110 private function _requestFailed($wpErrorOrResponse) { 111 $retVal = (is_wp_error($wpErrorOrResponse)|| 112 !$wpErrorOrResponse || 113 !$wpErrorOrResponse['response'] || 114 !$wpErrorOrResponse['response']['code'] || 115 $wpErrorOrResponse['response']['code'] != 200); 116 if(!$retVal) { 117 unset($this->last_error); 118 $this->last_error->response = $wpErrorOrResponse['response']; 119 if(is_wp_error($wpErrorOrResponse)) { $this->last_error->wp_error = $wpErrorOrResponse; } 120 } 121 return $retVal; 122 } 93 123 } 94 124 95 //// Support for WP 3.x 96 //if(!class_exists('WP_Http')) { 97 // require_once(ABSPATH.WPINC.'/class-http.php'); 98 //} 99 // 125 100 126 //// TODO: Still need to add error handling into this! 101 127 //class TwitterAPIWP { … … 109 135 // } 110 136 // 111 // private function _requestFailed($wpErrorOrResponse) {112 // $retVal = (is_wp_error($wpErrorOrResponse)||113 // !$wpErrorOrResponse ||114 // !$wpErrorOrResponse['response'] ||115 // !$wpErrorOrResponse['response']['code'] ||116 // $wpErrorOrResponse['response']['code'] != 200);117 // if(!$retVal) {118 // unset($this->last_error);119 // $this->last_error->response = $wpErrorOrResponse['response'];120 // if(is_wp_error($wpErrorOrResponse)) { $this->last_error->wp_error = $wpErrorOrResponse; }121 // }122 // return $retVal;123 // }124 137 // 125 138 // /** … … 132 145 // } 133 146 // 134 // /**135 // * http://apiwiki.twitter.com/Twitter-Search-API-Method:-search136 // * @param $paramAry137 // * @return bool138 // */139 // public function search($paramAry) {140 // $request = 'http://search.twitter.com/search.json?';141 // foreach($paramAry as $idx => $param) {142 // $request .= $idx.'='.$param.'&';143 // }144 // $response = $this->wp_http->request($request);145 // return $this->_requestFailed($response) ? false : $response['body'];146 // }147 147 // 148 148 // /** -
twitter-api-shortcodes/trunk/libs/twitter.search.class.inc.php
r373349 r377014 5 5 6 6 public $paging = false; 7 public $limit = 0;7 public $limit = 15; 8 8 9 9 public $term = ''; … … 13 13 private $_statuses = array(); 14 14 private $_id = 0; 15 16 private $_nextPage = null; 17 18 public function getId() 19 { 20 return $this->_id; 21 } 15 22 16 23 public function __construct($id, $wpdb, $tapi=null) … … 44 51 // TODO: Should/can we specify a larger rpp? 45 52 $params = array('q' => $this->term, 'rpp' => 100); 53 if(isset($latestStatusIdCached)) $params['since_id'] = $latestStatusIdCached; 46 54 } 47 $response = $this->tapi->search($params); 55 try { 56 $response = $this->tapi->search($params); 57 } catch (Exception $e) { 58 error_log(print_r($e, true)); 59 } 48 60 49 61 foreach ($response->results as $status) { 50 if (strval($status->id) != $latestStatusIdCached) { 51 $status_obj = new TwitterStatus($this->_wpdb, $this->tapi); 52 $status_obj->load_json($status); 53 $status_obj->cacheToDb(); 62 $status_obj = new TwitterStatus($this->_wpdb, $this->tapi); 63 $status_obj->load_json($status); 64 $status_obj->cacheToDb(); 54 65 55 $this->_wpdb->insert(TasForWp::$StatusSearchTableName, 56 array( 57 'status_id' => $status_obj->id_str, 58 'search_id' => $this->_id 59 ) 60 ); 61 $this->_statuses[] = $status_obj; 62 } else { 63 break 2; 64 } 66 $this->_wpdb->insert(TasForWp::$StatusSearchTableName, 67 array( 68 'status_id' => $status_obj->id_str, 69 'search_id' => $this->_id 70 ) 71 ); 72 $this->_statuses[] = $status_obj; 65 73 } 66 74 … … 72 80 } 73 81 82 public function older_statuses_available() 83 { 84 return isset($this->_nextPage); 85 } 86 74 87 private function fetchStatuses() 75 88 { 76 $jsonObjs = array();77 78 89 // We're only going to look in the DB for the statuses associated with this search 79 90 // if the tag indicates that we should archive the statuses, it's a waste of an SQL 80 91 // call otherwise. 81 92 if ($this->archive) { 93 $limit_clause = " LIMIT "; 94 if($this->paging) { 95 $limit_clause .= ($this->page * $this->limit).","; 96 } 97 $limit_clause .= $this->limit; 98 82 99 $query = sprintf("SELECT * FROM `%s` WHERE id IN (SELECT status_id FROM `%s` WHERE search_id = %s)%s", 83 100 TasForWp::$StatusByIdTableName, 84 101 TasForWp::$StatusSearchTableName, 85 102 $this->_id, 86 $ this->limit > 0 ? "LIMIT $this->limit" : ""103 $limit_clause 87 104 ); 88 105 $rows = $this->_wpdb->get_results($query); 106 $this->_nextPage = (count($rows) < $this->limit) ? null : true; 89 107 foreach ($rows as $row) { 90 array_push($jsonObjs, json_decode($row->status_json)); 108 $status_obj = new TwitterStatus($row->id); 109 $status_obj->load_json($row->status_json); 110 $this->_statuses[] = $status_obj; 91 111 } 92 112 } else { … … 96 116 $params['rpp'] = $this->limit; 97 117 } 118 if(isset($this->page)) { 119 $params['page'] = $this->page; 120 } 121 if(isset($this->max_status_id)) { 122 $params['max_id'] = $this->max_status_id; 123 } 98 124 $response = $this->tapi->search($params); 99 #$jsonObjs = $response->results; 125 126 $this->_nextPage = $response->next_page; 100 127 101 128 foreach($response->results as $result) … … 122 149 private function getMaxStatusId() 123 150 { 124 return $this->_wpdb->get_var("SELECT max(status_id) FROM `".TasForWp::$Status ByIdTableName."` WHERE search_id = $this->_id");151 return $this->_wpdb->get_var("SELECT max(status_id) FROM `".TasForWp::$StatusSearchTableName."` WHERE search_id = $this->_id"); 125 152 } 126 153 -
twitter-api-shortcodes/trunk/tests/phpunit.xml
r372245 r377014 1 <phpunit >1 <phpunit bootstrap="bootstrap.php" backupGlobals="false" backupStaticAttributes="false" colors="true"> 2 2 <logging> 3 <log type="coverage-html" target="./reports" charset="UTF-8" /> 3 <log type="coverage-clover" target="./test-output/unit-clover.xml"/> 4 <log type="junit" target="./test-output/unit-junit.xml" /> 4 5 </logging> 6 <testsuites> 7 <testsuite name="unit"> 8 <directory>unit</directory> 9 </testsuite> 10 </testsuites> 5 11 </phpunit> -
twitter-api-shortcodes/trunk/tests/wp-integration/functionsTest.php
r373349 r377014 1 1 <?php 2 2 class FunctionsTest extends WPTestCase { 3 //class FunctionsTest extends PHPUnit_Framework_TestCase { 3 4 protected $jsonStr = <<<EOF 4 5 {"menu": { … … 35 36 $this->assertTrue($obj == $this->jsonObj); 36 37 } 37 38 /*public function testStatusNormalizer() {39 $searchJson = file_get_contents(DIR_TESTDATA.'/twitter-api-shortcodes/one-search-result.json');40 41 $getJson = <<<EOF42 43 EOF;44 45 $normSearchObj = jsonGenderBender($searchJson);46 $this->assertNull($normSearchObj->user);47 $this->assertNull($normSearchObj->created_at_ts);48 $this->assertTrue($normSearchObj->source == htmlspecialchars($normSearchObj->source,ENT_COMPAT,"ISO-8859-1",false));49 normalizeStatus($normSearchObj);50 $this->assertNotNull($normSearchObj->user);51 $this->assertNotNull($normSearchObj->created_at_ts);52 $this->assertTrue($normSearchObj->source != htmlspecialchars($normSearchObj->source,ENT_COMPAT,"ISO-8859-1",false));53 }*/54 38 } -
twitter-api-shortcodes/trunk/tests/wp-integration/tasforwpTest.php
r373349 r377014 1 1 <?php 2 class TasForWPUnitTests extends WPTestCase { 2 class TasForWPTest extends WPTestCase { 3 //class TasForWPTest extends PHPUnit_Framework_TestCase { 3 4 public static function print_wp_die($message) 4 5 { … … 8 9 public static function print_wp_die_hook($function) 9 10 { 10 return "TasForWP UnitTests::print_wp_die";11 return "TasForWPTest::print_wp_die"; 11 12 } 12 13 … … 241 242 { 242 243 wp_set_current_user(0); 243 add_filter('wp_die_handler', array('TasForWP UnitTests','print_wp_die_hook'));244 add_filter('wp_die_handler', array('TasForWPTest','print_wp_die_hook')); 244 245 ob_start(); 245 246 TasForWp::tas_admin_options(); … … 248 249 $constraint = $this->stringContains('You know what you did wrong naughty boy! Go to your room!'); 249 250 $this->assertThat($result, $constraint, $result); 250 remove_filter('wp_die_handler', array('TasForWP UnitTests','print_wp_die_hook'));251 remove_filter('wp_die_handler', array('TasForWPTest','print_wp_die_hook')); 251 252 } 252 253 -
twitter-api-shortcodes/trunk/twitter-api-shortcodes.php
r373349 r377014 42 42 add_action('admin_menu', TasForWp::$admin_menu_hook); 43 43 add_action('wp_print_styles', TasForWp::$wp_print_styles_hook); 44 add_action('wp_print_scripts', TasForWp::$wp_print_scripts_hook); 44 45 add_action('admin_print_scripts', TasForWp::$admin_print_scripts_hook); 45 46 add_action('admin_print_styles', TasForWp::$admin_print_styles_hook); … … 49 50 50 51 add_action('init', 'TasForWp::tas_add_tinymce_buttons_action'); 52 53 add_action('wp_ajax_nopriv_tas_search', TasForWp::$search_ajax_hook); 54 add_action('wp_ajax_tas_search', TasForWp::$search_ajax_hook);
Note: See TracChangeset
for help on using the changeset viewer.