Estoy tratando de crear un complemento de Wordpress que exporte cada publicación de blog a mi carpeta de Dropbox. El código está abajo pero tengo un problema.
Si ejecuto este código fuera de Wordpress, funciona perfectamente. La tabla se crea y mi token se almacena. Si utilizo un navegador diferente, funciona perfectamente ... sin autenticación porque se lee desde la base de datos.
Mi problema es que cuando pongo esto en mi complemento, la tabla no se crea en absoluto. Entonces, si voy a un navegador diferente, debo volver a autenticarme.
Ayuda por favor.
Código:
/*
* 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');
?>