Skip to content

Commit

Permalink
v2.9
Browse files Browse the repository at this point in the history
better public filter
  • Loading branch information
aurovrata committed Nov 19, 2020
1 parent 290de98 commit 195b4b5
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 69 deletions.
16 changes: 15 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Due to a [bug](https://core.trac.wordpress.org/ticket/50070) in WordPress core,

v2.3 is now multi-post taxonomy enabled. A taxonomy registered with multiple post types can has its term's posts in each type ranked manually and separately.

**UPGRADE NOTE** if you are upgrading from v1.x, your old ranking data remains unaffected in the custom table used by the v1.x plugin. However, in v2.x all the ranking is now stored as post meta. While upgrading, some users have complained of missing posts/lost rankings. If this is the case, you can reset your order for given term using the reset checkbox/button provided in the admin page (see screenshot #4). It will reload the ranking from the v1.x custom table. For more details please read this [post](https://wordpress.org/support/topic/how-to-upgrade-to-v-2-x/).
**UPGRADE NOTE** if you are upgrading from v1.x, your old ranking data remains unaffected in the custom table used by the v1.x plugin. However, in v2.x all the ranking is now stored as post meta. While upgrading, some users have complained of missing posts/lost rankings. If this is the case, you can reset your order for given term using the reset checkbox/button provided in the admin page (see screenshot #4). It will reload the ranking from the v1.x custom table. Please read FAQ #17 for more information on how to migrate your data.

If your term was not sorted in the v1.x table or you are upgrading from v2.0.x or v2.1.x, then the reset button will reload the post order as per the default WP post table listing, which can be changed using the filtrs provided (see FAQ #7).

Expand Down Expand Up @@ -314,12 +314,26 @@ $adj->prev_post; //this is the previous post ID, null is the post is the first i
$adj->next_post; //this is the next post ID, null is the post is the last in the list.

`
= 17. I have upgraded from v1.X, should I delete the old table ? =

The old plugin (v1.X) used a custom table to stored the manual order of posts. This legacy data will be imported into the new plugin when you upgrade. However, it is important that you delete the old table once you have successfully imported your ranking data.

**How so I know if the data was successfully imported?**
Go to your posts reorder admin pages, for each manually sorted term, check that your posts are in the correct order and that none are missing. If the lists of posts are correct (the right number of posts in the right order), **then move the 2nd posts to the first position and back to the 2nd position**. This will automatically save the ranked list into your database usign the latest format. Repeat for all manually sorted terms. Once they are all saved with the new format, you will need to delete the legacy table.

If you have spurious post listings then it is possible the imported data has been corrupted, you can reset it using the provided reset button, however, if the list of posts is still showing erroneous results then you will need to reset the manual ranking for that term one you have deleted the legacy table, and manually re-order your posts.

**How do I delete the legacy data? **
In your dashboard, navigate to the Settings->Reorder Posts page, scroll to the bottom of the page and proceed to the delete the legacy table under the section title: **'Delete old custom table from plugin v1.x'**.

== Thanks to ==
@maddogprod for helping resolve custom taxonomy front-end ordering.
@menard1965 for helping resolve `get_adjacent_post` prev/next ranked posts.
@alexjamesbishop/ for helping fix the 'orderby' bug.
== Changelog ==
= 2.9.0 =
* improved front-end query filter logic.
* improved upgrade messaging for legacy data.
= 2.8.0 =
* expose functionaliy to get adjacent posts.
= 2.7.8 =
Expand Down
7 changes: 7 additions & 0 deletions admin/class-reorder-post-within-categories-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Reorder_Post_Within_Categories_Admin {
public $adminOptionsName = "deefuse_ReOrderSettingAdminOptions";

public $old_table_name = "reorder_post_rel";
/** @since 2.9.0 flag terms with v1.x rankings */
private $old_ranking_exists =false;
/**
* Save plugin settings, to keep track of ugrades.
* @since 2.0.0
Expand Down Expand Up @@ -419,6 +421,8 @@ public function shuffle_order(){
*/
protected function _get_order($post_type, $term_id, $start=0, $length=null){
global $wpdb;
$this->old_ranking_exists = false;

$query = $wpdb->prepare("SELECT rpwc_pm.post_id
FROM {$wpdb->postmeta} as rpwc_pm, {$wpdb->posts} as rpwc_p
WHERE rpwc_pm.meta_key ='_rpwc2'
Expand All @@ -442,6 +446,8 @@ protected function _get_order($post_type, $term_id, $start=0, $length=null){
FROM {$table_name} as rpwc
LEFT JOIN {$wpdb->posts} as wp on wp.ID = rpwc.post_id
WHERE rpwc.category_id = %d AND wp.post_type=%s order by rpwc.id", $term_id, $post_type));
/** @since 2.9.0 display admin notice warning. */
$this->old_ranking_exists = !empty($ranking);
}
// debug_msg($ranking, "ranking " );
if(empty($ranking)){
Expand Down Expand Up @@ -700,6 +706,7 @@ public function print_order_page(){
);
$posts_array = get_posts($args);
/** @since 2.4.1 better for multi post type */
// debug_msg($post_type_detail->name, $cat_to_retrieve_post);
$total = $this->count_posts_in_term($post_type_detail->name, $cat_to_retrieve_post);
$total = $total[$cat_to_retrieve_post];
foreach($posts_array as $post) $posts[$post->ID]=$post;
Expand Down
6 changes: 6 additions & 0 deletions admin/css/reorder-post-within-categories-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
* All of the CSS for your admin-specific functionality should be
* included in this file.
*/
p.warning {
padding: 10px;
border: solid 1px #ff9f9f;
background-color: #e3e3e0;
color: black;
}
.sortable-items:hover > .title.future::before,
.sortable-items:hover > .title.private::before,
.sortable-items:hover > .title.draft::before,
Expand Down
11 changes: 7 additions & 4 deletions admin/partials/reorder-post-within-categories-admin-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@
</label><br/>
<label for="override-orderby">
<input type="checkbox" <?=$disabled?><?=$override?>id="override-orderby" class="settings"/>
<span><?=__('Override &apos;orderby&apos; query attribute, (more <a href="https://wordpress.org/plugins/reorder-post-within-categories/#%0A10.%20my%20posts%20are%20not%20being%20ranked%20on%20the%20front-end%0A">detail</a>)', 'reorder-post-within-categories')?></span>
<span><?=__("Override 'orderby' query attribute", 'reorder-post-within-categories')?></span>
</label>
<p>
<?=__('<strong>Caution: </strong>Overriding &apos;orderby&apos; query attribute can have important consequences on WooCommerce listings where themes can display products ranked on various parameters such as price. This option overrides all other sortings, read these <a href="https://wordpress.org/plugins/reorder-post-within-categories/#%0A10.%20my%20posts%20are%20not%20being%20ranked%20on%20the%20front-end%0A">details</a> to see how to gain a finer control over this.','reorder-post-within-categories')?>
<?=__('<strong>Caution: </strong>Overriding &apos;orderby&apos; query attribute can have important consequences on WooCommerce listings where themes can display products ranked on various parameters such as price. This option overrides all other sortings, read <a href="https://wordpress.org/plugins/reorder-post-within-categories/#faq">FAQ #10</a> to see how to gain a finer control over this.','reorder-post-within-categories')?>
</p>
</br/>
<div id="reset-order">
<h4 style="margin:5px 0"><?=__('Reset the order!', 'reorder-post-within-categories')?></h4>
<label for="reset-button">
<input type="checkbox" value="reset-button" id="enable-reset"/>
<?=__('reset order for all posts, <strong>careful</strong>, this cannot be undone!','reorder-post-within-categories')?>
<input type="checkbox" value="reset-button" id="enable-reset" />
<?=_e('reset order for all posts, <strong>careful</strong>, this cannot be undone!','reorder-post-within-categories')?>
</label>
<div>
<a class="button disabled"><?=__('Reset order','reorder-post-within-categories')?></a>
Expand All @@ -141,6 +141,9 @@
<input type="hidden" name="termID" id="termIDCat" value="<?=$cat_to_retrieve_post?>">
<span class="spinner" id="spinnerAjaxRadio"></span>
</div>
<?php if($this->old_ranking_exists):?>
<p class="warning"><?= __('NOTE: the plugin has detected that you have a v1.X legacy data table with an existing ranking for this term and has loaded the manual order found. However, please delete the legacy ranking data once you have successfully ranked all your posts, for more information (see <a href="https://wordpress.org/plugins/reorder-post-within-categories/#faq">FAQ #17</a>)','reorder-post-within-categories')?></p>
<?php endif;?>
<h3 class="floatLeft"><?=sprintf( __('Grid of %s, classified as %s:', 'reorder-post-within-categories'), $post_type_detail->labels->name, $term_selected)?></h3>
<span id="spinnerAjaxUserOrdering" class="spinner"></span>
<div class="clearBoth"></div>
Expand Down
102 changes: 40 additions & 62 deletions public/class-reorder-post-within-categories-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class Reorder_Post_Within_Categories_Public {
*/
private $plugin_name;

/**
* The store the manually ranked terms.
*
* @since 2.9.0
* @access private
* @var Array $ranked_terms term IDs => post_types.
* @var Array $tax_options plugin's stored options.
*/
static private $ranked_terms;
static private $tax_options;
/**
* The version of this plugin.
*
Expand All @@ -54,51 +64,6 @@ public function __construct( $plugin_name, $version ) {

}

/**
* Register the stylesheets for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_styles() {

/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Reorder_Post_Within_Categories_Loader as all of the hooks are defined
* in that particular class.
*
* The Reorder_Post_Within_Categories_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/

wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/reorder-post-within-categories-public.css', array(), $this->version, 'all' );

}

/**
* Register the JavaScript for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_scripts() {

/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Reorder_Post_Within_Categories_Loader as all of the hooks are defined
* in that particular class.
*
* The Reorder_Post_Within_Categories_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/

wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/reorder-post-within-categories-public.js', array( 'jquery' ), $this->version, false );

}
/**
* filter post_join query.
* hooked on 'posts_join'.
Expand Down Expand Up @@ -175,9 +140,23 @@ private function is_manual_sort_query($wp_query, $print_dbg=false){
*/
static private function is_ranked($taxonomy, $term_id, &$type, $wp_query=null, $print_dbg=false){
if(empty($wp_query) && empty($type)) return false;
$tax_options = get_option(RPWC_OPTIONS_2, array());

// debug_msg($wp_query, 'query ');
/** @since 2.9.0 speedup ranking check */
if(!isset(self::$tax_options)){
self::$tax_options = get_option(RPWC_OPTIONS_2, array());
self::$ranked_terms = array();
foreach(self::$tax_options as $p=>$terms){
if($p){
foreach($terms as $tid=>$r){
if( isset(self::$ranked_terms[$tid]) ) array_push(self::$ranked_terms[$tid], $p);
else self::$ranked_terms[$tid] = array($p);
}
}
}
}
if(isset(self::$ranked_terms[$term_id])) $type = self::$ranked_terms[$term_id];
else return false; //term id is not manually ranked.

switch(true){
case 'any' == $type:
return false; //multi type search cannot be done.
Expand Down Expand Up @@ -227,21 +206,20 @@ static private function is_ranked($taxonomy, $term_id, &$type, $wp_query=null, $
if(empty($type)){
//find if any post types has multiple posts in this term.
foreach($post_type as $pt){
if(self::count_posts($pt, $term_id, $taxonomy) > 1){
$types_with_posts[]=$pt;
switch(true){
case ('attachment' == $pt): //unlikely being displayed.
break;
default:
if(empty($type)){
if($print_dbg) debug_msg("RPWC2 SORT VALIDATION, using type '{$pt}'.");
$type = $pt;
}else{
if($print_dbg) debug_msg("RPWC2 SORT VALIDATION, ignoring type '{$pt}', if this is the post type you are trying to sort, use the 'rpwc2_filter_multiple_post_type' hook as detailed in FAQ #10.");
}
break;
}
}
// if(self::count_posts($pt, $term_id, $taxonomy) > 1){ already counted in admin.
$types_with_posts[]=$pt;
switch(true){
case ('attachment' == $pt): //unlikely being displayed.
break;
default:
if(empty($type)){
if($print_dbg) debug_msg("RPWC2 SORT VALIDATION, using type '{$pt}'.");
$type = $pt;
}else{
if($print_dbg) debug_msg("RPWC2 SORT VALIDATION, ignoring type '{$pt}', if this is the post type you are trying to sort, use the 'rpwc2_filter_multiple_post_type' hook as detailed in FAQ #10.");
}
break;
}
}
}
if(empty($type) || !is_string($type)){
Expand Down
4 changes: 2 additions & 2 deletions reorder-post-within-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Plugin Name: ReOrder Posts within Categories
* Plugin URI: https://github.com/aurovrata/ReOrder-posts-within-categories
* Description: This is a short description of what the plugin does. It's displayed in the WordPress admin area.
* Version: 2.8.0
* Version: 2.9.0
* Author: Aurorata V.
* Author URI: https://profiles.wordpress.org/aurovrata/
* License: GPL-2.0+
Expand All @@ -33,7 +33,7 @@
/**
* Current plugin version.
*/
define( 'REORDER_POST_WITHIN_CATEGORIES_VERSION', '2.8.0' );
define( 'REORDER_POST_WITHIN_CATEGORIES_VERSION', '2.9.0' );
//options for plugin used in both public and admin classes.
define('RPWC_OPTIONS', 'deefuse_ReOrderOrderedCategoriesOptions');
define('RPWC_OPTIONS_2', '_rpwc2_sort_options');
Expand Down

0 comments on commit 195b4b5

Please sign in to comment.