Alphabet.php
2 weeks ago
BackToTop.php
2 weeks ago
ColumnGap.php
2 weeks ago
ColumnWidth.php
2 weeks ago
Columns.php
2 weeks ago
CssLengthCommon.php
2 weeks ago
ExcludePosts.php
2 weeks ago
ExcludeTerms.php
2 weeks ago
GroupBy.php
2 weeks ago
HideEmptyTerms.php
2 weeks ago
HideEmpty_Deprecated.php
2 weeks ago
InstanceId.php
2 weeks ago
ParentPost.php
2 weeks ago
ParentTermCommon.php
2 weeks ago
ParentTermId.php
2 weeks ago
ParentTermSlugOrId.php
2 weeks ago
PostType.php
2 weeks ago
PostsTerms.php
2 weeks ago
SymbolsFirst.php
2 weeks ago
Taxonomy.php
2 weeks ago
TermsCommon.php
2 weeks ago
TermsTerms.php
2 weeks ago
Columns.php
136 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Columns 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 | * Columns Query Part extension |
| 20 | */ |
| 21 | class Columns extends Extension { |
| 22 | /** |
| 23 | * The default number of columns. |
| 24 | * |
| 25 | * @since 4.3.2 |
| 26 | * @var int |
| 27 | */ |
| 28 | public const DEFAULT_COLUMN_COUNT = 3; |
| 29 | |
| 30 | /** |
| 31 | * The lowest number of columns that may be configured. |
| 32 | * |
| 33 | * @since 4.3.2 |
| 34 | * @var int |
| 35 | */ |
| 36 | public const MIN_COLUMN_COUNT = 1; |
| 37 | |
| 38 | /** |
| 39 | * The highest number of columns that may be configured. |
| 40 | * |
| 41 | * Matches MAX_POSTS_COLUMNS in scripts/blocks/constants.js. This is a different |
| 42 | * axis to the template's `.max-N-columns` class, which is derived from the item |
| 43 | * count -- the stylesheet combines the two with min(). |
| 44 | * |
| 45 | * @since 4.3.2 |
| 46 | * @var int |
| 47 | */ |
| 48 | public const MAX_COLUMN_COUNT = 15; |
| 49 | |
| 50 | /** |
| 51 | * The attribute for this Query Part. |
| 52 | * |
| 53 | * @since 4.0.0 |
| 54 | * @var string |
| 55 | */ |
| 56 | public $attribute_name = 'columns'; |
| 57 | |
| 58 | /** |
| 59 | * The number of columns. |
| 60 | * |
| 61 | * @var int |
| 62 | */ |
| 63 | public $columns = self::DEFAULT_COLUMN_COUNT; |
| 64 | |
| 65 | /** |
| 66 | * Sanitize the shortcode attribute. |
| 67 | * |
| 68 | * @param mixed $value The value of the shortcode attribute. |
| 69 | * @param array $attributes The complete set of shortcode attributes. |
| 70 | * @return int The sanitized column count. |
| 71 | */ |
| 72 | public function sanitize_attribute( $value, array $attributes ) { |
| 73 | return $this->sanitize_column_count( $value ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Update the query with this extension's additional configuration. |
| 78 | * |
| 79 | * @param \eslin87\AlphaListing\Query $query The query. |
| 80 | * @param string $display The display/query type. |
| 81 | * @param string $key The name of the attribute. |
| 82 | * @param mixed $value The shortcode attribute value. |
| 83 | * @param array $attributes The complete set of shortcode attributes. |
| 84 | * @return mixed The updated query. |
| 85 | */ |
| 86 | public function shortcode_query( $query, string $display, string $key, $value, array $attributes ) { |
| 87 | $this->columns = $this->sanitize_column_count( $value ); |
| 88 | $this->add_hook( 'filter', 'alphalisting_styles', array( $this, 'return_styles' ), 10, 3 ); |
| 89 | return $query; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Return the stylesheet for this instance. |
| 94 | * |
| 95 | * @param string $styles The stylesheet. |
| 96 | * @param mixed $query The listing query instance passed by the filter. |
| 97 | * @param mixed $instance_id The listing instance id passed by the filter. |
| 98 | * @return string The stylesheet with the column count appended. |
| 99 | */ |
| 100 | public function return_styles( $styles, $query = null, $instance_id = null ): string { |
| 101 | return sprintf( '%s --alphalisting-column-count: %d; ', $styles, $this->columns ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Ensure the provided column count is a safe integer. |
| 106 | * |
| 107 | * @param mixed $value Potential column count. |
| 108 | * @return int The sanitized column count. |
| 109 | */ |
| 110 | protected function sanitize_column_count( $value ): int { |
| 111 | if ( is_string( $value ) ) { |
| 112 | $value = trim( $value ); |
| 113 | } |
| 114 | |
| 115 | if ( '' === $value || null === $value ) { |
| 116 | return self::DEFAULT_COLUMN_COUNT; |
| 117 | } |
| 118 | |
| 119 | if ( is_numeric( $value ) ) { |
| 120 | $value = (int) floor( (float) $value ); |
| 121 | } else { |
| 122 | return self::DEFAULT_COLUMN_COUNT; |
| 123 | } |
| 124 | |
| 125 | if ( $value < self::MIN_COLUMN_COUNT ) { |
| 126 | return self::MIN_COLUMN_COUNT; |
| 127 | } |
| 128 | |
| 129 | if ( $value > self::MAX_COLUMN_COUNT ) { |
| 130 | return self::MAX_COLUMN_COUNT; |
| 131 | } |
| 132 | |
| 133 | return $value; |
| 134 | } |
| 135 | } |
| 136 |