Alphabet.php
1 month ago
BackToTop.php
1 month ago
ColumnGap.php
1 month ago
ColumnWidth.php
1 month ago
Columns.php
1 month ago
ExcludePosts.php
1 month ago
ExcludeTerms.php
1 month ago
HideEmptyTerms.php
1 month ago
HideEmpty_Deprecated.php
1 month ago
InstanceId.php
1 month ago
ParentPost.php
1 month ago
ParentTermCommon.php
1 month ago
ParentTermId.php
1 month ago
ParentTermSlugOrId.php
1 month ago
PostType.php
1 month ago
PostsTerms.php
1 month ago
SymbolsFirst.php
1 month ago
Taxonomy.php
1 month ago
TermsCommon.php
1 month ago
TermsTerms.php
1 month ago
ExcludePosts.php
67 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Exclude Posts Query Part. |
| 4 | * |
| 5 | * @package alphalisting |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace eslin87\AlphaListing\Shortcode\QueryParts; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | use \eslin87\AlphaListing\Shortcode\Extension; |
| 17 | use \eslin87\AlphaListing\Strings; |
| 18 | |
| 19 | /** |
| 20 | * Exclude Posts Query Part extension |
| 21 | */ |
| 22 | class ExcludePosts extends Extension { |
| 23 | /** |
| 24 | * The attribute for this Query Part. |
| 25 | * |
| 26 | * @since 4.0.0 |
| 27 | * @var string |
| 28 | */ |
| 29 | public $attribute_name = 'exclude-posts'; |
| 30 | |
| 31 | /** |
| 32 | * The types of listing this shortcode extension may be used with. |
| 33 | * |
| 34 | * @since 4.0.0 |
| 35 | * @var array<string> |
| 36 | */ |
| 37 | public $display_types = array( 'posts' ); |
| 38 | |
| 39 | /** |
| 40 | * Update the query with this extension's additional configuration. |
| 41 | * |
| 42 | * @param \AlphaListing\Query $query The query. |
| 43 | * @param string $display The display/query type. |
| 44 | * @param string $key The name of the attribute. |
| 45 | * @param mixed $value The shortcode attribute value. |
| 46 | * @param array $attributes The complete set of shortcode attributes. |
| 47 | * @return mixed The updated query. |
| 48 | */ |
| 49 | public function shortcode_query_for_display_and_attribute( $query, string $display, string $key, $value, array $attributes ) { |
| 50 | $exclude_posts = Strings::maybe_mb_split( ',', $value ); |
| 51 | $exclude_posts = array_map( 'trim', $exclude_posts ); |
| 52 | $exclude_posts = array_map( 'intval', $exclude_posts ); |
| 53 | $exclude_posts = array_filter( |
| 54 | $exclude_posts, |
| 55 | function( int $value ): bool { |
| 56 | return 0 < $value; |
| 57 | } |
| 58 | ); |
| 59 | $exclude_posts = array_unique( $exclude_posts ); |
| 60 | |
| 61 | if ( ! empty( $exclude_posts ) ) { |
| 62 | $query = wp_parse_args( $query, array( 'post__not_in' => $exclude_posts ) ); |
| 63 | } |
| 64 | return $query; |
| 65 | } |
| 66 | } |
| 67 |