/**
  * Updates the category to the database via the cnTerm class.
  *
  * @access public
  * @since unknown
  * @return bool
  */
 public function update()
 {
     $args = array('name' => $this->name, 'slug' => $this->slug, 'description' => $this->description, 'parent' => $this->parent);
     // Make sure the category isn't being set to itself as a parent.
     if ($this->id === $this->parent) {
         cnMessage::set('error', 'category_self_parent');
         return FALSE;
     }
     remove_filter('pre_term_description', 'wp_filter_kses');
     $result = cnTerm::update($this->id, 'category', $args);
     if (is_wp_error($result)) {
         cnMessage::set('error', $result->get_error_message());
         return FALSE;
     } else {
         cnMessage::set('success', 'category_updated');
         return TRUE;
     }
 }
 /**
  * Update one item from the collection
  *
  * @since 8.5.26
  *
  * @param WP_REST_Request $request Full data about the request.
  *
  * @return WP_Error|WP_REST_Request
  */
 public function update_item($request)
 {
     if (isset($request['parent'])) {
         //if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
         //
         //	return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Can not set resource parent, taxonomy is not hierarchical.' ), array( 'status' => 400 ) );
         //}
         $parent = cnTerm::get((int) $request['parent'], $this->taxonomy);
         if (!$parent) {
             return new WP_Error('rest_term_invalid', __("Parent resource doesn't exist.", 'connections'), array('status' => 400));
         }
     }
     $prepared_term = $this->prepare_item_for_database($request);
     $term = cnTerm::get((int) $request['id'], $this->taxonomy);
     // Only update the term if we haz something to update.
     if (!empty($prepared_term)) {
         $update = cnTerm::update($term->term_id, $term->taxonomy, (array) $prepared_term);
         if (is_wp_error($update)) {
             return $update;
         }
     }
     $term = cnTerm::get((int) $request['id'], $this->taxonomy);
     /* This action is documented in lib/endpoints/class-wp-rest-terms-controller.php */
     do_action("cn_rest_insert_{$this->taxonomy}", $term, $request, FALSE);
     $fields_update = $this->update_additional_fields_for_object($term, $request);
     if (is_wp_error($fields_update)) {
         return $fields_update;
     }
     $request->set_param('context', 'view');
     $response = $this->prepare_item_for_response($term, $request);
     return rest_ensure_response($response);
 }
 /**
  * Updates the category to the database via the cnTerm class.
  *
  * @access public
  * @since unknown
  * @return bool
  */
 public function update()
 {
     $args = array('name' => $this->name, 'slug' => $this->slug, 'description' => $this->description, 'parent' => $this->parent);
     // Make sure the category isn't being set to itself as a parent.
     if ($this->id === $this->parent) {
         cnMessage::set('error', 'category_self_parent');
         return FALSE;
     }
     // @todo Add option for user to set the default category, which should not be able to be deleted.
     //$defaults['default'] = get_option( 'cn_default_category' );
     // Temporarily hard code the default category to the Uncategorized category
     // and ensure it can not be deleted. This should be removed when the default
     // category can be set by the user.
     $default_category = cnTerm::getBy('slug', 'uncategorized', 'category');
     $defaults['default'] = $default_category->term_id;
     // Do not change the default category.
     // This should be able to be removed after the user configurable default category is implemented.
     if ($this->id == $default_category->term_id) {
         cnMessage::set('error', 'category_update_uncategorized');
         return FALSE;
     }
     remove_filter('pre_term_description', 'wp_filter_kses');
     $result = cnTerm::update($this->id, 'category', $args);
     if (is_wp_error($result)) {
         cnMessage::set('error', $result->get_error_message());
         return FALSE;
     } else {
         cnMessage::set('success', 'category_updated');
         return TRUE;
     }
 }
