Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. /*
  3. Plugin Name: Test plugin
  4. Description: A test plugin to demonstrate wordpress functionality with BuildVu
  5. Author: Amy Pearson
  6. Maintainer: Xinyu Zhang
  7. Version: 2.0
  8. */
  9.  
  10. require_once(plugin_dir_path(__FILE__) . 'IDRCloudClient.php');
  11. use IDRsolutions\IDRCloudClient;
  12.  
  13. add_action('admin_menu', 'test_plugin_setup_menu');
  14.  
  15. function test_plugin_setup_menu(){
  16.     add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init' );
  17. }
  18.  
  19. function test_init(){
  20.     test_handle_post();
  21. ?>
  22.     <h1>Hello World!</h1>
  23.     <h2>Upload a File</h2>
  24.     <!-- Form to handle the upload - The enctype value here is very important -->
  25.     <form  method="post" enctype="multipart/form-data">
  26.         <input type='file' id='test_upload_pdf' name='test_upload_pdf'></input>
  27.         <?php submit_button('Upload') ?>
  28.     </form>
  29. <?php
  30. }
  31.  
  32. function test_handle_post(){
  33.     // First check if the file appears on the _FILES array
  34.     if(isset($_FILES['test_upload_pdf'])){
  35.         $pdf = $_FILES['test_upload_pdf'];
  36.  
  37.         // Use the wordpress function to upload
  38.         // test_upload_pdf corresponds to the position in the $_FILES array
  39.         // 0 means the content is not associated with any other posts
  40.         $uploaded=media_handle_upload('test_upload_pdf', 0);
  41.         // Error checking using WP functions
  42.         if(is_wp_error($uploaded)){
  43.             echo "Error uploading file: " . $uploaded->get_error_message();
  44.         }else{
  45.             echo "File upload successful!";
  46.             test_convert($uploaded);
  47.         }
  48.     }
  49. }
  50.  
  51. function test_convert($id) {
  52.     // Get the file and post details
  53.     $file = get_attached_file($id);
  54.     $post = get_post($id);
  55.  
  56.     // Set the endpoint for the online converter
  57.     $endpoint = "https://cloud.idrsolutions.com/cloud/" . IDRCloudClient::INPUT_BUILDVU;
  58.  
  59.     $filePath = realpath($file);
  60.     $fileName = pathinfo($filePath, PATHINFO_FILENAME); // Get the filename without extension
  61.  
  62.      // This is where the output will be written to
  63.     $outputdir = preg_replace("[\\/]", DIRECTORY_SEPARATOR, plugin_dir_path(__FILE__)) . "output" . DIRECTORY_SEPARATOR . $fileName . DIRECTORY_SEPARATOR;
  64.  
  65.     // Create the directory if it doesn't exist
  66.     if (!file_exists($outputdir)) {
  67.         mkdir($outputdir, 0777, true);
  68.     }
  69.  
  70.     $conversion_params = array(
  71.         'token' => $your_token,
  72.         'input' => IDRCloudClient::INPUT_UPLOAD,
  73.         'file' => $filePath
  74.     );
  75.  
  76.     $results = IDRCloudClient::convert(array(
  77.         'endpoint' => $endpoint,
  78.         'parameters' => $conversion_params
  79.     ));
  80.  
  81.     // This method is very important as it allows us access to the file system
  82.     WP_Filesystem();
  83.  
  84.     IDRCloudClient::downloadOutput($results, $outputdir);
  85.  
  86.     $downloadUrl = $results['downloadUrl'];
  87.     $basename = pathinfo($downloadUrl, PATHINFO_BASENAME);
  88.  
  89.     // Unzip the file
  90.     $result = unzip_file($outputdir . $basename, $outputdir);
  91. }
  92.