Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
A great starting point.
Browse files Browse the repository at this point in the history
  • Loading branch information
aubreypwd committed Oct 14, 2017
0 parents commit 4cb601a
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
1 change: 1 addition & 0 deletions assets/css/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
1 change: 1 addition & 0 deletions assets/js/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
5 changes: 5 additions & 0 deletions includes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Add a new App class

To add a new class, simply create your file in this directory,
e.g. `class-something.php` and now when you attach it to `App` it will
be auto-loaded.
160 changes: 160 additions & 0 deletions includes/class-app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
/**
* Main Application Instance.
*
* @since Unknown
* @package Company/Package
*/

namespace Company\Package;
use Exception;

/**
* Application Loader.
*
* Everything starts here. If you create a new class,
* attach it to this class.
*
* @author Unknown
* @since Unknown
*/
class App {

/**
* Plugin basename.
*
* @author Unknown
* @var string
* @since Unknown
*/
protected $basename = '';

/**
* URL of plugin directory.
*
* @author Unknown
* @var string
* @since Unknown
*/
protected $url = '';

/**
* Path of plugin directory.
*
* @author Unknown
* @var string
* @since Unknown
*/
protected $path = '';

/**
* Construct.
*
* @author Unknown
* @since Unknown
*
* @param string $plugin_file The plugin file, usually __FILE__ of the base plugin.
*
* @throws Exception If $plugin_file parameter is invalid (prevents plugin from loading).
*/
public function __construct( $plugin_file ) {

// Check input validity.
if ( empty( $plugin_file ) || ! stream_resolve_include_path( $plugin_file ) ) {

// Translators: Displays a message if a plugin file is not passed.
throw new Exception( sprintf( esc_html__( 'Invalid plugin file %1$s supplied to %2$s', 'company-package' ), $plugin_file, __METHOD__ ) );
}

// Plugin setup.
$this->basename = plugin_basename( $plugin_file );
$this->url = plugin_dir_url( $plugin_file );
$this->path = plugin_dir_path( $plugin_file );

// Loaders.
$this->auto_loader();
$this->attach();
}

/**
* Register the autoloader.
*
* @since Unknown
* @author Unknown
*/
private function auto_loader() {

// Register our autoloader.
spl_autoload_register( array( $this, 'autoload' ) );
}

/**
* Require classes.
*
* @author Unknown
* @since Unknown
*
* @param string $class_name Fully qualified name of class to try and load.
*
* @return void Early exit if we can't load the class.
*/
public function autoload( $class_name ) {

// If our class doesn't have our namespace, don't load it.
if ( 0 !== strpos( $class_name, 'Company\\Package\\' ) ) {
return;
}

$parts = explode( '\\', $class_name );

// Include our file.
$includes_dir = trailingslashit( $this->path ) . 'includes/';
$file = 'class-' . strtolower( end( $parts ) ) . '.php';

if ( stream_resolve_include_path( $includes_dir . $file ) ) {
require_once $includes_dir . $file;
}
}

/**
* Load and attach app elements to the app class.
*
* Make your classes/element small and do only one thing. If you
* need to pass $this to it so you can access other classes
* functionality.
*
* When you add something that gets attached
*
* @author Unknown
* @since Unknown
*/
private function attach() {
$this->shared = new Shared();
}

/**
* This plugin's url.
*
* @author Unknown
* @since Unknown
*
* @param string $path (Optional) appended path.
* @return string URL and path.
*/
public function url( $path = '' ) {
return is_string( $path ) && ! empty( $path ) ?
trailingslashit( $this->url ) . $path :
trailingslashit( $this->url );
}

/**
* Re-attribute user content to site author.
*
* @author Unknown
* @author Unknown
*
* @since Unknown
*/
public function deactivate_plugin() {
}
}
18 changes: 18 additions & 0 deletions includes/class-shared.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Shared functionality.
*
* @since Unknown
* @package Company/Package
*/

namespace Company\Package;

/**
* Shared functionality between several areas.
*
* @author Unknown
* @since Unknown
*/
class Shared {
}
1 change: 1 addition & 0 deletions includes/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
62 changes: 62 additions & 0 deletions plugin-name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Plugin Name:
* Plugin URI:
* Description:
* Version:
* Author:
* Author URI:
* License:
* License URI:
* Text Domain:
* Network:
*
* @since Unknown
* @package Company/Package
*/

// Our namespace.
namespace Company\Package;

// Classes outside our namespace.
use Exception;

// Require the App class.
require_once 'includes/class-app.php';

/**
* Helper function to access the application instance for the Client Plugin.
*
* @author Unknown
* @since Unknown
*
* @return App|null App if success, null if exception caught (error will be logged).
*/
function app() {
static $app;

if ( ! $app instanceof App ) {

// Start the app.
try {
$app = new App( __FILE__ );
} catch ( Exception $e ) {

// Catch any errors and log them if debugging is enabled.
if ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) {

// @codingStandardsIgnoreLine Conditionally debug.
error_log( $e->getMessage() );
}

// Return null so no further action can take place.
return null;
}
}

return $app;
}
app(); // Initialize the app.

// When we deactivate this plugin...
register_deactivation_hook( __FILE__, array( app(), 'deactivate_plugin' ) );
12 changes: 12 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# BEFORE TESTS EXECUTION
Run this command:
`bash tests/install-wp-tests.sh wordpress_test root '' localhost latest`

Replace `root` with the username of your database and replace '' with the database password. Also replace `localhost` with the hostname of your database. You can find all three of these values in your wp-config.php file.

You can now type `phpunit` into the command line and the unit tests will run.

Please note, you also might need to modify the path(s) in bootstrap.php with correct path for your WordPress location, it's a known phpunit issue related to git mirroring.

# HOW TO RUN TESTS
1. Go to plugin folder and execute `phpunit` command. It should start unit testing process and run SampleTest which would return true.
25 changes: 25 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* PHPUnit bootstrap file
*
* @package Company\Package
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
}

// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';

/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/plugin-name.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
1 change: 1 addition & 0 deletions tests/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Loading

0 comments on commit 4cb601a

Please sign in to comment.