diff --git a/src/BuildDirector/LegacyBuildDirector.php b/src/BuildDirector/LegacyBuildDirector.php index 76add6942f98ec50aa4ff3918afc886be9978f84..0ff5df972860488d64eb25e6eca84d2e3c821daa 100644 --- a/src/BuildDirector/LegacyBuildDirector.php +++ b/src/BuildDirector/LegacyBuildDirector.php @@ -4,13 +4,14 @@ namespace WPDesk\PluginBuilder\BuildDirector; use WPDesk\PluginBuilder\Builder\AbstractBuilder; use WPDesk\PluginBuilder\Plugin\AbstractPlugin; +use WPDesk\PluginBuilder\Storage\StorageFactory; class LegacyBuildDirector { /** @var AbstractBuilder */ private $builder; - public function __construct(AbstractBuilder $builder) { + public function __construct( AbstractBuilder $builder ) { $this->builder = $builder; } @@ -20,7 +21,9 @@ class LegacyBuildDirector { public function buildPlugin() { $this->builder->build_plugin(); $this->builder->init_plugin(); - $this->builder->store_plugin(); + + $storage = new StorageFactory(); + $this->builder->store_plugin( $storage->create_storage() ); } /** diff --git a/src/Builder/AbstractBuilder.php b/src/Builder/AbstractBuilder.php index ef1a4ead500ff91daa08974cab9ff72e7fccd828..592c969e1c31e68243ade2e929314424866be3dc 100644 --- a/src/Builder/AbstractBuilder.php +++ b/src/Builder/AbstractBuilder.php @@ -3,6 +3,7 @@ namespace WPDesk\PluginBuilder\Builder; use WPDesk\PluginBuilder\Plugin\AbstractPlugin; +use WPDesk\PluginBuilder\Storage\PluginStorage; abstract class AbstractBuilder { /** @@ -14,7 +15,7 @@ abstract class AbstractBuilder { /** * Store plugin class in some kind of storage */ - public function store_plugin() { + public function store_plugin( PluginStorage $storage ) { } /** @@ -34,7 +35,7 @@ abstract class AbstractBuilder { * * @param $settings */ - public function set_settings($settings) { + public function set_settings( $settings ) { } /** @@ -42,7 +43,7 @@ abstract class AbstractBuilder { * * @param $view */ - public function set_view($view) { + public function set_view( $view ) { } /** @@ -50,7 +51,7 @@ abstract class AbstractBuilder { * * @param $tracker */ - public function set_tracker($tracker) { + public function set_tracker( $tracker ) { } /** @@ -58,6 +59,6 @@ abstract class AbstractBuilder { * * @param $helper */ - public function set_helper($helper) { + public function set_helper( $helper ) { } } \ No newline at end of file diff --git a/src/Builder/InfoBuilder.php b/src/Builder/InfoBuilder.php index d8785517ac9a37276c6daca92cd55251db720447..7afa279fc239574d0f17720b1c1412becc3aa2d2 100644 --- a/src/Builder/InfoBuilder.php +++ b/src/Builder/InfoBuilder.php @@ -3,7 +3,7 @@ namespace WPDesk\PluginBuilder\Builder; use WPDesk\PluginBuilder\Plugin\AbstractPlugin; -use WPDesk\PluginBuilder\Plugin\PluginStorage; +use WPDesk\PluginBuilder\Storage\PluginStorage; class InfoBuilder extends AbstractBuilder { const FILTER_PLUGIN_CLASS = 'wp_builder_plugin_class'; @@ -16,15 +16,11 @@ class InfoBuilder extends AbstractBuilder { /** @var \WPDesk_Buildable */ private $info; - /** @var PluginStorage */ - private $storage; - - /** @var string */ + /** @var string */ protected $storage_id; - public function __construct( \WPDesk_Buildable $info, PluginStorage $storage ) { - $this->info = $info; - $this->storage = $storage; + public function __construct( \WPDesk_Buildable $info ) { + $this->info = $info; $this->storage_id = $info->get_class_name(); } @@ -38,8 +34,8 @@ class InfoBuilder extends AbstractBuilder { $this->plugin = new $class_name( $this->info ); } - public function store_plugin() { - $this->storage->add_to_storage( $this->storage_id, $this->plugin ); + public function store_plugin( PluginStorage $storage ) { + $storage->add_to_storage( $this->storage_id, $this->plugin ); } public function init_plugin() { diff --git a/src/Plugin/AbstractPlugin.php b/src/Plugin/AbstractPlugin.php index 709e5d6402caea8159b8103f519752673b29e684..b356088ce0abf1315a5ce0c4d4718edaeec249af 100644 --- a/src/Plugin/AbstractPlugin.php +++ b/src/Plugin/AbstractPlugin.php @@ -11,9 +11,9 @@ namespace WPDesk\PluginBuilder\Plugin; abstract class AbstractPlugin implements \WPDesk_Translable { /** @var \WPDesk_Plugin_Info */ - protected $plugin_info; + protected $plugin_info; - /** @var string */ + /** @var string */ protected $plugin_namespace; /** @var string */ @@ -24,128 +24,128 @@ abstract class AbstractPlugin implements \WPDesk_Translable { /** @var string */ protected $settings_url; - + /** * AbstractPlugin constructor. * * @param \WPDesk_Plugin_Info $plugin_info */ - public function __construct( $plugin_info ) { - $this->plugin_info = $plugin_info; - $this->plugin_namespace = strtolower($plugin_info->get_plugin_dir()); - } - - public function init() { - $this->init_base_variables(); - $this->hooks(); - } - - /** - * @return $this - */ - public function get_plugin() { - return $this; - } - - /** - * @return string - */ - public function get_text_domain() { - return $this->plugin_info->get_text_domain(); - } - - /** - * @return void - */ - public function load_plugin_text_domain() { - load_plugin_textdomain( $this->get_text_domain(), false, $this->get_namespace() . '/lang/' ); - } - - public function init_base_variables( ) { - $this->plugin_url = plugin_dir_url( $this->plugin_info->get_plugin_dir() ); - } - - /** - * @return void - */ - protected function hooks() { - add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); - - add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) ); - - add_action( 'plugins_loaded', array( $this, 'load_plugin_text_domain' ) ); - add_filter( 'plugin_action_links_' . plugin_basename( $this->get_plugin_file_path() ), array( - $this, - 'links_filter' - ) ); - - } - - /** - * - * @return string - */ - public function get_plugin_url() { - return esc_url( trailingslashit( $this->plugin_url ) ); - } - - public function get_plugin_assets_url() { - return esc_url( trailingslashit( $this->get_plugin_url() . 'assets' ) ); - } - - /** - * @return string - */ - public function get_plugin_file_path() { - return $this->plugin_info->get_plugin_dir(); - } - - /** - * @return string - */ - public function get_namespace() { - return $this->plugin_namespace; - } - - public function admin_enqueue_scripts( ) { - } - - public function wp_enqueue_scripts() { - } - - /** - * action_links function. - * - * @access public - * - * @param mixed $links - * - * @return array - */ - public function links_filter( $links ) { - $support_link = get_locale() === 'pl_PL' ? 'https://www.wpdesk.pl/support/' : 'https://www.wpdesk.net/support'; - - $plugin_links = array( - '<a href="' . $support_link . '">' . __( 'Support', $this->get_text_domain() ) . '</a>', - ); - $links = array_merge( $plugin_links, $links ); - - if ( $this->docs_url ) { - $plugin_links = array( - '<a href="' . $this->docs_url . '">' . __( 'Docs', $this->get_text_domain() ) . '</a>', - ); - $links = array_merge( $plugin_links, $links ); - } - - if ( $this->settings_url ) { - $plugin_links = array( - '<a href="' . $this->settings_url . '">' . __( 'Settings', $this->get_text_domain() ) . '</a>', - ); - $links = array_merge( $plugin_links, $links ); - } - - return $links; - } + public function __construct( $plugin_info ) { + $this->plugin_info = $plugin_info; + $this->plugin_namespace = strtolower( $plugin_info->get_plugin_dir() ); + } + + public function init() { + $this->init_base_variables(); + $this->hooks(); + } + + /** + * @return $this + */ + public function get_plugin() { + return $this; + } + + /** + * @return string + */ + public function get_text_domain() { + return $this->plugin_info->get_text_domain(); + } + + /** + * @return void + */ + public function load_plugin_text_domain() { + load_plugin_textdomain( $this->get_text_domain(), false, $this->get_namespace() . '/lang/' ); + } + + public function init_base_variables() { + $this->plugin_url = plugin_dir_url( $this->plugin_info->get_plugin_dir() ); + } + + /** + * @return void + */ + protected function hooks() { + add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); + + add_action( 'wp_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ] ); + + add_action( 'plugins_loaded', [ $this, 'load_plugin_text_domain' ] ); + add_filter( 'plugin_action_links_' . plugin_basename( $this->get_plugin_file_path() ), [ + $this, + 'links_filter' + ] ); + + } + + /** + * + * @return string + */ + public function get_plugin_url() { + return esc_url( trailingslashit( $this->plugin_url ) ); + } + + public function get_plugin_assets_url() { + return esc_url( trailingslashit( $this->get_plugin_url() . 'assets' ) ); + } + + /** + * @return string + */ + public function get_plugin_file_path() { + return $this->plugin_info->get_plugin_dir(); + } + + /** + * @return string + */ + public function get_namespace() { + return $this->plugin_namespace; + } + + public function admin_enqueue_scripts() { + } + + public function wp_enqueue_scripts() { + } + + /** + * action_links function. + * + * @access public + * + * @param mixed $links + * + * @return array + */ + public function links_filter( $links ) { + $support_link = get_locale() === 'pl_PL' ? 'https://www.wpdesk.pl/support/' : 'https://www.wpdesk.net/support'; + + $plugin_links = [ + '<a href="' . $support_link . '">' . __( 'Support', $this->get_text_domain() ) . '</a>', + ]; + $links = array_merge( $plugin_links, $links ); + + if ( $this->docs_url ) { + $plugin_links = [ + '<a href="' . $this->docs_url . '">' . __( 'Docs', $this->get_text_domain() ) . '</a>', + ]; + $links = array_merge( $plugin_links, $links ); + } + + if ( $this->settings_url ) { + $plugin_links = [ + '<a href="' . $this->settings_url . '">' . __( 'Settings', $this->get_text_domain() ) . '</a>', + ]; + $links = array_merge( $plugin_links, $links ); + } + + return $links; + } } diff --git a/src/Builder/Exception/ClassAlreadyExists.php b/src/Storage/Exception/ClassAlreadyExists.php similarity index 55% rename from src/Builder/Exception/ClassAlreadyExists.php rename to src/Storage/Exception/ClassAlreadyExists.php index 2b69111bc281e6f2882ea2a0f1d2570d3867f500..7fb8a0b7116e9548c0b4899b4ea95090abe478c4 100644 --- a/src/Builder/Exception/ClassAlreadyExists.php +++ b/src/Storage/Exception/ClassAlreadyExists.php @@ -1,6 +1,6 @@ <?php -namespace WPDesk\PluginBuilder\Builder\Exception; +namespace WPDesk\PluginBuilder\Storage\Exception; class ClassAlreadyExists extends \RuntimeException { diff --git a/src/Builder/Exception/ClassNotExists.php b/src/Storage/Exception/ClassNotExists.php similarity index 54% rename from src/Builder/Exception/ClassNotExists.php rename to src/Storage/Exception/ClassNotExists.php index bbcfe32efed7b4946626d581d5844f25e6bfae20..d873f70655c0b76fcf0b1b89d232d3cdc564e38d 100644 --- a/src/Builder/Exception/ClassNotExists.php +++ b/src/Storage/Exception/ClassNotExists.php @@ -1,6 +1,6 @@ <?php -namespace WPDesk\PluginBuilder\Builder\Exception; +namespace WPDesk\PluginBuilder\Storage\Exception; class ClassNotExists extends \RuntimeException { diff --git a/src/Storage/PluginStorage.php b/src/Storage/PluginStorage.php new file mode 100644 index 0000000000000000000000000000000000000000..e8a5fce580aaf5bd7acf53ef131fdad9f7ceacb9 --- /dev/null +++ b/src/Storage/PluginStorage.php @@ -0,0 +1,22 @@ +<?php + +namespace WPDesk\PluginBuilder\Storage; + +use WPDesk\PluginBuilder\Plugin\AbstractPlugin; + +interface PluginStorage { + + /** + * @param string $class + * @param AbstractPlugin $object + */ + public function add_to_storage( $class, $object ); + + /** + * @param string $class + * + * @return AbstractPlugin + */ + public function get_from_storage( $class ); +} + diff --git a/src/Plugin/PluginStorage.php b/src/Storage/StaticStorage.php similarity index 82% rename from src/Plugin/PluginStorage.php rename to src/Storage/StaticStorage.php index 6342b500aedefab1cfa2f8902b3a27b26ce77f11..bae150dd37e7a1c6784801c22f50da95539a27dd 100644 --- a/src/Plugin/PluginStorage.php +++ b/src/Storage/StaticStorage.php @@ -1,8 +1,10 @@ <?php -namespace WPDesk\PluginBuilder\Plugin; +namespace WPDesk\PluginBuilder\Storage; -class PluginStorage { +use WPDesk\PluginBuilder\Plugin\AbstractPlugin; + +class StaticStorage implements PluginStorage { protected static $instances = []; /** diff --git a/src/Plugin/StorageFactory.php b/src/Storage/StorageFactory.php similarity index 61% rename from src/Plugin/StorageFactory.php rename to src/Storage/StorageFactory.php index cdfd229f98666db128fa77482e4e58346dc7d7ea..8a1422f53e033dc1fc3b1da9cf1dee71a302f23a 100644 --- a/src/Plugin/StorageFactory.php +++ b/src/Storage/StorageFactory.php @@ -1,6 +1,6 @@ <?php -namespace WPDesk\PluginBuilder\Plugin; +namespace WPDesk\PluginBuilder\Storage; class StorageFactory { @@ -8,7 +8,7 @@ class StorageFactory { * @return PluginStorage */ public function create_storage() { - return new PluginStorage(); + return new StaticStorage(); } }