jaredatch / Shared-Counts

WordPress plugin that leverages SharedCount.com API to quickly retrieve, cache, and display various social sharing counts.
GNU General Public License v2.0
49 stars 16 forks source link

wp cli commands for updating share counts #80

Open billerickson opened 5 years ago

billerickson commented 5 years ago

Originally reported here: https://github.com/jaredatch/EA-Share-Count/issues/102

I've never done it, but seems like adding custom CLI commands is fairly straightforward. It would be cool to "prime the pump" and preload/update share counts without relying on admin page refresh via https://github.com/billerickson/Shared-Counts-Cache-Status. I'm probably edge case of who would need this, but would be a nice addition :)

Desired Commands

billerickson commented 5 years ago

I think that's a great idea. It should be pretty simple to setup.

wp shared-counts update 123 Update the counts on a specific post (id=123)

wp shared-counts bulk-update --count=200 Update counts on the 200 most recent posts

wp-shared-counts bulk-update --count=500 --only_empty=true Update the 500 most recent posts that don't have any share count data (ex: on a development site where you just installed Shared Counts)

JiveDig commented 5 years ago

🙌

jaredatch commented 5 years ago

wp shared-counts display 123 Display shares for the post in a "table" view, sorta like in CLI if you view the database structure.

wp shared-counts popular ---count=100 Display 100 most shared posts (title, permalink, total shares). Basically what the dashboard widget does.

billerickson commented 5 years ago

I asked @richardbuff to help with this since he's written a few custom cli scripts.

richardbuff commented 5 years ago

I started hacking away on this. I have the easiest one (popular posts) done at the moment: https://cl.ly/37936ec88814

JiveDig commented 4 years ago

I needed to bulk/force update counts that got zero'd out due to hitting the sharedcount.com API limit (this issues is resolved in v1.4 but i'm still stuck with thousands of 0's). The cache status plugin only updates posts without ANY value set. All these have the key, it's just 0 when FB debugger shows shares.

Anyway, this works for quick/dirty but still pretty safe and solid.

<?php

/**
 * A rough WP-CLI command/file to force update shared counts on posts.
 *
 * @version  0.1.0
 * @author   Mike Hemberger (@JiveDig)
 *
 * @uses     Shared Counts plugin.
 * @link     https://wordpress.org/plugins/shared-counts/
 *
 * HOW TO USE:
 *
 * 1. Put this code in a file named shared-counts-update.php in your WP root, typically public_html.
 * 2. Use WP-CLI command to force update of posts (make sure you have enough API hits available on sharedcount.com).
 * 3. The first parameter is the number of posts (posts_per_page) and the second is the offset. wp eval-file shared-counts-update.php posts_per_page offset
 * 4. Example: wp eval-file shared-counts-update.php 200 100
 *
 * The above example will update 200 posts with an offset of 100, so starting from 101.
 */

if ( ! function_exists( 'shared_counts' ) ) {
    WP_CLI::error( 'Please activate Shared Counts plugin to use this command.' );
}

$number = isset( $args[0] ) ? absint( $args[0] ) : 100;
$offset = isset( $args[1] ) ? absint( $args[1] ) : 0;

$posts = new WP_Query( array(
    'post_type'           => 'post',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => true,
    'posts_per_page'      => $number,
    'offset'              => $offset,
    'meta_query'          => array(
        'relation' => 'OR',
        array(
            'key'     => 'shared_counts_total',
            'value'   => 0,
            'type'    => 'numeric',
            'compare' => '='
        ),
        array(
            'key'     => 'shared_counts_total',
            'compare' => 'NOT EXISTS'
        ),
    ),
) );

if ( $posts->have_posts() ) {
    $count = 1;
    while ( $posts->have_posts() ) : $posts->the_post();
        $update = shared_counts()->core->counts( get_the_ID(), true, true );
        $total  = shared_counts()->core->count( get_the_ID(), $type = 'total', $echo = false, $round = 2 );
        $log    = sprintf( '(%s) : %s : %s share(s) : %s ', $count, get_the_ID(), $total, get_the_title() );
        $count++;
        WP_CLI::log( $log );
    endwhile;
    WP_CLI::success( sprintf( '%s post counts were updated.', $posts->post_count ) );
} else {
    WP_CLI::success( 'There were no posts to update.' );
}
wp_reset_postdata();
JiveDig commented 4 years ago

I think it would be really beneficial to add a WP_CLI::log() that shows when the API limit is reached too.