$label ) { $privacy_filters[ $option ] = array( 'text' => $label, 'priority' => $priority, 'allow_multiple' => false, ); // Make it possible to attach a file to more than one object if ( 'groups' === $option || 'members' === $option ) { $privacy_filters[ $option ]['allow_multiple'] = true; if ( 'members' === $option ) { $privacy_filters[ $option ]['autocomplete_placeholder'] = __( 'Start typing a member name', 'buddydrive' ); } else { $privacy_filters[ $option ]['autocomplete_placeholder'] = __( 'Start typing a group name', 'buddydrive' ); } } $priority += 5; } $privacy_filters[ 'folder' ] = array( 'text' => __( 'Add to an existing folder', 'buddydrive' ), 'priority' => $priority, 'allow_multiple' => false, 'autocomplete_placeholder' => __( 'Start typing a folder name', 'buddydrive' ) ); $l10n_array['settings'] = array( 'buddydrive_scope' => buddydrive_get_current_scope(), 'privacy_filters' => $privacy_filters, 'loop_filters' => array( 'modified' => array( 'text' => __( 'Last edit', 'buddydrive' ), 'priority' => 5, ), 'title' => array( 'text' => __( 'Name', 'buddydrive' ), 'priority' => 10, ), ), 'nonces' => array( 'fetch_items' => wp_create_nonce( 'buddydrive_fetch_items' ), 'fetch_objects' => wp_create_nonce( 'buddydrive_fetch_objects' ), 'update_item' => wp_create_nonce( 'buddydrive_update_item' ), 'new_folder' => wp_create_nonce( 'buddydrive_new_folder' ), 'bulk_edit' => wp_create_nonce( 'buddydrive_bulk_edit' ), 'user_stats' => wp_create_nonce( 'buddydrive_user_stats' ), ), ); $l10n_array['strings'] = array( 'allCrumb' => __( 'All', 'buddydrive' ), 'loadMore' => __( 'Load More', 'buddydrive' ), 'privacyFilterLabel' => __( 'Select your privacy preferences.', 'buddydrive' ), 'passwordInputLabel' => __( 'Define your password.', 'buddydrive' ), 'loopFilterLabel' => __( 'Order items by:', 'buddydrive' ), 'editErrors' => array( 'title' => __( 'Please make sure to provide a name for your file or folder.', 'buddydrive' ), 'groups' => __( 'Please make sure to select a group using the autocomplete field.', 'buddydrive' ), 'members' => __( 'Please make sure to select a member using the autocomplete field.', 'buddydrive' ), 'folder' => __( 'Please make sure to select a folder using the autocomplete field.', 'buddydrive' ), 'password' => __( 'Please make sure to provide a password for your file or folder.', 'buddydrive' ), ), 'saveEdits' => __( 'Saving your changes, please wait.', 'buddydrive' ), ); if ( buddydrive_current_user_can( 'buddydrive_upload' ) ) { $l10n_array['settings']['manage_toolbar'] = array( 'new_file' => array( 'id' => 'new_file', 'text' => __( 'New File', 'buddydrive' ), 'dashicon' => 'welcome-add-page', ), 'new_folder' => array( 'id' => 'new_folder', 'text' => __( 'New Folder', 'buddydrive' ), 'dashicon' => 'portfolio', ), 'bulk' => array( 'id' => 'bulk', 'text' => __( 'Bulk Actions', 'buddydrive' ), 'dashicon' => 'forms', ), ); if ( bp_is_my_profile() ) { $l10n_array['settings']['manage_toolbar']['stats'] = array( 'id' => 'stats', 'text' => __( 'Stats', 'buddydrive' ), 'dashicon' => 'chart-bar', ); } $l10n_array['settings']['bulk_actions'] = array( 'delete' => array( 'id' => 'delete', 'text' => __( 'Delete selected items', 'buddydrive' ), ), 'remove' => array( 'id' => 'remove', 'text' => __( 'Remove selected files from folder', 'buddydrive' ), ), ); // In a group you can only remove from the group if ( bp_is_group() ) { $l10n_array['settings']['bulk_actions'] = array( 'remove' => array( 'id' => 'group_remove', 'text' => __( 'Remove selected items from the group', 'buddydrive' ), ), ); } $l10n_array['strings']['new_folder_name'] = __( 'Name of your folder', 'buddydrive' ); $l10n_array['strings']['new_folder_button'] = __( 'Create folder', 'buddydrive' ); } return apply_filters( 'buddydrive_localize_ui', $l10n_array ); } /** * Outputs the BuddyDrive 2.0 UI * * @since 2.0.0 * * @return string HTML Output */ function buddydrive_ui() { wp_localize_script( 'buddydrive-models-js', 'BuddyDrive_App', buddydrive_localize_ui() ); if ( ! buddydrive_current_user_can( 'buddydrive_upload' ) ) { // css front-end complementary styles if ( wp_style_is( 'buddydrive-front-end-style', 'registered' ) ) { wp_enqueue_style( 'buddydrive-front-end-style' ); } else { wp_enqueue_style( 'buddydrive-app-style' ); } wp_enqueue_script( 'buddydrive-app-js' ); } else { bp_attachments_enqueue_scripts( 'BuddyDrive_Attachment' ); } wp_add_inline_style( 'buddydrive-app-style', sprintf(' #buddydrive-main #buddydrive-loading, #buddydrive-main .buddydrive-stats-loading { background-image: url(%s); } ', esc_url( admin_url( 'images/spinner-2x.gif' ) ) ) ); // allways buddydrive_get_asset_template_part( 'index' ); } /** * Displays the user's BuddyDrive root url or a link to it * * @param boolean $user_id the id of the user * @uses buddydrive_get_user_buddydrive_url() to get the user's BuddyDrive url * @return string outputs the link to user's BuddyDrive */ function buddydrive_user_buddydrive_url( $linkonly = false ) { $url = buddydrive_get_user_buddydrive_url(); if( ! empty($linkonly ) ) { echo $url; } else { $output = apply_filters( 'buddydrive_user_buddydrive_url', ' ' . esc_html__( 'Manage files', 'buddydrive' ) .'' ); echo $output; } } /** * Builds the user's BuddyDrive root url * * @param integer $user_id the id of the user * @uses bp_displayed_user_id() to get the displayed user id * @uses bp_loggedin_user_id() to get the current user id * @uses bp_core_get_user_domain to get the user's home page link * @uses buddydrive_get_slug() to get the slug of BuddyDrive * @return string $buddydrive_link the link to user's BuddyDrive */ function buddydrive_get_user_buddydrive_url( $user_id = 0 ) { if ( empty( $user_id ) ) { $displayed_user_id = bp_displayed_user_id(); $user_id = !empty( $displayed_user_id ) ? $displayed_user_id : bp_loggedin_user_id(); } $user_domain = bp_core_get_user_domain( $user_id ); $buddydrive_link = trailingslashit( $user_domain . buddydrive_get_slug() ); return $buddydrive_link; } /** * Builds the BuddyDrive Group url * * @since 2.0.0 Add the User ID Parameter * * @param integer $group_id the group id * @param integer $user_id the User ID * * @return string $buddydrive_link the link to user's BuddyDrive */ function buddydrive_get_group_buddydrive_url( $group_id = 0, $user_id = 0 ) { $buddydrive_link = false; if ( bp_is_group() ) { $group = groups_get_current_group(); } elseif ( ! empty( $group_id ) ) { if ( is_array( $group_id ) ) { /** * Link to the user's BuddyDrive in case there is more than * one group. */ if ( ( count( $group_id ) > 1 && ! empty( $user_id ) ) || ! bp_is_active( 'groups' ) ) { return buddydrive_get_user_buddydrive_url( $user_id ); // Take the first ! } else { $group_id = reset( $group_id ); } } $group = groups_get_group( array( 'group_id' => $group_id ) ); } if ( ! empty( $group ) ) { $group_link = bp_get_group_permalink( $group ); $buddydrive_link = trailingslashit( $group_link . buddydrive_get_slug() ); } return $buddydrive_link; } /** * Builds the link to the Shared by friends BuddyDrive * * @param integer $user_id the id of the user * @uses bp_displayed_user_id() to get displayed user id * @uses bp_core_get_user_domain() to get the user's home page url * @uses buddydrive_get_slug() to get BuddyDrive slug * @uses buddydrive_get_friends_subnav_slug() to get BuddyDrive's friends subnav * @return string $buddydrive_friends the url to the shared by friends BuddyDrive */ function buddydrive_get_friends_buddydrive_url( $user_id = 0 ) { if ( empty( $user_id ) ) { $user_id = bp_displayed_user_id(); } $user_domain = bp_core_get_user_domain( $user_id ); $buddydrive_link = trailingslashit( $user_domain . buddydrive_get_slug() ); $buddydrive_friends = trailingslashit( $buddydrive_link . buddydrive_get_friends_subnav_slug() ); return $buddydrive_friends; } /** * Are we on a group's BuddyDrive ? * * @uses bp_is_groups_component() to check we're on the group component * @uses bp_is_single_item() to check we're in a single group * @uses bp_is_current_action() to check the acction is BuddyDrive * @return boolean true or false */ function buddydrive_is_group() { if ( bp_is_groups_component() && bp_is_single_item() && bp_is_current_action( buddydrive_get_slug() ) ) { return true; } else { return false; } } /** * Are we on current user's BuddyDrive * * @uses is_user_logged_in() to check we have a loggedin user * @uses bp_is_my_profile() to check the current user is on his profile * @uses bp_current_action() to check he's on his BuddyDrive * @return boolean true or false */ function buddydrive_is_user_buddydrive() { if ( is_user_logged_in() && bp_is_my_profile() && 'files' === bp_current_action() ) { return true; } else { return false; } } /** * Holds the variables we need while using ajax * * @return array the args to pass to the BuddyDrive Loop */ function buddydrive_querystring() { return apply_filters( 'buddydrive_querystring', array() ); } /** * Update a user's upload space * * @since 1.3.0 * * @param int $user_id the ID of the user * @param int $bytes the number of bytes to add to user's space * @return bool true on success, false otherwise */ function buddydrive_update_user_space( $user_id = 0, $bytes = 0 ) { if ( empty( $user_id ) || empty( $bytes ) ) { return false; } // Get the user's uploaded bytes $user_total_space = get_user_meta( $user_id, '_buddydrive_total_space', true ); if ( ! empty( $user_total_space ) ) { $user_total_space = intval( $user_total_space ) + intval( $bytes ); } else { $user_total_space = intval( $bytes ); } // no negative space! if ( $user_total_space < 0 ) { delete_user_meta( $user_id, '_buddydrive_total_space' ); // Update user's space } else { update_user_meta( $user_id, '_buddydrive_total_space', $user_total_space ); } return true; } /** * Get the space data for the requested user ID * * @since 2.0.0 * * @param int $user_id The user ID we need the space data for * @return array Associative array containg the space used in percent & diff */ function buddydrive_get_user_space_data( $user_id = 0 ) { if ( empty( $user_id ) ) { return 0; } $max_space = buddydrive_get_quota_by_user_id( $user_id ); $max_space = intval( $max_space ) * 1024 * 1024 ; $used_space = get_user_meta( $user_id, '_buddydrive_total_space', true ); $used_space = intval( $used_space ); return apply_filters( 'buddydrive_get_user_space_data', array( 'percent' => number_format( ( $used_space / $max_space ) * 100, 2 ), 'diff' => $max_space - $used_space, ) ); } /** * Upload a file * * @since 1.3.0 * * @param array $file the $_FILES var * @param int $user_id the ID of the user submitting the file * @return array the upload result */ function buddydrive_upload_item( $file = array(), $user_id = 0 ) { if ( empty( $file ) || empty( $user_id ) ) { return false; } // In multisite, we need to remove some filters if ( is_multisite() ) { remove_filter( 'upload_mimes', 'check_upload_mimes' ); remove_filter( 'upload_size_limit', 'upload_size_limit_filter' ); } // Accents can be problematic. add_filter( 'sanitize_file_name', 'remove_accents', 10, 1 ); $buddydrive_attachment = new BuddyDrive_Attachment(); $upload = $buddydrive_attachment->upload( $file ); // Restore/remove filters if ( is_multisite() ) { add_filter( 'upload_mimes', 'check_upload_mimes' ); add_filter( 'upload_size_limit', 'upload_size_limit_filter' ); } // Others can deal with Accents in filename the way they want. remove_filter( 'sanitize_file_name', 'remove_accents', 10, 1 ); $action_suffix = '_failed'; /** * file was uploaded !! * Now we can update the user's space */ if ( isset( $upload['file'] ) && empty( $upload['error'] ) ) { $action_suffix = '_succeeded'; buddydrive_update_user_space( $user_id, $file['buddyfile-upload']['size'] ); } /** * Allow actions once the file is processed * * Use buddydrive_upload_item_failed to do actions in case the file was not uploaded * Use buddydrive_upload_item_succeeded to do actions in case the file was uploaded * * @since 1.3 * * @param array $upload upload results * @param array $file the file before being moved to upload dir * @param int $user_id the ID of the user who uploaded the file. */ do_action( "buddydrive_upload_item{$action_suffix}", $upload, $file, $user_id ); return $upload; } /** * Saves or Updates a BuddyDrive item * * @param array $args the different argument of the item to save * @uses bp_loggedin_user_id() to default to current user id * @uses wp_parse_args() to merge defaults and args array * @uses BuddyDrive_Item::save() to save data in DB * @return int the item id */ function buddydrive_save_item( $args = '' ) { $defaults = array( 'id' => false, 'type' => '', 'user_id' => bp_loggedin_user_id(), 'parent_folder_id' => 0, 'title' => false, 'content' => false, 'mime_type' => false, 'guid' => false, 'metas' => false, ); $params = wp_parse_args( $args, $defaults ); // Setup item to be added $buddydrive_item = new BuddyDrive_Item(); $buddydrive_item->id = (int) $params['id']; $buddydrive_item->type = $params['type']; $buddydrive_item->user_id = (int) $params['user_id']; $buddydrive_item->parent_folder_id = (int) $params['parent_folder_id']; $buddydrive_item->title = $params['title']; $buddydrive_item->content = $params['content']; $buddydrive_item->mime_type = $params['mime_type']; $buddydrive_item->guid = $params['guid']; $buddydrive_item->metas = $params['metas']; if ( ! $buddydrive_item->save() ) { return false; } do_action( 'buddydrive_save_item', $buddydrive_item->id, $params ); return $buddydrive_item->id; } /** * Add an item (folder or file) to the database * * @since 2.0.0 * * @param array $args { * An array of arguments. * @type string $type Is this a folder or a file? Required. * @type int $user_id The user ID of the item owner. Defaults to current user. Required * @type int $parent_folder_id The parent folder ID. Defauls to 0. Optional. * @type string $title The name of the item. Required. * @type string $content The description of the item. Optional. * @type string $mime_type The mime type for the file ('folder' in case of a folder). Optional. * @type string $guid The URL to the item. Optional. * @type mixed $customs Custom data. Optional. * @type string $privacy The privacy of the item. Defaults to buddydrive_get_default_privacy(). Optional. * @type array $groups The list of group IDs the item is attached to (in case of a Groups privacy). Optional. * @type string $password The password to access to the item (in case of a Password privacy). Optional. * @type array $members The list of member IDs the item is attached to (in case of a Members privacy). Optional. * } * @return int The ID of the added item. */ function buddydrive_add_item( $args = array() ) { $params = bp_parse_args( $args, array( 'type' => '', 'user_id' => bp_loggedin_user_id(), 'parent_folder_id' => 0, 'title' => '', 'content' => '', 'mime_type' => false, 'guid' => '', 'customs' => false, 'privacy' => 'private', 'groups' => array(), 'password' => '', 'members' => array(), ), 'buddydrive_add_item' ); if ( empty( $params['type'] ) || empty( $params['title'] ) || empty( $params['user_id'] ) ) { return false; } // Init meta $meta = new stdClass(); $default_privacy = buddydrive_get_default_privacy(); // Defaults to private if ( empty( $params['privacy'] ) ) { $meta->privacy = $default_privacy; } else { $meta->privacy = $params['privacy']; } if ( ! empty( $params['parent_folder_id'] ) ) { $parent = (int) $params['parent_folder_id']; $meta->privacy = buddydrive_get_privacy( $parent ); } if ( 'password' === $meta->privacy ) { if ( isset( $parent ) ) { $meta->password = get_post_field( 'post_password', $parent ); } elseif ( ! empty( $params['password'] ) ) { $meta->password = $params['password']; } else { $meta->privacy = $default_privacy; } } if ( 'groups' === $meta->privacy ) { if ( isset( $parent ) ) { $meta->groups = get_post_meta( $parent, '_buddydrive_sharing_groups' ); } else if ( ! empty( $params['groups'] ) ) { $meta->groups = wp_parse_id_list( $params['groups'] ); } else { $meta->privacy = $default_privacy; } } if ( 'members' === $meta->privacy ) { if ( isset( $parent ) ) { $meta->members = get_post_meta( $parent, '_buddydrive_sharing_members' ); } else if ( ! empty( $params['members'] ) ) { // Add the owner to the list. $meta->members = wp_parse_id_list( array_merge( $params['members'], array( $params['user_id'] ) ) ); } else { $meta->privacy = $default_privacy; } } if ( ! empty( $params['customs'] ) ) { $meta->buddydrive_meta = $params['customs']; } if ( is_numeric( $params['title'] ) ) { $params['title'] = 'f-' . $params['title']; } // Sanitize meta if ( isset( $meta->password ) ) { $meta->password = wp_kses( $meta->password, array() ); } // Save the item $item_id = buddydrive_save_item( array( 'type' => $params['type'], 'user_id' => (int) $params['user_id'], 'parent_folder_id' => (int) $params['parent_folder_id'], 'title' => wp_kses( $params['title'], array() ), 'content' => wp_kses( $params['content'], array() ), 'mime_type' => $params['mime_type'], 'guid' => esc_url_raw( $params['guid'] ), 'metas' => $meta, ) ); do_action( 'buddydrive_add_item', $item_id, $params, $args ); return $item_id; } /** * Updates a BuddyDrive item * * @param array $args the arguments to update * @param object $item the BuddyDrive item * @uses wp_parse_args() to merge defaults and args array * @uses buddydrive_get_file_post_type() to get the BuddyFile post type * @uses get_post_meta() to get privacy options * @uses buddydrive_save_item() to update the item * @return integer $modified the id of the item updated */ function buddydrive_update_item( $args = '', $item = false ) { if ( empty( $item ) ) { return false; } $old_pass = false; if ( ! empty( $item->password ) ) { $old_pass = $item->password; } $old_group = false; if ( ! empty( $item->group ) ) { $old_group = $item->group; } $old_members = false; if ( ! empty( $item->members ) ) { $old_members = $item->members; } $defaults = array( 'id' => $item->ID, 'type' => $item->post_type, 'user_id' => $item->user_id, 'parent_folder_id' => $item->post_parent, 'title' => $item->title, 'content' => $item->content, 'mime_type' => $item->mime_type, 'guid' => $item->guid, 'privacy' => $item->check_for, 'password' => $old_pass, 'groups' => $old_group, 'members' => $old_members, 'buddydrive_meta' => false ); $params = wp_parse_args( $args, $defaults ); // Backward compability if ( isset( $params['group'] ) ) { $params['groups'] = $params['group']; unset( $params['group'] ); } // if the parent folder was set, then we need to define a default privacy status if ( ! empty( $item->post_parent ) && empty( $params['parent_folder_id'] ) ) { $default_privacy = 'private'; if ( ! empty( $args['privacy'] ) && buddydrive_get_privacy( 'buddydrive_' . $args['privacy'] ) ) { $default_privacy = $args['privacy']; } $params['privacy'] = $default_privacy; } elseif ( ! empty( $params['parent_folder_id'] ) && $params['type'] === buddydrive_get_file_post_type() ) { $params['privacy'] = buddydrive_get_privacy( $params['parent_folder_id'] ); } // building the meta object $meta = new stdClass(); $meta->privacy = $params['privacy']; // Delete the thumbnail if the public file became private if ( 'public' === $item->check_for && 'public' !== $meta->privacy ) { buddydrive_delete_thumbnail( $item->ID ); } if ( 'password' === $meta->privacy ) { if ( ! empty( $params['password'] ) ) { $meta->password = $params['password']; } elseif ( ! empty( $params['parent_folder_id'] ) ) { $meta->password = get_post_field( 'post_password', $params['parent_folder_id'] ); } } if ( 'groups' === $meta->privacy ) { if ( ! empty( $params['groups'] ) ) { $meta->groups = $params['groups']; } elseif ( ! empty( $params['parent_folder_id'] ) ) { $meta->groups = get_post_meta( $params['parent_folder_id'], '_buddydrive_sharing_groups' ); } } if ( 'members' === $meta->privacy ) { if ( ! empty( $params['members'] ) ) { $meta->members = wp_parse_id_list( array_merge( $params['members'], array( $params['user_id'] ) ) ); } elseif ( ! empty( $params['parent_folder_id'] ) ) { $meta->members = get_post_meta( $params['parent_folder_id'], '_buddydrive_sharing_members' ); } } if ( ! empty( $params['buddydrive_meta'] ) ) { $meta->buddydrive_meta = $params['buddydrive_meta']; } // preparing the args for buddydrive_save_item $params['metas'] = $meta; // we dont need privacy, password and group as it's in $meta unset( $params['privacy'] ); unset( $params['password'] ); unset( $params['groups'] ); unset( $params['members'] ); $modified = buddydrive_save_item( $params ); if ( empty( $modified ) ) { return false; } // Remove all groups if privacy changed if ( ! empty( $old_group ) && 'groups' !== $meta->privacy ) { delete_post_meta( $item->ID, '_buddydrive_sharing_groups' ); } // Remove all members if privacy changed if ( ! empty( $old_members ) && 'members' !== $meta->privacy ) { delete_post_meta( $item->ID, '_buddydrive_sharing_members' ); } do_action( 'buddydrive_update_item', $params, $args, $item ); return $modified; } /** * Deletes one or more BuddyDrive Item(s) * * @since 2.0.0 Returns an array of deleted item ids on success * * @param array $args the argument ( the ids to delete and the user_id to check upon ) * * @return array|boolean the list of deleted items or false */ function buddydrive_delete_item( $args = '' ) { $defaults = array( 'ids' => false, 'user_id' => bp_loggedin_user_id() ); $params = wp_parse_args( $args, $defaults ); if ( ! empty( $params['ids'] ) && ! is_array( $params['ids'] ) ) { $params['ids'] = explode( ',', $params['ids'] ); } $buddydrive_item = new BuddyDrive_Item(); if ( $items = $buddydrive_item->delete( $params['ids'], $params['user_id'] ) ) { return $items; } else { return false; } } /** * Bulk remove parent for a list of item ids. * * @since 2.0.0 * * @param array $item_ids The list of item ids to remove the parent for * @return array the list of item ids who got their parent removed */ function buddydrive_items_remove_parent( $item_ids = array() ) { // Sanitize $item_ids = wp_parse_id_list( $item_ids ); // Init result $removed = array(); foreach ( $item_ids as $item_id ) { // First validate the file $buddyfile = buddydrive_get_buddyfile( $item_id, buddydrive_get_file_post_type() ); // Do nothing if no parent or the user can't perform this action if ( empty( $buddyfile->post_parent ) ) { continue; } // Do nothing if the user can't perform this action if ( ! buddydrive_current_user_can( 'buddydrive_remove_parent', array( 'owner_id' => $buddyfile->user_id, 'parent_owner_id' => get_post_field( 'post_author', $buddyfile->post_parent ) ) ) ) { continue; } do_action( 'buddydrive_items_remove_parent_before', $buddyfile ); $item_removed = buddydrive_update_item( array( 'parent_folder_id' => 0 ), $buddyfile ); if ( ! empty( $item_removed ) ) { $removed[] = $item_removed; } } do_action( 'buddydrive_items_remove_parent_after', $removed, $item_ids ); // Return the list of bulk edited items return $removed; } /** * Returns BuddyDrive items datas for an array of ids * * @param array $ids the list of BuddyDrive items ids * @uses BuddyDrive_Item::get_buddydrive_by_ids() to query the DB for items * @return array BuddyDrive items */ function buddydrive_get_buddyfiles_by_ids( $ids = array() ) { if ( empty( $ids ) ) return false; $buddydrive_item = new BuddyDrive_Item(); return $buddydrive_item->get_buddydrive_by_ids( $ids ); } /** * Removes all the BuddyDrive Items from a group if it's about to be deleted * * @param integer $group_id the group id * @uses groups_get_group() to get a group object for the group id * @uses BuddyDrive_Item::group_remove_items() to delete the group id options for the BuddyDrive items * @return boolean true or false */ function buddydrive_remove_buddyfiles_from_group( $group_id = 0 ) { $new_privacy = 'private'; $group = groups_get_group( array( 'group_id' => $group_id ) ); if ( isset( $group->status ) && 'public' === $group->status ) { $new_privacy = 'public'; } $buddydrive_item = new BuddyDrive_Item(); return $buddydrive_item->group_remove_items( $group_id, $new_privacy ); } add_action( 'groups_before_delete_group', 'buddydrive_remove_buddyfiles_from_group', 1 ); /** * Gets a single BuddyDrive items * * @param string|int $name the post name or the id of the item to get * @param string $type the BuddyDrive post type * @uses buddydrive_get_file_post_type() to default to the BuddyFile post type * @uses BuddyDrive_Item::get() to get the BuddyDrive item * @uses buddydrive_get_root_url() to get BuddyDrive root url * @uses get_post_meta() to get item's privacy options * @return object the BuddyDrive item */ function buddydrive_get_buddyfile( $name = false, $type = false ) { if ( empty( $name ) ) { return false; } if ( is_a( $name, 'WP_Post' ) ) { $buddyfile = $name; } else { if ( empty( $type ) ) { $type = buddydrive_get_file_post_type(); } if ( is_numeric( $name ) ) { $args = array( 'id' => $name, 'type' => $type ); } else { $args = array( 'name' => $name, 'type' => $type ); } $buddydrive_item = new BuddyDrive_Item(); $buddydrive_item->get( $args ); if ( empty( $buddydrive_item->query->post->ID ) ) { return false; } else { $buddyfile = $buddydrive_item->query->post; } } $buddyfile->user_id = $buddyfile->post_author; $buddyfile->title = $buddyfile->post_title; $buddyfile->content = $buddyfile->post_content; // do we have a file ? if ( $buddyfile->post_type === buddydrive_get_file_post_type() ) { $buddyitem_slug = 'file'; $buddyfile->file = basename( $buddyfile->guid ); $buddyfile->path = buddydrive()->upload_dir .'/'. $buddyfile->file; $buddyfile->mime_type = $buddyfile->post_mime_type; // Then it must be a folder } else { $buddyitem_slug = $buddyfile->mime_type = 'folder'; } $slug = trailingslashit( $buddyitem_slug .'/' . $buddyfile->post_name ); $link = buddydrive_get_root_url() .'/'. $slug; $buddyfile->link = $link; /* privacy */ $buddydrive_status = get_post_status_object( $buddyfile->post_status ); if ( isset( $buddydrive_status->buddydrive_privacy ) ) { $privacy = $buddydrive_status->buddydrive_privacy; } else { $privacy = get_post_meta( $buddyfile->ID, '_buddydrive_sharing_option', true ); } // by default check for user_id if ( empty( $privacy ) ) { $privacy = buddydrive_get_default_privacy(); } $buddyfile->check_for = $privacy; $core_stati = wp_list_pluck( buddydrive_get_stati( true ), 'label', 'buddydrive_privacy' ); if ( 'password' === $privacy ) { $buddyfile->password = $buddyfile->post_password; } elseif ( 'groups' === $privacy ) { // Get all groups $buddyfile->group = get_post_meta( $buddyfile->ID, '_buddydrive_sharing_groups' ); } elseif ( 'members' === $privacy ) { // Get all members $buddyfile->members = get_post_meta( $buddyfile->ID, '_buddydrive_sharing_members' ); } elseif ( ! isset( $core_stati[ $privacy ] ) ) { /** * Filter here for custom privacy options * * @since 1.3.3 * * @param string $value By default 'private'. * @param object $buddyfile The BuddyDrive file object. */ $buddyfile->check_for = apply_filters( 'buddydrive_get_buddyfile_check_for', $buddyfile->check_for, $buddyfile ); } return $buddyfile; } /** * Removes a single BuddyDrive items from group * * @param int $item_id the BuddyDrive item id * @param int $group_id the group id * @uses groups_get_group() to get the group object for the given group_id * @uses BuddyDrive_Item::remove_from_group() to delete the options in the DBs * @return boolean true or false */ function buddydrive_remove_item_from_group( $item_id = false , $group_id = false ) { $new_privacy = 'private'; $buddydrive_item = new BuddyDrive_Item(); $group = groups_get_group( array( 'group_id' => $group_id ) ); if ( isset( $group->status ) && 'public' === $group->status ) { $new_privacy = 'public'; } return $buddydrive_item->remove_from_group( $item_id, $new_privacy, $group_id ); } /** * Bulk remove a list of items from a group * * @since 2.0.0 * * @param array $items A list of item IDs to remove from the Group. * @param int $group_id The Group ID, the items needs to be removed from. * @return array The list of removed item IDs. */ function buddydrive_items_remove_from_group( $items = array(), $group_id = 0 ) { $items = wp_parse_id_list( $items ); $updated = array(); if ( empty( $items ) ) { return $updated; } // I should either use a function or a static method instead.. $buddydrive_item = new BuddyDrive_Item(); foreach ( $items as $item ) { $updated[] = $buddydrive_item->remove_from_group( $item, 'private', $group_id ); } return $updated; } /** * Handles an embed BuddyDrive item * * @param array $matches the result of the preg_match * @param array $attr * @param string $url * @param array $rawattr * @uses is_multisite() to check for multisite config * @uses bp_get_root_blog_id() to get the root blog id * @uses switch_to_blog() to change for root blog id * @uses buddydrive_get_buddyfile() to get the BuddyDrive Item * @uses buddydrive_get_file_post_type() to get the BuddyFile post type * @uses wp_mime_type_icon() to get the WordPress crystal icon * @uses buddydrive_get_folder_post_type() to get the BuddyFolder post type * @uses buddydrive_get_group_buddydrive_url() to build the url to the BuddyDrive group * @uses buddydrive_get_user_buddydrive_url() to get the user's BuddyDrive url * @uses buddydrive_get_images_url() to get the image url of the plugin * @uses the BuddyDrive Loop and some tempkate tags * @uses wp_reset_postdata() to avoid some weird link.. * @uses restore_current_blog() to restore the child blog. * @return string $embed the html output */ function wp_embed_handler_buddydrive( $matches, $attr, $url, $rawattr ) { $current_blog = get_current_blog_id(); if ( is_multisite() && (int) $current_blog !== (int) bp_get_root_blog_id() ) { switch_to_blog( bp_get_root_blog_id() ); } $item = null; $content = ''; if ( 'file' === $matches[1] ) { $item = buddydrive_get_buddyfile( $matches[2], buddydrive_get_file_post_type() ); if ( empty( $item ) ) { return ''; } $link = $item->link; $filename = $item->path; // It's a folfer } else { $item = buddydrive_get_buddyfile( $matches[2], buddydrive_get_folder_post_type() ); if ( empty( $item ) ) { return ''; } $link = buddydrive_get_user_buddydrive_url( $item->user_id ); if ( 'buddydrive_groups' === $item->post_status ) { $link = buddydrive_get_group_buddydrive_url( $item->group ); }; $link .= '#view/' . $item->ID; $filename = 'folder'; } if ( ! $item ) { return; } $icon = buddydrive_get_icon( $filename, $item->check_for, $item->ID ); if ( ! empty( $item->content ) ) { $content = sprintf( '
%s
', $item->content ); } $embed = sprintf( '