Skip to content

Commit e02b874

Browse files
authored
Merge pull request #509 from pfefferle/improve/plugin-loader
Improve and simplify the plugin loader
2 parents f8dfc39 + 3dd46e5 commit e02b874

File tree

9 files changed

+492
-223
lines changed

9 files changed

+492
-223
lines changed

includes/class-admin.php

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public static function meta_boxes( $object, $box ) {
6363
/**
6464
* Add comment-type as column in WP-Admin
6565
*
66-
* @param array $column the column to implement
67-
* @param int $comment_id the comment id
66+
* @param array $column The column to implement.
67+
* @param int $comment_id The comment id.
6868
*/
6969
public static function manage_comments_custom_column( $column, $comment_id ) {
7070
if ( 'comment_type' !== $column ) {
@@ -74,16 +74,25 @@ public static function manage_comments_custom_column( $column, $comment_id ) {
7474
}
7575

7676
/**
77-
* Add bulk option to bulk comment handler
77+
* Add bulk option to bulk comment handler.
78+
*
79+
* @param array $bulk_actions The bulk actions.
80+
*
81+
* @return array The bulk actions.
7882
*/
7983
public static function bulk_comment_actions( $bulk_actions ) {
8084
$bulk_actions['refresh_webmention'] = __( 'Refresh Webmention', 'webmention' );
8185
return $bulk_actions;
8286
}
8387

8488
/**
85-
* Add bulk action handler to comments
89+
* Add bulk action handler to comments.
90+
*
91+
* @param string $redirect_to The redirect URL.
92+
* @param string $doaction The action to perform.
93+
* @param array $comment_ids The comment IDs.
8694
*
95+
* @return string The redirect URL.
8796
*/
8897
public static function bulk_comment_action_handler( $redirect_to, $doaction, $comment_ids ) {
8998
if ( 'refresh_webmention' !== $doaction ) {
@@ -98,12 +107,12 @@ public static function bulk_comment_action_handler( $redirect_to, $doaction, $co
98107
}
99108

100109
/**
101-
* Add an action link
110+
* Add an action link.
102111
*
103-
* @param array $links the settings links
104-
* @param string $file the plugin filename
112+
* @param array $links The settings links.
113+
* @param string $file The plugin filename.
105114
*
106-
* @return array the filtered array
115+
* @return array The filtered array.
107116
*/
108117
public static function plugin_action_links( $links, $file ) {
109118
if ( stripos( $file, 'webmention' ) === false || ! function_exists( 'admin_url' ) ) {
@@ -120,12 +129,12 @@ public static function plugin_action_links( $links, $file ) {
120129
}
121130

122131
/**
123-
* Add a plugin meta link
132+
* Add a plugin meta link.
124133
*
125-
* @param array $links the settings links
126-
* @param string $file the plugin filename
134+
* @param array $links The settings links.
135+
* @param string $file The plugin filename.
127136
*
128-
* @return array the filtered array
137+
* @return array The filtered array.
129138
*/
130139
public static function plugin_row_meta( $links, $file ) {
131140
if ( stripos( $file, 'webmention' ) === false || ! function_exists( 'admin_url' ) ) {
@@ -175,16 +184,26 @@ public static function comment_types_dropdown( $types ) {
175184
}
176185

177186
/**
178-
* Add comment-type as column in WP-Admin
187+
* Add comment-type as column in WP-Admin.
188+
*
189+
* @param array $columns The list of column names.
179190
*
180-
* @param array $columns the list of column names
191+
* @return array The filtered columns.
181192
*/
182193
public static function comment_columns( $columns ) {
183194
$columns['comment_type'] = esc_html__( 'Comment-Type', 'webmention' );
184195

185196
return $columns;
186197
}
187198

199+
/**
200+
* Add comment-type as column in WP-Admin.
201+
*
202+
* @param array $actions The actions.
203+
* @param object $comment The comment object.
204+
*
205+
* @return array The filtered actions.
206+
*/
188207
public static function comment_row_actions( $actions, $comment ) {
189208
$query = array(
190209
'_wpnonce' => wp_create_nonce( "approve-comment_$comment->comment_ID" ),
@@ -213,6 +232,11 @@ public static function comment_row_actions( $actions, $comment ) {
213232
return $actions;
214233
}
215234

235+
/**
236+
* Add a webmention approve domain.
237+
*
238+
* @param string $host The host.
239+
*/
216240
public static function add_webmention_approve_domain( $host ) {
217241
$approvelist = get_webmention_approve_domains();
218242
$approvelist[] = $host;
@@ -221,6 +245,11 @@ public static function add_webmention_approve_domain( $host ) {
221245
update_option( 'webmention_approve_domains', $approvelist );
222246
}
223247

248+
/**
249+
* Transition to approve list.
250+
*
251+
* @param object $comment The comment object.
252+
*/
224253
public static function transition_to_approvelist( $comment ) {
225254
if ( ! current_user_can( 'moderate_comments' ) ) {
226255
return;
@@ -267,9 +296,7 @@ public static function admin_menu() {
267296
* Load settings page
268297
*/
269298
public static function settings_page() {
270-
require_once __DIR__ . '/class-db.php';
271-
\Webmention\DB::update_database();
272-
\Webmention\remove_semantic_linkbacks();
299+
Upgrade::maybe_upgrade();
273300

274301
add_thickbox();
275302
wp_enqueue_script( 'plugin-install' );
@@ -326,6 +353,9 @@ public static function add_help_tab() {
326353
);
327354
}
328355

356+
/**
357+
* Register settings.
358+
*/
329359
public static function register_settings() {
330360
register_setting(
331361
'webmention',
@@ -450,7 +480,7 @@ public static function register_settings() {
450480
}
451481

452482
/**
453-
* Add recommended privacy content
483+
* Add recommended privacy content.
454484
*/
455485
public static function add_privacy_policy_content() {
456486
$content =

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-cli.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,7 @@ public function generate( $args, $assoc_args ) {
315315
* @return void
316316
*/
317317
public function db_migration( $args, $assoc_args ) {
318-
require_once __DIR__ . '/class-db.php';
319-
320-
DB::update_database();
318+
Upgrade::maybe_upgrade();
321319

322320
WP_CLI::success( __( 'DB Migration finished', 'webmention' ) );
323321
}

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;

0 commit comments

Comments
 (0)