Wordpress API



  • Hey everyone-

    1. How does Media Cloud behave when using the wordpress api for creating products, adding media etc?

    2. Does media cloud expose any api endpoints via the wordpress api?

    Regards,
    Chad



  • @wholesalesolar

    Media Cloud is "hooked" into WordPress. So long as your creating attachments via any of the WordPress api methods, Media Cloud should handle it just fine.

    There's no API for Media Cloud to expose as we try to have the plugin work as transparently with WordPress as possible.



  • Hey Jon-
    Thanks for the info.

    We are building data integrations to legacy systems via rest api's.

    Specifically, we would like the ability to place files directly into google cloud storage, and then tell media cloud to import a specific image/pdf/etc from gcs to wordpress.
    Then we get the id(s), an are able to associate those to the products as we create/update them via wp api.

    1. Is there a way to hit your "wp cli import from cloud" via rest api?
    2. And to point it to a specific file? (Though we could move to a directory structure like /products/{product_id}/{product_image_x)

    thx/Chad



  • @wholesalesolar

    No, Media Cloud doesn't expost anything via the Rest API.

    However, and I don't really know jack about the REST API, but:

    If you can create an attachment and attachment metadata via the REST api, to have the file automatically recognized by Media Cloud as a cloud storage file, you need to add S3 metadata. For example this is the attachment metadata for an upload to S3:

    Array
    (
        [width] => 2784
        [height] => 1856
        [file] => 2019/11/SAMPLE-00010.jpg
        [sizes] => Array
            (
                [thumbnail] => Array
                    (
                        [file] => SAMPLE-00010-150x150.jpg
                        [width] => 150
                        [height] => 150
                        [mime-type] => image/jpeg
                        [s3] => Array
                            (
                                [url] => https://somecloudstorageprovider.com/app/uploads/2019/11/19192934/SAMPLE-00010-150x150.jpg
                                [provider] => s3
                                [bucket] => your-bucket-name
                                [key] => app/uploads/2019/11/19192934/SAMPLE-00010-150x150.jpg
                                [privacy] => public-read
                                [v] => 1.0.0
                                [mime-type] => image/jpeg
                                [region] => ap-southeast-1
                            )
    
                    )
    
                [medium] => Array
                    (
                        [file] => SAMPLE-00010-300x200.jpg
                        [width] => 300
                        [height] => 200
                        [mime-type] => image/jpeg
                        [s3] => Array
                            (
                                [url] => https://somecloudstorageprovider.com/app/uploads/2019/11/19192934/SAMPLE-00010-300x200.jpg
                                [provider] => s3
                                [bucket] => your-bucket-name
                                [key] => app/uploads/2019/11/19192934/SAMPLE-00010-300x200.jpg
                                [privacy] => public-read
                                [v] => 1.0.0
                                [mime-type] => image/jpeg
                                [region] => ap-southeast-1
                            )
    
                    )
    
                [medium_large] => Array
                    (
                        [file] => SAMPLE-00010-768x512.jpg
                        [width] => 768
                        [height] => 512
                        [mime-type] => image/jpeg
                        [s3] => Array
                            (
                                [url] => https://somecloudstorageprovider.com/app/uploads/2019/11/19192934/SAMPLE-00010-768x512.jpg
                                [provider] => s3
                                [bucket] => your-bucket-name
                                [key] => app/uploads/2019/11/19192934/SAMPLE-00010-768x512.jpg
                                [privacy] => public-read
                                [v] => 1.0.0
                                [mime-type] => image/jpeg
                                [region] => ap-southeast-1
                            )
    
                    )
    
                [large] => Array
                    (
                        [file] => SAMPLE-00010-1024x683.jpg
                        [width] => 1024
                        [height] => 683
                        [mime-type] => image/jpeg
                        [s3] => Array
                            (
                                [url] => https://somecloudstorageprovider.com/app/uploads/2019/11/19192934/SAMPLE-00010-1024x683.jpg
                                [provider] => s3
                                [bucket] => your-bucket-name
                                [key] => app/uploads/2019/11/19192934/SAMPLE-00010-1024x683.jpg
                                [privacy] => public-read
                                [v] => 1.0.0
                                [mime-type] => image/jpeg
                                [region] => ap-southeast-1
                            )
    
                    )
    
            )
    
        [image_meta] => Array
            (
                [aperture] => 2.8
                [credit] => 
                [camera] => Canon EOS 5D Mark II
                [caption] => 
                [created_timestamp] => 1383149894
                [copyright] => 
                [focal_length] => 70
                [iso] => 500
                [shutter_speed] => 0.025
                [title] => 
                [orientation] => 1
                [keywords] => Array
                    (
                    )
    
            )
    
        [filesize] => 716100
        [s3] => Array
            (
                [url] => https://somecloudstorageprovider.com/app/uploads/2019/11/19192934/SAMPLE-00010.jpg
                [provider] => s3
                [bucket] => your-bucket-name
                [key] => app/uploads/2019/11/19192934/SAMPLE-00010.jpg
                [privacy] => public-read
                [v] => 1.0.0
                [mime-type] => image/jpeg
                [region] => ap-southeast-1
            )
    
    )
    

    So if you can set attachment metadata via the rest api, you simply need to add the s3 blocks. You should set the provider to google and you can skip region. But url, provider, bucket, key, mime-type, v should all be there.



  • @wholesalesolar

    If your file isn't an image though, you'll be SOL as you need to add a custom post meta called ilab_s3_info that contains the s3 data above.



  • @wholesalesolar

    So yes, for images, this is totally doable, but you'll have to do it on your end. What I'd urge you to do is upload an image on a test setup with Media Cloud and then look at the _wp_attachment_metadata for the post in the DB. This is the information you will have to send via meta field to the REST API's update media:

    https://developer.wordpress.org/rest-api/reference/media/#update-a-media-item



  • Hey Jon-

    So, heads up - Media Cloud appears to have some issues when uploading media via the WP Rest API.

    Most of the mechanics on the wordpress end work... but the http response has some PHP notice output in it that breaks the response...

    This is what shows up in the beginning of the response...
    "Notice: Undefined offset: 2 in /app/web/wp-content/plugins/ilab-media-tools-premium/classes/Tools/ToolSettings.php on line 75"

    thx/chad



  • @wholesalesolar

    Thanks, will fix in the next update.



  • @wholesalesolar

    If you want to fix it before I can push the update, which is looking like this weekend or Monday:

    On line 49'ish of classes/Storage/Driver/GoogleCloud/GoogleStorageSettings.php, change:

    'useBucketPolicyOnly' => ['mcloud-storage-bucket-policy-only', false],
    

    To:

    'useBucketPolicyOnly' => ['mcloud-storage-bucket-policy-only', null, false],
    


  • Thx Jon. I'll patch on this end, and grab the fix next week.
    👍


Log in to reply