*/ public static function taxonomies(): array { $out = array(); foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $taxonomy ) { if ( ! $taxonomy->publicly_queryable || in_array( $taxonomy->name, array( 'post_format', 'product_visibility' ), true ) ) { continue; } $out[] = (string) $taxonomy->name; } /** * Filter the taxonomies whose terms get SEO fields. * * @param array $out Taxonomy names. */ return (array) apply_filters( 'airanksuite/editor/term_taxonomies', $out ); } /** @var array Taxonomies already hooked, so a re-register is harmless. */ private array $hooked = array(); /** * Hook the taxonomies that exist now, and any registered afterwards — * WooCommerce and most themes register theirs on `init`, which can be * after this plugin's modules boot. */ public function boot(): void { foreach ( self::taxonomies() as $taxonomy ) { $this->hook( $taxonomy ); } add_action( 'registered_taxonomy', function ( $taxonomy ): void { if ( in_array( (string) $taxonomy, self::taxonomies(), true ) ) { $this->hook( (string) $taxonomy ); } }, 10, 1 ); } private function hook( string $taxonomy ): void { if ( isset( $this->hooked[ $taxonomy ] ) ) { return; } $this->hooked[ $taxonomy ] = true; add_action( "{$taxonomy}_edit_form_fields", array( $this, 'render' ), 20, 2 ); add_action( "edited_{$taxonomy}", array( $this, 'save' ), 10, 1 ); add_action( "created_{$taxonomy}", array( $this, 'save' ), 10, 1 ); } /** * The current values of one term, in the shape the fields use. * * @return array */ public static function values( int $term_id ): array { $social = get_term_meta( $term_id, ImportWriter::TERM_SOCIAL, true ); $social = is_array( $social ) ? $social : array(); $robots = get_term_meta( $term_id, ImportWriter::TERM_ROBOTS, true ); return array( 'title' => (string) get_term_meta( $term_id, ImportWriter::TERM_TITLE, true ), 'desc' => (string) get_term_meta( $term_id, ImportWriter::TERM_DESC, true ), 'canonical' => (string) get_term_meta( $term_id, ImportWriter::TERM_CANONICAL, true ), 'robots' => is_array( $robots ) ? array_values( array_map( 'strval', $robots ) ) : array(), 'og_title' => (string) ( $social['og_title'] ?? '' ), 'og_desc' => (string) ( $social['og_desc'] ?? '' ), 'og_image' => (string) ( $social['og_image'] ?? '' ), ); } /** * @param WP_Term|int $term Term being edited. * @param string $taxonomy Taxonomy name (unused; the hook passes it). */ public function render( $term, string $taxonomy = '' ): void { $term = $term instanceof WP_Term ? $term : get_term( (int) $term ); if ( ! $term instanceof WP_Term ) { return; } $tax = get_taxonomy( $term->taxonomy ); if ( $tax && ! current_user_can( $tax->cap->edit_terms ) ) { return; } $values = self::values( (int) $term->term_id ); $preview = Archives::preview_for_term( $term ); $source = array( 'term' => __( 'This is what you typed here.', 'ai-rank-suite' ), 'template' => __( 'From the template for this taxonomy — type a title above to override it for this term only.', 'ai-rank-suite' ), 'default' => __( 'The built-in fallback — no template is set for this taxonomy.', 'ai-rank-suite' ), ); wp_nonce_field( 'airanksuite_term_' . (int) $term->term_id, 'airanksuite_term_nonce' ); ?>

__( 'noindex — keep this archive out of search results', 'ai-rank-suite' ), 'nofollow' => __( 'nofollow — do not follow the links on it', 'ai-rank-suite' ), 'noarchive' => __( 'noarchive — no cached copy', 'ai-rank-suite' ), 'nosnippet' => __( 'nosnippet — no text snippet', 'ai-rank-suite' ), 'noimageindex' => __( 'noimageindex — do not index its images', 'ai-rank-suite' ), ); foreach ( RobotsModule::DIRECTIVES as $directive ) { printf( '', esc_attr( $directive ), checked( in_array( $directive, $values['robots'], true ), true, false ), esc_html( $labels[ $directive ] ?? $directive ) ); } ?> taxonomy ) : null; if ( $tax && ! current_user_can( $tax->cap->edit_terms ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $input = (array) wp_unslash( $_POST['airanksuite_term'] ); $this->write( $term_id, ImportWriter::TERM_TITLE, Sanitize::template( (string) ( $input['title'] ?? '' ) ) ); $this->write( $term_id, ImportWriter::TERM_DESC, Sanitize::template_multiline( (string) ( $input['desc'] ?? '' ) ) ); $this->write( $term_id, ImportWriter::TERM_CANONICAL, esc_url_raw( trim( (string) ( $input['canonical'] ?? '' ) ) ) ); $robots = array_values( array_intersect( RobotsModule::DIRECTIVES, array_map( 'sanitize_key', (array) ( $input['robots'] ?? array() ) ) ) ); if ( empty( $robots ) ) { delete_term_meta( $term_id, ImportWriter::TERM_ROBOTS ); } else { update_term_meta( $term_id, ImportWriter::TERM_ROBOTS, $robots ); } $social = array(); foreach ( self::SOCIAL_KEYS as $key ) { $value = trim( (string) ( $input[ $key ] ?? '' ) ); if ( '' === $value ) { continue; } $social[ $key ] = 'og_image' === $key ? esc_url_raw( $value ) : sanitize_text_field( $value ); } if ( empty( $social ) ) { delete_term_meta( $term_id, ImportWriter::TERM_SOCIAL ); } else { update_term_meta( $term_id, ImportWriter::TERM_SOCIAL, $social ); } } /** * An empty field deletes its meta rather than storing '': "nothing here" * and "deliberately blank" must not read the same to the archive output, * and an undo journal that restores '' would leave the row behind. */ private function write( int $term_id, string $key, string $value ): void { if ( '' === $value ) { delete_term_meta( $term_id, $key ); return; } update_term_meta( $term_id, $key, $value ); } }