indexing.php 53.8 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 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
<?php
/**
 * /lib/indexing.php
 *
 * @package Relevanssi
 * @author  Mikko Saari
 * @license https://wordpress.org/about/gpl/ GNU General Public License
 * @see     https://www.relevanssi.com/
 */

add_filter( 'relevanssi_index_get_post_type', 'relevanssi_index_get_post_type', 1, 2 );
add_filter( 'relevanssi_indexing_restriction', 'relevanssi_image_filter' );

/**
 * Blocks image attachments from the index.
 *
 * @param array $restriction An array with two values: 'mysql' for the MySQL
 * query and 'reason' for the blocking reason.
 *
 * @return array The image attachment blocking MySQL code, if the image
 * attachments are blocked.
 */
function relevanssi_image_filter( $restriction ) {
	if ( 'off' === get_option( 'relevanssi_index_image_files', 'off' ) ) {
		global $wpdb;

		$restriction['mysql']  .= "
		AND post.ID NOT IN (
		SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment'
		AND post_mime_type LIKE 'image%' )";
		$restriction['reason'] .= ' ' . __( 'Relevanssi image attachment filter', 'relevanssi' );
	}

	return $restriction;
}

/**
 * Returns the total number of posts to index.
 *
 * Counts the total number of posts to index, considering post type restrictions and
 * the valid statuses.
 *
 * @return int The number of posts to index.
 */
function relevanssi_count_total_posts() {
	$extend = false;
	return relevanssi_indexing_post_counter( $extend );
}

/**
 * Returns the number of posts missing from the index.
 *
 * Counts the total number of posts to index, considering post type restrictions and
 * the valid statuses, and only looks at posts missing from the index.
 *
 * @return int The number of posts to index.
 */
function relevanssi_count_missing_posts() {
	$extend = true;
	return relevanssi_indexing_post_counter( $extend );
}

/**
 * Counts the total number of posts.
 *
 * Counts the total number of posts to index, considering post type restrictions and
 * the valid statuses.
 *
 * @global object $wpdb The WordPress database interface.
 *
 * @param boolean $extend If true, count only missing posts. If false, count all
 * posts. Default false.
 *
 * @return int The number of posts to index.
 */
