ap_insert_notification( array $args = array() )

Description #

Insert notification.

Parameters #

  • $args
    array (Optional) Arguments. Default value: array()

Source #

File: addons/notifications/functions.php

function ap_insert_notification( $args = array() ) {

	// Dont do insert notification if defined.
	if ( defined( 'AP_DISABLE_INSERT_NOTI' ) && AP_DISABLE_INSERT_NOTI ) {
		return;
	}

	$args = wp_parse_args(
		$args,
		array(
			'user_id'  => get_current_user_id(),
			'actor'    => 0,
			'parent'   => '',
			'ref_id'   => 0,
			'ref_type' => '',
			'verb'     => '',
			'seen'     => 0,
			'date'     => current_time( 'mysql' ),
		)
	);

	// Return if user_id is empty or 0.
	if ( empty( $args['user_id'] ) ) {
		return false;
	}

	$noti_args = array(
		'numbers'  => 1,
		'parent'   => $args['parent'],
		'ref_type' => $args['ref_type'],
		'verb'     => $args['verb'],
		'user_id'  => $args['user_id'],
	);

	global $wpdb;
	$exists = ap_get_notifications( $noti_args );

	// If already exists then just update date and mark as unread.
	if ( ! empty( $exists ) ) {
		return $wpdb->update( // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->prefix . 'ap_notifications',
			array(
				'noti_ref_id' => $args['ref_id'],
				'noti_actor'  => $args['actor'],
				'noti_date'   => $args['date'],
				'noti_verb'   => $args['verb'],
				'noti_seen'   => 0,
			),
			array(
				'noti_id' => $exists[0]->noti_id,
			)
		);
	}

	$insert = $wpdb->insert( // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
		$wpdb->prefix . 'ap_notifications',
		array(
			'noti_user_id'  => $args['user_id'],
			'noti_actor'    => $args['actor'],
			'noti_parent'   => $args['parent'],
			'noti_ref_id'   => $args['ref_id'],
			'noti_ref_type' => $args['ref_type'],
			'noti_verb'     => $args['verb'],
			'noti_date'     => $args['date'],
			'noti_seen'     => $args['seen'],
		),
		array( '%d', '%d', '%d', '%d', '%s', '%s', '%s', '%d' )
	);

	if ( false === $insert ) {
		return false;
	}

	return $wpdb->insert_id;
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment