Astra_List_Font_Families::execute( array $args )

Execute the ability.


Description


Parameters

$args

(array) (Required) Input arguments.


Return

(array) Result array.


Source

File: inc/abilities/customizer/globals/typography/class-astra-list-font-families.php

	public function execute( $args ) {
		$limit        = isset( $args['limit'] ) ? absint( $args['limit'] ) : 20;
		$category     = isset( $args['category'] ) ? sanitize_text_field( $args['category'] ) : 'all';
		$search       = isset( $args['search'] ) ? sanitize_text_field( $args['search'] ) : '';
		$popular_only = isset( $args['popular_only'] ) ? (bool) $args['popular_only'] : true;

		$limit = min( $limit, 100 );

		if ( $popular_only ) {
			$fonts = $this->get_popular_fonts();
		} else {
			$fonts = $this->get_all_google_fonts();
		}

		if ( 'all' !== $category ) {
			$fonts = array_filter(
				$fonts,
				static function ( $font ) use ( $category ) {
					return isset( $font['category'] ) && $font['category'] === $category;
				}
			);
		}

		if ( ! empty( $search ) ) {
			$search_lower = strtolower( $search );
			$fonts        = array_filter(
				$fonts,
				static function ( $font ) use ( $search_lower ) {
					return strpos( strtolower( $font['name'] ), $search_lower ) !== false;
				}
			);
		}

		$fonts = array_slice( $fonts, 0, $limit );

		$category_stats = array();
		foreach ( $fonts as $font ) {
			$cat = $font['category'];
			if ( ! isset( $category_stats[ $cat ] ) ) {
				$category_stats[ $cat ] = 0;
			}
			$category_stats[ $cat ]++;
		}

		return Astra_Abilities_Response::success(
			/* translators: %d: number of font families */
			sprintf( __( 'Found %d font families.', 'astra' ), count( $fonts ) ),
			array(
				'fonts'      => array_values( $fonts ),
				'total'      => count( $fonts ),
				'statistics' => array(
					'by_category'  => $category_stats,
					'total_fonts'  => count( $fonts ),
					'popular_only' => $popular_only,
				),
				'filters'    => array(
					'category'     => $category,
					'search'       => $search,
					'limit'        => $limit,
					'popular_only' => $popular_only,
				),
			)
		);
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.