*/ private static $notes = array(); /** * Running group number, one per content with footnotes. * * @var int */ private static $group = 0; /** * Hooks: the list is added after do_shortcode, before the table of contents (12) reads the headings. * Same priority as do_shortcode (11); core adds that one first, so it runs before ours. */ public static function init(): void { add_filter( 'the_content', array( __CLASS__, 'start' ), 1 ); add_filter( 'the_content', array( __CLASS__, 'append_list' ), 11 ); } /** * Every content starts its own numbering. * * @param mixed $content Post content. * @return mixed */ public static function start( $content ) { self::$notes = array(); return $content; } /** * [threece_fn]: the raised number linking to its entry. * * @param string $content Footnote text. * @param string $classes Extra CSS classes. * @return string */ public static function reference( $content, $classes ) { $text = trim( (string) preg_replace( '#?p>#', ' ', do_shortcode( (string) $content ) ) ); if ( '' === trim( wp_strip_all_tags( $text ) ) ) { return ''; } if ( empty( self::$notes ) ) { ++self::$group; } self::$notes[] = $text; $number = count( self::$notes ); wp_enqueue_style( 'threece-elements' ); return sprintf( '%5$d', esc_attr( trim( 'threece-fn ' . $classes ) ), esc_attr( self::id( 'threece-fn', $number ) ), esc_attr( self::id( 'threece-fnref', $number ) ), /* translators: %d: footnote number */ esc_attr( sprintf( __( 'Footnote %d', '3task-toolbar-for-classic-editor' ), $number ) ), $number ); } /** * Adds the list of the collected footnotes to the end of the content. * * @param mixed $content Post content after do_shortcode. * @return mixed */ public static function append_list( $content ) { if ( ! is_string( $content ) || empty( self::$notes ) ) { return $content; } $items = ''; foreach ( self::$notes as $index => $text ) { $number = $index + 1; $items .= sprintf( '