diff --git a/README.md b/README.md index 643cfce5f41b5445ce54f6633abe14a66981800c..8af5e1acf9ce15587338029457d132b761d68e2b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Before commit execute: `npm run prod` ! ## Usage ```bash -composer require --dev wpdesk/wp-wpdesk-activation-reminder +composer require wpdesk/wp-wpdesk-activation-reminder composer update ``` diff --git a/src/ActivationReminder/Composer/InvalidSettingValue.php b/src/ActivationReminder/Composer/InvalidSettingValue.php new file mode 100644 index 0000000000000000000000000000000000000000..64c1e65e10505e68611ca114b14270cb730ba09d --- /dev/null +++ b/src/ActivationReminder/Composer/InvalidSettingValue.php @@ -0,0 +1,12 @@ +<?php + +namespace WPDesk\ActivationReminder\Composer; + +class InvalidSettingValue extends \RuntimeException { + + public function __construct( $field, $value ) { + $message = sprintf( 'Invalid Activation Reminder setting value for field %1$s: %2$s!', $field, isset( $value ) ? $value : ' not set' ); + parent::__construct( $message ); + } + +} \ No newline at end of file diff --git a/src/ActivationReminder/Composer/Plugin.php b/src/ActivationReminder/Composer/Plugin.php index 60b554b275956b4f70f2cc13d76b1cf89e0d2de3..8e2cd877243afbef14adbbf8bf9cfc3e09b3b172 100644 --- a/src/ActivationReminder/Composer/Plugin.php +++ b/src/ActivationReminder/Composer/Plugin.php @@ -82,7 +82,7 @@ class Plugin implements PluginInterface, Capable, EventSubscriberInterface { * @param Event $event */ public function generateReminder(Event $event) { - passthru("composer prepare-activation-reminder"); + passthru("composer prepare-activation-reminder" ); } } diff --git a/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php b/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php index 59e9f129b01228809970f93645c1656176ca16ee..62e0e3efe7063d0443f2536f39a664dfab5bde4e 100644 --- a/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php +++ b/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php @@ -33,52 +33,43 @@ class PrepareActivationReminderCommand extends BaseCommand { $output->writeln("Creating activation reminder."); - if ( $this->package_has_activation_remider() ) { - $classLoader = require( 'vendor/autoload.php' ); + $settings = Settings::create_from_composer_settings( $this->getComposer()->getPackage()->getExtra() ); - $class_map = $classLoader->getClassMap(); + $classLoader = require( 'vendor/autoload.php' ); + $class_map = $classLoader->getClassMap(); - $random_class = $this->get_random_class( $class_map ); + $random_class = $this->get_random_class( $class_map ); + $random_letter = strtolower( chr( rand( 65, 90 ) ) ); - $random_letter = strtolower( chr( rand( 65, 90 ) ) ); - - $target_file = $class_map[ $random_class ]; - $target_file = str_replace( '.php', $random_letter . '.php', $target_file ); - $target_file = str_replace( getcwd() . '/vendor/composer/../../', '', $target_file ); + $target_file = $this->create_or_get_target_file_name( $class_map, $random_class, $random_letter ); + if ( ! file_exists( $target_file ) ) { $target_dir = dirname( $target_file ); - $output->writeln( "Target file name: $target_file" ); - - $this->clear_vendor_prefixed(); - - passthru( 'composer generate-vendor-prefixed' ); - copy( 'vendor/wpdesk/wp-wpdesk-activation-reminder/src/Reminder.php', $target_file ); - - $this->regenerate_autoload( $target_file ); - $popup_javascript_file = $target_dir . '/popup.js'; $popup_css_file = $target_dir . '/popup.css'; - copy( 'vendor/wpdesk/wp-wpdesk-activation-reminder/assets/js/popup.js', $popup_javascript_file ); copy( 'vendor/wpdesk/wp-wpdesk-activation-reminder/assets/css/popup.css', $popup_css_file ); + $this->prepare_class( $random_class . $random_letter, $target_file, $popup_javascript_file, $popup_css_file, $settings ); + } - $this->prepare_class( $random_class . $random_letter, $target_file, $popup_javascript_file, $popup_css_file ); + $this->regenerate_autoload( $target_file ); - $output->writeln( "Activation reminder created." ); - } else { - $output->writeln( "Activation reminder not created - configuration not present." ); - } + $output->writeln( "Activation reminder created." ); } - /** - * @return bool - */ - private function package_has_activation_remider() { - $extra = $this->getComposer()->getPackage()->getExtra(); + private function create_or_get_target_file_name( array $class_map, $random_class, $random_letter ) { + $target_file_store = 'vendor/wpdesk/wp-wpdesk-activation-reminder/target-file'; + if ( ! file_exists( $target_file_store ) ) { + $target_file = $class_map[ $random_class ]; + $target_file = str_replace( '.php', $random_letter . '.php', $target_file ); + $target_file = str_replace( getcwd() . '/vendor/composer/../../', '', $target_file ); + + file_put_contents( $target_file_store, $target_file ); + } - return isset( $extra['activation-reminder'] ); + return file_get_contents( $target_file_store ); } /** @@ -97,6 +88,7 @@ class PrepareActivationReminderCommand extends BaseCommand $autoload['files'][] = $class_file; $package->setAutoload( $autoload ); + $composer->getAutoloadGenerator()->setDevMode( true ); $composer->getAutoloadGenerator()->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize ); } @@ -105,30 +97,22 @@ class PrepareActivationReminderCommand extends BaseCommand * @param string $class_file * @param string $popup_javascript_file * @param string $popup_css_file + * @param Settings $settings */ - private function prepare_class( $class_name, $class_file, $popup_javascript_file, $popup_css_file ) { - $composer = $this->getComposer(); - $package = $composer->getPackage(); - $extra = $package->getExtra(); - - $plugin_title = $extra['activation-reminder']['plugin-title']; - $plugin_dir = $extra['activation-reminder']['plugin-dir']; - $logo_url = $extra['activation-reminder']['logo-url']; - $buy_plugin_url = $extra['activation-reminder']['buy-plugin-url']; - + private function prepare_class( $class_name, $class_file, $popup_javascript_file, $popup_css_file, $settings ) { $namespace = $this->prepare_namespace_from_class_name( $class_name ); $short_classname = $this->prepare_short_class_name_from_class_name( $class_name ); $file_contents = file_get_contents( $class_file ); $file_contents = str_replace( 'namespace ReminderNamespace;', 'namespace ' . $namespace . ';', $file_contents ); $file_contents = str_replace( 'class Reminder', 'class ' . $short_classname, $file_contents ); - $file_contents = str_replace( 'plugin-dir', $plugin_dir, $file_contents ); - $file_contents = str_replace( 'plugin-title', $plugin_title, $file_contents ); - $file_contents = str_replace( 'popup-javascript-file', $plugin_dir . '/' . $popup_javascript_file, $file_contents ); - $file_contents = str_replace( 'popup-css-file', $plugin_dir . '/' . $popup_css_file, $file_contents ); + $file_contents = str_replace( 'plugin-dir', $settings->get_plugin_dir(), $file_contents ); + $file_contents = str_replace( 'plugin-title', $settings->get_plugin_title(), $file_contents ); + $file_contents = str_replace( 'popup-javascript-file', $settings->get_plugin_dir() . '/' . $popup_javascript_file, $file_contents ); + $file_contents = str_replace( 'popup-css-file', $settings->get_plugin_dir() . '/' . $popup_css_file, $file_contents ); $file_contents = str_replace( 'script-version', rand(1, 1000), $file_contents ); - $file_contents = str_replace( 'logo-url', $logo_url, $file_contents ); - $file_contents = str_replace( 'buy-plugin-url', $buy_plugin_url, $file_contents ); + $file_contents = str_replace( 'logo-url', $settings->get_logo_url(), $file_contents ); + $file_contents = str_replace( 'buy-plugin-url', $settings->get_buy_plugin_url(), $file_contents ); $file_contents = str_replace( 'new Reminder();', 'new ' . $short_classname . '();', $file_contents ); file_put_contents( $class_file, $file_contents ); } @@ -169,25 +153,4 @@ class PrepareActivationReminderCommand extends BaseCommand return $class_name; } - /** - * - */ - private function clear_vendor_prefixed() { - $this->delete_all( 'vendor_prefixed' ); - } - - /** - * @param string $dir - */ - private function delete_all( $dir ) { - foreach ( glob( $dir . '/*' ) as $file ) { - if ( is_dir( $file ) ) { - $this->delete_all( $file ); - } else { - unlink( $file ); - } - } - rmdir( $dir ); - } - } diff --git a/src/ActivationReminder/Composer/Settings.php b/src/ActivationReminder/Composer/Settings.php new file mode 100644 index 0000000000000000000000000000000000000000..3a29497b8a6a069d5c9729c1cfdd7fe4652433db --- /dev/null +++ b/src/ActivationReminder/Composer/Settings.php @@ -0,0 +1,113 @@ +<?php + +namespace WPDesk\ActivationReminder\Composer; + +class Settings { + + const PLUGIN_TITLE = 'plugin-title'; + const PLUGIN_DIR = 'plugin-dir'; + const LOGO_URL = 'logo-url'; + const BUY_PLUGIN_URL = 'buy-plugin-url'; + const ACTIVATION_REMINDER = 'activation-reminder'; + + /** + * @var string + */ + private $plugin_title; + + /** + * @var string + */ + private $plugin_dir; + + /** + * @var string + */ + private $logo_url; + + /** + * @var string + */ + private $buy_plugin_url; + + /** + * @param string $plugin_title + * @param string $plugin_dir + * @param string $logo_url + * @param string $buy_plugin_url + */ + public function __construct( string $plugin_title, string $plugin_dir, string $logo_url, string $buy_plugin_url ) { + $this->plugin_title = $plugin_title; + $this->plugin_dir = $plugin_dir; + $this->logo_url = $logo_url; + $this->buy_plugin_url = $buy_plugin_url; + } + + /** + * @return string + */ + public function get_plugin_title(): string { + return $this->plugin_title; + } + + /** + * @return string + */ + public function get_plugin_dir(): string { + return $this->plugin_dir; + } + + /** + * @return string + */ + public function get_logo_url(): string { + return $this->logo_url; + } + + /** + * @return string + */ + public function get_buy_plugin_url(): string { + return $this->buy_plugin_url; + } + + /** + * + */ + public static function validate_settings( $settings ) { + if ( ! isset( $settings[ self::ACTIVATION_REMINDER ] ) ) { + throw new InvalidSettingValue( self::ACTIVATION_REMINDER, null ); + } + if ( ! is_array( $settings[ self::ACTIVATION_REMINDER ] ) ) { + throw new InvalidSettingValue( self::ACTIVATION_REMINDER, 'should be array' ); + } + $settings_fields = [ + self::PLUGIN_TITLE, + self::PLUGIN_DIR, + self::LOGO_URL, + self::BUY_PLUGIN_URL, + ]; + foreach ( $settings_fields as $field ) { + if ( ! isset( $settings[ self::ACTIVATION_REMINDER ][ $field ] ) ) { + throw new InvalidSettingValue( $field, null ); + } + } + } + + + /** + * @param array $settings + * + * @return self + */ + public static function create_from_composer_settings( array $settings ) { + self::validate_settings( $settings ); + return new self( + $settings[ self::ACTIVATION_REMINDER ][ self::PLUGIN_TITLE ], + $settings[ self::ACTIVATION_REMINDER ][ self::PLUGIN_DIR ], + $settings[ self::ACTIVATION_REMINDER ][ self::LOGO_URL ], + $settings[ self::ACTIVATION_REMINDER ][ self::BUY_PLUGIN_URL ] + ); + } + +} \ No newline at end of file diff --git a/src/Reminder.php b/src/Reminder.php index bf28dd1f9a1226e610c044190b004dadc2681840..8aae35443c549936f56fa1899efe9df9d3583814 100644 --- a/src/Reminder.php +++ b/src/Reminder.php @@ -46,6 +46,6 @@ class Reminder { } -if ( function_exists( 'add_action' ) ) { +if ( defined( 'ABSPATH' ) ) { new Reminder(); }