Skip to content

Commit 608daf9

Browse files
authored
Add plugins page & API (#123)
Squashed * Initial feature commit * Remember last tab * Refactor some logic * Use the data attr * Use getter to expose plugin instances * Document feature * PHPCBF autofix * Fix rest of the lint * Document usage with spaces * Introduce way to force user to a page, once * Make lists filterable and hide lists if empty * Text changes * I love you WPCS
1 parent 0898b57 commit 608daf9

5 files changed

Lines changed: 422 additions & 2 deletions

File tree

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,29 @@ add_filter('wplf_dynamic_values', function($values) {
198198
// <input type="text" placeholder="%SOMETHING%" name="something">
199199
```
200200

201+
## Plugins
202+
1.5 exposes a new function, `wplf()`. It simply returns the class instance of WP Libre Form.
203+
204+
It allows you to register your plugin as a WP Libre Form plugin, which in turn allows you to expose an API and a settings page for your plugin, should you want to.
205+
206+
```php
207+
$wplf = wplf();
208+
$plugin = new YourPlugin();
209+
210+
$wplf->plugins->register([
211+
"name" => "YourPlugin", // The name you wish to show on the WPLF plugin page. Willl also be used to access public methods in your plugin
212+
"description" => "What your plugin does in a sentence or two",
213+
"link" => "https://toyourplugin.com", // Plugin URL. Can be wordpress.org or pretty much any URL where you can download the plugin
214+
"version" => YOUR_PLUGIN_VERSION, // Define a constant containing your plugin version
215+
"instance" => $plugin, // Your plugin, instantiated. Users can access your public methods
216+
"settings_page" => [$plugin, "render_settings_page"], // Function that renders your settings page, or a string that contains the link to it. Leave empty to disable.
217+
]);
218+
```
219+
220+
If you use spaces in the name, you can access the plugin instance like this:
221+
222+
`wplf()->plugins->{"Your plugin"}->somePublicMethod()`
223+
201224
## Javascript API
202225

203226
### Client side callbacks

assets/scripts/wplf-plugins.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(function($) {
2+
var menu = $('.wplf-plugins-menu');
3+
var pages = $('.wplf-plugins-page');
4+
var previousItem = localStorage.getItem('wplf-plugins-page-last-active-tab') || 'General';
5+
6+
menu.on('click', '.nav-tab', function(e) {
7+
var item = $(e.target);
8+
var page = item.attr('data-page');
9+
var target = $('.wplf-plugins-page[data-page="' + page + '"]');
10+
11+
item.siblings().removeClass('nav-tab-active');
12+
item.addClass('nav-tab-active');
13+
14+
pages.hide();
15+
target.show();
16+
17+
try {
18+
localStorage.setItem('wplf-plugins-page-last-active-tab', page);
19+
} catch (e) {
20+
// No one cares. Safari just throws in PB.
21+
// The tab remember feature just won't work.
22+
console.error(e);
23+
}
24+
e.preventDefault();
25+
});
26+
27+
if (previousItem) {
28+
var target = $('.wplf-plugins-menu [data-page="' + previousItem + '"]');
29+
30+
target.click();
31+
}
32+
})(jQuery);

assets/styles/wplf-plugins.css

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.wplf-plugins {
2+
box-sizing: border-box;
3+
}
4+
5+
.wplf-plugins *, .wplf-plugins *:before, .wplf-plugins *:after {
6+
box-sizing: border-box;
7+
}
8+
9+
.wplf-plugins-page {
10+
display: none;
11+
overflow: hidden;
12+
}
13+
14+
.wplf-plugin-list {
15+
overflow: hidden;
16+
}
17+
18+
.wplf-plugin-box {
19+
padding: 1rem;
20+
border: 1px solid #222;
21+
width: 48%;
22+
margin-right: 2%;
23+
float: left;
24+
background: #fff;
25+
}
26+
27+
.wplf-plugin-box__meta {
28+
overflow: hidden;
29+
line-height: 30px; /* Poor man centering */
30+
}
31+
32+
.wplf-plugin-box__meta--version {
33+
color: #ccc;
34+
}
35+
36+
.wplf-plugin-box__meta--links {
37+
float: right;
38+
}
39+
40+
@media (max-width: 768px) {
41+
.wplf-plugin-box {
42+
width: 100%;
43+
margin-right: 0;
44+
margin-bottom: 1rem;
45+
}
46+
}

0 commit comments

Comments
 (0)