Created
August 31, 2016 17:18
-
-
Save gemmadlou/6eff9c3d1c944e9d2130cc501fc5a824 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Save post Hook | |
function grab_fb_video_image( $post_id, $post) { | |
if ( wp_is_post_revision( $post_id ) ) | |
return; | |
$image = get_field('featured_image'); | |
if ($image || is_null($image)) { // No need to continue - image is already there, or no field exists | |
return; | |
} | |
preg_match('/videos%2F([^"]+)%2F/', $post->post_content, $match); | |
if (!isset($match[1])) { | |
return; | |
} | |
$videoId = $match[1]; | |
$fb = new Facebook\Facebook([ | |
'app_id' => FB_APP_ID, | |
'app_secret' => FB_APP_SECRET, | |
'default_graph_version' => 'v2.7', | |
]); | |
// Temporary access token for now 60 days - it's barebones access - | |
// PUBLIC data only - so not worried about including this in repository | |
$userAccessToken = 'EAAJfdgJVlNEBAMHpqsi6HAFtPpWUQFzlZCPnRd934fuz1ZBhBZB7wdgowF7gdOY9aoyDYuxZC4KaSlq23JqKBKOgE9zB3sRVqzMEu5YNgDRofZBzFvCtuAmow2lH3sqLCm55aT3K3YKC8zd7UoKZAw'; | |
$response = $fb->get("/$videoId/thumbnails", $userAccessToken); | |
$videoJSON = $response->getBody(); | |
// @TODO - check if is not JSON | |
if (!$videoJSON) { | |
return; | |
} | |
$videoData = json_decode($videoJSON); | |
if (!isset($videoData->data[0])) { | |
return; | |
} | |
$tmp = download_url( $videoData->data[0]->uri ); | |
$file_array = array( | |
'name' => basename( strtok($videoData->data[0]->uri, '?') ), | |
'tmp_name' => $tmp | |
); | |
if ( is_wp_error( $tmp ) ) { | |
@unlink( $file_array[ 'tmp_name' ] ); | |
return $tmp; | |
} | |
$free_post_id = 0; // 0 not to attach it to any post | |
$id = media_handle_sideload( $file_array, $free_post_id ); | |
if ( is_wp_error( $id ) ) { | |
@unlink( $file_array['tmp_name'] ); | |
return $id; | |
} | |
$absoluteFileURI = get_attached_file($id); | |
$image = wp_get_image_editor( $absoluteFileURI ); | |
if ( ! is_wp_error( $image ) ) { | |
$current_size = $image->get_size(); | |
$smallest_size = min($current_size); | |
//$image->resize( $smallest_size, $smallest_size , true); | |
$image->crop(0, 0, $smallest_size, $smallest_size); | |
$filename = $image->generate_filename(); | |
$image->save( $filename ); | |
// Attachment Creation - Needed to add this to Media Uploads Library | |
$filetype = wp_check_filetype( basename( $filename), null ); | |
$wp_upload_dir = wp_upload_dir(); | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), | |
'post_mime_type' => $filetype['type'], | |
'post_title' => get_the_title($post_id), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
$uploadDir = $wp_upload_dir['path'] . '/' . basename( $filename ); | |
$attach_id = wp_insert_attachment( $attachment, $uploadDir, $post_id ); | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
// End of attachment creation | |
// Finally add this to ACF field | |
update_field('featured_image', $attach_id, $post_id); | |
} | |
} | |
add_action( 'save_post', 'grab_fb_video_image', 10, 3 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment