<?php /** * Plugin Name: WP-API: Print taxonomy data to posts * Description: A plugin to print taxonomy data to posts and custom post types, just like in V1 of WP-API. * Version: 1.0 * Author: Christian Nikkanen * Author URI: http://christiannikkanen.me * License: MIT */ defined('ABSPATH') or die('No script kiddies please!'); add_action("rest_api_init", function () { $post_types = get_post_types(); foreach ($post_types as $post_type) { // loop all post types and add field "terms" to api output. register_rest_field($post_type, "terms", array("get_callback" => function ($post) { $taxonomies = get_post_taxonomies($post['id']); $terms_and_taxonomies = []; foreach ($taxonomies as $taxonomy_name) { $terms_and_taxonomies[$taxonomy_name] = wp_get_post_terms($post['id'], $taxonomy_name); } return $terms_and_taxonomies; // return array with taxonomy & term data })); } });
/** * @param int $original_post_id * @param string $lang * @param bool $duplicate sets whether missing terms should be created by duplicating the original term */ private function synchronize_terms($original_post_id, $lang, $duplicate) { global $wpml_post_translations; $wpml_post_translations->reload(); $translated_post_id = $wpml_post_translations->element_id_in($original_post_id, $lang); if ((bool) $translated_post_id === true) { $taxonomies = get_post_taxonomies($original_post_id); foreach ($taxonomies as $tax) { $terms_on_original = wp_get_object_terms($original_post_id, $tax); if (!$this->sitepress->is_translated_taxonomy($tax)) { if ($this->sitepress->get_setting('sync_post_taxonomies')) { // Taxonomy is not translated so we can just copy from the original foreach ($terms_on_original as $key => $term) { $terms_on_original[$key] = $term->term_id; } wp_set_object_terms($translated_post_id, $terms_on_original, $tax); } } else { /** @var int[] $translated_terms translated term_ids */ $translated_terms = $this->get_translated_term_ids($terms_on_original, $lang, $tax, $duplicate); wp_set_object_terms($translated_post_id, $translated_terms, $tax); } } } clean_object_term_cache($original_post_id, get_post_type($original_post_id)); }
/** * Creates a set of classes for each site entry upon display. Each entry is given the class of * 'hentry'. Posts are given category, tag, and author classes. Alternate post classes of odd, * even, and alt are added. * * @param string|array $class One or more classes to add to the class list. * @param int $post_id An optional post ID. * @return array * @since 1.1 */ function momtaz_get_post_class($class = '', $post_id = 0) { $classes = array(); // Get post object $post = get_post($post_id); if (empty($post)) { return $classes; } // hAtom compliance. $classes[] = 'hentry'; // Get post context. $context = momtaz_get_post_context($post_id); // Merge the classes array with post context. $classes = array_merge($classes, (array) $context); // Post taxonomies $post_taxonomies = get_post_taxonomies($post); if (!empty($post_taxonomies)) { foreach ($post_taxonomies as $taxonomy) { $terms = get_the_terms($post->ID, $taxonomy); if (!empty($terms)) { foreach ($terms as $term) { $classes[] = 'term-' . sanitize_html_class($term->slug, $term->term_id); } } } } // Sticky posts. if (is_home() && !is_paged() && is_sticky($post->ID)) { $classes[] = 'sticky'; } // Is this post protected by a password? if (post_password_required($post)) { $classes[] = 'post-password-required'; } // Post alt class. if (!momtaz_is_the_single($post)) { static $post_alt = 0; $classes[] = 'set-' . ++$post_alt; $classes[] = $post_alt % 2 ? 'odd' : 'even'; } // Has a custom excerpt? if (has_excerpt($post)) { $classes[] = 'has-excerpt'; } // Custom classes. if (!empty($class)) { if (!is_array($class)) { $class = preg_split('#\\s+#', $class); } $classes = array_merge($classes, $class); } // Apply the WordPress filters. $classes = apply_filters('post_class', $classes, $class, $post->ID); // Apply the Momtaz FW filters. $classes = apply_filters('momtaz_get_post_class', $classes, $post); // Removes any duplicate and empty classes. $classes = array_unique(array_filter($classes)); return $classes; }
function post_grid_term_slug_list($post_id) { $term_slug_list = ''; $post_taxonomies = get_post_taxonomies($post_id); foreach ($post_taxonomies as $taxonomy) { $term_list[] = wp_get_post_terms(get_the_ID(), $taxonomy, array("fields" => "all")); } return $term_slug_list; }
function um_access_category_settings() { global $post, $wp_query, $ultimatemember; if (is_front_page() || is_home()) { return; } if (is_single() || get_post_taxonomies($post)) { $taxonomies = get_post_taxonomies($post); $categories_ids = array(); foreach ($taxonomies as $key => $value) { $term_list = wp_get_post_terms($post->ID, $value, array("fields" => "ids")); foreach ($term_list as $term_id) { array_push($categories_ids, $term_id); } } foreach ($categories_ids as $term => $term_id) { $opt = get_option("category_{$term_id}"); if (isset($opt['_um_accessible'])) { switch ($opt['_um_accessible']) { case 0: $ultimatemember->access->allow_access = true; $ultimatemember->access->redirect_handler = false; // open to everyone break; case 1: if (is_user_logged_in()) { $ultimatemember->access->redirect_handler = isset($opt['_um_redirect']) ? $opt['_um_redirect'] : site_url(); } if (!is_user_logged_in()) { $ultimatemember->access->allow_access = true; } break; case 2: if (!is_user_logged_in()) { $ultimatemember->access->redirect_handler = isset($opt['_um_redirect']) && !empty($opt['_um_redirect']) ? $opt['_um_redirect'] : um_get_core_page('login'); } if (is_user_logged_in() && isset($opt['_um_roles']) && !empty($opt['_um_roles'])) { if (!in_array(um_user('role'), $opt['_um_roles'])) { if (is_user_logged_in()) { $ultimatemember->access->redirect_handler = isset($opt['_um_redirect']) ? $opt['_um_redirect'] : site_url(); } if (!is_user_logged_in()) { $ultimatemember->access->redirect_handler = um_get_core_page('login'); } } } } } if (is_archive()) { $ultimatemember->access->allow_access = true; $ultimatemember->access->redirect_handler = false; // open to everyone } } } }
/** * Check the provided taxonomy along with the given post id to see if any restrictions are found * * @since 2.5 * @param $post_id * @param $taxonomy * @param null $user_id * * @return int|bool true if tax is restricted, false if user can access, -1 if unrestricted or invalid */ function rcp_is_post_taxonomy_restricted($post_id, $taxonomy, $user_id = null) { $restricted = -1; if (current_user_can('edit_post', $post_id)) { return $restricted; } // make sure this post supports the supplied taxonomy $post_taxonomies = get_post_taxonomies($post_id); if (!in_array($taxonomy, (array) $post_taxonomies)) { return $restricted; } $terms = get_the_terms($post_id, $taxonomy); if (empty($terms) || is_wp_error($terms)) { return $restricted; } if (!$user_id) { $user_id = get_current_user_id(); } // Loop through the categories and determine if one has restriction options foreach ($terms as $term) { $term_meta = rcp_get_term_restrictions($term->term_id); if (empty($term_meta['paid_only']) && empty($term_meta['subscriptions']) && (empty($term_meta['access_level']) || 'None' == $term_meta['access_level'])) { continue; } $restricted = false; /** Check that the user has a paid subscription ****************************************************************/ $paid_only = !empty($term_meta['paid_only']); if ($paid_only && !rcp_is_paid_user($user_id)) { $restricted = true; } /** If restricted to one or more subscription levels, make sure that the user is a member of one of the levels */ $subscriptions = !empty($term_meta['subscriptions']) ? array_map('absint', $term_meta['subscriptions']) : false; if ($subscriptions && !in_array(rcp_get_subscription_id($user_id), $subscriptions)) { $restricted = true; } /** If restricted to one or more access levels, make sure that the user is a member of one of the levls ********/ $access_level = !empty($term_meta['access_level']) ? absint($term_meta['access_level']) : 0; if ($access_level > 0 && !rcp_user_has_access($user_id, $access_level)) { $restricted = true; } $match_all = apply_filters('rcp_restricted_taxonomy_term_match_all', false, $post_id, $taxonomy, $user_id); // if we are matching all terms then it only takes one restricted term to restrict the taxonomy if ($restricted && $match_all) { break; } // if we are matching any term, then we only need the user to have access to one if (!$match_all && !$restricted) { break; } } return apply_filters('rcp_is_post_taxonomy_restricted', $restricted, $taxonomy, $post_id, $user_id); }
private function synchronize_terms($original_post_id, $lang, $duplicate) { global $wpml_post_translations; $wpml_post_translations->reload(); $translated_post_id = $wpml_post_translations->element_id_in($original_post_id, $lang); if ((bool) $translated_post_id === true) { $taxonomies = get_post_taxonomies($original_post_id); foreach ($taxonomies as $tax) { $terms_on_original = wp_get_object_terms($original_post_id, $tax); /** @var int[] $translated_terms translated term_ids */ $translated_terms = $this->get_translated_term_ids($terms_on_original, $lang, $tax, $duplicate); wp_set_object_terms($translated_post_id, $translated_terms, $tax); } } }
function wpjam_category_post_thumbnail_uri($post_thumbnail_uri, $post) { $post_taxonomies = get_post_taxonomies($post); if ($post_taxonomies) { if (in_array('category', $post_taxonomies)) { $categories = get_the_category($post); if ($categories) { foreach ($categories as $category) { if ($term_thumbnail = get_term_meta($category->term_id, 'thumbnail', true)) { return $term_thumbnail; } } } } } }
function portfilo_breadcrumbs() { global $post, $wp_query; $homeLink = home_url(); $showCurrent = 1; echo '<div data-wow-delay="1s" class="breadcrum pull-left wow fadeIn animated" style="visibility: visible; animation-delay: 1s; animation-name: fadeIn;">'; echo '<ul>'; echo '<li><a href="' . $homeLink . '"><i class="glyphicon glyphicon-home"></i></a> <span class="breadcrum-sep">/</span></li>'; // Blog Category if (is_category()) { echo '<li><span class="active">' . __('Archive by category', 'portfilo') . ' "' . single_cat_title('', false) . '"</span></li>'; } elseif (is_search()) { echo '<li><span class="active">' . __('Search results for', 'portfilo') . ' "' . get_search_query() . '"</span></li>'; } elseif (is_day()) { echo '<li><a href="' . get_year_link($wp_query->query_vars['year']) . '">' . $wp_query->query_vars['year'] . '</a> <span class="breadcrum-sep">/</span></li>'; echo '<li><a href="' . get_month_link($wp_query->query_vars['year'], $wp_query->query_vars['monthnum']) . '">' . date_i18n("F", mktime(0, 0, 0, $wp_query->query_vars['monthnum'], 10)) . '</a> <span class="breadcrum-sep">/</span></li>'; echo '<li><span class="active">' . $wp_query->query_vars['day'] . '</span></li>'; } elseif (is_month()) { echo '<li><a href="' . get_year_link($wp_query->query_vars['year']) . '">' . $wp_query->query_vars['year'] . '</a> <span class="breadcrum-sep">/</span></li>'; echo '<li><span class="active">' . date_i18n("F", mktime(0, 0, 0, $wp_query->query_vars['monthnum'], 10)) . '</span></li>'; } elseif (is_year()) { echo '<li><span class="active">' . $wp_query->query_vars['year'] . '</span></li>'; } elseif (get_post_type() == 'post') { $post_for_page_id = get_option('page_for_posts'); echo '<li><a href="' . get_page_link($post_for_page_id) . '">' . get_the_title($post_for_page_id) . '</a></li>'; } elseif (is_single() && !is_attachment()) { // Custom post type if (get_post_type() != 'post') { global $wpdb; $post_type = get_post_type_object(get_post_type()); $slug = $post_type->rewrite; if ($slug['slug'] == 'portfolio-item' && ($portfolio_page_id = get_option('portfolio-page'))) { echo '<li><a href="' . get_page_link($portfolio_page_id) . '">' . get_the_title($portfolio_page_id) . '</a> <span class="breadcrum-sep">/</span></li>'; } else { echo '<li><a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> <span class="breadcrum-sep">/</span></li>'; } if ($showCurrent == 1) { echo '<li><span class="active">' . get_the_title() . '</span></li>'; } } else { $cat = get_the_category(); $cat = $cat[0]; echo '<li>'; echo get_category_parents($cat, TRUE, ' <span class="breadcrum-sep">/</span>'); echo '</li>'; echo '<li><span class="active">' . wp_title('', false) . '</span></li>'; } } elseif (get_post_taxonomies()) { global $wpdb; $post_type = get_post_type_object(get_post_type()); $slug = $post_type->rewrite; if ($post_type->name == 'portfolio' && ($portfolio_page_id = get_option('portfolio-page'))) { echo '<li><a href="' . get_page_link($portfolio_page_id) . '">' . get_the_title($portfolio_page_id) . '</a> <span class="breadcrum-sep">/</span></li>'; } else { echo '<li><a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> <span class="breadcrum-sep">/</span></li>'; } if (is_tax()) { $terms_object = get_queried_object(); echo '<li><span class="active">' . $terms_object->name . '</span></li>'; } } elseif (is_page() && $post->post_parent) { $parent_id = $post->post_parent; $breadcrumbs = array(); while ($parent_id) { $page = get_page($parent_id); $breadcrumbs[] = '<li><a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a> <span class="breadcrum-sep">/</span></li>'; $parent_id = $page->post_parent; } $breadcrumbs = array_reverse($breadcrumbs); for ($i = 0; $i < count($breadcrumbs); $i++) { echo $breadcrumbs[$i]; } if ($showCurrent == 1) { echo '<li><span class="active">' . get_the_title() . '</span></li>'; } } else { echo '<li><span class="active">' . get_the_title() . '</span></li>'; } echo '</ul>'; echo '</div>'; }
/** * Remove the default meta box from the post editing screen and add our custom meta box. * * @param string $object_type The object type (eg. the post type) * @param mixed $object The object (eg. a WP_Post object) * @return null */ function meta_boxes($object_type, $object) { if (!is_a($object, 'WP_Post')) { return; } $post_type = $object_type; $post = $object; $taxos = get_post_taxonomies($post); if (in_array($this->taxo->taxonomy, $taxos)) { $tax = get_taxonomy($this->taxo->taxonomy); # Remove default meta box: if ($this->taxo->args['hierarchical']) { remove_meta_box("{$this->taxo->taxonomy}div", $post_type, 'side'); } else { remove_meta_box("tagsdiv-{$this->taxo->taxonomy}", $post_type, 'side'); } if (!current_user_can($tax->cap->assign_terms)) { return; } if ($this->args['meta_box']) { # Set the 'meta_box' argument to the actual meta box callback function name: if ('simple' == $this->args['meta_box']) { if ($this->taxo->args['exclusive']) { $this->args['meta_box'] = array($this, 'meta_box_radio'); } else { $this->args['meta_box'] = array($this, 'meta_box_simple'); } } else { if ('radio' == $this->args['meta_box']) { $this->taxo->args['exclusive'] = true; $this->args['meta_box'] = array($this, 'meta_box_radio'); } else { if ('dropdown' == $this->args['meta_box']) { $this->taxo->args['exclusive'] = true; $this->args['meta_box'] = array($this, 'meta_box_dropdown'); } } } # Add the meta box, using the plural or singular taxonomy label where relevant: if ($this->taxo->args['exclusive']) { add_meta_box("{$this->taxo->taxonomy}div", $tax->labels->singular_name, $this->args['meta_box'], $post_type, 'side'); } else { add_meta_box("{$this->taxo->taxonomy}div", $tax->labels->name, $this->args['meta_box'], $post_type, 'side'); } } else { if (false !== $this->args['meta_box']) { # This must be an 'exclusive' taxonomy. Add the radio meta box: add_meta_box("{$this->taxo->taxonomy}div", $tax->labels->singular_name, array($this, 'meta_box_radio'), $post_type, 'side'); } } } }
/** * Create the taxonomy list for the post footer * * Lists categories, tags, and custom taxonomies * * Override: childtheme_override_postfooter_posttax <br> * Filter: thematic_postfooter_posttax */ function thematic_postfooter_posttax() { $post_type_tax = get_post_taxonomies(); $post_tax_list = ''; if (isset($post_type_tax) && $post_type_tax) { foreach ($post_type_tax as $tax) { if ($tax == 'category') { $post_tax_list .= thematic_postfooter_postcategory(); } elseif ($tax == 'post_tag') { $post_tax_list .= thematic_postfooter_posttags(); } else { $post_tax_list .= thematic_postfooter_postterms($tax); } } } return apply_filters('thematic_postfooter_posttax', $post_tax_list); // Filter for default post terms }
/** * Related Posts * * @since 1.0 */ function dp_related_posts($args = '') { global $post; $query_args = array(); $defaults = array('view' => 'grid-mini', 'number' => 0, 'fields' => ''); $args = wp_parse_args($args, $defaults); extract($args); // Only displayed on singular post pages if (!is_singular()) { return; } // Check limited number if (!$number) { return; } // Check taxonomies $taxes = get_post_taxonomies($post->ID); if (empty($taxes)) { return; } $taxes = array_unique(array_merge(array('category', 'post_tag'), $taxes)); $in_tax_query_array = array(); $and_tax_query_array = array(); foreach ($taxes as $tax) { $terms = get_the_terms($post->ID, $tax); if (empty($terms)) { continue; } $term_ids = array(); foreach ($terms as $term) { $term_ids[] = $term->term_id; } $in_tax_query_array[$tax] = array('taxonomy' => $tax, 'field' => 'id', 'terms' => $term_ids, 'operator' => 'IN'); $and_tax_query_array[$tax] = array('taxonomy' => $tax, 'field' => 'id', 'terms' => $term_ids, 'operator' => 'AND'); } if (empty($in_tax_query_array) && empty($and_tax_query_array)) { return; } $query_args = array('post_type' => get_post_type($post->ID), 'ignore_sticky_posts' => true, 'posts_per_page' => $number); $current_post_id = $post->ID; $found_posts = array(); // Multiple Taxonomy Query: relation = AND, operator = AND $query_args['tax_query'] = $and_tax_query_array; $query_args['tax_query']['relation'] = 'AND'; $query_args['post__not_in'] = array($post->ID); $related = new WP_Query($query_args); foreach ($related->posts as $post) { $found_posts[] = $post->ID; } // Multiple Taxonomy Query: relation = AND, operator = IN if (count($found_posts) < $number) { $query_args['tax_query'] = $in_tax_query_array; $query_args['tax_query']['relation'] = 'AND'; $query_args['post__not_in'] = array_merge(array($current_post_id), $found_posts); $related = new WP_Query($query_args); foreach ($related->posts as $post) { $found_posts[] = $post->ID; } } // Foreach Each Taxonomy Query: operator = AND if (count($found_posts) < $number) { unset($and_tax_query_array['post_format']); foreach ($and_tax_query_array as $and_tax_query) { $query_args['tax_query'] = array($and_tax_query); $query_args['post__not_in'] = array_merge(array($current_post_id), $found_posts); $related = new WP_Query($query_args); foreach ($related->posts as $post) { $found_posts[] = $post->ID; } if (count($found_posts) > $number) { break; } } } // Foreach Each Taxonomy Query: operator = IN if (count($found_posts) < $number) { unset($in_tax_query_array['post_format']); foreach ($in_tax_query_array as $in_tax_query) { $query_args['tax_query'] = array($in_tax_query); $query_args['post__not_in'] = array_merge(array($current_post_id), $found_posts); $related = new WP_Query($query_args); foreach ($related->posts as $post) { $found_posts[] = $post->ID; } if (count($found_posts) > $number) { break; } } } if (empty($found_posts)) { return; } $query_args['tax_query'] = ''; $query_args['post__in'] = $found_posts; $related = new WP_Query($query_args); if ($fields == 'object') { return $related; } if (!empty($args['template']) && is_callable($args['template'])) { call_user_func($args['template'], $related); return; } ?> <div class="section-box related-posts"> <div class="section-header"><h3 class="section-title"><?php _e('You may also like', 'dp'); ?> </h3></div> <div class="section-content <?php echo $view; ?> "><div class="nag cf"> <?php if ($related->have_posts()) { while ($related->have_posts()) { $related->the_post(); global $post; global $section_view; $section_view = 'grid-mini'; get_template_part('item-video'); } } wp_reset_query(); ?> </div></div> </div><!-- end .related-posts --> <?php }
/** * Returns a string of all the categories, tags and taxonomies the current post is under. * * @access public * @param string $separator * @return string */ function ci_list_cat_tag_tax($separator = ', ') { global $post; $taxonomies = get_post_taxonomies(); $i = 0; $the_terms = array(); $the_terms_temp = array(); $the_terms_list = ''; foreach ($taxonomies as $taxonomy) { $the_terms_temp[] = get_the_term_list($post->ID, $taxonomy, '', $separator, ''); } foreach ($the_terms_temp as $term) { if (!empty($term)) { $the_terms[] = $term; } } $terms_count = count($the_terms); for ($i = 0; $i < $terms_count; $i++) { $the_terms_list .= $the_terms[$i]; if ($i < $terms_count - 1) { $the_terms_list .= $separator; } } if (!empty($the_terms_list)) { return $the_terms_list; } else { return __('Uncategorized', 'ci_theme'); } }
/** * Display adjacent post link. * * Can be either next post link or previous. * * Based on adjacent_post_link() from wp-includes/link-template.php * * @param array|string $args Optional. Override default arguments. * @param bool $previous Optional, default is true. Whether display link to previous post. * @return bool True if next/previous post is found, otherwise false. */ function fusion_adjacent_post_link_plus($args = '', $format = '%link »', $previous = true) { $defaults = array('order_by' => 'post_date', 'order_2nd' => 'post_date', 'meta_key' => '', 'post_type' => '', 'loop' => false, 'end_post' => false, 'thumb' => false, 'max_length' => 0, 'format' => '', 'link' => '%title', 'date_format' => '', 'tooltip' => '%title', 'in_same_cat' => false, 'in_same_tax' => false, 'in_same_format' => false, 'in_same_author' => false, 'in_same_meta' => false, 'ex_cats' => '', 'ex_cats_method' => 'weak', 'in_cats' => '', 'ex_posts' => '', 'in_posts' => '', 'before' => '', 'after' => '', 'num_results' => 1, 'return' => false, 'echo' => true); // If Post Types Order plugin is installed, // default to sorting on menu_order if (function_exists('CPTOrderPosts')) { $defaults['order_by'] = 'menu_order'; } $r = wp_parse_args($args, $defaults); if (empty($r['format'])) { $r['format'] = $format; } if (empty($r['date_format'])) { $r['date_format'] = get_option('date_format'); } if (!function_exists('get_post_format')) { $r['in_same_format'] = false; } $posts = fusion_get_adjacent_post_plus($r, $previous); if ($previous && is_attachment()) { $posts = array(); $posts[] =& get_post($GLOBALS['post']->post_parent); } // If there is no next/previous post, // return false so themes may conditionally display inactive link text. if (empty($posts)) { return false; } // if sorting by date, display posts in reverse chronological order. // Otherwise display in alpha/numeric order. if ($previous && 'post_date' != $r['order_by'] || !$previous && 'post_date' == $r['order_by']) { $posts = array_reverse($posts, true); } // Option to return something other than the formatted link if ($r['return']) { if ($r['num_results'] == 1) { reset($posts); $post = current($posts); switch ($r['return']) { case 'id': return $post->ID; case 'href': return get_permalink($post); case 'object': return $post; case 'title': return $post->post_title; case 'date': return mysql2date($r['date_format'], $post->post_date); } } elseif ('object' == $r['return']) { return $posts; } } $output = $r['before']; // When num_results > 1, multiple adjacent posts may be returned. // Use foreach to display each adjacent post. foreach ($posts as $post) { $title = $post->post_title; if (empty($post->post_title)) { $title = $previous ? esc_attr__('Previous Post', 'Avada') : esc_attr__('Next Post', 'Avada'); } $title = apply_filters('the_title', $title, $post->ID); $date = mysql2date($r['date_format'], $post->post_date); $author = get_the_author_meta('display_name', $post->post_author); // Set anchor title attribute to long post title or custom tooltip text. // Supports variable replacement in custom tooltip. $tooltip = ''; if ($r['tooltip']) { $tooltip = str_replace('%title', $title, $r['tooltip']); $tooltip = str_replace('%date', $date, $tooltip); $tooltip = str_replace('%author', $author, $tooltip); $tooltip = ' title="' . esc_attr($tooltip) . '"'; } // Truncate the link title to nearest whole word under the length specified. $max_length = 1 > intval($r['max_length']) ? 9999 : intval($r['max_length']); if ($max_length < strlen($title)) { $title = substr($title, 0, strrpos(substr($title, 0, $max_length), ' ')); // mod for LTR larguages. $title = sprintf(esc_attr__('$s...', 'Avada'), $title); } $rel = $previous ? 'prev' : 'next'; $anchor = '<a href="' . get_permalink($post) . '" rel="' . $rel . '"' . $tooltip . '>'; $link = str_replace('%title', $title, $r['link']); $link = str_replace('%date', $date, $link); $link = $anchor . $link . '</a>'; $format = str_replace('%link', $link, $r['format']); $format = str_replace('%title', $title, $format); $format = str_replace('%date', $date, $format); $format = str_replace('%author', $author, $format); if (('custom' == $r['order_by'] || 'numeric' == $r['order_by']) && !empty($r['meta_key'])) { $meta = get_post_meta($post->ID, $r['meta_key'], true); $format = str_replace('%meta', $meta, $format); } elseif ($r['in_same_meta']) { $meta = get_post_meta($post->ID, $r['in_same_meta'], true); $format = str_replace('%meta', $meta, $format); } // Get the category list, including custom taxonomies // (only if the %category variable has been used). if (false !== strpos($format, '%category')) { $term_list = ''; $taxonomies = array_filter(get_post_taxonomies($post->ID), 'is_taxonomy_hierarchical'); if ($r['in_same_format'] && get_post_format($post->ID)) { $taxonomies[] = 'post_format'; } foreach ($taxonomies as &$taxonomy) { // No, this is not a mistake. Yes, we are testing the result of the assignment ( = ). // We are doing it this way to stop it from appending a comma when there is no next term. if ($next_term = get_the_term_list($post->ID, $taxonomy, '', ', ', '')) { $term_list .= $next_term; if (current($taxonomies)) { $term_list .= ', '; } } } $format = str_replace('%category', $term_list, $format); } // Optionally add the post thumbnail to the link. // Wrap the link in a span to aid CSS styling. if ($r['thumb'] && has_post_thumbnail($post->ID)) { if (true === $r['thumb']) { // use 'post-thumbnail' as the default size $r['thumb'] = 'post-thumbnail'; } $thumbnail = '<a class="post-thumbnail" href="' . get_permalink($post) . '" rel="' . $rel . '"' . $tooltip . '>' . get_the_post_thumbnail($post->ID, $r['thumb']) . '</a>'; $format = $thumbnail . '<span class="post-link">' . $format . '</span>'; } // If more than one link is returned, wrap them in <li> tags if (1 < intval($r['num_results'])) { $format = '<li>' . $format . '</li>'; } $output .= $format; } $output .= $r['after']; // If echo is false, don't display anything. Return the link as a PHP string. if (!$r['echo'] || 'output' === $r['return']) { return $output; } $adjacent = $previous ? 'previous' : 'next'; echo apply_filters("{$adjacent}_post_link_plus", $output, $r); return true; }
/** * Cache translated posts * * @param $posts */ function cache_translations($posts) { global $wpdb, $wp_query, $sitepress; static $last_query = false; if (defined('WPML_DISABLE_CACHE_TRANSLATIONS') && is_admin()) { return; } if (isset($sitepress) && isset($wp_query) && $wp_query->is_main_query()) { if ($last_query == $wp_query->query_vars_hash) { return; } $sticky_posts_ids = get_option('sticky_posts'); if ($sticky_posts_ids) { if (count($sticky_posts_ids) == 1) { $sticky_posts_prepared = $wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", array($sticky_posts_ids[0])); } else { $sticky_posts_prepared = "SELECT * FROM {$wpdb->posts} WHERE ID IN (" . implode(',', array_filter($sticky_posts_ids)) . ")"; } $sticky_posts = $wpdb->get_results($sticky_posts_prepared); $posts_objects = array_map('get_post', $sticky_posts); if (!$posts) { $posts = $posts_objects; } else { $posts = array_merge($posts, $posts_objects); //Remove duplicates $posts = array_map("unserialize", array_unique(array_map("serialize", $posts))); } } if ($posts) { $terms = array(); //Query specific cache $cache_key = $wp_query->query_vars_hash; $cache_group = 'wp_query:posts_translations'; $cached_posts_translations = wp_cache_get($cache_key, $cache_group); if (!$cached_posts_translations) { $post_types = array(); foreach ($posts as $post) { $post_types[$post->post_type][] = $post->ID; } $trids = array(); if ($post_types) { $trid_cache_group = 'element_trid'; foreach ($post_types as $post_type => $posts_ids) { $element_type = 'post_' . $post_type; $s_post_type_ids = join(',', array_filter($posts_ids)); $trids_prepared = $wpdb->prepare("SELECT trid, element_id, language_code, source_language_code FROM {$wpdb->prefix}icl_translations WHERE element_id IN (" . $s_post_type_ids . ") AND element_type=%s GROUP BY trid", array($element_type)); $post_type_trids_data = $wpdb->get_results($trids_prepared); foreach ($post_type_trids_data as $post_type_trid_data) { $element_id = $post_type_trid_data->element_id; $trid_cache_key = $element_id . ':post_' . $post_type; $trid = wp_cache_get($trid_cache_key, $trid_cache_group); if (!$trid) { $trid = $post_type_trid_data->trid; $trids[] = $trid; wp_cache_add($trid_cache_key, $trid, $trid_cache_group); } if ($trid) { $element_language_details_cache_group = 'element_language_details'; $element_language_details = wp_cache_get($trid_cache_key, $element_language_details_cache_group); if (!$element_language_details) { $details = new stdClass(); $details->trid = $trid; $details->language_code = $post_type_trid_data->language_code; $details->source_language_code = $post_type_trid_data->source_language_code; wp_cache_add($trid_cache_key, $details, $element_language_details_cache_group); } } //Deal with taxonomies //$_taxonomies = get_post_taxonomies($element_id); $_taxonomies = get_post_taxonomies($element_id); foreach ($_taxonomies as $_taxonomy) { if ($sitepress->is_translated_taxonomy($_taxonomy)) { $_terms = wp_get_post_terms($element_id, $_taxonomy); foreach ($_terms as $_term) { $terms[$_term->taxonomy][] = $_term->term_id; } } } } } } if ($trids) { if (count($trids) == 1) { $posts_translations_prepared = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}icl_translations WHERE trid = %d ", array($trids[0])); } else { $posts_translations_prepared = "SELECT * FROM {$wpdb->prefix}icl_translations WHERE trid IN (" . join(',', array_filter($trids)) . ") "; } $posts_translations = $wpdb->get_results($posts_translations_prepared); $post_ids = array(); foreach ($posts_translations as $posts_translation) { $post_ids[] = $posts_translation->element_id; } $posts_data = wp_cache_get($cache_key, 'wp_query:posts'); if (!$posts_data && $post_ids) { $posts_prepared = "SELECT * FROM {$wpdb->posts} WHERE ID IN (" . join(',', array_filter($post_ids)) . ") "; $posts_data = $wpdb->get_results($posts_prepared); wp_cache_set($cache_key, $posts_data, 'wp_query:posts'); } if ($posts_data) { foreach ($posts_data as $post) { $_post = wp_cache_get($post->ID, 'posts'); if (!$_post) { $_post = $post; $_post = sanitize_post($_post, 'raw'); wp_cache_add($_post->ID, $_post, 'posts'); } } } } if ($terms) { $cache_group = 'element_language_details'; foreach ($terms as $taxonomy => $term_ids) { $element_type = 'tax_' . $taxonomy; $terms_translations_prepared = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type = %s AND element_id IN (" . join(',', $term_ids) . ")", array($element_type)); $terms_translations = $wpdb->get_results($terms_translations_prepared); foreach ($terms_translations as $terms_translation) { $cache_key = $terms_translation->element_id . ':' . $terms_translation->element_type; $cached_details = wp_cache_get($cache_key, $cache_group); if (!$cached_details) { wp_cache_set($cache_key, $terms_translation, $cache_group); } $icl_object_id_cache_group = 'icl_object_id'; $icl_object_id_cache_key_args = array($terms_translation->element_id, $taxonomy, false, $terms_translation->language_code); $icl_object_id_cache_key = implode(':', array_filter($icl_object_id_cache_key_args)); $icl_object_id = wp_cache_get($cache_key, $cache_group); if (!$icl_object_id) { wp_cache_set($icl_object_id_cache_key, $terms_translation->element_id, $icl_object_id_cache_group); } $icl_object_id_cache_key_args = array($terms_translation->element_id, $taxonomy, true, $terms_translation->language_code); $icl_object_id_cache_key = implode(':', array_filter($icl_object_id_cache_key_args)); $icl_object_id = wp_cache_get($cache_key, $cache_group); if (!$icl_object_id) { wp_cache_set($icl_object_id_cache_key, $terms_translation->element_id, $icl_object_id_cache_group); } } } } } } $last_query = $wp_query->query_vars_hash; } }
function ambrosite_body_class($classes) { $post_name_prefix = 'postname-'; $page_name_prefix = 'pagename-'; $single_term_prefix = 'single-'; $single_parent_prefix = 'parent-'; $category_parent_prefix = 'parent-category-'; $term_parent_prefix = 'parent-term-'; $site_prefix = 'site-'; global $wp_query; if (is_single()) { $wp_query->post = $wp_query->posts[0]; setup_postdata($wp_query->post); $classes[] = $post_name_prefix . $wp_query->post->post_name; $taxonomies = array_filter(get_post_taxonomies($wp_query->post->ID), "is_taxonomy_hierarchical"); foreach ($taxonomies as $taxonomy) { $tax_name = $taxonomy != 'category' ? $taxonomy . '-' : ''; $terms = get_the_terms($wp_query->post->ID, $taxonomy); if ($terms) { foreach ($terms as $term) { if (!empty($term->slug)) { $classes[] = $single_term_prefix . $tax_name . sanitize_html_class($term->slug, $term->term_id); } while ($term->parent) { $term =& get_term((int) $term->parent, $taxonomy); if (!empty($term->slug)) { $classes[] = $single_parent_prefix . $tax_name . sanitize_html_class($term->slug, $term->term_id); } } } } } } elseif (is_archive()) { if (is_category()) { $cat = $wp_query->get_queried_object(); while ($cat->parent) { $cat =& get_category((int) $cat->parent); if (!empty($cat->slug)) { $classes[] = $category_parent_prefix . sanitize_html_class($cat->slug, $cat->cat_ID); } } } elseif (is_tax()) { $term = $wp_query->get_queried_object(); while ($term->parent) { $term =& get_term((int) $term->parent, $term->taxonomy); if (!empty($term->slug)) { $classes[] = $term_parent_prefix . sanitize_html_class($term->slug, $term->term_id); } } } } elseif (is_page()) { $wp_query->post = $wp_query->posts[0]; setup_postdata($wp_query->post); $classes[] = $page_name_prefix . $wp_query->post->post_name; } if (is_multisite()) { global $blog_id; $classes[] = $site_prefix . $blog_id; } return $classes; }
function mfn_breadcrumbs($class = false) { global $post; $translate['home'] = mfn_opts_get('translate') ? mfn_opts_get('translate-home', 'Home') : __('Home', 'betheme'); $homeLink = home_url(); $separator = ' <span><i class="icon-right-open"></i></span>'; // Plugin | bbPress ----------------------------------- if (function_exists('is_bbpress') && is_bbpress()) { bbp_breadcrumb(array('before' => '<ul class="breadcrumbs">', 'after' => '</ul>', 'sep' => '<i class="icon-right-open"></i>', 'crumb_before' => '<li>', 'crumb_after' => '</li>', 'home_text' => $translate['home'])); return true; } // end: bbPress ------------------------------------- // Default breadcrumbs -------------------------------- $breadcrumbs = array(); // Home prefix -------------------------------- $breadcrumbs[] = '<a href="' . $homeLink . '">' . $translate['home'] . '</a>'; // Blog ------------------------------------------- if (get_post_type() == 'post') { $blogID = false; if (get_option('page_for_posts')) { $blogID = get_option('page_for_posts'); // Setings / Reading } elseif (mfn_opts_get('blog-page')) { $blogID = mfn_opts_get('blog-page'); // Theme Options / Getting Started / Blog } if ($blogID) { $breadcrumbs[] = '<a href="' . get_permalink($blogID) . '">' . get_the_title($blogID) . '</a>'; } } // Plugin | Events Calendar ------------------------------------------- if (function_exists('tribe_is_month') && (tribe_is_event_query() || tribe_is_month() || tribe_is_event() || tribe_is_day() || tribe_is_venue())) { if (function_exists('tribe_get_events_link')) { $breadcrumbs[] = '<a href="' . tribe_get_events_link() . '">' . tribe_get_events_title() . '</a>'; } } elseif (is_front_page() || is_home()) { // do nothing // Blog | Tag ------------------------------------- } elseif (is_tag()) { $breadcrumbs[] = '<a href="' . curPageURL() . '">' . single_tag_title('', false) . '</a>'; // Blog | Category -------------------------------- } elseif (is_category()) { $breadcrumbs[] = '<a href="' . curPageURL() . '">' . single_cat_title('', false) . '</a>'; // Blog | Author ---------------------------------- } elseif (is_author()) { $breadcrumbs[] = '<a href="' . curPageURL() . '">' . get_the_author() . '</a>'; // Blog | Day ------------------------------------- } elseif (is_day()) { $breadcrumbs[] = '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>'; $breadcrumbs[] = '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('F') . '</a>'; $breadcrumbs[] = '<a href="' . curPageURL() . '">' . get_the_time('d') . '</a>'; // Blog | Month ----------------------------------- } elseif (is_month()) { $breadcrumbs[] = '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>'; $breadcrumbs[] = '<a href="' . curPageURL() . '">' . get_the_time('F') . '</a>'; // Blog | Year ------------------------------------ } elseif (is_year()) { $breadcrumbs[] = '<a href="' . curPageURL() . '">' . get_the_time('Y') . '</a>'; // Single ----------------------------------------- } elseif (is_single() && !is_attachment()) { // Custom Post Type ----------------- if (get_post_type() != 'post') { $post_type = get_post_type_object(get_post_type()); $slug = $post_type->rewrite; $portfolio_page_id = mfn_wpml_ID(mfn_opts_get('portfolio-page')); // Portfolio Page ------------ if ($slug['slug'] == mfn_opts_get('portfolio-slug', 'portfolio-item') && $portfolio_page_id) { $breadcrumbs[] = '<a href="' . get_page_link($portfolio_page_id) . '">' . get_the_title($portfolio_page_id) . '</a>'; } // Category ---------- if ($portfolio_page_id) { $terms = get_the_terms(get_the_ID(), 'portfolio-types'); if (!empty($terms) && !is_wp_error($terms)) { $term = $terms[0]; $breadcrumbs[] = '<a href="' . get_term_link($term) . '">' . $term->name . '</a>'; } } // Single Item -------- $breadcrumbs[] = '<a href="' . curPageURL() . '">' . get_the_title() . '</a>'; // Blog | Single -------------------- } else { $cat = get_the_category(); if (!empty($cat)) { $breadcrumbs[] = get_category_parents($cat[0], true, $separator); } $breadcrumbs[] = '<a href="' . curPageURL() . '">' . get_the_title() . '</a>'; } // Taxonomy --------------------------------------- } elseif (!is_page() && get_post_taxonomies()) { // Portfolio ------------------------ $post_type = get_post_type_object(get_post_type()); if ($post_type->name == 'portfolio' && ($portfolio_page_id = mfn_wpml_ID(mfn_opts_get('portfolio-page')))) { $breadcrumbs[] = '<a href="' . get_page_link($portfolio_page_id) . '">' . get_the_title($portfolio_page_id) . '</a>'; } $breadcrumbs[] = '<a href="' . curPageURL() . '">' . single_cat_title('', false) . '</a>'; // Page with parent ------------------------------- } elseif (is_page() && $post->post_parent) { $parent_id = $post->post_parent; $parents = array(); while ($parent_id) { $page = get_page($parent_id); $parents[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>'; $parent_id = $page->post_parent; } $parents = array_reverse($parents); $breadcrumbs = array_merge_recursive($breadcrumbs, $parents); $breadcrumbs[] = '<a href="' . curPageURL() . '">' . get_the_title(mfn_ID()) . '</a>'; // Default ---------------------------------------- } else { $breadcrumbs[] = '<a href="' . curPageURL() . '">' . get_the_title(mfn_ID()) . '</a>'; } // PRINT ------------------------------------------------------------------ echo '<ul class="breadcrumbs ' . $class . '">'; $count = count($breadcrumbs); $i = 1; foreach ($breadcrumbs as $bk => $bc) { if (strpos($bc, $separator)) { // Category parents fix echo '<li>' . $bc . '</li>'; } else { if ($i == $count) { $separator = ''; } echo '<li>' . $bc . $separator . '</li>'; } $i++; } echo '</ul>'; }
/** * Flushes post cache * * @param integer $post_id * @return boolean */ function flush_post($post_id = null) { if (!$post_id) { $post_id = w3_detect_post_id(); } if ($post_id) { $full_urls = array(); $global_urls = array(); $post = null; $terms = array(); $feeds = $this->_config->get_array('pgcache.purge.feed.types'); $limit_post_pages = $this->_config->get_integer('pgcache.purge.postpages_limit'); if ($this->_config->get_boolean('pgcache.purge.terms') || $this->_config->get_boolean('pgcache.purge.feed.terms')) { $taxonomies = get_post_taxonomies($post_id); $terms = wp_get_post_terms($post_id, $taxonomies); } /** * @var $purge_urls W3_SharedPageUrls */ $purge_urls = w3_instance('W3_SharedPageUrls'); $post = get_post($post_id); $post_type = in_array($post->post_type, array('post', 'page', 'attachment', 'revision')) ? null : $post->post_type; $front_page = get_option('show_on_front'); /** * Home (Frontpage) URL */ if ($this->_config->get_boolean('pgcache.purge.home') && $front_page == 'posts' || $this->_config->get_boolean('pgcache.purge.front_page')) { $global_urls = array_merge($global_urls, $purge_urls->get_frontpage_urls($limit_post_pages)); } /** * Home (Post page) URL */ if ($this->_config->get_boolean('pgcache.purge.home') && $front_page != 'posts') { $global_urls = array_merge($global_urls, $purge_urls->get_postpage_urls($limit_post_pages)); } /** * Post URL */ if ($this->_config->get_boolean('pgcache.purge.post')) { $full_urls = array_merge($full_urls, $purge_urls->get_post_urls($post_id)); } /** * Post comments URLs */ if ($this->_config->get_boolean('pgcache.purge.comments') && function_exists('get_comments_pagenum_link')) { $full_urls = array_merge($full_urls, $purge_urls->get_post_comments_urls($post_id)); } /** * Post author URLs */ if ($this->_config->get_boolean('pgcache.purge.author')) { $full_urls = array_merge($full_urls, $purge_urls->get_post_author_urls($post->post_author, $limit_post_pages)); } /** * Post terms URLs */ if ($this->_config->get_boolean('pgcache.purge.terms')) { $full_urls = array_merge($full_urls, $purge_urls->get_post_terms_urls($terms, $limit_post_pages)); } /** * Daily archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.daily') && $post) { $full_urls = array_merge($full_urls, $purge_urls->get_daily_archive_urls($post, $limit_post_pages)); } /** * Monthly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.monthly') && $post) { $full_urls = array_merge($full_urls, $purge_urls->get_monthly_archive_urls($post, $limit_post_pages)); } /** * Yearly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.yearly') && $post) { $full_urls = array_merge($full_urls, $purge_urls->get_yearly_archive_urls($post, $limit_post_pages)); } /** * Feed URLs */ if ($this->_config->get_boolean('pgcache.purge.feed.blog')) { $global_urls = array_merge($global_urls, $purge_urls->get_feed_urls($feeds, $post_type)); } if ($this->_config->get_boolean('pgcache.purge.feed.comments')) { $full_urls = array_merge($full_urls, $purge_urls->get_feed_comments_urls($post_id, $feeds)); } if ($this->_config->get_boolean('pgcache.purge.feed.author')) { $full_urls = array_merge($full_urls, $purge_urls->get_feed_author_urls($post->post_author, $feeds)); } if ($this->_config->get_boolean('pgcache.purge.feed.terms')) { $full_urls = array_merge($full_urls, $purge_urls->get_feed_terms_urls($terms, $feeds)); } /** * Purge selected pages */ if ($this->_config->get_array('pgcache.purge.pages')) { $pages = $this->_config->get_array('pgcache.purge.pages'); $global_urls = array_merge($global_urls, $purge_urls->get_pages_urls($pages)); } /** * Purge sitemaps if a sitemap option has a regex */ if ($this->_config->get_string('pgcache.purge.sitemap_regex')) { $cache = $this->_get_cache(); $cache->flush('sitemaps'); } if ($this->_do_flush_global_urls()) { $full_urls = array_merge($global_urls, $full_urls); } elseif ($this->_flushes == 0) { $this->_repeated_urls = $global_urls; } /** * Flush cache */ if (count($full_urls)) { $this->_flushes++; $cache = $this->_get_cache(); $mobile_groups = $this->_get_mobile_groups(); $referrer_groups = $this->_get_referrer_groups(); $encryptions = $this->_get_encryptions(); $compressions = $this->_get_compressions(); foreach ($full_urls as $url) { if (!in_array($url, $this->_repeated_urls) && !in_array($url, $this->_flushed_urls)) { $this->_flushed_urls[] = $url; $this->flush_url($url, $cache, $mobile_groups, $referrer_groups, $encryptions, $compressions); } elseif (!in_array($url, $this->_repeated_urls)) { $this->_repeated_urls[] = $url; } } } return true; } return false; }
function wpt_taxonomy_template($tweet, $post_ID) { $taxonomies = get_post_taxonomies($post_ID); foreach ($taxonomies as $tax) { $terms = get_the_terms($post_ID, $tax); if (is_array($terms)) { foreach ($terms as $term) { $template = get_term_meta($term->term_id, '_wpt_term_template', true); if ($template != '') { return apply_filters('wpt_term_template_filter', $template, $term->term_id); } } } } return $tweet; }
/** * JS is injected into post.php when action=edit in order to add an asterisk * to protected terms. This is very, very, very bad form, but there aren't the * necessary hooks to do this server side (well, there is, but its obviously * never been used for anything since it's very buggy), so it's either this or * nothing. In this case, I side with usability over good coding practices. * * @param type $term_name */ public static function tag_protected_terms_heirarchal() { global $current_screen; if ($current_screen->base === 'post' && isset($_REQUEST['post'])) { ?> <script type="text/javascript">jQuery(function(){<?php //Get taxonomies for this post $taxonomies = get_post_taxonomies($_REQUEST['post']); //For each taxonomy, get a list of term ids used for this post foreach ($taxonomies as $tax) { //Initialize vars $terms = get_terms($tax); $termlist = array(); //Build an array out of the term ids... foreach ($terms as $term) { //...but only if it's protected if (CTXPS_Queries::check_term_protection($term->term_id, $tax)) { $termlist[] = $term->term_id; } } //Join the array into a CSV $termlist = join(',', $termlist); //Generate javascript to add asterisk to protected terms if (!empty($termlist)) { $tarray = "{$tax}_protect"; ?> var <?php echo $tarray; ?> = [<?php echo $termlist; ?> ]; for(x in <?php echo $tarray; ?> ){ jQuery('#<?php echo $tax; ?> div input[value="'+<?php echo $tarray; ?> [x]+'"]').parent().append('*'); jQuery('#<?php echo $tax; ?> div option[value="'+<?php echo $tarray; ?> [x]+'"]').append('*'); } <?php } //So there's no accidental carryovers unset($terms, $termlist); } ?> });</script><?php } //Nothing to do return false; }
public function filter_post_cache_transients($transients, $mod, $locale, $sharing_url) { // clear blog home page $transients['WpssoHead::get_head_array'][] = 'locale:' . $locale . '_url:' . home_url('/'); // clear date based archive pages $year = get_the_time('Y', $mod['id']); $month = get_the_time('m', $mod['id']); $day = get_the_time('d', $mod['id']); $transients['WpssoHead::get_head_array'][] = 'locale:' . $locale . '_url:' . get_year_link($year); $transients['WpssoHead::get_head_array'][] = 'locale:' . $locale . '_url:' . get_month_link($year, $month); $transients['WpssoHead::get_head_array'][] = 'locale:' . $locale . '_url:' . get_day_link($year, $month, $day); // clear term archive page meta tags (and json markup) foreach (get_post_taxonomies($mod['id']) as $tax_name) { foreach (wp_get_post_terms($mod['id'], $tax_name) as $term) { $transients['WpssoHead::get_head_array'][] = 'locale:' . $locale . '_term:' . $term->term_id . '_tax:' . $tax_name; } } // clear author archive page meta tags (and json markup) $author_id = get_post_field('post_author', $mod['id']); $transients['WpssoHead::get_head_array'][] = 'locale:' . $locale . '_user:' . $author_id; return $transients; }
/** * Hydrate custom type * @param $object Object to hydrate * @param $fields Array['fields' => [], 'taxonomies' => []] This array allow to choose exactly which custom fields and taxonomies need to be retrieve (performance improvement) * You can set 'fields' attribute for custom fields and 'taxonomies' attribute for taxonomies. The arrays can be a list of string or can have others array inside to describe what to do * on a key (exemple: univers => ['color'] rather than 'univers' which get all the univers fields). * @param $depth maximal depth for hydration * @filter rootpress_before_hydrate Filter occur before begin the hydrate * @filter rootpress_after_hydrate_<post_type> Filter occur after object have been hydrated */ public static function hydrate(&$object, $fields = [], $depth = 2) { // Case null or not an object if (is_null($object) || !is_object($object) && !is_array($object)) { return $object; } else { if (is_a($object, 'WP_Error')) { return $object; } else { if (is_array($object) && array_key_exists('user_registered', $object) && array_key_exists('user_avatar', $object) && isset($object['ID'])) { $object = get_user_by('ID', $object['ID']); } else { if (is_array($object) && isset($fields['fields']) && !empty($fields['fields'])) { return self::hydrates($object, $fields, $depth); } } } } // Determine if object is a post, a taxonomy or a user $type = ''; if (get_class($object) === 'WP_Post') { $type = 'post'; } else { if (get_class($object) === 'WP_Term') { $type = 'taxonomy'; } else { if (get_class($object) === 'WP_User') { $type = 'user'; } else { return $object; } } } // Get is ID $ID = self::getIdFromWpObject($object); // Create hash for cache system $fieldsMD5 = md5(serialize($fields)); // Fire filter which allow rootpress to transform WP_Post as Model entity $object = apply_filters('rootpress_before_hydrate', $object); // Prevent infinite looping if ($depth > 0 || is_array($fields) && !empty($fields)) { // Call cached system if we have already cache this item since the beginning of the request (base on ID and fields when caching) if (!self::$disableCache && isset(self::$objectCache[$type . '_' . $ID . '_depth_' . $depth . '_fields_' . $fieldsMD5])) { return self::$objectCache[$type . '_' . $ID . '_depth_' . $depth . '_fields_' . $fieldsMD5]; } // Keep count self::$countHydrate++; /* Hydrate customs fields */ // Get only fields wanted if (!empty($fields) && isset($fields['fields'])) { $customFields = []; foreach ($fields['fields'] as $field_key => $field) { $fieldName = is_array($field) ? $field_key : $field; $acfKey = self::getAcfKey($object, $type, $ID); $customFields[$fieldName] = get_field($fieldName, $acfKey); } } else { $acfKey = self::getAcfKey($object, $type, $ID); $customFields = get_fields($acfKey); if ($customFields === false) { $customFields = []; } } // Add the custom fields to the object foreach ($customFields as $key => $value) { $object->{$key} = $value; $current = $object->{$key}; // Hydrate the child(s) if it's an object if (is_array($object->{$key}) && count($object->{$key}) > 0 && isset($current[0]) && is_object($current[0]) && in_array(get_class($current[0]), ['WP_Post', 'WP_Term', 'WP_User'])) { // If we declare specific fields to return, send them to Hydratator $fieldsForThisKey = isset($fields['fields']) && isset($fields['fields'][$key]) && is_array($fields['fields'][$key]) ? $fields['fields'][$key] : []; // Handle WP_User case by using setter to avoid notice from magic WP_User setter function // TODO Can we find a better method than disable php notice ? "Notice: Indirect modification of overloaded property WP_User as no effect" $actualLevelReporting = error_reporting(E_ERROR | E_WARNING); $object->{$key} = self::hydrates($object->{$key}, $fieldsForThisKey, $depth - 1); error_reporting($actualLevelReporting); } else { if (is_object($current) && in_array(get_class($current), ['WP_Post', 'WP_Term', 'WP_User'])) { // If we declare specific fields to return, send them to Hydratator $fieldsForThisKey = isset($fields['fields']) && isset($fields['fields'][$key]) && is_array($fields['fields'][$key]) ? $fields['fields'][$key] : []; $object->{$key} = self::hydrate($object->{$key}, $fieldsForThisKey, $depth - 1); } else { if (is_array($object->{$key}) && isset($fields['fields']) && isset($fields['fields'][$key]) && is_array($fields['fields'][$key])) { $fieldsForThisKey = $fields['fields'][$key]; $object->{$key} = self::hydrates($object->{$key}, $fieldsForThisKey, $depth - 1); } } } } // Hydrate taxonomies $taxonomies = []; // Get only taxonomies wanted if (!empty($fields) && isset($fields['taxonomies'])) { foreach ($fields['taxonomies'] as $field_key => $field) { $fieldName = is_array($field) ? $field_key : $field; $taxonomies[$fieldName] = get_the_terms($ID, $fieldName); if (is_array($taxonomies[$fieldName])) { $taxonomies[$fieldName] = array_values($taxonomies[$fieldName]); } } } else { $taxonomiesList = get_post_taxonomies($ID); foreach ($taxonomiesList as $taxo) { $taxonomies[$taxo] = get_the_terms($ID, $taxo); if (is_array($taxonomies[$taxo])) { $taxonomies[$taxo] = array_values($taxonomies[$taxo]); } } } // Hydrate taxonomies foreach ($taxonomies as $key => $value) { $object->{$key} = $value; $fieldsForThisKey = isset($fields['taxonomies']) && isset($fields['taxonomies'][$key]) && is_array($fields['taxonomies'][$key]) ? $fields['taxonomies'][$key] : []; if ($value != false) { if (is_array($value)) { $object->{$key} = self::hydrates($value, $fieldsForThisKey, $depth - 1); } else { $object->{$key} = self::hydrate($value, $fieldsForThisKey, $depth - 1); } } } } // Filter which allow to do custom hydratation if (isset($object->post_type)) { $object = apply_filters('rootpress_after_hydrate_' . self::getTypeFromWpObject($object), $object); } // Call clean method if exist which allow to clean the object from undesired values (default from wordpress for example) if (method_exists($object, 'clean')) { $object->clean(); } // Call construct if exist which allow to change some values on creation if (method_exists($object, 'construct')) { $object->construct(); } // Put the object in cache self::$objectCache[$type . '_' . $ID . '_depth_' . $depth . '_fields_' . $fieldsMD5] = $object; return $object; }
/** * Determine whether the widget should be displayed based on conditions set by the user. * * @param array $instance The widget settings. * @return array Settings to display or bool false to hide. */ public static function filter_widget($instance) { global $wp_query; if (empty($instance['conditions']) || empty($instance['conditions']['rules'])) { return $instance; } // Store the results of all in-page condition lookups so that multiple widgets with // the same visibility conditions don't result in duplicate DB queries. static $condition_result_cache = array(); $condition_result = false; foreach ($instance['conditions']['rules'] as $rule) { $condition_key = self::generate_condition_key($rule); if (isset($condition_result_cache[$condition_key])) { $condition_result = $condition_result_cache[$condition_key]; } else { switch ($rule['major']) { case 'date': switch ($rule['minor']) { case '': $condition_result = is_date(); break; case 'month': $condition_result = is_month(); break; case 'day': $condition_result = is_day(); break; case 'year': $condition_result = is_year(); break; } break; case 'page': // Previously hardcoded post type options. if ('post' == $rule['minor']) { $rule['minor'] = 'post_type-post'; } else { if (!$rule['minor']) { $rule['minor'] = 'post_type-page'; } } switch ($rule['minor']) { case '404': $condition_result = is_404(); break; case 'search': $condition_result = is_search(); break; case 'archive': $condition_result = is_archive(); break; case 'posts': $condition_result = $wp_query->is_posts_page; break; case 'home': $condition_result = is_home(); break; case 'front': if (current_theme_supports('infinite-scroll')) { $condition_result = is_front_page(); } else { $condition_result = is_front_page() && !is_paged(); } break; default: if (substr($rule['minor'], 0, 10) == 'post_type-') { $condition_result = is_singular(substr($rule['minor'], 10)); } elseif ($rule['minor'] == get_option('page_for_posts')) { // If $rule['minor'] is a page ID which is also the posts page $condition_result = $wp_query->is_posts_page; } else { // $rule['minor'] is a page ID $condition_result = is_page($rule['minor']); // Check if $rule['minor'] is parent of page ID if (!$condition_result && isset($rule['has_children']) && $rule['has_children']) { $condition_result = wp_get_post_parent_id(get_the_ID()) == $rule['minor']; } } break; } break; case 'tag': if (!$rule['minor'] && is_tag()) { $condition_result = true; } else { $rule['minor'] = self::maybe_get_split_term($rule['minor'], $rule['major']); if (is_singular() && $rule['minor'] && has_tag($rule['minor'])) { $condition_result = true; } else { $tag = get_tag($rule['minor']); if ($tag && !is_wp_error($tag) && is_tag($tag->slug)) { $condition_result = true; } } } break; case 'category': if (!$rule['minor'] && is_category()) { $condition_result = true; } else { $rule['minor'] = self::maybe_get_split_term($rule['minor'], $rule['major']); if (is_category($rule['minor'])) { $condition_result = true; } else { if (is_singular() && $rule['minor'] && in_array('category', get_post_taxonomies()) && has_category($rule['minor'])) { $condition_result = true; } } } break; case 'loggedin': $condition_result = is_user_logged_in(); if ('loggedin' !== $rule['minor']) { $condition_result = !$condition_result; } break; case 'author': $post = get_post(); if (!$rule['minor'] && is_author()) { $condition_result = true; } else { if ($rule['minor'] && is_author($rule['minor'])) { $condition_result = true; } else { if (is_singular() && $rule['minor'] && $rule['minor'] == $post->post_author) { $condition_result = true; } } } break; case 'role': if (is_user_logged_in()) { $current_user = wp_get_current_user(); $user_roles = $current_user->roles; if (in_array($rule['minor'], $user_roles)) { $condition_result = true; } else { $condition_result = false; } } else { $condition_result = false; } break; case 'taxonomy': $term = explode('_tax_', $rule['minor']); // $term[0] = taxonomy name; $term[1] = term id if (isset($term[0]) && isset($term[1])) { $term[1] = self::maybe_get_split_term($term[1], $term[0]); } if (isset($term[1]) && is_tax($term[0], $term[1])) { $condition_result = true; } else { if (isset($term[1]) && is_singular() && $term[1] && has_term($term[1], $term[0])) { $condition_result = true; } else { if (is_singular() && ($post_id = get_the_ID())) { $terms = get_the_terms($post_id, $rule['minor']); // Does post have terms in taxonomy? if ($terms && !is_wp_error($terms)) { $condition_result = true; } } } } break; } if ($condition_result || self::$passed_template_redirect) { // Some of the conditions will return false when checked before the template_redirect // action has been called, like is_page(). Only store positive lookup results, which // won't be false positives, before template_redirect, and everything after. $condition_result_cache[$condition_key] = $condition_result; } } if ($condition_result) { break; } } if ('show' == $instance['conditions']['action'] && !$condition_result || 'hide' == $instance['conditions']['action'] && $condition_result) { return false; } return $instance; }
/** * Purges cdn mirror post cache * * @param integer $post_id * @return boolean */ function purge_post($post_id) { if (!$post_id) { $post_id = $this->_detect_post_id(); } if ($post_id) { $full_urls = array(); $global_urls = array(); $post = null; $terms = array(); $feeds = $this->_config->get_array('pgcache.purge.feed.types'); $limit_post_pages = $this->_config->get_integer('pgcache.purge.postpages_limit'); if ($this->_config->get_boolean('pgcache.purge.terms') || $this->_config->get_boolean('varnish.pgcache.feed.terms')) { $taxonomies = get_post_taxonomies($post_id); $terms = wp_get_post_terms($post_id, $taxonomies); } $purge_urls = w3_instance('W3_PageUrls'); switch (true) { case $this->_config->get_boolean('pgcache.purge.author'): case $this->_config->get_boolean('pgcache.purge.archive.daily'): case $this->_config->get_boolean('pgcache.purge.archive.monthly'): case $this->_config->get_boolean('pgcache.purge.archive.yearly'): case $this->_config->get_boolean('pgcache.purge.feed.author'): $post = get_post($post_id); } $front_page = get_option('show_on_front'); /** * Home (Frontpage) URL */ if ($this->_config->get_boolean('pgcache.purge.home') && $front_page == 'posts' || $this->_config->get_boolean('pgcache.purge.front_page')) { $global_urls = array_merge($global_urls, $purge_urls->get_frontpage_urls($limit_post_pages)); } /** * Home (Post page) URL */ if ($this->_config->get_boolean('pgcache.purge.home') && $front_page != 'posts') { $global_urls = array_merge($global_urls, $purge_urls->get_postpage_urls($limit_post_pages)); } /** * Post URL */ if ($this->_config->get_boolean('pgcache.purge.post')) { $full_urls = array_merge($full_urls, $purge_urls->get_post_urls($post_id)); } /** * Post comments URLs */ if ($this->_config->get_boolean('pgcache.purge.comments') && function_exists('get_comments_pagenum_link')) { $full_urls = array_merge($full_urls, $purge_urls->get_post_comments_urls($post_id)); } /** * Post author URLs */ if ($this->_config->get_boolean('pgcache.purge.author') && $post) { $full_urls = array_merge($full_urls, $purge_urls->get_post_author_urls($post->post_author, $limit_post_pages)); } /** * Post terms URLs */ if ($this->_config->get_boolean('pgcache.purge.terms')) { $full_urls = array_merge($full_urls, $purge_urls->get_post_terms_urls($terms, $limit_post_pages)); } /** * Daily archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.daily') && $post) { $full_urls = array_merge($full_urls, $purge_urls->get_daily_archive_urls($post, $limit_post_pages)); } /** * Monthly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.monthly') && $post) { $full_urls = array_merge($full_urls, $purge_urls->get_montly_archive_urls($post, $limit_post_pages)); } /** * Yearly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.yearly') && $post) { $full_urls = array_merge($full_urls, $purge_urls->get_yearly_archive_urls($post, $limit_post_pages)); } /** * Feed URLs */ if ($this->_config->get_boolean('pgcache.purge.feed.blog')) { $global_urls = array_merge($global_urls, $purge_urls->get_feed_urls($feeds)); } if ($this->_config->get_boolean('pgcache.purge.feed.comments')) { $full_urls = array_merge($full_urls, $purge_urls->get_feed_comments_urls($post_id, $feeds)); } if ($this->_config->get_boolean('pgcache.purge.feed.author') && $post) { $full_urls = array_merge($full_urls, $purge_urls->get_feed_author_urls($post->post_author, $feeds)); } if ($this->_config->get_boolean('pgcache.purge.feed.terms')) { $full_urls = array_merge($full_urls, $purge_urls->get_feed_terms_urls($terms, $feeds)); } /** * Purge selected pages */ if ($this->_config->get_array('pgcache.purge.pages')) { $pages = $this->_config->get_array('pgcache.purge.pages'); $global_urls = array_merge($global_urls, $purge_urls->get_pages_urls($pages)); } if ($this->_do_flush_global_urls()) { $full_urls = array_merge($global_urls, $full_urls); } elseif ($this->_flushes == 0) { $this->_repeated_urls = $global_urls; } /** * Flush cache */ if (count($full_urls)) { $this->_flushes++; $w3_plugin_cdncommon = w3_instance('W3_Plugin_CdnCommon'); $results = array(); $files = array(); foreach ($full_urls as $url) { if (!in_array($url, $this->_repeated_urls) && !in_array($url, $this->_flushed_urls)) { $parsed = parse_url($url); $local_site_path = isset($parsed['path']) ? ltrim($parsed['path'], '/') : ''; $remote_path = $w3_plugin_cdncommon->uri_to_cdn_uri($local_site_path); $files[] = $w3_plugin_cdncommon->build_file_descriptor($local_site_path, $remote_path); $this->_flushed_urls[] = $url; } elseif (!in_array($url, $this->_repeated_urls)) { $this->_repeated_urls[] = $url; } } return $w3_plugin_cdncommon->purge($files, false, $results); } } return false; }
public function getPostObj($data) { $algolia_registry = \Algolia\Core\Registry::getInstance(); $obj = new \stdClass(); foreach ($this->contentFieldsNames as $key => $value) { $name = $value["label"]; $obj->{$name} = $this->cast($data->{$key}, $value["type"]); } $obj->author = get_the_author_meta('display_name', $data->post_author); $obj->author_first_name = get_the_author_meta('first_name', $data->post_author); $obj->author_last_name = get_the_author_meta('last_name', $data->post_author); $obj->author_login = get_the_author_meta('user_login', $data->post_author); $obj->permalink = get_permalink($data->ID); $this->getContent($data, $obj); unset($obj->excerpt); //$obj->excerpt = my_excerpt($data->post_content, get_the_excerpt()); $thumbnail_id = get_post_thumbnail_id($data->ID); if ($thumbnail_id) { $obj->featureImage = $this->getImage($thumbnail_id); } if ($algolia_registry->metas && isset($algolia_registry->metas[$data->post_type]) && is_array($algolia_registry->metas[$data->post_type])) { foreach (get_post_meta($data->ID) as $meta_key => $meta_value) { if (in_array($meta_key, array_keys($algolia_registry->metas[$data->post_type]))) { if ($algolia_registry->metas[$data->post_type][$meta_key]["indexable"]) { $obj->{$meta_key} = $this->try_cast($meta_value[0]); } } } } foreach (get_post_taxonomies($data->ID) as $tax) { $terms = wp_get_post_terms($data->ID, $tax); if (count($terms) <= 0) { continue; } if (isset($algolia_registry->metas['tax']) && isset($algolia_registry->metas['tax'][$tax])) { $obj->{$tax} = array_map(function ($obj) { return $obj->name; }, $terms); } } if (has_filter('prepare_algolia_record')) { $obj = apply_filters('prepare_algolia_record', $obj); } return (array) $obj; }
/** * Fetches a list of all terms associated with the post, ignoring taxonomy. * * @param int $post_id The id of the post to get terms for. * @return array An array containing all the terms attached to this post, regardless of taxonomy */ public static function get_post_terms($post_id) { //initialize variables $terms = array(); //Get all terms for this post, regardless of taxonomy foreach (get_post_taxonomies($post_id) as $taxonomy) { $terms = array_merge(wp_get_post_terms($post_id, $taxonomy), $terms); } return $terms; }
/** * Flushes post cache * * @param integer $post_id * @return boolean */ function flush_post($post_id = null) { if (!$post_id) { $post_id = $this->_detect_post_id(); } if ($post_id) { $uris = array(); $domain_url = w3_get_domain_url(); $feeds = $this->_config->get_array('pgcache.purge.feed.types'); if ($this->_config->get_boolean('pgcache.purge.terms') || $this->_config->get_boolean('pgcache.purge.feed.terms')) { $taxonomies = get_post_taxonomies($post_id); $terms = nxt_get_post_terms($post_id, $taxonomies); } switch (true) { case $this->_config->get_boolean('pgcache.purge.author'): case $this->_config->get_boolean('pgcache.purge.archive.daily'): case $this->_config->get_boolean('pgcache.purge.archive.monthly'): case $this->_config->get_boolean('pgcache.purge.archive.yearly'): case $this->_config->get_boolean('pgcache.purge.feed.author'): $post = get_post($post_id); } /** * Home URL */ if ($this->_config->get_boolean('pgcache.purge.home')) { $home_path = w3_get_home_path(); $site_path = w3_get_site_path(); $uris[] = $home_path; if ($site_path != $home_path) { $uris[] = $site_path; } } /** * Post URL */ if ($this->_config->get_boolean('pgcache.purge.post')) { $post_link = post_permalink($post_id); $post_uri = str_replace($domain_url, '', $post_link); $uris[] = $post_uri; } /** * Post comments URLs */ if ($this->_config->get_boolean('pgcache.purge.comments') && function_exists('get_comments_pagenum_link')) { $comments_number = get_comments_number($post_id); $comments_per_page = get_option('comments_per_page'); $comments_pages_number = @ceil($comments_number / $comments_per_page); for ($pagenum = 1; $pagenum <= $comments_pages_number; $pagenum++) { $comments_pagenum_link = $this->_get_comments_pagenum_link($post_id, $pagenum); $comments_pagenum_uri = str_replace($domain_url, '', $comments_pagenum_link); $uris[] = $comments_pagenum_uri; } } /** * Post author URLs */ if ($this->_config->get_boolean('pgcache.purge.author') && $post) { $posts_number = count_user_posts($post->post_author); $posts_per_page = get_option('posts_per_page'); $posts_pages_number = @ceil($posts_number / $posts_per_page); $author_link = get_author_link(false, $post->post_author); $author_uri = str_replace($domain_url, '', $author_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $author_pagenum_link = $this->_get_pagenum_link($author_uri, $pagenum); $author_pagenum_uri = str_replace($domain_url, '', $author_pagenum_link); $uris[] = $author_pagenum_uri; } } /** * Post terms URLs */ if ($this->_config->get_boolean('pgcache.purge.terms')) { $posts_per_page = get_option('posts_per_page'); foreach ($terms as $term) { $term_link = get_term_link($term, $term->taxonomy); $term_uri = str_replace($domain_url, '', $term_link); $posts_pages_number = @ceil($term->count / $posts_per_page); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $term_pagenum_link = $this->_get_pagenum_link($term_uri, $pagenum); $term_pagenum_uri = str_replace($domain_url, '', $term_pagenum_link); $uris[] = $term_pagenum_uri; } } } /** * Daily archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.daily') && $post) { $post_date = strtotime($post->post_date); $post_year = gmdate('Y', $post_date); $post_month = gmdate('m', $post_date); $post_day = gmdate('d', $post_date); $posts_per_page = get_option('posts_per_page'); $posts_number = $this->_get_archive_posts_count($post_year, $post_month, $post_day); $posts_pages_number = @ceil($posts_number / $posts_per_page); $day_link = get_day_link($post_year, $post_month, $post_day); $day_uri = str_replace($domain_url, '', $day_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $day_pagenum_link = $this->_get_pagenum_link($day_uri, $pagenum); $day_pagenum_uri = str_replace($domain_url, '', $day_pagenum_link); $uris[] = $day_pagenum_uri; } } /** * Monthly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.monthly') && $post) { $post_date = strtotime($post->post_date); $post_year = gmdate('Y', $post_date); $post_month = gmdate('m', $post_date); $posts_per_page = get_option('posts_per_page'); $posts_number = $this->_get_archive_posts_count($post_year, $post_month); $posts_pages_number = @ceil($posts_number / $posts_per_page); $month_link = get_month_link($post_year, $post_month); $month_uri = str_replace($domain_url, '', $month_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $month_pagenum_link = $this->_get_pagenum_link($month_uri, $pagenum); $month_pagenum_uri = str_replace($domain_url, '', $month_pagenum_link); $uris[] = $month_pagenum_uri; } } /** * Yearly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.yearly') && $post) { $post_date = strtotime($post->post_date); $post_year = gmdate('Y', $post_date); $posts_per_page = get_option('posts_per_page'); $posts_number = $this->_get_archive_posts_count($post_year); $posts_pages_number = @ceil($posts_number / $posts_per_page); $year_link = get_year_link($post_year); $year_uri = str_replace($domain_url, '', $year_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $year_pagenum_link = $this->_get_pagenum_link($year_uri, $pagenum); $year_pagenum_uri = str_replace($domain_url, '', $year_pagenum_link); $uris[] = $year_pagenum_uri; } } /** * Feed URLs */ if ($this->_config->get_boolean('pgcache.purge.feed.blog')) { foreach ($feeds as $feed) { $feed_link = get_feed_link($feed); $feed_uri = str_replace($domain_url, '', $feed_link); $uris[] = $feed_uri; } } if ($this->_config->get_boolean('pgcache.purge.feed.comments')) { foreach ($feeds as $feed) { $post_comments_feed_link = get_post_comments_feed_link($post_id, $feed); $post_comments_feed_uri = str_replace($domain_url, '', $post_comments_feed_link); $uris[] = $post_comments_feed_uri; } } if ($this->_config->get_boolean('pgcache.purge.feed.author') && $post) { foreach ($feeds as $feed) { $author_feed_link = get_author_feed_link($post->post_author, $feed); $author_feed_uri = str_replace($domain_url, '', $author_feed_link); $uris[] = $author_feed_uri; } } if ($this->_config->get_boolean('pgcache.purge.feed.terms')) { foreach ($terms as $term) { foreach ($feeds as $feed) { $term_feed_link = get_term_feed_link($term->term_id, $term->taxonomy, $feed); $term_feed_uri = str_replace($domain_url, '', $term_feed_link); $uris[] = $term_feed_uri; } } } /** * Flush cache */ if (count($uris)) { $cache =& $this->_get_cache(); $mobile_groups = $this->_get_mobile_groups(); $referrer_groups = $this->_get_referrer_groups(); $encryptions = $this->_get_encryptions(); $compressions = $this->_get_compressions(); foreach ($uris as $uri) { foreach ($mobile_groups as $mobile_group) { foreach ($referrer_groups as $referrer_group) { foreach ($encryptions as $encryption) { foreach ($compressions as $compression) { $page_key = $this->_get_page_key($uri, $mobile_group, $referrer_group, $encryption, $compression); $cache->delete($page_key); } } } } } /** * Purge varnish servers */ if ($this->_config->get_boolean('varnish.enabled')) { $varnish =& w3_instance('W3_Varnish'); foreach ($uris as $uri) { $varnish->purge($uri); } } } return true; } return false; }
/** * Purges cdn's post cache * * @param integer $post_id * @return boolean */ public static function w3tc_flush_post($post_id) { if (!$post_id) { $post_id = Util_Environment::detect_post_id(); } if (!$post_id) { return false; } $config = Dispatcher::config(); $full_urls = array(); $post = null; $terms = array(); $feeds = $config->get_array('pgcache.purge.feed.types'); $limit_post_pages = $config->get_integer('pgcache.purge.postpages_limit'); if ($config->get_boolean('pgcache.purge.terms') || $config->get_boolean('varnish.pgcache.feed.terms')) { $taxonomies = get_post_taxonomies($post_id); $terms = wp_get_post_terms($post_id, $taxonomies); } switch (true) { case $config->get_boolean('pgcache.purge.author'): case $config->get_boolean('pgcache.purge.archive.daily'): case $config->get_boolean('pgcache.purge.archive.monthly'): case $config->get_boolean('pgcache.purge.archive.yearly'): case $config->get_boolean('pgcache.purge.feed.author'): $post = get_post($post_id); } $front_page = get_option('show_on_front'); /** * Home (Frontpage) URL */ if ($config->get_boolean('pgcache.purge.home') && $front_page == 'posts' || $config->get_boolean('pgcache.purge.front_page')) { $full_urls = array_merge($full_urls, Util_PageUrls::get_frontpage_urls($limit_post_pages)); } /** * Home (Post page) URL */ if ($config->get_boolean('pgcache.purge.home') && $front_page != 'posts') { $full_urls = array_merge($full_urls, Util_PageUrls::get_postpage_urls($limit_post_pages)); } /** * Post URL */ if ($config->get_boolean('pgcache.purge.post')) { $full_urls = array_merge($full_urls, Util_PageUrls::get_post_urls($post_id)); } /** * Post comments URLs */ if ($config->get_boolean('pgcache.purge.comments') && function_exists('get_comments_pagenum_link')) { $full_urls = array_merge($full_urls, Util_PageUrls::get_post_comments_urls($post_id)); } /** * Post author URLs */ if ($config->get_boolean('pgcache.purge.author') && $post) { $full_urls = array_merge($full_urls, Util_PageUrls::get_post_author_urls($post->post_author, $limit_post_pages)); } /** * Post terms URLs */ if ($config->get_boolean('pgcache.purge.terms')) { $full_urls = array_merge($full_urls, Util_PageUrls::get_post_terms_urls($terms, $limit_post_pages)); } /** * Daily archive URLs */ if ($config->get_boolean('pgcache.purge.archive.daily') && $post) { $full_urls = array_merge($full_urls, Util_PageUrls::get_daily_archive_urls($post, $limit_post_pages)); } /** * Monthly archive URLs */ if ($config->get_boolean('pgcache.purge.archive.monthly') && $post) { $full_urls = array_merge($full_urls, Util_PageUrls::get_monthly_archive_urls($post, $limit_post_pages)); } /** * Yearly archive URLs */ if ($config->get_boolean('pgcache.purge.archive.yearly') && $post) { $full_urls = array_merge($full_urls, Util_PageUrls::get_yearly_archive_urls($post, $limit_post_pages)); } /** * Feed URLs */ if ($config->get_boolean('pgcache.purge.feed.blog')) { $full_urls = array_merge($full_urls, Util_PageUrls::get_feed_urls($feeds)); } if ($config->get_boolean('pgcache.purge.feed.comments')) { $full_urls = array_merge($full_urls, Util_PageUrls::get_feed_comments_urls($post_id, $feeds)); } if ($config->get_boolean('pgcache.purge.feed.author') && $post) { $full_urls = array_merge($full_urls, Util_PageUrls::get_feed_author_urls($post->post_author, $feeds)); } if ($config->get_boolean('pgcache.purge.feed.terms')) { $full_urls = array_merge($full_urls, Util_PageUrls::get_feed_terms_urls($terms, $feeds)); } /** * Purge selected pages */ if ($config->get_array('pgcache.purge.pages')) { $pages = $config->get_array('pgcache.purge.pages'); $full_urls = array_merge($full_urls, Util_PageUrls::get_pages_urls($pages)); } /** * Queue flush */ if (count($full_urls)) { $o = Dispatcher::component('Cdn_Fsd_CacheFlush'); foreach ($full_urls as $url) { $o->queued_urls[$url] = '*'; } } return true; }
function thematic_post_class($print = true) { global $post, $thematic_post_alt, $thematic_content_length, $taxonomy; // hentry for hAtom compliace, gets 'alt' for every other post DIV, describes the post type and p[n] $c = array('hentry', "p{$thematic_post_alt}", str_replace('_', '-', $post->post_type), $post->post_status); // Author for the post queried $c[] = 'author-' . sanitize_title_with_dashes(strtolower(get_the_author_meta('user_login'))); // Category for the post queried foreach ((array) get_the_category() as $cat) { $c[] = 'category-' . $cat->slug; } // Tags for the post queried; if not tagged, use .untagged if (get_the_tags() == null) { $c[] = 'untagged'; } else { foreach ((array) get_the_tags() as $tag) { $c[] = 'tag-' . $tag->slug; } } if (function_exists('get_post_type_object')) { // Taxonomies and terms for the post queried $single_post_type = get_post_type_object(get_post_type($post->ID)); // Check for post types without taxonomy inclusion if (isset($single_post_type->taxonomy)) { foreach ((array) get_the_terms($post->ID, get_post_taxonomies()) as $term) { // Remove tags and categories from results if ($term->taxonomy != 'category') { if ($term->taxonomy != 'post_tag') { $c[] = 'p-tax-' . $term->taxonomy; $c[] = 'p-' . $term->taxonomy . '-' . $term->slug; } } } } } // For posts displayed as full content if ($thematic_content_length == 'full') { $c[] = 'is-full'; } // For posts displayed as excerpts if ($thematic_content_length == 'excerpt') { $c[] = 'is-excerpt'; if (has_excerpt() && !preg_match('/<!--more(.*?)?-->/', $post->post_content)) { // For wp-admin Write Page generated excerpts $c[] = 'custom-excerpt'; } else { // For automatically generated excerpts $c[] = 'auto-excerpt'; } } // For single posts that had a wp-admin Write Page generated excerpt if (has_excerpt() && is_single()) { $c[] = 'has-excerpt'; } // For posts using more tag if (preg_match('/<!--more(.*?)?-->/', $post->post_content)) { if (!is_single()) { $c[] = 'wp-teaser'; } elseif (is_single()) { $c[] = 'has-teaser'; } } // For posts with comments open or closed if (comments_open()) { $c[] = 'comments-open'; } else { $c[] = 'comments-closed'; } // For posts with pings open or closed if (pings_open()) { $c[] = 'pings-open'; } else { $c[] = 'pings-closed'; } // For password-protected posts if ($post->post_password) { $c[] = 'protected'; } // For sticky posts if (is_sticky()) { $c[] = 'sticky'; } // Applies the time- and date-based classes (below) to post DIV thematic_date_classes(mysql2date('U', $post->post_date), $c); // If it's the other to the every, then add 'alt' class if (++$thematic_post_alt % 2) { $c[] = 'alt'; } // Adds post slug class, prefixed by 'slug-' $c[] = 'slug-' . $post->post_name; // Separates classes with a single space, collates classes for post DIV $c = join(' ', apply_filters('post_class', $c)); // Available filter: post_class // And tada! return $print ? print $c : $c; }
/** * Determine whether the widget should be displayed based on conditions set by the user. * * @param array $instance The widget settings. * @return array Settings to display or bool false to hide. */ public static function filter_widget($instance) { global $post, $wp_query; if (empty($instance['conditions']) || empty($instance['conditions']['rules'])) { return $instance; } $condition_result = false; foreach ($instance['conditions']['rules'] as $rule) { switch ($rule['major']) { case 'date': switch ($rule['minor']) { case '': $condition_result = is_date(); break; case 'month': $condition_result = is_month(); break; case 'day': $condition_result = is_day(); break; case 'year': $condition_result = is_year(); break; } break; case 'page': // Previously hardcoded post type options. if ('post' == $rule['minor']) { $rule['minor'] = 'post_type-post'; } else { if (!$rule['minor']) { $rule['minor'] = 'post_type-page'; } } switch ($rule['minor']) { case '404': $condition_result = is_404(); break; case 'search': $condition_result = is_search(); break; case 'archive': $condition_result = is_archive(); break; case 'posts': $condition_result = $wp_query->is_posts_page; break; case 'home': $condition_result = is_home(); break; case 'front': if (current_theme_supports('infinite-scroll')) { $condition_result = is_front_page(); } else { $condition_result = is_front_page() && !is_paged(); } break; default: if (substr($rule['minor'], 0, 10) == 'post_type-') { $condition_result = is_singular(substr($rule['minor'], 10)); } else { // $rule['minor'] is a page ID $condition_result = is_page($rule['minor']); } break; } break; case 'tag': if (!$rule['minor'] && is_tag()) { $condition_result = true; } else { if (is_singular() && $rule['minor'] && has_tag($rule['minor'])) { $condition_result = true; } else { $tag = get_tag($rule['minor']); if ($tag && is_tag($tag->slug)) { $condition_result = true; } } } break; case 'category': if (!$rule['minor'] && is_category()) { $condition_result = true; } else { if (is_category($rule['minor'])) { $condition_result = true; } else { if (is_singular() && $rule['minor'] && in_array('category', get_post_taxonomies()) && has_category($rule['minor'])) { $condition_result = true; } } } break; case 'author': if (!$rule['minor'] && is_author()) { $condition_result = true; } else { if ($rule['minor'] && is_author($rule['minor'])) { $condition_result = true; } else { if (is_singular() && $rule['minor'] && $rule['minor'] == $post->post_author) { $condition_result = true; } } } break; case 'taxonomy': $term = explode('_tax_', $rule['minor']); // $term[0] = taxonomy name; $term[1] = term id $terms = get_the_terms($post->ID, $rule['minor']); // Does post have terms in taxonomy? if (is_tax($term[0], $term[1])) { $condition_result = true; } else { if (is_singular() && $term[1] && has_term($term[1], $term[0])) { $condition_result = true; } else { if (is_singular() && $terms & !is_wp_error($terms)) { $condition_result = true; } } } break; } if ($condition_result) { break; } } if ('show' == $instance['conditions']['action'] && !$condition_result || 'hide' == $instance['conditions']['action'] && $condition_result) { return false; } return $instance; }