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.

Revisions

  1. bryan3561 revised this gist May 24, 2017. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -60,3 +60,13 @@ function bkc_meta_views_shares( $post_id = null ) {
    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);
    */
  2. bryan3561 revised this gist May 24, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,8 @@ function bkc_meta_views_shares( $post_id = null ) {
    }

    if (isset($posts_metas[$post_id]))
    return $posts_metas[$post_id];
    return json_decode( json_encode( $posts_metas[$post_id] ), false );


    /*
    * Extraer los datos desde mashared
  3. bryan3561 created this gist May 24, 2017.
    61 changes: 61 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    <?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 $posts_metas[$post_id];

    /*
    * 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 );
    }
    }