Skip to content

Commit

Permalink
Remove favorite button block, use shared block (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle authored Jul 16, 2024
1 parent 9f5384c commit eba66dc
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// Block files
require_once( __DIR__ . '/src/blocks/copy-button/index.php' );
require_once( __DIR__ . '/src/blocks/delete-button/index.php' );
require_once( __DIR__ . '/src/blocks/favorite-button/index.php' );
require_once( __DIR__ . '/src/blocks/pattern-preview/index.php' );
require_once( __DIR__ . '/src/blocks/pattern-thumbnail/index.php' );
require_once( __DIR__ . '/src/blocks/post-status/index.php' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@

namespace WordPressdotorg\Theme\Pattern_Directory_2024\Block_Config;

use WP_Block_Supports;
use WP_Block_Supports, WP_Error;
use const WordPressdotorg\Pattern_Directory\Pattern_Post_Type\POST_TYPE;
use function WordPressdotorg\Theme\Pattern_Directory_2024\get_patterns_count;
use function WordPressdotorg\Pattern_Directory\Favorite\{add_favorite, get_favorite_count, is_favorite, remove_favorite};

add_action( 'init', __NAMESPACE__ . '\register_block_bindings' );
add_filter( 'wporg_query_total_label', __NAMESPACE__ . '\update_query_total_label', 10, 2 );
add_filter( 'wporg_query_filter_options_curation', __NAMESPACE__ . '\get_curation_options' );
add_filter( 'wporg_query_filter_options_sort', __NAMESPACE__ . '\get_sort_options' );
add_action( 'wporg_query_filter_in_form', __NAMESPACE__ . '\inject_other_filters' );
add_filter( 'wporg_favorite_button_settings', __NAMESPACE__ . '\get_favorite_settings', 10, 2 );
add_filter( 'render_block_core/search', __NAMESPACE__ . '\inject_category_search_block' );
add_filter( 'wporg_block_navigation_menus', __NAMESPACE__ . '\add_site_navigation_menus' );
add_filter( 'render_block_core/query-title', __NAMESPACE__ . '\update_archive_title', 10, 3 );
Expand Down Expand Up @@ -268,6 +270,49 @@ function inject_other_filters( $key ) {
}
}

/**
* Configure the favorite button.
*
* @param array $settings Array of settings for this filter.
* @param int $post_id The current post ID.
*
* @return array|bool Settings array or false if not a theme.
*/
function get_favorite_settings( $settings, $post_id ) {
if ( ! $post_id ) {
return false;
}

return array(
'count' => get_favorite_count( $post_id ),
'is_favorite' => is_favorite( $post_id ),
'add_callback' => function( $_post_id ) {
$success = add_favorite( $_post_id );
if ( $success ) {
return get_favorite_count( $_post_id );
}

return new WP_Error(
'favorite-failed',
__( 'Unable to favorite this pattern.', 'wporg-patterns' ),
array( 'status' => 500 )
);
},
'delete_callback' => function( $_post_id ) {
$success = remove_favorite( $_post_id );
if ( $success ) {
return get_favorite_count( $_post_id );
}

return new WP_Error(
'unfavorite-failed',
__( 'Unable to remove this pattern from your favorites.', 'wporg-patterns' ),
array( 'status' => 500 )
);
},
);
}

/**
* Inject the current category into the search form.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<div class="wp-block-group">
<!-- wp:post-title {"isLink":true,"fontSize":"small","fontFamily":"inter"} /-->

<!-- wp:wporg/favorite-button {"variant":"small"} /-->
<!-- wp:wporg/favorite-button {"showCount":true,"variant":"small"} /-->
</div>
<!-- /wp:group -->
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<!-- wp:group {"layout":{"type":"flex","flexWrap":"wrap"}} -->
<div class="wp-block-group">
<!-- wp:wporg/copy-button /-->
<!-- wp:wporg/favorite-button /-->
<!-- wp:wporg/favorite-button {"showCount":true} /-->
<!-- wp:group {"style":{"spacing":{"blockGap":"var:preset|spacing|10"}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group">
<!-- wp:avatar {"size":24,"style":{"border":{"radius":"100%"}}} /-->
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit eba66dc

Please sign in to comment.