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.

/*
Plugin Name: Scrape the Ape
Plugin URI: https://juust.org/
Description: How to scrape a zoo from Flickr onto the sidebar.
Version: 0.1
Author: Lord of Apes
Author URI: https://juust.org/
*/

function disp_apes( $args, $widget_args = 1 ) {
	extract( $args, EXTR_SKIP );
	if ( is_numeric($widget_args) )
		$widget_args = array( 'number' => $widget_args );
	$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
	extract( $widget_args, EXTR_SKIP );

	// Data should be stored as array:  array( number => data for that instance of the widget, ... )
	$options = get_option('widget_apes');
	if ( !isset($options[$number]) )
		return;

	extract($options[$number], EXTR_SKIP);

//this is the scraper bit :

	$flikker = join("",file("http://api.flickr.com/services/feeds/photos_public.gne?tags=".$apes_tags."&format=rss"));
	$flikkerhits = preg_split('/img src="\;/', $flikker, -1, PREG_SPLIT_OFFSET_CAPTURE);
	foreach($flikkerhits as $flikkerhit){
	    $i++;
	    if($i>1) $apes[]=substr($flikkerhit[0], 0, strpos($flikkerhit[0], 'width')-7);
	}
	$strgrid = "";
	$currentcol=1;
	$currentrow=1;
	$columns=$apes_cols;
	$rows=$apes_rows;
	$strgrid .= "
"; for($a=0;$a"; $strgrid .= ""; $strgrid .= ""; $currentcol++; if($currentcol>$columns) { $currentcol=1; $strgrid .= " "; $currentrow++; if($currentrow>$rows) { break; } else { $strgrid .= " "; } } } $strgrid .= "
"; echo $strgrid; } function apes_control($widget_args) { global $wp_registered_widgets; static $updated = false; // have we already updated the data after a POST submit if ( is_numeric($widget_args) ) $widget_args = array( 'number' => $widget_args ); $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) ); extract( $widget_args, EXTR_SKIP ); // Data should be stored as array: array( number => data for that instance of the widget, ... ) $options = get_option('widget_apes'); if ( !is_array($options) ) $options = array(); // We need to update the data if ( !$updated && !empty($_POST['sidebar']) ) { // Tells us what sidebar to put the data in $sidebar = (string) $_POST['sidebar']; $sidebars_widgets = wp_get_sidebars_widgets(); if ( isset($sidebars_widgets[$sidebar]) ) $this_sidebar =& $sidebars_widgets[$sidebar]; else $this_sidebar = array(); foreach ( $this_sidebar as $_widget_id ) { // 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 // since widget ids aren't necessarily persistent across multiple updates if ( 'disp_apes' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) { $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number']; if ( !in_array( "apes-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number} unset($options[$widget_number]); } } foreach ( (array) $_POST['apes'] as $widget_number => $widget_apes_instance ) { // compile data from $widget_random_image_instance $apes_tags=strip_tags(stripslashes( $widget_apes_instance['apes_tags'])); $apes_rows=strip_tags(stripslashes( $widget_apes_instance['apes_rows'])); $apes_cols=strip_tags(stripslashes( $widget_apes_instance['apes_cols'])); $options[$widget_number] = compact('apes_tags', 'apes_cols', 'apes_rows'); } update_option('widget_apes', $options); $updated = true; // So that we don't go through this more than once } // Here we echo out the form if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS $apes_tags = 'apes'; $apes_cols = 3; $apes_rows = 2; $number = '%i%'; } else { $apes_tags = attribute_escape($options[$number]['apes_tags']); $apes_cols=$options[$number]['apes_cols']; $apes_rows=attribute_escape($options[$number]['apes_rows']); } // The form has inputs with names like widget-many[$number][something] so that all data for that instance of // the widget are stored in one $_POST variable: $_POST['widget-many'][$number] ?> < ?php } /* Function: apes_register ** ** Registers the apes widgets with the widget page ** ** args: none ** returns: nothing */ function apes_register() { if ( !$options = get_option('widget_apes') ) $options = array(); $widget_ops = array('classname' => 'widget_apes', 'description' => __('Displays Flickr Apes')); $control_ops = array('apes_cols' => 3, 'apes_rows' => 2, 'apes_tags' => 'apes', 'id_base' => 'apes'); $name = __('Scrape the Apes'); $registered = false; foreach ( array_keys($options) as $o ) { // Old widgets can have null values for some reason if ( !isset($options[$o]['apes_tags']) ) continue; // $id should look like {$id_base}-{$o} $id = "apes-$o"; // Never never never translate an id $registered = true; wp_register_sidebar_widget( $id, $name, 'disp_apes', $widget_ops, array( 'number' => $o ) ); wp_register_widget_control( $id, $name, 'apes_control', $control_ops, array( 'number' => $o ) ); } // If there are none, we register the widget's existance with a generic template if ( !$registered ) { wp_register_sidebar_widget( 'apes-1', $name, 'disp_apes', $widget_ops, array( 'number' => -1 ) ); wp_register_widget_control( 'apes-1', $name, 'apes_control', $control_ops, array( 'number' => -1 ) ); } } // This is important 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 *