Commit 63c21671 authored by Isaque Melo's avatar Isaque Melo

#1 Facebook module wip

parent ad6ff54d
......@@ -10,7 +10,9 @@ License: GPL2
require 'library/functions.php';
require 'library/panel-options.php';
require 'library/api.php';
require 'library/images.php';
require 'library/related-posts.php';
require 'library/authors.php';
require 'pagebuilder-components/index.php';
\ No newline at end of file
<?php
require 'modules/facebook/index.php';
// Facebook
function get_facebook_shares( $data ) {
$access_token = '1855140564616229|32c79e113db5a2911aeed2c4ca330b3f';
$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 ( !is_array($response) && is_wp_error( $response ) ) {
return false;
}
$body = json_decode( $response['body'] );
$count = intval( $body->engagement->share_count );
// Update post meta
guaraci\set_post_shares( $post_id, $count );
return $count;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'guaraci/facebook', '/shares/(?P<post_id>\d+)', array(
'methods' => 'GET',
'callback' => 'get_facebook_shares',
) );
} );
......@@ -96,4 +96,44 @@ function get_terms_order_by_posts_publish_date(string $taxonomy, int $limit = 10
$terms = array_map(function($term){ return new \WP_Term($term); }, $results);
return $terms;
}
function get_posts_ranking() {
if(!class_exists('\\AjaxPageviews')){
return;
}
$cache_key = 'most_viewed_within_hour';
// use wp cache for optimization
$ids = get_transient( $cache_key );
if ( $ids === false ) {
$most_viewed = \AjaxPageviews::get_top_viewed(500, ['post_type' => 'post', 'from' => strtotime('-24 hour')]);
$ids = array();
foreach ( $most_viewed as $post => $value ) {
array_push( $ids, $value->post_id );
}
set_transient( $cache_key, $ids, 120 );
}
var_dump($ids);
return $ids;
}
function get_post_ranking_position( $post_id ) {
$posts_ranking = get_posts_ranking(); // the function already returns a cached value
// linear search
foreach ( $posts_ranking as $key => $id ) {
if ($post_id == $id) {
//print('The post(' . $id . ') rank position is ' . ($key + 1));
return $key + 1;
}
}
return false;
}
\ No newline at end of file
<?php
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
return true;
} elseif {
// define metadate
} else {
return false;
}
}
function update_share_business_rule( $post_id ) {
// criar função que retorna o intervalo de atualização do post, dado o post_id
// intervalo - no ranking das mais lidas - posts/hora - soma total por hora
// a cada 1h - de 1 a 100 - 100 - 100
// a cada 2h - de 100 a 200 - 50 - 150
// a cada 4h - de 200 a 300 - 25 - 175
// a cada 8h - de 300 a 500 - 25 - 200
// a cada 24h - os demais posts
$rank_position = get_post_ranking_position( $post_id )?: 501; // if is not in rank, place into a regular group
$last_time_updated = get_post_meta( $post_id, 'updated_timestamp', true );
$date_obj = \DateTime::createFromFormat('U', $last_time_updated);
$time_interval = $date_obj->diff(new \DateTime('NOW'));
$hours_passed = ($time_interval->days * 24) + $time_interval->h;
var_dump($rank_position);
if ( $rank_position >= 1 && $rank_position < 100 && $hours_passed >= 1){
return true;
} elseif ( $rank_position >= 100 && $rank_position < 200 && $hours_passed >= 2) {
return true;
} elseif ( $rank_position >= 200 && $rank_position < 300 && $hours_passed >= 4) {
return true;
} elseif ( $rank_position >= 300 && $rank_position < 400 && $hours_passed >= 8) {
return true;
} else {
return false;
}
}
function set_post_shares( $post_id, $shares ) {
update_post_meta( $post_id, 'shares', $shares );
update_post_meta( $post_id, 'updated_timestamp', time() );
}
function get_post_shares( $post_id ) {
get_post_meta( $post_id, 'shares', true);
}
<?php
function add_my_theme_page() {
add_theme_page("Dados de Integração", "Dados de Integração", "manage_options", "integration-options", "theme_option_page", null, 99);
add_options_page("Dados de Integração", "Dados de Integração", "manage_options", "integration-options", "theme_option_page", null, 99);
}
add_action( 'admin_menu', 'add_my_theme_page' );
......@@ -33,7 +33,7 @@ function display_fb_app_id(){
function display_fb_app_secret() {
$fb_app_secret = get_option( 'fb_app_secret' );
echo '<input name="fb_app_secret" id="fb_app_secret" type="text" value="'. $fb_app_secret .'" class="code" /> ';
echo '<input name="fb_app_secret" id="fb_app_secret" type="password" value="'. $fb_app_secret .'" class="code" /> ';
}
function test_theme_settings(){
......
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