Advertisement
imath

add a checkin button to blog posts

Aug 13th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.25 KB | None | 0 0
  1. <?php
  2. /* sorry for this quick and dirty way */
  3. add_filter('the_content', 'imath_test_checkin_for_post', 10, 1);
  4.  
  5. function imath_test_checkin_for_post( $content ) {
  6.     if( is_single() ) {
  7.        
  8.         $post_address = get_post_meta( get_the_ID(), 'bpci_places_address', true );
  9.        
  10.         if( empty( $post_address ) )
  11.             return $content;
  12.            
  13.         $lat = get_post_meta( get_the_ID(), 'bpci_places_lat', true );;
  14.         $lng = get_post_meta( get_the_ID(), 'bpci_places_lng', true );;
  15.        
  16.         return $content . '<div style="clear:both" id="checkin-btn">
  17.                             <a href="#checkin">Checkin</a>
  18.                             <input type="hidden" id="lat" value="'.$lat.'">
  19.                             <input type="hidden" id="lng" value="'.$lng.'">
  20.                             <input type="hidden" id="address" value="'.$post_address.'">
  21.                             <input type="hidden" id="ci_post_id" value="'.get_the_ID().'">
  22.                            </div>';
  23.     }
  24.     else
  25.         return $content;
  26. }
  27.  
  28. add_action('wp_footer', 'imath_add_js_to_footer');
  29.  
  30. function imath_add_js_to_footer(){
  31.     if( is_single() ) {
  32.         ?>
  33.         <script>
  34.         jQuery(document).ready(function($){
  35.             $("#checkin-btn a").click(function(){
  36.                
  37.                 if( !$('#lat').val() || !$('#lng').val() || !$('#address').val() )
  38.                     return false;
  39.                
  40.                 $.post( ajaxurl, {
  41.                     action: 'imath_regular_post_checkin',
  42.                     'bpci-lat': $('#lat').val(),
  43.                     'bpci-lng': $('#lng').val(),
  44.                     'bpci-address': $('#address').val(),
  45.                     'post_id': $('#ci_post_id').val()
  46.                 },
  47.                 function(response)
  48.                 {
  49.                     if( response == "checkedin") {
  50.                         $('#checkin-btn').html('checked in!');
  51.                     }
  52.                     else {
  53.                         alert(response);
  54.                     }
  55.                 });
  56.                
  57.                 return false
  58.             });
  59.         });
  60.         </script>
  61.         <?php
  62.     }
  63. }
  64.  
  65. function imath_save_activity_checkin(){
  66.     global $blog_id;
  67.    
  68.     //user stuff
  69.     $user_id = bp_loggedin_user_id();
  70.     $from_user_link   = bp_core_get_userlink( $user_id );
  71.    
  72.     //post stuff
  73.     $post_id = intval( $_POST['post_id'] );
  74.     $title = get_the_title( $post_id );
  75.     $postlink = get_permalink( $post_id );
  76.    
  77.     $activity_link = '<a href="' . $postlink .'" title="' . $title . '">' . $title . '</a>';
  78.     $activity_action  = sprintf( __( '%s checked-in %s'), $from_user_link, $activity_link );
  79.     $primary_link     = bp_core_get_userlink( $user_id, false, true );
  80.     $activity_content = false;
  81.    
  82.     $activity_id = bp_activity_add( array(
  83.         'user_id'           => $user_id,
  84.         'action'            => apply_filters( 'bp_activity_new_update_action', $activity_action ),
  85.         'content'           => apply_filters( 'bp_activity_new_update_content', $activity_content ),
  86.         'primary_link'      => apply_filters( 'bp_activity_new_update_primary_link', $primary_link ),
  87.         'component'         => 'activity', /* replace with your custom component */
  88.         'type'              => 'activity_update', /* replace with your custom type */
  89.         'item_id'           => $blog_id,
  90.         'secondary_item_id' => $post_id
  91.     ) );
  92.    
  93.     // it's important to add this hook so that the geo data will be saved as activity meta...
  94.     do_action( 'bp_activity_posted_update', $activity_content, $user_id, $activity_id );
  95.    
  96.     if( $activity_id )
  97.         echo 'checkedin';
  98.        
  99.     else
  100.         echo 'OOps caramba !';
  101.    
  102.     die();
  103. }
  104.  
  105. add_action( 'wp_ajax_imath_regular_post_checkin', 'imath_save_activity_checkin' );
  106. add_action( 'wp_ajax_nopriv_imath_regular_post_checkin', 'imath_save_activity_checkin' );
  107. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement