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

Allow post_parent to be chosen from the Administration #35

Merged
merged 6 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
47 changes: 47 additions & 0 deletions inc/init/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function( $view ) {

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

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

if ( $quantity === 0 ){
return Admin::add_message( sprintf( __( 'Zero is not a good number of %s to fake...', 'fakerpress' ), 'posts' ), 'error' );
Expand All @@ -41,6 +42,7 @@ function( $view ) {
'tax_input' => array( $taxonomies ),
'post_status' => array( array( 'publish' ) ),
'post_date' => array( $min_date, $max_date ),
'post_parent' => array( $post_parents ),
'post_type' => array( 'post' ),
'post_author' => array( $post_author ),
'post_type' => array( $post_types ),
Expand Down Expand Up @@ -79,3 +81,48 @@ function() {
Admin::add_menu( 'posts', __( 'Posts', 'fakerpress' ), __( 'Posts', 'fakerpress' ), 'manage_options', 5 );
}
);

// We need o create a base class to lie all method related to AJAX
add_action(
'wp_ajax_' . Plugin::$slug . '.query_posts',
function ( $request = null ){
$response = (object) array(
'status' => false,
'message' => __( 'Your request has failed', 'fakerpress' ),
'results' => array(),
'more' => true,
);

if ( ( ! Admin::$is_ajax && is_null( $request ) ) || ! is_user_logged_in() ){
return ( Admin::$is_ajax ? exit( json_encode( $response ) ) : $response );
}

$request = (object) $_POST;


if ( isset( $request->query['post_type'] ) && ! is_array( $request->query['post_type'] ) ){
$request->query['post_type'] = array_map( 'trim', (array) explode( ',', $request->query['post_type'] ) );
}

$query = new \WP_Query( $request->query );

if ( ! $query->have_posts() ){
return ( Admin::$is_ajax ? exit( json_encode( $response ) ) : $response );
}

$response->status = true;
$response->message = __( 'Request successful', 'fakerpress' );

foreach ( $query->posts as $k => $post ) {
$query->posts[ $k ]->post_type = get_post_type_object( $post->post_type );
}

$response->results = $query->posts;

if ( $query->max_num_pages >= $request->query['paged'] ){
$response->more = false;
}

return ( Admin::$is_ajax ? exit( json_encode( $response ) ) : $response );
}
);
4 changes: 4 additions & 0 deletions providers/wp-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public function post_status( $haystack = array() ){
return $this->generator->randomElement( (array) $haystack );
}

public function post_parent( $haystack = array(), $rate = 70 ){
return $this->generator->numberBetween( 0, 100 ) < absint( $rate ) ? 0 : $this->generator->randomElement( (array) $haystack );
}

public function ping_status( $haystack = array() ){
if ( empty( $haystack ) ){
$haystack = static::$default['ping_status'];
Expand Down
92 changes: 90 additions & 2 deletions ui/js/fields.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Taxonomies Fields
// Simple Select2 Fields
( function( $ ){
'use strict';
$(document).ready(function(){
Expand All @@ -12,7 +12,25 @@
return { 'results': $select.data( 'possibilities' ) };
}
});
});
})
.on( 'change', function( event ) {
var data = $( this ).data( 'value' );

if ( event.added ){
if ( _.isArray( data ) ) {
data.push( event.added );
} else {
data = [ event.added ];
}
} else {
if ( _.isArray( data ) ) {
data = _.without( data, event.removed );
} else {
data = [];
}
}
$( this ).data( 'value', data ).attr( 'data-value', JSON.stringify( data ) );
} );

$( '.field-date-selection' ).each(function(){
var $select = $(this);
Expand Down Expand Up @@ -98,3 +116,73 @@
});
});
}( jQuery, _ ) );

