@newfold/wp-module-runtime 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,205 @@
1
+ <a href="https://newfold.com/" target="_blank">
2
+ <img src="https://newfold.com/content/experience-fragments/newfold/site-header/master/_jcr_content/root/header/logo.coreimg.svg/1621395071423/newfold-digital.svg" alt="Newfold Logo" title="Newfold Digital" align="right"
3
+ height="42" />
4
+ </a>
5
+
6
+ # WordPress Runtime Module
7
+
8
+ Runtime for Newfold WP modules and plugins
9
+
10
+ ## Module Responsibilities
11
+
12
+ * `prepareRuntime` method in `Runtime` class accepts `container` object as parameter and generates a PHP object which contains:
13
+
14
+ siteUrl,
15
+ siteTitle,
16
+ adminUrl,
17
+ homeUrl,
18
+ capabilties,
19
+ Woocommerce plugin active/inactive status,
20
+ YithBooking plugins active/inactive status(yith-woocommerce-booking-extended / yith-woocommerce-booking-premium / yith-woocommerce-booking),
21
+ JetpackBoost plugin active/inactive status,
22
+ WordPress version,
23
+ currentTheme.
24
+
25
+ * Above PHP object is then converted into a JSON-encoded string and assigned `NewfoldRuntime` JS variable.
26
+ * `NewfoldRuntime` JSON object can be imported in any React components in `newfold-labs` modules as specified in `Usage` section below.
27
+ * Structure of `NewfoldRuntime` JSON object consumed by React components is as below.
28
+ * Please note, `redundant values` are kept for backward compatibility and will be removed soon, as part `tech-debt` initiative.
29
+
30
+ ```
31
+ {
32
+ "site": {
33
+ "url": "Your wordpress site url",
34
+ "title": "Your wordpress site name"
35
+ },
36
+ "adminUrl": "Your wordpress site wp-admin url",
37
+ "base_url": "Your wordpress site index.php url",
38
+ "homeUrl": "Your wordpress site url",
39
+ "capabilities": {
40
+ "canAccessAI": boolean,
41
+ "canAccessGlobalCTB": boolean,
42
+ "canAccessHelpCenter": boolean,
43
+ "hasEcomdash": boolean,
44
+ "hasYithExtended": boolean,
45
+ "isEcommerce": boolean,
46
+ "isJarvis": boolean
47
+ },
48
+ "sdk": {
49
+ "wpversion": "Current WordPress version",
50
+ "plugin": {
51
+ "url": "Brand plugin build url",
52
+ "version": "Brand plugin version",
53
+ "assets": "Brand plugin assets url",
54
+ "brand": "Current WordPress hosting brand name"
55
+ },
56
+ "ecommerce": {
57
+ "brand_settings": {
58
+ "brand": "Hosting brand value",
59
+ "name": "Hosting brand name",
60
+ "url": "Hosting brand page url",
61
+ "hireExpertsInfo": "Marketplace service purchase url",
62
+ "support": "Hosting brand support contact url",
63
+ "adminPage": "Plugin Dashboard Homepage url",
64
+ "setup": {
65
+ "payment": [
66
+ Payment options supported for eg: "Paypal", "Razorpay", "Stripe"
67
+ ],
68
+ "shipping": [
69
+ Shipping options supporter for eg: "Shippo"
70
+ ]
71
+ },
72
+ "defaultContact": {
73
+ "woocommerce_default_country": "Default country code",
74
+ "woocommerce_currency": "Default currency code"
75
+ },
76
+ "wondercartBuyNow": "Marketplace link to purchase WonderCart"
77
+ },
78
+ "nonces": {
79
+ "gateway_toggle": ""gateway_toggle_code"
80
+ },
81
+ "install_token": "NFD_INSTALLER_token"
82
+ }
83
+ },
84
+ "siteUrl": "Your wordpress site url",
85
+ "siteTitle": "Your wordpress site name",
86
+ "restUrl": "Your wordpress site /wp-json/",
87
+ "restNonce": "restNonce_value",
88
+ "isWoocommerceActive": boolean,
89
+ "isYithBookingActive": boolean,
90
+ "isJetpackBoostActive": boolean,
91
+ "wpVersion": "Current WordPress version",
92
+ "currentTheme": "Current Theme Name",
93
+ "context": {
94
+ "platform": "default",
95
+ "brand": {
96
+ "name": "Hosting brand name"
97
+ }
98
+ },
99
+ "plugin": {
100
+ "url": "Brand plugin build url",
101
+ "version": "Brand plugin version",
102
+ "assets": "Brand plugin assets url",
103
+ "brand": "Current WordPress hosting brand name"
104
+ },
105
+ "comingSoon": {
106
+ enable: {
107
+ success: boolean
108
+ }
109
+ isEnabled: boolean,
110
+ disabled: {
111
+ success: boolean
112
+ }
113
+ lastChanged: timeStamp when coming soon setting was updated last time
114
+ toggleAdminBarSiteStatus: null
115
+ }
116
+ }
117
+ ```
118
+
119
+ ## Critical Paths
120
+
121
+ * Runtime should provides,
122
+ 1. WordPress site meta data
123
+ 2. Site capabilties
124
+ 3. WooCommerce & YITH plugin active/inactive status
125
+ 4. Brand plugin information
126
+ 5. Ecommerce details like, Payment & Shipping third party softwares supported and Support contact information
127
+ 6. WordPress version
128
+ 7. Current WordPress Theme.
129
+ * Runtime module should support addition of custom values as per need.
130
+
131
+ ## Installation
132
+
133
+ ### 1. Setup GitHub registry
134
+
135
+ Follow instructions at [GH Packages Setup](https://gist.github.com/aulisius/1a6e4961f17039d82275a6941331b021).
136
+
137
+ ### 2. Install the `@newfold-labs/wp-module-runtime` npm package.
138
+
139
+ ```bash
140
+ npm install @newfold-labs/wp-module-runtime
141
+ ```
142
+
143
+ ## PHP Module
144
+
145
+ The PHP module can be installed only on the Brand plugin.
146
+
147
+ ### 1. Add the Newfold Satis to your `composer.json`.
148
+
149
+ ```bash
150
+ composer config repositories.newfold composer https://newfold-labs.github.io/satis
151
+ ```
152
+
153
+ ### 2. Require the `newfold-labs/wp-module-runtime` package.
154
+
155
+ ```bash
156
+ composer require newfold-labs/wp-module-runtime
157
+ ```
158
+
159
+ ## Usage
160
+
161
+ In your React component you can import the runtime module as so,
162
+
163
+ ```js
164
+ import { NewfoldRuntime } from "@newfold-labs/wp-module-runtime";
165
+
166
+ function Component(props) {
167
+ if (NewfoldRuntime.hasCapability("hasYithExtended")) {
168
+ //
169
+ }
170
+ }
171
+ ```
172
+
173
+ ## Advanced Usage
174
+
175
+ ### Adding custom values
176
+
177
+ While the default runtime has useful values, you can extend the runtime and add newer values under `NewfoldRuntime.sdk`. This is done via use of WP filter.
178
+
179
+ __An example usage__
180
+
181
+ ```php
182
+ add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ) );
183
+
184
+ public function add_to_runtime( $sdk ) {
185
+ return array_merge( $sdk, array( 'my_field' => 'custom value' ) );
186
+ }
187
+ ```
188
+
189
+ Now when you use `NewfoldRuntime.sdk.my_field`, you'll see the value as `'custom value'`;
190
+
191
+ ### Type definition for `NewfoldRuntime.sdk`
192
+
193
+ As the runtime can be extended via the `newfold-runtime` filter, you can also configure the type definitions to make sure you safely access the extended values. Since `sdk` is an TS interface, it can be easily extended in the following manner.
194
+
195
+ ```ts
196
+ declare module "@newfold-labs/wp-module-runtime" {
197
+ export interface DefaultSdk {
198
+ my_field: string;
199
+ }
200
+ }
201
+ ```
202
+
203
+ Now when you use `NewfoldRuntime.sdk`, your editor should give you the correct type hints.
204
+
205
+ [More on Newfold WordPress Modules](https://github.com/newfold-labs/wp-module-loader)
package/build/index.js ADDED
@@ -0,0 +1 @@
1
+ (()=>{"use strict";var e={d:(t,i)=>{for(var o in i)e.o(i,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:i[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{NewfoldRuntime:()=>o});const i=window.wp.url,o={hasCapability:e=>!0===window.NewfoldRuntime?.capabilities[e],adminUrl:e=>window.NewfoldRuntime?.admin_url+e,createApiUrl:(e,t={})=>(0,i.addQueryArgs)(window.NewfoldRuntime?.base_url,{rest_route:e,...t}),get siteDetails(){return window.NewfoldRuntime?.site},get sdk(){return window.NewfoldRuntime?.sdk},get isWoo(){return window.NewfoldRuntime?.isWoocommerceActive},get isYithBooking(){return window.NewfoldRuntime?.isYithBookingActive},get ecommerce(){return window.NewfoldRuntime?.ecommerce},get plugin(){return window.NewfoldRuntime?.plugin},get wpversion(){return window.NewfoldRuntime?.wpversion},get siteTitle(){return window.NewfoldRuntime?.siteTitle},get currentTheme(){return window.NewfoldRuntime?.currentTheme}};var r=exports;for(var n in t)r[n]=t[n];t.__esModule&&Object.defineProperty(r,"__esModule",{value:!0})})();
package/index.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ export interface DefaultSdk {
2
+ wpversion: string;
3
+ }
4
+
5
+ type Capabilities =
6
+ | "hasEcomdash"
7
+ | "hasYithExtended"
8
+ | "isEcommerce"
9
+ | "isJarvis"
10
+ | "canAccessHelpCenter"
11
+ | "canAccessAI";
12
+
13
+ interface RuntimeSdk {
14
+ hasCapability: (name: Capabilities) => boolean;
15
+ adminUrl: (path: string) => string;
16
+ createApiUrl: (path: string, qs?: Record<string, any>) => string;
17
+ siteDetails: { url: string; title: string };
18
+ sdk: DefaultSdk;
19
+ }
20
+
21
+ export const NewfoldRuntime: RuntimeSdk;
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@newfold/wp-module-runtime",
3
+ "description": "Runtime for Newfold WP modules and plugins",
4
+ "license": "GPL-2.0-or-later",
5
+ "version": "1.1.0",
6
+ "main": "build/index.js",
7
+ "files": [
8
+ "build/index.js",
9
+ "index.d.ts"
10
+ ],
11
+ "types": "index.d.ts",
12
+ "scripts": {
13
+ "prepublishOnly": "npm run build",
14
+ "build": "wp-scripts build ./src/index.js --config ./scripts/webpack.config.js",
15
+ "lint": "wp-scripts lint-js ./src/index.js",
16
+ "start": "wp-scripts start ./src/index.js --config ./scripts/webpack.config.js",
17
+ "test:unit": "echo \"No tests implemented\"",
18
+ "test:e2e": "echo \"No tests implemented\""
19
+ },
20
+ "dependencies": {
21
+ "@wordpress/url": "4.15.0"
22
+ },
23
+ "devDependencies": {
24
+ "@wordpress/scripts": "^30.8.0"
25
+ }
26
+ }