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