PluginProbe
Polylang / 2.4
Polylang v2.4
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 / settings / table-languages.php

table-languages.php in Polylang 2.4, at settings/table-languages.php

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