-
Notifications
You must be signed in to change notification settings - Fork 5
/
Making your plugins MLP compatible.txt
243 lines (174 loc) · 7.7 KB
/
Making your plugins MLP compatible.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
How to make your plugin MLP compatible.
You need to take some simple steps to make your plugins MLP compatible.
Note that by so doing, your plugin will be able to operate with or without
the MLP Pack.
There are two areas in which you can make your plugin compatible.
1) String localisation.
If you adhere to these guidelines and the MLP Pack happens to be installed
along with your plugin then the administrative users of the site will be
able to easily localise your plugins' strings.
2) Access to localised content.
If you adhere to these guidelines then if the MLP Pack is installed, your
plugin will automatically be supplied with localised content -- articles;
category or section titles; image meta-data (captions and alt-text); link
or file descriptions.
Section 1: String Localisation.
===============================
Example code is given below for an imaginary plugin
called 'sed_example_plugin_name'.
Here are the steps...
1) Have an internal array of named strings.
2) Register a callback routine.
3) Copy the callback routine, changing details as needed. This will pass your
array of strings to the MLP pack and register them as owned by your plugin.
4) Write, or modify the example, plugin-specific gTxt() function that...
4a) tries to get the string from the $textarray and if that fails,
4b) tries to get the string from it's private array and if that fails,
4c) returns the name of the string you were trying to access.
4d) does any variale substitutions that are needed
5) Use the specific gTxt() function consistently throughout your pluign.
/*==============================================================================
EXAMPLE CODE
Here is an example skeleton that meets the above requirements.
I am using the prefix sed_example here for my routines but you *must* change
that for your own plugins.
==============================================================================*/
/*------------------------- PART 1: Define the strings -----------------------*/
#
# Define the string that will get prefixed to your strings when they are
# injected into the txp_lang table.
#
# Keep it less than 10 chars.
# Change both parts of the define for your plugins.
#
define( 'SED_EXAMPLE_PREFIX' , 'sed_xmpl' );
#
# Define the array of strings.
#
# Change the name and content of the array for your plugin.
# Variable substitutions will be done in your gTxt() routine, not here.
#
$sed_example_strings = array(
'string1' => 'Welcome {name}!',
'string2' => 'See you again {time}.',
);
/*------------------------ PART 2: Register A Callback -----------------------*/
#
# Register the callback for the enumerate string event.
# If the MLP pack is not present and active this will NOT get called.
#
register_callback( 'sed_example_enumerate_strings' , 'l10n.enumerate_strings' );
/*---------------------- PART 3: Registration Callback -----------------------*/
#
# Here's a callback routine used to register the above strings with
# the MLP Pack (if installed).
#
function sed_example_enumerate_strings($event , $step='' , $pre=0)
{
global $sed_example_strings;
$r = array (
'owner' => 'sed_example_plugin_name', # Change to your plugin's name
'prefix' => SED_EXAMPLE_PREFIX, # Its unique string prefix
'lang' => 'en-gb', # The language of the initial strings.
'event' => 'public', # public/admin/common = which interface the strings will be loaded into
'strings' => $sed_example_strings, # The strings themselves.
);
return $r;
}
/*--------------------- PART 4: Plugin Specific gTxt() -----------------------*/
function sed_example_gTxt( $what , $args = array() )
{
global $textarray;
global $sed_example_strings;
#
# Prepare the prefixed key for use...
#
$key = SED_EXAMPLE_PREFIX . '-' . $what;
$key = strtolower($key);
#
# Grab from the global textarray (possibly edited by MLP) if we can...
#
if(isset($textarray[$key]))
{
$str = $textarray[$key];
}
else
{
#
# The string isn't in the localised $textarray so fallback to using
# the string array in the plugin (which is not prefixed.)
#
$key = strtolower($what);
if( isset( $sed_example_strings[$key] ) )
$str = $sed_example_strings[$key];
else
#
# Fallback to returning the key if the string is not present...
#
$str = $what;
}
#
# If needed, perform substitutions...
#
if( !empty($args) )
$str = strtr( $str , $args );
return $str;
}
/*==============================================================================
END EXAMPLE CODE
==============================================================================*/
Section 2: Access Localised Content
===================================
The MLP Pack creates tables with localised content in them from the master
textpattern table. The pack then uses a table-name remapping on the public
interface to serve language specific content to plugins through the DB layer.
This remapping is not done on the admin side.
Although a language-specific temporary textpattern table could have been created
to hide the original textpattern table for each client connection this would put
a continuous un-needed load on the server so the MLP pack maintains a set of
copies of the textpattern table -- one table per site language -- and then
relies on a simple function to remap any queries against the 'textpattern' table
to the copy with content in that language.
If your plugin uses any of the safe_blah() functions (except safe_query) or the
fetch() routine; then this will happen without your needing to do anything special.
However, if your plugin accesses the textpattern table via a call to safe_query()
or any of the other routines in txplib_db.php then it will work, but will
potentially retreive rows for languages that the site visitor is not using.
To get around this you can make a few simple adjustments...
a) use the built-in DB access functions found in textpattern/lib/txplib_db.php
rather than going to the DB via PHP's MySQL access routines.
b) Don't use the PFX constant.
Huh? Yes, if your plugin is meant for TxP 4.0.4 or above then this is easy as you
can simply switch all PFX prefixing over to the safe_pfx() or safe_pfx_j()
routines. The MLP Pack currently uses these to do some crafty table name changing
to give access to localised content.
For plugins that need to work in TxP versions prior to 4.0.4 and you use the PFX
constant then you can use the following routine to prefix your tables (and
redirect to localised tables if the MLP Pack is present)...
/*==============================================================================
EXAMPLE CODE
Here is an example skeleton that meets the above requirements.
I am using the prefix sed_example_ here for my routines but you *must* change
that for your own plugins.
==============================================================================*/
function sed_example_pfx( $table )
{
if( is_callable( 'safe_pfx' ) )
$table = safe_pfx( $table );
else
$table = PFX.$table;
return $table;
}
(Remember to rename the function according to your plugin's naming convention).
Now use this call whenever you need to construct the name of a table for a call
to safe_query().
Or...
c) Try not to use the safe_query() routine but rather use the specific routines
for the operations you are trying to perform (this is not possible for all queries)
For example, instead of doing...
safe_query( "select * from ".PFX."textpattern where `ID`='$my_id'" );
either use your prefix routine...
$table = sed_example_pfx( 'textpattern' );
safe_query( "select * from $table where `ID`='$my_id'" );
or use a more specfic access routine...
safe_row( '*', 'textpattern', "`ID`='$my_id'" );