<?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;
}
/**
* 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);
}
/**
* 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');
}
}
}
}
请发表评论