| 1 |
<?php |
| 2 |
/** |
| 3 |
* Subscribe forms class |
| 4 |
*/ |
| 5 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 6 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 7 |
} |
| 8 |
|
| 9 |
class SIB_Forms_List extends WP_List_Table { |
| 10 |
|
| 11 |
/** Class constructor */ |
| 12 |
public function __construct() { |
| 13 |
|
| 14 |
parent::__construct( |
| 15 |
array('singular' => __( 'Form', 'sib_lang' ), //singular name of the listed records |
| 16 |
'plural' => __( 'Forms', 'sib_lang' ), //plural name of the listed records |
| 17 |
'ajax' => false) //does this table support ajax? |
| 18 |
); |
| 19 |
|
| 20 |
add_action( 'admin_head', array( &$this, 'admin_header' ) ); |
| 21 |
|
| 22 |
} |
| 23 |
/** |
| 24 |
* Retrieve contacts data from the database |
| 25 |
* |
| 26 |
* @param int $per_page |
| 27 |
* @param int $page_number |
| 28 |
* |
| 29 |
* @return mixed |
| 30 |
*/ |
| 31 |
public static function getForms( $per_page = 5, $page_number = 1 ) { |
| 32 |
|
| 33 |
$result = SIB_Forms::getForms(); |
| 34 |
$start = ( $page_number - 1 ) * $per_page; |
| 35 |
usort( $result, array(__CLASS__, 'usort_reorder' ) ); |
| 36 |
$result = array_slice($result, $start, $per_page); |
| 37 |
return $result; |
| 38 |
} |
| 39 |
/** |
| 40 |
* Returns the count of records in the database. |
| 41 |
* |
| 42 |
* @return null|string |
| 43 |
*/ |
| 44 |
public static function record_count() { |
| 45 |
$result = SIB_Forms::getForms(); |
| 46 |
return count($result); |
| 47 |
} |
| 48 |
/** Text displayed when no customer data is available */ |
| 49 |
public function no_items() { |
| 50 |
_e( 'No forms avaliable.', 'sib_lang' ); |
| 51 |
} |
| 52 |
/** |
| 53 |
* Render a column when no column specific method exist. |
| 54 |
* |
| 55 |
* @param array $item |
| 56 |
* @param string $column_name |
| 57 |
* |
| 58 |
* @return mixed |
| 59 |
*/ |
| 60 |
public function column_default( $item, $column_name ) { |
| 61 |
switch ( $column_name ) { |
| 62 |
case 'id': |
| 63 |
return '[sibwp_form id='.$item['id'].']'; |
| 64 |
case 'title': |
| 65 |
case 'attributes': |
| 66 |
case 'listName': |
| 67 |
case 'date': |
| 68 |
return $item[ $column_name ]; |
| 69 |
//default: |
| 70 |
//return print_r( $item, true ); //Show the whole array for troubleshooting purposes |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Render the bulk edit checkbox |
| 76 |
* |
| 77 |
* @param array $item |
| 78 |
* |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
function column_cb( $item ) { |
| 82 |
return sprintf( |
| 83 |
'<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['id'] |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Method for form title column |
| 90 |
* |
| 91 |
* @param array $item an array of DB data |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
function column_title( $item ) { |
| 96 |
|
| 97 |
$delete_nonce = wp_create_nonce( 'sib_delete_form' ); |
| 98 |
|
| 99 |
$title = '<strong>' . $item['title'] . '</strong>'; |
| 100 |
|
| 101 |
$actions = array( |
| 102 |
'edit' => sprintf( '<a href="?page=%s&action=%s&id=%s">Edit</a>', esc_attr( $_REQUEST['page'] ), 'edit', absint( $item['id'] ) ), |
| 103 |
'duplicate' => sprintf( '<a href="?page=%s&action=%s&id=%s">Duplicate</a>', esc_attr( $_REQUEST['page'] ), 'duplicate', absint( $item['id'] ) ), |
| 104 |
'delete' => sprintf( '<a class="sib-form-delete" href="?page=%s&action=%s&id=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['id'] ), $delete_nonce ) |
| 105 |
); |
| 106 |
|
| 107 |
return $title . $this->row_actions( $actions ); |
| 108 |
} |
| 109 |
|
| 110 |
function column_trans( $item ) { |
| 111 |
$languages = apply_filters('wpml_active_languages', NULL, array()); |
| 112 |
|
| 113 |
$results = ''; |
| 114 |
if(!empty( $languages )) |
| 115 |
{ |
| 116 |
foreach($languages as $language) |
| 117 |
{ |
| 118 |
$exist = SIB_Forms_Lang::get_form_ID($item['id'], $language['language_code']); |
| 119 |
|
| 120 |
if($exist == null) |
| 121 |
{ |
| 122 |
$img_src = plugins_url('img/add_translation.png', dirname(__FILE__)); |
| 123 |
|
| 124 |
$href = sprintf( '<a href="?page=%s&action=%s&pid=%s&lang=%s" style="width: 20px; text-align: center;padding: 2px 1px;">', esc_attr( $_REQUEST['page'] ), 'edit', absint( $item['id'] ), $language['language_code'] ); |
| 125 |
$results .= $href .'<img src="'.$img_src.'" style="margin:2px;"></a>'; |
| 126 |
} |
| 127 |
else{ |
| 128 |
$img_src = plugins_url('img/edit_translation.png', dirname(__FILE__)); |
| 129 |
$href = sprintf( '<a href="?page=%s&action=%s&id=%s&pid=%s&lang=%s" style="width: 20px; text-align: center;padding: 2px 1px;">', esc_attr( $_REQUEST['page'] ), 'edit', absint( $exist ) , absint( $item['id']), $language['language_code']); |
| 130 |
$results .= $href .'<img src="'.$img_src.'" style="margin:2px;"></a>'; |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
} |
| 135 |
return $results; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Associative array of columns |
| 140 |
* |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
function get_columns() { |
| 144 |
$columns = array( |
| 145 |
'cb' => '<input type="checkbox" />', |
| 146 |
'title' => __( 'Form Name', 'sib_lang' ), |
| 147 |
'id' => __( 'Shortcode', 'sib_lang' ), |
| 148 |
'attributes' => __( 'Visible attributes', 'sib_lang' ), |
| 149 |
'listName' => __( 'Linked List', 'sib_lang' ), |
| 150 |
'date' => __( 'Last Update', 'sib_lang' ) |
| 151 |
); |
| 152 |
if(function_exists('icl_plugin_action_links')) |
| 153 |
{ |
| 154 |
$languages = apply_filters('wpml_active_languages', NULL, array()); |
| 155 |
|
| 156 |
$results = ''; |
| 157 |
if(!empty( $languages )) |
| 158 |
{ |
| 159 |
foreach($languages as $language) |
| 160 |
{ |
| 161 |
$results .= '<img src="'.$language['country_flag_url'].'" style="margin:2px;">'; |
| 162 |
} |
| 163 |
} |
| 164 |
$columns['trans'] = $results; |
| 165 |
} |
| 166 |
return $columns; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* Columns to make sortable. |
| 172 |
* |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public function get_sortable_columns() { |
| 176 |
$sortable_columns = array( |
| 177 |
'date' => array( 'date', true ), |
| 178 |
'title' => array( 'title', false ), |
| 179 |
); |
| 180 |
|
| 181 |
return $sortable_columns; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Returns an associative array containing the bulk action |
| 186 |
* |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
public function get_bulk_actions() { |
| 190 |
$actions = array( |
| 191 |
'bulk-delete' => 'Delete' |
| 192 |
); |
| 193 |
|
| 194 |
return $actions; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Handles data query and filter, sorting, and pagination. |
| 200 |
*/ |
| 201 |
public function prepare_items() { |
| 202 |
|
| 203 |
$this->_column_headers = $this->get_column_info(); |
| 204 |
|
| 205 |
/** Process bulk action */ |
| 206 |
$this->process_bulk_action(); |
| 207 |
|
| 208 |
$per_page = $this->get_items_per_page( 'forms_per_page', 50 ); |
| 209 |
$current_page = $this->get_pagenum(); |
| 210 |
$total_items = self::record_count(); |
| 211 |
|
| 212 |
$this->set_pagination_args( array( |
| 213 |
'total_items' => $total_items, //WE have to calculate the total number of items |
| 214 |
'per_page' => $per_page //WE have to determine how many items to show on a page |
| 215 |
) ); |
| 216 |
|
| 217 |
$this->items = self::getForms( $per_page, $current_page ); |
| 218 |
} |
| 219 |
|
| 220 |
public function process_bulk_action() { |
| 221 |
|
| 222 |
//Detect when a bulk action is being triggered... |
| 223 |
if ( 'delete' === $this->current_action() ) { |
| 224 |
|
| 225 |
// In our file that handles the request, verify the nonce. |
| 226 |
$nonce = esc_attr( $_REQUEST['_wpnonce'] ); |
| 227 |
|
| 228 |
if ( ! wp_verify_nonce( $nonce, 'sib_delete_form' ) ) { |
| 229 |
die( 'Go get a life script kiddies' ); |
| 230 |
} |
| 231 |
else { |
| 232 |
SIB_Forms::deleteForm( absint( esc_attr($_GET['id']) ) ); |
| 233 |
SIB_Forms_Lang::remove_trans( absint( esc_attr($_GET['id']) ) ); |
| 234 |
wp_redirect(add_query_arg('page', SIB_Page_Form::PAGE_ID, admin_url('admin.php'))); exit; |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
// If the delete bulk action is triggered |
| 240 |
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) |
| 241 |
|| ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) |
| 242 |
) { |
| 243 |
|
| 244 |
$delete_ids = esc_sql( $_POST['bulk-delete'] ); |
| 245 |
|
| 246 |
// loop over the array of record IDs and delete them |
| 247 |
foreach ( $delete_ids as $id ) { |
| 248 |
SIB_Forms::deleteForm( $id ); |
| 249 |
SIB_Forms_Lang::remove_trans($id); |
| 250 |
} |
| 251 |
wp_redirect(esc_url(add_query_arg(NULL,NULL))); exit; |
| 252 |
|
| 253 |
} |
| 254 |
} |
| 255 |
public function pagination($which){ |
| 256 |
echo '<a href="'.add_query_arg(array('page' => 'sib_page_form','action'=>'edit'/*,'id'=>'new'*/), admin_url('admin.php')).'" class="btn btn-primary" style="float:right; margin: 2px 1px 8px 15px;" >'.__("Add New Form", "sib_lang").'</a>'; |
| 257 |
parent::pagination($which); |
| 258 |
} |
| 259 |
|
| 260 |
static function usort_reorder( $a, $b ) { |
| 261 |
// If no sort, default to title |
| 262 |
$orderby = ( ! empty( $_GET['orderby'] ) ) ? esc_attr($_GET['orderby']) : 'title'; // by title |
| 263 |
// If no order, default to asc |
| 264 |
$order = ( ! empty($_GET['order'] ) ) ? esc_attr($_GET['order']) : 'ask'; |
| 265 |
// Determine sort order |
| 266 |
$result = strcmp( $a[$orderby], $b[$orderby] ); |
| 267 |
// Send final sort direction to usort |
| 268 |
return ( $order === 'ask' ) ? $result : -$result; |
| 269 |
} |
| 270 |
|
| 271 |
function admin_header() { |
| 272 |
$page = ( isset($_GET['page'] ) ) ? esc_attr( $_GET['page'] ) : false; |
| 273 |
if( 'sib_page_form' != $page ) |
| 274 |
return; |
| 275 |
|
| 276 |
echo '<style type="text/css">'; |
| 277 |
echo '.wp-list-table .column-date { width: 150px; }'; |
| 278 |
echo '</style>'; |
| 279 |
} |
| 280 |
} |