Skip to content

Commit

Permalink
Merge pull request #96 from bordoni/fix/problematic-faker-optional
Browse files Browse the repository at this point in the history
Object Meta and Taxonomy problems
  • Loading branch information
bordoni authored Jun 14, 2016
2 parents 842e393 + 0a04c35 commit 1f6e20d
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 32 deletions.
2 changes: 1 addition & 1 deletion fakerpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: FakerPress
* Plugin URI: https://fakerpress.com
* Description: FakerPress is a clean way to generate fake data to your WordPress instalation, great for developers who need testing
* Version: 0.4.5
* Version: 0.4.6
* Author: Gustavo Bordoni
* Author URI: http://bordoni.me
* Text Domain: fakerpress
Expand Down
1 change: 0 additions & 1 deletion inc/class-fp-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,6 @@ public static function get_meta_types() {
$range->class = array();
$range->label = __( 'Range of possible numbers', 'fakerpress' );
$range->_min = 0;
$range->_max = 9;

$html[] = Field::wrapper( Field::type_range( $range, null, 'string' ), $range );
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight );
Expand Down
2 changes: 1 addition & 1 deletion inc/class-fp-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Plugin {
* @since 0.1.0
* @var string
*/
const version = '0.4.5';
const version = '0.4.6';

/**
* A static variable that holds a dinamic instance of the class
Expand Down
13 changes: 13 additions & 0 deletions modules/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ public function generate() {
return $this;
}

/**
* A method to make it easier to debug which variables will be actually saved
*
* @return array
*/
public function get_values() {
$values = array();
foreach ( $this->data as $name => $item ) {
$values[ $name ] = $this->data[ $name ]->value;
}
return $values;
}

protected function apply( $item ) {
if ( ! isset( $item->generator ) ){
return reset( $item->arguments );
Expand Down
3 changes: 2 additions & 1 deletion modules/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public function parse_request( $qty, $request = array() ) {
$this->set( 'ping_status' );
$this->set( 'tax_input', $taxonomies_configuration );

$post_id = $this->generate()->save();
$generated = $this->generate();
$post_id = $generated->save();

if ( $post_id && is_numeric( $post_id ) ){
foreach ( $metas as $meta_index => $meta ) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fakerpress",
"title": "FakerPress",
"version": "0.4.5",
"version": "0.4.6",
"homepage": "http://fakerpress.com/",
"repository": {
"type": "git",
Expand Down
42 changes: 21 additions & 21 deletions providers/wp-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private function meta_parse_separator( $separator ){
public function meta_type_numbers( $number = array( 0, 9 ), $weight = 50 ) {
$number = ( is_array( $number ) ? call_user_func_array( array( $this->generator, 'numberBetween' ), $number ) : $number );

return $this->generator->optional( $weight, null )->randomElement( (array) $number );
return $this->generator->optional( (int) $weight, null )->randomElement( (array) $number );
}

public function meta_type_elements( $elements = '', $qty = 1, $separator = ',', $weight = 50 ) {
Expand All @@ -47,7 +47,7 @@ public function meta_type_elements( $elements = '', $qty = 1, $separator = ',',
$elements = explode( ',', $elements );
$qty = $this->meta_parse_qty( $qty, $elements );

$value = $this->generator->optional( $weight, null )->randomElements( (array) $elements, $qty );
$value = $this->generator->optional( (int) $weight, null )->randomElements( (array) $elements, $qty );
if ( is_null( $value ) ) {
return $value;
}
Expand All @@ -56,23 +56,23 @@ public function meta_type_elements( $elements = '', $qty = 1, $separator = ',',
}

public function meta_type_letter( $weight = 50 ) {
return $this->generator->optional( $weight, null )->randomLetter();
return $this->generator->optional( (int) $weight, null )->randomLetter();
}

public function meta_type_words( $qty = 8, $weight = 50 ) {
$qty = $this->meta_parse_qty( $qty );

return $this->generator->optional( $weight, null )->sentence( $qty );
return $this->generator->optional( (int) $weight, null )->sentence( $qty );
}

public function meta_type_text( $type = 'sentences', $qty = 3, $separator = "\r\n\r\n", $weight = 50 ) {
$separator = $this->meta_parse_separator( $separator );
$qty = $this->meta_parse_qty( $qty );

if ( 'sentences' === $type ){
$value = $this->generator->optional( $weight, null )->sentences( $qty );
$value = $this->generator->optional( (int) $weight, null )->sentences( $qty );
} else {
$value = $this->generator->optional( $weight, null )->paragraphs( $qty );
$value = $this->generator->optional( (int) $weight, null )->paragraphs( $qty );
}

if ( is_null( $value ) ) {
Expand All @@ -86,7 +86,7 @@ public function meta_type_html( $elements, $qty = 6, $weight = 50 ) {
$qty = $this->meta_parse_qty( $qty );
$elements = explode( ',', $elements );

$value = $this->generator->optional( $weight, null )->html_elements( array(
$value = $this->generator->optional( (int) $weight, null )->html_elements( array(
'elements' => $elements,
'qty' => $qty,
) );
Expand All @@ -113,31 +113,31 @@ public function meta_type_wp_query( $query, $weight = 50 ) {
return null;
}

$value = $this->generator->optional( $weight, null )->randomElement( (array) $query->posts );
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) $query->posts );

return $value;
}

public function meta_type_lexify( $template, $weight = 50 ) {
$value = $this->generator->optional( $weight, null )->bothify( (string) $template );
$value = $this->generator->optional( (int) $weight, null )->bothify( (string) $template );

return $value;
}

public function meta_type_asciify( $template, $weight = 50 ) {
$value = $this->generator->optional( $weight, null )->asciify( (string) $template );
$value = $this->generator->optional( (int) $weight, null )->asciify( (string) $template );

return $value;
}

public function meta_type_regexify( $template, $weight = 50 ) {
$value = $this->generator->optional( $weight, null )->regexify( (string) $template );
$value = $this->generator->optional( (int) $weight, null )->regexify( (string) $template );

return $value;
}

public function meta_type_timezone( $weight = 50 ) {
$value = $this->generator->optional( $weight, null )->timezone;
$value = $this->generator->optional( (int) $weight, null )->timezone;

return $value;
}
Expand Down Expand Up @@ -177,7 +177,7 @@ public function meta_type_company( $template, $weight = 50 ) {
}
}

$value = $this->generator->optional( $weight, null )->randomElement( (array) implode( '', $text ) );
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) implode( '', $text ) );

return $value;
}
Expand Down Expand Up @@ -217,7 +217,7 @@ public function meta_type_person( $template, $gender = 'female', $weight = 50 )
}
}

$value = $this->generator->optional( $weight, null )->randomElement( (array) implode( '', $text ) );
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) implode( '', $text ) );

return $value;
}
Expand Down Expand Up @@ -304,7 +304,7 @@ public function meta_type_geo( $template, $weight = 50 ) {
}
}

$value = $this->generator->optional( $weight, null )->randomElement( (array) implode( '', $text ) );
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) implode( '', $text ) );

