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

Merge branch 'feature/configuration' into 'feature/popup'

Feature/configuration

See merge request !4
parents 73e70ab9 a6c148aa
No related branches found
2 merge requests!4Feature/configuration,!3feature(popup): popup
Pipeline #11396 passed with warnings
...@@ -11,7 +11,7 @@ Before commit execute: `npm run prod` ! ...@@ -11,7 +11,7 @@ Before commit execute: `npm run prod` !
## Usage ## Usage
```bash ```bash
composer require --dev wpdesk/wp-wpdesk-activation-reminder composer require wpdesk/wp-wpdesk-activation-reminder
composer update composer update
``` ```
......
<?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
...@@ -33,52 +33,43 @@ class PrepareActivationReminderCommand extends BaseCommand ...@@ -33,52 +33,43 @@ class PrepareActivationReminderCommand extends BaseCommand
{ {
$output->writeln("Creating activation reminder."); $output->writeln("Creating activation reminder.");
if ( $this->package_has_activation_remider() ) { $settings = Settings::create_from_composer_settings( $this->getComposer()->getPackage()->getExtra() );
$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_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 = $this->create_or_get_target_file_name( $class_map, $random_class, $random_letter );
$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 );
return isset( $extra['activation-reminder'] ); file_put_contents( $target_file_store, $target_file );
}
return file_get_contents( $target_file_store );
} }
/** /**
...@@ -97,6 +88,7 @@ class PrepareActivationReminderCommand extends BaseCommand ...@@ -97,6 +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()->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize ); $composer->getAutoloadGenerator()->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize );
} }
...@@ -105,30 +97,22 @@ class PrepareActivationReminderCommand extends BaseCommand ...@@ -105,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 );
} }
...@@ -169,25 +153,4 @@ class PrepareActivationReminderCommand extends BaseCommand ...@@ -169,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 );
}
} }
<?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
...@@ -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.
Please register or to comment