Created
May 31, 2016 12:08
-
-
Save y-krn/7a628a240d62e430549359ead9fd79ee 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 | |
new External_Link_Builder(); | |
/** | |
* Class Name: External Link Builder | |
*@author @ottanxyz | |
*/ | |
class External_Link_Builder { | |
function __construct() { | |
wp_embed_register_handler( | |
'external_link_builder', | |
'/(http(s?):\\/\\/.*)$/i', | |
array( &$this, 'handler' ) | |
); | |
add_shortcode( 'external_links', array( &$this, 'display' ) ); | |
} | |
public function handler( $matches, $attr, $url, $rawattr ) { | |
$args = preg_split( '/#/', $matches[1] ); | |
return "[external_links url=\"{$args[0]}\"]"; | |
} | |
public function display( $p ) { | |
if ( ! isset( $p['url'] ) || ! $p['url'] ) { | |
return; | |
} | |
if ( false === ( $results = get_transient( 'external-' . urlencode( $p['url'] ) ) ) ) { | |
require_once dirname( __FILE__ ) . '/../lib/OpenGraph.php'; | |
$graph = OpenGraph::fetch( $p['url'] ); | |
global $post; | |
$results = array(); | |
if ( isset( $graph->image ) ) { | |
$image_url = $this->_media_handle_sideload( $graph->image, $post->ID ); | |
} else { | |
$image_url = get_template_directory_uri() . '/img/m_e_others_501.png'; | |
} | |
if ( isset( $graph->site_name ) ) { | |
$site_name = $graph->site_name; | |
} else { | |
$site_name = parse_url( $p['url'] ); | |
$site_name = $site_name['host']; | |
} | |
$results['image_url'] = $image_url; | |
$results['permalink'] = $p['url']; | |
$results['title'] = $graph->title; | |
$results['description'] = $graph->description; | |
$results['site_name'] = $site_name; | |
set_transient( 'external-' . urlencode( $p['url'] ), $results, YEAR_IN_SECONDS ); | |
} | |
$permalink = $results['permalink']; | |
$title = $results['title']; | |
$image_url = $results['image_url']; | |
$site_name = $results['site_name']; | |
if ( isset( $results['description'] ) ) { | |
$description = mb_substr( str_replace( array( "\r\n", "\r", "\n" ), '', strip_tags( $results['description'] ) ), 0, 120 ) . '... '; | |
} else { | |
$description = ''; | |
} | |
return <<< EOM | |
<div class="wp-embed"> | |
<div class="wp-embed-featured-image square"> | |
<a href="${permalink}" target="_top"> | |
<img src="${image_url}" class="attachment-top-thumbnail size-top-thumbnail" alt="${title}"></a> | |
</div> | |
<p class="wp-embed-heading"><a href="${permalink}" target="_top">${title}</a></p> | |
<div class="wp-embed-excerpt"><p>${description}<a href="${permalink}" class="wp-embed-more" target="_top">続きを読む</a></p></div> | |
<div class="wp-embed-footer"> | |
<div class="wp-embed-site-title"><a href="${permalink}" target="_top"><span>${site_name}</span></a></div> | |
</div> | |
</div> | |
EOM; | |
} | |
private function _media_handle_sideload( $url, $post_id ) { | |
if ( ! function_exists( 'media_handle_sideload' ) ) { | |
require_once ABSPATH . 'wp-admin/includes/file.php'; | |
require_once ABSPATH . 'wp-admin/includes/media.php'; | |
require_once ABSPATH . 'wp-admin/includes/image.php'; | |
} | |
/* | |
* Build the $file_array with | |
* $url = the url of the image | |
* $temp = storing the image in wordpress | |
*/ | |
$url = explode( '?', $url ); | |
$tmp = download_url( $url[0] ); | |
$file_array = array( | |
'name' => basename( $url[0] ), | |
'tmp_name' => $tmp, | |
); | |
/** | |
* Check for download errors | |
* if there are error unlink the temp file name | |
*/ | |
if ( is_wp_error( $tmp ) ) { | |
unlink( $file_array['tmp_name'] ); | |
return $tmp; | |
} | |
/** | |
* now we can actually use media_handle_sideload | |
* we pass it the file array of the file to handle | |
* and the post id of the post to attach it to | |
* $post_id can be set to '0' to not attach it to any particular post | |
*/ | |
$id = media_handle_sideload( $file_array, $post_id ); | |
/** | |
* We don't want to pass something to $id | |
* if there were upload errors. | |
* So this checks for errors | |
*/ | |
if ( is_wp_error( $id ) ) { | |
unlink( $file_array['tmp_name'] ); | |
return $id; | |
} | |
/** | |
* No we can get the url of the sideloaded file | |
* $value now contains the file url in WordPress | |
* $id is the attachment id | |
*/ | |
return wp_get_attachment_url( $id ); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment