diff --git a/h5p-core-fonts-fix.php b/h5p-core-fonts-fix.php index fac767c..152ac28 100644 --- a/h5p-core-fonts-fix.php +++ b/h5p-core-fonts-fix.php @@ -1,45 +1,170 @@ null, 'styles' => null ]; + + if ( $source ) { + if ( preg_match( + '/public\s+static\s+\$coreApi\s*=\s*(?:array\s*\(|\[)\s*' + . "'majorVersion'\s*=>\s*(\d+)\s*,\s*'minorVersion'\s*=>\s*(\d+)/", + $source, + $m + ) ) { + $values['coreApi'] = [ + 'majorVersion' => (int) $m[1], + 'minorVersion' => (int) $m[2], + ]; + } + + if ( preg_match( '/public\s+static\s+\$styles\s*=\s*(?:array\s*\(|\[)(.*?)(?:\)|\])\s*;/s', $source, $m ) + && preg_match_all( "/'([^']+\.css)'/", $m[1], $found ) + ) { + $values['styles'] = $found[1]; + } + } + + set_site_transient( + 'pb_h5p_shipped_core_values', + [ 'mtime' => $mtime, 'values' => $values ], + DAY_IN_SECONDS + ); + + return $values; +} + add_action( 'plugins_loaded', function () { if ( ! class_exists( 'H5PCore' ) ) { return; } - // add_core_assets() resolves these against plugins_url('h5p/h5p-php-library'). - $library_dir = WP_PLUGIN_DIR . '/h5p/h5p-php-library/'; + // add_core_assets() resolves style paths against plugins_url('h5p/h5p-php-library'). + $library_dir = WP_PLUGIN_DIR . '/h5p/h5p-php-library/'; + $classes_file = $library_dir . 'h5p.classes.php'; - $missing = []; - foreach ( [ 'styles/h5p-fonts.css' ] as $style ) { - if ( ! in_array( $style, H5PCore::$styles, true ) && file_exists( $library_dir . $style ) ) { - $missing[] = $style; + if ( ! is_readable( $classes_file ) ) { + return; + } + + $shipped = pb_h5p_shipped_core_values( $classes_file ); + $loaded = H5PCore::$styles; + + // 1. Adopt the shipped stylesheet list, which preserves upstream's ordering — the + // theme layer has to load after h5p.css to override it. Only files actually + // present are included, and anything the loaded list has that the shipped one + // doesn't is kept, so this can only ever add. + if ( $shipped['styles'] ) { + $styles = array_values( array_filter( + $shipped['styles'], + function ( $style ) use ( $library_dir ) { + return file_exists( $library_dir . $style ); + } + ) ); + H5PCore::$styles = array_merge( $styles, array_values( array_diff( $loaded, $styles ) ) ); + } else { + // Fallback: prepend whichever known-missing sheets are present. + $missing = array_values( array_filter( + PB_H5P_RESTORE_STYLES, + function ( $style ) use ( $library_dir, $loaded ) { + return ! in_array( $style, $loaded, true ) && file_exists( $library_dir . $style ); + } + ) ); + if ( $missing ) { + H5PCore::$styles = array_merge( $missing, $loaded ); } } - if ( $missing ) { - // Prepend, mirroring upstream ordering. - H5PCore::$styles = array_merge( $missing, H5PCore::$styles ); + // 2. Report the coreApi the shipped library actually implements. Only ever raised, + // so a future vendor copy that overtakes the H5P plugin is left untouched. + if ( ! $shipped['coreApi'] ) { + return; + } + + $current = H5PCore::$coreApi; + $is_newer = $shipped['coreApi']['majorVersion'] > $current['majorVersion'] + || ( $shipped['coreApi']['majorVersion'] === $current['majorVersion'] + && $shipped['coreApi']['minorVersion'] > $current['minorVersion'] ); + + if ( $is_newer ) { + H5PCore::$coreApi = $shipped['coreApi']; } }, 100 );