### File

`projects/app-base-library/src/lib/shared/cms/wordpress.ts`

### Index

###### Properties

- Public api
- Public endpoint
- Public model
- Public ready
- Public settings
- Public site

###### Methods

- Private discover
- Public getMenu
- Public getMenus
- Public getParams
- Public getPost
- Public getPosts
- Public init
- Public initApi
- Private initCustomRoutes
- Public restoreLocal
- Public saveLocal

### Constructor

`constructor()`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:13

### Properties

Public api
`api: `any``
Type : `any`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:11

Public endpoint
`endpoint: `string``
Type : `string`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:9

Public model
`model: `any``
Type : `any`
Default value : `{ menus: {}, params: {}, collections: { post: {}} }`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:8

Public ready
`ready: `boolean``
Type : `boolean`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:13

Public settings
`settings: `any``
Type : `any`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:10

Public site
`site: `any``
Type : `any`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:12

### Methods

Private discover
`discover()`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:171
Returns : `any`

Public getMenu
`getMenu(mid: )`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:128

Parameters :

Name | Optional
mid | no

Returns : `any`

Public getMenus
`getMenus()`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:138
Returns : `any`

Public getParams
`getParams()`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:89
Returns : `any`

Public getPost
`getPost(id: , type: )`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:99

Parameters :

Name | Optional
id | no
type | no

Returns : `any`

Public getPosts
`getPosts()`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:73
Returns : `any`

Public init
`init(options: any)`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:19

Parameters :

Name | Type | Optional
options | `any` | no

Returns : `any`

Public initApi
`initApi(settings: )`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:40

Parameters :

Name | Optional
settings | no

Returns : `any`

Private initCustomRoutes
`initCustomRoutes()`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:64
Returns : `void`

Public restoreLocal
`restoreLocal()`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:167
Returns : `void`

Public saveLocal
`saveLocal()`
Defined in projects/app-base-library/src/lib/shared/cms/wordpress.ts:163
Returns : `void`

    import { Storage } from '../util/storage';
    declare var WPAPI;
    declare var Promise;
    declare var fetch;

    export class Wordpress {

        public model: any = { menus: {}, params: {}, collections: { post: {}} };
        public endpoint: string;
        public settings: any;
        public api: any;
        public site: any;
        public ready: boolean;

        constructor() {

        }

        public init(options: any) {
            const self = this;
            if (typeof window !== 'undefined') {
                window['wp'] = window['wp'] || {};
            }
            return new Promise(function (resolve, reject) {
                self.initApi(options).then(function() {
                    if (options.app_server) {
                        self.getParams().then(() => {
                            self.getMenus().then(() => {
                                self.ready = true;
                                resolve();
                            });
                        });
                    } else {
                        resolve();
                    }
                });
            });
        }

        public initApi(settings) {
            const self = this;
            this.settings = settings;
            this.endpoint = this.settings.protocol + '//' + this.settings.host + '/index.php/wp-json/';
            return new Promise(function(resolve, reject) {
                if (typeof window !== 'undefined' && !window['WPAPI']) {
                    console.error('ERROR: WPAPI is not defined as global');
                    reject();
                }
                if (location && location.origin === self.settings.protocol + '//' + self.settings.host ) {
                    // works only on same domain as wp
                    self.discover().then(function() {
                        self.api = new WPAPI({ endpoint: self.endpoint });
                        self.initCustomRoutes();
                        resolve();
                    });
                } else {
                    self.api = new WPAPI({ endpoint: self.endpoint });
                    self.initCustomRoutes();
                    resolve();
                }
            });
        }

        private initCustomRoutes() {
            // app-server plugin
            this.api.params =  this.api.registerRoute( 'app-server/v1', '/params' );
            this.api.menus =  this.api.registerRoute( 'app-server/v1', '/menus/(?P<id>[a-zA-Z(-]+)' );
            // pods plugin: custom content types
            this.api.apps =  this.api.registerRoute( 'wp/v2', '/apps/(?P<id>\\d+)' );
            this.api.iframes =  this.api.registerRoute( 'wp/v2', '/iframes/(?P<id>\\d+)' );
        }

        public getPosts() {
            const self = this;
            return new Promise(function (resolve, reject) {
                self.api.posts().then(function(posts){
                    const items = [];
                    posts.forEach((item) => {
                        if (item.id) {
                            self.model.collections.post[item.id] = item;
                            items.push(item);
                        }
                    });
                    resolve(items);
                });
            });
        }

        public getParams() {
            const self = this;
            return new Promise(function (resolve, reject) {
                self.api.params().then(function(params){
                    self.model.params = params;
                    resolve(params);
                });
            });
        }

        public getPost(id, type) {
            const self = this;
            if (!this.model.collections[type]) {
                this.model.collections[type] = {};
            }
            return new Promise(function (resolve, reject) {
                function parse(id, type, object) {
                    self.model.collections[type][id] = object;
                    resolve(object);
                }
                if (self.settings.cache === true && self.model.collections[type][id]) {
                    resolve(self.model.collections[type][id]);
                } else {
                    if (type === 'post') {
                        self.api.posts().id(id).then((res) => parse(id, type, res));
                    }
                    if (type === 'page') {
                        self.api.pages().id(id).then((res) => parse(id, type, res));
                    }
                    if (type === 'app') {
                        self.api.apps().id(id).then((res) => parse(id, type, res));
                    }
                    if (type === 'iframe') {
                        self.api.iframes().id(id).then((res) => parse(id, type, res));
                    }
                }
            });
        };

        public getMenu(mid) {
            const self = this;
            return new Promise(function (resolve, reject) {
                self.api.menus().id(mid).then((menu) => {
                    self.model.menus[mid] = menu;
                    resolve(self.model.menus[mid]);
                });
            });
        }

        public getMenus() {
            const self = this;
            return new Promise(function (resolve, reject) {
                const mids = [];
                let i = 0;
                if (!self.model.params.menus.length) {
                    resolve(self.model.menus);
                    return;
                }
                self.model.params.menus.forEach( (menu) => {
                    mids.push(menu.slug);
                    // mids.push(menu.term_id);
                });
                mids.forEach( (mid) => {
                    self.getMenu(mid).then(function(menu){
                        self.model.menus[mid] = menu;
                        i++;
                        if (i === mids.length) {
                            resolve(self.model.menus);
                        }
                    });
                });
            });
        }

        public saveLocal() {
            Storage.saveLocalModel(this.model, this.model.params.baseUrl);
        }

        public restoreLocal() {
            this.model = Storage.restoreLocalModel(this.model.params.baseUrl);
        }

        private discover() {
            const self = this;
            const apiPromise = WPAPI.discover( this.settings.protocol + '//' + this.settings.host );
            return new Promise(function(resolve, reject) {
                apiPromise.then(function( site ) {
                    self.site = site;
                    resolve();
                });
            });
        }

    }

# results matching ""

# No results matching ""