Travels of a Commercial WordPress Plugin Developer 1

My job at the moment is updating and coding YourMembers, which is a commercial plugin for WordPress.

Recently I’ve been working on a method to Auto Upgrade the plugin, since commercial plugins cannot go on Extend, and there is no commercial equivalent. Extend being the WordPress plugin repository.

Commercial plugins get no nice little “Plugin needs updating Circle” on the plugins tab, or a nice little message on the plugins page, let alone access to the auto updater!

Which leaves us (commercial plugin devs) to write our own version checkers and hook in accordingly.

So now I have a nice little PHP class which will detect whether it can fwrite/rename and if not if it can FTP instead, much the same as the WordPress updater does.

Now this is all well and good, but I needed a method to download and unzip the new plugin file. Grabbing the file is easy, either file_get_contents (heaven forbid), or curl (yay for curl), and write the file (being a zip) to the web servers temporary directory and process from there.

Now the problem here is how to unzip. A Brief look at how WordPress does it seems to show it has three different methods of unzipping. So dump that.

Didn’t feel the need to use PHP’s ZipArchive class, since I don’t need to edit the zip, just extract its contents.

Off to php.net I go and I find some nice handy functions, and quite surprised to find that the zip_read function can handily recurs into directories within the zip file to return their paths/names. Mildly annoying at the time as I had gone and written a recur loop to go inside directories…. and got thrown a error, nice headdesk moment!

So basically the Zip functions, the basic ones of PHP, are actually quite nice, handy and useful!

Heres my code snippet:

  1. <?php
  2. class someclass() {
  3.         private function unpack() {
  4.                 $from = $this->tmp_write;
  5.                 $to = $this->tmp_out;
  6.                 @mkdir($to);
  7.                
  8.                 $zip = zip_open($from);
  9.                 if (is_resource($zip)) {
  10.                         $this->readzip($zip);
  11.                         zip_close($zip);
  12.                 } else {
  13.                         // failed to open
  14.                         $this->error = 1;
  15.                 }
  16.         }
  17.        
  18.         private function readzip($zip) {
  19.                 $from = $this->tmp_write;
  20.                 $to = $this->tmp_out;
  21.                
  22.                 echo ‘</pre><textarea style="width: 100%; height: 100px;">’;
  23.                
  24.                 while (FALSE !== ($file = zip_read($zip))) {
  25.                         $name = zip_entry_name($file);
  26.                         echo $name . "\n";
  27.                         if (!strpos($name, ‘.’)) {
  28.                                 // is dir
  29.                                 @mkdir($to . ‘/’ . $name);
  30.                         } else {
  31.                                 // it recurs into directorys on its own!
  32.                                 $item = zip_entry_open($zip, $file);
  33.                                 $size = zip_entry_filesize($file);
  34.                                
  35.                                 $read = zip_entry_read($file, $size);
  36.                                 $fp = fopen($to . ‘/’ . $name, ‘w’);
  37.                                 fwrite($fp, $read);
  38.                                 fclose($fp);
  39.                         }
  40.                 }
  41.                
  42.                 echo ‘</textarea><pre>’;
  43.         }
  44. }
  45. ?>

I just installed this code snippet plugin, which I might need to change its background colours…..

Any suggestions for code snippet plugins greatly appreciated!

Comments (2)

  1. 12:56, 29th January, 2012Anton Oliinyk  / Reply

    Actually there is a little open-source plug-in called “Upgrademe” that provides auto-update functionality for commercial plugin-in.
    It’s not intended to provide fake impression of built-in WP update system (which may change over versions), it simply feeds WP with latest version information and update download URLs for privately hosted plug-ins.
    Read my post about it if you’re interested: Automatically updating commercial WordPress plug-ins

    • 13:04, 29th January, 2012Barry Carlyon  / Reply

      In the end we found a PHP Class that plugins can include that does the job quite well.
      You can find details here http://w-shadow.com/blog/2010/09/02/automatic-updates-for-any-plugin/
      I just never got around to writing a follow up post :-D

      The problem with upgradeMe is that I would have ship the commercial plugin with another plugin just to handle updates….
      Sure I can fold in upgradeMe into my Plugin Core. But then I already have (but with something else that still works to this day).

Leave a Reply

Pingbacks (0)

› No pingbacks yet.