*/ const WORRY = array( 'fine' => 'Fine, just so you know', 'look' => 'Worth a look', 'fix' => 'Fix before you go on', 'unknown' => 'Not checked yet', ); /** * The entries, loaded once. * * @return array> */ public static function entries() { static $entries = null; if ( null === $entries ) { $entries = is_file( __DIR__ . '/data-explainers.php' ) ? (array) include __DIR__ . '/data-explainers.php' : array(); } return $entries; } /** * The explanation for a finding, or null when there is none. * * @param array $check A check: title, status, blocking, optional explain key and facts. * @return array{noticed:string,why:string,worry:string,worry_label:string,do:string}|null */ public static function say( array $check ) { $title = (string) ( $check['title'] ?? '' ); $status = (string) ( $check['status'] ?? '' ); $key = (string) ( $check['explain'] ?? '' ); foreach ( self::entries() as $e ) { if ( isset( $e['s'] ) && ! in_array( $status, (array) $e['s'], true ) ) { continue; } $hit = false; if ( '' !== $key && isset( $e['k'] ) ) { $hit = ( $key === $e['k'] ); } elseif ( isset( $e['m'] ) ) { $hit = ( $title === $e['m'] ); } elseif ( isset( $e['re'] ) ) { $hit = (bool) preg_match( $e['re'], $title ); } if ( ! $hit ) { continue; } $vars = self::vars( $check ); $worry = (string) ( $e['worry'] ?? '' ); if ( 'pass' === $status ) { $worry = ''; } elseif ( 'note' === $status ) { $worry = 'fine'; } elseif ( '' === $worry ) { $worry = 'warning' === $status ? 'fix' : ( 'review' === $status ? 'look' : ( 'note' === $status ? 'fine' : ( 'not_checked' === $status ? 'unknown' : '' ) ) ); } return array( 'noticed' => strtr( (string) $e['noticed'], $vars ), 'why' => strtr( (string) ( $e['why'] ?? '' ), $vars ), 'worry' => $worry, 'worry_label' => self::WORRY[ $worry ] ?? '', 'do' => strtr( (string) ( $e['do'] ?? '' ), $vars ), ); } return null; } /** * Placeholder values for a finding. * * @param array $check The check. * @return array */ private static function vars( array $check ) { $title = (string) ( $check['title'] ?? '' ); $facts = (array) ( $check['facts'] ?? array() ); $noun = ''; $plugins = ''; if ( preg_match( '~^(WordPress|PHP|WooCommerce)\b~', $title, $m ) ) { $noun = $m[1]; } $slug = false !== strpos( $title, ': ' ) ? trim( (string) substr( strrchr( $title, ':' ), 1 ), " \n\r\t\v\x00" ) : ''; if ( ! empty( $facts['plugins'] ) ) { $names = array_values( array_unique( array_map( 'strval', (array) $facts['plugins'] ) ) ); $last = count( $names ) > 1 ? array_pop( $names ) : ''; $plugins = '' === $last ? $names[0] : implode( ', ', $names ) . ' and ' . $last; } return array( '{noun}' => $noun, '{slug}' => $slug, '{plugins}' => '' !== $plugins ? $plugins : 'Some of your plugins', ); } }