/** * Template function for fetching a single catalog item by id * * @param integer $id The id of a catalog item to fetch * @return CataBlogItem|NULL Returns a CataBlogItem object if a catalog item was found, otherwise NULL */ function catablog_get_item($id = false) { if (is_numeric($id) && $id > 0) { return CataBlogItem::getItem($id); } return null; }
public function getCataBlogItems() { return CataBlogItem::getItemsByIds($this->item_ids); }
?> </p> <?php } else { ?> <ul id="catablog-import-messages"> <?php if (isset($_REQUEST['catablog_clear_db'])) { ?> <li class="updated"><em><?php _e("removing catalog items...", 'catablog'); ?> </em></li> <?php $items = CataBlogItem::getItems(); ?> <?php foreach ($items as $item) { ?> <?php $item->delete(false); ?> <?php } ?> <li class="updated"><?php _e("Success: <em>All</em> catalog items removed successfully", 'catablog'); ?> </li>
die("<li class='error'>" . __('Login failure. You must be logged into the WordPress Admin section.', 'catablog') . "</li>"); } // make sure the attempting uploader has permission to edit posts if (!current_user_can('edit_posts')) { die("<li class='error'>" . __('Your Admin account does not have permission to "edit_posts".', 'catablog') . "</li>"); } // make sure the attempting uploader had passed the correct nonce value check_admin_referer('catablog_swfupload'); // create global variable for catablog class global $wp_plugin_catablog_class; $tmp_name = $_FILES['Filedata']['tmp_name']; $_FILES['new_image'] = $_FILES['Filedata']; if (strlen($tmp_name) < 1) { die("<li class='error'>" . __('Image could not be uploaded to the server, please try again.', 'catablog') . "</li>"); } $new_item = new CataBlogItem(); $valid_image = $new_item->validateImage($tmp_name); if ($valid_image === true) { $new_item_title = $_FILES['Filedata']['name']; $new_item_title = preg_replace('/\\.[^.]+$/', '', $new_item_title); $new_item_title = str_replace(array('_', '-', '.'), ' ', $new_item_title); $new_item_order = wp_count_posts($new_item->getCustomPostName())->publish + 1; $new_item->setOrder($new_item_order); $new_item->setTitle($new_item_title); $new_item->setImage($tmp_name); $new_item->setSubImages(array()); $default_term = $wp_plugin_catablog_class->get_default_term(); $new_item->setCategories(array($default_term->term_id => $default_term->name)); $new_item->save(); // load the user settings for which fields to display in the quick edit form. $user = wp_get_current_user();
private function load_array_to_database($data) { // extract a list of every category in the import $import_terms = array($this->default_term_name); foreach ($data as $key => $row) { $row_terms = array(); if ($this->string_length($row['categories']) > 0) { $row_terms = explode('|', $row['categories']); } foreach ($row_terms as $row_term) { $import_terms[] = $row_term; } } $import_terms = array_intersect_key($import_terms, array_unique(array_map('strtolower', $import_terms))); // extract a list of every category that is not already created $make_terms = $import_terms; if (isset($_REQUEST['catablog_clear_db']) === false) { $existant_terms = $this->get_terms(true); foreach ($existant_terms as $existant_term) { foreach ($make_terms as $key => $make_term) { if ($make_term == $existant_term->name) { unset($make_terms[$key]); unset($import_terms[$key]); $import_terms[$existant_term->term_id] = $existant_term->name; } } } } // create the neccessary new categories from the previous extraction $new_term_made = false; foreach ($make_terms as $key => $make_term) { $category_slug = $this->string2slug($make_term); $attr = array('slug' => $category_slug); $insert_return = wp_insert_term($make_term, $this->custom_tax_name, $attr); if ($insert_return instanceof WP_Error) { foreach ($insert_return->get_error_messages() as $error) { echo '<li class="error">' . __("Error:", 'catablog') . sprintf(' %s %s', "<strong>{$make_term}</strong>", $error) . '</li>'; } } else { if (isset($insert_return['term_id'])) { $new_term_id = $insert_return['term_id']; $new_term_name = $make_term; if ($new_term_id > 0) { unset($import_terms[$key]); unset($make_terms[$key]); $import_terms[$new_term_id] = $new_term_name; $new_term_made = true; echo '<li class="updated">' . __("Success:", 'catablog') . sprintf(__(' %s inserted into catalog categories.', 'catablog'), "<em>{$make_term}</em>") . '</li>'; } } } } // render complete making new categories message if ($new_term_made) { echo '<li class="updated"><strong>' . __('All New Categories Created', 'catablog') . '</strong></li>'; echo '<li> </li>'; } else { echo '<li class="updated"><strong>' . __('No New Categories Created', 'catablog') . '</strong></li>'; echo '<li> </li>'; } // load all existing catalog item ids $existant_ids = CataBlogItem::getItemIds(); // set new order to the number of catablog posts plus one // $new_order = wp_count_posts($this->custom_post_name)->publish + 1; // import each new catalog item foreach ($data as $row) { // set error and confermation variables $error = false; $success_message = '<li class="updated">' . __('Insert:', 'catablog') . sprintf(__(' %s inserted into the database.', 'catablog'), '<em>' . $row['title'] . '</em>') . '</li>'; // $success_message = '<li class="updated">' . __('Update:', 'catablog') . sprintf(__(' %s updated in database.', 'catablog'), '<em>'.$row['title'].'</em>') . '</li>'; $error_message = '<li class="error">' . __('Error:', 'catablog') . sprintf(__(' %s was not inserted into the database.', 'catablog'), '<strong>' . $row['title'] . '</strong>') . '</li>'; // Validate that the title and image values are set if ($this->string_length($row['title']) < 1) { $error = '<li class="error"><strong>' . __('Error:', 'catablog') . "</strong> " . __('Item had no title and could not be made.', 'catablog') . '</li>'; } if ($this->string_length($row['image']) < 1) { $error = '<li class="error"><strong>' . __('Error:', 'catablog') . "</strong> " . __('Item had no primary image name and could not be made.', 'catablog') . '</li>'; } if ($error) { echo $error; } else { // transform categories array if (isset($row['categories'])) { $categories = $row['categories']; if (is_array($categories) === false) { $categories = explode('|', $categories); $row['categories'] = array(); } foreach ($categories as $cat) { foreach ($import_terms as $term_id => $term_name) { if (strtolower($term_name) == strtolower($cat)) { $row['categories'][] = $term_id; } } } } // transform subimages array if (isset($row['subimages'])) { $subimages = $row['subimages']; if (is_array($subimages) === false) { if ($this->string_length($subimages) > 0) { $subimages = explode('|', $subimages); } else { $subimages = array(); } } $row['sub_images'] = $subimages; unset($row['subimages']); } // unset id if it is not already in the database. if (isset($row['id'])) { if (in_array($row['id'], $existant_ids)) { $success_message = '<li class="updated">' . __('Update:', 'catablog') . sprintf(__(' %s updated in database.', 'catablog'), '<em>' . $row['title'] . '</em>') . '</li>'; } else { $success_message = '<li class="updated">' . __('Insert:', 'catablog') . sprintf(__(' %s inserted into the database.', 'catablog'), '<em>' . $row['title'] . '</em>') . '</li>'; unset($row['id']); } } $item = new CataBlogItem($row); // $item->setOrder($new_order); // var_dump($item); $results = $item->save(); if ($results === true) { // if (!isset($row['id'])) { // $existant_ids[] = $item->getId(); // } echo $success_message; } else { echo $results; } } // $new_order += 1; // return false; } echo '<li class="updated"><strong>' . sprintf(__('%s Catalog Items Inserted', 'catablog'), count($data)) . '</strong></li>'; }
/** * Get a collection of catalog items from the database using an array of ids. * May possibly return an empty array. * * @param ids $ids The array of catalog item ids to be fetched from the database. * @return array An array of CataBlogItem objects */ public static function getItemsByIds($ids) { $items = array(); if (!is_array($ids) || empty($ids)) { $ids = array(-1); } $ids = array_unique($ids); $cata = new CataBlogItem(); $params = array('post_type' => $cata->getCustomPostName(), 'post__in' => $ids, 'numberposts' => -1); $posts = get_posts($params); // return an array of CataBlogItems foreach ($posts as $post) { $item = new CataBlogItem(); $item->id = $post->ID; $item->title = $post->post_title; $item->description = $post->post_content; $item->date = $post->post_date; $item->categories = array(); $item->order = $post->menu_order; $item->_post_name = $post->post_name; $item_cats = array(); if (true) { // $load_categories $category_ids = array(); $terms = get_the_terms($post->ID, $item->_custom_tax_name); if (is_array($terms)) { foreach ($terms as $term) { $category_ids[$term->term_id] = $term->name; } } $item->categories = $category_ids; } $meta = get_post_meta($post->ID, $item->_post_meta_name, true); $item->processPostMeta($meta); $items[$item->id] = $item; } return $items; }