PluginProbe
WPSSO Core – Complete Schema Markup and Meta Tags / trunk
WPSSO Core – Complete Schema Markup and Meta Tags vtrunk
22.6.1 22.6.0 22.5.3 22.5.2 22.5.1 22.4.0 22.3.0 22.2.1 22.2.0 22.1.3 22.1.2 22.1.1 22.1.0 22.0.0 21.13.3 21.13.2 trunk 21.13.1
wpsso / lib / util-custom-fields.php

util-custom-fields.php in WPSSO Core – Complete Schema Markup and Meta Tags trunk, at lib/util-custom-fields.php

243 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * License: GPLv3
4 * License URI: https://www.gnu.org/licenses/gpl.txt
5 * Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/)
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9
10 die( 'These aren\'t the droids you\'re looking for.' );
11 }
12
13 if ( ! defined( 'WPSSO_PLUGINDIR' ) ) {
14
15 die( 'Do. Or do not. There is no try.' );
16 }
17
18 if ( ! class_exists( 'WpssoUtilCustomFields' ) ) {
19
20 class WpssoUtilCustomFields {
21
22 private $p; // Wpsso class object.
23 private $u; // WpssoUtil class object.
24
25 /*
26 * Instantiated by WpssoUtil->__construct().
27 */
28 public function __construct( &$plugin, &$util ) {
29
30 $this->p =& $plugin;
31 $this->u =& $util;
32
33 if ( $this->p->debug->enabled ) {
34
35 $this->p->debug->mark();
36 }
37
38 $this->u->add_plugin_filters( $this, array(
39 'import_custom_fields' => 4,
40 ) );
41 }
42
43 /*
44 * The 'import_custom_fields' filter is executed before the 'wpsso_get_md_options' and 'wpsso_get_post_options'
45 * filters, custom field values may get overwritten by these filters.
46 *
47 * The 'import_custom_fields' filter is also executed before the 'wpsso_get_md_defaults' and
48 * 'wpsso_get_post_defaults' filters, so submitted form values that are identical can be removed.
49 */
50 public function filter_import_custom_fields( array $md_opts, array $mod, $wp_meta = array(), $alt_opts = array() ) {
51
52 if ( $this->p->debug->enabled ) {
53
54 $this->p->debug->mark();
55 }
56
57 if ( function_exists( 'is_sitemap' ) && is_sitemap() ) {
58
59 if ( $this->p->debug->enabled ) {
60
61 $this->p->debug->log( 'skipping importing custom fields for sitemap' );
62 }
63
64 return $md_opts;
65 }
66
67 if ( $this->p->debug->enabled ) {
68
69 $this->p->debug->mark( 'importing custom fields' ); // Begin timer.
70
71 if ( ! empty( $alt_opts ) ) {
72
73 $this->p->debug->log_arr( 'alt_opts', $alt_opts );
74 }
75 }
76
77 $charset = get_bloginfo( $show = 'charset', $filter = 'raw' );
78 $cf_md_index = WpssoConfig::get_cf_md_index(); // Uses a local cache.
79 $md_keys_multi = WpssoConfig::get_md_keys_multi(); // Uses a local cache.
80
81 foreach ( $cf_md_index as $opt_cf_key => $md_key ) {
82
83 if ( empty( $md_key ) ) { // Just in case.
84
85 if ( $this->p->debug->enabled ) {
86
87 $this->p->debug->log( 'custom field ' . $opt_cf_key . ' key is disabled' );
88 }
89
90 continue;
91 }
92
93 /*
94 * Check to see if we have an alternate $cf_key value for WooCommerce variations.
95 */
96 if ( isset( $alt_opts[ $opt_cf_key ] ) ) {
97
98 if ( empty( $alt_opts[ $opt_cf_key ] ) ) { // Just in case.
99
100 if ( $this->p->debug->enabled ) {
101
102 $this->p->debug->log( 'meta keys ' . $opt_cf_key . ' value is empty' );
103 }
104
105 continue;
106 }
107
108 $cf_key = $alt_opts[ $opt_cf_key ]; // Example: 'hwp_var_gtin'.
109
110 } else {
111
112 if ( empty( $this->p->options[ $opt_cf_key ] ) ) {
113
114 if ( $this->p->debug->enabled ) {
115
116 $this->p->debug->log( 'custom field ' . $opt_cf_key . ' option is empty' );
117 }
118
119 continue;
120 }
121
122 $cf_key = $this->p->options[ $opt_cf_key ]; // Example: '_format_video_url'.
123 }
124
125 if ( $this->p->debug->enabled ) {
126
127 $this->p->debug->log( 'using custom field ' . $cf_key . ' key for ' . $md_key . ' option' );
128 }
129
130 /*
131 * WordPress offers metadata in array element 0.
132 */
133 $cf_val = null;
134
135 if ( isset( $wp_meta[ $cf_key ][ 0 ] ) ) {
136
137 $cf_val = maybe_unserialize( $wp_meta[ $cf_key ][ 0 ] );
138
139 } elseif ( $this->p->debug->enabled ) {
140
141 $this->p->debug->log( 'no element 0 in ' . $cf_key . ' array' );
142 }
143
144 /*
145 * Apply filters for the custom field (value is null if the custom field does not exist).
146 */
147 $filter_name = SucomUtil::sanitize_hookname( 'wpsso_import_cf_' . $cf_key );
148
149 if ( $this->p->debug->enabled ) {
150
151 $this->p->debug->log( 'applying filter \'' . $filter_name . '\'' );
152 }
153
154 $cf_val = apply_filters( $filter_name, $cf_val, $mod, $wp_meta );
155
156 /*
157 * Decode and trim the string or each array element.
158 */
159 $values = array();
160
161 if ( is_array( $cf_val ) ) {
162
163 if ( $this->p->debug->enabled ) {
164
165 $this->p->debug->log( $cf_key . ' is an array of ' . count( $cf_val ) . ' elements (decoding each value)' );
166 }
167
168 foreach ( $cf_val as $val ) { // Decode and trim each array element.
169
170 if ( is_array( $val ) ) { // Just in case - flatten multi-dimensional arrays.
171
172 $val = SucomUtil::array_implode( $val );
173 }
174
175 $values[] = trim( html_entity_decode( SucomUtil::decode_utf8( $val ), ENT_QUOTES, $charset ) );
176 }
177
178 } elseif ( null !== $cf_val ) {
179
180 if ( $this->p->debug->enabled ) {
181
182 $this->p->debug->log( 'decoding ' . $cf_key . ' as string of ' . strlen( $cf_val ) . ' chars' );
183 }
184
185 $values[] = trim( html_entity_decode( SucomUtil::decode_utf8( $cf_val ), ENT_QUOTES, $charset ) );
186 }
187
188 /*
189 * Check if the value(s) should be split into multiple numeric options.
190 */
191 if ( ! empty( $values ) ) {
192
193 if ( ! empty( $md_keys_multi[ $md_key ] ) ) {
194
195 /*
196 * If $cf_val was not an array, then $values[ 0 ] will be a string - split that string into an array.
197 */
198 if ( ! is_array( $cf_val ) ) {
199
200 $values = array_map( 'trim', explode( PHP_EOL, reset( $values ) ) );
201
202 if ( $this->p->debug->enabled ) {
203
204 $this->p->debug->log( 'exploded ' . $cf_key . ' into array of ' . count( $values ) . ' elements' );
205 }
206 }
207
208 $this->u->maybe_renum_md_key( $md_opts, $md_key, $values, $is_disabled = true );
209
210 } else {
211
212 $md_opts[ $md_key ] = reset( $values );
213
214 $md_opts[ $md_key . ':disabled' ] = true;
215
216 if ( $this->p->debug->enabled ) {
217
218 $this->p->debug->log( 'option ' . $md_key . ' = ' . print_r( $md_opts[ $md_key ], true ) );
219 }
220
221 /*
222 * If this is a '_value' option, add the '_units' option.
223 */
224 $this->u->maybe_add_md_key_units( $md_opts, $md_key );
225
226 /*
227 * If this is an '_img_url' option, add the image dimensions and unset the '_img_id' option.
228 */
229 $this->u->maybe_add_img_url_size( $md_opts, $md_key );
230 }
231 }
232 }
233
234 if ( $this->p->debug->enabled ) {
235
236 $this->p->debug->mark( 'importing custom fields' ); // End timer.
237 }
238
239 return $md_opts;
240 }
241 }
242 }
243