Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Test plugin
- Description: A test plugin to demonstrate wordpress functionality with BuildVu
- Author: Amy Pearson
- Maintainer: Xinyu Zhang
- Version: 0.1.1
- */
- 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
- $file = get_attached_file($id);
- $post = get_post($id); // Get the post object
- // set the endpoint to the online converter
- $endpoint = "https://cloud.idrsolutions.com/cloud/" . IDRCloudClient::INPUT_BUILDVU;
- $filePath = realpath($file);
- // plugin_dir_path(__FILE__) gets the location of the plugin directory
- // Using preg replace to replace the directory seperators with the correct type
- // This is where the output will be written to
- // $outputdir = preg_replace("[\\/]", DIRECTORY_SEPARATOR, plugin_dir_path(__FILE__)) . "output".DIRECTORY_SEPARATOR. $file->post_title. DIRECTORY_SEPARATOR;
- $outputdir = preg_replace("[\\/]", DIRECTORY_SEPARATOR, plugin_dir_path(__FILE__)) . "output" . DIRECTORY_SEPARATOR . $post->post_title . DIRECTORY_SEPARATOR;
- echo $outputdir;
- 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);
- // Unzip the file
- $result=unzip_file($outputdir.$post->post_title.".zip", $outputdir);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment