sanitize_column_count( $value ); } /** * Update the query with this extension's additional configuration. * * @param \eslin87\AlphaListing\Query $query The query. * @param string $display The display/query type. * @param string $key The name of the attribute. * @param mixed $value The shortcode attribute value. * @param array $attributes The complete set of shortcode attributes. * @return mixed The updated query. */ public function shortcode_query( $query, string $display, string $key, $value, array $attributes ) { $this->columns = $this->sanitize_column_count( $value ); $this->add_hook( 'filter', 'alphalisting_styles', array( $this, 'return_styles' ), 10, 3 ); return $query; } /** * Return the stylesheet for this instance. * * @param string $styles The stylesheet. * @param mixed $query The listing query instance passed by the filter. * @param mixed $instance_id The listing instance id passed by the filter. * @return string The stylesheet with the column count appended. */ public function return_styles( $styles, $query = null, $instance_id = null ): string { return sprintf( '%s --alphalisting-column-count: %d; ', $styles, $this->columns ); } /** * Ensure the provided column count is a safe integer. * * @param mixed $value Potential column count. * @return int The sanitized column count. */ protected function sanitize_column_count( $value ): int { if ( is_string( $value ) ) { $value = trim( $value ); } if ( '' === $value || null === $value ) { return self::DEFAULT_COLUMN_COUNT; } if ( is_numeric( $value ) ) { $value = (int) floor( (float) $value ); } else { return self::DEFAULT_COLUMN_COUNT; } if ( $value < self::MIN_COLUMN_COUNT ) { return self::MIN_COLUMN_COUNT; } if ( $value > self::MAX_COLUMN_COUNT ) { return self::MAX_COLUMN_COUNT; } return $value; } }