Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need help with theme update API script WordPress

Hey everyone. I'm trying to implement an update API script for my theme and I'm a bit stuck. It consists of two files, an index.php which resides at a location on my server which acts as the api url, and an update.php which is used by the theme. Here are the two files:

index.php
<?php

// Sample Plugin & Theme API by Kaspars Dambis ([email protected])

$packages['ifeaturepro'] = array(
'versions' => array(
'1.1.5' => array(
'version' => '1.1.5',
'date' => '2011-05-25',
'package' => 'http://orangeola.com/ifeature/cache/ifeaturepro1.1.5.zip'
)
),
'info' => array(
'url' => 'http://cyberchimps.com/ifeaturepro/'
)
);


// Process API requests

$action = $_POST['action'];
$args = unserialize(stripslashes($_POST['request']));

if (is_array($args))
$args = array_to_object($args);

if (is_array($packages[$args->slug]))
$latest_package = array_shift($packages[$args->slug]['versions']);



// basic_check

if ($action == 'basic_check') {
$update_info = array_to_object($latest_package);
$update_info->slug = $args->slug;

if (version_compare($args->version, $latest_package['version'], '<'))
$update_info->new_version = $update_info->version;

print serialize($update_info);
}

// theme_update

if ($action == 'theme_update') {
$update_info = array_to_object($latest_package);

//$update_data = new stdClass;
$update_data = array();
$update_data['package'] = $update_info->package;
$update_data['new_version'] = $update_info->version;
$update_data['url'] = $packages[$args->slug]['info']['url'];

if (version_compare($args->version, $latest_package['version'], '<'))
print serialize($update_data);
}


function array_to_object($array = array()) {
if (empty($array) || !is_array($array))
return false;

$data = new stdClass;
foreach ($array as $akey => $aval)
$data->{$akey} = $aval;
return $data;
}

?>


update.php

<?php

/*
// TEMP: Enable update check on every request. Normally you don't need this! This is for testing only!
set_site_transient('update_themes', null);
*/

add_filter('pre_set_site_transient_update_themes', 'check_for_update');

function check_for_update($checked_data) {
global $wp_version;

if (empty($checked_data->checked))
return $checked_data;

$api_url = 'http://orangeola.com/api/';
$theme_base = basename(dirname(dirname(__FILE__)));

$request = array(
'slug' => $theme_base,
'version' => $checked_data->checked[$theme_base .'/'. $theme_base .'.php']
);

// Start checking for an update
$send_for_check = array(
'body' => array(
'action' => 'theme_update',
'request' => serialize($request),
'api-key' => md5(get_bloginfo('url'))
),
'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
);

$raw_response = wp_remote_post($api_url, $send_for_check);

if (!is_wp_error($raw_response) && ($raw_response['response']['code'] == 200))
$response = unserialize($raw_response['body']);

// Feed the update data into WP updater
if (!empty($response))
$checked_data->response[$theme_base] = $response;

return $checked_data;
}


if (is_admin())
$current = get_transient('update_themes');

?>


The index.php is in the right location, the zip file is in the right directory, and I've used the proper theme slug in the $packages array, so I'm unsure as to what the problem is. This is definitely uncharted territory for me, and I'll be happy to provide any additional information.

Answers (4)

2011-05-25

AdamGold answers:

What's the exact problem?

Are you sure you are using [[LINK href="https://github.com/jeremyclark13/automatic-theme-plugin-update"]]the latest version of the code[[/LINK]]?

I suggest that you try to use [[LINK href="http://clark-technet.com/2010/09/wordpress-theme-developers-tip-theme-update-noticifications"]]another code[[/LINK]] and combine the downloads with it.


Tyler Cunningham comments:

I have tried both his code (which is an offshoot of the original code), as well as this original code.

That second code would only notify the user that an update was available, I want it to notify AND allow the user to update which is what I'm assuming will be the result of the code I am trying to use.

2011-05-26

Sébastien | French WordpressDesigner answers:

maybe
'user-agent' => 'wordpress/' . $wp_version . '; ' . get_bloginfo('url')
instead of
'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')

(dumb ?)

2011-05-26

Duncan O'Neill answers:

A simple suggestion which might help yield some answers as to why your script is 'failing';

Turn on debug. Note that this will display database errors and PHP warnings on your site;

http://codex.wordpress.org/Editing_wp-config.php

All you need to do is add the following line to /wp-config.php, or change the false value to true if the define statement is already there;

define('WP_DEBUG', true);

best,

2011-05-28

Christianto answers:

Tyler,

Have you make it work?
What is the array of $checked_data that pass to the function? Could you show to us..

Thanks