Skip to content

Commit

Permalink
Async Backfill (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
markparolisi authored and lautarodragan committed Nov 27, 2018
1 parent 20a832e commit 104162e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
17 changes: 17 additions & 0 deletions admin/class-poet-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ public function register_setting() {
$this->plugin, // Page.
'poet_setting_section_id' // Section.
);
add_settings_field(
'backfill', // ID.
__( 'Backfill all posts' ), // Title.
array( $this, 'backfill_callback' ), // Callback.
$this->plugin, // Page.
'poet_setting_section_id' // Section.
);
}

/**
Expand Down Expand Up @@ -170,6 +177,9 @@ public function sanitize( $input ) {
if ( isset( $input['active'] ) ) {
$new_input['active'] = (int) $input['active'];
}
if ( isset( $input['backfill'] ) ) {
$new_input['backfill'] = (int) $input['backfill'];
}

return $new_input;
}
Expand Down Expand Up @@ -212,6 +222,13 @@ public function active_callback() {
echo '<input type="checkbox" id="active" name="poet_option[active]" ' . checked( 1, $checked, false ) . ' />';
}

/**
* Returns backfill checkbox input
*/
public function backfill_callback() {
$checked = isset( get_option( 'poet_option' )['backfill'] ) ? 1 : 0;
echo '<input type="checkbox" id="backfill" name="poet_option[backfill]" ' . checked( 1, $checked, false ) . ' />';
}


/**
Expand Down
57 changes: 57 additions & 0 deletions includes/class-poet-backfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* Feature to backfill old posts to Frost API.
*
* @TODO Make a WP-CLI for this as well so one could batch process tens/hundreds of thousands of posts
*
* @package Poet
* @subpackage Poet/includes
*/
class Poet_Backfill {

/**
* Schedule a cron to backfill posts if the user has this feature enabled.
*/
public static function init() {
$should_backfill = isset( get_option( 'poet_option' )['active'] ) ? 1 : 0;

if( $should_backfill ) {
if (! wp_next_scheduled ( 'poet_backfill_posts' )) {
wp_schedule_event(time(), 'hourly', 'poet_backfill_posts');
}
}

add_action( 'poet_backfill_posts', [__CLASS__, 'backfill_posts'] );

}

/**
* Backfill posts to the Frost API
*/
public static function backfill_posts() {

// Only running 100 at a time so the jobs don't back up or spike host resources
$query_args = [
'posts_per_page' => 100,
'post_status' => 'publish',
'no_found_rows' => true,
'meta_query' => [
[
'key' => 'poet_work_id',
'compare' => 'NOT EXISTS',
],
],
'fields' => 'ids',
];

$backfill_posts = new WP_Query($query_args);

if( !empty( $backfill_posts->posts ) ) {
foreach( $backfill_posts->posts as $backfill_post_id ) {
\Poet_Public::post_article( $backfill_post_id );
}
}
}

}
8 changes: 7 additions & 1 deletion includes/class-poet.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ private function load_dependencies() {
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-poet-i18n.php';

/**
* The class responsible for defining handling the post backfill feature
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-poet-backfill.php';

/**
* The class responsible for defining all actions that occur in the admin area.
*/
Expand Down Expand Up @@ -144,6 +149,7 @@ private function define_admin_hooks() {
$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_options_page' );
$this->loader->add_filter( 'plugin_action_links_' . $this->poet, $plugin_admin, 'add_settings_link' );
$this->loader->add_action( 'admin_init', $plugin_admin, 'register_setting' );
$this->loader->add_action( 'admin_init', 'Poet_Backfill', 'init' );
}

/**
Expand All @@ -156,7 +162,7 @@ private function define_public_hooks() {
$plugin_public = new Poet_Public( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
$this->loader->add_action( 'save_post', $plugin_public, 'post_article' );
$this->loader->add_action( 'save_post', 'Poet_Public', 'post_article' );
$this->loader->add_filter( 'the_content', $plugin_public, 'poet_badge_handler' );
}

Expand Down
2 changes: 1 addition & 1 deletion public/class-poet-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function poet_badge_handler( $content ) {
*
* @param string $post_id ID post from WP.
*/
public function post_article( $post_id ) {
public static function post_article( $post_id ) {

$active = isset( get_option( 'poet_option' )['active'] ) ? 1 : 0;
$api_url = ! empty( get_option( 'poet_option' )['api_url'] ) ? 1 : 0;
Expand Down

0 comments on commit 104162e

Please sign in to comment.