예제 #1
0
 /**
  * Get the default category ID.
  *
  * NOTE: This is also the callback for the `default_option_{name}` filter @see get_option().
  *
  * NOTE: Uses the @see get_option() and @see update_option() functions instead of the @see cnSettingsAPI()
  *       because it is used in places where the cnSettingsAPI() has not yet been fully initialized.
  *
  * @access public
  * @since  8.3.3
  * @static
  *
  * @uses   remove_filter()
  * @uses   get_option()
  * @uses   cnTerm::exists()
  * @uses   cnTerm::getBy()
  * @uses   cnTerm::insert()
  * @uses   update_option()
  * @uses   is_wp_error()
  * @uses   add_filter()
  *
  * @return int
  */
 public static function getDefaultCategoryID()
 {
     $id = 0;
     // Remove filter to prevent an infinite loop.
     remove_filter('default_option_cn_default_category', array(__CLASS__, 'getDefaultCategoryID'));
     // Use get_option() rather than cnSettingsAPI::get() because the class may not yet be initialized.
     $category = get_option('connections_category');
     // Check to ensure the default category ID is saved in the options table before returning it.
     if (FALSE === $category || !isset($category['default']) || empty($category['default'])) {
         // If there was no default category set, check for the "Uncategorized" category. If it exists return its
         // `id` and if it does not, then create it an return the `id`.
         if (cnTerm::exists('uncategorized', 'category')) {
             $category = cnTerm::getBy('slug', 'uncategorized', 'category', ARRAY_A);
             // Ensure nothing went wrong when checking for the "Uncategorized" category.
             // If not, save the `id` in the options table.
             if (FALSE !== $category) {
                 $id = $category['term_id'];
                 // Use update_option() rather than cnSettingsAPI::set() because the class may not yet be initialized.
                 update_option('connections_category', array('default' => $id));
             }
         } else {
             $category = cnTerm::insert(__('Uncategorized', 'connections'), 'category');
             // Ensure nothing went wrong when inserting the "Uncategorized" category.
             // If not, save the `id` in the options table.
             if (!is_wp_error($category)) {
                 $id = $category['term_id'];
                 // Use update_option() rather than cnSettingsAPI::set() because the class may not yet be initialized.
                 update_option('connections_category', array('default' => $id));
             }
         }
     } else {
         $id = $category['default'];
     }
     // Add the filter back.
     add_filter('default_option_cn_default_category', array(__CLASS__, 'getDefaultCategoryID'));
     /**
      * Allows the opportunity to change the default category.
      *
      * @since 8.3.3
      *
      * @param int $id The default category ID.
      */
     return apply_filters('cn_default_category', $id);
 }
예제 #2
0
 /**
  * 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);
 }