*/ 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( '##', ' ', 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( '
  • %2$s ↩︎
  • ', esc_attr( self::id( 'threece-fn', $number ) ), wp_kses_post( $text ), esc_attr( self::id( 'threece-fnref', $number ) ), /* translators: %d: footnote number */ esc_attr( sprintf( __( 'Back to reference %d', '3task-toolbar-for-classic-editor' ), $number ) ) ); } self::$notes = array(); return $content . "\n" . '
      ' . $items . '
    '; } /** * ID of a footnote or its reference. * * @param string $prefix threece-fn or threece-fnref. * @param int $number Footnote number. * @return string */ private static function id( $prefix, $number ) { return $prefix . '-' . self::$group . '-' . $number; } }