ui = $ui; $this->post_type_settings = $post_type_settings; $this->archive_settings = $archive_settings; $this->location_rules = $location_rules; $this->global_settings = $global_settings; } public function slug(): string { return 'content'; } public function label(): string { return __( 'Content', 'acez-seo' ); } public function icon(): string { return 'post-type'; } public function enqueue(): void { // wp.media modal: the per-type default Open Graph image picker. wp_enqueue_media(); wp_enqueue_script( 'acez-seo-media-picker', ACEZ_SEO_URL . 'assets/js/media-picker.js', array( 'media-editor' ), ACEZ_SEO_VERSION, true ); } /** * Owns archive:* keys and bare post-type slugs. The coordinator asks * this tab LAST — a CPT slug colliding with another tab's form key must * lose to the named key, exactly like in the original if/elseif chain. */ public function handles_form_key( string $raw_tab ): bool { if ( str_starts_with( $raw_tab, 'archive:' ) ) { return true; } return in_array( sanitize_key( $raw_tab ), PostTypes::supported(), true ); } /** @return array */ public function save( string $raw_tab ): array { // Defence in depth: SettingsPage::handle_save() verifies this nonce // before dispatch; re-checking keeps every entry point independently safe. check_admin_referer( FormControls::NONCE_ACTION ); if ( str_starts_with( $raw_tab, 'archive:' ) ) { $taxonomy = sanitize_key( substr( $raw_tab, strlen( 'archive:' ) ) ); if ( in_array( $taxonomy, Taxonomies::supported(), true ) ) { $this->archive_settings->update( $taxonomy, array( 'schema_enabled' => isset( $_POST['schema_enabled'] ), 'robots_index' => isset( $_POST['robots_index'] ), 'robots_follow' => isset( $_POST['robots_follow'] ), ) ); } // Archives live inside the "Treści" tab now — return to the same // taxonomy's view. return array( 'tab' => 'content', 'archive' => $taxonomy, ); } $post_type = sanitize_key( $raw_tab ); $this->save_post_type( $post_type ); // Post-type tabs live inside the "Treści" (content) tab now, not // as their own top-level tab — redirect back to the sub-view. return array( 'tab' => 'content', 'post_type' => $post_type, ); } private function save_post_type( string $post_type ): void { // Defence in depth: SettingsPage::handle_save() verifies this nonce // before dispatch; re-checking keeps every entry point independently safe. check_admin_referer( FormControls::NONCE_ACTION ); $this->post_type_settings->update( $post_type, array( 'schema_enabled' => isset( $_POST['schema_enabled'] ), 'title_template' => isset( $_POST['title_template'] ) ? TemplateSanitizer::sanitize_line( wp_strip_all_tags( wp_unslash( $_POST['title_template'] ) ) ) : '', 'description_template' => isset( $_POST['description_template'] ) ? TemplateSanitizer::sanitize_multiline( wp_strip_all_tags( wp_unslash( $_POST['description_template'] ) ) ) : '', 'robots_index' => isset( $_POST['robots_index'] ), 'robots_follow' => isset( $_POST['robots_follow'] ), 'author_schema_enabled' => isset( $_POST['author_schema_enabled'] ), 'og_image_id' => isset( $_POST['og_image_id'] ) ? absint( $_POST['og_image_id'] ) : 0, // The ACF field input renders only while ACF is active — when // it is absent from POST, keep the stored value instead of // wiping it during a temporary ACF deactivation (M49). 'og_acf_field' => isset( $_POST['og_acf_field'] ) ? TemplateSanitizer::sanitize_line( wp_strip_all_tags( wp_unslash( $_POST['og_acf_field'] ) ) ) : (string) ( $this->post_type_settings->get( $post_type )['og_acf_field'] ?? '' ), ) ); $selected_templates = isset( $_POST['location_page_templates'] ) ? array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['location_page_templates'] ) ) : array(); $rules = array(); foreach ( $selected_templates as $slug ) { $rules[] = array( 'type' => 'page_template', 'value' => (string) $slug, ); } $selected_conditions = isset( $_POST['location_conditional_tags'] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['location_conditional_tags'] ) ) : array(); foreach ( $selected_conditions as $tag ) { $rules[] = array( 'type' => 'conditional_tag', 'value' => (string) $tag, ); } $groups_data = array() !== $rules ? array( $rules ) : array(); $this->location_rules->update( $post_type, $groups_data ); /** * Fires after the per-post-type settings save — the SAME request, * already nonce-verified by SettingsPage's dispatcher. Extensions * read their own $_POST fields and persist to their own storage. * * @param string $post_type */ do_action( 'acez_seo_content_settings_save', $post_type ); } public function render(): void { $post_types = PostTypes::supported(); // Legacy ?tab=archives alias (the former top-level tab) — the // coordinator maps it onto this tab; here it just preselects the // archives group. $prefer_archives = isset( $_GET['tab'] ) && 'archives' === sanitize_key( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only navigation state (which sidebar group to preselect); nothing is written, and the sanitize_key()'d value is only compared against one literal. $taxonomies = Taxonomies::supported(); echo '

' . esc_html__( 'Default meta tags and schema.org settings per content type and for taxonomy archives. Choose an item from the list on the left.', 'acez-seo' ) . '

'; if ( array() === $post_types && array() === $taxonomies ) { echo '
' . esc_html__( 'No public content types were detected.', 'acez-seo' ) . '
'; return; } $requested_type = isset( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only navigation state (which content type the sidebar opens on); nothing is written, and the sanitize_key()'d value is accepted only if in_array() finds it in the supported-post-type list below. $requested_archive = isset( $_GET['archive'] ) ? sanitize_key( wp_unslash( $_GET['archive'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only navigation state (which taxonomy archive the sidebar opens on); nothing is written, and the sanitize_key()'d value is accepted only if in_array() finds it in the supported-taxonomy list below. $selected_archive = in_array( $requested_archive, $taxonomies, true ) ? $requested_archive : ''; // Legacy ?tab=archives (no explicit selection) or a site with no // public post types at all → open the first taxonomy archive. if ( '' === $selected_archive && ( $prefer_archives || array() === $post_types ) && array() !== $taxonomies ) { $selected_archive = $taxonomies[0]; } $selected_type = ''; if ( '' === $selected_archive && array() !== $post_types ) { $selected_type = in_array( $requested_type, $post_types, true ) ? $requested_type : $post_types[0]; } echo '
'; echo ''; echo '
'; if ( '' !== $selected_archive ) { echo '

' . esc_html__( 'Settings for archive pages (categories, tags, custom taxonomies): whether to generate schema.org (CollectionPage) and how to set robots.', 'acez-seo' ) . '

'; $this->render_archive_card( $selected_archive ); } else { $this->render_post_type_tab( $selected_type ); } echo '
'; echo '
'; } private function render_post_type_tab( string $post_type ): void { $settings = $this->post_type_settings->get( $post_type ); echo '

' . esc_html__( 'Default meta tags and schema.org settings for this content type. Individual entries can override them in the editor.', 'acez-seo' ) . '

'; $this->ui->card_open( __( 'Settings', 'acez-seo' ), 'post-type', '', 'content-settings' ); // Soft guidance only (deliberately NOT a hard gate): schema generation // works fine without Organization data — the node falls back to the // site name — so we inform instead of disabling the toggle. if ( '' === trim( (string) $this->global_settings->get()['organization_name'] ) ) { printf( '

%1$s %3$s

', esc_html__( 'Organization data is not filled in — the Organization node in the generated schema.org graph will fall back to the site name only (no logo, no social profiles), so your brand identity for search engines will be incomplete.', 'acez-seo' ), esc_url( admin_url( 'admin.php?page=' . FormControls::PAGE_SLUG . '&tab=global' ) ), esc_html__( 'Fill in the data in the Schema tab', 'acez-seo' ) ); } $this->ui->form_open( $post_type ); echo ''; $this->ui->checkbox_field( 'schema_enabled', __( 'Generate schema.org for this content type', 'acez-seo' ), (bool) $settings['schema_enabled'], 'schema_types' ); $this->ui->text_field( 'title_template', __( 'Title template', 'acez-seo' ), (string) $settings['title_template'], '%post_title% %sep% %site_name%', 'seo_title' ); $this->ui->textarea_field( 'description_template', __( 'Description template', 'acez-seo' ), (string) $settings['description_template'], '%post_excerpt%', 'seo_description' ); $this->ui->media_picker_field( 'og_image_id', 'post_type_og_image_id', __( 'Default Open Graph image', 'acez-seo' ), (int) ( $settings['og_image_id'] ?? 0 ), 'og_default_image' ); // M49 — only when ACF (or the Secure Custom Fields fork) is active. if ( \AcezSeo\Support\AcfDetector::is_active() ) { $this->ui->text_field( 'og_acf_field', __( 'ACF field for the OG image (optional)', 'acez-seo' ), (string) ( $settings['og_acf_field'] ?? '' ), __( 'e.g. main_image', 'acez-seo' ), 'og_acf_field' ); } $this->ui->checkbox_field( 'robots_index', __( 'Allow indexing (index)', 'acez-seo' ), (bool) $settings['robots_index'], 'seo_robots' ); $this->ui->checkbox_field( 'robots_follow', __( 'Allow following links (follow)', 'acez-seo' ), (bool) $settings['robots_follow'] ); $this->ui->checkbox_field( 'author_schema_enabled', __( 'Show author (Person) in schema.org', 'acez-seo' ), (bool) $settings['author_schema_enabled'], 'author_schema' ); $this->render_location_rules_field( $post_type ); /** * Fires inside the per-post-type settings form (same table, same * nonce) — extensions (e.g. the Pro addon's ACF→schema field * mapping, M30) add their own rows here. * * @param string $post_type * @param array $settings */ do_action( 'acez_seo_content_settings_fields', $post_type, $settings ); echo '
'; submit_button(); $this->ui->form_close(); $this->ui->card_close(); } private function render_archive_card( string $taxonomy ): void { $settings = $this->archive_settings->get( $taxonomy ); $taxonomy_object = get_taxonomy( $taxonomy ); $label = $taxonomy_object instanceof \WP_Taxonomy ? $taxonomy_object->labels->name : $taxonomy; $this->ui->card_open( $label, 'archive', '', 'archive-settings' ); $this->ui->form_open( 'archive:' . $taxonomy ); echo ''; $this->ui->checkbox_field( 'schema_enabled', __( 'Generate schema.org (CollectionPage) for archives of this taxonomy', 'acez-seo' ), (bool) $settings['schema_enabled'], 'schema_types' ); $this->ui->checkbox_field( 'robots_index', __( 'Allow indexing (index)', 'acez-seo' ), (bool) $settings['robots_index'], 'seo_robots' ); $this->ui->checkbox_field( 'robots_follow', __( 'Allow following links (follow)', 'acez-seo' ), (bool) $settings['robots_follow'] ); echo '
'; submit_button( __( 'Save', 'acez-seo' ) . ' — ' . $label ); $this->ui->form_close(); $this->ui->card_close(); } private function render_location_rules_field( string $post_type ): void { $templates = PageTemplates::all( $post_type ); $conditions = ConditionalTags::all(); $has_any_options = array() !== $templates || array() !== $conditions; if ( ! $has_any_options ) { return; } $selected_templates = $this->location_rules->get_page_template_slugs( $post_type ); $selected_conditions = $this->location_rules->get_conditional_tags( $post_type ); echo '' . esc_html__( 'Restrict generation', 'acez-seo' ) . wp_kses( HelpIcon::render( 'location_rules' ), KsesTags::help_icon() ) . ''; echo '

' . esc_html__( 'Leave everything unchecked to apply to all. Checking any option below (from either group) restricts generation to matching posts/pages only.', 'acez-seo' ) . '

'; if ( array() !== $templates ) { echo '

' . esc_html__( 'Page templates', 'acez-seo' ) . '

'; foreach ( $templates as $slug => $label ) { printf( '', esc_attr( $slug ), checked( in_array( $slug, $selected_templates, true ), true, false ), esc_html( $label ) ); } } if ( array() !== $conditions ) { echo '

' . esc_html__( 'Additional conditions', 'acez-seo' ) . '

'; foreach ( $conditions as $condition ) { printf( '', esc_attr( $condition['tag'] ), checked( in_array( $condition['tag'], $selected_conditions, true ), true, false ), esc_html( $condition['label'] ) ); } } echo ''; } }