Changeset 13636
- Timestamp:
- 11/07/2023 10:41:34 PM (20 months ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-activity/bp-activity-filters.php
r13491 r13636 104 104 add_filter( 'bp_get_activity_content', 'bp_activity_truncate_entry', 5 ); 105 105 106 add_filter( 'bp_get_total_favorite_count_for_user', 'bp_core_number_format' ); 107 add_filter( 'bp_get_total_mention_count_for_user', 'bp_core_number_format' ); 106 add_filter( 'bp_activity_get_user_favorites', 'bp_activity_sanitize_user_favorites_meta' ); 107 add_filter( 'bp_get_total_favorite_count_for_user', 'bp_core_number_format' ); 108 add_filter( 'bp_get_total_mention_count_for_user', 'bp_core_number_format' ); 108 109 109 110 add_filter( 'bp_activity_get_embed_excerpt', 'bp_activity_embed_excerpt_onclick_location_filter', 9 ); -
trunk/src/bp-activity/bp-activity-functions.php
r13539 r13636 1015 1015 1016 1016 /** 1017 * Sanitize callback for the User's favorites meta. 1018 * 1019 * @since 12.0.0 1020 * 1021 * @param array $value The list of favorited activity IDs. 1022 * @return array The sanitized list of favorited activity IDs. 1023 */ 1024 function bp_activity_sanitize_user_favorites_meta( $value = array() ) { 1025 return array_filter( wp_parse_id_list( $value ) ); 1026 } 1027 1028 /** 1029 * Use WordPress Meta API to deal with favorites meta properties and sanitization. 1030 * 1031 * @since 12.0.0 1032 */ 1033 function bp_activity_register_user_favorites_meta() { 1034 register_meta( 1035 'user', 1036 'bp_favorite_activities', 1037 array( 1038 'single' => true, 1039 'type' => 'array', 1040 'description' => __( 'The list of Activity IDs a user favorited.', 'buddypress' ), 1041 'show_in_rest' => false, // We're not showing this meta into the WP users REST endpoint. 1042 'sanitize_callback' => 'bp_activity_sanitize_user_favorites_meta', 1043 'default' => array(), 1044 ) 1045 ); 1046 } 1047 add_action( 'bp_init', 'bp_activity_register_user_favorites_meta' ); 1048 1049 /** 1017 1050 * Get a users favorite activity stream items. 1018 1051 * … … 1052 1085 */ 1053 1086 function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 1087 // Cast as an integer to make sure we're only saving integers into the user's meta. 1088 if ( ! empty( $activity_id ) ) { 1089 $activity_id = (int) $activity_id; 1090 } else { 1091 $activity_id = 0; 1092 } 1093 1094 if ( ! $activity_id ) { 1095 return false; 1096 } 1054 1097 1055 1098 // Fallback to logged in user if no user_id is passed. … … 1058 1101 } 1059 1102 1060 $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ); 1061 if ( empty( $my_favs ) || ! is_array( $my_favs ) ) { 1062 $my_favs = array(); 1063 } 1103 // Get user's existing favorites. 1104 $my_favs = bp_activity_get_user_favorites( $user_id ); 1064 1105 1065 1106 // Bail if the user has already favorited this activity item. 1066 if ( in_array( $activity_id, $my_favs ) ) {1107 if ( in_array( $activity_id, $my_favs, true ) ) { 1067 1108 return false; 1068 1109 } … … 1072 1113 1073 1114 // Update the total number of users who have favorited this activity. 1074 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 1075 $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1; 1115 $fav_count = (int) bp_activity_get_meta( $activity_id, 'favorite_count' ); 1116 if ( ! empty( $fav_count ) ) { 1117 $fav_count += 1; 1118 } else { 1119 $fav_count = 1; 1120 } 1076 1121 1077 1122 // Update user meta. … … 1127 1172 } 1128 1173 1129 $my_favs = bp_ get_user_meta( $user_id, 'bp_favorite_activities', true);1130 $my_favs = array_flip( (array)$my_favs );1174 $my_favs = bp_activity_get_user_favorites( $user_id ); 1175 $my_favs = array_flip( $my_favs ); 1131 1176 1132 1177 // Bail if the user has not previously favorited the item. … … 1136 1181 1137 1182 // Remove the fav from the user's favs. 1138 unset( $my_favs[ $activity_id] );1183 unset( $my_favs[ $activity_id ] ); 1139 1184 $my_favs = array_unique( array_flip( $my_favs ) ); 1140 1185 -
trunk/src/bp-templates/bp-legacy/buddypress-functions.php
r13526 r13636 1278 1278 $activity_id = (int) $_POST['id']; 1279 1279 $activity_item = new BP_Activity_Activity( $activity_id ); 1280 if ( ! bp_activity_user_can_read( $activity_item, bp_loggedin_user_id() ) ) { 1281 return; 1282 } 1283 1284 if ( bp_activity_add_user_favorite( $_POST['id'] ) ) 1285 _e( 'Remove Favorite', 'buddypress' ); 1286 else 1287 _e( 'Favorite', 'buddypress' ); 1280 if ( empty( $activity_item->id ) || ! bp_activity_user_can_read( $activity_item, bp_loggedin_user_id() ) ) { 1281 return; 1282 } 1283 1284 if ( bp_activity_add_user_favorite( $activity_id ) ) { 1285 esc_html_e( 'Remove Favorite', 'buddypress' ); 1286 } else { 1287 esc_html_e( 'Favorite', 'buddypress' ); 1288 } 1288 1289 1289 1290 exit; … … 1312 1313 } 1313 1314 1314 if ( bp_activity_remove_user_favorite( $_POST['id'] ) ) 1315 _e( 'Favorite', 'buddypress' ); 1316 else 1317 _e( 'Remove Favorite', 'buddypress' ); 1315 $activity_id = (int) $_POST['id']; 1316 1317 if ( bp_activity_remove_user_favorite( $activity_id ) ) { 1318 esc_html_e( 'Favorite', 'buddypress' ); 1319 } else { 1320 esc_html_e( 'Remove Favorite', 'buddypress' ); 1321 } 1318 1322 1319 1323 exit; -
trunk/src/bp-templates/bp-nouveau/includes/activity/ajax.php
r13503 r13636 104 104 $activity_id = (int) $_POST['id']; 105 105 $activity_item = new BP_Activity_Activity( $activity_id ); 106 if ( ! bp_activity_user_can_read( $activity_item, bp_loggedin_user_id() ) ) {107 wp_send_json_error(); 108 } 109 110 if ( bp_activity_add_user_favorite( $ _POST['id']) ) {106 if ( empty( $activity_item->id ) || ! bp_activity_user_can_read( $activity_item, bp_loggedin_user_id() ) ) { 107 wp_send_json_error(); 108 } 109 110 if ( bp_activity_add_user_favorite( $activity_id ) ) { 111 111 $response = array( 'content' => __( 'Remove Favorite', 'buddypress' ) ); 112 112 … … 150 150 } 151 151 152 if ( bp_activity_remove_user_favorite( $_POST['id'] ) ) { 152 $activity_id = (int) $_POST['id']; 153 154 if ( bp_activity_remove_user_favorite( $activity_id ) ) { 153 155 $response = array( 'content' => __( 'Mark as Favorite', 'buddypress' ) ); 154 156 -
trunk/tests/phpunit/testcases/activity/functions.php
r13414 r13636 1467 1467 1468 1468 /** 1469 * @group favorites 1470 * @group bp_activity_add_user_favorite 1471 */ 1472 public function test_add_user_favorite_not_an_activity_id() { 1473 $u1 = self::factory()->user->create(); 1474 $a = self::factory()->activity->create(); 1475 1476 // bp_activity_add_user_favorite() requires a logged-in user. 1477 $current_user = bp_loggedin_user_id(); 1478 $this->set_current_user( $u1 ); 1479 1480 // Only favorite for user 1 1481 bp_activity_add_user_favorite( $a, $u1 ); 1482 $user_favorites = array( $a ); 1483 $this->assertEquals( $user_favorites, bp_activity_get_user_favorites( $u1 ) ); 1484 1485 // Adds something that is not an activity id. 1486 bp_activity_add_user_favorite( 'not_an_activity_id', $u1 ); 1487 1488 // The above shouldn't be added. 1489 $this->assertEquals( $user_favorites, bp_activity_get_user_favorites( $u1 ) ); 1490 $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) ); 1491 1492 $this->set_current_user( $current_user ); 1493 } 1494 1495 /** 1469 1496 * @group bp_activity_post_update 1470 1497 */
Note: See TracChangeset
for help on using the changeset viewer.