'ROS_ATF_Leaderboard', 'incontent' => 'In-Article_All', 'before_content' => 'In-Article_All', 'after_content' => 'In-Article_All', 'sidebar' => 'ROS_Sticky_Sidebars', 'footer' => 'ROS_Pre-Footer_BTF', 'anchor' => 'ROS_Sticky_Footer', 'interstitial' => 'Special_Interstitial', 'reward' => 'Special_Reward', ]; public static function generate($ad_units, $only_new = false) { $lines = []; $seen = []; foreach ((array) $ad_units as $unit) { if (!is_array($unit) || ($only_new && self::in_gam($unit))) { continue; } $row = self::row($unit); if (isset($seen[$row[0]])) { continue; } $seen[$row[0]] = true; $lines[] = self::line($row); } return $lines ? implode("\n", $lines) . "\n" : ''; } public static function in_gam(array $unit) { return !empty($unit['gam']['code']) && (string) $unit['gam']['code'] === AdGhost_Helper::gam_code(AdGhost_Helper::normalize_unit($unit)); } public static function row(array $unit) { if (class_exists('AdGhost_Default_Ad_Units')) { $unit = AdGhost_Default_Ad_Units::fill_from_default($unit); } $unit = AdGhost_Helper::normalize_unit($unit); $code = AdGhost_Helper::gam_code($unit); $position = AdGhost_Helper::get_unit_position($unit); $special = in_array($unit['type'], ['interstitial', 'reward'], true); $sizes = $special ? 'out-of-page' : implode(';', array_filter(array_map('trim', preg_split('/[;,]/', (string) ($unit['sizes'] ?? ''))))); $name = trim((string) ($unit['name'] ?? '')) !== '' ? $unit['name'] : $code; $placement = trim((string) ($unit['gamPlacement'] ?? '')); if ($placement === '') { $placement = self::PLACEMENTS[$position] ?? 'ROS_' . str_replace(' ', '_', ucwords(str_replace('_', ' ', $position))); } return [ $code, preg_replace('/\s+/', '_', trim((string) $name)), $sizes, trim((string) ($unit['label'] ?? '')) !== '' ? $unit['label'] : $name, $placement, '_blank', ]; } private static function line(array $cells) { return implode(',', array_map(function ($cell) { $cell = (string) $cell; return preg_match('/[",\r\n]/', $cell) ? '"' . str_replace('"', '""', $cell) . '"' : $cell; }, $cells)); } public static function download() { check_ajax_referer('adghost_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_die('Unauthorized'); } $csv = self::generate(get_option('adghost_ad_units', [])); if ($csv === '') { wp_die('No ad units to export'); } header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=adghost-ad-units-' . gmdate('Y-m-d') . '.csv'); echo $csv; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- a CSV file download, not HTML. exit; } }