| 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 strings translations table |
| 9 |
* Thanks to Matt Van Andel ( http://www.mattvanandel.com ) for its plugin "Custom List Table Example" ! |
| 10 |
* |
| 11 |
* @since 0.6 |
| 12 |
*/ |
| 13 |
class PLL_Table_String extends WP_List_Table { |
| 14 |
protected $languages, $groups, $group_selected; |
| 15 |
|
| 16 |
/* |
| 17 |
* constructor |
| 18 |
* |
| 19 |
* @since 0.6 |
| 20 |
* |
| 21 |
* @param array $groups |
| 22 |
* @param string $group_selected |
| 23 |
*/ |
| 24 |
function __construct( $args ) { |
| 25 |
parent::__construct( array( |
| 26 |
'plural' => 'Strings translations', // do not translate ( used for css class ) |
| 27 |
'ajax' => false, |
| 28 |
) ); |
| 29 |
|
| 30 |
$this->languages = $args['languages']; |
| 31 |
$this->groups = $args['groups']; |
| 32 |
$this->group_selected = $args['selected']; |
| 33 |
} |
| 34 |
|
| 35 |
/* |
| 36 |
* displays the item information in a column ( default case ) |
| 37 |
* |
| 38 |
* @since 0.6 |
| 39 |
* |
| 40 |
* @param array $item |
| 41 |
* @param string $column_name |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
function column_default( $item, $column_name ) { |
| 45 |
return $item[ $column_name ]; |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* displays the checkbox in first column |
| 50 |
* |
| 51 |
* @since 1.1 |
| 52 |
* |
| 53 |
* @param array $item |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
function column_cb( $item ) { |
| 57 |
return sprintf( |
| 58 |
'<label class="screen-reader-text" for="cb-select-%1$s">%2$s</label><input id="cb-select-%1$s" type="checkbox" name="strings[]" value="%1$s" %3$s />', |
| 59 |
esc_attr( $item['row'] ), |
| 60 |
/* translators: %s is a string potentially in any language */ |
| 61 |
sprintf( __( 'Select %s' ), format_to_edit( $item['string'] ) ), |
| 62 |
empty( $item['icl'] ) ? 'disabled' : '' // only strings registered with WPML API can be removed |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* displays the string to translate |
| 68 |
* |
| 69 |
* @since 1.0 |
| 70 |
* |
| 71 |
* @param array $item |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
function column_string( $item ) { |
| 75 |
return format_to_edit( $item['string'] ); // don't interpret special chars for the string column |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
* displays the translations to edit |
| 80 |
* |
| 81 |
* @since 0.6 |
| 82 |
* |
| 83 |
* @param array $item |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
function column_translations( $item ) { |
| 87 |
$out = ''; |
| 88 |
foreach ( $item['translations'] as $key => $translation ) { |
| 89 |
$input_type = $item['multiline'] ? |
| 90 |
'<textarea name="translation[%1$s][%2$s]" id="%1$s-%2$s">%4$s</textarea>' : |
| 91 |
'<input type="text" name="translation[%1$s][%2$s]" id="%1$s-%2$s" value="%4$s" />'; |
| 92 |
$out .= sprintf( '<div class="translation"><label for="%1$s-%2$s">%3$s</label>'.$input_type.'</div>'."\n", |
| 93 |
esc_attr( $key ), |
| 94 |
esc_attr( $item['row'] ), |
| 95 |
esc_html( $this->languages[ $key ] ), |
| 96 |
format_to_edit( $translation ) ); // don't interpret special chars |
| 97 |
} |
| 98 |
return $out; |
| 99 |
} |
| 100 |
|
| 101 |
/* |
| 102 |
* gets the list of columns |
| 103 |
* |
| 104 |
* @since 0.6 |
| 105 |
* |
| 106 |
* @return array the list of column titles |
| 107 |
*/ |
| 108 |
function get_columns() { |
| 109 |
return array( |
| 110 |
'cb' => '<input type="checkbox" />', //checkbox |
| 111 |
'string' => __( 'String', 'polylang' ), |
| 112 |
'name' => __( 'Name', 'polylang' ), |
| 113 |
'context' => __( 'Group', 'polylang' ), |
| 114 |
'translations' => __( 'Translations', 'polylang' ), |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* gets the list of sortable columns |
| 120 |
* |
| 121 |
* @since 0.6 |
| 122 |
* |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
function get_sortable_columns() { |
| 126 |
return array( |
| 127 |
'string' => array( 'string', false ), |
| 128 |
'name' => array( 'name', false ), |
| 129 |
'context' => array( 'context', false ), |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* Sort items |
| 135 |
* |
| 136 |
* @since 0.6 |
| 137 |
* |
| 138 |
* @param object $a The first object to compare |
| 139 |
* @param object $b The second object to compare |
| 140 |
* @return int -1 or 1 if $a is considered to be respectively less than or greater than $b. |
| 141 |
*/ |
| 142 |
protected function usort_reorder( $a, $b ) { |
| 143 |
$result = strcmp( $a[ $_GET['orderby'] ], $b[ $_GET['orderby'] ] ); // determine sort order |
| 144 |
return ( empty( $_GET['order'] ) || 'asc' == $_GET['order'] ) ? $result : -$result; // send final sort direction to usort |
| 145 |
} |
| 146 |
|
| 147 |
/* |
| 148 |
* prepares the list of items for displaying |
| 149 |
* |
| 150 |
* @since 0.6 |
| 151 |
* |
| 152 |
* @param array $data |
| 153 |
*/ |
| 154 |
function prepare_items( $data = array() ) { |
| 155 |
$per_page = $this->get_items_per_page( 'pll_strings_per_page' ); |
| 156 |
$this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() ); |
| 157 |
|
| 158 |
if ( ! empty( $_GET['orderby'] ) ) {// no sort by default |
| 159 |
usort( $data, array( &$this, 'usort_reorder' ) ); |
| 160 |
} |
| 161 |
|
| 162 |
$total_items = count( $data ); |
| 163 |
$this->items = array_slice( $data, ( $this->get_pagenum() - 1 ) * $per_page, $per_page ); |
| 164 |
|
| 165 |
$this->set_pagination_args( array( |
| 166 |
'total_items' => $total_items, |
| 167 |
'per_page' => $per_page, |
| 168 |
'total_pages' => ceil( $total_items / $per_page ), |
| 169 |
) ); |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* get the list of possible bulk actions |
| 174 |
* |
| 175 |
* @since 1.1 |
| 176 |
* |
| 177 |
* @return array |
| 178 |
*/ |
| 179 |
function get_bulk_actions() { |
| 180 |
return array( 'delete' => __( 'Delete','polylang' ) ); |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* get the current action selected from the bulk actions dropdown. |
| 185 |
* overrides parent function to avoid submit button to trigger bulk actions |
| 186 |
* |
| 187 |
* @since 1.8 |
| 188 |
* |
| 189 |
* @return string|false The action name or False if no action was selected |
| 190 |
*/ |
| 191 |
public function current_action() { |
| 192 |
return empty( $_POST['submit'] ) ? parent::current_action() : false; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* displays the dropdown list to filter strings per group |
| 197 |
* |
| 198 |
* @since 1.1 |
| 199 |
* |
| 200 |
* @param string $which only 'top' is supported |
| 201 |
*/ |
| 202 |
function extra_tablenav( $which ) { |
| 203 |
if ( 'top' != $which ) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
echo '<div class="alignleft actions">'; |
| 208 |
printf( '<label class="screen-reader-text" for="select-group" >%s</label>', __( 'Filter by group', 'polylang' ) ); |
| 209 |
echo '<select id="select-group" name="group">' . "\n"; |
| 210 |
printf( |
| 211 |
'<option value="-1"%s>%s</option>' . "\n", |
| 212 |
-1 == $this->group_selected ? ' selected="selected"' : '', |
| 213 |
__( 'View all groups', 'polylang' ) |
| 214 |
); |
| 215 |
|
| 216 |
foreach ( $this->groups as $group ) { |
| 217 |
printf( |
| 218 |
'<option value="%s"%s>%s</option>' . "\n", |
| 219 |
esc_attr( urlencode( $group ) ), |
| 220 |
$this->group_selected == $group ? ' selected="selected"' : '', |
| 221 |
esc_html( $group ) |
| 222 |
); |
| 223 |
} |
| 224 |
echo '</select>'."\n"; |
| 225 |
|
| 226 |
submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) ); |
| 227 |
echo '</div>'; |
| 228 |
} |
| 229 |
} |
| 230 |
|