2015-06-15 00:59:46 +09:00
|
|
|
Plugin Development
|
2019-08-13 05:48:29 +09:00
|
|
|
==================
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
SamplePlugin.php
|
2019-08-13 05:48:29 +09:00
|
|
|
----------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
Each plugin requires a main class to interact with the GNU social system.
|
|
|
|
|
|
|
|
The main class usually extends the Plugin class that comes with GNU social.
|
|
|
|
|
|
|
|
The class has standard-named methods that will be called when certain events
|
|
|
|
happen in the code base. These methods have names like 'onX' where X is an
|
|
|
|
event name (see EVENTS.txt for the list of available events). Event handlers
|
|
|
|
have pre-defined arguments, based on which event they're handling. A typical
|
|
|
|
event handler:
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function onSomeEvent($paramA, &$paramB): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
if ($paramA == 'jed') {
|
|
|
|
throw new Exception(sprintf(_m("Invalid parameter %s"), $paramA));
|
|
|
|
}
|
|
|
|
$paramB = 'spock';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Event Handlers
|
2019-08-13 05:48:29 +09:00
|
|
|
--------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
Event handlers must return a Boolean value.
|
|
|
|
|
|
|
|
If they return false, all other event handlers for this event (in other plug-in)
|
|
|
|
will be skipped, and in some cases the default processing for that event would
|
|
|
|
be skipped. This is great for replacing the default action of an event.
|
|
|
|
|
|
|
|
If the handler returns true, processing of other event handlers and the default
|
|
|
|
processing will continue. This is great for extending existing functionality.
|
|
|
|
|
|
|
|
If the handler throws an exception, processing will stop, and the exception's
|
|
|
|
error will be shown to the user.
|
|
|
|
|
|
|
|
Installation
|
2019-08-13 05:48:29 +09:00
|
|
|
------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
2019-08-13 05:48:29 +09:00
|
|
|
To enable a plugin (like the SamplePlugin), site admins add the following code to their
|
2015-06-15 00:59:46 +09:00
|
|
|
config.php file:
|
|
|
|
|
|
|
|
```php
|
|
|
|
addPlugin('Sample');
|
|
|
|
```
|
|
|
|
|
2019-08-13 05:48:29 +09:00
|
|
|
Third Party Plugins must be installed in `local/plugins/{$name}/{$pluginclass}.php`
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
Here, `{$name}` is the name of the plugin, like 'Sample', and `{$pluginclass}`
|
|
|
|
is the name of the main class, like 'SamplePlugin'. Plugins that are part of
|
|
|
|
the main GNU social distribution go in 'plugins' and third-party or local ones
|
|
|
|
go in 'local'.
|
|
|
|
|
2019-08-13 05:48:29 +09:00
|
|
|
Simple plugins can be implemented as a single plugin. Others are more complex
|
|
|
|
and require additional plugins; these should use their own directory, like
|
2015-06-15 00:59:46 +09:00
|
|
|
'local/plugins/{$name}/'. All files related to the plugin, including images,
|
2019-08-13 05:48:29 +09:00
|
|
|
JavaScript, CSS, external libraries or PHP plugins should go in the plugin
|
2015-06-15 00:59:46 +09:00
|
|
|
directory.
|
|
|
|
|
|
|
|
Plugin Configuration
|
2019-08-13 05:48:29 +09:00
|
|
|
--------------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
Plugins are configured using public instance attributes. To set their values,
|
|
|
|
site administrators use this syntax:
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
addPlugin('Sample', ['attr1' => 'foo', 'attr2' => 'bar']);
|
2015-06-15 00:59:46 +09:00
|
|
|
```
|
|
|
|
|
|
|
|
The same plugin class can be initialized multiple times with different arguments:
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
addPlugin('EmailNotify', ['sendTo' => 'evan@status.net']);
|
|
|
|
addPlugin('EmailNotify', ['sendTo' => 'brionv@status.net']);
|
2015-06-15 00:59:46 +09:00
|
|
|
```
|
|
|
|
|
|
|
|
```php
|
|
|
|
class SamplePlugin extends Plugin
|
|
|
|
{
|
|
|
|
public $attr1 = null;
|
|
|
|
public $attr2 = null;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Initialization
|
2019-08-13 05:48:29 +09:00
|
|
|
--------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
Plugins overload this method to do any initialization they need, like connecting
|
2019-08-13 05:48:29 +09:00
|
|
|
to remote servers or creating paths or so on. @return bool hook value; true
|
2015-06-15 00:59:46 +09:00
|
|
|
means continue processing, false means stop.
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function initialize(): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Clean Up
|
2019-08-13 05:48:29 +09:00
|
|
|
--------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
Plugins overload this method to do any cleanup they need, like disconnecting from
|
|
|
|
remote servers or deleting temp files or so on.
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function cleanup(): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Database schema setup
|
2019-08-13 05:48:29 +09:00
|
|
|
---------------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
Plugins can add their own tables to the GNU social database. Plugins should use
|
|
|
|
GNU social's schema interface to add or delete tables. The ensureTable() method
|
|
|
|
provides an easy way to ensure a table's structure and availability.
|
|
|
|
|
|
|
|
By default, the schema is checked every time GNU social is run (say, when a Web
|
|
|
|
page is hit). Admins can configure their systems to only check the schema when
|
|
|
|
the checkschema.php script is run, greatly improving performance. However, they
|
|
|
|
need to remember to run that script after installing or upgrading a plugin!
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function onCheckSchema(): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
$schema = Schema::get();
|
|
|
|
|
|
|
|
// '''For storing user-submitted flags on profiles'''
|
|
|
|
|
2019-08-13 05:48:29 +09:00
|
|
|
$schema->ensureTable('user_greeting_count',[
|
|
|
|
new ColumnDef('user_id', 'integer', null, true, 'PRI'),
|
|
|
|
new ColumnDef('greeting_count', 'integer')
|
|
|
|
]
|
|
|
|
);
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-08-13 05:48:29 +09:00
|
|
|
Load related plugins when needed
|
|
|
|
--------------------------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
2019-08-13 05:48:29 +09:00
|
|
|
Most non-trivial plugins will require extra plugins to do their work. Typically
|
2015-06-15 00:59:46 +09:00
|
|
|
these include data classes, action classes, widget classes, or external libraries.
|
|
|
|
|
|
|
|
This method receives a class name and loads the PHP file related to that class.
|
|
|
|
By tradition, action classes typically have files named for the action, all
|
|
|
|
lower-case. Data classes are in files with the data class name, initial letter
|
|
|
|
capitalized.
|
|
|
|
|
|
|
|
Note that this method will be called for *all* overloaded classes, not just ones
|
|
|
|
in this plugin! So, make sure to return true by default to let other plugins,
|
|
|
|
and the core code, get a chance.
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function onAutoload($cls): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
2019-08-13 05:48:29 +09:00
|
|
|
$dir = __DIR__;
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
switch ($cls)
|
|
|
|
{
|
2019-08-13 05:48:29 +09:00
|
|
|
case 'HelloAction':
|
|
|
|
include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
|
|
|
|
return false;
|
|
|
|
case 'User_greeting_count':
|
|
|
|
include_once $dir . '/'.$cls.'.php';
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
2015-06-15 00:59:46 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Map URLs to actions
|
2019-08-13 05:48:29 +09:00
|
|
|
-------------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
This event handler lets the plugin map URLs on the site to actions (and thus an
|
|
|
|
action handler class). Note that the action handler class for an action will be
|
|
|
|
named 'FoobarAction', where action = 'foobar'. The class must be loaded in the
|
|
|
|
onAutoload() method.
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function onRouterInitialized($m): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
$m->connect('main/hello',
|
2019-08-13 05:48:29 +09:00
|
|
|
['action' => 'hello']);
|
2015-06-15 00:59:46 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Modify the default menu to link to our custom action
|
2019-08-13 05:48:29 +09:00
|
|
|
-----------------------------------------------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
Using event handlers, it's possible to modify the default UI for pages almost
|
|
|
|
without limit. In this method, we add a menu item to the default primary menu
|
|
|
|
for the interface to link to our action.
|
|
|
|
|
|
|
|
Action Class
|
2019-08-13 05:48:29 +09:00
|
|
|
------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
The Action class provides a rich set of events to hook, as well as output methods.
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function onEndPrimaryNav($action): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
// '''common_local_url()''' gets the correct URL for the action name we provide
|
|
|
|
|
|
|
|
$action->menuItem(common_local_url('hello'),
|
|
|
|
_m('Hello'), _m('A warm greeting'), false, 'nav_hello');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-12 23:03:30 +09:00
|
|
|
public function onPluginVersion(&$versions): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
2019-08-13 05:48:29 +09:00
|
|
|
$versions[] = [
|
|
|
|
'name' => 'Sample',
|
|
|
|
'version' => GNUSOCIAL_VERSION,
|
|
|
|
'author' => 'Brion Vibber, Evan Prodromou',
|
|
|
|
'homepage' => 'http://example.org/plugin',
|
|
|
|
'rawdescription' =>
|
|
|
|
_m('A sample plugin to show basics of development for new hackers.')
|
|
|
|
];
|
2015-06-15 00:59:46 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
hello.php
|
2019-08-13 05:48:29 +09:00
|
|
|
---------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
This section is taken directly from the 'hello.php'. ( plugins/Sample/hello.php )
|
|
|
|
|
|
|
|
Give a warm greeting to our friendly user.
|
|
|
|
|
|
|
|
This sample action shows some basic ways of doing output in an action class.
|
|
|
|
|
|
|
|
Action classes have several output methods that they override from the parent class.
|
|
|
|
|
|
|
|
```php
|
|
|
|
class HelloAction extends Action
|
|
|
|
{
|
|
|
|
var $user = null;
|
|
|
|
var $gc = null;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Take arguments for running
|
2019-08-13 05:48:29 +09:00
|
|
|
--------------------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
This method is called first, and it lets the action class get all its arguments
|
|
|
|
and validate them. It's also the time to fetch any relevant data from the database.
|
|
|
|
|
2019-04-26 08:34:17 +09:00
|
|
|
Action classes should run parent::prepare(array $args = []) as the first line
|
|
|
|
of this method to make sure the default argument-processing happens.
|
2015-06-15 00:59:46 +09:00
|
|
|
|
2019-08-13 05:48:29 +09:00
|
|
|
```php
|
|
|
|
public function prepare(array $args = []): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
parent::prepare($args);
|
|
|
|
|
|
|
|
$this->user = common_current_user();
|
|
|
|
|
|
|
|
if (!empty($this->user)) {
|
|
|
|
$this->gc = User_greeting_count::inc($this->user->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Handle request
|
2019-08-13 05:48:29 +09:00
|
|
|
--------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
This is the main method for handling a request. Note that most preparation
|
|
|
|
should be done in the prepare() method; by the time handle() is called the
|
|
|
|
action should be more or less ready to go.
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function handle(): void
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
2019-04-26 08:34:17 +09:00
|
|
|
parent::handle();
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
$this->showPage();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Title of this page
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Override this method to show a custom title.
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function title(): string
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
if (empty($this->user)) {
|
|
|
|
return _m('Hello');
|
|
|
|
} else {
|
|
|
|
return sprintf(_m('Hello, %s'), $this->user->nickname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Show content in the content area
|
2019-08-13 05:48:29 +09:00
|
|
|
--------------------------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
The default GNU social page has a lot of decorations: menus, logos, tabs, all
|
|
|
|
that jazz. This method is used to show content in the content area of the
|
|
|
|
page; it's the main thing you want to overload. This method also demonstrates
|
|
|
|
use of a plural localized string.
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function showContent(): void
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
if (empty($this->user)) {
|
2019-08-13 05:48:29 +09:00
|
|
|
$this->element('p', ['class' => 'greeting'],
|
2015-06-15 00:59:46 +09:00
|
|
|
_m('Hello, stranger!'));
|
|
|
|
} else {
|
2019-08-13 05:48:29 +09:00
|
|
|
$this->element('p', ['class' => 'greeting'],
|
2015-06-15 00:59:46 +09:00
|
|
|
sprintf(_m('Hello, %s'), $this->user->nickname));
|
2019-08-13 05:48:29 +09:00
|
|
|
$this->element('p', ['class' => 'greeting_count'],
|
2015-06-15 00:59:46 +09:00
|
|
|
sprintf(_m('I have greeted you %d time.',
|
|
|
|
'I have greeted you %d times.',
|
|
|
|
$this->gc->greeting_count),
|
|
|
|
$this->gc->greeting_count));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Return true if read only.
|
2019-08-13 05:48:29 +09:00
|
|
|
-------------------------
|
2015-06-15 00:59:46 +09:00
|
|
|
|
|
|
|
Some actions only read from the database; others read and write. The simple
|
|
|
|
database load-balancer built into GNU social will direct read-only actions to
|
|
|
|
database mirrors (if they are configured) and read-write actions to the master database.
|
|
|
|
|
|
|
|
This defaults to false to avoid data integrity issues, but you should make sure
|
|
|
|
to overload it for performance gains.
|
|
|
|
|
|
|
|
```php
|
2019-08-13 05:48:29 +09:00
|
|
|
public function isReadOnly($args): bool
|
2015-06-15 00:59:46 +09:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
```
|