<?php

/* MOST PLAYED GAMES COUNTER */
  function getMostPlayed($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
    return "0";
  }
    return $count.'';
  }
  function setMostPlayed($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
    $count = 0;
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
  }else{
    $count++;
    update_post_meta($postID, $count_key, $count);
  }
  }

    add_filter('manage_posts_columns', 'posts_column_views');
    add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
    function posts_column_views($defaults){
    $defaults['post_views'] = __('Views/Plays', 'Gameleon');
    return $defaults;
}

    function posts_custom_column_views($column_name, $id){
    if($column_name === 'post_views'){
    echo getMostPlayed(get_the_ID());
}
}

/* RELATED GAMES FUNCTION. */
  function get_related_games($post_id, $tags = array()) {
    $query = new WP_Query();
    $post_types = get_post_types();
    unset($post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
    if($tags) {
    foreach($tags as $tag) {
    $tagsA[] = $tag->term_id;
  }
  }
    $args = wp_parse_args(array(
    'showposts' => 10,
    'cat'  => -get_option('tigu_exclude_related'), 
    'post_type' => $post_types,
    'post__not_in' => array($post_id),
    'ignore_sticky_posts' => 1,
));
    $query = new WP_Query($args);
    return $query;
  }


/* RELATED POSTS FUNCTION. */
  function get_related_posts($post_id, $tags = array()) {
    $query = new WP_Query();
    $post_types = get_post_types();
    unset($post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
    if($tags) {
    foreach($tags as $tag) {
    $tagsA[] = $tag->term_id;
  }
  }
    $args = wp_parse_args(array(
    'showposts' => 7,
    'cat'  => -get_option('tigu_exclude_related'), 
    'post_type' => $post_types,
    'post__not_in' => array($post_id),
    'ignore_sticky_posts' => 1,
));
    $query = new WP_Query($args);
    return $query;
  }


/* KRIESI PAGINATION */
  function kriesi_pagination($pages = '', $range = 2)
  {
    $showitems = ($range * 2)+1;
    global $paged;
    if(empty($paged)) $paged = 1;
    if($pages == '')
  {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages)
  {
    $pages = 1;
  }
  }
    if(1 != $pages)
  {
    echo "<div class='pagination'>";
    if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'><span class='arrows'>&laquo;</span> First</a>";
    if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'><span class='arrows'>&lsaquo;</span> Previous</a>";
    for ($i=1; $i <= $pages; $i++)
  {
    if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
  {
    echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
  }
  }
    if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>Next <span class='arrows'>&rsaquo;</span></a>";
    if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last <span class='arrows'>&raquo;</span></a>";
    echo "</div>\n";
  }
  }
  function string_limit_words($string, $word_limit)
  {
    $words = explode(' ', $string, ($word_limit + 1));
    if(count($words) > $word_limit) {
    array_pop($words);
  }
    return implode(' ', $words);
  }


/* Adding the game file, embeded code and game instructions to the editor */

    $prefix = 'custom_meta_';
    $meta_box = array(
    'fields' => array(
   array(
    'name'    => 'Upload your swf game file:',
    'desc'    => 'Upload your swf game file using upload/insert media button, or paste your embed code. <br />*<small>Note that if you are using embed code, you should use the HTML tab instead of Visual. Anyway, sometimes you must play around with the dimension of the game, to display the proportional width and heigh.</small>',
    'id'      => $prefix . 'gamefile',
    'type'    => 'wysiwyg',
    'std' => '',
    'options' => array(
    'textarea_rows' => 3,
),
),
   array(
    'name' => 'Game Instructions:',
    'desc' => 'Instructions about how to play the game (key used to play, move, jump, etc.).',
    'id' => $prefix . 'instructions',
    'type'    => 'wysiwyg',
    'std' => '',
    'options' => array(
    'media_buttons' => false,
    'textarea_rows' => 3,

),
),
   array(
    'name' => 'Game Difficulty:',
    'id' => $prefix . 'difficulty',
    'type' => 'select',
    'options' => array('Very Easy', 'Easy', 'Medium', 'Hard', 'Very Hard' )

),
)
);

  function my_game_meta_add_box() {
    global $meta_box;

  }
  add_action('admin_menu', 'my_game_meta_add_box');
  function display_gamecode() {
    global $meta_box, $post;
    echo '<input type="hidden" name="flash_game_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    echo '<table class="form-table">';
  foreach ($meta_box['fields'] as $field) {
    $meta = get_post_meta($post->ID, $field['id'], true);
    echo '<tr>',
    '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
    '<td>';
    switch ($field['type']) {
  case 'wysiwyg':
    wp_editor( $meta ? $meta : $field['std'], $field['id'], isset( $field['options'] ) ? $field['options'] : array() );
    echo '<p>', $field['desc'], '</p>';
  break;
  case 'select':
    echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  foreach ($field['options'] as $option) {
    echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  }
    echo '</select>';
  break;
  }
    echo '<td>',
        '</tr>';
  }
    echo '</table>';
  }
  function my_game_meta_save_data($post_id) {
    global $meta_box;

 if( !isset( $_POST['flash_game_meta_box_nonce'] ) || !wp_verify_nonce($_POST['flash_game_meta_box_nonce'], basename(__FILE__))) {
    return $post_id;
  }
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;
  }
    if ('page' == $_POST['post_type']) {
    if (!current_user_can('edit_page', $post_id)) {
    return $post_id;
  }
  } elseif (!current_user_can('edit_post', $post_id)) {
    return $post_id;
  }
  foreach ($meta_box['fields'] as $field) {
    $old = get_post_meta($post_id, $field['id'], true);
    $new = $_POST[$field['id']];
    if ($new && $new != $old) {
    update_post_meta($post_id, $field['id'], $new);
  } elseif ('' == $new && $old) {
    delete_post_meta($post_id, $field['id'], $old);
  }
  }
  }

/* GET CATEGORY COUNT */
function wt_get_category_count($input = '') {
	global $wpdb;
	if($input == '')
	{
		$category = get_the_category();
		return $category[0]->category_count;
	}
	elseif(is_numeric($input))
	{
		$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input";
		return $wpdb->get_var($SQL);
	}
	else
	{
		$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug='$input'";
		return $wpdb->get_var($SQL);
	}
}


/* CUSTOM EXCERPT FUNCTION */
function get_excerpt($length) {
global $post;
$text = $post->post_excerpt;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
$text = strip_shortcodes($text);
$text = strip_tags($text); 
$text = mb_substr($text,0,$length).' ...';
echo $text;
}

add_filter('the_content', 'shortcode_empty_paragraph_fix');
    function shortcode_empty_paragraph_fix($content)
    {   
        $array = array (
            '<p>[' => '[', 
            ']</p>' => ']', 
            ']<br />' => ']'
        );
        $content = strtr($content, $array);
		return $content;
    }
function themesettings_admin_head() { ?>

<script type="text/javascript">
jQuery(function(){
var message = '<p><?php _e("Theme Activation Successful! The Gameleon Theme comes with an", "Gameleon"); ?> <a href="<?php echo admin_url('admin.php?page=themeoption'); ?>"><?php _e("OPTIONS PANEL", "Gameleon"); ?></a> <?php _e("to configure its settings. This theme also supports widgets, please visit the", "Gameleon"); ?> <a href="<?php echo admin_url('widgets.php'); ?>"><?php _e("WIDGETS SETTINGS PAGE", "Gameleon"); ?></a> <?php _e("to configure them", "Gameleon"); ?>.</p>';
jQuery('.themes-php #message2').html(message);
});
</script>

<?php }

add_action('admin_head', 'themesettings_admin_head');
  add_action('save_post', 'my_game_meta_save_data');
  remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);