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