| 1 |
<?php |
| 2 |
|
| 3 |
use AcyMailing\Core\AcymPlugin; |
| 4 |
use AcyMailing\Helpers\TabHelper; |
| 5 |
|
| 6 |
class plgAcymPost extends AcymPlugin |
| 7 |
{ |
| 8 |
private bool $groupedByCategory = false; |
| 9 |
private $currentCategory = null; |
| 10 |
private array $acfFields = []; |
| 11 |
private array $handledFieldTypes = [ |
| 12 |
'text', |
| 13 |
'textarea', |
| 14 |
'number', |
| 15 |
'range', |
| 16 |
'email', |
| 17 |
'url', |
| 18 |
'password', |
| 19 |
'image', |
| 20 |
'file', |
| 21 |
'select', |
| 22 |
'checkbox', |
| 23 |
'radio', |
| 24 |
'true_false', |
| 25 |
'date_picker', |
| 26 |
'date_time_picker', |
| 27 |
'time_picker', |
| 28 |
'color_picker', |
| 29 |
'button_group', |
| 30 |
'link', |
| 31 |
]; |
| 32 |
|
| 33 |
public function __construct() |
| 34 |
{ |
| 35 |
parent::__construct(); |
| 36 |
$this->cms = 'WordPress'; |
| 37 |
$this->rootCategoryId = 0; |
| 38 |
|
| 39 |
$this->pluginDescription->name = acym_translation('ACYM_ARTICLE'); |
| 40 |
$this->pluginDescription->icon = '<i class="cell acymicon-wordpress"></i>'; |
| 41 |
$this->pluginDescription->icontype = 'raw'; |
| 42 |
|
| 43 |
if (ACYM_CMS === 'wordpress') { |
| 44 |
$this->displayOptions = [ |
| 45 |
'title' => ['ACYM_TITLE', true], |
| 46 |
'image' => ['ACYM_FEATURED_IMAGE', true], |
| 47 |
'excerpt' => ['ACYM_EXCERPT', false], |
| 48 |
'intro' => ['ACYM_INTRO_ONLY', true], |
| 49 |
'content' => ['ACYM_FULL_TEXT', false], |
| 50 |
'cats' => ['ACYM_CATEGORIES', false], |
| 51 |
'author' => ['ACYM_AUTHOR', false], |
| 52 |
'readmore' => ['ACYM_READ_MORE', false], |
| 53 |
]; |
| 54 |
|
| 55 |
$this->initCustomView(true); |
| 56 |
|
| 57 |
$this->settings = [ |
| 58 |
'custom_view' => [ |
| 59 |
'type' => 'custom_view', |
| 60 |
'tags' => array_merge($this->displayOptions, $this->replaceOptions, $this->customOptions, $this->elementOptions), |
| 61 |
], |
| 62 |
]; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public function initCustomOptionsCustomView() |
| 67 |
{ |
| 68 |
$customFields = acym_loadObjectList('SELECT post_title, post_excerpt, post_content FROM #__posts WHERE post_status = "publish" AND post_type = "acf-field"'); |
| 69 |
if (empty($customFields)) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
foreach ($customFields as $customField) { |
| 74 |
$settings = unserialize($customField->post_content); |
| 75 |
if (!in_array($settings['type'], $this->handledFieldTypes)) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
$this->customOptions[$customField->post_excerpt] = [$customField->post_title]; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function getStandardStructure(string &$customView): void |
| 84 |
{ |
| 85 |
$tag = new stdClass(); |
| 86 |
$tag->id = 0; |
| 87 |
|
| 88 |
$format = new stdClass(); |
| 89 |
$format->tag = $tag; |
| 90 |
$format->title = '{title}'; |
| 91 |
$format->afterTitle = ''; |
| 92 |
$format->afterArticle = ''; |
| 93 |
$format->imagePath = '{image}'; |
| 94 |
$format->description = '{intro}'; |
| 95 |
$format->link = '{link}'; |
| 96 |
$format->customFields = []; |
| 97 |
$customView = '<div class="acymailing_content">'.$this->pluginHelper->getStandardDisplay($format).'</div>'; |
| 98 |
} |
| 99 |
|
| 100 |
public function initReplaceOptionsCustomView(): void |
| 101 |
{ |
| 102 |
$this->replaceOptions = [ |
| 103 |
'link' => ['ACYM_LINK'], |
| 104 |
'picthtml' => ['ACYM_IMAGE'], |
| 105 |
'readmore' => ['ACYM_READ_MORE'], |
| 106 |
]; |
| 107 |
} |
| 108 |
|
| 109 |
public function initElementOptionsCustomView(): void |
| 110 |
{ |
| 111 |
$query = 'SELECT post.* |
| 112 |
FROM #__posts AS post |
| 113 |
WHERE post.post_type = "post" |
| 114 |
AND post.post_status = "publish"'; |
| 115 |
$element = acym_loadObject($query); |
| 116 |
if (empty($element)) return; |
| 117 |
foreach ($element as $key => $value) { |
| 118 |
$this->elementOptions[$key] = [$key]; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public function getPossibleIntegrations(): ?object |
| 123 |
{ |
| 124 |
return $this->pluginDescription; |
| 125 |
} |
| 126 |
|
| 127 |
public function insertionOptions(?object $defaultValues = null): void |
| 128 |
{ |
| 129 |
$this->defaultValues = $defaultValues; |
| 130 |
$this->prepareWPCategories('category'); |
| 131 |
|
| 132 |
$tabHelper = new TabHelper(); |
| 133 |
$identifier = $this->name; |
| 134 |
$tabHelper->startTab(acym_translation('ACYM_ONE_BY_ONE'), !empty($this->defaultValues->defaultPluginTab) && $identifier === $this->defaultValues->defaultPluginTab); |
| 135 |
|
| 136 |
$defaultSizes = ['thumbnail', 'medium', 'medium_large', 'large', 'post-thumbnail']; |
| 137 |
$imageFeaturedSize = ['full' => 'ACYM_ORIGINAL_IMAGE_RESOLUTION']; |
| 138 |
|
| 139 |
foreach (wp_get_registered_image_subsizes() as $sizeName => $sizes) { |
| 140 |
if (in_array($sizeName, $defaultSizes) && (!empty($sizes['width']) || !empty($sizes['height']))) { |
| 141 |
$imageFeaturedSize[$sizeName] = $sizes['width'].'x'.$sizes['height'].'px'; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$displayOptions = [ |
| 146 |
[ |
| 147 |
'title' => 'ACYM_DISPLAY', |
| 148 |
'type' => 'checkbox', |
| 149 |
'name' => 'display', |
| 150 |
'options' => $this->displayOptions, |
| 151 |
], |
| 152 |
]; |
| 153 |
|
| 154 |
$this->initAcfData(); |
| 155 |
if (!empty($this->acfFields)) { |
| 156 |
$customFieldsOptions = []; |
| 157 |
foreach ($this->acfFields as $field) { |
| 158 |
if (!in_array($field['type'], $this->handledFieldTypes)) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
$customFieldsOptions[$field['ID']] = [$field['label'], false]; |
| 162 |
} |
| 163 |
$displayOptions[] = [ |
| 164 |
'title' => 'ACF', |
| 165 |
'type' => 'checkbox', |
| 166 |
'name' => 'acf', |
| 167 |
'options' => $customFieldsOptions, |
| 168 |
]; |
| 169 |
} |
| 170 |
|
| 171 |
$displayOptions = array_merge( |
| 172 |
$displayOptions, |
| 173 |
[ |
| 174 |
[ |
| 175 |
'title' => 'ACYM_FEATURED_IMAGE_SIZE', |
| 176 |
'type' => 'select', |
| 177 |
'name' => 'size', |
| 178 |
'options' => $imageFeaturedSize, |
| 179 |
'default' => 'full', |
| 180 |
], |
| 181 |
[ |
| 182 |
'title' => 'ACYM_CLICKABLE_TITLE', |
| 183 |
'type' => 'boolean', |
| 184 |
'name' => 'clickable', |
| 185 |
'default' => true, |
| 186 |
], |
| 187 |
[ |
| 188 |
'title' => 'ACYM_CLICKABLE_IMAGE', |
| 189 |
'type' => 'boolean', |
| 190 |
'name' => 'clickableimg', |
| 191 |
'default' => false, |
| 192 |
], |
| 193 |
[ |
| 194 |
'title' => 'ACYM_REPLACE_SHORTCODES', |
| 195 |
'type' => 'boolean', |
| 196 |
'name' => 'replaceshortcode', |
| 197 |
'default' => false, |
| 198 |
], |
| 199 |
[ |
| 200 |
'title' => 'ACYM_TRUNCATE', |
| 201 |
'type' => 'intextfield', |
| 202 |
'isNumber' => 1, |
| 203 |
'name' => 'wrap', |
| 204 |
'text' => 'ACYM_TRUNCATE_AFTER', |
| 205 |
'default' => 0, |
| 206 |
], |
| 207 |
[ |
| 208 |
'title' => 'ACYM_DISPLAY_PICTURES', |
| 209 |
'type' => 'pictures', |
| 210 |
'name' => 'pictures', |
| 211 |
], |
| 212 |
] |
| 213 |
); |
| 214 |
|
| 215 |
$zoneContent = $this->getFilteringZone().$this->prepareListing(); |
| 216 |
$this->displaySelectionZone($zoneContent); |
| 217 |
$this->pluginHelper->displayOptions($displayOptions, $identifier, 'individual', $this->defaultValues); |
| 218 |
|
| 219 |
$tabHelper->endTab(); |
| 220 |
$identifier = 'auto'.$this->name; |
| 221 |
$tabHelper->startTab(acym_translation('ACYM_BY_CATEGORY'), !empty($this->defaultValues->defaultPluginTab) && $identifier === $this->defaultValues->defaultPluginTab); |
| 222 |
|
| 223 |
$catOptions = [ |
| 224 |
[ |
| 225 |
'title' => 'ACYM_ORDER_BY', |
| 226 |
'type' => 'select', |
| 227 |
'name' => 'order', |
| 228 |
'options' => [ |
| 229 |
'ID' => 'ACYM_ID', |
| 230 |
'post_date' => 'ACYM_PUBLISHING_DATE', |
| 231 |
'post_modified' => 'ACYM_MODIFICATION_DATE', |
| 232 |
'post_title' => 'ACYM_TITLE', |
| 233 |
'menu_order' => 'ACYM_MENU_ORDER', |
| 234 |
'rand' => 'ACYM_RANDOM', |
| 235 |
], |
| 236 |
], |
| 237 |
[ |
| 238 |
'title' => 'ACYM_GROUP_BY_CATEGORY', |
| 239 |
'type' => 'boolean', |
| 240 |
'name' => 'groupbycat', |
| 241 |
'default' => false, |
| 242 |
], |
| 243 |
]; |
| 244 |
$this->autoContentOptions($catOptions); |
| 245 |
|
| 246 |
$this->autoCampaignOptions($catOptions); |
| 247 |
|
| 248 |
$displayOptions = array_merge($displayOptions, $catOptions); |
| 249 |
|
| 250 |
ob_start(); |
| 251 |
acym_display(acym_translation('ACYM_SPECIAL_CONTENT_WARNING'), 'warning', false); |
| 252 |
$warningMessage = ob_get_clean(); |
| 253 |
$this->displaySelectionZone($warningMessage.$this->getCategoryListing()); |
| 254 |
$this->pluginHelper->displayOptions($displayOptions, $identifier, 'grouped', $this->defaultValues); |
| 255 |
|
| 256 |
$tabHelper->endTab(); |
| 257 |
|
| 258 |
$tabHelper->display('plugin'); |
| 259 |
} |
| 260 |
|
| 261 |
public function prepareListing(): string |
| 262 |
{ |
| 263 |
$this->querySelect = 'SELECT post.ID, post.post_title, post.post_date, post.post_content '; |
| 264 |
$this->query = 'FROM #__posts AS post '; |
| 265 |
$this->filters = []; |
| 266 |
$this->filters[] = 'post.post_type = "post"'; |
| 267 |
$this->filters[] = 'post.post_status = "publish"'; |
| 268 |
$this->searchFields = ['post.ID', 'post.post_title']; |
| 269 |
$this->pageInfo->order = 'post.ID'; |
| 270 |
$this->elementIdTable = 'post'; |
| 271 |
$this->elementIdColumn = 'ID'; |
| 272 |
|
| 273 |
parent::prepareListing(); |
| 274 |
|
| 275 |
//if a category is selected |
| 276 |
if (!empty($this->pageInfo->filter_cat)) { |
| 277 |
$this->query .= 'JOIN #__term_relationships AS cat ON post.ID = cat.object_id'; |
| 278 |
$this->filters[] = 'cat.term_taxonomy_id = '.intval($this->pageInfo->filter_cat); |
| 279 |
} |
| 280 |
|
| 281 |
$listingOptions = [ |
| 282 |
'header' => [ |
| 283 |
'post_title' => [ |
| 284 |
'label' => 'ACYM_TITLE', |
| 285 |
'size' => '7', |
| 286 |
], |
| 287 |
'post_date' => [ |
| 288 |
'label' => 'ACYM_PUBLISHING_DATE', |
| 289 |
'size' => '4', |
| 290 |
'type' => 'date', |
| 291 |
], |
| 292 |
'ID' => [ |
| 293 |
'label' => 'ACYM_ID', |
| 294 |
'size' => '1', |
| 295 |
'class' => 'text-center', |
| 296 |
], |
| 297 |
], |
| 298 |
'id' => 'ID', |
| 299 |
'rows' => $this->getElements(), |
| 300 |
]; |
| 301 |
|
| 302 |
return $this->getElementsListing($listingOptions); |
| 303 |
} |
| 304 |
|
| 305 |
public function replaceContent(object &$email): void |
| 306 |
{ |
| 307 |
$this->initAcfData(); |
| 308 |
$this->replaceMultiple($email); |
| 309 |
$this->replaceOne($email); |
| 310 |
} |
| 311 |
|
| 312 |
public function generateByCategory(object &$email): object |
| 313 |
{ |
| 314 |
$tags = $this->pluginHelper->extractTags($email, 'auto'.$this->name); |
| 315 |
$this->tags = []; |
| 316 |
|
| 317 |
if (empty($tags)) return $this->generateCampaignResult; |
| 318 |
|
| 319 |
foreach ($tags as $oneTag => $parameter) { |
| 320 |
if (isset($this->tags[$oneTag])) continue; |
| 321 |
|
| 322 |
$query = 'SELECT DISTINCT post.`ID` |
| 323 |
FROM #__posts AS post |
| 324 |
LEFT JOIN #__term_relationships AS cat ON post.ID = cat.object_id'; |
| 325 |
|
| 326 |
$where = []; |
| 327 |
|
| 328 |
$selectedArea = $this->getSelectedArea($parameter); |
| 329 |
if (!empty($selectedArea)) { |
| 330 |
$where[] = 'cat.term_taxonomy_id IN ('.implode(',', $selectedArea).')'; |
| 331 |
} |
| 332 |
|
| 333 |
$where[] = 'post.post_type = "post"'; |
| 334 |
$where[] = 'post.post_status = "publish"'; |
| 335 |
if (!empty($parameter->min_publish)) { |
| 336 |
$parameter->min_publish = acym_date(acym_replaceDate($parameter->min_publish), 'Y-m-d H:i:s', false); |
| 337 |
$where[] = 'post.post_date_gmt >= '.acym_escapeDB($parameter->min_publish); |
| 338 |
} |
| 339 |
|
| 340 |
if (!empty($parameter->onlynew)) { |
| 341 |
$lastGenerated = $this->getLastGenerated($email->id); |
| 342 |
if (!empty($lastGenerated)) { |
| 343 |
$where[] = 'post.post_date_gmt > '.acym_escapeDB(acym_date($lastGenerated, 'Y-m-d H:i:s', false)); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
$query .= ' WHERE ('.implode(') AND (', $where).')'; |
| 348 |
|
| 349 |
$this->groupedByCategory = !empty($parameter->groupbycat); |
| 350 |
$this->tags[$oneTag] = $this->finalizeCategoryFormat($query, $parameter, 'post'); |
| 351 |
} |
| 352 |
|
| 353 |
return $this->generateCampaignResult; |
| 354 |
} |
| 355 |
|
| 356 |
protected function groupByCategory(array $elements): array |
| 357 |
{ |
| 358 |
if (!$this->groupedByCategory || empty($elements)) return $elements; |
| 359 |
|
| 360 |
acym_arrayToInteger($elements); |
| 361 |
$idsWithCatids = acym_loadObjectList( |
| 362 |
'SELECT map.`object_id`, taxonomy.`term_id` |
| 363 |
FROM #__term_relationships AS map |
| 364 |
JOIN #__term_taxonomy AS taxonomy |
| 365 |
ON map.`term_taxonomy_id` = taxonomy.`term_taxonomy_id` |
| 366 |
WHERE taxonomy.`taxonomy` = "category" |
| 367 |
AND map.`object_id` IN ('.implode(', ', $elements).')' |
| 368 |
); |
| 369 |
usort( |
| 370 |
$idsWithCatids, |
| 371 |
function ($a, $b) { |
| 372 |
return strtolower($a->term_id) > strtolower($b->term_id) ? 1 : -1; |
| 373 |
} |
| 374 |
); |
| 375 |
$elements = []; |
| 376 |
foreach ($idsWithCatids as $oneArticle) { |
| 377 |
if (in_array($oneArticle->object_id, $elements)) continue; |
| 378 |
$elements[] = $oneArticle->object_id; |
| 379 |
} |
| 380 |
|
| 381 |
return $elements; |
| 382 |
} |
| 383 |
|
| 384 |
public function replaceIndividualContent(object $tag): string |
| 385 |
{ |
| 386 |
$allowedStatuses = ['publish']; |
| 387 |
$allowedStatuses = acym_triggerCmsHook('onAcymPostInsertion', [$allowedStatuses], false); |
| 388 |
if (empty($allowedStatuses) || !is_array($allowedStatuses)) { |
| 389 |
$allowedStatuses = ['publish']; |
| 390 |
} |
| 391 |
$allowedStatuses = array_map('acym_escapeDB', $allowedStatuses); |
| 392 |
|
| 393 |
$query = 'SELECT post.*, `user`.`user_nicename`, `user`.`display_name` |
| 394 |
FROM #__posts AS post |
| 395 |
LEFT JOIN #__users AS `user` |
| 396 |
ON `user`.`ID` = `post`.`post_author` |
| 397 |
WHERE post.post_type = "post" |
| 398 |
AND post.post_status IN ('.implode(', ', $allowedStatuses).') |
| 399 |
AND post.ID = '.intval($tag->id); |
| 400 |
|
| 401 |
$element = $this->initIndividualContent($tag, $query); |
| 402 |
|
| 403 |
if (empty($element)) return ''; |
| 404 |
|
| 405 |
$varFields = $this->getCustomLayoutVars($element); |
| 406 |
|
| 407 |
$link = get_permalink($element->ID); |
| 408 |
$link = $this->getLinkTranslated($link); |
| 409 |
$varFields['{link}'] = $link; |
| 410 |
|
| 411 |
$title = ''; |
| 412 |
$varFields['{title}'] = $element->post_title; |
| 413 |
if (in_array('title', $tag->display)) $title = $varFields['{title}']; |
| 414 |
|
| 415 |
$afterTitle = ''; |
| 416 |
$afterArticle = ''; |
| 417 |
|
| 418 |
$imagePath = ''; |
| 419 |
$imageId = get_post_thumbnail_id($tag->id); |
| 420 |
if (!empty($imageId)) { |
| 421 |
if (empty($tag->size)) $tag->size = 'full'; |
| 422 |
$imagePath = get_the_post_thumbnail_url($tag->id, $tag->size); |
| 423 |
} |
| 424 |
$varFields['{image}'] = $imagePath; |
| 425 |
$varFields['{picthtml}'] = '<img class="content_main_image" alt="" src="'.$imagePath.'">'; |
| 426 |
if (!in_array('image', $tag->display)) $imagePath = ''; |
| 427 |
|
| 428 |
$contentText = ''; |
| 429 |
$varFields['{excerpt}'] = $this->cleanExtensionContent($element->post_excerpt); |
| 430 |
if (in_array('excerpt', $tag->display) && !empty($varFields['{excerpt}'])) { |
| 431 |
$contentText .= '<p>'.$varFields['{excerpt}'].'</p>'; |
| 432 |
} |
| 433 |
|
| 434 |
$varFields['{content}'] = $this->cleanExtensionContent($element->post_content); |
| 435 |
$varFields['{intro}'] = $this->cleanExtensionContent($this->getIntro($element->post_content)); |
| 436 |
$varFields['{content}'] = !empty($tag->replaceshortcode) ? $this->replaceShortcode($varFields['{content}']) : $varFields['{content}']; |
| 437 |
$varFields['{intro}'] = !empty($tag->replaceshortcode) ? $this->replaceShortcode($varFields['{intro}']) : $varFields['{intro}']; |
| 438 |
if (in_array('content', $tag->display)) { |
| 439 |
$contentText .= $varFields['{content}']; |
| 440 |
} elseif (in_array('intro', $tag->display)) { |
| 441 |
$contentText .= $varFields['{intro}']; |
| 442 |
} |
| 443 |
|
| 444 |
$customFields = []; |
| 445 |
$varFields['{author}'] = empty($element->display_name) ? $element->user_nicename : $element->display_name; |
| 446 |
if (in_array('author', $tag->display) && !empty($varFields['{author}'])) { |
| 447 |
$customFields[] = [ |
| 448 |
$varFields['{author}'], |
| 449 |
acym_translation('ACYM_AUTHOR'), |
| 450 |
]; |
| 451 |
} |
| 452 |
|
| 453 |
$varFields['{cats}'] = get_the_term_list($tag->id, 'category', '', ', '); |
| 454 |
if (in_array('cats', $tag->display)) { |
| 455 |
$customFields[] = [ |
| 456 |
$varFields['{cats}'], |
| 457 |
acym_translation('ACYM_CATEGORIES'), |
| 458 |
]; |
| 459 |
} |
| 460 |
|
| 461 |
$varFields['{readmore}'] = '<a class="acymailing_readmore_link" style="text-decoration:none;" target="_blank" href="'.$link.'"><span class="acymailing_readmore">'.esc_html( |
| 462 |
acym_translation('ACYM_READ_MORE') |
| 463 |
).'</span></a>'; |
| 464 |
if (in_array('readmore', $tag->display)) { |
| 465 |
$afterArticle .= $varFields['{readmore}']; |
| 466 |
} |
| 467 |
|
| 468 |
if (!empty($this->acfFields)) { |
| 469 |
$tag->acf = empty($tag->acf) ? [] : explode(',', $tag->acf); |
| 470 |
|
| 471 |
foreach ($this->acfFields as $field) { |
| 472 |
$varFields['{'.$field['name'].'}'] = acf_get_value($tag->id, $field); |
| 473 |
|
| 474 |
if (empty($varFields['{'.$field['name'].'}']) && (!is_string($varFields['{'.$field['name'].'}']) || strlen($varFields['{'.$field['name'].'}']) === 0)) { |
| 475 |
continue; |
| 476 |
} |
| 477 |
|
| 478 |
$varFields['{'.$field['name'].'}'] = $this->getFormattedAcfFieldValue($varFields['{'.$field['name'].'}'], $tag->id, $field); |
| 479 |
|
| 480 |
if (in_array($field['ID'], $tag->acf) && strlen($varFields['{'.$field['name'].'}']) > 0) { |
| 481 |
$customFields[] = [ |
| 482 |
$varFields['{'.$field['name'].'}'], |
| 483 |
acf_get_field_label($field), |
| 484 |
]; |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
$format = new stdClass(); |
| 490 |
$format->tag = $tag; |
| 491 |
$format->title = $title; |
| 492 |
$format->afterTitle = $afterTitle; |
| 493 |
$format->afterArticle = $afterArticle; |
| 494 |
$format->imagePath = $imagePath; |
| 495 |
$format->description = $contentText; |
| 496 |
$format->link = empty($tag->clickable) && empty($tag->clickableimg) ? '' : $link; |
| 497 |
$format->customFields = $customFields; |
| 498 |
$result = '<div class="acymailing_content">'.$this->pluginHelper->getStandardDisplay($format).'</div>'; |
| 499 |
|
| 500 |
$categoryTitle = ''; |
| 501 |
$postCategories = acym_loadObjectList( |
| 502 |
'SELECT terms.`term_id`, terms.`name` |
| 503 |
FROM #__terms AS terms |
| 504 |
JOIN #__term_taxonomy AS taxonomy |
| 505 |
ON terms.`term_id` = taxonomy.`term_id` |
| 506 |
JOIN #__term_relationships AS map |
| 507 |
ON map.`term_taxonomy_id` = taxonomy.`term_taxonomy_id` |
| 508 |
WHERE map.`object_id` = '.intval($element->ID), |
| 509 |
'term_id' |
| 510 |
); |
| 511 |
$catIds = array_keys($postCategories); |
| 512 |
if (!empty($tag->groupbycat) && !in_array($this->currentCategory, $catIds)) { |
| 513 |
$this->currentCategory = min($catIds); |
| 514 |
|
| 515 |
$categoryTitle = '<h1 class="acymailing_category_title">'.$postCategories[$this->currentCategory]->name.'</h1>'; |
| 516 |
$categoryTitle = '<div class="acymailing_content"><a target="_blank" href="'.get_category_link($this->currentCategory).'">'.$categoryTitle.'</a></div>'; |
| 517 |
} |
| 518 |
|
| 519 |
return $categoryTitle.$this->finalizeElementFormat($result, $tag, $varFields); |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Used in Gutenberg block and elementor |
| 524 |
* |
| 525 |
* @param bool $ajax |
| 526 |
* @param int $postPerPage |
| 527 |
* |
| 528 |
* @return array|array[]|void |
| 529 |
*/ |
| 530 |
public function getPosts(bool $ajax = true, int $postPerPage = 20) |
| 531 |
{ |
| 532 |
$return = $ajax ? [] : [0 => [0, acym_translation('ACYM_SELECT_AN_ARTICLE')]]; |
| 533 |
|
| 534 |
$search = acym_getVar('string', 'searchedterm', ''); |
| 535 |
if (!empty($search)) { |
| 536 |
$search = acym_escapeDB('%'.$search.'%'); |
| 537 |
$search = 'post_title LIKE '.$search.' OR post_name LIKE '.$search.' OR post_content LIKE '.$search; |
| 538 |
$search = ' AND ('.$search.')'; |
| 539 |
} |
| 540 |
|
| 541 |
$limit = ''; |
| 542 |
if (!empty($postPerPage)) { |
| 543 |
$limit = 'LIMIT '.$postPerPage; |
| 544 |
} |
| 545 |
|
| 546 |
$query = 'SELECT ID, post_title FROM #__posts WHERE post_status = "publish" AND post_type IN ("post", "page") '.$search.' '.$limit; |
| 547 |
$posts = acym_loadObjectList($query); |
| 548 |
foreach ($posts as $post) { |
| 549 |
$return[] = [$post->ID, $post->post_title]; |
| 550 |
} |
| 551 |
|
| 552 |
if ($ajax) { |
| 553 |
echo json_encode($return); |
| 554 |
exit; |
| 555 |
} else { |
| 556 |
return $return; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
protected function getTranslationId(int $elementId, string $translationTool, bool $defaultLanguage = false): int |
| 561 |
{ |
| 562 |
$languageCode = $this->emailLanguage; |
| 563 |
|
| 564 |
if ($defaultLanguage) { |
| 565 |
$languageCode = $this->config->get('multilingual_default', ACYM_DEFAULT_LANGUAGE); |
| 566 |
} else { |
| 567 |
$idDefaultLanguage = $this->getTranslationId($elementId, $translationTool, true); |
| 568 |
|
| 569 |
// We only translate inserted articles of the default language |
| 570 |
if ($idDefaultLanguage !== $elementId) { |
| 571 |
return $elementId; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
$languageCode = substr($languageCode, 0, 2); |
| 576 |
|
| 577 |
if ($translationTool === 'polylang') { |
| 578 |
if (acym_isExtensionActive('polylang/polylang.php') && function_exists('pll_get_post')) { |
| 579 |
$translationId = pll_get_post($elementId, $languageCode); |
| 580 |
if (!empty($translationId)) $elementId = $translationId; |
| 581 |
} |
| 582 |
} elseif ($translationTool === 'wpml') { |
| 583 |
if (acym_isExtensionActive('sitepress-multilingual-cms/sitepress.php')) { |
| 584 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML hook for integration. |
| 585 |
$elementId = apply_filters('wpml_object_id', $elementId, 'post', true, $languageCode); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
return intval($elementId); |
| 590 |
} |
| 591 |
|
| 592 |
private function initAcfData(): void |
| 593 |
{ |
| 594 |
$installed = acym_isExtensionActive('advanced-custom-fields/acf.php') || acym_isExtensionActive('advanced-custom-fields-pro/acf.php'); |
| 595 |
if (!$installed || !function_exists('acf_get_field_groups')) { |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
$fieldGroups = acf_get_field_groups(['post_type' => 'post']); |
| 600 |
foreach ($fieldGroups as $fieldGroup) { |
| 601 |
if (empty($fieldGroup['active'])) { |
| 602 |
continue; |
| 603 |
} |
| 604 |
|
| 605 |
$this->acfFields = array_merge($this->acfFields, acf_get_fields($fieldGroup)); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
private function getFormattedAcfFieldValue($value, $postId, $field): string |
| 610 |
{ |
| 611 |
$value = acf_format_value($value, $postId, $field); |
| 612 |
if ($field['type'] === 'email') { |
| 613 |
$value = '<a href="mailto:'.$value.'">'.$value.'</a>'; |
| 614 |
} elseif ($field['type'] === 'url') { |
| 615 |
$value = '<a href="'.$value.'" target="_blank">'.$value.'</a>'; |
| 616 |
} elseif ($field['type'] === 'link') { |
| 617 |
$value = '<a href="'.$value['url'].'" target="_blank">'.(empty($value['title']) ? $value['url'] : $value['title']).'</a>'; |
| 618 |
} elseif ($field['type'] === 'image') { |
| 619 |
if (empty($value['link'])) { |
| 620 |
$value = ''; |
| 621 |
} else { |
| 622 |
$value = '<img alt="'.esc_attr($value['alt']).'" src="'.esc_url($value['link']).'" />'; |
| 623 |
} |
| 624 |
} elseif (in_array($field['type'], ['checkbox', 'select'])) { |
| 625 |
if (is_array($value)) { |
| 626 |
$value = implode(', ', $value); |
| 627 |
} |
| 628 |
} elseif ($field['type'] === 'true_false') { |
| 629 |
$value = empty($value) ? acym_translation('ACYM_NO') : acym_translation('ACYM_YES'); |
| 630 |
} elseif ($field['type'] === 'file') { |
| 631 |
if (empty($value['link'])) { |
| 632 |
$value = ''; |
| 633 |
} else { |
| 634 |
$value = '<a href="'.$value['link'].'" target="_blank">'.esc_html( |
| 635 |
$value['filename'] |
| 636 |
).'</a>'; |
| 637 |
} |
| 638 |
} elseif ($field['type'] === 'textarea') { |
| 639 |
$value = nl2br($value); |
| 640 |
} elseif (in_array($field['type'], ['time_picker', 'date_picker', 'date_time_picker'])) { |
| 641 |
if ($field['display_format'] !== $field['return_format']) { |
| 642 |
$value = gmdate($field['display_format'], strtotime($value)); |
| 643 |
} |
| 644 |
} else { |
| 645 |
$value = esc_html($value); |
| 646 |
} |
| 647 |
|
| 648 |
return $value; |
| 649 |
} |
| 650 |
} |
| 651 |
|