Plugin Directory

Changeset 544246


Ignore:
Timestamp:
05/14/2012 08:51:12 PM (14 years ago)
Author:
golddave
Message:
 
Location:
mlb-standings/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mlb-standings/trunk/MLBStandings.php

    r540932 r544246  
    1212Change Log
    1313
     141.1
     15  * Added option to highlight team in standings.
     16  * Added AJAX menu for team selection. Only teams from the selected division will be available in the team select box.
     17  * Rewrote settings page to better conform to the Wordpress settings API.
     18  * Refactored code to remove unnecesary settings and variables.
     19  * Added link to settings page to the MLB Standings listing on the plugin page.
     20  * Changed download function to use WP_Http API eliminating dependencay on cURL.
     21  * Now saving XML to the database instead of downloading eliminating dependancy on file system.
     22  * Now using WP Transients API calls to cache the standings XML data instead of using a custome function.
     23 
    14241.0
    1525  * First public release.
     
    1828function ShowMLBStandings() {
    1929    $options = get_option('MLBStandings_options');
    20     $filename = dirname(__FILE__)."/standings.xml";
    21    
    22     $date = date('h', time());
    23    
    24     if (($date !== $options['hour']) || (!file_exists($filename))) {
    25         update_option($options['hour'], $date);
    26        
    27         $sourcefile = "http://erikberg.com/mlb/standings.xml";
    28        
    29         if (!download2($sourcefile, $filename)) {
    30             echo "failed to copy $sourcefile...\n";
    31         }
    32     }
    33    
    34     $xml = simplexml_load_file($filename);
     30    if (!download2()) {
     31        echo "failed to copy $sourcefile...\n";
     32    }
     33   
     34//  $filename = dirname(__FILE__)."/standings.xml";
     35// 
     36//  $date = date('h', time());
     37// 
     38//  $transient = get_transient("standingsxml");
     39// 
     40//  if (!$transient) {
     41//      if (download2($sourcefile, $filename)) {
     42//          set_transient("standingsxml", $filename, 60*5);
     43//      }
     44//  }
     45// 
     46//  $transient = $filename;
     47   
     48//  $xml = get_transient( $transient );
     49// 
     50//  if ( false === $transient || '' === $transient ){
     51//      if (false === $transient) {
     52//          echo "false<br>";
     53//      }
     54//      if ('' === $transient) {
     55//          echo "empty<br>";
     56//      }
     57//      echo "ran<br>";
     58//      echo $options['team']."<br>";
     59//      if (!download2($sourcefile, $filename)) {
     60//          echo "failed to copy $sourcefile...\n";
     61//      }
     62//      set_transient( $transient, $xml, 120 );
     63//  }
     64
     65   
     66//  if (($date !== $options['hour']) || (!file_exists($filename))) {
     67//      update_option($options['hour'], $date);
     68//      echo "ran<br>";
     69//     
     70//      $sourcefile = "http://erikberg.com/mlb/standings.xml";
     71//     
     72//      if (!download2($sourcefile, $filename)) {
     73//          echo "failed to copy $sourcefile...\n";
     74//      }
     75//  }
     76   
     77    //$xml = simplexml_load_file($filename);
     78    $xml = simplexml_load_string($options['xml']);
    3579   
    3680    $type = $xml->xpath("//standing/standing-metadata/sports-content-codes/sports-content-code/@code-type");
     
    91135        $arr = array(   "division" => "MLB-NL-E",
    92136                        "team" => "Mets",
    93                         "hour" => date('h', time()));
     137//                      "hour" => date('h', time()),
     138//                      "timestamp" => "Wed, 9 May 2012 11:05:11 GMT",
     139                        "xml" => "" );
    94140        update_option('MLBStandings_options', $arr);
    95141    }
     142//  if (!download2()) {
     143//      echo "failed to copy $sourcefile...\n";
     144//  }
    96145}
    97146
     
    106155// Init plugin options to white list our options
    107156function MLBStandings_init(){
     157//  register_setting( 'MLBStandings_plugin_options', 'MLBStandings_options', 'validate' );
    108158    register_setting( 'MLBStandings_plugin_options', 'MLBStandings_options' );
    109159}
     
    142192        <form method="post" action="options.php">
    143193            <?php settings_fields('MLBStandings_plugin_options'); ?>
     194            <?php if (!download2()) {echo "failed to copy $sourcefile...\n"; }; ?>
    144195            <?php $options = get_option('MLBStandings_options'); ?>
    145196
     
    165216                    <th scope="row">Team</th>
    166217                    <td>
    167                         <select name='MLBStandings_options[team]' id="teams" class="teams">
     218                        <select name='MLBStandings_options[team]' id="myteam">
    168219                        </select>
    169220                        <span style="color:#666666;margin-left:2px;">Select the team you'd like bolded in the standings.</span>
     
    171222                </tr>
    172223            </table>
    173             <input type="hidden" name='MLBStandings_options[hour]' value=<?php echo date('h', time()); ?>>
     224            <input type="hidden" name='MLBStandings_options[xml]' value=<?php substr($options['xml'],0,strlen($options['xml'])); ?>>
    174225            <p class="submit">
    175226            <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
     
    180231    <script type='text/javascript'>
    181232        function teamchanger() {
    182             jQuery("#teams").empty()
     233            jQuery("#myteam").empty()
    183234            switch(jQuery("#mydiv").val()) {
    184235                case "MLB-AL-E":
    185                     jQuery("#teams").append("<option value='Orioles' <?php selected('Orioles', $options['team']); ?>>Baltimore Orioles</option><option value='Red Sox' <?php selected('Red Sox', $options['team']); ?>>Boston Red Sox</option><option value='Yankees' <?php selected('Yankees', $options['team']); ?>>New York Yankees</option><option value='Rays' <?php selected('Rays', $options['team']); ?>>Tampa Bay Rays</option><option value='Blue Jays' <?php selected('Blue Jays', $options['team']); ?>>Toronto Blue Jays</option>");
     236                    jQuery("#myteam").append("<option value='Orioles' <?php selected('Orioles', $options['team']); ?>>Baltimore Orioles</option><option value='Red Sox' <?php selected('Red Sox', $options['team']); ?>>Boston Red Sox</option><option value='Yankees' <?php selected('Yankees', $options['team']); ?>>New York Yankees</option><option value='Rays' <?php selected('Rays', $options['team']); ?>>Tampa Bay Rays</option><option value='Blue Jays' <?php selected('Blue Jays', $options['team']); ?>>Toronto Blue Jays</option>");
    186237                    break;
    187238                case "MLB-AL-C":
    188                     jQuery("#teams").append("<option value ='White Sox' <?php selected('White Sox', $options['team']); ?>>Chicago White Sox</option><option value ='Indians' <?php selected('Indians', $options['team']); ?>>Cleveland Indians</option><option value ='Tigers' <?php selected('Tigers', $options['team']); ?>>Detroit Tigers</option><option value ='Royals' <?php selected('Royals', $options['team']); ?>>Kansas City Royals</option><option value ='Twins' <?php selected('Twins', $options['team']); ?>>Minnesota Twins</option>");
     239                    jQuery("#myteam").append("<option value ='White Sox' <?php selected('White Sox', $options['team']); ?>>Chicago White Sox</option><option value ='Indians' <?php selected('Indians', $options['team']); ?>>Cleveland Indians</option><option value ='Tigers' <?php selected('Tigers', $options['team']); ?>>Detroit Tigers</option><option value ='Royals' <?php selected('Royals', $options['team']); ?>>Kansas City Royals</option><option value ='Twins' <?php selected('Twins', $options['team']); ?>>Minnesota Twins</option>");
    189240                    break;
    190241                case "MLB-AL-W":
    191                     jQuery("#teams").append("<option value ='Angels' <?php selected('Angels', $options['team']); ?>>Los Angeles Angels</option><option value ='Athletics' <?php selected('Athletics', $options['team']); ?>>Oakland Athletics</option><option value ='Mariners' <?php selected('Mariners', $options['team']); ?>>Seattle Mariners</option><option value ='Rangers' <?php selected('Rangers', $options['team']); ?>>Texas Rangers</option>");
     242                    jQuery("#myteam").append("<option value ='Angels' <?php selected('Angels', $options['team']); ?>>Los Angeles Angels</option><option value ='Athletics' <?php selected('Athletics', $options['team']); ?>>Oakland Athletics</option><option value ='Mariners' <?php selected('Mariners', $options['team']); ?>>Seattle Mariners</option><option value ='Rangers' <?php selected('Rangers', $options['team']); ?>>Texas Rangers</option>");
    192243                    break;
    193244                case "MLB-NL-E":
    194                     jQuery("#teams").append("<option value ='Braves' <?php selected('Braves', $options['team']); ?>>Atlanta Braves</option><option value ='Marlins' <?php selected('Marlins', $options['team']); ?>>Miami Marlins</option><option value ='Mets' <?php selected('Mets', $options['team']); ?>>New York Mets</option><option value ='Phillies' <?php selected('Phillies', $options['team']); ?>>Philadelphia Phillies</option><option value ='Nationals' <?php selected('Nationals', $options['team']); ?>>Washington Nationals</option>");
     245                    jQuery("#myteam").append("<option value ='Braves' <?php selected('Braves', $options['team']); ?>>Atlanta Braves</option><option value ='Marlins' <?php selected('Marlins', $options['team']); ?>>Miami Marlins</option><option value ='Mets' <?php selected('Mets', $options['team']); ?>>New York Mets</option><option value ='Phillies' <?php selected('Phillies', $options['team']); ?>>Philadelphia Phillies</option><option value ='Nationals' <?php selected('Nationals', $options['team']); ?>>Washington Nationals</option>");
    195246                    break;
    196247                case "MLB-NL-C":
    197                     jQuery("#teams").append("<option value ='Cubs' <?php selected('Cubs', $options['team']); ?>>Chicago Cubs</option><option value ='Reds' <?php selected('Reds', $options['team']); ?>>Cincinnati Reds</option><option value ='Astros' <?php selected('Astros', $options['team']); ?>>Houston Astros</option><option value ='Brewers' <?php selected('Brewers', $options['team']); ?>>Milwaukee Brewers</option><option value ='Pirates' <?php selected('Pirates', $options['team']); ?>>Pittsburgh Pirates</option><option value ='Cardinals' <?php selected('Cardinals', $options['team']); ?>>St. Louis Cardinals</option>");
     248                    jQuery("#myteam").append("<option value ='Cubs' <?php selected('Cubs', $options['team']); ?>>Chicago Cubs</option><option value ='Reds' <?php selected('Reds', $options['team']); ?>>Cincinnati Reds</option><option value ='Astros' <?php selected('Astros', $options['team']); ?>>Houston Astros</option><option value ='Brewers' <?php selected('Brewers', $options['team']); ?>>Milwaukee Brewers</option><option value ='Pirates' <?php selected('Pirates', $options['team']); ?>>Pittsburgh Pirates</option><option value ='Cardinals' <?php selected('Cardinals', $options['team']); ?>>St. Louis Cardinals</option>");
    198249                    break;
    199250                case "MLB-NL-W":
    200                     jQuery("#teams").append("<option value ='Diamondbacks' <?php selected('Diamondbacks', $options['team']); ?>>Arizona Diamondbacks</option><option value ='Rockies' <?php selected('Rockies', $options['team']); ?>>Colorado Rockies</option><option value ='Dodgers' <?php selected('Dodgers', $options['team']); ?>>Los Angeles Dodgers</option><option value ='Padres' <?php selected('Padres', $options['team']); ?>>San Diego Padres</option><option value ='Giants' <?php selected('Giants', $options['team']); ?>>San Francisco Giants</option>");
     251                    jQuery("#myteam").append("<option value ='Diamondbacks' <?php selected('Diamondbacks', $options['team']); ?>>Arizona Diamondbacks</option><option value ='Rockies' <?php selected('Rockies', $options['team']); ?>>Colorado Rockies</option><option value ='Dodgers' <?php selected('Dodgers', $options['team']); ?>>Los Angeles Dodgers</option><option value ='Padres' <?php selected('Padres', $options['team']); ?>>San Diego Padres</option><option value ='Giants' <?php selected('Giants', $options['team']); ?>>San Francisco Giants</option>");
    201252                    break;
    202253            }
    203254        }
    204        
    205255        jQuery(document).ready(function() {
    206256            teamchanger()
     
    210260        });
    211261    </script>
    212 
    213 
    214262    <?php   
    215263}
     264
     265//function validate($input) {
     266//  if (strlen($input['xml']) < 5000) {
     267//      delete_transient( "standingsxml" );
     268//      if (!download2()) {
     269//          echo "failed to copy $sourcefile...\n";
     270//      }
     271//  }   
     272//  return $input;
     273//}
    216274
    217275// Display a Settings link on the main Plugins page
     
    227285}
    228286
    229 
    230 
    231 
    232 
    233 
    234 
    235 
    236 
    237 
    238 
    239 function download2($sourcefile, $filename) {
    240     // create a new curl resource
    241     $ch = curl_init();
    242    
    243     // set URL and other appropriate options
    244     curl_setopt($ch, CURLOPT_URL, $sourcefile);
    245     curl_setopt($ch, CURLOPT_USERAGENT, 'MLBStandings; (http://golddave.com/)');
    246     curl_setopt($ch, CURLOPT_REFERER, get_bloginfo('url'));
    247     curl_setopt($ch, CURLOPT_HEADER, false);
    248     curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    249     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    250    
    251     set_time_limit(300); # 5 minutes for PHP
    252     curl_setopt($ch, CURLOPT_TIMEOUT, 300); # and also for CURL
    253    
    254     $outfile = fopen($filename, 'wb');
    255     curl_setopt($ch, CURLOPT_FILE, $outfile);
    256    
    257     // grab file from URL
    258     curl_exec($ch);
    259     fclose($outfile);
    260    
    261     // close CURL resource, and free up system resources
    262     curl_close($ch);
    263     sleep(10);
     287function download2() {
     288    $options = get_option('MLBStandings_options');
     289    //delete_transient( "standingsxml" );
     290    $transient = get_transient("standingsxml");
     291   
     292    if ((!$transient) || (!$options['xml']) || (strlen($options['xml'])<5000)) {
     293    //if (!$transient) {
     294        //echo "download needed";
     295        //Include WP_Http class
     296        if( !class_exists( 'WP_Http' ) ) include_once( ABSPATH . WPINC. '/class-http.php' );
     297       
     298        //Set source and destination files
     299        $url = "http://erikberg.com/mlb/standings.xml";
     300        $filename = dirname(__FILE__)."/standings.xml";
     301       
     302        //Instantiate the request
     303        $request = new WP_Http;
     304       
     305        //Add parameters for request
     306        $args = array();
     307        $args['useragent'] = 'MLBStandings; (http://golddave.com/)';
     308        $args['referer'] = get_bloginfo('url');
     309        //$args['header'] = false;
     310        //$args['binarytransfer'] = true;
     311        //$args['returntransfer'] = true;
     312        $args['timeout'] =  300;
     313        //open destination file and add to parameters
     314//      $outfile = fopen($filename, 'wb');
     315        //$args['file'] = $outfile;
     316       
     317        //Execute request
     318        $result = $request->request($url, $args);
     319        //$body = $result[body];
     320       
     321        //Set XML option to the body of our request
     322        if ( $options['xml'] != $result[body] ) {
     323            $options['xml'] = $result[body];
     324            update_option('MLBStandings_options', $options);
     325        }
     326       
     327        set_transient("standingsxml", $filename, 60*60);
     328       
     329        //write the body of the file into the destination file and close
     330//      fwrite($outfile, $result[body]);
     331//      fclose($outfile);
     332       
     333        //Set XML option to the body of our request
     334        //  update_option($options['xml'], $result[body]);
     335       
     336        //sleep(10);
     337    }
     338   
     339    $transient = $filename;
     340
     341    //return true so the calling function doesn't return an error
    264342    return true;
    265343}
     344
     345//  // create a new curl resource
     346//  $ch = curl_init();
     347// 
     348//  // set URL and other appropriate options
     349//  curl_setopt($ch, CURLOPT_URL, $sourcefile);
     350//  curl_setopt($ch, CURLOPT_USERAGENT, 'MLBStandings; (http://golddave.com/)');
     351//  curl_setopt($ch, CURLOPT_REFERER, get_bloginfo('url'));
     352//  curl_setopt($ch, CURLOPT_HEADER, false);
     353//  curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
     354//  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     355// 
     356//  set_time_limit(300); # 5 minutes for PHP
     357//  curl_setopt($ch, CURLOPT_TIMEOUT, 300); # and also for CURL
     358// 
     359//  $outfile = fopen($filename, 'wb');
     360//  curl_setopt($ch, CURLOPT_FILE, $outfile);
     361// 
     362//  // grab file from URL
     363//  curl_exec($ch);
     364//  fclose($outfile);
     365// 
     366//  // close CURL resource, and free up system resources
     367//  curl_close($ch);
     368//  sleep(10);
     369//  return true;
     370//}
    266371
    267372class MLBStandings_Widget extends WP_Widget {
  • mlb-standings/trunk/standings.css

    r540932 r544246  
    77/* Standings body class. */
    88#mlb_standings_body table {
    9     width: 100%;
     9    width: 10%;
    1010    padding: 2px;
    1111    border-collapse: collapse; 
Note: See TracChangeset for help on using the changeset viewer.