Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/01/2015 05:22:59 PM (10 years ago)
Author:
johnjamesjacoby
Message:

Notifications: Introduce notification metadata table.

  • Bump DB version
  • Introduce DB upgrade function & routine for 2.3.0
  • Add metadata table to Notifications installation function
  • Add metadata CRUD functions to Notifications component

See #6257.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/bp-notifications-functions.php

    r9352 r9572  
    612612    return apply_filters( 'bp_notifications_get_registered_components', $component_names, $active_components );
    613613}
     614
     615/** Meta **********************************************************************/
     616
     617/**
     618 * Delete a meta entry from the DB for a notification item.
     619 *
     620 * @since BuddyPress (2.3.0)
     621 *
     622 * @global object $wpdb WordPress database access object.
     623 *
     624 * @param int    $notification_id ID of the notification item whose metadata is being deleted.
     625 * @param string $meta_key        Optional. The key of the metadata being deleted. If
     626 *                                omitted, all metadata associated with the notification
     627 *                                item will be deleted.
     628 * @param string $meta_value      Optional. If present, the metadata will only be
     629 *                                deleted if the meta_value matches this parameter.
     630 * @param bool   $delete_all      Optional. If true, delete matching metadata entries
     631 *                                for all objects, ignoring the specified object_id. Otherwise,
     632 *                                only delete matching metadata entries for the specified
     633 *                                notification item. Default: false.
     634 *
     635 * @return bool                   True on success, false on failure.
     636 */
     637function bp_notifications_delete_meta( $notification_id, $meta_key = '', $meta_value = '', $delete_all = false ) {
     638
     639    // Legacy - if no meta_key is passed, delete all for the item
     640    if ( empty( $meta_key ) ) {
     641        $all_meta = bp_notifications_get_meta( $notification_id );
     642        $keys     = ! empty( $all_meta )
     643            ? array_keys( $all_meta )
     644            : array();
     645
     646        // With no meta_key, ignore $delete_all
     647        $delete_all = false;
     648    } else {
     649        $keys = array( $meta_key );
     650    }
     651
     652    $retval = true;
     653
     654    add_filter( 'query', 'bp_filter_metaid_column_name' );
     655    foreach ( $keys as $key ) {
     656        $retval = delete_metadata( 'notifications', $notification_id, $key, $meta_value, $delete_all );
     657    }
     658    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     659
     660    return $retval;
     661}
     662
     663/**
     664 * Get metadata for a given notification item.
     665 *
     666 * @since BuddyPress (2.3.0)
     667 *
     668 * @uses apply_filters() To call the 'bp_notifications_get_meta' hook.
     669 *
     670 * @param int    $notification_id ID of the notification item whose metadata is being requested.
     671 * @param string $meta_key        Optional. If present, only the metadata matching
     672 *                                that meta key will be returned. Otherwise, all metadata for the
     673 *                                notification item will be fetched.
     674 * @param bool   $single          Optional. If true, return only the first value of the
     675 *                                specified meta_key. This parameter has no effect if meta_key is not
     676 *                                specified. Default: true.
     677 *
     678 * @return mixed                  The meta value(s) being requested.
     679 */
     680function bp_notifications_get_meta( $notification_id = 0, $meta_key = '', $single = true ) {
     681    add_filter( 'query', 'bp_filter_metaid_column_name' );
     682    $retval = get_metadata( 'notifications', $notification_id, $meta_key, $single );
     683    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     684
     685    /**
     686     * Filters the metadata for a specified notification item.
     687     *
     688     * @since BuddyPress (2.3.0)
     689     *
     690     * @param mixed  $retval          The meta values for the notification item.
     691     * @param int    $notification_id ID of the notification item.
     692     * @param string $meta_key        Meta key for the value being requested.
     693     * @param bool   $single          Whether to return one matched meta key row or all.
     694     */
     695    return apply_filters( 'bp_notifications_get_meta', $retval, $notification_id, $meta_key, $single );
     696}
     697
     698/**
     699 * Update a piece of notification meta.
     700 *
     701 * @since BuddyPress (1.2.0)
     702 *
     703 * @param  int    $notification_id ID of the notification item whose metadata is being
     704 *                                 updated.
     705 * @param  string $meta_key        Key of the metadata being updated.
     706 * @param  mixed  $meta_value      Value to be set.
     707 * @param  mixed  $prev_value      Optional. If specified, only update existing
     708 *                                 metadata entries with the specified value.
     709 *                                 Otherwise, update all entries.
     710 *
     711 * @return bool|int                Returns false on failure. On successful
     712 *                                 update of existing metadata, returns true. On
     713 *                                 successful creation of new metadata,  returns
     714 *                                 the integer ID of the new metadata row.
     715 */
     716function bp_notifications_update_meta( $notification_id, $meta_key, $meta_value, $prev_value = '' ) {
     717    add_filter( 'query', 'bp_filter_metaid_column_name' );
     718    $retval = update_metadata( 'notifications', $notification_id, $meta_key, $meta_value, $prev_value );
     719    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     720
     721    return $retval;
     722}
     723
     724/**
     725 * Add a piece of notification metadata.
     726 *
     727 * @since BuddyPress (2.3.0)
     728 *
     729 * @param int    $notification_id ID of the notification item.
     730 * @param string $meta_key        Metadata key.
     731 * @param mixed  $meta_value      Metadata value.
     732 * @param bool   $unique          Optional. Whether to enforce a single metadata value
     733 *                                for the given key. If true, and the object already has a value for
     734 *                                the key, no change will be made. Default: false.
     735 *
     736 * @return int|bool               The meta ID on successful update, false on failure.
     737 */
     738function bp_notifications_add_meta( $notification_id, $meta_key, $meta_value, $unique = false ) {
     739    add_filter( 'query', 'bp_filter_metaid_column_name' );
     740    $retval = add_metadata( 'notifications', $notification_id, $meta_key, $meta_value, $unique );
     741    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     742
     743    return $retval;
     744}
Note: See TracChangeset for help on using the changeset viewer.