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

Introduce setting for controlling thread sort order #3

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
=== Plugin Name ===
Contributors: alexkingorg, crowdfavorite
Tags: content, timeline, display, presentation, story, storyline, context
Requires at least: 3.5
Tested up to: 3.5.1
Requires at least: 4.4.0
Tested up to: 4.5.1
Stable tag: 1.0b1
License: GPLv2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down
83 changes: 81 additions & 2 deletions threads.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,16 @@ function cfth_thread_notice($posts, $query) {

function cfth_timeline_posts($term_id) {
$term = get_term_by('id', $term_id, 'threads');
if ( function_exists( 'get_term_meta' ) ) {
$term_meta = get_term_meta( $term_id, 'threads_meta', true );
}
$order = isset( $term_meta['order_term_meta'] ) ? $term_meta['order_term_meta'] : 'ASC';
if ($term) {
$query_params = apply_filters('threads_timeline_posts_query', array(
'posts_per_page' => -1,
'taxonomy' => 'threads',
'term' => $term->slug,
'order' => 'ASC',
'order' => $order,
));
$query = new WP_Query($query_params);
return $query->posts;
Expand All @@ -149,6 +153,11 @@ function cfth_timeline_posts($term_id) {
}

function cfth_timeline_content($term_id) {
$term = get_term_by('id', $term_id, 'threads');
if ( function_exists( 'get_term_meta' ) ) {
$term_meta = get_term_meta( $term_id, 'threads_meta', true );
}
$order = isset( $term_meta['order_term_meta'] ) ? $term_meta['order_term_meta'] : 'ASC';
$posts = cfth_timeline_posts($term_id);
if (!count($posts)) {
return array();
Expand All @@ -170,7 +179,14 @@ function cfth_timeline_content($term_id) {
if ($prev) {
$prev_timestamp = strtotime($prev->post_date_gmt);
$this_timestamp = strtotime($_post->post_date_gmt);
$prev->threads_data['time_offset'] = $this_timestamp - $prev_timestamp;

if( isset( $term_meta['order_term_meta'] ) && 'DESC' === $term_meta['order_term_meta'] ) {
// offset calculation if post order selected as DESC.
$prev->threads_data['time_offset'] = $prev_timestamp - $this_timestamp;
} else {
// offset calculation if post order selected as ASC.
$prev->threads_data['time_offset'] = $this_timestamp - $prev_timestamp;
}
}
$prev = $_post;
}
Expand Down Expand Up @@ -375,3 +391,66 @@ function cfth_timeline_css() {
<?php
}

/**
* Meta field markup for add thread page.
*
* @since 1.0b1
*/
function cfth_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="term_meta[order_term_meta]"><?php _e( 'Post order', 'threads' ); ?></label>
<select name="term_meta[order_term_meta]" id="term_meta[order_term_meta]">
<option value="ASC"><?php esc_html_e( 'Oldest to Newest (ASC)', 'threads' ); ?></option>
<option value="DESC"><?php esc_html_e( 'Newest to Oldest (DESC)', 'threads' ); ?></option>
</select>
<p class="description"><?php esc_html_e( 'Sort threaded posts with newest first (DESC), or oldest first (ASC).','threads' ); ?></p>
</div>
<?php
}

add_action( 'threads_add_form_fields', 'cfth_taxonomy_add_new_meta_field' );

/**
* Meta field markup for edit thread page.
*
* @since 1.0b1
*/
function cfth_taxonomy_edit_meta_field( $term ) {
if ( function_exists( 'get_term_meta' ) ) {
$term_meta = get_term_meta( $term->term_id, 'threads_meta', true );
}
?>

<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[order_term_meta]"><?php _e( 'Post order', 'threads' ); ?></label></th>

Choose a reason for hiding this comment

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

Needs escaping, esc_html_e() instead of _e()

<td>
<select name="term_meta[order_term_meta]" id="term_meta[order_term_meta]">
<option value="ASC" <?php selected( $term_meta['order_term_meta'], 'ASC' ); ?> ><?php esc_html_e( 'Oldest to Newest (ASC)', 'threads' ); ?></option>
<option value="DESC" <?php selected( $term_meta['order_term_meta'], 'DESC' ); ?> ><?php esc_html_e( 'Newest to Oldest (DESC)', 'threads' ); ?></option>
</select>
<p class="description"><?php esc_html_e( 'Sort threaded posts with newest first (DESC), or oldest first (ASC).','threads' ); ?></p>
</td>
</tr>
<?php
}

add_action( 'threads_edit_form_fields', 'cfth_taxonomy_edit_meta_field' );

/**
* Save meta field for thread taxonomy.
*
* @since 1.0b1
*/
function cfth_save_threads_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$term_meta = array_map( 'esc_attr', $_POST['term_meta'] );
if ( function_exists( 'update_term_meta' ) ) {
update_term_meta( $term_id, 'threads_meta', $term_meta );
}
}
}

add_action( 'edited_threads', 'cfth_save_threads_custom_meta' );
add_action( 'create_threads', 'cfth_save_threads_custom_meta' );