/** * Get json encoded data to used in Frontend */ public static function getPostsData() { global $post; if (!$post) { return false; } if (!array_key_exists('post_type', $post)) { return false; } if (!preg_match('/post\\.php|post-new\\.php/', $_SERVER['SCRIPT_NAME'])) { return false; } $class = str_replace(' ', '', ucwords(str_replace(Base::SEPARATOR, ' ', $post->post_type))); if (class_exists($class)) { $custom_post = \Taco\Post\Factory::create($post); $fields = $custom_post->getFields(); $array_of_json_results = array(); foreach ($fields as $k => $v) { if (array_key_exists('data-post-type', $fields[$k])) { list($class, $method) = self::getPostTypeStructure($fields[$k]); if (!strlen($class)) { self::hideField(); return false; } $posts_helper = new $class(); // must be in the format of "id => title" $pairs = $posts_helper->{$method}(); $existing_pairs = self::getExistingPairs($pairs, explode(',', $custom_post->get($k))); $existing_ids = array_keys($existing_pairs); if (!Arr::iterable($existing_ids)) { update_post_meta($custom_post->ID, $k, ''); } else { update_post_meta($custom_post->ID, $k, join(',', $existing_ids)); } $array_of_json_results[$k] = $pairs; } } if (Arr::iterable($array_of_json_results)) { // save it for later when the front-end gets loaded self::$posts_json_object = json_encode($array_of_json_results); return true; } return false; } }
/** * Find a term * @param integer $term_id * @return object */ public static function find($term_id) { $instance = Post\Factory::create(get_called_class()); $instance->load($term_id); return $instance; }
/** * Get edit link when admin is logged in * @param int $id (post ID or term ID) * @param string $edit_type (post type or taxonomy slug) * @param string $label (optional admin-facing name for $edit_type) * @param bool $display_inline (omit wrapping paragraph) * @return string (HTML) */ function get_edit_link($id = null, $edit_type = 'post', $label = null, $display_inline = false) { if (!(is_user_logged_in() && current_user_can('manage_options'))) { return null; } $link_class = 'class="front-end-edit-link"'; $link_tag = $display_inline ? 'span' : 'p'; if (is_null($label)) { $label = Str::human(str_replace('-', ' ', $edit_type)); } $subclasses = \Taco\Post\Loader::getSubclasses(); $subclasses_machine = array_map(function ($el) { $el = substr($el, strrpos($el, '\\')); $el = Str::camelToHuman($el); $el = Str::machine($el, '-'); return $el; }, $subclasses); if (in_array($edit_type, $subclasses_machine)) { // Edit post or display list of posts of this type $post_type_link = !is_null($id) ? get_edit_post_link($id) : '/wp-admin/edit.php?post_type=' . $edit_type; return sprintf('<%s %s><a href="%s">Edit %s</a></%s>', $link_tag, $link_class, $post_type_link, $label, $link_tag); } // Find an applicable post type for editing a custom term $post_type = null; $post_types_by_taxonomy = []; foreach ($subclasses as $subclass) { if (strpos($subclass, '\\') !== false) { $subclass = '\\' . $subclass; } $taxonomies = \Taco\Post\Factory::create($subclass)->getTaxonomies(); if (Arr::iterable($taxonomies)) { foreach ($taxonomies as $key => $taxonomy) { $taxonomy_slug = is_array($taxonomy) ? $key : $taxonomy; $post_types_by_taxonomy[$taxonomy_slug][] = $subclass; } } } $post_types_by_taxonomy = array_unique($post_types_by_taxonomy); if (array_key_exists($edit_type, $post_types_by_taxonomy)) { $post_type = reset($post_types_by_taxonomy[$edit_type]); $post_type = substr($post_type, strrpos($post_type, '\\')); $post_type = Str::camelToHuman($post_type); $post_type = Str::machine($post_type, '-'); } else { $post_type = 'post'; } if (is_null($id)) { // View taxonomy term list return sprintf('<%s %s><a href="/wp-admin/edit-tags.php?taxonomy=%s&post_type=%s">View %ss</a></%s>', $link_tag, $link_class, $edit_type, $post_type, $label, $link_tag); } // Edit term return sprintf('<%s %s><a href="%s">Edit %s</a></%s>', $link_tag, $link_class, get_edit_term_link($id, $edit_type, $post_type), $label, $link_tag); }
public static function updateSubPosts($post_id, $fields_values, $object_post_parent = null) { $post_id = trim(preg_replace('/\\D/', '', $post_id)); $subpost = \SubPost::find($post_id); $field_assigned_to = $subpost->get('field_assigned_to'); $subpost_fields = \Taco\Post\Factory::create($object_post_parent->ID)->getFields()[$field_assigned_to][$subpost->get('fields_variation')]['fields']; if (wp_is_post_revision($post_parent)) { return false; } $array_remove_values = array_diff(array_keys($fields_values), array_keys($subpost_fields)); foreach ($fields_values as $k => $v) { update_post_meta($post_id, $k, $v); } foreach ($array_remove_values as $field_key) { delete_post_meta($post_id, $field_key); } remove_action('save_post', 'AddMany::saveAll'); return true; }
<?php //setup the page $page = \Taco\Post\Factory::create($post); get_header(); ?> on the page <?php echo $page->getTheTitle(); echo $page->getTheContent(); ?> <?php get_footer();
/** * Find a post * @param integer $post_id * @return object */ public static function find($post_id, $load_terms = true) { $instance = Post\Factory::create(get_called_class()); $instance->load($post_id, $load_terms); return $instance; }
/** * Get breadcrumbs HTML * @param \Taco\Post $post_obj * @return string HTML */ public static function getBreadcrumbs(\Taco\Post $post_obj) { $separator = ' » '; $post_id = $post_obj->get('ID'); $post_type = $post_obj->getPostType(); $ancestor_links = array(); $post_title = null; if (is_archive()) { // This is the blog archive, what to do for breadcrumbs here? return null; } if ($post_type == 'page') { $post_title = $post_obj->getTheTitle(); $ancestors = get_post_ancestors($post_id); if (Arr::iterable($ancestors)) { $ancestors = array_reverse($ancestors); foreach ($ancestors as $ancestor_post_id) { $ancestor = \Taco\Post\Factory::create($ancestor_post_id, false); $single_post = get_post($post_id); $ancestor_links[] = sprintf('<li><a href="%s">%s</a></li>', $ancestor->getPermalink(), $ancestor->getTheTitle()); } } } elseif ($post_type == 'article') { $ancestor_links[] = '<li><a href="' . URL_NEWS . '">News</a></li>'; $topics = $post_obj->getTerms('topic'); if (Arr::iterable($topics)) { $topic = reset($topics); $ancestor_links[] = sprintf('<li><a href="%s">%s</a></li>', $topic->getPermalink(), $topic->get('name')); } } // Don't display breadcrumbs unless there's at least one ancestor if (!Arr::iterable($ancestor_links)) { return null; } return sprintf('<ul class="bread-crumbs"> %s <li>%s</li> </ul>', join('', $ancestor_links), $post_title); }
<?php get_header(); // get page $blog_post = \Taco\Post\Factory::create($post); ?> on the post <?php echo $blog_post->getTheTitle(); echo $blog_post->getTheContent(); ?> <?php get_footer();