Skip to content

Commit

Permalink
Basic pages for settings revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
inpsyde-maticluznar committed Sep 30, 2024
1 parent f1b821b commit 8f3fb30
Show file tree
Hide file tree
Showing 15 changed files with 582 additions and 75 deletions.
18 changes: 18 additions & 0 deletions public/images/logo/black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
116 changes: 116 additions & 0 deletions src/Settings/MollieSettingsPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Settings;

use Mollie\WooCommerce\Settings\Page\AbstractPage;
use Mollie\WooCommerce\Settings\Page\PageAdvancedSettings;
use Mollie\WooCommerce\Settings\Page\PageApiKeys;
use Mollie\WooCommerce\Settings\Page\PageNoApiKey;
use Mollie\WooCommerce\Settings\Page\PagePaymentMethods;
use Mollie\WooCommerce\Shared\Data;
use WC_Settings_Page;

class MollieSettingsPage extends WC_Settings_Page
{
protected Settings $settingsHelper;
protected string $pluginPath;
protected string $pluginUrl;
protected array $gateways;
protected array $paymentMethods;
protected bool $isTestModeEnabled;
protected Data $dataHelper;

public function __construct(
Settings $settingsHelper,
string $pluginPath,
string $pluginUrl,
array $gateways,
array $paymentMethods,
bool $isTestModeEnabled,
Data $dataHelper
) {
$this->id = 'mollie_settings';
$this->label = __('Mollie Settings', 'mollie-payments-for-woocommerce');
$this->settingsHelper = $settingsHelper;
$this->pluginPath = $pluginPath;
$this->pluginUrl = $pluginUrl;
$this->gateways = $gateways;
$this->isTestModeEnabled = $isTestModeEnabled;
$this->dataHelper = $dataHelper;
$this->paymentMethods = $paymentMethods;
$this->registerContentFieldType();
$this->outputSections();
parent::__construct();
}

public function registerContentFieldType(): void
{
add_action('woocommerce_admin_field_mollie_custom_input', static function ($value): void {
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php
echo esc_attr($value['id']); ?>"><?php
echo esc_html($value['title']); ?></label>
</th>
<td class="forminp">
<?php
if (!empty($value['value'])): ?>
<?= $value['value']; ?>
<?php
endif; ?>
</td>
</tr>
<?php
});

add_action('woocommerce_admin_field_mollie_content', static function ($value): void {
if (!empty($value['value'])) {
echo $value['value'];
}
});
}

public function outputSections()
{
add_action(
'woocommerce_sections_' . $this->id,
[$this, 'output_sections']
);
}

protected function pages(): array
{
return [
PageNoApiKey::class,
PageApiKeys::class,
PagePaymentMethods::class,
PageAdvancedSettings::class
];
}

public function get_settings($currentSection = '')
{
$mollieSettings = null;
foreach ($this->pages() as $pageClass) {
/** @var AbstractPage $page */
$page = new $pageClass($this->settingsHelper, $this->pluginUrl);
if ($page->slug() === $currentSection) {
$mollieSettings = $page->settings();
break;
}
}

if (!$mollieSettings) {
$mollieSettings = (new PageNoApiKey($this->settingsHelper, $this->pluginUrl))->settings();
}

return apply_filters(
'woocommerce_get_settings_' . $this->id,
$mollieSettings,
$currentSection
);
}
}
55 changes: 55 additions & 0 deletions src/Settings/Page/AbstractPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Settings\Page;

use Mollie\WooCommerce\Settings\Page\Section\AbstractSection;
use Mollie\WooCommerce\Settings\Settings;

abstract class AbstractPage
{
protected Settings $settings;
protected string $pluginUrl;

public function __construct(Settings $settings, string $pluginUrl)
{
$this->settings = $settings;
$this->pluginUrl = $pluginUrl;
}

abstract public function isTab(): bool;

abstract public function slug(): string;

public function tabName(): string
{
return '';
}

protected function sections(): array
{
return [];
}

public function settings(): array
{
$settings = [];
$styles = [];

foreach ($this->sections() as $sectionClass) {
/** @var AbstractSection $section */
$section = new $sectionClass($this->settings, $this->pluginUrl);
foreach ($section->config() as $field) {
$settings[] = $field;
}
$styles[$sectionClass] = preg_replace('/\s+/', '', $section->styles());
}
array_unshift($settings, [
'id' => $this->settings->getSettingId('styles'),
'type' => 'mollie_content',
'value' => implode($styles)
]);
return $settings;
}
}
68 changes: 0 additions & 68 deletions src/Settings/Page/Components.php

This file was deleted.

File renamed without changes.
18 changes: 18 additions & 0 deletions src/Settings/Page/PageAdvancedSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Settings\Page;

class PageAdvancedSettings extends AbstractPage {

public function isTab(): bool
{
// TODO: Implement isTab() method.
}

public function slug(): string
{
return 'mollie_advanced';
}
}
18 changes: 18 additions & 0 deletions src/Settings/Page/PageApiKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Settings\Page;

class PageApiKeys extends AbstractPage {

public function isTab(): bool
{
// TODO: Implement isTab() method.
}

public function slug(): string
{
return 'mollie_api_keys';
}
}
32 changes: 32 additions & 0 deletions src/Settings/Page/PageNoApiKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Settings\Page;

use Mollie\WooCommerce\Settings\Page\Section\ConnectionFields;
use Mollie\WooCommerce\Settings\Page\Section\Header;
use Mollie\WooCommerce\Settings\Page\Section\Instructions;
use Mollie\WooCommerce\Settings\Page\Section\Notices;

class PageNoApiKey extends AbstractPage {
public function isTab(): bool
{
// TODO: Implement isTab() method.
}

public function slug(): string
{
return 'mollie_no_api_key';
}

public function sections(): array
{
return [
Header::class,
Notices::class,
Instructions::class,
ConnectionFields::class
];
}
}
18 changes: 18 additions & 0 deletions src/Settings/Page/PagePaymentMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Settings\Page;

class PagePaymentMethods extends AbstractPage {

public function isTab(): bool
{
// TODO: Implement isTab() method.
}

public function slug(): string
{
return 'mollie_payment_methods';
}
}
30 changes: 30 additions & 0 deletions src/Settings/Page/Section/AbstractSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Settings\Page\Section;

use Mollie\WooCommerce\Settings\Settings;

abstract class AbstractSection
{
protected Settings $settings;
protected string $pluginUrl;

public function __construct(Settings $settings, string $pluginUrl)
{
$this->settings = $settings;
$this->pluginUrl = $pluginUrl;
}

abstract public function config(): array;

public function styles(): string{
return '';
}

public function images(): string
{
return $this->pluginUrl . '/public/images/';
}
}
Loading

0 comments on commit 8f3fb30

Please sign in to comment.