Automatická generácia SLUG bbPress
Tento jednoduchý plugin by mal pomôcť všetkým, ktorý sa rozhodli prejsť na bbPress a pritom sa snažia o migráciu z iného riešenia.
Nie som autorom pluginu, ale rozhodol som sa prípadnych záujemcov poistiť: http://www.simonwheatley.co.uk/2009/07/03/migration-to-bbpress-fix-topic-slugs/ Migrácia z phpBB bola hotovým utrpením, a funkčné SEO odkazy boli pre mňa zásadným faktorom. A keďže som chcel v čo najmenšom možnom množstve eliminovať stratu údajov, museli začať hodiny záloh a úprav v DB. Vopred upozrňujem, že prechod z iného riešenia (u mňa phpBB) može byť utrpením o to viac, ak sa snažíte o integráciu s WordPressom. Mňa zachránilo hľadanie a nájdenie pluginu…
Autorom pluginu je simonwheatley, ktorému týmto ďakujem. ( Thanks man! )
Tu pre záujemcov, ktorý sa dostali až sem poskytujem náprava vo forme pluginu. Postup je jednoduchý:
- Vytvoriť súbor s ľubovolným názvom v priečinku my-plugins
- Skopírovať php kód, ktorý je uvedený nižšie do vytvoreného súboru
- uložiť
- aktivovať plugin Creatre Slugs
- Vymazať vami vytovrený súbor
<?php
/*
Plugin Name: Create Slugs
Description: Creates topic and forum slugs where previously there weren't any. Outputs into error log, only works on plugin activation.
Plugin URI: http://simonwheatley.co.uk/bbpress/create-slugs
Author: simonwheatley
Author URI: http://simonwheatley.co.uk
Version: 1.1
*/
class CreateSlugs
{
protected $topics_have_slugs;
protected $forums_have_slugs;
function __construct()
{
global $bbdb;
$this->db = & $bbdb;
$this->topics_have_slugs = false;
$this->forums_have_slugs = false;
}
public function create_slugs()
{
$this->create_topic_slugs();
if ( ! $this->topics_have_slugs ) return;
$this->create_forum_slugs();
if ( ! $this->forums_have_slugs ) return;
// Finished!
echo "Everything now has slugs, you can disable the create topic slugs plugin by removing it from the plugins directory.";
exit;
}
protected function maybe_finished()
{
// if ( ! $this->forums_have_slugs ) return;
}
protected function create_topic_slugs()
{
error_log( "TOPICS" );
$topics = $this->slugless( 'topics' );
if ( ! count( $topics ) ) $this->topics_have_slugs = true;
foreach( $topics AS $topic ) {
$new_slug = $this->generate_slug( 'topic', $topic->topic_id, $topic->topic_title );
$this->update_topic_slug( $topic->topic_id, $new_slug );
error_log( "$new_slug >> $topic->topic_title" );
}
error_log( "Last Query: " . $this->db->last_query );
}
protected function create_forum_slugs()
{
error_log( "FORUMS" );
$forums = $this->slugless( 'forums' );
if ( ! count( $forums ) ) $this->forums_have_slugs = true;
foreach( $forums AS $forum ) {
$new_slug = $this->generate_slug( 'forum', $forum->forum_id, $forum->forum_name );
$this->update_forum_slug( $forum->forum_id, $new_slug );
error_log( "$new_slug >> $forum->forum_name" );
}
}
protected function slugless( $type )
{
// Just do 1,000 at a time for now
if ( $type == 'topics' ) {
$topics_table = $this->db->topics;
$sql = " SELECT topic_id, topic_title FROM $topics_table WHERE topic_slug IS NULL OR topic_slug = '' LIMIT 1000 ";
}
if ( $type == 'forums' ) {
$forums_table = $this->db->forums;
$sql = " SELECT forum_id, forum_name FROM $forums_table WHERE forum_slug IS NULL OR forum_slug = '' LIMIT 1000 ";
}
// No need to prepared the SQL, no user input
return $this->db->get_results( $sql );
}
protected function generate_slug( $type, $id, $title )
{
$slug = $_slug = bb_slug_sanitize( wp_specialchars_decode( $title, ENT_QUOTES ) );
if ( strlen( $_slug ) < 1 ) $slug = $_slug = '0';
// Ensure the slug is unique
while ( is_numeric( $slug ) || $existing_slug = $this->existing_slug( $type, $id, $slug ) ) {
$slug = bb_slug_increment( $_slug, $existing_slug );
}
return $slug;
}
protected function existing_slug( $type, $id, $slug )
{
if ( $type == 'topic' ) {
$table = $this->db->topics;
$sql = "SELECT topic_slug FROM $table WHERE topic_slug = %s AND topic_id != %d";
}
if ( $type == 'forum' ) {
$table = $this->db->forums;
$sql = "SELECT forum_slug FROM $table WHERE forum_slug = %s AND forum_id != %d";
}
$prepared_sql = $this->db->prepare( $sql, $slug, $id );
return $this->db->get_var( $prepared_sql );
}
protected function update_topic_slug( $topic_id, $topic_slug )
{
$table = $this->db->topics;
$data = array( 'topic_slug' => $topic_slug );
$where = array( 'topic_id' => $topic_id );
$this->db->update( $table, $data, $where );
}
protected function update_forum_slug( $forum_id, $forum_slug )
{
$table = $this->db->forums;
$data = array( 'forum_slug' => $forum_slug );
$where = array( 'forum_id' => $forum_id );
$this->db->update( $table, $data, $where );
}
}
$create_slugs = new CreateSlugs();
add_action( 'bb_init', array( & $create_slugs, 'create_slugs' ) );
?>
Po útrapách, ktoré som musel s bbPress zažiť, odporúčam každému sa tomuto kusu H**** vyhnúť. Ako dobrý je WordPress, tak zlý je bbPress. Uvidíme či sa niečo na tomto fakte zmení, ale vzhľadom na rýchlosť vývoja a svojráznosť komunity tomu veľké šance nedávam. To už skôr vlastné riešenie…
Podobné pravidlo platí aj pre BuddyPress. Vývojári týchto aplikácií by sa mali poriadne zamyslieť a popracovať na nich. Za ten čas, čo som zabil s nastavovaním a ladením niečoho, čo nakoniec aj tak nešlo alebo sa na záver rozsypalo som mohol začať robiť zhola novú a vlastnú aplikáciu…