forked from 10up/ads-txt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathads-txt.php
122 lines (107 loc) · 3.1 KB
/
ads-txt.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
<?php
/**
* Plugin Name: Ads.txt Manager
* Description: Create, manage, and validate your Ads.txt from within WordPress, just like any other content asset. Requires PHP 5.3+ and WordPress 4.9+.
* Version: 1.4.0
* Author: 10up
* Author URI: https://10up.com
* License: GPLv2 or later
* Text Domain: ads-txt
*
* @package Ads_Txt_Manager
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
define( 'ADS_TXT_MANAGER_VERSION', '1.4.0' );
define( 'ADS_TXT_MANAGE_CAPABILITY', 'edit_ads_txt' );
define( 'ADS_TXT_MANAGER_POST_OPTION', 'adstxt_post' );
define( 'APP_ADS_TXT_MANAGER_POST_OPTION', 'app_adstxt_post' );
require_once __DIR__ . '/inc/post-type.php';
require_once __DIR__ . '/inc/admin.php';
require_once __DIR__ . '/inc/save.php';
/**
* Display the contents of /ads.txt when requested.
*
* @return void
*/
function tenup_display_ads_txt() {
$request = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false;
if ( '/ads.txt' === $request ) {
$post_id = get_option( ADS_TXT_MANAGER_POST_OPTION );
// Will fall through if no option found, likely to a 404.
if ( ! empty( $post_id ) ) {
$post = get_post( $post_id );
if ( ! $post instanceof WP_Post ) {
return;
}
header( 'Content-Type: text/plain' );
$adstxt = $post->post_content;
/**
* Filter the ads.txt content.
*
* @since 1.2.0
*
* @param type $adstxt The existing ads.txt content.
*/
echo esc_html( apply_filters( 'ads_txt_content', $adstxt ) );
die();
}
} elseif ( '/app-ads.txt' === $request ) {
$post_id = get_option( APP_ADS_TXT_MANAGER_POST_OPTION );
// Will fall through if no option found, likely to a 404.
if ( ! empty( $post_id ) ) {
$post = get_post( $post_id );
if ( ! $post instanceof WP_Post ) {
return;
}
header( 'Content-Type: text/plain' );
$adstxt = $post->post_content;
/**
* Filter the app-ads.txt content.
*
* @since 1.3.0
*
* @param type $app_adstxt The existing ads.txt content.
*/
echo esc_html( apply_filters( 'app_ads_txt_content', $adstxt ) );
die();
}
}
}
add_action( 'init', 'tenup_display_ads_txt' );
/**
* Add custom capabilities.
*
* @return void
*/
function add_adstxt_capabilities() {
$role = get_role( 'administrator' );
if ( ! $role->has_cap( ADS_TXT_MANAGE_CAPABILITY ) ) {
$role->add_cap( ADS_TXT_MANAGE_CAPABILITY );
}
}
add_action( 'admin_init', 'add_adstxt_capabilities' );
register_activation_hook( __FILE__, 'add_adstxt_capabilities' );
/**
* Remove custom capabilities when deactivating the plugin.
*
* @return void
*/
function remove_adstxt_capabilities() {
$role = get_role( 'administrator' );
$role->remove_cap( ADS_TXT_MANAGE_CAPABILITY );
}
register_deactivation_hook( __FILE__, 'remove_adstxt_capabilities' );
/**
* Add a query var to detect when ads.txt has been saved.
*
* @param array $qvars Array of query vars.
*
* @return array Array of query vars.
*/
function tenup_ads_txt_add_query_vars( $qvars ) {
$qvars[] = 'ads_txt_saved';
return $qvars;
}
add_filter( 'query_vars', 'tenup_ads_txt_add_query_vars' );