wpml-admin-table-sort.php
2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
class WPML_Admin_Table_Sort {
/** @var string $primary_column */
private $primary_column;
/** @var string $url_args */
private $url_args;
/** @var string $current_url */
private $current_url;
/** @var string */
private $orderby_param;
/** @var string */
private $order_param;
/**
* @param string $orderby_param
* @param string $order_param
*/
public function __construct( $orderby_param = 'orderby', $order_param = 'order' ) {
$this->orderby_param = $orderby_param;
$this->order_param = $order_param;
}
/**
* @param string $primary_column
*/
public function set_primary_column( $primary_column ) {
$this->primary_column = $primary_column;
}
/**
* @param string $column
*
* @return string
*/
public function get_column_url( $column ) {
$query_args = array(
$this->orderby_param => $column,
$this->order_param => 'desc',
);
if ( $this->get_current_orderby() === $column && $this->get_current_order() === 'desc' ) {
$query_args[ $this->order_param ] = 'asc';
}
return add_query_arg( $query_args, $this->get_current_url() );
}
/**
* @param string $column
*
* @return string
*/
public function get_column_classes( $column ) {
$classes = 'manage-column column-' . $column;
if ( $this->is_primary( $column ) ) {
$classes .= ' column-primary';
}
if ( $this->get_current_orderby() === $column ) {
$classes .= ' sorted ' . $this->get_current_order();
} else {
$classes .= ' sortable asc';
}
return $classes;
}
/**
* @param string $column
*
* @return bool
*/
private function is_primary( $column ) {
return $this->primary_column === $column;
}
/**
* @return string|null
*/
private function get_current_orderby() {
$url_args = $this->get_url_args();
return isset( $url_args[ $this->orderby_param ] ) ? $url_args[ $this->orderby_param ] : null;
}
/**
* @return string|null
*/
private function get_current_order() {
$url_args = $this->get_url_args();
return isset( $url_args[ $this->order_param ] ) ? $url_args[ $this->order_param ] : null;
}
/**
* @return array
*/
public function get_current_sorters() {
return array(
$this->orderby_param => $this->get_current_orderby(),
$this->order_param => $this->get_current_order(),
);
}
/**
* @return array
*/
private function get_url_args() {
if ( ! $this->url_args ) {
$this->url_args = array();
$url_query = wpml_parse_url( $this->get_current_url(), PHP_URL_QUERY );
parse_str( $url_query, $this->url_args );
}
return $this->url_args;
}
/**
* @return string
*/
private function get_current_url() {
if ( ! $this->current_url ) {
$this->current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
$this->current_url = remove_query_arg( 'paged', $this->current_url );
}
return $this->current_url;
}
}