Skip to content

Commit

Permalink
Abstraction of the plugin file
Browse files Browse the repository at this point in the history
  • Loading branch information
Luehrsen committed Mar 28, 2022
1 parent 2697d84 commit 76480b8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

4 changes: 2 additions & 2 deletions plugin/inc/ACF/ACF.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function add_filters() {
* @return string Save path.
*/
public function acf_json_save_point( $path ) {
$path = LHPBPP_PATH . 'acf-json';
$path = lh_plugin()->get_plugin_path() . 'acf-json';
return $path;
}

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

return $paths;
}
Expand Down
7 changes: 4 additions & 3 deletions plugin/inc/Blocks/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WpMunich\lhpbpp\Component;
use function add_action;
use function acf_register_block_type;
use function WpMunich\lhpbpp\lh_plugin;

/**
* A class to handle the plugins blocks.
Expand Down Expand Up @@ -47,7 +48,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', LHPBPP_PATH . 'blocks/acf/template.php', 'acf-demo-block' ),
'render_template' => apply_filters( 'lh_acf_block_template_path', lh_plugin()->get_plugin_path() . 'blocks/acf/template.php', 'acf-demo-block' ),
'mode' => 'auto',
'supports' => array(
'align' => array( 'wide', 'full' ),
Expand Down Expand Up @@ -81,13 +82,13 @@ public function add_block_categories( $categories, $post ) {
public function enqueue_block_editor_assets() {
$screen = get_current_screen();

$assets = wp_json_file_decode( LHPBPP_PATH . '/admin/dist/assets.json', array( 'associative' => true ) );
$assets = wp_json_file_decode( lh_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',
LHPBPP_URL . '/admin/dist/js/blocks-helper.min.js',
lh_plugin()->get_plugin_url() . '/admin/dist/js/blocks-helper.min.js',
array_merge( array(), $block_helper_assets['dependencies'] ),
$block_helper_assets['version'],
true
Expand Down
46 changes: 46 additions & 0 deletions plugin/inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,50 @@ public function __construct(
public function svg() {
return $this->svg;
}

/**
* Get the plugin version.
*
* @return string The plugin version.
*/
public function get_plugin_version() {
// Check if we can use the `get_plugin_data()` function.
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}

/**
* The plugin data as an array.
* We use this to avoid updating plugin data on multiple locations. This makes
* the file header of the plugin main file the single source of truth.
*/
$plugin_data = get_plugin_data( LHPBPP_FILE );

return $plugin_data['Version'] ?? '0.0.1';
}

/**
* Get the main plugin file.
*
* @return string The main plugin file.
*/
public function get_plugin_file() {
return LHPBPP_FILE;
}

/**
* Get the plugin directory path.
*
* @return string The plugin directory path.
*/
public function get_plugin_path() {
return plugin_dir_path( $this->get_plugin_file() );
}

/**
* Get the plugin directory URL.
*/
public function get_plugin_url() {
return plugin_dir_url( $this->get_plugin_file() );
}
}
6 changes: 4 additions & 2 deletions plugin/inc/SVG/SVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace WpMunich\lhpbpp\SVG;
use WpMunich\lhpbpp\Component;
use function add_action;
use function WpMunich\lhpbpp\lh_plugin;

use \WP_Error;

/**
Expand Down Expand Up @@ -47,8 +49,8 @@ public function get_svg( $path = null, $args = array() ) {
case ( file_exists( get_template_directory() . $path ) ):
$final_path = get_template_directory() . $path;
break;
case ( file_exists( LHPBPP_PATH . $path ) ):
$final_path = LHPBPP_PATH . $path;
case ( file_exists( lh_plugin()->get_plugin_path() . $path ) ):
$final_path = lh_plugin()->get_plugin_path() . $path;
break;
default:
return false;
Expand Down
3 changes: 2 additions & 1 deletion plugin/inc/i18n/I18N.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WpMunich\lhpbpp\Component;
use function add_action;
use function load_plugin_textdomain;
use function WpMunich\lhpbpp\lh_plugin;

/**
* A class to handle textdomains and other i18n related logic..
Expand All @@ -34,7 +35,7 @@ public function load_plugin_textdomain() {
load_plugin_textdomain(
'lhpbpp',
false,
LHPBPP_PATH . '/languages/'
lh_plugin()->get_plugin_path() . '/languages/'
);
}
}
50 changes: 4 additions & 46 deletions plugin/lhpbpp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Description: A base boilerplate for Luehrsen // Heinrich WordPress projects.
* Author: Luehrsen // Heinrich
* Author URI: https://www.luehrsen-heinrich.de
* Version: 0.0.1
* Version: 0.0.5
* Text Domain: lhpbpp
* Domain Path: /languages
*/
Expand All @@ -19,11 +19,6 @@
die;
}

// Check if we can use the `get_plugin_data()` function.
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}

// Set a constant for the plugin's main file.
if ( ! defined( 'LHPBPP_FILE' ) ) {
/**
Expand All @@ -34,52 +29,15 @@
define( 'LHPBPP_FILE', __FILE__ );
}

// Set a constant for the plugin's public URL.
if ( ! defined( 'LHPBPP_URL' ) ) {
/**
* The URL to the plugin's public folder.
*
* @var string
*/
define( 'LHPBPP_URL', plugin_dir_url( LHPBPP_FILE ) );
}

// Set a constant for the plugin's directory path.
if ( ! defined( 'LHPBPP_PATH' ) ) {
/**
* The path to the plugin's directory.
*
* @var string
*/
define( 'LHPBPP_PATH', plugin_dir_path( LHPBPP_FILE ) );
}

/**
* The plugin data as an array.
* We use this to avoid updating plugin data on multiple locations. This makes
* the file header of the plugin main file the single source of truth.
*/
$plugin_data = get_plugin_data( LHPBPP_FILE );

// Set a constant for the plugin's current version.
if ( ! defined( 'LHPBPP_VERSION' ) ) {
/**
* The plugin version.
*
* @var string
*/
define( 'LHPBPP_VERSION', $plugin_data['Version'] );
}

// Load the autoloader.
require LHPBPP_PATH . 'vendor/autoload.php';
require plugin_dir_path( LHPBPP_FILE ) . 'vendor/autoload.php';

// Load the `wp_lhpbpp()` entry point function.
require LHPBPP_PATH . 'inc/functions.php';
require plugin_dir_path( LHPBPP_FILE ) . 'inc/functions.php';

// If we are in the development environment, load some test functions.
if ( wp_get_environment_type() === 'development' ) {
require LHPBPP_PATH . 'inc/test.php';
require plugin_dir_path( LHPBPP_FILE ) . 'inc/test.php';
}

// Initialize the plugin.
Expand Down
2 changes: 1 addition & 1 deletion theme/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: A boilerplate for WordPress projects
* Author: Luehrsen // Heinrich
* Author URI: http://www.luehrsen-heinrich.de
* Version: 0.0.1
* Version: 0.0.5
* Text Domain: lhpbpt
* Domain Path: /languages
* License: GPL-2.0+
Expand Down

0 comments on commit 76480b8

Please sign in to comment.