Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 2b622e7

Browse files
Julian VasaJulian Vasa
authored andcommitted
Fixed some self-hosted wp rest-api issues
1 parent 1b295d8 commit 2b622e7

21 files changed

+186
-101
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ A simple app build with Ionic v4.
1212

1313
change the wordpress.com urls in `src/app/services/wordpress.service.ts`
1414

15+
If you have a self-hosted Wordpress blog it should work fine also with rest-api endpoints of your instance.
16+
https://developer.wordpress.org/rest-api/reference/<br>
17+
If you have a self-hosted wordpress blog change variable wp_org = true<br>
18+
`public wp_org: boolean = true;`<br>
19+
in `src/app/services/wordpress.service.ts`
20+
1521
It looks something like this:
1622

1723
![Screenshot](Screenshot.png)
1824
![Screenshot2](Screenshot2.png)
25+
![Screenshot3](Screenshot3.png)
26+
![Screenshot4](Screenshot4.png)
27+

Screenshot1.png

114 KB
Loading

Screenshot2.png

-158 KB
Loading

Screenshot3.png

183 KB
Loading

Screenshot4.png

10.6 KB
Loading

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platforms/ios/www/tabs-tabs-module.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/services/wordpress.service.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
11
import { Injectable } from '@angular/core';
22
import {HttpClient} from '@angular/common/http';
33
import 'rxjs/add/operator/map';
4-
import {of} from 'rxjs/observable/of';
4+
55
@Injectable()
66
export class WordpressService {
77
items: any[];
88
categories: any[];
9+
public wp_org: boolean = false;
10+
mainUrl: String = "https://public-api.wordpress.com/rest/v1.1/sites/unegatuaj.com/";
11+
912
constructor(private http: HttpClient) {
13+
if(this.wp_org == true) {
14+
this.mainUrl = "http://demo.wp-api.org/wp-json/wp/v2/";
15+
}
1016
}
1117

1218
public getPosts(page: number): any {
13-
return this.http.get("https://public-api.wordpress.com/rest/v1.2/sites/unegatuaj.com/posts/?status=publish&page="+page);
19+
return this.http.get(this.mainUrl +"posts/?status=publish&page="+page);
1420
}
1521

1622
public getPostsByCat(categoryName: string, page: number): any {
17-
return this.http.get("https://public-api.wordpress.com/rest/v1.2/sites/unegatuaj.com/posts/?status=publish&category="+categoryName+"&page="+page);
23+
return this.http.get(this.mainUrl + "posts/?status=publish&category="+categoryName+"&page="+page);
1824
}
1925

2026
public getCategories(): any {
21-
return this.http.get("https://public-api.wordpress.com/rest/v1.1/sites/unegatuaj.com/categories?order_by=count&order=DESC");
27+
if(this.wp_org){
28+
return this.http.get(this.mainUrl + "categories?order_by=count&order=desc");
29+
}
30+
return this.http.get(this.mainUrl + "categories?order_by=count&order=DESC");
2231
}
2332

2433
public search(searchStr: string, page: number): any {
25-
return this.http.get("https://public-api.wordpress.com/rest/v1.2/sites/unegatuaj.com/posts/?status=publish&search="+searchStr+"&page="+page);
34+
return this.http.get(this.mainUrl + "posts/?status=publish&search="+searchStr+"&page="+page);
2635
}
2736

2837
public getPost(recipeId: string): any {
29-
return this.http.get("https://public-api.wordpress.com/rest/v1.1/sites/unegatuaj.com/posts/"+recipeId);
38+
return this.http.get(this.mainUrl + "posts/"+recipeId);
3039
}
3140
}

src/app/tab1/tab1.page.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<ion-header>
22
<ion-toolbar color="warning">
33
<ion-title>
4-
Recetat e fundit
4+
Recent posts
55
</ion-title>
66
</ion-toolbar>
77
</ion-header>
@@ -18,7 +18,7 @@
1818
<div [innerHTML]="thumbs.get(item).content"></div>
1919
</ion-card-content>
2020
</ion-card>
21-
<ion-button *ngIf="loaded === true" (click)="next()">Me shume receta</ion-button>
21+
<ion-button *ngIf="loaded === true" (click)="next()">More posts</ion-button>
2222

2323

2424
</ion-content>

src/app/tab1/tab1.page.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,30 @@ export class Tab1Page implements OnInit {
2525

2626
loadPosts(){
2727
this.loading = true;
28-
this.wordpressService.getPosts(this.page).subscribe(data => {
29-
this.items = data.posts;
30-
for (let res of data.posts) {
31-
if(!this.thumbs.has(res.ID)){
32-
this.thumbs.set(res.ID, {id: res.ID, title: res.title, content: res.content.replace('<li class="jetpack-recipe-print"><a href="#">Print</a></li>','')});
33-
}
34-
}
35-
this.loading = false;
36-
this.loaded = true;
37-
});
28+
if(this.wordpressService.wp_org){
29+
this.wordpressService.getPosts(this.page).subscribe(data => {
30+
this.items = data;
31+
for (let res of data) {
32+
if(!this.thumbs.has(res.id)){
33+
this.thumbs.set(res.id, {id: res.id, title: res.title.rendered, content: res.content.rendered});
34+
}
35+
}
36+
this.loading = false;
37+
this.loaded = true;
38+
});
39+
}
40+
else {
41+
this.wordpressService.getPosts(this.page).subscribe(data => {
42+
this.items = data.posts;
43+
for (let res of data.posts) {
44+
if(!this.thumbs.has(res.ID)){
45+
this.thumbs.set(res.ID, {id: res.ID, title: res.title, content: res.content.replace('<li class="jetpack-recipe-print"><a href="#">Print</a></li>','')});
46+
}
47+
}
48+
this.loading = false;
49+
this.loaded = true;
50+
});
51+
}
3852
}
3953

4054
next() {

0 commit comments

Comments
 (0)