-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: develop
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,12 +135,14 @@ function cfth_thread_notice($posts, $query) { | |
|
||
function cfth_timeline_posts($term_id) { | ||
$term = get_term_by('id', $term_id, 'threads'); | ||
$term_meta = get_term_meta( $term_id, 'threads_meta', true ); | ||
$order = ( isset( $term_meta['order_term_meta'] )) ? $term_meta['order_term_meta'] : 'ASC'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is an extra set of parenthesis around the first item in your ternary operation here, you can remove these |
||
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; | ||
|
@@ -149,6 +151,9 @@ function cfth_timeline_posts($term_id) { | |
} | ||
|
||
function cfth_timeline_content($term_id) { | ||
$term = get_term_by('id', $term_id, 'threads'); | ||
$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(); | ||
|
@@ -170,7 +175,7 @@ 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; | ||
$prev->threads_data['time_offset'] = ( isset( $term_meta['order_term_meta'] ) && 'DESC' === $term_meta['order_term_meta']) ? $prev_timestamp - $this_timestamp : $this_timestamp - $prev_timestamp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a confusing way to handle this logic. I would separate the steps and include some documentation to explain why this is being handled in this way. That will make the code easier to maintain long-term. |
||
} | ||
$prev = $_post; | ||
} | ||
|
@@ -375,3 +380,58 @@ function cfth_timeline_css() { | |
<?php | ||
} | ||
|
||
// Meta field markup for add thread page | ||
function threads_taxonomy_add_new_meta_field() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pay attention to your function prefixes. This doesn't match the prefix used by all legacy functions. |
||
?> | ||
<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 _e( 'ASC', 'threads' ); ?></option> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs escaping, |
||
<option value="DESC"><?php _e( 'DESC', 'threads' ); ?></option> | ||
</select> | ||
<p class="description"><?php _e( 'Enter a value for this field','threads' ); ?></p> | ||
</div> | ||
<?php | ||
} | ||
|
||
add_action( 'threads_add_form_fields', 'threads_taxonomy_add_new_meta_field', 10, 2 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this action you've specified that our callback will accept 2 arguments but I see zero being captured in the function definition. |
||
|
||
// Meta field markup for edit thread page | ||
function threads_taxonomy_edit_meta_field( $term ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pay attention to your function prefixes. This doesn't match the prefix used by all legacy functions. |
||
$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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs escaping, |
||
<td> | ||
<select name="term_meta[order_term_meta]" id="term_meta[order_term_meta]"> | ||
<option value="ASC" <?php echo ( isset( $term_meta['order_term_meta'] ) && 'ASC' === $term_meta['order_term_meta'] )? 'selected' : ''; ?> ><?php _e( 'ASC', 'threads' ); ?></option> | ||
<option value="DESC" <?php echo ( isset( $term_meta['order_term_meta'] ) && 'DESC' === $term_meta['order_term_meta'] )? 'selected' : ''; ?>><?php _e( 'DESC', 'threads' ); ?></option> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WP has a helper function named <?php echo ( isset( $term_meta['order_term_meta'] ) && 'DESC' === $term_meta['order_term_meta'] )? 'selected' : ''; ?> can become this: <?php selected( $term_meta['order_term_meta'], 'DESC' ); ?> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs escaping, |
||
</select> | ||
<p class="description"><?php _e( 'Enter a value for this field','threads' ); ?></p> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs escaping, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This description is also very meaningless. Instead, it should probably read "Sort threaded posts with newest first (DESC), or oldest first (ASC)." You could also probably change the select input option names to be more humanized as well: |
||
</td> | ||
</tr> | ||
<?php | ||
} | ||
|
||
add_action( 'threads_edit_form_fields', 'threads_taxonomy_edit_meta_field', 10, 2 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On this action you've specified the callback expects 2 parameters but you're only using 1 in the function definition. |
||
|
||
// Save meta field for thread taxonomy. | ||
function save_threads_custom_meta( $term_id ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pay attention to your function prefixes. This function doesn't have a prefix at all. |
||
if ( isset( $_POST['term_meta'] ) ) { | ||
$term_meta = get_term_meta( $term_id, 'threads_meta', true ); | ||
$cat_keys = array_keys( $_POST['term_meta'] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A variable named |
||
|
||
foreach ( $cat_keys as $key ) { | ||
if ( isset ( $_POST['term_meta'][$key] ) ) { | ||
$term_meta[$key] = $_POST['term_meta'][$key]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is absolutely zero sanitization happening here and there definitely should be. What if someone hits the admin area with a POST request that contains malicious injection code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to iterate over a theoretical loop of term meta options? We've only defined a single option thus far. Instead of using a loop we could just work directly with that one single piece of meta and be done. |
||
} | ||
} | ||
|
||
update_term_meta( $term_id, 'threads_meta', $term_meta ); | ||
} | ||
} | ||
|
||
add_action( 'edited_threads', 'save_threads_custom_meta', 10, 2 ); | ||
add_action( 'create_threads', 'save_threads_custom_meta', 10, 2 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've declared here, twice, that you're passing 2 arguments to the callback function. However, the function only makes use of a single parameter |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function was introduced as of 4.4.0. Using it here means that the plugin's required minimum WP version will be 4.4.0. That means an update to the README and the plugin's heading docblock.
We should also, for good measure, confirm this function exists using
if ( function_exists( 'get_term_meta' ) )
simply because it is so new