*/ public static function sanitize_slug_list( $raw ) { if ( is_array( $raw ) ) { $raw = implode( ',', $raw ); } $out = array(); foreach ( explode( ',', (string) $raw ) as $piece ) { $piece = trim( $piece ); if ( '' === $piece || '*' === $piece ) { continue; } $piece = sanitize_title( $piece ); if ( '' !== $piece ) { $out[ $piece ] = $piece; } } return array_values( $out ); } /** * Which categories this instance of the shortcode may show. * * An empty array means every category. * * @param string $category_attr The category attribute. * @param mixed $download_only The download_only attribute. * @return array */ public static function resolve_scope( $category_attr, $download_only ) { $scope = self::sanitize_slug_list( $category_attr ); if ( true !== $download_only && 'true' !== $download_only && '1' !== $download_only ) { return $scope; } $download = ACS_Portfolio_Download::get_download_category_slugs(); if ( empty( $download ) ) { return $scope; } if ( empty( $scope ) ) { return $download; } $both = array_values( array_intersect( $scope, $download ) ); return empty( $both ) ? array( self::NO_MATCH_SLUG ) : $both; } /** * Build a tax_query for a list of slugs. * * @param array $slugs Category slugs. * @return array> */ private static function scope_tax_query( $slugs ) { return array( array( 'taxonomy' => ACS_Portfolio_Post_Type::TAXONOMY, 'field' => 'slug', 'terms' => array_values( $slugs ), ), ); } /** * The sort options offered in the drop-down. * * @return array */ public static function sort_options() { $options = array( 'date_desc' => __( 'Newest first', 'acs-portfolio' ), 'date_asc' => __( 'Oldest first', 'acs-portfolio' ), 'title_asc' => __( 'Title A to Z', 'acs-portfolio' ), 'title_desc' => __( 'Title Z to A', 'acs-portfolio' ), ); /** * Filter the sort options. * * @param array $options Value => label. */ return (array) apply_filters( 'acs_portfolio_sort_options', $options ); } /** * Turn a sort value into query arguments. * * @param string $sort Sort value. * @return array */ public static function sort_args( $sort ) { switch ( $sort ) { case 'date_asc': $args = array( 'orderby' => 'date', 'order' => 'ASC', ); break; case 'title_asc': $args = array( 'orderby' => 'title', 'order' => 'ASC', ); break; case 'title_desc': $args = array( 'orderby' => 'title', 'order' => 'DESC', ); break; case 'date_desc': default: $args = array( 'orderby' => 'date', 'order' => 'DESC', ); break; } /** * Filter the query arguments produced by a sort value. * * @param array $args Query arguments. * @param string $sort The requested sort value. */ return (array) apply_filters( 'acs_portfolio_sort_args', $args, $sort ); } /** * Render the shortcode. * * @param array|string $atts Shortcode attributes. * @return string */ public function render( $atts ) { $atts = shortcode_atts( array( 'limit' => '-1', 'category' => '', 'download_only' => 'false', 'layout' => 'grid', 'columns' => '3', 'show_filter' => 'true', 'show_sort' => 'true', ), $atts, 'acs_portfolio' ); /** * Filter the resolved shortcode attributes. * * An add-on uses this to allow layouts and column counts this plugin * does not offer on its own. * * @param array $atts Attributes after defaults are applied. */ $atts = (array) apply_filters( 'acs_portfolio_shortcode_atts', $atts ); ACS_Portfolio_Core::need_frontend_assets(); $layout = in_array( $atts['layout'], array( 'grid', 'list' ), true ) ? $atts['layout'] : 'grid'; $columns = in_array( (string) $atts['columns'], array( '2', '3', '4' ), true ) ? (string) $atts['columns'] : '3'; $scope = self::resolve_scope( $atts['category'], $atts['download_only'] ); $classes = array( 'acs-portfolio-wrapper', 'acs-portfolio-layout-' . $layout, 'acs-portfolio-col-' . $columns, ); /** * Filter the wrapper classes. * * @param array $classes Class names. * @param array $atts Shortcode attributes. */ $classes = array_map( 'sanitize_html_class', (array) apply_filters( 'acs_portfolio_wrapper_classes', $classes, $atts ) ); $query_args = array( 'post_type' => ACS_Portfolio_Post_Type::POST_TYPE, 'posts_per_page' => (int) $atts['limit'], 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', ); if ( ! empty( $scope ) ) { $query_args['tax_query'] = self::scope_tax_query( $scope ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Filtering by the plugin's own category is the point of the shortcode, and the taxonomy holds a handful of terms. } ob_start(); ?>
render_filter( $scope ); ?>
$scope Category slugs the shortcode is limited to. * @return void */ private function render_filter( $scope ) { $term_args = array( 'taxonomy' => ACS_Portfolio_Post_Type::TAXONOMY, 'hide_empty' => false, ); if ( ! empty( $scope ) ) { $term_args['slug'] = $scope; } $terms = get_terms( $term_args ); if ( empty( $terms ) || is_wp_error( $terms ) ) { return; } ?>
$query_args Query arguments. * @return string */ public static function render_items( $query_args ) { /** * Filter the query used to list portfolio items. * * @param array $query_args Query arguments. */ $query_args = (array) apply_filters( 'acs_portfolio_query_args', $query_args ); $query = new WP_Query( $query_args ); ob_start(); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); /** * Filter the template used for one card. * * @param string $template Absolute path to the template file. */ $template = (string) apply_filters( 'acs_portfolio_item_template', ACS_PORTFOLIO_PATH . 'templates/portfolio-item.php' ); if ( file_exists( $template ) ) { include $template; } } wp_reset_postdata(); } else { echo '

' . esc_html__( 'No portfolio items found.', 'acs-portfolio' ) . '

'; } return ob_get_clean(); } /** * Ajax handler for the category filter and the sort drop-down. * * @return void */ public function ajax_filter() { if ( ! check_ajax_referer( self::NONCE, 'nonce', false ) ) { wp_send_json_error( 'Invalid nonce' ); return; } $category = isset( $_POST['category'] ) ? sanitize_title( wp_unslash( $_POST['category'] ) ) : ''; $sort = isset( $_POST['sort'] ) ? sanitize_key( wp_unslash( $_POST['sort'] ) ) : 'date_desc'; $scope = isset( $_POST['scope'] ) ? self::sanitize_slug_list( sanitize_text_field( wp_unslash( $_POST['scope'] ) ) ) : array(); $query_args = array_merge( array( 'post_type' => ACS_Portfolio_Post_Type::POST_TYPE, 'posts_per_page' => -1, 'post_status' => 'publish', ), self::sort_args( $sort ) ); if ( '' !== $category ) { // A category outside the scope must not widen what this shortcode shows. $terms = ( empty( $scope ) || in_array( $category, $scope, true ) ) ? array( $category ) : $scope; $query_args['tax_query'] = self::scope_tax_query( $terms ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Filtering by the plugin's own category is the point of the shortcode, and the taxonomy holds a handful of terms. } elseif ( ! empty( $scope ) ) { $query_args['tax_query'] = self::scope_tax_query( $scope ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Filtering by the plugin's own category is the point of the shortcode, and the taxonomy holds a handful of terms. } wp_send_json_success( self::render_items( $query_args ) ); } }