class-ecs-drb-source-acf.php
3 weeks ago
class-ecs-drb-source-base.php
4 months ago
class-ecs-drb-source-json.php
4 months ago
class-ecs-drb-source-posts.php
4 months ago
class-ecs-drb-source-terms.php
4 months ago
class-ecs-drb-source-posts.php
202 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Dynamic Repeater Builder — Posts Source |
| 4 | */ |
| 5 | |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; |
| 8 | } |
| 9 | |
| 10 | class ECS_DRB_Source_Posts extends ECS_DRB_Source_Base { |
| 11 | |
| 12 | public function get_id(): string { |
| 13 | return 'posts'; |
| 14 | } |
| 15 | |
| 16 | public function get_label(): string { |
| 17 | return __( 'Posts', 'ele-custom-skin' ); |
| 18 | } |
| 19 | |
| 20 | public function get_config_schema(): array { |
| 21 | $post_types = get_post_types( [ 'public' => true ], 'objects' ); |
| 22 | $pt_options = array_values( array_map( fn( $pt ) => [ 'value' => $pt->name, 'label' => $pt->label ], $post_types ) ); |
| 23 | |
| 24 | $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' ); |
| 25 | $tax_options = [ [ 'value' => '', 'label' => __( '— none —', 'ele-custom-skin' ) ] ]; |
| 26 | foreach ( $taxonomies as $tax ) { |
| 27 | $tax_options[] = [ 'value' => $tax->name, 'label' => $tax->label ]; |
| 28 | } |
| 29 | |
| 30 | return [ |
| 31 | [ |
| 32 | 'key' => 'post_type', |
| 33 | 'label' => __( 'Post Type', 'ele-custom-skin' ), |
| 34 | 'type' => 'select', |
| 35 | 'options' => $pt_options, |
| 36 | 'default' => 'post', |
| 37 | ], |
| 38 | [ |
| 39 | 'key' => 'posts_per_page', |
| 40 | 'label' => __( 'Number of Posts', 'ele-custom-skin' ), |
| 41 | 'type' => 'number', |
| 42 | 'default' => 10, |
| 43 | 'min' => 1, |
| 44 | 'max' => 200, |
| 45 | ], |
| 46 | [ |
| 47 | 'key' => 'orderby', |
| 48 | 'label' => __( 'Order By', 'ele-custom-skin' ), |
| 49 | 'type' => 'select', |
| 50 | 'options' => [ |
| 51 | [ 'value' => 'date', 'label' => 'Date' ], |
| 52 | [ 'value' => 'title', 'label' => 'Title' ], |
| 53 | [ 'value' => 'menu_order', 'label' => 'Menu Order' ], |
| 54 | [ 'value' => 'modified', 'label' => 'Modified' ], |
| 55 | [ 'value' => 'rand', 'label' => 'Random' ], |
| 56 | ], |
| 57 | 'default' => 'date', |
| 58 | ], |
| 59 | [ |
| 60 | 'key' => 'order', |
| 61 | 'label' => __( 'Order', 'ele-custom-skin' ), |
| 62 | 'type' => 'select', |
| 63 | 'options' => [ |
| 64 | [ 'value' => 'DESC', 'label' => 'Descending' ], |
| 65 | [ 'value' => 'ASC', 'label' => 'Ascending' ], |
| 66 | ], |
| 67 | 'default' => 'DESC', |
| 68 | ], |
| 69 | [ |
| 70 | 'key' => 'taxonomy', |
| 71 | 'label' => __( 'Filter by Taxonomy', 'ele-custom-skin' ), |
| 72 | 'type' => 'select', |
| 73 | 'options' => $tax_options, |
| 74 | 'default' => '', |
| 75 | ], |
| 76 | [ |
| 77 | 'key' => 'tax_terms', |
| 78 | 'label' => __( 'Term Slugs (comma-separated)', 'ele-custom-skin' ), |
| 79 | 'type' => 'text', |
| 80 | 'default' => '', |
| 81 | 'placeholder' => 'e.g. news, events', |
| 82 | 'autocomplete' => 'taxonomy_terms', |
| 83 | ], |
| 84 | [ |
| 85 | 'key' => 'search', |
| 86 | 'label' => __( 'Search', 'ele-custom-skin' ), |
| 87 | 'type' => 'text', |
| 88 | 'default' => '', |
| 89 | 'placeholder' => '', |
| 90 | ], |
| 91 | [ |
| 92 | 'key' => 'date_format', |
| 93 | 'label' => __( 'Date Format', 'ele-custom-skin' ), |
| 94 | 'type' => 'text', |
| 95 | 'default' => get_option( 'date_format' ), |
| 96 | 'placeholder' => 'Y-m-d', |
| 97 | ], |
| 98 | ]; |
| 99 | } |
| 100 | |
| 101 | public function get_available_fields( array $config ): array { |
| 102 | $fields = [ |
| 103 | [ 'key' => 'post_id', 'label' => __( 'Post ID', 'ele-custom-skin' ) ], |
| 104 | [ 'key' => 'post_title', 'label' => __( 'Post Title', 'ele-custom-skin' ) ], |
| 105 | [ 'key' => 'post_excerpt', 'label' => __( 'Post Excerpt', 'ele-custom-skin' ) ], |
| 106 | [ 'key' => 'post_content', 'label' => __( 'Post Content (full)', 'ele-custom-skin' ) ], |
| 107 | [ 'key' => 'post_date', 'label' => __( 'Post Date', 'ele-custom-skin' ) ], |
| 108 | [ 'key' => 'post_modified', 'label' => __( 'Post Modified', 'ele-custom-skin' ) ], |
| 109 | [ 'key' => 'post_url', 'label' => __( 'Post URL', 'ele-custom-skin' ) ], |
| 110 | [ 'key' => 'post_slug', 'label' => __( 'Post Slug', 'ele-custom-skin' ) ], |
| 111 | [ 'key' => 'post_author', 'label' => __( 'Post Author Name', 'ele-custom-skin' ) ], |
| 112 | [ 'key' => 'thumbnail_url', 'label' => __( 'Thumbnail URL', 'ele-custom-skin' ) ], |
| 113 | [ 'key' => 'thumbnail_id', 'label' => __( 'Thumbnail ID', 'ele-custom-skin' ) ], |
| 114 | [ 'key' => 'category_names', 'label' => __( 'Categories (comma list)', 'ele-custom-skin' ) ], |
| 115 | ]; |
| 116 | |
| 117 | if ( function_exists( 'acf_get_field_groups' ) ) { |
| 118 | $post_type = $config['post_type'] ?? 'post'; |
| 119 | $groups = acf_get_field_groups( [ 'post_type' => $post_type ] ); |
| 120 | foreach ( $groups as $group ) { |
| 121 | $group_fields = acf_get_fields( $group['key'] ); |
| 122 | if ( $group_fields ) { |
| 123 | foreach ( $group_fields as $f ) { |
| 124 | $fields[] = [ |
| 125 | 'key' => 'acf_' . $f['name'], |
| 126 | 'label' => 'ACF: ' . $f['label'], |
| 127 | ]; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return $fields; |
| 134 | } |
| 135 | |
| 136 | public function get_rows( array $config ): array { |
| 137 | $args = [ |
| 138 | 'post_type' => sanitize_key( $config['post_type'] ?? 'post' ), |
| 139 | 'posts_per_page' => intval( $config['posts_per_page'] ?? 10 ), |
| 140 | 'orderby' => sanitize_key( $config['orderby'] ?? 'date' ), |
| 141 | 'order' => in_array( $config['order'] ?? 'DESC', [ 'ASC', 'DESC' ], true ) ? $config['order'] : 'DESC', |
| 142 | 'post_status' => 'publish', |
| 143 | ]; |
| 144 | |
| 145 | if ( ! empty( $config['taxonomy'] ) && ! empty( $config['tax_terms'] ) ) { |
| 146 | $term_slugs = array_map( 'trim', explode( ',', $config['tax_terms'] ) ); |
| 147 | $args['tax_query'] = [ [ |
| 148 | 'taxonomy' => sanitize_key( $config['taxonomy'] ), |
| 149 | 'field' => 'slug', |
| 150 | 'terms' => $term_slugs, |
| 151 | ] ]; |
| 152 | } |
| 153 | |
| 154 | if ( ! empty( $config['search'] ) ) { |
| 155 | $args['s'] = sanitize_text_field( $config['search'] ); |
| 156 | } |
| 157 | |
| 158 | $posts = get_posts( $args ); |
| 159 | $date_format = sanitize_text_field( $config['date_format'] ?? get_option( 'date_format' ) ); |
| 160 | $rows = []; |
| 161 | |
| 162 | $available_acf_keys = []; |
| 163 | if ( function_exists( 'get_field' ) ) { |
| 164 | foreach ( $this->get_available_fields( $config ) as $f ) { |
| 165 | if ( str_starts_with( $f['key'], 'acf_' ) ) { |
| 166 | $available_acf_keys[] = $f['key']; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | foreach ( $posts as $post ) { |
| 172 | $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
| 173 | $thumbnail_url = $thumbnail_id ? (string) wp_get_attachment_image_url( $thumbnail_id, 'full' ) : ''; |
| 174 | |
| 175 | $row = [ |
| 176 | 'post_id' => (string) $post->ID, |
| 177 | 'post_title' => $post->post_title, |
| 178 | 'post_excerpt' => get_the_excerpt( $post ), |
| 179 | 'post_content' => $post->post_content, |
| 180 | 'post_date' => get_the_date( $date_format, $post ), |
| 181 | 'post_modified' => get_the_modified_date( $date_format, $post ), |
| 182 | 'post_url' => get_permalink( $post->ID ), |
| 183 | 'post_slug' => $post->post_name, |
| 184 | 'post_author' => get_the_author_meta( 'display_name', $post->post_author ), |
| 185 | 'thumbnail_url' => $thumbnail_url, |
| 186 | 'thumbnail_id' => (string) $thumbnail_id, |
| 187 | 'category_names' => implode( ', ', wp_get_post_categories( $post->ID, [ 'fields' => 'names' ] ) ), |
| 188 | ]; |
| 189 | |
| 190 | foreach ( $available_acf_keys as $fkey ) { |
| 191 | $acf_name = substr( $fkey, 4 ); |
| 192 | $val = get_field( $acf_name, $post->ID ); |
| 193 | $row[ $fkey ] = is_array( $val ) ? ( $val['url'] ?? $val['ID'] ?? implode( ', ', array_filter( $val, 'is_scalar' ) ) ) : (string) $val; |
| 194 | } |
| 195 | |
| 196 | $rows[] = $row; |
| 197 | } |
| 198 | |
| 199 | return $rows; |
| 200 | } |
| 201 | } |
| 202 |