Programming

PHP rocks, JAVA sucks…what else can I say?

FlickrViewer for AutoViewer

When I was creating the website for my photography endeavor, I wanted a place to show off my photos. I stumbled upon AutoViewer and thought it would be a good method for displaying my photos. However, I wanted the ease of using Flickr’s Slideshow so I could easily update my portfolio.

Mark Sweeting has a PHP script, FlickrViewer, which allows for integration of a Flickr Set with SimpleViewer, the sister program to AutoViewer. Being a programmer, I modified Mark’s script to work with AutoViewer and voila: FlickrViewer for AutoViewer.

There are two major things that I changed. The first was making the XML output compatible with what AutoViewer expects1. The second thing was to add a series of calls to Flickr to get the appropriate image URLs.

This is not an official product offering, so the extend that I’m planning on maintaining this code is the extent that I need to for my own purposes and the extent that someone wants to fund development. Otherwise, have it. Released under the GNU General Public License.

Download: flickrViewer v0.9 for AutoViewer

You’ll also need to download AutoViewer.

You’ll also may need an API key from Flickr if you want to share private photos. It’s free at http://www.flickr.com/services/api/misc.api_keys.html

0
  1. which, for some reasons, is different than what SimpleViewer expects 

New Plugin: WP Plugin Data

For better or worse, I’ve released a brand new plugin. It was one of those things I did spur of the moment because I wanted to implement something and the way I wanted to do it wasn’t available (insert some comment about necessity being the mother of invention here).

The new plugin is WP Plugin Data. It’s designed to use the WordPress.org Plugin API to get data about a specific plugin. John Blackbourn already wrote a plugin, Plugin Info, that does something similar. However, it requires that you set a Custom Field to the plugin name. I didn’t like this approach because I could only grab data for one plugin per a page.

So, using John’s plugin as a template, I wrote my own plugin that used shortcodes to add the data. And then I released it. This is why I love the GNU GPL. As for naming, I really hate it when plugins use the “WP” moniker in their titles. Typically, the plugin has absolutely no affiliation to WordPress and I feel the only reason they put “WP” in their name is to gain some form of (undeserved) recognition. For this reason, I really didn’t want to put “WP” in the title. However, I did end up using because this plugin actually does integrate itself into the WP.org backend using the API…so I feel the use is justified.

Anyway, enough about that.

Read more, or download version 0.5!

0

Countdown Timer v2.3.5

Hot off the press, Countdown Timer 2.3.5 is now available for consumption.

This is a small update that resolves a few issues, including:

  • Updated calculation routine to ensure that dates are accurately when “Months” are not displayed.
  • Updated readme.txt file to make some things more clear
  • Fixed small display issue in the administration menu

Also included are six new languages:
Latvian, Romanian, Russian, Danish, Lithuanian, and Serbian.

Read more or download version 2.3.5!

0

Happy Unix Day

In many computer system, time is kept track as number of seconds since midnight on January 1, 1970 (also know as epoch). This time format originated with the Unix system, which is why it’s often referred to as Unix time. At this very instant, the number of seconds since epoch is exactly 1,234,567,890. Happy Unix Day.

I think the next time we’ll probably celebrate is Monday, January 18th, 2038 at 20:14:07 hours. This is when we run into the real millennium bug. Why? Unix time is a 32-bit signed integer (it’s probably a signed integer so that dates before 1 Jan 1970 can be expressed). So, a signed 32-bit number has the range of -231 to +231-1…which is -2,147,483,648 to 2,147,483,647 for everyone who doesn’t want to break out their calculators.

2,147,483,647 turns out to be Monday, January 18th, 2038 at 20:14:07 hours (mountain time). What happens after this? The computer will think it’s Friday, December 13th, 1901 at 12:45:52 hours. Why does it think this? Because computers still count in binary. The important thing to know about a signed binary number is that the sign bit (i.e. how the computer knows if a number is negative or positive, 0 = positive and 1 = negative) is the most significant bit (i.e. the left-most bit). So, if we have a 32-bit signed number, that really means 31-bits worth of numbers, plus a bit for the plus or negative. Thus, the largest positive number we can have is:
0111 1111 1111 1111 1111 1111 1111 1111

…which is 2,147,483,647 in decimal (bonus points if you figure out the hex value of that by converting in your head).

