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

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