ap_insert_reputation_event( string $slug, string $label, string $description, int $points, int $activity, string $parent_type = '', string $icon = '' )

Description #

Insert a reputation event to database.

Parameters #

  • $slug
    string (Required) Reputation event unique slug.
  • $label
    string (Required) Reputation event label, must be less than 100 letters.
  • $description
    string (Required) Reputation event description.
  • $points
    int (Required) Signed point value.
  • $activity
    int (Required) Activity label.
  • $parent_type
    string (Optional) Parent type. Default value: ''
  • $icon
    string (Optional) Reputation event icon. Default value: ''

Changelog #

VersionDescription
4.4.0Added new argument $icon for setting the respective reputation event icon.
4.3.0Introduced.

Source #

File: includes/reputation.php

function ap_insert_reputation_event( $slug, $label, $description, $points, $activity, $parent_type = '', $icon = '' ) {
	global $wpdb;

	$slug     = sanitize_key( $slug );
	$existing = ap_get_reputation_event_by_slug( $slug );

	if ( $existing ) {
		return new WP_Error( 'already_exits' );
	}

	$inserted = $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
		$wpdb->ap_reputation_events,
		array(
			'slug'        => $slug,
			'label'       => $label,
			'description' => $description,
			'points'      => $points,
			'activity'    => $activity,
			'parent'      => $parent_type,
			'icon'        => $icon,
		),
		array(
			'%s',
			'%s',
			'%s',
			'%d',
			'%s',
			'%s',
			'%s',
		)
	);

	if ( $inserted ) {
		$inserted_id = $wpdb->insert_id;

		// Delete cache.
		wp_cache_delete( 'all', 'ap_get_all_reputation_events' );

		/**
		 * Hook called right after inserting a reputation event.
		 *
		 * @param object $event Reputation event id.
		 * @since 4.3.0
		 */
		do_action( 'ap_inserted_reputation_event', $inserted_id );

		return $inserted_id;
	}

	return new WP_Error( 'failed_to_insert_rep_event' );
}

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