Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CloudConvert API in WordPress

I am trying to use the CloudConvert API within a custom WordPress plugin. What I'd like to do is initialise all of the CloudConvert API stuff just once, on activation, and then be able to use it multiple times as needed for file conversions. Specifically, I want to put a file conversion form on a WooCommerce product page using a hook.

Here's my custom plugin code:

require __DIR__ . '/vendor/autoload.php';
use \CloudConvert\Api;

function add_file_conversion()
{
    $api = new Api("---");

    $psd_process = $api->createProcess([
        "inputformat" => "psd",
        "outputformat" => "png",
    ]);

    $psd_process->start([
        "input" => "upload",
        "outputformat" => "png",
        "download" => true
    ]);
    ?>
    <form id="files-convert-form" action="<?php echo $psd_process->upload->url; ?>">
        <label>If you have a PSD or PDF file, please convert it </label>
        <input type="file" name="myfile" id="files-convert" autocomplete="off"/>
        <button type="button" class="btn btn-primary" id="action-convert">Convert</button>
    </form>
    <?php
}
add_action('woocommerce_single_product_summary', 'add_file_conversion', 25);

The PHP Wrapper at https://github.com/cloudconvert/cloudconvert-php seemed the way to go, so I tried using the PHAR as they recommend, both as a package and unpackaged (as the code shows currently). I don't have much experience using PHARs so it took me a while to figure out that it needs to go in /wp-admin for it to be found - I didn't have any luck setting the include path to get it to look in my plugin directory.

Anyway, both the packaged PHAR and unpacked autoload.php files seem to load ok in that the plugin doesn't throw any errors on activation. Instead, when the add_file_conversion() method is run, I get this error:

[12-Dec-2017 11:17:58 UTC] PHP Fatal error:  Class 'CloudConvert\Api' not found in /.../wp-content/plugins/cloudconvert-puzzlepic/cloudconvert-puzzlepic.php on line 19

where line 19 is the call to 'new API("---")'

I also tried making $api global and moving its initialisation up directly under the 'use' statement, but then calling it within the add_file_conversion() function throws an error because $api is null.

I feel like it should be so simple but have not found a way to get it working yet. Can anyone help please?

like image 461
Jo Batkin Avatar asked Dec 12 '17 11:12

Jo Batkin


1 Answers

Just tested on my end, and it's working fine. Looks like something went wrong with the installation. Here are steps to follow to get it working:

  1. In the plug-in root directory create composer.json file with the following content:

    {
        "name": "Example Application",
        "description": "This is an example",
        "require": {
            "cloudconvert/cloudconvert-php": "2.2.*"
        }
    }
    
  2. run composer install command on the same directory (in case you haven't installed composer, you can download and install from here). It will install cloudconvert with dependencies into the vendor directory, and will create vendor/autoload.php which you will include in your project.

  3. require autoload.php from your plug-in:

    require __DIR__ . '/vendor/autoload.php';
    use \CloudConvert\Api;
    $api = new Api("your_api_key");
    var_dump($api); exit;
    

You can wrap the functionality you use cloudconvert in a class, attach hooks, and include that file whenever you need to operate with.

like image 72
Avag Sargsyan Avatar answered Nov 15 '22 22:11

Avag Sargsyan