Skip to content

Commit

Permalink
Scaffold update (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
screid123 authored Oct 21, 2021
1 parent f160e8e commit 5cfad9f
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 37 deletions.
18 changes: 18 additions & 0 deletions .ci/data/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ summary: |-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
unreleased: false
releases:
- name: v1.0.2
tag_name: v1.0.2
prerelease: false
link: false
date: "2021-10-21"
notice: false
added:
- Added Pimple container for dependency injection, and new `/includes/Services.php` to manage the dependencies/services.
- New `/includes/functions.php` for loading global (namespaced) functions.
- Checks to make sure the Composer dependencies loaded before initializing the plugin.
changed:
- Removed `/includes/PluginInfo.php` and static methods in favor of using `$this->plugin` methods (via [plugin awareness](https://github.com/cedaro/wp-plugin#plugin-awareness)).
- Moved `wp_localize_script()` call in the `Admin::load_assets` method to inside check a for the `/assets/admin.js` file.
deprecated: false
removed: false
fixed:
- The `gulp watch` command now watches root PHP files.
security: false
- name: v1.0.1
tag_name: v1.0.1
prerelease: false
Expand Down
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/screid123/wp-plugin-template/compare/v1.0.1...HEAD)
## [Unreleased](https://github.com/screid123/wp-plugin-template/compare/v1.0.2...HEAD)

## v1.0.2 - 2021-10-21

### 📦 Added
- Added Pimple container for dependency injection, and new `/includes/Services.php` to manage the dependencies/services.
- New `/includes/functions.php` for loading global (namespaced) functions.
- Checks to make sure the Composer dependencies loaded before initializing the plugin.

### 👌 Changed
- Removed `/includes/PluginInfo.php` and static methods in favor of using `$this->plugin` methods (via [plugin awareness](https://github.com/cedaro/wp-plugin#plugin-awareness)).
- Moved `wp_localize_script()` call in the `Admin::load_assets` method to inside check a for the `/assets/admin.js` file.

### 🐛 Fixed
- The `gulp watch` command now watches root PHP files.

## v1.0.1 - 2021-10-07

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"WP_Plugin_Template\\": "includes/"
},
"files": [
"includes/functions.php",
"lib/RV_Updater.php"
]
},
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function composerUpdate(cb) {
* Watch files and build on change.
*/
function watch() {
gulp.watch('./includes/**/*', gulp.series(composerUpdate, build));
gulp.watch(['./includes/**/*', './*.php'], gulp.series(composerUpdate, build));
gulp.watch(['./lib/**/*', './vendor/**/*'], copy);
}

Expand Down
3 changes: 3 additions & 0 deletions includes/Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*
* @package WP_Plugin_Template
*/

// Cannot `declare( strict_types=1 );` to avoid fatal if prior to PHP 7.0.0, since we did not yet verify the PHP version.

namespace WP_Plugin_Template;

/**
Expand Down
26 changes: 13 additions & 13 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*
* @package WP_Plugin_Template
*/

declare( strict_types=1 );

namespace WP_Plugin_Template;
Expand Down Expand Up @@ -110,20 +109,21 @@ protected function load_assets(): void {
$script_asset['version'],
true
);

/**
* Use `wp_localize_script` to add info for REST calls, e.g. - base URL, nonce, environment, etc.
* This creates a global on the page using a camelCased version of the plugin slug prefixed with an underscore.
* Example: wp-plugin-template -> _wpPluginTemplate
*/
$object_name = '_' . lcfirst( str_replace( ' ', '', ucwords( strtr( $this->plugin->get_slug(), '_-', ' ') ) ) );
wp_localize_script( $handle, $object_name, [
'env' => constant( 'PANTHEON_ENVIRONMENT' ) ?? 'live',
'baseUrl' => esc_url( get_rest_url( get_current_network_id() ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
// More?
] );
}

/**
* Use `wp_localize_script` to add info for REST calls, e.g. - base URL, nonce, environment, etc.
* This creates a global on the page using a camelCased version of the plugin slug prefixed with an underscore.
* Example: wp-plugin-template -> _wpPluginTemplate
*/
$object_name = '_' . lcfirst( str_replace( ' ', '', ucwords( strtr( $this->plugin->get_slug(), '_-', ' ') ) ) );
wp_localize_script( $handle, $object_name, [
'env' => constant( 'PANTHEON_ENVIRONMENT' ) ?? 'live',
'baseUrl' => esc_url( get_rest_url( get_current_network_id() ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
// More?
] );
}


Expand Down
4 changes: 2 additions & 2 deletions includes/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace WP_Plugin_Template;

use WP_Plugin_Template\Dependencies\Cedaro\WP\Plugin\PluginFactory;
use WP_Plugin_Template\Dependencies\Micropackage\Requirements\Requirements;

/**
Expand Down Expand Up @@ -72,14 +71,15 @@ public function __construct( string $entry ) {

// Create the container and register the service provider.
$this->container = new Container();
$this->container->register( new ServiceProvider() );
$this->container->register( new Services() );

// Initialize the plugin and inject the container.
$this->plugin = ( new Plugin() )
->set_basename( plugin_basename( $entry ) )
->set_container( $this->container )
->set_directory( plugin_dir_path( $entry ) )
->set_file( $entry )
->set_prefix( '{{SLUG}}' )
->set_slug( '{{SLUG}}' )
->set_url( plugin_dir_url( $entry ) );
}
Expand Down
3 changes: 3 additions & 0 deletions includes/Deactivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*
* @package WP_Plugin_Template
*/

// Cannot `declare( strict_types=1 );` to avoid fatal if prior to PHP 7.0.0, since we did not yet verify the PHP version.

namespace WP_Plugin_Template;

/**
Expand Down
1 change: 0 additions & 1 deletion includes/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*
* @package WP_Plugin_Template
*/

declare( strict_types=1 );

namespace WP_Plugin_Template;
Expand Down
12 changes: 3 additions & 9 deletions includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*
* @package WP_Plugin_Template;
*/
declare( strict_types=1 );

namespace WP_Plugin_Template;

use WP_Plugin_Template\Dependencies\Cedaro\WP\Plugin\Plugin as BasePlugin;
Expand Down Expand Up @@ -41,17 +43,9 @@ public function get_prefix(): string {
* @param string $prefix
* @return $this
*/
protected function set_prefix( string $prefix ): Plugin {
public function set_prefix( string $prefix ): Plugin {
$this->prefix = str_replace( '-', '_', sanitize_key( $prefix ) );
return $this;
}

/**
* @inheritDoc
*/
public function set_slug( $slug ): Plugin {
$this->set_prefix( $slug );
return parent::set_slug( $slug );
}

}
4 changes: 2 additions & 2 deletions includes/ServiceProvider.php → includes/Services.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* ServiceProvider class.
* Services class.
*
* @package WP_Plugin_Template
*/
Expand All @@ -16,7 +16,7 @@
*
* @package WP_Plugin_Template
*/
class ServiceProvider implements ServiceProviderInterface {
class Services implements ServiceProviderInterface {

/**
* @inheritDoc
Expand Down
26 changes: 26 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Helper functions
*
* @package WP_Plugin_Template
*/
namespace WP_Plugin_Template;

/**
* Display a notice about missing dependencies.
*/
function display_missing_dependencies_notice(): void {
printf(
'<div class="notice notice-error"><p>%s</p></div>',
wp_kses(
__( '{{NAME}} is missing required dependencies. <a href="{{URI}}" target="_blank" rel="noopener noreferer">Learn more.</a>', '{{TEXT_DOMAIN}}' ),
[
'a' => [
'href' => true,
'rel' => true,
'target' => true,
],
]
)
);
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"version": "1.0.1",
"version": "1.0.2",
"scripts": {
"build": "npm run build:php && npm run build:assets",
"build:assets": "NODE_ENV=production wp-scripts build assets/js/admin.js assets/js/frontend.js --output-path=build/assets",
Expand Down
7 changes: 7 additions & 0 deletions wp-plugin-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
require_once( __DIR__ . '/vendor/autoload.php' );
}

// Display a notice and bail if dependencies are missing.
if ( ! class_exists( __NAMESPACE__ . '\Bootstrap' ) ) {
require_once(__DIR__ . '/includes/functions.php');
add_action( 'admin_notices', __NAMESPACE__ . '\display_missing_dependencies_notice' );
return;
}

// Register activation hook.
register_activation_hook( __FILE__, [ __NAMESPACE__ . '\Activator', 'activate' ] );

Expand Down

0 comments on commit 5cfad9f

Please sign in to comment.