self::MAX_ATTRIBUTES ) { /* translators: %d: maximum number of attributes. */ return array( sprintf( __( 'Define at most %d attributes.', 'feedweaver' ), self::MAX_ATTRIBUTES ) ); } $errors = array(); $paths = self::element_paths( $mappings ); $seen = array(); foreach ( array_values( $attributes ) as $i => $row ) { $n = $i + 1; $element = isset( $row['element'] ) && is_string( $row['element'] ) ? $row['element'] : null; $name = isset( $row['name'] ) ? $row['name'] : ''; $type = isset( $row['type'] ) ? $row['type'] : ''; $value = isset( $row['value'] ) ? $row['value'] : ''; if ( null === $element || ! in_array( $element, $paths, true ) ) { /* translators: %d: attribute row number. */ $errors[] = sprintf( __( 'Attribute %d: choose the element it belongs to from your field mappings (or the item element).', 'feedweaver' ), $n ); } if ( ! Path_Validator::is_valid_attribute_name( $name, true ) ) { /* translators: %d: attribute row number. */ $errors[] = sprintf( __( 'Attribute %d: the name must be letters, numbers, hyphens or underscores, start with a letter or underscore, and not start with "xml" (xml:lang is allowed).', 'feedweaver' ), $n ); } else { $prefix = Path_Validator::prefix_of( $name ); if ( null !== $prefix && 'xml' !== $prefix && ! in_array( $prefix, $prefixes, true ) ) { /* translators: 1: attribute row number, 2: prefix. */ $errors[] = sprintf( __( 'Attribute %1$d: the namespace prefix "%2$s" is not declared under XML Namespaces.', 'feedweaver' ), $n, $prefix ); } $key = ( null === $element ? '?' : $element ) . "\0" . $name; if ( isset( $seen[ $key ] ) ) { /* translators: 1: attribute row number, 2: attribute name. */ $errors[] = sprintf( __( 'Attribute %1$d: "%2$s" is already set on the same element.', 'feedweaver' ), $n, $name ); } $seen[ $key ] = true; } if ( 'static' === $type ) { if ( ! is_string( $value ) || strlen( $value ) > self::MAX_STATIC ) { /* translators: 1: attribute row number, 2: maximum length. */ $errors[] = sprintf( __( 'Attribute %1$d: fixed text may be at most %2$d characters.', 'feedweaver' ), $n, self::MAX_STATIC ); } } elseif ( 'source' === $type ) { if ( ! Source_Catalog::is_scalar_source( $value ) ) { /* translators: %d: attribute row number. */ $errors[] = sprintf( __( 'Attribute %d: choose a WordPress field that holds a single value (not a list of terms).', 'feedweaver' ), $n ); } elseif ( Source_Catalog::is_meta( $value ) ) { foreach ( Meta_Policy::check( (string) Source_Catalog::meta_key( $value ) ) as $reason ) { /* translators: 1: attribute row number, 2: reason. */ $errors[] = sprintf( __( 'Attribute %1$d: %2$s', 'feedweaver' ), $n, $reason ); } } } else { /* translators: %d: attribute row number. */ $errors[] = sprintf( __( 'Attribute %d: choose fixed text or a WordPress field as the value.', 'feedweaver' ), $n ); } }//end foreach return $errors; } /** * Validates custom names for repeated (taxonomy) child elements. * * @param array $repeat_names List of array{path:string,name:string}. * @param array $mappings Mappings. * @param string[] $prefixes Declared prefixes. * @return string[] */ public static function validate_repeat_names( array $repeat_names, array $mappings, array $prefixes ): array { if ( count( $repeat_names ) > self::MAX_REPEAT_NAMES ) { /* translators: %d: maximum. */ return array( sprintf( __( 'Define at most %d repeated element names.', 'feedweaver' ), self::MAX_REPEAT_NAMES ) ); } $taxonomy_paths = array(); foreach ( $mappings as $mapping ) { if ( isset( $mapping['source'], $mapping['path'] ) && Source_Catalog::is_taxonomy( $mapping['source'] ) ) { $taxonomy_paths[] = trim( (string) $mapping['path'] ); } } $errors = array(); $seen = array(); foreach ( array_values( $repeat_names ) as $i => $row ) { $n = $i + 1; $path = isset( $row['path'] ) && is_string( $row['path'] ) ? $row['path'] : ''; $name = isset( $row['name'] ) ? $row['name'] : ''; if ( ! in_array( $path, $taxonomy_paths, true ) ) { /* translators: %d: repeated element row number. */ $errors[] = sprintf( __( 'Repeated element %d: choose one of your taxonomy fields.', 'feedweaver' ), $n ); } elseif ( isset( $seen[ $path ] ) ) { /* translators: 1: repeated element row number, 2: path. */ $errors[] = sprintf( __( 'Repeated element %1$d: "%2$s" already has a child element name.', 'feedweaver' ), $n, $path ); } $seen[ $path ] = true; if ( ! Path_Validator::is_valid_name( $name, true ) ) { /* translators: %d: repeated element row number. */ $errors[] = sprintf( __( 'Repeated element %d: the name must be letters, numbers, hyphens or underscores, start with a letter or underscore, and not start with "xml".', 'feedweaver' ), $n ); } else { $prefix = Path_Validator::prefix_of( $name ); if ( null !== $prefix && ! in_array( $prefix, $prefixes, true ) ) { /* translators: 1: repeated element row number, 2: prefix. */ $errors[] = sprintf( __( 'Repeated element %1$d: the namespace prefix "%2$s" is not declared under XML Namespaces.', 'feedweaver' ), $n, $prefix ); } } }//end foreach return $errors; } }