/** * Initiates a BuddyPress Docs query * * @since 1.2 */ function bp_docs_has_docs($args = array()) { global $bp, $wp_query; // The if-empty is because, like with WP itself, we use bp_docs_has_docs() both for the // initial 'if' of the loop, as well as for the 'while' iterator. Don't want infinite // queries if (empty($bp->bp_docs->doc_query)) { // Build some intelligent defaults // Default to current group id, if available $d_group_id = bp_is_group() ? bp_get_current_group_id() : array(); // If this is a Started By tab, set the author ID $d_author_id = bp_docs_is_started_by() ? bp_displayed_user_id() : array(); // If this is an Edited By tab, set the edited_by id $d_edited_by_id = bp_docs_is_edited_by() ? bp_displayed_user_id() : array(); // Default to the tags in the URL string, if available $d_tags = isset($_REQUEST['bpd_tag']) ? explode(',', urldecode($_REQUEST['bpd_tag'])) : array(); // Order and orderby arguments $d_orderby = !empty($_GET['orderby']) ? urldecode($_GET['orderby']) : apply_filters('bp_docs_default_sort_order', 'modified'); if (empty($_GET['order'])) { // If no order is explicitly stated, we must provide one. // It'll be different for date fields (should be DESC) if ('modified' == $d_orderby || 'date' == $d_orderby) { $d_order = 'DESC'; } else { $d_order = 'ASC'; } } else { $d_order = $_GET['order']; } // Search $d_search_terms = !empty($_GET['s']) ? urldecode($_GET['s']) : ''; // Parent id $d_parent_id = !empty($_REQUEST['parent_doc']) ? (int) $_REQUEST['parent_doc'] : ''; // Page number, posts per page $d_paged = 1; if (!empty($_GET['paged'])) { $d_paged = absint($_GET['paged']); } else { if (bp_docs_is_global_directory() && is_a($wp_query, 'WP_Query') && 1 < $wp_query->get('paged')) { $d_paged = absint($wp_query->get('paged')); } } $d_posts_per_page = !empty($_GET['posts_per_page']) ? absint($_GET['posts_per_page']) : 10; // doc_slug $d_doc_slug = !empty($bp->bp_docs->query->doc_slug) ? $bp->bp_docs->query->doc_slug : ''; $defaults = array('doc_id' => array(), 'doc_slug' => $d_doc_slug, 'group_id' => $d_group_id, 'parent_id' => $d_parent_id, 'author_id' => $d_author_id, 'edited_by_id' => $d_edited_by_id, 'tags' => $d_tags, 'order' => $d_order, 'orderby' => $d_orderby, 'paged' => $d_paged, 'posts_per_page' => $d_posts_per_page, 'search_terms' => $d_search_terms); $r = wp_parse_args($args, $defaults); $doc_query_builder = new BP_Docs_Query($r); $bp->bp_docs->doc_query = $doc_query_builder->get_wp_query(); } return $bp->bp_docs->doc_query->have_posts(); }
/** * @group BP_Docs_Query */ function test_bp_docs_query_null_group() { $g = $this->factory->group->create(); $d1 = $this->factory->doc->create(array('group' => $g)); $d2 = $this->factory->doc->create(); $q = new BP_Docs_Query(array('group_id' => array())); // Remove access protection for the moment because I'm lazy remove_action('pre_get_posts', 'bp_docs_general_access_protection', 28); $wp_query = $q->get_wp_query(); add_action('pre_get_posts', 'bp_docs_general_access_protection', 28); $found = wp_list_pluck($wp_query->posts, 'ID'); $this->assertSame($found, array($d2)); }
/** * Get a list of an item's docs for display in the parent dropdown * * @package BuddyPress Docs * @since 1.0-beta */ function bp_docs_edit_parent_dropdown() { global $bp; $include = array(); $doc_query_builder = new BP_Docs_Query(array('doc_slug' => false, 'posts_per_page' => -1)); $doc_query = $doc_query_builder->get_wp_query(); if ($doc_query->have_posts()) { while ($doc_query->have_posts()) { $doc_query->the_post(); $include[] = get_the_ID(); } } $current_doc = get_queried_object(); $exclude = $parent = false; if (isset($current_doc->post_type) && bp_docs_get_post_type_name() === $current_doc->post_type) { $exclude = array($current_doc->ID); $parent = $current_doc->post_parent; } $pages = wp_dropdown_pages(array('post_type' => $bp->bp_docs->post_type_name, 'exclude' => $exclude, 'include' => $include, 'selected' => $parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)', 'bp-docs'), 'sort_column' => 'menu_order, post_title', 'echo' => 0)); echo $pages; }
/** * Get a list of an item's docs for display in the parent dropdown * * @package BuddyPress Docs * @since 1.0-beta */ function bp_docs_edit_parent_dropdown() { global $bp; $include = array(); $query_args = apply_filters('bp_docs_parent_dropdown_query_args', array('doc_slug' => false, 'posts_per_page' => -1)); $doc_query_builder = new BP_Docs_Query($query_args); $doc_query = $doc_query_builder->get_wp_query(); if ($doc_query->have_posts()) { while ($doc_query->have_posts()) { $doc_query->the_post(); $include[] = get_the_ID(); } } $current_doc = get_queried_object(); $exclude = $parent = false; // If this is a failed submission, use the value from the POST cookie if (!empty(buddypress()->bp_docs->submitted_data->parent_id)) { $parent = intval(buddypress()->bp_docs->submitted_data->parent_id); } else { if (isset($current_doc->post_type) && bp_docs_get_post_type_name() === $current_doc->post_type) { $exclude = array($current_doc->ID); $parent = $current_doc->post_parent; } } $pages = wp_dropdown_pages(array('post_type' => $bp->bp_docs->post_type_name, 'exclude' => $exclude, 'include' => $include, 'selected' => $parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)', 'bp-docs'), 'sort_column' => 'menu_order, post_title', 'echo' => 0)); echo $pages; }
/** * Update the Doc count for a given item * * @since 1.2 */ function bp_docs_update_doc_count($item_id = 0, $item_type = '') { global $bp; $doc_count = 0; $docs_args = array('doc_slug' => ''); switch ($item_type) { case 'group': $docs_args['author_id'] = ''; $docs_args['group_id'] = $item_id; break; case 'user': $docs_args['author_id'] = $item_id; $docs_args['group_id'] = ''; break; default: $docs_args['author_id'] = ''; $docs_args['group_id'] = ''; break; } $query = new BP_Docs_Query($docs_args); $query->get_wp_query(); if ($query->query->have_posts()) { $doc_count = $query->query->found_posts; } // BP has a stupid bug that makes it delete groupmeta when it equals 0. We'll save // a string instead of zero to work around this if (!$doc_count) { $doc_count = '0'; } // Save the count switch ($item_type) { case 'group': groups_update_groupmeta($item_id, 'bp-docs-count', $doc_count); break; case 'user': update_user_meta($item_id, 'bp_docs_count', $doc_count); break; default: bp_update_option('bp_docs_count', $doc_count); break; } return $doc_count; }
/** * Initiates a BuddyPress Docs query * * @since 1.2 */ function bp_docs_has_docs($args = array()) { global $bp, $wp_query; // The if-empty is because, like with WP itself, we use bp_docs_has_docs() both for the // initial 'if' of the loop, as well as for the 'while' iterator. Don't want infinite // queries if (empty($bp->bp_docs->doc_query)) { // Build some intelligent defaults // Default to current group id, if available if (bp_is_active('groups') && bp_is_group()) { $d_group_id = bp_get_current_group_id(); } else { if (!empty($_REQUEST['group_id'])) { // This is useful for the AJAX request for folder contents. $d_group_id = $_REQUEST['group_id']; } else { if (bp_docs_is_mygroups_directory()) { $my_groups = groups_get_user_groups(bp_loggedin_user_id()); $d_group_id = !empty($my_groups['total']) ? $my_groups['groups'] : array(0); } else { $d_group_id = null; } } } // If this is a Started By tab, set the author ID $d_author_id = bp_docs_is_started_by() ? bp_displayed_user_id() : array(); // If this is an Edited By tab, set the edited_by id $d_edited_by_id = bp_docs_is_edited_by() ? bp_displayed_user_id() : array(); // Default to the tags in the URL string, if available $d_tags = isset($_REQUEST['bpd_tag']) ? explode(',', urldecode($_REQUEST['bpd_tag'])) : array(); // Order and orderby arguments $d_orderby = !empty($_GET['orderby']) ? urldecode($_GET['orderby']) : apply_filters('bp_docs_default_sort_order', 'modified'); if (empty($_GET['order'])) { // If no order is explicitly stated, we must provide one. // It'll be different for date fields (should be DESC) if ('modified' == $d_orderby || 'date' == $d_orderby) { $d_order = 'DESC'; } else { $d_order = 'ASC'; } } else { $d_order = $_GET['order']; } // Search $d_search_terms = !empty($_GET['s']) ? urldecode($_GET['s']) : ''; // Parent id $d_parent_id = !empty($_REQUEST['parent_doc']) ? (int) $_REQUEST['parent_doc'] : ''; // Folder id $d_folder_id = null; if (!empty($_GET['folder'])) { $d_folder_id = intval($_GET['folder']); } else { if (bp_docs_enable_folders_for_current_context()) { /* * 0 means we exclude docs that are in a folder. * So we only want this to be set in folder-friendly contexts. */ $d_folder_id = 0; } } // Page number, posts per page $d_paged = 1; if (!empty($_GET['paged'])) { $d_paged = absint($_GET['paged']); } else { if (bp_docs_is_global_directory() && is_a($wp_query, 'WP_Query') && 1 < $wp_query->get('paged')) { $d_paged = absint($wp_query->get('paged')); } else { $d_paged = absint($wp_query->get('paged', 1)); } } // Use the calculated posts_per_page number from $wp_query->query_vars. // If that value isn't set, we assume 10 posts per page. $d_posts_per_page = absint($wp_query->get('posts_per_page', 10)); // doc_slug $d_doc_slug = !empty($bp->bp_docs->query->doc_slug) ? $bp->bp_docs->query->doc_slug : ''; $defaults = array('doc_id' => array(), 'doc_slug' => $d_doc_slug, 'group_id' => $d_group_id, 'parent_id' => $d_parent_id, 'folder_id' => $d_folder_id, 'author_id' => $d_author_id, 'edited_by_id' => $d_edited_by_id, 'tags' => $d_tags, 'order' => $d_order, 'orderby' => $d_orderby, 'paged' => $d_paged, 'posts_per_page' => $d_posts_per_page, 'search_terms' => $d_search_terms, 'update_attachment_cache' => false); if (function_exists('bp_parse_args')) { $r = bp_parse_args($args, $defaults, 'bp_docs_has_docs'); } else { $r = wp_parse_args($args, $defaults); } $doc_query_builder = new BP_Docs_Query($r); $bp->bp_docs->doc_query = $doc_query_builder->get_wp_query(); if ($r['update_attachment_cache']) { $doc_ids = wp_list_pluck($bp->bp_docs->doc_query->posts, 'ID'); $att_hash = array_fill_keys($doc_ids, array()); if ($doc_ids) { $attachments = get_posts(array('post_type' => 'attachment', 'post_parent__in' => $doc_ids, 'update_post_term_cache' => false)); foreach ($attachments as $a) { $att_hash[$a->post_parent][] = $a; } foreach ($att_hash as $doc_id => $doc_atts) { wp_cache_set('bp_docs_attachments:' . $doc_id, $doc_atts, 'bp_docs_nonpersistent'); } } } } return $bp->bp_docs->doc_query->have_posts(); }
/** * Get a list of an item's docs for display in the parent dropdown * * @package BuddyPress Docs * @since 1.0-beta */ function bp_docs_edit_parent_dropdown() { global $bp; $include = array(); $doc_query_builder = new BP_Docs_Query(array('doc_slug' => false, 'posts_per_page' => -1)); $doc_query = $doc_query_builder->get_wp_query(); if ($doc_query->have_posts()) { while ($doc_query->have_posts()) { $doc_query->the_post(); $include[] = get_the_ID(); } } // Exclude the current doc, if this is 'edit' and not 'create' mode $exclude = !empty($bp->bp_docs->current_post->ID) ? array($bp->bp_docs->current_post->ID) : false; // Highlight the existing parent doc, if any $parent = !empty($bp->bp_docs->current_post->post_parent) ? $bp->bp_docs->current_post->post_parent : false; $pages = wp_dropdown_pages(array('post_type' => $bp->bp_docs->post_type_name, 'exclude' => $exclude, 'include' => $include, 'selected' => $parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)', 'bp-docs'), 'sort_column' => 'menu_order, post_title', 'echo' => 0)); echo $pages; }