Latest Publications

PHP Helpers: html_var_dump

With the last function being html_print_r this next one should come as no suprise, html_var_dump:

if ( !function_exists( 'html_var_dump' ) ) {
    function html_var_dump( $data ) {
        $out = "\n<pre class='html-var-dump'";
        $out .= " style='border: 1px solid #ccc; padding: 7px;'>\n";

        ob_start( );
        var_dump( $data );
        $out .= esc_html( ob_get_contents( ) );
        ob_end_clean( );

        $out .= "\n</pre>\n";

        return $out;
    }
}

Even though I tend to use print_r more often than var_dump, there are times where the type data that var_dump provides can be very helpful. Unfortunately var_dump will always display data, so it needed to be wrapped in a few output control functions ( ob_start, ob_get_contents, and ob_end_clean ) to capture it.

WordPress for BlackBerry

It’s a mobile app kind of week :-)

The WordPress BlackBerry App is out of beta and ready with it’s 1.0 release.

Just like the other WordPress mobile apps it’s open source and free. You can find more help and info in the FAQ, forums, and Trac site (for tickets and patches).

These mobile apps use XML-RPC to communicate with your WordPress blog, so make sure you have that enabled first. You can find that in wp-admin under Settings -> Writing in the Remote Publishing section.

WordPress for Android

Today the WordPress Android App is out the door and open to the public ( download ).

Like the other WordPress mobile apps this one is open source, and we’re excited to have other people contribute and help. There’s a developer blog, support forum, and Trac install for tickets and patches.

PHP Helpers: html_print_r

Before I get to the next PHP Helpers function I wanted to mention that I’ve made the code available as a Google Code Project at http://code.google.com/p/php-helpers/. I added specific licensing terms ( MIT style ) – http://code.google.com/p/php-helpers/source/browse/trunk/license.txt. There’s also a changelog available to see when functions were added.

With that out of the way here’s the next PHP Helpers function, html_print_r:

if ( !function_exists( 'html_print_r' ) ) {
    function html_print_r( $data ) {
        $out = "\n<pre class='html-print-r'";
        $out .= " style='border: 1px solid #ccc; padding: 7px;'>\n";
        $out .= esc_html( print_r( $data, TRUE ) );
        $out .= "\n</pre>\n";

        return $out;
    }
}

I added this because I use PHP’s print_r in an HTML context quite a bit. To make the output look reasonable I was always adding PRE tags, which got boring fast.

CSS Border Radius Percentages and Elliptical Borders

When using CSS border radius I’ve always specified the radius in pixels (px), something like this:

.round-box {
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
}

This got me to wondering, does it support percentages as well? So I tried this:

.round-box {
    border-radius: 5%;
    -moz-border-radius: 5%;
    -webkit-border-radius: 5%;
}

This worked in Firefox 3.6 but not in Chrome. Some searching around revealed the Mozilla -moz-border-radius page. For border radius it specifically mentions that it supports length units as well as percentages:

A percentage, relative to the width of the box (the percentage is relative to the width even when specifying the radius for a height).

That page also mentioned support for elliptical borders. To do that you add another radius value separated by a slash:

.round-box {
    border-radius: 15px / 50px;
    -moz-border-radius: 15px / 50px;
    -webkit-border-radius: 15px / 50px;
}

The elliptical border worked on Chrome as well. If you bend this tight enough you can get pretty close to a circle.

I wanted Internet Explorer to add support for border radius before; now that I’ve got even more radius toys to play with I’m practically begging. I’m sorry Internet Explorer users but I’m tired of various border hacks when there are simple and clean CSS methods available.

“Weird Al” Yankovic Blog

Not sure when exactly this happened, but Weird Al has a blog on WordPress.com – http://alyankovic.wordpress.com/

That’s awesome!

Just Hanging Out

Michael and Sarah

Michael and Sarah

PHP Helpers: esc_html

Next up in the PHP Helpers series is esc_html:

if ( !function_exists( 'esc_html' ) ) {
    function esc_html( $html, $char_set = 'UTF-8' ) {
        if ( empty( $html ) ) {
            return '';
        }

        $html = (string) $html;
        $html = htmlspecialchars( $html, ENT_QUOTES, $char_set );

        return $html;
    }
}

As you might have guessed this function escapes strings for HTML output. It’s not much more than a wrapper around htmlspecialchars, but provides central place to tweak your desired default behavior.

There’s room to experiment with additional optimizations and strict checking in this as well. I added the basics (look for empty strings, type cast to a string), if you’ve got a favorite additional check leave a comment below.

First Rule of Collaboration: If You Can’t Link To It, It Didn’t Happen

I’ve been mulling over the merits of different collaboration tools. What struck me is the importance of being able to refer to past conversations. In the world of the Internet that means URLs.

