/** * BuddyDrive Loop : do we have items for the query asked * * @param array $args the arguments of the query * @global object $buddydrive_template * @uses buddydrive_get_folder_post_type() to get BuddyFolder post type * @uses buddydrive_get_file_post_type() to get BuddyFile post type * @uses bp_displayed_user_id() to default to current displayed user * @uses bp_current_action() to get the current action ( files / friends / admin) * @uses bp_is_active() to check if groups component is active * @uses buddydrive_is_group() are we on a group's BuddyDrive ? * @uses wp_parse_args() to merge defaults and args * @uses BuddyDrive_Item::get() to request the DB * @uses BuddyDrive_Item::have_posts to know if BuddyItems matched the query * @return the result of the query */ function buddydrive_has_items($args = '') { global $buddydrive_template; // This keeps us from firing the query more than once if (empty($buddydrive_template)) { $defaulttype = array(buddydrive_get_folder_post_type(), buddydrive_get_file_post_type()); $user = $group_id = $buddyscope = false; if (bp_displayed_user_id()) { $user = bp_displayed_user_id(); } $buddyscope = bp_current_action(); if ($buddyscope == buddydrive_get_friends_subnav_slug()) { $buddyscope = 'friends'; } if (is_admin()) { $buddyscope = 'admin'; } if (bp_is_active('groups') && buddydrive_is_group()) { $group = groups_get_current_group(); $group_id = $group->id; $buddyscope = 'groups'; } /*** * Set the defaults for the parameters you are accepting via the "buddydrive_has_items()" * function call */ $defaults = array('id' => false, 'name' => false, 'group_id' => $group_id, 'user_id' => $user, 'per_page' => 10, 'paged' => 1, 'type' => $defaulttype, 'buddydrive_scope' => $buddyscope, 'search' => false, 'buddydrive_parent' => 0, 'exclude' => 0, 'orderby' => 'title', 'order' => 'ASC'); $r = bp_parse_args($args, $defaults, 'buddydrive_has_items'); if ('admin' === $r['buddydrive_scope'] && !bp_current_user_can('bp_moderate')) { $r['buddydrive_scope'] = 'files'; } $buddydrive_template = new BuddyDrive_Item(); if (!empty($search)) { $buddydrive_template->get(array('per_page' => $r['per_page'], 'paged' => $r['paged'], 'type' => $r['type'], 'buddydrive_scope' => $r['buddydrive_scope'], 'search' => $r['search'], 'orderby' => $r['orderby'], 'order' => $r['order'])); } else { $buddydrive_template->get(array('id' => $r['id'], 'name' => $r['name'], 'group_id' => $r['group_id'], 'user_id' => $r['user_id'], 'per_page' => $r['per_page'], 'paged' => $r['paged'], 'type' => $r['type'], 'buddydrive_scope' => $r['buddydrive_scope'], 'buddydrive_parent' => $r['buddydrive_parent'], 'exclude' => $r['exclude'], 'orderby' => $r['orderby'], 'order' => $r['order'])); } do_action('buddydrive_has_items_catch_total_count', $buddydrive_template->query->found_posts); } return apply_filters('buddydrive_has_items', $buddydrive_template->have_posts()); }
/** * Post an activity in user's profile * * @uses check_admin_referer() for security reasons * @uses bp_loggedin_user_id() to get the current user id * @uses buddydrive_get_folder_post_type() to get the BuddyFolder post type * @uses buddydrive_get_name() so that it's possible to brand the plugin * @uses buddydrive_get_file_post_type() to get the BuddyFile post type * @uses buddydrive_get_buddyfile() to get item * @uses bp_core_get_userlink() to get link to user's profile * @uses bp_activity_add() to finaly record the activity without updating the latest meta * @return int 1 or string an error message */ function buddydrive_share_in_profile() { // Bail if not a POST action if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { return; } // Check the nonce check_admin_referer('buddydrive_actions', '_wpnonce_buddydrive_actions'); $buddyitem = intval($_POST['itemid']); if (empty($buddyitem)) { _e('this is embarassing, it did not work :(', 'buddydrive'); die; } $link = $_POST['url']; $result = false; $user_id = bp_loggedin_user_id(); $item_type = 'folder' == $_POST['itemtype'] ? buddydrive_get_folder_post_type() : buddydrive_get_file_post_type(); if (!empty($buddyitem)) { $buddyfile = buddydrive_get_buddyfile($buddyitem, $item_type); if (empty($buddyfile->ID) || $buddyfile->check_for != 'public') { // no item or not a public one ?? _e('We could not find your BuddyDrive item or its privacy is not set to public', 'buddydrive'); die; } $action = sprintf(__('%1$s shared a %2$s Item', 'buddydrive'), bp_core_get_userlink($user_id), buddydrive_get_name()); $content = $link; $args = array('user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => bp_core_get_userlink($user_id, false, true), 'component' => 'activity', 'type' => 'activity_update'); $result = bp_activity_add($args); } if (!empty($result)) { echo 1; } else { echo _e('this is embarassing, it did not work :(', 'buddydrive'); } die; }
/** * @group save * @group update */ public function test_buddydrive_update_item() { // create the upload dir $upload_dir = buddydrive_get_upload_data(); $meta = new stdClass(); $meta->privacy = 'public'; $expected_ids = array(); $expected_ids['file_id'] = buddydrive_save_item(array('type' => buddydrive_get_file_post_type(), 'user_id' => $this->user_id, 'title' => 'screenshot-1.png', 'content' => 'foo bar file', 'mime_type' => 'image/png', 'guid' => trailingslashit($upload_dir['url']) . 'screenshot-1.png', 'metas' => $meta)); $file_object = buddydrive_get_buddyfile($expected_ids['file_id']); $this->assertTrue('public' === $file_object->check_for); $meta->privacy = 'private'; $expected_ids['folder_id'] = buddydrive_save_item(array('type' => buddydrive_get_folder_post_type(), 'title' => 'foo', 'content' => 'foo bar folder', 'metas' => $meta)); buddydrive_update_item(array('parent_folder_id' => $expected_ids['folder_id']), $file_object); $file_object = buddydrive_get_buddyfile($expected_ids['file_id']); $this->assertTrue((int) $file_object->post_parent === (int) $expected_ids['folder_id']); $this->assertTrue('private' === $file_object->check_for); $folder_object = buddydrive_get_buddyfile($expected_ids['folder_id'], buddydrive_get_folder_post_type()); buddydrive_update_item(array('privacy' => 'public'), $folder_object); $file_object = buddydrive_get_buddyfile($expected_ids['file_id']); $this->assertTrue('public' === $file_object->check_for); }
/** * Update the privacy of children files linked to a folder if updated * * @param array $params associtive array of parameters * @param array $args * @param object $item the folder object * @uses buddydrive_get_folder_post_type() to check it's a folder * @uses BuddyDrive_Item::update_children() to update the files */ function buddydrive_update_children($params, $args, $item) { if ($item->post_type != buddydrive_get_folder_post_type()) { return; } $parent_id = intval($params['id']); $metas = $params['metas']; $buddydrive_update_children = new BuddyDrive_Item(); $buddydrive_update_children->update_children($parent_id, $metas); }
/** * displays folder post type of the plugin * * @uses buddydrive_get_folder_post_type() to get it! */ function buddydrive_folder_post_type() { echo buddydrive_get_folder_post_type(); }
/** * Name column, and "quick admin" rollover actions. * * Called "comment" in the CSS so we can re-use some WP core CSS. * * @param array $item A singular item (one full row) * @uses bp_get_admin_url() to build the admin url * @uses wp_nonce_url() for security reasons * @uses buddydrive_get_root_url() to get the BuddyDrive root url * @uses buddydrive_get_folder_post_type() to get the BuddyFolder post type * @uses esc_url() to sanitize url * @see WP_List_Table::single_row_columns() * @since BuddyDrive (1.0) */ function column_comment($item = array()) { // Preorder items: download | Edit | Delete $actions = array('download' => '', 'edit' => '', 'delete' => ''); // Build actions URLs $base_url = bp_get_admin_url('admin.php?page=buddydrive-files&bid=' . $item['ID']); $delete_url = wp_nonce_url($base_url . "&action=delete", 'buddydrive-delete'); $edit_url = $base_url . '&action=edit'; $download = trailingslashit('file/' . $item['post_name']); $visit_url = buddydrive_get_root_url() . '/' . $download; // Download if ($item['post_type'] != buddydrive_get_folder_post_type()) { $actions['download'] = sprintf('<a href="%s">%s</a>', esc_url($visit_url), __('Download', 'buddydrive')); } // Edit $actions['edit'] = sprintf('<a href="%s">%s</a>', esc_url($edit_url), __('Edit', 'buddydrive')); // Delete $actions['delete'] = sprintf('<a href="%s">%s</a>', esc_url($delete_url), __('Delete', 'buddydrive')); // Other plugins can filter which actions are shown $actions = apply_filters('buddydrive_admin_comment_row_actions', array_filter($actions), $item); $content = apply_filters('buddydrive_get_item_title', $item['post_title']); $icon = $item['post_type'] != buddydrive_get_folder_post_type() ? '<i class="icon bd-icon-file"></i>' : '<i class="icon bd-icon-folder"></i>'; echo $icon . ' ' . $content . ' ' . $this->row_actions($actions); }
/** * 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) { $link = $title = $icon = $content = $mime_type = $filelist = $hw_attr = false; $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()); } if ($matches[1] == 'file') { $buddyfile = buddydrive_get_buddyfile($matches[2], buddydrive_get_file_post_type()); if (empty($buddyfile)) { return ''; } $link = $buddyfile->link; $title = $buddyfile->title; $content = $buddyfile->content; $mime_type = $buddyfile->mime_type; $icon = wp_mime_type_icon($buddyfile->ID); if ('public' === $buddyfile->check_for) { $thumbnail = buddydrive_get_thumbnail($buddyfile->ID, 'thumburl', false); if (!empty($thumbnail[0])) { $icon = $thumbnail[0]; $hw_attr = image_hwstring($thumbnail[1], $thumbnail[2]); } } // It's a folfer } else { $buddyfile = buddydrive_get_buddyfile($matches[2], buddydrive_get_folder_post_type()); if (empty($buddyfile)) { return ''; } $buddydrive_root_link = $buddyfile->check_for == 'groups' ? buddydrive_get_group_buddydrive_url($buddyfile->group) : buddydrive_get_user_buddydrive_url($buddyfile->user_id); $link = $buddydrive_root_link . '?folder-' . $buddyfile->ID; $title = $buddyfile->title; $mime_type = $buddyfile->mime_type; $icon = buddydrive_get_images_url() . 'folder.png'; } $embed = '<table style="width:auto"><tr>'; $tdwidth = 'width:60px;'; if (!empty($hw_attr)) { $tdwidth = ''; } $embed .= '<td style="vertical-align:middle;' . $tdwidth . '"><a href="' . esc_url($link) . '" title="' . esc_attr($title) . '"><img src="' . esc_url($icon) . '" alt="' . esc_attr($mime_type) . '" class="buddydrive-thumb" ' . $hw_attr . '></a></td>'; $embed .= '<td style="vertical-align:middle"><h6 style="margin:0"><a href="' . esc_url($link) . '" title="' . esc_attr($title) . '">' . esc_html($title) . '</a></h6>'; if (!empty($content)) { $embed .= '<p style="margin:0">' . esc_html($content) . '</p>'; } if ($matches[1] == 'folder') { global $buddydrive_template; if (buddydrive_has_items(array('buddydrive_parent' => $buddyfile->ID))) { $filelist = '<p style="margin-top:1em;margin-bottom:0">' . esc_html__('Files included in this folder :', 'buddydrive') . '</p><ul>'; while (buddydrive_has_items()) { buddydrive_the_item(); $filelist .= '<li><a href="' . esc_url(buddydrive_get_action_link()) . '" title="' . esc_attr(buddydrive_get_item_title()) . '">' . esc_html(buddydrive_get_item_title()) . '</a></li>'; } $filelist .= '</ul>'; $buddydrive_template = false; } wp_reset_postdata(); $embed .= $filelist; } $embed .= '</td></tr></table>'; if (is_multisite() && (int) $current_blog !== (int) bp_get_root_blog_id()) { restore_current_blog(); } return apply_filters('embed_buddydrive', $embed, $matches, $attr, $url, $rawattr); }
/** * Deletes a list of items or all the items of a given user * * @param array $ids array of BuddyDrive Item ids * @param int $user_id the id of a user * @global object $wpdb * @uses get_user_meta() to get the quota of the user id * @uses buddydrive_get_buddyfile() to get the BuddyDrive item * @uses wp_delete_post() to delete the BuddyDrive post type * @uses update_user_meta() to eventually update user's quota * @return int number of deleted items */ public function delete($ids = false, $user_id = false) { global $wpdb; $buddydrive_ids = array(); $spaces = array(); $new_space = false; $ids = array_filter(wp_parse_id_list($ids)); if (!empty($ids)) { //we need to get the children $in = '("' . implode('","', $ids) . '")'; $buddydrive_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->base_prefix}posts WHERE post_parent IN {$in}"); $buddydrive_ids = array_merge($buddydrive_ids, $ids); } elseif (!empty($user_id) && empty($ids)) { // in case a user is deleted $buddydrive_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->base_prefix}posts WHERE post_author = %d AND post_type IN (%s, %s)", $user_id, buddydrive_get_folder_post_type(), buddydrive_get_file_post_type())); $new_user = (int) apply_filters('buddydrive_set_owner_on_user_deleted', 0); // The new user must have the power to post in any group if (!empty($new_user) && user_can($new_user, 'bp_moderate') && !empty($buddydrive_ids)) { $wpdb->query($wpdb->prepare("UPDATE {$wpdb->base_prefix}posts SET post_author = %d WHERE post_author = %d AND post_type IN (%s, %s)", $new_user, $user_id, buddydrive_get_folder_post_type(), buddydrive_get_file_post_type())); foreach ($buddydrive_ids as $post_id) { clean_post_cache($post_id); } } } if (empty($buddydrive_ids)) { return false; } if (empty($new_user)) { foreach ($buddydrive_ids as $id) { $buddyfile = buddydrive_get_buddyfile($id); if (!empty($buddyfile)) { if (!empty($buddyfile->path) && file_exists($buddyfile->path)) { if (!isset($spaces[$buddyfile->user_id])) { $spaces[$buddyfile->user_id] = filesize($buddyfile->path); } else { $spaces[$buddyfile->user_id] += filesize($buddyfile->path); } unlink($buddyfile->path); } // Delete the thumbnail if ('public' === $buddyfile->check_for) { buddydrive_delete_thumbnail($buddyfile->ID); } } wp_delete_post($id, true); } } if (!empty($spaces)) { foreach ($spaces as $u_id => $space) { $user_total_space = get_user_meta($u_id, '_buddydrive_total_space', true); $user_total_space = intval($user_total_space); if ($space < $user_total_space) { buddydrive_update_user_space($u_id, -1 * absint($space)); } else { delete_user_meta($u_id, '_buddydrive_total_space'); } } } return count($buddydrive_ids); }
/** * registering BuddyDrive custom post types * * @uses buddydrive_get_folder_post_type() to get the BuddyFolder post type * @uses buddydrive_get_file_post_type() to get the BuddyFile post type * @uses register_post_type() to register the post type */ public function register_post_types() { // Set up some labels for the post type $labels_file = array('name' => __('BuddyFiles', 'buddydrive'), 'singular' => __('BuddyFile', 'buddydrive'), 'menu_name' => __('BuddyDrive Files', 'buddydrive'), 'all_items' => __('All BuddyFiles', 'buddydrive'), 'singular_name' => __('BuddyFile', 'buddydrive'), 'add_new' => __('Add New BuddyFile', 'buddydrive'), 'add_new_item' => __('Add New BuddyFile', 'buddydrive'), 'edit_item' => __('Edit BuddyFile', 'buddydrive'), 'new_item' => __('New BuddyFile', 'buddydrive'), 'view_item' => __('View BuddyFile', 'buddydrive'), 'search_items' => __('Search BuddyFiles', 'buddydrive'), 'not_found' => __('No BuddyFiles Found', 'buddydrive'), 'not_found_in_trash' => __('No BuddyFiles Found in Trash', 'buddydrive')); $args_file = array('label' => __('BuddyFile', 'buddydrive'), 'labels' => $labels_file, 'public' => false, 'rewrite' => false, 'show_ui' => false, 'show_in_admin_bar' => false, 'supports' => array('title', 'editor', 'author')); // Register the post type for files. register_post_type(buddydrive_get_file_post_type(), $args_file); $labels_folder = array('name' => __('BuddyFolders', 'buddydrive'), 'singular' => __('BuddyFolder', 'buddydrive'), 'menu_name' => __('BuddyDrive Folders', 'buddydrive'), 'all_items' => __('All BuddyFolders', 'buddydrive'), 'singular_name' => __('BuddyFolder', 'buddydrive'), 'add_new' => __('Add New BuddyFolder', 'buddydrive'), 'add_new_item' => __('Add New BuddyFolder', 'buddydrive'), 'edit_item' => __('Edit BuddyFolder', 'buddydrive'), 'new_item' => __('New BuddyFolder', 'buddydrive'), 'view_item' => __('View BuddyFolder', 'buddydrive'), 'search_items' => __('Search BuddyFolders', 'buddydrive'), 'not_found' => __('No BuddyFolders Found', 'buddydrive'), 'not_found_in_trash' => __('No BuddyFolders Found in Trash', 'buddydrive')); $args_folder = array('label' => __('BuddyFolder', 'buddydrive'), 'labels' => $labels_folder, 'public' => false, 'rewrite' => false, 'show_ui' => false, 'show_in_admin_bar' => false, 'supports' => array('title', 'editor', 'author')); // Register the post type for files. register_post_type(buddydrive_get_folder_post_type(), $args_folder); parent::register_post_types(); }