| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 6 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 7 |
} |
| 8 |
|
| 9 |
class FV_Player_List_Table extends WP_List_Table { |
| 10 |
|
| 11 |
public $args; |
| 12 |
|
| 13 |
public $base_url; |
| 14 |
|
| 15 |
public $counts; |
| 16 |
|
| 17 |
private $post_types; |
| 18 |
|
| 19 |
public $total_impressions = 0; |
| 20 |
|
| 21 |
public $total_clicks = 0; |
| 22 |
|
| 23 |
public $total_items = 0; |
| 24 |
|
| 25 |
private $dropdown_cache = false; |
| 26 |
|
| 27 |
public function __construct( $args = array() ) { |
| 28 |
$this->args = $args; |
| 29 |
|
| 30 |
parent::__construct( array( |
| 31 |
'singular' => 'Log entry', |
| 32 |
'plural' => 'Log entries', |
| 33 |
'ajax' => false, |
| 34 |
'screen' => !empty($args['screen']) ? $args['screen'] : false |
| 35 |
) ); |
| 36 |
|
| 37 |
// initialize video and video meta objects, so if there are no video tables created in the DB, |
| 38 |
// we'll create them now (and no SQL errors will be displayed on the listing page) |
| 39 |
new FV_Player_Db_Video(-1); |
| 40 |
new FV_Player_Db_Video_Meta(-1); |
| 41 |
|
| 42 |
$this->process_bulk_action(); |
| 43 |
$this->base_url = admin_url( 'admin.php?page=fv_player' ); |
| 44 |
} |
| 45 |
|
| 46 |
public function advanced_filters() { |
| 47 |
if ( ! empty( $_REQUEST['orderby'] ) ) |
| 48 |
echo '<input type="hidden" name="orderby" value="' . esc_attr( sanitize_text_field( $_REQUEST['orderby'] ) ) . '" />'; |
| 49 |
if ( ! empty( $_REQUEST['order'] ) ) |
| 50 |
echo '<input type="hidden" name="order" value="' . esc_attr( sanitize_text_field( $_REQUEST['order'] ) ) . '" />'; |
| 51 |
?> |
| 52 |
<p class="search-box"> |
| 53 |
<label class="screen-reader-text" for="fv_player_search">Search players:</label> |
| 54 |
<input type="search" id="fv_player_search" name="s" value="<?php _admin_search_query(); ?>" /> |
| 55 |
<?php submit_button( "Search players", 'button', false, false, array('ID' => 'search-submit') ); ?><br/> |
| 56 |
</p> |
| 57 |
<?php |
| 58 |
} |
| 59 |
|
| 60 |
function get_columns() { |
| 61 |
return self::get_columns_worker(); |
| 62 |
} |
| 63 |
|
| 64 |
public static function get_columns_worker() { |
| 65 |
$cols = array( |
| 66 |
//'cb' => '<input type="checkbox" />', |
| 67 |
'id' => __( 'Player', 'fv-player' ), |
| 68 |
'player_name' => __( 'Player Name', 'fv-player' ), |
| 69 |
'date_created' => __( 'Date', 'fv-player' ), |
| 70 |
'author' => __( 'Author', 'fv-player' ), |
| 71 |
'thumbs' => __( 'Videos', 'fv-player' ), |
| 72 |
'subtitles_count' => __( 'Subtitles', 'fv-player' ), |
| 73 |
'chapters_count' => __( 'Chapters', 'fv-player' ), |
| 74 |
'transcript_count' => __( 'Transcript', 'fv-player' ), |
| 75 |
'embeds' => __( 'Embedded on', 'fv-player' ) |
| 76 |
); |
| 77 |
|
| 78 |
global $fv_fp; |
| 79 |
if( $fv_fp->_get_option('video_stats_enable') ) { |
| 80 |
$cols['stats_play'] = __( 'Plays', 'fv-player' ); |
| 81 |
} |
| 82 |
|
| 83 |
return $cols; |
| 84 |
} |
| 85 |
|
| 86 |
public function get_sortable_columns() { |
| 87 |
return array( |
| 88 |
'id' => array( 'id', true ), |
| 89 |
'author' => array( 'author', true ), |
| 90 |
'player_name' => array( 'player_name', true ), |
| 91 |
'date_created' => array( 'date_created', true ), |
| 92 |
// TODO: Disabled due to poor performance |
| 93 |
/*'subtitles_count' => array( 'subtitles_count', true ), |
| 94 |
'chapters_count' => array( 'chapters_count', true ), |
| 95 |
'transcript_count' => array( 'transcript_count', true )*/ |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
protected function get_primary_column_name() { |
| 100 |
return 'id'; |
| 101 |
} |
| 102 |
|
| 103 |
function get_user_dropdown( $user_id, $name = false, $disabled = false ) { |
| 104 |
if( !$this->dropdown_cache ) { |
| 105 |
$this->dropdown_cache = wp_dropdown_users( array( |
| 106 |
'name' => 'user_id', |
| 107 |
'role__not_in' => array('subscriber'), |
| 108 |
'show_option_none' => 'All users', |
| 109 |
'echo' => false |
| 110 |
) ); |
| 111 |
} |
| 112 |
$html = $this->dropdown_cache; |
| 113 |
|
| 114 |
$html = str_replace("value='".$user_id."'>","value='".$user_id."' selected>",$html); |
| 115 |
if( $name ) $html = str_replace("name='user_id' ","name='".$name."' ' ",$html); |
| 116 |
if( $disabled ) $html = str_replace("<select ","<select disabled='disabled' ",$html); |
| 117 |
|
| 118 |
return $html; |
| 119 |
} |
| 120 |
|
| 121 |
public function column_cb( $player ) { |
| 122 |
return sprintf( |
| 123 |
'<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 124 |
'log_id', |
| 125 |
$player->id |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
public function column_default( $player, $column_name ) { |
| 130 |
$id = $player->id; |
| 131 |
|
| 132 |
// TODO: This should be done in a sensible way |
| 133 |
// if any of the videos for this player contain a coconut_processing_ placeholder, |
| 134 |
// try to run Coconut's job check, so we can update that SRC if it was already |
| 135 |
// processed |
| 136 |
if ( function_exists( 'FV_Player_Coconut' ) ) { |
| 137 |
foreach ( $player->video_objects as $video_object ) { |
| 138 |
if ( strpos( $video_object->getSrc(), 'coconut_processing_' ) !== false ) { |
| 139 |
FV_Player_Coconut()->jobs_check(); |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
switch ( $column_name ) { |
| 146 |
case 'id': |
| 147 |
$value = '<span class="fv_player_id_value" data-player_id="'. $id .'">' . $id . '</span>'; |
| 148 |
break; |
| 149 |
case 'date_created' : |
| 150 |
$value = $player->date_created > 0 ? "<abbr title='$player->date_created'>".gmdate('Y/m/d',strtotime($player->date_created))."</abbr>" : false; |
| 151 |
break; |
| 152 |
case 'player_name' : |
| 153 |
$name = $player->player_name ? $player->player_name : join( ', ', $player->video_titles ); |
| 154 |
$name = flowplayer::filter_possible_html($name); |
| 155 |
if ( 'published' !== $player->status ) { |
| 156 |
$name .= ' (' . ucfirst( $player->status ) . ')'; |
| 157 |
} |
| 158 |
|
| 159 |
$value = "<a href='#' class='fv-player-edit' data-player_id='{$id}'>" . $name . "</a>\n"; |
| 160 |
$value .= "<div class='row-actions'>"; |
| 161 |
$value .= "<a href='#' class='fv-player-edit' data-player_id='{$id}'>Edit</a> | "; |
| 162 |
$value .= "<a href='#' class='fv-player-export' data-player_id='{$id}' data-nonce='".wp_create_nonce('fv-player-db-export-'.$id)."'>Export</a> | "; |
| 163 |
$value .= "<a href='#' class='fv-player-clone' data-player_id='{$id}' data-nonce='".wp_create_nonce('fv-player-db-export-'.$id)."'>Clone</a> | "; |
| 164 |
$value .= "<span class='trash'><a href='#' class='fv-player-remove' data-player_id='{$id}' data-nonce='".wp_create_nonce('fv-player-db-remove-'.$id)."'>Delete</a></span>"; |
| 165 |
|
| 166 |
$value .= '<input type="text" class="fv-player-shortcode-input" readonly value="'.esc_attr('[fvplayer id="'. $id .'"]').'" style="display: none" /><a href="#" class="button fv-player-shortcode-copy">Copy Shortcode</a>'; |
| 167 |
|
| 168 |
$value .= apply_filters( 'fv_player_list_table_extra_row_actions', '', $id, $player ); |
| 169 |
|
| 170 |
$value .= "</div>\n"; |
| 171 |
break; |
| 172 |
case 'embeds': |
| 173 |
$value = ''; |
| 174 |
|
| 175 |
foreach( $player->embeds AS $post_id => $post ) { |
| 176 |
|
| 177 |
$title = !empty($post->post_title) ? $post->post_title : '#'.$post->ID; |
| 178 |
if( $post->post_status != 'publish' ) { |
| 179 |
$title .= ' ('.$post->post_status.')'; |
| 180 |
} |
| 181 |
|
| 182 |
$value .= '<li><a href="'.get_permalink($post).'" target="_blank">'.$title.'</a></li>'; |
| 183 |
} |
| 184 |
|
| 185 |
if( $value ) $value = '<ul>'.$value.'</ul>'; |
| 186 |
|
| 187 |
break; |
| 188 |
case 'author': |
| 189 |
$value = '<a href="#">'.get_the_author_meta( 'user_nicename' , $player->author ).'</a>'; |
| 190 |
break; |
| 191 |
case 'stats_play': |
| 192 |
$value= ''; |
| 193 |
if( $player->stats_play ) $value = '<a href="'. admin_url( 'admin.php?page=fv_player_stats&player_id=' . $id ) .'" target="_blank">'. $player->stats_play .'</a>'; |
| 194 |
break; |
| 195 |
case 'thumbs': |
| 196 |
$value = join( ' ', $player->thumbs ); |
| 197 |
break; |
| 198 |
default: |
| 199 |
$value = isset($player->$column_name) && $player->$column_name ? $player->$column_name : ''; |
| 200 |
break; |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
// Use manage_toplevel_page_fv_player_columns to add new columns and what's below to add content |
| 205 |
do_action( "manage_toplevel_page_fv_player_custom_column", $column_name, $player ); |
| 206 |
|
| 207 |
return $value; |
| 208 |
} |
| 209 |
|
| 210 |
public function get_bulk_actions() { // todo: any bulk action? |
| 211 |
return array(); |
| 212 |
} |
| 213 |
|
| 214 |
public function get_views() { |
| 215 |
$current = isset( $_GET['post_type'] ) ? sanitize_key( $_GET['post_type'] ) : 'all'; |
| 216 |
|
| 217 |
|
| 218 |
if ( ! empty( $_GET['post_type'] ) ) { |
| 219 |
// Remove the taxonomy arg from the URL |
| 220 |
$post_type_taxonomies = fv_player_get_post_type_taxonomies( sanitize_key( $_GET['post_type'] ) ); |
| 221 |
|
| 222 |
foreach ( $post_type_taxonomies AS $tax ) { |
| 223 |
if ( ! empty( $_GET[ $tax ] ) ) { |
| 224 |
$url = add_query_arg( array( |
| 225 |
$tax => false |
| 226 |
) ); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
foreach( $this->post_types AS $k => $v ) { |
| 232 |
|
| 233 |
// Omit post types which have no player |
| 234 |
if ( 'all' != $k && ( !isset( $v->player_count ) || $v->player_count === 0 ) ) { |
| 235 |
continue; |
| 236 |
} |
| 237 |
|
| 238 |
$count = $v->player_count; |
| 239 |
|
| 240 |
$url_post_type = 'all' != $k ? $k : false; |
| 241 |
|
| 242 |
$url = add_query_arg( array( |
| 243 |
'post_type' => $url_post_type, |
| 244 |
'paged' => false |
| 245 |
), isset($url) ? $url : false ); |
| 246 |
|
| 247 |
$class = $current == $k ? ' class="current"' : ''; |
| 248 |
|
| 249 |
$views['post_type-'.$k] = sprintf( '<a href="%s"%s>%s</a>', $url, $class, $v->label . ' <span class="count">('.number_format($count).')' ); |
| 250 |
} |
| 251 |
|
| 252 |
return $views; |
| 253 |
} |
| 254 |
|
| 255 |
public function process_bulk_action() { // todo: any bulk action? |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
public function get_result_counts() { |
| 260 |
global $FV_Player_Db; |
| 261 |
$this->total_items = $FV_Player_Db->getListPageCount(); |
| 262 |
|
| 263 |
$all_post_type = new stdClass; |
| 264 |
$all_post_type->label = 'All'; |
| 265 |
|
| 266 |
$total_count = array(); |
| 267 |
|
| 268 |
// Get counts per post type and only do this once |
| 269 |
if ( empty( $this->post_types ) ) { |
| 270 |
$this->post_types = array( 'all' => $all_post_type ); |
| 271 |
|
| 272 |
$this->post_types = array_merge( $this->post_types, get_post_types( array( 'public' => true ), 'objects' ) ); |
| 273 |
|
| 274 |
// Get post IDs, post types and post statuses for each player |
| 275 |
// Each player might be in multiple posts |
| 276 |
global $wpdb; |
| 277 |
$player_to_post_id_to_post_type = $wpdb->get_results( "SELECT pl.id, pm.meta_value, p.ID, p.post_type, p.post_status FROM {$wpdb->prefix}fv_player_players AS pl LEFT JOIN {$wpdb->prefix}fv_player_playermeta AS pm ON pl.id = pm.id_player AND pm.meta_key = 'post_id' LEFT JOIN {$wpdb->posts} AS p ON p.ID = pm.meta_value" ); |
| 278 |
|
| 279 |
$no_post_attached = 0; |
| 280 |
|
| 281 |
foreach ( $player_to_post_id_to_post_type AS $v ) { |
| 282 |
// Only count for the post type if it's not in Trash |
| 283 |
if ( 'trash' != $v->post_status ) { |
| 284 |
foreach ( $this->post_types AS $post_type => $post_type_details ) { |
| 285 |
if ( $v->post_type == $post_type ) { |
| 286 |
if ( !isset( $this->post_types[ $post_type ]->player_count ) ) { |
| 287 |
$this->post_types[ $post_type ]->player_count = 0; |
| 288 |
} |
| 289 |
$this->post_types[ $post_type ]->player_count++; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// Player may not be used in any post |
| 295 |
if ( ! $v->meta_value ) { |
| 296 |
$no_post_attached++; |
| 297 |
} |
| 298 |
|
| 299 |
$total_count[ $v->id ] = true; |
| 300 |
} |
| 301 |
|
| 302 |
if( $no_post_attached ) { |
| 303 |
$no_post_type = new stdClass; |
| 304 |
$no_post_type->label = 'None'; |
| 305 |
$no_post_type->player_count = $no_post_attached; |
| 306 |
|
| 307 |
$this->post_types['none'] = $no_post_type; |
| 308 |
} |
| 309 |
|
| 310 |
$this->post_types[ 'all' ]->player_count = count( $total_count ); |
| 311 |
} |
| 312 |
|
| 313 |
if ( ! empty( $_GET['post_type'] ) ) { |
| 314 |
$post_type = sanitize_key( $_GET['post_type'] ); |
| 315 |
if ( ! empty( $this->post_types[ $post_type ] ) ) { |
| 316 |
$this->total_items = $this->post_types[ $post_type ]->player_count; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
public function get_data() { |
| 322 |
$current = !empty($_GET['paged']) ? intval($_GET['paged']) : 1; |
| 323 |
$order = !empty($_GET['order']) ? sanitize_key($_GET['order']) : 'desc'; |
| 324 |
$order_by = !empty($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) : 'date_created'; |
| 325 |
$single_id = !empty($_GET['id']) ? absint($_GET['id']) : null; |
| 326 |
$search = !empty($_GET['s']) ? sanitize_text_field( $_GET['s'] ) : null; |
| 327 |
$post_type = !empty($_GET['post_type']) ? sanitize_key( $_GET['post_type'] ) : null; |
| 328 |
|
| 329 |
if(!empty($this->args['player_id'])) $single_id = $this->args['player_id']; |
| 330 |
|
| 331 |
$per_page = $this->args['per_page']; |
| 332 |
$offset = ( $current - 1 ) * $per_page; |
| 333 |
|
| 334 |
$args = array( |
| 335 |
'offset' => $offset, |
| 336 |
'order' => $order, |
| 337 |
'order_by' => $order_by, |
| 338 |
'player_id' => $single_id, |
| 339 |
'per_page' => $per_page, |
| 340 |
'post_type' => $post_type, |
| 341 |
'search' => $search, |
| 342 |
); |
| 343 |
|
| 344 |
// Add any know taxonomy to the arguments |
| 345 |
if( $post_type ) { |
| 346 |
$post_type_taxonomies = fv_player_get_post_type_taxonomies( $post_type ); |
| 347 |
|
| 348 |
foreach ( $post_type_taxonomies AS $tax ) { |
| 349 |
if ( !empty( $_GET[ $tax ] ) ) { |
| 350 |
$args[ 'tax_'.$tax ] = sanitize_key( $_GET[ $tax ] ); |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
global $FV_Player_Db; |
| 356 |
return $FV_Player_Db->getListPageData( $args ); |
| 357 |
} |
| 358 |
|
| 359 |
public function prepare_items() { |
| 360 |
|
| 361 |
wp_reset_vars( array( 'action', 'payment', 'orderby', 'order', 's' ) ); |
| 362 |
|
| 363 |
$data = $this->get_data(); |
| 364 |
|
| 365 |
$this->get_result_counts(); |
| 366 |
|
| 367 |
$this->items = $data; |
| 368 |
|
| 369 |
$this->set_pagination_args( array( |
| 370 |
'total_items' => $this->total_items, |
| 371 |
'per_page' => $this->args['per_page'], |
| 372 |
'total_pages' => ceil( $this->total_items / $this->args['per_page'] ), |
| 373 |
) ); |
| 374 |
} |
| 375 |
|
| 376 |
} |
| 377 |
|