/**
  * see #286
  */
 function test_change_group_association()
 {
     $group = $this->factory->group->create();
     $group2 = $this->factory->group->create();
     $doc_id = $this->factory->doc->create(array('group' => $group));
     bp_docs_set_associated_group_id($doc_id, $group);
     $this->assertEquals(bp_docs_get_associated_group_id($doc_id), $group);
 }
 function create_object($args)
 {
     $post_id = wp_insert_post($args);
     if (isset($args['group'])) {
         bp_docs_set_associated_group_id($post_id, $args['group']);
     }
     return $post_id;
 }
 /**
  * Saves a doc.
  *
  * This method handles saving for both new and existing docs. It detects the difference by
  * looking for the presence of $this->doc_slug
  *
  * @since 1.0-beta
  */
 function save($args = false)
 {
     global $bp, $wp_rewrite;
     // bbPress plays naughty with revision saving
     add_action('pre_post_update', 'wp_save_post_revision');
     // Get the required taxonomy items associated with the group. We only run this
     // on a save because it requires extra database hits.
     $this->setup_terms();
     // Set up the default value for the result message
     $results = array('message' => __('Unknown error. Please try again.', 'bp-docs'), 'redirect' => 'create');
     // Backward compatibility. Had to change to doc_content to work with wp_editor
     $doc_content = '';
     if (isset($_POST['doc_content'])) {
         $doc_content = $_POST['doc_content'];
     } else {
         if (isset($_POST['doc']['content'])) {
             $doc_content = $_POST['doc']['content'];
         }
     }
     // Check group associations
     // @todo Move into group integration piece
     // This group id is only used to check whether the user can associate the doc with the group.
     $associated_group_id = isset($_POST['associated_group_id']) ? intval($_POST['associated_group_id']) : null;
     if (bp_is_active('groups')) {
         if (!empty($associated_group_id) && !current_user_can('bp_docs_associate_with_group', $associated_group_id)) {
             $retval = array('message_type' => 'error', 'message' => __('You are not allowed to associate a Doc with that group.', 'bp-docs'), 'redirect_url' => bp_docs_get_create_link());
             return $retval;
         }
     }
     if (empty($_POST['doc']['title'])) {
         // The title field is required
         $result['message'] = __('The title field is required.', 'bp-docs');
         $result['redirect'] = !empty($this->doc_slug) ? 'edit' : 'create';
     } else {
         $defaults = array('post_type' => $this->post_type_name, 'post_title' => $_POST['doc']['title'], 'post_name' => isset($_POST['doc']['permalink']) ? sanitize_title($_POST['doc']['permalink']) : sanitize_title($_POST['doc']['title']), 'post_content' => sanitize_post_field('post_content', $doc_content, 0, 'db'), 'post_status' => 'publish');
         $r = wp_parse_args($args, $defaults);
         if (empty($this->doc_slug)) {
             $this->is_new_doc = true;
             $r['post_author'] = bp_loggedin_user_id();
             // If there's a 'doc_id' value in the POST, use
             // the autodraft as a starting point
             if (isset($_POST['doc_id']) && 0 != $_POST['doc_id']) {
                 $post_id = (int) $_POST['doc_id'];
                 $r['ID'] = $post_id;
                 wp_update_post($r);
             } else {
                 $post_id = wp_insert_post($r);
             }
             if (!$post_id) {
                 $result['message'] = __('There was an error when creating the doc.', 'bp-docs');
                 $result['redirect'] = 'create';
             } else {
                 $this->doc_id = $post_id;
                 $the_doc = get_post($this->doc_id);
                 $this->doc_slug = $the_doc->post_name;
                 // A normal, successful save
                 $result['message'] = __('Doc successfully created!', 'bp-docs');
                 $result['redirect'] = 'single';
             }
         } else {
             $this->is_new_doc = false;
             $doc = bp_docs_get_current_doc();
             $this->doc_id = $doc->ID;
             $r['ID'] = $this->doc_id;
             // Make sure the post_name is set
             if (empty($r['post_name'])) {
                 $r['post_name'] = sanitize_title($r['post_title']);
             }
             // Make sure the post_name is unique
             $r['post_name'] = wp_unique_post_slug($r['post_name'], $this->doc_id, $r['post_status'], $this->post_type_name, $doc->post_parent);
             $this->doc_slug = $r['post_name'];
             // Save pre-update post data, for comparison by callbacks.
             $this->previous_revision = clone $doc;
             if (!wp_update_post($r)) {
                 $result['message'] = __('There was an error when saving the doc.', 'bp-docs');
                 $result['redirect'] = 'edit';
             } else {
                 // Remove the edit lock
                 delete_post_meta($this->doc_id, '_edit_lock');
                 delete_post_meta($this->doc_id, '_bp_docs_last_pinged');
                 // When the post has been autosaved, we need to leave a
                 // special success message
                 if (!empty($_POST['is_auto']) && $_POST['is_auto']) {
                     $result['message'] = __('You idled a bit too long while in Edit mode. In order to allow others to edit the doc you were working on, your changes have been autosaved. Click the Edit button to return to Edit mode.', 'bp-docs');
                 } else {
                     // A normal, successful save
                     $result['message'] = __('Doc successfully edited!', 'bp-docs');
                 }
                 $result['redirect'] = 'single';
             }
             $post_id = $this->doc_id;
         }
     }
     // If the Doc was successfully created, run some more stuff
     if (!empty($post_id)) {
         // Add to a group, if necessary
         if (!is_null($associated_group_id)) {
             bp_docs_set_associated_group_id($post_id, $associated_group_id);
         }
         // Make sure the current user is added as one of the authors
         wp_set_post_terms($post_id, $this->user_term_id, $this->associated_item_tax_name, true);
         // Save the last editor id. We'll use this to create an activity item
         update_post_meta($this->doc_id, 'bp_docs_last_editor', bp_loggedin_user_id());
         // Save settings
         bp_docs_save_doc_access_settings($this->doc_id);
         // Increment the revision count
         $revision_count = get_post_meta($this->doc_id, 'bp_docs_revision_count', true);
         update_post_meta($this->doc_id, 'bp_docs_revision_count', intval($revision_count) + 1);
     }
     // Provide a custom hook for plugins and optional components.
     // WP's default save_post isn't enough, because we need something that fires
     // only when we save from the front end (for things like taxonomies, which
     // the WP admin handles automatically)
     do_action('bp_docs_doc_saved', $this);
     do_action('bp_docs_after_save', $this->doc_id);
     $message_type = $result['redirect'] == 'single' ? 'success' : 'error';
     // Stuff data into a cookie so it can be accessed on next page load
     if ('error' === $message_type) {
         setcookie('bp-docs-submit-data', json_encode($_POST), time() + 30, '/');
     }
     $redirect_base = trailingslashit(bp_get_root_domain());
     if ($wp_rewrite->using_index_permalinks()) {
         $redirect_base .= 'index.php/';
     }
     $redirect_url = apply_filters('bp_docs_post_save_redirect_base', trailingslashit($redirect_base . bp_docs_get_docs_slug()));
     if ($result['redirect'] == 'single') {
         $redirect_url .= $this->doc_slug;
     } else {
         if ($result['redirect'] == 'edit') {
             $redirect_url .= $this->doc_slug . '/' . BP_DOCS_EDIT_SLUG;
         } else {
             if ($result['redirect'] == 'create') {
                 $redirect_url .= BP_DOCS_CREATE_SLUG;
             }
         }
     }
     $retval = array('message_type' => $message_type, 'message' => $result['message'], 'redirect_url' => $redirect_url);
     return $retval;
 }
