Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-06-04 08:43:33

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Theme prefs plugin

Hi,
I’m trying to set a plugin to easily manage themes prefs from the Txp advanced prefs page.
I used to use adi_variables in the past, but I’m playing with rah_flat now, which is not compatible with.
As Jukka suggested in this Github post I’m trying to add the prefs managed with adi_variables though new prefs fields.
To achieve this, as I don’t really know PHP, I customized smd_at_work which seemed to be light enough to me.
The problem for now is that It adds the fields I want, but the link the prefs in the plugin page is not display and above all the fields are not removed when I disable or remove the plugin. Honestly, as the code is mainly that of Stef’s plugin, I don’t really understand why the behavior is different…

Here is the code:

if (txpinterface === 'admin') {
	add_privs('prefs.oui_pinbox_theme_prefs', '1');
	add_privs('plugin_prefs.oui_pinbox_theme_prefs', '1');
	register_callback('oui_pinbox_theme_prefs_welcome', 'plugin_lifecycle.oui_pinbox_theme_prefs');
	register_callback('oui_pinbox_theme_prefs_install', 'prefs', null, 1);
	register_callback('oui_pinbox_theme_prefs_options', 'plugin_prefs.oui_pinbox_theme_prefs', null, 1);
}

/**
 * Handler for plugin lifecycle events.
 *
 * @param string $evt Textpattern action event
 * @param string $stp Textpattern action step
 */
function oui_pinbox_theme_prefs_welcome($evt, $stp)
{
	switch ($stp) {
		case 'installed':
		case 'enabled':
			oui_pinbox_theme_prefs_install();
			break;
		case 'deleted':
			if (function_exists('remove_pref')) {
				// 4.6 API
				remove_pref(null, 'oui_pinbox_theme_prefs');
			} else {
				safe_delete('txp_prefs', "event='oui_pinbox_theme_prefs'");
			}
			safe_delete('txp_lang', "name LIKE 'oui\_pinbox\_theme\_prefs%'");
			break;
	}
}

/**
 * Install the prefs if necessary.
 *
 * This is a separate function so it can be used as a direct callback.
 * When operating under a plugin cache environment, the install lifecycle
 * event is never fired, so this is a fallback.
 *
 * The lifecycle callback remains for deletion purposes under a regular installation,
 * since the plugin cannot detect this in a cache environment.
 *
 * @see oui_pinbox_theme_prefs_welcome()
 * @todo change PREF_ADVANCED to PREF_PLUGIN from 4.6
 */
function oui_pinbox_theme_prefs_install()
{
	if (get_pref('oui_pinbox_theme_prefs_sections_sort_order', null) === null) {
		set_pref('oui_pinbox_theme_prefs_sections_sort_order', 'articles, about', 'oui_pinbox_theme_prefs', PREF_ADVANCED, 'text_input', 10);
	}
	if (get_pref('oui_pinbox_theme_prefs_about_section_name', null) === null) {
		set_pref('oui_pinbox_theme_prefs_about_section_name', 'about', 'oui_pinbox_theme_prefs', PREF_ADVANCED, 'text_input', 10);
	}
	if (get_pref('oui_pinbox_theme_prefs_imprint_section_name', null) === null) {
		set_pref('oui_pinbox_theme_prefs_imprint_section_name', 'imprint', 'oui_pinbox_theme_prefs', PREF_ADVANCED, 'text_input', 10);
	}
	if (get_pref('oui_pinbox_theme_prefs_contact_informations', null) === null) {
		set_pref('oui_pinbox_theme_prefs_contact_informations', 'imprint', 'oui_pinbox_theme_prefs', PREF_ADVANCED, 'text_input', 10);
	}
	if (get_pref('oui_pinbox_theme_prefs_contact_email', null) === null) {
		set_pref('oui_pinbox_theme_prefs_contact_email', 'imprint', 'oui_pinbox_theme_prefs', PREF_ADVANCED, 'text_input', 10);
	}
	if (get_pref('oui_pinbox_theme_prefs_subscribe_email', null) === null) {
		set_pref('oui_pinbox_theme_prefs_subscribe_email', 'imprint', 'oui_pinbox_theme_prefs', PREF_ADVANCED, 'text_input', 10);
	}
}

/**
 * Jump to the prefs panel.
 */
function oui_pinbox_theme_prefs_options()
{
	$link = oui_pinbox_theme_prefs_prefs_link();

	header('Location: ' . $link);
}

/**
 * Fetch the admin-side prefs panel link.
 *
 * It's version dependent, as 4.6.0 doesn't have the notion of
 * Advanced Prefs.
 */
function oui_pinbox_theme_prefs_prefs_link()
{
	global $dbversion;
	if (version_compare($dbversion, '4.6-dev') < 0) {
		$link = '?event=prefs&step=advanced_prefs';
	} else {
		$link = '?event=prefs';
	}

	return $link;
}

Last edited by NicolasGraph (2015-06-04 08:44:51)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#2 2015-06-04 09:11:57

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,493
Website

Re: Theme prefs plugin

Nothing wrong with the code, you probably just need to set the plugin flags. Seems like you’ve already set the pluign type to ‘Admin only’, which is good for this type of plugin.

If you have ied_plugin_composer installed, it’s easy: just check the ‘Has prefs’ and ‘Event notify’ checkboxes and save. If you’re not using the composer, you need to open up your plugin’s .php template file and ensure there’s a line in the header section near the top (somewhere just after the $plugin['type']) that reads:

$plugin['flags'] = '3';

Then compile the plugin and reinstall it. That does the same thing as the plugin composer’s checkboxes and informs Txp that your plugin needs an Options link and that it should listen for install/enable/disable/delete events.

Hope that helps.

Last edited by Bloke (2015-06-04 09:24:39)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2015-06-04 09:33:15

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Theme prefs plugin

Thanks Stef, I have ied_plugin_composer.
The Options link is ok now but I can’t remove the fields for now. I need to try on another install…


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#4 2015-06-04 09:56:07

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Theme prefs plugin

Ok, I found what was wrong with the fields removing.
In fact, my plugin name oui_pinbox_theme_prefs is too long to be fully displayed in the prefs header; it is cut somewhere by Txp and I can only see oui_pinbox_t.

Changing this part of the code:

function oui_pinbox_theme_prefs_welcome($evt, $stp)
{
	switch ($stp) {
		case 'installed':
		case 'enabled':
			oui_pinbox_theme_prefs_install();
			break;
		case 'deleted':
			if (function_exists('remove_pref')) {
				// 4.6 API
				remove_pref(null, 'oui_pinbox_theme_prefs');
			} else {
				safe_delete('txp_prefs', "event='oui_pinbox_theme_prefs'");
			}
			safe_delete('txp_lang', "name LIKE 'oui\_pinbox\_theme\_prefs%'");
			break;
	}
}

to this:

function oui_pinbox_theme_prefs_welcome($evt, $stp)
{
	switch ($stp) {
		case 'installed':
		case 'enabled':
			oui_pinbox_theme_prefs_install();
			break;
		case 'deleted':
			if (function_exists('remove_pref')) {
				// 4.6 API
				remove_pref(null, 'oui_pinbox_t');
			} else {
				safe_delete('txp_prefs', "event='oui_pinbox_t'");
			}
			safe_delete('txp_lang', "name LIKE 'oui\_pinbox\_t%'");
			break;
	}
}

resolved the problem as (if I understand well) the plugin can detect the prefs to remove.

It seems that the plugin name, or at least the prefs header, must be short.


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#5 2015-06-04 09:59:56

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,493
Website

Re: Theme prefs plugin

NicolasGraph wrote #291293:

It seems that the plugin name, or at least the prefs header, must be short.

Ah yes, forgot about that, sorry. The prefs table’s event column is pitifully short in 4.5.x (11 or 12 characters I think). From 4.6+ the restriction has been lifted to 255 chars, which is why your code worked for me when I tested it in my 4.6-dev environment.

It’ll work fine if you set your prefs using a shorter event name (3rd argument), for example:

set_pref('oui_pinbox_theme_prefs_contact_email', 'imprint', 'oui_pinbox', PREF_ADVANCED, 'text_input', 10);

and searching for event='oui_pinbox' during the cleanup routine.

Alternatively, your solution of keeping the event name long and just checking for a truncated name during cleanup is equally good. Means that 4.6 users at least get the full event name.

Last edited by Bloke (2015-06-04 10:04:33)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#6 2015-06-04 10:04:43

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Theme prefs plugin

Ok, good news for 4.6!
Thanks Stef.

(Maybe this topic should be in the Plugin discussions part?)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#7 2015-06-04 10:06:41

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,493
Website

Re: Theme prefs plugin

NicolasGraph wrote #291295:

(Maybe this topic should be in the Plugin discussions part?)

Moved. Also edited my post above to add some info about using a shorter event name when setting prefs.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#8 2015-06-04 14:15:46

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,165

Re: Theme prefs plugin

Added oui to the list of TXP dev prefixes.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#9 2015-06-04 14:29:15

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Theme prefs plugin

uli wrote #291304:

Added oui to the list of TXP dev prefixes.

Thanks Uli!

Does someone could tell me how to add some informations in the tool pop-up of each field I add in the prefs? The ? link…


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#10 2015-06-04 14:46:04

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,493
Website

Re: Theme prefs plugin

NicolasGraph wrote #291305:

how to add some informations in the tool pop-up of each field I add in the prefs? The ? link…

Sadly, for 4.5.x the only way is to use jQuery to intercept the help clicks on certain elements and replace them with your own handler.

From 4.6+ there’s a callback in the popHelp() function to allow plugins to customise the content. There will also be some helper functions to render standard help templates to fit in with the current theme, plus we’ve introduced the notion of inline help to show up on-screen, which is manipulated through Textpacks for full i18n.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB