Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
Add custom random name generator (#84)
Browse files Browse the repository at this point in the history
* Generate word combinations between all the available adjectives and sustantives, not just alliterations

* Add setting for alliteration-only subdomains

* Factor out CustomNameGenerator into own file

* Add custom set of adjectives and nouns

* Add more nouns

* Add more adjectives

* Bump plugin version to 2.0
  • Loading branch information
oskosk authored Feb 7, 2018
1 parent db42a1a commit 8e95edc
Show file tree
Hide file tree
Showing 6 changed files with 1,917 additions and 5 deletions.
2 changes: 1 addition & 1 deletion jurassic.ninja.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* Plugin Name: Jurassic Ninja
* Description: Launch ephemeral instances of WordPress + Jetpack using ServerPilot and an Ubuntu Box.
* Version: 1.0
* Version: 2.0
* Author: Osk
**/

Expand Down
24 changes: 24 additions & 0 deletions lib/class-customnamegenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace jn;

if ( ! defined( '\\ABSPATH' ) ) {
exit;

}
class CustomNameGenerator extends \Nubs\RandomNameGenerator\Alliteration {
// Taken from \Nubs\RandomNameGenerator\Alliteration::_construct
public function __construct( Randomizer $randomizer = null ) {
$this->_randomizer = $randomizer;
$this->_adjectives = file( __DIR__ . '/words/adjectives.txt', FILE_IGNORE_NEW_LINES );
$this->_nouns = file( __DIR__ . '/words/nouns.txt', FILE_IGNORE_NEW_LINES );
}
// Based on \Nubs\RandomNameGenerator\Alliteration::getName
public function getName( $alliteration = true ) {
$adjective = $this->_getRandomWord( $this->_adjectives );
$noun = $alliteration ?
$this->_getRandomWord( $this->_nouns, $adjective[0] ) :
$this->_getRandomWord( $this->_nouns );
return ucwords( "{$adjective} {$noun}" );
}
}
7 changes: 7 additions & 0 deletions lib/settings-stuff.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ function add_settings_page() {
'type' => 'checkbox',
'checked' => false,
),
'use_alliterations_for_subdomain' => array(
'id' => 'use_alliterations_for_subdomain',
'title' => __( 'Base the site domain just on alliterations', 'jurassic-ninja' ),
'text' => __( 'Example: ugly-unicorn.domain and not fancy-unicorn.domain.', 'jurassic-ninja' ),
'type' => 'checkbox',
'checked' => true,
),
'add_jetpack_by_default' => array(
'id' => 'add_jetpack_by_default',
'title' => __( 'Add Jetpack to every launched WordPress', 'jurassic-ninja' ),
Expand Down
12 changes: 8 additions & 4 deletions lib/stuff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
if ( ! class_exists( 'RationalOptionPages' ) ) {
require_once __DIR__ . '/RationalOptionPages.php';
}
if ( ! class_exists( 'CustomNameGenerator' ) ) {
require_once __DIR__ . '/class-customnamegenerator.php';
}
require_once __DIR__ . '/rest-api-stuff.php';

define( 'REST_API_NAMESPACE', 'jurassic.ninja' );
Expand Down Expand Up @@ -415,14 +418,15 @@ function generate_random_subdomain() {
'syrian',
];
// Filter out some words that could lead to offensive combinations
$MAX_ATTEMPTS = 10;
$max_attempts = 10;
$regexp = implode( '|', $blacklisted_words );
$name = 'First try';
$i = 0;

do {
$generator = new \Nubs\RandomNameGenerator\Alliteration();
$name = $generator->getName();
} while( $i++ < $MAX_ATTEMPTS && preg_match("($regexp)", $name ) === 1 );
$generator = new CustomNameGenerator();
$name = $generator->getName( settings( 'use_alliterations_for_subdomain', true ) );
} while ( $i++ < $max_attempts && preg_match( "($regexp)", $name ) === 1 );

$slug = create_slug( $name );
return $slug;
Expand Down
Loading

0 comments on commit 8e95edc

Please sign in to comment.