From a944eb75b4740c4d21cbd6e081e692e0f3c07466 Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Fri, 15 Sep 2017 16:30:25 -0700
Subject: [PATCH] Load all of wp-admin's includes, for all actions, since these
 normally happen in that context

---
 includes/class-custom-action.php      |  5 +----
 includes/class-delete-all.php         |  2 --
 includes/class-delete-permanently.php |  2 --
 includes/class-edit.php               |  3 ---
 includes/class-move-to-trash.php      |  2 --
 includes/class-restore-from-trash.php |  2 --
 includes/trait-bulk-actions.php       | 12 ++++++++++++
 7 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/includes/class-custom-action.php b/includes/class-custom-action.php
index f35e715..69753c0 100644
--- a/includes/class-custom-action.php
+++ b/includes/class-custom-action.php
@@ -33,9 +33,6 @@ class Custom_Action {
 	 * @param object $vars Bulk-request variables.
 	 */
 	public static function process_via_cron( $vars ) {
-		// Normally processed in the admin context.
-		require_once( ABSPATH . 'wp-admin/includes/admin.php' );
-
 		// Provide for capabilities checks.
 		wp_set_current_user( $vars->user_id );
 
@@ -50,7 +47,7 @@ class Custom_Action {
 		// Run the custom action as Core does. See note above.
 		$return_url = apply_filters( 'handle_bulk_actions-' . $vars->current_screen->id, $return_url, $vars->action, $vars->posts );
 
-		//
+		// Can't get much more than this in terms of success or failure.
 		$results = compact( 'return_url', 'vars' );
 		do_action( 'bulk_actions_cron_offload_custom_request_completed', $results, $vars );
 	}
diff --git a/includes/class-delete-all.php b/includes/class-delete-all.php
index 14744d7..ec51f75 100644
--- a/includes/class-delete-all.php
+++ b/includes/class-delete-all.php
@@ -64,8 +64,6 @@ class Delete_All {
 		$count = 0;
 
 		if ( is_array( $post_ids ) && ! empty( $post_ids ) ) {
-			require_once ABSPATH . '/wp-admin/includes/post.php';
-
 			$deleted    = array();
 			$locked     = array();
 			$auth_error = array();
diff --git a/includes/class-delete-permanently.php b/includes/class-delete-permanently.php
index 7221433..6b0419a 100644
--- a/includes/class-delete-permanently.php
+++ b/includes/class-delete-permanently.php
@@ -32,8 +32,6 @@ class Delete_Permanently {
 		$count = 0;
 
 		if ( is_array( $vars->posts ) && ! empty( $vars->posts ) ) {
-			require_once ABSPATH . '/wp-admin/includes/post.php';
-
 			$deleted    = array();
 			$locked     = array();
 			$auth_error = array();
diff --git a/includes/class-edit.php b/includes/class-edit.php
index 9f1b9f5..595f984 100644
--- a/includes/class-edit.php
+++ b/includes/class-edit.php
@@ -35,9 +35,6 @@ class Edit {
 			return;
 		}
 
-		// We want to use `bulk_edit_posts()`.
-		require_once ABSPATH . '/wp-admin/includes/post.php';
-
 		// `bulk_edit_posts()` takes an array, normally `$_REQUEST`, so we convert back.
 		$request_array = get_object_vars( $vars );
 		unset( $request_array['action'] );
diff --git a/includes/class-move-to-trash.php b/includes/class-move-to-trash.php
index 92f372d..8ccf2f0 100644
--- a/includes/class-move-to-trash.php
+++ b/includes/class-move-to-trash.php
@@ -32,8 +32,6 @@ class Move_To_Trash {
 		$count = 0;
 
 		if ( is_array( $vars->posts ) && ! empty( $vars->posts ) ) {
-			require_once ABSPATH . '/wp-admin/includes/post.php';
-
 			$trashed    = array();
 			$locked     = array();
 			$auth_error = array();
diff --git a/includes/class-restore-from-trash.php b/includes/class-restore-from-trash.php
index 7a65393..9100f07 100644
--- a/includes/class-restore-from-trash.php
+++ b/includes/class-restore-from-trash.php
@@ -32,8 +32,6 @@ class Restore_From_Trash {
 		$count = 0;
 
 		if ( is_array( $vars->posts ) && ! empty( $vars->posts ) ) {
-			require_once ABSPATH . '/wp-admin/includes/post.php';
-
 			$restored   = array();
 			$locked     = array();
 			$auth_error = array();
diff --git a/includes/trait-bulk-actions.php b/includes/trait-bulk-actions.php
index b6214ee..a504c2b 100644
--- a/includes/trait-bulk-actions.php
+++ b/includes/trait-bulk-actions.php
@@ -47,6 +47,18 @@ trait Bulk_Actions {
 		}
 	}
 
+	/**
+	 * Prepare environment for individual actions
+	 *
+	 * @param object $vars Bulk-request variables.
+	 */
+	public static function process_via_cron( $vars ) {
+		// Normally processed in the admin context.
+		require_once( ABSPATH . 'wp-admin/includes/admin.php' );
+
+		parent::process_via_cron( $vars );
+	}
+
 	/**
 	 * Render the post-redirect notice, or hand off to class for other notices
 	 */
-- 
GitLab