function tb_download_package($item_number) { global $tb_package_names; if ($item_number <= 0) { echo __('ID of the addon to install not provided', 'tweetblender') . '... ' . __('aborting', 'tweetblender'); return; } // get user-friendly package name $package_name = $tb_package_names[$item_number]; // Show status updates on screen echo "<h3>" . sprintf(__('Installing %s for Tweet Blender','tweetblender'), $package_name) . "</h3>\n"; // get purchase transaction id $txn_id = tb_get_txn_id($item_number); if (!isset($txn_id) || strlen($txn_id) < 10) { echo __('Can not confirm purchase', 'tweetblender') . '... ' . __('aborting', 'tweetblender'); return; } // Download echo __("Downloading package",'tweetblender') . "... \n"; $response = wp_remote_get('http://tweetblender.com/download.php?item_number=' . $item_number . '&blog_url=' . urlencode(get_bloginfo('url')) . '&txn_id=' . $txn_id); // if couldn't download - report error if(is_wp_error($response)) { echo __('not able to download', 'tweetblender') . '. ' . __('Error', 'tweetblender') . ': ' . $response->get_error_message(); return; } // else, if validation error elseif(isset($response['headers']['validation-error'])) { echo __('not able to download', 'tweetblender') . '. ' . __('Error', 'tweetblender') . ': ' . $response['headers']['validation-error']; return; } // else - proceed to save else { $package_file_name = $response['headers']['package-file-name']; // form file name $file_name = WP_PLUGIN_DIR . '/'. $package_file_name . '.zip'; // if couldn't save - report error if (file_put_contents($file_name,wp_remote_retrieve_body($response)) === false) { echo sprintf(__("unable to save file %s. Check directory permissions",'tweetblender'),$file_name); return; } // else - proceed to unzip else { echo __('done', 'tweetblender') . "<br />"; // Unpack echo __('Unpacking', 'tweetblender') . "... \n"; class_exists('PclZip') || include_once dirname(__FILE__) . '/pclzip.lib.php'; $archive = new PclZip($file_name); // if can't unzip - report error if (($v_result_list = $archive->extract(PCLZIP_OPT_SET_CHMOD, 0777, PCLZIP_OPT_PATH, WP_PLUGIN_DIR)) == 0) { echo __('unable to unzip', 'tweetblender') . '. ' . __('Error','tweetblender') . ': ' . $archive->get_error_message(); return; } // else - proceed to activate else { echo __('done', 'tweetblender') . "<br />"; // clean up by removing zip file unlink($file_name); // Activate echo __('Activating', 'tweetblender') . "... \n"; $activation = activate_plugin(WP_PLUGIN_DIR . '/' . $package_file_name . '/' . $package_file_name . '.php'); // if can't activate - report error if (is_wp_error($activation)) { echo __('unable to activate, please try to do it manually', 'tweetblender') . '. ' . __('Error', 'tweetblender') . ': ' . $activation->get_error_message(); return; } // else - wrap it up else { echo __('done', 'tweetblender') . "<br />"; /* TODO: change permissions to the same as ours $info = stat(__FILE__); $info['uid']; $info['gid']; */ // Done, link to admin $url = tb_get_current_page_url(); $url = str_replace('&install_addon=1', '', $url); echo __('All Done!', 'tweetblender') . "<br /><br /><a href='$url'>" . __('Start using', 'tweetblender') . ' ' . $package_name . "</a><br /><br />"; } } } } }