Plugin Directory

Changeset 377014


Ignore:
Timestamp:
04/25/2011 06:45:04 PM (15 years ago)
Author:
rgeyer
Message:

Released to Thomas with paging

Location:
twitter-api-shortcodes/trunk
Files:
6 added
2 deleted
5 edited
3 moved

Legend:

Unmodified
Added
Removed
  • twitter-api-shortcodes/trunk/TODO.txt

    r322494 r377014  
    1212SEARCH
    1313- 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.
    1514
    1615MISC
  • twitter-api-shortcodes/trunk/libs/tasforwp.class.inc.php

    r373349 r377014  
    2626  public static $admin_menu_hook          = "TasForWp::tas_admin_menu";
    2727  public static $wp_print_styles_hook     = "TasForWp::tas_wp_print_styles";
     28  public static $wp_print_scripts_hook    = "TasForWp::tas_wp_print_scripts";
    2829  public static $admin_print_scripts_hook = "TasForWp::tas_admin_print_scripts";
    2930  public static $admin_print_styles_hook  = "TasForWp::tas_admin_print_styles";
     31  public static $search_ajax_hook         = "TasForWp::tas_search_ajax";
     32
    3033  public static $status_by_id_shortcode   = "TasForWp::twitter_status_by_id_func";
    3134  public static $search_shortcode         = "TasForWp::twitter_search_func";
     
    114117  }
    115118
     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
    116138  // TODO: Need to add a schedule to update author avatar URL's on cached statuses.  Also need to add deactivation.
    117139  public static function tas_install()
     
    202224
    203225    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;
    204267  }
    205268
     
    485548  public function formatSearch(TwitterSearch $search)
    486549  {
    487     $retVal = '<div class="tweet-list">';
     550    $thisDivGuid = sprintf("tas_search_%s", uniqid());
     551    $retVal = sprintf('<div class="tweet-list" id="%s">', $thisDivGuid);
    488552    $statuses = $search->getStatuses();
     553    $max_status_id = $statuses[0]->id_str;
    489554    if (is_array($statuses)) {
    490555      foreach ($statuses as $status) {
     
    493558    }
    494559    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      }
    496576    }
    497577    $retVal .= '</div>';
    498578    return $retVal;
    499579  }
     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  }
    500590} TasForWp::getInstance();
  • twitter-api-shortcodes/trunk/libs/twitter.api.wp.class.inc.php

    r372245 r377014  
    33require_once(ABSPATH.'wp-content/plugins/twitter-api-shortcodes/libs/jmathai-twitter-async/EpiOAuth.php');
    44require_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');
    58
    69// Support for WP 3.x
     
    3538   ******************************************************************************************/
    3639
     40  /**
     41   * http://apiwiki.twitter.com/Twitter-Search-API-Method:-search
     42   * @param  $paramAry
     43   * @return bool
     44   */
    3745  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']);
    4254  }
    4355
     
    91103    );
    92104  }
     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  }
    93123}
    94124
    95 //// Support for WP 3.x
    96 //if(!class_exists('WP_Http')) {
    97 //  require_once(ABSPATH.WPINC.'/class-http.php');
    98 //}
    99 //
     125
    100126//// TODO: Still need to add error handling into this!
    101127//class TwitterAPIWP {
     
    109135//  }
    110136//
    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 //  }
    124137//
    125138//  /**
     
    132145//  }
    133146//
    134 //  /**
    135 //   * http://apiwiki.twitter.com/Twitter-Search-API-Method:-search
    136 //   * @param  $paramAry
    137 //   * @return bool
    138 //   */
    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 //  }
    147147//
    148148//  /**
  • twitter-api-shortcodes/trunk/libs/twitter.search.class.inc.php

    r373349 r377014  
    55
    66  public $paging = false;
    7   public $limit = 0;
     7  public $limit = 15;
    88
    99  public $term = '';
     
    1313  private $_statuses = array();
    1414  private $_id = 0;
     15
     16  private $_nextPage = null;
     17
     18  public function getId()
     19  {
     20    return $this->_id;
     21  }
    1522
    1623  public function __construct($id, $wpdb, $tapi=null)
     
    4451            // TODO: Should/can we specify a larger rpp?
    4552            $params = array('q' => $this->term, 'rpp' => 100);
     53            if(isset($latestStatusIdCached)) $params['since_id'] = $latestStatusIdCached;
    4654          }
    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          }
    4860
    4961          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();
    5465
    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;
    6573          }
    6674
     
    7280  }
    7381
     82  public function older_statuses_available()
     83  {
     84    return isset($this->_nextPage);
     85  }
     86
    7487  private function fetchStatuses()
    7588  {
    76     $jsonObjs = array();
    77 
    7889    // We're only going to look in the DB for the statuses associated with this search
    7990    // if the tag indicates that we should archive the statuses, it's a waste of an SQL
    8091    // call otherwise.
    8192    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
    8299      $query = sprintf("SELECT * FROM `%s` WHERE id IN (SELECT status_id FROM `%s` WHERE search_id = %s)%s",
    83100        TasForWp::$StatusByIdTableName,
    84101        TasForWp::$StatusSearchTableName,
    85102        $this->_id,
    86         $this->limit > 0 ? "LIMIT $this->limit" : ""
     103        $limit_clause
    87104      );
    88105      $rows = $this->_wpdb->get_results($query);
     106      $this->_nextPage = (count($rows) < $this->limit) ? null : true;
    89107      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;
    91111      }
    92112    } else {
     
    96116          $params['rpp'] = $this->limit;
    97117        }
     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        }
    98124        $response = $this->tapi->search($params);
    99         #$jsonObjs = $response->results;
     125
     126        $this->_nextPage = $response->next_page;
    100127
    101128        foreach($response->results as $result)
     
    122149  private function getMaxStatusId()
    123150  {
    124     return $this->_wpdb->get_var("SELECT max(status_id) FROM `".TasForWp::$StatusByIdTableName."` WHERE search_id = $this->_id");
     151    return $this->_wpdb->get_var("SELECT max(status_id) FROM `".TasForWp::$StatusSearchTableName."` WHERE search_id = $this->_id");
    125152  }
    126153
  • twitter-api-shortcodes/trunk/tests/phpunit.xml

    r372245 r377014  
    1 <phpunit>
     1<phpunit bootstrap="bootstrap.php" backupGlobals="false" backupStaticAttributes="false" colors="true">
    22  <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" />
    45  </logging>
     6  <testsuites>
     7    <testsuite name="unit">
     8      <directory>unit</directory>
     9    </testsuite>
     10  </testsuites>
    511</phpunit>
  • twitter-api-shortcodes/trunk/tests/wp-integration/functionsTest.php

    r373349 r377014  
    11<?php
    22class FunctionsTest extends WPTestCase {
     3//class FunctionsTest extends PHPUnit_Framework_TestCase {
    34  protected $jsonStr = <<<EOF
    45{"menu": {
     
    3536    $this->assertTrue($obj == $this->jsonObj);
    3637  }
    37 
    38   /*public function testStatusNormalizer() {
    39     $searchJson = file_get_contents(DIR_TESTDATA.'/twitter-api-shortcodes/one-search-result.json');
    40 
    41     $getJson = <<<EOF
    42 
    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   }*/
    5438}
  • twitter-api-shortcodes/trunk/tests/wp-integration/tasforwpTest.php

    r373349 r377014  
    11<?php
    2 class TasForWPUnitTests extends WPTestCase {
     2class TasForWPTest extends WPTestCase {
     3//class TasForWPTest extends PHPUnit_Framework_TestCase {
    34  public static function print_wp_die($message)
    45  {
     
    89  public static function print_wp_die_hook($function)
    910  {
    10     return "TasForWPUnitTests::print_wp_die";
     11    return "TasForWPTest::print_wp_die";
    1112  }
    1213
     
    241242  {
    242243    wp_set_current_user(0);
    243     add_filter('wp_die_handler', array('TasForWPUnitTests','print_wp_die_hook'));
     244    add_filter('wp_die_handler', array('TasForWPTest','print_wp_die_hook'));
    244245    ob_start();
    245246    TasForWp::tas_admin_options();
     
    248249    $constraint = $this->stringContains('You know what you did wrong naughty boy! Go to your room!');
    249250    $this->assertThat($result, $constraint, $result);
    250     remove_filter('wp_die_handler', array('TasForWPUnitTests','print_wp_die_hook'));
     251    remove_filter('wp_die_handler', array('TasForWPTest','print_wp_die_hook'));
    251252  }
    252253
  • twitter-api-shortcodes/trunk/twitter-api-shortcodes.php

    r373349 r377014  
    4242add_action('admin_menu', TasForWp::$admin_menu_hook);
    4343add_action('wp_print_styles', TasForWp::$wp_print_styles_hook);
     44add_action('wp_print_scripts', TasForWp::$wp_print_scripts_hook);
    4445add_action('admin_print_scripts', TasForWp::$admin_print_scripts_hook);
    4546add_action('admin_print_styles', TasForWp::$admin_print_styles_hook);
     
    4950
    5051add_action('init', 'TasForWp::tas_add_tinymce_buttons_action');
     52
     53add_action('wp_ajax_nopriv_tas_search', TasForWp::$search_ajax_hook);
     54add_action('wp_ajax_tas_search', TasForWp::$search_ajax_hook);
Note: See TracChangeset for help on using the changeset viewer.