BP_REST_Topics_Endpoint::update_item( WP_REST_Request $request )
Update/Edit a topic.
Description
Parameters
- $request
-
(Required) Full details about the request.
Return
(WP_REST_Response) | WP_Error
Source
File: bp-forums/classes/class-bp-rest-topics-endpoint.php
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 | public function update_item( $request ) { $request ->set_param( 'context' , 'edit' ); $topic_new = $this ->prepare_topic_for_database( $request ); // Define local variable(s). $revisions_removed = false; $topic = 0; $topic_id = 0; $topic_author = 0; $forum_id = 0; $topic_title = '' ; $topic_content = '' ; $topic_edit_reason = '' ; $anonymous_data = array (); // Topic id was not passed. if ( empty ( $topic_new ->bbp_topic_id ) ) { new WP_Error( 'bp_rest_topic_invalid_id' , __( 'Invalid topic ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); // Topic id was passed. } elseif ( is_numeric ( $topic_new ->bbp_topic_id ) ) { $topic_id = (int) $topic_new ->bbp_topic_id; $topic = bbp_get_topic( $topic_id ); } // Topic does not exist. if ( empty ( $topic ) ) { new WP_Error( 'bp_rest_bbp_edit_topic_not_found' , __( 'Sorry, The discussion you want to edit was not found.' , 'buddyboss' ), array ( 'status' => 404, ) ); } // Topic exists. // Check users ability to create new topic. if ( ! bbp_is_topic_anonymous( $topic_id ) ) { // User cannot edit this topic. if ( ! current_user_can( 'edit_topic' , $topic_id ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_permissions' , __( 'Sorry, You do not have permission to edit that discussion.' , 'buddyboss' ), array ( 'status' => 403, ) ); } // Set topic author. $topic_author = bbp_get_topic_author_id( $topic_id ); // It is an anonymous post. } else { $anonymous_args = array ( 'bbp_anonymous_name' => ! empty ( $request [ 'anonymous_name' ] ) ? sanitize_text_field( $request [ 'anonymous_name' ] ) : '' , 'bbp_anonymous_email' => ! empty ( $request [ 'anonymous_email' ] ) ? sanitize_email( $request [ 'anonymous_email' ] ) : '' , 'bbp_anonymous_website' => ! empty ( $request [ 'anonymous_website' ] ) ? sanitize_text_field( $request [ 'anonymous_website' ] ) : '' , ); // Filter anonymous data. $anonymous_data = bbp_filter_anonymous_post_data( $anonymous_args ); } // Remove kses filters from title and content for capable users. if ( current_user_can( 'unfiltered_html' ) ) { remove_filter( 'bbp_edit_topic_pre_title' , 'wp_filter_kses' ); remove_filter( 'bbp_edit_topic_pre_content' , 'bbp_encode_bad' , 10 ); remove_filter( 'bbp_edit_topic_pre_content' , 'bbp_filter_kses' , 30 ); } /** Topic Forum */ // Forum id was not passed. if ( empty ( $topic_new ->bbp_forum_id ) ) { return new WP_Error( 'bp_rest_bbp_topic_forum_id' , __( 'Sorry, Forum ID is missing.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Forum id was passed. } elseif ( is_numeric ( $topic_new ->bbp_forum_id ) ) { $forum_id = (int) $topic_new ->bbp_forum_id; } // Current forum this topic is in. $current_forum_id = bbp_get_topic_forum_id( $topic_id ); // Forum exists. if ( ! empty ( $forum_id ) && ( $forum_id !== $current_forum_id ) ) { // Forum is a category. if ( bbp_is_forum_category( $forum_id ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_forum_category' , __( 'Sorry, This forum is a category. No discussions can be created in this forum.' , 'buddyboss' ), array ( 'status' => 400, ) ); // Forum is not a category. } else { // Forum is closed and user cannot access. if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum' , $forum_id ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_forum_closed' , __( 'Sorry, This forum has been closed to new discussions.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** * Added logic for group forum * Current user is part of that group or not. * We need to check manually because bbpress updating that caps only on group forum page and * in API those conditional tag will not work. */ $is_member = false; $group_ids = array (); if ( function_exists( 'bbp_get_forum_group_ids' ) ) { $group_ids = bbp_get_forum_group_ids( $forum_id ); if ( ! empty ( $group_ids ) ) { foreach ( $group_ids as $group_id ) { if ( groups_is_user_member( $topic_author , $group_id ) ) { $is_member = true; break ; } } } } // Forum is private and user cannot access. if ( bbp_is_forum_private( $forum_id ) && ! bbp_is_user_keymaster() ) { if ( ( empty ( $group_ids ) && ! current_user_can( 'read_private_forums' ) ) || ( ! empty ( $group_ids ) && ! $is_member ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_forum_private' , __( 'Sorry, This forum is private and you do not have the capability to read or create new discussions in it.' , 'buddyboss' ), array ( 'status' => 400, ) ); } // Forum is hidden and user cannot access. } elseif ( bbp_is_forum_hidden( $forum_id ) && ! bbp_is_user_keymaster() ) { if ( ( empty ( $group_ids ) && ! current_user_can( 'read_hidden_forums' ) ) || ( ! empty ( $group_ids ) && ! $is_member ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_forum_hidden' , __( 'Sorry, This forum is hidden and you do not have the capability to read or create new discussions in it.' , 'buddyboss' ), array ( 'status' => 400, ) ); } } } } /** Discussion Title */ if ( ! empty ( $topic_new ->bbp_topic_title ) ) { $topic_title = esc_attr( wp_strip_all_tags( $topic_new ->bbp_topic_title ) ); } // Filter and sanitize. $topic_title = apply_filters( 'bbp_edit_topic_pre_title' , $topic_title , $topic_id ); // No topic title. if ( empty ( $topic_title ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_title' , __( 'Sorry, Your discussion needs a title.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Content */ if ( ! empty ( $topic_new ->bbp_topic_content ) ) { $topic_content = $topic_new ->bbp_topic_content; } // Filter and sanitize. $topic_content = apply_filters( 'bbp_edit_topic_pre_content' , $topic_content , $topic_id ); // No topic content. if ( empty ( $topic_content ) ) { return new WP_Error( 'bp_rest_bbp_edit_topic_content' , __( 'Sorry, Your discussion cannot be empty.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Blacklist */ if ( ! bbp_check_for_blacklist( $anonymous_data , $topic_author , $topic_title , $topic_content ) ) { return new WP_Error( 'bp_rest_bbp_topic_blacklist' , __( 'Sorry, Your discussion cannot be edited at this time.' , 'buddyboss' ), array ( 'status' => 400, ) ); } /** Topic Status */ // Maybe put into moderation. if ( ! bbp_check_for_moderation( $anonymous_data , $topic_author , $topic_title , $topic_content ) ) { // Set post status to pending if public or closed. if ( in_array( $topic ->post_status, array ( bbp_get_public_status_id(), bbp_get_closed_status_id(), ), true ) ) { $topic_status = bbp_get_pending_status_id(); } // Check a whitelist of possible topic status ID's. } elseif ( ! empty ( $topic_new ->bbp_topic_status ) && in_array( $topic_new ->bbp_topic_status, array_keys ( bbp_get_topic_statuses() ), true ) ) { $topic_status = $topic_new ->bbp_topic_status; // Use existing post_status. } else { $topic_status = $topic ->post_status; } /** Topic Tags */ // Either replace terms. if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags' ) && ! empty ( $topic_new ->bbp_topic_tags ) ) { // Escape tag input. $terms = esc_attr( wp_strip_all_tags( $topic_new ->bbp_topic_tags ) ); // Explode by comma. if ( strstr ( $terms , ',' ) ) { $terms = explode ( ',' , $terms ); } // Add topic tag ID as main key. $terms = array ( bbp_get_topic_tag_tax_id() => $terms ); // ...or remove them. } elseif ( isset( $topic_new ->bbp_topic_tags ) ) { $terms = array ( bbp_get_topic_tag_tax_id() => array () ); // Existing terms. } else { $terms = array ( bbp_get_topic_tag_tax_id() => explode ( ',' , bbp_get_topic_tag_names( $topic_id , ',' ) ) ); } /** Additional Actions (Before Save) */ do_action( 'bbp_edit_topic_pre_extras' , $topic_id ); // Add the content of the form to $topic_data as an array. // Just in time manipulation of topic data before being edited. $topic_data = apply_filters( 'bbp_edit_topic_pre_insert' , array ( 'ID' => $topic_id , 'post_title' => $topic_title , 'post_content' => $topic_content , 'post_status' => $topic_status , 'post_parent' => $forum_id , 'post_author' => $topic_author , 'post_type' => bbp_get_topic_post_type(), 'tax_input' => $terms , ) ); // Toggle revisions to avoid duplicates. if ( post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) { $revisions_removed = true; remove_post_type_support( bbp_get_topic_post_type(), 'revisions' ); } // Insert topic. $topic_id = wp_update_post( $topic_data ); // Toggle revisions back on. if ( true === $revisions_removed ) { $revisions_removed = false; add_post_type_support( bbp_get_topic_post_type(), 'revisions' ); } if ( empty ( $topic_id ) || is_wp_error( $topic_id ) ) { $append_error = ( ( is_wp_error( $topic_id ) && $topic_id ->get_error_message() ) ? __( 'The following problem(s) have been found with your topic: ' , 'buddyboss' ) . $topic_id ->get_error_message() . __( 'Please try again.' , 'buddyboss' ) : __( 'We are facing a problem to update a topic.' , 'buddyboss' ) ); return new WP_Error( 'bp_rest_bbp_topic_error' , $append_error , array ( 'status' => 400, ) ); } // Update counts, etc... do_action( 'bbp_edit_topic' , $topic_id , $forum_id , $anonymous_data , $topic_author , true /* Is edit */ ); /** Revisions */ // Revision Reason. if ( ! empty ( $topic_new ->bbp_topic_edit_reason ) ) { $topic_edit_reason = esc_attr( wp_strip_all_tags( $topic_new ->bbp_topic_edit_reason ) ); } // Update revision log. if ( ! empty ( $topic_new ->bbp_log_topic_edit ) && ( true === $topic_new ->bbp_log_topic_edit ) ) { $revision_id = wp_save_post_revision( $topic_id ); if ( ! empty ( $revision_id ) ) { bbp_update_topic_revision_log( array ( 'topic_id' => $topic_id , 'revision_id' => $revision_id , 'author_id' => bbp_get_current_user_id(), 'reason' => $topic_edit_reason , ) ); } } /** Move Topic */ // If the new forum id is not equal to the old forum id, run the. // bbp_move_topic action and pass the topic's forum id as the. // first arg and topic id as the second to update counts. if ( $forum_id !== $topic ->post_parent ) { bbp_move_topic_handler( $topic_id , $topic ->post_parent, $forum_id ); } /** Stickies */ if ( ! empty ( $topic_new ->bbp_stick_topic ) && in_array( $topic_new ->bbp_stick_topic, array_keys ( bbp_get_topic_types() ), true ) ) { // What's the caps? if ( current_user_can( 'moderate' ) ) { // What's the haps? switch ( $topic_new ->bbp_stick_topic ) { // Sticky in forum. case 'stick' : bbp_stick_topic( $topic_id ); break ; // Sticky in all forums. case 'super' : bbp_stick_topic( $topic_id , true ); break ; // Normal. case 'unstick' : default : bbp_unstick_topic( $topic_id ); break ; } } } // Handle Subscription Checkbox. if ( bbp_is_subscriptions_active() ) { $author_id = bbp_get_user_id( 0, true, true ); // Check if subscribed. $subscribed = bbp_is_user_subscribed( $author_id , $topic_id ); // Subscribed and unsubscribing. if ( true === $subscribed && empty ( $topic_new ->bbp_topic_subscription ) ) { bbp_remove_user_subscription( $author_id , $topic_id ); // Not subscribed and subscribing. } elseif ( false === $subscribed && ! empty ( $topic_new ->bbp_topic_subscription ) ) { bbp_add_user_subscription( $author_id , $topic_id ); } } /** Additional Actions (After Save) */ do_action( 'bbp_edit_topic_post_extras' , $topic_id ); $topic = get_post( $topic_id ); $fields_update = $this ->update_additional_fields_for_object( $topic , $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update ; } $retval = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $topic , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a topic is updated and fetched via the REST API. * * @param array $topic Updated topic. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_topic_update_item' , $topic , $response , $request ); return $response ; } |
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.