Commit f279758a authored by Rafael Chaves Freitas's avatar Rafael Chaves Freitas

Merge branch 'develop'

parents 1e58dd95 d37de855
......@@ -9,7 +9,10 @@ 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
namespace guaraci;
require 'modules/facebook/index.php';
// Facebook
function get_facebook_shares( $data ) {
if (!get_option( 'fb_app_id' ) || !get_option( 'fb_app_secret' )) {
return false;
}
// example
// $access_token = '693244528146031|ae2b14c2d4f31b3434212179ee5550d8';
$access_token = get_option( 'fb_app_id' ). '|' . get_option( 'fb_app_secret' );
$post_id = $data['post_id'];
if( 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 ) || $response['response']['code'] != 200){
return get_post_shares( $post_id );
}
$body = json_decode( $response['body'] );
$count = intval( $body->engagement->share_count );
// Update post meta
set_post_shares( $post_id, $count );
} else {
$count = get_post_shares( $post_id );
}
return $count;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'guaraci/facebook', '/shares/(?P<post_id>\d+)', array(
'methods' => 'GET',
'callback' => 'guaraci\get_facebook_shares',
) );
} );
......@@ -96,4 +96,45 @@ 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 = wp_cache_get( $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 );
}
// stores in cache for 1 hour
wp_cache_add( $cache_key, $ids, 3600 );
}
//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
// in the fucture change namespace to facebook
namespace guaraci;
function need_share_update( $post_id ) {
if ( !metadata_exists( 'post', $post_id, 'fb_shares' ) ) {
// the first time its called create the metadata for the next turn
update_post_meta( $post_id, 'fb_shares', 0 );
update_post_meta( $post_id, 'fb_shares_updated_at', time() );
return false;
} elseif ( update_share_business_rule( $post_id ) ) {
return true;
} 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, 'fb_shares_updated_at', 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, 'fb_shares', $shares );
update_post_meta( $post_id, 'fb_shares_updated_at', time() );
}
function get_post_shares( $post_id ) {
return get_post_meta( $post_id, 'fb_shares', true);
}
<?php
function add_my_theme_page() {
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' );
function theme_option_page() {
?>
<div class="wrap">
<h1>Dados de Integração</h1>
<form method="post" action="options.php">
<?php
settings_fields("integration-options-grp");
do_settings_sections("integration-options");
submit_button();
?>
</form>
</div>
<?php
}
function theme_section_description(){
//echo '<p>Theme Option Section</p>';
}
function display_fb_app_id(){
$fb_app_id = get_option( 'fb_app_id' );
echo '<input name="fb_app_id" id="fb_app_id" type="text" value="'. $fb_app_id .'" class="code" /> ';
}
function display_fb_app_secret() {
$fb_app_secret = get_option( 'fb_app_secret' );
echo '<input name="fb_app_secret" id="fb_app_secret" type="password" value="'. $fb_app_secret .'" class="code" /> ';
}
function test_theme_settings(){
//add_option('fb_app_id', 0);
add_settings_section( 'first_section', 'Facebook', 'theme_section_description','integration-options');
add_settings_field( 'fb_app_id','App ID','display_fb_app_id','integration-options','first_section');
register_setting( 'integration-options-grp', 'fb_app_id');
add_settings_field( 'fb_app_secret','App Secret','display_fb_app_secret','integration-options','first_section');
register_setting( 'integration-options-grp', 'fb_app_secret');
}
add_action('admin_init','test_theme_settings');
\ No newline at end of file
......@@ -14,23 +14,31 @@ if(!class_exists('\\AjaxPageviews')){
return;
}
class MostReadPostsList extends PostsList {
function __construct() {
parent::__construct('most-read-posts-list', 'Lista de posts Mais lidos', '', '', '', plugin_dir_path(__FILE__));
}
function get_template_variables($instance, $args) {
if (isset($instance['conteudo'])) {
parse_str($instance['conteudo'], $conteudo);
$date = json_decode($conteudo['date_query']);
function __construct() {
parent::__construct('most-read-posts-list', 'Lista de posts Mais lidos', '', '', '', plugin_dir_path(__FILE__));
}
$posts_query_args = siteorigin_widget_post_selector_process_query($instance['conteudo']);
$most_read = \AjaxPageviews::get_top_viewed(1000, ['post_type' => $conteudo['post_type'], 'from' => $date->after]);
$ids = array();
foreach ($most_read as $post => $value) {
array_push($ids, $value->post_id);
}
$posts_query_args['post__in'] = $ids;
$posts_query_args['orderby'] = 'post__in';
$most_read_query = new \WP_Query($posts_query_args);
function get_template_variables($instance, $args) {
if (isset($instance['conteudo'])) {
parse_str($instance['conteudo'], $conteudo);
}
$posts_query_args = siteorigin_widget_post_selector_process_query($instance['conteudo']);
$date = $posts_query_args['date_query'];
$posts_query_args['date_query'] = null;
$most_read = \AjaxPageviews::get_top_viewed(1000, ['post_type' => $conteudo['post_type'], 'from' => $date['after']]);
//var_dump($most_read);
$ids = array();
foreach ($most_read as $post => $value) {
array_push($ids, $value->post_id);
}
$posts_query_args['post__in'] = $ids;
$posts_query_args['orderby'] = 'post__in';
$most_read_query = new \WP_Query($posts_query_args);
return [
'query' => $most_read_query,
'link' => (object) $instance['link'],
......
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