Last active
April 22, 2019 03:20
-
-
Save bamadesigner/676debc0bb66082018c47a873c8b3a39 to your computer and use it in GitHub Desktop.
Custom functions used for WP All Import of articles
This file contains hidden or 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 | |
function article_get_header_regex( $level ) { | |
$level = (int) $level; | |
return '/^\<h' . $level . '[^\>]*\>(.*)\<\/?h' . $level . '\>/i'; | |
} | |
function article_get_end_of_url( $url ) { | |
// Remove trailing "/". | |
$last_chr = substr( $url, -1 ); | |
if ( '/' == $last_chr ) { | |
$url = substr( $url, 0, -1 ); | |
} | |
// Get last "part" of URL. | |
preg_match( '/([^\/]+)$/i', $url, $matches ); | |
if ( ! empty( $matches[1] ) ) { | |
return trim( $matches[1] ); | |
} | |
return null; | |
} | |
function article_get_subheading( $content ) { | |
// Remove title header. | |
$content = preg_replace( article_get_header_regex( 3 ), '', $content ); | |
// Get subheading matches | |
preg_match( article_get_header_regex( 4 ), $content, $matches ); | |
if ( ! empty( $matches[1] ) ) { | |
return trim( strip_tags( $matches[1] ) ); | |
} | |
return null; | |
} | |
function update_image_src( $content ) { | |
preg_match_all( '/\<img[^\>]+src\=[\"\']+(\/portals[^\'\"]+)[\"\']+[^\/\>]*\/?\>/i', $content, $all_matches ); | |
if ( empty( $all_matches[1] ) ) { | |
return $content; | |
} | |
$matches = $all_matches[1]; | |
// Trying to keep the work down on the import. | |
if ( count( $matches ) > 2 ) { | |
return $content; | |
} | |
$blog_url = ''; | |
// Build replacements. | |
$replacements = []; | |
foreach( $matches as $match ) { | |
$replacements[] = $blog_url . $match; | |
} | |
$content = str_replace( $matches, $replacements, $content ); | |
return $content; | |
} | |
function article_optimize_content( $content ) { | |
// Remove title header. | |
$content = preg_replace( article_get_header_regex( 3 ), '', $content ); | |
// Remove subheading. | |
$content = preg_replace( article_get_header_regex( 4 ), '', $content ); | |
// Update paths to images. | |
if ( false !== strpos( $content, '/portals/' ) ) { | |
$content = update_image_src( $content ); | |
} | |
return trim( $content ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment