utils.php
47.9 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
<?php
/**
* /lib/utils.php
*
* @package Relevanssi
* @author Mikko Saari
* @license https://wordpress.org/about/gpl/ GNU General Public License
* @see https://www.relevanssi.com/
*/
/**
* Returns a Relevanssi_Taxonomy_Walker instance.
*
* Requires the class file and generates a new Relevanssi_Taxonomy_Walker instance.
*
* @return object A new Relevanssi_Taxonomy_Walker instance.
*/
function get_relevanssi_taxonomy_walker() {
require_once 'class-relevanssi-taxonomy-walker.php';
return new Relevanssi_Taxonomy_Walker();
}
/**
* Adds apostrophes around a string.
*
* @param string $str The string.
*
* @return string The string with apostrophes around it.
*/
function relevanssi_add_apostrophes( $str ) {
return "'" . $str . "'";
}
/**
* Adds quotes around a string.
*
* @param string $str The string.
*
* @return string The string with quotes around it.
*/
function relevanssi_add_quotes( $str ) {
return '"' . $str . '"';
}
/**
* Wraps the relevanssi_mb_trim() function so that it can be used as a callback
* for array_walk().
*
* @since 2.1.4
*
* @see relevanssi_mb_trim.
*
* @param string $str String to trim.
*/
function relevanssi_array_walk_trim( string &$str ) {
$str = relevanssi_mb_trim( $str );
}
/**
* Converts sums in an array to averages, based on an array containing counts.
*
* Both arrays need to have (key, value) pairs with the same keys. The values
* in $arr are then divided by the matching values in $counts, so when we have
* sums in $arr and counts in $counts, we end up with averages.
*
* @param array $arr The array with sums, passed as reference.
* @param array $counts The array with counts.
*/
function relevanssi_average_array( array &$arr, array $counts ) {
array_walk(
$arr,
function ( &$value, $key ) use ( $counts ) {
$value = round( $value / $counts[ $key ], 2 );
}
);
}
/**
* Returns 'checked' if the option is enabled.
*
* @param string $option Value to check.
*
* @return string If the option is 'on', returns 'checked', otherwise returns an
* empty string.
*/
function relevanssi_check( string $option ) {
$checked = '';
if ( 'on' === $option ) {
$checked = 'checked';
}
return $checked;
}
/**
* Closes tags in a bit of HTML code.
*
* Used to make sure no tags are left open in excerpts. This method is not
* foolproof, but it's good enough for now.
*
* @param string $html The HTML code to analyze.
*
* @return string The HTML code, with tags closed.
*/
function relevanssi_close_tags( string $html ) {
$result = array();
preg_match_all(
'#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU',
$html,
$result
);
$opened_tags = $result[1];
preg_match_all( '#</([a-z]+)>#iU', $html, $result );
$closed_tags = $result[1];
$len_opened = count( $opened_tags );
if ( count( $closed_tags ) === $len_opened ) {
return $html;
}
$opened_tags = array_reverse( $opened_tags );
for ( $i = 0; $i < $len_opened; $i++ ) {
if ( ! in_array( $opened_tags[ $i ], $closed_tags, true ) ) {
$html .= '</' . $opened_tags[ $i ] . '>';
} else {
unset(
$closed_tags[ array_search( $opened_tags[ $i ], $closed_tags, true ) ]
);
}
}
return $html;
}
/**
* Counts search term occurrances in the Relevanssi index.
*
* @param string $query The search query. Will be split at spaces.
* @param string $mode Output mode. Possible values 'array' or 'string'.
* Default is 'array'.
*
* @return array|string An array of search term occurrances, or a string with
* the number of occurrances.
*/
function relevanssi_count_term_occurrances( string $query, string $mode = 'array' ) {
global $wpdb, $relevanssi_variables;
$terms = explode( ' ', $query );
$counts = array();
foreach ( $terms as $term ) {
$term = trim( $term );
if ( empty( $term ) ) {
continue;
}
$counts[ $term ] = $wpdb->get_var(
$wpdb->prepare(
'SELECT SUM(content + title + comment + tag +
link + author + category + excerpt + taxonomy + customfield
+ mysqlcolumn) AS total FROM ' .
$relevanssi_variables['relevanssi_table'] . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
' WHERE term = %s
GROUP BY term',
$term
)
);
}
if ( 'array' === $mode ) {
return $counts;
} elseif ( 'string' === $mode ) {
$strings = array();
foreach ( $counts as $term => $count ) {
$strings[] = "<span class='search_term'>$term</span>: <span class='count'>$count</span>";
}
return implode( ', ', $strings );
}
}
/**
* Prints out debugging notices.
*
* If WP_CLI is available, prints out the debug notice as a WP_CLI::log(),
* otherwise if debug mode is on, uses error_log(), otherwise just echo.
*
* @param string $notice The notice to print out.
*/
function relevanssi_debug_echo( string $notice ) {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::log( $notice );
} elseif ( relevanssi_log_debug() ) {
error_log( 'RELEVANSSI: ' . $notice ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
} else {
echo esc_html( $notice ) . "\n";
}
}
/**
* Runs do_shortcode() on content, but safeguards the global $post to make sure
* it isn't changed by the shortcodes. If shortcode expansion is disabled in
* Relevanssi settings, runs strip_shortcodes() on the content.
*
* @uses relevanssi_disable_shortcodes() Disables problem shortcodes.
* @see do_shortcode() Expands shortcodes.
* @see strip_shortcodes() Strips off shortcodes.
*
* @param string $content The content where the shortcodes are expanded.
*
* @return string
*/
function relevanssi_do_shortcode( string $content ): string {
if ( 'on' === get_option( 'relevanssi_expand_shortcodes' ) ) {
// TablePress support.
if ( function_exists( 'relevanssi_enable_tablepress_shortcodes' ) ) {
$tablepress_controller = relevanssi_enable_tablepress_shortcodes();
}
relevanssi_disable_shortcodes();
/**
* This needs to be global here, otherwise the safety mechanism doesn't
* work correctly.
*/
global $post;
$global_post_before_shortcode = null;
if ( isset( $post ) ) {
$global_post_before_shortcode = $post;
}
$content = do_shortcode( $content );
if ( $global_post_before_shortcode ) {
$post = $global_post_before_shortcode; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
unset( $tablepress_controller );
} else {
$content = strip_shortcodes( $content );
}
return $content;
}
/**
* Recursively flattens a multidimensional array to produce a string.
*
* @param array $arr The source array.
*
* @return string The array contents as a string.
*/
function relevanssi_flatten_array( array $arr ) {
$return_value = '';
foreach ( new RecursiveIteratorIterator( new RecursiveArrayIterator( $arr ) ) as $value ) {
$return_value .= ' ' . $value;
}
return trim( $return_value );
}
/**
* Generates from and to date values from ranges.
*
* Possible values in the $request array: 'from' and 'to' for direct dates,
* 'this_year' for Jan 1st to today, 'this_month' for 1st of month to today,
* 'last_month' for 1st of previous month to last of previous month,
* 'this_week' for Monday of this week to today (or Sunday, if the
* relevanssi_week_starts_on_sunday returns `true`), 'last_week' for the
* previous week, 'last_30' for from 30 days ago to today, 'last_7' for from
* 7 days ago to today.
*
* @param array $request The request array where the settings are.
* @param string $from The default 'from' date in "Y-m-d" format.
* @return array The from date in 'from' and the to date in 'to' in "Y-m-d"
* format.
*/
function relevanssi_from_and_to( array $request, string $from ): array {
$today = gmdate( 'Y-m-d' );
$week_start = 'monday';
$to = $today;
/**
* Controls whether the week starts on Sunday or Monday.
*
* @param boolean If `true`, week starts on Sunday. Default `false`, week
* starts on Monday.
*/
if ( apply_filters( 'relevanssi_week_starts_on_sunday', false ) ) {
$week_start = 'sunday';
}
if ( ! isset( $request['everything'] ) && isset( $request['from'] ) && $request['from'] > $from ) {
$from = $request['from'];
}
if ( ! isset( $request['everything'] ) && isset( $request['to'] ) && $request['to'] < $today ) {
$to = $request['to'];
}
if ( isset( $request['this_year'] ) ) {
$from = gmdate( 'Y-m-d', strtotime( 'first day of january this year' ) );
$to = gmdate( 'Y-m-d' );
}
if ( isset( $request['this_month'] ) ) {
$from = gmdate( 'Y-m-d', strtotime( 'first day of this month' ) );
$to = gmdate( 'Y-m-d' );
}
if ( isset( $request['last_month'] ) ) {
$from = gmdate( 'Y-m-d', strtotime( 'first day of previous month' ) );
$to = gmdate( 'Y-m-d', strtotime( 'last day of previous month' ) );
}
if ( isset( $request['this_week'] ) ) {
$from = gmdate( 'Y-m-d', strtotime( 'previous ' . $week_start ) );
$to = gmdate( 'Y-m-d' );
}
if ( isset( $request['last_week'] ) ) {
$start = 'sunday' === $week_start ? gmdate( 'w' ) + 7 : gmdate( 'w' ) + 6;
$end = 'sunday' === $week_start ? gmdate( 'w' ) + 1 : gmdate( 'w' );
$from = gmdate( 'Y-m-d', strtotime( '-' . $start . ' days' ) );
$to = gmdate( 'Y-m-d', strtotime( '-' . $end . ' days' ) );
}
if ( isset( $request['last_30'] ) ) {
$from = gmdate( 'Y-m-d', strtotime( '-30 days' ) );
$to = gmdate( 'Y-m-d' );
}
if ( isset( $request['last_7'] ) ) {
$from = gmdate( 'Y-m-d', strtotime( '-7 days' ) );
$to = gmdate( 'Y-m-d' );
}
return array(
'from' => $from,
'to' => $to,
);
}
/**
* Generates closing tags for an array of tags.
*
* @param array $tags Array of tag names.
*
* @return array $closing_tags Array of closing tags.
*/
function relevanssi_generate_closing_tags( array $tags ) {
$closing_tags = array();
foreach ( $tags as $tag ) {
$a = str_replace( '<', '</', $tag );
$b = str_replace( '>', '/>', $tag );
$closing_tags[] = $a;
$closing_tags[] = $b;
}
return $closing_tags;
}
/**
* Returns a post object based on ID, **type**id notation or an object.
*
* @uses relevanssi_get_post_object() Fetches post objects.
*
* @param int|string|WP_Post $source The source identified to parse, either a
* post ID integer, a **type**id string or a post object.
*
* @return array An array containing the actual object in 'object' and the
* format of the original value in 'format'. The value can be 'object', 'id'
* or 'id=>parent'.
*/
function relevanssi_get_an_object( $source ) {
$object = $source;
$format = 'object';
if ( ! is_object( $source ) ) {
// Convert from post ID to post.
$object = relevanssi_get_post_object( $source );
$format = 'id';
} elseif ( isset( $source->type ) ) {
// Convert from id=>type to post.
$object = relevanssi_get_post_object( $source->ID );
$format = 'id=>type';
} elseif ( ! isset( $source->post_content ) ) {
// Convert from id=>parent to post.
$object = relevanssi_get_post_object( $source->ID );
$format = 'id=>parent';
}
return array(
'object' => $object,
'format' => $format,
);
}
/**
* Returns the attachment filename suffix.
*
* Reads the filename from $post->guid and returns the file suffix.
*
* @param WP_Post|int $post The post object or post ID.
* @return string The suffix if it is found, an empty string otherwise.
*/
function relevanssi_get_attachment_suffix( $post ): string {
if ( ! is_object( $post ) ) {
$post = relevanssi_get_post( $post );
if ( is_wp_error( $post ) ) {
return '';
}
}
if ( 'attachment' !== $post->post_type ) {
return '';
}
list( , $type ) = explode( '.', basename( $post->guid ) );
return $type;
}
/**
* Returns the locale or language code.
*
* If WPML or Polylang is not available, returns `get_locale()` value. With
* WPML or Polylang, first this function checks to see if the global $post is
* set. If it is, the function returns the language of the post, as we're
* working on a post and need to use the correct language.
*
* If the global $post is not set, this function returns for Polylang the
* results of `pll_current_language()`, for WPML it uses `wpml_current_language`
* and `wpml_active_languages`.
*
* @param boolean $locale If true, return locale; if false, return language
* code.
*
* @return string The locale or the language code.
*/
function relevanssi_get_current_language( bool $locale = true ) {
$current_language = get_locale();
if ( ! $locale ) {
$current_language = substr( $current_language, 0, 2 );
}
if ( class_exists( 'Polylang', false ) ) {
global $post;
if ( isset( $post ) ) {
if ( isset( $post->term_id ) && function_exists( 'pll_get_term_language' ) ) {
$current_language = pll_get_term_language( $post->term_id, $locale ? 'locale' : 'slug' );
} elseif ( ! isset( $post->user_id ) && function_exists( 'pll_get_post_language' ) ) {
$current_language = pll_get_post_language( $post->ID, $locale ? 'locale' : 'slug' );
}
} elseif ( function_exists( 'pll_current_language' ) ) {
$pll_language = pll_current_language( $locale ? 'locale' : 'slug' );
$current_language = $pll_language ? $pll_language : $current_language;
}
}
if ( function_exists( 'icl_object_id' ) && ! function_exists( 'pll_is_translated_post_type' ) ) {
global $post;
if ( isset( $post ) ) {
$language_details = array(
'locale' => '',
'language_code' => '',
);
if ( isset( $post->term_id ) ) {
// Terms don't have a locale, just a language code.
$element = array(
'element_id' => relevanssi_get_term_tax_id( $post->term_id, $post->post_type ),
'element_type' => $post->post_type,
);
$language_code = apply_filters( 'wpml_element_language_code', null, $element );
$language_details['language_code'] = $language_code;
} elseif ( ! isset( $post->user_id ) && 'post_type' !== $post->post_type ) {
// Users don't have language details.
$language_details = apply_filters( 'wpml_post_language_details', null, $post->ID );
}
if ( is_wp_error( $language_details ) ) {
$current_language = apply_filters( 'wpml_current_language', null );
} else {
$current_language = $language_details[ $locale ? 'locale' : 'language_code' ];
}
} elseif ( $locale ) {
$languages = apply_filters( 'wpml_active_languages', null );
foreach ( $languages as $l ) {
if ( $l['active'] ) {
$current_language = $l['default_locale'];
break;
}
}
} else {
$current_language = apply_filters( 'wpml_current_language', null );
}
}
return $current_language;
}
/**
* Gets the permalink to the current post within Loop.
*
* Uses get_permalink() to get the permalink, then adds the 'highlight'
* parameter if necessary using relevanssi_add_highlight().
*
* @param int|WP_Post $post Post ID or post object. Default is the global $post.
*
* @see get_permalink()
*
* @return string The permalink.
*/
function relevanssi_get_permalink( $post = 0 ) {
/**
* Filters the permalink.
*
* @param string The permalink, generated by get_permalink().
*/
$permalink = apply_filters( 'relevanssi_permalink', get_permalink( $post ) );
return $permalink;
}
/**
* Replacement for get_post() that uses the Relevanssi post cache.
*
* Tries to fetch the post from the Relevanssi post cache. If that doesn't work,
* gets the post using get_post().
*
* @param int|string $post_id The post ID. Usually an integer post ID, but can
* also be a string (u_<user ID>, p_<post type name> or
* **<taxonomy>**<term ID>).
* @param int $blog_id The blog ID, default -1. If -1, will be replaced
* with the actual current blog ID from get_current_blog_id().
*
* @return object|WP_Error The post object or a WP_Error object if the post
* doesn't exist.
*/
function relevanssi_get_post( $post_id, int $blog_id = -1 ) {
if ( -1 === $blog_id ) {
$blog_id = get_current_blog_id();
}
if ( function_exists( 'relevanssi_premium_get_post' ) ) {
return relevanssi_premium_get_post( $post_id, $blog_id );
}
global $relevanssi_post_array;
$post = null;
if ( isset( $relevanssi_post_array[ $post_id ] ) ) {
$post = $relevanssi_post_array[ $post_id ];
}
if ( ! $post ) {
$post = get_post( $post_id );
$relevanssi_post_array[ $post_id ] = $post;
}
if ( ! $post ) {
$post = new WP_Error( 'post_not_found', __( 'The requested post does not exist.' ) );
}
return $post;
}
/**
* Fetches post meta value for a large group of posts with just one query.
*
* This function can be used to reduce the number of database queries. Instead
* of looping through an array of posts and calling get_post_meta() for each
* individual post, you can get all the values with this function with just one
* database query.
*
* @param array $post_ids An array of post IDs.
* @param string $field The name of the field.
*
* @return array An array of post_id, meta_value pairs.
*/
function relevanssi_get_post_meta_for_all_posts( array $post_ids, string $field ): array {
global $wpdb;
$post_ids_string = implode( ',', $post_ids );
$meta_values = array();
if ( $post_ids_string ) {
$meta_values = $wpdb->get_results(
$wpdb->prepare(
"SELECT post_id, meta_value FROM $wpdb->postmeta
WHERE meta_key = %s
AND post_id IN ( $post_ids_string )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$field
)
);
}
$results = array();
foreach ( $meta_values as $row ) {
$results[ $row->post_id ] = $row->meta_value;
}
return $results;
}
/**
* Returns an object based on ID.
*
* Wrapper to handle non-post cases (terms, user profiles). Regular posts are
* passed on to relevanssi_get_post().
*
* @uses relevanssi_get_post() Used to fetch regular posts.
*
* @param int|string $post_id An ID, either an integer post ID or a
* **type**id string for terms and users.
*
* @return WP_Post|WP_Term|WP_User|WP_Error An object, type of which depends on
* the target object. If relevanssi_get_post() doesn't find the post, this
* returns a WP_Error.
*/
function relevanssi_get_post_object( $post_id ) {
$object = null;
if ( '*' === substr( $post_id, 0, 1 ) ) {
// Convert from **type**id to a user or a term object.
$parts = explode( '**', $post_id );
$type = $parts[1] ?? null;
$id = $parts[2] ?? null;
if ( $type && $id ) {
if ( 'user' === $type ) {
$object = get_user_by( 'id', $id );
} else {
$object = get_term( $id, $type );
}
}
} else {
$object = relevanssi_get_post( $post_id );
}
return $object;
}
/**
* Returns the term taxonomy ID for a term based on term ID.
*
* @global object $wpdb The WordPress database interface.
*
* @param int $term_id The term ID.
* @param string $taxonomy The taxonomy.
*
* @return int Term taxonomy ID.
*/
function relevanssi_get_term_tax_id( int $term_id, string $taxonomy ) {
global $wpdb;
return $wpdb->get_var(
$wpdb->prepare(
"SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = %s",
$term_id,
$taxonomy
)
);
}
/**
* Fetches the taxonomy based on term ID.
*
* Fetches the taxonomy from wp_term_taxonomy based on term_id.
*
* @global object $wpdb The WordPress database interface.
*
* @param int $term_id The term ID.
*
* @deprecated Will be removed in future versions.
*
* @return string $taxonomy The term taxonomy.
*/
function relevanssi_get_term_taxonomy( int $term_id ) {
global $wpdb;
$taxonomy = $wpdb->get_var( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
return $taxonomy;
}
/**
* Gets a list of tags for post.
*
* Replacement for get_the_tags() that does the same, but applies Relevanssi
* search term highlighting on the results.
*
* @uses relevanssi_the_tags() Does the actual work.
*
* @param string $before What is printed before the tags, default ''.
* @param string $separator The separator between items, default ', '.
* @param string $after What is printed after the tags, default ''.
* @param int $post_id The post ID. Default current post ID (in the Loop).
*/
function relevanssi_get_the_tags( string $before = '', string $separator = ', ', string $after = '', int $post_id = 0 ) {
return relevanssi_the_tags( $before, $separator, $after, false, $post_id );
}
/**
* Returns the post title with highlighting.
*
* Reads the highlighted title from $post->post_highlighted_title. Uses the
* relevanssi_get_post() to fecth the post.
*
* @uses relevanssi_get_post() Fetches post objects.
*
* @param int|WP_Post $post The post ID or a post object.
*
* @return string The post title with highlights and an empty string, if the
* post cannot be found.
*/
function relevanssi_get_the_title( $post ) {
if ( is_numeric( $post ) ) {
$post = relevanssi_get_post( $post );
}
if ( is_wp_error( $post ) ) {
return '';
}
if ( empty( $post->post_highlighted_title ) ) {
$post->post_highlighted_title = $post->post_title;
}
return $post->post_highlighted_title;
}
/**
* Adds a soft hyphen to a string at every five characters.
*
* @param string $str The string to hyphenate.
*
* @return string The hyphenated string.
*/
function relevanssi_hyphenate( $str ) {
$str = preg_replace( '/([^\s]{8})([^\s])/u', '$1­$2', html_entity_decode( $str ) );
return $str;
}
/**
* Returns an imploded string if the option exists and is an array, an empty
* string otherwise.
*
* @see implode()
*
* @param array $request An array of option values.
* @param string $option The key to check.
* @param string $glue The glue string for implode(), default ','.
*
* @return string Imploded string or an empty string.
*/
function relevanssi_implode( array $request, string $option, string $glue = ',' ) {
if ( isset( $request[ $option ] ) && is_array( $request[ $option ] ) ) {
return implode( $glue, $request[ $option ] );
}
return '';
}
/**
* Increases a value. If it's not set, sets it first to the default value.
*
* @param int $value The value to increase (passed by reference).
* @param int $increase The amount to increase the value, default 1.
* @param int $def_value The default value, default 0.
*/
function relevanssi_increase_value( &$value, $increase = 1, $def_value = 0 ) {
if ( ! isset( $value ) ) {
$value = $def_value;
}
$value += $increase;
}
/**
* Returns the intval of the option if it exists, null otherwise.
*
* @see intval()
*
* @param array $request An array of option values.
* @param string $option The key to check.
*
* @return int|null Integer value of the option, or null.
*/
function relevanssi_intval( array $request, string $option ) {
if ( isset( $request[ $option ] ) ) {
return intval( $request[ $option ] );
}
return null;
}
/**
* Returns true if the search is from Relevanssi Live Ajax Search.
*
* Checks if $wp_query->query_vars['action'] is set to "relevanssi_live_search".
*
* @return bool True if the search is from Relevanssi Live Ajax Search, false
* otherwise.
*/
function relevanssi_is_live_search() {
global $wp_query;
$relevanssi_live_search = false;
if ( isset( $wp_query->query_vars['action'] ) && 'relevanssi_live_search' === $wp_query->query_vars['action'] ) {
$relevanssi_live_search = true;
}
return $relevanssi_live_search;
}
/**
* Checks if a string is a multiple-word phrase.
*
* Replaces hyphens, quotes and ampersands with spaces if necessary based on
* the Relevanssi advanced indexing settings.
*
* @param string $str The string to check.
*
* @return boolean True if the string is a multiple-word phrase, false otherwise.
*/
function relevanssi_is_multiple_words( string $str ): bool {
if ( empty( $str ) ) {
return false;
}
$punctuation = get_option( 'relevanssi_punctuation' );
if ( 'replace' === $punctuation['hyphens'] ) {
$str = str_replace(
array(
'-',
'–',
'—',
),
' ',
$str
);
}
if ( 'replace' === $punctuation['quotes'] ) {
$str = str_replace(
array(
'’',
"'",
'’',
'‘',
'”',
'“',
'„',
'´',
'″',
),
' ',
$str
);
}
if ( 'replace' === $punctuation['ampersands'] ) {
$str = str_replace(
array(
'&',
'&',
'&',
),
' ',
$str
);
}
if ( count( explode( ' ', $str ) ) > 1 ) {
return true;
}
return false;
}
/**
* Launches an asynchronous Ajax action.
*
* Makes a wp_remote_post() call with the specific action. Handles nonce
* verification.
*
* @see wp_remove_post()
* @see wp_create_nonce()
*
* @param string $action The action to trigger (also the name of the
* nonce).
* @param array $payload_args The parameters sent to the action. Defaults to
* an empty array.
*
* @return WP_Error|array The wp_remote_post() response or WP_Error on failure.
*/
function relevanssi_launch_ajax_action( string $action, array $payload_args = array() ) {
$cookies = array();
foreach ( $_COOKIE as $name => $value ) {
$cookies[] = "$name=" . rawurlencode(
is_array( $value ) ? wp_json_encode( $value ) : $value
);
}
$default_payload = array(
'action' => $action,
'_nonce' => wp_create_nonce( $action ),
);
$payload = array_merge( $default_payload, $payload_args );
$args = array(
'timeout' => 0.01,
'blocking' => false,
'body' => $payload,
'headers' => array(
'cookie' => implode( '; ', $cookies ),
),
);
$url = admin_url( 'admin-ajax.php' );
return wp_remote_post( $url, $args );
}
/**
* Returns a legal value.
*
* @param array $request An array of option values.
* @param string $option The key to check.
* @param array $values The legal values.
* @param string $def_val The default value.
*
* @return string|null A legal value or the default value, null if the option
* isn't set.
*/
function relevanssi_legal_value( array $request, string $option, array $values, string $def_val ) {
$value = null;
if ( isset( $request[ $option ] ) ) {
$value = $def_val;
if ( in_array( $request[ $option ], $values, true ) ) {
$value = $request[ $option ];
}
}
return $value;
}
/**
* Multibyte friendly case-insensitive string comparison.
*
* If multibyte string functions are available, do strnatcmp() after using
* mb_strtoupper() to both strings. Otherwise use strnatcasecmp().
*
* @see strnatcasecmp() Falls back to this if multibyte functions are
* not available.
* @see strnatcmp() Used to compare the strings.
* @see mb_strtoupper() Used to convert strings to uppercase.
*
* @param string $str1 First string to compare.
* @param string $str2 Second string to compare.
* @param string $encoding The encoding to use, default mb_internal_encoding().
*
* @return int $val Returns < 0 if str1 is less than str2; > 0 if str1 is
* greater than str2, and 0 if they are equal.
*/
function relevanssi_mb_strcasecmp( $str1, $str2, $encoding = '' ): int {
if ( ! function_exists( 'mb_internal_encoding' ) ) {
return strnatcasecmp( $str1, $str2 );
} else {
if ( empty( $encoding ) ) {
$encoding = mb_internal_encoding();
}
return strnatcmp( mb_strtoupper( $str1, $encoding ), mb_strtoupper( $str2, $encoding ) );
}
}
/**
* Multibyte friendly case-insensitive string search.
*
* If multibyte string functions are available, do mb_stristr(). Otherwise,
* do stristr().
*
* @see stristr() Falls back to this if multibyte functions are not
* available.
*
* @param string $haystack The string to search in.
* @param string $needle The string to search for.
* @param string $encoding The encoding to use, default mb_internal_encoding().
*
* @return bool True if the needle was found in the haystack, false otherwise.
*/
function relevanssi_mb_stristr( $haystack, $needle, $encoding = '' ): bool {
if ( ! function_exists( 'mb_internal_encoding' ) ) {
return stristr( $haystack, $needle );
} else {
if ( empty( $encoding ) ) {
$encoding = mb_internal_encoding();
}
return mb_stristr( $haystack, $needle, false, $encoding );
}
}
/**
* Trims multibyte strings.
*
* Removes the 194+160 non-breakable spaces, removes null bytes and removes
* whitespace.
*
* @param string $str The source string.
*
* @return string Trimmed string.
*/
function relevanssi_mb_trim( string $str ) {
$str = str_replace( chr( 194 ) . chr( 160 ), '', $str );
$str = str_replace( "\0", '', $str );
$str = preg_replace( '/(^\s+)|(\s+$)/us', '', $str );
return $str;
}
/**
* Returns 'on' if option exists and value is not 'off', otherwise 'off'.
*
* @param array $request An array of option values.
* @param string $option The key to check.
*
* @return string 'on' or 'off'.
*/
function relevanssi_off_or_on( array $request, string $option ) {
if ( isset( $request[ $option ] ) && 'off' !== $request[ $option ] ) {
return 'on';
}
return 'off';
}
/**
* Removes quotes (", ”, “) from a string.
*
* @param string $str The string to clean.
*
* @return string The cleaned string.
*/
function relevanssi_remove_quotes( string $str ) {
return str_replace( array( '”', '“', '"' ), '', $str );
}
/**
* Removes quotes from array keys. Does not keep array values.
*
* Used to remove phrase quotes from search term array, which have the format
* of (term => hits). The number of hits is not needed, so this function
* discards it as a side effect.
*
* @uses relevanssi_remove_quotes() This does the actual work.
*
* @param array $arr An array to process.
*
* @return array The same array with quotes removed from the keys.
*/
function relevanssi_remove_quotes_from_array_keys( array $arr ) {
$arr = array_keys( $arr );
array_walk(
$arr,
function ( &$key ) {
$key = relevanssi_remove_quotes( $key );
}
);
return array_flip( $arr );
}
/**
* Returns an ID=>parent object from a post (or a term, or a user).
*
* @param WP_Post|WP_Term|WP_User $post_object The source object.
*
* @return object An object with the attributes ID and post_parent set. For
* terms and users, ID is the term or user ID and post_parent is 0. For bad
* inputs, returns 0 and 0.
*/
function relevanssi_return_id_parent( $post_object ) {
$id_parent_object = new stdClass();
if ( isset( $post_object->ID ) ) {
$id_parent_object->ID = $post_object->ID;
$id_parent_object->post_parent = $post_object->post_parent;
} elseif ( isset( $post_object->term_id ) ) {
$id_parent_object->ID = $post_object->term_id;
$id_parent_object->post_parent = 0;
} elseif ( isset( $post_object->user_id ) ) {
$id_parent_object->ID = $post_object->user_id;
$id_parent_object->post_parent = 0;
} else {
$id_parent_object->ID = 0;
$id_parent_object->post_parent = 0;
}
return $id_parent_object;
}
/**
* Returns an ID=>type object from a post (or a term, or a user).
*
* @param WP_Post|WP_Term|WP_User $post_object The source object.
*
* @return object An object with the attributes ID and type set. Type is
* 'post', 'user', 'term' or 'post_type'. For terms, also fills in 'taxonomy',
* for post types 'name'.
*/
function relevanssi_return_id_type( $post_object ) {
$id_type_object = new stdClass();
if ( isset( $post_object->ID ) ) {
$id_type_object->ID = $post_object->ID;
$id_type_object->type = 'post';
} elseif ( isset( $post_object->term_id ) ) {
$id_type_object->ID = $post_object->term_id;
$id_type_object->type = 'term';
$id_type_object->taxonomy = $post_object->taxonomy;
} elseif ( isset( $post_object->user_id ) ) {
$id_type_object->ID = $post_object->user_id;
$id_type_object->type = 'user';
} else {
$id_type_object->ID = 0;
$id_type_object->post_parent = 0;
}
return $id_type_object;
}
/**
* Returns "off".
*
* Useful for returning "off" to filters easily.
*
* @return string A string with value "off".
*/
function relevanssi_return_off() {
return 'off';
}
/**
* Returns "OR".
*
* @return string A string with value "OR".
*/
function relevanssi_return_or() {
return 'OR';
}
/**
* Gets a post object, returns ID, ID=>parent or the post object.
*
* @uses relevanssi_return_id_type() Used to return ID=>type results.
* @uses relevanssi_return_id_parent() Used to return ID=>parent results.
*
* @param object $post The post object.
* @param string $return_value The value to return, possible values are 'id'
* for returning the ID and 'id=>parent' for returning the ID=>parent object,
* otherwise the post object is returned.
*
* @return int|object|WP_Post The post object in the desired format.
*/
function relevanssi_return_value( $post, string $return_value ) {
if ( 'id' === $return_value ) {
return $post->ID;
} elseif ( 'id=>type' === $return_value ) {
return relevanssi_return_id_type( $post );
} elseif ( 'id=>parent' === $return_value ) {
return relevanssi_return_id_parent( $post );
}
return $post;
}
/**
* Sanitizes hex color strings.
*
* A copy of sanitize_hex_color(), because that isn't always available.
*
* @param string $color A hex color string to sanitize.
*
* @return string Sanitized hex string, or an empty string.
*/
function relevanssi_sanitize_hex_color( string $color ) {
if ( '' === $color ) {
return '';
}
if ( '#' !== substr( $color, 0, 1 ) ) {
$color = '#' . $color;
}
// 3 or 6 hex digits, or the empty string.
if ( preg_match( '|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
return $color;
}
return '';
}
/**
* Returns 'selected' if the option matches a value.
*
* @param string $option Value to check.
* @param string $value The 'selected' value.
*
* @return string If the option matches the value, returns 'selected', otherwise
* returns an empty string.
*/
function relevanssi_select( string $option, string $value ) {
$selected = '';
if ( $option === $value ) {
$selected = 'selected';
}
return $selected;
}
/**
* Strips all tags from content, keeping non-tags that look like tags.
*
* Strips content that matches <[!a-zA-Z\/]*> to remove HTML tags and HTML
* comments, but not things like "<30 grams, 4>1".
*
* @param string $content The content.
*
* @return string The content with tags stripped.
*/
function relevanssi_strip_all_tags( $content ): string {
if ( ! is_string( $content ) ) {
$content = '';
}
$content = preg_replace( '/<!--.*?-->/ums', '', $content );
$content = preg_replace( '/<[!a-zA-Z\/][^>].*?>/ums', ' ', $content );
return $content ?? '';
}
/**
* Strips invisible elements from text.
*
* Strips <style>, <script>, <object>, <embed>, <applet>, <noscript>, <noembed>,
* <iframe> and <del> tags and their contents and comments from the text.
*
* @param string $text The source text.
*
* @return string The processed text.
*/
function relevanssi_strip_invisibles( $text ) {
if ( ! is_string( $text ) ) {
$text = strval( $text );
}
$text = preg_replace(
array(
'@<style[^>]*?>.*?</style>@siu',
'@<script[^>]*?.*?</script>@siu',
'@<object[^>]*?.*?</object>@siu',
'@<embed[^>]*?.*?</embed>@siu',
'@<applet[^>]*?.*?</applet>@siu',
'@<noscript[^>]*?.*?</noscript>@siu',
'@<noembed[^>]*?.*?</noembed>@siu',
'@<iframe[^>]*?.*?</iframe>@siu',
'@<del[^>]*?.*?</del>@siu',
'@<!--.*?-->@siu',
),
' ',
$text
);
return $text;
}
/**
* Strips tags from contents, keeping the allowed tags.
*
* The allowable tags are read from the relevanssi_excerpt_allowable_tags
* option. Relevanssi also adds extra spaces after some tags to make sure words
* are not stuck together after the tags are removed. The function also removes
* invisible content.
*
* @uses relevanssi_strip_invisibles() Used to remove scripts and other tags.
* @see strip_tags() Used to remove tags.
*
* @param string|null $content The content.
*
* @return string The content without tags.
*/
function relevanssi_strip_tags( $content ) {
if ( ! is_string( $content ) ) {
$content = strval( $content );
}
$content = relevanssi_strip_invisibles( $content );
$space_tags = array(
'/(<\/?p.*?>)/',
'/(<\/?br.*?>)/',
'/(<\/?h[1-6].*?>)/',
'/(<\/?div.*?>)/',
'/(<\/?blockquote.*?>)/',
'/(<\/?hr.*?>)/',
'/(<\/?li.*?>)/',
'/(<img.*?>)/',
'/(<\/td>)/',
);
$content = preg_replace( $space_tags, '$1 ', $content );
return strip_tags(
$content,
get_option( 'relevanssi_excerpt_allowable_tags', '' )
);
}
/**
* Returns the position of substring in the string.
*
* Uses mb_stripos() if possible, falls back to mb_strpos() and mb_strtoupper()
* if that cannot be found, and falls back to just strpos() if even that is not
* possible.
*
* @param string $haystack String where to look.
* @param string $needle The string to look for.
* @param int $offset Where to start, default 0.
*
* @return mixed False, if no result or $offset outside the length of $haystack,
* otherwise the position (which can be non-false 0!).
*/
function relevanssi_stripos( $haystack, $needle, int $offset = 0 ) {
if ( ! is_string( $haystack ) ) {
$haystack = strval( $haystack );
}
if ( ! is_string( $needle ) ) {
$needle = strval( $needle );
}
if ( $offset > relevanssi_strlen( $haystack ) ) {
return false;
}
if ( preg_match( '/[\?\*]/', $needle ) ) {
// There's a ? or an * in the string, which means it's a wildcard search
// query (a Premium feature) and requires some extra steps.
$needle_regex = str_replace(
array( '?', '*' ),
array( '.', '.*' ),
preg_quote( $needle, '/' )
);
$pos_found = false;
while ( ! $pos_found ) {
preg_match(
"/$needle_regex/i",
$haystack,
$matches,
PREG_OFFSET_CAPTURE,
$offset
);
/**
* This trickery is necessary, because PREG_OFFSET_CAPTURE gives
* wrong offsets for multibyte strings. The mb_strlen() gives the
* correct offset, the rest of this is because the $offset received
* as a parameter can be before the first $position, leading to an
* infinite loop.
*/
$pos = isset( $matches[0][1] )
? mb_strlen( substr( $haystack, 0, $matches[0][1] ) )
: false;
if ( $pos && $pos > $offset ) {
$pos_found = true;
} elseif ( $pos ) {
++$offset;
} else {
$pos_found = true;
}
}
} elseif ( function_exists( 'mb_stripos' ) ) {
if ( '' === $haystack ) {
$pos = false;
} else {
$pos = mb_stripos( $haystack, $needle, $offset );
}
} elseif ( function_exists( 'mb_strpos' ) && function_exists( 'mb_strtoupper' ) && function_exists( 'mb_substr' ) ) {
$pos = mb_strpos(
mb_strtoupper( $haystack ),
mb_strtoupper( $needle ),
$offset
);
} else {
$pos = strpos( strtoupper( $haystack ), strtoupper( $needle ), $offset );
}
return $pos;
}
/**
* Returns the length of the string.
*
* Uses mb_strlen() if available, otherwise falls back to strlen().
*
* @param string $s The string to measure.
*
* @return int The length of the string.
*/
function relevanssi_strlen( $s ) {
if ( ! is_string( $s ) ) {
$s = strval( $s );
}
if ( function_exists( 'mb_strlen' ) ) {
return mb_strlen( $s );
}
return strlen( $s );
}
/**
* Multibyte friendly strtolower.
*
* If multibyte string functions are available, returns mb_strtolower() and
* falls back to strtolower() if multibyte functions are not available.
*
* @param string $str The string to lowercase.
*
* @return string $str The string in lowercase.
*/
function relevanssi_strtolower( $str ) {
if ( ! is_string( $str ) ) {
$str = strval( $str );
}
if ( ! function_exists( 'mb_strtolower' ) ) {
return strtolower( $str );
} else {
return mb_strtolower( $str );
}
}
/**
* Multibyte friendly substr.
*
* If multibyte string functions are available, returns mb_substr() and falls
* back to substr() if multibyte functions are not available.
*
* @param string $str The source string.
* @param int $start If start is non-negative, the returned string will
* start at the start'th position in str, counting from zero. If start is
* negative, the returned string will start at the start'th character from the
* end of string.
* @param int|null $length Maximum number of characters to use from string. If
* omitted or null is passed, extract all characters to the end of the string.
*
* @return string $str The string in lowercase.
*/
function relevanssi_substr( $str, int $start, $length = null ) {
if ( ! is_string( $str ) ) {
$str = strval( $str );
}
if ( ! function_exists( 'mb_substr' ) ) {
return substr( $str, $start, $length );
} else {
return mb_substr( $str, $start, $length );
}
}
/**
* Prints out the post excerpt.
*
* Prints out the post excerpt from $post->post_excerpt, unless the post is
* protected. Only works in the Loop.
*
* @see post_password_required() Used to check for password requirements.
*
* @global $post The global post object.
*/
function relevanssi_the_excerpt() {
global $post;
if ( ! post_password_required( $post ) ) {
echo '<p>' . $post->post_excerpt . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
esc_html_e( 'There is no excerpt because this is a protected post.', 'relevanssi' );
}
}
/**
* Echoes out the permalink to the current post within Loop.
*
* Uses get_permalink() to get the permalink, then adds the 'highlight'
* parameter if necessary using relevanssi_add_highlight(), then echoes it out.
*
* @param int|WP_Post $post Post ID or post object. Default is the global $post.
*
* @uses relevanssi_get_permalink() Fetches the current post permalink.
*/
function relevanssi_the_permalink( $post = 0 ) {
echo esc_url( relevanssi_get_permalink( $post ) );
}
/**
* Prints out a list of tags for post.
*
* Replacement for the_tags() that does the same, but applies Relevanssi search term
* highlighting on the results.
*
* @param string $before What is printed before the tags, default ''.
* @param string $separator The separator between items, default ', '.
* @param string $after What is printed after the tags, default ''.
* @param boolean $echoed If true, echo, otherwise return the result. Default true.
* @param int $post_id The post ID. Default current post ID (in the Loop).
*/
function relevanssi_the_tags( string $before = '', string $separator = ', ', string $after = '', bool $echoed = true, int $post_id = 0 ) {
$tag_list = get_the_tag_list( $before, $separator, $after, $post_id );
$found = preg_match_all( '~<a href=".*?" rel="tag">(.*?)</a>~', $tag_list, $matches );
if ( $found ) {
$originals = $matches[0];
$tag_names = $matches[1];
$highlighted = array();
$count = count( $matches[0] );
for ( $i = 0; $i < $count; $i++ ) {
$highlighted_tag_name = relevanssi_highlight_terms( $tag_names[ $i ], get_search_query(), true );
$highlighted[ $i ] = str_replace( '>' . $tag_names[ $i ] . '<', '>' . $highlighted_tag_name . '<', $originals[ $i ] );
}
$tag_list = str_replace( $originals, $highlighted, $tag_list );
}
if ( $echoed ) {
echo $tag_list; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
return $tag_list;
}
}
/**
* Prints out post title with highlighting.
*
* Uses the global $post object. Reads the highlighted title from
* $post->post_highlighted_title. This used to accept one parameter, the
* `$echo` boolean, but in 2.12.3 / 4.10.3 the function signature was matched
* to copy `the_title()` function in WordPress core. The original behaviour is
* still supported: `relevanssi_the_title()` without arguments works exactly as
* before and `relevanssi_the_title( false )` returns the title.
*
* @global object $post The global post object.
*
* @param boolean|string $before Markup to prepend to the title. Can also be a
* boolean for whether to echo or return the title.
* @param string $after Markup to append to the title.
* @param boolean $echoed Whether to echo or return the title. Default
* true for echo.
*
* @return void|string Void if $echoed argument is true, current post title with
* highlights if $echoed is false.
*/
function relevanssi_the_title( $before = true, string $after = '', bool $echoed = true ) {
if ( true === $before ) {
$before = '';
$echoed = true;
} elseif ( false === $before ) {
$before = '';
$echoed = false;
}
global $post;
if ( empty( $post->post_highlighted_title ) ) {
$post->post_highlighted_title = $post->post_title;
}
if ( relevanssi_strlen( $post->post_highlighted_title ) === 0 ) {
return;
}
$title = $before . $post->post_highlighted_title . $after;
if ( $echoed ) {
echo $title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
return $title;
}
}
/**
* Turns off options, ie. sets them to "off".
*
* If the specified options don't exist in the request array, they are set to
* "off".
*
* @param array $request The _REQUEST array, passed as reference.
* @param array $options An array of option names.
*/
function relevanssi_turn_off_options( array &$request, array $options ) {
array_walk(
$options,
function ( $option ) use ( &$request ) {
if ( ! isset( $request[ $option ] ) ) {
$request[ $option ] = 'off';
}
}
);
}
/**
* Sets an option after doing floatval.
*
* @param array $request An array of option values.
* @param string $option The key to check.
* @param boolean $autoload Should the option autoload, default true.
* @param float $def_val The default value if floatval() fails, default 0.
* @param boolean $positive If true, replace negative values and zeroes with
* $def_val.
*/
function relevanssi_update_floatval( array $request, string $option, bool $autoload = true, float $def_val = 0, bool $positive = false ) {
if ( isset( $request[ $option ] ) ) {
$value = floatval( $request[ $option ] );
if ( ! $value ) {
$value = $def_val;
}
if ( $positive && $value <= 0 ) {
$value = $def_val;
}
update_option( $option, $value, $autoload );
}
}
/**
* Sets an option after doing intval.
*
* @param array $request An array of option values.
* @param string $option The key to check.
* @param boolean $autoload Should the option autoload, default true.
* @param int $def_val The default value if intval() fails, default 0.
*/
function relevanssi_update_intval( array $request, string $option, bool $autoload = true, int $def_val = 0 ) {
if ( isset( $request[ $option ] ) ) {
$value = intval( $request[ $option ] );
if ( ! $value ) {
$value = $def_val;
}
update_option( $option, $value, $autoload );
}
}
/**
* Sets an option with one of the listed legal values.
*
* @param array $request An array of option values.
* @param string $option The key to check.
* @param array $values The legal values.
* @param string $def_val The default value.
* @param boolean $autoload Should the option autoload, default true.
*/
function relevanssi_update_legal_value( array $request, string $option, array $values, string $def_val, bool $autoload = true ) {
if ( isset( $request[ $option ] ) ) {
$value = $def_val;
if ( in_array( $request[ $option ], $values, true ) ) {
$value = $request[ $option ];
}
update_option( $option, $value, $autoload );
}
}
/**
* Sets an on/off option according to the request value.
*
* @param array $request An array of option values.
* @param string $option The key to check.
* @param boolean $autoload Should the option autoload, default true.
*/
function relevanssi_update_off_or_on( array $request, string $option, bool $autoload = true ) {
relevanssi_update_legal_value(
$request,
$option,
array( 'off', 'on' ),
'off',
$autoload
);
}
/**
* Sets an option after sanitizing and unslashing the value.
*
* @param array $request An array of option values.
* @param string $option The key to check.
* @param boolean $autoload Should the option autoload, default true.
*/
function relevanssi_update_sanitized( array $request, string $option, bool $autoload = true ) {
if ( isset( $request[ $option ] ) ) {
$value = sanitize_text_field( wp_unslash( $request[ $option ] ) );
update_option( $option, $value, $autoload );
}
}
/**
* Returns true if $_SERVER['HTTP_USER_AGENT'] is on the bot block list.
*
* Looks for bot user agents in the $_SERVER['HTTP_USER_AGENT'] and returns true
* if a match is found.
*
* @return bool True if $_SERVER['HTTP_USER_AGENT'] is a bot.
*/
function relevanssi_user_agent_is_bot(): bool {
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
/**
* Filters the bots Relevanssi should block from search queries.
*
* Lets you filter the bots that are blocked from Relevanssi search
* queries.
*
* @param array $bots An array of bot user agents.
*/
$bots = apply_filters( 'relevanssi_bots_to_block', relevanssi_bot_block_list() );
foreach ( array_values( $bots ) as $lookfor ) {
if ( false !== stristr( $_SERVER['HTTP_USER_AGENT'], $lookfor ) ) {
return true;
}
}
}
return false;
}