Introduction

Avec WordPressMu, il devient très facile d'ouvrir une plateforme de blogs.

Mais il n'y a pas beaucoup d'intérêt à ouvrir une énième plateforme si elle propose uniquement la version standard.

Il faut alors personnaliser le thème du portail, et puis y ajouter des fonctions spécifiques.

La phase d'inscription et de création d'un nouveau blog

La version officielle

La version de référence est WPmu 1.2.5a

La création se déroule normalement en 3 étapes:

  • CREATION DE L'UTILISATEUR: Le nom de l'utilisateur et le mail
  • CREATION DU BLOG: Le nom du blog, son titre et le choix d'un site public ou privé
  • ACTIVATION DE L'UTILISATEUR: si toutes les informations sont valides, un mail est envoyé à l'utilisateur pour activer son site. Il contient un lien avec une clé d'activation.

Une version simplifiée

La phase d'activation est une bonne sécurité pour éviter la création automatique de splogs.

Mais il peut être jugé trop lourd par les vrais utilisateurs et certains abandonneront avant d'activer leur site.

De plus, les systèmes anti-spam des webmails peuvent dérouter le mail d'activation.

Il faut donc une version de la procédure d'activation qui soit immédiate: le site doit être actif à la fin de l'inscription.

Il faut toutefois garder une sécurisation pour éviter les splogs automatiques.

Tout en un

On peut sur un seul formulaire demander toutes les informations nécessaires:

  • NOM de l'utilisateur
  • MAIL de l'utilisateur
  • ADRESSE ET TITRE du site
  • choix d'un site PUBLIC ou PRIVE

La partie Technique

Le code impliqué

Le fichier wp-signup.php

Le fichier wp-activate.php

Ce fichier appelle essentiellement la fonction wpmu_activate_signup avec la valeur de la clé fournie.

	$result = wpmu_activate_signup($key);

Cette fonction est définie dans le fichier wp-includes/wpmu-functions.php

function wpmu_activate_signup($key) {
	global $wpdb;
 
	$result = array();
	$signup = $wpdb->get_row("SELECT * FROM $wpdb->signups WHERE activation_key = '$key'");
 
	if ( empty($signup) )
		return new WP_Error('invalid_key', __('Invalid activation key.'));
 
	if ( $signup->active )
		return new WP_Error('already_active', __('The blog is already active.'), $signup);
 
	$meta = unserialize($signup->meta);
	$user_login = $wpdb->escape($signup->user_login);
	$user_email = $wpdb->escape($signup->user_email);
	wpmu_validate_user_signup($user_login, $user_email);
	$password = generate_random_password();
 
	$user_id = username_exists($user_login);
 
	if ( ! $user_id )
		$user_id = wpmu_create_user($user_login, $password, $user_email);
	else
		$user_already_exists = true;
 
	if ( ! $user_id )
		return new WP_Error('create_user', __('Could not create user'), $signup);
 
	$now = current_time('mysql', true);
 
	if ( empty($signup->domain) ) {
		$wpdb->query("UPDATE $wpdb->signups SET active = '1', activated = '$now' WHERE activation_key = '$key'");
		if ( isset($user_already_exists) )
			return new WP_Error('user_already_exists', __('That username is already activated.'), $signup);
		wpmu_welcome_user_notification($user_id, $password, $meta);
		add_user_to_blog('1', $user_id, 'subscriber');
		do_action('wpmu_activate_user', $user_id, $password, $meta);
		return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta);
	}
 
	wpmu_validate_blog_signup($signup->domain, $signup->title);
	$blog_id = wpmu_create_blog($signup->domain, $signup->path, $signup->title, $user_id, $meta);
 
	// TODO: What to do if we create a user but cannot create a blog?
	if ( is_wp_error($blog_id) ) {
		// If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
		// setting the activation flag.  Let's just set the active flag and instruct the user to reset their password.
		if ( 'blog_taken' == $blog_id->get_error_code() ) {
			$blog_id->add_data($signup);
			$wpdb->query("UPDATE $wpdb->signups SET active = '1', activated = '$now' WHERE activation_key = '$key'");
			error_log("Blog $blog_id failed to complete activation.", 0);	
		}
 
		return $blog_id;
	}
 
	$wpdb->query("UPDATE $wpdb->signups SET active = '1', activated = '$now' WHERE activation_key = '$key'");
 
	wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
 
	do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta);
 
	return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
}

Le fichier wp-includes/wpmu-functions.php

La fonction wpmu_signup_blog

