scholarslab/ivanhoe

View on GitHub
single-ivanhoe_game.php

Summary

Maintainability
A
2 hrs
Test Coverage
<?php
    get_header();

    $original_query           = $wp_query;
    $ivanhoe_game_id          = $post->ID;
    $ivanhoe_parent_permalink = get_permalink( $post->ID );
    $role                     = ivanhoe_user_has_role( $post->ID );

    // Character list
    $character_args = array(
        'post_type' => 'ivanhoe_role',
        'post_parent' => $ivanhoe_game_id
    );
    $characters = new WP_Query ( $character_args );
    $character_posts = $characters->get_posts();


    // Pagination
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $pagination_args = array (
        'post_type'   => 'ivanhoe_move',
        'post_parent' => $post->ID,
        'paged'       => $paged
    );

?>

<?php if (have_posts()) : while(have_posts()) : the_post(); ?>

        <div>
        <!-- Shows either the make a move button or the make a role button -->
            <?php if ( is_user_logged_in() ) :

                if ( $role !== FALSE ) :

                ?>

                    <form name="move_info" action='<?php echo home_url(); ?>' method='get'>
                        <input type="hidden" name="ivanhoe" value="ivanhoe_move">
                        <input type='hidden' name='parent_post' value='<?php echo $ivanhoe_game_id; ?>'>
                        <input type='hidden' name='ivanhoe_role_id' value='<?php echo $role->ID; ?>'>
                        <input type="submit" name="movesubmit" value="<?php _e( 'Make a Move', 'ivanhoe' ); ?>" class="btn" id="respond-to-move">
                        <h3 id='multisource-title'>Responding to the following</h3>
                        <ul class="multisource-response">
                        </ul>
                    </form>

                <?php else : ?>

                    <?php $url = ivanhoe_role_form_url($post); ?>

                    <?php echo ivanhoe_a($url, 'Make a Role', 'class="btn"', ESCAPE_TEXT); ?>

                <?php endif; ?>

            <?php endif; ?>
        <!-- Ends section showing buttons -->

        </div>

        <!-- Shows game description -->

        <h3><?php _e( 'Game Description', 'ivanhoe' ); ?></h3>


        <?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?>

        <div id='game-excerpt'><?php the_content(); ?> </div>

        <!-- Shows role of current user -->

        <?php if ($role !== FALSE): ?>

        <h3><?php _e( 'Your Current Role', 'ivanhoe' ); ?></h3>
        <article class="role">
            <a href="<?php echo get_permalink( $role->ID); ?>" class="image-container"><?php echo get_the_post_thumbnail($role->ID, 'medium'); ?></a>
            <a href="<?php echo get_permalink( $role->ID ); ?>"><?php echo $role->post_title; ?></a>
        </article>

        <?php endif; ?>
        <!-- Ends section showing current role -->

        <!-- Shows list of other characters -->
        <?php
            if ( !empty( $character_posts ) ) :

            if ( is_user_logged_in() && $role !== FALSE ) :
                if (!($character_posts[0] == $role) || (sizeof($character_posts) > 1 )) : ?>
                <h3><?php _e( 'Other Characters', 'ivanhoe' ); ?></h3>
                <article>
                <?php if ( $characters->have_posts() ) : ?>
                    <ul class='character_list'>
                        <?php while ( $characters->have_posts() ) : $characters->the_post();
                            if ($post->ID == $role->ID) continue; ?>
                                <li class='role'>
                                    <a href="<?php echo get_permalink( $post->ID ); ?>" class="image-container"><?php echo get_the_post_thumbnail($post->ID, 'medium'); ?></a>
                                    <a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo $post->post_title; ?></a>
                                </li>
                        <?php endwhile; ?>
                    </ul>
                <?php endif;
                        endif; ?>
                </article>
                <?php wp_reset_postdata(); ?>

            <?php else : ?>
                <h3><?php _e( 'Characters', 'ivanhoe' ); ?></h3>
                <article>
                    <?php if ( $characters->have_posts() ) : ?>
                        <ul class='character_list'>
                    <?php while ( $characters->have_posts() ) : $characters->the_post(); ?>
                        <li class='role'>
                            <a href="<?php echo get_permalink( $post->ID ); ?>" class="image-container"><?php echo get_the_post_thumbnail($post->ID, 'medium'); ?></a>
                            <a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo $post->post_title; ?></a>
                        </li>
                    <?php endwhile; ?>
                        </ul>
                    <?php endif; ?>
                </article>
                <?php wp_reset_postdata(); ?>
                <?php endif;
            endif; ?>
        <!-- Ends section showing other characters -->

     </div>

<article class="game">




    <!-- Main content of page -->
    <header>
    <h1><?php the_title(); ?></h1>
    <p>
        <?php printf( __('Playing since: %s', 'ivanhoe' ), get_the_time('F j, Y') ); ?>
    </p>

    </header>

    <?php
    // removed pagination logic from here
    $wp_query = new WP_Query( $pagination_args );
    if ( $wp_query->have_posts()) : ?>

    <div id="moves">

        <?php
        while($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header>
               <?php if ( is_user_logged_in() ) :

                if ( $role !== FALSE ) :

                ?>
                <?php $ivanhoe_post_id=$post->ID; ?>
                <span class="new_source btn" data-title="<?php echo get_the_title($ivanhoe_post_id); ?>" data-value="<?php echo $ivanhoe_post_id; ?>">Add to Moves</span>
                <?php endif; endif; ?>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p><span class="byline"><?php the_author_posts_link(); ?></span>
            &middot; <time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('F j, Y'); ?></time></p>




            </header>

            <div class="excerpt">

                <?php
                    echo ivanhoe_media_excerpt();
                    the_excerpt();
                ?>

            </div>

            <div class="game-discussion-source">
                <?php echo ivanhoe_display_move_source( $post ); ?>
            </div>

            <div class="game-discussion-response">
                <?php echo ivanhoe_display_move_responses( $post ); ?>
            </div>

        </article>

        <?php endwhile; ?>

        <?php echo ivanhoe_paginate_links($wp_query);?>
    </div>

    <?php else : ?>

    <p><?php _e( 'There are no moves for this game.', 'ivanhoe' ); ?></p>

    <?php endif; ?>

<?php $wp_query = $original_query; ?>

</article>
<!-- Ends main content of page -->

<?php endwhile; endif; ?>

<?php get_footer(); ?>

<!--
  TODO: for some reason, loading JS with the enqueue script makes .readmore
  throw an exepction. Calling it from the site works.
-->

<script type="text/javascript">
    $(document).ready(function(){

        $('#moves').infinitescroll({
            navSelector: '#pagination',
            nextSelector: '#pagination .next',
            itemSelector: '#moves article.ivanhoe_move'
        });

         $('#game-excerpt').readmore({});

    });
</script>

<?php if ( is_user_logged_in() ) : ?>
    <script type="text/javascript">
        $(document).ready(function(){
         "use strict";

        //TODO: this can be broken in to seperate functions
        function multisource () {
            var ivanhoe_selected_moves = {};

            function update_button(){
                var li = $('.multisource-response li');
                var header = $('#multisource-title');

                //TODO: fix this
               /* if (!document.move_info === undefined) {
                  if (li.length === 0 ) {
                  } else {
                      document.move_info.
                      movesubmit.value="Respond";
                      header.show ();
                  }
                }*/

            if (li.length == 0){
                      document.move_info.movesubmit.value="Make a Move";
                      header.hide ();
                } else {
                    document.move_info.movesubmit.value="Respond";
                    header.show ();

                }

            }

            $('#moves').on('click', '.new_source.btn', function(){
                var $this = $(this);
                var value = $this.data('value');
                $this.addClass('clicked');
                if (ivanhoe_selected_moves[value] == null) {
                    $('.multisource-response').append
                    ("<li><input type='hidden' value='" + value + "' name='move_source[]'>" + $this.data('title') + "</li>").click
                    (function( event ) {
                        $(event.target).remove();
                        $this.removeClass('clicked');
                        update_button();
                        delete ivanhoe_selected_moves[value];
                    });
                    update_button();
                ivanhoe_selected_moves[value] = true;
                }
            });

            update_button();
        }

        multisource();

    });
    </script>
<?php endif;