diff --git a/packages/wp-plugin/essentials/inc/dashboard/index.php b/packages/wp-plugin/essentials/inc/dashboard/index.php index 6b13d53e..84a57c2f 100644 --- a/packages/wp-plugin/essentials/inc/dashboard/index.php +++ b/packages/wp-plugin/essentials/inc/dashboard/index.php @@ -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; /* @@ -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 @@ -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')); +}); + diff --git a/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/block.json b/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/block.json index 13beeef0..549ebcf3 100644 --- a/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/block.json +++ b/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/block.json @@ -13,5 +13,9 @@ }, "textdomain": "ionos-essentials", "editorScript": "file:./index.js", + "viewScript": [ + "file:./view.js", + "wp-util" + ], "render": "file:./render.php" } diff --git a/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/data.php b/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/data.php new file mode 100644 index 00000000..2798d5b3 --- /dev/null +++ b/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/data.php @@ -0,0 +1,62 @@ + 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, + ) + ); +} + diff --git a/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/edit.js b/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/edit.js deleted file mode 100644 index 712e160f..00000000 --- a/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/edit.js +++ /dev/null @@ -1,14 +0,0 @@ -import { __ } from '@wordpress/i18n'; -import { useBlockProps } from '@wordpress/block-editor'; -import ServerSideRender from '@wordpress/server-side-render'; - -export default function Edit(props) { - return ( -
'; -//$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' ) ); +}); diff --git a/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/view.js b/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/view.js new file mode 100644 index 00000000..a1d27470 --- /dev/null +++ b/packages/wp-plugin/essentials/src/dashboard/blocks/next-best-actions/view.js @@ -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); + } + }); + }); +});