Created
August 13, 2016 03:41
-
-
Save y-krn/72ab71894fb68b38418736e8b3d106cc 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 Chrome_Web_Store_Link_Builder(); | |
/** | |
* Class Name: Chrome Web Store Link Builder | |
*@author @ottanxyz | |
*/ | |
class Chrome_Web_Store_Link_Builder { | |
function __construct() { | |
wp_embed_register_handler( | |
'chrome_web_store_link_builder', | |
'/https:\\/\\/chrome\\.google\\.com\\/webstore\\/detail\\/(.*)$/i', | |
array( &$this, 'handler' ) | |
); | |
add_shortcode( 'chrome-web-store', array( &$this, 'display' ) ); | |
} | |
public function handler( $matches, $attr, $url, $rawattr ) { | |
return "[chrome-web-store url=\"{$matches[0]}\"]"; | |
} | |
public function display( $p ) { | |
if ( ! isset( $p['url'] ) ) { | |
return; | |
} | |
if ( false === ( $results = get_transient( 'chrome-web-store-' . $p['url'] ) ) ) { | |
require_once dirname( __FILE__ ) . '/../lib/simple_html_dom.php'; | |
$html = file_get_html( $p['url'] ); | |
$results['url'] = $p['url']; | |
$results['name'] = $html->find( 'meta[itemprop=name]' )[0]->content; | |
$results['image'] = $html->find( 'meta[itemprop=image]' )[0]->content; | |
$results['description'] = $html->find( 'div[itemprop=description]' )[0]->plaintext; | |
$html->clear(); | |
set_transient( 'chrome-web-store-' . $p['url'], $results, YEAR_IN_SECONDS ); | |
} | |
$url = $results['url']; | |
$image = $results['image']; | |
$name = $results['name']; | |
// $description = mb_substr( str_replace( array( "\r\n", "\r", "\n" ), '', $results['description'] ), 0, 120 ); | |
return <<< EOM | |
<div class="wpappbox"> | |
<div class="appicon"> | |
<a target="_blank" rel="nofollow" href="${url}" title="${name}" sl-processed="1"><img src="${image}" scale="0" width="60"></a> | |
</div> | |
<div class="applinks"> | |
</div> | |
<div class="appdetails"> | |
<div class="apptitle"> | |
<a target="_blank" rel="nofollow" href="${url}" title="${name}" class="apptitle" sl-processed="1">${name}</a></div> | |
</div> | |
</div> | |
EOM; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment