Created
January 8, 2020 21:37
-
-
Save sybrew/fc9460d5ea8346a883176108c6e70381 to your computer and use it in GitHub Desktop.
Override content image generator to strip SVG images in TSF.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Don't include the PHP tag is PHP is already active | |
add_filter( 'the_seo_framework_image_generation_params', function( $args ) { | |
if ( isset( $args['cbs']['content'] ) ) { | |
$args['cbs']['content'] = 'my_content_image_generator'; | |
} | |
return $args; | |
}); | |
function my_content_image_generator( $args = null, $size = 'full' ) { | |
$tsf = \the_seo_framework(); | |
if ( null === $args ) { | |
if ( $tsf->is_singular() ) { | |
$content = $tsf->get_post_content(); | |
} | |
} else { | |
if ( $args['taxonomy'] ) { | |
$content = ''; | |
} else { | |
$content = $tsf->get_post_content( $args['id'] ); | |
} | |
} | |
$matches = []; | |
// strlen( '<img src=a>' ) === 11; yes, that's a valid self-closing tag with a relative source. | |
if ( strlen( $content ) > 10 && false !== stripos( $content, '<img' ) ) { | |
preg_match_all( | |
'/<img[^>]+src=(\"|\')?([^\"\'>\s]+)\1?.*?>/mi', | |
$content, | |
$matches, | |
PREG_SET_ORDER | |
); | |
} | |
if ( $matches ) { | |
foreach ( $matches as $match ) { | |
// Skip SVG files... | |
if ( false !== strpos( pathinfo( $match[2], PATHINFO_EXTENSION ), 'svg' ) ) continue; | |
// Assume every URL to be correct? Yes. WordPress assumes that too. | |
yield [ | |
'url' => $match[2] ?: '', | |
'id' => 0, | |
]; | |
} | |
} else { | |
yield [ | |
'url' => '', | |
'id' => 0, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment