/** * @group bulk_delete */ public function test_buddydrive_bulk_delete_items() { // create the upload dir $upload_dir = buddydrive_get_upload_data(); $expected_ids = array(); $quota_left = 0; $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); $items_users = array('screenshot-1.png' => $u1, 'screenshot-2.png' => $u1, 'screenshot-3.png' => $u2); $args = array('type' => buddydrive_get_file_post_type(), 'mime_type' => 'image/png'); foreach ($items_users as $item => $user) { $f = trailingslashit(buddydrive()->plugin_dir) . $item; copy($f, trailingslashit($upload_dir['dir']) . $item); $fs = filesize($item); buddydrive_update_user_space($user, filesize($item)); if ('screenshot-1.png' === $item) { $quota_left = $fs; } $args = array_merge($args, array('user_id' => $user, 'title' => $item, 'guid' => trailingslashit($upload_dir['url']) . $item)); $expected_ids[$item] = buddydrive_save_item($args); } unset($expected_ids['screenshot-1.png']); $count = buddydrive_delete_item(array('ids' => $expected_ids)); $this->assertTrue($count === count($expected_ids)); $not_deleted = buddydrive_get_buddyfiles_by_ids($expected_ids); $this->assertTrue(empty($not_deleted)); $this->assertEquals($quota_left, (int) get_user_meta($u1, '_buddydrive_total_space', true), 'u1 quota should be set'); $this->assertEmpty((int) get_user_meta($u2, '_buddydrive_total_space', true), 'u2 quota should be 0'); }
/** * 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; }
/** * Do we have a file ? * * @global object $buddydrive_template * @uses buddydrive_get_file_post_type() to get the BuddyFile post type * @return boolean true or false */ function buddydrive_is_buddyfile() { global $buddydrive_template; $is_buddyfile = false; if ($buddydrive_template->query->post->post_type == buddydrive_get_file_post_type()) { $is_buddyfile = true; } return $is_buddyfile; }
/** * Adds the link to the file or list of files at the bottom of the message * * @param string $message the content of the private message * @uses buddydrive_get_buddyfile() to get the file or folder object * @uses buddydrive_get_file_post_type() to get the file post type * @uses buddydrive_get_folder_post_type() to get the folder post type * @return string $message with the link to the file/folder */ function buddydrive_update_message_content($message) { if (!empty($_POST['_buddyitem_link'])) { $password = $password_check = false; if (!empty($_POST['_buddyitem_pass'])) { $buddyitem = buddydrive_get_buddyfile($_REQUEST['_buddyitem_id'], array(buddydrive_get_file_post_type(), buddydrive_get_folder_post_type())); if (!empty($buddyitem->post_parent)) { $parent = buddydrive_get_buddyfile($buddyitem->post_parent, buddydrive_get_folder_post_type()); $password_check = $parent->password; } else { $password_check = $buddyitem->password; } $password = !empty($password_check) ? '<p>' . sprintf(__('Password : %s', 'buddydrive'), $password_check) . '</p>' : false; } $message->message .= "\n" . $_POST['_buddyitem_link'] . "\n" . $password; } }
/** * displays file post type of the plugin * * @uses buddydrive_get_file_post_type() to get it! */ function buddydrive_file_post_type() { echo buddydrive_get_file_post_type(); }
/** * @group delete */ public function test_buddydrive_delete_item_zero() { // create the upload dir $upload_dir = buddydrive_get_upload_data(); $expected_id = buddydrive_save_item(array('type' => buddydrive_get_file_post_type(), 'user_id' => $this->user_id, 'title' => 'screenshot-1.png', 'content' => 'foo file', 'mime_type' => 'image/png', 'guid' => trailingslashit($upload_dir['url']) . 'screenshot-1.png')); $count = buddydrive_delete_item(array('ids' => 0, 'user_id' => false)); $this->assertEmpty($count); $not_deleted = buddydrive_get_buddyfiles_by_ids($expected_id); $this->assertTrue(!empty($not_deleted)); }
/** * Get the list of views available on this table (e.g. "all", "folder"). * * @since BuddyDrive (1.0) * @uses remove_query_arg() to remove arguments to url * @uses esc_attr() to sanitize data * @uses esc_url() to sanitize url * @uses add_query_arg() to add arguments to the url */ function get_views() { $url_base = remove_query_arg(array('s', 'orderby', 'order', 'buddydrive_type', '_wpnonce', '_wp_http_referer', 'action', 'action2', 'paged'), $_SERVER['REQUEST_URI']); ?> <ul class="subsubsub"> <li class="all"><a href="<?php echo esc_url($url_base); ?> " class="<?php if ('all' == $this->view) { echo 'current'; } ?> "><?php _e('All', 'buddydrive'); ?> </a> |</li> <li class="public"><a href="<?php echo esc_url(add_query_arg('buddydrive_type', buddydrive_get_file_post_type(), $url_base)); ?> " class="<?php if (buddydrive_get_file_post_type() == $this->view) { echo 'current'; } ?> "><?php _e('Files', 'buddydrive'); ?> </a> |</li> <li class="private"><a href="<?php echo esc_url(add_query_arg('buddydrive_type', buddydrive_get_folder_post_type(), $url_base)); ?> " class="<?php if (buddydrive_get_folder_post_type() == $this->view) { echo 'current'; } ?> "><?php _e('Folders', 'buddydrive'); ?> </a></li> <?php do_action('buddydrive_list_table_get_views', $url_base, $this->view); ?> </ul> <?php }
/** * Get a user's file count * * @since 1.2.2 * * @param int $user_id the user id * @return int the files count */ function buddydrive_count_user_files($user_id = 0) { $bd = buddydrive(); if (empty($user_id)) { $user_id = bp_loggedin_user_id(); } if (empty($user_id) || !bp_is_root_blog()) { return false; } // Count only once per page load. if (!empty($bp->users_file_count[$user_id])) { return $bp->users_file_count[$user_id]; } $bp->users_file_count[$user_id] = count_user_posts($user_id, buddydrive_get_file_post_type()); return $bp->users_file_count[$user_id]; }
/** * @group get * @group scope */ public function test_buddydrive_item_get_by_scope() { $u2 = $this->factory->user->create(); // Admin $this->set_current_user(1); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'admin')); // Admin should see everything $this->assertTrue((int) $by_scope->query->found_posts === 2); // Update the privacy of the file $file_object = buddydrive_get_buddyfile($this->expected_ids['foo']); buddydrive_update_item(array('privacy' => 'public'), $file_object); // Any user $this->set_current_user($u2); add_filter('bp_displayed_user_id', array($this, 'set_displayed_user_id'), 10, 1); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'files')); $file = wp_list_pluck($by_scope->query->posts, 'ID'); $this->assertTrue($this->expected_ids['foo'] === (int) $file[0], 'only public files should be listed'); // The owner $this->set_current_user($this->user_id); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'files')); // Owner should see everything $this->assertTrue((int) $by_scope->query->found_posts === 2); remove_filter('bp_displayed_user_id', array($this, 'set_displayed_user_id'), 10, 1); // Any user $this->set_current_user($u2); // Update the privacy and owner of the file $file_object = buddydrive_get_buddyfile($this->expected_ids['bar']); buddydrive_update_item(array('privacy' => 'public', 'user_id' => $u2), $file_object); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'public')); // Custom loops should be able to list all public files $this->assertTrue((int) $by_scope->query->found_posts === 2); buddydrive_update_item(array('privacy' => 'private'), $file_object); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'public')); // Custom loops should be able to list all public files $this->assertTrue((int) $by_scope->query->found_posts === 1); }
/** * Build script datas for the Uploader UI * * @since 1.3.0 * * @return array the javascript localization data */ public function script_data() { // Get default script data $script_data = parent::script_data(); $script_data['bp_params'] = array('object' => buddydrive_get_file_post_type(), 'item_id' => bp_loggedin_user_id(), 'privacy' => 'public'); if (buddydrive_current_group_is_enabled()) { $script_data['bp_params']['privacy'] = 'groups'; $script_data['bp_params']['privacy_item_id'] = bp_get_current_group_id(); } // Include our specific css $script_data['extra_css'] = array('buddydrive-public-style'); // Include our specific js $script_data['extra_js'] = array('buddydrive-public-js'); return apply_filters('buddydrive_attachment_script_data', $script_data); }
/** * 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(); }