Skip to content

Commit

Permalink
Merge pull request #6 from iryz/feature/taxonomy_provider
Browse files Browse the repository at this point in the history
Provider and Module for Terms - close #1
  • Loading branch information
bordoni committed Jun 14, 2014
2 parents 002556c + d9a9237 commit 2fb713c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 30 deletions.
3 changes: 3 additions & 0 deletions inc/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
// Require the Post module
require_once Plugin::path( 'modules/post.php' );

// Require the Post module
require_once Plugin::path( 'modules/term.php' );

// Require the Comment module
require_once Plugin::path( 'modules/comment.php' );

Expand Down
1 change: 1 addition & 0 deletions modules/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// Inluding needed providers
include_once \Fakerpress\Plugin::path( 'providers/html.php' );
include_once \Fakerpress\Plugin::path( 'providers/wp-term.php' );
include_once \Fakerpress\Plugin::path( 'providers/post.php' );
include_once \Fakerpress\Plugin::path( 'providers/comment.php' );

Expand Down
30 changes: 0 additions & 30 deletions modules/taxonomies.php

This file was deleted.

19 changes: 19 additions & 0 deletions modules/term.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace FakerPress\Module;

class Term extends Base {

public $dependencies = array(
'\Faker\Provider\Lorem',
);

public $provider = '\Faker\Provider\WP_Term';

public function save() {
$args = array(
'description' => $this->description,
'parent' => $this->parent_term,
);
return wp_insert_term( $this->name, $this->taxonomy, $args );
}
}
75 changes: 75 additions & 0 deletions providers/wp-term.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace Faker\Provider;

class WP_Term extends Base {
public function name( $min = 1, $max = 8 ) {
if ( is_array( $min ) ){
$name = $this->generator->randomElement( $min );
} else {
// Not sure if this is the best approach, but it will work no metter what...
if ( ! is_numeric( $min ) ){
$min = 3;
}
if ( ! is_numeric( $max ) ){
$max = 10;
}
$name = $this->generator->sentence( $this->generator->numberBetween( $min, $max ) );

// This removes the last dot on the end of the sentence
$name = substr( $name, 0, strlen( $name ) - 1 );
}

return $name;
}

public function taxonomy( $taxonomies = array( 'category', 'post_tag' ), $args = array() ){
if ( empty( $taxonomies ) ){
// Merge the returned terms to those provided
$taxonomies = get_taxonomies( $args, 'names' );
}

return $this->generator->randomElement( (array) $taxonomies );
}

public function description( $min = 5, $max = 50 ){
if ( is_array( $min ) ){
$description = $this->generator->randomElement( $min );
} else {
// Not sure if this is the best approach, but it will work no metter what...
if ( ! is_numeric( $min ) ){
$min = 5;
}
if ( ! is_numeric( $max ) ){
$max = 50;
}
$description = $this->generator->sentence( $this->generator->numberBetween( $min, $max ) );

// This removes the last dot on the end of the sentence
$description = substr( $description, 0, strlen( $description ) - 1 );
}

return $description;
}

public function parent_term( $terms = array(), $taxonomies = array(), $args = array() ){
if ( ! empty( $taxonomies ) ){
// We only need the ids to be returned
$args['fields'] = 'ids';

// Merge the returned terms to the one provided
$terms = array_merge( (array) $terms, get_terms( $taxonomies, $args ) );
}

return $this->generator->randomElement( (array) $terms );
}


// For now I think we should omit the slug, since it's auto-gen, but we need to figure a way to do it later
/*
public function slug(){
return $slug;
}
*/

}

0 comments on commit 2fb713c

Please sign in to comment.