Skip to content

Commit

Permalink
Better dependency injection (#378)
Browse files Browse the repository at this point in the history
* Update the DI logic for the plugin

* Dependency Injection in the theme

* Rephrase the namespace

* Remove function prefix

as we are already namespaced

* Remove function prefix

because namespacing

* Use the same container for theme & plugin

* fix tests

* Fix theme namespacing

* pass the tests

* Remove QM debugging
  • Loading branch information
Luehrsen authored Oct 9, 2023
1 parent c90bd43 commit db80ada
Show file tree
Hide file tree
Showing 48 changed files with 321 additions and 395 deletions.
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Thank you Human Made!
-->
<exclude name="WordPress.PHP.YodaConditions" />
<exclude name="WordPress.NamingConventions.ValidHookName.UseUnderscores" />
</rule>
<rule ref="WordPress-Docs"/>

Expand Down
2 changes: 1 addition & 1 deletion plugin/acf-json/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Silence is golden.
*
* @package lhpbpp
* @package lhpbp\plugin
*/

return;
2 changes: 1 addition & 1 deletion plugin/blocks/acf/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @link https://www.advancedcustomfields.com/resources/acf_register_block_type/
*
* @package lhpbpp
* @package lhpbp\plugin
*/

// Create id attribute allowing for custom "anchor" value.
Expand Down
6 changes: 3 additions & 3 deletions plugin/blocks/demo/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* @param string $content The block content.
* @param WP_Block $block The block type.
*
* @package lhpbpp
* @package lhpbp\plugin
*/

use function WpMunich\lhpbpp\lh_plugin;
use function WpMunich\lhpbp\plugin\plugin;

$attr = wp_parse_args(
$attributes,
Expand All @@ -18,7 +18,7 @@
)
);

