handle_form_submissions();
}
private function handle_form_submissions(): void {
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified below via check_admin_referer().
if (!isset($_POST['contai_randomize_dates']) && !isset($_POST['contai_update_thumbnails'])) {
return;
}
check_admin_referer('contai_post_maintenance', 'contai_maintenance_nonce');
if (!current_user_can('manage_options')) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified above.
if (isset($_POST['contai_randomize_dates'])) {
$this->action_type = 'randomize_dates';
$this->randomize_post_dates();
} else {
$this->action_type = 'update_thumbnails';
$this->update_all_post_thumbnails();
}
$this->action_executed = true;
}
public function render(): void {
$this->render_notices();
?>
action_executed) {
return;
}
if (!empty($this->error_message)) {
?>
action_type === 'randomize_dates') {
?>
action_type === 'update_thumbnails') {
?>
thumbnails_updated)
); ?>
get_col("
SELECT ID FROM {$wpdb->posts}
WHERE post_type = 'post' AND post_status = 'publish'
");
if (empty($post_ids)) {
$this->error_message = __('No published posts found to randomize.', '1platform-content-ai');
return;
}
foreach ($post_ids as $post_id) {
$days_ago = wp_rand(0, 365);
$hours = wp_rand(0, 23);
$minutes = wp_rand(0, 59);
$seconds = wp_rand(0, 59);
$timestamp = strtotime("-{$days_ago} days");
$timestamp = mktime($hours, $minutes, $seconds, (int) gmdate('n', $timestamp), (int) gmdate('j', $timestamp), (int) gmdate('Y', $timestamp));
$local_date = wp_date('Y-m-d H:i:s', $timestamp);
$gmt_date = get_gmt_from_date($local_date);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->update(
$wpdb->posts,
[
'post_date' => $local_date,
'post_date_gmt' => $gmt_date,
'post_modified' => $local_date,
'post_modified_gmt' => $gmt_date,
],
['ID' => $post_id],
['%s', '%s', '%s', '%s'],
['%d']
);
clean_post_cache((int) $post_id);
}
$this->dates_randomized = count($post_ids);
}
private function update_all_post_thumbnails(): void {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$post_ids = $wpdb->get_col("
SELECT ID FROM {$wpdb->posts}
WHERE post_type = 'post' AND post_status = 'publish'
");
if (empty($post_ids)) {
$this->error_message = __('No published posts found to update.', '1platform-content-ai');
return;
}
foreach ($post_ids as $post_id) {
if ($this->set_random_image_as_thumbnail((int) $post_id)) {
$this->thumbnails_updated++;
}
}
}
private function set_random_image_as_thumbnail(int $post_id): bool {
$content = get_post_field('post_content', $post_id);
if (empty($content)) {
return false;
}
preg_match_all('//', $content, $matches);
if (empty($matches[1])) {
return false;
}
$random_image_url = $matches[1][array_rand($matches[1])];
if (empty($random_image_url)) {
return false;
}
$random_image_url = esc_url_raw($random_image_url);
if (empty($random_image_url) || !wp_http_validate_url($random_image_url)) {
return false;
}
$attachment_id = media_sideload_image($random_image_url, $post_id, null, 'id');
if (is_wp_error($attachment_id)) {
return false;
}
set_post_thumbnail($post_id, $attachment_id);
return true;
}
}