• Resolved erykko

    (@erykko)


    I am querying the Simple History API using the example Query API – Simple History (simple-history.com) here. However, I am getting a fatal error for the class ‘SimpleHistory’. Is there a workaround to this?

    For reference here is my code

    function register_simple_history_api_endpoint() {
        register_rest_route('simple-history/v1', '/history', array(
            'methods'  => 'GET',
            'callback' => 'get_simple_history_entries',
        ));
    }
    add_action('rest_api_init', 'register_simple_history_api_endpoint');
    
    function get_simple_history_entries() {
        $log_query = new SimpleHistoryLogQuery();
    	$simple_history = SimpleHistory::get_instance();
        $query_results = $log_query->query([
            'posts_per_page' => 99,
        ]);
    
        if (is_wp_error($query_results)) {
            return $query_results; // Return any error that occurred while fetching the entries.
        }
    
        $output = '';
    
        printf(
            '
            <p>Found %1$d rows.</p>
            <p>Viewing page %2$d of %3$d.</p>
            ',
            esc_html($query_results['total_row_count']),
            esc_html($query_results['page_current']),
            esc_html($query_results['pages_count'])
        );
    
        $log_rows = $query_results['log_rows'];
    
        if (!empty($log_rows)) {
            $output .= '<ul>';
    
            foreach ($log_rows as $row) {
                $header_output = $simple_history->getLogRowHeaderOutput($row);
                $text_output = $simple_history->getLogRowPlainTextOutput($row);
                $details_output = $simple_history->getLogRowDetailsOutput($row);
    
                $output .= '<li>';
                $output .= '<hr />';
                $output .= '<p>' . $header_output . '</p>';
                $output .= '<p>' . $text_output . '</p>';
                $output .= '<p>' . $details_output . '</p>';
                $output .= '</li>';
            }
    
            $output .= '</ul>';
        }
    
        return $output;
    }
    • This topic was modified 2 years, 8 months ago by erykko.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Pär Thernström

    (@eskapism)

    Oh sorry, the example code needed an update. I’ve corrected the code now, please try again.

    What you need to change is the first two lines in your get_simple_history_entries() function to this:

    $log_query = new \Simple_History\Log_Query;
    $simple_history = \Simple_History\Simple_History::get_instance();

    Hope that works!

    Thread Starter erykko

    (@erykko)

    Thank you for the fix, any idea why I’m getting the output as

    <p>Found 0 rows.</p>
                <p>Viewing page 1 of 0.</p>
            <ul></ul>

    When the plugin has several entries? Querying directly from the database works well. Thanks

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Uncaught Error: Class “SimpleHistory”’ is closed to new replies.