From 044d1107538f1adb2bd376e62b6a7db7d2d304e8 Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Tue, 19 Aug 2014 20:08:20 -0300 Subject: [PATCH 1/6] Create a post_parent field that will only be shown when we have the post type hierarchical --- ui/js/fields.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++- view/posts.php | 14 +++++++++-- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/ui/js/fields.js b/ui/js/fields.js index e6deadf..21dfa9f 100644 --- a/ui/js/fields.js +++ b/ui/js/fields.js @@ -1,4 +1,4 @@ -// Taxonomies Fields +// Simple Select2 Fields ( function( $ ){ 'use strict'; $(document).ready(function(){ @@ -98,3 +98,66 @@ }); }); }( 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, + 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 (term, page) { + return { + action: 'fakerpress.query_posts', + search: search, // search term + page_limit: 10, + page: page, + post_type: null + }; + }, + results: function ( data ) { // parse the results into the format expected by Select2. + $.each( data.results, function( k, result ){ + result.text = _.template('<%= post_type %>: <%= ID %>')( 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._ ) ); + diff --git a/view/posts.php b/view/posts.php index 9ecc1d9..44baeb0 100644 --- a/view/posts.php +++ b/view/posts.php @@ -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, ); @@ -78,15 +79,24 @@

- +
- +

+ + + +
+ +
+

+ + From dbb2ed6240117c54f63053f9dcc0b843e0b7ea28 Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Wed, 20 Aug 2014 17:12:03 -0300 Subject: [PATCH 2/6] Including the Query_Posts Ajax Request --- inc/init/posts.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/inc/init/posts.php b/inc/init/posts.php index a746aa6..1cf75c2 100644 --- a/inc/init/posts.php +++ b/inc/init/posts.php @@ -79,3 +79,47 @@ 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'] ) ){ + $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 ); + } +); From f4d9ff51cc400701546cf4fef04b956863492c51 Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Wed, 20 Aug 2014 17:12:32 -0300 Subject: [PATCH 3/6] Creating the Post parents fields depending on the Post Type you've selected --- ui/js/fields.js | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/ui/js/fields.js b/ui/js/fields.js index 21dfa9f..e9f1865 100644 --- a/ui/js/fields.js +++ b/ui/js/fields.js @@ -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); @@ -105,28 +123,35 @@ $(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('<%= post_type.labels.singular_name %>: <%= ID %>')( post ) + }, + formatResult: function ( post ){ + return _.template('<%= post_type.labels.singular_name %>: <%= ID %>')( 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 (term, page) { + data: function (search, page) { return { action: 'fakerpress.query_posts', - search: search, // search term - page_limit: 10, - page: page, - post_type: null + 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.text = _.template('<%= post_type %>: <%= ID %>')( result ); result.id = result.ID; } ); return data; From be571289d6031a4048724d2d9700ea141174b095 Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Wed, 20 Aug 2014 17:24:25 -0300 Subject: [PATCH 4/6] Includes a post_parent choice on the provider --- providers/wp-post.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/providers/wp-post.php b/providers/wp-post.php index bdee239..caa31f0 100644 --- a/providers/wp-post.php +++ b/providers/wp-post.php @@ -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']; From 53fc2caae1b3a37afe1fafcddfb52d3ec4266399 Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Wed, 20 Aug 2014 17:26:33 -0300 Subject: [PATCH 5/6] Parsing the post_parents on the posts creation --- inc/init/posts.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inc/init/posts.php b/inc/init/posts.php index 1cf75c2..021ab79 100644 --- a/inc/init/posts.php +++ b/inc/init/posts.php @@ -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' ); @@ -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 ), From 8a23a2a21fc81fb8b365c4d19d37facfd0378623 Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Wed, 20 Aug 2014 18:43:12 -0300 Subject: [PATCH 6/6] Adding a check for the split on Post types --- inc/init/posts.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/init/posts.php b/inc/init/posts.php index 021ab79..9edceb8 100644 --- a/inc/init/posts.php +++ b/inc/init/posts.php @@ -99,7 +99,8 @@ function ( $request = null ){ $request = (object) $_POST; - if ( isset( $request->query['post_type'] ) ){ + + 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'] ) ); }