Skip to content

Instantly share code, notes, and snippets.

@anegoda1995
Created February 9, 2017 14:28
Show Gist options
  • Save anegoda1995/8ae197e79823130ca4fc7869a01b783b to your computer and use it in GitHub Desktop.
Save anegoda1995/8ae197e79823130ca4fc7869a01b783b to your computer and use it in GitHub Desktop.
Class for realise filter by data from page
<?php
require_once '../../../wp-load.php';
$bloginfo = get_bloginfo('template_url');
$jsonString = stripslashes($_POST['filter']);
$filterObj = new Filter($jsonString);
/*
echo '<br>Ниже фильтр по имени<br>';
var_dump($filterObj->getProjectsIdsFilteredByName($filterObj->requestName));
echo '<br>Ниже фильтр по типу проекта<br>';
var_dump($filterObj->getProjectsIdsFilteredByType($filterObj->requestType));
echo '<br>Ниже фильтр по площади<br>';
var_dump($filterObj->getProjectsIdsFilteredBySquare($filterObj->requestSquare));
echo '<br>Ниже фильтр по этажности<br>';
var_dump($filterObj->getProjectsIdsFilteredByFloors($filterObj->requestFloors));
echo '<br>Ниже фильтр по цене<br>';
var_dump($filterObj->getProjectsIdsFilteredByPrice($filterObj->requestPrice));
echo '<br><br>';
echo 'Правильный массив';
var_dump($filterObj->getFilteredPosts());
*/
$posts_array = [];
foreach ($filterObj->getFilteredPosts() as $neededPostId)
{
$pa = get_post($neededPostId);
$post_thumbnail = get_the_post_thumbnail_url((int) $pa->ID);
$tmp_array = (array) $pa;
array_push($tmp_array, $post_thumbnail);
array_push($tmp_array, $bloginfo);
array_push($tmp_array, get_field('pricevalue', $pa->ID));
array_push($tmp_array, get_field('square', $pa->ID));
array_push($tmp_array, get_field('projecttype', $pa->ID)[0]['typename']);
$tmp_array['post_date'] = date("d.m.Y", strtotime($pa->post_date));
$pa = $tmp_array;
array_push($posts_array, $pa);
/*print_r('<pre>');
print_r($pa);
print_r('</pre>');*/
}
echo json_encode($posts_array);
class Filter
{
public $requestName,
$requestType,
$requestSquare,
$requestFloors,
$requestPrice;
public function __construct($jsonString)
{
$jsonString = json_decode($jsonString);
/*print_r('<pre>');
print_r($jsonString);
print_r('</pre>');*/
$this->requestName = $jsonString[0];
$this->requestType = (array) $jsonString[1];
$this->requestSquare = $jsonString[2];
$this->requestFloors = (array) $jsonString[3];
$this->requestPrice = $jsonString[4];
}
public function getFilteredPosts()
{
$tmparray = [
$this->getProjectsIdsFilteredByName($this->requestName),
array_intersect($this->getProjectsIdsFilteredByType($this->requestType), $this->getProjectsIdsFilteredByFloors($this->requestFloors)),
$this->getProjectsIdsFilteredBySquare($this->requestSquare),
$this->getProjectsIdsFilteredByPrice($this->requestPrice)
];
$counts = array_map('count', $tmparray);
$min = min($counts);
$key = array_flip($counts)[$min];
$smallest_arr = $tmparray[$key];
return $smallest_arr;
}
public function getProjectsIdsOfAllPosts()
{
$idsOfAllProjects = [];
$posts_array = get_posts(['category_name' => 'completeprojects', 'post_type' => 'post', 'numberposts' => -1]);
foreach ($posts_array as $pa)
{
array_push($idsOfAllProjects, $pa->ID);
}
return $idsOfAllProjects;
}
public function getProjectsIdsFilteredByName($requestName)
{
$projectsIdsFilteredByName = [];
$posts_array = get_posts(['category_name' => 'completeprojects', 'post_type' => 'post', 'numberposts' => -1, 'name' => $requestName]);
foreach ($posts_array as $pa)
{
array_push($projectsIdsFilteredByName, $pa->ID);
}
return $projectsIdsFilteredByName;
}
public function getProjectsIdsFilteredByType($requestType)
{
$projectsIdsFilteredByType = [];
$posts_array = get_posts(['category_name' => 'completeprojects', 'numberposts' => -1, 'post_type' => 'post']);
foreach ($requestType as $key => $valueOfRequestType)
{
if ($valueOfRequestType)
{
foreach ($posts_array as $pa)
{
foreach (get_field('projecttype', $pa->ID) as $typename)
{
if ($key == $typename['typename'])
{
array_push($projectsIdsFilteredByType, $pa->ID);
}
}
}
}
}
return array_unique($projectsIdsFilteredByType);
}
public function getProjectsIdsFilteredBySquare($requestSquare)
{
$projectsIdsFilteredBySquare = [];
$square = 0;
$posts_array = get_posts(['category_name' => 'completeprojects', 'numberposts' => -1, 'post_type' => 'post']);
foreach ($posts_array as $pa)
{
$square = (int) get_field('square', $pa->ID);
//echo '$square = ' . $square;
if ($square >= (int) $requestSquare[0] && $square <= (int) $requestSquare[1])
{
array_push($projectsIdsFilteredBySquare, $pa->ID);
}
}
return array_unique($projectsIdsFilteredBySquare);
}
public function getProjectsIdsFilteredByFloors($requestFloors)
{
$projectsIdsFilteredByFloors = [];
$posts_array = get_posts(['category_name' => 'completeprojects', 'numberposts' => -1, 'post_type' => 'post']);
foreach ($posts_array as $pa)
{
foreach ($requestFloors as $key => $valueRequestFloors)
{
//var_dump($key.' '.$valueRequestFloors);
if ($valueRequestFloors)
{
switch (get_field('floors', $pa->ID))
{
case 1: {
if ($key == '1 этаж')
{
array_push($projectsIdsFilteredByFloors, $pa->ID);
}
break;
}
case 2: {
if ($key == '2 этажа')
{
array_push($projectsIdsFilteredByFloors, $pa->ID);
}
break;
}
case 3: {
if ($key == '3 этажа')
{
array_push($projectsIdsFilteredByFloors, $pa->ID);
}
break;
}
}
if ($key == 'с мансардой' && get_field('attic', $pa->ID) == true)
{
array_push($projectsIdsFilteredByFloors, $pa->ID);
}
}
}
}
return array_unique($projectsIdsFilteredByFloors);
}
public function getProjectsIdsFilteredByPrice($requestPrice)
{
$projectsIdsFilteredByPrice = [];
$price = 0;
$posts_array = get_posts(['category_name' => 'completeprojects', 'numberposts' => -1, 'post_type' => 'post']);
foreach ($posts_array as $pa)
{
$price = (int) get_field('pricevalue', $pa->ID);
//echo '$square = ' . $square;
if ($price >= (int) $requestPrice[0] && $price <= (int) $requestPrice[1])
{
array_push($projectsIdsFilteredByPrice, $pa->ID);
}
}
return array_unique($projectsIdsFilteredByPrice);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment