label lists for the filters that offer choices (countries, states, payment and shipping * methods, roles). Built once per request. Cities have no list on purpose: customers' cities are * never listed, the merchant types them. * * @since 1.0.0 */ final class Options { /** * Filters with a list. */ const LISTS = array( 'country', 'state', 'payment', 'shipping', 'role' ); /** * Lists built this request. * * @var array> */ private static $memo = array(); /** * A filter's choices. * * @since 1.0.0 * @param string $filter Filter (see LISTS). * @return array Code => label. */ public static function get( $filter ) { if ( ! in_array( $filter, self::LISTS, true ) ) { return array(); } if ( ! isset( self::$memo[ $filter ] ) ) { /** * Filter the choices an Analytics order filter offers (code => label). Country, state and * role codes are also checked against this list; payment and shipping codes are checked by * format only, so orders paid or shipped with methods removed since still match. * * @since 1.0.0 * @param array $options Code => label. * @param string $filter country, state (CC:ST), payment, shipping (type or type:instance) or role. */ $options = (array) apply_filters( 'apwc_analytics_filters_options', self::build( $filter ), $filter ); $clean = array(); foreach ( $options as $code => $label ) { if ( is_scalar( $label ) && '' !== (string) $code ) { $clean[ (string) $code ] = wp_strip_all_tags( html_entity_decode( (string) $label, ENT_QUOTES, 'UTF-8' ) ); } } self::$memo[ $filter ] = $clean; } return self::$memo[ $filter ]; } /** * Forget built lists (tests, or after roles or methods change mid-request). * * @since 1.0.0 * @return void */ public static function reset() { self::$memo = array(); } /** * Lists for the browser: [ { value, label } ] per filter, in display order. * * @since 1.0.0 * @param string[] $filters Filters. * @return array> */ public static function for_js( array $filters ) { $out = array(); foreach ( array_intersect( self::LISTS, $filters ) as $filter ) { $out[ $filter ] = array(); foreach ( self::get( $filter ) as $code => $label ) { $out[ $filter ][] = array( 'value' => (string) $code, 'label' => $label, ); } } return $out; } /** * Unfiltered list. * * @param string $filter Filter. * @return array */ private static function build( $filter ) { switch ( $filter ) { case 'country': return Lookup::countries(); case 'state': return Lookup::states(); case 'payment': return Lookup::gateways(); case 'shipping': return Lookup::shipping_methods(); case 'role': // Lookup's list with our prefixed guest key (a role could be called "guest"). $roles = Lookup::roles(); unset( $roles['guest'] ); return array( Params::GUEST => __( 'Guest (no account)', 'admin-powerups-for-woocommerce' ) ) + $roles; } return array(); } }