• Resolved Dmitry Kohan

    (@dmay1989)


    Please tell me how, in the example from the plugin documentation, to allow access to logs on the frontend for roles other than administrator. I tried using the simple_history/view_history_capability filter – it doesn’t work.

    Can you tell me what I’m doing wrong? In the page template, I call the simple_history_user_action_output($paged) function and get zero entries, although they actually exist if you give the user the administrator role.

    add_filter( 'simple_history/view_history_capability', function( $capability ) {
    $capability = 'view_fileinput';
    return $capability;
    } );

    function simple_history_user_action_output($paged) {
    $log_query = new \Simple_History\Log_Query;
    $simple_history = \Simple_History\Simple_History::get_instance();

    $current_user = wp_get_current_user();
    $user_id = $current_user -> ID;

    $query_results = $log_query->query( [
    'posts_per_page' => 5,
    'user' => $user_id,
    'loggers' => ['SimplePostLogger', 'SimpleUserLogger', 'SimpleLogger'],
    'paged' => $paged
    ] );

    printf(
    '
    <p>Найдено %1$d строк(и).</p>
    <p>Страница %2$d из %3$d.</p>
    ',
    esc_html( $query_results['total_row_count'] ),
    esc_html( $query_results['page_current'] ),
    esc_html( $query_results['pages_count'] )
    );

    echo "<ul>";

    foreach ( $query_results['log_rows'] as $row ) {
    $header_output = $simple_history->getLogRowHeaderOutput( $row );
    $text_output = $simple_history->getLogRowPlainTextOutput( $row );
    $details_output = $simple_history->getLogRowDetailsOutput( $row );

    echo "<li>";
    echo "<hr />";
    echo "<p>{$header_output}</p>";
    echo "<p>{$text_output}</p>";
    echo "<p>{$details_output}</p>";
    echo "</li>";
    }

    echo "</ul>";
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Pär Thernström

    (@eskapism)

    Hi @dmay1989

    The filter simple_history/view_history_capability only controls access to the Simple History admin pages — it doesn’t change what Log_Query
    returns. Each logger has its own capability check, and the query filters out loggers the current user can’t read. The ones you’re querying
    require edit_pages (SimpleLogger, SimplePostLogger) and edit_users (SimpleUserLogger), so your custom role gets zero rows.

    Use the per-logger filter instead:

    add_filter(
        'simple_history/loggers_user_can_read/can_read_single_logger',
        function ( $user_can_read_logger, $logger_instance, $user_id ) {
            if ( user_can( $user_id, 'view_fileinput' ) ) {
                $allowed = [ 'SimpleLogger', 'SimplePostLogger', 'SimpleUserLogger' ];
                if ( in_array( $logger_instance->get_slug(), $allowed, true ) ) {
                    return true;
                }
            }
            return $user_can_read_logger;
        },
        10,
        3
    );


    Let me know how it goes!

    Thread Starter Dmitry Kohan

    (@dmay1989)

    Hi @eskapism

    Thank you so much for your help, but unfortunately it didn’t work. I even tried to return true; in the filter you specified (

    simple_history/loggers_user_can_read/can_read_single_logger

    ), but still only logs from ‘SimpleLogger’ are output. And the logs from ‘SimplePostLogger’, ‘SimpleUserLogger’ are still not output for some reason…

    Thread Starter Dmitry Kohan

    (@dmay1989)

    Maybe I’m somehow applying this filter incorrectly?

    Plugin Author Pär Thernström

    (@eskapism)

    I don’t have any user with view_fileinput-capability. But here is an example that shows the log to all users on the frontend, perhaps that is a start:

    // Grant read access to the three loggers.
    // Scoped so it only applies inside simple_history_user_action_output() —
    // no other code that calls Log_Query will be affected.
    function simple_history_user_action_grant_access( $user_can_read_logger, $logger_instance, $user_id ) {
        $allowed = [ 'SimpleLogger', 'SimplePostLogger', 'SimpleUserLogger' ];
        if ( in_array( $logger_instance->get_slug(), $allowed, true ) ) {
            return true;
        }
        return $user_can_read_logger;
    }
    
    function simple_history_user_action_output( $paged ) {
        $log_query      = new \Simple_History\Log_Query();
        $simple_history = \Simple_History\Simple_History::get_instance();
    
        add_filter( 'simple_history/loggers_user_can_read/can_read_single_logger', 'simple_history_user_action_grant_access', 10, 3 );
    
        $query_results = $log_query->query(
            [
                'posts_per_page' => 5,
                'loggers'        => [ 'SimplePostLogger', 'SimpleUserLogger', 'SimpleLogger' ],
                'paged'          => $paged,
            ]
        );
    
        remove_filter( 'simple_history/loggers_user_can_read/can_read_single_logger', 'simple_history_user_action_grant_access', 10 );
    
        printf(
            '
                <p>Найдено %1$d строк(и).</p>
                <p>Страница %2$d из %3$d.</p>
            ',
            esc_html( $query_results['total_row_count'] ),
            esc_html( $query_results['page_current'] ),
            esc_html( $query_results['pages_count'] )
        );
    
        echo '<ul>';
    
        foreach ( $query_results['log_rows'] as $row ) {
            $header_output  = $simple_history->getLogRowHeaderOutput( $row );
            $text_output    = $simple_history->getLogRowPlainTextOutput( $row );
            $details_output = $simple_history->getLogRowDetailsOutput( $row );
    
            echo '<li>';
            echo '<hr />';
            echo "<p>{$header_output}</p>";
            echo "<p>{$text_output}</p>";
            echo "<p>{$details_output}</p>";
            echo '</li>';
        }
    
        echo '</ul>';
    }
    
    add_shortcode( 'sh_custom_output_test', function () {
        if ( ! class_exists( '\Simple_History\Log_Query' ) ) {
            return '<p>Simple History is not active.</p>';
        }
    
        $paged = isset( $_GET['paged'] ) ? max( 1, (int) $_GET['paged'] ) : 1;
    
        ob_start();
        simple_history_user_action_output( $paged );
        return ob_get_clean();
    } );
Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.