juust ~ php oddities

Unordered list of one element
  • rss
  • begin
  • about
    • vcard
    • WTF is BroJesus
  • php scripts
    • flickr wp widget
    • google multi key serp tool, php script
    • gwt plugin
  • php classes
    • php pagerank class
    • fibonacci class
    • robots.txt parser php class
  • serp
    • serp dashboard wordpress plugin
  • services

flickr wp widget

mijn eerste widget

I woke up this morning (19-7) and I reckoned ‘monkeys… are more fun than humans’ so I first programmed a quick flickr ape-scraper and then programmed one to put em in a grid, Then I thought man, I still have to make a wordpress plugin one of these days and monkey-plugins are fun, the rest sucks, so i turned it into a wordpress widget.

Ha! my first wordpress widget! monkeys can be fun.

  1.  
  2. /*
  3. Plugin Name: Scrape the Ape
  4. Plugin URI: http://juust.org/
  5. Description: How to scrape a zoo from Flickr onto the sidebar.
  6. Version: 0.1
  7. Author: Lord of Apes
  8. Author URI: http://juust.org/
  9. */
  10.  
  11. function disp_apes( $args, $widget_args = 1 ) {
  12.  extract( $args, EXTR_SKIP );
  13.  if ( is_numeric($widget_args) )
  14.   $widget_args = array( 'number' => $widget_args );
  15.  $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  16.  extract( $widget_args, EXTR_SKIP );
  17.  
  18.  // Data should be stored as array:  array( number => data for that instance of the widget, … )
  19.  $options = get_option('widget_apes');
  20.  if ( !isset($options[$number]) )
  21.   return;
  22.  
  23.  extract($options[$number], EXTR_SKIP);
  24.  
  25.  
  26.  $flikker = join("",file("http://api.flickr.com/services/feeds/photos_public.gne?tags=".$apes_tags."&format=rss"));
  27.  $flikkerhits = preg_split('/img src=&quot\;/', $flikker, -1, PREG_SPLIT_OFFSET_CAPTURE);
  28.  foreach($flikkerhits as $flikkerhit){
  29.      $i++;
  30.      if($i>1) $apes[]=substr($flikkerhit[0], 0, strpos($flikkerhit[0], 'width')-7);
  31.  }
  32.  $strgrid = "";
  33.  $currentcol=1;
  34.  $currentrow=1;
  35.  $columns=$apes_cols;
  36.  $rows=$apes_rows;
  37.  $strgrid .= "<div class=\"ApesZoo\"><table><tbody><tr>";    
  38.  for($a=0;$a<count ($apes);$a++) {
  39.      $strgrid .= "<td class=\"ApesCage\">";
  40.      $strgrid .= "<img src=\"".$apes[$a]."\" width=\"45px\" height=\"45px\" />";
  41.      $strgrid .= "";
  42.      $currentcol++;
  43.      if($currentcol>$columns) {
  44.   $currentcol=1;
  45.                 $strgrid .=  "</count></tr>";
  46.   $currentrow++;
  47.   if($currentrow>$rows) {
  48.      break;
  49.   } else {
  50.      $strgrid .=  "<tr>";
  51.   }
  52.      }
  53.         }
  54.  $strgrid .= "</tr></tbody></table></div>";
  55.  echo $strgrid;
  56.  
  57. }
  58.  
  59. function apes_control($widget_args) {
  60.  global $wp_registered_widgets;
  61.  static $updated = false; // Whether or not we have already updated the data after a POST submit
  62.  
  63.  if ( is_numeric($widget_args) )
  64.   $widget_args = array( 'number' => $widget_args );
  65.  $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  66.  extract( $widget_args, EXTR_SKIP );
  67.  
  68.  // Data should be stored as array:  array( number => data for that instance of the widget, … )
  69.  $options = get_option('widget_apes');
  70.  if ( !is_array($options) )
  71.   $options = array();
  72.  
  73.  // We need to update the data
  74.  if ( !$updated && !empty($_POST['sidebar']) ) {
  75.   // Tells us what sidebar to put the data in
  76.   $sidebar = (string) $_POST['sidebar'];
  77.  
  78.   $sidebars_widgets = wp_get_sidebars_widgets();
  79.   if ( isset($sidebars_widgets[$sidebar]) )
  80.    $this_sidebar =& $sidebars_widgets[$sidebar];
  81.   else
  82.    $this_sidebar = array();
  83.  
  84.   foreach ( $this_sidebar as $_widget_id ) {
  85.    // Remove all widgets of this type from the sidebar.  We'll add the new data in a second.  This makes sure we don't get any duplicate data
  86.    // since widget ids aren't necessarily persistent across multiple updates
  87.    if ( 'disp_apes' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
  88.     $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
  89.     if ( !in_array( "apes-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
  90.      unset($options[$widget_number]);
  91.    }
  92.   }
  93.  
  94.   foreach ( (array) $_POST['apes'] as $widget_number => $widget_apes_instance ) {
  95.    // compile data from $widget_random_image_instance
  96.    $apes_tags=strip_tags(stripslashes( $widget_apes_instance['apes_tags']));
  97.    $apes_rows=strip_tags(stripslashes( $widget_apes_instance['apes_rows']));
  98.    $apes_cols=strip_tags(stripslashes( $widget_apes_instance['apes_cols']));
  99.    $options[$widget_number] = compact('apes_tags', 'apes_cols', 'apes_rows');
  100.   }
  101.  
  102.   update_option('widget_apes', $options);
  103.  
  104.   $updated = true; // So that we don't go through this more than once
  105.  }
  106.  
  107.  
  108.  // Here we echo out the form
  109.  if ( -1 == $number ) {
  110.   $apes_tags = 'apes';
  111.   $apes_cols = 3;
  112.   $apes_rows = 2;
  113.   $number = '%i%';
  114.  }
  115.  else {
  116.   $apes_tags=attribute_escape($options[$number]['apes_tags']);
  117.   $apes_cols=attribute_escape($options[$number]['apes_cols']);
  118.   $apes_rows=attribute_escape($options[$number]['apes_rows']);
  119.  }
  120.  
  121.  // The form has inputs with names like widget-many[$number][something] so that all data for that instance of
  122.  // the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
  123. ?>
  124.  <p style="text-align:left; line-height=1.2em"><label for="apes-apes_tags-<?php echo $number; ?>">Tags:  <input style="width: 200px;" id="apes-apes_tags-<?php echo $number; ?/>" name="apes[< ?php echo $number; ?>][apes_tags]" type="text" value="< ?php echo $apes_tags; ?>" /></label></p>
  125.  <p style="text-align:left; line-height=1.2em"><label for="apes-apes_cols-<?php echo $number; ?>">Colums:  <input style="width: 200px;" id="apes-apes_cols-<?php echo $number; ?/>" name="apes[< ?php echo $number; ?>][apes_cols]" type="text" value="< ?php echo $apes_cols; ?>" /></label></p>
  126.  <p style="text-align:left; line-height=1.2em"><label for="apes-apes_rows-<?php echo $number; ?>">rows:  <input style="width: 200px;" id="apes-apes_rows-<?php echo $number; ?/>" name="apes[< ?php echo $number; ?>][apes_rows]" type="text" value="< ?php echo $apes_rows; ?>" /></label></p>
  127. < ?php
  128. }
  129.  
  130. /* Function: apes_register
  131. **
  132. ** Registers the apes widgets with the widget page
  133. **
  134. ** args: none
  135. ** returns: nothing
  136. */
  137.  
  138. function apes_register() {
  139.  if ( !$options = get_option('widget_apes') )
  140.   $options = array();
  141.  
  142.  $widget_ops = array('classname' => 'widget_apes', 'description' => __('Displays Flickr Apes'));
  143.  $control_ops = array('apes_cols' => 3, 'apes_rows' => 2, 'apes_tags' => 'apes', 'id_base' => 'apes');
  144.  $name = __('Scrape the Apes');
  145.  
  146.  $registered = false;
  147.  foreach ( array_keys($options) as $o ) {
  148.   // Old widgets can have null values for some reason
  149.   if ( !isset($options[$o]['apes_tags']) )
  150.    continue;
  151.  
  152.   // $id should look like {$id_base}-{$o}
  153.   $id = "apes-$o"; // Never never never translate an id
  154.   $registered = true;
  155.   wp_register_sidebar_widget( $id, $name, 'disp_apes', $widget_ops, array( 'number' => $o ) );
  156.   wp_register_widget_control( $id, $name, 'apes_control', $control_ops, array( 'number' => $o ) );
  157.  }
  158.  
  159.  // If there are none, we register the widget's existance with a generic template
  160.  if ( !$registered ) {
  161.   wp_register_sidebar_widget( 'apes-1', $name, 'disp_apes', $widget_ops, array( 'number' => -1 ) );
  162.   wp_register_widget_control( 'apes-1', $name, 'apes_control', $control_ops, array( 'number' => -1 ) );
  163.  }
  164. }
  165.  
  166. // This is important
  167. add_action( 'widgets_init', 'apes_register' )

enough monkey business.
related posts :


pt. I scrape flickr apes
pt. II cute apes
pt.III sidebar zoo

         
Tags
php, widget, wordpress
Comments rss
Comments rss
Trackback
Trackback

Leave a Reply

Click here to cancel reply.

Recent Posts

  • p2p with wordpress xml-rpc
  • Tweets on Google’s frontpage
  • happy new year
  • metaWeblog.newPost posting to Wordpress from Word
  • IE is retarded

click me!
rss
Comments rss
Blog Directory
Web Developement Blogs - BlogCatalog Blog Directory
Listed in LS Blogs the Blog Directory and Blog Search Engine
Blog Flux Directory
joopita.com free web directory and search engine
design by jide
sitemap
8298 confirmed spam kills