Having a history, or log, of discussions is critical. Some technologies don’t necessarily provide a built-in way to log conversations, but are easily augmented to do so. One example of this is IRC. A good bot can be added to log the conversation in a channel, and that in turn can be used to produce a web accessible archive.

WordPress.org does this at https://irclogs.wordpress.org/. But it’s more than just dumping the channel logs as HTML on a web page, each conversation can be linked to directly. For example, here is the link to the start of last week’s dev meetup in #wordpress-dev : https://irclogs.wordpress.org/chanlog.php?channel=wordpress-dev&day=2010-01-14#m55606. From a collaboration point of view being able to link back like this is critical. If we’re chatting about a topic that was covered during that conversation we don’t have to depend on our memory, notes or gut instinct; we can link directly to an authoritative source.

Email lists fall into this category as well, despite being inherently different in a critical way. Where IRC is real-time group chat, email is asynchronous; it might be days before you get response back. An archive of an email list can give the same sort of specific links that an IRC archive can.

Other communication tools, like blogs and forums, by their very nature provide links. For instance, here is the proposed WordPress 3.0 dev schedule from the WordPress Development Updates blog. Same goes for forums of course.

It’s worth making a mention of the P2 WordPress theme. It’s kind of a Twitter + forum blend. On WordPress.com you can use it along with the IM support for posts and comments, that adds a realtime element to a blog. At Automattic we have numerous internal private blogs that are powered by P2, I use the IM feature to get instant notification when a new post or comment is added. Matt has a great post on the impact P2 has had at Automattic.

What ever tools you use to collaborate with others, make sure there is a way to log the conversations, keeping a history is vital. And beyond that, if you can’t share links to that history, then it may as well not exist.

Setting up SVNSync and SVN-Notify

Long story short, I wanted to get commit emails for a Subversion repo. I don’t have any admin rights to the repo so I figured the easiest way was to just setup a local repo, keep up to date with the live repo with svnsync and then configure svn-notify on my local repo. The local repo is only used for svn-notify, I’m not using it to checkout copies of the code or anything else.

A good resource to getting started with svnsync is http://svn.apache.org/repos/asf/subversion/trunk/notes/svnsync.txt. Go take a look at that first, should make the rest of this easy to follow.

I assume that you already have subversion/svnsync and svn-notify installed and ready to go. I’m not going to cover installing those.

Here are the steps I followed to put this all together, first svnsync:

  1. Create local svn repo: svnadmin create /home/svn/repo-name
  2. Edit /home/svn/repo-name/hooks/pre-revprop-change so that it looks like:
    #!/bin/sh
    USER="$3"
    
    if [ "$USER" = "svnsync" ]; then exit 0; fi
    
    echo "Only the svnsync user can change revprops" >&2
    exit 1
    

    This limits changes to the local repo to only the ’svnsync’ user.

  3. Make it executable: chmod +x /home/svn/repo-name/hooks/pre-revprop-change
  4. Initialize the sync between repos: svnsyc init --username svnsync file:///home/svn/repo-name http://svn.example.com/repo-name
  5. Run the first sync: svnsync sync file:///home/svn/repo-name
  6. Schedule a cron job to run the sync periodically, say once every hour (on a nice odd number like 17 after the hour): 17 * * * * /usr/local/bin/svnsync --non-interactive sync file:///home/svn/repo-name

With svnsync in place adding svn-notify for our locally synced repo is pretty easy. Edit /home/svn/repo-name/hooks/post-commit to look like:

#!/bin/sh

REPOS="$1"
REV="$2"

delayed_notify( ) {
sleep 10

/usr/local/bin/svnnotify --repos-path "$1" --revision "$2" \
--to commit-list@example.com \
--from commit-list@example.com \
--with-diff \
--subject-cx \
--subject-prefix 'REPO-NAME ' \
--handler HTML::ColorDiff
}

delayed_notify $1 $2 > /dev/null 2>&1 &

Then make that hook executable: chmod +x /home/svn/repo-name/hooks/post-commit

There are two downsides to this approach. First, the commit emails don’t go out right away after a commit, svnsync has to be run first. In my example above that means it could be an hour after the commit before the email goes out. I don’t consider this a big deal and the gap can be reduced by syncing more often. The second problem bugs me, the user listed for all of the commits is the svnsync user, instead of the actual user that committed the change to the master repo. Since all of the commits are replayed as the svnsync user there doesn’t appear to be an easy way to fix this. If you know of one let me know.

And there we go, now you can get commit emails for a repo that you don’t have any admin rights to. Not glamorous or ideal, but better than nothing.

Update: A big thank you Peter Westwood (a.k.a westi) for suggesting a technique to delay svn-notify long enough for svnsync to finish replaying the rev-prop changes. I tried it out and it solved the problem where all commit emails showed the ’svnsync’ user for every commit.