DC Social image update

<?php
if (!defined('ABSPATH')) {
    exit;
}

const DC_GALLERY_SOURCE_URL = 'https://devotedcreations.com/support/marketing/social-media-graphics/';
const DC_GALLERY_OPTION = 'dc_remote_gallery_assets_v2';
const DC_GALLERY_LAST_SYNC_OPTION = 'dc_remote_gallery_last_sync_v2';
const DC_GALLERY_CRON_HOOK = 'dc_remote_gallery_weekly_sync_v2';

function dc_gallery_register_weekly_schedule($schedules) {
    if (!isset($schedules['weekly'])) {
        $schedules['weekly'] = [
            'interval' => 7 * DAY_IN_SECONDS,
            'display'  => 'Once Weekly',
        ];
    }
    return $schedules;
}
add_filter('cron_schedules', 'dc_gallery_register_weekly_schedule');

function dc_gallery_schedule_sync() {
    if (!wp_next_scheduled(DC_GALLERY_CRON_HOOK)) {
        wp_schedule_event(time() + 300, 'weekly', DC_GALLERY_CRON_HOOK);
    }
}
add_action('init', 'dc_gallery_schedule_sync');

function dc_gallery_extract_assets($html) {
    preg_match_all('#https://devotedcreations\.com/recycle/wp-content/uploads/[^"\'\s<>]+#i', $html, $matches);
    $urls = array_values(array_unique($matches[0] ?? []));

    $skip_patterns = [
        '/\/compiler\//i',
        '/\/elementor\/css\//i',
        '/\.css(\?|$)/i',
        '/-\d+x\d+\.(png|jpe?g|webp)$/i',
    ];

    $items = [];

    foreach ($urls as $url) {
        $skip = false;
        foreach ($skip_patterns as $pattern) {
            if (preg_match($pattern, $url)) {
                $skip = true;
                break;
            }
        }
        if ($skip) {
            continue;
        }

        $path = wp_parse_url($url, PHP_URL_PATH);
        $filename = basename((string) $path);
        $lower = strtolower($filename);
        $ext = strtolower(pathinfo($lower, PATHINFO_EXTENSION));

        if (!in_array($ext, ['jpg', 'jpeg', 'png', 'svg', 'webp'], true)) {
            continue;
        }

        $is_social_graphic = (bool) preg_match('/img_|[a-f0-9]{8}-[a-f0-9]{4}-/i', $lower);

        if (!$is_social_graphic) {
            continue;
        }

        $items[] = [
            'url'      => esc_url_raw($url),
            'filename' => $filename,
            'label'    => trim(preg_replace('/[-_]+/', ' ', rawurldecode(pathinfo($filename, PATHINFO_FILENAME)))),
        ];
    }

    usort($items, function($a, $b) {
        return strcasecmp($a['filename'], $b['filename']);
    });

    return $items;
}

function dc_gallery_sync_now() {
    $response = wp_remote_get(DC_GALLERY_SOURCE_URL, [
        'timeout'     => 30,
        'redirection' => 5,
        'user-agent'  => 'WordPress Devoted Gallery Sync',
    ]);

    if (is_wp_error($response)) {
        return $response;
    }

    $code = wp_remote_retrieve_response_code($response);
    $body = wp_remote_retrieve_body($response);

    if ($code < 200 || $code >= 300 || empty($body)) {
        return new WP_Error('dc_gallery_fetch_failed', 'Unable to fetch gallery source page.', ['status' => $code]);
    }

    $items = dc_gallery_extract_assets($body);

    update_option(DC_GALLERY_OPTION, [
        'source_url' => DC_GALLERY_SOURCE_URL,
        'synced_at'  => current_time('mysql'),
        'count'      => count($items),
        'items'      => $items,
    ], false);

    update_option(DC_GALLERY_LAST_SYNC_OPTION, time(), false);

    return $items;
}
add_action(DC_GALLERY_CRON_HOOK, 'dc_gallery_sync_now');

function dc_gallery_maybe_prime_cache() {
    $data = get_option(DC_GALLERY_OPTION);
    if (empty($data) || empty($data['items'])) {
        dc_gallery_sync_now();
    }
}
add_action('init', 'dc_gallery_maybe_prime_cache');

