Skip to content

Commit

Permalink
implement dismiss logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DerHerrFeldmann committed Feb 21, 2025
1 parent 3ff0393 commit ca35b9b
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 63 deletions.
45 changes: 45 additions & 0 deletions packages/wp-plugin/essentials/inc/dashboard/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace ionos_wordpress\essentials\dashboard;

use function ionos_wordpress\essentials\dashboard\blocks\next_best_actions\getNBAData;
use function ionos_wordpress\essentials\dashboard\blocks\next_best_actions\model\getNBAElements;

use const ionos_wordpress\essentials\PLUGIN_DIR;

/*
Expand Down Expand Up @@ -42,6 +45,7 @@
\register_block_type(PLUGIN_DIR . '/build/dashboard/blocks/quick-links');
\register_block_type(PLUGIN_DIR . '/build/dashboard/blocks/vulnerability');
\register_block_type(PLUGIN_DIR . '/build/dashboard/blocks/next-best-actions');

});

// remove our blocks from all other post types
Expand Down Expand Up @@ -166,3 +170,44 @@
EOF
);
});

\add_action('load-' . ADMIN_PAGE_HOOK, function () {

});
add_action('wp_ajax_execute-nba-callback', function() {
$process = isset($_GET['process']) ? sanitize_text_field($_GET['process']) : '';
$id = isset($_GET['id']) ? sanitize_text_field($_GET['id']) : '';

if (empty($process) || empty($id)) {
\wp_send_json_error(new \WP_Error('missing_parameters', 'Missing parameters'));
}

require_once PLUGIN_DIR . '/build/dashboard/blocks/next-best-actions/data.php';
$elements = getNBAData();
if (! is_array($elements)) {
\wp_send_json_error(new \WP_Error('no_actions', 'No actions found'));
}

foreach ($elements as $element) {
if ($element->__get('id') === $id ) {

switch ($process) {
case 'click-nba-dismiss':
$element->__set('dismissed', true);
\wp_send_json_success(['addClass' => 'dismissed']);
break;
case 'click-nba-action':
$callback = $element->__get('completeOnClickCallback');
$result = \call_user_func($callback) === true;
if ($result === true) {
$element->__set('completed', true);
}
\wp_send_json_success( [ 'redirect' => $element->__get('link') ] );
break;
}
}
}

\wp_send_json_error(new \WP_Error('action_not_found', 'Action not found'));
});

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
},
"textdomain": "ionos-essentials",
"editorScript": "file:./index.js",
"viewScript": [
"file:./view.js",
"wp-util"
],
"render": "file:./render.php"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* This class represents the Next Best Action (NBA) model.
*/
namespace ionos_wordpress\essentials\dashboard\blocks\next_best_actions;

$model_path = __DIR__ . '/model.php';
if (! file_exists($model_path)) {
return;
}
require_once $model_path;

function getNBAData() {
return array(
new Model(
id: 'checkPluginsPage',
title: 'Check Plugins Page',
description: 'Check the plugins page for updates and security issues.',
link: admin_url('plugins.php'),
image: 'https://raw.githubusercontent.com/codeformm/mitaka-kichijoji-wapuu/main/mitaka-kichijoji-wapuu.png',
completeOnClickCallback: fn() => false,
callback: fn() => false,
),
new Model(
id: 'checkThemesPage',
title: 'Check Themes Page',
description: 'Check the themes page for updates and security issues.',
link: admin_url('themes.php'),
image: 'https://raw.githubusercontent.com/codeformm/mitaka-kichijoji-wapuu/main/mitaka-kichijoji-wapuu.png',
completeOnClickCallback: fn() => false,
callback: fn() => true,
),
new Model(
id: 'checkUpdatesPage',
title: 'Check Updates Page',
description: 'Check the updates page for updates and security issues.',
link: admin_url('update-core.php'),
image: 'https://raw.githubusercontent.com/codeformm/mitaka-kichijoji-wapuu/main/mitaka-kichijoji-wapuu.png',
completeOnClickCallback: fn() => true,
callback: fn() => true,
),
new Model(
id: 'checkSettingsPage',
title: 'Check Settings Page',
description: 'Check the settings page for updates and security issues.',
link: admin_url('options-general.php'),
image: 'https://raw.githubusercontent.com/codeformm/mitaka-kichijoji-wapuu/main/mitaka-kichijoji-wapuu.png',
completeOnClickCallback: fn() => true,
callback: fn() => true,
),
new Model(
id: 'checkUsersPage',
title: 'Check Users Page',
description: 'Check the users page for updates and security issues.',
link: admin_url('users.php'),
image: 'https://raw.githubusercontent.com/codeformm/mitaka-kichijoji-wapuu/main/mitaka-kichijoji-wapuu.png',
completeOnClickCallback: fn() => true,
callback: fn() => true,
)
);
}

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* This class represents the Next Best Action (NBA) model.
*/

