-
Notifications
You must be signed in to change notification settings - Fork 6
/
autoreadmore.php
1128 lines (965 loc) · 29.9 KB
/
autoreadmore.php
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* AutoReadMore plugin
*
* @from https://github.com/gruz/AutoReadMore
* @author ConseilgGouz
* @copyright (C) 2024 www.conseilgouz.com. All Rights Reserved.
* @license GNU/GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Content\Site\Helper\RouteHelper;
use Joomla\CMS\Router\Route;
use Joomla\String\StringHelper;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Registry\Registry;
use Joomla\CMS\Version;
use Joomla\CMS\Language\Text;
use ConseilGouz\Plugin\Content\Autoreadmore\Helper\AutoReadMoreString;
class PlgContentAutoReadMore extends CMSPlugin {
protected $plg_name;
protected $plg_type;
protected $plg_full_name;
protected $plg_path_relative;
protected $plg_path;
protected $params_content;
protected $fulltext_loaded;
protected $trimming_dots;
protected $alternative_readmore;
public function __construct(&$subject, $config)
{
parent::__construct($subject, $config);
$jinput = Factory::getApplication()->input;
if ($jinput->get('option', null) == 'com_dump')
{
return;
}
// Load languge for frontend
$this->plg_name = $config['name'];
$this->plg_type = $config['type'];
$this->plg_full_name = 'plg_' . $config['type'] . '_' . $config['name'];
$this->plg_path_relative = '/plugins/' . $this->plg_type . '/' . $this->plg_name . '/';
$this->plg_path = JPATH_PLUGINS . '/' . $this->plg_type . '/' . $this->plg_name . '/';
$j = new Version();
$version=substr($j->getShortVersion(), 0,1);
if ($version < "4") { // Joomla 4.0
JLoader::registerNamespace('ConseilGouz\Plugin\Content\Autoreadmore', JPATH_SITE . '/plugins/content/autoreadmore/src', false, false, 'psr4');
}
}
/**
* Loads English and the national to prevent untranslated constants
*
* It's the common function I use in many plugins
*
* @return void
*/
private function _loadLanguage()
{
$path = dirname(__FILE__);
$ext_name = 'plg_' . $this->plg_type . '_' . $this->plg_name;
$jlang = Factory::getLanguage();
$jlang->load($ext_name, $path, 'en-GB', true);
$jlang->load($ext_name, $path, $jlang->getDefault(), true);
$jlang->load($ext_name, $path, null, true);
}
/**
* Truncates the article text
*
* @param string $context The context of the content being passed to the plugin.
* @param object &$article The article object
* @param object &$params The article params
* @param int $page Returns int 0 when is called not form an article, and empty when called from an article
*
* @return void
*/
public function onContentPrepare($context, &$article, &$params, $page=null)
{
if (!Factory::getApplication()->isClient('site'))
{
return false;
}
if (is_object($params) && ($params instanceof Registry) && $params->get("autoreadmore")) {
return true;
}
$jinput = Factory::getApplication()->input;
if ($jinput->get('option', null, 'CMD') == 'com_dump')
{
return;
}
// SOME SPECIAL RETURNS }
$debug = $this->params->get('debug',false);
if ($debug)
{
if (function_exists('dump') && false)
{
dump($article, 'context = ' . $context);
}
else
{
if ($debug == 1)
{
Factory::getApplication()->enqueueMessage(
'Context : ' . $context . '<br />' .
'Title : ' . @$article->title . '<br />' .
'Id : ' . @$article->id . '<br />' .
'CatId : ' . @$article->catid . '<br />',
'warning');
}
elseif ($debug == 2)
{
echo '<pre style="height:180px;overflow:auto;">';
echo '<b>Context : ' . $context . '</b><br />';
echo '<b>Content Item object : </b><br />';
print_r(json_decode(json_encode($article)));
if (!empty($params))
{
echo '<b>Params:</b><br />';
print_r(json_decode(json_encode($params)));
}
else
{
echo '<b>Params NOT passed</b><br />';
}
echo '</pre>' . PHP_EOL;
}
}
}
if ($context == 'com_tags.tag')
{
// $context = $article->type_alias;
$article->catid = $article->core_catid;
$article->id = $article->content_item_id;
$article->slug = $article->id . ':' . $article->core_alias;
}
$thereIsPluginCode = false;
if ($this->params->get('PluginCode','ignore') != 'ignore')
{
$possibleParams = array('text', 'introtext', 'fulltext');
foreach ($possibleParams as $paramName)
{
if (isset($article->{$paramName}) && strpos($article->{$paramName}, '{autoreadmore}') !== false)
{
$article->{$paramName} = str_replace(
array('{autoreadmore}', '<p>{autoreadmore}</p>', '<span>{autoreadmore}</span>'),
'',
$article->{$paramName}
);
$thereIsPluginCode = true;
}
}
}
if (!$this->_checkIfAllowedContext($context, $article))
{
return;
}
if (!$this->_checkIfAllowedCategoryAndItem($context, $article))
{
return;
}
if (is_object($params) && ($params instanceof Registry))
{
$this->params_content = $params;
}
elseif (is_array($params))
{
$this->params_content = (object) $params;
}
else
{
$this->params_content = new Registry;
// Load my plugin params.
$this->params_content->loadString($params, 'JSON');
}
// Add css code
if (!isset($GLOBALS['+xji*;!1']))
{
$doc = Factory::getDocument();
$csscode = $this->params->get('csscode','');
$doc->addStyleDeclaration($csscode);
$GLOBALS['+xji*;!1'] = true;
}
if (isset($article->introtext))
{
// For core article
$text = $article->introtext;
}
else
{
// In most non core content items and modules
$text = $article->text;
}
// Fulltext is not loaded, we must load it manually if needed
$this->fulltext_loaded = false;
if ($this->params->get('Ignore_Existing_Read_More') && isset($article->introtext) && isset($article->fulltext))
{
$text = $article->introtext . PHP_EOL . $article->fulltext;
if (file_exists(JPATH_PLUGINS ."/content/cck")) { //check if Seblod is installed
$text = preg_replace( '/::cck::(\d+)::\/cck::/', '', $text ) ; //remove the ::cck:: tags
$text = preg_replace( '/::introtext::/', '', $text ) ; //remove the ::introtext:: tags, keep content
$text = preg_replace( '/::\/introtext::/', '', $text ) ; //remove the ::introtext:: tags, keep content
$text = preg_replace( '/::fulltext::/', '', $text ) ; //remove the ::fulltext:: tags, keep content
$text = preg_replace( '/::\/fulltext::/', '', $text ) ; //remove the ::fulltext:: tags, keep content
}
$this->fulltext_loaded = true;
}
elseif ($this->params->get('Ignore_Existing_Read_More') && isset($article->readmore) && $article->readmore > 0 )
{
// If we ignore manual readmore and we know it's present, then we must load the full text
$text .= $this->loadFullText($article->id);
$this->fulltext_loaded = true;
}
// apply content plugins
if ($context == 'com_content.category') {
PluginHelper::importPlugin('content');
$myparams = $this->params_content;
$myparams->set("autoreadmore",true);
$item_cls = new \stdClass;
$item_cls->text = $text;
$item_cls->id = $article->id;
Factory::getApplication()->triggerEvent('onContentPrepare', array ($context, &$item_cls, &$myparams, 0));
$text = $item_cls->text;
}
$ImageAsHTML = true;
if (in_array($context, array("com_content.featured", "com_content.category")))
{
$ImageAsHTML = $this->params->get('ImageAsHTML');
}
$thumbnails = $this->getThumbNails($text, $article, $context, $ImageAsHTML);
// How many characters are we allowed?
$app = Factory::getApplication();
// Get current menu item number of leading articles
// It's strange, but I couldn't call the variable as $params - it removes the header line in that case. I can't believe, but true.
// So don't use $params = $app->getParams(); Either use another var name like $gparams = $app->getParams(); or the direct call
// as shown below: $app->getParams()->def('num_leading_articles', 0);
$num_leading_articles = $app->getParams()->def('num_leading_articles', 0);
// Count how many times the plugin is called to know if the current article is a leading one
$globalCount = $app->get('Count', 0, $this->plg_name);
$globalCount++;
$app->set('Count', $globalCount, $this->plg_name);
// $GLOBALS['plg_content_AutoReadMore_Count'] = (isset($GLOBALS['plg_content_AutoReadMore_Count'])) ?
// $GLOBALS['plg_content_AutoReadMore_Count']+1 : 1;
// ~ if ($GLOBALS['plg_content_AutoReadMore_Count'] <= $num_leading_articles)
if ($globalCount <= $num_leading_articles)
{
// This is a leading (full-width) article.
$maxLimit = $this->params->get('leadingMax');
}
else
{
// This is not a leading article.
$maxLimit = $this->params->get('introMax');
}
if (!is_numeric($maxLimit))
{
$maxLimit = 500;
}
$this->trimming_dots = '';
if ($this->params->get('add_trimming_dots') != 0)
{
$this->trimming_dots = $this->params->get('trimming_dots');
}
$limittype = $this->params->get('limittype');
if (isset($article->readmore))
{
$original_readmore = $article->readmore;
}
$noSpaceLanguage = $this->params->get('noSpaceLanguage');
switch ($this->params->get('PluginCode'))
{
case 'only':
if (!$thereIsPluginCode)
{
// Set a fake limit type if no truncate is needed
$limittype = -1;
}
break;
case 'except':
if ($thereIsPluginCode)
{
// Set a fake limit type if no truncate is needed
$limittype = -1;
}
break;
case 'ignore':
default :
break;
}
// Limit by chars
if ($limittype == 0)
{
if (StringHelper::strlen(strip_tags($text)) > $maxLimit)
{
if ($this->params->get('Strip_Formatting') == 1)
{
// First, remove all new lines
$text = preg_replace("/\r\n|\r|\n/", "", $text);
// Next, replace <br /> tags with \n
$text = preg_replace("/<BR[^>]*>/i", "\n", $text);
// Replace <p> tags with \n\n
$text = preg_replace("/<P[^>]*>/i", "\n\n", $text);
// Strip all tags
$text = strip_tags($text);
// Truncate
$text = StringHelper::substr($text, 0, $maxLimit);
// $text = String::truncate($text, $maxLimit, '...', true);
// Pop off the last word in case it got cut in the middle
$text = preg_replace("/[.,!?:;]? [^ ]*$/", "", $text);
// Add ... to the end of the article.
$text = trim($text) . $this->trimming_dots;
// Replace \n with <br />
$text = str_replace("\n", "<br />", $text);
}
else
{
// Truncate
// $text = StringHelper::substr($text, 0, $maxLimit);
$text = AutoReadMoreString::truncate($text, $maxLimit, '…', true, $noSpaceLanguage);
if (!$noSpaceLanguage)
{
// Pop off the last word in case it got cut in the middle
$text = preg_replace("/[.,!?:;]? [^ ]*$/", "", $text);
}
// Pop off the last tag, if it got cut in the middle.
$text = preg_replace('/<[^>]*$/', '', $text);
$text = $this->addTrimmingDots($text);
// Use Tidy to repair any bad XHTML (unclosed tags etc)
$text = AutoReadMoreString::cleanUpHTML($text);
}
// Add a "read more" link, makes sense only for com_content
$article->readmore = true;
}
}
// Limit by words
elseif ($limittype == 1)
{
$original_length = StringHelper::strlen($text);
$text = AutoReadMoreString::truncateByWords($text, $maxLimit, $article->readmore);
$newLength = StringHelper::strlen($text);
if ($newLength !== $original_length)
{
$article->readmore = true;
}
$text = $this->addTrimmingDots($text);
$text = AutoReadMoreString::cleanUpHTML($text);
}
// Limit by paragraphs
elseif ($limittype == 2)
{
$paragraphs = explode('</p>', $text);
if (count($paragraphs) <= ($maxLimit + 1))
{
// Do nothing, as we have $maxLimit paragraphs
}
else
{
$text = array();
for ($i = 0; $i < $maxLimit; $i++)
{
$text[] = $paragraphs[$i];
}
unset ($paragraphs);
$text = implode('</p>', $text);
$article->readmore = true;
}
}
if ($this->params->get('Strip_Formatting') == 1)
{
$text = strip_tags($text);
}
// If we have thumbnails, add it to $text.
if ($this->params->get('Force_Image_Count'))
{
$text = preg_replace("/<img[^>]+\>/i", '', $text);
}
$text = $thumbnails . $text;
if ($this->params->get('wrap_output') == 1)
{
$template = $this->params->get('wrap_output_template');
$text = str_replace('%OUTPUT%', $text, $template);
}
if ($this->params->get('readmore_text') && empty($this->alternative_readmore))
{
$article->alternative_readmore = Text::_($this->params->get('readmore_text'));
}
$debug_addon = '';
if ($debug)
{
$debug_addon = '<code>[DEBUG: AutoReadMore fired here]</code>';
}
// conflict with other content plugins
if (isset($article->fulltext) && ($article->fulltext == "")) $article->fulltext = $article->introtext;
$article->introtext = $text . $debug_addon;
$article->text = $text . $debug_addon;
if (isset($article->readmore) && !$article->readmore)
{
if (!$this->params->get('Ignore_Existing_Read_More') && isset($original_readmore))
{
$article->readmore = $original_readmore;
}
}
}
/**
* Checkis if the content item has to be parsed by the plugin
*
* @param string $context Context
* @param object $article Content item object
*
* @return bool True if has to be parsed
*/
public function _checkIfAllowedCategoryAndItem ($context, $article)
{
if (!isset($article->catid) && !isset($article->id) )
{
return true;
}
$data = array();
// Prepare data from joomla core articles or frontpage
if (($this->params->get('joomla_articles') && $context == 'com_content.category')
|| ($this->params->get('Enabled_Front_Page') && $context == 'com_content.featured') )
{
$prefix = '';
if ($context == 'com_content.featured')
{
$prefix = 'fp_';
}
$row = array(
'category_switch' => $this->params->get($prefix . 'categories_switch'),
'category_ids' => $this->params->get($prefix . 'categories'),
'item_switch' => $this->params->get($prefix . 'articles_switch'),
'item_ids' => $this->params->get($prefix . 'id'),
);
$data[$context] = $row;
}
if ($this->params->get('joomla_articles') && ($context == 'com_content.category') ) {
if (($this->params->get('joomla_articles_featured',1) == 0) && ($article->featured == 1))
// ignore featured items in category view
return false;
}
$context_switch = $this->params->get('context_switch');
if ($context_switch == 'include')
{
$paramsContexts = $this->params->get('contextsToInclude');
$contextsToInclude = json_decode($paramsContexts);
// The default joomla installation procedure doesn't store defaut params into the DB in the correct way
if (!empty($paramsContexts) && $contextsToInclude === null)
{
$paramsContexts = str_replace("'", '"', $paramsContexts);
$contextsToInclude = json_decode($paramsContexts);
}
if (!empty($contextsToInclude) && !empty($contextsToInclude->context))
{
foreach ($contextsToInclude->context as $k => $v)
{
if ($v != $context)
{
continue;
}
$row = array(
'category_switch' => $contextsToInclude->context_categories_switch[$k],
'category_ids' => $contextsToInclude->categories_ids[$k],
'item_switch' => $contextsToInclude->context_content_items_switch[$k],
'item_ids' => $contextsToInclude->context_content_item_ids[$k],
);
$data[$context] = $row;
}
}
}
if (empty($data[$context]))
{
return true;
}
$item_switch = $data[$context]['item_switch'];
$item_ids = $data[$context]['item_ids'];
if (!is_array($item_ids))
{
$item_ids = array_map('trim', explode(',', $item_ids ?? ''));
}
$category_switch = $data[$context]['category_switch'];
$category_ids = $data[$context]['category_ids'];
if (!is_array($category_ids))
{
$category_ids = array_map('trim', explode(',', $category_ids ?? ''));
}
switch ($item_switch)
{
// Some articles are selected
case '1':
if (in_array($article->id, $item_ids))
{
return true;
}
break;
// Some articles are excluded
case '2':
// If the article is among the excluded ones - return false
if (in_array($article->id, $item_ids))
{
return false;
}
break;
// No specific articles set
case '0':
default :
break;
}
$in_array = in_array($article->catid, $category_ids);
switch ($category_switch)
{
// ALL CATS
case '0':
return true;
break;
// Selected cats
case '1':
if ($in_array)
{
return true;
}
return false;
break;
// Excludes cats
case '2':
if ($in_array)
{
return false;
}
return true;
break;
default :
break;
}
return true;
}
/**
* Check if current context is allowed either by settings or by some hardcoded rules
*
* @param string $context Context passed to the onContentPrepare method
* @param object $article Content item object
*
* @return bool true if allowed, false otherwise
*/
public function _checkIfAllowedContext ($context, $article)
{
$jinput = Factory::getApplication()->input;
$context_global = explode('.', $context);
$context_global = $context_global[0];
// Some hard-coded contexts to exclude
$hard_coded_exclude_global_contexts = array(
'com_virtuemart', // Never fire for VirtueMart
);
$contextsToExclude = array(
'com_tz_portfolio.p_article', // Never run for full article
'com_content.article', // Never run for full article
// 'mod_custom.content', // never run at a custom HTML module - DISABLED here,
// because the user must be allowed to choose this. At some circumstances joomla HTML modules may be needed to cut
);
if (in_array($context_global, $hard_coded_exclude_global_contexts) || in_array($context, $contextsToExclude) )
{
return false;
}
// SOME SPECIAL RETURNS {
// Fix easyblog
if ($context == 'easyblog.blog' && $jinput->get('view', null, 'CMD') == 'entry')
{
return false;
}
$view = $jinput->get('view', null, 'CMD');
$article_id = $jinput->get('id', null, 'INT');
if (isset($article->id))
{
if ( ($view == "article" && $article->id == $article_id)
|| ($context == 'com_k2.item' && $article->id == $article_id))
{
// If it's already a full article - go away'
if (!isset($GLOBALS['joomlaAutoReadMorePluginArticleShown']) )
{
// But leave a flag not to go aways in a module
$GLOBALS['joomlaAutoReadMorePluginArticleShown'] = $article_id;
return false;
}
}
}
if ($this->params->get('Enabled_Front_Page') == 0 and $context == 'com_content.featured')
{
return false;
}
elseif ($this->params->get('Enabled_Front_Page') == 1 and $context == 'com_content.featured')
{
return true;
}
if ($this->params->get('joomla_articles') == 0 and $context == 'com_content.category')
{
return false;
}
elseif ($context == 'com_content.categories')
{
if ($this->params->get('joomla_articles_parse_category'))
{
return true;
}
else
{
return false;
}
}
elseif ($this->params->get('joomla_articles') == 1 and in_array($context, ['com_content.category', 'com_content.categories']) )
{
// If it's an article, as a category desc doesn't contain anything in it's object except ->text
if (isset($article->id))
{
return true;
}
else
{
if ($this->params->get('joomla_articles_parse_category'))
{
return true;
}
else
{
return false;
}
}
}
$context_switch = $this->params->get('context_switch');
switch ($context_switch)
{
case 'include':
$paramsContexts = $this->params->get('contextsToInclude');
$contextsToInclude = json_decode($paramsContexts);
// The default joomla installation procedure doesn't store defaut params into the DB in the correct way
if (!empty($paramsContexts) && $contextsToInclude === null)
{
$paramsContexts = str_replace("'", '"', $paramsContexts);
$contextsToInclude = json_decode($paramsContexts);
}
if (!empty($contextsToInclude) && !empty($contextsToInclude->context))
{
foreach ($contextsToInclude->context as $k => $v)
{
if ($context == $v)
{
return true;
}
}
}
return false;
case 'exclude':
// Not to work on modules, like mod_roksprocket.article
if ($this->params->get('exclude_mod_contexts') && strpos($context, 'mod_') === 0)
{
return false;
}
$contextsToExclude = $this->params->get('contextsToExclude');
$contextsToExclude = array_map('trim', explode(",", $contextsToExclude));
if (in_array($context, $contextsToExclude))
{
return false;
}
break;
case 'all_enabled':
return true;
break;
case 'all_disabled':
// This check just in case, should never come here in such a case
if (!in_array($context, array('com_content.category', 'com_content.featured')))
{
return false;
}
break;
default :
break;
}
return true;
}
/**
* Add Trimming dots
*
* @param string $text Text to add trimming symbol(s)
*
* @return string Text with added trimming symbol(s)
*/
public function addTrimmingDots($text)
{
// Add ... to the end of the article if the last character is a letter or a number.
if ($this->params->get('add_trimming_dots') == 2)
{
if (preg_match('/\w/ui', StringHelper::substr($text, -1)))
{
$text = trim($text) . $this->trimming_dots;
}
}
else
{
$text = trim($text) . $this->trimming_dots;
}
return $text;
}
/**
* Returns the full text of the article based on the article id
*
* @param integer $id The id of the artice to load
*
* @return string The article fulltext
*/
public function loadFullText ($id)
{
$article = JTable::getInstance("content");
$article->load($id);
$article->fulltext_loaded = true;
return $article->fulltext;
}
/**
* Returns text with handled images - added classes, stripped attributes, if needed
*
* @param string &$text HTML code of the article
* @param object &$article Article object for additional information like $article->id
* @param text $context Context
* @param bool $ImageAsHTML If to return html code or to update the $article object
*
* @return text HTML code to include containing images
*/
public function getThumbNails( & $text, & $article, $context, $ImageAsHTML = true)
{
// Are we working with any thumbnails?
if ($this->params->get('Thumbnails') < 1)
{
return;
}
$thumbnails = array();
switch ($this->params->get('image_search_pattern')) {
case 'custom':
$patterns = explode(PHP_EOL, $this->params->get('image_search_pattern_custom'));
$patterns = array_map('trim', $patterns);
break;
case 'a_wrapped':
$patterns = ['~<a[^>]+><img [^>]+></a>~ui'];
break;
case 'img_only':
default :
$patterns = ['~<img [^>]*>~iu'];
break;
}
$totalThumbNails = $this->params->get('Thumbnails');
// ~ $patterns = [
// ~ '/<img [^>]*>/iu',
// ~ '~<a[^>]+><img [^>]+></a>~ui',
// ~ ];
$total_matches = [];
$fulltext = '';
foreach ($patterns as $pattern)
{
// Extract all images from the article.
$imagesfound = preg_match_all($pattern, $text, $matches);
// If we found less thumbnail then expected and the fulltext is not loaded,
// then load fulltext and search in it also
$matches_tmp = array ();
$json = json_decode($article->images);
if (!empty($json->image_intro))
{
$totalThumbNails--;
}
if ($totalThumbNails < 0)
{
$totalThumbNails = 0;
}
if ($imagesfound < $totalThumbNails && empty($fulltext))
{
if (isset($article->fulltext))
{
$fulltext = $article->fulltext;
}
elseif(isset($article->id) && !$this->fulltext_loaded && in_array($context, array('com_content.category','com_content.featured')))
{
$this->loadFullText($article->id);
}
$matches_tmp = $matches[0];
$imagesfound = preg_match_all($pattern, $fulltext, $matches);
}
$matches = array_merge($matches_tmp, $matches[0]);
foreach ($matches as $km => $match)
{
$placeholder = '// ##mygruz20170704012529###' . $km . '###// ##mygruz20170704012529';
$text = str_replace($match, $placeholder, $text);
$fulltext = str_replace($match, $placeholder, $fulltext);
if (!in_array($match, $total_matches))
{
$total_matches[$placeholder] = $match;
}
}
}
$matches = [];
foreach ($total_matches as $placeholder => $match)
{
$text = str_replace($placeholder, $match, $text);
$matches[] = $match;
}
// Loop through the thumbnails.
for ($thumbnail = 0; $thumbnail < $totalThumbNails; $thumbnail++)
{
if (!isset($matches[$thumbnail]))
{
break;
}
// Remove the image from $text
$text = str_replace($matches[$thumbnail], '', $text);
// See if we need to remove styling.
if (trim($this->params->get('Thumbnails_Class')) != '')
{
// Remove style, class, width, border, and height attributes.
if ($this->params->get('Strip_Image_Formatting'))
{
// Add CSS class name.
$matches[$thumbnail] = preg_replace('/(style|class|width|height|border) ?= ?[\'"][^\'"]*[\'"]/i', '', $matches[$thumbnail]);
$matches[$thumbnail] = preg_replace('@/?>$@', 'class="' . $this->params->get('Thumbnails_Class') . '" />', $matches[$thumbnail]);
}
else
{
$matches[$thumbnail] = preg_replace('@(class=["\'])@', '$1' . $this->params->get('Thumbnails_Class') . ' ', $matches[$thumbnail], -1, $count);
if ($count < 1)
{
$matches[$thumbnail] = preg_replace('@/?>$@', 'class="' . $this->params->get('Thumbnails_Class') . '" />', $matches[$thumbnail]);
}
}
}
if (trim($matches[$thumbnail]) != '')
{
if ($ImageAsHTML)
{
$thumbnails[] = $matches[$thumbnail];
}
elseif ($thumbnail === 0 )
{
// Just flag for later see if there was at least one image
$thumbnails[] = '';
foreach (array('image_intro' => 'src', 'image_intro_alt' => 'alt', 'image_intro_caption' => 'title') as $k => $v)
{
$match = null;
preg_match('@' . $v . '="([^"]+)"@', $matches[$thumbnail], $match);
// ${$v} = array_pop($match);
$json->{$k} = array_pop($match);
}
}
}
}
if (empty($thumbnails) && trim($this->params->get('default_image')) != '' )
{
$Thumbnails_Class = $this->params->get('Thumbnails_Class');
$Thumbnails_Class_Check = trim($Thumbnails_Class);
if (!empty($Thumbnails_Class_Check))
{
$Thumbnails_Class = ' class="' . $Thumbnails_Class . '"';
}
else