Last active
September 2, 2016 19:13
-
-
Save shackep/0f75f5051b6de438032816cbff251801 to your computer and use it in GitHub Desktop.
Refactored Auto Featured Image.
This file contains 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 | |
class First_Image_To_Thumbnail{ | |
public function __construct(){ | |
add_action('transition_post_status', array($this, 'autoset_featured'), 10, 3 ); | |
} | |
public function search_post_content_first_item( $tag, $post ){ | |
$dom = new DOMDocument(); | |
@$dom->loadHTML( $post->post_content ); | |
$search_dom = new DOMXPath($dom); | |
$found_tag = $search_dom->query('//'.$tag.''); | |
$is_found_tag = $found_tag->length; | |
if ( !$is_found_tag ) { | |
return FALSE; | |
} | |
return $found_tag; | |
} | |
public function item_has_class($found_tag,$class_to_match){ | |
$item_class = $found_tag->item(0)->getAttribute( 'class' ); | |
$do_classes_match = strpos( $item_class, $class_to_match ); | |
if($do_classes_match == FALSE ){ | |
return FALSE;} | |
else{ | |
return TRUE; | |
} | |
} | |
public function side_load_found_image_as_thumb($found_image, $post){ | |
$image_src = $found_image->item(0)->getAttribute( 'src' ); | |
$tmp = download_url( $image_src ); | |
$file_array = array( | |
'name' => basename( $image_src ), | |
'tmp_name' => $tmp | |
); | |
if ( is_wp_error( $tmp ) ) { | |
@unlink( $file_array[ 'tmp_name' ] ); | |
return $tmp; | |
} | |
$added_attachment = media_handle_sideload($file_array, $post->ID); | |
set_post_thumbnail( $post->ID, $added_attachment ); | |
return; | |
} | |
public function match_found_image_with_attached( $found_image, $post ){ | |
$attached_images = get_attached_media( 'image' ); | |
$imageclass = $found_image->item(0)->getAttribute( 'class' ); | |
foreach ( $attached_images as $attachment_id => $attachment ) { | |
$current_attachment = 'wp-image-' . $attachment_id; | |
$first_attached_image = strpos( $imageclass, $current_attachment ); | |
if ( $first_attached_image !== FALSE ) { | |
return $attachment_id; | |
} | |
return $first_attached_image; | |
} | |
} | |
public function guess_attachment_based_on_class($found_image){ | |
$imageclass = $found_image->item(0)->getAttribute( 'class' ); | |
$guess_attach_id = explode( ' wp-image-',$imageclass ); | |
$maybe_attachment = $guess_attach_id[1]; | |
$is_attachment_image = wp_attachment_is_image( $maybe_attachment ); | |
if( !$is_attachment_image ){ | |
return $is_attachment_image; | |
} | |
return $maybe_attachment; | |
} | |
public function should_autoset_run($new_status, $old_status, $post){ | |
if ( $new_status != 'publish' || wp_is_post_autosave( $post->ID ) || $post->post_type != 'post' || empty( $post->post_content || has_post_thumbnail( $post->ID ) )) { | |
return FALSE; | |
} | |
$already_has_thumb = has_post_thumbnail( $post->ID ); | |
if($already_has_thumb){ | |
return FALSE; | |
} | |
return TRUE; | |
} | |
public function autoset_featured( $new_status, $old_status, $post ) { | |
if(!$this->should_autoset_run($new_status, $old_status, $post)){ | |
return; | |
} | |
$found_image = $this->search_post_content_first_item('img', $post ); | |
if(false == $found_image){ | |
return; | |
} | |
if(!$this->item_has_class($found_image,'wp-image-')){ | |
$this->side_load_found_image_as_thumb($found_image, $post); | |
return; | |
} | |
$attached_images = get_attached_media( 'image' ); | |
if ( $attached_images ) { | |
$attachment_match = $this->match_found_image_with_attached( $found_image, $post ); | |
if($attachment_match !== FALSE ){ | |
set_post_thumbnail( $post->ID, $attachment_match ); | |
return; | |
} | |
} | |
$maybe_attachment = $this->guess_attachment_based_on_class($found_image); | |
if(!$maybe_attachment == false){ | |
set_post_thumbnail( $post->ID, $maybe_attachment ); | |
} | |
return; | |
} | |
} | |
$add_thumbnails = new First_Image_To_Thumbnail(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment