scrape the ape three : sidebar zoo

“Scrape the Ape” plugin.

i ripped the widget code framework from Marcel Proulx http://www.district30.net and changed it a bit so it runs my Ape-Scrape. I’ll break my brains on the specifics of widget-codes later.

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

do not expect to see this on wordpress.org ;) when i figure out how the widget code works i’ll make something proper.

Posted in php, wordpress and tagged , , , .

One Comment

  1. Pingback: marcel proulx

Leave a Reply

Your email address will not be published. Required fields are marked *