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

Fix bug with children listing categories in listing package #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions includes/components/class-listing-package.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function __construct( $args = [] ) {
add_filter( 'hivepress/v1/templates/listing_edit_page', [ $this, 'alter_listing_edit_page' ] );
}

// Update listing package categories.
add_action( 'hivepress/v1/models/listing_package/update_categories', [ $this, 'update_package_categories' ], 10, 2 );

parent::__construct( $args );
}

Expand Down Expand Up @@ -582,4 +585,36 @@ public function alter_listing_edit_page( $template ) {

return $template;
}

/**
* Update listing package categories.
*
* @param int $package_id Listing package ID.
* @param array $value Listing package categories ID.
*/
public function update_package_categories( $package_id, $value ) {
if ( ! $value ) {
return;
}

// Remove action.
remove_action( 'hivepress/v1/models/listing_package/update_categories', [ $this, 'update_package_categories' ], 10, 2 );

// Set parent and child categories.
$categories = (array) $value;

foreach ( (array) $value as $category ) {
$categories = array_merge( $categories, get_term_children( $category, 'hp_listing_category' ) );
}

// Get package.
$package = Models\Listing_Package::query()->get_by_id( $package_id );

if ( ! $package ) {
return;
}

// Update package categories.
$package->set_categories( $categories )->save_categories();
}
}
Loading