| @@ -6,12 +6,12 @@ | ||
| 6 | 6 | use Averta\Core\Utility\Str; |
| 7 | 7 | use Averta\WordPress\Utility\JSON; |
| 8 | 8 | |
| 9 | 9 | |
| 10 | -class Posts extends Base | |
| 10 | +class Posts extends DataSourceBase implements DataSourceInterface | |
| 11 | 11 | { |
| 12 | 12 | /** |
| 13 | - * Source name | |
| 13 | + * DataSource name | |
| 14 | 14 | * |
| 15 | 15 | * @var string |
| 16 | 16 | */ |
| 17 | 17 | protected $type = 'wpPost'; |
| @@ -16,19 +16,29 @@ | ||
| 16 | 16 | */ |
| 17 | 17 | protected $type = 'wpPost'; |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * Input params to filter source result | |
| 20 | + * DataSource properties | |
| 21 | 21 | * |
| 22 | 22 | * @var array |
| 23 | 23 | */ |
| 24 | - protected $params = [ | |
| 24 | + protected $properties = [ | |
| 25 | + 'type' => 'wpPost', | |
| 26 | + 'postType' => 'post' | |
| 27 | + ]; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Default input params for retrieving dataSource records | |
| 31 | + * | |
| 32 | + * @var array | |
| 33 | + */ | |
| 34 | + protected $defaultInputParams = [ | |
| 25 | 35 | 'postType' => 'post', |
| 26 | 36 | 'perpage' => 5, |
| 27 | 37 | 'excerptLength' => 55, |
| 28 | 38 | 'offset' => 0, |
| 29 | 39 | 'linkSlides' => true, |
| 30 | - 'orderBy' => 'date', | |
| 40 | + 'orderBy' => 'post__in', | |
| 31 | 41 | 'order' => 'DESC', |
| 32 | 42 | 'imageSource' => 'featured', |
| 33 | 43 | 'excludedIds' => '', |
| 34 | 44 | 'includedIds' => '', |
| @@ -38,43 +48,15 @@ | ||
| 38 | 48 | 's' => '' |
| 39 | 49 | ]; |
| 40 | 50 | |
| 41 | 51 | /** |
| 42 | - * List of valid dynamic tags for this source | |
| 52 | + * Asset groups of this DataSource | |
| 43 | 53 | * |
| 44 | - * @var string[] | |
| 54 | + * @var array | |
| 45 | 55 | */ |
| 46 | - protected $tags = [ | |
| 47 | - '{{{uuid}}}', | |
| 48 | - '{{{linkSlides}}}', | |
| 49 | - '{{{url}}}', | |
| 56 | + protected $assetGroupNames = [ 'post', 'taxonomy', 'acf', 'metaboxio' ]; | |
| 50 | 57 | |
| 51 | - '{{{id}}}', | |
| 52 | - '{{{featuredImage}}}', | |
| 53 | - '{{{featuredImageSrc}}}', | |
| 54 | - '{{{author.name}}}', | |
| 55 | - '{{{author.page}}}', | |
| 56 | - '{{{title}}}', | |
| 57 | - '{{{excerpt}}}', | |
| 58 | - '{{{content}}}', | |
| 59 | - '{{{date}}}', | |
| 60 | - '{{{readMore}}}', | |
| 61 | - '{{categoryToStr}}', // converts the array of categories to this form cat1, cat2, cat3 | |
| 62 | - '{{post_tagToStr}}' // converts the array of tags to this form tag1, tag2, tag3 | |
| 63 | - ]; | |
| 64 | - | |
| 65 | 58 | /** |
| 66 | - * Prepares query arguments to get records | |
| 67 | - * | |
| 68 | - * @param array|object $args | |
| 69 | - * | |
| 70 | - * @return array | |
| 71 | - */ | |
| 72 | - protected function prepare( $args ){ | |
| 73 | - return Arr::merge( $args, $this->params ); | |
| 74 | - } | |
| 75 | - | |
| 76 | - /** | |
| 77 | 59 | * Retrieves the list of records based on query params |
| 78 | 60 | * |
| 79 | 61 | * @param $args |
| 80 | 62 | * |
| @@ -88,9 +70,8 @@ | ||
| 88 | 70 | 'order' => $args['order'], |
| 89 | 71 | 'orderby' => $args['orderBy'], |
| 90 | 72 | 'offset' => $args['offset'], |
| 91 | 73 | 'paged' => $args['page'], |
| 92 | - 's' => $args['s'], | |
| 93 | 74 | 'post__in' => $args['includedIds'], |
| 94 | 75 | 'post__not_in' => $args['excludedIds'], |
| 95 | 76 | 'tax_query' => [], |
| 96 | 77 | 'meta_query' => [] |
| @@ -95,8 +76,12 @@ | ||
| 95 | 76 | 'tax_query' => [], |
| 96 | 77 | 'meta_query' => [] |
| 97 | 78 | ]; |
| 98 | 79 | |
| 80 | + if( !empty( $args['s'] ) ){ | |
| 81 | + $queryArgs['s'] = $args['s']; | |
| 82 | + } | |
| 83 | + | |
| 99 | 84 | if ( !empty( $args['taxonomies'] ) ) { |
| 100 | 85 | $taxonomies = $args['taxonomies']; |
| 101 | 86 | |
| 102 | 87 | if( JSON::isJson( $args['taxonomies'] ) ){ |
| @@ -102,23 +87,19 @@ | ||
| 102 | 87 | if( JSON::isJson( $args['taxonomies'] ) ){ |
| 103 | 88 | $taxonomies = JSON::decode( $args['taxonomies'] ); |
| 104 | 89 | } |
| 105 | 90 | |
| 106 | - if( !empty( $taxonomies->category ) ){ | |
| 107 | - $queryArgs['tax_query'][] = [ | |
| 108 | - 'taxonomy' => 'category', | |
| 109 | - 'field' => 'slug', | |
| 110 | - 'terms' => $taxonomies->category | |
| 111 | - ]; | |
| 91 | + if ( !empty( $taxonomies ) ) { | |
| 92 | + foreach( $taxonomies as $taxonomySlug => $value ) { | |
| 93 | + if ( !empty( $value ) ) { | |
| 94 | + $queryArgs['tax_query'][] = [ | |
| 95 | + 'taxonomy' => $taxonomySlug, | |
| 96 | + 'field' => 'slug', | |
| 97 | + 'terms' => $value | |
| 98 | + ]; | |
| 99 | + } | |
| 100 | + } | |
| 112 | 101 | } |
| 113 | - | |
| 114 | - if( !empty( $taxonomies->post_tag ) ){ | |
| 115 | - $queryArgs['tax_query'][] = [ | |
| 116 | - 'taxonomy' => 'post_tag', | |
| 117 | - 'field' => 'slug', | |
| 118 | - 'terms' => $taxonomies->post_tag | |
| 119 | - ]; | |
| 120 | - } | |
| 121 | 102 | } |
| 122 | 103 | |
| 123 | 104 | if( Data::isTrue( $args['excludeNonThumbnail'] ) ){ |
| 124 | 105 | $queryArgs['meta_query'][] = [ |
| @@ -129,16 +110,9 @@ | ||
| 129 | 110 | |
| 130 | 111 | return new \WP_Query( $queryArgs ); |
| 131 | 112 | } |
| 132 | 113 | |
| 133 | - /** | |
| 134 | - * Renders preview for query params | |
| 135 | - * | |
| 136 | - * @param array $args | |
| 137 | - * | |
| 138 | - * @return array | |
| 139 | - */ | |
| 140 | - public function previewRecords( array $args = [] ) { | |
| 114 | + public function previewRecords( array $args = [] ) { | |
| 141 | 115 | $args = $this->prepare( $args ); |
| 142 | 116 | $availableRecords = $this->getRecords( $args ); |
| 143 | 117 | |
| 144 | 118 | $records = []; |
| @@ -143,18 +117,20 @@ | ||
| 143 | 117 | |
| 144 | 118 | $records = []; |
| 145 | 119 | |
| 146 | 120 | if ( $availableRecords && $availableRecords->have_posts() ) { |
| 121 | + $acfModule = \Depicter::resolve( 'depicter.dataSources.tags.acf' ); | |
| 122 | + $acfModuleAvailable = $acfModule->isAvailable(); | |
| 123 | + $acfFields = $acfModule->getFieldGroups( $args ); | |
| 147 | 124 | |
| 148 | - | |
| 149 | 125 | foreach( $availableRecords->posts as $post ) { |
| 150 | - $featuredImage = []; | |
| 126 | + $featuredImage = null; | |
| 151 | 127 | if( has_post_thumbnail( $post->ID ) ){ |
| 152 | 128 | $featuredImageId = get_post_thumbnail_id( $post->ID ); |
| 153 | 129 | $imageInfo = wp_get_attachment_image_src( $featuredImageId, 'full' ); |
| 154 | 130 | $featuredImage = [ |
| 155 | 131 | 'id' => $featuredImageId, |
| 156 | - 'url' => $imageInfo[0] ?: '', | |
| 132 | + 'src' => $imageInfo[0] ?: '', | |
| 157 | 133 | 'width' => $imageInfo[1] ?: '', |
| 158 | 134 | 'height' => $imageInfo[2] ?: '', |
| 159 | 135 | ]; |
| 160 | 136 | } |
| @@ -164,9 +140,9 @@ | ||
| 164 | 140 | 'id' => $post->ID, |
| 165 | 141 | 'title' => get_the_title( $post->ID ), |
| 166 | 142 | 'url' => get_permalink( $post->ID ), |
| 167 | 143 | 'featuredImage' => $featuredImage, |
| 168 | - 'date' => get_the_date('', $post->ID ), | |
| 144 | + 'date' => get_the_date('Y-m-d h:m:s', $post->ID ), | |
| 169 | 145 | 'excerpt' => $excerpt, |
| 170 | 146 | 'author' => [ |
| 171 | 147 | 'name' => get_the_author_meta( 'display_name', $post->post_author ), |
| 172 | 148 | 'page' => get_author_posts_url( $post->post_author ), |
| @@ -171,11 +147,12 @@ | ||
| 171 | 147 | 'name' => get_the_author_meta( 'display_name', $post->post_author ), |
| 172 | 148 | 'page' => get_author_posts_url( $post->post_author ), |
| 173 | 149 | ], |
| 174 | 150 | 'content' => get_the_content(null, false, $post->ID ), |
| 175 | - 'taxonomies'=> [] | |
| 151 | + 'taxonomy'=> [] | |
| 176 | 152 | ]; |
| 177 | 153 | |
| 154 | + // append taxonomy data | |
| 178 | 155 | $taxonomies = get_object_taxonomies( $args['postType'], 'objects' ); |
| 179 | 156 | |
| 180 | 157 | if ( !empty( $taxonomies ) ) { |
| 181 | 158 | foreach( $taxonomies as $taxonomySlug => $taxonomy ) { |
| @@ -193,14 +170,36 @@ | ||
| 193 | 170 | 'label' => $term->name, |
| 194 | 171 | 'link' => get_term_link( $term->term_id ) |
| 195 | 172 | ]; |
| 196 | 173 | } |
| 174 | + } else { | |
| 175 | + $taxonomyInfo[ "terms" ] = null; | |
| 197 | 176 | } |
| 198 | 177 | |
| 199 | - $postInfo['taxonomies'][] = $taxonomyInfo; | |
| 178 | + $postInfo['taxonomy'][ $taxonomySlug ] = $taxonomyInfo; | |
| 200 | 179 | } |
| 201 | 180 | } |
| 202 | 181 | |
| 182 | + // append acf data | |
| 183 | + if( $acfModuleAvailable ){ | |
| 184 | + $postInfo['acf'] = []; | |
| 185 | + | |
| 186 | + if ( !empty( $acfFields ) ) { | |
| 187 | + foreach( $acfFields as $fieldId => $field ) { | |
| 188 | + $postInfo['acf'][ $fieldId ] = $acfModule->getSlugValue( $fieldId, ['post' => $post->ID ] ); | |
| 189 | + if( $field['type'] === 'image' ){ | |
| 190 | + $imageInfo = wp_get_attachment_image_src( $postInfo['acf'][ $fieldId ], 'full' ); | |
| 191 | + $postInfo['acf'][ $fieldId ] = [ | |
| 192 | + 'id' => $postInfo['acf'][ $fieldId ], | |
| 193 | + 'src' => $imageInfo[0] ?? '', | |
| 194 | + 'width' => $imageInfo[1] ?? '', | |
| 195 | + 'height' => $imageInfo[2] ?? '', | |
| 196 | + ]; | |
| 197 | + } | |
| 198 | + } | |
| 199 | + } | |
| 200 | + } | |
| 201 | + | |
| 203 | 202 | $records[] = $postInfo; |
| 204 | 203 | } |
| 205 | 204 | } |
| 206 | 205 | |
| @@ -206,8 +205,33 @@ | ||
| 206 | 205 | |
| 207 | 206 | return $records; |
| 208 | 207 | } |
| 209 | 208 | |
| 209 | + public function previewRecords2( array $args = [] ) { | |
| 210 | + $args = $this->prepare( $args ); | |
| 211 | + $availableRecords = $this->getRecords( $args ); | |
| 212 | + | |
| 213 | + $records = []; | |
| 214 | + | |
| 215 | + if ( $availableRecords && $availableRecords->have_posts() ) { | |
| 216 | + foreach( $availableRecords->posts as $post ) { | |
| 217 | + $item = []; | |
| 218 | + $args['post'] = $post; | |
| 219 | + | |
| 220 | + $assetGroups = $this->getAssetGroupNames(); | |
| 221 | + foreach( $assetGroups as $assetGroup ){ | |
| 222 | + if( $module = \Depicter::dataSource()->tagsManager()->getModule( $assetGroup ) ){ | |
| 223 | + $item[ $assetGroup ] = $module->getValuesForRecord( $args ); | |
| 224 | + } | |
| 225 | + } | |
| 226 | + | |
| 227 | + $records[] = $item; | |
| 228 | + } | |
| 229 | + } | |
| 230 | + | |
| 231 | + return $records; | |
| 232 | + } | |
| 233 | + | |
| 210 | 234 | /** |
| 211 | 235 | * search records by title |
| 212 | 236 | * |
| 213 | 237 | * @param array $args |
| @@ -239,48 +263,27 @@ | ||
| 239 | 263 | return $records; |
| 240 | 264 | } |
| 241 | 265 | |
| 242 | 266 | /** |
| 243 | - * Get list of tags and their values for the query result | |
| 267 | + * Get list of datasheets and corresponding required arguments | |
| 244 | 268 | * |
| 245 | 269 | * @param array $args |
| 246 | 270 | * |
| 247 | 271 | * @return array |
| 248 | 272 | */ |
| 249 | - public function getRecordsWithTags( array $args = [] ){ | |
| 273 | + public function getDataSheetArgs( array $args = [] ){ | |
| 250 | 274 | $args = $this->prepare( $args ); |
| 251 | 275 | $availableRecords = $this->getRecords( $args ); |
| 252 | 276 | |
| 277 | + $dataSheetArgs = Arr::merge( $args, $this->properties ); | |
| 253 | 278 | $records = []; |
| 279 | + | |
| 254 | 280 | if ( $availableRecords && $availableRecords->have_posts() ) { |
| 255 | - // $taxonomies = get_object_taxonomies( $args['postType'], 'objects' ); | |
| 256 | 281 | |
| 257 | 282 | foreach( $availableRecords->posts as $post ) { |
| 258 | - | |
| 259 | - $excerpt = !empty( $args['excerptLength'] ) ? Str::trimByChars( get_the_excerpt( $post->ID ), $args['excerptLength'] ) : get_the_excerpt( $post->ID ); | |
| 260 | - | |
| 261 | - $thumbnailId = get_post_thumbnail_id( $post->ID ); | |
| 262 | - $featuredImageInfo = wp_get_attachment_image_src( $thumbnailId, 'full' ); | |
| 263 | - | |
| 264 | - $postInfo = [ | |
| 265 | - '{{{uuid}}}' => $post->ID, | |
| 266 | - '{{{linkSlides}}}' => !! $args['linkSlides'], | |
| 267 | - '{{{url}}}' => get_permalink( $post->ID ), | |
| 268 | - | |
| 269 | - '{{{id}}}' => $post->ID, | |
| 270 | - '{{{title}}}' => get_the_title( $post->ID ), | |
| 271 | - '{{{featuredImageSrc}}}' => ! empty( $featuredImageInfo[0] ) ? $featuredImageInfo[0] : '', | |
| 272 | - '{{{featuredImage}}}' => ! empty( $thumbnailId ) ? $thumbnailId : '', | |
| 273 | - '{{{date}}}' => get_the_date('', $post->ID ), | |
| 274 | - '{{{excerpt}}}' => $excerpt, | |
| 275 | - '{{{author.name}}}' => get_the_author_meta( 'display_name', $post->post_author ), | |
| 276 | - '{{{author.page}}}' => get_author_posts_url( $post->post_author ), | |
| 277 | - '{{{content}}}' => get_the_content(null, false, $post->ID ), | |
| 278 | - '{{categoryToStr}}' => $this->getTaxonomyTermsStr( $post->ID, 'category' ), | |
| 279 | - '{{post_tagToStr}}' => $this->getTaxonomyTermsStr( $post->ID, 'post_tag' ), | |
| 280 | - ]; | |
| 281 | - | |
| 282 | - $records[] = $postInfo; | |
| 283 | + $postArgs = $dataSheetArgs; | |
| 284 | + $postArgs['post'] = $post; | |
| 285 | + $records[] = $postArgs; | |
| 283 | 286 | } |
| 284 | 287 | } |
| 285 | 288 | |
| 286 | 289 | return $records; |
| @@ -294,12 +297,8 @@ | ||
| 294 | 297 | * @return array |
| 295 | 298 | */ |
| 296 | 299 | public function getTypes( string $postType = 'all' ) { |
| 297 | 300 | if ( $postType == 'all' ) { |
| 298 | - $availablePostType = array( | |
| 299 | - 'post' => get_post_type_object('post'), | |
| 300 | - 'page' => get_post_type_object('page') | |
| 301 | - ); | |
| 302 | 301 | $postTypes = get_post_types( |
| 303 | 302 | array( |
| 304 | 303 | 'public' => true, |
| 305 | 304 | '_builtin' => false |
| @@ -306,9 +305,9 @@ | ||
| 306 | 305 | ), |
| 307 | 306 | 'objects' |
| 308 | 307 | ); |
| 309 | 308 | |
| 310 | - $postTypes = array_merge( $availablePostType, $postTypes ); | |
| 309 | + // $postTypes = array_merge( $availablePostType, $postTypes ); | |
| 311 | 310 | } else { |
| 312 | 311 | if ( !post_type_exists( $postType ) ) { |
| 313 | 312 | return []; |
| 314 | 313 | } |
| @@ -357,24 +356,8 @@ | ||
| 357 | 356 | return $result; |
| 358 | 357 | } |
| 359 | 358 | |
| 360 | 359 | /** |
| 361 | - * Get taxonomy terms string | |
| 362 | - * | |
| 363 | - * @param int $postID | |
| 364 | - * @param string $taxonomy | |
| 365 | - * @return string | |
| 366 | - */ | |
| 367 | - protected function getTaxonomyTermsStr( $postID, $taxonomy ) { | |
| 368 | - $terms = get_the_terms( $postID, $taxonomy ); | |
| 369 | - if ( !empty( $terms ) ) { | |
| 370 | - return join( ', ', wp_list_pluck($terms, 'name') ); | |
| 371 | - } | |
| 372 | - | |
| 373 | - return ''; | |
| 374 | - } | |
| 375 | - | |
| 376 | - /** | |
| 377 | 360 | * Search by title in wp query |
| 378 | 361 | * |
| 379 | 362 | * @param string $search |
| 380 | 363 | * @param object $wp_query |
| @@ -381,14 +364,15 @@ | ||
| 381 | 364 | * @return string $search |
| 382 | 365 | */ |
| 383 | 366 | public function searchByTitleOnly( $search, $wp_query ) { |
| 384 | 367 | global $wpdb; |
| 368 | + | |
| 385 | 369 | if ( empty( $search ) ) { |
| 386 | 370 | return $search; |
| 387 | 371 | } |
| 388 | 372 | |
| 389 | 373 | $queryVars = $wp_query->query_vars; |
| 390 | - $n = !empty($queryVars['exact']) ? '' : '%'; | |
| 374 | + $n = !empty( $queryVars['exact'] ) ? '' : '%'; | |
| 391 | 375 | $search = ''; |
| 392 | 376 | $searchAnd = ''; |
| 393 | 377 | foreach ( (array) $queryVars['search_terms'] as $term ) { |
| 394 | 378 | $term = esc_sql( $wpdb->esc_like( $term ) ); |
| @@ -400,8 +384,44 @@ | ||
| 400 | 384 | if ( !is_user_logged_in() ) { |
| 401 | 385 | $search .= " AND ($wpdb->posts.post_password = '') "; |
| 402 | 386 | } |
| 403 | 387 | } |
| 388 | + | |
| 404 | 389 | return $search; |
| 390 | + } | |
| 391 | + | |
| 392 | + /** | |
| 393 | + * Get taxonomy terms string | |
| 394 | + * | |
| 395 | + * @param int $postID | |
| 396 | + * @param string $taxonomy | |
| 397 | + * @return string | |
| 398 | + */ | |
| 399 | + protected function getTaxonomyTermsStr( $postID, $taxonomy ) { | |
| 400 | + $terms = get_the_terms( $postID, $taxonomy ); | |
| 401 | + | |
| 402 | + if ( !empty( $terms ) ) { | |
| 403 | + return join( ', ', wp_list_pluck($terms, 'name') ); | |
| 404 | + } | |
| 405 | + | |
| 406 | + return ''; | |
| 407 | + } | |
| 408 | + | |
| 409 | + /** | |
| 410 | + * Get list of asset groups for this dataSource | |
| 411 | + * | |
| 412 | + * @param $args | |
| 413 | + * | |
| 414 | + * @return array | |
| 415 | + */ | |
| 416 | + public function getAssets( $args ){ | |
| 417 | + $assetGroupNames = $this->getAssetGroupNames(); | |
| 418 | + | |
| 419 | + $groups = []; | |
| 420 | + foreach( $assetGroupNames as $assetGroupName ){ | |
| 421 | + $groups[ $assetGroupName ] = $args; | |
| 422 | + } | |
| 423 | + | |
| 424 | + return \Depicter::dataSource()->tagsManager()->getAssetsInGroups( $groups ); | |
| 405 | 425 | } |
| 406 | 426 | |
| 407 | 427 | } |