true ], 'objects' ); $post_types = wp_list_pluck( $post_types, 'label', 'name' ); $exclude = apply_filters( 'advanced_news_ticker_exclude_post_type', [ 'attachment', 'revision', 'nav_menu_item', 'elementor_library', 'e-landing-page', 'e-floating-buttons', ] ); if ( ! empty( $exc ) ) { $exclude = array_merge( $exclude, (array) $exc ); } foreach ( $exclude as $ex ) { unset( $post_types[ $ex ] ); } return $post_types; } /** * Build a safe link attribute string. * * @param array $link Elementor URL control value. * @param bool $echo Whether to print the result. * * @return string|void */ public static function link_attr( $link, $echo = true ) { if ( empty( $link['url'] ) ) { return ''; } $attr = 'href="' . esc_url( $link['url'] ) . '"'; if ( ! empty( $link['is_external'] ) ) { $attr .= ' target="_blank"'; } $rel = []; if ( ! empty( $link['nofollow'] ) ) { $rel[] = 'nofollow'; } if ( ! empty( $link['is_external'] ) ) { $rel[] = 'noopener'; $rel[] = 'noreferrer'; } if ( $rel ) { $attr .= ' rel="' . esc_attr( implode( ' ', array_unique( $rel ) ) ) . '"'; } if ( ! $echo ) { return $attr; } // Every component above is escaped individually. echo $attr; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Allowed values for the orderby argument. * * @return string[] */ public static function allowed_orderby() { return [ 'date', 'ID', 'author', 'title', 'modified', 'parent', 'comment_count', 'menu_order', 'meta_value', 'meta_value_num', 'rand', ]; } /** * Translate widget settings into WP_Query arguments. * * @param array $data Widget settings. * * @return array */ public static function query_args( $data ) { $data = (array) $data; $_post_type = ! empty( $data['post_type'] ) ? sanitize_key( $data['post_type'] ) : 'post'; $post_type = self::available_post_type( $_post_type ); $post_limit = isset( $data['post_limit'] ) && '' !== $data['post_limit'] ? (int) $data['post_limit'] : 5; if ( 0 === $post_limit ) { $post_limit = 5; } $args = [ 'post_type' => $post_type, 'ignore_sticky_posts' => 1, 'post_status' => 'publish', 'posts_per_page' => $post_limit, 'no_found_rows' => true, ]; $tax_query = []; $_taxonomies = get_object_taxonomies( $post_type, 'objects' ); foreach ( $_taxonomies as $object ) { if ( in_array( $object->name, self::get_excluded_taxonomy(), true ) ) { continue; } $setting_key = $object->name . '_ids'; if ( empty( $data[ $setting_key ] ) ) { continue; } $terms = array_filter( array_map( 'absint', (array) $data[ $setting_key ] ) ); if ( ! $terms ) { continue; } $tax_query[] = [ 'taxonomy' => $object->name, 'field' => 'term_id', 'terms' => $terms, ]; } if ( $tax_query ) { if ( count( $tax_query ) > 1 ) { $relation = ! empty( $data['tax_relation'] ) && 'AND' === strtoupper( $data['tax_relation'] ) ? 'AND' : 'OR'; $tax_query['relation'] = $relation; } //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query $args['tax_query'] = $tax_query; } if ( ! empty( $data['tags'] ) ) { $args['tag_slug__in'] = array_map( 'sanitize_title', (array) $data['tags'] ); } $order_by = ! empty( $data['orderby'] ) && in_array( $data['orderby'], self::allowed_orderby(), true ) ? $data['orderby'] : 'date'; $args['orderby'] = $order_by; if ( in_array( $order_by, [ 'meta_value', 'meta_value_num' ], true ) && ! empty( $data['meta_key'] ) ) { //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key $args['meta_key'] = sanitize_text_field( $data['meta_key'] ); } $args['order'] = ! empty( $data['order'] ) && 'ASC' === strtoupper( $data['order'] ) ? 'ASC' : 'DESC'; // Only the select2 control matching the chosen post type is relevant. $selected_ids = $data[ $post_type . '_ids' ] ?? []; $selected_ids = array_filter( array_map( 'absint', (array) $selected_ids ) ); if ( $selected_ids ) { $args['post__in'] = $selected_ids; } if ( ! empty( $data['exclude'] ) ) { $excluded = array_filter( array_map( 'absint', (array) $data['exclude'] ) ); if ( $excluded ) { $args['post__not_in'] = $excluded; } } $offset = isset( $data['offset'] ) ? absint( $data['offset'] ) : 0; if ( $offset > 0 ) { $args['offset'] = $offset; } return apply_filters( 'advanced_news_ticker_query_args', $args, $data ); } /** * Print HTML with wp_kses * * @param string $html Raw HTML. * @param string $context Context key. * @param bool $echo Whether to print. * * @return string|void */ public static function html( $html, $context = 'plain', $echo = true ) { // Define reusable tag configurations. $base_tags = [ 'a' => [ 'href' => [], 'class' => [], 'rel' => [], 'title' => [], 'target' => [] ], 'b' => [], 'img' => [ 'src' => [], 'alt' => [], 'class' => [], 'width' => [], 'height' => [], 'srcset' => [], 'loading' => [] ], 'p' => [ 'class' => [] ], 'span' => [ 'class' => [], 'style' => [], 'title' => [] ], 'strong' => [], 'br' => [], ]; // Context-specific tag adjustments. $tags_by_context = [ 'plain' => $base_tags, 'link' => [ 'a' => $base_tags['a'] ], 'image' => [ 'img' => $base_tags['img'] ], 'title' => $base_tags, 'default' => array_merge( $base_tags, [ 'abbr' => [ 'title' => [] ], 'blockquote' => [ 'cite' => [] ], 'code' => [], 'del' => [ 'datetime' => [], 'title' => [] ], 'div' => [ 'class' => [], 'style' => [], 'title' => [], 'id' => [] ], 'h1' => [ 'class' => [], 'style' => [], 'title' => [], 'id' => [] ], 'h2' => [ 'class' => [], 'style' => [], 'title' => [], 'id' => [] ], 'h3' => [ 'class' => [], 'style' => [], 'title' => [], 'id' => [] ], 'h4' => [ 'class' => [], 'style' => [], 'title' => [], 'id' => [] ], 'h5' => [ 'class' => [], 'style' => [], 'title' => [], 'id' => [] ], 'h6' => [ 'class' => [], 'style' => [], 'title' => [], 'id' => [] ], 'i' => [ 'class' => [] ], 'li' => [ 'class' => [] ], 'ol' => [ 'class' => [] ], 'ul' => [ 'class' => [] ], 'iframe' => [ 'class' => [], 'id' => [], 'name' => [], 'src' => [], 'title' => [], 'frameBorder' => [], 'width' => [], 'height' => [], 'scrolling' => [], 'allow' => [], 'allowvr' => [], 'allowFullScreen' => [], 'webkitallowfullscreen' => [], 'mozallowfullscreen' => [], 'loading' => [], ], ] ), ]; // Determine tags for the given context. $tags = $tags_by_context[ $context ] ?? $tags_by_context['default']; if ( ! $echo ) { return wp_kses( $html, $tags ); } echo wp_kses( $html, $tags ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Get Available Post Type * * @param string $post_type Post type slug. * * @return string */ public static function available_post_type( $post_type ) { $post_type_object = get_post_type_object( $post_type ); if ( ! $post_type_object ) { return 'post'; } if ( ! empty( $post_type_object->public ) ) { return $post_type; } if ( current_user_can( 'edit_posts' ) ) { return $post_type; } return 'post'; } /** * Get Excluded Taxonomy * * @return string[] */ public static function get_excluded_taxonomy() { return [ 'post_format', 'nav_menu', 'link_category', 'wp_theme', 'wp_template_part_area', 'wp_pattern_category', 'elementor_library_type', 'elementor_library_category', 'product_visibility', 'product_shipping_class', ]; } /** * Term id => name map for a taxonomy. * * @param string $cat Taxonomy slug. * * @return array */ public static function get_categories_by_id( $cat = 'category' ) { $terms = get_terms( [ 'taxonomy' => $cat, 'hide_empty' => true, ] ); $options = []; if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $options[ $term->term_id ] = $term->name; } } return $options; } }