diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..ccef0560f5e81eb78c47d43c0464ed4e04ee6434 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +.idea \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000000000000000000000000000000000000..f9267976aeee29c2940b41428f004a2694b3e161 --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "wpdesk/wp-builder", + "authors": [ + { + "name": "Krzysiek", + "email": "krzysiek@wpdesk.pl" + } + ], + "require": { + "php": ">=5.5", + "wpdesk/wp-basic-requirements": "dev-master" + }, + "require-dev": { + "phpunit/phpunit": "<7", + "wp-coding-standards/wpcs": "^0.14.1", + "squizlabs/php_codesniffer": "^3.0.2", + "mockery/mockery": "*", + "10up/wp_mock": "*", + "wimg/php-compatibility": "^8" + }, + "autoload": { + "psr-4": {"WPDesk\\PluginBuilder\\": "src/"} + }, + "autoload-dev": { + }, + "scripts": { + "phpcs": "phpcs", + "phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never", + "phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage", + "phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never", + "phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage" + } +} diff --git a/src/Builder/AbstractBuilder.php b/src/Builder/AbstractBuilder.php new file mode 100644 index 0000000000000000000000000000000000000000..296089c714749a62a3d0aa3ae3f6a820a975661b --- /dev/null +++ b/src/Builder/AbstractBuilder.php @@ -0,0 +1,33 @@ +<?php + +namespace WPDesk\PluginBuilder\Builder; + +use WPDesk\PluginBuilder\Plugin\AbstractPlugin; + +class AbstractBuilder { + protected static $instances = []; + + /** + * @param string $class + * @param AbstractPlugin $object + */ + protected function addToStorage( $class, $object ) { + if ( isset( self::$instances[ $class ] ) ) { + throw new Exception\ClassAlreadyExists( "Class {$class} already exists" ); + } + self::$instances[ $class ] = $object; + } + + /** + * @param string $class + * + * @return AbstractPlugin + */ + protected function getFromStorage( $class ) { + if ( isset( self::$instances[ $class ] ) ) { + return self::$instances[ $class ]; + } else { + throw new Exception\ClassNotExists( "Class {$class} not exists in storage" ); + } + } +} \ No newline at end of file diff --git a/src/Builder/Exception/ClassAlreadyExists.php b/src/Builder/Exception/ClassAlreadyExists.php new file mode 100644 index 0000000000000000000000000000000000000000..2b69111bc281e6f2882ea2a0f1d2570d3867f500 --- /dev/null +++ b/src/Builder/Exception/ClassAlreadyExists.php @@ -0,0 +1,8 @@ +<?php + +namespace WPDesk\PluginBuilder\Builder\Exception; + +class ClassAlreadyExists extends \RuntimeException +{ + +} \ No newline at end of file diff --git a/src/Builder/Exception/ClassNotExists.php b/src/Builder/Exception/ClassNotExists.php new file mode 100644 index 0000000000000000000000000000000000000000..bbcfe32efed7b4946626d581d5844f25e6bfae20 --- /dev/null +++ b/src/Builder/Exception/ClassNotExists.php @@ -0,0 +1,8 @@ +<?php + +namespace WPDesk\PluginBuilder\Builder\Exception; + +class ClassNotExists extends \RuntimeException +{ + +} \ No newline at end of file diff --git a/src/Builder/InfoBuilder.php b/src/Builder/InfoBuilder.php new file mode 100644 index 0000000000000000000000000000000000000000..50c6dba8e8bf21924afc37636afa8a920ce2bd93 --- /dev/null +++ b/src/Builder/InfoBuilder.php @@ -0,0 +1,42 @@ +<?php + +namespace WPDesk\PluginBuilder\Builder; + +use WPDesk\PluginBuilder\Plugin\AbstractPlugin; + +class InfoBuilder extends AbstractBuilder { + const FILTER_PLUGIN_CLASS = 'wp_builder_plugin_class'; + const HOOK_BEFORE_PLUGIN_INIT = 'wp_builder_before_plugin_init'; + const HOOK_AFTER_PLUGIN_INIT = 'wp_builder_before_init'; + + /** + * Builds instance of plugin + * + * @param \WPDesk_Plugin_Info $info + * @return AbstractPlugin + * + * @return AbstractPlugin + */ + public function build_from_info( \WPDesk_Plugin_Info $info ) { + $class_name = apply_filters( self::FILTER_PLUGIN_CLASS, $info->get_class_name() ); + + /** @var AbstractPlugin $plugin */ + $plugin = new $class_name( $info ); + $this->addToStorage( $info->get_class_name(), $plugin ); + + do_action( self::HOOK_BEFORE_PLUGIN_INIT, $plugin ); + $plugin->init(); + do_action( self::HOOK_AFTER_PLUGIN_INIT, $plugin ); + + return $plugin; + } + + /** + * @param string $class + * + * @return AbstractPlugin + */ + public function get_plugin_instance( $class ) { + return $this->getFromStorage( $class ); + } +} \ No newline at end of file diff --git a/src/Plugin/AbstractPlugin.php b/src/Plugin/AbstractPlugin.php new file mode 100644 index 0000000000000000000000000000000000000000..d432d54bee89d00f6455ff4b86ed619927ca8a31 --- /dev/null +++ b/src/Plugin/AbstractPlugin.php @@ -0,0 +1,152 @@ +<?php + +namespace WPDesk\PluginBuilder\Plugin; + +/** + * Base plugin class for WP Desk plugins + * + * @author Grzegorz + * + */ +abstract class AbstractPlugin { + + /** @var \WPDesk_Plugin_Info */ + protected $plugin_info; + + /** @var string */ + protected $plugin_namespace; + + /** @var string */ + protected $plugin_url; + + /** @var string */ + protected $docs_url; + + /** @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->load_plugin_text_domain(); + $this->hooks(); + } + + /** + * @return $this + */ + public function get_plugin() { + return $this; + } + + /** + * @return string + */ + public function get_text_domain() { + return $this->get_namespace(); + } + + /** + * @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', 'wpdesk-plugin' ) . '</a>', + ); + $links = array_merge( $plugin_links, $links ); + + if ( $this->docs_url ) { + $plugin_links = array( + '<a href="' . $this->docs_url . '">' . __( 'Docs', 'wpdesk-plugin' ) . '</a>', + ); + $links = array_merge( $plugin_links, $links ); + } + + if ( $this->settings_url ) { + $plugin_links = array( + '<a href="' . $this->settings_url . '">' . __( 'Settings', 'wpdesk-plugin' ) . '</a>', + ); + $links = array_merge( $plugin_links, $links ); + } + + return $links; + } + +} +