Last active
December 2, 2022 15:10
-
-
Save marlonassuncao/a0d4fbb5a4411f695a862a6463c8c46e to your computer and use it in GitHub Desktop.
ActivityPublishNotification.php
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 | |
/** | |
* Register New custom automatic push notification. | |
*/ | |
namespace BuddyBossApp\Custom; | |
use BuddyBossApp\Jobs; | |
use BuddyBossApp\Notification\IntegrationAbstract; | |
/** | |
* Class ActivityPublishNotification | |
* | |
* @package BuddyBossApp\Custom | |
*/ | |
class NewActivityPublishNotification extends IntegrationAbstract { | |
/** | |
* | |
*/ | |
public function load() { | |
$this->hooks(); | |
/** | |
* Register push group for notification. | |
* Within one group you can add multiple subscription types. | |
* | |
* @param string $name Group unique list | |
* @param String $label Group name. | |
* | |
*/ | |
$this->register_push_group( 'notification_custom_activity', __( "Novas atividades", "buddyboss-app" ) ); | |
/** | |
* Register push type for notification. | |
* | |
* @param string $type Register push type name | |
* @param string $admin_label Label for admin. This label will be visible on the Push notifications manage setting page for admin. | |
* @param string $user_label Label. This label will be visible on the Push Notifications manage tab for user in app. | |
*/ | |
$this->register_push_type( 'notification_custom_activity', __( "Nova atividade publicada.", "buddyboss-app" ), __( "Nova atividade publicada.", "buddyboss-app" ), array( 'push_group' => 'notification_custom_activity' ) ); | |
} | |
/** | |
* register hooks for sending notifications. | |
*/ | |
public function hooks() { | |
add_action( 'publish_activity', array( $this, 'send_publish_activity_notification' ), 999, 2 ); | |
add_action( 'bbapp_queue_task_publish_activity', array( $this, 'handle_publish_activity_job' ), 999 ); | |
} | |
/** | |
* @param $post_id | |
* @param $post | |
*/ | |
public function send_publish_activity_notification( $post_id, $post ) { | |
/** | |
* If you are sending a notification to large then you can't send notification to all users together due to PHP or server limited. | |
* Instead of sending notification to all users you need to do divided all users into the batch for better performance. | |
* you can create batch using out class \BuddyBossApp\Jobs | |
*/ | |
$jobs = Jobs::instance(); | |
$jobs->add( 'publish_activity', array( 'book_id' => $post_id, 'paged' => 1, 'timestamp' => time() ) ); | |
$jobs->start(); | |
} | |
/** | |
* @param $task | |
*/ | |
public function handle_publish_activity_job( $task ) { | |
$task_data = maybe_unserialize( $task->data ); | |
$primary_text = __( "Nova atividade publicada." ); | |
$secondary_text = sprintf( __( "%s" ), get_the_title( $task_data['book_id'] ) ); | |
$users = get_users( array( | |
'fields' => 'ids', | |
'number' => 200, | |
'paged' => $task_data['paged'], | |
) ); | |
if ( ! empty( $users ) ) { | |
$this->send_push( | |
array( | |
'primary_text' => $primary_text, | |
'secondary_text' => $secondary_text, | |
'user_ids' => $users, | |
'data' => array(), | |
'push_data' => array( | |
'link' => get_permalink( $task_data['book_id'] ), | |
), | |
'subscription_type' => 'notification_custom_activity', | |
'normal_notification' => true, | |
'normal_notification_data' => array( | |
'component_name' => 'notification_custom_activity', | |
'component_action' => 'notification_custom_activity_action', | |
'item_id' => $task_data['book_id'], | |
'secondary_item_id' => get_the_title( $task_data['book_id'] ), | |
) | |
) | |
); | |
$jobs = Jobs::instance(); | |
$jobs->add( 'publish_activity', array( | |
'book_id' => $task_data['book_id'], | |
'paged' => ( $task_data['paged'] + 1 ), | |
'timestamp' => time(), | |
) ); | |
$jobs->start(); | |
} | |
} | |
/** | |
* @param $component_name | |
* @param $component_action | |
* @param $item_id | |
* @param $secondary_item_id | |
* @param $notification_id | |
* | |
* @return array|void | |
*/ | |
public function format_notification( $component_name, $component_action, $item_id, $secondary_item_id, $notification_id ) { | |
/** | |
* This will help to update notification content for web. | |
* You need to return data in following structure | |
* array( | |
* 'text' => Notification text/html, | |
* 'link' => link of content, | |
* ) | |
*/ | |
if ( 'notification_custom_activity' === $component_action ) { | |
return array( | |
'text' => $secondary_item_id, | |
'link' => get_permalink( $item_id ), | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment