-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from iryz/feature/taxonomy_provider
Provider and Module for Terms - close #1
- Loading branch information
Showing
5 changed files
with
98 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
*/ | ||
|
||
} |