Help_Command::parse_reference_links
Parse reference links from longdescription.
Method of the class: Help_Command{}
No Hooks.
Returns
String. The longdescription which has links as footnote.
Usage
$result = Help_Command::parse_reference_links( $longdesc );
- $longdesc(string) (required)
- The longdescription from the $command->get_longdesc().
Help_Command::parse_reference_links() Help Command::parse reference links code WP-CLI 2.13.0-alpha
private static function parse_reference_links( $longdesc ) {
$description = '';
foreach ( explode( "\n", $longdesc ) as $line ) {
if ( 0 === strpos( $line, '#' ) ) {
break;
}
$description .= $line . "\n";
}
// Fires if it has description text at the head of `$longdesc`.
if ( $description ) {
$links = []; // An array of URLs from the description.
$pattern = '/\[.+?\]\((https?:\/\/.+?)\)/';
$newdesc = preg_replace_callback(
$pattern,
function ( $matches ) use ( &$links ) {
static $count = 0;
$count++;
$links[] = $matches[1];
return str_replace( '(' . $matches[1] . ')', '[' . $count . ']', $matches[0] );
},
$description
);
$footnote = '';
$link_count = count( $links );
for ( $i = 0; $i < $link_count; $i++ ) {
$n = $i + 1;
$footnote .= '[' . $n . '] ' . $links[ $i ] . "\n";
}
if ( $footnote ) {
$newdesc = trim( $newdesc ) . "\n\n---\n" . $footnote;
$longdesc = str_replace( trim( $description ), trim( $newdesc ), $longdesc );
}
}
return $longdesc;
}