Support sorting posts in category
This commit is contained in:
@@ -12,6 +12,7 @@ namespace Ogre\Sort;
|
|||||||
|
|
||||||
use Ogre\Singleton;
|
use Ogre\Singleton;
|
||||||
use Ogre\Sort as Plugin;
|
use Ogre\Sort as Plugin;
|
||||||
|
use WP_Query;
|
||||||
|
|
||||||
if (!defined('ABSPATH')) exit;
|
if (!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@@ -26,9 +27,6 @@ final class Sort {
|
|||||||
protected array $current_relationship = [];
|
protected array $current_relationship = [];
|
||||||
|
|
||||||
protected function __construct() {
|
protected function __construct() {
|
||||||
$this->relationships = [];
|
|
||||||
$this->current_relationship = [];
|
|
||||||
|
|
||||||
add_action('init', [$this, 'init'], 100);
|
add_action('init', [$this, 'init'], 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +38,7 @@ final class Sort {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_relationship_type(array $relationship):bool {
|
public function get_relationship_type(array $relationship):string {
|
||||||
if (isset($relationship['post_type']) && isset($relationship['taxonomy'])) {
|
if (isset($relationship['post_type']) && isset($relationship['taxonomy'])) {
|
||||||
return 'term';
|
return 'term';
|
||||||
} else if (isset($relationship['post_type']) && !isset($relationship['taxonomy'])) {
|
} else if (isset($relationship['post_type']) && !isset($relationship['taxonomy'])) {
|
||||||
@@ -48,7 +46,7 @@ final class Sort {
|
|||||||
} else if (!isset($relationship['post_type']) && isset($relationship['taxonomy'])) {
|
} else if (!isset($relationship['post_type']) && isset($relationship['taxonomy'])) {
|
||||||
return 'taxonomy';
|
return 'taxonomy';
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +112,7 @@ final class Sort {
|
|||||||
foreach ($this->relationships as $relationship) {
|
foreach ($this->relationships as $relationship) {
|
||||||
switch ($relationship['type']) {
|
switch ($relationship['type']) {
|
||||||
case 'term':
|
case 'term':
|
||||||
if ($screen->base == 'edit' && $screen->post_type == $relationship['post_type'] && (isset($_GET[$relationship['taxonomy']]) || (isset($_GET['taxonomy']) && $_GET['taxonomy'] == $relationship['taxonomy']))) {
|
if ($screen->base == 'edit' && $screen->post_type == $relationship['post_type'] && (isset($_GET[$relationship['taxonomy']]) || ($relationship['taxonomy'] === 'category' && isset($_GET['category_name'])) || (isset($_GET['taxonomy']) && $_GET['taxonomy'] == $relationship['taxonomy']))) {
|
||||||
$this->current_relationship = $relationship;
|
$this->current_relationship = $relationship;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -140,9 +138,9 @@ final class Sort {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ($this->current_relationship != false) break;
|
if (!empty($this->current_relationship)) break;
|
||||||
}
|
}
|
||||||
if ($this->current_relationship == false) return;
|
if (empty($this->current_relationship)) return;
|
||||||
|
|
||||||
// Ajax
|
// Ajax
|
||||||
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
|
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
|
||||||
@@ -162,7 +160,7 @@ final class Sort {
|
|||||||
wp_enqueue_script('ogre-sort', Plugin::get_url('assets/sort.js'), ['jquery'], Plugin::get_version(), true);
|
wp_enqueue_script('ogre-sort', Plugin::get_url('assets/sort.js'), ['jquery'], Plugin::get_version(), true);
|
||||||
wp_localize_script('ogre-sort', 'ogre_sort', $vars);
|
wp_localize_script('ogre-sort', 'ogre_sort', $vars);
|
||||||
|
|
||||||
wp_enqueue_style('ogre-sort', Plugin::get_url('assets/sort/css'), false, Plugin::get_version());
|
wp_enqueue_style('ogre-sort', Plugin::get_url('assets/sort.css'), false, Plugin::get_version());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add Meta when Post is Updated
|
// Add Meta when Post is Updated
|
||||||
@@ -221,10 +219,10 @@ final class Sort {
|
|||||||
|
|
||||||
// Query Filters
|
// Query Filters
|
||||||
|
|
||||||
public function pre_get_posts($wp_query) {
|
public function pre_get_posts(WP_Query $wp_query):void {
|
||||||
if (empty($this->relationships)) return false;
|
if (empty($this->relationships)) return;
|
||||||
if (isset($wp_query->query['orderby']) && !empty($wp_query->query['orderby']) && ((is_admin() && isset($_GET['orderby'])) || $wp_query->query['orderby'] != 'date')) return false;
|
if (isset($wp_query->query['orderby']) && !empty($wp_query->query['orderby']) && ((is_admin() && isset($_GET['orderby'])) || $wp_query->query['orderby'] != 'date')) return;
|
||||||
//if (!is_admin() && isset($wp_query->query['suppress_filters'])) return false; // NOTE: suppress_filters set to true by default in get_posts
|
//if (!is_admin() && isset($wp_query->query['suppress_filters'])) return; // NOTE: suppress_filters set to true by default in get_posts
|
||||||
//if (!isset($wp_query->query['post_type'])) $wp_query->set('post_type', 'post');
|
//if (!isset($wp_query->query['post_type'])) $wp_query->set('post_type', 'post');
|
||||||
|
|
||||||
$relationships = [];
|
$relationships = [];
|
||||||
@@ -236,22 +234,24 @@ final class Sort {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'term':
|
case 'term':
|
||||||
if (isset($relationship['taxonomy']) && isset($wp_query->query[$relationship['taxonomy']])) {
|
if (isset($wp_query->query[$relationship['taxonomy']])) {
|
||||||
$relationships[] = $relationship;
|
$relationships[] = $relationship;
|
||||||
} else if (isset($relationship['taxonomy']) && isset($wp_query->query['taxonomy']) && $wp_query->query['taxonomy'] == $relationship['taxonomy']) {
|
} else if (isset($wp_query->query['taxonomy']) && $wp_query->query['taxonomy'] == $relationship['taxonomy']) {
|
||||||
$relationships[] = $relationship;
|
$relationships[] = $relationship;
|
||||||
} else if (isset($relationship['taxonomy']) && isset($wp_query->query['tax_query']) && is_array($wp_query->query['tax_query']) && !empty($wp_query->query['tax_query'])) {
|
} else if (isset($wp_query->query['tax_query']) && is_array($wp_query->query['tax_query']) && !empty($wp_query->query['tax_query'])) {
|
||||||
foreach ($wp_query->query['tax_query'] as $tax) {
|
foreach ($wp_query->query['tax_query'] as $tax) {
|
||||||
if (!isset($tax['taxonomy'])) continue;
|
if (!isset($tax['taxonomy'])) continue;
|
||||||
if ($relationship['taxonomy'] != $tax['taxonomy']) continue;
|
if ($relationship['taxonomy'] != $tax['taxonomy']) continue;
|
||||||
$relationships[] = $relationship;
|
$relationships[] = $relationship;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else if ($relationship['taxonomy'] === 'category' && isset($wp_query->query['category_name'])) {
|
||||||
|
$relationships[] = $relationship;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (empty($relationships)) return false;
|
if (empty($relationships)) return;
|
||||||
|
|
||||||
$current_relationship = $relationships[0];
|
$current_relationship = $relationships[0];
|
||||||
foreach ($relationships as $relationship) {
|
foreach ($relationships as $relationship) {
|
||||||
@@ -262,7 +262,7 @@ final class Sort {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (empty($current_relationship)) $current_relationship = $relationships[0];
|
if (empty($current_relationship)) $current_relationship = $relationships[0];
|
||||||
if (empty($current_relationship)) return false;
|
if (empty($current_relationship)) return;
|
||||||
|
|
||||||
switch ($current_relationship['type']) {
|
switch ($current_relationship['type']) {
|
||||||
case 'term':
|
case 'term':
|
||||||
@@ -280,6 +280,8 @@ final class Sort {
|
|||||||
$term_id = $tax['terms'];
|
$term_id = $tax['terms'];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else if ($current_relationship['taxonomy'] === 'category' && isset($wp_query->query['category_name'])) {
|
||||||
|
$term_id = $wp_query->query['category_name'];
|
||||||
}
|
}
|
||||||
if (empty($term_id)) break;
|
if (empty($term_id)) break;
|
||||||
|
|
||||||
@@ -469,14 +471,7 @@ final class Sort {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$exists = false;
|
if (empty(array_filter($this->relationships, fn (array $item):bool => empty(array_diff($item, $relationship))))) {
|
||||||
foreach ($this->relationships as $rel) {
|
|
||||||
if (($relationship['type'] == $rel['type']) && (!isset($relationship['post_type']) || $relationship['post_type'] == $rel['post_type']) && (!isset($relationship['taxonomy']) || $relationship['taxonomy'] == $rel['taxonomy'])) {
|
|
||||||
$exists = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($exists == false) {
|
|
||||||
wp_send_json_error(__('Relationship does not exist.', 'ogrecore'));
|
wp_send_json_error(__('Relationship does not exist.', 'ogrecore'));
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user