-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathclass-wpseo-option-woo.php
226 lines (193 loc) · 6.43 KB
/
class-wpseo-option-woo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* @package Internals
* @since 1.1.0
* @version 1.1.0
*/
// Avoid direct calls to this file
if ( ! class_exists( 'Yoast_WooCommerce_SEO' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
/*******************************************************************
* Option: wpseo_woo
*******************************************************************/
if ( ! class_exists( 'WPSEO_Option_Woo' ) && class_exists( 'WPSEO_Option' ) ) {
class WPSEO_Option_Woo extends WPSEO_Option {
/**
* @var string option name
*/
public $option_name = 'wpseo_woo';
/**
* @var string option group name for use in settings forms
*/
public $group_name = 'wpseo_woo_options';
/**
* @var bool whether to include the option in the return for WPSEO_Options::get_all()
*/
public $include_in_all = false;
/**
* @var bool whether this option is only for when the install is multisite
*/
public $multisite_only = false;
/**
* @var int Database version to check whether the plugins options need updating.
*/
public $db_version = 2;
/**
* @var array Array of defaults for the option
* Shouldn't be requested directly, use $this->get_defaults();
*/
protected $defaults = array(
// Non-form fields, set via validation routine
'dbversion' => 0, // leave default as 0 to ensure activation/upgrade works
// Form fields:
'data1_type' => 'price',
'data2_type' => 'stock',
'schema_brand' => '',
'schema_manufacturer' => '',
'breadcrumbs' => true,
'hide_columns' => true,
'metabox_woo_top' => true,
);
/**
* @var array $valid_data_types Array of pre-defined valid data types, will be enriched with taxonomies
*/
public $valid_data_types = array();
/**
* Add the actions and filters for the option
*
* @return \WPSEO_Option_Woo
*/
protected function __construct() {
parent::__construct();
// Set and translate the valid data types
$this->valid_data_types = array(
'price' => __( 'Price', 'yoast-woo-seo' ),
'stock' => __( 'Stock', 'yoast-woo-seo' ),
);
}
/**
* Get the singleton instance of this class
*
* @return object
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Validate the option
*
* @param array $dirty New value for the option
* @param array $clean Clean value for the option, normally the defaults
* @param array $old Old value of the option
*
* @todo remove code using $short, there is no "short form" anymore.
*
* @return array Validated clean value for the option to be saved to the database
*/
protected function validate_option( $dirty, $clean, $old ) {
// Have we receive input from a short (license only) form ?
$short = ( isset( $dirty['short_form'] ) && $dirty['short_form'] === 'on' ) ? true : false;
// Prepare an array of valid data types and taxonomies to validate against
$valid_data_types = array_keys( $this->valid_data_types );
$valid_taxonomies = array();
$taxonomies = get_object_taxonomies( 'product', 'objects' );
if ( is_array( $taxonomies ) && $taxonomies !== array() ) {
foreach ( $taxonomies as $tax ) {
$tax_name = strtolower( $tax->name );
$valid_data_types[] = $tax_name;
$valid_taxonomies[] = $tax_name;
}
}
unset( $taxonomies, $tax, $tax_name );
foreach ( $clean as $key => $value ) {
switch ( $key ) {
case 'dbversion':
$clean[ $key ] = $this->db_version;
break;
case 'data1_type':
case 'data2_type':
if ( isset( $dirty[ $key ] ) ) {
if ( in_array( $dirty[ $key ], $valid_data_types, true ) ) {
$clean[ $key ] = $dirty[ $key ];
} else {
if ( sanitize_title_with_dashes( $dirty[ $key ] ) === $dirty[ $key ] ) {
// Allow taxonomies which may not be registered yet
$clean[ $key ] = $dirty[ $key ];
}
}
} else {
if ( $short && isset( $old[ $key ] ) ) {
if ( in_array( $old[ $key ], $valid_data_types, true ) ) {
$clean[ $key ] = $old[ $key ];
} else {
if ( sanitize_title_with_dashes( $old[ $key ] ) === $old[ $key ] ) {
// Allow taxonomies which may not be registered yet
$clean[ $key ] = $old[ $key ];
}
}
}
}
break;
case 'schema_brand':
case 'schema_manufacturer':
if ( isset( $dirty[ $key ] ) ) {
if ( in_array( $dirty[ $key ], $valid_taxonomies, true ) ) {
$clean[ $key ] = $dirty[ $key ];
} else {
if ( sanitize_title_with_dashes( $dirty[ $key ] ) === $dirty[ $key ] ) {
// Allow taxonomies which may not be registered yet
$clean[ $key ] = $dirty[ $key ];
}
}
} else {
if ( $short && isset( $old[ $key ] ) ) {
if ( in_array( $old[ $key ], $valid_taxonomies, true ) ) {
$clean[ $key ] = $old[ $key ];
} else {
if ( sanitize_title_with_dashes( $old[ $key ] ) === $old[ $key ] ) {
// Allow taxonomies which may not be registered yet
$clean[ $key ] = $old[ $key ];
}
}
}
}
break;
/* boolean (checkbox) field - may not be in form */
case 'breadcrumbs':
case 'hide_columns':
case 'metabox_woo_top':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = WPSEO_WooCommerce_Wrappers::validate_bool( $dirty[ $key ] );
} else {
if ( $short && isset( $old[ $key ] ) ) {
$clean[ $key ] = WPSEO_WooCommerce_Wrappers::validate_bool( $old[ $key ] );
} else {
$clean[ $key ] = false;
}
}
break;
}
}
return $clean;
}
/**
* Clean a given option value
*
* @param array $option_value Old (not merged with defaults or filtered) option value to
* clean according to the rules for this option
* @param string $current_version (optional) Version from which to upgrade, if not set,
* version specific upgrades will be disregarded
*
* @return array Cleaned option
*/
/*protected function clean_option( $option_value, $current_version = null ) {
return $option_value;
}*/
} /* End of class WPSEO_Option_Woo */
} /* End of class-exists wrapper */