PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / settings / table-languages.php

table-languages.php in Polylang trunk, at src/settings/table-languages.php

281 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 if ( ! class_exists( 'WP_List_Table' ) ) {
7 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; // since WP 3.1
8 }
9
10 /**
11 * A class to create the languages table in Polylang settings
12 * Thanks to Matt Van Andel ( http://www.mattvanandel.com ) for its plugin "Custom List Table Example" !
13 *
14 * @since 0.1
15 */
16 class PLL_Table_Languages extends WP_List_Table {
17
18 /**
19 * Constructor
20 *
21 * @since 0.1
22 */
23 public function __construct() {
24 parent::__construct(
25 array(
26 'plural' => 'Languages', // Do not translate ( used for css class )
27 'ajax' => false,
28 )
29 );
30 }
31
32 /**
33 * Generates content for a single row of the table.
34 *
35 * @since 1.8
36 *
37 * @param PLL_Language $item The language item.
38 * @return void
39 */
40 public function single_row( $item ) {
41 /**
42 * Filter the list of classes assigned a row in the languages list table
43 *
44 * @since 1.8
45 *
46 * @param array $classes The list of class names.
47 * @param PLL_Language $item The language item.
48 */
49 $classes = apply_filters( 'pll_languages_row_classes', array(), $item );
50 echo '<tr' . ( empty( $classes ) ? '>' : ' class="' . esc_attr( implode( ' ', $classes ) ) . '">' );
51 $this->single_row_columns( $item );
52 echo '</tr>';
53 }
54
55 /**
56 * Displays the item information in a column ( default case ).
57 *
58 * @since 0.1
59 *
60 * @param PLL_Language $item The language item.
61 * @param string $column_name The column name.
62 * @return string|int
63 */
64 public function column_default( $item, $column_name ) {
65 switch ( $column_name ) {
66 case 'locale':
67 case 'slug':
68 return esc_html( $item->$column_name );
69
70 case 'term_group':
71 return (int) $item->$column_name;
72
73 case 'count':
74 return $item->get_tax_prop( 'language', $column_name );
75
76 case 'flag':
77 return $item->get_admin_flag();
78
79 default:
80 return '';
81 }
82 }
83
84 /**
85 * Displays the item information in the column 'name'
86 * Displays the edit and delete action links
87 *
88 * @since 0.1
89 *
90 * @param PLL_Language $item The language item.
91 * @return string
92 */
93 public function column_name( $item ) {
94 return sprintf(
95 '<a title="%s" href="%s">%s</a>',
96 esc_attr__( 'Edit this language', 'polylang' ),
97 esc_url( admin_url( 'admin.php?page=mlang&amp;pll_action=edit&amp;lang=' . $item->term_id ) ),
98 esc_html( $item->name )
99 );
100 }
101
102 /**
103 * Displays the item information in the default language
104 * Displays the 'make default' action link
105 *
106 * @since 1.8
107 *
108 * @param PLL_Language $item The language item.
109 * @return string
110 */
111 public function column_default_lang( $item ) {
112 if ( ! $item->is_default ) {
113 $s = sprintf(
114 '<div class="row-actions"><span class="default-lang">
115 <a class="icon-default-lang" title="%1$s" href="%2$s"><span class="screen-reader-text">%3$s</span></a>
116 </span></div>',
117 esc_attr__( 'Select as default language', 'polylang' ),
118 wp_nonce_url( '?page=mlang&amp;pll_action=default-lang&amp;noheader=true&amp;lang=' . $item->term_id, 'default-lang' ),
119 /* translators: accessibility text, %s is a native language name */
120 esc_html( sprintf( __( 'Choose %s as default language', 'polylang' ), $item->name ) )
121 );
122
123 /**
124 * Filters the default language row action in the languages list table.
125 *
126 * @since 1.8
127 *
128 * @param string $s The html markup of the action.
129 * @param PLL_Language $item The language item.
130 */
131 $s = apply_filters( 'pll_default_lang_row_action', $s, $item );
132 } else {
133 $s = sprintf(
134 '<span class="icon-default-lang"><span class="screen-reader-text">%1$s</span></span>',
135 /* translators: accessibility text */
136 esc_html__( 'Default language', 'polylang' )
137 );
138 }
139
140 return $s;
141 }
142
143 /**
144 * Gets the list of columns
145 *
146 * @since 0.1
147 *
148 * @return string[] The list of column titles.
149 */
150 public function get_columns() {
151 return array(
152 'name' => esc_html__( 'Full name', 'polylang' ),
153 'locale' => esc_html__( 'Locale', 'polylang' ),
154 'slug' => esc_html__( 'Code', 'polylang' ),
155 'default_lang' => sprintf( '<span title="%1$s" class="icon-default-lang"><span class="screen-reader-text">%2$s</span></span>', esc_attr__( 'Default language', 'polylang' ), esc_html__( 'Default language', 'polylang' ) ),
156 'term_group' => esc_html__( 'Order', 'polylang' ),
157 'flag' => esc_html__( 'Flag', 'polylang' ),
158 'count' => esc_html__( 'Posts', 'polylang' ),
159 );
160 }
161
162 /**
163 * Gets the list of sortable columns
164 *
165 * @since 0.1
166 *
167 * @return array
168 */
169 public function get_sortable_columns() {
170 return array(
171 'name' => array( 'name', true ), // sorted by name by default
172 'locale' => array( 'locale', false ),
173 'slug' => array( 'slug', false ),
174 'term_group' => array( 'term_group', false ),
175 'count' => array( 'count', false ),
176 );
177 }
178
179 /**
180 * Gets the name of the default primary column.
181 *
182 * @since 2.1
183 *
184 * @return string Name of the default primary column, in this case, 'name'.
185 */
186 protected function get_default_primary_column_name() {
187 return 'name';
188 }
189
190 /**
191 * Generates and display row actions links for the list table.
192 *
193 * @since 1.8
194 *
195 * @param PLL_Language $item The language item being acted upon.
196 * @param string $column_name Current column name.
197 * @param string $primary Primary column name.
198 * @return string The row actions output.
199 */
200 protected function handle_row_actions( $item, $column_name, $primary ) {
201 if ( $primary !== $column_name ) {
202 return '';
203 }
204
205 $actions = array(
206 'edit' => sprintf(
207 '<a title="%s" href="%s">%s</a>',
208 esc_attr__( 'Edit this language', 'polylang' ),
209 esc_url( admin_url( 'admin.php?page=mlang&amp;pll_action=edit&amp;lang=' . $item->term_id ) ),
210 esc_html__( 'Edit', 'polylang' )
211 ),
212 'delete' => sprintf(
213 '<a title="%s" href="%s" onclick = "return confirm( \'%s\' );">%s</a>',
214 esc_attr__( 'Delete this language and all its associated data', 'polylang' ),
215 wp_nonce_url( '?page=mlang&amp;pll_action=delete&amp;noheader=true&amp;lang=' . $item->term_id, 'delete-lang' ),
216 esc_js( __( 'You are about to permanently delete this language. Are you sure?', 'polylang' ) ),
217 esc_html__( 'Delete', 'polylang' )
218 ),
219 );
220
221 /**
222 * Filters the list of row actions in the languages list table.
223 *
224 * @since 1.8
225 *
226 * @param array $actions A list of html markup actions.
227 * @param PLL_Language $item The language item.
228 */
229 $actions = apply_filters( 'pll_languages_row_actions', $actions, $item );
230
231 return $this->row_actions( $actions );
232 }
233
234 /**
235 * Sorts language items.
236 *
237 * @since 0.1
238 *
239 * @param PLL_Language $a The first language to compare.
240 * @param PLL_Language $b The second language to compare.
241 * @return int -1 or 1 if $a is considered to be respectively less than or greater than $b.
242 */
243 protected function usort_reorder( $a, $b ) {
244 $orderby = ! empty( $_GET['orderby'] ) ? sanitize_key( $_GET['orderby'] ) : 'name'; // phpcs:ignore WordPress.Security.NonceVerification
245 // Determine sort order
246 if ( is_numeric( $a->$orderby ) ) {
247 $result = $a->$orderby > $b->$orderby ? 1 : -1;
248 } else {
249 $result = strcmp( $a->$orderby, $b->$orderby );
250 }
251 // Send final sort direction to usort.
252 return ( empty( $_GET['order'] ) || 'asc' === $_GET['order'] ) ? $result : -$result; // phpcs:ignore WordPress.Security.NonceVerification
253 }
254
255 /**
256 * Prepares the list of languages for display.
257 *
258 * @since 0.1
259 *
260 * @param PLL_Language[] $data The list of languages.
261 * @return void
262 */
263 public function prepare_items( $data = array() ) {
264 $per_page = $this->get_items_per_page( 'pll_lang_per_page' );
265 $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() );
266
267 usort( $data, array( $this, 'usort_reorder' ) );
268
269 $total_items = count( $data );
270 $this->items = array_slice( $data, ( $this->get_pagenum() - 1 ) * $per_page, $per_page );
271
272 $this->set_pagination_args(
273 array(
274 'total_items' => $total_items,
275 'per_page' => $per_page,
276 'total_pages' => (int) ceil( $total_items / $per_page ),
277 )
278 );
279 }
280 }
281