Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single project view #140

Merged
merged 9 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions wp-content/themes/currentorg/css/current-ltw-projects.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wp-content/themes/currentorg/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,4 @@ function largo_excerpt( $the_post = null, $sentence_count = 5, $use_more = null,

return $output;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,58 @@ function current_ltw_projects_assets() {
wp_register_script(
'current-ltw-script',
get_stylesheet_directory_uri() . '/js/current-ltw-projects.js',
array(),
array( 'jquery' ),
filemtime( get_stylesheet_directory() . '/js/current-ltw-projects.js' ),
true
);
wp_localize_script(
'current-ltw-script',
'ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
);
}
add_action( 'wp_enqueue_scripts', 'current_ltw_projects_assets' );

/**
* Load a single project when it is selected from the project list
*/
function current_ltw_projects_load_single_project_callback() {

// make sure post id is set and actually a valid post
if( isset( $_GET['post_id'] ) && get_post_status( $_GET['post_id'] ) ) {

$post_id = $_GET["post_id"];

// set up basic args for our query to grab the post
$single_project_args = array(
'post_type' => 'projects',
'p' => $post_id
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be sanitized; since post IDs are always integers we could do abs(intval( $post_id ))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 554eded

);

$single_project_query = new WP_Query( $single_project_args );

if( $single_project_query->have_posts() ){

while ( $single_project_query->have_posts() ) : $single_project_query->the_post();

// show singular project with the project single template partial
get_template_part( 'partials/projects', 'single-holder' );

endwhile;

}

} else {

echo '<p>The project could not be found.</p>';

}

wp_die();

}
add_action( 'wp_ajax_load_more_post', 'current_ltw_projects_load_single_project_callback' );
add_action( 'wp_ajax_nopriv_load_more_post', 'current_ltw_projects_load_single_project_callback' );
/**
* Filter queries for the projects post type
*/
Expand Down
53 changes: 53 additions & 0 deletions wp-content/themes/currentorg/js/current-ltw-projects.js
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
var $ = jQuery;
$(document).ready(function(){

current_ltw_projects_load_param_project_if_exists();

$( ".projects-list article .entry-title a" ).click(function(e){
e.preventDefault();
var post_id = $(this).data("post-id");
current_ltw_projects_load_single_project(post_id);
});

});

/**
* Function to load the single project partial for whatever project
* is provided.
*
* @param int post_id The id of the project we want to load
*/
function current_ltw_projects_load_single_project( post_id ) {

// give a reassuring "loading" notice
$(".projects-single-holder").html("Loading project...");

// actually load the single project
$(".projects-single-holder").load(ajax_object.ajax_url+"?action=load_more_post&post_id="+post_id);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider https://current.test/ltw-test-page/ and the per-project page https://current.test/ltw-test-page/?project_id=3251741:

On page load, https://current.test/ltw-test-page/ displays in the single-project area the final post from the queue, which is the last item in the post list.

The DOM looks like this:

<div class="projects-single-holder">
	<h1 class="projects-single-title">Youth Reporting Institute</h1>
	....
</div>

Styles on .projects-single-holder give the project a single black outline.

Clicking on the last item in the list loads the post:

<div class="projects-single-holder">
	<div class="projects-single-holder">
		<h1 class="projects-single-title">Youth Reporting Institute</h1>
		....
	</div>
</div>

The post now has a double black outline.

Loading the project-specific URL https://current.test/ltw-test-page/?project_id=3251741 means that that project is, upon page load, queried for by JS and then loaded by JS:

<div class="projects-single-holder">
	<div class="projects-single-holder">
		<h1 class="projects-single-title">Youth Reporting Institute</h1>
		....
	</div>
</div>

The post now has a double black outline.

In conclusion: loading the project via JS means that we get an extra div.projects-single-holder.

  • should we have separate divs for inner and outer, so that the AJAX response can reply with only the inner div to be appended to the outer? This might require splitting partials/project-single-holder.php into two files: the holder and the held.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benlk I noticed that too and wanted to hear your opinion on what you think would be best. I think splitting up the "holder" and the "held" sounds like a viable solution.

Copy link
Collaborator

@benlk benlk Jun 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#140 (comment) explores a little more the holder/held distinction; I think what we'd end up with is:

div.project-single-layout
    button.project-single-close
    div.project-single-holder
         article.project-single-held

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benlk 8a616c2 starts the implementation of that structure


// return and let's be happy
return;

}

/**
* Fires on page load to check if a post_id query param is set
* If it is set, attemps to load the specified project by the ID given
*/
function current_ltw_projects_load_param_project_if_exists() {

// find all current query params
var get_url_params = new URLSearchParams(window.location.search);

// if the post id query param exists, let's use it
if(get_url_params.get('project_id') || Number.isInteger(get_url_params.get('project_id'))) {

var post_id = get_url_params.get('project_id');

// actually try and load the project
current_ltw_projects_load_single_project( post_id );

return;

}

}
12 changes: 12 additions & 0 deletions wp-content/themes/currentorg/less/current-ltw-projects.less
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@
.projects-single-holder {
border: 1px solid @grey-d;
padding: @common-whitespace * 0.5;
.entry-content {
.project-tags {
margin-left: 0;
}
label {
font-weight: bold;
display: inline;
}
.project-specific-link {
display: block;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix projects-list-item'); ?>>

<?php

echo '<div class="' . $entry_classes . '">';

if ( largo_has_categories_or_tags() ) {
Expand All @@ -43,15 +44,15 @@
?>

<h2 class="entry-title">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( array( 'before' => __( 'Permalink to', 'largo' ) . ' ' ) )?>" rel="bookmark"><?php the_title(); ?></a>
<a href="?project_id=<?php echo get_the_ID(); ?>" title="<?php the_title_attribute( array( 'before' => __( 'Permalink to', 'largo' ) . ' ' ) )?>" rel="bookmark" data-post-id="<?php echo get_the_ID(); ?>"><?php the_title(); ?></a>
</h2>

<?php
// we may need to redo these links as search query params instead
$status = get_the_terms( get_the_ID(), 'project-status' );
$categories = get_the_terms( get_the_ID(), 'project-category' );
// @todo this throws errors when no terms are found for this type of post
if ( is_array( $status) && is_array( $status ) ) {
if ( is_array( $status) && is_array( $categories ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch. 👍

$terms = array_merge( $status, $categories );
} else if ( is_array( $status ) ) {
$terms = $status;
Expand Down
3 changes: 1 addition & 2 deletions wp-content/themes/currentorg/partials/projects-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
// so we can pass $query to these
include( locate_template( 'partials/projects-search-form.php', false, false ) );
include( locate_template( 'partials/projects-list.php', false, false ) );

get_template_part( 'partials/projects-single-holder' );
include( locate_template( 'partials/projects-single-holder.php', false, false ) );
182 changes: 181 additions & 1 deletion wp-content/themes/currentorg/partials/projects-single-holder.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,183 @@
<?php

$args = array (
// post-specific, should probably not be filtered but may be useful
'post_id' => $post->ID,
'hero_class' => largo_hero_class( $post->ID, FALSE ),

// only used to determine the existence of a youtube_url
'values' => get_post_custom( $post->ID ),

// this should be filtered in the event of a term-specific archive
'featured' => false,

// $show_thumbnail does not control whether or not the thumbnail is displayed;
// it controls whether or not the thumbnail is displayed normally.
'show_thumbnail' => TRUE,
'show_excerpt' => TRUE,
);

$args = apply_filters( 'largo_content_partial_arguments', $args, get_queried_object() );

extract( $args );

$entry_classes = 'entry-content';

$show_top_tag = largo_has_categories_or_tags();

$custom = get_post_custom();

if ( $featured ) {
$entry_classes .= ' span10 with-hero';
$show_thumbnail = FALSE;
}
if( ! empty ( $custom['project-video'][0] ) ) {
$show_youtube = TRUE;
$show_thumbnail = FALSE;
}

?>
<div class="projects-single-holder">
<h3>Single project goes here.</h3>
<h1 class="projects-single-title"><?php echo the_title(); ?></h1>
<?php

echo '<div class="' . $entry_classes . '">';

// we may need to redo these links as search query params instead
$status = get_the_terms( get_the_ID(), 'project-status' );
$categories = get_the_terms( get_the_ID(), 'project-category' );
// @todo this throws errors when no terms are found for this type of post
if ( is_array( $status ) && is_array( $categories ) ) {
$terms = array_merge( $status, $categories );
} else if ( is_array( $status ) ) {
$terms = $status;
} else if ( is_array( $categories ) ) {
$terms = $categories;
} else {
$terms = array();
}

if ( ! empty( $terms ) ) {
echo '<ul class="project-tags">';
$terms_count = count( $terms );
$terms_index = 0;
$delimiter = ' | ';
foreach ( $terms as $term ) {
$terms_index++;
printf(
'<a class="project-tag %1$s-%2$s" href="%3$s">%4$s </a>',
esc_attr( $term->taxonomy ),
esc_attr( $term->slug ),
// @todo: make this be a link that triggers the search filter for this term
get_term_link( $term ),
esc_html( $term->name ),
);
if( $terms_index != $terms_count ){
echo '<span class="delimiter">' . $delimiter . '</span>';
}
}
echo '</ul>';
}

// thumbnail or video
if ( $show_thumbnail ) {
echo '<div class="has-thumbnail '.$hero_class.'"><a href="?project_id=' . get_the_ID() . '">' . get_the_post_thumbnail() . '</a></div>';
} else if( $show_youtube && wp_oembed_get( $custom['project-video'][0] ) ) {
echo wp_oembed_get( $custom['project-video'][0] );
}

// organization
if ( ! empty( $custom['project-organization'][0] ) ) {
printf(
'<div><label class="project-single-organization">%1$s:</label>
<span class="project-organization">%2$s</span></div>',
__( 'Organization', 'current-ltw-projects' ),
esc_html( $custom['project-organization'][0] )
);
}

// year submitted
printf(
'<div><label class="project-single-submission-year">%1$s:</label>
<span class="project-submission-year">%2$s</span></div>',
__( 'Year Submitted', 'current-ltw-projects' ),
get_the_date( 'Y' )
);

// type of org
$org_types = get_the_terms( get_the_ID(), 'project-org-type' );
if ( ! empty( $org_types ) ) {
$delimiter = ', ';
$org_types_count = count( $org_types );
$org_type_index = 0;
_e( '<div><label class="project-org-types-label">Type of Organization: </label>', 'current-ltw-projects' );
foreach ( $org_types as $org_type ) {
$org_type_index++;
printf(
'<a href="%3$s">%4$s</a>',
esc_attr( $org_type->taxonomy ),
esc_attr( $org_type->slug ),
// @todo: make this be a link that triggers the search filter for this term
get_term_link( $org_type ),
esc_html( $org_type->name )
);
if( $org_type_index != $org_types_count ){
echo '<span class="comma-delimiter">' . $delimiter . '</span>';
}
}
echo '</ul></div>';
}

// contact name and email
if ( ! empty( $custom['project-contact-name'][0] ) || ! empty( $custom['project-contact-email'] ) ) {
printf(
'<label class="project-single-contact">%1$s: </label>',
__( 'Contact', 'current-ltw-projects' ),
);
}

if ( ! empty( $custom['project-contact-name'][0] ) ) {
printf(
'<span class="project-contact-name">%1$s</span><br/>',
esc_html( $custom['project-contact-name'][0] )
);
}

if ( ! empty( $custom['project-contact-email'][0] ) ) {
printf(
'<span class="project-contact-email">%1$s</span>',
esc_html( $custom['project-contact-email'][0] )
);
}

// tell your story / main content
the_content();

// project revenue
if ( ! empty( $custom['project-revenue'][0] ) ) {
printf(
'<p class="project-revenue">%1$s</p>',
esc_html( $custom['project-revenue'][0] )
);
}

// specific impact
if ( ! empty( $custom['project-impact'][0] ) ) {
printf(
'<p class="project-specific-impact">%1$s</p>',
esc_html( $custom['project-impact'][0] )
);
}

// primary url
if ( ! empty( $custom['project-link'][0] ) ) {
printf(
'<label class="project-specific-link">%1$s:</label>
<a class="project-specific-link" href="%2$s" target="_blank">%2$s</a>',
__( 'Project Specific Link', 'current-ltw-projects' ),
esc_html( $custom['project-link'][0] )
);
}

?>
</div>