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

Corrects Product Sync Issues #714

Merged
merged 3 commits into from Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion includes/admin/functions-tools.php
Expand Up @@ -364,7 +364,8 @@ function wpas_delete_synced_products( $resync = false ) {
}
if ( true === $resync ) {
/* Delete the initial synchronization marker so that it's done again */
delete_option( "wpas_sync_$post_type" );
// delete_option( "wpas_sync_$post_type" );
update_option( "wpas_sync_$post_type", 0 );
/* Synchronize */
$sync->run_initial_sync();
}
Expand Down
73 changes: 69 additions & 4 deletions includes/class-product-sync.php
Expand Up @@ -542,6 +542,28 @@ public function get_terms( $terms, $taxonomies, $args ) {
return $terms;
}

$index = array();
$sort = array(); // Used to store the orderby field from the term object.

// Set the array_multisort() arg flags based on the supplied orderby and order args.
if ( 'id' === $args['orderby'] ) {

$sort_flag = SORT_NUMERIC;

} else {

$sort_flag = SORT_REGULAR;
}

if ( 'DESC' === $args['order'] ) {

$sort_order = SORT_DESC;

} else {

$sort_order = SORT_ASC;
}

foreach ( $query->posts as $post ) {
if( isset( $post->ID ) ) {
$index[ $post->ID ] = $post;
Expand Down Expand Up @@ -569,11 +591,25 @@ public function get_terms( $terms, $taxonomies, $args ) {
}

if ( false !== $term ) {

$new_terms[] = apply_filters( 'wpas_get_terms_term', $term, $this->taxonomy );

if ( 'id' === $args['orderby'] ) {

$sort[] = (int) $term->{$args['orderby']};

} else {

$sort[] = strtolower( $term->{$args['orderby']} ); // Make lower case to get a natural sort since mixed case yields undesired results.
}

}

}

// Ensure terms are sorted according to the supplied args.
array_multisort( $sort, $sort_order, $sort_flag, $new_terms );

return apply_filters( 'wpas_get_terms', $new_terms );

}
Expand Down Expand Up @@ -872,7 +908,7 @@ public function notice_locked_tax() {

$message = apply_filters( 'wpas_taxonomy_locked_msg', sprintf( __( 'You cannot edit this term from here because it is linked to a post (of the %s post type). Please edit the post directly instead.', 'awesome-support' ), "<code>$this->post_type</code>" ) );

if ( $this->is_tax_screen() && true === $this->is_synced_term() ) { ?>
if ( $this->is_tax_screen() && true == $this->is_synced_term() ) { ?>
<div class="error">
<p><?php echo $message; ?></p>
</div>
Expand All @@ -894,7 +930,7 @@ public function lock_taxonomy() {

$message = apply_filters( 'wpas_taxonomy_locked_msg', sprintf( __( 'You cannot edit this term from here because it is linked to a post (of the %s post type). Please edit the post directly instead.', 'awesome-support' ), "<code>$this->post_type</code>" ) );

if ( $this->is_tax_screen() && true === $this->is_synced_term() ) {
if ( $this->is_tax_screen() && true == $this->is_synced_term() ) {
wp_die( $message, __( 'Term Locked', 'awesome-support' ), array( 'back_link' => true ) );
}

Expand All @@ -908,6 +944,25 @@ public function lock_taxonomy() {
*/
public function run_initial_sync() {

$slug = WPAS_eCommerce_Integration::get_instance()->plugin;

// Get the list of products to include/exclude
$raw_include = wpas_get_option( 'support_products_' . $slug . '_include', array() );
$raw_exclude = wpas_get_option( 'support_products_' . $slug . '_exclude', array() );

// Initialize empty arrays just in case the if statements below turn out to be true.
// $raw_exclude/include in the if statements below can be empty if the user did not click SAVE on the PRODUCTS configuration tab.
$include = array();
$exclude = array();

if ( ! empty( $raw_include ) ) {
$include = array_filter( $raw_include ); // Because of the "None" option, the option returns an array with an empty value if none is selected. We need to filter that
}

if ( ! empty( $raw_exclude ) ) {
$exclude = array_filter( $raw_exclude ); // Because of the "None" option, the option returns an array with an empty value if none is selected. We need to filter that
}

$args = array(
'post_type' => $this->post_type,
'post_status' => 'publish',
Expand All @@ -922,6 +977,14 @@ public function run_initial_sync() {
'update_post_meta_cache' => false,
);

if ( ! empty( $include ) ) {
$args['post__in'] = $include;
}

if ( ! empty( $exclude ) ) {
$args['post__not_in'] = $exclude;
}

$query = new WP_Query( $args );
$count = 0;

Expand All @@ -937,12 +1000,14 @@ public function run_initial_sync() {

/* If the term was successfully created we increment our counter */
if ( false !== $term ) {
++$count;
$count = get_option( "wpas_sync_$this->post_type", 0 );
//++$count;
update_option( "wpas_sync_$this->post_type", ++$count );
}

}

add_option( "wpas_sync_$this->post_type", $count );
// add_option( "wpas_sync_$this->post_type", $count );

return $count;

Expand Down