return $value;
}
Expand Down Expand Up @@ -340,37 +340,37 @@ public function meta_type_date( $interval, $format = 'Y-m-d H:i:s', $weight = 50

$selected = $this->generator->dateTimeBetween( (string) $min, (string) $max )->format( $format );

$value = $this->generator->optional( $weight, null )->randomElement( (array) $selected );
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) $selected );

return $value;
}

public function meta_type_ip( $weight = 50 ) {
$value = $this->generator->optional( $weight, null )->ipv4;
$value = $this->generator->optional( (int) $weight, null )->ipv4;

return $value;
}

public function meta_type_domain( $weight = 50 ) {
$value = $this->generator->optional( $weight, null )->domainName;
$value = $this->generator->optional( (int) $weight, null )->domainName;

return $value;
}

public function meta_type_email( $weight = 50 ) {
$value = $this->generator->optional( $weight, null )->email;
$value = $this->generator->optional( (int) $weight, null )->email;

return $value;
}

public function meta_type_user_agent( $weight = 50 ) {
$value = $this->generator->optional( $weight, null )->userAgent;
$value = $this->generator->optional( (int) $weight, null )->userAgent;

return $value;
}

public function meta_type_raw( $weight = 100, $value = null, $default = null ) {
if ( $weight >= $this->generator->numberBetween( 0, 100 ) ) {
if ( (int) $weight >= $this->generator->numberBetween( 0, 100 ) ) {
return $value;
} else {
return $default;
Expand Down
2 changes: 1 addition & 1 deletion providers/wp-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function tax_input( $config = null ) {
}

// Select the elements based on qty
$output[ $taxonomy ] = $this->generator->optional( $rate, null )->randomElements( $terms, $qty );
$output[ $taxonomy ] = $this->generator->optional( (int) $rate, null )->randomElements( $terms, (int) $qty );
}
}

Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Thank you for wanting to make FakerPress better for everyone! [We salute you](ht

## Changelog ##

### 0.4.6 — 14 of June, 2016 ###
* Fix: Post Meta and taxonomy is finally working again — Thanks [@peachey_a](https://wordpress.org/support/topic/generated-posts-not-assigned-categories) and [@zoeitsolutions](https://wordpress.org/support/topic/user-meta-not-being-generated)
* Fix: Allow Meta Number generation using any type of range, doesn't limit from 0 to 9

### 0.4.5 — 11 of June, 2016 ###
* Fix: Users Module was using Post Meta methods on flag related methods, preventing the users to be deleted when "Let it Go!" — Thanks [@derpixler](https://github.com/bordoni/fakerpress/issues/84)
* Fix: Taxonomy and Meta Modules had a compatibility problem with Faker, preveting users to use Meta and Taxonomy Properly — Thanks [@rayrutjes](https://github.com/bordoni/fakerpress/issues/94)
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Thank you for wanting to make FakerPress better for everyone! [We salute you](ht

== Changelog ==

= 0.4.6 — 14 of June, 2016 =
* Fix: Post Meta and taxonomy is finally working again — Thanks [@peachey_a](https://wordpress.org/support/topic/generated-posts-not-assigned-categories) and [@zoeitsolutions](https://wordpress.org/support/topic/user-meta-not-being-generated)
* Fix: Allow Meta Number generation using any type of range, doesn't limit from 0 to 9

= 0.4.5 — 11 of June, 2016 =
* Fix: Users Module was using Post Meta methods on flag related methods, preventing the users to be deleted when "Let it Go!" — Thanks [@derpixler](https://github.com/bordoni/fakerpress/issues/84)
* Fix: Taxonomy and Meta Modules had a compatibility problem with Faker, preveting users to use Meta and Taxonomy Properly — Thanks [@rayrutjes](https://github.com/bordoni/fakerpress/issues/94)
Expand Down
12 changes: 8 additions & 4 deletions ui/js/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ if ( 'undefined' === typeof window.fakerpress ){
attrClass = 'notice is-dismissible ' + attrClass;
}

console.log( data, html );

var noticeContent = _.template( html )( data ),
templateVars = {
'attrClass': attrClass,
Expand Down Expand Up @@ -66,13 +68,15 @@ if ( 'undefined' === typeof window.fakerpress ){
$spinner.removeClass( 'is-active' );
$button.prop( 'disabled', false );

fp.log( $response, '<%= message %>', { message: jqXHR.responseText }, 'notice-warning' );
fp.log( $response, '<%= message %>', { message: jqXHR.responseText }, 'notice-error' );
}
},
success: function( data, textStatus, jqXHR ) {
$spinner.removeClass( 'is-active' );

if ( null !== data && data.status ){
if ( null === data ) {
$button.prop( 'disabled', false );
fp.log( $response, '<%= message %>', { message: jqXHR.responseText }, 'notice-error' );
} else if ( data.status ){
if ( data.is_capped && data.offset < data.total ){
_POST.offset = data.offset;
_POST.total = data.total;
Expand All @@ -85,7 +89,7 @@ if ( 'undefined' === typeof window.fakerpress ){
fp.log( $response, 'Faked <%= total %>: <%= message %>', { message: data.message, total: data.results.length }, 'notice-success' );
} else {
$button.prop( 'disabled', false );
fp.log( $response, '<%= message %>', data, 'notice-warning' );
fp.log( $response, '<%= message %>', data, 'notice-error' );
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion ui/js/module.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f6e20d

Please sign in to comment.