Skip to content

Commit

Permalink
Fix DI, remove unwanted plugin-wide Constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubboucek committed Oct 13, 2019
1 parent 054db5e commit 15281e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions simpleshop-cz.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
/**
* Start plugin
*/
SimpleShop::$pluginMainFile = __FILE__;
SimpleShop::getInstance();
6 changes: 5 additions & 1 deletion src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ class Admin {
*/
private $pluginDirUrl;

/**
* @param Plugin $loader
* @param string $pluginMainFile
*/
public function __construct(Plugin $loader) {
$this->loader = $loader;

$this->pluginDirUrl = plugin_dir_url(__DIR__ . '/../');
$this->pluginDirUrl = plugin_dir_url($loader->get_plugin_main_file());

add_action( 'admin_menu', [ $this, 'add_settings_page' ] );
add_filter( 'manage_edit-ssc_group_columns', [ $this, 'ssc_group_columns' ] );
Expand Down
19 changes: 13 additions & 6 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ class Plugin {
/**
* @var string
*/
private $pluginDirPath;
private $pluginMainFile;

/**
* @param string $mainFile Filename of main plugin path. Used for native WP functions
*/
public function __construct( $mainFile ) {
$this->pluginMainFile = $mainFile;

public function __construct() {
$this->init();
$this->init_i18n();

$this->pluginDirPath = plugin_dir_path( __DIR__ . '/../' );

$this->secure_key = $this->load_api_key();
$this->email = $this->load_email();

add_action( 'tgmpa_register', [ $this, 'register_required_plugins' ] );
register_activation_hook( $this->pluginDirPath, [ $this, 'ssc_activation_hook' ] );
register_activation_hook( plugin_dir_path( $this->pluginMainFile ), [ $this, 'ssc_activation_hook' ] );
}

private function init() {
Expand Down Expand Up @@ -157,7 +160,11 @@ public function init_i18n() {
}

public function load_textdomain_i18n() {
$plugin_rel_path = str_replace( WP_PLUGIN_DIR . '/', '', $this->pluginDirPath . 'languages/' );
$plugin_rel_path = str_replace( WP_PLUGIN_DIR . '/', '', plugin_dir_path( $this->pluginMainFile ) . 'languages/' );
load_plugin_textdomain( 'simpleshop-cz', false, $plugin_rel_path );
}

public function get_plugin_main_file() {
return $this->pluginMainFile;
}
}
7 changes: 6 additions & 1 deletion src/SimpleShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
namespace Redbit\SimpleShop\WpPlugin;

class SimpleShop {
/**
* @var string
*/
public static $pluginMainFile = __DIR__ . '/../simpleshop-cz.php';

/**
* @var Plugin
*/
Expand All @@ -29,6 +34,6 @@ public static function getInstance() {
* @return Plugin
*/
protected static function factory() {
return new Plugin();
return new Plugin(self::$pluginMainFile);
}
}

0 comments on commit 15281e8

Please sign in to comment.