// Record signup information for future activation. wpmu_validate_signup() should be run
// on the inputs before calling wpmu_signup().
function wpmu_signup_blog($domain, $path, $title, $user, $user_email, $meta = '') {
	global $wpdb;
 
	$key = substr( md5( time() . rand() . $domain ), 0, 16 );
	$registered = current_time('mysql', true);
	$meta = serialize($meta);
	$domain = $wpdb->escape($domain);
	$path = $wpdb->escape($path);
	$title = $wpdb->escape($title);
	$wpdb->query( "INSERT INTO $wpdb->signups ( domain, path, title, user_login, user_email, registered, activation_key, meta )
					VALUES ( '$domain', '$path', '$title', '$user', '$user_email', '$registered', '$key', '$meta' )" );
 
	wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
}

la fonction wpmu_validate_blog_signup

function wpmu_validate_blog_signup($blog_id, $blog_title, $user = '') {
	global $wpdb, $domain, $base;
 
	$blog_id = sanitize_user( $blog_id );
	$blog_title = strip_tags( $blog_title );
 
	$errors = new WP_Error();
	$illegal_names = get_site_option( "illegal_names" );
	if( $illegal_names == false ) {
	    $illegal_names = array( "www", "web", "root", "admin", "main", "invite", "administrator" );
	    add_site_option( "illegal_names", $illegal_names );
	}
 
	if ( empty( $blog_id ) )
	    $errors->add('blog_id', __("Please enter a blog name"));
 
	preg_match( "/[a-zA-Z0-9]+/", $blog_id, $maybe );
	if( $blog_id != $maybe[0] ) {
	    $errors->add('blog_id', __("Only letters and numbers allowed"));
	}
	if( in_array( $blog_id, $illegal_names ) == true ) {
	    $errors->add('blog_id',  __("That name is not allowed"));
	}
	if( strlen( $blog_id ) < 4 ) {
	    $errors->add('blog_id',  __("Blog name must be at least 4 characters"));
	}
 
	if ( strpos( " " . $blog_id, "_" ) != false )
		$errors->add('blog_id', __("Sorry, blog names may not contain the character '_'!"));
 
	// all numeric?
	preg_match( '/[0-9]*/', $blog_id, $match );
	if ( $match[0] == $blog_id )
		$errors->add('blog_id', __("Sorry, blog names must have letters too!"));
 
	$blog_id = apply_filters( "newblog_id", $blog_id );
 
	$blog_title = stripslashes(  $blog_title );
 
	if ( empty( $blog_title ) )
	    $errors->add('blog_title', __("Please enter a blog title"));
 
	// Check if the domain/path has been used already.
	if( constant( "VHOST" ) == 'yes' ) {
		$mydomain = "$blog_id.$domain";
		$path = $base;
	} else {
		$mydomain = "$domain";
		$path = $base.$blog_id.'/';
	}
	if ( domain_exists($mydomain, $path) )
		$errors->add('blog_id', __("Sorry, that blog already exists!"));
 
	if ( username_exists($blog_id) ) {
		if  ( !is_object($user) && ( $user->user_login != $blog_id ) )
			$errors->add('blog_id', __("Sorry, that blog is reserved!"));
	}
 
	// Has someone already signed up for this domain?
	// TODO: Check email too?
	$signup = $wpdb->get_row("SELECT * FROM $wpdb->signups WHERE domain = '$mydomain' AND path = '$path'");
	if ( ! empty($signup) ) {
		$registered_at =  mysql2date('U', $signup->registered);
		$now = current_time( 'timestamp', true );
		$diff = $now - $registered_at;
		// If registered more than two days ago, cancel registration and let this signup go through.
		if ( $diff > 172800 ) {
			$wpdb->query("DELETE FROM $wpdb->signups WHERE domain = '$mydomain' AND path = '$path'");
		} else {
			$errors->add('blog_id', __("That blog is currently reserved but may be available in a couple days."));
		}
	}
 
	$result = array('domain' => $mydomain, 'path' => $path, 'blog_id' => $blog_id, 'blog_title' => $blog_title,
				'errors' => $errors);
 
	return apply_filters('wpmu_validate_blog_signup', $result);
}

API: wpmu_create_blog

Cette fonction est définie dans le fichier wp-includes/wpmu-functions.php

function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
	$domain = sanitize_user( $domain );
	$title = strip_tags( $title );
	$user_id = (int) $user_id;
 
	if( empty($path) )
		$path = '/';
 
	// Check if the domain has been used already. We should return an error message.
	if ( domain_exists($domain, $path, $site_id) )
		return new WP_Error('blog_taken', __('Blog already exists.'));
 
	if ( !defined("WP_INSTALLING") )
		define( "WP_INSTALLING", true );
 
	if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
		return new WP_Error('insert_blog', __('Could not create blog.'));
 
	switch_to_blog($blog_id);
 
	install_blog($blog_id, $title);
 
	install_blog_defaults($blog_id, $user_id);
 
	add_user_to_blog($blog_id, $user_id, 'administrator');
 
	restore_current_blog();
 
	if ( is_array($meta) ) foreach ($meta as $key => $value) {
		update_blog_status( $blog_id, $key, $value );
		update_blog_option( $blog_id, $key, $value );
	}
 
	do_action( 'wpmu_new_blog', $blog_id, $user_id );
 
	return $blog_id;
}

La fonction est utilisée dans seulement quelques contextes (ce qui est plutôt sain!):

./wp-includes/wpmu-functions.php:1188:  $blog_id = wpmu_create_blog($signup->domain, $signup->path, $signup->title, $user_id, $meta);
./wp-includes/wpmu-functions.php:1237:function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
./wp-admin/wpmu-edit.php:145:                   $blog_id = wpmu_create_blog($newdomain, $path, wp_specialchars( $blog['title'] ), $user_id ,'', $current_site->id);
./wp-signup.php:209:    wpmu_create_blog($domain, $path, $blog_title, $current_user->id, $meta);
./index-install.php:402:        wpmu_create_blog( $domain, $base, $weblog_title, $user_id, array() );
 
wordpressmu/customization.txt · Dernière modification: 2007/10/16 16:49 (modification externe) microWebAgency.com
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki