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