Skip to:
Content
Pages
Categories
Search
Top
Bottom

Forum Replies Created

Viewing 8 replies - 1 through 8 (of 8 total)
  • @chicho1969

    Participant

    thanks @danbp ,

    I just made test, but still the same. I put the first function (celbirthday_notifier_setup_globals ()) in bp-custom.php within plugins folder and still the same, the function does nothing, unless you called: celbirthday_notifier_setup_globals ();
    I believe that I am missing some basics to understand this.

    @chicho1969

    Participant

    okay, This is almost done, but is really rare, not everything works as it should.
    First of all, this is not a pluggin, I am writing all these code in bp-functions.php in my bududdypress folder within my theme folder. (this may be my problem).
    Step by step:
    1st Problem setup globals is not working as it espected:

    define( 'BP_NOTIFIER_CELBIRTHDAY_SLUG', 'celbirthday_notifier' );
    function celbirthday_notifier_setup_globals () {
    	global $bp, $wpdb;
    	$bp = buddypress();
    	
    	$bp->celbirthday_notifier = new stdClass();
    	$bp->celbirthday_notifier->id = 'celbirthday_notifier'; //I asume others are not going to use this is
    	$bp->celbirthday_notifier->slug = BP_NOTIFIER_CELBIRTHDAY_SLUG;
    	$bp->celbirthday_notifier->notification_callback = 'celbirthday_notifier_format_notifications'; //show the notification
    	
    	$bp->active_components[$bp->celbirthday_notifier->id] = $bp->celbirthday_notifier->id;
    
    	do_action( 'bp_setup_globals' );
    }

    This function is not doing nothing unless I call it directly:
    celbirthday_notifier_setup_globals ();
    I Know this is not the proper way, but it is the only way I found to make it run.
    Does anyone know why it does not work properly?

    Continue with the notification:

    //Format notifications
    function celbirthday_notifier_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     if ( 'new_birthday' === $action ) {
    	if ( (int) $total_items > 1 ) {
    	      
    	    if ( 'string' === $format ) {
    			$famoso_name =  get_the_title( $secondary_item_id );
    			return apply_filters( 'celebrities_multiple_verifications_notification','Say Happybirthday to '.$famoso_name);	  
    		  }
          } else {	 
          if ( 'string' === $format ) {  //falta definir $famoso_link y mas 
    	   $famoso_name =  get_the_title( $secondary_item_id );
    	  	return apply_filters( 'celebrities_single_verifications_notification','Say Happybirthday to '.$famoso_name);
             // $return = apply_filters( $filter, '<a href="' . esc_url( $famoso_link ) . '" title="birthday anounce">' . esc_html( $text ) . '</a>', (int) $total_items, $text, $famoso_link );
    		// $message = apply_filters( 'bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link );
    
    	} } }
     do_action( 'celbirthday_notifier_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); 
    return false; }

    ok, this format function is working right, although I have to do some more work. After it, I have two more functions (mark and delete notifications) wich are also working fine.

    //mark notification
    function celebrities_mark_screen_notifications() {
    	global $bp;
    	
    	bp_notifications_mark_notifications_by_item_id( $bp->loggedin_user->id, $bp->celbirthday_notifier->id, 'new_birthday', $secondary_item_id = false, $is_new='0');
    }
    add_action( 'bp_notifications_screen', 'celebrities_mark_screen_notifications' ); 

    and…

    function celbirthday_notifier_remove_screen_notifications() {
    	global $bp;
    	
    	bp_core_delete_notifications_for_user_by_type( $bp->loggedin_user->id, $bp->profile->slug, 'new_birthday' );
    }
    add_action( 'bp_notifications_screen', 'celbirthday_notifier_remove_screen_notifications' );
    add_action( 'xprofile_screen_display_profile', 'celbirthday_notifier_remove_screen_notifications' );
    

    As I say, this works fine, but a I am not sure if I need the second “add_action” (xprofile screen).
    And finally, to add notification I use this code (which is not working):

    //Adding notification
    function buddypress_celebrities_add_notification() 
    {    
    if ( bp_is_active( 'notifications' ) )
     {  
    
    	$fecha_act = date("Y-m-d");
    	$res_birth = $wpdb->get_results( "SELECT wp_best_favs.user_id, wp_postmeta.post_id
    					FROM wp_best_favs
    					LEFT JOIN wp_postmeta ON ( wp_postmeta.post_id = wp_best_favs.celeb_ID ) 
    					WHERE (
    					wp_postmeta.meta_key =  'fecha_na')
    					AND (DAY(  <code>wp_postmeta</code>.<code>meta_value</code> ) = DAY(  '$fecha_act' ))					
    					AND (MONTH(  <code>wp_postmeta</code>.<code>meta_value</code> ) = MONTH(  '$fecha_act' ))
    					");
    			$res_birth = array_filter($res_birth);  
    			$matriz = objectToArray($res_birth);  //I use this function to convert objet array to simple array
    			if(count($matriz)==0) 
    			{ 
    				// está vacío 
    			}  else { 
    				global $wpdb;
    				$bp = buddypress();
    				$current_time = bp_core_current_time();
    				foreach ($matriz as $v1) {
    					
    					$famoso_id = $v1[post_id];
    					$receiver_user_id = $v1[user_id];
    						
    				$args = array(
    					'user_id'           => $receiver_user_id,
    					'item_id'           => $famoso_id,
    					'secondary_item_id' => $famoso_id,
    					'component_name'    => $bp->celbirthday_notifier->slug,
    					'component_action'  => 'new_birthday',
    					'date_notified'     => $current_time,
    					'is_new'            => 1,
    	       				 );
    			 bp_notifications_add_notification( $args );
    		}
    	}
     } 
    } 
    do_action( 'bp_init', 'buddypress_celebrities_add_notification' ); 

    So this is my bigger problem:
    Add notifications (function buddypress_celebrities_add_notification()) is not working.
    I am pretty sure this is because I am not hooking properly, but I tried so many hooks and none of it did work.
    I Know the code is fine because if I comment the two first lines and the two last lines (so I run the code not as a function) the notifications are being added to database.

    Anyone can help me, to find a proper hook, or just pointing me in the right direction.
    Thanks in advance.

    @chicho1969

    Participant

    Thank you @danbp , will see, in the main time I keep trying and will update if I make progress.

    @chicho1969

    Participant

    Thanks @danbp ,
    I´ll try to explain you:

    I have a custom_post_type (celebrities). it real name is “famosos”.
    BuddyPressUsers can mark any celebrity as fasvourite.
    This part is working fine.
    So now, I am trying to send a notification (to users that have marked as favorite certain celebriry) that “today” is that celebrity´s birthday.

    For that purpose, I have a SQL query that works fine and gives me an array with budypress user id and the celebritie post id. (this is also working fine)

    SO I have to setup a custom notificationa that checks every day that SQL QUery to gets users and celebrities ids to notify user about today´s birthday (of their favourite celebrities)

    and this is what I did at first:

    define( 'BP_ACTIVITY_CELBIRTHDAY_SLUG', 'celbirthday_notifier' );
    function celbirthday_notifier_setup_globals () {
    	
    	$bp = buddypress();
    	
    	$bp->celbirthday_notifier = new stdClass();
    	$bp->celbirthday_notifier->id = 'celbirthday_notifier'; //I asume others are not going to use this is
    	$bp->celbirthday_notifier->slug = BP_ACTIVITY_CELBIRTHDAY_SLUG;
    	$bp->celbirthday_notifier->notification_callback = 'celbirthday_notifier_format_notifications'; //show the notification
    	
    	$bp->active_components[$bp->celbirthday_notifier->id] = $bp->celbirthday_notifier->id;
    
    	do_action( 'celbirthday_notifier_setup_globals' );
    }
    
    add_action( 'bp_setup_globals', 'celbirthday_notifier_setup_globals' );

    and then try to format the notification:

    function celbirthday_notifier_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     if ( 'new_birthday' === $action ) {
                    $famoso_name =  get_the_title( $secondary_item_id );
    		$subject = 'Celebrity birthday';
    		$message = 'Say happy birthday to $famoso_name';
    		$content = 'Tody is $famoso_name birthday .....somethin else ';
    		if ( (int) $total_items > 1 ) {
              $text = sprintf( __( 'You have %d celebrities birthday', 'bbpress' ), (int) $total_items );
              $filter = 'bp_activity_at_message_notification_message';
          } else {
              if ( !empty( $secondary_item_id ) ) {
                  //nada
              } else {
                  $text = sprintf( __( 'You have %d new birthday of %s', 'bbpress' ), (int) $total_items, $famoso_name );
              }
              $filter = 'bp_activity_at_message_notification_message';
          }
    	 }
          if ( 'string' === $format ) {  //falta definir $famoso_link y mas 
              $return = apply_filters( $filter, '<a href="' . esc_url( $famoso_link ) . '" title="birthday anounce">' . esc_html( $text ) . '</a>', (int) $total_items, $text, $famoso_link );
    		// $message = apply_filters( 'bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link );
    
    } } do_action( 'celbirthday_notifier_format_notifications', $action, $item_id, $secondary_item_id, $total_items );

    and then add notification this way:

    function buddypress_celebrities_add_notification( $fecha_act,  $famoso_name, $receiver_user_id) 
    {    if ( bp_is_active( 'notifications' ) ) { 
    
    	$fecha_act = date("Y-m-d");
    	$res_birth = $wpdb->get_results( "SELECT wp_best_favs.user_id, wp_postmeta.post_id
    					FROM wp_best_favs
    					LEFT JOIN wp_postmeta ON ( wp_postmeta.post_id = wp_best_favs.celeb_ID ) 
    					WHERE (
    					wp_postmeta.meta_key =  'fecha_na')
    					AND (DAY(  <code>wp_postmeta</code>.<code>meta_value</code> ) = DAY(  '$fecha_act' ))					AND (MONTH(  <code>wp_postmeta</code>.<code>meta_value</code> ) = MONTH(  '$fecha_act' )							)
    									") ;
    			$res_birth = array_filter($res_birth);  
    			$matriz = objectToArray($res_birth);  //convertimos el array de objetos en array normal para manejarlo mas facil
    			$current_time = bp_core_current_time();
    			if(count($matriz)==0) { 
    				// está vacío 
    				}  else { 
    					global $wpdb;
    					$bp = buddypress();
    					$subject = 'Celebrity birthday';
    					$message = '1 Say happy birthday to $famoso_name';
    					$content = '2 Say happy birthday to $famoso_name';
    					foreach ($matriz as $v1) {
    					
    						$famoso_id = $v1[post_id];
    						$famoso_name = get_the_title ($v1[post_id]);
    						$receiver_user_id = $v1[user_id];
    						
    					$args = array(
    							'user_id'           => $receiver_user_id,
    							'item_id'           => $famoso_id,
    							'secondary_item_id' => $famoso_id,
    							'component_name'    => $bp->celbirthday_notifier->id,
    							'component_action'  => 'new_birthday',
    							'date_notified'     => $current_time,
    							'is_new'            => 1,
    	       					 );
    							 
    							 bp_notifications_add_notification( $args );
    						 
    			}	}  }	} add_action( 'new_birthday', 'buddypress_celebrities_add_notification', 10, 1 );

    so far this is what I am trying to do, hope you understand.
    Thanks in advance.

    @chicho1969

    Participant

    Thanks for reply.

    Yes I read those articles (…and much more), but still not understanding, Sorry about it, thus I am asking for help.

    I don,t understand what do you mean with use a function name twice:
    (// do_action( ‘setup_globals’ ); This is commented) ,

    is this a better way to do it?:

    function sr_notifications_test_setup_globals () {	
    	global $bp, $current_blog;
    	$bp->sr_notifications_test = new stdClass();
    	$bp->sr_notifications_test->id = 'sr_notifications_test';
    	$bp->sr_notifications_test->slug = 'sr_notifications_test';
    	$bp->sr_notifications_test->notification_callback = 'sr_format_notifications_test'; 
    	$bp->active_components[$bp->sr_notifications_test->id] = $bp->sr_notifications_test->id;
    	do_action( 'sr_notifications_test_setup_globals ' );
    }
    add_action( 'bp_notification_settings', 'sr_notifications_test_setup_globals ' ); 
    

    Anyway, what I understood is that I have to write those 3 functions to make my custom notifications works:
    1.- setup_globals
    2.- format_notifications (called from set_up_globals, notification_callback)
    If Iunderstood you´, the filter I have to use is bp_notifications_get_notifications_for_user
    3.- _add_notification

    is this the right way?

    @chicho1969

    Participant

    forget it I misunderstood

    @chicho1969

    Participant

    You may try this pluging: BuddyPress Activity Plus, I am pretty sure that it doews what you are looking for. link; https://wordpress.org/plugins/buddypress-activity-plus/

    @chicho1969

    Participant

    Ok, now I am trying this:

    //function class
    
    function setup_globals( $args = array() ) {
    global $bp;
    $sr_notifications_test_slug = 'sr_notifications_test';
    parent::setup_globals( array(
    'id' => 'sr_notifications_test',
    'slug' => $sr_notifications_test_slug,
    'notification_callback' => array( $this, 'sr_format_notifications_test' )
    ) );
    /* Register this in the active components array */
    $bp->active_components[$sr_notifications_test_slug] = 'sr_notifications_test';
    // do_action( 'setup_globals' );
    }
    
    //format notofication
    function sr_format_notifications_test( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    
    switch ( $action ) {
    case 'my_test':
    $link = get_permalink( $item_id );
    $text = 'Test Notification';
    
    $return = apply_filters( $filter, array(
    'text' => $text,
    'link' => $link
    ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
    break;
    }
    
    do_action( 'sr_format_notifications_test', $action, $item_id, $secondary_item_id, $total_items );
    return $return;
    }
    
    //Notification added to DB:
    bp_notifications_add_notification( array(
    'user_id' => $user_id,
    'item_id' => $activity->id,
    'secondary_item_id' => $activity->user_id,
    'component_name' => buddypress()->activity->id,
    'component_action' => 'my_test',
    'date_notified' => bp_core_current_time(),
    'is_new' => 1
    
    ) );
    print_r ($bp->active_components[$sr_notifications_test_slug]);

    and this is the error I have: (still the same)

    
    WordPress database error: [Table 'WP_db_funs.n' doesn't exist]
    SELECT * FROM n WHERE component_name IN ('activity') AND component_action IN ('my_test') AND is_new = 1
    WordPress database error: [Incorrect table name ”]
    INSERT INTO (user_id,item_id,secondary_item_id,component_name,component_action,date_notified,is_new) VALUES (0,0,0,'activity','my_test','2015-08-12 15:57:21′,1)

    Please if anybody Knows about this I will apreciate any help 🙁

    Table ‘WP_db_funs.n’ doesn’t exist how come?? table n ??

Viewing 8 replies - 1 through 8 (of 8 total)
Skip to toolbar