Created
February 21, 2014 19:26
-
-
Save leofrozenyogurt/9141548 to your computer and use it in GitHub Desktop.
featured posts admin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Morgans_Featured_Admin | |
* | |
* Manages Morgan's Featured Post | |
* | |
* @author Carlos Barrantes | |
* | |
*/ | |
class Morgans_Featured_Admin { | |
/** | |
* Tabs, query_id => display_name | |
*/ | |
var $tabs = array( 'main' => 'Main', 'culture' => 'Culture', 'thelist' => 'The List', 'housestories' => 'Leo Test' ); | |
/** | |
* Current selected tab | |
*/ | |
var $selected_tab = ''; | |
/** | |
* Register page resources | |
*/ | |
function admin_init_page() { | |
if( isset( $_REQUEST['post_id'] ) && isset( $_REQUEST['location'] )) { | |
$this->remove_post_location(); | |
} | |
if ( !empty( $_REQUEST['tab'] ) ) { | |
$this->selected_tab = $_REQUEST['tab']; | |
} else { | |
$this->selected_tab = array_shift( array_keys( $this->tabs ) ); | |
} | |
wp_register_style( 'wp-mf-style', plugins_url( 'styles.css', __FILE__ ) ); | |
wp_register_script('morgans-post-carousel-js', plugins_url('jcarousellite_1.0.1.js', __FILE__ ), array( 'jquery' ) ); | |
wp_register_script( 'morgans-featured', plugins_url( 'scripts.js', __FILE__ ), array( 'morgans-post-carousel-js' ) ); | |
} | |
/** | |
* Admin menu option | |
*/ | |
function add_submenu() { | |
$page = add_posts_page( | |
'Morgans Featured', 'Featured', 'publish_posts', | |
'featured_published_posts', array( $this, 'render_page' ) | |
); | |
add_action( 'admin_print_styles-' . $page, array( $this, 'mfa_admin_styles' ) ); | |
add_action( 'admin_print_scripts-' . $page, array( $this, 'mfa_admin_scripts' )); | |
} | |
/** | |
* Renders Admin Page | |
*/ | |
function render_page() { | |
require_once( MFEATURED_PATH_ADMIN . 'class-morgan-featured-list.php' ); | |
$this->table_list = new Morgan_Featured_List; | |
$this->table_list->set_tab( $this->selected_tab ); | |
$this->table_list->prepare_items(); | |
include_once( MFEATURED_PATH_ADMIN . 'template.php' ); | |
} | |
/** | |
* Enqueu style sheets | |
*/ | |
function mfa_admin_styles() { | |
wp_enqueue_style( 'wp-mf-style' ); | |
} | |
/** | |
* Enqueu style sheets | |
*/ | |
function mfa_admin_scripts() { | |
wp_enqueue_script( 'morgans-featured' ); | |
} | |
/** | |
* Removes location from that post | |
*/ | |
function remove_post_location() { | |
$post_id = (int) $_REQUEST[ 'post_id' ]; | |
$location = $_REQUEST[ 'location' ]; | |
$meta = get_post_meta( $post_id, 'mhg_feat_data' ); | |
$meta = $meta[0]; | |
if( ( $idx = array_search( $location, $meta ) ) ) { | |
unset( $meta[ $idx ] ); | |
$meta = array_values( $meta ); | |
} | |
die( update_post_meta( $post_id, 'mhg_feat_data', $meta ) ); | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(!class_exists('WP_List_Table')){ | |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); | |
} | |
/** | |
* Morgans_Featured_Admin | |
* | |
* Morgan's Featured Post List | |
* | |
* @author Carlos Barrantes | |
* | |
*/ | |
class Morgan_Featured_List extends WP_List_Table { | |
var $mhg_items_per_page = 10; | |
var $items = array(); | |
var $types_map = array( 'home' => 'home', 'location'=> 'loc_', 'property' => 'pro_' ); | |
function __construct(){ | |
parent::__construct( array( | |
'singular' => 'post', //singular name of the listed records | |
'plural' => 'posts', //plural name of the listed records | |
'ajax' => false //does this table support ajax? | |
) ); | |
} | |
function set_tab( $tab ) { | |
$this->tab = $tab; | |
} | |
/** | |
* | |
*/ | |
function get_columns() { | |
$columns = array( | |
'switch' => '<span class="switcher"></span>', | |
'title' => '', | |
); | |
return $columns; | |
} | |
/** | |
* Default column constructor | |
*/ | |
function column_default( $item, $column_name ){ | |
return $item[$column_name]; | |
} | |
/** | |
* Switch column constructor | |
*/ | |
function column_switch( $item ) { | |
return '<span data-action="switcher" class="switcher off"></span>'; | |
} | |
/** | |
* Display default and collapsed row | |
*/ | |
function single_row( $item ) { | |
static $row_class = ''; | |
$row_class = ( $row_class == '' ? ' class="alternate"' : '' ); | |
echo '<tr' . $row_class . ' data-row="data">'; | |
echo $this->single_row_columns( $item ); | |
echo '</tr>'; | |
echo '<tr' . $row_class . '>'; | |
echo '<td class="column-description" colspan="2" data-col="preview">'; | |
echo '<div class="featured-post-wrapper">'; | |
echo '<button class="arrow prev button"><<</button>'; | |
echo '<div class="list-wrapper">'; | |
echo '<div class="featured-post-list-wrapper">'; | |
echo '<ul>'; | |
foreach ( $item['posts'] as $post ) { | |
//for ( $i=0; $i < 5; $i++ ) { //dev | |
$href = add_query_arg( array( 'post_id' => $post[ 'id' ], 'location' => $post[ 'location' ] ) ); | |
echo '<li class="featured-post">'; | |
echo '<div>'; | |
echo '<img width="130" height="130" src="'. $post['image'] .'"/> <p>'. $post['description'] .'</p> | |
<p><a href="'. $href .'" data-post-id="'. $post[ 'id' ] .'" data-location="'. $post[ 'location' ] .'" class="button">Remove</a></p></div>'; | |
echo '</li>'; | |
//} | |
} | |
echo '</ul>'; | |
echo '</div>'; | |
echo '</div>'; | |
echo '<button class="arrow next button">>></button>'; | |
echo '</div>'; | |
echo '</td>'; | |
echo '</tr>'; | |
} | |
/** | |
* Get Featured published posts | |
*/ | |
function get_featured_items() { | |
$type = $this->types_map[ $this->tab ]; | |
$items = $matches = $locations = $skip = array(); | |
$is_home = false; | |
$args = array( | |
'posts_per_page' => 5, | |
'offset' => 0, | |
'category' => '', | |
'orderby' => 'post_date', | |
'order' => 'DESC', | |
'include' => '', | |
'exclude' => '', | |
'meta_key' => '', | |
'meta_value' => '', | |
'post_type' => 'post', | |
'post_mime_type' => '', | |
'post_parent' => '', | |
'post_status' => 'publish', | |
'suppress_filters' => true ); | |
$posts = get_posts( $args ); | |
if ( !empty( $posts ) ) { | |
foreach ($posts as $post) { | |
$meta = get_post_meta( $post->ID, 'mhg_feat_data' ); | |
if( empty( $meta[ 0 ] ) ) break; | |
$meta = $meta[0]; | |
if ( $meta[0] == 'home' && $this->tab == 'home' ) { | |
$is_home = true; | |
array_shift( $meta ); | |
} | |
foreach ($meta as $location) { | |
$location_name = substr( $location, 4 ); | |
$curr_type = substr( $location, 0, 4 ); | |
if( $is_home || $type == $curr_type ) { | |
if( !isset( $locations[ $location_name ] ) ) { | |
$locations[ $location_name ] = array(); | |
} | |
preg_match_all( '/<img [^>]*src=["|\']([^"|\']+)/i', $post->post_content, $matches ); | |
$temp = array( | |
'id' => $post->ID, | |
'image' => !empty( $matches[1] ) ? $matches[1][0] : '', | |
'description' => substr( $post->post_title, 0, 100 ), | |
'location' => $location, | |
); | |
array_push( $locations[ $location_name ], $temp ); | |
if ( in_array( $location_name, $skip ) ) { | |
continue; | |
}else{ | |
array_push( $skip, $location_name); | |
} | |
array_push( $items, array( | |
'title' => $location_name, | |
'posts' => &$locations[ $location_name ], | |
)); | |
} | |
} | |
} | |
} | |
return $items; | |
} | |
/** | |
* Prepare the items to be displayed | |
*/ | |
function prepare_items() { | |
$per_page = $this->mhg_items_per_page; | |
$columns = $this->get_columns(); | |
$hidden = array(); | |
$sortable = array(); | |
$this->_column_headers = array( $columns, $hidden, $sortable ); | |
$data = $this->items = $this->get_featured_items(); | |
$current_page = $this->get_pagenum(); | |
$total_items = count( $data ); | |
$data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page); | |
$this->set_pagination_args( array( | |
'total_items' => $total_items, | |
'per_page' => $per_page, | |
'total_pages' => ceil( $total_items / $per_page ) | |
) ); | |
} | |
/** | |
* Overwrite the super Display function to remove the top table navigator | |
* Display the table | |
*/ | |
function display() { | |
?> | |
<table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0"> | |
<thead> | |
<tr> | |
<?php $this->print_column_headers(); ?> | |
</tr> | |
</thead> | |
<tfoot> | |
<tr> | |
<?php $this->print_column_headers( false ); ?> | |
</tr> | |
</tfoot> | |
<tbody id="the-list"> | |
<?php $this->display_rows_or_placeholder(); ?> | |
</tbody> | |
</table> | |
<?php | |
$this->display_tablenav( 'bottom' ); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment