- Timestamp:
- 10/22/2015 06:02:55 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-notifications/classes/class-bp-notifications-notification.php
r10248 r10303 7 7 * @package BuddyPress 8 8 * @subpackage NotificationsClasses 9 *10 9 * @since 1.9.0 11 10 */ … … 115 114 public function save() { 116 115 117 // Return value 116 // Return value. 118 117 $retval = false; 119 118 120 // Default data and format 119 // Default data and format. 121 120 $data = array( 122 121 'user_id' => $this->user_id, … … 141 140 do_action_ref_array( 'bp_notification_before_save', array( &$this ) ); 142 141 143 // Update 142 // Update. 144 143 if ( ! empty( $this->id ) ) { 145 144 $result = self::_update( $data, array( 'ID' => $this->id ), $data_format, array( '%d' ) ); 146 145 147 // Insert 146 // Insert. 148 147 } else { 149 148 $result = self::_insert( $data, $data_format ); 150 149 } 151 150 152 // Set the notification ID if successful 151 // Set the notification ID if successful. 153 152 if ( ! empty( $result ) && ! is_wp_error( $result ) ) { 154 153 global $wpdb; … … 163 162 * @since 2.0.0 164 163 * 165 * @param BP_Notifications_Notification $value Current instance of the notification item being saved. Passed by reference. 164 * @param BP_Notifications_Notification $value Current instance of the notification item being saved. 165 * Passed by reference. 166 166 */ 167 167 do_action_ref_array( 'bp_notification_after_save', array( &$this ) ); 168 168 169 // Return the result 169 // Return the result. 170 170 return $retval; 171 171 } … … 184 184 $bp = buddypress(); 185 185 186 // Look for a notification 186 // Look for a notification. 187 187 $notification = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->notifications->table_name} WHERE id = %d", $this->id ) ); 188 188 189 // Setup the notification data 189 // Setup the notification data. 190 190 if ( ! empty( $notification ) && ! is_wp_error( $notification ) ) { 191 191 $this->item_id = $notification->item_id; … … 240 240 * @param array $data_format See {@link wpdb::insert()}. 241 241 * @param array $where_format See {@link wpdb::insert()}. 242 *243 242 * @return int|false The number of rows updated, or false on error. 244 243 */ … … 259 258 * BP_Notification_Notification object. 260 259 * @param array $where_format See {@link wpdb::insert()}. 261 *262 260 * @return int|false The number of rows updated, or false on error. 263 261 */ … … 281 279 * @param string $join_sql SQL JOIN fragment. 282 280 * @param string $meta_query_sql SQL meta query fragment. 283 *284 281 * @return string WHERE clause. 285 282 */ … … 290 287 $where = ''; 291 288 292 // id289 // The id. 293 290 if ( ! empty( $args['id'] ) ) { 294 291 $id_in = implode( ',', wp_parse_id_list( $args['id'] ) ); … … 296 293 } 297 294 298 // user_id295 // The user_id. 299 296 if ( ! empty( $args['user_id'] ) ) { 300 297 $user_id_in = implode( ',', wp_parse_id_list( $args['user_id'] ) ); … … 302 299 } 303 300 304 // item_id301 // The item_id. 305 302 if ( ! empty( $args['item_id'] ) ) { 306 303 $item_id_in = implode( ',', wp_parse_id_list( $args['item_id'] ) ); … … 308 305 } 309 306 310 // secondary_item_id307 // The secondary_item_id. 311 308 if ( ! empty( $args['secondary_item_id'] ) ) { 312 309 $secondary_item_id_in = implode( ',', wp_parse_id_list( $args['secondary_item_id'] ) ); … … 314 311 } 315 312 316 // component_name313 // The component_name. 317 314 if ( ! empty( $args['component_name'] ) ) { 318 315 if ( ! is_array( $args['component_name'] ) ) { … … 331 328 } 332 329 333 // component_action330 // The component_action. 334 331 if ( ! empty( $args['component_action'] ) ) { 335 332 if ( ! is_array( $args['component_action'] ) ) { … … 348 345 } 349 346 350 // is_new347 // If is_new. 351 348 if ( ! empty( $args['is_new'] ) && 'both' !== $args['is_new'] ) { 352 349 $where_conditions['is_new'] = "is_new = 1"; … … 355 352 } 356 353 357 // search_terms354 // The search_terms. 358 355 if ( ! empty( $args['search_terms'] ) ) { 359 356 $search_terms_like = '%' . bp_esc_like( $args['search_terms'] ) . '%'; … … 361 358 } 362 359 363 // date query360 // The date query. 364 361 if ( ! empty( $args['date_query'] ) ) { 365 362 $where_conditions['date_query'] = self::get_date_query_sql( $args['date_query'] ); 366 363 } 367 364 368 // meta query365 // The meta query. 369 366 if ( ! empty( $meta_query_sql['where'] ) ) { 370 367 $where_conditions['meta_query'] = $meta_query_sql['where']; … … 385 382 $where_conditions = apply_filters( 'bp_notifications_get_where_conditions', $where_conditions, $args, $select_sql, $from_sql, $join_sql, $meta_query_sql ); 386 383 387 // Custom WHERE 384 // Custom WHERE. 388 385 if ( ! empty( $where_conditions ) ) { 389 386 $where = 'WHERE ' . implode( ' AND ', $where_conditions ); … … 403 400 * @param array $args See {@link BP_Notifications_Notification::get()} 404 401 * for more details. 405 *406 402 * @return string ORDER BY clause. 407 403 */ 408 404 protected static function get_order_by_sql( $args = array() ) { 409 405 410 // Setup local variable 406 // Setup local variable. 411 407 $conditions = array(); 412 408 $retval = ''; 413 409 414 // Order by 410 // Order by. 415 411 if ( ! empty( $args['order_by'] ) ) { 416 412 $order_by = implode( ', ', (array) $args['order_by'] ); … … 418 414 } 419 415 420 // Sort order direction 416 // Sort order direction. 421 417 if ( ! empty( $args['sort_order'] ) && in_array( $args['sort_order'], array( 'ASC', 'DESC' ) ) ) { 422 418 $sort_order = $args['sort_order']; … … 424 420 } 425 421 426 // Custom ORDER BY 422 // Custom ORDER BY. 427 423 if ( ! empty( $conditions ) ) { 428 424 $retval = 'ORDER BY ' . implode( ' ', $conditions ); … … 441 437 * @param array $args See {@link BP_Notifications_Notification::get()} 442 438 * for more details. 443 * 444 * @return string LIMIT clause. 439 * @return string $retval LIMIT clause. 445 440 */ 446 441 protected static function get_paged_sql( $args = array() ) { 447 442 global $wpdb; 448 443 449 // Setup local variable 444 // Setup local variable. 450 445 $retval = ''; 451 446 452 // Custom LIMIT 447 // Custom LIMIT. 453 448 if ( ! empty( $args['page'] ) && ! empty( $args['per_page'] ) ) { 454 449 $page = absint( $args['page'] ); … … 500 495 * See {@BP_Notifications_Notification::get()} 501 496 * for a breakdown. 502 *503 497 * @return array Associative array of 'data' and 'format' args. 504 498 */ … … 509 503 ); 510 504 511 // id505 // The id. 512 506 if ( ! empty( $args['id'] ) ) { 513 507 $where_clauses['data']['id'] = absint( $args['id'] ); … … 515 509 } 516 510 517 // user_id511 // The user_id. 518 512 if ( ! empty( $args['user_id'] ) ) { 519 513 $where_clauses['data']['user_id'] = absint( $args['user_id'] ); … … 521 515 } 522 516 523 // item_id517 // The item_id. 524 518 if ( ! empty( $args['item_id'] ) ) { 525 519 $where_clauses['data']['item_id'] = absint( $args['item_id'] ); … … 527 521 } 528 522 529 // secondary_item_id523 // The secondary_item_id. 530 524 if ( ! empty( $args['secondary_item_id'] ) ) { 531 525 $where_clauses['data']['secondary_item_id'] = absint( $args['secondary_item_id'] ); … … 533 527 } 534 528 535 // component_name529 // The component_name. 536 530 if ( ! empty( $args['component_name'] ) ) { 537 531 $where_clauses['data']['component_name'] = $args['component_name']; … … 539 533 } 540 534 541 // component_action535 // The component_action. 542 536 if ( ! empty( $args['component_action'] ) ) { 543 537 $where_clauses['data']['component_action'] = $args['component_action']; … … 545 539 } 546 540 547 // is_new541 // If is_new. 548 542 if ( isset( $args['is_new'] ) ) { 549 543 $where_clauses['data']['is_new'] = ! empty( $args['is_new'] ) ? 1 : 0; … … 563 557 * @param int $user_id ID of the user being checked. 564 558 * @param int $notification_id ID of the notification being checked. 565 *566 559 * @return bool True if the notification belongs to the user, otherwise 567 560 * false. … … 580 573 * @since 2.3.0 581 574 * 582 * @param mixed $args575 * @param mixed $args Args to parse. 583 576 * @return array 584 577 */ … … 643 636 global $wpdb; 644 637 645 // Parse the arguments 638 // Parse the arguments. 646 639 $r = self::parse_args( $args ); 647 640 648 // Get BuddyPress 641 // Get BuddyPress. 649 642 $bp = buddypress(); 650 643 651 // METADATA 644 // METADATA. 652 645 $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] ); 653 646 654 // SELECT 647 // SELECT. 655 648 $select_sql = "SELECT *"; 656 649 657 // FROM 650 // FROM. 658 651 $from_sql = "FROM {$bp->notifications->table_name} n "; 659 652 660 // JOIN 653 // JOIN. 661 654 $join_sql = $meta_query_sql['join']; 662 655 663 // WHERE 656 // WHERE. 664 657 $where_sql = self::get_where_sql( array( 665 658 'id' => $r['id'], … … 674 667 ), $select_sql, $from_sql, $join_sql, $meta_query_sql ); 675 668 676 // ORDER BY 669 // ORDER BY. 677 670 $order_sql = self::get_order_by_sql( array( 678 671 'order_by' => $r['order_by'], … … 680 673 ) ); 681 674 682 // LIMIT %d, %d 675 // LIMIT %d, %d. 683 676 $pag_sql = self::get_paged_sql( array( 684 677 'page' => $r['page'], … … 686 679 ) ); 687 680 688 // Concatenate query parts 681 // Concatenate query parts. 689 682 $sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} {$order_sql} {$pag_sql}"; 690 683 … … 697 690 * @since 1.9.0 698 691 * 699 * @see BP_Notifications_Notification::get() for a description of 700 * arguments. 692 * @see BP_Notifications_Notification::get() for a description of arguments. 701 693 * 702 694 * @param array $args See {@link BP_Notifications_Notification::get()}. 703 *704 695 * @return int Count of located items. 705 696 */ … … 707 698 global $wpdb; 708 699 709 // Parse the arguments 700 // Parse the arguments. 710 701 $r = self::parse_args( $args ); 711 702 712 // Load BuddyPress 703 // Load BuddyPress. 713 704 $bp = buddypress(); 714 705 715 // METADATA 706 // METADATA. 716 707 $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] ); 717 708 718 // SELECT 709 // SELECT. 719 710 $select_sql = "SELECT COUNT(*)"; 720 711 721 // FROM 712 // FROM. 722 713 $from_sql = "FROM {$bp->notifications->table_name} n "; 723 714 724 // JOIN 715 // JOIN. 725 716 $join_sql = $meta_query_sql['join']; 726 717 727 // WHERE 718 // WHERE. 728 719 $where_sql = self::get_where_sql( array( 729 720 'id' => $r['id'], … … 738 729 ), $select_sql, $from_sql, $join_sql, $meta_query_sql ); 739 730 740 // Concatenate query parts 731 // Concatenate query parts. 741 732 $sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql}"; 742 733 743 // Return the queried results 734 // Return the queried results. 744 735 return $wpdb->get_var( $sql ); 745 736 } … … 758 749 * @param array $meta_query An array of meta_query filters. See the 759 750 * documentation for WP_Meta_Query for details. 760 *761 751 * @return array $sql_array 'join' and 'where' clauses. 762 752 */ 763 753 public static function get_meta_query_sql( $meta_query = array() ) { 764 754 765 // Default array keys & empty values 755 // Default array keys & empty values. 766 756 $sql_array = array( 767 757 'join' => '', … … 769 759 ); 770 760 771 // Bail if no meta query 761 // Bail if no meta query. 772 762 if ( empty( $meta_query ) ) { 773 763 return $sql_array; 774 764 } 775 765 776 // WP_Meta_Query expects the table name at $wpdb->notificationmeta 766 // WP_Meta_Query expects the table name at $wpdb->notificationmeta. 777 767 $GLOBALS['wpdb']->notificationmeta = buddypress()->notifications->table_name_meta; 778 768 … … 780 770 $meta_sql = $n_meta_query->get_sql( 'notification', 'n', 'id' ); 781 771 782 // Strip the leading AND - it's handled in get() 772 // Strip the leading AND - it's handled in get(). 783 773 $sql_array['where'] = preg_replace( '/^\sAND/', '', $meta_sql['where'] ); 784 774 $sql_array['join'] = $meta_sql['join']; … … 800 790 * @param array $date_query An array of date_query parameters. See the 801 791 * documentation for the first parameter of WP_Date_Query. 802 *803 792 * @return string 804 793 */ 805 794 public static function get_date_query_sql( $date_query = array() ) { 806 795 807 // Bail if not a proper date query format 796 // Bail if not a proper date query format. 808 797 if ( empty( $date_query ) || ! is_array( $date_query ) ) { 809 798 return ''; 810 799 } 811 800 812 // Date query 801 // Date query. 813 802 $date_query = new BP_Date_Query( $date_query, 'date_recorded' ); 814 803 815 // Strip the leading AND - it's handled in get() 804 // Strip the leading AND - it's handled in get(). 816 805 return preg_replace( '/^\sAND/', '', $date_query->get_sql() ); 817 806 } … … 827 816 * @param array $update_args Associative array of fields to update, 828 817 * and the values to update them to. Of the format 829 * array( 'user_id' => 4, 'component_name' => 'groups', ) 818 * array( 'user_id' => 4, 'component_name' => 'groups', ). 830 819 * @param array $where_args Associative array of columns/values, to 831 820 * determine which rows should be updated. Of the format 832 * array( 'item_id' => 7, 'component_action' => 'members', ) 833 * 821 * array( 'item_id' => 7, 'component_action' => 'members', ). 834 822 * @return int|bool Number of rows updated on success, false on failure. 835 823 */ … … 866 854 * @param array $args Associative array of columns/values, to determine 867 855 * which rows should be deleted. Of the format 868 * array( 'item_id' => 7, 'component_action' => 'members', ) 869 * 856 * array( 'item_id' => 7, 'component_action' => 'members', ). 870 857 * @return int|bool Number of rows deleted on success, false on failure. 871 858 */ … … 898 885 * 899 886 * @param int $id ID of the notification item to be deleted. 900 *901 887 * @return bool True on success, false on failure. 902 888 */ … … 916 902 * @param string $status Optional. Status of notifications to fetch. 917 903 * 'is_new' to get only unread items, 'all' to get all. 918 *919 904 * @return array Associative array of notification items. 920 905 */ … … 933 918 * @param int $user_id ID of the user whose notifications are being 934 919 * fetched. 935 *936 920 * @return array Associative array of unread notification items. 937 921 */ … … 950 934 * @param int $user_id ID of the user whose notifications are being 951 935 * fetched. 952 *953 936 * @return array Associative array of unread notification items. 954 937 */ … … 994 977 $notifications = self::get( $r ); 995 978 996 // Bail if no notifications 979 // Bail if no notifications. 997 980 if ( empty( $notifications ) ) { 998 981 return false; … … 1017 1000 * @param string $component_action Name of the component action. 1018 1001 * @param int $secondary_item_id The ID of the secondary item. 1019 *1020 1002 * @return bool|int False on failure to update. ID on success. 1021 1003 */ 1022 1004 public static function mark_all_for_user( $user_id, $is_new = 0, $item_id = 0, $component_name = '', $component_action = '', $secondary_item_id = 0 ) { 1023 1005 1024 // Values to be updated 1006 // Values to be updated. 1025 1007 $update_args = array( 1026 1008 'is_new' => $is_new, 1027 1009 ); 1028 1010 1029 // WHERE clauses 1011 // WHERE clauses. 1030 1012 $where_args = array( 1031 1013 'user_id' => $user_id, … … 1061 1043 * @param string $component_action Name of the component action. 1062 1044 * @param int $secondary_item_id The ID of the secondary item. 1063 *1064 1045 * @return bool|int 1065 1046 */ 1066 1047 public static function mark_all_from_user( $user_id, $is_new = 0, $component_name = '', $component_action = '', $secondary_item_id = 0 ) { 1067 1048 1068 // Values to be updated 1049 // Values to be updated. 1069 1050 $update_args = array( 1070 1051 'is_new' => $is_new, 1071 1052 ); 1072 1053 1073 // WHERE clauses 1054 // WHERE clauses. 1074 1055 $where_args = array( 1075 1056 'item_id' => $user_id, … … 1106 1087 * @param int $secondary_item_id Optional. ID of the secondary 1107 1088 * associated item. 1108 *1109 1089 * @return bool|int 1110 1090 */ 1111 1091 public static function mark_all_by_type( $item_id, $is_new = 0, $component_name = '', $component_action = '', $secondary_item_id = 0 ) { 1112 1092 1113 // Values to be updated 1093 // Values to be updated. 1114 1094 $update_args = array( 1115 1095 'is_new' => $is_new, 1116 1096 ); 1117 1097 1118 // WHERE clauses 1098 // WHERE clauses. 1119 1099 $where_args = array( 1120 1100 'item_id' => $item_id,
Note: See TracChangeset
for help on using the changeset viewer.