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

273 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 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 }
132
133 return $s;
134 }
135
136 /**
137 * Gets the list of columns
138 *
139 * @since 0.1
140 *
141 * @return array the list of column titles
142 */
143 public function get_columns() {
144 return array(
145 'name' => esc_html__( 'Full name', 'polylang' ),
146 'locale' => esc_html__( 'Locale', 'polylang' ),
147 'slug' => esc_html__( 'Code', 'polylang' ),
148 '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' ) ),
149 'term_group' => esc_html__( 'Order', 'polylang' ),
150 'flag' => esc_html__( 'Flag', 'polylang' ),
151 'count' => esc_html__( 'Posts', 'polylang' ),
152 );
153 }
154
155 /**
156 * Gets the list of sortable columns
157 *
158 * @since 0.1
159 *
160 * @return array
161 */
162 public function get_sortable_columns() {
163 return array(
164 'name' => array( 'name', true ), // sorted by name by default
165 'locale' => array( 'locale', false ),
166 'slug' => array( 'slug', false ),
167 'term_group' => array( 'term_group', false ),
168 'count' => array( 'count', false ),
169 );
170 }
171
172 /**
173 * Gets the name of the default primary column.
174 *
175 * @since 2.1
176 *
177 * @return string Name of the default primary column, in this case, 'name'.
178 */
179 protected function get_default_primary_column_name() {
180 return 'name';
181 }
182
183 /**
184 * Generates and display row actions links for the list table.
185 *
186 * @since 1.8
187 *
188 * @param object $item The item being acted upon.
189 * @param string $column_name Current column name.
190 * @param string $primary Primary column name.
191 * @return string The row actions output.
192 */
193 protected function handle_row_actions( $item, $column_name, $primary ) {
194 if ( $primary !== $column_name ) {
195 return '';
196 }
197
198 $actions = array(
199 'edit' => sprintf(
200 '<a title="%s" href="%s">%s</a>',
201 esc_attr__( 'Edit this language', 'polylang' ),
202 esc_url( admin_url( 'admin.php?page=mlang&amp;pll_action=edit&amp;lang=' . $item->term_id ) ),
203 esc_html__( 'Edit', 'polylang' )
204 ),
205 'delete' => sprintf(
206 '<a title="%s" href="%s" onclick = "return confirm( \'%s\' );">%s</a>',
207 esc_attr__( 'Delete this language and all its associated data', 'polylang' ),
208 wp_nonce_url( '?page=mlang&amp;pll_action=delete&amp;noheader=true&amp;lang=' . $item->term_id, 'delete-lang' ),
209 esc_js( __( 'You are about to permanently delete this language. Are you sure?', 'polylang' ) ),
210 esc_html__( 'Delete', 'polylang' )
211 ),
212 );
213
214 /**
215 * Filter the list of row actions in the languages list table
216 *
217 * @since 1.8
218 *
219 * @param array $actions list of html markup actions
220 * @param object $item
221 */
222 $actions = apply_filters( 'pll_languages_row_actions', $actions, $item );
223
224 return $this->row_actions( $actions );
225 }
226
227 /**
228 * Sort items
229 *
230 * @since 0.1
231 *
232 * @param object $a The first object to compare
233 * @param object $b The second object to compare
234 * @return int -1 or 1 if $a is considered to be respectively less than or greater than $b.
235 */
236 protected function usort_reorder( $a, $b ) {
237 $orderby = ! empty( $_GET['orderby'] ) ? sanitize_key( $_GET['orderby'] ) : 'name'; // phpcs:ignore WordPress.Security.NonceVerification
238 // Determine sort order
239 if ( is_numeric( $a->$orderby ) ) {
240 $result = $a->$orderby > $b->$orderby ? 1 : -1;
241 } else {
242 $result = strcmp( $a->$orderby, $b->$orderby );
243 }
244 // Send final sort direction to usort
245 return ( empty( $_GET['order'] ) || 'asc' === $_GET['order'] ) ? $result : -$result; // phpcs:ignore WordPress.Security.NonceVerification
246 }
247
248 /**
249 * Prepares the list of items for displaying
250 *
251 * @since 0.1
252 *
253 * @param array $data
254 */
255 public function prepare_items( $data = array() ) {
256 $per_page = $this->get_items_per_page( 'pll_lang_per_page' );
257 $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() );
258
259 usort( $data, array( $this, 'usort_reorder' ) );
260
261 $total_items = count( $data );
262 $this->items = array_slice( $data, ( $this->get_pagenum() - 1 ) * $per_page, $per_page );
263
264 $this->set_pagination_args(
265 array(
266 'total_items' => $total_items,
267 'per_page' => $per_page,
268 'total_pages' => ceil( $total_items / $per_page ),
269 )
270 );
271 }
272 }
273