| 1 |
<?php |
| 2 |
/** |
| 3 |
* Import helper class |
| 4 |
* |
| 5 |
* @package WPFunnels\Importer |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPFunnels\Importer; |
| 9 |
|
| 10 |
use WPFunnels\Importer\Image\Wpfnl_Image_Importer; |
| 11 |
use WPFunnels\Wpfnl; |
| 12 |
|
| 13 |
/** |
| 14 |
* Import helper class |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
class Wpfnl_Importer_Helper { |
| 19 |
|
| 20 |
/** |
| 21 |
* Class instance |
| 22 |
* |
| 23 |
* @var mixed |
| 24 |
*/ |
| 25 |
private static $instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* Get Instance class |
| 29 |
* |
| 30 |
* @return Wpfnl_Importer_Helper |
| 31 |
*/ |
| 32 |
public static function get_instance() { |
| 33 |
if ( ! isset( self::$instance ) ) { |
| 34 |
self::$instance = new self(); |
| 35 |
} |
| 36 |
|
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Get post contents |
| 43 |
* |
| 44 |
* @param int $step_id Funnel step id. |
| 45 |
* |
| 46 |
* @return string|string[] |
| 47 |
* @since 1.0.0 |
| 48 |
*/ |
| 49 |
public function get_post_contents( $step_id ) { |
| 50 |
// get the post contents. |
| 51 |
$content = get_post_field( 'post_content', $step_id ); |
| 52 |
|
| 53 |
$content = stripslashes( $content ); |
| 54 |
|
| 55 |
// get all links from $content. |
| 56 |
$links = wp_extract_urls( $content ); |
| 57 |
|
| 58 |
if ( empty( $links ) ) { |
| 59 |
return $content; |
| 60 |
} |
| 61 |
|
| 62 |
$normal_links = array(); |
| 63 |
$image_links = array(); |
| 64 |
$mapping_array = array(); |
| 65 |
|
| 66 |
// Step 1: store image link and normal links. |
| 67 |
foreach ( $links as $key => $link ) { |
| 68 |
if ( preg_match( '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*\.(?:jpg|png|gif|jpeg)/i', $link ) ) { |
| 69 |
$image_links[] = $link; |
| 70 |
} else { |
| 71 |
$normal_links = $link; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// Step 2: save image to the site. |
| 76 |
if ( ! empty( $image_links ) ) { |
| 77 |
foreach ( $image_links as $key => $image_url ) { |
| 78 |
$image = array( |
| 79 |
'url' => $image_url, |
| 80 |
'id' => 0, |
| 81 |
); |
| 82 |
$saved_image = Wpfnl_Image_Importer::get_instance()->import( $image ); |
| 83 |
|
| 84 |
if ( $saved_image ) { |
| 85 |
$mapping_array[] = array( |
| 86 |
'old' => $image_url, |
| 87 |
'new' => $saved_image['url'], |
| 88 |
); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Step 3: replace image url with new one. |
| 94 |
foreach ( $mapping_array as $key => $mapping ) { |
| 95 |
$content = str_replace( $mapping['old'], $mapping['new'], $content ); |
| 96 |
} |
| 97 |
return $content; |
| 98 |
} |
| 99 |
} |
| 100 |
|