false, 'sanitize_callback' => static fn( $value ) => null === $value ? null : (bool) $value, 'validate_callback' => static fn( $value ) => null === $value || is_bool( $value ), ); return array( 'index' => $tri_state, 'in_sitemap' => $tri_state, 'schema_type' => array( 'required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'title_template' => array( 'required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'description_template' => array( 'required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'seo_analysis_enabled' => array( 'required' => false, 'type' => 'boolean', ), ); } /** * Merges a request's type-level fields into an existing stored entry, * present-key-aware (has_param(), not get_param()) so a tri-state field * explicitly set to null ("inherit") is distinguished from a field the * client simply didn't send this time — both are indistinguishable via * get_param() alone, since JSON null and "absent" both decode to PHP * null. Shared by save_type() and save_taxonomy(). * * @param WP_REST_Request $request REST request. * @param array $existing Existing stored entry for this key. * @return array */ protected function merge_type_level_fields( WP_REST_Request $request, array $existing ): array { foreach ( array( 'index', 'in_sitemap' ) as $field ) { if ( $request->has_param( $field ) ) { $existing[ $field ] = $request->get_param( $field ); } } foreach ( array( 'schema_type', 'title_template', 'description_template' ) as $field ) { $value = $request->get_param( $field ); if ( null !== $value ) { $existing[ $field ] = (string) $value; } } if ( $request->has_param( 'seo_analysis_enabled' ) ) { $existing['seo_analysis_enabled'] = (bool) $request->get_param( 'seo_analysis_enabled' ); } return $existing; } /** * Registers this class's action hooks with the plugin's hook loader. * * @param ABDS_SEO_Loader $loader Hook loader. */ public function register_hooks( ABDS_SEO_Loader $loader ): void { $loader->add_action( 'rest_api_init', $this, 'register_routes' ); } /** * Registers every REST route this controller owns. */ public function register_routes(): void { register_rest_route( self::NAMESPACE_V1, '/search-appearance/settings', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_settings' ), 'permission_callback' => array( $this, 'can_manage_plugin' ), ) ); register_rest_route( self::NAMESPACE_V1, '/search-appearance/settings/global', array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'save_global' ), 'permission_callback' => array( $this, 'can_manage_plugin' ), 'args' => array( 'title_separator' => array( 'required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'title_template' => array( 'required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'description_template' => array( 'required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'default_robots' => array( 'required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => fn( $value ) => in_array( $value, array( 'index,follow', 'noindex,follow', 'index,nofollow', 'noindex,nofollow' ), true ), ), 'default_og_image' => array( 'required' => false, 'sanitize_callback' => 'absint', ), 'schema_entity' => array( 'required' => false, 'type' => 'string', 'sanitize_callback' => array( 'ABDS_SEO_Schema_Settings', 'entity' ), ), 'org_name' => array( 'required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'org_logo' => array( 'required' => false, 'sanitize_callback' => 'absint', ), 'org_profiles' => array( 'required' => false, 'sanitize_callback' => array( 'ABDS_SEO_Schema_Settings', 'profiles' ), ), ), ) ); register_rest_route( self::NAMESPACE_V1, '/search-appearance/settings/type/(?P[a-zA-Z0-9_-]+)', array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'save_type' ), 'permission_callback' => array( $this, 'can_manage_plugin' ), 'args' => $this->type_level_args(), ) ); register_rest_route( self::NAMESPACE_V1, '/search-appearance/settings/taxonomy/(?P[a-zA-Z0-9_-]+)', array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'save_taxonomy' ), 'permission_callback' => array( $this, 'can_manage_plugin' ), 'args' => $this->type_level_args(), ) ); $archive_tri_state = array( 'required' => false, 'sanitize_callback' => static fn( $value ) => null === $value ? null : (bool) $value, 'validate_callback' => static fn( $value ) => null === $value || is_bool( $value ), ); register_rest_route( self::NAMESPACE_V1, '/search-appearance/settings/archives', array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'save_archives' ), 'permission_callback' => array( $this, 'can_manage_plugin' ), 'args' => array( 'author_archive_index' => $archive_tri_state, 'date_archive_index' => array( 'required' => false, 'type' => 'boolean', ), 'search_index' => array( 'required' => false, 'type' => 'boolean', ), 'attachment_redirect' => array( 'required' => false, 'type' => 'boolean', ), ), ) ); } /** * GET /search-appearance/settings — the full snapshot: every tab's * stored settings plus the enumerated post-type/taxonomy lists, in one * response, so tab-switching in the client is instant. Later build * steps add more keys here; the shape below already reserves them. * * @return WP_REST_Response */ public function get_settings(): WP_REST_Response { $settings = ABDS_SEO_Settings_Installer::merge_v2_defaults( get_option( 'abds_seo_settings', array() ) ); return new WP_REST_Response( array( 'global' => $settings['global'], 'type_settings' => $settings['type_settings'], 'taxonomy_settings' => $settings['taxonomy_settings'], 'archive_settings' => $settings['archive_settings'], 'post_types' => ABDS_SEO_Taxonomy_Helper::public_post_types(), 'taxonomies' => ABDS_SEO_Taxonomy_Helper::public_taxonomies(), 'published_author_count' => ABDS_SEO_Author_Count::published_author_count(), 'schema_defaults' => $this->schema_defaults( (array) $settings['global'] ), ) ); } /** * What the Global tab's "Organization / person" fields fall back to when * left empty, so the screen can show it instead of an unexplained blank: * the site name, and the theme's custom logo. * * @param array $global_settings Stored global settings. * @return array{site_name:string, theme_logo_url:string, logo_url:string} */ protected function schema_defaults( array $global_settings ): array { $theme_logo_id = (int) get_theme_mod( 'custom_logo' ); $logo_id = (int) ( $global_settings['org_logo'] ?? 0 ); return array( 'site_name' => wp_specialchars_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES ), 'theme_logo_url' => $theme_logo_id > 0 ? (string) wp_get_attachment_image_url( $theme_logo_id, 'medium' ) : '', 'logo_url' => $logo_id > 0 ? (string) wp_get_attachment_image_url( $logo_id, 'medium' ) : '', ); } /** * POST /search-appearance/settings/global — saves the Global tab, * returns the fresh full settings snapshot (same shape as GET) so the * client never has to guess whether its local state matches storage. * * @param WP_REST_Request $request REST request. * @return WP_REST_Response */ public function save_global( WP_REST_Request $request ): WP_REST_Response { $settings = ABDS_SEO_Settings_Installer::merge_v2_defaults( get_option( 'abds_seo_settings', array() ) ); foreach ( array( 'title_separator', 'title_template', 'description_template', 'default_robots' ) as $field ) { $value = $request->get_param( $field ); if ( null !== $value ) { $settings['global'][ $field ] = (string) $value; } } $og_image = $request->get_param( 'default_og_image' ); if ( null !== $og_image ) { $settings['global']['default_og_image'] = (int) $og_image; } // Site entity for the structured-data graph. The values were already // normalised by the route's sanitize callbacks. if ( null !== $request->get_param( 'schema_entity' ) ) { $settings['global']['schema_entity'] = (string) $request->get_param( 'schema_entity' ); } if ( null !== $request->get_param( 'org_name' ) ) { $settings['global']['org_name'] = (string) $request->get_param( 'org_name' ); } if ( null !== $request->get_param( 'org_profiles' ) ) { $settings['global']['org_profiles'] = (array) $request->get_param( 'org_profiles' ); } if ( null !== $request->get_param( 'org_logo' ) ) { $logo = (int) $request->get_param( 'org_logo' ); // Only a real image attachment (or 0 = none / theme logo) is accepted. $settings['global']['org_logo'] = ( $logo > 0 && wp_attachment_is_image( $logo ) ) ? $logo : 0; } update_option( 'abds_seo_settings', $settings ); return $this->get_settings(); } /** * POST /search-appearance/settings/type/{type} — saves one post type's * settings. 404s on a type that isn't currently public (covers both a * typo'd slug and a type that was public when the client loaded but has * since been deregistered) — the same enumerated list this route * validates against is what the Content Types tab renders rows from, so * there is exactly one path to writing this policy, not a client free * to send an arbitrary slug. * * @param WP_REST_Request $request REST request. * @return WP_REST_Response|WP_Error */ public function save_type( WP_REST_Request $request ) { $type = (string) $request->get_param( 'type' ); if ( ! array_key_exists( $type, ABDS_SEO_Taxonomy_Helper::public_post_types() ) ) { return new WP_Error( 'abds_seo_unknown_post_type', __( 'That post type is not public.', 'abds-seo' ), array( 'status' => 404 ) ); } $settings = ABDS_SEO_Settings_Installer::merge_v2_defaults( get_option( 'abds_seo_settings', array() ) ); $settings['type_settings'][ $type ] = $this->merge_type_level_fields( $request, (array) ( $settings['type_settings'][ $type ] ?? array() ) ); update_option( 'abds_seo_settings', $settings ); return $this->get_settings(); } /** * POST /search-appearance/settings/taxonomy/{taxonomy} — same contract * as save_type(), keyed against the public taxonomy list instead. * * @param WP_REST_Request $request REST request. * @return WP_REST_Response|WP_Error */ public function save_taxonomy( WP_REST_Request $request ) { $taxonomy = (string) $request->get_param( 'taxonomy' ); if ( ! array_key_exists( $taxonomy, ABDS_SEO_Taxonomy_Helper::public_taxonomies() ) ) { return new WP_Error( 'abds_seo_unknown_taxonomy', __( 'That taxonomy is not public.', 'abds-seo' ), array( 'status' => 404 ) ); } $settings = ABDS_SEO_Settings_Installer::merge_v2_defaults( get_option( 'abds_seo_settings', array() ) ); $settings['taxonomy_settings'][ $taxonomy ] = $this->merge_type_level_fields( $request, (array) ( $settings['taxonomy_settings'][ $taxonomy ] ?? array() ) ); update_option( 'abds_seo_settings', $settings ); return $this->get_settings(); } /** * POST /search-appearance/settings/archives — saves the Archives + * System tabs' settings in one request (they share one storage bucket, * archive_settings). `author_archive_index` is tri-state: explicit * true/false overrides the dynamic 1-author recommendation, null * explicitly restores "follow the recommendation" — see * ABDS_SEO_Policy_Engine::author_archive_settings(). date_archive/ * search/attachment_redirect are plain booleans (no dynamic default to * restore to). 404 is never accepted here — it isn't user-configurable, * see ABDS_SEO_Policy_Engine::type_settings_for_key(). * * @param WP_REST_Request $request REST request. * @return WP_REST_Response */ public function save_archives( WP_REST_Request $request ): WP_REST_Response { $settings = ABDS_SEO_Settings_Installer::merge_v2_defaults( get_option( 'abds_seo_settings', array() ) ); $archive = (array) $settings['archive_settings']; if ( $request->has_param( 'author_archive_index' ) ) { $archive['author_archive']['index'] = $request->get_param( 'author_archive_index' ); } foreach ( array( 'date_archive_index' => array( 'date_archive', 'index' ), 'search_index' => array( 'search', 'index' ), ) as $field => $path ) { $value = $request->get_param( $field ); if ( null !== $value ) { $archive[ $path[0] ][ $path[1] ] = (bool) $value; } } $attachment_redirect = $request->get_param( 'attachment_redirect' ); if ( null !== $attachment_redirect ) { $archive['attachment_redirect'] = (bool) $attachment_redirect; } $settings['archive_settings'] = $archive; update_option( 'abds_seo_settings', $settings ); return $this->get_settings(); } }