Skip to content

Instantly share code, notes, and snippets.

@groupewibi
Last active January 11, 2023 01:03
Show Gist options
  • Save groupewibi/98399391b18dc88b693192a7fb8ec028 to your computer and use it in GitHub Desktop.
Save groupewibi/98399391b18dc88b693192a7fb8ec028 to your computer and use it in GitHub Desktop.
Remove Visual Composer Shortcake from content in Wordpress
<?php
/**
* Afficher les statistiques des shortcodes dans le contenu
* (?) Permettre de pouvoir passer dans une variable les Shortcode à supprimer.
* $post_id : string
* $mycontent : String
* (? $shortcodes : Array
*/
function afficher_stats_shortcode($mycontent, $simulation){
// (?) Mettre en place une boucle permettant de lister des shortcode dans un array pour permettre de lister tous les shortcodes
echo 'SIMULATION ? '.$simulation;
echo '<div class="alert alert-warning" role="alert">';
if( $simulation == 1) {echo '<span class="badge text-bg-danger">[SIMULATION]</span>'};
echo '<span class="badge text-bg-danger">'.substr_count( $mycontent, 'vc_row' ).'</span> <span class="text-danger">vc_row </span><span class="badge text-bg-danger">'.substr_count( $mycontent, 'vc_column' ).'</span> <span class="text-danger">vc_column </span><span class="badge text-bg-danger">'.substr_count( $mycontent, 'vc_column_text' ).'</span> <span class="text-danger">vc_column_text </span><span class="badge text-bg-danger">'.substr_count( $mycontent, 'vc_masonry_grid' ).'</span> <span class="text-danger">vc_masonry_grid </span><span class="badge text-bg-danger">'.substr_count( $mycontent, 'vc_single_image' ).'</span> <span class="text-danger">vc_single_image </span><span class="badge text-bg-danger">'.substr_count( $mycontent, 'vc_masonry_media_grid' ).'</span> <span class="text-danger">vc_masonry_media_grid </span></div>';
}
/**
* Supprimer les shortccode contenu dans du contenu Wordpress
* (?) Permettre de pouvoir passer dans une variable les Shortcode à supprimer.
* $post_id : string
* $mycontent : String
* (? $shortcodes : Array
*/
function remove_the_shortcode( $post_id, $mycontent, $simulation) {
afficher_stats_shortcode($mycontent, $simulation);
// Rechercher les occurences
$mycontent = preg_replace( array(
'/\[vc_row.*?\]/',
'/\[\/vc_row\]/',
'/\[vc_column_text.*?\]/',
'/\[\/vc_column_text\]/',
'/\[vc_column.*?\]/',
'/\[\/vc_column\]/',
),
'', $mycontent);
// Mettre à jour le post
$updated_post = array(
'ID' => $post_id,
'post_content' => $mycontent
);
echo '<span class="badge text-bg-danger">';
// Gestion du test
if( $simulation != 1) {
// wp_update_post( $updated_post(, true ));
// Gestion des erreurs
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
} else {
echo 'Contenu Sauvegardé';
}
};
echo '</span>';
afficher_stats_shortcode($mycontent, $simulation);
return $mycontent;
}
/**
* Suppression du shortchode vc_single_image du contenu.
* Shortcode : vc_single_image
* *mycontent : String
* $simulation) : true/false
* (?) Update le Post
*/
function transform_shortcode_vc_single_image( $post_id, $mycontent, $simulation) {
afficher_stats_shortcode($mycontent, $simulation);
preg_match_all('/\[vc_single_image.*?image="(.*?)" img_size="(.*?)".*?\]/', $mycontent, $matches, PREG_SET_ORDER);
if ($matches > 0) {
$i = 0;
foreach ($matches as $match){
//Défiunition des valeurs
$stringtoreplace = $match[0];
$image_id = $match[1];
$image_size = $match[2];
$image_data = wp_get_attachment_metadata($image_id);
$image_caption = wp_get_attachment_caption($image_id);
$image_url = wp_get_attachment_url( $image_id );
$image_width = $image_data['width'];
$image_height = $image_data['height'];
//Remplacement du tag
$replacement = '<!-- wp:image {"id":' .$image_id. ',"width":'. $image_width .',"height":' . $image_height . ',"sizeSlug":"'.$image_size.'","linkDestination":"none","style":{"color":{}}} -->';
$replacement .= '<figure class="wp-block-image size-full is-resized">';
$replacement .= '<img src="'.$image_url.'" alt="'.$image_caption.'" class="wp-image-'.$image_id.'" width="'.$image_width.'" height="'.$image_height.'"/>';
$replacement .= '<figcaption class="wp-element-caption">'.$image_caption.'</figcaption>';
$replacement .= '</figure><!-- /wp:image -->';
$mycontent = str_replace($stringtoreplace, $replacement, $mycontent);
// Mettre à jour le post
$updated_post = array(
'ID' => $post_id,
'post_content' => $mycontent
);
echo '<span class="badge text-bg-danger">';
// Gestion du test
if( $simulation != 1) {
// wp_update_post( $updated_post(, true ));
// Gestion des erreurs
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
} else {
echo 'Contenu Sauvegardé';
};
};
echo '</span>';
} //End Foreach
afficher_stats_shortcode($mycontent, $simulation);
return $mycontent;
}; //End Match
}; //End Function
/**
* Supprimer les shortccode vc_masonry_media_grid du contenu Wordpress
* (?) Permettre de pouvoir passer dans une variable les Shortcode à supprimer.
* Shortcode : vc_masonry_media_grid
* $post_id : string
* $mycontent : String
* $simulation : Bol
* (? $shortcodes : Array
*/
function transform_shortcode_vc_masonry_media_grid( $post_id, $mycontent, $simulation) {
// Tranforler le shortcode vc_masonry_media_grid en gallerie image dans le contenu.
$preg = preg_match_all('/\[{vc_masonry_media_grid}.*?include="(.*?)".*?\]/', $mycontent, $matches, PREG_SET_ORDER);
afficher_stats_shortcode ($mycontent, $simulation);
if ($matches > 0) {
foreach ($matches as $match){
// Pour chaque shortcode, je défini la valeur de remplacement
$replacement = '<!-- wp:gallery {"linkTo":"none"} --><figure class="wp-block-gallery has-nested-images columns-default is-cropped">';
$stringtoreplace = $match[0];
$imageIDs = explode(',',$match[1]);
foreach ($imageIDs as $image_id) {
// Je récupère les meta de Image pour chaque ID trouvé
$image_url = wp_get_attachment_url( $image_id );
$image_width = $image_data['width'];
$image_height = $image_data['height'];
$image_caption = wp_get_attachment_caption($image_id);
$replacement .= '<!-- wp:image {"id":' .$image_id. ',"width":'. $image_width .',"height":' . $image_height . ',"sizeSlug":"'.$image_size.'","linkDestination":"none","style":{"color":{}}} -->';
$replacement .= '<figure class="wp-block-image size-full is-resized">';
$replacement .= '<img src="'.$image_url.'"alt="{Ceci est le texte alternatif}" class="wp-image-'.$image_id.'" width="'.$image_width.'" height="'.$image_height.'"/>';
$replacement .= '<figcaption class="wp-element-caption">'.$image_caption.'</figcaption>';
$replacement .= '</figure><!-- /wp:image -->';
}
$replacement .= '</figure><!-- /wp:gallery -->';
$mycontent = str_replace($stringtoreplace, $replacement, $mycontent);
}
// Mettre à jour le post
$updated_post = array(
'ID' => $post_id,
'post_content' => $mycontent
);
echo '<span class="badge text-bg-danger">';
// Gestion du test
if( $simulation != 1) {
// wp_update_post( $updated_post(, true ));
// Gestion des erreurs
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
} else {
echo 'Contenu Sauvegardé';
}
};
echo '</span>';
return $mycontent;
echo '<h6>Afficher les Statistiques</h6>';
afficher_stats_shortcode($mycontent, $simulation);
} else {
echo '<span class="badge text-bg-danger">Aucune correspondance trouvée</span>';
} //Fin If Matches
} ; // FIN function transform_shortcode_vc_masonry_media_grid()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment