get_setting( '_tcacc_source', 'manual' ); if ( 'posts' === $source ) { return $this->items_from_posts( $renderer ); } if ( 'acf' === $source ) { $acf_items = $this->items_from_acf( $renderer ); return null === $acf_items ? $items : $acf_items; } return $items; } /** * Post types that can be listed (public, and never the accordions themselves). * * @return array Slug => label. */ private function get_post_type_choices() { $choices = array(); foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $slug => $type ) { if ( in_array( $slug, array( 'attachment', 'accordion_tp' ), true ) ) { continue; } $choices[ $slug ] = $type->labels->name; } return $choices; } /** * Taxonomies that can filter the posts. * * @return array Slug => label. */ private function get_taxonomy_choices() { $choices = array(); foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $slug => $taxonomy ) { if ( 'post_format' === $slug ) { continue; } $choices[ $slug ] = $taxonomy->labels->name; } return $choices; } /** * Build the items from WordPress posts. * * @param TCAccordion_Renderer $renderer Renderer. * @return array */ private function items_from_posts( $renderer ) { $post_type = sanitize_key( (string) $renderer->get_setting( '_tcacc_src_post_type', 'post' ) ); if ( ! array_key_exists( $post_type, $this->get_post_type_choices() ) ) { $post_type = 'post'; } $count = absint( $renderer->get_setting( '_tcacc_src_count', '10' ) ); $count = max( 1, min( 100, $count ? $count : 10 ) ); $orderby = (string) $renderer->get_setting( '_tcacc_src_orderby', 'date' ); $orderby = in_array( $orderby, array( 'date', 'title', 'menu_order', 'modified', 'rand' ), true ) ? $orderby : 'date'; $order = ( 'ASC' === strtoupper( (string) $renderer->get_setting( '_tcacc_src_order', 'DESC' ) ) ) ? 'ASC' : 'DESC'; $args = array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => $count, 'orderby' => $orderby, 'order' => $order, 'has_password' => false, 'ignore_sticky_posts' => true, 'no_found_rows' => true, ); $taxonomy = sanitize_key( (string) $renderer->get_setting( '_tcacc_src_taxonomy', '' ) ); $terms = array(); if ( '' !== $taxonomy && taxonomy_exists( $taxonomy ) ) { $terms = array_filter( array_map( 'sanitize_title', explode( ',', (string) $renderer->get_setting( '_tcacc_src_terms', '' ) ) ) ); if ( ! empty( $terms ) ) { $args['tax_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query array( 'taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => array_values( $terms ), ), ); } } $use_excerpt = ( 'full' !== $renderer->get_setting( '_tcacc_src_content', 'excerpt' ) ); $read_more = ( 'yes' === $renderer->get_setting( '_tcacc_src_readmore', 'no' ) ); $group_by_term = ( 'yes' === $renderer->get_setting( '_tcacc_src_group_by_term', 'no' ) ) && '' !== $taxonomy && taxonomy_exists( $taxonomy ); $query = new WP_Query( $args ); $items = array(); foreach ( $query->posts as $post ) { $content = preg_replace( '//s', '', strip_shortcodes( (string) $post->post_content ) ); if ( $use_excerpt ) { $text = has_excerpt( $post ) ? $post->post_excerpt : wp_trim_words( wp_strip_all_tags( $content ), 55 ); $html = '

' . esc_html( $text ) . '

'; } else { $html = wpautop( wp_kses_post( $content ) ); } if ( $read_more ) { $html .= '

' . esc_html__( 'Read more', 'accordions-wp' ) . '

'; } $group = ''; if ( $group_by_term ) { $names = wp_get_post_terms( $post->ID, $taxonomy, array( 'fields' => 'names', 'number' => 1 ) ); $group = ( ! is_wp_error( $names ) && ! empty( $names ) ) ? mb_substr( wp_strip_all_tags( $names[0] ), 0, 40 ) : ''; } $items[] = array( 'title' => wp_strip_all_tags( get_the_title( $post ) ), 'description' => $html, 'group' => $group, ); } return $items; } /** * Build the items from an Advanced Custom Fields repeater. * * @param TCAccordion_Renderer $renderer Renderer. * @return array|null Items, or null when ACF is not available. */ private function items_from_acf( $renderer ) { if ( ! function_exists( 'get_field' ) ) { return null; } $clean = static function ( $name ) { return preg_replace( '/[^A-Za-z0-9_\-]/', '', (string) $name ); }; $field = $clean( $renderer->get_setting( '_tcacc_acf_field', '' ) ); $title_field = $clean( $renderer->get_setting( '_tcacc_acf_title', '' ) ); $content_field = $clean( $renderer->get_setting( '_tcacc_acf_content', '' ) ); if ( '' === $field || '' === $title_field || '' === $content_field ) { return array(); } $location = ( 'option' === $renderer->get_setting( '_tcacc_acf_location', 'current' ) ) ? 'option' : get_queried_object_id(); $rows = get_field( $field, $location ); $items = array(); if ( ! is_array( $rows ) ) { return $items; } foreach ( $rows as $row ) { if ( ! is_array( $row ) ) { continue; } $title = isset( $row[ $title_field ] ) ? wp_strip_all_tags( (string) $row[ $title_field ] ) : ''; $content = isset( $row[ $content_field ] ) ? wpautop( wp_kses_post( (string) $row[ $content_field ] ) ) : ''; if ( '' !== $title || '' !== $content ) { $items[] = array( 'title' => $title, 'description' => $content, ); } } return $items; } /** * Settings rows. * * @param WP_Post $post Accordion post. * @return void */ public function render_settings( $post ) { $get = static function ( $key, $default = '' ) use ( $post ) { $value = get_post_meta( $post->ID, $key, true ); return ( '' === $value || false === $value ) ? $default : $value; }; $source = $get( '_tcacc_source', 'manual' ); ?>

200, '_tcacc_acf_field' => 60, '_tcacc_acf_title' => 60, '_tcacc_acf_content' => 60, ); foreach ( $text_fields as $key => $max ) { if ( isset( $_POST[ $key ] ) ) { update_post_meta( $post_id, $key, mb_substr( sanitize_text_field( wp_unslash( $_POST[ $key ] ) ), 0, $max ) ); } } $choices = array( '_tcacc_source' => array( 'manual', 'posts', 'acf' ), '_tcacc_src_orderby' => array( 'date', 'title', 'menu_order', 'modified', 'rand' ), '_tcacc_src_order' => array( 'DESC', 'ASC' ), '_tcacc_src_content' => array( 'excerpt', 'full' ), '_tcacc_src_readmore' => array( 'no', 'yes' ), '_tcacc_src_group_by_term' => array( 'no', 'yes' ), '_tcacc_acf_location' => array( 'current', 'option' ), ); foreach ( $choices as $key => $allowed ) { if ( isset( $_POST[ $key ] ) ) { $value = sanitize_text_field( wp_unslash( $_POST[ $key ] ) ); update_post_meta( $post_id, $key, in_array( $value, $allowed, true ) ? $value : $allowed[0] ); } } if ( isset( $_POST['_tcacc_src_post_type'] ) ) { $type = sanitize_key( wp_unslash( $_POST['_tcacc_src_post_type'] ) ); update_post_meta( $post_id, '_tcacc_src_post_type', array_key_exists( $type, $this->get_post_type_choices() ) ? $type : 'post' ); } if ( isset( $_POST['_tcacc_src_taxonomy'] ) ) { $taxonomy = sanitize_key( wp_unslash( $_POST['_tcacc_src_taxonomy'] ) ); update_post_meta( $post_id, '_tcacc_src_taxonomy', ( '' === $taxonomy || array_key_exists( $taxonomy, $this->get_taxonomy_choices() ) ) ? $taxonomy : '' ); } if ( isset( $_POST['_tcacc_src_count'] ) ) { $count = absint( wp_unslash( $_POST['_tcacc_src_count'] ) ); update_post_meta( $post_id, '_tcacc_src_count', max( 1, min( 100, $count ? $count : 10 ) ) ); } // phpcs:enable WordPress.Security.NonceVerification.Missing } } new TCAccordion_Pro_Dynamic();