Skip to content

Commit

Permalink
Added relation fields with taxonomy in AeriaMetabox
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele Diener committed Aug 31, 2016
1 parent c1a3bed commit 7d1223c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 55 deletions.
3 changes: 2 additions & 1 deletion classes/AeriaMetabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,9 @@ function show_field_select($field, $meta) {
function show_field_select_ajax($field, $meta) {
if (!is_array($meta)) $meta = (array) $meta;
$meta_value = html_addslashes(empty($meta)?'':$meta[0]);
$field['with'] = !empty($field['with']) && in_array($field['with'], ["post_type", "taxonomy"]) ? $field['with'] : "post_type";
$this->show_field_begin($field, $meta);
echo "<input type='hidden' value=\"{$meta_value}\" name='{$field['id']}' class='input-xlarge select2_ajax' data-relation='{$field['relation']}' data-placeholder='Select an Option..'". ($field['multiple'] ? " data-multiple='true' style='height:auto'" : "data-multiple='false'") ." /> ";
echo "<input type='hidden' value=\"{$meta_value}\" name='{$field['id']}' class='input-xlarge select2_ajax' data-relation='{$field['relation']}' data-with='{$field['with']}' data-placeholder='Select an Option..'". ($field['multiple'] ? " data-multiple='true' style='height:auto'" : "data-multiple='false'") ." /> ";
$this->show_field_end($field, $meta);
}

Expand Down
157 changes: 106 additions & 51 deletions classes/AeriaUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,80 @@ public static function search($q=null,$page=1,$posts_per_page=10,$post_type='pos
$page = isset($_REQUEST['page'])?$_REQUEST['page']:$page;
$posts_per_page = isset($_REQUEST['posts_per_page'])?$_REQUEST['posts_per_page']:$posts_per_page;
$post_type = isset($_REQUEST['post_type'])?$_REQUEST['post_type']:$post_type;
$type = !empty($_REQUEST['type']) && in_array($_REQUEST['type'], ["post_type", "taxonomy"]) ? $_REQUEST['type'] : "post_type" ;

if(is_numeric($page) && is_numeric($posts_per_page)) {

if(strlen($q)>0) {
$results = get_posts([
'posts_per_page' => $posts_per_page,
'offset' => ($page-1)*$posts_per_page,
'post_type' => $post_type,
'orderby' => 'title',
'order' => 'ASC',
'post_title_like' => $q,
'suppress_filters' => false

]);
}else{
$results = get_posts([
'posts_per_page' => $posts_per_page,
'offset' => ($page-1)*$posts_per_page,
'post_type' => $post_type,
'orderby' => 'title',
'order' => 'ASC',
'suppress_filters' => false
]);
}


$number_results = wp_count_posts($post_type);

$posts_result = [];
foreach ($results as $key => $result) {
$posts_result[] = [
'id' => $result->ID,
'text' => $result->post_title
if ($type == "post_type") {
if(strlen($q)>0) {
$results = get_posts([
'posts_per_page' => $posts_per_page,
'offset' => ($page-1)*$posts_per_page,
'post_type' => $post_type,
'orderby' => 'title',
'order' => 'ASC',
'post_title_like' => $q,
'suppress_filters' => false

]);
}else{
$results = get_posts([
'posts_per_page' => $posts_per_page,
'offset' => ($page-1)*$posts_per_page,
'post_type' => $post_type,
'orderby' => 'title',
'order' => 'ASC',
'suppress_filters' => false
]);
}


$number_results = wp_count_posts($post_type);
$posts_result = [];
foreach ($results as $key => $result) {
$posts_result[] = [
'id' => $result->ID,
'text' => $result->post_title
];
}

$posts = [
'total' => $number_results->publish,
'result' => $posts_result
];
} else {
if(strlen($q)>0) {
$results = get_terms( $post_type, [
'hide_empty' => false,
'offset' => ($page-1)*$posts_per_page,
'name__like' => $q,
'orderby' => 'name',
'order' => 'ASC',
'number' => $posts_per_page,
]);
} else {
$results = get_terms( $post_type, [
'hide_empty' => false,
'offset' => ($page-1)*$posts_per_page,
'orderby' => 'name',
'order' => 'ASC',
'number' => $posts_per_page,
]);
}
$number_results = wp_count_terms($post_type);
$posts_result = [];
foreach ($results as $key => $result) {
$posts_result[] = [
'id' => $result->term_id,
'text' => $result->name
];
}

$posts = [
'total' => $number_results,
'result' => $posts_result
];
}

$posts = [
'total' => $number_results->publish,
'result' => $posts_result
];

}

header("Content-Type: application/json",true);
Expand All @@ -60,30 +93,52 @@ public static function search($q=null,$page=1,$posts_per_page=10,$post_type='pos

}

public static function search_init($id=null,$multiple=false){

public static function search_init($id=null,$multiple=false,$post_type='post'){
$id = isset($_REQUEST['id'])?$_REQUEST['id']:$id;
$multiple = isset($_REQUEST['multiple'])?$_REQUEST['multiple']:$multiple;
$type = !empty($_REQUEST['type']) && in_array($_REQUEST['type'], ["post_type", "taxonomy"]) ? $_REQUEST['type'] : "post_type" ;
$post_type = isset($_REQUEST['post_type'])?$_REQUEST['post_type']:$post_type;

if ($type == "post_type") {
if($multiple=="true") {
$result = [];
$posts = explode(',',$id);
foreach ($posts as $key => $post_id) {
$post = new AeriaPost($post_id);
$result[] = [
'id' => $post->id,
'text' => $post->title
];
}
}else{
$post = new AeriaPost($id);

if($multiple=="true") {
$result = [];
$posts = explode(',',$id);
foreach ($posts as $key => $post_id) {
$post = new AeriaPost($post_id);
$result[] = [
$result = [
'id' => $post->id,
'text' => $post->title
];
}
}else{
$post = new AeriaPost($id);
} else {
if($multiple=="true") {
$result = [];
$posts = explode(',',$id);
foreach ($posts as $key => $post_id) {
$post = get_term_by('id', $post_id, $post_type);
$result[] = [
'id' => $post->term_id,
'text' => $post->name
];
}
}else{
$post = get_term_by('id', $id, $post_type);

$result = [
'id' => $post->id,
'text' => $post->title
];
$result = [
'id' => $post->term_id,
'text' => $post->name
];
}
}

header("Content-Type: application/json",true);
echo json_encode($result);
exit;
Expand Down
10 changes: 7 additions & 3 deletions resources/js/select2-aeria.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ window.aeria_init_select2_ajax = function(){
}else{
multiple = false;
}
var relation = $this.attr('data-relation');
var relation = $this.attr('data-relation'),
type = $this.attr('data-with');
if( ! $this.data("select2")){
$this.select2({
minimumInputLength: 0,
Expand All @@ -72,7 +73,8 @@ window.aeria_init_select2_ajax = function(){
page: page,
posts_per_page : posts_per_page,
action: 'aeria_search',
post_type: relation
post_type: relation,
type: type
};
},
results: function (data, page) {
Expand All @@ -89,7 +91,9 @@ window.aeria_init_select2_ajax = function(){
data: {
id : id,
action : 'aeria_search_init',
multiple : multiple
multiple : multiple,
type: type,
post_type: relation,
},
dataType: "json"
}).done(function(data) {
Expand Down

0 comments on commit 7d1223c

Please sign in to comment.