If you add “1” to that binary number, it rolls over to:
1000 0000 0000 0000 0000 0000 0000 0000

…which you might think is negative zero. Of course, you would then ask yourself what’s the difference between negative zero and positive zero. And the answer is: there is none (at least none that most computer programmers care about). So instead of having two versions of zero, some genius decided to have only one version of zero (he kept the positive version), and to make extend the negative range of numbers by one. Thus, two’s compliment was invented and every signed-integer format uses two’s compliment.

To figure out the actual value of a negative number (1000 0000 0000 0000 0000 0000 0000 0000, in our case), you just subtract one and then flip all the bits (all the 1’s become 0’s and all the 0’s become 1’s) and read the resulting value as an unsigned value

So,
Start with: 1000 0000 0000 0000 0000 0000 0000 0000
Subtract 1 to get: 0111 1111 1111 1111 1111 1111 1111 1111
Flip all the bits to get: 1000 0000 0000 0000 0000 0000 0000 0000
Which is: 2,147,483,648 in decimal
But remember we started out with a negative number, so we have to add the negative sign back in to get: -2,147,483,648

And -2,147,483,648 is Friday, December 13th, 1901 at 12:45:52 hours in Unix time.

For what it’s worth, I was really only planning on saying “Happy Unix Day” but I got carried away. Sorry about that.

1FFFFFFFFFFFFFFF

0

The Birth of a Site: Andrew Ferguson Photography

In a few short days, I’ll be releasing the next step in my photographic endeavor: my official photography website. I’ve spent about two weeks working on the site, which, for me, is quite a bit of time. But I wanted to get it right. Every one who I’ve showed it to thus far has said that it look very professional. I thought I would spend some time discussing how I went about developing the site, in the hopes that it might be interesting to someone.

For a while now, I’ve maintained a Portfolio Set on Flickr. Recently, I used some slick software from Airtight Interactive called SimpleViewer, combined with a PHP script called FlickrViewer (which makes API calls to Flickr and creates the necessary files SimpleViewer needs), to launch a cleaner version of my portfolio: https://andrewferguson.net/portfolio/.

After my Photographer for Hire post from a couple weeks ago, I wanted an even slicker site that was dedicated (in part) to the type of photography I was targeting (i.e. portraits). Originally, my first though was to use this awesome WordPress theme that I’d recently heard about: Monotone. In a nutshell, the color of the site is based on the first picture of the blog post. There were a couple of issues with this method, however. First, the theme was really only designed to handle one image per a blog post (seriously, all the other images were stripped out). Second, I didn’t want to divide my blogging between two sites. Third, I had to manually upload photos that I had already put on Flickr; I wanted to be able to just pull them in from Flickr. So after about a week of messing around, I decided to abandon the WordPress-based site and go home brew.

I already had SimpleViewer working with Flickr, and I knew that Airtight Interactive had another similar program called AutoViewer. So I decided to use that as my starting point. I took my current portfolio viewer…
inspiration
…and designed a main page around that. I got out a piece of paper and drew the following:

boxes

Three boxes. But what about content? I took a quick trip around some of my favorite photo sites and wrote down all the links they had, then dutifully narrowed them down to three categories: photos, about me, and cost. I decided that I’d divide photos into portraits and landscapes (although really it should be “pictures of people” and “pictures not of people”). From three boxes to four boxes: Portraits, Landscapes, Rates, Andrew.

Using Photoshop, I mocked up the following:
afdi

I really wish I had screenshots of all the interim steps. Instead, you’ll just have to trust me that this wasn’t a straight forward processes.

With a design in the bag, I started the process of converting image to code. First I coded the main page. It’s all done with DIV tags and CSS. It was really just a matter of specifying the size of each element box and then making the whole thing float in the middle with some margin-left: auto; margin-right: auto; magic. I created a couple of PHP functions to distribute common code elements, primarily the XHTML above and below the BODY tag. And that was pretty much it.

From that, I coded the rates and about me page. The portraits and landscape page was pretty much already in the bag, I just had to tweak the FlickrViewer code (which took longer then anticipated and involved a pretty heavy rewrite of the existing code, but that’s another story). I showed it to some people and got some feedback, incessantly tweaking every little detail until I finally had something that I thought was good:
afdi-final
I think it’s rather remarkable how close the final design is to the mockup. I’ve tested it across several browsers and everything seems to be in order, including fully XHTML 1.0 Transitional and CSS compliant.

