Advertisement
rejuancse

note

Apr 25th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. /* --------------------------------------
  2. * 12. Love It Button
  3. * -------------------------------------- */
  4. $('.thm-love-btn').on('click', function(e) {
  5. e.preventDefault();
  6. var that = $(this);
  7. var campaign_id = that.data('campaign');
  8. var user_id = that.data('user');
  9.  
  10. if( user_id != 0 && campaign_id ){
  11. $.ajax({
  12. type:"POST",
  13. url: ajax_objects.ajaxurl,
  14. data: {'action': 'thm_campaign_action', 'campaign_id': campaign_id},
  15. success:function(data){
  16. data = JSON.parse(data);
  17. if (data.success == 1){
  18. that.find('.amount').html(data.number);
  19. if( data.message == 'love' ){
  20. that.addClass( 'active' ).parents('.themeum-campaign-post').find('.themeum-campaign-img').addClass('active');
  21. }else{
  22. that.removeClass( 'active' ).parents('.themeum-campaign-post').find('.themeum-campaign-img').removeClass('active');
  23. }
  24. }
  25. }
  26. });
  27. }else{
  28. $('#myModal').modal('show');
  29. }
  30. });
  31.  
  32.  
  33.  
  34. ========================================
  35.  
  36. <a href="#" class="thm-love-btn <?php echo esc_attr($active); ?>" data-campaign="<?php echo get_the_ID(); ?>" data-user="<?php echo get_current_user_id(); ?>">
  37. <i class="fa fa-heart-o"></i>
  38. </a>
  39.  
  40. ====================================
  41.  
  42.  
  43.  
  44.  
  45.  
  46. <!-- <i class="back-heart"></i> -->
  47. <span class="thm-Price-amount text-center">
  48. <span class="thm-love-btn" data-campaign="<?php echo get_the_ID(); ?>" data-user="<?php echo get_current_user_id(); ?>">
  49. <i class="back-heart"></i>
  50. <span class="woocommerce-Price-amount amount latest-price">
  51. <?php
  52. $love_count = get_post_meta( get_the_ID(),'loved_campaign_ids', true );
  53. if( $love_count ){
  54. echo esc_attr($love_count);
  55. }else{
  56. echo '0';
  57. }
  58. ?>
  59. </span>
  60. <span class="thm-raise-sp"><?php _e('Love it', 'backnow') ?></span>
  61. </span>
  62. </span>
  63.  
  64.  
  65. =========================================
  66.  
  67. function.php
  68.  
  69. /* -------------------------------------------
  70. * Love it Action
  71. * ------------------------------------------- */
  72. add_action( 'wp_ajax_thm_campaign_action','themeum_campaign_action' );
  73. add_action( 'wp_ajax_nopriv_thm_campaign_action', 'themeum_campaign_action' );
  74. function themeum_campaign_action(){
  75. if ( ! is_user_logged_in()){
  76. die(json_encode(array('success'=> 0, 'message' => __('Please Sign In first', 'backnow') )));
  77. }
  78.  
  79. $loved_campaign_ids = array();
  80. $user_id = get_current_user_id();
  81. $campaign_id = sanitize_text_field($_POST['campaign_id']);
  82. $prev_campaign_ids = get_user_meta($user_id, 'loved_campaign_ids', true);
  83. $postid = get_post_meta( $campaign_id, 'loved_campaign_ids', true );
  84.  
  85. if ($prev_campaign_ids){
  86. $loved_campaign_ids = json_decode( $prev_campaign_ids, true );
  87. }
  88.  
  89. if (in_array($campaign_id, $loved_campaign_ids)){
  90. if(($key = array_search($campaign_id, $loved_campaign_ids)) !== false) {
  91. unset( $loved_campaign_ids[$key] );
  92. }
  93. $json_update_campaign_ids = json_encode($loved_campaign_ids);
  94. update_user_meta($user_id, 'loved_campaign_ids', $json_update_campaign_ids);
  95. if( $postid ){
  96. $postid = (int)$postid - 1;
  97. update_post_meta( $campaign_id, 'loved_campaign_ids', $postid );
  98. }else{
  99. $postid = 0;
  100. update_post_meta( $campaign_id, 'loved_campaign_ids', 0 );
  101. }
  102. die(json_encode(array('success'=> 1, 'number' => $postid, 'message' => 'delete' )));
  103. }else{
  104. $loved_campaign_ids[] = $campaign_id;
  105. update_user_meta($user_id, 'loved_campaign_ids', json_encode($loved_campaign_ids) );
  106. if( $postid ){
  107. $postid = (int)$postid + 1;
  108. update_post_meta( $campaign_id, 'loved_campaign_ids', $postid );
  109. }else{
  110. $postid = 1;
  111. update_post_meta( $campaign_id, 'loved_campaign_ids', 1 );
  112. }
  113. die(json_encode(array('success'=> 1, 'number' => $postid , 'message' => 'love' )));
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement