Documentation for the Textpattern plugin smd_lib by Stef Dawson follows this short message from our sponsor ;-)
If you like my code and deem it worthy, feel free to show your appreciation with something from my UK Amazon wish list (or US) or donate to the Stef Dawson community coding pot, either via paypal.me/stefdawson or by following the Donate button below to PayPal. Thanks!
smd_ plugin library
Offers no public textpattern tags. It is simply a shared library of common functions used by smd_ plugins.
Function Reference
smd_addQSVar
smd_removeQSVar
Add or remove a query string variable to the given URL, taking into account any existing variables that may be in the URL already. ‘Add’ takes three arguments, ‘Remove’ just takes the first two:
- The URL string to add to/remove from
- The id of the querystring (the bit before the = sign)
- The value of the new querystring (the bit after the = sign)
e.g. smd_addQSVar($thisarticle['url_title'], 'tpg', 15);
would add tpg=15
to the current article’s URL. If there are no other variables currently in the URL, it is added with a question mark, otherwise it is appended with an ampersand.
smd_doList
Return an expanded list of items with the following properties:
- Anything containing ‘?’ or ‘!’ is checked for a match with a TXP field (
<txp:variable />
, image, file, link, global article, url POST/GET/SERVER, or individual article, in that order) - Any ranges of items are expanded (e.g. 4-7 => 4,5,6,7) if the
rng
option permits it - TXP fields may themselves be lists or ranges
- Anything that is not a TXP field is used verbatim
- The items are returned as 2 lists; inclusion and exclusion
Args ( [*] = mandatory ):
- [*] lst = the list as a delimited string
- rng = whether to allow ranges or not (bool). Default = true
- sub = the type of subcategory to traverse (image, file, link, article, none=”“) and how many levels to go down (e.g. image:2). Default = ‘’
- qte = whether to quote each item in the array or not (bool). Default = true
- dlm = the delimiter (string). Default = “,”
- lax = Whether to be lax or strict about what characters constitute a field; primarily whether spaces are allowed in, say, custom fields. Default = “1”
smd_getOpts
Deprecated as it is mostly superseded by smd_doList; this one is clunkier but has $idprefix so it remains for now. It searches the passed string for predetermined sequences of characters and, if that sequence is in the given $allowed array, replaces it as follows:
- ?c = current global category (!c = not current category)
- ?s = current section (!s = not current section)
- ?t = current article title (!t = not current title)
- ?id = current article ID, prepended with $idprefix (!id = not current ID)
- ?q = current query term (!q = not current query term)
- ?field = contents of the current article’s field (could be a comma-separated list)
- !field = not the contents of the current article’s field (could be a comma-separated list)
Integer ranges (e.g. 1-5) will be expanded into their individual values if the $allowRange option is true; anything else is returned verbatim. It outputs two arrays: the 1st contains items for inclusion, the 2nd contains items for exclusion.
Args ( [*] = mandatory ):
- [*] The string to search for matches
- [*] An array containing shortcuts that are “allowed” to be found in the string (?c, ?s, ?t, ?field etc)
- The prefix for ?id strings
- Boolean indicating whether to allow range expansion or not
- RegEx string to split options at (see smd_split)
- preg_split option (see smd_split)
smd_split
Returns an array of items from a string of (usually) comma-separated values. If any values contain ranges of numbers like 1-5 that need ‘expanding’ first (and $allowRange is true), they are dealt with. Takes the following arguments ( [*] = mandatory args) :
- [*] The string to split
- Boolean indicating whether to allow range expansion or not (i.e. 1-5 becomes 1,2,3,4,5)
- The regular expression character classes to match. If a full RegEx starting and ending with ‘/’ characters is supplied, the expression is used verbatim. Without the ‘/’ characters, the expression is treated as a list of character classes to find. Defaults to “/(,|,\s)+/” which is a comma, or comma and a whitespace character.
- preg_split option constant as defined in the php manual
smd_MLP
Instantiate one of these to handle MLP in your plugin like this:
1) Declare a unique global variable, e.g. global $myPlug
2) Define your default string replacement array (doesn’t need to be global), e.g:
$myStrings = array (“msg1” => “This is message 1”, “msg2” => “This is message 2”);
3) Create an MLP handler:
$myPlug = new smd_MLP(“plugin_name”, “plugin_prefix”, $myStrings);
4) That’s it! There are two optional args to smd_MLP: a) the default (full) language to use, e.g “da-dk”. Defaults to “en-gb”. b) the interface the strings are for. Choose from “public” (the default), “admin” or “common”
5) To use a replacement string in your code: a) Make sure to import the unique global variable: e.g. global $myPlug; b) Call $myPlug->gTxt(“messageID”); [ e.g. $myPlug->gTxt(“msg1”) ] c) If you want to replace any args in your message string, pass an associative array as the 2nd arg to gTxt()
smd_doDblQuote
Alternative to the core’s doQuote(). This one dbl-quotes instead of sgl-quotes
smd_pregPos
Lifted from one of the comments in the PHP manual, this just looks for a RegEx string within another, returning the matches it finds and the position of the first match.
array_combine
PHP4 equivalent of the standard PHP5 function, lifted from php.net
htmlspecialchars_decode
PHP4 equivalent of the standard PHP5 function, lifted from php.net
Source code
If you’d rather spend time with the bytes, you’ll need to step into the view source page.
Legacy software
If, for some inexplicable reason, you need a prior version of a plugin, it can probably be found on the plugin archive page.
Experimental software
If you’re feeling brave, or fancy dipping your toe in shark-infested water, you can test out some of my beta code. It can be found on the plugin beta page.