WordPress.com News

We are happy to announce our new REST Application Programming Interface (API) that lets developers explore, interact, and create great new content with the vast community of sites on the WordPress.com network and, in the near future, Jetpack-enabled sites.

The API gives developers access to posts and comments, as well as the ability to Follow, Like, or Reblog content for users. Other features from WordPress.com, like the daily handpicked content on Freshly Pressed, are also available through the API.

An excellent example of an application that uses the new API is the Windows 8 WordPress.com app, available now.

Our goal with the new API is to simplify the experience of using and adding to the data available on WordPress.com. To do this, we now use the OAuth2 protocol to authenticate requests for data. To retrieve public data, you can make unauthenticated requests. To perform actions…

View original post 168 more words

Filter arrays of data by any field in the array/objects

I frequently need to grab a subset of data from an array. I just stumbled over the FilterIterator which provides a nice interface to do just this. Based on this class here is a implementation which lets you perform basic filtering.

Here is an example:

$dummy_array = array(
	array( 'id' => 1, 'name' => 'Jon Doe', 'date' => '2012-01-01' ),
	array( 'id' => 2, 'name' => 'Jack Black', 'date' => '2012-02-01' ),
	array( 'id' => 3, 'name' => 'Jane Doe', 'date' => '2012-02-04' ),
	array( 'id' => 4, 'name' => 'Simon Smith', 'date' => '2012-03-01' ), 
);

echo "\nall Doe\n";
$does = simple_array_filter( $dummy_array, 'name', 'regex', '#doe$#i' );
foreach( $does as $row )
	var_export( $row );

echo "\nall in February\n";
$febs = simple_array_filter( $dummy_array )
	->add_filter( 'date', 'newer_than', '2012-02-01 00:00:00' )
	->add_filter( 'date', 'older_than', '2012-02-29 23:59:59' );
foreach( $febs as $row )
	var_export( $row );

And here is the class that makes it happen.

/**
 * Provide easy filtering iterator for arrays
 * @see: http://php.net/manual/en/class.filteriterator.php
 */
class Simple_Array_Filter extends FilterIterator {
    private $filters = array();
	private $data = array();
	
    public function __construct( $data, $filter_field = NULL, $filter_method = NULL, $filter_value = NULL ) {
		$this->data = $data;
		$iterator = $this->get_iterator();
		parent::__construct( $iterator );

		if ( ! is_null( $filter_field ) && ! is_null( $filter_method ) && ! is_null( $filter_value ) )
			$this->add_filter( $filter_field, $filter_method, $filter_value );
    }

	public function get_iterator() {
		if ( is_object( $this->data ) && method_exists( $this->data, 'getIterator' ) )
			return $this->data->getIterator();
		if ( is_array( $this->data ) ) {
			$object = new ArrayObject( $this->data );
			return $object->getIterator();
		}
		return $this->data;
    }
	
    public function accept() {
        $data = $this->getInnerIterator()->current();
		
		return $this->dispatch_filter( $data );
	}

	public function add_filter( $filter_field, $filter_method, $filter_value ) {
		$this->filters[] = array( 'filter_field' => $filter_field, 'filter_method' => $filter_method, 'filter_value' => $filter_value );
		return $this;
	}
	
	private function dispatch_filter( $data ) {
		$result = true; // by default don't filter
		foreach( $this->filters as $filter ) {
			extract( $filter );
			if ( is_object( $data ) ) {
				if ( isset( $data->{$filter_field} ) ) {
					$cmp_value = $data->{$filter_field};
				}
			} else if ( is_array( $data ) ) {
				if ( isset( $data[$filter_field] ) ) {
					$cmp_value = $data[$filter_field];
				}
			}
			
			if ( empty( $cmp_value ) )
				continue;
			
			if ( is_callable( array( &$this, $filter_method ) ) )
				$result = call_user_func( array( &$this, $filter_method ), $cmp_value, $filter_value );
			else if ( function_exists( $filter_method ) ) {
				$result = call_user_func( $filter_method, $cmp_value, $filter_value );
			}

			if ( false == $result )
				return false;
		}
		return true;
	}

	private function gt( $cmp_value, $filter_value ) {
		if ( is_numeric( $cmp_value ) )
			return (int) $cmp_value > (int) $filter_value;
		else
			return strcmp( $cmp_value, $filter_value ) > 0;
	}
	
	private function lt( $cmp_value, $filter_value ) {
		if ( is_numeric( $cmp_value ) )
			return (int) $cmp_value < (int) $filter_value;
		else
			return strcmp( $cmp_value, $filter_value ) < 0;

	}
	
	private function eq( $cmp_value, $filter_value ) {
		if ( is_numeric( $cmp_value ) )
			return (int) $cmp_value == (int) $filter_value;
		else
			return strcmp( $cmp_value, $filter_value ) == 0;

	}
	
