XinyuZhang

test-plugin.php

Dec 17th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  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: 0.1.1
  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
  53.     $file = get_attached_file($id);
  54.     $post = get_post($id);  // Get the post object
  55.  
  56.     // set the endpoint to the online converter
  57.     $endpoint = "https://cloud.idrsolutions.com/cloud/" . IDRCloudClient::INPUT_BUILDVU;
  58.  
  59.     $filePath = realpath($file);
  60.  
  61.     // plugin_dir_path(__FILE__) gets the location of the plugin directory
  62.     // Using preg replace to replace the directory seperators with the correct type
  63.     // This is where the output will be written to
  64.     // $outputdir = preg_replace("[\\/]", DIRECTORY_SEPARATOR, plugin_dir_path(__FILE__)) . "output".DIRECTORY_SEPARATOR. $file->post_title. DIRECTORY_SEPARATOR;
  65.     $outputdir = preg_replace("[\\/]", DIRECTORY_SEPARATOR, plugin_dir_path(__FILE__)) . "output" . DIRECTORY_SEPARATOR . $post->post_title . DIRECTORY_SEPARATOR;
  66.  
  67.     echo $outputdir;
  68.     if (!file_exists($outputdir)) {
  69.         mkdir($outputdir, 0777, true);
  70.     }
  71.  
  72.     $conversion_params = array(
  73.     'token' => $your_token,
  74.     'input' => IDRCloudClient::INPUT_UPLOAD,
  75.     'file' => $filePath
  76.     );
  77.  
  78.     $results = IDRCloudClient::convert(array(
  79.      'endpoint' => $endpoint,
  80.      'parameters' => $conversion_params
  81.     ));
  82.         // This method is very important as it allows us access to the file system
  83.         WP_Filesystem();
  84.  
  85.         IDRCloudClient::downloadOutput($results, $outputdir);
  86.         // Unzip the file
  87.         $result=unzip_file($outputdir.$post->post_title.".zip", $outputdir);
  88. }
  89.  
  90. ?>
Tags: php wordpress
Advertisement
Add Comment
Please, Sign In to add comment