BP_Media
Database interaction class for the BuddyBoss media component.
Description
Instance methods are available for creating/editing an media, static methods for querying media.
Source
File: bp-media/classes/class-bp-media.php
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 | class BP_Media { /** Properties ************************************************************/ /** * ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $id ; /** * Blog ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $blog_id ; /** * Attachment ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $attachment_id ; /** * User ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $user_id ; /** * Title of the media item. * * @since BuddyBoss 1.0.0 * @var string */ var $title ; /** * Album ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $album_id ; /** * Activity ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $activity_id ; /** * Group ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $group_id ; /** * Privacy of the media item. * * @since BuddyBoss 1.0.0 * @var string */ var $privacy ; /** * Menu order of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $menu_order ; /** * Upload date of the media item. * * @since BuddyBoss 1.0.0 * @var string */ var $date_created ; /** * Error holder. * * @since BuddyBoss 1.0.0 * * @var WP_Error */ public $errors ; /** * Error type to return. Either 'bool' or 'wp_error'. * * @since BuddyBoss 1.0.0 * * @var string */ public $error_type = 'bool' ; /** * Constructor method. * * @since BuddyBoss 1.0.0 * * @param int|bool $id Optional. The ID of a specific activity item. */ function __construct( $id = false ) { // Instantiate errors object. $this ->errors = new WP_Error; if ( ! empty ( $id ) ) { $this ->id = (int) $id ; $this ->populate(); } } /** * Populate the object with data about the specific media item. * * @since BuddyBoss 1.0.0 */ public function populate() { global $wpdb ; $row = wp_cache_get( $this ->id, 'bp_media' ); if ( false === $row ) { $bp = buddypress(); $row = $wpdb ->get_row( $wpdb ->prepare( "SELECT * FROM {$bp->media->table_name} WHERE id = %d" , $this ->id ) ); wp_cache_set( $this ->id, $row , 'bp_media' ); } if ( empty ( $row ) ) { $this ->id = 0; return ; } $this ->id = (int) $row ->id; $this ->blog_id = (int) $row ->blog_id; $this ->attachment_id = (int) $row ->attachment_id; $this ->user_id = (int) $row ->user_id; $this ->title = $row ->title; $this ->album_id = (int) $row ->album_id; $this ->activity_id = (int) $row ->activity_id; $this ->group_id = (int) $row ->group_id; $this ->privacy = $row ->privacy; $this ->menu_order = (int) $row ->menu_order; $this ->date_created = $row ->date_created; } /** * Save the media item to the database. * * @since BuddyBoss 1.0.0 * * @return WP_Error|bool True on success. */ public function save() { global $wpdb ; $bp = buddypress(); $this ->id = apply_filters_ref_array( 'bp_media_id_before_save' , array ( $this ->id, & $this ) ); $this ->blog_id = apply_filters_ref_array( 'bp_media_blog_id_before_save' , array ( $this ->blog_id, & $this ) ); $this ->attachment_id = apply_filters_ref_array( 'bp_media_attachment_id_before_save' , array ( $this ->attachment_id, & $this ) ); $this ->user_id = apply_filters_ref_array( 'bp_media_user_id_before_save' , array ( $this ->user_id, & $this ) ); $this ->title = apply_filters_ref_array( 'bp_media_title_before_save' , array ( $this ->title, & $this ) ); $this ->album_id = apply_filters_ref_array( 'bp_media_album_id_before_save' , array ( $this ->album_id, & $this ) ); $this ->activity_id = apply_filters_ref_array( 'bp_media_activity_id_before_save' , array ( $this ->activity_id, & $this ) ); $this ->group_id = apply_filters_ref_array( 'bp_media_group_id_before_save' , array ( $this ->group_id, & $this ) ); $this ->privacy = apply_filters_ref_array( 'bp_media_privacy_before_save' , array ( $this ->privacy, & $this ) ); $this ->menu_order = apply_filters_ref_array( 'bp_media_menu_order_before_save' , array ( $this ->menu_order, & $this ) ); $this ->date_created = apply_filters_ref_array( 'bp_media_date_created_before_save' , array ( $this ->date_created, & $this ) ); /** * Fires before the current media item gets saved. * * Please use this hook to filter the properties above. Each part will be passed in. * * @since BuddyBoss 1.0.0 * * @param BP_Media $this Current instance of the media item being saved. Passed by reference. */ do_action_ref_array( 'bp_media_before_save' , array ( & $this ) ); if ( 'wp_error' === $this ->error_type && $this ->errors->get_error_code() ) { return $this ->errors; } if ( empty ( $this ->attachment_id ) //|| empty( $this->activity_id ) //todo: when forums media is saving, it should have activity id assigned if settings enabled need to check this ) { if ( 'bool' === $this ->error_type ) { return false; } else { if ( empty ( $this ->activity_id ) ) { $this ->errors->add( 'bp_media_missing_activity' ); } else { $this ->errors->add( 'bp_media_missing_attachment' ); } return $this ->errors; } } // If we have an existing ID, update the media item, otherwise insert it. if ( ! empty ( $this ->id ) ) { $q = $wpdb ->prepare( "UPDATE {$bp->media->table_name} SET blog_id = %d, attachment_id = %d, user_id = %d, title = %s, album_id = %d, activity_id = %d, group_id = %d, privacy = %s, menu_order = %d, date_created = %s WHERE id = %d" , $this ->blog_id, $this ->attachment_id, $this ->user_id, $this ->title, $this ->album_id, $this ->activity_id, $this ->group_id, $this ->privacy, $this ->menu_order, $this ->date_created, $this ->id ); } else { $q = $wpdb ->prepare( "INSERT INTO {$bp->media->table_name} ( blog_id, attachment_id, user_id, title, album_id, activity_id, group_id, privacy, menu_order, date_created ) VALUES ( %d, %d, %d, %s, %d, %d, %d, %s, %d, %s )" , $this ->blog_id, $this ->attachment_id, $this ->user_id, $this ->title, $this ->album_id, $this ->activity_id, $this ->group_id, $this ->privacy, $this ->menu_order, $this ->date_created ); } if ( false === $wpdb ->query( $q ) ) { return false; } // If this is a new media item, set the $id property. if ( empty ( $this ->id ) ) { $this ->id = $wpdb ->insert_id; } /** * Fires after an media item has been saved to the database. * * @since BuddyBoss 1.0.0 * * @param BP_Media $this Current instance of media item being saved. Passed by reference. */ do_action_ref_array( 'bp_media_after_save' , array ( & $this ) ); return true; } /** Static Methods ***************************************************/ /** * Get media items, as specified by parameters. * * @since BuddyBoss 1.0.0 * * @param array $args { * An array of arguments. All items are optional. * @type int $page Which page of results to fetch. Using page=1 without per_page will result * in no pagination. Default: 1. * @type int|bool $per_page Number of results per page. Default: 20. * @type int|bool $max Maximum number of results to return. Default: false (unlimited). * @type string $fields Media fields to return. Pass 'ids' to get only the media IDs. * 'all' returns full media objects. * @type string $sort ASC or DESC. Default: 'DESC'. * @type string $order_by Column to order results by. * @type array $exclude Array of media IDs to exclude. Default: false. * @type string $search_terms Limit results by a search term. Default: false. * @type string|bool $count_total If true, an additional DB query is run to count the total media items * for the query. Default: false. * } * @return array The array returned has two keys: * - 'total' is the count of located medias * - 'medias' is an array of the located medias */ public static function get( $args = array () ) { global $wpdb ; $bp = buddypress(); $r = wp_parse_args( $args , array ( 'page' => 1, // The current page. 'per_page' => 20, // Media items per page. 'max' => false, // Max number of items to return. 'fields' => 'all' , // Fields to include. 'sort' => 'DESC' , // ASC or DESC. 'order_by' => 'date_created' , // Column to order by. 'exclude' => false, // Array of ids to exclude. 'in' => false, // Array of ids to limit query by (IN). 'search_terms' => false, // Terms to search by. 'privacy' => false, // public, loggedin, onlyme, friends, grouponly, message. 'count_total' => false, // Whether or not to use count_total. ) ); // Select conditions. $select_sql = "SELECT DISTINCT m.id" ; $from_sql = " FROM {$bp->media->table_name} m" ; $join_sql = '' ; // Where conditions. $where_conditions = array (); // Searching. if ( $r [ 'search_terms' ] ) { $search_terms_like = '%' . bp_esc_like( $r [ 'search_terms' ] ) . '%' ; $where_conditions [ 'search_sql' ] = $wpdb ->prepare( 'm.title LIKE %s' , $search_terms_like ); /** * Filters whether or not to include users for search parameters. * * @since BuddyBoss 1.0.0 * * @param bool $value Whether or not to include user search. Default false. */ if ( apply_filters( 'bp_media_get_include_user_search' , false ) ) { $user_search = get_user_by( 'slug' , $r [ 'search_terms' ] ); if ( false !== $user_search ) { $user_id = $user_search ->ID; $where_conditions [ 'search_sql' ] .= $wpdb ->prepare( ' OR m.user_id = %d' , $user_id ); } } } // Sorting. $sort = $r [ 'sort' ]; if ( $sort != 'ASC' && $sort != 'DESC' ) { $sort = 'DESC' ; } switch ( $r [ 'order_by' ] ) { case 'id' : case 'user_id' : case 'blog_id' : case 'attachment_id' : case 'title' : case 'album_id' : case 'activity_id' : case 'group_id' : case 'menu_order' : break ; default : $r [ 'order_by' ] = 'date_created' ; break ; } $order_by = 'm.' . $r [ 'order_by' ]; // Exclude specified items. if ( ! empty ( $r [ 'exclude' ] ) ) { $exclude = implode( ',' , wp_parse_id_list( $r [ 'exclude' ] ) ); $where_conditions [ 'exclude' ] = "m.id NOT IN ({$exclude})" ; } // The specific ids to which you want to limit the query. if ( ! empty ( $r [ 'in' ] ) ) { $in = implode( ',' , wp_parse_id_list( $r [ 'in' ] ) ); $where_conditions [ 'in' ] = "m.id IN ({$in})" ; // we want to disable limit query when include media ids $r [ 'page' ] = false; $r [ 'per_page' ] = false; } if ( ! empty ( $r [ 'activity_id' ] ) ) { $where_conditions [ 'activity' ] = "m.activity_id = {$r['activity_id']}" ; } // existing-media check to query media which has no albums assigned if ( ! empty ( $r [ 'album_id' ] ) && 'existing-media' != $r [ 'album_id' ] ) { $where_conditions [ 'album' ] = "m.album_id = {$r['album_id']}" ; } else if ( ! empty ( $r [ 'album_id' ] ) && 'existing-media' == $r [ 'album_id' ] ) { $where_conditions [ 'album' ] = "m.album_id = 0" ; } if ( ! empty ( $r [ 'user_id' ] ) ) { $where_conditions [ 'user' ] = "m.user_id = {$r['user_id']}" ; } if ( ! empty ( $r [ 'group_id' ] ) ) { $where_conditions [ 'user' ] = "m.group_id = {$r['group_id']}" ; } if ( ! empty ( $r [ 'privacy' ] ) ) { $privacy = "'" . implode ( "', '" , $r ['privacy '] ) . "' "; $where_conditions [ 'privacy' ] = "m.privacy IN ({$privacy})" ; } /** * Filters the MySQL WHERE conditions for the Media items get method. * * @since BuddyBoss 1.0.0 * * @param array $where_conditions Current conditions for MySQL WHERE statement. * @param array $r Parsed arguments passed into method. * @param string $select_sql Current SELECT MySQL statement at point of execution. * @param string $from_sql Current FROM MySQL statement at point of execution. * @param string $join_sql Current INNER JOIN MySQL statement at point of execution. */ $where_conditions = apply_filters( 'bp_media_get_where_conditions' , $where_conditions , $r , $select_sql , $from_sql , $join_sql ); if ( empty ( $where_conditions ) ) { $where_conditions [ '2' ] = '2' ; } // Join the where conditions together. $where_sql = 'WHERE ' . join( ' AND ' , $where_conditions ); /** * Filter the MySQL JOIN clause for the main media query. * * @since BuddyBoss 1.0.0 * * @param string $join_sql JOIN clause. * @param array $r Method parameters. * @param string $select_sql Current SELECT MySQL statement. * @param string $from_sql Current FROM MySQL statement. * @param string $where_sql Current WHERE MySQL statement. */ $join_sql = apply_filters( 'bp_media_get_join_sql' , $join_sql , $r , $select_sql , $from_sql , $where_sql ); // Sanitize page and per_page parameters. $page = absint( $r [ 'page' ] ); $per_page = absint( $r [ 'per_page' ] ); $retval = array ( 'medias' => null, 'total' => null, 'has_more_items' => null, ); // Query first for media IDs. $media_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY {$order_by} {$sort}, m.id {$sort}" ; if ( ! empty ( $per_page ) && ! empty ( $page ) ) { // We query for $per_page + 1 items in order to // populate the has_more_items flag. $media_ids_sql .= $wpdb ->prepare( " LIMIT %d, %d" , absint( ( $page - 1 ) * $per_page ), $per_page + 1 ); } /** * Filters the paged media MySQL statement. * * @since BuddyBoss 1.0.0 * * @param string $media_ids_sql MySQL statement used to query for Media IDs. * @param array $r Array of arguments passed into method. */ $media_ids_sql = apply_filters( 'bp_media_paged_activities_sql' , $media_ids_sql , $r ); $cache_group = 'bp_media' ; $cached = bp_core_get_incremented_cache( $media_ids_sql , $cache_group ); if ( false === $cached ) { $media_ids = $wpdb ->get_col( $media_ids_sql ); bp_core_set_incremented_cache( $media_ids_sql , $cache_group , $media_ids ); } else { $media_ids = $cached ; } $retval [ 'has_more_items' ] = ! empty ( $per_page ) && count ( $media_ids ) > $per_page ; // If we've fetched more than the $per_page value, we // can discard the extra now. if ( ! empty ( $per_page ) && count ( $media_ids ) === $per_page + 1 ) { array_pop ( $media_ids ); } if ( 'ids' === $r [ 'fields' ] ) { $medias = array_map ( 'intval' , $media_ids ); } else { $medias = self::get_media_data( $media_ids ); } if ( 'ids' !== $r [ 'fields' ] ) { // Get the fullnames of users so we don't have to query in the loop. //$medias = self::append_user_fullnames( $medias ); // Pre-fetch data associated with media users and other objects. $medias = BP_Media::prefetch_object_data( $medias ); } $retval [ 'medias' ] = $medias ; // If $max is set, only return up to the max results. if ( ! empty ( $r [ 'count_total' ] ) ) { /** * Filters the total media MySQL statement. * * @since BuddyBoss 1.0.0 * * @param string $value MySQL statement used to query for total medias. * @param string $where_sql MySQL WHERE statement portion. * @param string $sort Sort direction for query. */ $total_medias_sql = apply_filters( 'bp_media_total_medias_sql' , "SELECT count(DISTINCT m.id) FROM {$bp->media->table_name} m {$join_sql} {$where_sql}" , $where_sql , $sort ); $cached = bp_core_get_incremented_cache( $total_medias_sql , $cache_group ); if ( false === $cached ) { $total_medias = $wpdb ->get_var( $total_medias_sql ); bp_core_set_incremented_cache( $total_medias_sql , $cache_group , $total_medias ); } else { $total_medias = $cached ; } if ( ! empty ( $r [ 'max' ] ) ) { if ( (int) $total_medias > (int) $r [ 'max' ] ) { $total_medias = $r [ 'max' ]; } } $retval [ 'total' ] = $total_medias ; } return $retval ; } /** * Convert media IDs to media objects, as expected in template loop. * * @since BuddyBoss 1.0.0 * * @param array $media_ids Array of media IDs. * @return array */ protected static function get_media_data( $media_ids = array () ) { global $wpdb ; // Bail if no media ID's passed. if ( empty ( $media_ids ) ) { return array (); } // Get BuddyPress. $bp = buddypress(); $medias = array (); $uncached_ids = bp_get_non_cached_ids( $media_ids , 'bp_media' ); // Prime caches as necessary. if ( ! empty ( $uncached_ids ) ) { // Format the media ID's for use in the query below. $uncached_ids_sql = implode( ',' , wp_parse_id_list( $uncached_ids ) ); // Fetch data from media table, preserving order. $queried_adata = $wpdb ->get_results( "SELECT * FROM {$bp->media->table_name} WHERE id IN ({$uncached_ids_sql})" ); // Put that data into the placeholders created earlier, // and add it to the cache. foreach ( ( array ) $queried_adata as $adata ) { wp_cache_set( $adata ->id, $adata , 'bp_media' ); } } // Now fetch data from the cache. foreach ( $media_ids as $media_id ) { // Integer casting. $media = wp_cache_get( $media_id , 'bp_media' ); if ( ! empty ( $media ) ) { $media ->id = (int) $media ->id; $media ->blog_id = (int) $media ->blog_id; $media ->user_id = (int) $media ->user_id; $media ->attachment_id = (int) $media ->attachment_id; $media ->album_id = (int) $media ->album_id; $media ->activity_id = (int) $media ->activity_id; $media ->group_id = (int) $media ->group_id; $media ->menu_order = (int) $media ->menu_order; //fetch attachment data $attachment_data = new stdClass(); $attachment_data ->full = wp_get_attachment_image_url( $media ->attachment_id, 'full' ); $attachment_data ->thumb = wp_get_attachment_image_url( $media ->attachment_id, 'bp-media-thumbnail' ); $attachment_data ->activity_thumb = wp_get_attachment_image_url( $media ->attachment_id, 'bp-activity-media-thumbnail' ); $attachment_data ->meta = wp_get_attachment_metadata( $media ->attachment_id ); $media ->attachment_data = $attachment_data ; } $medias [] = $media ; } // Then fetch user data. $user_query = new BP_User_Query( array ( 'user_ids' => wp_list_pluck( $medias , 'user_id' ), 'populate_extras' => false, ) ); // Associated located user data with media items. foreach ( $medias as $a_index => $a_item ) { $a_user_id = intval ( $a_item ->user_id ); $a_user = isset( $user_query ->results[ $a_user_id ] ) ? $user_query ->results[ $a_user_id ] : '' ; if ( ! empty ( $a_user ) ) { $medias [ $a_index ]->user_email = $a_user ->user_email; $medias [ $a_index ]->user_nicename = $a_user ->user_nicename; $medias [ $a_index ]->user_login = $a_user ->user_login; $medias [ $a_index ]->display_name = $a_user ->display_name; } } return $medias ; } /** * Append xProfile fullnames to an media array. * * @since BuddyBoss 1.0.0 * * @param array $medias Medias array. * @return array */ protected static function append_user_fullnames( $medias ) { if ( bp_is_active( 'xprofile' ) && ! empty ( $medias ) ) { $media_user_ids = wp_list_pluck( $medias , 'user_id' ); if ( ! empty ( $media_user_ids ) ) { $fullnames = bp_core_get_user_displaynames( $media_user_ids ); if ( ! empty ( $fullnames ) ) { foreach ( ( array ) $medias as $i => $media ) { if ( ! empty ( $fullnames [ $media ->user_id ] ) ) { $medias [ $i ]->user_fullname = $fullnames [ $media ->user_id ]; } } } } } return $medias ; } /** * Pre-fetch data for objects associated with media items. * * Media items are associated with users, and often with other * BuddyPress data objects. Here, we pre-fetch data about these * associated objects, so that inline lookups - done primarily when * building action strings - do not result in excess database queries. * * @since BuddyBoss 1.0.0 * * @param array $medias Array of media. * @return array $medias Array of media. */ protected static function prefetch_object_data( $medias ) { /** * Filters inside prefetch_object_data method to aid in pre-fetching object data associated with media item. * * @since BuddyBoss 1.0.0 * * @param array $medias Array of media. */ return apply_filters( 'bp_media_prefetch_object_data' , $medias ); } /** * Delete media items from the database. * * To delete a specific media item, pass an 'id' parameter. * Otherwise use the filters. * * @since BuddyBoss 1.0.0 * * @param array $args { * @int $id Optional. The ID of a specific item to delete. * @int $blog_id Optional. The blog ID to filter by. * @int $attachment_id Optional. The attachment ID to filter by. * @int $user_id Optional. The user ID to filter by. * @string $title Optional. The title to filter by. * @int $album_id Optional. The album ID to filter by. * @int $activity_id Optional. The activity ID to filter by. * @int $group_id Optional. The group ID to filter by. * @string $privacy Optional. The privacy to filter by. * @string $date_created Optional. The date to filter by. * } * * @return array|bool An array of deleted media IDs on success, false on failure. */ public static function delete ( $args = array () ) { global $wpdb ; $bp = buddypress(); $r = wp_parse_args( $args , array ( 'id' => false, 'blog_id' => false, 'attachment_id' => false, 'user_id' => false, 'title' => false, 'album_id' => false, 'activity_id' => false, 'group_id' => false, 'privacy' => false, 'date_created' => false, ) ); // Setup empty array from where query arguments. $where_args = array (); // ID. if ( ! empty ( $r [ 'id' ] ) ) { $where_args [] = $wpdb ->prepare( "id = %d" , $r [ 'id' ] ); } // blog ID. if ( ! empty ( $r [ 'blog_id' ] ) ) { $where_args [] = $wpdb ->prepare( "blog_id = %d" , $r [ 'blog_id' ] ); } // attachment ID. if ( ! empty ( $r [ 'attachment_id' ] ) ) { $where_args [] = $wpdb ->prepare( "attachment_id = %d" , $r [ 'attachment_id' ] ); } // User ID. if ( ! empty ( $r [ 'user_id' ] ) ) { $where_args [] = $wpdb ->prepare( "user_id = %d" , $r [ 'user_id' ] ); } // title. if ( ! empty ( $r [ 'title' ] ) ) { $where_args [] = $wpdb ->prepare( "title = %s" , $r [ 'title' ] ); } // album ID. if ( ! empty ( $r [ 'album_id' ] ) ) { $where_args [] = $wpdb ->prepare( "album_id = %d" , $r [ 'album_id' ] ); } // activity ID. if ( ! empty ( $r [ 'activity_id' ] ) ) { $where_args [] = $wpdb ->prepare( "activity_id = %d" , $r [ 'activity_id' ] ); } // group ID. if ( ! empty ( $r [ 'group_id' ] ) ) { $where_args [] = $wpdb ->prepare( "group_id = %d" , $r [ 'group_id' ] ); } // privacy. if ( ! empty ( $r [ 'privacy' ] ) ) { $where_args [] = $wpdb ->prepare( "privacy = %s" , $r [ 'privacy' ] ); } // Date created. if ( ! empty ( $r [ 'date_created' ] ) ) { $where_args [] = $wpdb ->prepare( "date_created = %s" , $r [ 'date_created' ] ); } // Bail if no where arguments. if ( empty ( $where_args ) ) { return false; } // Join the where arguments for querying. $where_sql = 'WHERE ' . join( ' AND ' , $where_args ); // Fetch all media being deleted so we can perform more actions. $medias = $wpdb ->get_results( "SELECT * FROM {$bp->media->table_name} {$where_sql}" ); /** * Action to allow intercepting media items to be deleted. * * @since BuddyBoss 1.0.0 * * @param array $medias Array of media. * @param array $r Array of parsed arguments. */ do_action_ref_array( 'bp_media_before_delete' , array ( $medias , $r ) ); // Attempt to delete media from the database. $deleted = $wpdb ->query( "DELETE FROM {$bp->media->table_name} {$where_sql}" ); // Bail if nothing was deleted. if ( empty ( $deleted ) ) { return false; } /** * Action to allow intercepting media items just deleted. * * @since BuddyBoss 1.0.0 * * @param array $medias Array of media. * @param array $r Array of parsed arguments. */ do_action_ref_array( 'bp_media_after_delete' , array ( $medias , $r ) ); // Pluck the media IDs out of the $medias array. $media_ids = wp_parse_id_list( wp_list_pluck( $medias , 'id' ) ); $activity_ids = wp_parse_id_list( wp_list_pluck( $medias , 'activity_id' ) ); $attachment_ids = wp_parse_id_list( wp_list_pluck( $medias , 'attachment_id' ) ); // Handle accompanying attachments and meta deletion. if ( ! empty ( $attachment_ids ) ) { // Loop through attachment ids and attempt to delete. foreach ( $attachment_ids as $attachment_id ) { if ( bp_is_active( 'activity' ) ) { $parent_activity_id = get_post_meta( $attachment_id , 'bp_media_parent_activity_id' , true ); if ( ! empty ( $parent_activity_id ) ) { $activity_media_ids = bp_activity_get_meta( $parent_activity_id , 'bp_media_ids' , true ); if ( ! empty ( $activity_media_ids ) ) { $activity_media_ids = explode ( ',' , $activity_media_ids ); $activity_media_ids = array_diff ( $activity_media_ids , $media_ids ); if ( ! empty ( $activity_media_ids ) ) { $activity_media_ids = implode( ',' , $activity_media_ids ); bp_activity_update_meta( $parent_activity_id , 'bp_media_ids' , $activity_media_ids ); } else { $activity_ids [] = $parent_activity_id ; } } } } wp_delete_post( $attachment_id , true ); } } // delete related activity if ( ! empty ( $activity_ids ) && bp_is_active( 'activity' ) ) { foreach ( $activity_ids as $activity_id ) { $activity = new BP_Activity_Activity( (int) $activity_id ); // Check access. if ( bp_activity_user_can_delete( $activity ) ) { /** This action is documented in bp-activity/bp-activity-actions.php */ do_action( 'bp_activity_before_action_delete_activity' , $activity ->id, $activity ->user_id ); // Deleting an activity comment. if ( 'activity_comment' == $activity ->type ) { if ( bp_activity_delete_comment( $activity ->item_id, $activity ->id ) ) { /** This action is documented in bp-activity/bp-activity-actions.php */ do_action( 'bp_activity_action_delete_activity' , $activity ->id, $activity ->user_id ); } // Deleting an activity. } else { if ( bp_activity_delete( array ( 'id' => $activity ->id, 'user_id' => $activity ->user_id ) ) ) { /** This action is documented in bp-activity/bp-activity-actions.php */ do_action( 'bp_activity_action_delete_activity' , $activity ->id, $activity ->user_id ); } } } } } return $media_ids ; } /** * Count total media for the given user * * @since BuddyBoss 1.0.0 * * @param int $user_id * * @return array|bool|int */ public static function total_media_count( $user_id = 0 ) { global $wpdb ; $bp = buddypress(); $privacy = array ( 'public' ); if ( is_user_logged_in() ) { $privacy [] = 'loggedin' ; if ( bp_is_active( 'friends' ) ) { $is_friend = friends_check_friendship( get_current_user_id(), $user_id ); if ( $is_friend ) { $privacy [] = 'friends' ; } } } $privacy = "'" . implode( "', '" , $privacy ) . "'" ; $count_sql = "SELECT COUNT(*) FROM {$bp->media->table_name} WHERE user_id = {$user_id} AND privacy IN ({$privacy})" ; $cache_group = 'bp_media_user_media_count' ; $cached = bp_core_get_incremented_cache( $count_sql , $cache_group ); if ( false === $cached ) { $results = $wpdb ->get_col( $count_sql ); $total_count = intval ( $results [0] ); bp_core_set_incremented_cache( $count_sql , $cache_group , $total_count ); } else { $total_count = $cached ; } return $total_count ; } /** * Count total media for the given group * * @since BuddyBoss 1.0.0 * * @param int $group_id * * @return array|bool|int */ public static function total_group_media_count( $group_id = 0 ) { global $wpdb ; $bp = buddypress(); $count_sql = "SELECT COUNT(*) FROM {$bp->media->table_name} WHERE group_id = {$group_id}" ; $cache_group = 'bp_media_group_media_count' ; $cached = bp_core_get_incremented_cache( $count_sql , $cache_group ); if ( false === $cached ) { $results = $wpdb ->get_col( $count_sql ); $total_count = intval ( $results [0] ); bp_core_set_incremented_cache( $count_sql , $cache_group , $total_count ); } else { $total_count = $cached ; } return $total_count ; } /** * Get all media ids for the album * * @since BuddyBoss 1.0.0 * @param bool $album_id * * @return array|bool */ public static function get_album_media_ids( $album_id = false ) { if ( ! $album_id ) { return false; } global $wpdb ; $bp = buddypress(); // Select conditions. $select_sql = "SELECT DISTINCT m.id FROM {$bp->media->table_name} m WHERE m.album_id = {$album_id}" ; $cache_group = 'bp_media_album_media_ids' ; $cached = bp_core_get_incremented_cache( $select_sql , $cache_group ); if ( false === $cached ) { $media_ids = $wpdb ->get_col( $select_sql ); bp_core_set_incremented_cache( $select_sql , $cache_group , $media_ids ); } else { $media_ids = $cached ; } return $media_ids ; } } |
Changelog
Version | Description |
---|---|
BuddyPress 1.0.0 | Introduced. |
Methods
- __construct — Constructor method.
- append_user_fullnames — Append xProfile fullnames to an media array.
- delete — Delete media items from the database.
- get — Get media items, as specified by parameters.
- get_activity_attachment_id — Get media attachment id for the activity.
- get_activity_media_id — Get media id for the activity.
- get_album_media_ids — Get all media ids for the album
- get_in_operator_sql — Create SQL IN clause for filter queries.
- get_media_data — Convert media IDs to media objects, as expected in template loop.
- get_scope_query_sql — Get the SQL for the 'scope' param in BP_Media::get().
- populate — Populate the object with data about the specific media item.
- prefetch_object_data — Pre-fetch data for objects associated with media items.
- save — Save the media item to the database.
- total_group_media_count — Count total media for the given group
- total_media_count — Count total media for the given user
- total_user_group_media_count — Count total groups media for the given user.
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.