<?php
/*
Plugin Name: Test plugin
Description: A test plugin to demonstrate wordpress functionality with BuildVu
Author: Amy Pearson
Maintainer: Xinyu Zhang
Version: 2.0
*/
require_once(plugin_dir_path(__FILE__) . 'IDRCloudClient.php');
use IDRsolutions\IDRCloudClient;
add_action('admin_menu', 'test_plugin_setup_menu');
function test_plugin_setup_menu(){
add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init' );
}
function test_init(){
test_handle_post();
?>
<h1>Hello World!</h1>
<h2>Upload a File</h2>
<!-- Form to handle the upload - The enctype value here is very important -->
<form method="post" enctype="multipart/form-data">
<input type='file' id='test_upload_pdf' name='test_upload_pdf'></input>
<?php submit_button('Upload') ?>
</form>
<?php
}
function test_handle_post(){
// First check if the file appears on the _FILES array
if(isset($_FILES['test_upload_pdf'])){
$pdf = $_FILES['test_upload_pdf'];
// Use the wordpress function to upload
// test_upload_pdf corresponds to the position in the $_FILES array
// 0 means the content is not associated with any other posts
$uploaded=media_handle_upload('test_upload_pdf', 0);
// Error checking using WP functions
if(is_wp_error($uploaded)){
echo "Error uploading file: " . $uploaded->get_error_message();
}else{
echo "File upload successful!";
test_convert($uploaded);
}
}
}
function test_convert($id) {
// Get the file and post details
$file = get_attached_file($id);
$post = get_post($id);
// Set the endpoint for the online converter
$endpoint = "https://cloud.idrsolutions.com/cloud/" . IDRCloudClient::INPUT_BUILDVU;
$filePath = realpath($file);
$fileName = pathinfo($filePath, PATHINFO_FILENAME); // Get the filename without extension
// This is where the output will be written to
$outputdir = preg_replace("[\\/]", DIRECTORY_SEPARATOR, plugin_dir_path(__FILE__)) . "output" . DIRECTORY_SEPARATOR . $fileName . DIRECTORY_SEPARATOR;
// Create the directory if it doesn't exist
if (!file_exists($outputdir)) {
mkdir($outputdir, 0777, true);
}
$conversion_params = array(
'token' => $your_token,
'input' => IDRCloudClient::INPUT_UPLOAD,
'file' => $filePath
);
$results = IDRCloudClient::convert(array(
'endpoint' => $endpoint,
'parameters' => $conversion_params
));
// This method is very important as it allows us access to the file system
WP_Filesystem();
IDRCloudClient::downloadOutput($results, $outputdir);
$downloadUrl = $results['downloadUrl'];
$basename = pathinfo($downloadUrl, PATHINFO_BASENAME);
// Unzip the file
$result = unzip_file($outputdir . $basename, $outputdir);
}