	private function regex( $cmp_value, $filter_value ) {
		return preg_match( $filter_value, $cmp_value );
	}

	private function older_than( $cmp_value, $filter_value ) {
		if ( !is_numeric( $cmp_value ) )
			$cmp_value = strtotime( $cmp_value );
		if ( !is_numeric( $filter_value ) )
			$filter_value = strtotime( $filter_value );
		else
			$filter_value = $filter_value;

		return $cmp_value < $filter_value;
	}

	private function newer_than( $cmp_value, $filter_value ) {
		if ( !is_numeric( $cmp_value ) )
			$cmp_value = strtotime( $cmp_value );
		if ( !is_numeric( $filter_value ) )
			$filter_value = strtotime( $filter_value );
		else
			$filter_value = $filter_value;

		return $cmp_value > $filter_value;

	}
}
if ( !function_exists( 'simple_array_filter' ) ) {
	function simple_array_filter( $data, $filter_field = NULL, $filter_method = NULL, $filter_value = NULL ) {
		$simple_array_filter = new Simple_Array_Filter( $data, $filter_field, $filter_method, $filter_value );
		return $simple_array_filter;
	}
}

Minimalistic progress bar for CLI based PHP scripts

I’m doing some CLI scripting here and there and as I’m still on the way to implement it the right way using a framework like wpcli, wpshell or the like for my scripts I sometimes like to put a little progress bar in my scripts to see how things are going and how much longer the script might run. So this is what I do.

<?php

$time1 = time(); 	// set the start time

dosomethingnasty();
function dosomethingnasty() {
	for( $i=0; $i < 300; $i++ ) {
		progress( $i, 300 );
		sleep( 1 );
	}
}


/**
 * Print simple progress bar.
 * 
 * @access public
 * @param int $processed amount of items processed
 * @param int $max maximum amount of items to process
 * @return void
 */
function progress( $processed, $max ) {
	global $time1;
	$progress = round( $processed / ( $max / 100 ), 2);
	$progress_points = floor($progress/2);
	$time_x=time();
	$timediff = $time_x - $time1;
	$estimation = round( ( ( ( 100 / $progress *  $timediff ) - $timediff ) / 60 ), 2 );
	echo str_pad( str_repeat( "#", $progress_points ), 52, " ", STR_PAD_RIGHT) . sprintf( "%.2f", $progress ) . str_pad( "% ( ". sprintf( "%.2f", $estimation ) . " min left )", 27, " ", STR_PAD_RIGHT). "\r" ;
}

Updated documentation and Coda Syntax mode to WordPress 3.3 “Sonny”

The latest and greatest WordPress 3.3, codename “Sonny” is ready for download.

Apart of all the little tweaks and fixes there are also a bunch of very useful feature coming along with this version:

Experienced users will appreciate the new drag-and-drop uploader, hover menus for the navigation, the new toolbar, improved co-editing support, and the new Tumblr importer. We’ve also been thinking a ton about what the WordPress experience is like for people completely new to the software. Version 3.3 has significant improvements there with pointer tips for new features included in each update, a friendly welcome message for first-time users, and revamped help tabs throughout the interface. Finally we’ve improved the dashboard experience on the iPad and other tablets with better touch support.
There is a ton of candy for developers as well. I’d recommend starting your exploration with the new editor API, new jQuery version, better ways to hook into the help screens, more performant post-slug-only permalinks, and of course the entire list of improvements on the Codex and in Trac.

Of course we updated our documentation to the newest version and also the WordPress Syntax Mode for Coda got an update.

Please have a look at our Function, Action and Filter index for a quick overview and see the list of some of the new and deprecated functions below.

New functions:
Theme related

Meta data related

Admin bar related

Other functions

Deprecated functions:

  • _wp_admin_html_begin
  • _ipad_meta
  • wp_tiny_mce
  • wp_quicktags
  • wp_print_editor_js
  • wp_preload_dialogs
  • wp_admin_bar_dashboard_view_site_menu
  • wpmu_admin_redirect_add_updated_param
  • wpmu_admin_do_redirect
  • type_url_form_video
  • type_url_form_image
  • type_url_form_file
  • type_url_form_audio
  • the_editor
  • start_post_rel_link
  • screen_options
  • screen_meta
  • screen_layout
  • sanitize_user_object
  • parent_post_rel_link
  • media_upload_video
  • media_upload_image
  • media_upload_file
  • media_upload_audio
  • is_blog_user
  • index_rel_link
  • get_user_metavalues
  • get_user_by_email
  • get_userdatabylogin
  • get_parent_post_rel_link
  • get_index_rel_link
  • get_boundary_post_rel_link
  • favorite_actions
  • add_contextual_help

_wp_footer_scripts

Definition:
function _wp_footer_scripts() {}

Private, for use in *_footer_scripts hooks

Source code

function _wp_footer_scripts() {

	print_late_styles();

	print_footer_scripts();

}

18001