Skip to content

Instantly share code, notes, and snippets.

@bryan3561
Last active May 24, 2017 20:05
Show Gist options
  • Select an option

  • Save bryan3561/e794f534e03b0c8edff28452b3a99acc to your computer and use it in GitHub Desktop.

Select an option

Save bryan3561/e794f534e03b0c8edff28452b3a99acc to your computer and use it in GitHub Desktop.
Como tener rápidamente las visitas de mashared y las compartidas para usarlas donde se quiera consumiendo menos recursos
<?php
if ( ! function_exists( 'bkc_meta_views_shares' ) ) {
function bkc_meta_views_shares( $post_id = null ) {
$post_meta = [];
static $posts_metas = [];
/*
* Si el ID no fue pasado, se asigna automaticamente el del post
* que esta siendo leido en el momento donde se uso la funcion.
*/
if ( is_null( $post_id ) ) {
global $post;
$post_id = $post->ID;
}
if (isset($posts_metas[$post_id]))
return json_decode( json_encode( $posts_metas[$post_id] ), false );
/*
* Extraer los datos desde mashared
*/
$view_count_tmp = (int) get_post_meta( $post_id, '_mts_view_count', true );
$shares_tmp = (int) get_post_meta( $post_id, 'mashsb_shares', true );
/*
* Si no tienen un valor los datos, se inician en 0
*/
$post_meta['view_count'] = ( $view_count_tmp ) ? $view_count_tmp : 0;
$post_meta['shares'] = ( $shares_tmp ) ? $shares_tmp : 0;
/*
* Numero inicial adicional a compartidas y visitas reales
*/
$getFakecount = getFakecount();
$porcentaje_adicional = 0;
if ( $getFakecount > 0 ) {
$porcentaje_adicional = round( ( $getFakecount * 50 ) / 100 );
}
if ( $post_meta['view_count'] === 0 ) {
$post_meta['view_count'] = $getFakecount + $porcentaje_adicional;
} else {
$post_meta['view_count'] = $post_meta['view_count'] + $getFakecount + $porcentaje_adicional;
}
if ( $post_meta['shares'] === 0 ) {
$post_meta['shares'] = $getFakecount;
} else {
$post_meta['shares'] = $post_meta['shares'] + $getFakecount;
}
$post_meta['shares'] = round( $post_meta['shares'] );
$post_meta['view_count'] = round( $post_meta['view_count'] );
$posts_metas[$post_id] = $post_meta;
return json_decode( json_encode( $post_meta ), false );
}
}
/*
* Usos
* var_dump(bkc_meta_views_shares());
* var_dump(bkc_meta_views_shares()->view_count);
* var_dump(bkc_meta_views_shares()->shares);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment