Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp cli commands for updating share counts #80

Open
6 tasks
billerickson opened this issue May 13, 2019 · 7 comments
Open
6 tasks

wp cli commands for updating share counts #80

billerickson opened this issue May 13, 2019 · 7 comments
Milestone

Comments

@billerickson
Copy link
Collaborator

billerickson commented May 13, 2019

Originally reported here: jaredatch/EA-Share-Count#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
Copy link
Collaborator Author

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
Copy link

JiveDig commented May 13, 2019

🙌

@jaredatch jaredatch added this to the 1.4.0 milestone May 14, 2019
@jaredatch
Copy link
Owner

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
Copy link
Collaborator Author

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

@richardbuff
Copy link

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

@JiveDig
Copy link

JiveDig commented Dec 12, 2019

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
Copy link

JiveDig commented Dec 12, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants