| @@ -4,10 +4,10 @@ | ||
| 4 | 4 | use Averta\Core\Utility\Arr; |
| 5 | 5 | use Averta\Core\Utility\Data; |
| 6 | 6 | use Averta\Core\Utility\Str; |
| 7 | 7 | use Averta\WordPress\Utility\JSON; |
| 8 | -use Averta\WordPress\Utility\Post; | |
| 9 | 8 | |
| 9 | + | |
| 10 | 10 | class Posts extends DataSourceBase implements DataSourceInterface |
| 11 | 11 | { |
| 12 | 12 | /** |
| 13 | 13 | * DataSource name |
| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | * DataSource name |
| 14 | 14 | * |
| 15 | 15 | * @var string |
| 16 | 16 | */ |
| 17 | - protected string $type = 'wpPost'; | |
| 17 | + protected $type = 'wpPost'; | |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * DataSource properties |
| 21 | 21 | * |
| @@ -20,9 +20,9 @@ | ||
| 20 | 20 | * DataSource properties |
| 21 | 21 | * |
| 22 | 22 | * @var array |
| 23 | 23 | */ |
| 24 | - protected array $properties = [ | |
| 24 | + protected $properties = [ | |
| 25 | 25 | 'type' => 'wpPost', |
| 26 | 26 | 'postType' => 'post' |
| 27 | 27 | ]; |
| 28 | 28 | |
| @@ -30,14 +30,14 @@ | ||
| 30 | 30 | * Default input params for retrieving dataSource records |
| 31 | 31 | * |
| 32 | 32 | * @var array |
| 33 | 33 | */ |
| 34 | - protected array $defaultInputParams = [ | |
| 34 | + protected $defaultInputParams = [ | |
| 35 | 35 | 'postType' => 'post', |
| 36 | 36 | 'perpage' => 5, |
| 37 | 37 | 'excerptLength' => 55, |
| 38 | 38 | 'offset' => 0, |
| 39 | - 'linkSlides' => false, | |
| 39 | + 'linkSlides' => true, | |
| 40 | 40 | 'orderBy' => 'post__in', |
| 41 | 41 | 'order' => 'DESC', |
| 42 | 42 | 'imageSource' => 'featured', |
| 43 | 43 | 'excludedIds' => '', |
| @@ -52,9 +52,9 @@ | ||
| 52 | 52 | * Asset groups of this DataSource |
| 53 | 53 | * |
| 54 | 54 | * @var array |
| 55 | 55 | */ |
| 56 | - protected array $assetGroupNames = [ 'post', 'taxonomy', 'acf', 'metaboxio' ]; | |
| 56 | + protected $assetGroupNames = [ 'post', 'taxonomy', 'acf', 'metaboxio' ]; | |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * Retrieves the list of records based on query params |
| 60 | 60 | * |
| @@ -73,11 +73,9 @@ | ||
| 73 | 73 | 'paged' => $args['page'], |
| 74 | 74 | 'post__in' => $args['includedIds'], |
| 75 | 75 | 'post__not_in' => $args['excludedIds'], |
| 76 | 76 | 'tax_query' => [], |
| 77 | - 'meta_query' => [], | |
| 78 | - 'post_status' => 'publish', | |
| 79 | - 'ignore_sticky_posts' => true | |
| 77 | + 'meta_query' => [] | |
| 80 | 78 | ]; |
| 81 | 79 | |
| 82 | 80 | if( !empty( $args['s'] ) ){ |
| 83 | 81 | $queryArgs['s'] = $args['s']; |
| @@ -114,42 +112,18 @@ | ||
| 114 | 112 | } |
| 115 | 113 | |
| 116 | 114 | public function previewRecords( array $args = [] ) { |
| 117 | 115 | $args = $this->prepare( $args ); |
| 116 | + $availableRecords = $this->getRecords( $args ); | |
| 118 | 117 | |
| 119 | 118 | $records = []; |
| 120 | - | |
| 121 | - // Check if user selects to include sticky posts then prepend them first because we ignore the sticky posts in the main query | |
| 122 | - $stickyPostIDs = get_option('sticky_posts'); | |
| 123 | - $includedIds = $args['includedIds']; | |
| 124 | - if ( !empty( $args['sticky'] ) && Data::isTrue( $args['sticky'] ) && ! empty( $stickyPostIDs ) && $args['postType'] == 'post' ) { | |
| 125 | - $args['includedIds'] = $stickyPostIDs; | |
| 126 | - $wp_query = $this->getRecords( $args ); | |
| 127 | - $records = Arr::merge( $this->extractRecordsArray( $wp_query, $args ), $records ); | |
| 128 | 119 | |
| 129 | - // Modify query args to get remaining posts without considering sticky posts | |
| 130 | - $args['perpage'] = count( $stickyPostIDs ) > $args['perpage'] ? 0 : $args['perpage'] - count( $stickyPostIDs); | |
| 131 | - $args['includedIds'] = $includedIds; | |
| 132 | - $args['excludedIds'] = Arr::merge( $stickyPostIDs, $args['excludedIds']); | |
| 133 | - } | |
| 134 | - | |
| 135 | - if ( $args['perpage'] ) { | |
| 136 | - $wp_query = $this->getRecords( $args ); | |
| 137 | - $records = Arr::merge( $this->extractRecordsArray( $wp_query, $args ), $records ); | |
| 138 | - } | |
| 139 | - | |
| 140 | - return $records; | |
| 141 | - } | |
| 142 | - | |
| 143 | - public function extractRecordsArray( $wp_query, $args ) { | |
| 144 | - $records = []; | |
| 145 | - | |
| 146 | - if ( $wp_query && $wp_query->have_posts() ) { | |
| 120 | + if ( $availableRecords && $availableRecords->have_posts() ) { | |
| 147 | 121 | $acfModule = \Depicter::resolve( 'depicter.dataSources.tags.acf' ); |
| 148 | 122 | $acfModuleAvailable = $acfModule->isAvailable(); |
| 149 | 123 | $acfFields = $acfModule->getFieldGroups( $args ); |
| 150 | 124 | |
| 151 | - foreach( $wp_query->posts as $post ) { | |
| 125 | + foreach( $availableRecords->posts as $post ) { | |
| 152 | 126 | $featuredImage = null; |
| 153 | 127 | if( has_post_thumbnail( $post->ID ) ){ |
| 154 | 128 | $featuredImageId = get_post_thumbnail_id( $post->ID ); |
| 155 | 129 | $imageInfo = wp_get_attachment_image_src( $featuredImageId, 'full' ); |
| @@ -160,14 +134,9 @@ | ||
| 160 | 134 | 'height' => $imageInfo[2] ?: '', |
| 161 | 135 | ]; |
| 162 | 136 | } |
| 163 | 137 | |
| 164 | - $content = get_the_content(null, false, $post->ID ); | |
| 165 | - $excerpt = Post::getExcerptTrimmedByWords( $post->ID ); | |
| 166 | - if ( ! empty( $args['excerptLength'] ) ) { | |
| 167 | - $excerpt = Str::trimByChars( $excerpt, $args['excerptLength'] ); | |
| 168 | - } | |
| 169 | - | |
| 138 | + $excerpt = !empty( $args['excerptLength'] ) ? Str::trimByChars( get_the_excerpt( $post->ID ), $args['excerptLength'] ) : get_the_excerpt( $post->ID ); | |
| 170 | 139 | $postInfo = [ |
| 171 | 140 | 'id' => $post->ID, |
| 172 | 141 | 'title' => get_the_title( $post->ID ), |
| 173 | 142 | 'url' => get_permalink( $post->ID ), |
| @@ -177,9 +146,9 @@ | ||
| 177 | 146 | 'author' => [ |
| 178 | 147 | 'name' => get_the_author_meta( 'display_name', $post->post_author ), |
| 179 | 148 | 'page' => get_author_posts_url( $post->post_author ), |
| 180 | 149 | ], |
| 181 | - 'content' => $content, | |
| 150 | + 'content' => get_the_content(null, false, $post->ID ), | |
| 182 | 151 | 'taxonomy'=> [] |
| 183 | 152 | ]; |
| 184 | 153 | |
| 185 | 154 | // append taxonomy data |
| @@ -234,9 +203,9 @@ | ||
| 234 | 203 | } |
| 235 | 204 | } |
| 236 | 205 | |
| 237 | 206 | return $records; |
| 238 | - } | |
| 207 | + } | |
| 239 | 208 | |
| 240 | 209 | public function previewRecords2( array $args = [] ) { |
| 241 | 210 | $args = $this->prepare( $args ); |
| 242 | 211 | $availableRecords = $this->getRecords( $args ); |
| @@ -302,45 +271,22 @@ | ||
| 302 | 271 | * @return array |
| 303 | 272 | */ |
| 304 | 273 | public function getDataSheetArgs( array $args = [] ){ |
| 305 | 274 | $args = $this->prepare( $args ); |
| 275 | + $availableRecords = $this->getRecords( $args ); | |
| 276 | + | |
| 277 | + $dataSheetArgs = Arr::merge( $args, $this->properties ); | |
| 306 | 278 | $records = []; |
| 307 | 279 | |
| 308 | - // Check if user selects to include sticky posts then prepend them first because we ignore the sticky posts in the main query | |
| 309 | - $stickyPostIDs = get_option('sticky_posts'); | |
| 310 | - $includedIds = $args['includedIds']; | |
| 311 | - if ( !empty( $args['sticky'] ) && Data::isTrue( $args['sticky'] ) && ! empty( $stickyPostIDs ) && $args['postType'] == 'post' ) { | |
| 312 | - $args['includedIds'] = $stickyPostIDs; | |
| 313 | - $dataSheetArgs = Arr::merge( $args, $this->properties ); | |
| 314 | - $wp_query = $this->getRecords( $args ); | |
| 315 | - if ( $wp_query && $wp_query->have_posts() ) { | |
| 280 | + if ( $availableRecords && $availableRecords->have_posts() ) { | |
| 316 | 281 | |
| 317 | - foreach( $wp_query->posts as $post ) { | |
| 318 | - $postArgs = $dataSheetArgs; | |
| 319 | - $postArgs['post'] = $post; | |
| 320 | - $records[] = $postArgs; | |
| 321 | - } | |
| 282 | + foreach( $availableRecords->posts as $post ) { | |
| 283 | + $postArgs = $dataSheetArgs; | |
| 284 | + $postArgs['post'] = $post; | |
| 285 | + $records[] = $postArgs; | |
| 322 | 286 | } |
| 323 | - | |
| 324 | - // Modify query args to get remaining posts without considering sticky posts | |
| 325 | - $args['perpage'] = count( $stickyPostIDs ) > $args['perpage'] ? 0 : $args['perpage'] - count( $stickyPostIDs); | |
| 326 | - $args['includedIds'] = $includedIds; | |
| 327 | - $args['excludedIds'] = Arr::merge( $stickyPostIDs, $args['excludedIds']); | |
| 328 | 287 | } |
| 329 | 288 | |
| 330 | - if ( $args['perpage'] ) { | |
| 331 | - $wp_query = $this->getRecords( $args ); | |
| 332 | - $dataSheetArgs = Arr::merge( $args, $this->properties ); | |
| 333 | - if ( $wp_query && $wp_query->have_posts() ) { | |
| 334 | - | |
| 335 | - foreach( $wp_query->posts as $post ) { | |
| 336 | - $postArgs = $dataSheetArgs; | |
| 337 | - $postArgs['post'] = $post; | |
| 338 | - $records[] = $postArgs; | |
| 339 | - } | |
| 340 | - } | |
| 341 | - } | |
| 342 | - | |
| 343 | 289 | return $records; |
| 344 | 290 | } |
| 345 | 291 | |
| 346 | 292 | /** |
| @@ -457,7 +403,25 @@ | ||
| 457 | 403 | return join( ', ', wp_list_pluck($terms, 'name') ); |
| 458 | 404 | } |
| 459 | 405 | |
| 460 | 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 ); | |
| 461 | 425 | } |
| 462 | 426 | |
| 463 | 427 | } |