Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions includes/class-autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Autoloader for Indieweb.
*
* @package Indieweb
*/

namespace Indieweb;

/**
* An Autoloader that respects WordPress's filename standards.
*/
class Autoloader {

/**
* Namespace separator.
*/
const NS_SEPARATOR = '\\';

/**
* The prefix to compare classes against.
*
* @var string
* @access protected
*/
protected $prefix;

/**
* Length of the prefix string.
*
* @var int
* @access protected
*/
protected $prefix_length;

/**
* Path to the file to be loaded.
*
* @var string
* @access protected
*/
protected $path;

/**
* Constructor.
*
* @param string $prefix Namespace prefix all classes have in common.
* @param string $path Path to the files to be loaded.
*/
public function __construct( $prefix, $path ) {
$this->prefix = $prefix;
$this->prefix_length = \strlen( $prefix );
$this->path = \rtrim( $path . '/' );
}

/**
* Registers Autoloader's autoload function.
*
* @throws \Exception When autoload_function cannot be registered.
*
* @param string $prefix Namespace prefix all classes have in common.
* @param string $path Path to the files to be loaded.
*/
public static function register_path( $prefix, $path ) {
$loader = new self( $prefix, $path );
\spl_autoload_register( array( $loader, 'load' ) );
}

/**
* Loads a class if its namespace starts with `$this->prefix`.
*
* @param string $class_name The class to be loaded.
*/
public function load( $class_name ) {
if ( \strpos( $class_name, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
return;
}

// Strip prefix from the start (ala PSR-4).
$class_name = \substr( $class_name, $this->prefix_length + 1 );
$class_name = \strtolower( $class_name );
$dir = '';

$last_ns_pos = \strripos( $class_name, self::NS_SEPARATOR );
if ( false !== $last_ns_pos ) {
$namespace = \substr( $class_name, 0, $last_ns_pos );
$namespace = \str_replace( '_', '-', $namespace );
$class_name = \substr( $class_name, $last_ns_pos + 1 );
$dir = \str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
}

$path = $this->path . $dir . 'class-' . \str_replace( '_', '-', $class_name ) . '.php';

if ( ! \file_exists( $path ) ) {
$path = $this->path . $dir . 'interface-' . \str_replace( '_', '-', $class_name ) . '.php';
}

if ( ! \file_exists( $path ) ) {
$path = $this->path . $dir . 'trait-' . \str_replace( '_', '-', $class_name ) . '.php';
}

if ( \file_exists( $path ) ) {
require_once $path;
}
}
}
102 changes: 50 additions & 52 deletions includes/class-general-settings.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
<?php
/**
* IndieWeb General Settings.
* Indieweb General Settings.
*
* @package IndieWeb
* @package Indieweb
*/

add_action( 'admin_menu', array( 'IndieWeb_General_Settings', 'admin_menu' ) );
add_action( 'init', array( 'IndieWeb_General_Settings', 'register_settings' ) );
add_action( 'admin_menu', array( 'IndieWeb_General_Settings', 'admin_settings' ), 11 );
namespace Indieweb;

/**
* General Settings class for IndieWeb plugin.
* General Settings class for Indieweb plugin.
*/
class IndieWeb_General_Settings {
class General_Settings {

/**
* Add admin menu item.
*/
public static function admin_menu() {
$page = 'iw_general_options';
// Add General Options Page.
add_submenu_page(
\add_submenu_page(
'indieweb',
__( 'Options', 'indieweb' ), // Page title.
__( 'Options', 'indieweb' ), // Menu title.
\__( 'Options', 'indieweb' ), // Page title.
\__( 'Options', 'indieweb' ), // Menu title.
'manage_options', // Access capability.
$page,
array( 'IndieWeb_General_Settings', 'general_options_page' )
array( self::class, 'general_options_page' )
);
}

Expand All @@ -35,46 +33,46 @@ public static function admin_menu() {
*/
public static function register_settings() {
$section = 'iw_identity_settings';
register_setting(
\register_setting(
$section,
'iw_single_author',
array(
'type' => 'boolean',
'description' => __( 'Single Author Site', 'indieweb' ),
'description' => \__( 'Single Author Site', 'indieweb' ),
'show_in_rest' => true,
'default' => is_multi_author() ? 0 : 1,
'default' => \is_multi_author() ? 0 : 1,
)
);

// Set Default Author.
register_setting(
\register_setting(
$section,
'iw_default_author',
array(
'type' => 'integer',
'description' => __( 'Default Author ID for this Site', 'indieweb' ),
'description' => \__( 'Default Author ID for this Site', 'indieweb' ),
'show_in_rest' => true,
'default' => 1,
)
);

register_setting(
\register_setting(
$section,
'iw_author_url',
array(
'type' => 'boolean',
'description' => __( 'Replace Author URL with User Website URL', 'indieweb' ),
'description' => \__( 'Replace Author URL with User Website URL', 'indieweb' ),
'show_in_rest' => true,
'default' => 1,
)
);

register_setting(
\register_setting(
$section,
'iw_relme_bw',
array(
'type' => 'boolean',
'description' => __( 'Black and White Rel-Me Icons', 'indieweb' ),
'description' => \__( 'Black and White Rel-Me Icons', 'indieweb' ),
'show_in_rest' => true,
'default' => 0,
)
Expand All @@ -89,56 +87,56 @@ public static function admin_settings() {
// Settings Section.
$section = 'iw_identity_settings';

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

add_settings_field(
\add_settings_field(
'iw_single_author', // ID used to identify the field throughout the theme.
'Single Author Site', // The label to the left of the option interface element.
array( 'IndieWeb_General_Settings', 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
array( self::class, 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
$page, // The page on which this option will be displayed.
$section, // The name of the section to which this field belongs.
array( // The array of arguments to pass to the callback. In this case, just a description.
'name' => 'iw_single_author',
'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' ),
'disabled' => ! is_multi_author(),
'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' ),
'disabled' => ! \is_multi_author(),
)
);

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

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

add_settings_field(
\add_settings_field(
'iw_relme_bw', // ID used to identify the field throughout the theme.
__( 'Black and White Icons', 'indieweb' ), // The label to the left of the option interface element.
array( 'IndieWeb_General_Settings', 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
\__( 'Black and White Icons', 'indieweb' ), // The label to the left of the option interface element.
array( self::class, 'checkbox_callback' ), // The name of the function responsible for rendering the option interface.
$page, // The page on which this option will be displayed.
$section, // The name of the section to which this field belongs.
array( // The array of arguments to pass to the callback. In this case, just a description.
'name' => 'iw_relme_bw',
'description' => __( 'If checked, the icon colors will not be loaded', 'indieweb' ),
'description' => \__( 'If checked, the icon colors will not be loaded', 'indieweb' ),
'disabled' => false,
)
);
Expand All @@ -150,15 +148,15 @@ public static function admin_settings() {
*/
public static function identity_options_callback() {
echo '<p>';
esc_html_e(
\esc_html_e(
'Using rel=me on a link indicates the link represents the same person or entity as
the current page. On a site with a single author, links to other profiles from their user profile will
appear on the homepage. On a site with multiple authors these links will appear on the author page only.',
'indieweb'
);
echo '</p>';
echo '<p>';
esc_html_e(
\esc_html_e(
'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,
on all other pages, the post author links will be used. To display the links, add the
widget, otherwise they will remain hidden. ',
Expand All @@ -172,17 +170,17 @@ public static function identity_options_callback() {
*/
public static function general_options_page() {
// If this is not a multi-author site, remove the single author setting.
if ( ! is_multi_author() ) {
delete_option( 'iw_single_author' );
if ( ! \is_multi_author() ) {
\delete_option( 'iw_single_author' );
}

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

settings_fields( 'iw_identity_settings' );
do_settings_sections( 'iw_general_options' );
\settings_fields( 'iw_identity_settings' );
\do_settings_sections( 'iw_general_options' );

submit_button();
\submit_button();

echo ' </form>';
echo '</div>';
Expand All @@ -194,38 +192,38 @@ public static function general_options_page() {
* @param array $args Field arguments.
*/
public static function checkbox_callback( array $args ) {
$option = get_option( $args['name'] );
$option = \get_option( $args['name'] );
$disabled = isset( $args['disabled'] ) ? $args['disabled'] : false;

$checked = $option;

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

if ( array_key_exists( 'description', $args ) ) {
echo '<label for="' . esc_html( $args['name'] ) . '">' . esc_html( $args['description'] ) . '</label>';
echo '<label for="' . \esc_html( $args['name'] ) . '">' . \esc_html( $args['description'] ) . '</label>';
}
}

/**
* Render the default author dropdown.
*/
public static function default_author_callback() {
$users = get_users(
$users = \get_users(
array(
'orderby' => 'ID',
'fields' => array( 'ID', 'display_name' ),
)
);

$option = get_option( 'iw_default_author' );
$option = \get_option( 'iw_default_author' );
?>

<select name="iw_default_author">
<?php foreach ( $users as $user ) : ?>
<option value="<?php echo absint( $user->ID ); ?>" <?php selected( $option, $user->ID ); ?>>
<option value="<?php echo \absint( $user->ID ); ?>" <?php \selected( $option, $user->ID ); ?>>
<?php
echo esc_html( $user->display_name );
echo \esc_html( $user->display_name );
?>
</option>
<?php endforeach; ?>
Expand Down
Loading