Google Analytics are in place to track my stats; and using Google Webmaster Tools I’ve made the powers that be aware of my site. I also took the opportunity to upload a sitemap, which I generated using an online sitemap generator.

There’s just a couple more things to wrap up before I officially launch, so check back on Saturday for the official launch of Andrew Ferguson Photography.

Questions?

0

Fun With Numbers

Ken Jennings posses four questions about 2009 which I thought would be fun to answer by making a small JavaScript to calculate the answers:

From ken-jennings.com:

It turns out that 2009 is not a prime number, as I briefly wondered if it might be. In fact, one of its several divisors is a perfect square. What is that factor?

While you’re killing time on meaningless stuff (I imagine Friday is going to be a slow work day, if you’re in the office at all): what was the last year that had such a large perfect square as a factor? What will be the next one?

And what year is the next prime number?

Solution and code after the break. Read More »Fun With Numbers

0

Using add_meta_box()

Note: Updated to be compatible with WP2.7.

Note: Technical content to follow.

In the WordPress 2.5 upgrade, Automattic completely revamped the administration interface – including the way that boxes were created for plugins (well, really the entire administration back end; however I will be focusing on plugins). Previously, you had to create the boxes manually, hard coding something to the effect of:

<div class="dbx-b-ox-wrapper">
<fieldset id="myplugin_fieldsetid" class="dbx-box">
<div class="dbx-handle-wrapper"><h3 class="dbx-handle"><?php _e( 'My Post Section Title', 'myplugin_textdomain' ); ?></h3></div>
<div class="dbx-content-wrapper">
<div class="dbx-content">
Your plugin box content here
</div>
</div>
</fieldset>
</div>

Source: http://codex.wordpress.org/Function_Reference/add_meta_box

But how do you get those slick looking boxes seen in WordPress 2.5 and WordPress 2.6 and WordPress 2.7? I looked online and I found plenty of sites detailing how to add meta boxes to the post page. However, I didn’t find anything describing how to implement the add_meta_box() function for your own plugin page.

I spent a bit of time time and reverse engineered the process. Once you have it figured out, it’s really quite simple. At the very least, it’s a lot more intuitive.

First, you’ll want to wrap your content you want to display in a function that echos it out. You’ll need a separate function for every box you want to add. After the function has been declared, you’ll want to add it using the add_meta_box() call. The usage for add_meta_box is:

<?php add_meta_box('id', 'title', 'callback', 'page', 'context', 'priority'); ?>

Here’s the PHPDoc data:
string $id String for use in the ‘id’ attribute of tags.
string $title Title of the meta box
string $callback Function that fills the box with the desired content. The function should echo its output.
string $page The type of edit page on which to show the box (post, page, link)
string $context The context within the page where the boxes should show (‘normal’, ‘advanced’)
string $priority The priority within the context where the boxes should show (‘high’, ‘low’)

add_meta_box simply adds the box to a queue. It doesn’t actually spit out any code. Thus, after you’ve created all the content callback functions and added them to the meta box queue with add_meta_box, you’ll need to execute do_meta_boxes().

<?php do_meta_boxes('page', 'context', 'object'); ?>

string $page The edit page which you want to display; this will be the same as $page specified in add_meta_box.
string $context The context within the page where the boxes should show (‘normal’, ‘advanced’).
? $object I don’t know, I just have it set to null.

All together, the code might look something like:

<?php
function yourplugin_helloworld_meta_box(){
?>
Hello, world!
<?php
}
add_meta_box("yourplugin_helloworld", __('Say Hello', 'yourplugin'), "yourplugin_helloworld_meta_box", "yourplugin");
do_meta_boxes('yourplugin','advanced',null);
?>

One weird/interesting thing I discovered is that ‘yourplugin’ can only consist of lowercase letters a-z and the hyphen “-” symbol. For all you programmers, it must return true when matched against /^[a-z-]+$/

I don’t know what, but that’s the check run on line 620 of /wp-admin/admin-ajax.php from WordPress 2.6.2.