示例#4
0
 /**
  * Saves a doc.
  *
  * This method handles saving for both new and existing docs. It detects the difference by
  * looking for the presence of $this->doc_slug
  *
  * @package BuddyPress Docs
  * @since 1.0-beta
  */
 function save($args = false)
 {
     global $bp;
     // bbPress plays naughty with revision saving
     add_action('pre_post_update', 'wp_save_post_revision');
     // Get the required taxonomy items associated with the group. We only run this
     // on a save because it requires extra database hits.
     $this->setup_terms();
     // Set up the default value for the result message
     $results = array('message' => __('Unknown error. Please try again.', 'bp-docs'), 'redirect' => 'create');
     // Backward compatibility. Had to change to doc_content to work with wp_editor
     $doc_content = '';
     if (isset($_POST['doc_content'])) {
         $doc_content = $_POST['doc_content'];
     } else {
         if (isset($_POST['doc']['content'])) {
             $doc_content = $_POST['doc']['content'];
         }
     }
     // Check group associations
     // @todo Move into group integration piece
     if (bp_is_active('groups')) {
         $associated_group_id = isset($_POST['associated_group_id']) ? intval($_POST['associated_group_id']) : 0;
         if ($associated_group_id && !BP_Docs_Groups_Integration::user_can_associate_doc_with_group(bp_loggedin_user_id(), $associated_group_id)) {
             bp_core_add_message(__('You are not allowed to associate a Doc with that group.', 'bp-docs'), 'error');
             bp_core_redirect(bp_docs_get_create_link());
         }
     }
     if (empty($_POST['doc']['title']) || empty($doc_content)) {
         // Both the title and the content fields are required
         $result['message'] = __('Both the title and the content fields are required.', 'bp-docs');
         $result['redirect'] = $this->current_view;
     } else {
         // If both the title and content fields are filled in, we can proceed
         $defaults = array('post_type' => $this->post_type_name, 'post_title' => $_POST['doc']['title'], 'post_name' => isset($_POST['doc']['permalink']) ? sanitize_title($_POST['doc']['permalink']) : sanitize_title($_POST['doc']['title']), 'post_content' => stripslashes(sanitize_post_field('post_content', $doc_content, 0, 'db')), 'post_status' => 'publish');
         $r = wp_parse_args($args, $defaults);
         if (empty($this->doc_slug)) {
             $this->is_new_doc = true;
             $r['post_author'] = bp_loggedin_user_id();
             // This is a new doc
             if (!($post_id = wp_insert_post($r))) {
                 $result['message'] = __('There was an error when creating the doc.', 'bp-docs');
                 $result['redirect'] = 'create';
             } else {
                 $this->doc_id = $post_id;
                 $the_doc = get_post($this->doc_id);
                 $this->doc_slug = $the_doc->post_name;
                 // A normal, successful save
                 $result['message'] = __('Doc successfully created!', 'bp-docs');
                 $result['redirect'] = 'single';
             }
         } else {
             $this->is_new_doc = false;
             $doc = get_queried_object();
             $this->doc_id = $doc->ID;
             $r['ID'] = $this->doc_id;
             // Make sure the post_name is set
             if (empty($r['post_name'])) {
                 $r['post_name'] = sanitize_title($r['post_title']);
             }
             // Make sure the post_name is unique
             $r['post_name'] = wp_unique_post_slug($r['post_name'], $this->doc_id, $r['post_status'], $this->post_type_name, $doc->post_parent);
             $this->doc_slug = $r['post_name'];
             if (!wp_update_post($r)) {
                 $result['message'] = __('There was an error when saving the doc.', 'bp-docs');
                 $result['redirect'] = 'edit';
             } else {
                 // Remove the edit lock
                 delete_post_meta($this->doc_id, '_edit_lock');
                 // When the post has been autosaved, we need to leave a
                 // special success message
                 if (!empty($_POST['is_auto']) && $_POST['is_auto']) {
                     $result['message'] = __('You idled a bit too long while in Edit mode. In order to allow others to edit the doc you were working on, your changes have been autosaved. Click the Edit button to return to Edit mode.', 'bp-docs');
                 } else {
                     // A normal, successful save
                     $result['message'] = __('Doc successfully edited!', 'bp-docs');
                 }
                 $result['redirect'] = 'single';
             }
             $post_id = $this->doc_id;
         }
     }
     // Add to a group, if necessary
     if (isset($associated_group_id)) {
         bp_docs_set_associated_group_id($post_id, $associated_group_id);
     }
     // Make sure the current user is added as one of the authors
     wp_set_post_terms($post_id, $this->user_term_id, $this->associated_item_tax_name, true);
     // Save the last editor id. We'll use this to create an activity item
     update_post_meta($this->doc_id, 'bp_docs_last_editor', bp_loggedin_user_id());
     // Save settings
     $settings = !empty($_POST['settings']) ? $_POST['settings'] : array();
     $verified_settings = bp_docs_verify_settings($settings, $post_id, bp_loggedin_user_id());
     $new_settings = array();
     foreach ($verified_settings as $verified_setting_name => $verified_setting) {
         $new_settings[$verified_setting_name] = $verified_setting['verified_value'];
         if ($verified_setting['verified_value'] != $verified_setting['original_value']) {
             $result['message'] = __('Your Doc was successfully saved, but some of your access settings have been changed to match the Doc\'s permissions.', 'bp-docs');
         }
     }
     update_post_meta($this->doc_id, 'bp_docs_settings', $new_settings);
     // The 'read' setting must also be saved to a taxonomy, for
     // easier directory queries
     $read_setting = isset($new_settings['read']) ? $new_settings['read'] : 'anyone';
     bp_docs_update_doc_access($this->doc_id, $read_setting);
     // Increment the revision count
     $revision_count = get_post_meta($this->doc_id, 'bp_docs_revision_count', true);
     update_post_meta($this->doc_id, 'bp_docs_revision_count', intval($revision_count) + 1);
     // Provide a custom hook for plugins and optional components.
     // WP's default save_post isn't enough, because we need something that fires
     // only when we save from the front end (for things like taxonomies, which
     // the WP admin handles automatically)
     do_action('bp_docs_doc_saved', $this);
     $message_type = $result['redirect'] == 'single' ? 'success' : 'error';
     $redirect_url = trailingslashit(bp_get_root_domain() . '/' . BP_DOCS_SLUG);
     if ($result['redirect'] == 'single') {
         $redirect_url .= $this->doc_slug;
     } else {
         if ($result['redirect'] == 'edit') {
             $redirect_url .= $this->doc_slug . '/' . BP_DOCS_EDIT_SLUG;
         } else {
             if ($result['redirect'] == 'create') {
                 $redirect_url .= BP_DOCS_CREATE_SLUG;
             }
         }
     }
     $retval = array('message_type' => $message_type, 'message' => $result['message'], 'redirect_url' => $redirect_url);
     return $retval;
 }