Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security and script localisation #1

Open
wppoland opened this issue Dec 6, 2024 · 0 comments
Open

Security and script localisation #1

wppoland opened this issue Dec 6, 2024 · 0 comments

Comments

@wppoland
Copy link

wppoland commented Dec 6, 2024

Just as a suggestion for admin-scripts.php:
1/ ensure the file cannot be accessed directly outside WP
2/ wp_localize_script to pass important dynamic data and JS to use the WP AJAX URL and securely verify requests with a nonce.

<?php

namespace TodoApp;

if (!defined('ABSPATH')) {
    exit;
}

add_action('admin_enqueue_scripts', __NAMESPACE__ . '\\scripts');

function scripts() {
    $screen = get_current_screen();

    if ($screen->id !== 'toplevel_page_todoapp') {
        return;
    }

    $admin_deps = require TODOAPP_PLUGIN_DIR_PATH . '/build/index.asset.php';

    wp_enqueue_script(
        'todoapp',
        TODOAPP_PLUGIN_DIR_URL . 'build/index.js',
        $admin_deps['dependencies'],
        $admin_deps['version'],
        true
    );

    wp_localize_script('todoapp', 'todoAppData', [
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce'    => wp_create_nonce('todoapp_nonce'),
    ]);
}

Czuwaj!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant