radgeek/feedwordpress

WP CLI as an alternative to cURL?

ThatStevensGuy opened this issue · 0 comments

Just a suggestion. Some people may want to use WP CLI instead of cURL to update FeedWordPress.

My site uses Composer, so the implementation might be a bit different to others.

It would be nicer if FeedWordPress had the WP CLI command built in. 'wp feedwordpress update' for example.

$feedwordpress variable doesn't seem to be fully accessible from WP CLI, so I had to redeclare it. I basically redo what the plugin does when it initialises, then it seems to work fine.

// crontab
*/10 * *   *   *     /usr/local/bin/wp mysite feedwordpress update --path="/var/www/mysite.com/html" > /dev/null 2>&1

// functions.php

/**
 * Include theme namespace.
 */
require_once __DIR__ . '/vendor/autoload.php';

if (class_exists('WP_CLI')) {
    WP_CLI::add_command('mysite feedwordpress', 'MySite\Theme\CLI\FeedWordPress');
}

// Class - /Theme/CLI/FeedWordPress.php

<?php

namespace MySite\Theme\CLI;

use WP_CLI;
use FeedWordPress as FWP;

class FeedWordPress
{
    /**
     * Update FeedWordPress
     *
     * ## EXAMPLES
     *
     *     wp mysite feedwordpress update
     *
     * @when after_wp_load
     *
     * @param array|null $args The arguments.
     * @param array|null $assoc_args The associative arguments.
     *
     * @return void
     */
    public function update($args = null, $assoc_args = null): void
    {
        global $feedwordpress;

        $feedwordpress = new FWP(); // Redeclare FeedWordPress.

        // Unsure if this is necessary, but do it just incase.
        if (!$feedwordpress->needs_upgrade()) {
            $feedwordpress->add_filters();
        }

        $feedwordpress->update();

        WP_CLI::success('Done!');
    }
}