Repository navigation
Expand file tree
/
Copy pathPosts.php
More file actions
479 lines (398 loc) · 16 KB
/
Copy pathPosts.php
File metadata and controls
479 lines (398 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
namespace Modularity\Module\Posts;
use Modularity\Helper\WpQueryFactory\WpQueryFactory;
use Modularity\Helper\WpService;
use Modularity\Module\Posts\Helper\DomainChecker;
use Modularity\Module\Posts\Helper\GetArchiveUrl;
use Modularity\Module\Posts\Helper\GetPosts\GetPostsInterface;
use Modularity\Module\Posts\Helper\GetPosts\PostsResultInterface;
use Modularity\Module\Posts\Helper\GetPosts\PostTypesFromSchemaType\PostTypesFromSchemaTypeResolver;
use Modularity\Module\Posts\Private\PrivateController;
use Modularity\Module\Posts\Helper\GetPosts\{
GetPosts,
GetPostsFromMultipleSites
};
use Modularity\Module\Posts\Helper\GetPosts\UserGroupResolver\UserGroupResolver;
/**
* Class Posts
* @package Modularity\Module\Posts
*/
class Posts extends \Modularity\Module
{
public $slug = 'posts';
public $supports = [];
public array $fields = [];
public $blockSupports = array(
'align' => ['full']
);
public GetPostsInterface $getPostsHelper;
public $archiveUrlHelper;
public string $postStatus;
public DomainChecker $domainChecker;
private PrivateController $privateController;
private $sliderCompatibleLayouts = ['items', 'news', 'index', 'grid', 'features-grid', 'segment'];
public function init()
{
$this->nameSingular = __('Posts', 'modularity');
$this->namePlural = __('Posts', 'modularity');
$this->description = __('Outputs selected posts in specified layout', 'modularity');
// Private controller
$this->privateController = new PrivateController($this);
// Saves meta data to expandable list posts
new \Modularity\Module\Posts\Helper\AddMetaToExpandableList();
// Populate schema types field
add_filter('acf/load_field/name=posts_data_schema_type', [$this, 'loadSchemaTypesField']);
// Populate schema types field
add_filter('acf/load_field/name=posts_data_network_sources', [$this, 'loadNetworkSourcesField']);
//Add full width data to view
add_filter('Modularity/Block/Data', array($this, 'blockData'), 50, 3);
add_filter(
'acf/fields/post_object/query/name=posts_data_posts',
array($this, 'removeUnwantedPostTypesFromManuallyPicked'), 10, 3
);
add_filter(
'acf/load_field/name=taxonomy_display',
array($this, 'loadTaxonomyDisplayField')
);
// Helpers
$this->archiveUrlHelper = new GetArchiveUrl();
new PostsAjax($this);
}
/**
* Load schema types field
*
* @param array $field
* @return array
*/
public function loadSchemaTypesField(array $field = []):array {
if(get_post_type() === 'acf-field-group') {
return $field;
}
global $wpdb;
$schemaTypesService = new \Municipio\SchemaData\Utils\SchemaTypesInUse($wpdb);
// Set options. E.g. ["Event" => "Event", "Article" => "Article"]...
$field['choices'] = array_combine( $schemaTypes = $schemaTypesService->getSchemaTypesInUse(), $schemaTypes );
return $field;
}
/**
* Load taxonomy display field
*
* @param array $field
* @return array
*/
public function loadTaxonomyDisplayField(array $field = []): array
{
$taxonomies = get_taxonomies([
'public' => true
], 'objects');
$choices = [];
foreach ($taxonomies as $taxonomyName => $taxonomyObj) {
$choices[$taxonomyName] = $taxonomyObj->labels->singular_name;
}
$field['choices'] = $choices;
return $field;
}
public function loadNetworkSourcesField(array $field = []) :array
{
if(!is_multisite() || get_post_type() === 'acf-field-group') {
return $field;
}
$field['choices'] = [];
foreach (get_sites(['number' => 0]) as $site) {
switch_to_blog($site->blog_id);
$field['choices'][$site->blog_id] = get_bloginfo('name');
restore_current_blog();
}
return $field;
}
/**
* @return array
*/
public function data(): array
{
$data = [];
$this->fields = $this->getFields();
$this->domainChecker = new DomainChecker($this->fields);
$data['posts_display_as'] = $this->fields['posts_display_as'] ?? false;
$data['display_reading_time'] = !empty($this->fields['posts_fields']) && in_array('reading_time', $this->fields['posts_fields']) ?? false;
// Posts
$data['preamble'] = $this->fields['preamble'] ?? false;
$data['posts_fields'] = $this->fields['posts_fields'] ?? [];
$data['posts_data_post_type'] = $this->fields['posts_data_post_type'] ?? false;
$data['posts_data_source'] = $this->fields['posts_data_source'] ?? false;
$data['postsSources'] = $this->fields['posts_data_network_sources'] ?? [];
$postsAndPaginationData = $this->getPostsResult();
$data['posts'] = $postsAndPaginationData->getPosts();
$data['stickyPosts'] = $postsAndPaginationData->getStickyPosts();
if( !empty($this->fields['posts_pagination']) && $this->fields['posts_pagination'] === 'page_numbers' ) {
$data['maxNumPages'] = $postsAndPaginationData->getNumberOfPages();
$data['paginationArguments'] = $this->getPaginationArguments($data['maxNumPages'], $this->getPageNumber());
} else {
$data['paginationArguments'] = null;
}
// Sorting
$data['sortBy'] = false;
$data['orderBy'] = false;
if (isset($this->fields['posts_sort_by']) && substr($this->fields['posts_sort_by'], 0, 9) === '_metakey_') {
$data['sortBy'] = 'meta_key';
$data['sortByKey'] = str_replace('_metakey_', '', $this->fields['posts_sort_by']);
}
$data['order'] = isset($this->fields['posts_sort_order']) ? $this->fields['posts_sort_order'] : 'asc';
// Setup filters
$filters = [
'orderby' => sanitize_text_field($data['sortBy']),
'order' => sanitize_text_field($data['order'])
];
if ($data['sortBy'] == 'meta_key') {
$filters['meta_key'] = $data['sortByKey'];
}
$data['filters'] = [];
if (isset($this->fields['posts_taxonomy_filter']) && $this->fields['posts_taxonomy_filter'] === true && !empty($this->fields['posts_taxonomy_type'])) {
$taxType = $this->fields['posts_taxonomy_type'];
$taxValues = (array)$this->fields['posts_taxonomy_value'];
$taxValues = implode('|', $taxValues);
$data['filters']["{$taxType}[]"] = $taxValues;
}
//Get archive link
$data['archiveLinkUrl'] = $this->archiveUrlHelper->getArchiveUrl(
$data['posts_data_post_type'],
$this->fields ?? null
);
// Archive link title
$data['archiveLinkTitle'] = $this->fields['archive_link_title'] ?? null;
// Archive link position
$data['archiveLinkAbovePosts'] = $this->fields['archive_link_above_posts'] ?? false;
//Add filters to archive link
if($data['archiveLinkUrl'] && is_array($data['filters']) && !empty($data['filters'])) {
$data['archiveLinkUrl'] .= "?" . http_build_query($data['filters']);
}
$data['ariaLabels'] = (object) [
'prev' => __('Previous slide', 'modularity'),
'next' => __('Next slide', 'modularity'),
];
if ($this->getID()) {
$data['sliderId'] = $this->getID();
} else {
$data['sliderId'] = uniqid();
$data['ID'] = uniqid();
}
$data['classList'] = [];
$data['lang'] = [
'showMore' => __('Show more', 'modularity'),
'readMore' => __('Read more', 'modularity'),
'save' => __('Save', 'modularity'),
'cancel' => __('Cancel', 'modularity'),
'description' => __('Description', 'modularity'),
'name' => __('Name', 'modularity'),
'saving' => __('Saving', 'modularity'),
'error' => __('An error occurred and the data could not be saved. Please try again later', 'modularity'),
'changeContent' => __('Change the lists content', 'modularity'),
];
return $data;
}
/**
* Get pagination query var name.
*
* @return string
*/
private function getPaginationQueryVarName():string {
return "{$this->slug}-{$this->getID()}-page";
}
/**
* Get current page number
*
* @return int Default is 1
*/
private function getPageNumber():int {
return filter_input(INPUT_GET, $this->getPaginationQueryVarName(), FILTER_VALIDATE_INT) ?: 1;
}
/**
* Get pagination arguments for page numbers.
*
* @param int $maxNumPages
* @param int $currentPage
* @return array
*/
private function getPaginationArguments(int $maxNumPages, int $currentPage):array {
if ($maxNumPages < 2) {
return [];
}
$listItemOne = [
'href' => remove_query_arg($this->getPaginationQueryVarName()),
'label' => __("First page", 'modularity')
];
$listItems = array_map(function($pageNumber) {
return [
'href' => add_query_arg($this->getPaginationQueryVarName(), $pageNumber),
'label' => sprintf(__("Page %d", 'modularity'), $pageNumber)
];
}, range(2, $maxNumPages));
return [
'list' => array_merge([$listItemOne], $listItems),
'current' => $currentPage,
'linkPrefix' => $this->getPaginationQueryVarName()
];
}
/**
* Add full width setting to frontend.
*
* @param [array] $viewData
* @param [array] $block
* @param [object] $module
* @return array
*/
public function blockData($viewData, $block, $module)
{
$viewData['noGutter'] = false;
if (in_array($block['name'], ['posts', 'acf/posts']) && $block['align'] == 'full') {
if (!is_admin()) {
$viewData['stretch'] = true;
}
$viewData['noGutter'] = true;
}
return $viewData;
}
/**
* Removes unwanted post types from the manually picked post types.
*
* @param array $args The arguments for the query.
* @param string $field The field name.
* @param int $id The ID of the module.
* @return array The modified arguments.
*/
public function removeUnwantedPostTypesFromManuallyPicked($args, $field, $id)
{
$skipablePostTypes = ['attachment'];
$args['post_type'] = array_filter($args['post_type'] ?? [], function($postType) use ($skipablePostTypes) {
return !in_array($postType, $skipablePostTypes);
});
return $args;
}
/**
* @return false|string
*/
public function template()
{
$template = !empty($this->data['posts_display_as']) ? $this->data['posts_display_as'] : 'list';
if (!empty($this->fields['show_as_slider']) && in_array($this->fields['posts_display_as'], $this->sliderCompatibleLayouts, true)) {
$template = 'slider';
}
$template = $this->replaceDeprecatedTemplate($template);
$this->getTemplateData($template);
$this->data['template'] = $template;
return apply_filters(
'Modularity/Module/Posts/template',
$template . '.blade.php',
$this,
$this->data,
$this->fields
);
}
/**
* @param $template
*/
public function getTemplateData(string $template = '', array $data = array())
{
if (empty($template)) {
return false;
}
if (!empty($data)) {
$this->data = $data;
}
$template = explode('-', $template);
$template = array_map('ucwords', $template);
$template = implode('', $template);
$class = '\Modularity\Module\Posts\TemplateController\\' . $template . 'Template';
$this->data['meta']['posts_display_as'] = $this->replaceDeprecatedTemplate($this->data['posts_display_as']);
if (class_exists($class)) {
$controller = new $class($this);
$this->data = array_merge($this->data, $controller->data);
$this->data = $this->privateController->decorateData($this->data, $this->fields);
}
}
/**
* Converts an associative array to an object.
*
* This function takes an associative array and converts it into an object by first
* encoding the array as a JSON string and then decoding it back into an object.
* The resulting object will have properties corresponding to the keys in the original array.
*
* @param array $array The associative array to convert to an object.
*
* @return object Returns an object representing the associative array.
*/
public function arrayToObject($array)
{
if(!is_array($array)) {
return $array;
}
return json_decode(json_encode($array));
}
/**
* Get posts and pagination data.
*
* @return PostsResultInterface
*/
public function getPostsResult(): PostsResultInterface
{
$stickyPostHelper = new \Municipio\StickyPost\Helper\GetStickyOption( new \Municipio\StickyPost\Config\StickyPostConfig(), WpService::get() );
$postTypesFromSchemaTypeResolver = new PostTypesFromSchemaTypeResolver();
if(!empty($this->fields['posts_data_network_sources'])){
global $wpdb;
$this->getPostsHelper = new GetPostsFromMultipleSites(
$this->fields,
$this->getPageNumber(),
array_map(fn($siteOption) => $siteOption['value'], $this->fields['posts_data_network_sources']),
$wpdb,
WpService::get(),
$postTypesFromSchemaTypeResolver,
new UserGroupResolver(WpService::get())
);
} else {
$this->getPostsHelper = new GetPosts(
$this->fields,
$this->getPageNumber(),
$stickyPostHelper,
WpService::get(),
new WpQueryFactory(),
$postTypesFromSchemaTypeResolver
);
}
return $this->getPostsHelper->getPosts();
}
/**
* For version 3.0 - Replace old post templates with existing replacement.
* @param $templateSlug
* @return mixed
*/
public function replaceDeprecatedTemplate($templateSlug)
{
// Add deprecated template/replacement slug to array.
$deprecatedTemplates = [
'items' => 'index',
];
if (array_key_exists($templateSlug, $deprecatedTemplates)) {
return $deprecatedTemplates[$templateSlug];
}
return $templateSlug;
}
public function adminEnqueue() {
$wpService = WpService::get();
$getCurrentPostId = fn() => $wpService->isArchive() ? false : $wpService->getTheID();
wp_register_script('mod-posts-taxonomy-filtering', MODULARITY_URL . '/dist/'
. \Modularity\Helper\CacheBust::name('js/mod-posts-taxonomy-filtering.js'));
wp_localize_script('mod-posts-taxonomy-filtering', 'modPostsTaxonomyFiltering', [
'currentPostID' => $getCurrentPostId(),
]);
wp_enqueue_script('mod-posts-taxonomy-filtering');
}
/**
* Available "magic" methods for modules:
* init() What to do on initialization
* data() Use to send data to view (return array)
* style() Enqueue style only when module is used on page
* script Enqueue script only when module is used on page
* adminEnqueue() Enqueue scripts for the module edit/add page in admin
* template() Return the view template (blade) the module should use when displayed
*/
}