/** * Get the combined contents of multiple file paths * @param string array * @return string */ public static function getCombinedContents($paths) { if (!Arr::iterable($paths)) { return ''; } $out = array(); foreach ($paths as $path) { $out[] = file_get_contents($path); } return join("\n", $out); }
/** * Create multiple instances based on term objects * This basically autoloads the meta data * @param array $terms * @param string $taxonomy * @return array */ public static function createMultiple($terms, $taxonomy = null) { if (!Arr::iterable($terms)) { return $terms; } $out = array(); foreach ($terms as $term) { $instance = self::create($term, $taxonomy); $out[$instance->get('term_id')] = $instance; } return $out; }
/** * Create multiple instances based on WP posts * This basically autoloads the meta data * @param array $posts * @param bool $load_terms * @return array */ public static function createMultiple($posts, $load_terms = true) { if (!Arr::iterable($posts)) { return $posts; } $out = array(); foreach ($posts as $k => $post) { if (!get_post_status($post)) { continue; } $record = self::create($post, $load_terms); $out[$k] = $record; } return $out; }
/** * Delete all the terms for this taxonomy * TODO Make this more efficient * @return integer Number of posts deleted */ public static function deleteAll() { $terms = static::getAll(); if (!Arr::iterable($terms)) { return 0; } $num_deleted = 0; foreach ($terms as $term) { if ($term->delete()) { $num_deleted++; } } return $num_deleted; }
public function getRelatedPosts($addbysearch_field = null, $number = 2) { // first check addbysearch fields for when related posts are manually assigned if (!is_null($addbysearch_field) && $this->get($addbysearch_field)) { $related_posts = \AddBySearch\AddBySearch::getPostsFromOrder($this->get($addbysearch_field)); if (Arr::iterable($related_posts)) { $related_posts = array_slice($related_posts, 0, $number); return $related_posts; } } // next get posts by related terms $tax_terms = $this->getTerms(); if (Arr::iterable($tax_terms)) { $terms_filtered = []; foreach ($tax_terms as $tax_name => $terms) { if ($tax_name === 'category') { continue; } foreach ($terms as $term) { if ($term->get('slug') === 'uncategorized') { continue; } $terms_filtered[] = $term; } } if (Arr::iterable($terms_filtered)) { $key = array_rand($terms_filtered, 1); $term = $terms_filtered[$key]; } if (is_object($term)) { $related = $this->getByTerm($term->get('taxonomy'), $term->get('slug'), 'slug', array('posts_per_page' => $number)); if (Arr::iterable($related)) { if (array_key_exists($this->ID, $related)) { unset($related[$this->ID]); } $filtered = []; foreach ($related as $r) { if ($this->ID == $r->ID) { continue; } $filtered[] = $r; } if (Arr::iterable($filtered)) { return $filtered; } } } return []; // still nothing? $related = $this->getAll(); $keys = array_rand($related, $number); $rand = []; foreach ($keys as $k) { $rand[$k] = $related[$k]; } if (array_key_exists($this->ID, $rand)) { unset($rand[$this->ID]); } return $rand; } }
public static function saveAll($post_id) { if (self::areThereDeletedIds()) { self::deleteSubPosts($_POST['addmany_deleted_ids']); } if (!array_key_exists('subposts', $_POST)) { return false; } $source = $_POST; $subposts = $source['subposts']; if (!Arr::iterable($subposts)) { return false; } foreach ($subposts as $record) { if (!Arr::iterable($record)) { continue; } foreach ($record as $k => $v) { self::updateSubPosts($k, $v, $record); } } return true; }
/** * Register the taxonomies * WordPress limits calls to register_taxonomy to once per taxonomy * So we need to externalize this from a single TacoPostUtil::load call * @return integer Number of taxonomies registered */ public static function registerTaxonomies() { global $taxonomies_infos; if (!Arr::iterable($taxonomies_infos)) { return 0; } // Start by grouping all taxonomy requests by taxonomy key $count = 0; $grouped = Collection::groupBy($taxonomies_infos, 'key'); foreach ($grouped as $key => $key_taxonomies) { // Now get all the post types $post_types = Collection::pluck($key_taxonomies, 'post_type'); $configs = Collection::pluck($key_taxonomies, 'config'); // Now we can finally register this taxonomy register_taxonomy($key, $post_types, current($configs)); $count++; } return $count; }
/** * Make the admin columns sortable * @param array $columns * @return array */ public function makeAdminColumnsSortable($columns) { $admin_columns = $this->getAdminColumns(); if (!Arr::iterable($admin_columns)) { return $columns; } foreach ($admin_columns as $k) { $columns[$k] = $k; } return $columns; }
/** * Delete all the posts * TODO Make this more efficient * @param $bypass_trash (aka force_delete per wp_delete_post) * @return integer Number of posts deleted */ public static function deleteAll($bypass_trash = false) { $num_deleted = 0; $all = static::getAll(false); if (!Arr::iterable($all)) { return $num_deleted; } foreach ($all as $post) { if ($post->delete($bypass_trash)) { $num_deleted++; } } return $num_deleted; }
/** * Get Taco Posts or Terms in the order specficied using a string of comma seperated ids * @param string $string_order comma seperated list of ids * @param bool $reverse should the collection be reversed * @param bool $is_term should the method return terms instead of posts * @param string $taxonomy if this method is returning terms, what taxonomy do they belong to * @param bool $load_terms should terms be loaded with posts * @return array */ public static function getPostsFromOrder($string_order = '', $reverse = false, $is_term = false, $taxonomy = null, $load_terms = true) { if (!strlen($string_order)) { return array(); } if (!preg_match('/\\d+/', $string_order)) { return array(); } $ids_array = explode(',', trim(strip_tags($string_order))); if (!$is_term) { $ids_array = self::getItemsIfExists($ids_array, $is_term); $items = \Taco\Post\Factory::createMultiple($ids_array, $load_terms); } else { $ids_array = self::getItemsIfExists($ids_array, true); $items = \Taco\Term\Factory::createMultiple($ids_array, $taxonomy); } $items = $reverse ? array_reverse($items) : $items; return Arr::iterable($items) ? $items : array(); }