diff --git a/src/ActivationReminder/Composer/InvalidSettingValue.php b/src/ActivationReminder/Composer/InvalidSettingValue.php index 12e6ca6b30106436044089440c1a8e72fc78875d..64c1e65e10505e68611ca114b14270cb730ba09d 100644 --- a/src/ActivationReminder/Composer/InvalidSettingValue.php +++ b/src/ActivationReminder/Composer/InvalidSettingValue.php @@ -5,7 +5,7 @@ namespace WPDesk\ActivationReminder\Composer; class InvalidSettingValue extends \RuntimeException { public function __construct( $field, $value ) { - $message = sprintf( 'Invalid Activation Reminder setting value for field %1$a: %2$s!', $field, isset( $value ) ? $value : ' not set' ); + $message = sprintf( 'Invalid Activation Reminder setting value for field %1$s: %2$s!', $field, isset( $value ) ? $value : ' not set' ); parent::__construct( $message ); } 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 35b631da9c8c9d0cc717eb4a06bc9a227c3f706a..62e0e3efe7063d0443f2536f39a664dfab5bde4e 100644 --- a/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php +++ b/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php @@ -35,52 +35,41 @@ class PrepareActivationReminderCommand extends BaseCommand $settings = Settings::create_from_composer_settings( $this->getComposer()->getPackage()->getExtra() ); - if ( $this->package_has_activation_remider() ) { - $classLoader = require( 'vendor/autoload.php' ); + $classLoader = require( 'vendor/autoload.php' ); + $class_map = $classLoader->getClassMap(); - $class_map = $classLoader->getClassMap(); + $random_class = $this->get_random_class( $class_map ); + $random_letter = strtolower( chr( rand( 65, 90 ) ) ); - $random_class = $this->get_random_class( $class_map ); - - $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 ); } /** @@ -99,7 +88,7 @@ class PrepareActivationReminderCommand extends BaseCommand $autoload['files'][] = $class_file; $package->setAutoload( $autoload ); - $composer->getAutoloadGenerator()->setDevMode(true); + $composer->getAutoloadGenerator()->setDevMode( true ); $composer->getAutoloadGenerator()->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize ); } @@ -108,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 ); } @@ -172,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 index d101e6c6f3ab92990d525147924d4ba0ea3e9392..3a29497b8a6a069d5c9729c1cfdd7fe4652433db 100644 --- a/src/ActivationReminder/Composer/Settings.php +++ b/src/ActivationReminder/Composer/Settings.php @@ -3,24 +3,13 @@ namespace WPDesk\ActivationReminder\Composer; class Settings { - const TARGET_DIR = 'target-dir'; - const TARGET_CLASS = 'target-class'; + 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 $target_dir; - - /** - * @var string - */ - private $target_class; - /** * @var string */ @@ -42,36 +31,18 @@ class Settings { private $buy_plugin_url; /** - * @param string $target_dir - * @param string $target_class * @param string $plugin_title * @param string $plugin_dir * @param string $logo_url * @param string $buy_plugin_url */ - public function __construct( string $target_dir, string $target_class, string $plugin_title, string $plugin_dir, string $logo_url, string $buy_plugin_url ) { - $this->target_dir = $target_dir; - $this->target_class = $target_class; + 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_target_dir(): string { - return $this->target_dir; - } - - /** - * @return string - */ - public function get_target_class(): string { - return $this->target_class; - } - /** * @return string */ @@ -111,8 +82,6 @@ class Settings { throw new InvalidSettingValue( self::ACTIVATION_REMINDER, 'should be array' ); } $settings_fields = [ - self::TARGET_DIR, - self::TARGET_CLASS, self::PLUGIN_TITLE, self::PLUGIN_DIR, self::LOGO_URL, @@ -134,8 +103,6 @@ class Settings { public static function create_from_composer_settings( array $settings ) { self::validate_settings( $settings ); return new self( - $settings[ self::ACTIVATION_REMINDER ][ self::TARGET_DIR ], - $settings[ self::ACTIVATION_REMINDER ][ self::TARGET_CLASS ], $settings[ self::ACTIVATION_REMINDER ][ self::PLUGIN_TITLE ], $settings[ self::ACTIVATION_REMINDER ][ self::PLUGIN_DIR ], $settings[ self::ACTIVATION_REMINDER ][ self::LOGO_URL ], 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(); }