Commit 1f153b4e authored by Isaque Melo's avatar Isaque Melo

#1 Wrap API with business rules

parent b526d38e
......@@ -7,24 +7,29 @@ function get_facebook_shares( $data ) {
$access_token = '693244528146031|ae2b14c2d4f31b3434212179ee5550d8';
$post_id = $data['post_id'];
$response = wp_remote_get( add_query_arg( array(
'id' => urlencode( get_permalink( $post_id ) ),
//'id' => urlencode( 'https://www.google.com/' ),
'access_token' => $access_token,
'fields' => 'engagement'
), 'https://graph.facebook.com/v3.0/' ) );
if( guaraci\need_share_update( $post_id ) ) {
$response = wp_remote_get( add_query_arg( array(
'id' => urlencode( get_permalink( $post_id ) ),
//'id' => urlencode( 'https://www.google.com/' ),
'access_token' => $access_token,
'fields' => 'engagement'
), 'https://graph.facebook.com/v3.0/' ) );
if ( !is_array($response) && is_wp_error( $response ) ) {
return false;
}
if ( !is_array($response) && is_wp_error( $response ) ) {
return false;
}
$body = json_decode( $response['body'] );
$count = intval( $body->engagement->share_count );
$body = json_decode( $response['body'] );
$count = intval( $body->engagement->share_count );
// Update post meta
guaraci\set_post_shares( $post_id, $count );
} else {
$count = guaraci\get_post_shares( $post_id );
}
// Update post meta
guaraci\set_post_shares( $post_id, $count );
return $count;
}
......
......@@ -106,7 +106,7 @@ function get_posts_ranking() {
$cache_key = 'most_viewed_within_hour';
// use wp cache for optimization
$ids = get_transient( $cache_key );
$ids = wp_cache_get( $cache_key );
if ( $ids === false ) {
$most_viewed = \AjaxPageviews::get_top_viewed(500, ['post_type' => 'post', 'from' => strtotime('-24 hour')]);
......@@ -116,7 +116,7 @@ function get_posts_ranking() {
array_push( $ids, $value->post_id );
}
set_transient( $cache_key, $ids, 120 );
wp_cache_add( $cache_key, $ids, 120 );
}
var_dump($ids);
......
......@@ -3,11 +3,14 @@ namespace guaraci;
function need_share_update( $post_id ) {
// se o metadado não existe ou se o metadado existe e foi atualizado a mais tempo do que o intervalo (F), retorna true
if ( !metadata_exists( 'post', $post_id, 'shares' )) {
// it has to be updated
if ( !metadata_exists( 'post', $post_id, 'shares' ) ) {
// the first time its called create the metadata for the next turn
update_post_meta( $post_id, 'shares', 0 );
update_post_meta( $post_id, 'updated_timestamp', time() );
return false;
} elseif ( update_share_business_rule( $post_id ) ) {
return true;
} elseif {
// define metadate
} else {
return false;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment