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
BackToTop.php
82 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Back To Top 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 | |
| 18 | /** |
| 19 | * Back To Top Query Part extension. |
| 20 | */ |
| 21 | class BackToTop extends Extension { |
| 22 | /** |
| 23 | * The attribute for this Query Part. |
| 24 | * |
| 25 | * @since 4.3.9 |
| 26 | * @var string |
| 27 | */ |
| 28 | public $attribute_name = 'back-to-top'; |
| 29 | |
| 30 | /** |
| 31 | * The default value for this Query Part. |
| 32 | * |
| 33 | * @since 4.3.9 |
| 34 | * @var string |
| 35 | */ |
| 36 | public $default_value = 'true'; |
| 37 | |
| 38 | /** |
| 39 | * Resolved "back to top" visibility for this listing. |
| 40 | * |
| 41 | * @var bool |
| 42 | */ |
| 43 | protected $show_back_to_top = true; |
| 44 | |
| 45 | /** |
| 46 | * Sanitize the shortcode attribute. |
| 47 | * |
| 48 | * @param mixed $value The value of the shortcode attribute. |
| 49 | * @param array $attributes The complete set of shortcode attributes. |
| 50 | * @return string |
| 51 | */ |
| 52 | public function sanitize_attribute( $value, array $attributes ) { |
| 53 | return alphalisting_is_truthy( $value ) ? 'true' : 'false'; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Update the query with this extension's additional configuration. |
| 58 | * |
| 59 | * @param mixed $query The query. |
| 60 | * @param string $display The display/query type. |
| 61 | * @param string $key The name of the attribute. |
| 62 | * @param mixed $value The shortcode attribute value. |
| 63 | * @param array $attributes The complete set of shortcode attributes. |
| 64 | * @return mixed The updated query. |
| 65 | */ |
| 66 | public function shortcode_query( $query, string $display, string $key, $value, array $attributes ) { |
| 67 | $this->show_back_to_top = alphalisting_is_truthy( $value ); |
| 68 | $this->add_hook( 'filter', 'alphalisting_show_back_to_top', array( $this, 'return_show_back_to_top' ), 10, 2 ); |
| 69 | return $query; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Return whether "Back to top" should be rendered. |
| 74 | * |
| 75 | * @param bool $show Whether to show the link. |
| 76 | * @return bool |
| 77 | */ |
| 78 | public function return_show_back_to_top( bool $show ): bool { |
| 79 | return $this->show_back_to_top; |
| 80 | } |
| 81 | } |
| 82 |