function dc_gallery_render_shortcode($atts = []) {
    $data = get_option(DC_GALLERY_OPTION);
    $items = is_array($data) && !empty($data['items']) ? $data['items'] : [];

    if (empty($items)) {
        return '<p>Gallery data is not available yet.</p>';
    }

    $uid = 'dcg_' . wp_generate_uuid4();
    $json = wp_json_encode($items, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    $updated = !empty($data['synced_at']) ? esc_html($data['synced_at']) : '';

    ob_start();
    ?>
    <style>
      #<?php echo esc_attr($uid); ?>{font-family:Arial,sans-serif;color:#171717}
      #<?php echo esc_attr($uid); ?> *{box-sizing:border-box}
      #<?php echo esc_attr($uid); ?> .dc-meta{font-size:13px;color:#666;margin:0 0 12px}
      #<?php echo esc_attr($uid); ?> .dc-grid{display:grid;grid-template-columns:repeat(6,minmax(0,1fr));gap:10px;max-height:calc((140px * 10) + (9 * 10px));overflow-y:auto;padding-right:6px}
      #<?php echo esc_attr($uid); ?> .dc-item{display:block;border:1px solid rgba(0,0,0,.08);border-radius:12px;overflow:hidden;background:#fff;cursor:pointer;padding:0}
      #<?php echo esc_attr($uid); ?> .dc-item img{display:block;width:100%;height:140px;object-fit:cover;background:#f5f5f5}
      #<?php echo esc_attr($uid); ?> .dc-caption{padding:8px 10px;font-size:12px;line-height:1.35;color:#555;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
      #<?php echo esc_attr($uid); ?>-lightbox{position:fixed;inset:0;background:rgba(0,0,0,.82);display:none;align-items:center;justify-content:center;z-index:99999;padding:24px}
      #<?php echo esc_attr($uid); ?>-lightbox.is-open{display:flex}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-overlay{position:absolute;inset:0}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-panel{position:relative;z-index:1;width:min(94vw,1400px);height:min(88vh,920px);background:#111;border-radius:18px;overflow:hidden;display:grid;grid-template-rows:1fr auto}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-stage{display:flex;align-items:center;justify-content:center;min-height:0;padding:18px;background:#111}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-stage img{max-width:100%;max-height:100%;object-fit:contain;display:block}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-bar{display:flex;align-items:center;gap:10px;padding:12px 14px;background:#181818;color:#fff;overflow-x:auto;white-space:nowrap}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-thumb{flex:0 0 auto;border:1px solid rgba(255,255,255,.18);border-radius:10px;overflow:hidden;background:#222;cursor:pointer;padding:0}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-thumb img{display:block;width:72px;height:72px;object-fit:cover}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-thumb.is-active{outline:2px solid #2bb3a8;outline-offset:2px}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-close,
      #<?php echo esc_attr($uid); ?>-lightbox .dc-nav,
      #<?php echo esc_attr($uid); ?>-lightbox .dc-open{position:absolute;z-index:2;border:none;border-radius:999px;cursor:pointer}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-close{top:14px;right:14px;width:42px;height:42px;background:rgba(255,255,255,.14);color:#fff;font-size:24px;line-height:1}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-nav{top:50%;transform:translateY(-50%);width:46px;height:46px;background:rgba(255,255,255,.14);color:#fff;font-size:22px}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-prev{left:14px}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-next{right:14px}
      #<?php echo esc_attr($uid); ?>-lightbox .dc-open{left:14px;top:14px;padding:10px 14px;background:#2bb3a8;color:#fff;text-decoration:none;font-size:14px}
      @media (max-width:1200px){#<?php echo esc_attr($uid); ?> .dc-grid{grid-template-columns:repeat(4,minmax(0,1fr))}}
      @media (max-width:800px){#<?php echo esc_attr($uid); ?> .dc-grid{grid-template-columns:repeat(2,minmax(0,1fr));max-height:none}#<?php echo esc_attr($uid); ?> .dc-item img{height:120px}}
    </style>

    <div id="<?php echo esc_attr($uid); ?>" class="dc-gallery-root" data-items='<?php echo esc_attr($json); ?>'>
      <div class="dc-meta">Last synced: <?php echo $updated; ?></div>
      <div class="dc-grid"></div>
    </div>

    <div id="<?php echo esc_attr($uid); ?>-lightbox" aria-hidden="true">