$icon = lh_plugin()->svg()->get_svg(
$icon = plugin()->svg()->get_svg(
$attr['icon'],
array(
'attributes' => array(
Expand Down
2 changes: 1 addition & 1 deletion plugin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"autoload": {
"psr-4": {
"WpMunich\\lhpbpp\\": "inc/"
"WpMunich\\lhpbp\\plugin\\": "inc/"
}
},
"config": {
Expand Down
19 changes: 10 additions & 9 deletions plugin/inc/ACF/ACF.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
/**
* LHPBPP\ACF\Component class
*
* @package lhpbpp
* @package lhpbp\plugin
*/

namespace WpMunich\lhpbpp\ACF;
use WpMunich\lhpbpp\Component;
use function WpMunich\lhpbpp\lh_plugin;
namespace WpMunich\lhpbp\plugin\ACF;
use WpMunich\lhpbp\plugin\Plugin_Component;

use function WpMunich\lhpbp\plugin\plugin;
use function acf_add_options_page;
use function add_action;
use function add_filter;
use function wp_get_environment_type;

/**
* A class to handle acf related logic..
* A class to handle acf related logic.
*/
class ACF extends Component {
class ACF extends Plugin_Component {
/**
* Validated and final page settings.
*
Expand Down Expand Up @@ -52,7 +53,7 @@ protected function add_filters() {
* @return string Save path.
*/
public function acf_json_save_point( $path ) {
$path = lh_plugin()->get_plugin_path() . 'acf-json';
$path = plugin()->get_plugin_path() . 'acf-json';
return $path;
}

Expand All @@ -64,7 +65,7 @@ public function acf_json_save_point( $path ) {
* @return array An array of paths.
*/
public function acf_json_load_point( $paths ) {
$paths[] = lh_plugin()->get_plugin_path() . 'acf-json';
$paths[] = plugin()->get_plugin_path() . 'acf-json';

return $paths;
}
Expand All @@ -82,7 +83,7 @@ public function add_options_page() {
'page_title' => __( 'L//H Settings', 'lhpbpp' ),
'menu_title' => __( 'L//H Settings', 'lhpbpp' ),
'menu_slug' => 'lhpbpp-plugin-general-settings',
'icon_url' => lh_plugin()->svg()->get_admin_menu_icon( 'img/icons/slashes.svg' ),
'icon_url' => plugin()->svg()->get_admin_menu_icon( 'img/icons/slashes.svg' ),
'capability' => 'edit_posts',
'redirect' => false,
)
Expand Down
62 changes: 62 additions & 0 deletions plugin/inc/Abstracts/Component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* This file contains the abstract component class.
* It is used to define the basic structure of a component, which we use to
* extend the plugin and the accompanying theme. A component is a class that
* contains all the logic for a specific feature of the plugin or theme. It combines
* actions and filters, the logic and helper functions.
*
* @package lhpbp\plugin
*/

namespace WpMunich\lhpbp\plugin\Abstracts;

use function WpMunich\lhpbp\plugin\plugin_container;

/**
* Abstract class for a component.
*/
abstract class Component {
/**
* Constructor.
* Used to initialize the component and add the needed actions and filters.
*
* @return void
*/
public function __construct() {
$this->add_actions();
$this->add_filters();
}

/**
* Add the needed WordPress actions for the component.
*
* @see https://codex.wordpress.org/Plugin_API/Action_Reference
*/
abstract protected function add_actions();

/**
* Add the needed WordPress filters for the component.
*
* @see https://codex.wordpress.org/Plugin_API/Filter_Reference
*/
abstract protected function add_filters();

/**
* Get the parent class.
*
* @return Object The parent class.
*/
public function get_parent() {
return get_parent_class( $this );
}

/**
* Get the DI container.
*
* @return \DI\Container The DI container.
*/
protected function container() {
return plugin_container();
}
}
29 changes: 15 additions & 14 deletions plugin/inc/Blocks/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
/**
* LHPBPP\Blocks\Component class
*
* @package lhpbpp
* @package lhpbp\plugin
*/

namespace WpMunich\lhpbpp\Blocks;
use WpMunich\lhpbpp\Component;
use function WpMunich\lhpbpp\lh_plugin;
namespace WpMunich\lhpbp\plugin\Blocks;
use WpMunich\lhpbp\plugin\Plugin_Component;

use function WpMunich\lhpbp\plugin\plugin;
use function acf_register_block_type;
use function add_action;
use function add_filter;
Expand All @@ -21,7 +22,7 @@
/**
* A class to handle the plugins blocks.
*/
class Blocks extends Component {
class Blocks extends Plugin_Component {

/**
* {@inheritDoc}
Expand Down Expand Up @@ -56,7 +57,7 @@ public function register_acf_block_types() {
'category' => 'lhpbpp-blocks',
'icon' => 'screenoptions',
'keywords' => array( __( 'ACF', 'lhpbpp' ), __( 'Demo', 'lhpbpp' ), __( 'Block', 'lhpbpp' ) ),
'render_template' => apply_filters( 'lh_acf_block_template_path', lh_plugin()->get_plugin_path() . 'blocks/acf/template.php', 'acf-demo-block' ),
'render_template' => apply_filters( 'lh_acf_block_template_path', plugin()->get_plugin_path() . 'blocks/acf/template.php', 'acf-demo-block' ),
'mode' => 'auto',
'supports' => array(
'align' => array( 'wide', 'full' ),
Expand Down Expand Up @@ -90,13 +91,13 @@ public function add_block_categories( $categories, $post ) {
public function enqueue_block_editor_assets() {
$screen = get_current_screen();

$assets = wp_json_file_decode( lh_plugin()->get_plugin_path() . '/admin/dist/assets.json', array( 'associative' => true ) );
$assets = wp_json_file_decode( plugin()->get_plugin_path() . '/admin/dist/assets.json', array( 'associative' => true ) );

if ( ! in_array( $screen->id, array( 'widgets' ), true ) ) {
$block_helper_assets = $assets['js/blocks-helper.min.js'] ?? array();
wp_enqueue_script(
'lhpbpp-blocks-helper',
lh_plugin()->get_plugin_url() . 'admin/dist/js/blocks-helper.min.js',
plugin()->get_plugin_url() . 'admin/dist/js/blocks-helper.min.js',
array_merge( array(), $block_helper_assets['dependencies'] ),
$block_helper_assets['version'],
true
Expand All @@ -106,24 +107,24 @@ public function enqueue_block_editor_assets() {
$block_assets = $assets['js/blocks.min.js'] ?? array();
wp_enqueue_script(
'lhpbpp-blocks',
lh_plugin()->get_plugin_url() . 'admin/dist/js/blocks.min.js',
plugin()->get_plugin_url() . 'admin/dist/js/blocks.min.js',
array_merge( array(), $block_assets['dependencies'] ),
$block_assets['version'],
true
);

wp_enqueue_style(
'lhpbpp-admin-components',
lh_plugin()->get_plugin_url() . '/admin/dist/css/components.min.css',
plugin()->get_plugin_url() . '/admin/dist/css/components.min.css',
array(),
lh_plugin()->get_plugin_version(),
plugin()->get_plugin_version(),
'all'
);

/**
* Load the translations for the block editor assets.
*/
$dir = lh_plugin()->get_plugin_path();
$dir = plugin()->get_plugin_path();
$path = $dir . '/languages/';

wp_set_script_translations(
Expand All @@ -143,7 +144,7 @@ public function enqueue_block_editor_assets() {
* Register the blocks.
*/
public function register_blocks() {
$blocks_path = lh_plugin()->get_plugin_path() . 'blocks/';
$blocks_path = plugin()->get_plugin_path() . 'blocks/';

$custom_blocks = array(
'demo',
Expand All @@ -169,7 +170,7 @@ public function register_blocks() {
* @return string The rendered block.
*/
public function provide_render_callback( $attributes, $content, $block ) {
$blocks_path = lh_plugin()->get_plugin_path() . 'blocks/';
$blocks_path = plugin()->get_plugin_path() . 'blocks/';
ob_start();

switch ( $block->name ) {
Expand Down
44 changes: 0 additions & 44 deletions plugin/inc/Component.php

This file was deleted.

Loading

0 comments on commit db80ada

Please sign in to comment.