-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsettings.php
More file actions
394 lines (341 loc) · 12.6 KB
/
settings.php
File metadata and controls
394 lines (341 loc) · 12.6 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
<?php
/**
* Admin Settings.
*
* @package AdRefreshControl
*/
namespace AdRefreshControl\Settings;
/**
* Default setup routine
*
* @return void
*/
function setup() {
add_action( 'admin_menu', __NAMESPACE__ . '\admin_menu', 20 );
add_action( 'admin_init', __NAMESPACE__ . '\setup_fields_sections' );
add_action( 'admin_init', __NAMESPACE__ . '\register_settings' );
}
/**
* Output setting menu option
*
* @since 1.0
*/
function admin_menu() {
add_submenu_page(
'options-general.php',
esc_html__( 'Ad Refresh Control', 'ad-refresh-control' ),
esc_html__( 'Ad Refresh Control', 'ad-refresh-control' ),
apply_filters( 'avc_menu_access', 'manage_options' ),
'ad-refresh-control-settings',
__NAMESPACE__ . '\settings_screen'
);
}
/**
* Output setting screen
*
* @since 1.0
*/
function settings_screen() {
?>
<div class="wrap">
<h2><?php esc_html_e( 'Ad Refresh Control Settings', 'ad-refresh-control' ); ?></h2>
<form method="post" action="options.php">
<?php settings_fields( 'avc_settings' ); ?>
<?php do_settings_sections( 'ad-refresh-control' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Register setting fields and sections
*
* @since 1.0
*/
function setup_fields_sections() {
add_settings_section( 'arc-section-1', '', '', 'ad-refresh-control' );
add_settings_field( 'disable_refresh', esc_html__( 'Disable Ad Refresh', 'ad-refresh-control' ), __NAMESPACE__ . '\disable_refresh_callback', 'ad-refresh-control', 'arc-section-1' );
add_settings_field( 'viewability_threshold', esc_html__( 'Viewability Threshold', 'ad-refresh-control' ), __NAMESPACE__ . '\activate_viewability_threshold_callback', 'ad-refresh-control', 'arc-section-1' );
add_settings_field( 'refresh_interval', esc_html__( 'Refresh Interval', 'ad-refresh-control' ), __NAMESPACE__ . '\refresh_interval_callback', 'ad-refresh-control', 'arc-section-1' );
add_settings_field( 'maximum_refreshes', esc_html__( 'Maximum Refreshes', 'ad-refresh-control' ), __NAMESPACE__ . '\maximum_refreshes_callback', 'ad-refresh-control', 'arc-section-1' );
add_settings_field( 'advertiser_ids', esc_html__( 'Excluded Advertiser IDs', 'ad-refresh-control' ), __NAMESPACE__ . '\advertiser_ids_callback', 'ad-refresh-control', 'arc-section-1' );
add_settings_field( 'line_item_ids', esc_html__( 'Line Items IDs to Exclude', 'ad-refresh-control' ), __NAMESPACE__ . '\line_items_callback', 'ad-refresh-control', 'arc-section-1' );
add_settings_field( 'sizes_to_exclude', esc_html__( 'Sizes to Exclude', 'ad-refresh-control' ), __NAMESPACE__ . '\sizes_to_exclude_callback', 'ad-refresh-control', 'arc-section-1' );
add_settings_field( 'slot_ids_to_exclude', esc_html__( 'Slot IDs to Exclude', 'ad-refresh-control' ), __NAMESPACE__ . '\slot_ids_to_exclude_callback', 'ad-refresh-control', 'arc-section-1' );
}
/**
* Output disable refresh field.
*
* @since 1.0
*/
function disable_refresh_callback() {
$avc_settings = get_option( 'avc_settings' );
$value = $avc_settings['disable_refresh'] ?? false;
?>
<label><input <?php checked( $value, true ); ?> type="checkbox" value="1" name="avc_settings[disable_refresh]">
<?php esc_html_e( 'Disable ad refresh on all ads.', 'ad-refresh-control' ); ?>
</label>
<?php
}
/**
* Output viewability threshold settings field
*
* @since 1.0
*/
function activate_viewability_threshold_callback() {
$avc_settings = get_option( 'avc_settings' );
$value = $avc_settings['viewability_threshold'] ?? 70;
?>
<label><input type="text" value="<?php echo esc_attr( $value ); ?>" name="avc_settings[viewability_threshold]">
<p><?php esc_html_e( 'The percentage of the ad slot which must be visible in the viewport in order to be considered eligible for being refreshed. It\'s recommended you do not lower this below 50 or you risk third-party viewability tracking platforms flagging your ad impressions as not having been viewed before refreshing.', 'ad-refresh-control' ); ?></p>
</label>
<?php
}
/**
* Output refresh interval settings field
*
* @since 1.0
*/
function refresh_interval_callback() {
$avc_settings = get_option( 'avc_settings' );
$value = $avc_settings['refresh_interval'] ?? 30;
/**
* Refresh interval.
*
* Allows developers to filter the default refresh interval value of 30 seconds.
* We strongly advise developers to not filter this to a value less than 30 seconds.
* Filterable via the avc_refresh_interval_value filter hook.
*
* @since 1.0.5
* @link https://github.com/10up/Ad-Refresh-Control/issues/46
*
* @param int $value The refresh interval in seconds. Defaults to 30.
*/
$value = absint( apply_filters( 'avc_refresh_interval_value', $value ) );
?>
<label><input type="text" value="<?php echo esc_attr( $value ); ?>" name="avc_settings[refresh_interval]">
<p><?php esc_html_e( 'The number of seconds that must pass between an ad crossing the viewability threshold and the the ad refreshing. The plugin enforces a minimum of 30 in order to avoid your site being flagged for abusing ad refreshes by advertisers. This value may however be overridden via the avc_refresh_interval_value filter hook.', 'ad-refresh-control' ); ?></p>
</label>
<?php
}
/**
* Output maximum refreshes settings field
*
* @since 1.0
*/
function maximum_refreshes_callback() {
$avc_settings = get_option( 'avc_settings' );
$value = $avc_settings['maximum_refreshes'] ?? 10;
?>
<label><input type="text" value="<?php echo esc_attr( $value ); ?>" name="avc_settings[maximum_refreshes]">
<p><?php esc_html_e( 'The number of times each ad slot is allowed to be refreshed. If this is set to 4 then an ad slot could have a total of 5 impressions by combining the initial loading of the ad with the 4 times it can refresh.', 'ad-refresh-control' ); ?></p>
</label>
<?php
}
/**
* Output the advertiser id settings field
*
* @since 1.0
*/
function advertiser_ids_callback() {
$avc_settings = get_option( 'avc_settings' );
$value = $avc_settings['advertiser_ids'] ?? [];
?>
<label><input type="text" value="<?php echo esc_attr( implode( ',', $value ) ); ?>" name="avc_settings[advertiser_ids]">
<p><?php esc_html_e( 'Prevent ad refreshes for specific advertiser IDs in the format of a comma-separated list (e.g., 125,594,293). If an ad slot ever displays an ad creative from one of the listed advertiser IDs then that ad slot will stop refreshing for the remainder of the page view.', 'ad-refresh-control' ); ?></p>
</label>
<?php
}
/**
* Output the line items settings field.
*
* @since 1.0.2
*/
function line_items_callback() {
$avc_settings = get_option( 'avc_settings' );
$value = $avc_settings['line_item_ids'] ?? [];
?>
<label><input type="text" value="<?php echo esc_attr( implode( ',', $value ) ); ?>" name="avc_settings[line_item_ids]">
<p><?php esc_html_e( 'Prevent ad refreshes for specific line item IDs. (Comma-separated list)', 'ad-refresh-control' ); ?></p>
</label>
<?php
}
/**
* Output the sizes to exclude field.
*
* @since 1.0.2
*/
function sizes_to_exclude_callback() {
$avc_settings = get_option( 'avc_settings' );
$value = $avc_settings['sizes_to_exclude'] ?? '';
?>
<label><input type="text" value="<?php echo esc_attr( $value ); ?>" name="avc_settings[sizes_to_exclude]">
<p><?php esc_html_e( 'Prevent ad refreshes for specific sizes. Accepts string (fluid) or array (300x250). Example: fluid, 300x250.', 'ad-refresh-control' ); ?></p>
</label>
<?php
}
/**
* Output the slot IDs to exclude field.
*
* @since 1.0.2
*/
function slot_ids_to_exclude_callback() {
$avc_settings = get_option( 'avc_settings' );
$value = $avc_settings['slot_ids_to_exclude'] ?? [];
$value = array_filter(
array_map(
'trim',
$value
),
'strlen'
);
?>
<label><input type="text" value="<?php echo esc_attr( implode( ', ', $value ) ); ?>" name="avc_settings[slot_ids_to_exclude]">
<p><?php esc_html_e( 'Prevent ad refreshes for specific slot IDs e.g. div-gpt-ad-grid-1. (Comma-separated list).', 'ad-refresh-control' ); ?></p>
</label>
<?php
}
/**
* Register settings for options table
*
* @since 1.0
*/
function register_settings() {
if ( apply_filters( 'avc_menu_access', current_user_can( 'manage_options' ) ) ) {
register_setting( 'avc_settings', 'avc_settings', __NAMESPACE__ . '\sanitize_settings' );
}
}
/**
* Sanitize settings for DB
*
* @param array $settings Array of settings.
* @since 1.0
*/
function sanitize_settings( $settings ) {
// disable_refresh
if ( isset( $settings['disable_refresh'] ) && filter_var( $settings['disable_refresh'], FILTER_VALIDATE_BOOLEAN ) ) {
$settings['disable_refresh'] = true;
} else {
$settings['disable_refresh'] = false;
}
// viewability_threshold
$viewability_threshold_default = 70;
if ( isset( $settings['viewability_threshold'] ) ) {
if ( ! is_numeric( $settings['viewability_threshold'] ) || intval( $settings['viewability_threshold'] ) < 0 || intval( $settings['viewability_threshold'] ) > 100 ) {
$settings['viewability_threshold'] = intval( $viewability_threshold_default );
}
} else {
$settings['viewability_threshold'] = $viewability_threshold_default;
}
/**
* Refresh interval.
*
* Allows developers to filter the default refresh interval value of 30 seconds.
* We strongly advise developers to not filter this to a value less than 30 seconds.
* Filterable via the avc_refresh_interval_value filter hook.
*
* @since 1.0.5
* @link https://github.com/10up/Ad-Refresh-Control/issues/46
*
* @param int $refresh_interval_default The refresh interval in seconds. Defaults to 30.
*/
$refresh_interval_default = 30;
$refresh_interval_default = absint( apply_filters( 'avc_refresh_interval_value', $refresh_interval_default ) );
if ( isset( $settings['refresh_interval'] ) ) {
if ( ! is_numeric( $settings['refresh_interval'] ) || intval( $settings['refresh_interval'] ) < 30 ) {
$settings['refresh_interval'] = intval( $refresh_interval_default );
}
} else {
$settings['refresh_interval'] = $refresh_interval_default;
}
// maximum_refreshes
$maximum_refreshes_default = 10;
if ( isset( $settings['maximum_refreshes'] ) ) {
if ( ! is_numeric( $settings['maximum_refreshes'] ) || intval( $settings['maximum_refreshes'] ) < 1 ) {
$settings['maximum_refreshes'] = $maximum_refreshes_default;
}
} else {
$settings['maximum_refreshes'] = $maximum_refreshes_default;
}
// advertiser_ids
$advertiser_ids_default = [];
if ( ! empty( $settings['advertiser_ids'] ) ) {
if ( is_array( $settings['advertiser_ids'] ) ) {
$advertiser_ids = $settings['advertiser_ids'];
} else {
$advertiser_ids = explode( ',', $settings['advertiser_ids'] );
}
$advertiser_ids = array_filter(
$advertiser_ids,
function( $advertiser_id ):int {
return is_numeric( $advertiser_id );
}
);
$advertiser_ids = array_map(
function( $advertiser_id ) {
return (int) $advertiser_id;
},
$advertiser_ids
);
$settings['advertiser_ids'] = $advertiser_ids;
} else {
$settings['advertiser_ids'] = $advertiser_ids_default;
}
// Line item IDs.
$line_item_ids_default = [];
if ( ! empty( $settings['line_item_ids'] ) ) {
if ( is_array( $settings['line_item_ids'] ) ) {
$line_item_ids = $settings['line_item_ids'];
} else {
$line_item_ids = explode( ',', $settings['line_item_ids'] );
}
$line_item_ids = array_filter(
$line_item_ids,
function ( $line_item_id ):int {
return is_numeric( $line_item_id );
}
);
$line_item_ids = array_map(
function ( $line_item_id ) {
return (int) $line_item_id;
},
$line_item_ids
);
$settings['line_item_ids'] = $line_item_ids;
} else {
$settings['line_item_ids'] = $line_item_ids_default;
}
// Sizes.
$sizes_to_exclude_default = '';
if ( ! empty( $settings['sizes_to_exclude'] ) ) {
$sizes = sanitize_text_field( $settings['sizes_to_exclude'] );
$settings['sizes_to_exclude'] = $sizes;
} else {
$settings['sizes_to_exclude'] = $sizes_to_exclude_default;
}
// Slot IDs.
$slot_ids_to_exclude_default = [];
if ( ! empty( $settings['slot_ids_to_exclude'] ) ) {
if ( is_array( $settings['slot_ids_to_exclude'] ) ) {
$slot_ids_to_exclude = $settings['slot_ids_to_exclude'];
} else {
$slot_ids_to_exclude = explode( ',', $settings['slot_ids_to_exclude'] );
}
$slot_ids_to_exclude = array_filter(
$slot_ids_to_exclude,
function ( $slot_id ):int {
return is_string( $slot_id );
}
);
$slot_ids_to_exclude = array_map(
function ( $slot_id ) {
return (string) trim( $slot_id );
},
$slot_ids_to_exclude
);
$settings['slot_ids_to_exclude'] = $slot_ids_to_exclude;
} else {
$settings['slot_ids_to_exclude'] = $slot_ids_to_exclude_default;
}
return $settings;
}