Ejemplo n.º 1
0
/**
 * Get the term_id for the associated_item term corresponding to a item_id
 *
 * Will create it if it's not found
 *
 * @since 1.2
 *
 * @param int $item_id Such as the group_id or user_id
 * @param str $item_type Such as 'user' or 'group' (slug of the parent term)
 * @param str $item_name Optional. This is the value that will be used to describe the term in the
 *    Dashboard.
 * @return int $item_term_id
 */
function bp_docs_get_item_term_id($item_id, $item_type, $item_name = '')
{
    global $bp;
    if (empty($item_id)) {
        return;
    }
    // Sanitization
    // @todo Maybe this should be more generous
    $item_type = 'group' == $item_type ? 'group' : 'user';
    $item_term_slug = 'bp_docs_associated_' . $item_type . '_' . $item_id;
    $item_term = get_term_by('slug', $item_term_slug, bp_docs_get_associated_item_tax_name());
    // If the item term doesn't exist, then create it
    if (empty($item_term)) {
        // Set up the arguments for creating the term. Filter this to set your own
        switch ($item_type) {
            case 'group':
                $item = groups_get_group('group_id=' . $item_id);
                $item_name = $item->name;
                break;
            case 'user':
            default:
                $item_name = bp_core_get_user_displayname($item_id);
                break;
        }
        $item_term_args = apply_filters('bp_docs_item_term_values', array('description' => sprintf(__('Docs associated with the %1$s %2$s', 'bp-docs'), $item_type, $item_name), 'slug' => $item_term_slug));
        // Create the item term
        $item_term = wp_insert_term($item_name, bp_docs_get_associated_item_tax_name(), $item_term_args);
        $term_id = isset($item_term['term_id']) ? $item_term['term_id'] : false;
    } else {
        $term_id = $item_term->term_id;
    }
    return apply_filters('bp_docs_get_item_term_id', $term_id, $item_id, $item_type, $item_name);
}
 /**
  * Delete transient on doc save
  */
 function delete_transient($object_id, $terms, $tt_ids, $taxonomy)
 {
     if (bp_docs_get_associated_item_tax_name() == $taxonomy) {
         delete_transient('associated_groups-' . $object_id);
     }
 }
Ejemplo n.º 3
0
function bp_docs_upgrade_1_2($udata = array())
{
    global $wpdb;
    $url_base = admin_url(add_query_arg(array('post_type' => bp_docs_get_post_type_name(), 'page' => 'bp-docs-upgrade'), 'edit.php'));
    if (empty($udata['total'])) {
        $udata['total'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s", bp_docs_get_post_type_name()));
    }
    if (!isset($udata['done'])) {
        $udata['done'] = 0;
    }
    if (empty($udata['group_terms_migrated'])) {
        $tn = bp_docs_get_associated_item_tax_name();
        // Get the group parent term
        $group_parent_term = term_exists('group', $tn);
        // Get all the group terms
        if ($group_parent_term) {
            // Delete the cached children terms, for good measure
            delete_option($tn . '_children');
            $group_terms = get_terms($tn, array('parent' => intval($group_parent_term['term_id'])));
            foreach ($group_terms as $group_term) {
                // Concatenate new term slugs
                $new_desc = sprintf(__('Docs associated with the group %s', 'bp-docs'), $group_term->description);
                $new_slug = 'bp_docs_associated_group_' . $group_term->name;
                $new_name = $group_term->description;
                wp_update_term($group_term->term_id, $tn, array('description' => $new_desc, 'slug' => $new_slug, 'name' => $new_name, 'parent' => 0));
            }
        }
        // Store that we're done
        $udata['group_terms_migrated'] = 1;
        $udata['message'] = __('Group terms migrated. Now migrating Doc access terms....', 'bp-docs');
        $udata['refresh_url'] = add_query_arg(array('do_upgrade' => '1', '_wpnonce' => wp_create_nonce('bp-docs-upgrade')), $url_base);
        $udata['total'] = 0;
    } else {
        if (intval($udata['done']) < intval($udata['total'])) {
            $counter = 0;
            while ($counter < 5) {
                $next_doc_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND ID > %d LIMIT 1", bp_docs_get_post_type_name(), intval($udata['last'])));
                if (!$next_doc_id) {
                    $udata['done'] = $udata['total'];
                    $all_done = true;
                    break;
                }
                // Set the 'read' setting to a taxonomy
                $doc_settings = get_post_meta($next_doc_id, 'bp_docs_settings', true);
                if (isset($doc_settings['read'])) {
                    $read_setting = $doc_settings['read'];
                } else {
                    $group = groups_get_group('group_id=' . bp_docs_get_associated_group_id($next_doc_id));
                    if (!empty($group->status) && 'public' != $group->status) {
                        $read_setting = 'group-members';
                        // Sanitize settings as well
                        foreach ($doc_settings as $doc_settings_key => $doc_settings_value) {
                            if (in_array($doc_settings_value, array('anyone', 'loggedin'))) {
                                $doc_settings[$doc_settings_key] = 'group-members';
                            }
                        }
                        $doc_settings['read'] = 'group-members';
                        update_post_meta($next_doc_id, 'bp_docs_settings', $doc_settings);
                    } else {
                        $read_setting = 'anyone';
                    }
                }
                bp_docs_update_doc_access($next_doc_id, $read_setting);
                // Count the total number of edits
                $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'revision' AND post_status = 'inherit' AND post_parent = %d", $next_doc_id));
                update_post_meta($next_doc_id, 'bp_docs_revision_count', $count + 1);
                $counter++;
                $udata['done']++;
                $udata['last'] = $next_doc_id;
                $udata['message'] = sprintf(__('Migrated %s of %s Docs. Migrating....', 'bp-docs'), $udata['done'], $udata['total']);
                $udata['refresh_url'] = add_query_arg(array('do_upgrade' => '1', '_wpnonce' => wp_create_nonce('bp-docs-upgrade')), $url_base);
            }
        } else {
            $all_done = true;
            $udata['refresh_url'] = add_query_arg(array('bp_docs_upgraded' => 1), admin_url());
        }
    }
    if (isset($all_done)) {
        bp_update_option('_bp_docs_done_upgrade_1_2', 1);
    }
    return $udata;
}