namespace ionos_wordpress\essentials\dashboard\blocks\next_best_actions\model;
namespace ionos_wordpress\essentials\dashboard\blocks\next_best_actions;

/**
* Class NBA
*/
final class NBA
final class Model
{
const WP_OPTION_NAME='NBA_OPTION';

Expand All @@ -18,6 +18,8 @@ function __construct(
readonly string $id,
readonly string $title,
readonly string $description,
readonly string $link,
readonly mixed $completeOnClickCallback,
readonly string $image,
readonly mixed $callback,
readonly bool $completed = false,
Expand All @@ -38,8 +40,10 @@ function __get(string $optionName): mixed {
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
'link' => $this->link,
'completeOnClickCallback' => $this->completeOnClickCallback,
'image' => $this->image,
'callback' => is_callable($this->callback) ? call_user_func($this->callback) : null,
'callback' => $this->callback,
'completed' => static::_getOption($this->id, 'completed'),
'dismissed' => static::_getOption($this->id, 'dismissed'),
default => throw new \InvalidArgumentException("Invalid property: $optionName"),
Expand All @@ -63,6 +67,7 @@ protected static function _setOption(string $id, string $optionName, string $val

protected static function _getOption(string $id, string $optionName): mixed {
$wp_option = static::_getWPOption();
return $wp_option[$id][$optionName] ?? null;
return $wp_option[$id][$optionName] ?? false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,41 @@

use ionos_wordpress\essentials\dashboard\blocks\next_best_actions\model\NBA;

$model_path = __DIR__ . '/model/nba.php';
$nba_data = __DIR__ . '/data.php';

if (! file_exists($model_path)) {
if (! file_exists($nba_data)) {
return;
}
require_once $nba_data;

require_once $model_path;
printf('<h3>%s</h3>', \esc_html__('Next best actions ⚡', 'ionos-essentials'));
$actions = getNBAData();
if (! is_array($actions)) {
return;
}

$actions = array(
new NBA(
id: 'checkPluginsPage',
title: 'Check Plugins Page',
description: 'Check the plugins page for updates and security issues.',
image: 'https://raw.githubusercontent.com/codeformm/mitaka-kichijoji-wapuu/main/mitaka-kichijoji-wapuu.png',
callback: function($action) {
$action->__set('completed', true);
wp_safe_redirect(admin_url('plugins.php'));
exit;
},
),
new NBA(
id: 'checkThemesPage',
title: 'Check Themes Page',
description: 'Check the themes page for updates and security issues.',
image: 'https://raw.githubusercontent.com/codeformm/mitaka-kichijoji-wapuu/main/mitaka-kichijoji-wapuu.png',
callback: function($action) {
$action->__set('completed', true);
wp_safe_redirect(admin_url('themes.php'));
exit;
},
),
);

echo '<ul class="wp-block-list">';
printf('<h3>%s</h3>', \esc_html__('Next best actions ⚡', 'ionos-essentials'));
echo '<ul class="wp-block-list ionos-dashboard-nba ">';
foreach ($actions as $action) {
//$action->__set('dismissed', 0);
if ($action->__get('dismissed') === "1" || $action->__get('completed') === "1") {
continue;
}
printf('<div class="wp-block-button is-style-outline is-style-outline--2">
<a href="#" class="wp-block-button__link wp-element-button" callback-id="click-nba-dismiss" data-id="%s">%s</a>
</div>',
\esc_attr($action->__get('id')),
'Dismiss'
);
printf(
'<li><a href="#" target="_top">%s</a></li>',
\esc_html($action->__get('title')) . ' -> ' . ($action->__get('completed') ? 'Completed' : 'Not completed')
'<a href="%s" class="ionos-dashboard nba-action-link %s" callback-id="click-nba-action" data-id="%s" />%s',
\esc_attr($action->__get('link')),
$action->__get('completed') ? 'completed' : '',
\esc_attr($action->__get('id')),
\esc_html($action->__get('title'))
);
}
echo '</ul>';
echo '</li></ul>';

//debug callback on dashboard
//echo '<pre>';
//$actions[0]->__set('completed', true);
//wp_die();
add_action('wp_ajax_execute-nba-callback', function() {
\wp_send_json_error( new \WP_Error( 'geht_nicht', 'Test' ) );
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
document.querySelectorAll('.ionos-dashboard-nba a').forEach((link) => {
link.addEventListener('click', (event) => {
event.preventDefault();

wp.ajax
.send('execute-nba-callback', {
type: 'GET',
data: {
process: link.getAttribute('callback-id'),
id: link.getAttribute('data-id'),
},
})
.then((response) => {
if (response.redirect !== undefined) {
window.top.location.href = response.redirect;
}
if (response.addClass !== undefined) {
link.classList.add(response.addClass);
}
});
});
});

0 comments on commit ca35b9b

Please sign in to comment.