Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created a field to control the HTML from the Post #37

Merged
merged 4 commits into from
Aug 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions inc/init/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function( $view ) {

$taxonomies = array_intersect( get_taxonomies( array( 'public' => true ) ), array_map( 'trim', explode( ',', Filter::super( INPUT_POST, 'fakerpress_taxonomies', FILTER_SANITIZE_STRING ) ) ) );

$post_content_use_html = Filter::super( INPUT_POST, 'fakerpress_post_content_use_html', FILTER_SANITIZE_STRING, 'off' ) === 'on';
$post_content_html_tags = array_map( 'trim', explode( ',', Filter::super( INPUT_POST, 'fakerpress_post_content_html_tags', FILTER_SANITIZE_STRING ) ) );

$post_parents = array_map( 'trim', explode( ',', Filter::super( INPUT_POST, 'fakerpress_post_parents', FILTER_SANITIZE_STRING ) ) );

if ( $quantity === 0 ){
Expand All @@ -43,6 +46,7 @@ function( $view ) {
'post_status' => array( array( 'publish' ) ),
'post_date' => array( $min_date, $max_date ),
'post_parent' => array( $post_parents ),
'post_content' => array( $post_content_use_html, array( 'elements' => $post_content_html_tags ) ),
'post_type' => array( 'post' ),
'post_author' => array( $post_author ),
'post_type' => array( $post_types ),
Expand Down
2 changes: 1 addition & 1 deletion modules/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Post extends Base {
public $dependencies = array(
'\Faker\Provider\Lorem',
'\Faker\Provider\DateTime',
'\Faker\Provider\Html',
'\Faker\Provider\HTML',
);

public $provider = '\Faker\Provider\WP_Post';
Expand Down
28 changes: 13 additions & 15 deletions providers/html.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?php
namespace Faker\Provider;

class Html extends Base {
class HTML extends Base {
/**
* @param \Faker\Generator $generator
*/
public function __construct( \Faker\Generator $generator ) {
$this->generator = $generator;

self::$sets = (object) self::$sets;

$provider = new Internet( $this->generator );
$this->generator->addProvider( $provider );
}

static private $sets = array(
static public $sets = array(
'self_close' => array( 'img', 'hr', 'submit' ),
'header' => array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ),
'list' => array( 'ul', 'ol' ),
'block' => array( 'div', 'p' ),
'block' => array( 'div', 'p', 'blockquote' ),
'item' => array( 'li' ),
'inline' => array(
'b', 'big', 'i', 'small', 'tt',
Expand All @@ -33,7 +31,7 @@ public function html_elements( $args = array() ){

$defaults = array(
'qty' => Base::randomNumber( 5, 25 ),
'elements' => array_merge( self::$sets->header, self::$sets->list, self::$sets->block ),
'elements' => array_merge( self::$sets['header'], self::$sets['list'], self::$sets['block'] ),
'attr' => array(),
'exclude' => array( 'div' ),
);
Expand All @@ -44,10 +42,10 @@ public function html_elements( $args = array() ){
if ( isset( $element ) ){
// Here we check if we need to exclude some elements from the next
// This one is to exclude header elements from apearing one after the other, or in the end of the string
if ( in_array( $element, self::$sets->header ) || $args->qty - 1 === $i ){
$exclude = array_merge( (array) $exclude, self::$sets->header );
} elseif ( $i > 1 && ( in_array( $els[$i - 1], self::$sets->list ) || in_array( $els[$i - 2], self::$sets->list ) ) ) {
$exclude = array_merge( (array) $exclude, self::$sets->list );
if ( in_array( $element, self::$sets['header'] ) || $args->qty - 1 === $i ){
$exclude = array_merge( (array) $exclude, self::$sets['header'] );
} elseif ( $i > 1 && ( in_array( $els[$i - 1], self::$sets['list'] ) || in_array( $els[$i - 2], self::$sets['list'] ) ) ) {
$exclude = array_merge( (array) $exclude, self::$sets['list'] );
}
}
$els[] = $element = Base::randomElement( array_diff( $args->elements, $exclude ) );
Expand Down Expand Up @@ -107,7 +105,7 @@ public function element( $name = 'div', $attr = array(), $text = null ){
'attr' => $attr,
);

$element->one_liner = in_array( $element->name, self::$sets->self_close );
$element->one_liner = in_array( $element->name, self::$sets['self_close'] );

$html = array();

Expand All @@ -129,17 +127,17 @@ public function element( $name = 'div', $attr = array(), $text = null ){

if ( ! is_null( $text ) ){
$html[] = $text;
} elseif ( in_array( $element->name, self::$sets->inline ) ){
} elseif ( in_array( $element->name, self::$sets['inline'] ) ){
$text = Lorem::text( Base::randomNumber( 5, 25 ) );
$html[] = substr( $text, 0, strlen( $text ) - 1 );
} elseif ( in_array( $element->name, self::$sets->item ) ){
} elseif ( in_array( $element->name, self::$sets['item'] ) ){
$text = Lorem::text( Base::randomNumber( 10, 60 ) );
$html[] = substr( $text, 0, strlen( $text ) - 1 );
} elseif ( in_array( $element->name, self::$sets->list ) ){
} elseif ( in_array( $element->name, self::$sets['list'] ) ){
for ( $i = 0; $i < Base::randomNumber( 1, 15 ); $i++ ) {
$html[] = $this->element( 'li' );
}
} elseif ( in_array( $element->name, self::$sets->header ) ){
} elseif ( in_array( $element->name, self::$sets['header'] ) ){
$text = Lorem::text( Base::randomNumber( 60, 200 ) );
$html[] = substr( $text, 0, strlen( $text ) - 1 );
} else {
Expand Down
7 changes: 4 additions & 3 deletions providers/wp-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public function post_title( $qty_words = 5 ) {
return $title;
}

public function post_content( $html = true ) {
public function post_content( $html = true, $args = array() ) {
if ( $html === true ){
$content = implode( "\n", $this->generator->html_elements() );
$content = implode( "\n", $this->generator->html_elements( $args ) );
} else {
$content = implode( "\r\n\r\n", $this->generator->paragraphs( $this->generator->randomDigitNotNull() ) );
}
Expand Down Expand Up @@ -51,7 +51,8 @@ public function tax_input( $taxonomies = null, $range = array( 1, 6 ) ) {

public function post_type( $haystack = array() ){
if ( empty( $haystack ) ){
$haystack = get_post_types( array(), 'names' );
// Later on we will remove the Attachment rule
$haystack = array_diff( get_post_types( array( 'public' => true, 'show_ui' => true ), 'names' ), array( 'attachment' ) );
}

return $this->generator->randomElement( (array) $haystack );
Expand Down
61 changes: 58 additions & 3 deletions ui/js/fields.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Simple Select2 Fields
( function( $ ){
( function( $, _ ){
'use strict';
$(document).ready(function(){
$( '.field-select2-simple' ).each(function(){
Expand Down Expand Up @@ -37,7 +37,24 @@
$select.select2({ width: 200 });
});
});
}( jQuery ) );
}( window.jQuery, window._ ) );

// Tagged Select2 Fields
( function( $, _ ){
'use strict';
$(document).ready(function(){
$( '.field-select2-tags' ).each(function(){
var $select = $(this);

$select.select2({
multiple: true,
width: 400,
tags: $select.data('tags'),
tokenSeparators: [',']
});
});
});
}( window.jQuery, window._ ) );

// Date Fields
( function( $ ){
Expand Down Expand Up @@ -182,7 +199,45 @@
return;
}
$field.data( 'post_parent', $field.parents( '.form-table' ).find( '.field-container-post_parent' ).detach() );
})
});
});
}( window.jQuery, window._ ) );

// Check for checkbox dependecies
( function( $, _ ){
'use strict';
$(document).ready(function(){
var checkDependency = function( event ){
var $box, $dependecyField;
if ( _.isNumber( event ) ){
$box = $( this );
$dependecyField = $( $box.data('fkDepends') );
} else {
$dependecyField = $( this );
$box = $dependecyField.data( 'fkDependent' );
}

var condition = $box.data('fkCondition'),
$placeholder = $dependecyField.data( 'fkPlaceholder' );

if ( ! $placeholder ){
$placeholder = $( "<div>" ).attr( 'id', _.uniqueId( 'fk-dependent-placeholder-' ) );
$dependecyField.data( 'fkPlaceholder', $placeholder );
}
$dependecyField.data( 'fkDependent', $box );

if ( _.isNumber( event ) ){
$dependecyField.on( 'change', checkDependency );
}

if ( $dependecyField.is(':checked') != condition ){
$box.after( $placeholder ).detach();
} else if ( $placeholder.is(':visible') ) {
$placeholder.replaceWith( $dependecyField.data( 'fkDependent' ) );
}
};

$( '.fk-field-dependent' ).each( checkDependency );
});
}( window.jQuery, window._ ) );

28 changes: 23 additions & 5 deletions view/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
);
}

$_elements = array_merge( \Faker\Provider\HTML::$sets['header'], \Faker\Provider\HTML::$sets['list'], \Faker\Provider\HTML::$sets['block'] );

// Mount the options for taxonomies
$taxonomies = get_taxonomies( array( 'public' => true ), 'object' );

Expand Down Expand Up @@ -70,7 +72,7 @@
<?php wp_nonce_field( Plugin::$slug . '.request.' . Admin::$view->slug . ( isset( Admin::$view->action ) ? '.' . Admin::$view->action : '' ) ); ?>
<table class="form-table" style="display: table;">
<tbody>
<tr>
<tr class='fk-field-container'>
<th scope="row"><label for="fakerpress_qty"><?php _e( 'Quantity', 'fakerpress' ); ?></label></th>
<td>
<div id="fakerpress[qty]">
Expand All @@ -97,7 +99,7 @@
<p class="description"><?php _e( 'What posts can be choosen as Parent to the ones created', 'fakerpress' ); ?></p>
</td>
</tr>
<tr>
<tr class='fk-field-container'>
<th scope="row"><label for="fakerpress_comment_status"><?php _e( 'Comments Status', 'fakerpress' ); ?></label></th>
<td>
<div id="fakerpress[comment_status]">
Expand All @@ -106,7 +108,23 @@
<p class="description"><?php _e( 'Sampling group of options for the comment status of the posts', 'fakerpress' ); ?></p>
</td>
</tr>
<tr>
<tr class='fk-field-container field-container-post_content_use_html'>
<th scope="row"><label for="fakerpress_post_content_use_html"><?php _e( 'Use HTML', 'fakerpress' ); ?></label></th>
<td>
<input type='checkbox' style="margin-top: -3px;" name='fakerpress_post_content_use_html' checked />
<p style='display: inline-block;' class="description"><?php _e( 'Use HTML on your randomized post content?', 'fakerpress' ); ?></p>
</td>
</tr>
<tr class='fk-field-container fk-field-dependent' data-fk-depends=".field-container-post_content_use_html input" data-fk-condition='true'>
<th scope="row"><label for="fakerpress_post_content_html_tags"><?php _e( 'HTML tags', 'fakerpress' ); ?></label></th>
<td>
<div id="fakerpress[post_content_html_tags]">
<input type='hidden' class='field-select2-tags' name='fakerpress_post_content_html_tags' value='<?php echo implode( ',', $_elements ); ?>' data-tags='<?php echo json_encode( $_elements ); ?>' />
</div>
<p class="description"><?php _e( 'Select the group of tags that can be selected to print on the Post Content', 'fakerpress' ); ?></p>
</td>
</tr>
<tr class='fk-field-container'>
<th scope="row"><label for="fakerpress_taxonomies"><?php _e( 'Taxonomies', 'fakerpress' ); ?></label></th>
<td>
<div id="fakerpress[taxonomies]">
Expand All @@ -115,7 +133,7 @@
<p class="description"><?php _e( 'From which taxonomies the related terms should be selected', 'fakerpress' ); ?></p>
</td>
</tr>
<tr>
<tr class='fk-field-container'>
<th scope="row"><label for="fakerpress_interval_date"><?php _e( 'Date', 'fakerpress' ); ?></label></th>
<td>
<div class='fakerpress-range-group'>
Expand All @@ -133,7 +151,7 @@
<p class="description-date description"><?php _e( 'Choose the range for the posts dates.', 'fakerpress' ); ?></p>
</td>
</tr>
<tr>
<tr class='fk-field-container'>
<th scope="row"><label for="fakerpress_author"><?php _e( 'Author', 'fakerpress' ); ?></label></th>
<td>
<div id="fakerpress[author]">
Expand Down