Created
August 25, 2023 09:01
-
-
Save bantunesm/b9e33a422ea8038c549ab76af685ea3b to your computer and use it in GitHub Desktop.
WordPress Importer Hooks for decoding base64 and unserialize content
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 | |
/** | |
* WordPress Importer Hooks for decoding base64 and unserialize content | |
* @see https://medium.com/@bantunes/wordpress-comment-jai-importé-500-articles-encodés-en-base64-d-un-builder-à-gutenberg-29c3d9d9e694 | |
* @author Bruno ANTUNES | |
* @version 1.0.0 | |
*/ | |
class MfnImporter { | |
public function __construct() { | |
add_action( 'import_post_meta', array( $this, 'store_decoded_meta' ), 10, 3 ); | |
add_action( 'import_end', array( $this, 'process_imported_posts' ) ); | |
} | |
public function store_decoded_meta( $post_id, $key, $value ) { | |
if ( 'mfn-page-items' == $key ) { | |
$decoded_value = base64_decode( $value ); | |
$unserialized_value = unserialize( $decoded_value ); | |
if ( $unserialized_value !== false ) { | |
update_post_meta($post_id, '_tmp_mfn_content', $this->extract_column_content($unserialized_value)); | |
} | |
} | |
} | |
public function extract_column_content($data) { | |
$content = ''; | |
// Boucler à travers chaque "élément" | |
foreach ($data as $main_item) { | |
if (isset($main_item['wraps']) && is_array($main_item['wraps'])) { | |
// Boucler à travers chaque "wrap" | |
foreach ($main_item['wraps'] as $wrap) { | |
if (isset($wrap['items']) && is_array($wrap['items'])) { | |
// Boucler à travers chaque "item" | |
foreach ($wrap['items'] as $item) { | |
if (isset($item['type']) && $item['type'] === 'column' && isset($item['fields']['content'])) { | |
// Convertir le contenu en bloc Gutenberg de type paragraphe | |
$content .= '<!-- wp:paragraph -->'; | |
$content .= $item['fields']['content']; | |
$content .= '<!-- /wp:paragraph -->'; | |
} | |
} | |
} | |
} | |
} | |
} | |
return $content; | |
} | |
public function process_imported_posts() { | |
$args = array( | |
'post_type' => 'post', // type de post que vous importez | |
'meta_key' => '_tmp_mfn_content', | |
'posts_per_page' => -1 | |
); | |
$posts = get_posts($args); | |
foreach ($posts as $post) { | |
$mfn_content = get_post_meta($post->ID, '_tmp_mfn_content', true); | |
if ($mfn_content) { | |
$updated_content = $post->post_content . "\n\n" . $mfn_content; | |
wp_update_post(array('ID' => $post->ID, 'post_content' => $updated_content)); | |
delete_post_meta($post->ID, '_tmp_mfn_content'); | |
} | |
} | |
} | |
} | |
new MfnImporter(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment