Skip to content

Commit ed0b53e

Browse files
authored
Merge pull request #295 from indieweb/feature/namespacing
2 parents 7780418 + 0eabb4c commit ed0b53e

12 files changed

+983
-837
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 Indieweb.
4+
*
5+
* @package Indieweb
6+
*/
7+
8+
namespace Indieweb;
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-general-settings.php

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
<?php
22
/**
3-
* IndieWeb General Settings.
3+
* Indieweb General Settings.
44
*
5-
* @package IndieWeb
5+
* @package Indieweb
66
*/
77

8-
add_action( 'admin_menu', array( 'IndieWeb_General_Settings', 'admin_menu' ) );
9-
add_action( 'init', array( 'IndieWeb_General_Settings', 'register_settings' ) );
10-
add_action( 'admin_menu', array( 'IndieWeb_General_Settings', 'admin_settings' ), 11 );
8+
namespace Indieweb;
119

1210
/**
13-
* General Settings class for IndieWeb plugin.
11+
* General Settings class for Indieweb plugin.
1412
*/
15-
class IndieWeb_General_Settings {
13+
class General_Settings {
1614

1715
/**
1816
* Add admin menu item.
1917
*/
2018
public static function admin_menu() {
2119
$page = 'iw_general_options';
2220
// Add General Options Page.
23-
add_submenu_page(
21+
\add_submenu_page(
2422
'indieweb',
25-
__( 'Options', 'indieweb' ), // Page title.
26-
__( 'Options', 'indieweb' ), // Menu title.
23+
\__( 'Options', 'indieweb' ), // Page title.
24+
\__( 'Options', 'indieweb' ), // Menu title.
2725
'manage_options', // Access capability.
2826
$page,
29-
array( 'IndieWeb_General_Settings', 'general_options_page' )
27+
array( self::class, 'general_options_page' )
3028
);
3129
}
3230

@@ -35,46 +33,46 @@ public static function admin_menu() {
3533
*/
3634
public static function register_settings() {
3735
$section = 'iw_identity_settings';
38-
register_setting(
36+
\register_setting(
3937
$section,
4038
'iw_single_author',
4139
array(
4240
'type' => 'boolean',
43-
'description' => __( 'Single Author Site', 'indieweb' ),
41+
'description' => \__( 'Single Author Site', 'indieweb' ),
4442
'show_in_rest' => true,
45-
'default' => is_multi_author() ? 0 : 1,
43+
'default' => \is_multi_author() ? 0 : 1,
4644
)
4745
);
4846

4947
// Set Default Author.
50-
register_setting(
48+
\register_setting(
5149
$section,
5250
'iw_default_author',
5351
array(
5452
'type' => 'integer',
55-
'description' => __( 'Default Author ID for this Site', 'indieweb' ),
53+
'description' => \__( 'Default Author ID for this Site', 'indieweb' ),
5654
'show_in_rest' => true,
5755
'default' => 1,
5856
)
5957
);
6058

61-
register_setting(
59+
\register_setting(
6260
$section,
6361
'iw_author_url',
6462
array(
6563
'type' => 'boolean',
66-
'description' => __( 'Replace Author URL with User Website URL', 'indieweb' ),
64+
'description' => \__( 'Replace Author URL with User Website URL', 'indieweb' ),
6765
'show_in_rest' => true,
6866
'default' => 1,
6967
)
7068
);
7169

72-
register_setting(
70+
\register_setting(
7371
$section,
7472
'iw_relme_bw',
7573
array(
7674
'type' => 'boolean',
77-
'description' => __( 'Black and White Rel-Me Icons', 'indieweb' ),
75+
'description' => \__( 'Black and White Rel-Me Icons', 'indieweb' ),
7876
'show_in_rest' => true,
7977
'default' => 0,
8078
)
@@ -89,56 +87,56 @@ public static function admin_settings() {
8987
// Settings Section.
9088
$section = 'iw_identity_settings';
9189

92-
add_settings_section(
90+
\add_settings_section(
9391
$section, // ID used to identify this section and with which to register options.
94-
__( 'Identity Settings', 'indieweb' ), // Title to be displayed on the administration page.
95-
array( 'IndieWeb_General_Settings', 'identity_options_callback' ), // Callback used to render the description of the section.
92+
\__( 'Identity Settings', 'indieweb' ), // Title to be displayed on the administration page.
93+
array( self::class, 'identity_options_callback' ), // Callback used to render the description of the section.
9694
$page // Page on which to add this section of options.
9795
);
9896

99-
add_settings_field(
97+
\add_settings_field(
10098
'iw_single_author', // ID used to identify the field throughout the theme.
10199
'Single Author Site', // The label to the left of the option interface element.
102-
array( 'IndieWeb_General_Settings', 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
100+
array( self::class, 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
103101
$page, // The page on which this option will be displayed.
104102
$section, // The name of the section to which this field belongs.
105103
array( // The array of arguments to pass to the callback. In this case, just a description.
106104
'name' => 'iw_single_author',
107-
'description' => __( 'If this website represents a single individual or entity, check this. This setting is disabled if you only have one user who has made a post.', 'indieweb' ),
108-
'disabled' => ! is_multi_author(),
105+
'description' => \__( 'If this website represents a single individual or entity, check this. This setting is disabled if you only have one user who has made a post.', 'indieweb' ),
106+
'disabled' => ! \is_multi_author(),
109107
)
110108
);
111109

112-
add_settings_field(
110+
\add_settings_field(
113111
'iw_default_author', // ID used to identify the field throughout the theme.
114112
'Default Author', // The label to the left of the option interface element.
115-
array( 'IndieWeb_General_Settings', 'default_author_callback' ), // The name of the function responsible for rendering the option interface.
113+
array( self::class, 'default_author_callback' ), // The name of the function responsible for rendering the option interface.
116114
$page, // The page on which this option will be displayed.
117115
$section // The name of the section to which this field belongs.
118116
);
119117

120-
add_settings_field(
118+
\add_settings_field(
121119
'iw_author_url', // ID used to identify the field throughout the theme.
122-
__( 'Use User Website URL for Author', 'indieweb' ), // The label to the left of the option interface element.
123-
array( 'IndieWeb_General_Settings', 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
120+
\__( 'Use User Website URL for Author', 'indieweb' ), // The label to the left of the option interface element.
121+
array( self::class, 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
124122
$page, // The page on which this option will be displayed.
125123
$section, // The name of the section to which this field belongs.
126124
array( // The array of arguments to pass to the callback. In this case, just a description.
127125
'name' => 'iw_author_url',
128-
'description' => __( 'If checked, this will replace the author page URL with the website URL from your user profile.', 'indieweb' ),
126+
'description' => \__( 'If checked, this will replace the author page URL with the website URL from your user profile.', 'indieweb' ),
129127
'disabled' => false,
130128
)
131129
);
132130

133-
add_settings_field(
131+
\add_settings_field(
134132
'iw_relme_bw', // ID used to identify the field throughout the theme.
135-
__( 'Black and White Icons', 'indieweb' ), // The label to the left of the option interface element.
136-
array( 'IndieWeb_General_Settings', 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
133+
\__( 'Black and White Icons', 'indieweb' ), // The label to the left of the option interface element.
134+
array( self::class, 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
137135
$page, // The page on which this option will be displayed.
138136
$section, // The name of the section to which this field belongs.
139137
array( // The array of arguments to pass to the callback. In this case, just a description.
140138
'name' => 'iw_relme_bw',
141-
'description' => __( 'If checked, the icon colors will not be loaded', 'indieweb' ),
139+
'description' => \__( 'If checked, the icon colors will not be loaded', 'indieweb' ),
142140
'disabled' => false,
143141
)
144142
);
@@ -150,15 +148,15 @@ public static function admin_settings() {
150148
*/
151149
public static function identity_options_callback() {
152150
echo '<p>';
153-
esc_html_e(
151+
\esc_html_e(
154152
'Using rel=me on a link indicates the link represents the same person or entity as
155153
the current page. On a site with a single author, links to other profiles from their user profile will
156154
appear on the homepage. On a site with multiple authors these links will appear on the author page only.',
157155
'indieweb'
158156
);
159157
echo '</p>';
160158
echo '<p>';
161-
esc_html_e(
159+
\esc_html_e(
162160
'The Default Author is the one whose that will be used on the home pages and archive pages. If the single author setting is not set,
163161
on all other pages, the post author links will be used. To display the links, add the
164162
widget, otherwise they will remain hidden. ',
@@ -172,17 +170,17 @@ public static function identity_options_callback() {
172170
*/
173171
public static function general_options_page() {
174172
// If this is not a multi-author site, remove the single author setting.
175-
if ( ! is_multi_author() ) {
176-
delete_option( 'iw_single_author' );
173+
if ( ! \is_multi_author() ) {
174+
\delete_option( 'iw_single_author' );
177175
}
178176

179177
echo '<div class="wrap">';
180178
echo ' <form method="post" action="options.php">';
181179

182-
settings_fields( 'iw_identity_settings' );
183-
do_settings_sections( 'iw_general_options' );
180+
\settings_fields( 'iw_identity_settings' );
181+
\do_settings_sections( 'iw_general_options' );
184182

185-
submit_button();
183+
\submit_button();
186184

187185
echo ' </form>';
188186
echo '</div>';
@@ -194,38 +192,38 @@ public static function general_options_page() {
194192
* @param array $args Field arguments.
195193
*/
196194
public static function checkbox_callback( array $args ) {
197-
$option = get_option( $args['name'] );
195+
$option = \get_option( $args['name'] );
198196
$disabled = isset( $args['disabled'] ) ? $args['disabled'] : false;
199197

200198
$checked = $option;
201199

202-
echo "<input name='" . esc_html( $args['name'] ) . "' type='hidden' value='0' />";
203-
echo "<input name='" . esc_html( $args['name'] ) . "' type='checkbox' value='1' " . checked( $checked, 1, false ) . ( $disabled ? ' disabled ' : ' ' ) . '/> ';
200+
echo "<input name='" . \esc_html( $args['name'] ) . "' type='hidden' value='0' />";
201+
echo "<input name='" . \esc_html( $args['name'] ) . "' type='checkbox' value='1' " . \checked( $checked, 1, false ) . ( $disabled ? ' disabled ' : ' ' ) . '/> ';
204202

205203
if ( array_key_exists( 'description', $args ) ) {
206-
echo '<label for="' . esc_html( $args['name'] ) . '">' . esc_html( $args['description'] ) . '</label>';
204+
echo '<label for="' . \esc_html( $args['name'] ) . '">' . \esc_html( $args['description'] ) . '</label>';
207205
}
208206
}
209207

210208
/**
211209
* Render the default author dropdown.
212210
*/
213211
public static function default_author_callback() {
214-
$users = get_users(
212+
$users = \get_users(
215213
array(
216214
'orderby' => 'ID',
217215
'fields' => array( 'ID', 'display_name' ),
218216
)
219217
);
220218

221-
$option = get_option( 'iw_default_author' );
219+
$option = \get_option( 'iw_default_author' );
222220
?>
223221

224222
<select name="iw_default_author">
225223
<?php foreach ( $users as $user ) : ?>
226-
<option value="<?php echo absint( $user->ID ); ?>" <?php selected( $option, $user->ID ); ?>>
224+
<option value="<?php echo \absint( $user->ID ); ?>" <?php \selected( $option, $user->ID ); ?>>
227225
<?php
228-
echo esc_html( $user->display_name );
226+
echo \esc_html( $user->display_name );
229227
?>
230228
</option>
231229
<?php endforeach; ?>

0 commit comments

Comments
 (0)