label (plain text). */ final class Options { /** * The options of a select2 control. * * @param array $control The control (source, post_type, taxonomy, multiple, zero, options). * @param array $sources The product's providers: source => callable( array $control ): ?array. * @param int $max The most items in a list. * @param array $strings The factory's words (`current`, `from_page`, `untitled`). * @return array|null */ public static function for_control( array $control, array $sources, int $max, array $strings ): ?array { if ( isset( $control['options'] ) && is_array( $control['options'] ) ) { return array_map( 'strval', $control['options'] ); } $source = (string) ( $control['source'] ?? '' ); $multiple = ! empty( $control['multiple'] ); if ( isset( $sources[ $source ] ) && is_callable( $sources[ $source ] ) ) { $list = call_user_func( $sources[ $source ], $control ); return is_array( $list ) ? array_map( 'strval', $list ) : null; } switch ( $source ) { case 'post': $zero = $multiple ? '' : (string) ( $control['zero'] ?? ( $strings['current'] ?? '' ) ); return self::posts( (string) ( $control['post_type'] ?? 'post' ), $zero, $max, (string) ( $strings['untitled'] ?? '#%d' ) ); case 'term': $zero = $multiple ? '' : (string) ( $control['zero'] ?? ( $strings['from_page'] ?? '' ) ); return self::terms( (string) ( $control['taxonomy'] ?? '' ), $zero, $max ); } return array(); } /** * Published posts of a type by title, in one query (no meta or term priming); "0" first when a zero label is * given; null above the limit. * * @param string $post_type Post type. * @param string $zero The label of "0" ('' for none). * @param int $max The most posts. * @param string $untitled The label of a post without a title (%d: its ID). * @return array|null */ public static function posts( string $post_type, string $zero, int $max, string $untitled ): ?array { $out = '' !== $zero ? array( '0' => $zero ) : array(); if ( '' === $post_type || ! post_type_exists( $post_type ) ) { return $out; } $posts = get_posts( array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => $max + 1, 'orderby' => 'title', 'order' => 'ASC', 'no_found_rows' => true, 'suppress_filters' => false, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); if ( count( $posts ) > $max ) { return null; } foreach ( $posts as $post ) { $title = wp_strip_all_tags( get_the_title( $post ) ); $out[ (string) $post->ID ] = '' !== $title ? $title : sprintf( $untitled, (int) $post->ID ); } return $out; } /** * The terms of a taxonomy by name (term IDs); "0" first when a zero label is given; null above the limit. * * @param string $taxonomy Taxonomy. * @param string $zero The label of "0" ('' for none). * @param int $max The most terms. * @return array|null */ public static function terms( string $taxonomy, string $zero, int $max ): ?array { $out = '' !== $zero ? array( '0' => $zero ) : array(); if ( '' === $taxonomy || ! taxonomy_exists( $taxonomy ) ) { return $out; } $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'number' => $max + 1, 'orderby' => 'name', ) ); if ( ! is_array( $terms ) ) { return $out; } if ( count( $terms ) > $max ) { return null; } foreach ( $terms as $term ) { if ( $term instanceof \WP_Term ) { $out[ (string) $term->term_id ] = $term->name; } } return $out; } }