diff --git a/admin/class-poet-admin.php b/admin/class-poet-admin.php index b507ff4..2cad04d 100755 --- a/admin/class-poet-admin.php +++ b/admin/class-poet-admin.php @@ -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. + ); } /** @@ -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; } @@ -212,6 +222,13 @@ public function active_callback() { echo ''; } + /** + * Returns backfill checkbox input + */ + public function backfill_callback() { + $checked = isset( get_option( 'poet_option' )['backfill'] ) ? 1 : 0; + echo ''; + } /** diff --git a/includes/class-poet-backfill.php b/includes/class-poet-backfill.php new file mode 100644 index 0000000..ae5a21a --- /dev/null +++ b/includes/class-poet-backfill.php @@ -0,0 +1,57 @@ + 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 ); + } + } + } + +} \ No newline at end of file diff --git a/includes/class-poet.php b/includes/class-poet.php index ccfdbe6..ff24eb3 100755 --- a/includes/class-poet.php +++ b/includes/class-poet.php @@ -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. */ @@ -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' ); } /** @@ -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' ); } diff --git a/public/class-poet-public.php b/public/class-poet-public.php index f59bb0c..8b12180 100755 --- a/public/class-poet-public.php +++ b/public/class-poet-public.php @@ -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;