├── package.json ├── phpunit.xml ├── .travis.yml ├── tests ├── bootstrap.php ├── test-update.php └── test-post-response.php ├── Gruntfile.js ├── README.md ├── bin └── install-wp-tests.sh ├── readme.txt └── seo-rest-api-fields.php /package.json: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "name": "seo-rest-api-fields", 4 | "version": "0.1.0", 5 | "main": "Gruntfile.js", 6 | "author": "YOUR NAME HERE", 7 | "devDependencies": { 8 | "grunt": "^0.4.5", 9 | "grunt-wp-i18n": "^0.5.0", 10 | "grunt-wp-readme-to-markdown": "~0.9.0" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | ./tests/ 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | notifications: 4 | email: 5 | on_success: never 6 | on_failure: change 7 | 8 | php: 9 | - 5.3 10 | - 5.5 11 | 12 | env: 13 | - WP_VERSION=latest WP_MULTISITE=0 14 | 15 | matrix: 16 | include: 17 | - php: 5.3 18 | env: WP_VERSION=latest WP_MULTISITE=1 19 | 20 | before_script: 21 | - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION 22 | 23 | script: phpunit 24 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | \n * Copyright (c) <%= grunt.template.today("yyyy") %>\n * This file is generated automatically. Do not edit.\n */\n'; 5 | // Project configuration 6 | grunt.initConfig( { 7 | 8 | pkg: grunt.file.readJSON( 'package.json' ), 9 | 10 | addtextdomain: { 11 | options: { 12 | textdomain: 'seo-rest-api-fields', 13 | }, 14 | target: { 15 | files: { 16 | src: [ '*.php', '**/*.php', '!node_modules/**', '!php-tests/**', '!bin/**' ] 17 | } 18 | } 19 | }, 20 | 21 | wp_readme_to_markdown: { 22 | your_target: { 23 | files: { 24 | 'README.md': 'readme.txt' 25 | } 26 | }, 27 | }, 28 | 29 | makepot: { 30 | target: { 31 | options: { 32 | domainPath: '/languages', 33 | mainFile: 'seo-rest-api-fields.php', 34 | potFilename: 'seo-rest-api-fields.pot', 35 | potHeaders: { 36 | poedit: true, 37 | 'x-poedit-keywordslist': true 38 | }, 39 | type: 'wp-plugin', 40 | updateTimestamp: true 41 | } 42 | } 43 | }, 44 | } ); 45 | 46 | grunt.loadNpmTasks( 'grunt-wp-i18n' ); 47 | grunt.loadNpmTasks( 'grunt-wp-readme-to-markdown' ); 48 | grunt.registerTask( 'i18n', ['addtextdomain', 'makepot'] ); 49 | grunt.registerTask( 'readme', ['wp_readme_to_markdown']); 50 | 51 | grunt.util.linefeed = '\n'; 52 | 53 | }; 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | REST API SEO Fields 3 | ================== 4 | 5 | Adds SEO Fields from [WordPress SEO by Yoast](https://wordpress.org/plugins/wordpress-seo/) to responses for posts in the [WordPress REST API 2.0-beta3](https://wordpress.org/plugins/rest-api/). Also allows for updating by an authenticated user. 6 | 7 | This plugin is a free plugin by [CalderaWP](https://CalderaWP.com). It is not an official add-on for WordPress SEO and is no way associated with the makers of WordPress SEO. 8 | 9 | * Requires [WordPress REST API (WP-API) 2.0-beta3](https://wordpress.org/plugins/rest-api/) or later. 10 | * Requires [WordPress SEO by Yoast](https://wordpress.org/plugins/wordpress-seo/) 11 | 12 | 13 | ### Other SEO Plugins? 14 | Right now this only works with WordPress SEO by Yoast. It could work with other plugins. Just create a new instance of the main class, and pass the title and description meta fields it uses. 15 | 16 | ``` 17 | if ( defined( 'REST_API_VERSION' ) && version_compare( REST_API_VERSION,'2.0-beta3', '>=' ) ) { 18 | //update these variables!!! 19 | $title_field = 'your_title_field'; 20 | $description_field = 'your_description_field'; 21 | new CWP_REST_API_SEO_Fields( $title_field, $description_field ); 22 | } 23 | 24 | } 25 | ``` 26 | 27 | The above should be performed at the rest_api_init action. 28 | 29 | ### License & Copyright 30 | * Copyright 2015 Josh Pollock for CalderaWP LLC. 31 | 32 | * Licensed under the terms of the [GNU General Public License version 2](http://www.gnu.org/licenses/gpl-2.0.html) or later. 33 | 34 | * Please share with your neighbor. 35 | 36 | -------------------------------------------------------------------------------- /bin/install-wp-tests.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ $# -lt 3 ]; then 4 | echo "usage: $0 [db-host] [wp-version]" 5 | exit 1 6 | fi 7 | 8 | DB_NAME=$1 9 | DB_USER=$2 10 | DB_PASS=$3 11 | DB_HOST=${4-localhost} 12 | WP_VERSION=${5-latest} 13 | 14 | WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} 15 | WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/} 16 | 17 | set -ex 18 | 19 | download() { 20 | if [ `which curl` ]; then 21 | curl -s "$1" > "$2"; 22 | elif [ `which wget` ]; then 23 | wget -nv -O "$2" "$1" 24 | fi 25 | } 26 | 27 | install_wp() { 28 | 29 | if [ -d $WP_CORE_DIR ]; then 30 | return; 31 | fi 32 | 33 | mkdir -p $WP_CORE_DIR 34 | 35 | if [ $WP_VERSION == 'latest' ]; then 36 | local ARCHIVE_NAME='latest' 37 | else 38 | local ARCHIVE_NAME="wordpress-$WP_VERSION" 39 | fi 40 | 41 | download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz 42 | tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR 43 | 44 | download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php 45 | } 46 | 47 | install_test_suite() { 48 | # portable in-place argument for both GNU sed and Mac OSX sed 49 | if [[ $(uname -s) == 'Darwin' ]]; then 50 | local ioption='-i .bak' 51 | else 52 | local ioption='-i' 53 | fi 54 | 55 | # set up testing suite if it doesn't yet exist 56 | if [ ! -d $WP_TESTS_DIR ]; then 57 | # set up testing suite 58 | mkdir -p $WP_TESTS_DIR 59 | svn co --quiet http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/ $WP_TESTS_DIR/includes 60 | fi 61 | 62 | cd $WP_TESTS_DIR 63 | 64 | if [ ! -f wp-tests-config.php ]; then 65 | download https://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php 66 | sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" "$WP_TESTS_DIR"/wp-tests-config.php 67 | sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php 68 | sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php 69 | sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php 70 | sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php 71 | fi 72 | 73 | } 74 | 75 | install_db() { 76 | # parse DB_HOST for port or socket references 77 | local PARTS=(${DB_HOST//\:/ }) 78 | local DB_HOSTNAME=${PARTS[0]}; 79 | local DB_SOCK_OR_PORT=${PARTS[1]}; 80 | local EXTRA="" 81 | 82 | if ! [ -z $DB_HOSTNAME ] ; then 83 | if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then 84 | EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" 85 | elif ! [ -z $DB_SOCK_OR_PORT ] ; then 86 | EXTRA=" --socket=$DB_SOCK_OR_PORT" 87 | elif ! [ -z $DB_HOSTNAME ] ; then 88 | EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" 89 | fi 90 | fi 91 | 92 | # create database 93 | mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA 94 | } 95 | 96 | install_wp 97 | install_test_suite 98 | install_db 99 | -------------------------------------------------------------------------------- /tests/test-update.php: -------------------------------------------------------------------------------- 1 | post_id = $this->factory->post->create(); 17 | update_post_meta( $this->post_id, '_yoast_wpseo_title', rand() ); 18 | update_post_meta( $this->post_id, '_yoast_wpseo_metadesc', rand() ); 19 | 20 | $this->editor_id = $this->factory->user->create( array( 21 | 'role' => 'editor', 22 | ) ); 23 | 24 | 25 | /** @var WP_REST_Server $wp_rest_server */ 26 | global $wp_rest_server; 27 | $this->server = $wp_rest_server = new WP_REST_Server; 28 | do_action( 'rest_api_init' ); 29 | 30 | 31 | } 32 | 33 | /** 34 | * Tidy up after tests 35 | */ 36 | public function tearDown() { 37 | parent::tearDown(); 38 | wp_delete_post( $this->post_id ); 39 | 40 | /** @var WP_REST_Server $wp_rest_server */ 41 | global $wp_rest_server; 42 | $wp_rest_server = null; 43 | } 44 | 45 | /** 46 | * Test we can update title 47 | */ 48 | public function test_update_title() { 49 | wp_set_current_user( $this->editor_id ); 50 | 51 | $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); 52 | $request->set_body_params( array( 53 | '_yoast_wpseo_title' => 'one two three', 54 | ) ); 55 | $response = $this->server->dispatch( $request ); 56 | $this->assertNotInstanceOf( 'WP_Error', $response ); 57 | $response = rest_ensure_response( $response ); 58 | $this->assertEquals( 200, $response->get_status() ); 59 | 60 | $this->assertEquals( 'one two three', get_post_meta( $this->post_id, '_yoast_wpseo_title', true ) ); 61 | } 62 | 63 | /** 64 | * Test we can update description 65 | */ 66 | public function test_update_description() { 67 | wp_set_current_user( $this->editor_id ); 68 | 69 | $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); 70 | $request->set_body_params( array( 71 | '_yoast_wpseo_metadesc' => 'uno dos tres', 72 | ) ); 73 | 74 | $response = $this->server->dispatch( $request ); 75 | $this->assertNotInstanceOf( 'WP_Error', $response ); 76 | $response = rest_ensure_response( $response ); 77 | $this->assertEquals( 200, $response->get_status() ); 78 | 79 | $this->assertEquals( 'uno dos tres', get_post_meta( $this->post_id, '_yoast_wpseo_metadesc', true ) ); 80 | } 81 | 82 | /** 83 | * Test we can update both description and title 84 | */ 85 | public function test_update_both() { 86 | wp_set_current_user( $this->editor_id ); 87 | 88 | $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); 89 | $request->set_body_params( array( 90 | '_yoast_wpseo_title' => '1 2 3', 91 | '_yoast_wpseo_metadesc' => '4 5 6', 92 | ) ); 93 | 94 | $response = $this->server->dispatch( $request ); 95 | $this->assertNotInstanceOf( 'WP_Error', $response ); 96 | $response = rest_ensure_response( $response ); 97 | $this->assertEquals( 200, $response->get_status() ); 98 | 99 | $this->assertEquals( '1 2 3', get_post_meta( $this->post_id, '_yoast_wpseo_title', true ) ); 100 | 101 | $this->assertEquals( '4 5 6', get_post_meta( $this->post_id, '_yoast_wpseo_metadesc', true ) ); 102 | 103 | } 104 | 105 | } 106 | 107 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === REST API SEO Fields === 2 | Contributors: Shelob9 3 | Donate link: http://example.com/ 4 | Tags: comments, spam 5 | Requires at least: 4.4 6 | Tested up to: 4.4 7 | Stable tag: 0.1.0 8 | License: GPLv2 or later 9 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 10 | 11 | Get or update SEO fields via the WordPress REST API. 12 | 13 | == Description == 14 | 15 | Adds SEO Fields from [WordPress SEO by Yoast](https://wordpress.org/plugins/wordpress-seo/) to responses for posts in the [WordPress REST API 2.0-beta3](https://wordpress.org/plugins/rest-api/). Also allows for updating by an authenticated user. 16 | 17 | This plugin is a free plugin by [CalderaWP](https://CalderaWP.com). It is not an official add-on for WordPress SEO and is no way associated with the makers of WordPress SEO. 18 | 19 | * Requires [WordPress REST API (WP-API) 2.0-beta3](https://wordpress.org/plugins/rest-api/) or later. 20 | * Requires [WordPress SEO by Yoast](https://wordpress.org/plugins/wordpress-seo/) 21 | 22 | This plugin is available on Github if you wish to contribute, or report bugs: [Github Readme](https://github.com/CalderaWP/seo-rest-api-fields/). 23 | 24 | == Installation == 25 | 26 | * Install WordPress SEO by Yoast, and the REST API plugin v2. 27 | * Install this plugin. 28 | * Activate this plugin. 29 | 30 | == Frequently Asked Questions == 31 | = Does It Work With Version 1 of The API? = 32 | 33 | No it does not. 34 | 35 | = Does It Work With Other SEO Plugins? = 36 | 37 | Out of the box? No, it does not. 38 | 39 | It could though. Create a new instance of the class with your own field values. See the [Github Readme](https://github.com/CalderaWP/seo-rest-api-fields/) for an example. 40 | 41 | == Screenshots == 42 | 43 | 1. This screen shot description corresponds to screenshot-1.(png|jpg|jpeg|gif). Note that the screenshot is taken from 44 | the /assets directory or the directory that contains the stable readme.txt (tags or trunk). Screenshots in the /assets 45 | directory take precedence. For example, `/assets/screenshot-1.png` would win over `/tags/4.3/screenshot-1.png` 46 | (or jpg, jpeg, gif). 47 | 2. This is the second screen shot 48 | 49 | == Changelog == 50 | 51 | = 1.0 = 52 | * A change since the previous version. 53 | * Another change. 54 | 55 | = 0.5 = 56 | * List versions from most recent at top to oldest at bottom. 57 | 58 | == Upgrade Notice == 59 | 60 | = 1.0 = 61 | Upgrade notices describe the reason a user should upgrade. No more than 300 characters. 62 | 63 | = 0.5 = 64 | This version fixes a security related bug. Upgrade immediately. 65 | 66 | == Arbitrary section == 67 | 68 | You may provide arbitrary sections, in the same format as the ones above. This may be of use for extremely complicated 69 | plugins where more information needs to be conveyed that doesn't fit into the categories of "description" or 70 | "installation." Arbitrary sections will be shown below the built-in sections outlined above. 71 | 72 | == A brief Markdown Example == 73 | 74 | Ordered list: 75 | 76 | 1. Some feature 77 | 1. Another feature 78 | 1. Something else about the plugin 79 | 80 | Unordered list: 81 | 82 | * something 83 | * something else 84 | * third thing 85 | 86 | Here's a link to [WordPress](http://wordpress.org/ "Your favorite software") and one to [Markdown's Syntax Documentation][markdown syntax]. 87 | Titles are optional, naturally. 88 | 89 | [markdown syntax]: http://daringfireball.net/projects/markdown/syntax 90 | "Markdown is what the parser uses to process much of the readme file" 91 | 92 | Markdown uses email style notation for blockquotes and I've been told: 93 | > Asterisks for *emphasis*. Double it up for **strong**. 94 | 95 | `` 96 | -------------------------------------------------------------------------------- /tests/test-post-response.php: -------------------------------------------------------------------------------- 1 | server = $wp_rest_server = new WP_REST_Server; 19 | do_action( 'rest_api_init' ); 20 | $this->post_id = $this->factory->post->create(); 21 | update_post_meta( $this->post_id, '_yoast_wpseo_title', 'title' ); 22 | update_post_meta( $this->post_id, '_yoast_wpseo_metadesc', 'description' ); 23 | } 24 | 25 | /** 26 | * Tidy up after tests 27 | */ 28 | public function tearDown() { 29 | parent::tearDown(); 30 | wp_delete_post( $this->post_id ); 31 | 32 | /** @var WP_REST_Server $wp_rest_server */ 33 | global $wp_rest_server; 34 | $wp_rest_server = null; 35 | } 36 | 37 | /** 38 | * Make sure we did not break posts query 39 | */ 40 | public function test_posts_response_still_works() { 41 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 42 | $response = $this->server->dispatch( $request ); 43 | $this->assertNotInstanceOf( 'WP_Error', $response ); 44 | $response = rest_ensure_response( $response ); 45 | $this->assertEquals( 200, $response->get_status() ); 46 | } 47 | 48 | /** 49 | * Make sure we did not break single post query 50 | */ 51 | public function test_post_response_still_works() { 52 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); 53 | $response = $this->server->dispatch( $request ); 54 | $this->assertNotInstanceOf( 'WP_Error', $response ); 55 | $response = rest_ensure_response( $response ); 56 | $this->assertEquals( 200, $response->get_status() ); 57 | } 58 | 59 | /** 60 | * Test that we have the title field in the response. 61 | */ 62 | public function test_has_title_field() { 63 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); 64 | $response = $this->server->dispatch( $request ); 65 | $response = rest_ensure_response( $response ); 66 | $data = $response->get_data(); 67 | $this->assertArrayHasKey( '_yoast_wpseo_title', $data ); 68 | 69 | } 70 | 71 | /** 72 | * Test that the title field in the response is correct. 73 | */ 74 | public function test_title_response() { 75 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); 76 | $response = $this->server->dispatch( $request ); 77 | $response = rest_ensure_response( $response ); 78 | $data = $response->get_data(); 79 | $this->assertEquals( $this->post_id, $data['id'] ); 80 | $this->assertEquals( get_post_meta( $this->post_id, '_yoast_wpseo_title', true ), $data['_yoast_wpseo_title'][0] ); 81 | } 82 | 83 | /** 84 | * Test that we have the description field in the response. 85 | */ 86 | public function test_has_description_field() { 87 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); 88 | $response = $this->server->dispatch( $request ); 89 | $response = rest_ensure_response( $response ); 90 | $data = $response->get_data(); 91 | $this->assertArrayHasKey( '_yoast_wpseo_title', $data ); 92 | 93 | } 94 | 95 | /** 96 | * Test that the description field in the response is correct. 97 | */ 98 | public function test_description_response() { 99 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); 100 | $response = $this->server->dispatch( $request ); 101 | $response = rest_ensure_response( $response ); 102 | $post = get_post( $this->post_id ); 103 | $data = $response->get_data(); 104 | $this->assertEquals( $this->post_id, $data['id'] ); 105 | $this->assertEquals( get_post_meta( $this->post_id, '_yoast_wpseo_metadesc', true ), $data['_yoast_wpseo_metadesc'][0] ); 106 | 107 | } 108 | } 109 | 110 | -------------------------------------------------------------------------------- /seo-rest-api-fields.php: -------------------------------------------------------------------------------- 1 | =' ) ) { 19 | if ( defined( 'WPSEO_FILE' ) ) { 20 | $title_field = '_yoast_wpseo_title'; 21 | $description_field = '_yoast_wpseo_metadesc'; 22 | new CWP_REST_API_SEO_Fields( $title_field, $description_field ); 23 | } 24 | 25 | } 26 | 27 | } 28 | 29 | class CWP_REST_API_SEO_Fields { 30 | 31 | /** 32 | * The meta key for SEO title 33 | * 34 | * @since 0.1.0 35 | * 36 | * @access protected 37 | * 38 | * @var string 39 | */ 40 | protected $title_field; 41 | 42 | /** 43 | * The meta key for SEO description 44 | * 45 | * @since 0.1.0 46 | * 47 | * @access protected 48 | * 49 | * @var string 50 | */ 51 | protected $description_field; 52 | 53 | /** 54 | * Post types to register fields for. 55 | * 56 | * @since 0.1.0 57 | * 58 | * @access protected 59 | * 60 | * @var array 61 | */ 62 | protected $post_types; 63 | 64 | /** 65 | * Constructor for class. 66 | * 67 | * @since 0.0.1 68 | * 69 | * @param string $title_field Name of meta field for SEO title. 70 | * @param string $description_field Name of meta field for SEO description. 71 | * @param string|array $post_types Optional. Post type(s) to allow. Default is post. 72 | */ 73 | public function __construct( $title_field, $description_field, $post_types = 'post' ) { 74 | $this->set_title_field( $title_field ); 75 | $this->set_description_field( $description_field ); 76 | $this->set_post_types( $post_types ); 77 | add_filter( 'is_protected_meta', array( $this, 'make_fields_public' ), 10, 2 ); 78 | $this->register_fields(); 79 | } 80 | 81 | /** 82 | * Make fields public when using REST API 83 | * 84 | * @since 0.1.0 85 | * 86 | * @uses "is_protected_meta" filter 87 | * @param bool $protected 88 | * @param string $meta_key 89 | * 90 | * @return bool 91 | */ 92 | public function make_fields_public( $protected, $meta_key ) { 93 | if ( $this->title_field == $meta_key || $this->description_field == $meta_key && defined( 'REST_REQUEST' ) && REST_REQUEST ) { 94 | $protected = false; 95 | } 96 | 97 | return $protected; 98 | } 99 | 100 | /** 101 | * Register the fields 102 | * 103 | * @since 0.1.0 104 | * 105 | * @access protected 106 | */ 107 | protected function register_fields() { 108 | register_api_field( $this->post_types, 109 | $this->title_field, 110 | array( 111 | 'get_callback' => array( $this, 'get_post_meta_cb' ), 112 | 'update_callback' => array( $this,'update_post_meta_cb' ), 113 | 'schema' => null, 114 | ) 115 | ); 116 | 117 | register_api_field( $this->post_types, 118 | $this->description_field, 119 | array( 120 | 'get_callback' => array( $this, 'get_post_meta_cb' ), 121 | 'update_callback' => array( $this,'update_post_meta_cb' ), 122 | 'schema' => null, 123 | ) 124 | ); 125 | } 126 | 127 | /** 128 | * Handler for getting custom field data. 129 | * 130 | * @since 0.1.0 131 | * 132 | * @param array $object The object from the response 133 | * @param string $field_name Name of field 134 | * @param WP_REST_Request $request Current request 135 | * 136 | * @return mixed 137 | */ 138 | public function get_post_meta_cb( $object, $field_name, $request ) { 139 | return get_post_meta( $object[ 'id' ], $field_name ); 140 | } 141 | 142 | /** 143 | * Handler for updating custom field data. 144 | * 145 | * @since 0.1.0 146 | * 147 | * @param object $object The object from the response 148 | * @param string $field_name Name of field 149 | * 150 | * @return bool|int 151 | */ 152 | public function update_post_meta_cb( $value, $object, $field_name ) { 153 | return update_post_meta( $object->ID, $field_name, $value ); 154 | } 155 | 156 | /** 157 | * Set title_field property 158 | * 159 | * @since 0.1.0 160 | * 161 | * @access private 162 | * 163 | * @param string $title_field 164 | */ 165 | private function set_title_field( $title_field ) { 166 | $this->title_field = $title_field; 167 | } 168 | 169 | /** 170 | * Set description_field property 171 | * 172 | * @since 0.1.0 173 | * 174 | * @access private 175 | * 176 | * @param string $description_field 177 | */ 178 | private function set_description_field( $description_field ) { 179 | $this->description_field = $description_field; 180 | } 181 | 182 | /** 183 | * Sets post_types propery and ensures is is an array. 184 | * 185 | * @since 0.1.0 186 | * 187 | * @access private 188 | * 189 | * @param string|array $post_types 190 | */ 191 | private function set_post_types( $post_types ) { 192 | if ( is_string( $post_types ) ) { 193 | $post_types = array( $post_types ); 194 | } 195 | 196 | $this->post_types = $post_types; 197 | } 198 | 199 | } 200 | --------------------------------------------------------------------------------