That’s the most basic level of creating your own meta boxes. However, there’s more. Do you notice how sometime the meta boxes are closed on the posts (or pages, or links) page? The open/closed position is remembered using some AJAX. The actual data is stored per user in the wp_usermeta table with a meta_key of ‘closedpostboxes_yourplugin’. The data is a serialized array that lists just the closed the boxes.

To get this functionality, you’ll need to perform a couple more steps. First, you’ll need to add some javascript. This code does two things, on load it closes boxes that should be closed (because they were previously closed) and it allows the boxes to be toggled open and closed.

jQuery(document).ready( function($) {
	// close postboxes that should be closed
	jQuery('.if-js-closed').removeClass('if-js-closed').addClass('closed');
		
	// postboxes
	<?php
	global $wp_version;
	if(version_compare($wp_version,"2.7-alpha", "<")){
		echo "add_postbox_toggles('yourpluging');"; //For WP2.6 and below
	}
	else{
		echo "postboxes.add_postbox_toggles('yourplugin');"; //For WP2.7 and above
	}
	?>
			
});

You’ll also need to add this PHP code:

<?php
wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
?>

This code registers a new nonce that your plugin will use to send authorized AJAX requests.

And that’s it. Give a holler in the comments if you have questions and I’ll try my best to answer them.

See also: http://xref.fergcorp.com/

0

Force User Field Registration End-of-Life

For better or for worse, this is the End-of-Life announcement for Force User Field Registration.

This history behind the plugin is rather odd for me. I originally wrote it as a proof-of-concept for Add Custom fields to User Profile & Registration Pages. I never planned to actually implement it and support it, which I suppose it what I get for trying to be helpful ;).

In fact, the orginial code was just posted as a snipnet on my blog over two years and required the user to copy and paste the code into a file themselves! I finally released the code in a file you could download a four months later. It’s seen several upgrades, mostly to deal with WordPress updates that broke stuff and I even forked it into a version for WordPress MU.

I think one of the most difficult things about maintaining this plugin over the last few years is the fact that I never use it myself. I really enjoy using my own plugins and not using them on a regular basis creates a very wide disconnect for me, I don’t get to eat my own dog food.

I digress.

I was answering a request related to the Force User Field Plugin and doing some research when I came across a superior plugin that not only meets all goals set out by Force User Field Registration, but far exceeds them.

It’s called Register Plus and is coded by Skullbit.

I downloaded it and played with it briefly; and I have to say, I really like it. There are a few quirks that I’m sure the developer will work out, but nothing blocking. It also implements a number of requests that users have asked for in the past and I have not added because they fall outside the scope of the plugin or I never got around to.

Thanks to everyone who has ever used the the plugin or left a comment. At last check, there were over 1600 downloads and 143 comments (probably half of which are mine, though).

The formal announcement:

Force User Field Registration is going End-of-Life on or before December 31, 2008 23:59 PST.
I will continue to provide official patch releases (i.e. 0.6.x) as needed to maintain compatibility with WordPress 2.6.x and WordPress MU based on WordPress 2.6.x.

On or before January 1, 2009 00:00 PST, I will provide help on a case by case basis, however I will no longer be releasing updates.

The code will remain posted and licensed under the GNU GPL. The official plugin page will remain up: Force User Field Registration

What I believe to be a suitable replacement:
Register Plus by Skullbit.

0

Countdown Timer v2.2

I wanted to release this earlier, but between finals, being sick, and field session, I haven’t had a lot of time. This version of Countdown Timer (almost) updates the UI to 2.5 look and feel. As it turns out, there’s actually some function calls I can use to better create the UI and I’ll be using those in the future. There’s a couple of bug updates and added some new functionality.

Also, Countdown Timer v2.2 requires WordPress 2.5 or higher!

The run down:

  • Updated some phrases that missed being i18n.
  • Updated i18n to use use sprintf’s to assist in proper translation.
  • Update the admin page to WordPress 2.5 Look and Feel.
  • Users are now able to define the data (text/HTML) that comes after the title and before the actual countdown.
  • Implemented a new function, fergcorp_countdownTimer_single($date), that allows users to create a one-off event outside of The Loop. $date should be PHP strtotime parseable string.
  • Plugin output is now XHTML 1.0 Strict compliant.

Read more or download version 2.2!

0