Completed
Push — develop ( 1d4633...83eb6a )
by David
02:35 queued 10s
created

Image_License_Cleanup_Service   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A post_get_non_public_domain_images() 0 21 3
1
<?php
2
3
namespace Wordlift\Images_Licenses;
4
5
class Image_License_Cleanup_Service {
6
7
	public function __construct() {
8
9
		add_action( 'wl_post_get_non_public_domain_images', array( $this, 'post_get_non_public_domain_images' ) );
10
11
	}
12
13
	/**
14
	 * @param array $data
15
	 *
16
	 * @return array
17
	 */
18
	public function post_get_non_public_domain_images( $data ) {
19
20
		return array_values( array_filter( $data, function ( $item ) {
21
22
			// Keep images that are referenced either as embeds or featured image.
23
			$posts_ids_as_embed          = $item['posts_ids_as_embed'];
24
			$posts_ids_as_featured_image = $item['posts_ids_as_featured_image'];
25
26
			if ( ! empty( $posts_ids_as_embed ) || ! empty( $posts_ids_as_featured_image ) ) {
27
				return true;
28
29
			}
30
31
			// Remove other images.
32
			$result = wp_delete_attachment( $item['attachment_id'], true );
33
34
			// If result is `WP_Post` it means we successfully deleted the attachment, and therefore we can remove
35
			// it from the results.
36
			return ! is_a( $result, 'WP_Post' );
37
		} ) );
38
	}
39
40
}
41