PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.4.0
AlphaListing v4.4.0
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 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
Columns.php
122 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 * @param mixed $query The listing query instance passed by the filter.
79 * @param mixed $instance_id The listing instance id passed by the filter.
80 * @return string
81 */
82 public function return_styles($styles, $query = null, $instance_id = null): string {
83 return sprintf(
84 "%s --alphalisting-column-count: %d; ",
85 $styles,
86 $this->columns,
87 );
88 }
89
90 /**
91 * Ensure the provided column count is a safe integer.
92 *
93 * @param mixed $value Potential column count.
94 * @return int
95 */
96 protected function sanitize_column_count($value): int {
97 if (is_string($value)) {
98 $value = trim($value);
99 }
100
101 if ("" === $value || null === $value) {
102 return self::DEFAULT_COLUMN_COUNT;
103 }
104
105 if (is_numeric($value)) {
106 $value = (int) floor((float) $value);
107 } else {
108 return self::DEFAULT_COLUMN_COUNT;
109 }
110
111 if ($value < self::MIN_COLUMN_COUNT) {
112 return self::MIN_COLUMN_COUNT;
113 }
114
115 if ($value > self::MAX_COLUMN_COUNT) {
116 return self::MAX_COLUMN_COUNT;
117 }
118
119 return $value;
120 }
121 }
122