'adghost-lite', 'format' => self::FORMAT, 'version' => defined('ADGHOST_VERSION') ? ADGHOST_VERSION : '', 'exported' => gmdate('c'), 'from' => home_url('/'), 'settings' => $settings, ]; } public static function import($file) { if (!is_array($file) || ($file['plugin'] ?? '') !== 'adghost-lite' || !isset($file['settings']) || !is_array($file['settings'])) { return ['ok' => false, 'error' => __('This is not an AdGhost settings file.', 'adghost-lite')]; } if ((int) ($file['format'] ?? 0) !== self::FORMAT) { return ['ok' => false, 'error' => __('This settings file comes from a newer AdGhost. Update AdGhost here first.', 'adghost-lite')]; } $skipped = []; $imported = 0; foreach ($file['settings'] as $key => $value) { $key = (string) $key; if (!in_array($key, self::SETTINGS, true)) { $skipped[$key] = __('not a setting that moves between sites', 'adghost-lite'); continue; } if (in_array($key, self::SCRIPT, true) && !current_user_can('unfiltered_html')) { $skipped[$key] = __('your account cannot add scripts to this site', 'adghost-lite'); continue; } $clean = AdGhost_Plugin::sanitize_request($value, $key); if ($clean === null) { $skipped[$key] = __('not a valid value', 'adghost-lite'); continue; } if ($key === 'network_code') { $clean = trim((string) $clean); if ($clean !== '' && !preg_match('/^\d{4,12}(,\d{4,12})?$/', $clean)) { $skipped[$key] = __('not a Google Ad Manager network code', 'adghost-lite'); continue; } } if ($key === 'ad_units') { $clean = AdGhost_Plugin::get_instance()->clean_ad_units($clean); } if ($key === 'targeting') { $clean = self::portable_targeting($clean); } update_option('adghost_' . $key, $clean); $imported++; } if (array_key_exists('ad_units', $file['settings']) && !isset($skipped['ad_units'])) { update_option('adghost_units_saved', true); AdGhost_Helper::flush_units_cache(); } if ($imported > 0) { AdGhost_Setup_Guide::stamp(); } return ['ok' => true, 'imported' => $imported, 'skipped' => $skipped, 'from' => esc_url_raw((string) ($file['from'] ?? ''))]; } private static function portable_targeting($targeting) { if (!is_array($targeting)) { return []; } unset($targeting['exclude_post_ids'], $targeting['include_post_ids']); return $targeting; } public static function export_url() { return wp_nonce_url(admin_url('admin-post.php?action=adghost_export_settings'), 'adghost_export_settings'); } public static function handle_export() { if (!current_user_can('manage_options')) { wp_die(esc_html__('You are not allowed to do that.', 'adghost-lite'), '', ['response' => 403]); } check_admin_referer('adghost_export_settings'); $host = (string) wp_parse_url(home_url('/'), PHP_URL_HOST); $name = 'adghost-settings-' . sanitize_file_name($host !== '' ? $host : 'site') . '-' . gmdate('Y-m-d') . '.json'; nocache_headers(); header('Content-Type: application/json; charset=utf-8'); header('Content-Disposition: attachment; filename="' . $name . '"'); echo wp_json_encode(self::export(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); exit; } public static function handle_import() { if (!current_user_can('manage_options')) { wp_die(esc_html__('You are not allowed to do that.', 'adghost-lite'), '', ['response' => 403]); } check_admin_referer('adghost_import_settings'); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- the upload's temporary path and size are read, never echoed; its contents are decoded and checked by import(). $upload = $_FILES['adghost_settings_file'] ?? null; if (!is_array($upload) || (int) ($upload['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || !is_uploaded_file((string) $upload['tmp_name'])) { $result = ['ok' => false, 'error' => __('Choose the settings file to import.', 'adghost-lite')]; } elseif ((int) $upload['size'] > self::MAX_BYTES) { $result = ['ok' => false, 'error' => __('That file is too large to be an AdGhost settings file.', 'adghost-lite')]; } else { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a local upload in PHP's temporary folder. $result = self::import(json_decode((string) file_get_contents((string) $upload['tmp_name']), true)); } set_transient(self::NOTICE . get_current_user_id(), $result, 5 * MINUTE_IN_SECONDS); wp_safe_redirect(AdGhost_Admin_Page::url('code')); exit; } public static function take_notice() { $key = self::NOTICE . get_current_user_id(); $result = get_transient($key); if ($result !== false) { delete_transient($key); } return is_array($result) ? $result : null; } }