PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.3.7
AlphaListing v4.3.7
trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / Shortcode / QueryParts / Columns.php
alphalisting / src / Shortcode / QueryParts Last commit date
Alphabet.php 6 months ago ColumnGap.php 6 months ago ColumnWidth.php 6 months ago Columns.php 6 months ago ExcludePosts.php 6 months ago ExcludeTerms.php 6 months ago HideEmptyTerms.php 6 months ago HideEmpty_Deprecated.php 6 months ago InstanceId.php 6 months ago ParentPost.php 6 months ago ParentTermCommon.php 6 months ago ParentTermId.php 6 months ago ParentTermSlugOrId.php 6 months ago PostType.php 6 months ago PostsTerms.php 6 months ago SymbolsFirst.php 6 months ago Taxonomy.php 6 months ago TermsCommon.php 6 months ago TermsTerms.php 6 months ago
Columns.php
120 lines
1 <?php
2 /**
3 * Alphabet 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 public const DEFAULT_COLUMN_COUNT = 3;
23 public const MIN_COLUMN_COUNT = 1;
24 public const MAX_COLUMN_COUNT = 15;
25
26 /**
27 * The attribute for this Query Part.
28 *
29 * @since 4.0.0
30 * @var string
31 */
32 public $attribute_name = "columns";
33
34 /**
35 * The number of columns.
36 *
37 * @var int
38 */
39 public $columns = self::DEFAULT_COLUMN_COUNT;
40
41 /**
42 * Sanitize the shortcode attribute.
43 *
44 * @param mixed $value The value of the shortcode attribute.
45 * @param array $attributes The complete set of shortcode attributes.
46 * @return int
47 */
48 public function sanitize_attribute($value, array $attributes) {
49 return $this->sanitize_column_count($value);
50 }
51
52 /**
53 * Update the query with this extension's additional configuration.
54 *
55 * @param \AlphaListing\Query $query The query.
56 * @param string $display The display/query type.
57 * @param string $key The name of the attribute.
58 * @param mixed $value The shortcode attribute value.
59 * @param array $attributes The complete set of shortcode attributes.
60 * @return mixed The updated query.
61 */
62 public function shortcode_query($query, string $display, string $key, $value, array $attributes) {
63 $this->columns = $this->sanitize_column_count($value);
64 $this->add_hook(
65 "filter",
66 "alphalisting_styles",
67 [$this, "return_styles"],
68 10,
69 3,
70 );
71 return $query;
72 }
73
74 /**
75 * Return the stylesheet for this instance.
76 *
77 * @param string $styles The stylesheet.
78 * @return string
79 */
80 public function return_styles($styles): string {
81 return sprintf(
82 "%s --alphalisting-column-count: %d; ",
83 $styles,
84 $this->columns,
85 );
86 }
87
88 /**
89 * Ensure the provided column count is a safe integer.
90 *
91 * @param mixed $value Potential column count.
92 * @return int
93 */
94 protected function sanitize_column_count($value): int {
95 if (is_string($value)) {
96 $value = trim($value);
97 }
98
99 if ("" === $value || null === $value) {
100 return self::DEFAULT_COLUMN_COUNT;
101 }
102
103 if (is_numeric($value)) {
104 $value = (int) floor((float) $value);
105 } else {
106 return self::DEFAULT_COLUMN_COUNT;
107 }
108
109 if ($value < self::MIN_COLUMN_COUNT) {
110 return self::MIN_COLUMN_COUNT;
111 }
112
113 if ($value > self::MAX_COLUMN_COUNT) {
114 return self::MAX_COLUMN_COUNT;
115 }
116
117 return $value;
118 }
119 }
120