Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/aimogen-pro/aiomatic-media-expirator.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
defined('ABSPATH') or die();
3
+
add_filter('attachment_fields_to_edit', 'aiomatic_attachment_expiration_field', 10, 2);
4
+
function aiomatic_attachment_expiration_field($form_fields, $post)
5
+
{
6
+
$values = get_post_meta($post->ID, 'expiry_check', true);
7
+
if(is_array($values))
8
+
{
9
+
$values = $values[0];
10
+
}
11
+
if ($values == "1")
12
+
{
13
+
$values_val = "checked";
14
+
}
15
+
else
16
+
{
17
+
$values_val = "";
18
+
}
19
+
$form_fields['expiry_check'] = array(
20
+
'label' => esc_html__('Enable Expiration', 'aiomatic-automatic-ai-content-writer'),
21
+
'input' => 'html',
22
+
'html' => '<input type="checkbox" value="1" '.$values_val.' name="attachments['.$post->ID.'][expiry_check]" id="attachments-'.$post->ID.'-expiry_check" />',
23
+
'value' => get_post_meta($post->ID, 'expiry_check', true),
24
+
'helps' => esc_html__('Set a date on which the image will be automatically deleted (by Aiomatic)', 'aiomatic-automatic-ai-content-writer')
25
+
);
26
+
$form_fields['expiry_date'] = array(
27
+
'label' => esc_html__('Expiration Date', 'aiomatic-automatic-ai-content-writer'),
28
+
'input' => 'text',
29
+
'value' => get_post_meta($post->ID, 'expiry_date', true),
30
+
'helps' => esc_html__('Date format: YYYY-MM-DD, +3 days, +1 day', 'aiomatic-automatic-ai-content-writer')
31
+
);
32
+
return $form_fields;
33
+
}
34
+
35
+
add_filter('attachment_fields_to_save', 'aiomatic_attachment_expiration_field_save', 10, 2);
36
+
function aiomatic_attachment_expiration_field_save($post, $attachment) {
37
+
if (isset($attachment['expiry_check'])) {
38
+
update_post_meta($post['ID'], 'expiry_check', $attachment['expiry_check']);
39
+
} else {
40
+
update_post_meta($post['ID'], 'expiry_check', '0');
41
+
}
42
+
if (isset($attachment['expiry_date']))
43
+
{
44
+
$mydate = strtotime($attachment['expiry_date']);
45
+
if($mydate !== false)
46
+
{
47
+
$tdate = date('Y-m-d', $mydate);
48
+
update_post_meta($post['ID'], 'expiry_date', $tdate);
49
+
}
50
+
}
51
+
52
+
return $post;
53
+
}
54
+
55
+
add_action('aiomatic_expired_post_delete', 'aiomatic_delete_expired_posts');
56
+
function aiomatic_delete_expired_posts()
57
+
{
58
+
$todays_date = date("Y-m-d");
59
+
$paged = 1;
60
+
$per_page = 100;
61
+
do {
62
+
$args = array(
63
+
'post_status' => 'any',
64
+
'post_type' => 'attachment',
65
+
'posts_per_page' => $per_page,
66
+
'paged' => $paged,
67
+
'meta_query' => array(
68
+
array(
69
+
'key' => 'expiry_date',
70
+
'value' => $todays_date,
71
+
'type' => 'DATE',
72
+
'compare' => '<',
73
+
),
74
+
array(
75
+
'key' => 'expiry_check',
76
+
'value' => 1,
77
+
),
78
+
),
79
+
);
80
+
$posts = new WP_Query($args);
81
+
if ($posts->have_posts()) {
82
+
while ($posts->have_posts()) {
83
+
$posts->the_post();
84
+
wp_delete_post(get_the_ID());
85
+
}
86
+
}
87
+
wp_reset_postdata();
88
+
$paged++;
89
+
} while ($posts->have_posts());
90
+
}
91
+
92
+
add_action('init', 'aiomatic_register_daily_post_delete_event');
93
+
function aiomatic_register_daily_post_delete_event()
94
+
{
95
+
if (!wp_next_scheduled('aiomatic_expired_post_delete'))
96
+
{
97
+
wp_schedule_event(time(), 'daily', 'aiomatic_expired_post_delete');
98
+
}
99
+
}
100
+
?>