| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Media_Cleanup; |
| 4 |
|
| 5 |
use JFB_Components\Module\Base_Module_It; |
| 6 |
|
| 7 |
// If this file is called directly, abort. |
| 8 |
if ( ! defined( 'WPINC' ) ) { |
| 9 |
die; |
| 10 |
} |
| 11 |
|
| 12 |
class Module implements Base_Module_It { |
| 13 |
|
| 14 |
public function rep_item_id() { |
| 15 |
return 'media-cleanup'; |
| 16 |
} |
| 17 |
|
| 18 |
public function condition(): bool { |
| 19 |
return true; |
| 20 |
} |
| 21 |
|
| 22 |
public function init_hooks() {} |
| 23 |
|
| 24 |
public static function collect_post_meta_attachment_ids( int $post_id, array $meta_keys ): array { |
| 25 |
$attachment_ids = array(); |
| 26 |
|
| 27 |
foreach ( $meta_keys as $meta_key ) { |
| 28 |
$attachment_ids = array_merge( |
| 29 |
$attachment_ids, |
| 30 |
self::normalize_attachment_ids( |
| 31 |
get_post_meta( $post_id, $meta_key, true ) |
| 32 |
) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
return self::normalize_attachment_ids( $attachment_ids ); |
| 37 |
} |
| 38 |
|
| 39 |
public static function maybe_delete_attachments( |
| 40 |
array $old_attachment_ids, |
| 41 |
array $new_attachment_ids = array() |
| 42 |
): void { |
| 43 |
$attachment_ids_to_delete = self::diff_attachment_ids( |
| 44 |
$old_attachment_ids, |
| 45 |
$new_attachment_ids |
| 46 |
); |
| 47 |
if ( empty( $attachment_ids_to_delete ) ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
self::delete_attachments( $attachment_ids_to_delete ); |
| 51 |
} |
| 52 |
|
| 53 |
public static function diff_attachment_ids( array $old_attachment_ids, array $new_attachment_ids ): array { |
| 54 |
$old_attachment_ids = self::normalize_attachment_ids( $old_attachment_ids ); |
| 55 |
$new_attachment_ids = self::normalize_attachment_ids( $new_attachment_ids ); |
| 56 |
return array_values( |
| 57 |
array_diff( $old_attachment_ids, $new_attachment_ids ) |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public static function normalize_attachment_ids( $value ): array { |
| 62 |
if ( empty( $value ) ) { |
| 63 |
return array(); |
| 64 |
} |
| 65 |
|
| 66 |
if ( is_numeric( $value ) ) { |
| 67 |
return array( absint( $value ) ); |
| 68 |
} |
| 69 |
|
| 70 |
if ( is_string( $value ) ) { |
| 71 |
$value = array_map( 'trim', explode( ',', $value ) ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! is_array( $value ) ) { |
| 75 |
return array(); |
| 76 |
} |
| 77 |
|
| 78 |
$ids = array(); |
| 79 |
|
| 80 |
foreach ( $value as $item ) { |
| 81 |
if ( is_numeric( $item ) ) { |
| 82 |
$ids[] = absint( $item ); |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
if ( is_array( $item ) && isset( $item['id'] ) && is_numeric( $item['id'] ) ) { |
| 87 |
$ids[] = absint( $item['id'] ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return array_values( |
| 92 |
array_unique( |
| 93 |
array_filter( |
| 94 |
array_map( 'absint', $ids ) |
| 95 |
) |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
public static function delete_attachments( array $attachment_ids ): void { |
| 101 |
|
| 102 |
$attachment_ids = self::normalize_attachment_ids( $attachment_ids ); |
| 103 |
|
| 104 |
foreach ( $attachment_ids as $attachment_id ) { |
| 105 |
if ( 'attachment' !== get_post_type( $attachment_id ) ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
wp_delete_attachment( $attachment_id, true ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public function remove_hooks() { |
| 114 |
} |
| 115 |
|
| 116 |
public static function get_post_meta_keys_for_cleanup( $modifier ): array { |
| 117 |
$form_id = jet_fb_action_handler()->get_form_id(); |
| 118 |
if ( ! $form_id || empty( $modifier->fields_map ) ) { |
| 119 |
return array(); |
| 120 |
} |
| 121 |
$meta_keys = array(); |
| 122 |
foreach ( $modifier->fields_map as $field_name => $meta_key ) { |
| 123 |
if ( empty( $field_name ) || empty( $meta_key ) ) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
$field = jet_form_builder()->form->get_field_by_name( |
| 127 |
$form_id, |
| 128 |
$field_name |
| 129 |
); |
| 130 |
if ( ! self::is_cleanup_enabled_media_field( $field ) ) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
$meta_keys[] = $meta_key; |
| 134 |
} |
| 135 |
return array_values( |
| 136 |
array_unique( |
| 137 |
array_filter( $meta_keys ) |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
private static function is_cleanup_enabled_media_field( $field ): bool { |
| 143 |
if ( ! is_array( $field ) || empty( $field ) ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
if ( 'jet-forms/media-field' !== ( $field['blockName'] ?? '' ) ) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
$attrs = $field['attrs'] ?? array(); |
| 150 |
if ( empty( $attrs['delete_uploaded_attachment'] ) ) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
if ( empty( $attrs['insert_attachment'] ) ) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
return in_array( |
| 157 |
$attrs['value_format'] ?? 'url', |
| 158 |
array( 'id', 'ids', 'both' ), |
| 159 |
true |
| 160 |
); |
| 161 |
} |
| 162 |
} |
| 163 |
|