Skip to content

Commit

Permalink
Add bulk full offload
Browse files Browse the repository at this point in the history
  • Loading branch information
av3nger committed Jan 26, 2024
1 parent 8bce952 commit cf0521d
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
= 1.8.0 =

Added:
* Bulk full offload

= 1.7.1 - 31.12.2023 =

Fixed:
Expand Down
5 changes: 5 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ If something is still not working for you, please let me know by creating a supp

== Changelog ==

= 1.8.0 =

Added:
* Bulk full offload

= 1.7.1 - 31.12.2023 =

Fixed:
Expand Down
2 changes: 2 additions & 0 deletions app/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ private function init_integrations() {
* @see Modules\Custom_Path
* @see Modules\Service
* @see Modules\CDN
* @see Modules\Full_Offload
*/
private function load_modules() {
$loader = Loader::get_instance();
Expand All @@ -227,6 +228,7 @@ private function load_modules() {
$loader->module( 'logging' );
$loader->module( 'custom-path' );
$loader->module( 'service' );
$loader->module( 'full-offload' );
}

/**
Expand Down
12 changes: 9 additions & 3 deletions app/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,15 @@ public function ajax_undo_image() {
* Remove (physically delete the files) selected image from WordPress media library.
*
* @since 1.2.1
*
* @param int|string|null $attachment_id Attachment ID.
*/
public function ajax_delete_image() {
public function ajax_delete_image( $attachment_id = null ) {
$this->check_ajax_request();

$attachment_id = (int) filter_input( INPUT_POST, 'data', FILTER_SANITIZE_NUMBER_INT );
if ( empty( $attachment_id ) ) {
$attachment_id = (int) filter_input( INPUT_POST, 'data', FILTER_SANITIZE_NUMBER_INT );
}

// This is a backward compat check to make sure we have the original offloaded before removing it.
$metadata = wp_get_attachment_metadata( $attachment_id );
Expand All @@ -517,7 +521,9 @@ public function ajax_delete_image() {

$this->delete_image( $attachment_id );

wp_send_json_success( $this->get_response_data( $attachment_id ) );
if ( 'cf_images_delete_image' === filter_input( INPUT_POST, 'action', FILTER_UNSAFE_RAW ) ) {
wp_send_json_success( $this->get_response_data( $attachment_id ) );
}
}

/**
Expand Down
116 changes: 116 additions & 0 deletions app/modules/class-full-offload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* Full Offload module
*
* Manage removing physical files from the media library.
*
* @link https://vcore.au
*
* @package CF_Images
* @subpackage CF_Images/App/Modules
* @author Anton Vanyukov <[email protected]>
*
* @since 1.8.0
*/

namespace CF_Images\App\Modules;

use CF_Images\App\Traits;

if ( ! defined( 'WPINC' ) ) {
die;
}

/**
* Full_Offload class.
*
* @since 1.8.0
*/
class Full_Offload extends Module {
use Traits\Ajax;
use Traits\Stats;

/**
* Action name.
*
* @var string
*/
private $action = 'full-remove';

/**
* Init the module.
*
* @since 1.8.0
*/
public function init() {
// Bulk remove actions.
add_filter( 'cf_images_bulk_actions', array( $this, 'add_bulk_action' ) );
add_filter( 'cf_images_wp_query_args', array( $this, 'add_wp_query_args' ), 10, 2 );
add_action( 'cf_images_bulk_step', array( $this, 'bulk_step' ), 10, 2 );
}

/**
* Extend bulk action so that the AJAX callback accepts the bulk request.
*
* @since 1.8.0
* @see Media::ajax_bulk_process()
*
* @param array $actions Supported actions.
*
* @return array
*/
public function add_bulk_action( array $actions ): array {
if ( ! in_array( $this->action, $actions, true ) ) {
$actions[] = $this->action;
}

return $actions;
}

/**
* Adjust the WP_Query args for bulk compress action.
*
* @since 1.8.0
* @see Ajax::get_wp_query_args()
*
* @param array $args WP_Query args.
* @param string $action Executing action.
*
* @return array
*/
public function add_wp_query_args( array $args, string $action ): array {
if ( $this->action !== $action ) {
return $args;
}

$args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
array(
'key' => '_cloudflare_image_offloaded',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_cloudflare_image_id',
'compare' => 'EXISTS',
),
);

return $args;
}

/**
* Perform bulk step.
*
* @since 1.8.0
* @see Media::ajax_bulk_process()
*
* @param int $attachment_id Attachment ID.
* @param string $action Executing action.
*/
public function bulk_step( int $attachment_id, string $action ) {
if ( $this->action !== $action ) {
return;
}

$this->media()->ajax_delete_image( $attachment_id );
}
}
15 changes: 15 additions & 0 deletions assets/_src/modules/full-offload.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { MouseEvent, useContext } from 'react';
import { mdiImageMultipleOutline } from '@mdi/js';

/**
Expand All @@ -11,9 +12,13 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import SettingsContext from '../context/settings';
import Card from '../components/card';
import ProgressBar from '../components/progress';

const FullOffload = () => {
const { inProgress, setInProgress } = useContext(SettingsContext);

return (
<Card
icon={mdiImageMultipleOutline}
Expand All @@ -33,6 +38,16 @@ const FullOffload = () => {
'cf-images'
)}
</p>

{inProgress && <ProgressBar action="full-remove" />}

<button
className="button is-small"
onClick={(e) => setInProgress(true)}
disabled={inProgress}
>
{__('Bulk delete', 'cf-images')}
</button>
</div>
</Card>
);
Expand Down

0 comments on commit cf0521d

Please sign in to comment.