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