// Post Query for Select2
( function( $, _ ){
'use strict';
$(document).ready(function(){
$( '.field-select2-posts' ).each(function(){
var $select = $(this);
$select.select2({
width: 400,
multiple: true,
data: {results:[]},
allowClear: true,
escapeMarkup: function (m) { return m; },
formatSelection: function ( post ){
return _.template('<abbr title="<%= post_title %>"><%= post_type.labels.singular_name %>: <%= ID %></abbr>')( post )
},
formatResult: function ( post ){
return _.template('<abbr title="<%= post_title %>"><%= post_type.labels.singular_name %>: <%= ID %></abbr>')( post )
},
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
dataType: 'json',
type: 'POST',
url: window.ajaxurl,
data: function (search, page) {
return {
action: 'fakerpress.query_posts',
query: {
s: search,
posts_per_page: 10,
paged: page,
post_type: _.pluck( _.where( $( '.field-post_type.select2-offscreen' ).data( 'value' ), { hierarchical: true } ) , 'id' )
}
};
},
results: function ( data ) { // parse the results into the format expected by Select2.
$.each( data.results, function( k, result ){
result.id = result.ID;
} );
return data;
}
},
});
});
});
}( jQuery, _ ) );

// Check if Post Type is hierarchical
( function( $, _ ){
'use strict';
$(document).ready(function(){
$( '.field-post_type' ).on( 'change', function( event ){
var $field = $(this),
$field_PostType = $field.parents( '.form-table' ).find( '.field-container-post_parent' );

if ( ! _.isUndefined( event.added ) && event.added.hierarchical === true && $field_PostType.length === 0 ){
$field.parents( '.field-container-post_type' ).after( $field.data( 'post_parent' ) );
} else if ( ! _.isUndefined( event.removed ) && event.removed.hierarchical === true && $field_PostType.length !== 0 ){
$field.data( 'post_parent', $field_PostType.detach() );
}
} ).each(function(){
var $field = $(this);

if ( ! $field.hasClass( 'select2-offscreen' ) ){
return;
}
$field.data( 'post_parent', $field.parents( '.form-table' ).find( '.field-container-post_parent' ).detach() );
})
});
}( window.jQuery, window._ ) );

14 changes: 12 additions & 2 deletions view/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
$_json_post_types_output = array();
foreach ( $post_types as $key => $post_type ) {
$_json_post_types_output[] = array(
'hierarchical' => $post_type->hierarchical,
'id' => $post_type->name,
'text' => $post_type->labels->name,
);
Expand Down Expand Up @@ -78,15 +79,24 @@
<p class="description"><?php _e( 'The amount of Posts you want to generate', 'fakerpress' ); ?></p>
</td>
</tr>
<tr>
<tr class='fk-field-container field-container-post_type'>
<th scope="row"><label for="fakerpress_post_types"><?php _e( 'Post Type', 'fakerpress' ); ?></label></th>
<td>
<div id="fakerpress[post_types]">
<input type='hidden' class='field-select2-simple' name='fakerpress_post_types' data-possibilities='<?php echo json_encode( $_json_post_types_output ); ?>' />
<input type='hidden' class='field-select2-simple field-post_type' name='fakerpress_post_types' data-possibilities='<?php echo json_encode( $_json_post_types_output ); ?>' />
</div>
<p class="description"><?php _e( 'Sampling group of Post Types', 'fakerpress' ); ?></p>
</td>
</tr>
<tr class='fk-field-container field-container-post_parent'>
<th scope="row"><label for="fakerpress_post_parents"><?php _e( 'Parents', 'fakerpress' ); ?></label></th>
<td>
<div id="fakerpress[post_parents]">
<input type='hidden' class='field-select2-posts field-post_parent' name='fakerpress_post_parents' />
</div>
<p class="description"><?php _e( 'What posts can be choosen as Parent to the ones created', 'fakerpress' ); ?></p>
</td>
</tr>
<tr>
<th scope="row"><label for="fakerpress_comment_status"><?php _e( 'Comments Status', 'fakerpress' ); ?></label></th>
<td>
Expand Down