Example #4
0
 /**
  * Updates a term.
  *
  * @deprecated 8.1.6 Use {@see cnTerm::update()} instead.
  * @see cnTerm::update()
  *
  * $termID - (int) ID of the term to be updated
  * $taxonomy - (string) taxonomy of the term to be updated
  * $attributes - (array)    name - (string)
  *                          slug - (string)
  *                          parent - (int)
  *                          description - (string)
  *
  * @param int    $termID
  * @param string $taxonomy
  * @param array  $attributes
  *
  * @return bool
  */
 public function updateTerm($termID, $taxonomy, $attributes)
 {
     $result = cnTerm::update($termID, $taxonomy, $attributes);
     return $result;
 }
 /**
  * Update term based on arguments provided.
  *
  * The $args will indiscriminately override all values with the same field name.
  * Care must be taken to not override important information needed to update or the
  * update will fail (or perhaps create a new term, neither would be acceptable).
  *
  * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
  * defined in $args already.
  *
  * 'alias_of' will create a term group, if it does not already exist, and update
  * it for the $term.
  *
  * If the 'slug' argument in $args is missing, then the 'name' in $args will be
  * used. It should also be noted that if you set 'slug' and it isn't unique then
  * a WP_Error will be passed back. If you don't pass any slug, then a unique one
  * will be created for you.
  *
  * For what can be overrode in $args, check the term scheme can contain and stay
  * away from the term keys.
  *
  * NOTE: This is the Connections equivalent of @see wp_update_term() in WordPress core ../wp-includes/taxonomy.php
  *
  * Actions:
  *    cn_edit_terms
  *        Passes: (int) $term_id, (string) $taxonomy
  *
  *    cn_edited_terms
  *        Passes: (int) $term_id, (string) $taxonomy
  *
  *    cn_edit_term_taxonomy
  *        Passes: (int) $term_taxonomy_id, (string) $taxonomy
  *
  *    cn_edited_term_taxonomy
  *        Passes: (int) $term_taxonomy_id, (string) $taxonomy
  *
  *    cn_edit_term
  *        Passes: (int) $term_id, (int) $taxonomy_term_id, (string) $taxonomy
  *
  *    cn_edit_$taxonomy
  *        Passes: (int) $taxonomy_term_id, (string) $taxonomy
  *
  *    cn_edited_term
  *        Passes: (int) $term_id, (int) $taxonomy_term_id, (string) $taxonomy
  *
  *    cn_edited_$taxonomy
  *        Passes: (int) $taxonomy_term_id, (string) $taxonomy
  *
  * Filters:
  *
  *    cn_update_term_parent
  *        Passes: (int) $parent_term_id, (int) $term_id, (string) $taxonomy, (array) $parsed_args, (array) $args
  *        Returns: $parent_term_id
  *
  *    cn_term_id_filter
  *        Passes: (int) $term_id, (int) $taxonomy_term_id
  *        Return: $term_id
  *
  * @access public
  * @since  8.1.6
  * @static
  *
  * @global wpdb $wpdb
  *
  * @param int    $term_id  The ID of the term
  * @param string $taxonomy The context in which to relate the term to the object.
  * @param array  $args     {
  *    Optional. Overwrite term field values.
  *
  *    @type string 'alias_of'        Slug of the term to make this term an alias of.
  *                                   Default: empty string.
  *                                   Accepts a term slug.
  *    @type string 'description'     The term description.
  *                                   Default: empty string.
  *    @type int    'parent'          The id of the parent term.
  *                                   Default: 0.
  *    @type string 'slug'            The term slug to use.
  *                                   Default: empty string.
  * }
  *
  * @return array|WP_Error Returns Term ID and Taxonomy Term ID or an instance of the WP_Error object.
  */
 public static function update($term_id, $taxonomy, $args = array())
 {
     global $wpdb;
     //@todo Add taxonomy check.
     //if ( ! taxonomy_exists($taxonomy) )
     //	return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
     $term_id = (int) $term_id;
     // First, get all of the original args
     $term = self::get($term_id, $taxonomy);
     if (is_wp_error($term)) {
         return $term;
     }
     if (!$term) {
         return new WP_Error('invalid_term', __('Empty Term', 'connections'));
     }
     $term = (array) $term->data;
     // Escape data pulled from DB.
     $term = wp_slash($term);
     // Merge old and new args with new args overwriting old ones.
     $args = array_merge($term, $args);
     $defaults = array('alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
     $args = wp_parse_args($args, $defaults);
     $args = sanitize_term($args, 'cn_' . $taxonomy, 'db');
     $parsed_args = $args;
     // expected_slashed ($name)
     $name = wp_unslash($args['name']);
     $description = wp_unslash($args['description']);
     $parsed_args['name'] = $name;
     $parsed_args['description'] = $description;
     if ('' == trim($name)) {
         return new WP_Error('empty_term_name', __('A name is required for this term', 'connections'));
     }
     if (0 < $parsed_args['parent'] && !cnTerm::exists((int) $parsed_args['parent'])) {
         return new WP_Error('missing_parent', __('Parent term does not exist.', 'connections'));
     }
     $empty_slug = FALSE;
     if (empty($args['slug'])) {
         $empty_slug = TRUE;
         $slug = sanitize_title($name);
     } else {
         $slug = $args['slug'];
     }
     $parsed_args['slug'] = $slug;
     $term_group = isset($parsed_args['term_group']) ? $parsed_args['term_group'] : 0;
     if ($args['alias_of']) {
         $alias = cnTerm::getBy('slug', $args['alias_of'], $taxonomy);
         if (!empty($alias->term_group)) {
             // The alias we want is already in a group, so let's use that one.
             $term_group = $alias->term_group;
         } elseif (!empty($alias->term_id)) {
             /*
              * The alias is not in a group, so we create a new one and add the alias to it.
              */
             $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM {$wpdb->terms}") + 1;
             cnTerm::update($alias->term_id, $taxonomy, array('term_group' => $term_group));
         }
         $parsed_args['term_group'] = $term_group;
     }
     /**
      * Filter the term parent.
      *
      * Hook to this filter to see if it will cause a hierarchy loop.
      *
      * @since 8.1.6
      *
      * @param int    $parent      ID of the parent term.
      * @param int    $term_id     Term ID.
      * @param string $taxonomy    Taxonomy slug.
      * @param array  $parsed_args An array of potentially altered update arguments for the given term.
      * @param array  $args        An array of update arguments for the given term.
      */
     $parent = apply_filters('cn_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args);
     // Check for duplicate slug
     $duplicate = self::getBy('slug', $slug, $taxonomy);
     if ($duplicate && $duplicate->term_id != $term_id) {
         // If an empty slug was passed or the parent changed, reset the slug to something unique.
         // Otherwise, bail.
         if ($empty_slug || $parent != $term['parent']) {
             $slug = self::unique_slug($slug, (object) $args);
         } else {
             return new WP_Error('duplicate_term_slug', sprintf(__('The slug &#8220;%s&#8221; is already in use by another term', 'connections'), $slug));
         }
     }
     $tt_id = (int) $wpdb->get_var($wpdb->prepare("SELECT tt.term_taxonomy_id FROM " . CN_TERM_TAXONOMY_TABLE . " AS tt INNER JOIN " . CN_TERMS_TABLE . " AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id));
     // Check whether this is a shared term that needs splitting.
     //$_term_id = _split_shared_term( $term_id, $tt_id );
     //if ( ! is_wp_error( $_term_id ) ) {
     //	$term_id = $_term_id;
     //}
     /**
      * Fires immediately before the given terms are edited.
      *
      * @since 8.1.6
      *
      * @param int    $term_id  Term ID.
      * @param string $taxonomy Taxonomy slug.
      */
     do_action('cn_edit_terms', $term_id, $taxonomy);
     $wpdb->update(CN_TERMS_TABLE, compact('name', 'slug', 'term_group'), compact('term_id'));
     if (empty($slug)) {
         $slug = sanitize_title($name, $term_id);
         $wpdb->update(CN_TERMS_TABLE, compact('slug'), compact('term_id'));
     }
     /**
      * Fires immediately after the given terms are edited.
      *
      * @since 8.1.6
      *
      * @param int    $term_id  Term ID
      * @param string $taxonomy Taxonomy slug.
      */
     do_action('cn_edited_terms', $term_id, $taxonomy);
     /**
      * Fires immediate before a term-taxonomy relationship is updated.
      *
      * @since 8.1.6
      *
      * @param int    $tt_id    Term taxonomy ID.
      * @param string $taxonomy Taxonomy slug.
      */
     do_action('cn_edit_term_taxonomy', $tt_id, $taxonomy);
     $wpdb->update(CN_TERM_TAXONOMY_TABLE, compact('term_id', 'taxonomy', 'description', 'parent'), array('term_taxonomy_id' => $tt_id));
     /**
      * Fires immediately after a term-taxonomy relationship is updated.
      *
      * @since 8.1.6
      *
      * @param int    $tt_id    Term taxonomy ID.
      * @param string $taxonomy Taxonomy slug.
      */
     do_action('cn_edited_term_taxonomy', $tt_id, $taxonomy);
     // Clean the relationship caches for all object types using this term
     $objects = $wpdb->get_col($wpdb->prepare("SELECT entry_id FROM " . CN_TERM_RELATIONSHIP_TABLE . " WHERE term_taxonomy_id = %d", $tt_id));
     //@todo implement the following block of code.
     //$tax_object = get_taxonomy( $taxonomy );
     //foreach ( $tax_object->object_type as $object_type ) {
     //	self::cleanRelationshipCache( $objects, $object_type );
     self::cleanRelationshipCache($objects, $taxonomy);
     // Clean the entry/term relationships directly until get_taxonomy() is implemented.
     //}
     /**
      * Fires after a term has been updated, but before the term cache has been cleaned.
      *
      * @since 8.1.6
      *
      * @param int    $term_id  Term ID.
      * @param int    $tt_id    Term taxonomy ID.
      * @param string $taxonomy Taxonomy slug.
      */
     do_action("cn_edit_term", $term_id, $tt_id, $taxonomy);
     /**
      * Fires after a term in a specific taxonomy has been updated, but before the term
      * cache has been cleaned.
      *
      * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.
      *
      * @since 8.1.6
      *
      * @param int $term_id Term ID.
      * @param int $tt_id   Term taxonomy ID.
      */
     do_action("cn_edit_{$taxonomy}", $term_id, $tt_id);
     /** @see cnTerm::insert() */
     $term_id = apply_filters('cn_term_id_filter', $term_id, $tt_id);
     self::cleanCache($term_id, $taxonomy);
     /**
      * Fires after a term has been updated, and the term cache has been cleaned.
      *
      * @since 8.1.6
      *
      * @param int    $term_id  Term ID.
      * @param int    $tt_id    Term taxonomy ID.
      * @param string $taxonomy Taxonomy slug.
      */
     do_action("cn_edited_term", $term_id, $tt_id, $taxonomy);
     /**
      * Fires after a term for a specific taxonomy has been updated, and the term
      * cache has been cleaned.
      *
      * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.
      *
      * @since 8.1.6
      *
      * @param int $term_id Term ID.
      * @param int $tt_id   Term taxonomy ID.
      */
     do_action("cn_edited_{$taxonomy}", $term_id, $tt_id);
     return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
 }