-
Notifications
You must be signed in to change notification settings - Fork 8
/
relevanssi-acf-subfields.php
140 lines (116 loc) · 3.82 KB
/
relevanssi-acf-subfields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
Plugin Name: Relevanssi: add ACF subfields to index
Plugin URI: http://github.com/cftp/relevanssi-acf-subfields/
Description: Finds subfields from ACF and feeds them to the Relevanssi indexer so they're findable in search
Version: 0.2
Author: Code for the People Ltd
Author URI: http://codeforthepeople.com/
*/
/* Copyright 2013 Code for the People Ltd
_____________
/ ____ \
_____/ \ \ \
/\ \ \___\ \
/ \ \ \
/ / / _______\
/ / / \ /
/ / / \ /
\ \ \ _____ ___\ /
\ \ /\ \ / \
\ \ / \____\/ _____\
\ \/ / / / \
\ /____/ /___\
\ /
\______________________/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
*
*
* @package Relevanssi ACF subfields
**/
class Relevanssi_ACF_Subfields {
/**
* A version integer.
*
* @var int
**/
var $version;
/**
* Singleton stuff.
*
* @access @static
*
* @return Relevanssi_ACF_Subfields object
*/
static public function init() {
static $instance = false;
if ( ! $instance )
$instance = new Relevanssi_ACF_Subfields;
return $instance;
}
/**
* Class constructor
*
* @return null
*/
public function __construct() {
add_filter( 'relevanssi_content_to_index', array( $this, 'filter_relevanssi_content_to_index' ), 10, 2 );
$this->version = 1;
}
// HOOKS
// =====
/**
* Hooks the WP filter relevanssi_content_to_index
*
* @filter relevanssi_content_to_index
*
* @param string $content Content to pass to Relevanssi
* @param object $post A WP_Post object
* @return string The content we're going to pass to Relevanssi
* @author Simon Wheatley
**/
public function filter_relevanssi_content_to_index( $content, WP_Post $post ) {
// If we don't have ACF available, bail out
if ( ! function_exists( 'the_repeater_field' ) )
return $content;
$custom_fields = relevanssi_get_custom_fields();
foreach ( $custom_fields as $custom_field ) {
if ( false === strpos( $custom_field, '%' ) )
continue;
preg_match( '/([a-z0-9\_]+)_\%_([a-z0-9\_]+)/i', $custom_field, $matches );
$field_name = $matches[ 1 ];
$subfield_name = $matches[ 2 ];
$num_fields = get_post_meta( $post->ID, $field_name, true );
for ( $i = 0; $i < $num_fields; $i++ ) {
$raw = get_post_meta( $post->ID, "{$field_name}_{$i}_{$subfield_name}", true );
// Copied and pasted from Relevanssi
remove_shortcode('noindex');
add_shortcode('noindex', 'relevanssi_noindex_shortcode_indexing');
$disable_shortcodes = get_option('relevanssi_disable_shortcodes');
$shortcodes = explode(',', $disable_shortcodes);
foreach ($shortcodes as $shortcode) {
remove_shortcode(trim($shortcode));
}
remove_shortcode('contact-form'); // Jetpack Contact Form causes an error message
remove_shortcode('starrater'); // GD Star Rating rater shortcode causes problems
$value = do_shortcode( $raw ) . PHP_EOL;
$content .= $value;
}
}
return $content;
}
}
// Initiate the singleton
Relevanssi_ACF_Subfields::init();