Git Product home page Git Product logo

wp-api-v2-client-java's Introduction

Codeship Status for Afrozaar/wp-api-v2-client-java

WP-API v2 Java Client

A Java client to version 2 of the WP REST API

(Currently coding against WP REST API Version 2.0-beta13.1)

See

Current Development Requirements

  • WordPress 4.4.1+ installation
  • JSON Basic Authentication (0.1 currently used)

Implemented

  • Posts CRUD
  • Post Meta CRUD
  • Terms CRUD
  • Taxonomy CRUD
  • Post Terms CRUD
  • Pages CRUD
  • Users

Work In Progress

...

Not Yet Implemented

  • Post Revisions
  • Post Types
  • Post Statuses
  • Comments

Basic Usage

Instantiating and using a new client

String baseUrl = "http://myhost";
String username = "myUsename";
String password = "myPassword";
boolean debug = false;

final Wordpress client = ClientFactory.fromConfig(ClientConfig.of(baseUrl, username, password, debug));

Creating a new post

final Post post = PostBuilder.aPost()
    .withTitle(TitleBuilder.aTitle().withRendered(expectedTitle).build())
    .withExcerpt(ExcerptBuilder.anExcerpt().withRendered(expectedExcerpt).build())
    .withContent(ContentBuilder.aContent().withRendered(expectedContent).build())
    .build();

final Post createdPost = client.createPost(post, PostStatus.publish);

Searching Posts

Search Posts not having a particular Meta Key

Sample Code

final PagedResponse<Post> response = client.search(SearchRequest.Builder.aSearchRequest(Post.class)
        .withUri(Request.POSTS)
        .withParam("filter[meta_key]", "baobab_indexed")
        .withParam("filter[meta_compare]", "NOT EXISTS") //RestTemplate takes care of escaping values ('space' -> '%20')
        .build());                

Equivalent Curl/httpie Request

$ http --auth 'username:password' http://myhost/wp-json/wp/v2/posts?filter[meta_key]=baobab_indexed&filter[meta_compare]=NOT%20EXISTS

Search types

The client is flexible enough to build search requests of a particular type, if that type supports filtering.

final PagedResponse<Media> tagResults = client.search(SearchRequest.Builder.aSearchRequest(Media.class)
    .withUri("/media")
    .withParam("filter[s]", "searchTerm")
    .build());

Available Filters

More Usage Examples

Advanced/Restricted Filtering

For advanced filtering in a particular use case, it is required to search for posts not having a particular custom field. In order to search for such posts, the standard filter keys are not sufficient, and needs to be enabled by allowing more keys.

Do note that the effect of this change is only visible when an authenticated call is made, as per the WP-API documentation.

A snippet containing the keys that you wish to use needs to be added to either your theme's functions.php file, or WP-API's plugin.php:

function my_allow_meta_query( $valid_vars ) {

        $valid_vars = array_merge( $valid_vars, array( 'meta_key', 'meta_value', 'meta_compare' ) );
        return $valid_vars;
}
add_filter( 'rest_query_vars', 'my_allow_meta_query' );

TODO

  • Add support for authentication providers such as OAuth. (Currently only basic authentication is used)

Testing

Live Testing

These tests are intended to run against a live WordPress installation.

For convenience, a wordpress docker has been created. This docker has a pre-installed-and-set-up wordpress instance, with the latest (beta9) version of rest-api and JSON Basic Auth plugins enabled. Configuration has already been included in the test configuration directory.

To make use of this docker, you can do the following:

docker run -d --name wp_build_test -p 80:80 afrozaar/wordpress:latest

More configuration is required (adding an entry to your hosts file), so see Afrozaar/docker-wordpress on GitHub.

Configuration

To run against your local wordpress installation, it is required to have a YAML configuration file available at: ${project}/src/test/resources/config/${hostname}-test.yaml with the following structure:

wordpress:
  username: "myUsername"
  password: "myPassword"
  baseUrl: "http://myhost"

debug: "true"

This configuration must not be included in version control. *.yaml is already included in the .gitignore file.

Please ensure that you do not commit hard-coded environment information.

Maven

Latest snapshot is available from our public maven repository at

Release versions should also be available on public maven repositories:

<dependency>
  <groupId>com.afrozaar.wordpress</groupId>
  <artifactId>wp-api-v2-client-java</artifactId>
  <version>2.0-beta13</version>
</dependency>

wp-api-v2-client-java's People

Contributors

johanmynhardt avatar freddieod avatar clintonbosch avatar michaelwiles avatar

Watchers

James Cloos avatar Manik Magar avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.