SeoSettings::all(), 'diagnostics' => $this->diagnostics(), ] ); } public function update(UpdateSeoSettingsRequest $request) { $data = $request->toSettingsData(); $added = Config::addOption(SeoSettings::OPTION_NAME->value, $data); if (!$added) { Config::updateOption(SeoSettings::OPTION_NAME->value, $data); } // Switching an archive segment on or off changes the rewrite rules the // portal registers. Dropping the stored rules makes WordPress rebuild // them on the next front-end request — flush_rewrite_rules() here would // rebuild them from the REST request's own rule set, which is not the // front end's. delete_option('rewrite_rules'); return Response::success( [ 'settings' => $data, 'diagnostics' => $this->diagnostics(), ] ); } /** * Read-only facts the settings screen needs to be honest about what is live. * * Without these an administrator cannot tell whether their SEO plugin is * winning, whether the portal is exposed to crawlers at all, or how much of * the community is actually indexable. * * @return array */ private function diagnostics(): array { $general = Config::getOption(GeneralSettings::OPTION_NAME->value, []); $counts = wp_count_posts(PostTypes::BIT_CONNECT->value); $archives = []; foreach (PortalTaxonomies::map() as $segment => $taxonomy) { $terms = get_terms(['taxonomy' => $taxonomy, 'hide_empty' => true, 'fields' => 'ids']); $archives[$segment] = [ 'indexable' => PortalTaxonomies::isIndexable($segment), 'terms' => is_wp_error($terms) ? 0 : \count((array) $terms), // Named and addressed here because the list is not fixed: a // plugin may add an archive the screen has no label for. 'label' => PortalTaxonomies::labelFor($segment), 'slug' => PortalTaxonomies::slugFor($segment), ]; } return [ 'seoPlugin' => SeoPluginBridge::detect(), 'portalIsPublic' => ($general['portalAccess'] ?? 'everyone') === 'everyone', // WordPress's own "Discourage search engines" — the switch that // keeps the whole site, portal included, out of search. 'searchEnginesDiscouraged' => (string) get_option('blog_public', '1') === '0', 'crawlerContent' => SeoContent::isEnabled(), // Empty when the sitemap is not being served, so the screen never // offers a URL that 404s. 'sitemapUrl' => PortalSitemap::isPublished() ? PortalSitemap::feedUrl() : '', 'portalUrl' => SeoContent::portalUrl(), 'publishedTopics' => (int) ($counts->publish ?? 0), 'archives' => $archives, ]; } }