airesvsg / acf-to-rest-api

Exposes Advanced Custom Fields Endpoints in the WordPress REST API
https://wordpress.org/plugins/acf-to-rest-api/
1.32k stars 111 forks source link

ACF Get Twitter media from oEmbed field #34

Closed mnguye08 closed 8 years ago

mnguye08 commented 8 years ago

Hi, I've a question about ACF & oEmbed field. Is there a way to retrieve media url from a Twitter status in an oEmbed field (by modifying/overriding the JSON response)? Thanks for your feedback. Kind regards

airesvsg commented 8 years ago

Hi @mnguye08, what's the URL? Thanks

mnguye08 commented 8 years ago

Hi @airesvsg, for example, with this tweet https://twitter.com/lequipe/status/734278660237070336?lang=fr, the code generated by the oEmbed field is

<blockquote class=\"twitter-tweet\" data-width=\"220\"><p lang=\"fr\" dir=\"ltr\">PSG : Zlatan Ibrahimovic honoré par la mairie de Paris <a href=\"https://t.co/kzhVSr0EPz\">https://t.co/kzhVSr0EPz</a> <a href=\"https://t.co/IlH1EbKfie\">pic.twitter.com/IlH1EbKfie</a></p>&mdash; L&#39;ÉQUIPE (@lequipe) <a href=\"https://twitter.com/lequipe/status/734278660237070336\">May 22, 2016</a></blockquote><script async src=\"//platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>

As you can see, I can't retrieve the image url (pic.twitter.com/IlH1EbKfie redirects automatically to the Twitter status).

Thanks again for your help!

airesvsg commented 8 years ago

Hi @mnguye08, You can use the filter: acf/rest_api/{type}/get_fields and the function ( get_status_image ). Add these codes in your functions.php

function get_status_image( $html ) {
    $matches = null;

    if ( preg_match_all( '/https:\/\/t.co\/[a-zA-Z0-9]+/', $html, $matches ) ) {
        $html = file_get_contents( end( $matches[0] ) );
        if ( preg_match( "/data-image-url=\"(.*)\"/", $html, $matches ) && count( $matches ) == 2 ) {
            return $matches[1];
        }
    }       

    return false;
}
add_filter( 'acf/rest_api/post/get_fields', function( $data ) {
    if ( method_exists( $data, 'get_data' ) ) {
        $data = $data->get_data();
    } else {
        $data = (array) $data;
    }

    if ( isset( $data['acf'] ) && isset( $data['acf']['oembed_field'] ) ) {
        $data['acf']['oembed_image'] = get_status_image( $data['acf']['oembed_field'] );
    }

    return $data;
} );

oembed_field is the acf field name.

Result: acf-oembed-rest-api

Thanks

mnguye08 commented 8 years ago

Hi @airesvsg, thanks to take time to find a solution! I've tried to use the filter /acf/rest_api/post/get_fields but got a 404 response.

{
"code": "rest_no_route",
"message": "Aucun itinéraire n’a été trouvé correspondant à l’URL et à la méthode de requête",
"data": {
"status": 404
}
}

For information (I don't know if it's linked), when I use this url "/wp-json/acf/v2/options", I have

{
"acf": false
}

An other point: the twitter oembed field named "tweet_url" by my side is under a flexible content named "footer".

Thanks again!

airesvsg commented 8 years ago

Hi @mnguye08, please change the filter to:

add_filter( 'acf/rest_api/option/get_fields', function( $data ) {
    if ( method_exists( $data, 'get_data' ) ) {
        $data = $data->get_data();
    } else {
        $data = (array) $data;
    }

    if ( isset( $data['acf'] ) && isset( $data['acf']['footer'] ) && ! empty( $data['acf']['footer'] ) ) {
        foreach( $data['acf']['footer'] as &$f ) {
            $f['tweet_url_img'] = get_status_image( $f['tweet_url'] );
        }
    }

    return $data;
} );

Thanks

mnguye08 commented 8 years ago

Hi @airesvsg, I finally use the 1st filter ("/acf/rest_api/post/get_fields") and it works like a charm! Thanks again for all your help!