<?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);

// NOTE: All variables and functions will need to be prefixed properly to allow multiple plugins to be updated

/******************Change this*******************/
$franz_api_url = 'http://updates.graphene-theme.com/';
/************************************************/

/*******************Child Theme******************
//Use this section to provide updates for a child theme
//If using on child theme be sure to prefix all functions properly to avoid 
//function exists errors
if ( function_exists( 'wp_get_theme' ) ){
    $franz_theme_data = wp_get_theme(get_option( 'stylesheet' ) );
    $franz_theme_version = $franz_theme_data->Version;  
} else {
    $franz_theme_data = get_theme_data( get_stylesheet_directory() . '/style.css' );
    $franz_theme_version = $franz_theme_data['Version'];
}    
$franz_theme_base = get_option( 'stylesheet' );
**************************************************/

$franz_theme_base = basename( FJ_ROOTDIR );
if ( function_exists( 'wp_get_theme' ) ){
    $franz_theme_data = wp_get_theme( $franz_theme_base );
    $franz_theme_version = $franz_theme_data->Version;  
} else {
    $franz_theme_data = get_theme_data( FJ_ROOTDIR . '/style.css' );
    $franz_theme_version = $franz_theme_data['Version'];
}    


//Uncomment below to find the theme slug that will need to be setup on the api server
//var_dump( $franz_theme_base);


/**
 * Take over the theme update check
 *
 * @package Graphene Mobile Neo
 * @since 1.0
 */
function franz_check_for_update( $checked_data ) {
	global $wp_version, $franz_theme_version, $franz_theme_base, $franz_api_url;

	$request = array(
		'slug' => $franz_theme_base,
		'version' => $franz_theme_version 
	);
	
	/* 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' ) . ' franz'
					);
	$raw_response = wp_remote_post( $franz_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[$franz_theme_base] = $response;

	return $checked_data;
}
add_filter( 'pre_set_site_transient_update_themes', 'franz_check_for_update' );


/**
 * Take over the Theme info screen on WP multisite
 *
 * @package Graphene Mobile Neo
 * @since 1.0
 */
function franz_api_call( $def, $action, $args ) {
	global $franz_theme_base, $franz_api_url, $franz_theme_version;
	
	if ( $args->slug != $franz_theme_base )
		return false;
	
	/* Get the current version */
	$args->version = $franz_theme_version;
	$request_string = prepare_request( $action, $args );
	$request = wp_remote_post( $franz_api_url, $request_string );

	if (is_wp_error( $request ) ) {
		$res = new WP_Error( 'themes_api_failed', __( 'An Unexpected HTTP Error occurred during the API request.</p> <p><a href="?" onclick="document.location.reload(); return false;">Try again</a>' ), $request->get_error_message() );
	} else {
		$res = unserialize( $request['body'] );
		if ( $res === false ) $res = new WP_Error( 'themes_api_failed', __( 'An unknown error occurred' ), $request['body'] );
	}
	
	return $res;
}
add_filter( 'themes_api', 'franz_api_call', 10, 3 );