Skip to content

Commit e95a85c

Browse files
committed
Added functionality to get custom post types and terms and ability to get post medias with a VIEW context
1 parent aa6dc53 commit e95a85c

6 files changed

Lines changed: 44 additions & 13 deletions

File tree

pom.xml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
<groupId>com.afrozaar.wordpress</groupId>
55
<artifactId>wp-api-v2-client-java</artifactId>
6-
<version>4.8.5</version>
6+
<version>4.9.0-SNAPSHOT</version>
77

88
<packaging>jar</packaging>
99

10-
1110
<name>${project.groupId}:${project.artifactId}</name>
1211
<description>A Java client implementation to the WordPress WP-API v2 plugin.</description>
1312
<url>https://github.com/afrozaar/wp-api-v2-client-java</url>
@@ -34,7 +33,6 @@
3433
</developer>
3534
<developer>
3635
<name>Freddie O'Donnell</name>
37-
<email></email>
3836
<organization>Afrozaar</organization>
3937
<organizationUrl>https://afrozaar.com</organizationUrl>
4038
</developer>
@@ -110,7 +108,7 @@
110108
<plugin>
111109
<groupId>org.apache.maven.plugins</groupId>
112110
<artifactId>maven-gpg-plugin</artifactId>
113-
<version>1.5</version>
111+
<version>1.6</version>
114112
<configuration>
115113
<useAgent>true</useAgent>
116114
</configuration>
@@ -245,12 +243,6 @@
245243
<artifactId>commons-beanutils-core</artifactId>
246244
<version>1.8.3</version>
247245
</dependency>
248-
<dependency>
249-
<groupId>org.codehaus.jackson</groupId>
250-
<artifactId>jackson-mapper-asl</artifactId>
251-
<version>1.9.10</version>
252-
<scope>test</scope>
253-
</dependency>
254246
<dependency>
255247
<groupId>com.google.code.findbugs</groupId>
256248
<artifactId>jsr305</artifactId>

src/main/java/com/afrozaar/wordpress/wpapi/v2/Client.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,17 @@ Request.MEDIAS, HttpMethod.GET, String.class, forExpand(),
277277
return Arrays.asList(medias);
278278
}
279279

280+
@Override
281+
public List<Media> getPostMedias(Long postId, @Nullable String context) {
282+
Media[] medias = CustomRenderableParser.parse(
283+
doExchange1(
284+
Request.MEDIAS, HttpMethod.GET, String.class, forExpand(),
285+
ImmutableMap.of("parent", postId, CONTEXT_, context), null
286+
).getBody(),
287+
Media[].class);
288+
return Arrays.asList(medias);
289+
}
290+
280291
@Override
281292
public List<Media> getMedia() {
282293
List<Media> collected = new ArrayList<>();
@@ -441,6 +452,23 @@ public boolean deletePostMeta(Long postId, Long metaId, Boolean force) {
441452
}
442453
}
443454

455+
@Override
456+
public boolean deleteCustomPostMeta(Long postId, Long metaId, Boolean force, String customPostTypeName) {
457+
if (supportsMetaDeleteViaPostMethod.apply(postId, metaId)) {
458+
// deleting meta via meta POST is available, so use that to delete.
459+
final ResponseEntity<Map> result = doExchange1(Request.CUSTOM_META_POST_DELETE, HttpMethod.POST, Map.class, forExpand(customPostTypeName, postId, metaId),
460+
isNull(force) ? null : ImmutableMap.of(FORCE, force), null);
461+
return result.getStatusCode().is2xxSuccessful() && "Deleted meta".equals(result.getBody().get("message"));
462+
} else {
463+
// attempt normal delete
464+
final ResponseEntity<Map> exchange = doExchange1(Request.CUSTOM_POST_META, HttpMethod.DELETE, Map.class, forExpand(customPostTypeName, postId, metaId),
465+
isNull(force) ? null : ImmutableMap.of(FORCE, force), null);
466+
Preconditions.checkArgument(exchange.getStatusCode().is2xxSuccessful(), format("Expected success on post meta delete request: /posts/%s/meta/%s", postId, metaId));
467+
468+
return exchange.getStatusCode().is2xxSuccessful();
469+
}
470+
}
471+
444472
@SuppressWarnings("unchecked")
445473
@Override
446474
public List<Taxonomy> getTaxonomies() {
@@ -646,7 +674,8 @@ public List<Term> getCategories() {
646674
return getAllTermsForEndpoint(Request.CATEGORIES);
647675
}
648676

649-
private List<Term> getAllTermsForEndpoint(final String endpoint, String... expandParams) {
677+
@Override
678+
public List<Term> getAllTermsForEndpoint(final String endpoint, String... expandParams) {
650679
List<Term> collected = new ArrayList<>();
651680
PagedResponse<Term> pagedResponse = this.getPagedResponse(endpoint, Term.class, expandParams);
652681
collected.addAll(pagedResponse.getList());
@@ -962,10 +991,12 @@ protected Map<String, Object> fieldsFrom(Post post) {
962991
}
963992

964993
private <T, B> ResponseEntity<T> doExchange0(HttpMethod method, URI uri, Class<T> typeRef, B body, @Nullable MediaType mediaType) {
965-
final Tuple2<String, String> authTuple = AuthUtil.authTuple(username, password);
966994
final RequestEntity.BodyBuilder builder = RequestEntity.method(method, uri)
967-
.header(authTuple.v1, authTuple.v2)
968995
.header(userAgentTuple.v1, userAgentTuple.v2);
996+
if (username != null && password != null) {
997+
final Tuple2<String, String> authTuple = AuthUtil.authTuple(username, password);
998+
builder.header(authTuple.v1, authTuple.v2);
999+
}
9691000

9701001
ofNullable(mediaType).ifPresent(builder::contentType);
9711002

src/main/java/com/afrozaar/wordpress/wpapi/v2/api/Categories.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public interface Categories {
99

1010
List<Term> getCategories();
1111

12+
List<Term> getAllTermsForEndpoint(final String endpoint, String... expandParams);
13+
1214
Term getCategory(Long id);
1315

1416
Term createCategory(Term categoryTerm);

src/main/java/com/afrozaar/wordpress/wpapi/v2/api/Medias.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ public interface Medias {
3434
Post setPostFeaturedMedia(Long postId, Media media);
3535

3636
List<Media> getPostMedias(Long postId);
37+
38+
List<Media> getPostMedias(Long postId, @Nullable String context);
39+
3740
}

src/main/java/com/afrozaar/wordpress/wpapi/v2/api/PostMetas.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ public interface PostMetas {
3030
boolean deletePostMeta(Long postId, Long metaId);
3131

3232
boolean deletePostMeta(Long postId, Long metaId, Boolean force);
33+
34+
boolean deleteCustomPostMeta(Long postId, Long metaId, Boolean force, String customPostTypeName);
3335
}

src/main/java/com/afrozaar/wordpress/wpapi/v2/request/Request.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public abstract class Request {
2626
public static final String CUSTOM_POST_METAS = "/{postType}/{postId}/meta";
2727
public static final String CUSTOM_POST_META = "/{postType}/{postId}/meta/{metaId}";
2828
public static final String META_POST_DELETE = "/posts/{postId}/meta/{metaId}/delete";
29+
public static final String CUSTOM_META_POST_DELETE = "/{postType}/{postId}/meta/{metaId}/delete";
2930
public static final String TAXONOMIES = "/taxonomies";
3031
public static final String TAXONOMY = "/taxonomies/{slug}";
3132
public static final String TERMS = "/terms/{taxonomySlug}";

0 commit comments

Comments
 (0)