Last active
August 13, 2016 03:39
-
-
Save y-krn/1f0bf483156ea9b72952ed9885d0faea 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 Google_Play_Link_Builder(); | |
/** | |
* Class Name: Google Play Link Builder | |
*@author @ottanxyz | |
*/ | |
class Google_Play_Link_Builder { | |
function __construct() { | |
wp_embed_register_handler( | |
'google_play_link_builder', | |
'/https:\\/\\/play\\.google\\.com\\/store\\/apps\\/details\\?id=(.*)$/i', | |
array( &$this, 'handler' ) | |
); | |
add_shortcode( 'google-play', array( &$this, 'display' ) ); | |
} | |
public function handler( $matches, $attr, $url, $rawattr ) { | |
return "[google-play id=\"{$matches[1]}\"]"; | |
} | |
public function display( $p ) { | |
if ( ! isset( $p['id'] ) ) { | |
return; | |
} | |
if ( false === ( $results = get_transient( 'google-play-' . $p['id'] ) ) ) { | |
require_once dirname( __FILE__ ) . '/../lib/simple_html_dom.php'; | |
$html = file_get_html( 'https://play.google.com/store/apps/details?id=' . $p['id'] ); | |
$results['url'] = 'https://play.google.com/store/apps/details?id=' . $p['id']; | |
$results['cover_image'] = $html->find( '.main-content img[itemprop=image]' )[0]->src; | |
$results['id_app_title'] = $html->find( '.main-content div[itemprop=name]' )[0]->plaintext; | |
$results['description'] = $html->find( '.main-content div[itemprop=description]' )[0]->plaintext; | |
$results['author'] = $html->find( '.main-content div[itemprop=author] span[itemprop=name]' )[0]->plaintext; | |
$results['price'] = $html->find( '.main-content span[itemprop=offers] meta[itemprop=price]' )[0]->content; | |
$html->clear(); | |
set_transient( 'google-play-' . $p['id'], $results, DAY_IN_SECONDS ); | |
} | |
$url = $results['url']; | |
$cover_image = $results['cover_image']; | |
$id_app_title = $results['id_app_title']; | |
$author = $results['author']; | |
$price = $results['price']; | |
if ( '0' == $price ) { | |
$price = '無料'; | |
} | |
$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="${id_app_title}" sl-processed="1"><img src="${cover_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="${id_app_title}" class="apptitle" sl-processed="1">${id_app_title}</a></div> | |
<div class="developer">Developer: ${author}</div> | |
<div class="price">Price: ${price}</div> | |
</div> | |
</div> | |
EOM; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment