jeremyclark13 / automatic-theme-plugin-update

Self hosted plugin and theme update scripts
http://clark-technet.com/2010/12/wordpress-self-hosted-plugin-update-api
548 stars 147 forks source link

Afte update Plugin Folder name is appended with some strings #45

Open mavinothkumar opened 8 years ago

mavinothkumar commented 8 years ago

Am working on a WordPress plugin where the updates will be done on the external server. I added the below code and everything is working good. But once it updated the plugin folder name will be appended with five digit characters followed by dash.

for example : 
before update plugin name - plugin-name
after  update plugin name - plugin-name-IbzGAQ

Am not sure why its auto append at end.

<?php
$api_url = 'http://server_address/update/';
$plugin_slug = 'plugin_slug';
$plugin_name = 'plugin_name';

// Take over the update check
add_filter('pre_set_site_transient_update_plugins', 'check_for_plugin_update');

function check_for_plugin_update($checked_data) {
  global $api_url, $plugin_slug, $wp_version, $plugin_name;
  $response = '';
  //Comment out these two lines during testing.
  if (empty($checked_data->checked))
    return $checked_data;

  $args = array(
      'slug' => $plugin_slug,
      'version' => $checked_data->checked[$plugin_slug .'/'. $plugin_name .'.php'],
  );
  $request_string = array(
      'body' => array(
          'action' => 'basic_check',
          'request' => serialize($args)
      ),
      'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
  );

  // Start checking for an update
  $raw_response = wp_remote_post($api_url, $request_string);

  if (!is_wp_error($raw_response) && ($raw_response['response']['code'] == 200)){
    $response = unserialize($raw_response['body']);
    if($response->version == 'invalid'){
      update_option('bc_rb_payment_info','no');
      return 'invalid';
    }
  }

  if (is_object($response) && !empty($response)) // Feed the update data into WP updater
    $checked_data->response[$plugin_slug .'/'. $plugin_name .'.php'] = $response;

  return $checked_data;
}

// Take over the Plugin info screen
add_filter('plugins_api', 'plugin_api_call', 10, 3);

function plugin_api_call($def, $action, $args) {
  global $plugin_slug, $api_url, $wp_version,$plugin_name;

  if (!isset($args->slug) || ($args->slug != $plugin_slug))
    return false;

  // Get the current version
  $plugin_info = get_site_transient('update_plugins');
  $current_version = $plugin_info->checked[$plugin_slug .'/'. $plugin_name .'.php'];
  $args->version = $current_version;

  $request_string = array(
      'body' => array(
          'action' => $action,
          'request' => serialize($args)
      ),
      'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
  );

  $request = wp_remote_post($api_url, $request_string);

  if (is_wp_error($request)) {
    $res = new WP_Error('plugins_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 {
    if ($request['response']['message'] == 'OK' || $request['response']['code'] == 200){
      $res = unserialize($request['body']);
    } else {
      $res = new WP_Error('plugins_api_failed', __('An unknown error occurred'), $request['body']);
    }
  }

  return $res;
}
PhilipCrumpton commented 5 years ago

I was having a similar problem with a theme update. My problem was the folder name inside the ZIP did not match the active theme directory exactly.

WordPress theme directory: "my-theme" Update ZIP file name: "my-theme-1.1.zip" Theme directory inside the ZIP: "my-theme-1.1"

This caused the problem. I fixed it by making the directory inside the ZIP match exactly: "my-theme"

Perhaps it's a similar issue for the plugins, too.