Skip to content

Commit d831c43

Browse files
committed
Improve and simplify the plugin loader
1 parent 9859a50 commit d831c43

File tree

4 files changed

+325
-175
lines changed

4 files changed

+325
-175
lines changed

includes/class-autoloader.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Autoloader for Webmention.
4+
*
5+
* @package Webmention
6+
*/
7+
8+
namespace Webmention;
9+
10+
/**
11+
* An Autoloader that respects WordPress's filename standards.
12+
*/
13+
class Autoloader {
14+
15+
/**
16+
* Namespace separator.
17+
*/
18+
const NS_SEPARATOR = '\\';
19+
20+
/**
21+
* The prefix to compare classes against.
22+
*
23+
* @var string
24+
* @access protected
25+
*/
26+
protected $prefix;
27+
28+
/**
29+
* Length of the prefix string.
30+
*
31+
* @var int
32+
* @access protected
33+
*/
34+
protected $prefix_length;
35+
36+
/**
37+
* Path to the file to be loaded.
38+
*
39+
* @var string
40+
* @access protected
41+
*/
42+
protected $path;
43+
44+
/**
45+
* Constructor.
46+
*
47+
* @param string $prefix Namespace prefix all classes have in common.
48+
* @param string $path Path to the files to be loaded.
49+
*/
50+
public function __construct( $prefix, $path ) {
51+
$this->prefix = $prefix;
52+
$this->prefix_length = \strlen( $prefix );
53+
$this->path = \rtrim( $path . '/' );
54+
}
55+
56+
/**
57+
* Registers Autoloader's autoload function.
58+
*
59+
* @throws \Exception When autoload_function cannot be registered.
60+
*
61+
* @param string $prefix Namespace prefix all classes have in common.
62+
* @param string $path Path to the files to be loaded.
63+
*/
64+
public static function register_path( $prefix, $path ) {
65+
$loader = new self( $prefix, $path );
66+
\spl_autoload_register( array( $loader, 'load' ) );
67+
}
68+
69+
/**
70+
* Loads a class if its namespace starts with `$this->prefix`.
71+
*
72+
* @param string $class_name The class to be loaded.
73+
*/
74+
public function load( $class_name ) {
75+
if ( \strpos( $class_name, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
76+
return;
77+
}
78+
79+
// Strip prefix from the start (ala PSR-4).
80+
$class_name = \substr( $class_name, $this->prefix_length + 1 );
81+
$class_name = \strtolower( $class_name );
82+
$dir = '';
83+
84+
$last_ns_pos = \strripos( $class_name, self::NS_SEPARATOR );
85+
if ( false !== $last_ns_pos ) {
86+
$namespace = \substr( $class_name, 0, $last_ns_pos );
87+
$namespace = \str_replace( '_', '-', $namespace );
88+
$class_name = \substr( $class_name, $last_ns_pos + 1 );
89+
$dir = \str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
90+
}
91+
92+
$path = $this->path . $dir . 'class-' . \str_replace( '_', '-', $class_name ) . '.php';
93+
94+
if ( ! \file_exists( $path ) ) {
95+
$path = $this->path . $dir . 'interface-' . \str_replace( '_', '-', $class_name ) . '.php';
96+
}
97+
98+
if ( ! \file_exists( $path ) ) {
99+
$path = $this->path . $dir . 'trait-' . \str_replace( '_', '-', $class_name ) . '.php';
100+
}
101+
102+
if ( \file_exists( $path ) ) {
103+
require_once $path;
104+
}
105+
}
106+
}

includes/class-comment.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public static function init() {
2020
add_filter( 'template_include', array( static::class, 'comment_template_include' ) );
2121

2222
add_filter( 'get_comment_link', array( static::class, 'remote_comment_link' ), 11, 2 );
23+
24+
// Default Comment Status.
25+
add_filter( 'get_default_comment_status', 'webmention_get_default_comment_status', 11, 3 );
26+
27+
add_action( 'comment_form_after', 'webmention_comment_form', 11 );
28+
add_action( 'comment_form_comments_closed', 'webmention_comment_form' );
2329
}
2430

2531
/**
@@ -251,7 +257,7 @@ public static function comment_template_include( $template ) {
251257

252258
// replace template
253259
if ( isset( $wp_query->query['replytocom'] ) ) {
254-
return apply_filters( 'webmention_comment_template', __DIR__ . '/../templates/webmention-comment.php' );
260+
return apply_filters( 'webmention_comment_template', WEBMENTION_PLUGIN_DIR . '/templates/webmention-comment.php' );
255261
}
256262

257263
return $template;

includes/class-webmention.php

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<?php
2+
/**
3+
* Webmention Class
4+
*
5+
* @package Webmention
6+
*/
7+
8+
namespace Webmention;
9+
10+
/**
11+
* Webmention Class
12+
*
13+
* @package Webmention
14+
*/
15+
class Webmention {
16+
/**
17+
* Instance of the class.
18+
*
19+
* @var Webmention
20+
*/
21+
private static $instance;
22+
23+
/**
24+
* Text domain.
25+
*
26+
* @var string
27+
*/
28+
const TEXT_DOMAIN = 'webmention';
29+
30+
/**
31+
* Get the instance of the class.
32+
*
33+
* @return Webmention
34+
*/
35+
public static function get_instance() {
36+
if ( null === self::$instance ) {
37+
self::$instance = new self();
38+
}
39+
40+
return self::$instance;
41+
}
42+
43+
/**
44+
* Do not allow multiple instances of the class.
45+
*/
46+
private function __construct() {
47+
// Do nothing.
48+
}
49+
50+
/**
51+
* Initialize the plugin.
52+
*/
53+
public function init() {
54+
$this->register_constants();
55+
56+
$this->register_hooks();
57+
$this->register_admin_hooks();
58+
$this->add_post_type_support();
59+
$this->unregister_legacy_hooks();
60+
61+
// Load language files.
62+
load_plugin_textdomain( self::TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
63+
}
64+
65+
/**
66+
* Get the plugin version.
67+
*
68+
* @return string
69+
*/
70+
public function get_version() {
71+
return WEBMENTION_VERSION;
72+
}
73+
74+
/**
75+
* Register hooks.
76+
*/
77+
public function register_hooks() {
78+
\add_action( 'init', array( Tools::class, 'init' ) );
79+
\add_action( 'init', array( Comment::class, 'init' ) );
80+
\add_action( 'init', array( Comment_Walker::class, 'init' ) );
81+
82+
// load local avatar support.
83+
\add_action( 'init', array( Avatar::class, 'init' ) );
84+
85+
// load HTTP 410 support.
86+
\add_action( 'init', array( HTTP_Gone::class, 'init' ) );
87+
88+
// initialize Webmention Sender.
89+
\add_action( 'init', array( Sender::class, 'init' ) );
90+
91+
// initialize Webmention Receiver.
92+
\add_action( 'init', array( Receiver::class, 'init' ) );
93+
94+
// initialize Webmention Discovery.
95+
\add_action( 'init', array( Discovery::class, 'init' ) );
96+
97+
if ( site_supports_blocks() ) {
98+
// initialize Webmention Bloks.
99+
\add_action( 'init', array( Block::class, 'init' ) );
100+
}
101+
102+
// load local avatar store.
103+
if ( 1 === (int) get_option( 'webmention_avatar_store_enable', 0 ) ) {
104+
\add_action( 'init', array( Avatar_Store::class, 'init' ) );
105+
}
106+
107+
// initialize Webmention Vouch
108+
if ( WEBMENTION_VOUCH ) {
109+
\add_action( 'init', array( Vouch::class, 'init' ) );
110+
}
111+
112+
\add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
113+
114+
// remove the "webmentions_disabled" meta value if the post is updated
115+
\add_action( 'updated_postmeta', array( $this, 'updated_postmeta' ), 10, 4 );
116+
}
117+
118+
/**
119+
* Register admin hooks.
120+
*/
121+
public function register_admin_hooks() {
122+
add_action( 'admin_init', array( Admin::class, 'admin_init' ) );
123+
add_action( 'admin_menu', array( Admin::class, 'admin_menu' ) );
124+
}
125+
126+
/**
127+
* Add support for Webmentions to custom post types.
128+
*/
129+
public function add_post_type_support() {
130+
// Add support for Webmentions to custom post types.
131+
$post_types = get_option( 'webmention_support_post_types', array( 'post', 'page' ) ) ? get_option( 'webmention_support_post_types', array( 'post', 'page' ) ) : array();
132+
133+
foreach ( $post_types as $post_type ) {
134+
add_post_type_support( $post_type, 'webmentions' );
135+
}
136+
}
137+
138+
/**
139+
* Register constants.
140+
*/
141+
private function register_constants() {
142+
\defined( 'WEBMENTION_ALWAYS_SHOW_HEADERS' ) || \define( 'WEBMENTION_ALWAYS_SHOW_HEADERS', 0 );
143+
\defined( 'WEBMENTION_COMMENT_APPROVE' ) || \define( 'WEBMENTION_COMMENT_APPROVE', 0 );
144+
\defined( 'WEBMENTION_COMMENT_TYPE' ) || \define( 'WEBMENTION_COMMENT_TYPE', 'webmention' );
145+
\defined( 'WEBMENTION_GRAVATAR_CACHE_TIME' ) || \define( 'WEBMENTION_GRAVATAR_CACHE_TIME', WEEK_IN_SECONDS );
146+
147+
\defined( 'WEBMENTION_AVATAR_QUALITY' ) || \define( 'WEBMENTION_AVATAR_QUALITY', null );
148+
\defined( 'WEBMENTION_AVATAR_SIZE' ) || \define( 'WEBMENTION_AVATAR_SIZE', 256 );
149+
150+
\define( 'WEBMENTION_PROCESS_TYPE_ASYNC', 'async' );
151+
\define( 'WEBMENTION_PROCESS_TYPE_SYNC', 'sync' );
152+
153+
\defined( 'WEBMENTION_PROCESS_TYPE' ) || \define( 'WEBMENTION_PROCESS_TYPE', WEBMENTION_PROCESS_TYPE_SYNC );
154+
155+
\defined( 'WEBMENTION_VOUCH' ) || \define( 'WEBMENTION_VOUCH', false );
156+
157+
// Mentions with content less than this length will be rendered in full.
158+
\defined( 'MAX_INLINE_MENTION_LENGTH' ) || \define( 'MAX_INLINE_MENTION_LENGTH', 300 );
159+
}
160+
161+
/**
162+
* Enqueue scripts.
163+
*/
164+
public function enqueue_scripts() {
165+
if ( is_singular() ) {
166+
wp_enqueue_style( self::TEXT_DOMAIN, WEBMENTION_PLUGIN_URL . 'assets/css/webmention.css', array(), $this->get_version() );
167+
}
168+
}
169+
170+
/**
171+
* Delete the webmentions_disabled meta value if the post is updated.
172+
*
173+
* @param int $meta_id The meta ID.
174+
* @param int $object_id The object ID.
175+
* @param string $meta_key The meta key.
176+
* @param mixed $meta_value The meta value.
177+
*/
178+
public function updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) {
179+
if ( 'webmentions_disabled' === $meta_key && empty( $meta_value ) ) {
180+
\delete_post_meta( $object_id, 'webmentions_disabled' );
181+
}
182+
}
183+
184+
/**
185+
* Remove old Webmention/Semantic-Linkbacks code.
186+
*/
187+
public function unregister_legacy_hooks() {
188+
// remove old Webmention code.
189+
remove_action( 'init', array( '\WebMentionFormPlugin', 'init' ) );
190+
remove_action( 'init', array( '\WebMentionForCommentsPlugin', 'init' ) );
191+
192+
// remove old Semantic Linkbacks code
193+
remove_action( 'plugins_loaded', array( 'Semantic_Linkbacks_Plugin', 'init' ), 11 );
194+
remove_action( 'admin_init', array( 'Semantic_Linkbacks_Plugin', 'admin_init' ) );
195+
}
196+
}

0 commit comments

Comments
 (0)