function save() { global $wpdb, $bp; // pre-save filter hooks $this->user_id = apply_filters('bp_links_link_user_id_before_save', $this->user_id, $this->id); $this->category_id = apply_filters('bp_links_link_category_id_before_save', $this->category_id, $this->id); $this->url = apply_filters('bp_links_link_url_before_save', $this->url, $this->id); $this->target = apply_filters('bp_links_link_target_before_save', $this->target, $this->id); $this->rel = apply_filters('bp_links_link_rel_before_save', $this->rel, $this->id); $this->slug = apply_filters('bp_links_link_slug_before_save', $this->slug, $this->id); $this->name = apply_filters('bp_links_link_name_before_save', $this->name, $this->id); $this->description = apply_filters('bp_links_link_description_before_save', $this->description, $this->id); $this->status = apply_filters('bp_links_link_status_before_save', $this->status, $this->id); // handle embed service values if ($this->embed() instanceof BP_Links_Embed_Service) { $this->embed_service = $this->embed()->key(); $this->embed_data = $this->embed()->export_data(); } elseif (empty($this->embed_service)) { $this->embed_remove(); } // make sure category_id actually exists if (!BP_Links_Category::check_category($this->category_id)) { return false; } // pre-save action hook do_action('bp_links_link_before_save', $this); // save the user vote if exists // (BP_Links_Vote::save() only triggers when a vote was cast) if ($this->_user_vote_obj instanceof BP_Links_Vote) { if (true !== $this->_user_vote_obj->save()) { return false; } } // if we have an id, we are updating if ($this->id) { // on update we need to recalculate // the denormalized data from the votes table // update the aggregate data $this->vote_count = apply_filters('bp_links_link_vote_count_before_update_save', BP_Links_Vote::link_vote_count($this->id), $this->id); $this->vote_total = apply_filters('bp_links_link_vote_total_before_update_save', BP_Links_Vote::link_vote_total($this->id), $this->id); // now we can recalculate the popularity $this->popularity = apply_filters('bp_links_link_popularity_before_update_save', $this->popularity_recalculate(), $this->id); // prepare the query $sql = $wpdb->prepare("UPDATE {$bp->links->table_name} SET\n\t\t\t\t\tuser_id = %d,\n\t\t\t\t\tcategory_id = %d,\n\t\t\t\t\turl = %s,\n\t\t\t\t\turl_hash = MD5(%s),\n\t\t\t\t\ttarget = %s,\n\t\t\t\t\trel = %s,\n\t\t\t\t\tslug = %s, \n\t\t\t\t\tname = %s,\n\t\t\t\t\tdescription = %s, \n\t\t\t\t\tstatus = %d,\n\t\t\t\t\tvote_count = %d,\n\t\t\t\t\tvote_total = %d,\n\t\t\t\t\tpopularity = %d,\n\t\t\t\t\tembed_service = %s,\n\t\t\t\t\tembed_status = %d,\n\t\t\t\t\tembed_data = %s,\n\t\t\t\t\tdate_updated = %s\n\t\t\t\tWHERE\n\t\t\t\t\tid = %d\n\t\t\t\t", $this->user_id, $this->category_id, $this->url, $this->url, $this->target, $this->rel, $this->slug, $this->name, $this->description, $this->status, $this->vote_count, $this->vote_total, $this->popularity, $this->embed_service, $this->embed_status, $this->embed_data, date('Y-m-d H:i:s'), $this->id); } else { // new record // generate a cloud id $this->cloud_id = $this->generate_cloud_id(); // these hooks allow changing the default values on new record creation $this->vote_count = apply_filters('bp_links_link_vote_count_before_insert_save', 0); $this->vote_total = apply_filters('bp_links_link_vote_total_before_insert_save', 0); $this->popularity = apply_filters('bp_links_link_popularity_before_insert_save', self::POPULARITY_DEFAULT); // prepare query $sql = $wpdb->prepare("INSERT INTO {$bp->links->table_name} (\n\t\t\t\t\tcloud_id,\n\t\t\t\t\tuser_id,\n\t\t\t\t\tcategory_id,\n\t\t\t\t\turl,\n\t\t\t\t\turl_hash,\n\t\t\t\t\ttarget,\n\t\t\t\t\trel,\n\t\t\t\t\tslug,\n\t\t\t\t\tname,\n\t\t\t\t\tdescription,\n\t\t\t\t\tstatus,\n\t\t\t\t\tvote_count,\n\t\t\t\t\tvote_total,\n\t\t\t\t\tpopularity,\n\t\t\t\t\tembed_service,\n\t\t\t\t\tembed_status,\n\t\t\t\t\tembed_data,\n\t\t\t\t\tdate_created\n\t\t\t\t) VALUES (\n\t\t\t\t\t%s, %d, %d, %s, MD5(%s), %s, %s, %s, %s, %s, %d, %d, %d, %d, %s, %d, %s, %s\n\t\t\t\t)", $this->cloud_id, $this->user_id, $this->category_id, $this->url, $this->url_hash, $this->target, $this->rel, $this->slug, $this->name, $this->description, $this->status, $this->vote_count, $this->vote_total, $this->popularity, $this->embed_service, $this->embed_status, $this->embed_data, date('Y-m-d H:i:s')); } if (false === $wpdb->query($sql)) { return false; } if (!$this->id) { $this->id = $wpdb->insert_id; } do_action('bp_links_link_after_save', $this); return true; }
function bp_links_categories_template($type, $per_page, $max) { global $bp; $this->pag_page = isset($_REQUEST['lcpage']) ? intval($_REQUEST['lcpage']) : 1; $this->pag_num = isset($_REQUEST['num']) ? intval($_REQUEST['num']) : $per_page; if (isset($_REQUEST['s'])) { $filter = $_REQUEST['s']; } switch ($type) { case 'all': default: $this->categories = BP_Links_Category::get_all_filtered($filter, $this->pag_num, $this->pag_page); break; } if (!$max || $max >= (int) $this->categories['total']) { $this->total_category_count = (int) $this->categories['total']; } else { $this->total_category_count = (int) $max; } $this->categories = $this->categories['categories']; if ($max) { if ($max >= count($this->categories)) { $this->category_count = count($this->categories); } else { $this->category_count = (int) $max; } } else { $this->category_count = count($this->categories); } if ((int) $this->total_category_count && (int) $this->pag_num) { $this->pag_links = paginate_links(array('base' => add_query_arg('lcpage', '%#%'), 'format' => '', 'total' => ceil((int) $this->total_category_count / (int) $this->pag_num), 'current' => (int) $this->pag_page, 'prev_text' => '«', 'next_text' => '»', 'mid_size' => 1)); } }
/** * Admin edit category action * * @return boolean */ function bp_links_admin_edit_category() { // error handling $error = false; // input value defaults $category_id = null; $category_name = null; $category_description = null; $category_priority = null; // // Handle create/update // if (isset($_POST['category_id'])) { if (is_numeric($_POST['category_id'])) { // edit the category $category_id = (int) $_POST['category_id']; $category = new BP_Links_Category($category_id); } else { // new category $category_id = null; $category = new BP_Links_Category(); } // new values if (isset($_POST['name'])) { $category_name = $_POST['name']; if (strlen($category_name) >= 3 && strlen($category_name) <= 50) { if (empty($category->slug) && $category->check_slug_raw($category_name)) { $error = true; $message = __('Link category slug from this name already exists.', 'buddypress-links') . ' ' . __('Please try again.', 'buddypress-links'); $message_type = 'error'; } else { $category->name = $category_name; } } else { $error = true; $message = sprintf(__('Link category name must be %1$d to %2$d characters in length.', 'buddypress-links'), 3, 50) . ' ' . __('Please try again.', 'buddypress-links'); $message_type = 'error'; } } else { $error = true; $message = __('Link category name is required.', 'buddypress-links') . ' ' . __('Please try again.', 'buddypress-links'); $message_type = 'error'; } if (isset($_POST['description'])) { $category_description = $_POST['description']; if (empty($category_description)) { $category->description = null; } else { if (strlen($category_description) >= 5 && strlen($category_description) <= 250) { $category->description = $category_description; } else { $error = true; $message = sprintf(__('Link category description must be %1$d to %2$d characters in length.', 'buddypress-links'), 5, 250) . ' ' . __('Please try again.', 'buddypress-links'); $message_type = 'error'; } } } if (isset($_POST['priority'])) { $category_priority = (int) $_POST['priority']; if ($category_priority >= 1 && $category_priority <= 100) { $category->priority = $category_priority; } else { $error = true; $message = sprintf(__('Link category priority must be a number from %1$d to %2$d.', 'buddypress-links'), 1, 100) . ' ' . __('Please try again.', 'buddypress-links'); $message_type = 'error'; } } else { $error = true; $message = __('Link category priority is required.', 'buddypress-links') . ' ' . __('Please try again.', 'buddypress-links'); $message_type = 'error'; } // try to save if (false === $error) { if ($category->save()) { $message = sprintf('%1$s <a href="?page=buddypress-links-admin-cats">%2$s</a> %3$s <a href="?page=buddypress-links-admin-cats&category_id=">%4$s</a>', __('Link category saved!', 'buddypress-links'), __('Return to list', 'buddypress-links'), __('or', 'buddypress-links'), __('Create new category', 'buddypress-links')); $message_type = 'updated'; } else { $message = __('There were errors when saving the link category.', 'buddypress-links') . ' ' . __('Please try again.', 'buddypress-links'); $message_type = 'error'; } } } else { if (is_numeric($_GET['category_id'])) { // edit the category $category_id = (int) $_GET['category_id']; $category = new BP_Links_Category($category_id); } else { // new category $category_id = null; $category = new BP_Links_Category(); } // defaults for new category $category_name = $category->name; $category_description = $category->description; $category_priority = $category->priority; } // // Display Page // if ($category_id) { $heading_text = __('Edit Category', 'buddypress-links') . ': ' . $category_name; $submit_text = __('Update Category', 'buddypress-links'); $action = 'update'; $nonce_action = 'update-link-category_' . $category_id; } else { $heading_text = __('New Category', 'buddypress-links'); $submit_text = __('Add Category', 'buddypress-links'); $action = 'create'; $nonce_action = 'add-link-category'; } require_once BP_LINKS_ADMIN_THEME_DIR . '/category-edit.php'; return true; }