> */ private $options_map; /** * @param array> $options_map */ public function __construct(string $id, string $name, string $stylesheet, string $category, array $options_map = []) { $this->id = $id; $this->name = $name; $this->stylesheet = $stylesheet; $this->category = $category; $this->options_map = $options_map; } public function id(): string { return $this->id; } public function name(): string { return $this->name; } public function plugin_file(): string { // PluginAdapterInterface compatibility — themes don't have a plugin file. return 'theme:' . $this->stylesheet; } public function is_active(array $wp_context): bool { $theme = isset($wp_context['theme']) && is_array($wp_context['theme']) ? $wp_context['theme'] : []; $stylesheet = strtolower((string) ($theme['stylesheet'] ?? '')); $template = strtolower((string) ($theme['template'] ?? '')); $needle = strtolower($this->stylesheet); return ($stylesheet === $needle) || ($template === $needle); } /** * @return array */ public function collect_context(): array { $ctx = []; foreach ($this->options_map as $opt_name => $keys) { $val = get_option($opt_name, null); if ($val === null) { continue; } $ctx[$opt_name] = $this->subset($val, $keys); } return Redactor::redact_array($ctx); } /** * @return array> */ public function generate_findings(array $wp_context): array { if (!$this->is_active($wp_context)) { return []; } $theme = isset($wp_context['theme']) && is_array($wp_context['theme']) ? $wp_context['theme'] : []; $version = (string) ($theme['version'] ?? ''); $ctx = $this->collect_context(); return [ [ 'id' => 'adapter_' . $this->id . '_detected', 'title' => $this->name . I18n::tr(' theme detected (Adapter)', ' - تم اكتشاف القالب (Adapter)'), 'category' => $this->category, 'severity' => 'info', 'score_impact' => 0, 'status' => 'open', 'check_id' => 'adapter:' . $this->id, 'evidence' => [ 'source' => 'theme_options', 'details' => 'theme=' . $this->stylesheet . ' version=' . $version, 'samples' => $ctx, ], 'why_it_matters' => I18n::tr( 'The active theme and its settings were detected. Theme settings can affect performance and SEO, so they provide useful diagnostic context.', 'تعرّف الفحص على الـ theme المفعّل وإعداداته، لأن إعدادات الـ theme تؤثر في الأداء والـ SEO.' ), 'recommendation_steps' => [ I18n::tr('If there are performance or layout findings, review the theme settings first.', 'إذا ظهرت نتائج عن الأداء أو الـ layout فراجع إعدادات الـ theme أولًا.'), ], 'retest' => [ 'how' => I18n::tr('Run the audit after changing the theme settings.', 'شغّل الفحص بعد أي تغيير في إعدادات الـ theme.'), 'pass_condition' => I18n::tr('The updated settings are reflected in the detected stack.', 'تنعكس الإعدادات في البرامج المرصودة.'), ], ], ]; } /** * @param mixed $val * @param array $keys * @return mixed */ private function subset($val, array $keys) { if (empty($keys)) { if (is_array($val)) { $out = []; $i = 0; foreach ($val as $k => $v) { if ($i >= 20) { break; } if (is_scalar($v) || $v === null) { $out[(string) $k] = $v; $i++; } } return $out; } if (is_scalar($val) || $val === null) { return $val; } return '[' . gettype($val) . ']'; } if (!is_array($val)) { return $val; } $out = []; foreach ($keys as $k) { if (array_key_exists($k, $val)) { $out[$k] = $val[$k]; } } return $out; } }