-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrust-txt.php
108 lines (94 loc) · 2.74 KB
/
trust-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
<?php
/**
* Plugin Name: Trust.txt Manager
* Description: Create, manage, and validate your Trust.txt from within WordPress, just like any other content asset. Requires PHP 5.3+ and WordPress 4.9+.
* Version: 1.3
* Author: rtCamp
* Author URI: https://rtcamp.com
* License: GPLv2 or later
* Text Domain: trust-txt
*
* @package Trust_Txt_Manager
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
define( 'TRUST_TXT_MANAGER_VERSION', '1.3' );
define( 'TRUST_TXT_MANAGE_CAPABILITY', 'edit_trust_txt' );
define( 'TRUST_TXT_MANAGER_POST_OPTION', 'trusttxt_post' );
require_once __DIR__ . '/inc/post-type.php';
require_once __DIR__ . '/inc/admin.php';
require_once __DIR__ . '/inc/save.php';
/**
* Display the contents of /trust.txt when requested.
*
* @return void
*/
function rtcamp_display_trust_txt() {
$request = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false;
$trust_well_known_path = get_option( 'trust_custom_path' );
$valid_paths = array(
'/trust.txt',
);
if ( $trust_well_known_path ) {
$valid_paths[] = '/.well-known/trust.txt';
}
if ( in_array( $request, $valid_paths, true ) ) {
$post_id = get_option( TRUST_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' );
$trusttxt = $post->post_content;
/**
* Filter the trust.txt content.
*
* @since 1.0
*
* @param type $trusttxt The existing trust.txt content.
*/
ob_clean();
echo esc_html( apply_filters( 'trust_txt_content', $trusttxt ) );
die();
}
}
}
add_action( 'init', 'rtcamp_display_trust_txt' );
/**
* Add custom capabilities.
*
* @return void
*/
function add_trusttxt_capabilities() {
$role = get_role( 'administrator' );
if ( ! $role->has_cap( TRUST_TXT_MANAGE_CAPABILITY ) ) {
$role->add_cap( TRUST_TXT_MANAGE_CAPABILITY );
}
}
add_action( 'admin_init', 'add_trusttxt_capabilities' );
register_activation_hook( __FILE__, 'add_trusttxt_capabilities' );
/**
* Remove custom capabilities when deactivating the plugin.
*
* @return void
*/
function remove_trusttxt_capabilities() {
$role = get_role( 'administrator' );
$role->remove_cap( TRUST_TXT_MANAGE_CAPABILITY );
}
register_deactivation_hook( __FILE__, 'remove_trusttxt_capabilities' );
/**
* Add a query var to detect when trust.txt has been saved.
*
* @param array $qvars Array of query vars.
*
* @return array Array of query vars.
*/
function rtcamp_trust_txt_add_query_vars( $qvars ) {
$qvars[] = 'trust_txt_saved';
return $qvars;
}
add_filter( 'query_vars', 'rtcamp_trust_txt_add_query_vars' );