Last active
April 28, 2021 19:25
-
-
Save Alimir/4987ad6f72138e1ffc75397bb196c471 to your computer and use it in GitHub Desktop.
Migrate counter values from LikeBtn to WP ULike
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 | |
add_action( 'admin_action_migrate_likebtn_to_wp_ulike', 'migrate_likebtn_to_wp_ulike_admin_action' ); | |
function migrate_likebtn_to_wp_ulike_admin_action(){ | |
global $wpdb; | |
// Do your stuff here | |
if( empty( $_POST['posts'] ) && empty( $_POST['comments'] ) ){ | |
wp_die( 'Please select an option!' ); | |
} | |
if( $_POST['posts'] ){ | |
$posts = $wpdb->get_results( "SELECT post_id FROM `$wpdb->postmeta` WHERE `meta_key` IN ('Likes', 'Dislikes') GROUP BY post_id" ); | |
foreach ( $posts as $post_key => $post_value ) { | |
$likes = get_post_meta( $post_value->post_id, 'Likes', true ); | |
$dislikes = get_post_meta( $post_value->post_id, 'Dislikes', true ); | |
$is_distinct = wp_ulike_setting_repo::isDistinct('post'); | |
if( $likes ){ | |
wp_ulike_update_meta_counter_value( $post_value->post_id, $likes, 'post', 'like', $is_distinct ); | |
} | |
if( $dislikes ){ | |
wp_ulike_update_meta_counter_value( $post_value->post_id, $dislikes, 'post', 'dislike', $is_distinct ); | |
} | |
} | |
} | |
if( $_POST['comments'] ){ | |
$comments = $wpdb->get_results( "SELECT comment_id FROM `$wpdb->commentmeta` WHERE `meta_key` IN ('Likes', 'Dislikes') GROUP BY comment_id" ); | |
foreach ( $comments as $comment_key => $comment_value ) { | |
$likes = get_comment_meta( $comment_value->comment_id, 'Likes', true ); | |
$dislikes = get_comment_meta( $comment_value->comment_id, 'Dislikes', true ); | |
$is_distinct = wp_ulike_setting_repo::isDistinct('post'); | |
if( $likes ){ | |
wp_ulike_update_meta_counter_value( $comment_value->comment_id, $likes, 'comment', 'like', $is_distinct ); | |
} | |
if( $dislikes ){ | |
wp_ulike_update_meta_counter_value( $comment_value->comment_id, $dislikes, 'comment', 'dislike', $is_distinct ); | |
} | |
} | |
} | |
wp_redirect( $_SERVER['HTTP_REFERER'] ); | |
exit(); | |
} | |
add_action( 'admin_menu', 'migrate_likebtn_to_wp_ulike_admin_menu' ); | |
function migrate_likebtn_to_wp_ulike_admin_menu(){ | |
add_management_page( 'Migrate LikeBtn to WP ULike', 'Migrate LikeBtn to WP ULike', 'administrator', 'migrate_likebtn_to_wp_ulike', 'migrate_likebtn_to_wp_ulike_do_page' ); | |
} | |
function migrate_likebtn_to_wp_ulike_do_page(){ | |
?> | |
<div class="wrap"> | |
<h1>Migrate LikeBtn to WP ULike</h1><br> | |
<form method="POST" action="<?php echo admin_url( 'admin.php' ); ?>"> | |
<label> | |
Posts | |
<input type="checkbox" name="posts" /> | |
</label><br> | |
<label> | |
Comments | |
<input type="checkbox" name="comments" /> | |
</label><br><br> | |
<input type="hidden" name="action" value="migrate_likebtn_to_wp_ulike" /> | |
<input class="button" type="submit" value="Do it!" /> | |
</form> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment