Piero V.

Flatpress: far vedere post non (apparentemente) collegati

Italiano

Scrivendo un plugin per i tag mi sono chiesto: “Come si potrebbe far vedere tutti i post con uno stesso tag?”

Andando avanti a pensare mi sono detto: “una specie di categoria”: ed ecco qui la risposta: quando ce n’è il bisogno, si fa creare una falsa categoria durante il filtro init.

Le categorie sono gestite dalla classe FPDB che le carica solo una volta e flatpress ne crea solo un’istanza.

Ecco il codice di base:

class plugin_tag_walker {
	var $valid=true;
	function current_key() {
		return current($this->array);
	}
	function next() {
		$n=next($this->array);
		if($n==false)
			$this->valid=false;
		return $n;
	}
}

class plugin_tag_fpdb {
	function length() {
		return $this->len;
	}
	function walker($p1=null) {
		$a=new plugin_tag_walker();
		$a->array=$this->array;
		return $a;
	}
}

function prova_cat() {
	global $fpdb, $fp_params;
	if(isset($_GET['tag'])) {       ### Solo se c'è il parametro GET tag
		$fp_params['cat']=-50;  ### L'id della categoria
		$p=new plugin_tag_fpdb();
		$p->array=array(
			'100715151839', ### Quali sono i post
			'100715163207',
			'100808220001'
		);
		$p->len=count($p->array);
		if($fp_params['count']>$p->len)
			$fp_params['count']=$p->len;
		$fpdb->_indexer[$fp_params['cat']]=&$p;
	}
	return true;
}

add_filter('init', 'prova_cat');

Praticamente la funzione prova_cat assegna alla categoria -50 un oggetto che ne restituisce un altro che una volta chiamato dal codice di flatpress restituisce i post.

In più controlla anche se ci sono altri post da far vedere.

Sembra funzionare tutto correttamente anche se esiste una categoria con quell’id, salvo un’eccezione: quando è solo un post, che quindi genera un errore, ma non ho ancora visto di una risoluzione, ma mi pare che si può assegnare true al parametro isSingle (era un altro errore dovuto a prettyURLs).

Questo “trucco” non intacca le categorie perché non sono salvate e non ha ripercussioni sul widget categorie perché per mostrarle non usa fpdb.

Penso che volendo si possa usare solo un’unica classe (e ci proverò 😊 )

Ho provato e funziona.

Dimenticavo: bisogna usare gli entry key e non gli entry id: sono la stessa cosa solo che senza la parola entry e il trattino tra data e ora.

English

I was writing a tag plugin for flatpress and I didn’t know how to show entries by tag.

Thinking I found the answer: creating a fake category using a function in filter and when you need it.

Categories are managed by FPDB class and they are loaded once. Flatpress uses only one istance of FPDB.

This is the basic code:

class plugin_tag_walker {
	var $valid=true;
	function current_key() {
		return current($this->array);
	}
	function next() {
		$n=next($this->array);
		if($n==false)
			$this->valid=false;
		return $n;
	}
}

class plugin_tag_fpdb {
	function length() {
		return $this->len;
	}
	function walker($p1=null) {
		$a=new plugin_tag_walker();
		$a->array=$this->array;
		return $a;
	}
}

function prova_cat() {
	global $fpdb, $fp_params;
	if(isset($_GET['tag'])) {       ### Only when there is get parameter tag
		$fp_params['cat']=-50;  ### Fake category id
		$p=new plugin_tag_fpdb();
		$p->array=array(
			'100715151839', ### Entries to show
			'100715163207',
			'100808220001'
		);
		$p->len=count($p->array);
		if($fp_params['count']>$p->len)
			$fp_params['count']=$p->len;
		$fpdb->_indexer[$fp_params['cat']]=&$p;
	}
	return true;
}

add_filter('init', 'prova_cat');

prova_cat function assigns an object to category -50 and this object returns another object that is called by the flatpress code and returns entries id.

He checks if there are other entries to show.

It seems to work, also a category with used id exists but if the post is just one there is an error. I haven’t solved yet but I remember that it’s possible to assign true to isSingle parameter. (it’s another error, due to prettyURLs)

This “dodge” doesn’t make changes to existent categories and the categories widget works well because it doesn’t use fpdb.

I think it’s possible - and I’ll try - to use just one class.

I’ve tried and it’s possible

P.S.: you have to use entry keys instead of entry ids: keys are ids without entry word and without - between day and date.