Exemplo n.º 1
2
 /**
  * @override
  */
 public function register_fields()
 {
     parent::register_fields();
     // Custom fields
     register_rest_field($this->_postTypeName, 'xp', array('get_callback' => array($this, 'get_xp')));
     register_rest_field($this->_postTypeName, 'perk_points', array('get_callback' => array($this, 'get_perk_points')));
     register_rest_field($this->_postTypeName, 'energy', array('get_callback' => array($this, 'get_energy_points')));
     register_rest_field($this->_postTypeName, 'starting_skill', array('update_callback' => array($this, 'initialize_character')));
 }
Exemplo n.º 2
1
function ohsupdates_register_api_hooks()
{
    /**
     * Add Fields to Updates API
     */
    register_rest_field('updates', 'media', array('get_callback' => 'updates_register_media', 'update_callback' => null, 'schema' => null));
}
<?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
        }));
    }
});
Exemplo n.º 4
0
function wp_listings_register_listing_meta()
{
    $allowed_meta_keys = allowed_meta_keys();
    foreach ($allowed_meta_keys as $listing_meta_key) {
        register_rest_field('listing', $listing_meta_key, array('get_callback' => 'wp_listings_get_listing_meta', 'update_callback' => 'wp_listings_update_listing_meta', 'schema' => null));
    }
}
Exemplo n.º 5
0
 /**
  * Register all custom fields to the API
  */
 public function register_fields()
 {
     $updateCallback = null;
     // TODO: Private fields
     foreach ($this->_fields as $field_name => $field) {
         $getCallback = array($this, 'get_field');
         $updateCallback = array($this, 'update_field');
         if (array_key_exists('select', $field)) {
             $getCallback = array($this, 'get_select_field');
         }
         if (array_key_exists('override', $field)) {
             $getCallback = array($this, 'get_repeater_field');
         }
         register_rest_field($this->_postTypeName, $field_name, array('get_callback' => $getCallback, 'update_callback' => $updateCallback));
     }
 }
/**
 * Register our enhanced better_featured_image field to all public post types
 * that support post thumbnails.
 *
 * @since  1.0.0
 */
function better_rest_api_featured_images_init()
{
    $post_types = get_post_types(array('public' => true), 'objects');
    foreach ($post_types as $post_type) {
        $post_type_name = $post_type->name;
        $show_in_rest = isset($post_type->show_in_rest) && $post_type->show_in_rest ? true : false;
        $supports_thumbnail = post_type_supports($post_type_name, 'thumbnail');
        // Only proceed if the post type is set to be accessible over the REST API
        // and supports featured images.
        if ($show_in_rest && $supports_thumbnail) {
            // Compatibility with the REST API v2 beta 9+
            if (function_exists('register_rest_field')) {
                register_rest_field($post_type_name, 'better_featured_image', array('get_callback' => 'better_rest_api_featured_images_get_field', 'schema' => null));
            } elseif (function_exists('register_api_field')) {
                register_rest_field($post_type_name, 'better_featured_image', array('get_callback' => 'better_rest_api_featured_images_get_field', 'schema' => null));
            }
        }
    }
}
Exemplo n.º 7
0
 /**
  * Registers the given fields.
  *
  * @since 1.0.0
  *
  * @param Common\Field\Collection $fields Field collection object.
  *
  * @return void
  */
 public function register_fields(Common\Field\Collection $fields)
 {
     if (!function_exists('register_rest_field')) {
         if (defined('WP_DEBUG') && WP_DEBUG) {
             trigger_error('Function register_rest_field() not available. Cannot register additional fields.');
         }
         return;
     }
     /**
      * Fires right before the fields are registered.
      *
      * @since 1.0.0
      *
      * @param Common\Field\Collection $fields Field collection object.
      */
     do_action('wp_rest_starter.register_fields', $fields);
     foreach ($fields as $resource => $resource_fields) {
         /** @var Common\Field\Field $field */
         foreach ($resource_fields as $field_name => $field) {
             register_rest_field($resource, $field_name, $field->get_definition());
         }
     }
 }
Exemplo n.º 8
0
function ohsapi_register_api_hooks()
{
    $namespace = 'ohs/v1';
    /**
     *	Homepage API
     */
    register_rest_route($namespace, '/home/', array('methods' => 'GET', 'callback' => 'ohsapi_get_home'));
    /**
     * Menu API
     */
    register_rest_route($namespace, '/menu/', array('methods' => 'GET', 'callback' => 'ohsapi_get_menu'));
    /**
     * Options API
     */
    register_rest_route($namespace, '/options/', array('methods' => 'GET', 'callback' => 'ohsapi_get_options'));
    /**
     * Add Fields to Posts API
     */
    register_rest_field('post', 'media', array('get_callback' => 'posts_register_media', 'update_callback' => null, 'schema' => null));
    /** 
     * Add Fields to Pages API
     */
    register_rest_field('page', 'media', array('get_callback' => 'posts_register_media', 'update_callback' => null, 'schema' => null));
}
Exemplo n.º 9
0
 /**
  *
  */
 public static function register_rest_data()
 {
     register_rest_route('btbooking/v1', '/booking/genbookingnumber', array('methods' => 'GET', 'callback' => 'btb_gen_booking_number'));
     register_rest_field('btb_event', 'btb_price', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => NULL, 'schema' => NULL));
     register_rest_field('btb_event', 'btb_price_hint', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => NULL, 'schema' => NULL));
     register_rest_field('btb_time', 'btb_start', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => NULL, 'schema' => NULL));
     register_rest_field('btb_time', 'btb_end', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => NULL, 'schema' => NULL));
     register_rest_field('btb_time', 'btb_price', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => NULL, 'schema' => NULL));
     register_rest_field('btb_time', 'btb_date_only', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => NULL, 'schema' => NULL));
     register_rest_field('btb_time', 'btb_free_slots', array('get_callback' => array('BTBooking', 'get_btb_free_slots'), 'update_callback' => NULL, 'schema' => NULL));
     register_rest_field('btb_time', 'btb_event_id', array('get_callback' => array('BTBooking', 'get_btb_time_event_id'), 'update_callback' => NULL, 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_slots', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_time_id', array('get_callback' => array('BTBooking', 'get_btb_booking_parent'), 'update_callback' => array('BTBooking', 'set_btb_booking_parent'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_event_id', array('get_callback' => array('BTBooking', 'get_btb_booked_event'), 'update_callback' => NULL, 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_booking_time', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_price', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_booking_number', array('get_callback' => array('BTBooking', 'get_btb_booking_number'), 'update_callback' => array('BTBooking', 'set_btb_booking_number'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_title', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_price', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_first_name', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_last_name', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_company', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_address', array('get_callback' => array('BTBooking', 'get_btb_meta_array'), 'update_callback' => array('BTBooking', 'set_btb_meta_array'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_mail', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_phone', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
     register_rest_field('btb_booking', 'btb_notes', array('get_callback' => array('BTBooking', 'get_btb_meta'), 'update_callback' => array('BTBooking', 'set_btb_meta'), 'schema' => NULL));
 }
 /**
  * Register REST API fields.
  */
 public function register_rest_fields()
 {
     register_rest_field('sp_table', 'data', array('get_callback' => 'SP_REST_API::get_post_data', 'update_callback' => 'SP_REST_API::update_post_meta_arrays', 'schema' => array('description' => __('League Table', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
 }
Exemplo n.º 11
0
 /**
  * Register the field for all post types and users.
  */
 public static function register()
 {
     foreach (['post'] as $type) {
         register_rest_field('post' === $type ? get_post_types() : $type, self::FIELD_NAME, ['get_callback' => [__CLASS__, 'get_' . $type . '_fields']]);
     }
 }
 public function test_get_additional_field_registration()
 {
     $schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit'));
     register_rest_field('status', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'), 'update_callback' => array($this, 'additional_field_update_callback')));
     $request = new WP_REST_Request('OPTIONS', '/wp/v2/statuses');
     $response = $this->server->dispatch($request);
     $data = $response->get_data();
     $this->assertArrayHasKey('my_custom_int', $data['schema']['properties']);
     $this->assertEquals($schema, $data['schema']['properties']['my_custom_int']);
     $request = new WP_REST_Request('GET', '/wp/v2/statuses/publish');
     $response = $this->server->dispatch($request);
     $this->assertArrayHasKey('my_custom_int', $response->data);
     global $wp_rest_additional_fields;
     $wp_rest_additional_fields = array();
 }
Exemplo n.º 13
0
 /**
  * Backwards compat shim
  */
 function register_api_field($object_type, $attributes, $args = array())
 {
     wc_deprecated_function('register_api_field', 'WPAPI-2.0', 'register_rest_field');
     register_rest_field($object_type, $attributes, $args);
 }
Exemplo n.º 14
0
 public function register_phase_field()
 {
     if (!function_exists('register_rest_field')) {
         trigger_error('Sellaporter requires the WP REST API', E_USER_ERROR);
     }
     register_rest_field('page', 'sellaporter_phase', array('get_callback' => array($this, 'get_current_phase')));
 }
Exemplo n.º 15
0
function register_fields()
{
    // for geo json
    register_rest_field(EDIBLE_POST_TYPE, 'map_data', array('get_callback' => 'get_geojson', 'update_callback' => null, 'schema' => null));
    // for area type
    register_rest_field(EDIBLE_POST_TYPE, 'area_type', array('get_callback' => 'get_areatype', 'update_callback' => null, 'schema' => null));
    register_rest_field(EDIBLE_POST_TYPE, 'image', array('get_callback' => 'get_postimageurl', 'update_callback' => null, 'schema' => null));
    register_rest_field(EDIBLE_POST_TYPE, 'suggested_uses', array('get_callback' => 'get_suggesteduses', 'update_callback' => null, 'schema' => null));
}
Exemplo n.º 16
0
 public function register()
 {
     $args = array_filter(array('get_callback' => $this->getCallback, 'update_callback' => $this->updateCallback, 'schema' => (array) $this->schema));
     register_rest_field($this->objectType, $this->field, $args);
 }
Exemplo n.º 17
0
 /**
  * Register REST fields.
  */
 public static function register_fields()
 {
     if (!function_exists('register_rest_field')) {
         return;
     }
     register_rest_field('sp_event', 'teams', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Teams', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit', 'embed'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_event', 'main_results', array('get_callback' => 'SP_REST_API::get_post_data', 'schema' => array('description' => __('Main Results', 'sportspress'), 'type' => 'array', 'context' => array('view', 'embed'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_event', 'outcome', array('get_callback' => 'SP_REST_API::get_post_data', 'schema' => array('description' => __('Outcome', 'sportspress'), 'type' => 'array', 'context' => array('view', 'embed'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_event', 'winner', array('get_callback' => 'SP_REST_API::get_post_data', 'schema' => array('description' => __('Winner', 'sportspress'), 'type' => 'integer', 'context' => array('view', 'embed'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_event', 'minutes', array('get_callback' => 'SP_REST_API::get_post_data', 'update_callback' => 'SP_REST_API::update_post_meta', 'schema' => array('description' => __('Full Time', 'sportspress'), 'type' => 'integer', 'context' => array('view', 'edit', 'embed'), 'arg_options' => array('sanitize_callback' => 'absint'))));
     register_rest_field('sp_event', 'players', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Players', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_event', 'offense', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Offense', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_event', 'defense', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Defense', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_event', 'staff', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Staff', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_event', 'results', array('get_callback' => 'SP_REST_API::get_post_data', 'update_callback' => 'SP_REST_API::update_post_meta_arrays', 'schema' => array('description' => __('Results', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_event', 'performance', array('get_callback' => 'SP_REST_API::get_post_data', 'update_callback' => 'SP_REST_API::update_post_meta_arrays_multi', 'schema' => array('description' => __('Box Score', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_team', 'staff', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Staff', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_team', 'tables', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('League Tables', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_team', 'lists', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Player Lists', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_team', 'events', array('get_callback' => 'SP_REST_API::get_post_ids_with_meta', 'schema' => array('description' => __('Events', 'sportspress'), 'type' => 'array', 'context' => array('view'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_team', 'abbreviation', array('get_callback' => 'SP_REST_API::get_post_meta', 'update_callback' => 'SP_REST_API::update_post_meta', 'schema' => array('description' => __('Abbreviation', 'sportspress'), 'type' => 'string', 'context' => array('view', 'edit', 'embed'), 'arg_options' => array('sanitize_callback' => 'sanitize_text_field'))));
     register_rest_field('sp_team', 'url', array('get_callback' => 'SP_REST_API::get_post_meta', 'update_callback' => 'SP_REST_API::update_post_meta', 'schema' => array('description' => __('Site URL', 'sportspress'), 'type' => 'string', 'context' => array('view', 'edit', 'embed'), 'arg_options' => array('sanitize_callback' => 'sanitize_text_field'))));
     register_rest_field('sp_player', 'number', array('get_callback' => 'SP_REST_API::get_post_meta', 'update_callback' => 'SP_REST_API::update_post_meta', 'schema' => array('description' => __('Squad Number', 'sportspress'), 'type' => 'integer', 'context' => array('view', 'edit', 'embed'), 'arg_options' => array('sanitize_callback' => 'absint'))));
     register_rest_field('sp_player', 'teams', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Teams', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_player', 'current_teams', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Current Teams', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_player', 'past_teams', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Past Teams', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_player', 'nationalities', array('get_callback' => 'SP_REST_API::get_post_data', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Nationalities', 'sportspress'), 'type' => 'array', 'context' => array('view', 'embed'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_player', 'metrics', array('get_callback' => 'SP_REST_API::get_post_data', 'update_callback' => 'SP_REST_API::update_post_meta_array', 'schema' => array('description' => __('Metrics', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_player', 'statistics', array('get_callback' => 'SP_REST_API::get_post_data', 'update_callback' => 'SP_REST_API::update_post_meta_arrays_multi', 'schema' => array('description' => __('Statistics', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_staff', 'teams', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Teams', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_staff', 'current_teams', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Current Teams', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_staff', 'past_teams', array('get_callback' => 'SP_REST_API::get_post_meta_recursive', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Past Teams', 'sportspress'), 'type' => 'array', 'context' => array('view', 'edit'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     register_rest_field('sp_staff', 'nationalities', array('get_callback' => 'SP_REST_API::get_post_data', 'update_callback' => 'SP_REST_API::update_post_meta_recursive', 'schema' => array('description' => __('Nationalities', 'sportspress'), 'type' => 'array', 'context' => array('view', 'embed'), 'arg_options' => array('sanitize_callback' => 'rest_sanitize_request_arg'))));
     do_action('sportspress_register_rest_fields');
 }
 /**
  * Register fields and their callbacks for read/write via REST
  *
  * @since  2.5.6
  *
  * @access protected
  *
  * @param string            $field_name Name of fields.
  * @param bool|string|array $read       Allowing reading?
  * @param bool|string|array $write      Allow writing?
  */
 protected function register($field_name, $read, $write)
 {
     $args = array();
     switch ($read) {
         case true === $read:
             $args['get_callback'] = array('PodsRESTHandlers', 'get_handler');
             break;
         case is_callable($read):
             $args['get_callback'] = $read;
             $read = true;
             break;
     }
     switch ($write) {
         case true === $write:
             $args['update_callback'] = array('PodsRESTHandlers', 'write_handler');
             break;
         case is_callable($write):
             $args['update_callback'] = $write;
             $write = true;
             break;
     }
     if ($read || $write) {
         register_rest_field($this->pod->pod, $field_name, $args);
     }
 }
Exemplo n.º 19
0
/**
 * Add the field "meta" to WP API responses for users read and write
 */
function register_meta_fields()
{
    \register_rest_field('post', 'meta', array('get_callback' => __NAMESPACE__ . '\\get_post_meta', 'update_callback' => __NAMESPACE__ . '\\set_post_meta', 'schema' => null));
    \register_rest_field('page', 'meta', array('get_callback' => __NAMESPACE__ . '\\get_post_meta', 'update_callback' => __NAMESPACE__ . '\\set_post_meta', 'schema' => null));
    \register_rest_field('user', 'meta', array('get_callback' => __NAMESPACE__ . '\\get_user_meta', 'update_callback' => __NAMESPACE__ . '\\set_user_meta', 'schema' => null));
    $custom_post_types = \get_post_types(array('public' => true, '_builtin' => false), 'names');
    foreach ($custom_post_types as $type) {
        \register_rest_field($type, 'meta', array('get_callback' => __NAMESPACE__ . '\\get_post_meta', 'update_callback' => __NAMESPACE__ . '\\set_post_meta', 'schema' => null));
    }
}
function slug_register_starship()
{
    register_rest_field('conheca', 'list_categories', array('get_callback' => 'slug_get_categorias', 'update_callback' => null, 'schema' => null));
    register_rest_field('conheca', 'url_featured_media', array('get_callback' => 'slug_get_imagem', 'update_callback' => null, 'schema' => null));
}
Exemplo n.º 21
0
 /**
  * Add Related Posts to the REST API Post response.
  *
  * @since 4.4.0
  *
  * @action rest_api_init
  * @uses register_rest_field, self::rest_get_related_posts
  * @return null
  */
 public function rest_register_related_posts()
 {
     register_rest_field('post', 'jetpack-related-posts', array('get_callback' => array($this, 'rest_get_related_posts'), 'update_callback' => null, 'schema' => null));
 }
 public function register_status_field($server)
 {
     $types = get_post_types(array('public' => true, 'show_in_rest' => true));
     foreach ($types as $type) {
         register_rest_field($type, 'status', array('get_callback' => function ($object, $field, $request) {
             return get_post_status($object['id']);
         }, 'update_callback' => null, 'schema' => array('type' => 'string', 'context' => ['view', 'edit', 'embed'])));
     }
 }
 public function test_additional_field_update_errors()
 {
     $schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit'));
     register_rest_field('tag', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'), 'update_callback' => array($this, 'additional_field_update_callback')));
     wp_set_current_user(self::$administrator);
     $tag_id = $this->factory->tag->create();
     // Check for error on update.
     $request = new WP_REST_Request('POST', sprintf('/wp/v2/tags/%d', $tag_id));
     $request->set_body_params(array('my_custom_int' => 'returnError'));
     $response = $this->server->dispatch($request);
     $this->assertErrorResponse('rest_invalid_param', $response, 400);
     global $wp_rest_additional_fields;
     $wp_rest_additional_fields = array();
 }
 public function add_related_events_to_event($response)
 {
     register_rest_field('event', 'related_events', array('get_callback' => array($this, 'add_related_events'), 'update_callback' => null, 'schema' => null));
 }
Exemplo n.º 25
0
function add_todo_list_field()
{
    register_rest_field('todo-list', 'tasks', array('get_callback' => 'get_todo_list', 'update_callback' => 'update_todo_list', 'schema' => null));
}
Exemplo n.º 26
0
function slug_register_spaceship()
{
    register_rest_field('post', 'starship', array('get_callback' => 'slug_get_spaceship', 'update_callback' => 'slug_update_spaceship', 'schema' => null));
}
Exemplo n.º 27
0
 public function test_additional_field_update_errors()
 {
     $schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit'));
     register_rest_field('user', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'), 'update_callback' => array($this, 'additional_field_update_callback')));
     wp_set_current_user(1);
     if (is_multisite()) {
         $current_user = wp_get_current_user(1);
         update_site_option('site_admins', array($current_user->user_login));
     }
     // Check for error on update.
     $request = new WP_REST_Request('POST', sprintf('/wp/v2/users/%d', self::$user));
     $request->set_body_params(array('my_custom_int' => 'returnError'));
     $response = $this->server->dispatch($request);
     $this->assertErrorResponse('rest_invalid_param', $response, 400);
     global $wp_rest_additional_fields;
     $wp_rest_additional_fields = array();
 }
 public function __construct()
 {
     $this->categories = get_categories();
     register_rest_field('post', 'e_categories', array('get_callback' => array($this, 'ct_get_categories'), 'update_callback' => array($this, 'ct_update_categories'), 'schema' => null));
     register_rest_field('post', 'e_tags', array('get_callback' => array($this, 'ct_get_tags'), 'update_callback' => array($this, 'ct_update_tags'), 'schema' => null));
 }
 public function test_additional_field_update_errors()
 {
     $schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit'));
     register_rest_field('attachment', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'), 'update_callback' => array($this, 'additional_field_update_callback')));
     wp_set_current_user(self::$editor_id);
     $attachment_id = $this->factory->attachment->create_object($this->test_file, 0, array('post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', 'post_author' => self::$editor_id));
     // Check for error on update.
     $request = new WP_REST_Request('POST', sprintf('/wp/v2/media/%d', $attachment_id));
     $request->set_body_params(array('my_custom_int' => 'returnError'));
     $response = $this->server->dispatch($request);
     $this->assertErrorResponse('rest_invalid_param', $response, 400);
     global $wp_rest_additional_fields;
     $wp_rest_additional_fields = array();
 }
Exemplo n.º 30
-1
function waq_register_meta_field()
{
    global $waq_custom_post_types;
    $post_types = array_merge(['post'], $waq_custom_post_types);
    foreach ($post_types as $key) {
        register_rest_field($key, 'waq_meta', array('get_callback' => 'waq_get_post_meta', 'update_callback' => null, 'schema' => null));
    }
}