palsushobhan

wcfm-group-duplicate-button

Jan 23rd, 2022 (edited)
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.77 KB | None | 0 0
  1. add_action('wcfm_vendor_groups_actions', function($actions, $wcfm_groups_single) {
  2.     $actions .= '<a class="wcfm_group_duplicate wcfm-action-icon" href="#" data-groupid="' . $wcfm_groups_single->ID . '"><span class="wcfmfa fa-copy text_tip" data-tip="' . esc_attr__( 'Duplicate', 'wc-frontend-manager' ) . '"></span></a>';
  3.     return $actions;
  4. }, 10, 2);
  5.  
  6. add_action('after_wcfm_groups', function() {
  7.     ?>
  8.     <script>
  9.         jQuery(function($) {
  10.             $('#wcfm-groups').on('click', '.wcfm_group_duplicate', function(){
  11.                 $('#wcfm-groups_wrapper').block({
  12.                     message: null,
  13.                     overlayCSS: {
  14.                         background: '#fff',
  15.                         opacity: 0.6
  16.                     }
  17.                 });
  18.                 var data = {
  19.                     action : 'wcfmgs_duplicate_group',
  20.                     groupid : $(this).data('groupid'),
  21.                     wcfm_ajax_nonce: wcfm_params.wcfm_ajax_nonce
  22.                 }  
  23.                 $.ajax({
  24.                     type:       'POST',
  25.                     url: wcfm_params.ajax_url,
  26.                     data: data,
  27.                     success:    function(response) {
  28.                         if(response) {
  29.                             $response_json = $.parseJSON(response);
  30.                             if($response_json.status) {
  31.                                 if( $response_json.redirect ) window.location = $response_json.redirect;   
  32.                             }
  33.                         }
  34.                     }
  35.                 });
  36.                 $('#wcfm-groups_wrapper').unblock();
  37.                 return false;
  38.             });
  39.         });
  40.     </script>
  41.     <?php
  42. });
  43. add_action('wp_ajax_wcfmgs_duplicate_group', function() {
  44.     global $wpdb;
  45.     if ( ! check_ajax_referer( 'wcfm_ajax_nonce', 'wcfm_ajax_nonce', false ) ) {
  46.         wp_send_json_error( __( 'Invalid nonce! Refresh your page and try again.', 'wc-frontend-manager' ) );
  47.         wp_die();
  48.     }
  49.     if ( empty( $_POST['groupid'] ) ) {
  50.         echo '{"status": false, "message": "' .  __( 'No group to copy' ) . '"}';
  51.     }
  52.     $group_id = isset( $_POST['groupid'] ) ? absint( $_POST['groupid'] ) : '';
  53.     $post = get_post( $group_id );
  54.    
  55.     if (isset( $post ) && $post != null) {
  56.         $args = array(
  57.             'comment_status' => $post->comment_status,
  58.             'ping_status'    => $post->ping_status,
  59.             'post_author'    => $post->post_author,
  60.             'post_content'   => $post->post_content,
  61.             'post_excerpt'   => $post->post_excerpt,
  62.             'post_name'      => $post->post_name,
  63.             'post_parent'    => $post->post_parent,
  64.             'post_password'  => $post->post_password,
  65.             'post_status'    => 'draft',
  66.             'post_title'     => $post->post_title . " (Copy)",
  67.             'post_type'      => $post->post_type,
  68.             'to_ping'        => $post->to_ping,
  69.             'menu_order'     => $post->menu_order
  70.         );
  71.  
  72.         $new_post_id = wp_insert_post( $args );
  73.    
  74.         $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$group_id");
  75.         if (count($post_meta_infos)!=0) {
  76.             $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
  77.             foreach ($post_meta_infos as $meta_info) {
  78.                 $meta_key = $meta_info->meta_key;
  79.                 if( $meta_key == '_wp_old_slug' ) continue;
  80.                 $meta_value = addslashes($meta_info->meta_value);
  81.                 $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
  82.             }
  83.             $sql_query.= implode(" UNION ALL ", $sql_query_sel);
  84.             $wpdb->query($sql_query);
  85.         }
  86.         echo '{"status": true, "redirect": "' . get_wcfm_groups_manage_url( $new_post_id ) . '", "id": "' . $new_post_id . '"}';
  87.     }
  88.     die;
  89. });
Add Comment
Please, Sign In to add comment