/** * Saves the category to the database via the cnTerm class. * * @return bool */ public function save() { $args = array('slug' => $this->slug, 'description' => $this->description, 'parent' => $this->parent); remove_filter('pre_term_description', 'wp_filter_kses'); $result = cnTerm::insert($this->name, 'category', $args); if (is_wp_error($result)) { cnMessage::set('error', $result->get_error_message()); return FALSE; } else { cnMessage::set('success', 'category_added'); return TRUE; } }
/** * Helper function to insert a new term. * * @access private * @since 8.5.5 * * @param string $name The term name. * @param string $slug The term slug. * @param string $desc The term description. * @param int $parent The term parent ID. * * @return array|WP_Error An array containing the term_id and term_taxonomy_id, WP_Error otherwise. */ private function insertTerm($name, $slug = '', $desc = '', $parent = 0) { $atts = array('slug' => $slug, 'description' => $desc, 'parent' => $parent); $result = cnTerm::insert($name, $this->type, $atts); if (is_wp_error($result)) { error_log('Term Import Error: ' . $result->get_error_message()); error_log(' - Name: ' . print_r($name, TRUE)); error_log(' - Slug: ' . print_r($slug, TRUE)); error_log(' - Parent ID: ' . print_r($parent, TRUE)); } return $result; }
/** * 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); }
/** * Create 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 create_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::insert($prepared_term->name, $this->taxonomy, $prepared_term); if (is_wp_error($term)) { // If we're going to inform the client that the term exists, give them the identifier they can actually use. if ($term_id = $term->get_error_data('term_exists')) { $existing_term = cnTerm::get($term_id, $this->taxonomy); $term->add_data($existing_term->term_id, 'term_exists'); } return $term; } $term = cnTerm::get($term['term_id'], $this->taxonomy); /** * Fires after a single term is created or updated via the REST API. * * @since 8.5.26 * * @param WP_Term $term Inserted Term object. * @param WP_REST_Request $request Request object. * @param boolean $creating True when creating term, false when updating. */ do_action("cn_rest_insert_{$this->taxonomy}", $term, $request, true); $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); $response = rest_ensure_response($response); $response->set_status(201); $response->header('Location', rest_url($this->namespace . '/' . $this->rest_base . '/' . $term->term_id)); return $response; }
/** * Adds a new term. * * $term - (string) Term name. * $taxonomy - (string) taxonomy of the term to be updated * $attributes - (array) slug - (string) * parent - (int) * description - (string) * * @access public * @deprecated 8.1.6 Use {@see cnTerm::insert()} instead. * @see cnTerm::insert() * * @param string $term * @param string $taxonomy * @param array $attributes * * @return int The term id. */ public function addTerm($term, $taxonomy, $attributes) { $result = cnTerm::insert($term, $taxonomy, $attributes); return $result; }
/** * Add the "Uncategorized" category" * * @access private * @since 0.7.5 * * @return void */ private static function addDefaultCategory() { $term = cnTerm::getBy('slug', 'uncategorized', 'category'); if (!$term) { $attributes['slug'] = ''; $attributes['parent'] = 0; $attributes['description'] = __('Entries not assigned to a category will automatically be assigned to this category and deleting a category which has been assigned to an entry will reassign that entry to this category. This category can not be edited or deleted.', 'connections'); cnTerm::insert(__('Uncategorized', 'connections'), 'category', $attributes); } }