Skip to content

Commit

Permalink
feat: scaffold plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaPipolo committed Apr 4, 2024
0 parents commit 27ade76
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

[*.yml]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[{*.txt,wp-config-sample.php}]
end_of_line = crlf

35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Patch/diff artifacts.
*.diff
*.orig
*.rej
interdiff*.txt

# emacs artifacts.
*~
\#*\#

# VIM
*.un~
Session.vim
.netrwhist
*~
*.swp
.lvimrc

# Hidden files.
.DS*
.project
.idea/
.vscode
.history

# Windows links.
*.url
*.lnk

# logs
*.log
*/debug.log

# Tags file
tags
68 changes: 68 additions & 0 deletions mailhog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Plugin Name: somoscuatro Mailhog.
* Version: 1.0.0
* Text Domain: somoscuatro-mailhog
* Description: Simple WordPress plugin to capture emails through Mailhog.
* Author: somoscuatro
* Author URI: https://somoscuatro.es/
* License: MIT
* License URI: http://www.gnu.org/licenses/gpl-2.0
*
* @package somoscuatro-mailhog
*/

declare(strict_types=1);

namespace Somoscuatro\Mailhog;

if ( ! defined( 'ABSPATH' ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
header( $_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found' );
exit;
}

/**
* Registers plugin autoloader.
*
* @see https://www.php-fig.org/psr/psr-4/
*
* @param string $class The class name.
*/
spl_autoload_register(
function ( $class_name ) {
// Only autoload classes from this namespace.
if ( false === strpos( $class_name, __NAMESPACE__ ) ) {
return;
}

// Remove namespace from class name.
$class_file = str_replace( __NAMESPACE__ . '\\', '', $class_name );

// Convert class name format to file name format.
$class_file = strtolower( $class_file );
$class_file = str_replace( '_', '-', $class_file );

// Convert sub-namespaces into directories.
$class_path = explode( '\\', $class_file );
$class_file = array_pop( $class_path );
$class_path = implode( '/', $class_path );

$file = realpath( __DIR__ . '/src' . ( $class_path ? "/$class_path" : '' ) . '/class-' . $class_file . '.php' );

// If the file exists, require it.
if ( file_exists( $file ) ) {
require_once $file;
} else {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( sprintf( 'File not found: %s', $file ), true );
}
}
);

register_activation_hook( __FILE__, __NAMESPACE__ . '\Schema::activate' );
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\Schema::deactivate' );
register_uninstall_hook( __FILE__, __NAMESPACE__ . '\Schema::uninstall' );

add_action( 'admin_init', __NAMESPACE__ . '\Admin::init' );
add_action( 'init', __NAMESPACE__ . '\Plugin::init', 20 );
22 changes: 22 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="WordPress Coding Standards" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<description>The Coding standard for the WordPress Coding Standards itself.</description>
<ini name="error_reporting" value="E_ALL &#38; ~E_DEPRECATED" />

<file>.</file>

<arg value="sp"/>
<arg name="extensions" value="php"/>
<arg name="basepath" value="."/>
<arg name="parallel" value="8"/>

<exclude-pattern>/bin/class-ruleset-test.php</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<rule ref="WordPress"></rule>
<!-- Exclude filename conventions for Visual Composer templates -->
<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase">
<exclude-pattern>*/vc_templates/*</exclude-pattern>
</rule>

</ruleset>
67 changes: 67 additions & 0 deletions src/class-admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Contains \Somoscuatro\Mailhog\Admin.
*
* @package somoscuatro-mailhog
*/

declare(strict_types=1);

namespace Somoscuatro\Mailhog;

/**
* Administrative back-end functionality.
*/
class Admin {

/**
* Initializes the admin functionality.
*/
public static function init(): void {
// Displays warnings about conflicting plugins.
add_action( 'admin_notices', __CLASS__ . '::display_missing_plugins_warning' );
}

/**
* Displays warnings about conflicting plugins.
*/
public static function display_missing_plugins_warning(): void {
$wp_mail_smtp_active = is_plugin_active( 'wp-mail-smtp/wp_mail_smtp.php' );
$disable_emails_active = is_plugin_active( 'disable-emails/disable-emails.php' );

if ( $wp_mail_smtp_active || $disable_emails_active ) :
?>
<div class="error">
<?php if ( $wp_mail_smtp_active ) : ?>
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s plugin name and link to plugin vendor page. */
__( 'Plugin %s is active. You need to deactivate it in order to use the mailhog plugin.', 'somoscuatro-mailhog' ),
'<a href="https://wpmailsmtp.com/" target="_blank">WP Mail SMTP</a>'
)
);
?>
</p>
<?php endif; ?>

<?php if ( $disable_emails_active ) : ?>
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s plugin name and link to plugin vendor page. */
__( 'Plugin %s is active. You need to deactivate it in order to use the mailhog plugin.', 'somoscuatro-mailhog' ),
'<a href="https://wordpress.org/plugins/disable-emails/" target="_blank">Disable Emails</a>'
)
);
?>
</p>
<?php endif; ?>
</div>
<?php
endif;
}

}
40 changes: 40 additions & 0 deletions src/class-plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Contains \Somoscuatro\Mailhog\Plugin.
*
* @package somoscuatro-mailhog
*/

declare(strict_types=1);

namespace Somoscuatro\Mailhog;

/**
* Administrative back-end functionality.
*/
class Plugin {

/**
* Plugin frontend initialization method.
*
* @implements init
*/
public static function init() {
add_filter(
'phpmailer_init',
function( $php_mailer ) {
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$php_mailer->Host = 'mailhog';
$php_mailer->Port = 1025;
$php_mailer->From = '[email protected]';
$php_mailer->FromName = 'WordPress';
$php_mailer->IsSMTP();
// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
},
10
);
add_filter( 'wp_mail_from', fn() => '[email protected]' );
add_filter( 'wp_mail_from_name', fn() => 'localhost' );
}

}
38 changes: 38 additions & 0 deletions src/class-schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Generic plugin lifetime and maintenance functionality.
*
* @package somoscuatro-mailhog
*/

declare(strict_types=1);

namespace Somoscuatro\Mailhog;

/**
* Generic plugin lifetime and maintenance functionality.
*/
class Schema {

/**
* Registers activation hook callback.
*
* @implements register_activation_hook
*/
public static function activate() { }

/**
* Registers deactivation hook callback.
*
* @implements register_deactivation_hook
*/
public static function deactivate() { }

/**
* Registers uninstall hook callback.
*
* @implements register_uninstall_hook
*/
public static function uninstall() { }

}

0 comments on commit 27ade76

Please sign in to comment.