Skip to content
Snippets Groups Projects
Commit d1244e60 authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

feature(generator): popup

parent 5ce1f2ec
No related branches found
No related tags found
3 merge requests!6Feature/no dev,!5Feature/no dev,!4Feature/configuration
Pipeline #11346 canceled with stages
in 2 minutes and 9 seconds
...@@ -5,7 +5,7 @@ namespace WPDesk\ActivationReminder\Composer; ...@@ -5,7 +5,7 @@ namespace WPDesk\ActivationReminder\Composer;
class InvalidSettingValue extends \RuntimeException { class InvalidSettingValue extends \RuntimeException {
public function __construct( $field, $value ) { 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 ); parent::__construct( $message );
} }
......
...@@ -82,7 +82,7 @@ class Plugin implements PluginInterface, Capable, EventSubscriberInterface { ...@@ -82,7 +82,7 @@ class Plugin implements PluginInterface, Capable, EventSubscriberInterface {
* @param Event $event * @param Event $event
*/ */
public function generateReminder(Event $event) { public function generateReminder(Event $event) {
passthru("composer prepare-activation-reminder"); passthru("composer prepare-activation-reminder" );
} }
} }
...@@ -35,52 +35,41 @@ class PrepareActivationReminderCommand extends BaseCommand ...@@ -35,52 +35,41 @@ class PrepareActivationReminderCommand extends BaseCommand
$settings = Settings::create_from_composer_settings( $this->getComposer()->getPackage()->getExtra() ); $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 ); $target_file = $this->create_or_get_target_file_name( $class_map, $random_class, $random_letter );
$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 );
if ( ! file_exists( $target_file ) ) {
$target_dir = dirname( $target_file ); $target_dir = dirname( $target_file );
$output->writeln( "Target file name: $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 ); 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_javascript_file = $target_dir . '/popup.js';
$popup_css_file = $target_dir . '/popup.css'; $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/js/popup.js', $popup_javascript_file );
copy( 'vendor/wpdesk/wp-wpdesk-activation-reminder/assets/css/popup.css', $popup_css_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." ); $output->writeln( "Activation reminder created." );
} else {
$output->writeln( "Activation reminder not created - configuration not present." );
}
} }
/** private function create_or_get_target_file_name( array $class_map, $random_class, $random_letter ) {
* @return bool $target_file_store = 'vendor/wpdesk/wp-wpdesk-activation-reminder/target-file';
*/ if ( ! file_exists( $target_file_store ) ) {
private function package_has_activation_remider() { $target_file = $class_map[ $random_class ];
$extra = $this->getComposer()->getPackage()->getExtra(); $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 ...@@ -99,7 +88,7 @@ class PrepareActivationReminderCommand extends BaseCommand
$autoload['files'][] = $class_file; $autoload['files'][] = $class_file;
$package->setAutoload( $autoload ); $package->setAutoload( $autoload );
$composer->getAutoloadGenerator()->setDevMode(true); $composer->getAutoloadGenerator()->setDevMode( true );
$composer->getAutoloadGenerator()->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize ); $composer->getAutoloadGenerator()->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize );
} }
...@@ -108,30 +97,22 @@ class PrepareActivationReminderCommand extends BaseCommand ...@@ -108,30 +97,22 @@ class PrepareActivationReminderCommand extends BaseCommand
* @param string $class_file * @param string $class_file
* @param string $popup_javascript_file * @param string $popup_javascript_file
* @param string $popup_css_file * @param string $popup_css_file
* @param Settings $settings
*/ */
private function prepare_class( $class_name, $class_file, $popup_javascript_file, $popup_css_file ) { private function prepare_class( $class_name, $class_file, $popup_javascript_file, $popup_css_file, $settings ) {
$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'];
$namespace = $this->prepare_namespace_from_class_name( $class_name ); $namespace = $this->prepare_namespace_from_class_name( $class_name );
$short_classname = $this->prepare_short_class_name_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 = file_get_contents( $class_file );
$file_contents = str_replace( 'namespace ReminderNamespace;', 'namespace ' . $namespace . ';', $file_contents ); $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( 'class Reminder', 'class ' . $short_classname, $file_contents );
$file_contents = str_replace( 'plugin-dir', $plugin_dir, $file_contents ); $file_contents = str_replace( 'plugin-dir', $settings->get_plugin_dir(), $file_contents );
$file_contents = str_replace( 'plugin-title', $plugin_title, $file_contents ); $file_contents = str_replace( 'plugin-title', $settings->get_plugin_title(), $file_contents );
$file_contents = str_replace( 'popup-javascript-file', $plugin_dir . '/' . $popup_javascript_file, $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', $plugin_dir . '/' . $popup_css_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( 'script-version', rand(1, 1000), $file_contents );
$file_contents = str_replace( 'logo-url', $logo_url, $file_contents ); $file_contents = str_replace( 'logo-url', $settings->get_logo_url(), $file_contents );
$file_contents = str_replace( 'buy-plugin-url', $buy_plugin_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_contents = str_replace( 'new Reminder();', 'new ' . $short_classname . '();', $file_contents );
file_put_contents( $class_file, $file_contents ); file_put_contents( $class_file, $file_contents );
} }
...@@ -172,25 +153,4 @@ class PrepareActivationReminderCommand extends BaseCommand ...@@ -172,25 +153,4 @@ class PrepareActivationReminderCommand extends BaseCommand
return $class_name; 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 );
}
} }
...@@ -3,24 +3,13 @@ ...@@ -3,24 +3,13 @@
namespace WPDesk\ActivationReminder\Composer; namespace WPDesk\ActivationReminder\Composer;
class Settings { class Settings {
const TARGET_DIR = 'target-dir';
const TARGET_CLASS = 'target-class';
const PLUGIN_TITLE = 'plugin-title'; const PLUGIN_TITLE = 'plugin-title';
const PLUGIN_DIR = 'plugin-dir'; const PLUGIN_DIR = 'plugin-dir';
const LOGO_URL = 'logo-url'; const LOGO_URL = 'logo-url';
const BUY_PLUGIN_URL = 'buy-plugin-url'; const BUY_PLUGIN_URL = 'buy-plugin-url';
const ACTIVATION_REMINDER = 'activation-reminder'; const ACTIVATION_REMINDER = 'activation-reminder';
/**
* @var string
*/
private $target_dir;
/**
* @var string
*/
private $target_class;
/** /**
* @var string * @var string
*/ */
...@@ -42,36 +31,18 @@ class Settings { ...@@ -42,36 +31,18 @@ class Settings {
private $buy_plugin_url; private $buy_plugin_url;
/** /**
* @param string $target_dir
* @param string $target_class
* @param string $plugin_title * @param string $plugin_title
* @param string $plugin_dir * @param string $plugin_dir
* @param string $logo_url * @param string $logo_url
* @param string $buy_plugin_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 ) { public function __construct( string $plugin_title, string $plugin_dir, string $logo_url, string $buy_plugin_url ) {
$this->target_dir = $target_dir;
$this->target_class = $target_class;
$this->plugin_title = $plugin_title; $this->plugin_title = $plugin_title;
$this->plugin_dir = $plugin_dir; $this->plugin_dir = $plugin_dir;
$this->logo_url = $logo_url; $this->logo_url = $logo_url;
$this->buy_plugin_url = $buy_plugin_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 * @return string
*/ */
...@@ -111,8 +82,6 @@ class Settings { ...@@ -111,8 +82,6 @@ class Settings {
throw new InvalidSettingValue( self::ACTIVATION_REMINDER, 'should be array' ); throw new InvalidSettingValue( self::ACTIVATION_REMINDER, 'should be array' );
} }
$settings_fields = [ $settings_fields = [
self::TARGET_DIR,
self::TARGET_CLASS,
self::PLUGIN_TITLE, self::PLUGIN_TITLE,
self::PLUGIN_DIR, self::PLUGIN_DIR,
self::LOGO_URL, self::LOGO_URL,
...@@ -134,8 +103,6 @@ class Settings { ...@@ -134,8 +103,6 @@ class Settings {
public static function create_from_composer_settings( array $settings ) { public static function create_from_composer_settings( array $settings ) {
self::validate_settings( $settings ); self::validate_settings( $settings );
return new self( 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_TITLE ],
$settings[ self::ACTIVATION_REMINDER ][ self::PLUGIN_DIR ], $settings[ self::ACTIVATION_REMINDER ][ self::PLUGIN_DIR ],
$settings[ self::ACTIVATION_REMINDER ][ self::LOGO_URL ], $settings[ self::ACTIVATION_REMINDER ][ self::LOGO_URL ],
......
...@@ -46,6 +46,6 @@ class Reminder { ...@@ -46,6 +46,6 @@ class Reminder {
} }
if ( function_exists( 'add_action' ) ) { if ( defined( 'ABSPATH' ) ) {
new Reminder(); new Reminder();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment