$model * @param int $post_id * @param string $subtree_id * @return array */ protected function analyze( array $model, int $post_id, string $subtree_id ): array { $by_count = array(); foreach ( (array) $model['rows'] as $row ) { $count = (int) $row['lane_count']; if ( $count < 2 ) { continue; } $by_count[ $count ][] = $row; } $plan = array(); foreach ( $by_count as $rows ) { if ( count( $rows ) < 2 ) { continue; } $tallies = array_count_values( array_map( static fn( array $r ): string => (string) $r['ratio'], $rows ) ); arsort( $tallies ); $winner = (string) array_key_first( $tallies ); $template = null; foreach ( $rows as $row ) { if ( (string) $row['ratio'] === $winner ) { $template = $row; break; } } if ( null === $template ) { continue; } $widths = array_map( static fn( array $l ): array => (array) $l['settings'], (array) $template['lanes'] ); foreach ( $rows as $row ) { if ( (string) $row['ratio'] === $winner ) { continue; } foreach ( array_values( (array) $row['lanes'] ) as $i => $lane ) { $source = $widths[ $i ] ?? array(); if ( isset( $source['_column_size'] ) ) { $plan[ (string) $lane['id'] ]['_column_size'] = $source['_column_size']; } if ( isset( $source['width'] ) ) { $plan[ (string) $lane['id'] ]['width'] = $source['width']; } } } } if ( array() === $plan ) { return Design_Mutator::report( array( 'changed' => array(), 'applied' => false ), '', __( 'Rows of each lane count already share one balance, so there was nothing to bring into line.', 'acrossai-abilities-manager' ) ); } $result = Design_Mutator::edit( $model, static function ( array &$settings, array $element ) use ( $plan ): array { $id = (string) ( $element['id'] ?? '' ); if ( ! isset( $plan[ $id ] ) ) { return array(); } $changes = array(); foreach ( $plan[ $id ] as $key => $value ) { $changes = array_merge( $changes, Design_Mutator::set( $settings, (string) $key, $value ) ); } return $changes; } ); if ( is_wp_error( $result ) ) { return $result; } return Design_Mutator::report( $result, __( 'Brought rows into line with the majority balance for their lane count.', 'acrossai-abilities-manager' ), __( 'Every row already matched the majority balance, so nothing was changed.', 'acrossai-abilities-manager' ) ); } }