function relevanssi_indexing_post_counter( $extend = false ) {
	global $wpdb;
	$restriction  = relevanssi_post_type_restriction();
	$valid_status = relevanssi_valid_status_array();
	$limit        = '';

	$query = relevanssi_generate_indexing_query( $valid_status, $extend, $restriction, $limit );
	$query = str_replace( 'SELECT post.ID', 'SELECT COUNT(post.ID)', $query );

	/**
	 * Allows actions to happen before the indexing query is run.
	 *
	 * The indexing query fetches a list of posts to index (either all posts or only
	 * those missing from the index, depending on the case).
	 */
	do_action( 'relevanssi_pre_indexing_query' );
	$count = $wpdb->get_var( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

	if ( empty( $count ) ) {
		$count = 0;
	}

	return $count;
}

/**
 * Generates the indexing query.
 *
 * Generates the query that fetches the list of posts to index. The parameters are
 * assumed to be safely escaped. In regular use, the values are generated by
 * Relevanssi functions which provide reliable source data.
 *
 * @global object $wpdb                 The WordPress database interface.
 * @global array  $relevanssi_variables The Relevanssi global variables array, used
 * for table names.
 *
 * @param string  $valid_status Comma-separated list of valid post statuses.
 * @param boolean $extend       If true, only care about posts missing from the
 * index. If false, take all posts. Default false.
 * @param string  $restriction  Query restrictions, MySQL code that restricts the
 * posts fetched in the desired way. Default ''.
 * @param string  $limit        MySQL code to set the LIMIT and OFFSET values.
 * Default ''.
 *
 * @return string MySQL query to fetch the posts.
 */
function relevanssi_generate_indexing_query( $valid_status, $extend = false, $restriction = '', $limit = '' ) {
	global $wpdb, $relevanssi_variables;
	$relevanssi_table = $relevanssi_variables['relevanssi_table'];

	/**
	 * Filters the WHERE restriction for indexing queries.
	 *
	 * This filter hook can be used to exclude posts from indexing as early as is
	 * possible.
	 *
	 * @since 4.0.9 / 2.1.5
	 *
	 * @param string The WHERE restriction.
	 *
	 * @param array $restriction An array with two values: 'mysql' for the MySQL
	 * query restriction to modify, 'reason' for the reason of restriction.
	 */
	$restriction = apply_filters(
		'relevanssi_indexing_restriction',
		array(
			'mysql'  => $restriction,
			'reason' => '',
		)
	);

	/**
	 * Backwards compatibility for the change in filter parameters in Premium
	 * 2.8.0 in March 2020. Remove this eventually.
	 */
	if ( is_string( $restriction ) ) {
		$restriction_array['mysql']  = $restriction;
		$restriction_array['reason'] = 'relevanssi_indexing_restriction filter';
		$restriction                 = $restriction_array;
	}

	if ( ! $extend ) {
		$q = "SELECT post.ID
		FROM $wpdb->posts post
		LEFT JOIN $wpdb->posts parent ON (post.post_parent=parent.ID)
		WHERE
			(post.post_status IN ($valid_status)
			OR
			(post.post_status='inherit'
				AND(
					(parent.ID is not null AND (parent.post_status IN ($valid_status)))
					OR (post.post_parent=0)
				)
			))
		{$restriction['mysql']} ORDER BY post.ID DESC $limit";
	} else {
		$processed_post_filter = 'r.doc is null';
		if ( 'noindex' !== get_option( 'relevanssi_internal_links', 'noindex' ) ) {
			$processed_post_filter = "(r.doc is null OR r.doc NOT IN (SELECT DISTINCT(doc) FROM $relevanssi_table WHERE link = 0))";
		}

		$q = "SELECT post.ID
		FROM $wpdb->posts post
		LEFT JOIN $wpdb->posts parent ON (post.post_parent=parent.ID)
		LEFT JOIN $relevanssi_table r ON (post.ID=r.doc)
		WHERE
		$processed_post_filter
		AND
			(post.post_status IN ($valid_status)
			OR
			(post.post_status='inherit'
				AND(
					(parent.ID is not null AND (parent.post_status IN ($valid_status)))
					OR (post.post_parent=0)
				)
			)
		)
		{$restriction['mysql']} ORDER BY post.ID DESC $limit";
	}

	/**
	 * Filters the Relevanssi indexing query.
	 *
	 * @param string $q The indexing MySQL query.
	 */
	return apply_filters( 'relevanssi_indexing_query', $q );
}

/**
 * Generates a post type restriction.
 *
 * Generates a post type restriction for the MySQL query based on the
 * 'relevanssi_index_post_types' option.
 *
 * @return string MySQL code for the post type restriction.
 */
function relevanssi_post_type_restriction() {
	$post_types  = array();
	$restriction = '';

	$types = get_option( 'relevanssi_index_post_types', array() );
	if ( ! is_array( $types ) ) {
		$types = array();
	}
	foreach ( $types as $type ) {
		if ( 'bogus' === $type ) {
			// 'bogus' is not a real post type Relevanssi uses to make sure
			// the post type setting is saved, even if it's empty.
			continue;
		}
		if ( post_type_exists( $type ) ) {
			// Only accept post types that actually exist.
			array_push( $post_types, "'$type'" );
		}
	}

	if ( empty( $post_types ) ) {
		$post_types[] = "'no_post_types_chosen_so_index_no_posts'";
	}
	if ( count( $post_types ) > 0 ) {
		$restriction = ' AND post.post_type IN (' . implode( ', ', $post_types ) . ') ';
	}

	return $restriction;
}

/**
 * Generates a list of valid post statuses.
 *
 * Generates a list of valid post statuses to use in indexing. By default,
 * Relevanssi accepts 'publish', 'draft', 'private', 'pending', and 'future'. If
 * you need to use a custom post status, you can use the
 * 'relevanssi_valid_status' filter hook to add your own post status to the list
 * of valid statuses.
 *
 * @param boolean $return_array If true, return array; default false, return
 * string.
 *
 * @return string|array A comma-separated list of escaped valid post statuses
 * ready for MySQL, or an unfiltered array, depending on the $return_array
 * parameter.
 */
function relevanssi_valid_status_array( $return_array = false ) {
	/**
	 * Filters the valid status array.
	 *
	 * Allows you to modify the array that contains all valid post statuses for
	 * Relevanssi post indexing.
	 *
	 * @return array Array of post statuses.
	 */
	$valid_status_array = apply_filters( 'relevanssi_valid_status', array( 'publish', 'draft', 'private', 'pending', 'future' ) );

	if ( $return_array ) {
		return $valid_status_array;
	}

	$valid_status = array();

	if ( is_array( $valid_status_array ) && count( $valid_status_array ) > 0 ) {
		foreach ( $valid_status_array as $status ) {
			$valid_status[] = "'" . esc_sql( $status ) . "'";
		}
		$valid_status = implode( ',', $valid_status );
	} else {
		// If the filter makes the setting a non-array, fall back to reasonable
		// default values.
		$valid_status = "'publish', 'draft', 'private', 'pending', 'future'";
	}
	return $valid_status;
}

/**
 * Builds the index.
 *
 * @global object $wpdb                 The WordPress database interface.
 * @global array  $relevanssi_variables The Relevanssi global variables array,
 * used for table names.
 *
 * @param boolean|int $extend_offset If numeric, offsets the indexing by that
 * amount. If true, doesn't truncate the index before indexing. If false,
 * truncates index before indexing. Default false.
 * @param boolean     $verbose       Not used anymore, kept for backwards
 * compatibility.
 * @param int         $post_limit    How many posts to index. Default null, no
 * limit.
 * @param boolean     $is_ajax       If true, indexing is done in AJAX context.
 * Default false.
 *
 * @return array In AJAX context, returns array with two values:
 * 'indexing_complete' tells whether indexing is completed or not, and 'indexed'
 * returns the number of posts indexed. Outside AJAX context, these values are
 * returned as an array in format of array(completed, posts indexed).
 */
function relevanssi_build_index( $extend_offset = false, $verbose = null, $post_limit = null, $is_ajax = false ) {
	global $wpdb, $relevanssi_variables;
	$relevanssi_table = $relevanssi_variables['relevanssi_table'];

	// Thanks to Julien Mession. This speeds up indexing a lot.
	wp_suspend_cache_addition( true );

	if ( false === $extend_offset ) {
		// Truncate the index first.
		relevanssi_truncate_index();

		// Taxonomy term, user profile and post type archive indexing.
		if ( function_exists( 'relevanssi_premium_indexing' ) ) {
			relevanssi_premium_indexing();
		}

		update_option( 'relevanssi_index', '', false );
	}

	$indexing_query_args = relevanssi_indexing_query_args( $extend_offset, $post_limit );

	// The values generated by these functions are safe to use for MySQL.
	$restriction  = relevanssi_post_type_restriction();
	$valid_status = relevanssi_valid_status_array();

	$query = relevanssi_generate_indexing_query(
		$valid_status,
		$indexing_query_args['extend'],
		$restriction,
		$indexing_query_args['limit']
	);

	/* This action documented earlier in lib/indexing.php. */
	do_action( 'relevanssi_pre_indexing_query' );

	$content = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	if ( defined( 'WP_CLI' ) && WP_CLI ) {
		// @codeCoverageIgnoreStart
		$progress = WP_CLI\Utils\make_progress_bar( 'Indexing posts', count( $content ) );
		// @codeCoverageIgnoreEnd
	}

	$custom_fields = relevanssi_get_custom_fields();

	$n                  = 0;
	$remove_first       = false;
	$bypass_global_post = true; // $bypassglobalpost set to true, because at this
	// point global $post should be null, but in some cases it is not.
	foreach ( $content as $post ) {
		$result = relevanssi_index_doc( $post->ID, $remove_first, $custom_fields, $bypass_global_post );
		if ( is_numeric( $result ) && $result > 0 ) {
			// $n calculates the number of posts indexed.
			++$n;
		}
		if ( defined( 'WP_CLI' ) && WP_CLI && isset( $progress ) ) {
			// @codeCoverageIgnoreStart
			$progress->tick();
			// @codeCoverageIgnoreEnd
		}
	}
	if ( defined( 'WP_CLI' ) && WP_CLI && isset( $progress ) ) {
		// @codeCoverageIgnoreStart
		$progress->finish();
		// @codeCoverageIgnoreEnd
	}

	$complete = false;
	$size     = $indexing_query_args['size'];

	if ( ( 0 === $size ) || ( count( $content ) < $size ) ) {
		$complete = true;
		update_option( 'relevanssi_indexed', 'done' );

		// To prevent empty indices.
		$wpdb->query( "ANALYZE TABLE $relevanssi_table" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		// Update the document count variable.
		relevanssi_async_update_doc_count();
	}

	wp_suspend_cache_addition( false );

	if ( $is_ajax ) {
		$response = array(
			'indexing_complete' => $complete,
			'indexed'           => $n,
		);
		return $response;
	}

	return array( $complete, $n );
}

/**
 * Indexes one document.
 *
 * Different cases:
 *
 * Build index:
 * - global $post is null, $index_post is a post object.
 *
 * Update post:
 * - global $post has the original $post, $index_post is the ID of revision.
 *
 * Quick edit:
 * - global $post is an array, $index_post is the ID of current revision.
 *
 * @global object $wpdb                 The WordPress database interface.
 * @global array  $relevanssi_variables The Relevanssi global variables array, used
 * for table names.
 * @global object $post                 The global post object.
 *
 * @param object|int $index_post         The post to index, either post object or
 * post ID.
 * @param boolean    $remove_first       If true, remove the post from the index
 * before indexing. Default false.
 * @param array      $custom_fields      The custom fields that are indexed for the
 * post. Default an empty string.
 * @param boolean    $bypass_global_post If true, do not use the global $post object.
 * Default false.
 * @param boolean    $debug              If true, echo out debugging information.
 * Default false.
 *
 * @return string|int Number of insert queries run, or -1 if the indexing fails,
 * or 'hide' in case the post is hidden or 'donotindex' if a filter blocks this.
 */
function relevanssi_index_doc( $index_post, $remove_first = false, $custom_fields = '', $bypass_global_post = false, $debug = false ) {
	global $wpdb, $post, $relevanssi_variables;
	$relevanssi_table = $relevanssi_variables['relevanssi_table'];
	$post_was_null    = true;
	$previous_post    = null;

	if ( relevanssi_log_debug() ) {
		$debug = true;
	}

	if ( $debug ) {
		relevanssi_debug_echo( 'Indexing post ID ' . $index_post . '...' );
	}

	// Check if this is a Jetpack Contact Form entry.
	if ( isset( $_REQUEST['contact-form-id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
		return -1;
	}

	if ( isset( $post ) ) {
		$post_was_null = false;
		$previous_post = $post;
	}

	if ( empty( $post ) || $bypass_global_post ) {
		$post = is_object( $index_post ) ? $index_post : get_post( $index_post ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
	}

	// Finally fetch the post again by ID. Complicated, yes, but unless we do this,
	// we might end up indexing the post before the updates come in.
	$post = isset( $post->ID ) ? get_post( $post->ID ) : null; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

	if ( null === $post ) {
		// At this point we should have something in $post; if not, quit.
		if ( $previous_post || $post_was_null ) {
			$post = $previous_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
		}
		return -1;
	}

	// Post exclusion feature from Relevanssi Premium.
	if ( function_exists( 'relevanssi_hide_post' ) ) {
		if ( relevanssi_hide_post( $post->ID ) ) {
			if ( $debug ) {
				relevanssi_debug_echo( 'relevanssi_hide_post() returned true.' );
			}
			if ( $previous_post || $post_was_null ) {
				$post = $previous_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
			}
			update_post_meta(
				$post->ID,
				'_relevanssi_noindex_reason',
				__( 'Relevanssi index exclude', 'relevanssi' )
			);
			return 'hide';
		}
	}

	$index_this_post        = false;
	$post->indexing_content = true;

	$index_types = get_option( 'relevanssi_index_post_types', array() );
	if ( is_array( $index_types ) && in_array( $post->post_type, $index_types, true ) ) {
		$index_this_post = true;
	}

	/**
	 * Filters whether a post is indexed or not.
	 *
	 * Allows you to filter whether a post is indexed or not.
	 *
	 * @param boolean|string If not false, the post is not indexed. The value
	 * can be a boolean, or a string containing an explanation for the
	 * exclusion. Default false.
	 * @param int            The post ID.
	 * @param WP_Post        The post object.
	 */
	$do_not_index = apply_filters( 'relevanssi_do_not_index', false, $post->ID, $post );
	if ( $do_not_index ) {
		// Filter says no.
		if ( true === $do_not_index ) {
			$do_not_index = __( 'Blocked by a filter function', 'relevanssi' );
		}
		if ( $debug ) {
			relevanssi_debug_echo( 'relevanssi_do_not_index says exclude, because: ' . $do_not_index );
		}
		update_post_meta( $post->ID, '_relevanssi_noindex_reason', $do_not_index );
		$index_this_post = false;
	}

	if ( $remove_first ) {
		// We are updating a post, so remove the old stuff first.
		relevanssi_remove_doc( $post->ID, true );
		if ( function_exists( 'relevanssi_remove_item' ) ) {
			relevanssi_remove_item( $post->ID, 'post' );
		}
		if ( $debug ) {
			relevanssi_debug_echo( 'Removed the post from the index.' );
		}
	}

	// This needs to be here, after the call to relevanssi_remove_doc(), because
	// otherwise a post that's in the index but shouldn't be there won't get removed.
	if ( ! $index_this_post ) {
		if ( $previous_post || $post_was_null ) {
			$post = $previous_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
		}
		return 'donotindex';
	}

	$n = 0;

	/**
	 * Allows filtering the indexed post before indexing.
	 *
	 * @param object $post The post object.
	 * @param object $post The post object again (in other uses for this filter, the
	 * second parameter actually makes sense).
	 */
	$post = apply_filters( 'relevanssi_post_to_index', $post, $post ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

	$min_word_length = get_option( 'relevanssi_min_word_length', 3 );
	$insert_data     = array();

	if ( 'none' !== get_option( 'relevanssi_index_comments' ) ) {
		$n += relevanssi_index_comments( $insert_data, $post->ID, $min_word_length, $debug );
	}

	$taxonomies = get_option( 'relevanssi_index_taxonomies_list', array() );
	foreach ( $taxonomies as $taxonomy ) {
		$n += relevanssi_index_taxonomy_terms( $insert_data, $post->ID, $taxonomy, $debug );
	}

	if ( 'on' === get_option( 'relevanssi_index_author' ) ) {
		$n += relevanssi_index_author( $insert_data, $post->post_author, $min_word_length, $debug, $post );
	}

	$n += relevanssi_index_custom_fields( $insert_data, $post->ID, $custom_fields, $min_word_length, $debug );

	if (
		isset( $post->post_excerpt )
		&& ( 'on' === get_option( 'relevanssi_index_excerpt' ) || 'attachment' === $post->post_type )
		) {
		// Attachment caption is stored in the excerpt.
		$n += relevanssi_index_excerpt( $insert_data, $post->post_excerpt, $min_word_length, $debug );
	}

	// Premium can index arbitrary MySQL columns.
	if ( function_exists( 'relevanssi_index_mysql_columns' ) ) {
		if ( $debug ) {
			relevanssi_debug_echo( 'Indexing MySQL columns.' );
		}
		$insert_data = relevanssi_index_mysql_columns( $insert_data, $post->ID );
	}

	// Premium can index PDF content for the parent post.
	if ( function_exists( 'relevanssi_index_pdf_for_parent' ) ) {
		if ( $debug ) {
			relevanssi_debug_echo( 'Indexing PDF content for parent post.' );
		}
		$insert_data = relevanssi_index_pdf_for_parent( $insert_data, $post->ID );
	}

	$n += relevanssi_index_title( $insert_data, $post, $min_word_length, $debug );
	$n += relevanssi_index_content( $insert_data, $post, $min_word_length, $debug );

	$values = relevanssi_convert_data_to_values( $insert_data, $post );

	if ( ! empty( $values ) ) {
		$values = implode( ', ', $values );
		$query  = "INSERT IGNORE INTO $relevanssi_table (doc, term, term_reverse, content, title, comment, tag, link, author, category, excerpt, taxonomy, customfield, type, taxonomy_detail, customfield_detail, mysqlcolumn, mysqlcolumn_detail) VALUES $values";
		if ( $debug ) {
			relevanssi_debug_echo( "Final indexing query:\n\t$query" );
		}
		$wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	}

	delete_post_meta( $post->ID, '_relevanssi_noindex_reason' );

	if ( $previous_post || $post_was_null ) {
		$post = $previous_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
	}

	return $n;
}

/**
 * Index taxonomy terms for given post and given taxonomy.
 *
 * @since 1.8
 *
 * @param array   $insert_data Insert query data array, modified here.
 * @param int     $post_id     The indexed post ID.
 * @param string  $taxonomy    Taxonomy name.
 * @param boolean $debug       If true, print out debugging notices.
 *
 * @return int The number of new tokens added.
 */
function relevanssi_index_taxonomy_terms( &$insert_data, $post_id, $taxonomy, $debug ) {
	if ( $debug ) {
		relevanssi_debug_echo( "Indexing taxonomy terms for $taxonomy" );
	}

	$n = 0;

	$min_word_length     = get_option( 'relevanssi_min_word_length', 3 );
	$post_taxonomy_terms = get_the_terms( $post_id, $taxonomy );

	if ( false === $post_taxonomy_terms || is_wp_error( $post_taxonomy_terms ) ) {
		return $n;
	}

	$term_string = '';
	foreach ( $post_taxonomy_terms as $post_term ) {
		if ( is_object( $post_term ) ) {
			$term_string .= $post_term->name . ' ';
		}
	}
	if ( $debug ) {
		relevanssi_debug_echo( "Taxonomy term content for $taxonomy: $term_string" );
	}
	/**
	 * Filters the taxonomy term content before indexing.
	 *
	 * The taxonomy term content is presented as a string of term names
	 * separated by spaces. If you want to edit this, it's probably best to just
	 * reconstruct it from the taxonomy term objects contained in the second
	 * parameter.
	 *
	 * @param string The taxonomy term content as a string.
	 * @param array  An array containing the taxonomy term objects for this
	 * taxonomy.
	 * @param string The taxonomy name.
	 * @param int    The post ID for the current post.
	 */
	$term_string = apply_filters(
		'relevanssi_tag_before_tokenize',
		trim( $term_string ),
		$post_taxonomy_terms,
		$taxonomy,
		$post_id
	);

	/** This filter is documented in lib/indexing.php */
	$term_tokens = apply_filters(
		'relevanssi_indexing_tokens',
		relevanssi_tokenize( $term_string, true, $min_word_length, 'indexing' ),
		'taxonomy-' . $taxonomy
	);

	if ( count( $term_tokens ) > 0 ) {
		foreach ( $term_tokens as $token => $count ) {
			++$n;

			switch ( $taxonomy ) {
				case 'post_tag':
					$type = 'tag';
					break;
				case 'category':
					$type = 'category';
					break;
				default:
					$type = 'taxonomy';
			}

			$insert_data[ $token ][ $type ] = isset( $insert_data[ $token ][ $type ] )
				? $insert_data[ $token ][ $type ] + $count : $count;

			$tax_detail = array();
			if ( isset( $insert_data[ $token ]['taxonomy_detail'] ) ) {
				$tax_detail = json_decode( $insert_data[ $token ]['taxonomy_detail'], true );
			}

			$tax_detail[ $taxonomy ] = isset( $tax_detail[ $taxonomy ] )
				? $tax_detail[ $taxonomy ] + $count : $count;

			$insert_data[ $token ]['taxonomy_detail'] = wp_json_encode( $tax_detail );
		}
	}

	return $n;
}

/**
 * Updates child posts when a parent post changes status.
 *
 * Called from 'transition_post_status' action hook when a post is edited,
 * published, or deleted. Will do the appropriate indexing action on the child
 * posts and attachments.
 *
 * @author renaissancehack
 *
 * @param string $new_status The new status.
 * @param string $old_status The old status.
 * @param object $post       The post object.
 *
 * @return null|array Null in problem cases, an array of 'removed' and
 * 'indexed' values that show how many posts were indexed and removed.
 */
function relevanssi_update_child_posts( $new_status, $old_status, $post ) {
	// Safety check, for WordPress Editorial Calendar incompatibility.
	if ( ! isset( $post ) || ! isset( $post->ID ) ) {
		return;
	}

	/** Documented in lib/indexing.php. */
	$index_statuses = apply_filters(
		'relevanssi_valid_status',
		array( 'publish', 'private', 'draft', 'pending', 'future' )
	);
	/**
	 * Filters the attachment and revision post types.
	 *
	 * When Relevanssi indexes posts, it also looks at the child posts. However,
	 * if the indexed post is a revision or an attachment, the child posts are
	 * not checked. You may extend this behaviour to other post types with this
	 * hook.
	 *
	 * @param array Array of post types, default 'attachment' and 'revision'.
	 */
	$attachment_revision_types = apply_filters(
		'relevanssi_index_attachment_revision_types',
		array( 'attachment', 'revision' )
	);

	$did_nothing = array(
		'removed' => 0,
		'indexed' => 0,
	);

	/**
	 * Either:
	 *
	 * 1. New status equals old status.
	 * 2. Both new and old status are in the list of stati to index.
	 * 3. The post is an attachment or a revision.
	 *
	 * In any of these cases, do nothing.
	 */
	if ( $new_status === $old_status ) {
		return $did_nothing;
	}
	if ( in_array( $new_status, $index_statuses, true ) && in_array( $old_status, $index_statuses, true ) ) {
		return $did_nothing;
	}
	if ( in_array( $post->post_type, $attachment_revision_types, true ) ) {
		return $did_nothing;
	}

	$post_types  = get_option( 'relevanssi_index_post_types', array() );
	$args        = array(
		'post_parent' => $post->ID,
		'post_type'   => $post_types,
	);
	$removed     = 0;
	$indexed     = 0;
	$child_posts = get_children( $args );

	if ( ! empty( $child_posts ) ) {
		if ( ! in_array( $new_status, $index_statuses, true ) ) {
			foreach ( $child_posts as $post ) {
				relevanssi_remove_doc( $post->ID );
				++$removed;
			}
		} else {
			foreach ( $child_posts as $post ) {
				relevanssi_publish( $post->ID );
				++$indexed;
			}
		}
	}
	return array(
		'removed' => $removed,
		'indexed' => $indexed,
	);
}

/**
 * Indexes a published post.
 *
 * @param int     $post_id            The post ID.
 * @param boolean $bypass_global_post If true, bypass the global $post object.
 * Default false.
 *
 * @return string|int Returns 'auto-draft' if the post is an auto draft and
 * thus skipped, or the relevanssi_index_doc() return value.
 *
 * @see relevanssi_index_doc()
 */
function relevanssi_publish( $post_id, $bypass_global_post = false ) {
	$post_status = get_post_status( $post_id );
	if ( 'auto-draft' === $post_status ) {
		return 'auto-draft';
	}

	$custom_fields = relevanssi_get_custom_fields();
	return relevanssi_index_doc( $post_id, true, $custom_fields, $bypass_global_post );
}

/**
 * Indexes a post after publishing or modification.
 *
 * Hooks on to 'wp_insert_post' action hook and triggers when wp_insert_post() is
 * used to add a post into the database. Doesn't use the global $post object, because
 * that doesn't have the correct post.
 *
 * @author Lumpysimon.
 *
 * @global object $wpdb The WP database interface.
 *
 * @param int $post_id The post ID.
 *
 * @return string|int Returns 'auto-draft' if the post is an auto draft and
 * thus skipped, 'revision' for revisions, 'nav_menu_item' for navigation menu
 * items, 'removed' if the post is removed or the relevanssi_index_doc() return
 * value from relevanssi_publish().
 *
 * @see relevanssi_publish()
 */
function relevanssi_insert_edit( $post_id ) {
	global $wpdb;
	if ( 'revision' === relevanssi_get_post_type( $post_id ) ) {
		return 'revision';
	}

	if ( 'nav_menu_item' === relevanssi_get_post_type( $post_id ) ) {
		return 'nav_menu_item';
	}

	$post_status = get_post_status( $post_id );
	if ( 'auto-draft' === $post_status ) {
		return 'auto-draft';
	}

	if ( 'inherit' === $post_status ) {
		// Get the post status from the parent post.
		$parent_id   = wp_get_post_parent_id( $post_id );
		$post_status = get_post_status( $parent_id );
	}

	$index_this_post = true;

	/* Documented in lib/indexing.php. */
	$restriction = apply_filters(
		'relevanssi_indexing_restriction',
		array(
			'mysql'  => '',
			'reason' => '',
		)
	);
	if ( ! empty( $restriction['mysql'] ) ) {
		// Check the indexing restriction filter: if the post passes the filter, this
		// should return the post ID.
		$is_unrestricted = $wpdb->get_var(
			"SELECT ID FROM $wpdb->posts AS post WHERE ID = $post_id {$restriction['mysql']}" // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		);
		if ( ! $is_unrestricted ) {
			$index_this_post = false;
		}
	}

	$return_array   = true;
	$index_statuses = relevanssi_valid_status_array( $return_array );
	if ( ! in_array( $post_status, $index_statuses, true ) ) {
		$index_this_post = false;
	}

	if ( $index_this_post ) {
		$bypass_global_post = true;
		$return_value       = relevanssi_publish( $post_id, $bypass_global_post );
	} else {
		// The post isn't supposed to be indexed anymore, remove it from index.
		relevanssi_remove_doc( $post_id );
		update_post_meta(
			$post_id,
			'_relevanssi_noindex_reason',
			trim( $restriction['reason'] )
		);
		$return_value = 'removed';
	}

	return $return_value;
}

/**
 * Updates comment indexing when comments are added, edited or deleted.
 *
 * @author OdditY
 *
 * @param int $comment_id Commend ID.
 *
 * @see relevanssi_comment_remove
 * @see relevanssi_comment_edit
 * @see relevanssi_publish
 *
 * @return int|string The relevanssi_publish return value, "nocommentfound" if
 * the comment doesn't exist or "donotindex" if it cannot be indexed.
 * comment indexing is disabled.
 */
function relevanssi_index_comment( $comment_id ) {
	$comment_indexing_type = get_option( 'relevanssi_index_comments' );
	$no_pingbacks          = false;

	if ( 'normal' === $comment_indexing_type ) {
		$no_pingbacks = true;
	}
	if ( 'normal' !== $comment_indexing_type && 'all' !== $comment_indexing_type ) {
		return 'donotindex';
	}

	$comment = get_comment( $comment_id );
	if ( ! $comment ) {
		return 'nocommentfound';
	}
	if ( $no_pingbacks && 'pingback' === $comment->comment_type ) {
		return 'donotindex';
	}
	if ( 1 !== intval( $comment->comment_approved ) ) {
		// Comment isn't approved, do not index.
		return 'donotindex';
	}
	return relevanssi_publish( $comment->comment_post_ID );
}

/**
 * Returns the comment text for a post.
 *
 * @param int $post_id The post ID.
 *
 * @return string All the comment content as a string that has the comment author
 * and the comment text.
 */
function relevanssi_get_comments( $post_id ) {
	/**
	 * If this filter returns true, the comments for the post are not indexed.
	 *
	 * @param boolean Return true to block the comment indexing. Default false.
	 * @param int     $post_id The post ID.
	 */
	if ( apply_filters( 'relevanssi_index_comments_exclude', false, $post_id ) ) {
		return '';
	}

	$comment_indexing = get_option( 'relevanssi_index_comments' );
	$comment_types    = array( 'comment' );
	$comment_string   = '';

	if ( 'all' === $comment_indexing ) {
		$comment_types[] = 'pings';
	}
	if ( 'none' === $comment_indexing ) {
		return '';
	}

	$offset = 0;
	$limit  = 20;

	while ( true ) {
		// Posts may have lots of comments. Do 20 at the time to avoid memory issues.
		$args     = array(
			'offset' => $offset,
			'number' => $limit,
			'type'   => $comment_types,
		);
		$comments = get_approved_comments(
			$post_id,
			/**
			 * Filters the arguments for get_approved_comments().
			 *
			 * Useful for indexing custom comment types, for example.
			 *
			 * @param array An array of arguments. Don't adjust 'offset' or
			 * 'number' to avoid problems.
			 */
			apply_filters(
				'relevanssi_get_approved_comments_args',
				$args
			)
		);
		if ( count( $comments ) === 0 ) {
			break;
		}
		foreach ( $comments as $comment ) {
			/**
			 * Filters the comment author before indexing.
			 *
			 * @param string Comment author display name.
			 * @param int    The comment ID.
			 */
			$comment_string .= ' ' . apply_filters( 'relevanssi_comment_author_to_index', $comment->comment_author, $comment->comment_ID );
			/**
			 * Filters the comment content before indexing.
			 *
			 * @param string Comment content.
			 * @param int    The comment ID.
			 */
			$comment_string .= ' ' . apply_filters( 'relevanssi_comment_content_to_index', $comment->comment_content, $comment->comment_ID );
		}
		$offset += $limit;
	}

	return $comment_string;
}

/**
 * Truncates the Relevanssi index.
 *
 * @global object $wpdb                 The WordPress database interface.
 * @global array  $relevanssi_variables The Relevanssi global variables array, used
 * for table names.
 *
 * @return boolean True on success, false on failure.
 */
function relevanssi_truncate_index() {
	global $wpdb, $relevanssi_variables;
	$relevanssi_table = $relevanssi_variables['relevanssi_table'];
	return $wpdb->query( "TRUNCATE TABLE $relevanssi_table" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}

/**
 * Remove post from the Relevanssi index.
 *
 * @global object $wpdb                 The WordPress database interface.
 * @global array  $relevanssi_variables The Relevanssi global variables array, used
 * for table names.
 *
 * @param int     $post_id             The post ID.
 * @param boolean $keep_internal_links If true, keep internal link indexing (a
 * Premium feature). Default false.
 */
function relevanssi_remove_doc( $post_id, $keep_internal_links = false ) {
	if ( function_exists( 'relevanssi_premium_remove_doc' ) ) {
		// Premium has a different method, because the index can include taxonomy
		// terms and user profiles.
		relevanssi_premium_remove_doc( $post_id, $keep_internal_links );
	} else {
		global $wpdb, $relevanssi_variables;

		$post_id = intval( $post_id );
		if ( empty( $post_id ) ) {
			// No post ID specified.
			return;
		}

		$wpdb->query(
			$wpdb->prepare(
				'DELETE FROM ' . $relevanssi_variables['relevanssi_table'] . ' WHERE doc=%d', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
				$post_id
			)
		);
	}
}

/**
 * Filter that allows you to set the index type based on the post type.
 *
 * @param string $type The index 'type' column value, default 'post'.
 * @param object $post The post object containing the post being indexed.
 *
 * @return string The index 'type' column value, default 'post'.
 */
function relevanssi_index_get_post_type( $type, $post ) {
	if ( 'attachment' === $post->post_type ) {
		$type = 'attachment';
	}
	return $type;
}

/**
 * Sets the indexing MySQL LIMIT parameter and other parameters.
 *
 * @param boolean|int $extend_offset If numeric, offsets the indexing by that
 * amount. If true, doesn't truncate the index before indexing. If false,
 * truncates index before indexing. Default false.
 * @param int         $post_limit    How many posts to index. Default null, no
 * limit.
 *
 * @return array Array with the LIMIT clause in 'limit', the extend boolean in
 * 'extend' and the size integer in 'size'.
 */
function relevanssi_indexing_query_args( $extend_offset, $post_limit ) {
	$size   = 0;
	$limit  = '';
	$extend = false;

	// If $post_limit parameter is present, numeric and > 0, use that.
	if ( isset( $post_limit ) && is_numeric( $post_limit ) && $post_limit > 0 ) {
		$size  = $post_limit;
		$limit = " LIMIT $post_limit";
	}

	if ( false === $extend_offset ) {
		$extend = false;
	} elseif ( ! is_numeric( $extend_offset ) ) {
		// Extending, so do not truncate and skip the posts already in the index.
		if ( ! $limit ) {
			// CLI request with no limit specified.
			$size  = 200;
			$limit = ' LIMIT 200';
		}

		$extend = true;
	} else { // $extend_offset is numeric.
		if ( ! $limit ) {
			// CLI request with no limit specified.
			$size  = 200;
			$limit = ' LIMIT 200';
		}

		if ( ! empty( $limit ) ) {
			$limit .= " OFFSET $extend_offset";
		}

		// Extend is set to false, because $limit now has LIMIT and OFFSET.
		$extend = false;
	}

	return array(
		'limit'  => $limit,
		'extend' => $extend,
		'size'   => $size,
	);
}

/**
 * Creates indexing queries for the comment content.
 *
 * @param array   $insert_data     The INSERT query data. Modified here.
 * @param int     $post_id         The indexed post ID.
 * @param int     $min_word_length The minimum word length.
 * @param boolean $debug           If true, print out debug notices.
 *
 * @return int The number of tokens added to the data.
 */
function relevanssi_index_comments( &$insert_data, $post_id, $min_word_length, $debug ) {
	$n = 0;
	if ( $debug ) {
		relevanssi_debug_echo( 'Indexing comments.' );
	}
	$post_comments = relevanssi_get_comments( $post_id );
	if ( ! empty( $post_comments ) ) {
		$post_comments = relevanssi_strip_invisibles( $post_comments );
		$post_comments = relevanssi_strip_all_tags( $post_comments );

		if ( $debug ) {
			relevanssi_debug_echo( "Comment content: $post_comments" );
		}
		/**
		 * Filters the indexing tokens before they are added to the $insert_data.
		 *
		 * @param array  An array of token-frequency pairs.
		 * @param string The context of the tokens (eg. 'content', 'title').
		 *
		 * @return array The filtered tokens.
		 */
		$post_comments_tokens = apply_filters(
			'relevanssi_indexing_tokens',
			relevanssi_tokenize( $post_comments, true, $min_word_length, 'indexing' ),
			'comments'
		);
		if ( count( $post_comments_tokens ) > 0 ) {
			foreach ( $post_comments_tokens as $token => $count ) {
				++$n;
				$insert_data[ $token ]['comment'] = $count;
			}
		}
	}
	return $n;
}

/**
 * Creates indexing queries for the post author.
 *
 * @param array   $insert_data     The INSERT query data. Modified here.
 * @param int     $post_author     The post author id.
 * @param int     $min_word_length The minimum word length.
 * @param boolean $debug           If true, print out debug notices.
 * @param WP_Post $post            The post object.
 *
 * @return int The number of tokens added to the data.
 */
function relevanssi_index_author( &$insert_data, $post_author, $min_word_length, $debug, $post ) {
	$n            = 0;
	$display_name = get_the_author_meta( 'display_name', $post_author );

	/**
	 * Filters the post author display_name before indexing it.
	 *
	 * @param string  $post_author The author display_name.
	 * @param WP_Post $post        The post object.
	 */
	$display_name = apply_filters( 'relevanssi_post_author', $display_name, $post );

	/** This filter is documented in lib/indexing.php */
	$name_tokens = apply_filters(
		'relevanssi_indexing_tokens',
		relevanssi_tokenize( $display_name, false, $min_word_length, 'indexing' ),
		'author'
	);
	if ( $debug ) {
		relevanssi_debug_echo( 'Indexing post author as: ' . implode( ' ', array_keys( $name_tokens ) ) );
	}
	foreach ( $name_tokens as $token => $count ) {
		++$n;
		if ( ! isset( $insert_data[ $token ]['author'] ) ) {
			$insert_data[ $token ]['author'] = 0;
		}
		$insert_data[ $token ]['author'] += $count;
	}

	return $n;
}

/**
 * Creates indexing query data for custom fields.
 *
 * @param array        $insert_data     The INSERT query data. Modified here.
 * @param int          $post_id         The indexed post ID.
 * @param string|array $custom_fields   The custom fields to index. Only allowed
 * string values are "all" and "visible". If you wish to specify a single custom
 * field, wrap it in an array.
 * @param int          $min_word_length The minimum word length.
 * @param boolean      $debug           If true, print out debug notices.
 *
 * @return int The number of tokens added to the data.
 */
function relevanssi_index_custom_fields( &$insert_data, $post_id, $custom_fields, $min_word_length, $debug ) {
	$n = 0;

	$custom_fields = relevanssi_generate_list_of_custom_fields( $post_id, $custom_fields );
	if ( empty( $custom_fields ) ) {
		return $n;
	}

	if ( $debug ) {
		relevanssi_debug_echo( 'Custom fields to index: ' . implode( ', ', $custom_fields ) );
	}

	foreach ( $custom_fields as $field ) {
		/**
		 * Filters the custom field value before indexing.
		 *
		 * @param array            Custom field values.
		 * @param string $field    The custom field name.
		 * @param int    $post_id The post ID.
		 */
		$values = apply_filters( 'relevanssi_custom_field_value', get_post_meta( $post_id, $field, false ), $field, $post_id );
		if ( empty( $values ) || ! is_array( $values ) ) {
			continue;
		}

		foreach ( $values as $value ) {
			// Quick hack : allow indexing of PODS relationship custom fields // TMV.
			if ( is_array( $value ) && isset( $value['post_title'] ) ) {
				$value = $value['post_title'];
			}

			if ( function_exists( 'relevanssi_index_acf' ) ) {
				// @codeCoverageIgnoreStart
				// Handle ACF fields. Only defined when ACF is active.
				$acf_tokens = relevanssi_index_acf( $insert_data, $post_id, $field, $value );
				if ( $acf_tokens ) {
					$n += $acf_tokens;
					continue;
				}
				// @codeCoverageIgnoreEnd
			}

			// Flatten other arrays.
			if ( is_array( $value ) ) {
				$value = relevanssi_flatten_array( $value );
			}

			if ( $debug ) {
				relevanssi_debug_echo( "\tKey: " . $field . ' - value: ' . $value );
			}

			if ( ! $value ) {
				continue;
			}

			$context      = 'custom_field';
			$remove_stops = true;
			if ( '_relevanssi_pdf_content' === $field ) {
				$context      = 'content';
				$remove_stops = 'body';
			}

			/** This filter is documented in lib/indexing.php */
			$value_tokens = apply_filters(
				'relevanssi_indexing_tokens',
				relevanssi_tokenize( $value, $remove_stops, $min_word_length, 'indexing' ),
				$context
			);

			foreach ( $value_tokens as $token => $count ) {
				++$n;
				if ( ! isset( $insert_data[ $token ]['customfield'] ) ) {
					$insert_data[ $token ]['customfield'] = 0;
				}
				$insert_data[ $token ]['customfield'] += $count;

				// Premium indexes more detail about custom fields.
				if ( function_exists( 'relevanssi_customfield_detail' ) ) {
					$insert_data = relevanssi_customfield_detail(
						$insert_data,
						$token,
						$count,
						$field
					);
				}
			}
		}
	}

	return $n;
}

/**
 * Creates indexing queries for the excerpt content.
 *
 * @param array   $insert_data     The INSERT query data. Modified here.
 * @param string  $excerpt         The post excerpt to index.
 * @param int     $min_word_length The minimum word length.
 * @param boolean $debug           If true, print out debug notices.
 *
 * @return int The number of tokens added to the data.
 */
function relevanssi_index_excerpt( &$insert_data, $excerpt, $min_word_length, $debug ) {
	$n = 0;

	// Include excerpt for attachments which use post_excerpt for captions - modified by renaissancehack.
	if ( $debug ) {
		relevanssi_debug_echo( "Indexing post excerpt: $excerpt" );
	}
	/** This filter is documented in common/indexing.php */
	$excerpt_tokens = apply_filters(
		'relevanssi_indexing_tokens',
		relevanssi_tokenize( $excerpt, true, $min_word_length, 'indexing' ),
		'excerpt'
	);
	foreach ( $excerpt_tokens as $token => $count ) {
		++$n;
		if ( ! isset( $insert_data[ $token ]['excerpt'] ) ) {
			$insert_data[ $token ]['excerpt'] = 0;
		}
		$insert_data[ $token ]['excerpt'] += $count;
	}
	return $n;
}

/**
 * Creates indexing queries for post title.
 *
 * @param array   $insert_data     The INSERT query data. Modified here.
 * @param object  $post_object     The post object.
 * @param int     $min_word_length The minimum word length.
 * @param boolean $debug           If true, print out debug notices.
 *
 * @return int The number of tokens added to the data.
 */
function relevanssi_index_title( &$insert_data, $post_object, $min_word_length, $debug ) {
	$n = 0;

	if ( empty( $post_object->post_title ) ) {
		return 0;
	}

	/**
	 * If this filter returns false, titles are not indexed at all.
	 *
	 * @param boolean Return false to prevent titles from being indexed. Default
	 * true.
	 * @param object  $post_object The post object.
	 */
	if ( ! apply_filters( 'relevanssi_index_titles', true, $post_object ) ) {
		return 0;
	}

	if ( $debug ) {
		relevanssi_debug_echo( 'Indexing post title.' );
	}
	/** This filter is documented in wp-includes/post-template.php */
	$filtered_title = apply_filters(
		'the_title',
		$post_object->post_title,
		$post_object->ID
	);
	/**
	 * Filters the title before tokenizing and indexing.
	 *
	 * @param string $post_object->post_title The title.
	 * @param object $post_object             The full post object.
	 */
	$filtered_title = apply_filters(
		'relevanssi_post_title_before_tokenize',
		$filtered_title,
		$post_object
	);
	$title_tokens   = relevanssi_tokenize(
		$filtered_title,
		/**
		 * Filters whether stopwords should be removed from titles in tokenizing or not.
		 *
		 * @param boolean If true, remove stopwords. Default true.
		 */
		apply_filters( 'relevanssi_remove_stopwords_in_titles', true ),
		$min_word_length,
		'indexing'
	);
	/** This filter is documented in lib/indexing.php */
	$title_tokens = apply_filters( 'relevanssi_indexing_tokens', $title_tokens, 'title' );

	if ( $debug ) {
		relevanssi_debug_echo( "\tTitle, tokenized: " . implode( ' ', array_keys( $title_tokens ) ) );
	}

	foreach ( $title_tokens as $token => $count ) {
		++$n;
		if ( ! isset( $insert_data[ $token ]['title'] ) ) {
			$insert_data[ $token ]['title'] = 0;
		}
		$insert_data[ $token ]['title'] += $count;
	}

	return $n;
}

/**
 * Creates indexing queries for post content.
 *
 * @param array   $insert_data     The INSERT query data. Modified here.
 * @param object  $post_object     The post object.
 * @param int     $min_word_length The minimum word length.
 * @param boolean $debug           If true, print out debug notices.
 *
 * @return int The number of tokens added to the data.
 */
function relevanssi_index_content( &$insert_data, $post_object, $min_word_length, $debug ) {
	$n = 0;

	/**
	 * If this filter returns false, post content is not indexed at all.
	 *
	 * @param boolean Return false to prevent post content from being indexed.
	 * Default true.
	 * @param object  $post_object The post object.
	 */
	if ( ! apply_filters( 'relevanssi_index_content', true, $post_object ) ) {
		return $n;
	}

	if ( 'on' === get_post_meta( $post_object->ID, '_relevanssi_hide_content', true ) ) {
		return $n;
	}

	if ( $debug ) {
		relevanssi_debug_echo( 'Indexing post content.' );
	}

	remove_shortcode( 'noindex' );
	add_shortcode( 'noindex', 'relevanssi_noindex_shortcode_indexing' );

	/**
	 * Filters the post content before indexing.
	 *
	 * @param string $post_object->post_content The post content.
	 * @param object $post_object               The full post object.
	 */
	$contents = apply_filters( 'relevanssi_post_content', $post_object->post_content, $post_object );
	if ( $debug ) {
		relevanssi_debug_echo( "\tPost content after relevanssi_post_content:\n$contents" );
	}

	/**
	 * Can be used to add extra content to the post before indexing.
	 *
	 * @author Alexander Gieg
	 *
	 * @param string               The additional content.
	 * @param WP_Post $post_object The post object.
	 */
	$additional_content = trim( apply_filters( 'relevanssi_content_to_index', '', $post_object ) );
	if ( ! empty( $additional_content ) ) {
		$contents .= ' ' . $additional_content;

		if ( $debug ) {
			relevanssi_debug_echo( "\tAdditional content from relevanssi_content_to_index:\n$additional_content" );
		}
	}

	$contents = relevanssi_do_shortcode( $contents );

	remove_shortcode( 'noindex' );
	add_shortcode( 'noindex', 'relevanssi_noindex_shortcode' );

	/**
	 * Filters the post content after shortcodes but before HTML stripping.
	 *
	 * @param string $contents    The post content.
	 * @param object $post_object The full post object.
	 */
	$contents = apply_filters(
		'relevanssi_post_content_after_shortcodes',
		$contents,
		$post_object
	);

	$contents = relevanssi_strip_invisibles( $contents );

	// Premium feature for better control over internal links.
	if ( function_exists( 'relevanssi_process_internal_links' ) ) {
		$contents = relevanssi_process_internal_links( $contents, $post_object->ID );
	}

	$contents = relevanssi_strip_all_tags( $contents );

	/**
	 * Filters the post content in indexing before tokenization.
	 *
	 * @param string $contents    The post content.
	 * @param object $post_object The full post object.
	 */
	$contents = apply_filters( 'relevanssi_post_content_before_tokenize', $contents, $post_object );

	/** This filter is documented in lib/indexing.php */
	$content_tokens = apply_filters(
		'relevanssi_indexing_tokens',
		relevanssi_tokenize( $contents, 'body', $min_word_length, 'indexing' ),
		'content'
	);

	if ( $debug ) {
		relevanssi_debug_echo( "\tContent, tokenized:\n" . implode( ' ', array_keys( $content_tokens ) ) );
	}

	foreach ( $content_tokens as $token => $count ) {
		++$n;
		if ( ! isset( $insert_data[ $token ]['content'] ) ) {
			$insert_data[ $token ]['content'] = 0;
		}
		$insert_data[ $token ]['content'] += $count;
	}

	return $n;
}

/**
 * Disables problematic shortcode before Relevanssi indexing to avoid problems.
 *
 * Uses the `relevanssi_disabled_shortcodes` filter hook to filter the
 * shortcodes. The disabled shortcodes are first removed with
 * remove_shortcode() and then given a reference to `__return_empty_string`.
 *
 * The option `relevanssi_disable_shortcodes` is also supported for legacy
 * reasons, but it's better to use the filter instead.
 */
function relevanssi_disable_shortcodes() {
	$default_disables = array(
		'contact-form', // Jetpack Contact Form causes an error message.
		'starrater', // GD Star Rating rater shortcode causes problems.
		'responsive-flipbook', // Responsive Flipbook causes problems.
		'avatar_upload', // WP User Avatar is incompatible.
		'product_categories', // A problematic WooCommerce shortcode.
		'recent_products', // A problematic WooCommerce shortcode.
		'php', // PHP Code for Posts.
		'watupro', // Watu PRO doesn't co-operate.
		'starbox', // Starbox shortcode breaks Relevanssi.
		'cfdb-save-form-post', // Contact Form DB.
		'cfdb-datatable',
		'cfdb-table',
		'cfdb-json',
		'cfdb-value',
		'cfdb-count',
		'cfdb-html',
		'woocommerce_cart', // WooCommerce.
		'woocommerce_checkout',
		'woocommerce_order_tracking',
		'woocommerce_my_account',
		'woocommerce_edit_account',
		'woocommerce_change_password',
		'woocommerce_view_order',
		'woocommerce_logout',
		'woocommerce_pay',
		'woocommerce_thankyou',
		'woocommerce_lost_password',
		'woocommerce_edit_address',
		'tc_process_payment',
		'maxmegamenu', // Max Mega Menu.
		'searchandfilter', // Search and Filter.
		'downloads', // Easy Digital Downloads.
		'download_history',
		'purchase_history',
		'download_checkout',
		'purchase_link',
		'download_cart',
		'edd_profile_editor',
		'edd_login',
		'edd_register',
		'swpm_protected', // Simple Membership Partially Protected content.
		'gravityform', // Gravity Forms.
		'sdm_latest_downloads', // SDM Simple Download Monitor.
		'slimstat', // Slimstat Analytics.
		'ninja_tables', // Ninja Tables.
	);

	$disable_shortcodes = get_option( 'relevanssi_disable_shortcodes' );
	$shortcodes         = explode( ',', $disable_shortcodes );
	/**
	 * Filters the shortcodes Relevanssi disables while indexing posts.
	 *
	 * @param array An array of shortcodes disabled.
	 *
	 * @return array An array of shortcodes disabled.
	 */
	$shortcodes = apply_filters(
		'relevanssi_disabled_shortcodes',
		array_unique( array_merge( $shortcodes, $default_disables ) )
	);

	foreach ( $shortcodes as $shortcode ) {
		if ( empty( $shortcode ) ) {
			continue;
		}
		remove_shortcode( trim( $shortcode ) );
		add_shortcode( trim( $shortcode ), '__return_empty_string' );
	}
}

/**
 * Converts INSERT query data array to query values.
 *
 * Takes the collected data and converts it to values that can be fed into
 * an INSERT query using $wpdb->prepare(). Provides filters to modify the
 * insert query values before and after the conversion.
 *
 * @global $wpdb The WordPress database interface.
 * @global $relevanssi_variables Used for the Relevanssi db table name.
 *
 * @param array  $insert_data An array of term => data pairs, where data has
 * token counts for the term in different contexts.
 * @param object $post        The indexed post object.
 *
 * @return array An array of values clauses for an INSERT query.
 */
function relevanssi_convert_data_to_values( $insert_data, $post ) {
	global $wpdb, $relevanssi_variables;

	$charset = $wpdb->get_col_charset(
		$relevanssi_variables['relevanssi_table'],
		'term'
	);

	/**
	 * Sets the indexed post 'type' column in the index.
	 *
	 * Default value is 'post', but other common values include 'attachment',
	 * 'user' and taxonomy name.
	 *
	 * @param string Type value.
	 * @param object The post object for the current post.
	 */
	$type = apply_filters( 'relevanssi_index_get_post_type', 'post', $post );

	/**
	 * Filters the indexing data before it is converted to INSERT queries.
	 *
	 * @param array  $insert_data All the tokens and their counts.
	 * @param object $post        The post object.
	 */
	$insert_data = apply_filters( 'relevanssi_indexing_data', $insert_data, $post );

	$values = array();
	foreach ( $insert_data as $term => $data ) {
		$content            = isset( $data['content'] ) ? $data['content'] : 0;
		$title              = isset( $data['title'] ) ? $data['title'] : 0;
		$comment            = isset( $data['comment'] ) ? $data['comment'] : 0;
		$tag                = isset( $data['tag'] ) ? $data['tag'] : 0;
		$link               = isset( $data['link'] ) ? $data['link'] : 0;
		$author             = isset( $data['author'] ) ? $data['author'] : 0;
		$category           = isset( $data['category'] ) ? $data['category'] : 0;
		$excerpt            = isset( $data['excerpt'] ) ? $data['excerpt'] : 0;
		$taxonomy           = isset( $data['taxonomy'] ) ? $data['taxonomy'] : 0;
		$customfield        = isset( $data['customfield'] ) ? $data['customfield'] : 0;
		$mysqlcolumn        = isset( $data['mysqlcolumn'] ) ? $data['mysqlcolumn'] : 0;
		$taxonomy_detail    = isset( $data['taxonomy_detail'] ) ? $data['taxonomy_detail'] : '';
		$customfield_detail = isset( $data['customfield_detail'] ) ? $data['customfield_detail'] : '';
		$mysqlcolumn_detail = isset( $data['mysqlcolumn_detail'] ) ? $data['mysqlcolumn_detail'] : '';

		if ( 'utf8' === $charset ) {
			$term = wp_encode_emoji( $term );
		}

		$term = trim( $term );

		$value = $wpdb->prepare(
			'(%d, %s, REVERSE(%s), %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %s, %s, %s, %d, %s)',
			$post->ID,
			$term,
			$term,
			$content,
			$title,
			$comment,
			$tag,
			$link,
			$author,
			$category,
			$excerpt,
			$taxonomy,
			$customfield,
			$type,
			$taxonomy_detail,
			$customfield_detail,
			$mysqlcolumn,
			$mysqlcolumn_detail
		);

		array_push( $values, $value );
	}

	/**
	 * Filters the INSERT query VALUES sections before they are inserted in the INSERT query.
	 *
	 * @param array  $values Value sets.
	 * @param object $post   The post object.
	 */
	return apply_filters( 'relevanssi_indexing_values', $values, $post );
}