-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathclass-webmention.php
203 lines (166 loc) · 5.14 KB
/
class-webmention.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
<?php
/**
* Webmention Class
*
* @package Webmention
*/
namespace Webmention;
/**
* Webmention Class
*
* @package Webmention
*/
class Webmention {
/**
* Instance of the class.
*
* @var Webmention
*/
private static $instance;
/**
* Text domain.
*
* @var string
*/
const TEXT_DOMAIN = 'webmention';
/**
* Default post types.
*
* @var array
*/
private $default_post_types = array( 'post', 'page' );
/**
* Whether the class has been initialized.
*
* @var boolean
*/
private $initialized = false;
/**
* Get the instance of the class.
*
* @return Webmention
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Do not allow multiple instances of the class.
*/
private function __construct() {
// Do nothing.
}
/**
* Initialize the plugin.
*/
public function init() {
if ( $this->initialized ) {
return;
}
$this->register_constants();
$this->register_hooks();
$this->register_admin_hooks();
$this->add_post_type_support();
// Load language files.
load_plugin_textdomain( self::TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
$this->initialized = true;
}
/**
* Get the plugin version.
*
* @return string
*/
public function get_version() {
return WEBMENTION_VERSION;
}
/**
* Register hooks.
*/
public function register_hooks() {
\add_action( 'init', array( Tools::class, 'init' ) );
\add_action( 'init', array( Comment::class, 'init' ) );
\add_action( 'init', array( Comment_Walker::class, 'init' ) );
// load local avatar support.
\add_action( 'init', array( Avatar::class, 'init' ) );
// load HTTP 410 support.
\add_action( 'init', array( HTTP_Gone::class, 'init' ) );
// initialize Webmention Sender.
\add_action( 'init', array( Sender::class, 'init' ) );
// initialize Webmention Receiver.
\add_action( 'init', array( Receiver::class, 'init' ) );
// initialize Webmention Discovery.
\add_action( 'init', array( Discovery::class, 'init' ) );
if ( site_supports_blocks() ) {
// initialize Webmention Bloks.
\add_action( 'init', array( Block::class, 'init' ) );
}
// load local avatar store.
if ( 1 === (int) get_option( 'webmention_avatar_store_enable', 0 ) ) {
\add_action( 'init', array( Avatar_Store::class, 'init' ) );
}
// initialize Webmention Vouch
if ( WEBMENTION_VOUCH ) {
\add_action( 'init', array( Vouch::class, 'init' ) );
}
\add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// remove the "webmentions_disabled" meta value if the post is updated
\add_action( 'updated_postmeta', array( $this, 'updated_postmeta' ), 10, 4 );
}
/**
* Register admin hooks.
*/
public function register_admin_hooks() {
add_action( 'admin_init', array( Admin::class, 'admin_init' ) );
add_action( 'admin_menu', array( Admin::class, 'admin_menu' ) );
}
/**
* Add support for Webmentions to custom post types.
*/
public function add_post_type_support() {
// Add support for Webmentions to custom post types.
$post_types = get_option( 'webmention_support_post_types', $this->default_post_types ) ? get_option( 'webmention_support_post_types', $this->default_post_types ) : array();
foreach ( $post_types as $post_type ) {
add_post_type_support( $post_type, 'webmentions' );
}
}
/**
* Register constants.
*/
public function register_constants() {
\defined( 'WEBMENTION_ALWAYS_SHOW_HEADERS' ) || \define( 'WEBMENTION_ALWAYS_SHOW_HEADERS', 0 );
\defined( 'WEBMENTION_COMMENT_APPROVE' ) || \define( 'WEBMENTION_COMMENT_APPROVE', 0 );
\defined( 'WEBMENTION_COMMENT_TYPE' ) || \define( 'WEBMENTION_COMMENT_TYPE', 'webmention' );
\defined( 'WEBMENTION_GRAVATAR_CACHE_TIME' ) || \define( 'WEBMENTION_GRAVATAR_CACHE_TIME', WEEK_IN_SECONDS );
\defined( 'WEBMENTION_AVATAR_QUALITY' ) || \define( 'WEBMENTION_AVATAR_QUALITY', null );
\defined( 'WEBMENTION_AVATAR_SIZE' ) || \define( 'WEBMENTION_AVATAR_SIZE', 256 );
\define( 'WEBMENTION_PROCESS_TYPE_ASYNC', 'async' );
\define( 'WEBMENTION_PROCESS_TYPE_SYNC', 'sync' );
\defined( 'WEBMENTION_PROCESS_TYPE' ) || \define( 'WEBMENTION_PROCESS_TYPE', WEBMENTION_PROCESS_TYPE_SYNC );
\defined( 'WEBMENTION_VOUCH' ) || \define( 'WEBMENTION_VOUCH', false );
// Mentions with content less than this length will be rendered in full.
\defined( 'MAX_INLINE_MENTION_LENGTH' ) || \define( 'MAX_INLINE_MENTION_LENGTH', 300 );
}
/**
* Enqueue scripts.
*/
public function enqueue_scripts() {
if ( is_singular() ) {
wp_enqueue_style( self::TEXT_DOMAIN, WEBMENTION_PLUGIN_URL . 'assets/css/webmention.css', array(), $this->get_version() );
}
}
/**
* Delete the webmentions_disabled meta value if the post is updated.
*
* @param int $meta_id The meta ID.
* @param int $object_id The object ID.
* @param string $meta_key The meta key.
* @param mixed $meta_value The meta value.
*/
public function updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) {
if ( 'webmentions_disabled' === $meta_key && empty( $meta_value ) ) {
\delete_post_meta( $object_id, 'webmentions_disabled' );
}
}
}