PluginProbe
Polylang / 2.8
Polylang v2.8
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.8, at settings/table-languages.php

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