I am trying to create a Wordpress plugin that exports each blog post to my dropbox folder. The code is below but I have a problem.
If I run this code outside Wordpress, it works perfectly. The table is created and my token is stored. If I use a different browser it works perfectly...no authentication because it reads from the DB.
My problem is that when I put this into my plugin, the table is not created at all. So if I go to a different browser, I have to re-authenticate.
Help please.
Code:
/*
* Copyright 2012 Erin Dalzell.
*
*/
require_once('Dropbox/API.php');
require_once('Dropbox/Exception.php');
require_once('Dropbox/OAuth/Consumer/ConsumerAbstract.php');
require_once('Dropbox/OAuth/Consumer/Curl.php');
require_once('Dropbox/OAuth/Storage/Encrypter.php');
require_once('Dropbox/OAuth/Storage/StorageInterface.php');
require_once('Dropbox/OAuth/Storage/Session.php');
require_once('Dropbox/OAuth/Storage/Filesystem.php');
require_once('Dropbox/OAuth/Storage/PDO.php');
include ABSPATH . '/wp-config.php';
function etd_initialize() {
global $current_user;
// Set your consumer key, secret and callback URL
// should be in the settings
$key = 'xxxxx';
$secret = 'yyyyy';
$current_user = wp_get_current_user();
// Check whether to use HTTPS and set the callback URL
$protocol = (!empty($_SERVER['HTTPS'])) ? 'https' : 'http';
$callback = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// Instantiate the Encrypter and storage objects
$encrypter = new \Dropbox\OAuth\Storage\Encrypter('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
// Instantiate the database data store and connect
$storage = new \Dropbox\OAuth\Storage\PDO($encrypter, 1);
$storage->connect(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);
$storage = new \Dropbox\OAuth\Storage\Session($encrypter);
$OAuth = new \Dropbox\OAuth\Consumer\Curl($key, $secret, $storage, $callback);
$dropbox = new \Dropbox\API($OAuth);
return $dropbox;
}
function export_to_dropbox($id) {
$dropbox = etd_initialize();
$post = get_post($id);
// Create a temporary file and write some data to it
$tmp = tempnam('/tmp','dropbox');
file_put_contents($tmp, $post->post_content);
// Upload the file with an alternative filename
$put = $dropbox->putFile($tmp, $post->post_title . '.md');
// Unlink the temporary file
unlink($tmp);
}
register_activation_hook( __FILE__, 'etd_initialize' );
add_action('publish_post', 'export_to_dropbox');
?>