| 330 | * Allow disabling some warnings. |
| 331 | * |
| 332 | * See https://meta.trac.wordpress.org/ticket/6256 |
| 333 | */ |
| 334 | function disable_discarding_some_warnings( $project_id, $translation_set_id, $translation_id, $warning, $user ) { |
| 335 | if ( 'unexpected_sprintf_token' === $warning ) { |
| 336 | GP::$current_route->die_with_error( __( 'This warning cannot be discarded. Please remove the unexpected placeholders. A % needs to be translated as %%.', 'wporg' ) ); |
| 337 | } |
| 338 | |
| 339 | if ( 'placeholders' === $warning ) { |
| 340 | // Since we only get the warning key, we need to determine the exact warning to decide if we should prevent it. |
| 341 | function _placeholders_counts( $string, $re ) { |
| 342 | $counts = array(); |
| 343 | preg_match_all( "/$re/", $string, $matches ); |
| 344 | foreach ( $matches[0] as $match ) { |
| 345 | $counts[ $match ] = gp_array_get( $counts, $match, 0 ) + 1; |
| 346 | } |
| 347 | |
| 348 | return $counts; |
| 349 | } |
| 350 | |
| 351 | $placeholders_re = apply_filters( 'gp_warning_placeholders_re', '(?<!%)%(\d+\$(?:\d+)?)?(\.\d+)?[bcdefgosuxEFGX%l@]' ); |
| 352 | $translation = GP::$translation->get( $translation_id ); |
| 353 | $original = GP::$original->get( $translation->original_id ); |
| 354 | foreach ( $translation->translations() as $k => $t ) { |
| 355 | $original_counts = _placeholders_counts( $k === 0 ? $original->singular : $original->plural, $placeholders_re ); |
| 356 | $translation_counts = _placeholders_counts( $t, $placeholders_re ); |
| 357 | $all_placeholders = array_unique( array_merge( array_keys( $original_counts ), array_keys( $translation_counts ) ) ); |
| 358 | foreach ( $all_placeholders as $placeholder ) { |
| 359 | $original_count = gp_array_get( $original_counts, $placeholder, 0 ); |
| 360 | $translation_count = gp_array_get( $translation_counts, $placeholder, 0 ); |
| 361 | if ( $original_count < $translation_count ) { |
| 362 | if ( '%%' === $placeholder ) { |
| 363 | // Extra %% is ok. |
| 364 | continue; |
| 365 | } |
| 366 | GP::$current_route->die_with_error( __( 'This warning cannot be discarded. Please remove the extra placeholders.', 'wporg' ) ); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /** |